1 /******************************************************************************
2 ** This file is an amalgamation of many separate C source files from SQLite
3 ** version 3.8.3.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 sqlite3.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 ** This header file defines the interface that the SQLite library
41 ** presents to client programs.  If a C-function, structure, datatype,
42 ** or constant definition does not appear in this file, then it is
43 ** not a published API of SQLite, is subject to change without
44 ** notice, and should not be referenced by programs that use SQLite.
45 **
46 ** Some of the definitions that are in this file are marked as
47 ** "experimental".  Experimental interfaces are normally new
48 ** features recently added to SQLite.  We do not anticipate changes
49 ** to experimental interfaces but reserve the right to make minor changes
50 ** if experience from use "in the wild" suggest such changes are prudent.
51 **
52 ** The official C-language API documentation for SQLite is derived
53 ** from comments in this file.  This file is the authoritative source
54 ** on how SQLite interfaces are suppose to operate.
55 **
56 ** The name of this file under configuration management is "sqlite.h.in".
57 ** The makefile makes some minor changes to this file (such as inserting
58 ** the version number) and changes its name to "sqlite3.h" as
59 ** part of the build process.
60 */
61 #ifndef _SQLITE3_H_
62 #define _SQLITE3_H_
63 #include <stdarg.h>     /* Needed for the definition of va_list */
64 
65 /*
66 ** Make sure we can call this stuff from C++.
67 */
68 #if 0
69 extern "C" {
70 #endif
71 
72 
73 /*
74 ** Add the ability to override 'extern'
75 */
76 #ifndef SQLITE_EXTERN
77 # define SQLITE_EXTERN extern
78 #endif
79 
80 #ifndef SQLITE_API
81 # define SQLITE_API
82 #endif
83 
84 
85 /*
86 ** These no-op macros are used in front of interfaces to mark those
87 ** interfaces as either deprecated or experimental.  New applications
88 ** should not use deprecated interfaces - they are support for backwards
89 ** compatibility only.  Application writers should be aware that
90 ** experimental interfaces are subject to change in point releases.
91 **
92 ** These macros used to resolve to various kinds of compiler magic that
93 ** would generate warning messages when they were used.  But that
94 ** compiler magic ended up generating such a flurry of bug reports
95 ** that we have taken it all out and gone back to using simple
96 ** noop macros.
97 */
98 #define SQLITE_DEPRECATED
99 #define SQLITE_EXPERIMENTAL
100 
101 /*
102 ** Ensure these symbols were not defined by some previous header file.
103 */
104 #ifdef SQLITE_VERSION
105 # undef SQLITE_VERSION
106 #endif
107 #ifdef SQLITE_VERSION_NUMBER
108 # undef SQLITE_VERSION_NUMBER
109 #endif
110 
111 /*
112 ** CAPI3REF: Compile-Time Library Version Numbers
113 **
114 ** ^(The [SQLITE_VERSION] C preprocessor macro in the sqlite3.h header
115 ** evaluates to a string literal that is the SQLite version in the
116 ** format "X.Y.Z" where X is the major version number (always 3 for
117 ** SQLite3) and Y is the minor version number and Z is the release number.)^
118 ** ^(The [SQLITE_VERSION_NUMBER] C preprocessor macro resolves to an integer
119 ** with the value (X*1000000 + Y*1000 + Z) where X, Y, and Z are the same
120 ** numbers used in [SQLITE_VERSION].)^
121 ** The SQLITE_VERSION_NUMBER for any given release of SQLite will also
122 ** be larger than the release from which it is derived.  Either Y will
123 ** be held constant and Z will be incremented or else Y will be incremented
124 ** and Z will be reset to zero.
125 **
126 ** Since version 3.6.18, SQLite source code has been stored in the
127 ** <a href="http://www.fossil-scm.org/">Fossil configuration management
128 ** system</a>.  ^The SQLITE_SOURCE_ID macro evaluates to
129 ** a string which identifies a particular check-in of SQLite
130 ** within its configuration management system.  ^The SQLITE_SOURCE_ID
131 ** string contains the date and time of the check-in (UTC) and an SHA1
132 ** hash of the entire source tree.
133 **
134 ** See also: [sqlite3_libversion()],
135 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
136 ** [sqlite_version()] and [sqlite_source_id()].
137 */
138 #define SQLITE_VERSION        "3.8.3.1"
139 #define SQLITE_VERSION_NUMBER 3008003
140 #define SQLITE_SOURCE_ID      "2014-02-11 14:52:19 ea3317a4803d71d88183b29f1d3086f46d68a00e"
141 
142 /*
143 ** CAPI3REF: Run-Time Library Version Numbers
144 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
145 **
146 ** These interfaces provide the same information as the [SQLITE_VERSION],
147 ** [SQLITE_VERSION_NUMBER], and [SQLITE_SOURCE_ID] C preprocessor macros
148 ** but are associated with the library instead of the header file.  ^(Cautious
149 ** programmers might include assert() statements in their application to
150 ** verify that values returned by these interfaces match the macros in
151 ** the header, and thus insure that the application is
152 ** compiled with matching library and header files.
153 **
154 ** <blockquote><pre>
155 ** assert( sqlite3_libversion_number()==SQLITE_VERSION_NUMBER );
156 ** assert( strcmp(sqlite3_sourceid(),SQLITE_SOURCE_ID)==0 );
157 ** assert( strcmp(sqlite3_libversion(),SQLITE_VERSION)==0 );
158 ** </pre></blockquote>)^
159 **
160 ** ^The sqlite3_version[] string constant contains the text of [SQLITE_VERSION]
161 ** macro.  ^The sqlite3_libversion() function returns a pointer to the
162 ** to the sqlite3_version[] string constant.  The sqlite3_libversion()
163 ** function is provided for use in DLLs since DLL users usually do not have
164 ** direct access to string constants within the DLL.  ^The
165 ** sqlite3_libversion_number() function returns an integer equal to
166 ** [SQLITE_VERSION_NUMBER].  ^The sqlite3_sourceid() function returns
167 ** a pointer to a string constant whose value is the same as the
168 ** [SQLITE_SOURCE_ID] C preprocessor macro.
169 **
170 ** See also: [sqlite_version()] and [sqlite_source_id()].
171 */
172 SQLITE_API const char sqlite3_version[] = SQLITE_VERSION;
173 SQLITE_API const char *sqlite3_libversion(void);
174 SQLITE_API const char *sqlite3_sourceid(void);
175 SQLITE_API int sqlite3_libversion_number(void);
176 
177 /*
178 ** CAPI3REF: Run-Time Library Compilation Options Diagnostics
179 **
180 ** ^The sqlite3_compileoption_used() function returns 0 or 1
181 ** indicating whether the specified option was defined at
182 ** compile time.  ^The SQLITE_ prefix may be omitted from the
183 ** option name passed to sqlite3_compileoption_used().
184 **
185 ** ^The sqlite3_compileoption_get() function allows iterating
186 ** over the list of options that were defined at compile time by
187 ** returning the N-th compile time option string.  ^If N is out of range,
188 ** sqlite3_compileoption_get() returns a NULL pointer.  ^The SQLITE_
189 ** prefix is omitted from any strings returned by
190 ** sqlite3_compileoption_get().
191 **
192 ** ^Support for the diagnostic functions sqlite3_compileoption_used()
193 ** and sqlite3_compileoption_get() may be omitted by specifying the
194 ** [SQLITE_OMIT_COMPILEOPTION_DIAGS] option at compile time.
195 **
196 ** See also: SQL functions [sqlite_compileoption_used()] and
197 ** [sqlite_compileoption_get()] and the [compile_options pragma].
198 */
199 #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
200 SQLITE_API int sqlite3_compileoption_used(const char *zOptName);
201 SQLITE_API const char *sqlite3_compileoption_get(int N);
202 #endif
203 
204 /*
205 ** CAPI3REF: Test To See If The Library Is Threadsafe
206 **
207 ** ^The sqlite3_threadsafe() function returns zero if and only if
208 ** SQLite was compiled with mutexing code omitted due to the
209 ** [SQLITE_THREADSAFE] compile-time option being set to 0.
210 **
211 ** SQLite can be compiled with or without mutexes.  When
212 ** the [SQLITE_THREADSAFE] C preprocessor macro is 1 or 2, mutexes
213 ** are enabled and SQLite is threadsafe.  When the
214 ** [SQLITE_THREADSAFE] macro is 0,
215 ** the mutexes are omitted.  Without the mutexes, it is not safe
216 ** to use SQLite concurrently from more than one thread.
217 **
218 ** Enabling mutexes incurs a measurable performance penalty.
219 ** So if speed is of utmost importance, it makes sense to disable
220 ** the mutexes.  But for maximum safety, mutexes should be enabled.
221 ** ^The default behavior is for mutexes to be enabled.
222 **
223 ** This interface can be used by an application to make sure that the
224 ** version of SQLite that it is linking against was compiled with
225 ** the desired setting of the [SQLITE_THREADSAFE] macro.
226 **
227 ** This interface only reports on the compile-time mutex setting
228 ** of the [SQLITE_THREADSAFE] flag.  If SQLite is compiled with
229 ** SQLITE_THREADSAFE=1 or =2 then mutexes are enabled by default but
230 ** can be fully or partially disabled using a call to [sqlite3_config()]
231 ** with the verbs [SQLITE_CONFIG_SINGLETHREAD], [SQLITE_CONFIG_MULTITHREAD],
232 ** or [SQLITE_CONFIG_MUTEX].  ^(The return value of the
233 ** sqlite3_threadsafe() function shows only the compile-time setting of
234 ** thread safety, not any run-time changes to that setting made by
235 ** sqlite3_config(). In other words, the return value from sqlite3_threadsafe()
236 ** is unchanged by calls to sqlite3_config().)^
237 **
238 ** See the [threading mode] documentation for additional information.
239 */
240 SQLITE_API int sqlite3_threadsafe(void);
241 
242 /*
243 ** CAPI3REF: Database Connection Handle
244 ** KEYWORDS: {database connection} {database connections}
245 **
246 ** Each open SQLite database is represented by a pointer to an instance of
247 ** the opaque structure named "sqlite3".  It is useful to think of an sqlite3
248 ** pointer as an object.  The [sqlite3_open()], [sqlite3_open16()], and
249 ** [sqlite3_open_v2()] interfaces are its constructors, and [sqlite3_close()]
250 ** and [sqlite3_close_v2()] are its destructors.  There are many other
251 ** interfaces (such as
252 ** [sqlite3_prepare_v2()], [sqlite3_create_function()], and
253 ** [sqlite3_busy_timeout()] to name but three) that are methods on an
254 ** sqlite3 object.
255 */
256 typedef struct sqlite3 sqlite3;
257 
258 /*
259 ** CAPI3REF: 64-Bit Integer Types
260 ** KEYWORDS: sqlite_int64 sqlite_uint64
261 **
262 ** Because there is no cross-platform way to specify 64-bit integer types
263 ** SQLite includes typedefs for 64-bit signed and unsigned integers.
264 **
265 ** The sqlite3_int64 and sqlite3_uint64 are the preferred type definitions.
266 ** The sqlite_int64 and sqlite_uint64 types are supported for backwards
267 ** compatibility only.
268 **
269 ** ^The sqlite3_int64 and sqlite_int64 types can store integer values
270 ** between -9223372036854775808 and +9223372036854775807 inclusive.  ^The
271 ** sqlite3_uint64 and sqlite_uint64 types can store integer values
272 ** between 0 and +18446744073709551615 inclusive.
273 */
274 #ifdef SQLITE_INT64_TYPE
275   typedef SQLITE_INT64_TYPE sqlite_int64;
276   typedef unsigned SQLITE_INT64_TYPE sqlite_uint64;
277 #elif defined(_MSC_VER) || defined(__BORLANDC__)
278   typedef __int64 sqlite_int64;
279   typedef unsigned __int64 sqlite_uint64;
280 #else
281   typedef long long int sqlite_int64;
282   typedef unsigned long long int sqlite_uint64;
283 #endif
284 typedef sqlite_int64 sqlite3_int64;
285 typedef sqlite_uint64 sqlite3_uint64;
286 
287 /*
288 ** If compiling for a processor that lacks floating point support,
289 ** substitute integer for floating-point.
290 */
291 #ifdef SQLITE_OMIT_FLOATING_POINT
292 # define double sqlite3_int64
293 #endif
294 
295 /*
296 ** CAPI3REF: Closing A Database Connection
297 **
298 ** ^The sqlite3_close() and sqlite3_close_v2() routines are destructors
299 ** for the [sqlite3] object.
300 ** ^Calls to sqlite3_close() and sqlite3_close_v2() return SQLITE_OK if
301 ** the [sqlite3] object is successfully destroyed and all associated
302 ** resources are deallocated.
303 **
304 ** ^If the database connection is associated with unfinalized prepared
305 ** statements or unfinished sqlite3_backup objects then sqlite3_close()
306 ** will leave the database connection open and return [SQLITE_BUSY].
307 ** ^If sqlite3_close_v2() is called with unfinalized prepared statements
308 ** and unfinished sqlite3_backups, then the database connection becomes
309 ** an unusable "zombie" which will automatically be deallocated when the
310 ** last prepared statement is finalized or the last sqlite3_backup is
311 ** finished.  The sqlite3_close_v2() interface is intended for use with
312 ** host languages that are garbage collected, and where the order in which
313 ** destructors are called is arbitrary.
314 **
315 ** Applications should [sqlite3_finalize | finalize] all [prepared statements],
316 ** [sqlite3_blob_close | close] all [BLOB handles], and
317 ** [sqlite3_backup_finish | finish] all [sqlite3_backup] objects associated
318 ** with the [sqlite3] object prior to attempting to close the object.  ^If
319 ** sqlite3_close_v2() is called on a [database connection] that still has
320 ** outstanding [prepared statements], [BLOB handles], and/or
321 ** [sqlite3_backup] objects then it returns SQLITE_OK but the deallocation
322 ** of resources is deferred until all [prepared statements], [BLOB handles],
323 ** and [sqlite3_backup] objects are also destroyed.
324 **
325 ** ^If an [sqlite3] object is destroyed while a transaction is open,
326 ** the transaction is automatically rolled back.
327 **
328 ** The C parameter to [sqlite3_close(C)] and [sqlite3_close_v2(C)]
329 ** must be either a NULL
330 ** pointer or an [sqlite3] object pointer obtained
331 ** from [sqlite3_open()], [sqlite3_open16()], or
332 ** [sqlite3_open_v2()], and not previously closed.
333 ** ^Calling sqlite3_close() or sqlite3_close_v2() with a NULL pointer
334 ** argument is a harmless no-op.
335 */
336 SQLITE_API int sqlite3_close(sqlite3*);
337 SQLITE_API int sqlite3_close_v2(sqlite3*);
338 
339 /*
340 ** The type for a callback function.
341 ** This is legacy and deprecated.  It is included for historical
342 ** compatibility and is not documented.
343 */
344 typedef int (*sqlite3_callback)(void*,int,char**, char**);
345 
346 /*
347 ** CAPI3REF: One-Step Query Execution Interface
348 **
349 ** The sqlite3_exec() interface is a convenience wrapper around
350 ** [sqlite3_prepare_v2()], [sqlite3_step()], and [sqlite3_finalize()],
351 ** that allows an application to run multiple statements of SQL
352 ** without having to use a lot of C code.
353 **
354 ** ^The sqlite3_exec() interface runs zero or more UTF-8 encoded,
355 ** semicolon-separate SQL statements passed into its 2nd argument,
356 ** in the context of the [database connection] passed in as its 1st
357 ** argument.  ^If the callback function of the 3rd argument to
358 ** sqlite3_exec() is not NULL, then it is invoked for each result row
359 ** coming out of the evaluated SQL statements.  ^The 4th argument to
360 ** sqlite3_exec() is relayed through to the 1st argument of each
361 ** callback invocation.  ^If the callback pointer to sqlite3_exec()
362 ** is NULL, then no callback is ever invoked and result rows are
363 ** ignored.
364 **
365 ** ^If an error occurs while evaluating the SQL statements passed into
366 ** sqlite3_exec(), then execution of the current statement stops and
367 ** subsequent statements are skipped.  ^If the 5th parameter to sqlite3_exec()
368 ** is not NULL then any error message is written into memory obtained
369 ** from [sqlite3_malloc()] and passed back through the 5th parameter.
370 ** To avoid memory leaks, the application should invoke [sqlite3_free()]
371 ** on error message strings returned through the 5th parameter of
372 ** of sqlite3_exec() after the error message string is no longer needed.
373 ** ^If the 5th parameter to sqlite3_exec() is not NULL and no errors
374 ** occur, then sqlite3_exec() sets the pointer in its 5th parameter to
375 ** NULL before returning.
376 **
377 ** ^If an sqlite3_exec() callback returns non-zero, the sqlite3_exec()
378 ** routine returns SQLITE_ABORT without invoking the callback again and
379 ** without running any subsequent SQL statements.
380 **
381 ** ^The 2nd argument to the sqlite3_exec() callback function is the
382 ** number of columns in the result.  ^The 3rd argument to the sqlite3_exec()
383 ** callback is an array of pointers to strings obtained as if from
384 ** [sqlite3_column_text()], one for each column.  ^If an element of a
385 ** result row is NULL then the corresponding string pointer for the
386 ** sqlite3_exec() callback is a NULL pointer.  ^The 4th argument to the
387 ** sqlite3_exec() callback is an array of pointers to strings where each
388 ** entry represents the name of corresponding result column as obtained
389 ** from [sqlite3_column_name()].
390 **
391 ** ^If the 2nd parameter to sqlite3_exec() is a NULL pointer, a pointer
392 ** to an empty string, or a pointer that contains only whitespace and/or
393 ** SQL comments, then no SQL statements are evaluated and the database
394 ** is not changed.
395 **
396 ** Restrictions:
397 **
398 ** <ul>
399 ** <li> The application must insure that the 1st parameter to sqlite3_exec()
400 **      is a valid and open [database connection].
401 ** <li> The application must not close the [database connection] specified by
402 **      the 1st parameter to sqlite3_exec() while sqlite3_exec() is running.
403 ** <li> The application must not modify the SQL statement text passed into
404 **      the 2nd parameter of sqlite3_exec() while sqlite3_exec() is running.
405 ** </ul>
406 */
407 SQLITE_API int sqlite3_exec(
408   sqlite3*,                                  /* An open database */
409   const char *sql,                           /* SQL to be evaluated */
410   int (*callback)(void*,int,char**,char**),  /* Callback function */
411   void *,                                    /* 1st argument to callback */
412   char **errmsg                              /* Error msg written here */
413 );
414 
415 /*
416 ** CAPI3REF: Result Codes
417 ** KEYWORDS: SQLITE_OK {error code} {error codes}
418 ** KEYWORDS: {result code} {result codes}
419 **
420 ** Many SQLite functions return an integer result code from the set shown
421 ** here in order to indicate success or failure.
422 **
423 ** New error codes may be added in future versions of SQLite.
424 **
425 ** See also: [SQLITE_IOERR_READ | extended result codes],
426 ** [sqlite3_vtab_on_conflict()] [SQLITE_ROLLBACK | result codes].
427 */
428 #define SQLITE_OK           0   /* Successful result */
429 /* beginning-of-error-codes */
430 #define SQLITE_ERROR        1   /* SQL error or missing database */
431 #define SQLITE_INTERNAL     2   /* Internal logic error in SQLite */
432 #define SQLITE_PERM         3   /* Access permission denied */
433 #define SQLITE_ABORT        4   /* Callback routine requested an abort */
434 #define SQLITE_BUSY         5   /* The database file is locked */
435 #define SQLITE_LOCKED       6   /* A table in the database is locked */
436 #define SQLITE_NOMEM        7   /* A malloc() failed */
437 #define SQLITE_READONLY     8   /* Attempt to write a readonly database */
438 #define SQLITE_INTERRUPT    9   /* Operation terminated by sqlite3_interrupt()*/
439 #define SQLITE_IOERR       10   /* Some kind of disk I/O error occurred */
440 #define SQLITE_CORRUPT     11   /* The database disk image is malformed */
441 #define SQLITE_NOTFOUND    12   /* Unknown opcode in sqlite3_file_control() */
442 #define SQLITE_FULL        13   /* Insertion failed because database is full */
443 #define SQLITE_CANTOPEN    14   /* Unable to open the database file */
444 #define SQLITE_PROTOCOL    15   /* Database lock protocol error */
445 #define SQLITE_EMPTY       16   /* Database is empty */
446 #define SQLITE_SCHEMA      17   /* The database schema changed */
447 #define SQLITE_TOOBIG      18   /* String or BLOB exceeds size limit */
448 #define SQLITE_CONSTRAINT  19   /* Abort due to constraint violation */
449 #define SQLITE_MISMATCH    20   /* Data type mismatch */
450 #define SQLITE_MISUSE      21   /* Library used incorrectly */
451 #define SQLITE_NOLFS       22   /* Uses OS features not supported on host */
452 #define SQLITE_AUTH        23   /* Authorization denied */
453 #define SQLITE_FORMAT      24   /* Auxiliary database format error */
454 #define SQLITE_RANGE       25   /* 2nd parameter to sqlite3_bind out of range */
455 #define SQLITE_NOTADB      26   /* File opened that is not a database file */
456 #define SQLITE_NOTICE      27   /* Notifications from sqlite3_log() */
457 #define SQLITE_WARNING     28   /* Warnings from sqlite3_log() */
458 #define SQLITE_ROW         100  /* sqlite3_step() has another row ready */
459 #define SQLITE_DONE        101  /* sqlite3_step() has finished executing */
460 /* end-of-error-codes */
461 
462 /*
463 ** CAPI3REF: Extended Result Codes
464 ** KEYWORDS: {extended error code} {extended error codes}
465 ** KEYWORDS: {extended result code} {extended result codes}
466 **
467 ** In its default configuration, SQLite API routines return one of 26 integer
468 ** [SQLITE_OK | result codes].  However, experience has shown that many of
469 ** these result codes are too coarse-grained.  They do not provide as
470 ** much information about problems as programmers might like.  In an effort to
471 ** address this, newer versions of SQLite (version 3.3.8 and later) include
472 ** support for additional result codes that provide more detailed information
473 ** about errors. The extended result codes are enabled or disabled
474 ** on a per database connection basis using the
475 ** [sqlite3_extended_result_codes()] API.
476 **
477 ** Some of the available extended result codes are listed here.
478 ** One may expect the number of extended result codes will increase
479 ** over time.  Software that uses extended result codes should expect
480 ** to see new result codes in future releases of SQLite.
481 **
482 ** The SQLITE_OK result code will never be extended.  It will always
483 ** be exactly zero.
484 */
485 #define SQLITE_IOERR_READ              (SQLITE_IOERR | (1<<8))
486 #define SQLITE_IOERR_SHORT_READ        (SQLITE_IOERR | (2<<8))
487 #define SQLITE_IOERR_WRITE             (SQLITE_IOERR | (3<<8))
488 #define SQLITE_IOERR_FSYNC             (SQLITE_IOERR | (4<<8))
489 #define SQLITE_IOERR_DIR_FSYNC         (SQLITE_IOERR | (5<<8))
490 #define SQLITE_IOERR_TRUNCATE          (SQLITE_IOERR | (6<<8))
491 #define SQLITE_IOERR_FSTAT             (SQLITE_IOERR | (7<<8))
492 #define SQLITE_IOERR_UNLOCK            (SQLITE_IOERR | (8<<8))
493 #define SQLITE_IOERR_RDLOCK            (SQLITE_IOERR | (9<<8))
494 #define SQLITE_IOERR_DELETE            (SQLITE_IOERR | (10<<8))
495 #define SQLITE_IOERR_BLOCKED           (SQLITE_IOERR | (11<<8))
496 #define SQLITE_IOERR_NOMEM             (SQLITE_IOERR | (12<<8))
497 #define SQLITE_IOERR_ACCESS            (SQLITE_IOERR | (13<<8))
498 #define SQLITE_IOERR_CHECKRESERVEDLOCK (SQLITE_IOERR | (14<<8))
499 #define SQLITE_IOERR_LOCK              (SQLITE_IOERR | (15<<8))
500 #define SQLITE_IOERR_CLOSE             (SQLITE_IOERR | (16<<8))
501 #define SQLITE_IOERR_DIR_CLOSE         (SQLITE_IOERR | (17<<8))
502 #define SQLITE_IOERR_SHMOPEN           (SQLITE_IOERR | (18<<8))
503 #define SQLITE_IOERR_SHMSIZE           (SQLITE_IOERR | (19<<8))
504 #define SQLITE_IOERR_SHMLOCK           (SQLITE_IOERR | (20<<8))
505 #define SQLITE_IOERR_SHMMAP            (SQLITE_IOERR | (21<<8))
506 #define SQLITE_IOERR_SEEK              (SQLITE_IOERR | (22<<8))
507 #define SQLITE_IOERR_DELETE_NOENT      (SQLITE_IOERR | (23<<8))
508 #define SQLITE_IOERR_MMAP              (SQLITE_IOERR | (24<<8))
509 #define SQLITE_IOERR_GETTEMPPATH       (SQLITE_IOERR | (25<<8))
510 #define SQLITE_IOERR_CONVPATH          (SQLITE_IOERR | (26<<8))
511 #define SQLITE_LOCKED_SHAREDCACHE      (SQLITE_LOCKED |  (1<<8))
512 #define SQLITE_BUSY_RECOVERY           (SQLITE_BUSY   |  (1<<8))
513 #define SQLITE_BUSY_SNAPSHOT           (SQLITE_BUSY   |  (2<<8))
514 #define SQLITE_CANTOPEN_NOTEMPDIR      (SQLITE_CANTOPEN | (1<<8))
515 #define SQLITE_CANTOPEN_ISDIR          (SQLITE_CANTOPEN | (2<<8))
516 #define SQLITE_CANTOPEN_FULLPATH       (SQLITE_CANTOPEN | (3<<8))
517 #define SQLITE_CANTOPEN_CONVPATH       (SQLITE_CANTOPEN | (4<<8))
518 #define SQLITE_CORRUPT_VTAB            (SQLITE_CORRUPT | (1<<8))
519 #define SQLITE_READONLY_RECOVERY       (SQLITE_READONLY | (1<<8))
520 #define SQLITE_READONLY_CANTLOCK       (SQLITE_READONLY | (2<<8))
521 #define SQLITE_READONLY_ROLLBACK       (SQLITE_READONLY | (3<<8))
522 #define SQLITE_READONLY_DBMOVED        (SQLITE_READONLY | (4<<8))
523 #define SQLITE_ABORT_ROLLBACK          (SQLITE_ABORT | (2<<8))
524 #define SQLITE_CONSTRAINT_CHECK        (SQLITE_CONSTRAINT | (1<<8))
525 #define SQLITE_CONSTRAINT_COMMITHOOK   (SQLITE_CONSTRAINT | (2<<8))
526 #define SQLITE_CONSTRAINT_FOREIGNKEY   (SQLITE_CONSTRAINT | (3<<8))
527 #define SQLITE_CONSTRAINT_FUNCTION     (SQLITE_CONSTRAINT | (4<<8))
528 #define SQLITE_CONSTRAINT_NOTNULL      (SQLITE_CONSTRAINT | (5<<8))
529 #define SQLITE_CONSTRAINT_PRIMARYKEY   (SQLITE_CONSTRAINT | (6<<8))
530 #define SQLITE_CONSTRAINT_TRIGGER      (SQLITE_CONSTRAINT | (7<<8))
531 #define SQLITE_CONSTRAINT_UNIQUE       (SQLITE_CONSTRAINT | (8<<8))
532 #define SQLITE_CONSTRAINT_VTAB         (SQLITE_CONSTRAINT | (9<<8))
533 #define SQLITE_CONSTRAINT_ROWID        (SQLITE_CONSTRAINT |(10<<8))
534 #define SQLITE_NOTICE_RECOVER_WAL      (SQLITE_NOTICE | (1<<8))
535 #define SQLITE_NOTICE_RECOVER_ROLLBACK (SQLITE_NOTICE | (2<<8))
536 #define SQLITE_WARNING_AUTOINDEX       (SQLITE_WARNING | (1<<8))
537 
538 /*
539 ** CAPI3REF: Flags For File Open Operations
540 **
541 ** These bit values are intended for use in the
542 ** 3rd parameter to the [sqlite3_open_v2()] interface and
543 ** in the 4th parameter to the [sqlite3_vfs.xOpen] method.
544 */
545 #define SQLITE_OPEN_READONLY         0x00000001  /* Ok for sqlite3_open_v2() */
546 #define SQLITE_OPEN_READWRITE        0x00000002  /* Ok for sqlite3_open_v2() */
547 #define SQLITE_OPEN_CREATE           0x00000004  /* Ok for sqlite3_open_v2() */
548 #define SQLITE_OPEN_DELETEONCLOSE    0x00000008  /* VFS only */
549 #define SQLITE_OPEN_EXCLUSIVE        0x00000010  /* VFS only */
550 #define SQLITE_OPEN_AUTOPROXY        0x00000020  /* VFS only */
551 #define SQLITE_OPEN_URI              0x00000040  /* Ok for sqlite3_open_v2() */
552 #define SQLITE_OPEN_MEMORY           0x00000080  /* Ok for sqlite3_open_v2() */
553 #define SQLITE_OPEN_MAIN_DB          0x00000100  /* VFS only */
554 #define SQLITE_OPEN_TEMP_DB          0x00000200  /* VFS only */
555 #define SQLITE_OPEN_TRANSIENT_DB     0x00000400  /* VFS only */
556 #define SQLITE_OPEN_MAIN_JOURNAL     0x00000800  /* VFS only */
557 #define SQLITE_OPEN_TEMP_JOURNAL     0x00001000  /* VFS only */
558 #define SQLITE_OPEN_SUBJOURNAL       0x00002000  /* VFS only */
559 #define SQLITE_OPEN_MASTER_JOURNAL   0x00004000  /* VFS only */
560 #define SQLITE_OPEN_NOMUTEX          0x00008000  /* Ok for sqlite3_open_v2() */
561 #define SQLITE_OPEN_FULLMUTEX        0x00010000  /* Ok for sqlite3_open_v2() */
562 #define SQLITE_OPEN_SHAREDCACHE      0x00020000  /* Ok for sqlite3_open_v2() */
563 #define SQLITE_OPEN_PRIVATECACHE     0x00040000  /* Ok for sqlite3_open_v2() */
564 #define SQLITE_OPEN_WAL              0x00080000  /* VFS only */
565 
566 /* Reserved:                         0x00F00000 */
567 
568 /*
569 ** CAPI3REF: Device Characteristics
570 **
571 ** The xDeviceCharacteristics method of the [sqlite3_io_methods]
572 ** object returns an integer which is a vector of these
573 ** bit values expressing I/O characteristics of the mass storage
574 ** device that holds the file that the [sqlite3_io_methods]
575 ** refers to.
576 **
577 ** The SQLITE_IOCAP_ATOMIC property means that all writes of
578 ** any size are atomic.  The SQLITE_IOCAP_ATOMICnnn values
579 ** mean that writes of blocks that are nnn bytes in size and
580 ** are aligned to an address which is an integer multiple of
581 ** nnn are atomic.  The SQLITE_IOCAP_SAFE_APPEND value means
582 ** that when data is appended to a file, the data is appended
583 ** first then the size of the file is extended, never the other
584 ** way around.  The SQLITE_IOCAP_SEQUENTIAL property means that
585 ** information is written to disk in the same order as calls
586 ** to xWrite().  The SQLITE_IOCAP_POWERSAFE_OVERWRITE property means that
587 ** after reboot following a crash or power loss, the only bytes in a
588 ** file that were written at the application level might have changed
589 ** and that adjacent bytes, even bytes within the same sector are
590 ** guaranteed to be unchanged.  The SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN
591 ** flag indicate that a file cannot be deleted when open.
592 */
593 #define SQLITE_IOCAP_ATOMIC                 0x00000001
594 #define SQLITE_IOCAP_ATOMIC512              0x00000002
595 #define SQLITE_IOCAP_ATOMIC1K               0x00000004
596 #define SQLITE_IOCAP_ATOMIC2K               0x00000008
597 #define SQLITE_IOCAP_ATOMIC4K               0x00000010
598 #define SQLITE_IOCAP_ATOMIC8K               0x00000020
599 #define SQLITE_IOCAP_ATOMIC16K              0x00000040
600 #define SQLITE_IOCAP_ATOMIC32K              0x00000080
601 #define SQLITE_IOCAP_ATOMIC64K              0x00000100
602 #define SQLITE_IOCAP_SAFE_APPEND            0x00000200
603 #define SQLITE_IOCAP_SEQUENTIAL             0x00000400
604 #define SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN  0x00000800
605 #define SQLITE_IOCAP_POWERSAFE_OVERWRITE    0x00001000
606 
607 /*
608 ** CAPI3REF: File Locking Levels
609 **
610 ** SQLite uses one of these integer values as the second
611 ** argument to calls it makes to the xLock() and xUnlock() methods
612 ** of an [sqlite3_io_methods] object.
613 */
614 #define SQLITE_LOCK_NONE          0
615 #define SQLITE_LOCK_SHARED        1
616 #define SQLITE_LOCK_RESERVED      2
617 #define SQLITE_LOCK_PENDING       3
618 #define SQLITE_LOCK_EXCLUSIVE     4
619 
620 /*
621 ** CAPI3REF: Synchronization Type Flags
622 **
623 ** When SQLite invokes the xSync() method of an
624 ** [sqlite3_io_methods] object it uses a combination of
625 ** these integer values as the second argument.
626 **
627 ** When the SQLITE_SYNC_DATAONLY flag is used, it means that the
628 ** sync operation only needs to flush data to mass storage.  Inode
629 ** information need not be flushed. If the lower four bits of the flag
630 ** equal SQLITE_SYNC_NORMAL, that means to use normal fsync() semantics.
631 ** If the lower four bits equal SQLITE_SYNC_FULL, that means
632 ** to use Mac OS X style fullsync instead of fsync().
633 **
634 ** Do not confuse the SQLITE_SYNC_NORMAL and SQLITE_SYNC_FULL flags
635 ** with the [PRAGMA synchronous]=NORMAL and [PRAGMA synchronous]=FULL
636 ** settings.  The [synchronous pragma] determines when calls to the
637 ** xSync VFS method occur and applies uniformly across all platforms.
638 ** The SQLITE_SYNC_NORMAL and SQLITE_SYNC_FULL flags determine how
639 ** energetic or rigorous or forceful the sync operations are and
640 ** only make a difference on Mac OSX for the default SQLite code.
641 ** (Third-party VFS implementations might also make the distinction
642 ** between SQLITE_SYNC_NORMAL and SQLITE_SYNC_FULL, but among the
643 ** operating systems natively supported by SQLite, only Mac OSX
644 ** cares about the difference.)
645 */
646 #define SQLITE_SYNC_NORMAL        0x00002
647 #define SQLITE_SYNC_FULL          0x00003
648 #define SQLITE_SYNC_DATAONLY      0x00010
649 
650 /*
651 ** CAPI3REF: OS Interface Open File Handle
652 **
653 ** An [sqlite3_file] object represents an open file in the
654 ** [sqlite3_vfs | OS interface layer].  Individual OS interface
655 ** implementations will
656 ** want to subclass this object by appending additional fields
657 ** for their own use.  The pMethods entry is a pointer to an
658 ** [sqlite3_io_methods] object that defines methods for performing
659 ** I/O operations on the open file.
660 */
661 typedef struct sqlite3_file sqlite3_file;
662 struct sqlite3_file {
663   const struct sqlite3_io_methods *pMethods;  /* Methods for an open file */
664 };
665 
666 /*
667 ** CAPI3REF: OS Interface File Virtual Methods Object
668 **
669 ** Every file opened by the [sqlite3_vfs.xOpen] method populates an
670 ** [sqlite3_file] object (or, more commonly, a subclass of the
671 ** [sqlite3_file] object) with a pointer to an instance of this object.
672 ** This object defines the methods used to perform various operations
673 ** against the open file represented by the [sqlite3_file] object.
674 **
675 ** If the [sqlite3_vfs.xOpen] method sets the sqlite3_file.pMethods element
676 ** to a non-NULL pointer, then the sqlite3_io_methods.xClose method
677 ** may be invoked even if the [sqlite3_vfs.xOpen] reported that it failed.  The
678 ** only way to prevent a call to xClose following a failed [sqlite3_vfs.xOpen]
679 ** is for the [sqlite3_vfs.xOpen] to set the sqlite3_file.pMethods element
680 ** to NULL.
681 **
682 ** The flags argument to xSync may be one of [SQLITE_SYNC_NORMAL] or
683 ** [SQLITE_SYNC_FULL].  The first choice is the normal fsync().
684 ** The second choice is a Mac OS X style fullsync.  The [SQLITE_SYNC_DATAONLY]
685 ** flag may be ORed in to indicate that only the data of the file
686 ** and not its inode needs to be synced.
687 **
688 ** The integer values to xLock() and xUnlock() are one of
689 ** <ul>
690 ** <li> [SQLITE_LOCK_NONE],
691 ** <li> [SQLITE_LOCK_SHARED],
692 ** <li> [SQLITE_LOCK_RESERVED],
693 ** <li> [SQLITE_LOCK_PENDING], or
694 ** <li> [SQLITE_LOCK_EXCLUSIVE].
695 ** </ul>
696 ** xLock() increases the lock. xUnlock() decreases the lock.
697 ** The xCheckReservedLock() method checks whether any database connection,
698 ** either in this process or in some other process, is holding a RESERVED,
699 ** PENDING, or EXCLUSIVE lock on the file.  It returns true
700 ** if such a lock exists and false otherwise.
701 **
702 ** The xFileControl() method is a generic interface that allows custom
703 ** VFS implementations to directly control an open file using the
704 ** [sqlite3_file_control()] interface.  The second "op" argument is an
705 ** integer opcode.  The third argument is a generic pointer intended to
706 ** point to a structure that may contain arguments or space in which to
707 ** write return values.  Potential uses for xFileControl() might be
708 ** functions to enable blocking locks with timeouts, to change the
709 ** locking strategy (for example to use dot-file locks), to inquire
710 ** about the status of a lock, or to break stale locks.  The SQLite
711 ** core reserves all opcodes less than 100 for its own use.
712 ** A [SQLITE_FCNTL_LOCKSTATE | list of opcodes] less than 100 is available.
713 ** Applications that define a custom xFileControl method should use opcodes
714 ** greater than 100 to avoid conflicts.  VFS implementations should
715 ** return [SQLITE_NOTFOUND] for file control opcodes that they do not
716 ** recognize.
717 **
718 ** The xSectorSize() method returns the sector size of the
719 ** device that underlies the file.  The sector size is the
720 ** minimum write that can be performed without disturbing
721 ** other bytes in the file.  The xDeviceCharacteristics()
722 ** method returns a bit vector describing behaviors of the
723 ** underlying device:
724 **
725 ** <ul>
726 ** <li> [SQLITE_IOCAP_ATOMIC]
727 ** <li> [SQLITE_IOCAP_ATOMIC512]
728 ** <li> [SQLITE_IOCAP_ATOMIC1K]
729 ** <li> [SQLITE_IOCAP_ATOMIC2K]
730 ** <li> [SQLITE_IOCAP_ATOMIC4K]
731 ** <li> [SQLITE_IOCAP_ATOMIC8K]
732 ** <li> [SQLITE_IOCAP_ATOMIC16K]
733 ** <li> [SQLITE_IOCAP_ATOMIC32K]
734 ** <li> [SQLITE_IOCAP_ATOMIC64K]
735 ** <li> [SQLITE_IOCAP_SAFE_APPEND]
736 ** <li> [SQLITE_IOCAP_SEQUENTIAL]
737 ** </ul>
738 **
739 ** The SQLITE_IOCAP_ATOMIC property means that all writes of
740 ** any size are atomic.  The SQLITE_IOCAP_ATOMICnnn values
741 ** mean that writes of blocks that are nnn bytes in size and
742 ** are aligned to an address which is an integer multiple of
743 ** nnn are atomic.  The SQLITE_IOCAP_SAFE_APPEND value means
744 ** that when data is appended to a file, the data is appended
745 ** first then the size of the file is extended, never the other
746 ** way around.  The SQLITE_IOCAP_SEQUENTIAL property means that
747 ** information is written to disk in the same order as calls
748 ** to xWrite().
749 **
750 ** If xRead() returns SQLITE_IOERR_SHORT_READ it must also fill
751 ** in the unread portions of the buffer with zeros.  A VFS that
752 ** fails to zero-fill short reads might seem to work.  However,
753 ** failure to zero-fill short reads will eventually lead to
754 ** database corruption.
755 */
756 typedef struct sqlite3_io_methods sqlite3_io_methods;
757 struct sqlite3_io_methods {
758   int iVersion;
759   int (*xClose)(sqlite3_file*);
760   int (*xRead)(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
761   int (*xWrite)(sqlite3_file*, const void*, int iAmt, sqlite3_int64 iOfst);
762   int (*xTruncate)(sqlite3_file*, sqlite3_int64 size);
763   int (*xSync)(sqlite3_file*, int flags);
764   int (*xFileSize)(sqlite3_file*, sqlite3_int64 *pSize);
765   int (*xLock)(sqlite3_file*, int);
766   int (*xUnlock)(sqlite3_file*, int);
767   int (*xCheckReservedLock)(sqlite3_file*, int *pResOut);
768   int (*xFileControl)(sqlite3_file*, int op, void *pArg);
769   int (*xSectorSize)(sqlite3_file*);
770   int (*xDeviceCharacteristics)(sqlite3_file*);
771   /* Methods above are valid for version 1 */
772   int (*xShmMap)(sqlite3_file*, int iPg, int pgsz, int, void volatile**);
773   int (*xShmLock)(sqlite3_file*, int offset, int n, int flags);
774   void (*xShmBarrier)(sqlite3_file*);
775   int (*xShmUnmap)(sqlite3_file*, int deleteFlag);
776   /* Methods above are valid for version 2 */
777   int (*xFetch)(sqlite3_file*, sqlite3_int64 iOfst, int iAmt, void **pp);
778   int (*xUnfetch)(sqlite3_file*, sqlite3_int64 iOfst, void *p);
779   /* Methods above are valid for version 3 */
780   /* Additional methods may be added in future releases */
781 };
782 
783 /*
784 ** CAPI3REF: Standard File Control Opcodes
785 **
786 ** These integer constants are opcodes for the xFileControl method
787 ** of the [sqlite3_io_methods] object and for the [sqlite3_file_control()]
788 ** interface.
789 **
790 ** The [SQLITE_FCNTL_LOCKSTATE] opcode is used for debugging.  This
791 ** opcode causes the xFileControl method to write the current state of
792 ** the lock (one of [SQLITE_LOCK_NONE], [SQLITE_LOCK_SHARED],
793 ** [SQLITE_LOCK_RESERVED], [SQLITE_LOCK_PENDING], or [SQLITE_LOCK_EXCLUSIVE])
794 ** into an integer that the pArg argument points to. This capability
795 ** is used during testing and only needs to be supported when SQLITE_TEST
796 ** is defined.
797 ** <ul>
798 ** <li>[[SQLITE_FCNTL_SIZE_HINT]]
799 ** The [SQLITE_FCNTL_SIZE_HINT] opcode is used by SQLite to give the VFS
800 ** layer a hint of how large the database file will grow to be during the
801 ** current transaction.  This hint is not guaranteed to be accurate but it
802 ** is often close.  The underlying VFS might choose to preallocate database
803 ** file space based on this hint in order to help writes to the database
804 ** file run faster.
805 **
806 ** <li>[[SQLITE_FCNTL_CHUNK_SIZE]]
807 ** The [SQLITE_FCNTL_CHUNK_SIZE] opcode is used to request that the VFS
808 ** extends and truncates the database file in chunks of a size specified
809 ** by the user. The fourth argument to [sqlite3_file_control()] should
810 ** point to an integer (type int) containing the new chunk-size to use
811 ** for the nominated database. Allocating database file space in large
812 ** chunks (say 1MB at a time), may reduce file-system fragmentation and
813 ** improve performance on some systems.
814 **
815 ** <li>[[SQLITE_FCNTL_FILE_POINTER]]
816 ** The [SQLITE_FCNTL_FILE_POINTER] opcode is used to obtain a pointer
817 ** to the [sqlite3_file] object associated with a particular database
818 ** connection.  See the [sqlite3_file_control()] documentation for
819 ** additional information.
820 **
821 ** <li>[[SQLITE_FCNTL_SYNC_OMITTED]]
822 ** No longer in use.
823 **
824 ** <li>[[SQLITE_FCNTL_SYNC]]
825 ** The [SQLITE_FCNTL_SYNC] opcode is generated internally by SQLite and
826 ** sent to the VFS immediately before the xSync method is invoked on a
827 ** database file descriptor. Or, if the xSync method is not invoked
828 ** because the user has configured SQLite with
829 ** [PRAGMA synchronous | PRAGMA synchronous=OFF] it is invoked in place
830 ** of the xSync method. In most cases, the pointer argument passed with
831 ** this file-control is NULL. However, if the database file is being synced
832 ** as part of a multi-database commit, the argument points to a nul-terminated
833 ** string containing the transactions master-journal file name. VFSes that
834 ** do not need this signal should silently ignore this opcode. Applications
835 ** should not call [sqlite3_file_control()] with this opcode as doing so may
836 ** disrupt the operation of the specialized VFSes that do require it.
837 **
838 ** <li>[[SQLITE_FCNTL_COMMIT_PHASETWO]]
839 ** The [SQLITE_FCNTL_COMMIT_PHASETWO] opcode is generated internally by SQLite
840 ** and sent to the VFS after a transaction has been committed immediately
841 ** but before the database is unlocked. VFSes that do not need this signal
842 ** should silently ignore this opcode. Applications should not call
843 ** [sqlite3_file_control()] with this opcode as doing so may disrupt the
844 ** operation of the specialized VFSes that do require it.
845 **
846 ** <li>[[SQLITE_FCNTL_WIN32_AV_RETRY]]
847 ** ^The [SQLITE_FCNTL_WIN32_AV_RETRY] opcode is used to configure automatic
848 ** retry counts and intervals for certain disk I/O operations for the
849 ** windows [VFS] in order to provide robustness in the presence of
850 ** anti-virus programs.  By default, the windows VFS will retry file read,
851 ** file write, and file delete operations up to 10 times, with a delay
852 ** of 25 milliseconds before the first retry and with the delay increasing
853 ** by an additional 25 milliseconds with each subsequent retry.  This
854 ** opcode allows these two values (10 retries and 25 milliseconds of delay)
855 ** to be adjusted.  The values are changed for all database connections
856 ** within the same process.  The argument is a pointer to an array of two
857 ** integers where the first integer i the new retry count and the second
858 ** integer is the delay.  If either integer is negative, then the setting
859 ** is not changed but instead the prior value of that setting is written
860 ** into the array entry, allowing the current retry settings to be
861 ** interrogated.  The zDbName parameter is ignored.
862 **
863 ** <li>[[SQLITE_FCNTL_PERSIST_WAL]]
864 ** ^The [SQLITE_FCNTL_PERSIST_WAL] opcode is used to set or query the
865 ** persistent [WAL | Write Ahead Log] setting.  By default, the auxiliary
866 ** write ahead log and shared memory files used for transaction control
867 ** are automatically deleted when the latest connection to the database
868 ** closes.  Setting persistent WAL mode causes those files to persist after
869 ** close.  Persisting the files is useful when other processes that do not
870 ** have write permission on the directory containing the database file want
871 ** to read the database file, as the WAL and shared memory files must exist
872 ** in order for the database to be readable.  The fourth parameter to
873 ** [sqlite3_file_control()] for this opcode should be a pointer to an integer.
874 ** That integer is 0 to disable persistent WAL mode or 1 to enable persistent
875 ** WAL mode.  If the integer is -1, then it is overwritten with the current
876 ** WAL persistence setting.
877 **
878 ** <li>[[SQLITE_FCNTL_POWERSAFE_OVERWRITE]]
879 ** ^The [SQLITE_FCNTL_POWERSAFE_OVERWRITE] opcode is used to set or query the
880 ** persistent "powersafe-overwrite" or "PSOW" setting.  The PSOW setting
881 ** determines the [SQLITE_IOCAP_POWERSAFE_OVERWRITE] bit of the
882 ** xDeviceCharacteristics methods. The fourth parameter to
883 ** [sqlite3_file_control()] for this opcode should be a pointer to an integer.
884 ** That integer is 0 to disable zero-damage mode or 1 to enable zero-damage
885 ** mode.  If the integer is -1, then it is overwritten with the current
886 ** zero-damage mode setting.
887 **
888 ** <li>[[SQLITE_FCNTL_OVERWRITE]]
889 ** ^The [SQLITE_FCNTL_OVERWRITE] opcode is invoked by SQLite after opening
890 ** a write transaction to indicate that, unless it is rolled back for some
891 ** reason, the entire database file will be overwritten by the current
892 ** transaction. This is used by VACUUM operations.
893 **
894 ** <li>[[SQLITE_FCNTL_VFSNAME]]
895 ** ^The [SQLITE_FCNTL_VFSNAME] opcode can be used to obtain the names of
896 ** all [VFSes] in the VFS stack.  The names are of all VFS shims and the
897 ** final bottom-level VFS are written into memory obtained from
898 ** [sqlite3_malloc()] and the result is stored in the char* variable
899 ** that the fourth parameter of [sqlite3_file_control()] points to.
900 ** The caller is responsible for freeing the memory when done.  As with
901 ** all file-control actions, there is no guarantee that this will actually
902 ** do anything.  Callers should initialize the char* variable to a NULL
903 ** pointer in case this file-control is not implemented.  This file-control
904 ** is intended for diagnostic use only.
905 **
906 ** <li>[[SQLITE_FCNTL_PRAGMA]]
907 ** ^Whenever a [PRAGMA] statement is parsed, an [SQLITE_FCNTL_PRAGMA]
908 ** file control is sent to the open [sqlite3_file] object corresponding
909 ** to the database file to which the pragma statement refers. ^The argument
910 ** to the [SQLITE_FCNTL_PRAGMA] file control is an array of
911 ** pointers to strings (char**) in which the second element of the array
912 ** is the name of the pragma and the third element is the argument to the
913 ** pragma or NULL if the pragma has no argument.  ^The handler for an
914 ** [SQLITE_FCNTL_PRAGMA] file control can optionally make the first element
915 ** of the char** argument point to a string obtained from [sqlite3_mprintf()]
916 ** or the equivalent and that string will become the result of the pragma or
917 ** the error message if the pragma fails. ^If the
918 ** [SQLITE_FCNTL_PRAGMA] file control returns [SQLITE_NOTFOUND], then normal
919 ** [PRAGMA] processing continues.  ^If the [SQLITE_FCNTL_PRAGMA]
920 ** file control returns [SQLITE_OK], then the parser assumes that the
921 ** VFS has handled the PRAGMA itself and the parser generates a no-op
922 ** prepared statement.  ^If the [SQLITE_FCNTL_PRAGMA] file control returns
923 ** any result code other than [SQLITE_OK] or [SQLITE_NOTFOUND], that means
924 ** that the VFS encountered an error while handling the [PRAGMA] and the
925 ** compilation of the PRAGMA fails with an error.  ^The [SQLITE_FCNTL_PRAGMA]
926 ** file control occurs at the beginning of pragma statement analysis and so
927 ** it is able to override built-in [PRAGMA] statements.
928 **
929 ** <li>[[SQLITE_FCNTL_BUSYHANDLER]]
930 ** ^The [SQLITE_FCNTL_BUSYHANDLER]
931 ** file-control may be invoked by SQLite on the database file handle
932 ** shortly after it is opened in order to provide a custom VFS with access
933 ** to the connections busy-handler callback. The argument is of type (void **)
934 ** - an array of two (void *) values. The first (void *) actually points
935 ** to a function of type (int (*)(void *)). In order to invoke the connections
936 ** busy-handler, this function should be invoked with the second (void *) in
937 ** the array as the only argument. If it returns non-zero, then the operation
938 ** should be retried. If it returns zero, the custom VFS should abandon the
939 ** current operation.
940 **
941 ** <li>[[SQLITE_FCNTL_TEMPFILENAME]]
942 ** ^Application can invoke the [SQLITE_FCNTL_TEMPFILENAME] file-control
943 ** to have SQLite generate a
944 ** temporary filename using the same algorithm that is followed to generate
945 ** temporary filenames for TEMP tables and other internal uses.  The
946 ** argument should be a char** which will be filled with the filename
947 ** written into memory obtained from [sqlite3_malloc()].  The caller should
948 ** invoke [sqlite3_free()] on the result to avoid a memory leak.
949 **
950 ** <li>[[SQLITE_FCNTL_MMAP_SIZE]]
951 ** The [SQLITE_FCNTL_MMAP_SIZE] file control is used to query or set the
952 ** maximum number of bytes that will be used for memory-mapped I/O.
953 ** The argument is a pointer to a value of type sqlite3_int64 that
954 ** is an advisory maximum number of bytes in the file to memory map.  The
955 ** pointer is overwritten with the old value.  The limit is not changed if
956 ** the value originally pointed to is negative, and so the current limit
957 ** can be queried by passing in a pointer to a negative number.  This
958 ** file-control is used internally to implement [PRAGMA mmap_size].
959 **
960 ** <li>[[SQLITE_FCNTL_TRACE]]
961 ** The [SQLITE_FCNTL_TRACE] file control provides advisory information
962 ** to the VFS about what the higher layers of the SQLite stack are doing.
963 ** This file control is used by some VFS activity tracing [shims].
964 ** The argument is a zero-terminated string.  Higher layers in the
965 ** SQLite stack may generate instances of this file control if
966 ** the [SQLITE_USE_FCNTL_TRACE] compile-time option is enabled.
967 **
968 ** <li>[[SQLITE_FCNTL_HAS_MOVED]]
969 ** The [SQLITE_FCNTL_HAS_MOVED] file control interprets its argument as a
970 ** pointer to an integer and it writes a boolean into that integer depending
971 ** on whether or not the file has been renamed, moved, or deleted since it
972 ** was first opened.
973 **
974 ** </ul>
975 */
976 #define SQLITE_FCNTL_LOCKSTATE               1
977 #define SQLITE_GET_LOCKPROXYFILE             2
978 #define SQLITE_SET_LOCKPROXYFILE             3
979 #define SQLITE_LAST_ERRNO                    4
980 #define SQLITE_FCNTL_SIZE_HINT               5
981 #define SQLITE_FCNTL_CHUNK_SIZE              6
982 #define SQLITE_FCNTL_FILE_POINTER            7
983 #define SQLITE_FCNTL_SYNC_OMITTED            8
984 #define SQLITE_FCNTL_WIN32_AV_RETRY          9
985 #define SQLITE_FCNTL_PERSIST_WAL            10
986 #define SQLITE_FCNTL_OVERWRITE              11
987 #define SQLITE_FCNTL_VFSNAME                12
988 #define SQLITE_FCNTL_POWERSAFE_OVERWRITE    13
989 #define SQLITE_FCNTL_PRAGMA                 14
990 #define SQLITE_FCNTL_BUSYHANDLER            15
991 #define SQLITE_FCNTL_TEMPFILENAME           16
992 #define SQLITE_FCNTL_MMAP_SIZE              18
993 #define SQLITE_FCNTL_TRACE                  19
994 #define SQLITE_FCNTL_HAS_MOVED              20
995 #define SQLITE_FCNTL_SYNC                   21
996 #define SQLITE_FCNTL_COMMIT_PHASETWO        22
997 
998 /*
999 ** CAPI3REF: Mutex Handle
1000 **
1001 ** The mutex module within SQLite defines [sqlite3_mutex] to be an
1002 ** abstract type for a mutex object.  The SQLite core never looks
1003 ** at the internal representation of an [sqlite3_mutex].  It only
1004 ** deals with pointers to the [sqlite3_mutex] object.
1005 **
1006 ** Mutexes are created using [sqlite3_mutex_alloc()].
1007 */
1008 typedef struct sqlite3_mutex sqlite3_mutex;
1009 
1010 /*
1011 ** CAPI3REF: OS Interface Object
1012 **
1013 ** An instance of the sqlite3_vfs object defines the interface between
1014 ** the SQLite core and the underlying operating system.  The "vfs"
1015 ** in the name of the object stands for "virtual file system".  See
1016 ** the [VFS | VFS documentation] for further information.
1017 **
1018 ** The value of the iVersion field is initially 1 but may be larger in
1019 ** future versions of SQLite.  Additional fields may be appended to this
1020 ** object when the iVersion value is increased.  Note that the structure
1021 ** of the sqlite3_vfs object changes in the transaction between
1022 ** SQLite version 3.5.9 and 3.6.0 and yet the iVersion field was not
1023 ** modified.
1024 **
1025 ** The szOsFile field is the size of the subclassed [sqlite3_file]
1026 ** structure used by this VFS.  mxPathname is the maximum length of
1027 ** a pathname in this VFS.
1028 **
1029 ** Registered sqlite3_vfs objects are kept on a linked list formed by
1030 ** the pNext pointer.  The [sqlite3_vfs_register()]
1031 ** and [sqlite3_vfs_unregister()] interfaces manage this list
1032 ** in a thread-safe way.  The [sqlite3_vfs_find()] interface
1033 ** searches the list.  Neither the application code nor the VFS
1034 ** implementation should use the pNext pointer.
1035 **
1036 ** The pNext field is the only field in the sqlite3_vfs
1037 ** structure that SQLite will ever modify.  SQLite will only access
1038 ** or modify this field while holding a particular static mutex.
1039 ** The application should never modify anything within the sqlite3_vfs
1040 ** object once the object has been registered.
1041 **
1042 ** The zName field holds the name of the VFS module.  The name must
1043 ** be unique across all VFS modules.
1044 **
1045 ** [[sqlite3_vfs.xOpen]]
1046 ** ^SQLite guarantees that the zFilename parameter to xOpen
1047 ** is either a NULL pointer or string obtained
1048 ** from xFullPathname() with an optional suffix added.
1049 ** ^If a suffix is added to the zFilename parameter, it will
1050 ** consist of a single "-" character followed by no more than
1051 ** 11 alphanumeric and/or "-" characters.
1052 ** ^SQLite further guarantees that
1053 ** the string will be valid and unchanged until xClose() is
1054 ** called. Because of the previous sentence,
1055 ** the [sqlite3_file] can safely store a pointer to the
1056 ** filename if it needs to remember the filename for some reason.
1057 ** If the zFilename parameter to xOpen is a NULL pointer then xOpen
1058 ** must invent its own temporary name for the file.  ^Whenever the
1059 ** xFilename parameter is NULL it will also be the case that the
1060 ** flags parameter will include [SQLITE_OPEN_DELETEONCLOSE].
1061 **
1062 ** The flags argument to xOpen() includes all bits set in
1063 ** the flags argument to [sqlite3_open_v2()].  Or if [sqlite3_open()]
1064 ** or [sqlite3_open16()] is used, then flags includes at least
1065 ** [SQLITE_OPEN_READWRITE] | [SQLITE_OPEN_CREATE].
1066 ** If xOpen() opens a file read-only then it sets *pOutFlags to
1067 ** include [SQLITE_OPEN_READONLY].  Other bits in *pOutFlags may be set.
1068 **
1069 ** ^(SQLite will also add one of the following flags to the xOpen()
1070 ** call, depending on the object being opened:
1071 **
1072 ** <ul>
1073 ** <li>  [SQLITE_OPEN_MAIN_DB]
1074 ** <li>  [SQLITE_OPEN_MAIN_JOURNAL]
1075 ** <li>  [SQLITE_OPEN_TEMP_DB]
1076 ** <li>  [SQLITE_OPEN_TEMP_JOURNAL]
1077 ** <li>  [SQLITE_OPEN_TRANSIENT_DB]
1078 ** <li>  [SQLITE_OPEN_SUBJOURNAL]
1079 ** <li>  [SQLITE_OPEN_MASTER_JOURNAL]
1080 ** <li>  [SQLITE_OPEN_WAL]
1081 ** </ul>)^
1082 **
1083 ** The file I/O implementation can use the object type flags to
1084 ** change the way it deals with files.  For example, an application
1085 ** that does not care about crash recovery or rollback might make
1086 ** the open of a journal file a no-op.  Writes to this journal would
1087 ** also be no-ops, and any attempt to read the journal would return
1088 ** SQLITE_IOERR.  Or the implementation might recognize that a database
1089 ** file will be doing page-aligned sector reads and writes in a random
1090 ** order and set up its I/O subsystem accordingly.
1091 **
1092 ** SQLite might also add one of the following flags to the xOpen method:
1093 **
1094 ** <ul>
1095 ** <li> [SQLITE_OPEN_DELETEONCLOSE]
1096 ** <li> [SQLITE_OPEN_EXCLUSIVE]
1097 ** </ul>
1098 **
1099 ** The [SQLITE_OPEN_DELETEONCLOSE] flag means the file should be
1100 ** deleted when it is closed.  ^The [SQLITE_OPEN_DELETEONCLOSE]
1101 ** will be set for TEMP databases and their journals, transient
1102 ** databases, and subjournals.
1103 **
1104 ** ^The [SQLITE_OPEN_EXCLUSIVE] flag is always used in conjunction
1105 ** with the [SQLITE_OPEN_CREATE] flag, which are both directly
1106 ** analogous to the O_EXCL and O_CREAT flags of the POSIX open()
1107 ** API.  The SQLITE_OPEN_EXCLUSIVE flag, when paired with the
1108 ** SQLITE_OPEN_CREATE, is used to indicate that file should always
1109 ** be created, and that it is an error if it already exists.
1110 ** It is <i>not</i> used to indicate the file should be opened
1111 ** for exclusive access.
1112 **
1113 ** ^At least szOsFile bytes of memory are allocated by SQLite
1114 ** to hold the  [sqlite3_file] structure passed as the third
1115 ** argument to xOpen.  The xOpen method does not have to
1116 ** allocate the structure; it should just fill it in.  Note that
1117 ** the xOpen method must set the sqlite3_file.pMethods to either
1118 ** a valid [sqlite3_io_methods] object or to NULL.  xOpen must do
1119 ** this even if the open fails.  SQLite expects that the sqlite3_file.pMethods
1120 ** element will be valid after xOpen returns regardless of the success
1121 ** or failure of the xOpen call.
1122 **
1123 ** [[sqlite3_vfs.xAccess]]
1124 ** ^The flags argument to xAccess() may be [SQLITE_ACCESS_EXISTS]
1125 ** to test for the existence of a file, or [SQLITE_ACCESS_READWRITE] to
1126 ** test whether a file is readable and writable, or [SQLITE_ACCESS_READ]
1127 ** to test whether a file is at least readable.   The file can be a
1128 ** directory.
1129 **
1130 ** ^SQLite will always allocate at least mxPathname+1 bytes for the
1131 ** output buffer xFullPathname.  The exact size of the output buffer
1132 ** is also passed as a parameter to both  methods. If the output buffer
1133 ** is not large enough, [SQLITE_CANTOPEN] should be returned. Since this is
1134 ** handled as a fatal error by SQLite, vfs implementations should endeavor
1135 ** to prevent this by setting mxPathname to a sufficiently large value.
1136 **
1137 ** The xRandomness(), xSleep(), xCurrentTime(), and xCurrentTimeInt64()
1138 ** interfaces are not strictly a part of the filesystem, but they are
1139 ** included in the VFS structure for completeness.
1140 ** The xRandomness() function attempts to return nBytes bytes
1141 ** of good-quality randomness into zOut.  The return value is
1142 ** the actual number of bytes of randomness obtained.
1143 ** The xSleep() method causes the calling thread to sleep for at
1144 ** least the number of microseconds given.  ^The xCurrentTime()
1145 ** method returns a Julian Day Number for the current date and time as
1146 ** a floating point value.
1147 ** ^The xCurrentTimeInt64() method returns, as an integer, the Julian
1148 ** Day Number multiplied by 86400000 (the number of milliseconds in
1149 ** a 24-hour day).
1150 ** ^SQLite will use the xCurrentTimeInt64() method to get the current
1151 ** date and time if that method is available (if iVersion is 2 or
1152 ** greater and the function pointer is not NULL) and will fall back
1153 ** to xCurrentTime() if xCurrentTimeInt64() is unavailable.
1154 **
1155 ** ^The xSetSystemCall(), xGetSystemCall(), and xNestSystemCall() interfaces
1156 ** are not used by the SQLite core.  These optional interfaces are provided
1157 ** by some VFSes to facilitate testing of the VFS code. By overriding
1158 ** system calls with functions under its control, a test program can
1159 ** simulate faults and error conditions that would otherwise be difficult
1160 ** or impossible to induce.  The set of system calls that can be overridden
1161 ** varies from one VFS to another, and from one version of the same VFS to the
1162 ** next.  Applications that use these interfaces must be prepared for any
1163 ** or all of these interfaces to be NULL or for their behavior to change
1164 ** from one release to the next.  Applications must not attempt to access
1165 ** any of these methods if the iVersion of the VFS is less than 3.
1166 */
1167 typedef struct sqlite3_vfs sqlite3_vfs;
1168 typedef void (*sqlite3_syscall_ptr)(void);
1169 struct sqlite3_vfs {
1170   int iVersion;            /* Structure version number (currently 3) */
1171   int szOsFile;            /* Size of subclassed sqlite3_file */
1172   int mxPathname;          /* Maximum file pathname length */
1173   sqlite3_vfs *pNext;      /* Next registered VFS */
1174   const char *zName;       /* Name of this virtual file system */
1175   void *pAppData;          /* Pointer to application-specific data */
1176   int (*xOpen)(sqlite3_vfs*, const char *zName, sqlite3_file*,
1177                int flags, int *pOutFlags);
1178   int (*xDelete)(sqlite3_vfs*, const char *zName, int syncDir);
1179   int (*xAccess)(sqlite3_vfs*, const char *zName, int flags, int *pResOut);
1180   int (*xFullPathname)(sqlite3_vfs*, const char *zName, int nOut, char *zOut);
1181   void *(*xDlOpen)(sqlite3_vfs*, const char *zFilename);
1182   void (*xDlError)(sqlite3_vfs*, int nByte, char *zErrMsg);
1183   void (*(*xDlSym)(sqlite3_vfs*,void*, const char *zSymbol))(void);
1184   void (*xDlClose)(sqlite3_vfs*, void*);
1185   int (*xRandomness)(sqlite3_vfs*, int nByte, char *zOut);
1186   int (*xSleep)(sqlite3_vfs*, int microseconds);
1187   int (*xCurrentTime)(sqlite3_vfs*, double*);
1188   int (*xGetLastError)(sqlite3_vfs*, int, char *);
1189   /*
1190   ** The methods above are in version 1 of the sqlite_vfs object
1191   ** definition.  Those that follow are added in version 2 or later
1192   */
1193   int (*xCurrentTimeInt64)(sqlite3_vfs*, sqlite3_int64*);
1194   /*
1195   ** The methods above are in versions 1 and 2 of the sqlite_vfs object.
1196   ** Those below are for version 3 and greater.
1197   */
1198   int (*xSetSystemCall)(sqlite3_vfs*, const char *zName, sqlite3_syscall_ptr);
1199   sqlite3_syscall_ptr (*xGetSystemCall)(sqlite3_vfs*, const char *zName);
1200   const char *(*xNextSystemCall)(sqlite3_vfs*, const char *zName);
1201   /*
1202   ** The methods above are in versions 1 through 3 of the sqlite_vfs object.
1203   ** New fields may be appended in figure versions.  The iVersion
1204   ** value will increment whenever this happens.
1205   */
1206 };
1207 
1208 /*
1209 ** CAPI3REF: Flags for the xAccess VFS method
1210 **
1211 ** These integer constants can be used as the third parameter to
1212 ** the xAccess method of an [sqlite3_vfs] object.  They determine
1213 ** what kind of permissions the xAccess method is looking for.
1214 ** With SQLITE_ACCESS_EXISTS, the xAccess method
1215 ** simply checks whether the file exists.
1216 ** With SQLITE_ACCESS_READWRITE, the xAccess method
1217 ** checks whether the named directory is both readable and writable
1218 ** (in other words, if files can be added, removed, and renamed within
1219 ** the directory).
1220 ** The SQLITE_ACCESS_READWRITE constant is currently used only by the
1221 ** [temp_store_directory pragma], though this could change in a future
1222 ** release of SQLite.
1223 ** With SQLITE_ACCESS_READ, the xAccess method
1224 ** checks whether the file is readable.  The SQLITE_ACCESS_READ constant is
1225 ** currently unused, though it might be used in a future release of
1226 ** SQLite.
1227 */
1228 #define SQLITE_ACCESS_EXISTS    0
1229 #define SQLITE_ACCESS_READWRITE 1   /* Used by PRAGMA temp_store_directory */
1230 #define SQLITE_ACCESS_READ      2   /* Unused */
1231 
1232 /*
1233 ** CAPI3REF: Flags for the xShmLock VFS method
1234 **
1235 ** These integer constants define the various locking operations
1236 ** allowed by the xShmLock method of [sqlite3_io_methods].  The
1237 ** following are the only legal combinations of flags to the
1238 ** xShmLock method:
1239 **
1240 ** <ul>
1241 ** <li>  SQLITE_SHM_LOCK | SQLITE_SHM_SHARED
1242 ** <li>  SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE
1243 ** <li>  SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED
1244 ** <li>  SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE
1245 ** </ul>
1246 **
1247 ** When unlocking, the same SHARED or EXCLUSIVE flag must be supplied as
1248 ** was given no the corresponding lock.
1249 **
1250 ** The xShmLock method can transition between unlocked and SHARED or
1251 ** between unlocked and EXCLUSIVE.  It cannot transition between SHARED
1252 ** and EXCLUSIVE.
1253 */
1254 #define SQLITE_SHM_UNLOCK       1
1255 #define SQLITE_SHM_LOCK         2
1256 #define SQLITE_SHM_SHARED       4
1257 #define SQLITE_SHM_EXCLUSIVE    8
1258 
1259 /*
1260 ** CAPI3REF: Maximum xShmLock index
1261 **
1262 ** The xShmLock method on [sqlite3_io_methods] may use values
1263 ** between 0 and this upper bound as its "offset" argument.
1264 ** The SQLite core will never attempt to acquire or release a
1265 ** lock outside of this range
1266 */
1267 #define SQLITE_SHM_NLOCK        8
1268 
1269 
1270 /*
1271 ** CAPI3REF: Initialize The SQLite Library
1272 **
1273 ** ^The sqlite3_initialize() routine initializes the
1274 ** SQLite library.  ^The sqlite3_shutdown() routine
1275 ** deallocates any resources that were allocated by sqlite3_initialize().
1276 ** These routines are designed to aid in process initialization and
1277 ** shutdown on embedded systems.  Workstation applications using
1278 ** SQLite normally do not need to invoke either of these routines.
1279 **
1280 ** A call to sqlite3_initialize() is an "effective" call if it is
1281 ** the first time sqlite3_initialize() is invoked during the lifetime of
1282 ** the process, or if it is the first time sqlite3_initialize() is invoked
1283 ** following a call to sqlite3_shutdown().  ^(Only an effective call
1284 ** of sqlite3_initialize() does any initialization.  All other calls
1285 ** are harmless no-ops.)^
1286 **
1287 ** A call to sqlite3_shutdown() is an "effective" call if it is the first
1288 ** call to sqlite3_shutdown() since the last sqlite3_initialize().  ^(Only
1289 ** an effective call to sqlite3_shutdown() does any deinitialization.
1290 ** All other valid calls to sqlite3_shutdown() are harmless no-ops.)^
1291 **
1292 ** The sqlite3_initialize() interface is threadsafe, but sqlite3_shutdown()
1293 ** is not.  The sqlite3_shutdown() interface must only be called from a
1294 ** single thread.  All open [database connections] must be closed and all
1295 ** other SQLite resources must be deallocated prior to invoking
1296 ** sqlite3_shutdown().
1297 **
1298 ** Among other things, ^sqlite3_initialize() will invoke
1299 ** sqlite3_os_init().  Similarly, ^sqlite3_shutdown()
1300 ** will invoke sqlite3_os_end().
1301 **
1302 ** ^The sqlite3_initialize() routine returns [SQLITE_OK] on success.
1303 ** ^If for some reason, sqlite3_initialize() is unable to initialize
1304 ** the library (perhaps it is unable to allocate a needed resource such
1305 ** as a mutex) it returns an [error code] other than [SQLITE_OK].
1306 **
1307 ** ^The sqlite3_initialize() routine is called internally by many other
1308 ** SQLite interfaces so that an application usually does not need to
1309 ** invoke sqlite3_initialize() directly.  For example, [sqlite3_open()]
1310 ** calls sqlite3_initialize() so the SQLite library will be automatically
1311 ** initialized when [sqlite3_open()] is called if it has not be initialized
1312 ** already.  ^However, if SQLite is compiled with the [SQLITE_OMIT_AUTOINIT]
1313 ** compile-time option, then the automatic calls to sqlite3_initialize()
1314 ** are omitted and the application must call sqlite3_initialize() directly
1315 ** prior to using any other SQLite interface.  For maximum portability,
1316 ** it is recommended that applications always invoke sqlite3_initialize()
1317 ** directly prior to using any other SQLite interface.  Future releases
1318 ** of SQLite may require this.  In other words, the behavior exhibited
1319 ** when SQLite is compiled with [SQLITE_OMIT_AUTOINIT] might become the
1320 ** default behavior in some future release of SQLite.
1321 **
1322 ** The sqlite3_os_init() routine does operating-system specific
1323 ** initialization of the SQLite library.  The sqlite3_os_end()
1324 ** routine undoes the effect of sqlite3_os_init().  Typical tasks
1325 ** performed by these routines include allocation or deallocation
1326 ** of static resources, initialization of global variables,
1327 ** setting up a default [sqlite3_vfs] module, or setting up
1328 ** a default configuration using [sqlite3_config()].
1329 **
1330 ** The application should never invoke either sqlite3_os_init()
1331 ** or sqlite3_os_end() directly.  The application should only invoke
1332 ** sqlite3_initialize() and sqlite3_shutdown().  The sqlite3_os_init()
1333 ** interface is called automatically by sqlite3_initialize() and
1334 ** sqlite3_os_end() is called by sqlite3_shutdown().  Appropriate
1335 ** implementations for sqlite3_os_init() and sqlite3_os_end()
1336 ** are built into SQLite when it is compiled for Unix, Windows, or OS/2.
1337 ** When [custom builds | built for other platforms]
1338 ** (using the [SQLITE_OS_OTHER=1] compile-time
1339 ** option) the application must supply a suitable implementation for
1340 ** sqlite3_os_init() and sqlite3_os_end().  An application-supplied
1341 ** implementation of sqlite3_os_init() or sqlite3_os_end()
1342 ** must return [SQLITE_OK] on success and some other [error code] upon
1343 ** failure.
1344 */
1345 SQLITE_API int sqlite3_initialize(void);
1346 SQLITE_API int sqlite3_shutdown(void);
1347 SQLITE_API int sqlite3_os_init(void);
1348 SQLITE_API int sqlite3_os_end(void);
1349 
1350 /*
1351 ** CAPI3REF: Configuring The SQLite Library
1352 **
1353 ** The sqlite3_config() interface is used to make global configuration
1354 ** changes to SQLite in order to tune SQLite to the specific needs of
1355 ** the application.  The default configuration is recommended for most
1356 ** applications and so this routine is usually not necessary.  It is
1357 ** provided to support rare applications with unusual needs.
1358 **
1359 ** The sqlite3_config() interface is not threadsafe.  The application
1360 ** must insure that no other SQLite interfaces are invoked by other
1361 ** threads while sqlite3_config() is running.  Furthermore, sqlite3_config()
1362 ** may only be invoked prior to library initialization using
1363 ** [sqlite3_initialize()] or after shutdown by [sqlite3_shutdown()].
1364 ** ^If sqlite3_config() is called after [sqlite3_initialize()] and before
1365 ** [sqlite3_shutdown()] then it will return SQLITE_MISUSE.
1366 ** Note, however, that ^sqlite3_config() can be called as part of the
1367 ** implementation of an application-defined [sqlite3_os_init()].
1368 **
1369 ** The first argument to sqlite3_config() is an integer
1370 ** [configuration option] that determines
1371 ** what property of SQLite is to be configured.  Subsequent arguments
1372 ** vary depending on the [configuration option]
1373 ** in the first argument.
1374 **
1375 ** ^When a configuration option is set, sqlite3_config() returns [SQLITE_OK].
1376 ** ^If the option is unknown or SQLite is unable to set the option
1377 ** then this routine returns a non-zero [error code].
1378 */
1379 SQLITE_API int sqlite3_config(int, ...);
1380 
1381 /*
1382 ** CAPI3REF: Configure database connections
1383 **
1384 ** The sqlite3_db_config() interface is used to make configuration
1385 ** changes to a [database connection].  The interface is similar to
1386 ** [sqlite3_config()] except that the changes apply to a single
1387 ** [database connection] (specified in the first argument).
1388 **
1389 ** The second argument to sqlite3_db_config(D,V,...)  is the
1390 ** [SQLITE_DBCONFIG_LOOKASIDE | configuration verb] - an integer code
1391 ** that indicates what aspect of the [database connection] is being configured.
1392 ** Subsequent arguments vary depending on the configuration verb.
1393 **
1394 ** ^Calls to sqlite3_db_config() return SQLITE_OK if and only if
1395 ** the call is considered successful.
1396 */
1397 SQLITE_API int sqlite3_db_config(sqlite3*, int op, ...);
1398 
1399 /*
1400 ** CAPI3REF: Memory Allocation Routines
1401 **
1402 ** An instance of this object defines the interface between SQLite
1403 ** and low-level memory allocation routines.
1404 **
1405 ** This object is used in only one place in the SQLite interface.
1406 ** A pointer to an instance of this object is the argument to
1407 ** [sqlite3_config()] when the configuration option is
1408 ** [SQLITE_CONFIG_MALLOC] or [SQLITE_CONFIG_GETMALLOC].
1409 ** By creating an instance of this object
1410 ** and passing it to [sqlite3_config]([SQLITE_CONFIG_MALLOC])
1411 ** during configuration, an application can specify an alternative
1412 ** memory allocation subsystem for SQLite to use for all of its
1413 ** dynamic memory needs.
1414 **
1415 ** Note that SQLite comes with several [built-in memory allocators]
1416 ** that are perfectly adequate for the overwhelming majority of applications
1417 ** and that this object is only useful to a tiny minority of applications
1418 ** with specialized memory allocation requirements.  This object is
1419 ** also used during testing of SQLite in order to specify an alternative
1420 ** memory allocator that simulates memory out-of-memory conditions in
1421 ** order to verify that SQLite recovers gracefully from such
1422 ** conditions.
1423 **
1424 ** The xMalloc, xRealloc, and xFree methods must work like the
1425 ** malloc(), realloc() and free() functions from the standard C library.
1426 ** ^SQLite guarantees that the second argument to
1427 ** xRealloc is always a value returned by a prior call to xRoundup.
1428 **
1429 ** xSize should return the allocated size of a memory allocation
1430 ** previously obtained from xMalloc or xRealloc.  The allocated size
1431 ** is always at least as big as the requested size but may be larger.
1432 **
1433 ** The xRoundup method returns what would be the allocated size of
1434 ** a memory allocation given a particular requested size.  Most memory
1435 ** allocators round up memory allocations at least to the next multiple
1436 ** of 8.  Some allocators round up to a larger multiple or to a power of 2.
1437 ** Every memory allocation request coming in through [sqlite3_malloc()]
1438 ** or [sqlite3_realloc()] first calls xRoundup.  If xRoundup returns 0,
1439 ** that causes the corresponding memory allocation to fail.
1440 **
1441 ** The xInit method initializes the memory allocator.  For example,
1442 ** it might allocate any require mutexes or initialize internal data
1443 ** structures.  The xShutdown method is invoked (indirectly) by
1444 ** [sqlite3_shutdown()] and should deallocate any resources acquired
1445 ** by xInit.  The pAppData pointer is used as the only parameter to
1446 ** xInit and xShutdown.
1447 **
1448 ** SQLite holds the [SQLITE_MUTEX_STATIC_MASTER] mutex when it invokes
1449 ** the xInit method, so the xInit method need not be threadsafe.  The
1450 ** xShutdown method is only called from [sqlite3_shutdown()] so it does
1451 ** not need to be threadsafe either.  For all other methods, SQLite
1452 ** holds the [SQLITE_MUTEX_STATIC_MEM] mutex as long as the
1453 ** [SQLITE_CONFIG_MEMSTATUS] configuration option is turned on (which
1454 ** it is by default) and so the methods are automatically serialized.
1455 ** However, if [SQLITE_CONFIG_MEMSTATUS] is disabled, then the other
1456 ** methods must be threadsafe or else make their own arrangements for
1457 ** serialization.
1458 **
1459 ** SQLite will never invoke xInit() more than once without an intervening
1460 ** call to xShutdown().
1461 */
1462 typedef struct sqlite3_mem_methods sqlite3_mem_methods;
1463 struct sqlite3_mem_methods {
1464   void *(*xMalloc)(int);         /* Memory allocation function */
1465   void (*xFree)(void*);          /* Free a prior allocation */
1466   void *(*xRealloc)(void*,int);  /* Resize an allocation */
1467   int (*xSize)(void*);           /* Return the size of an allocation */
1468   int (*xRoundup)(int);          /* Round up request size to allocation size */
1469   int (*xInit)(void*);           /* Initialize the memory allocator */
1470   void (*xShutdown)(void*);      /* Deinitialize the memory allocator */
1471   void *pAppData;                /* Argument to xInit() and xShutdown() */
1472 };
1473 
1474 /*
1475 ** CAPI3REF: Configuration Options
1476 ** KEYWORDS: {configuration option}
1477 **
1478 ** These constants are the available integer configuration options that
1479 ** can be passed as the first argument to the [sqlite3_config()] interface.
1480 **
1481 ** New configuration options may be added in future releases of SQLite.
1482 ** Existing configuration options might be discontinued.  Applications
1483 ** should check the return code from [sqlite3_config()] to make sure that
1484 ** the call worked.  The [sqlite3_config()] interface will return a
1485 ** non-zero [error code] if a discontinued or unsupported configuration option
1486 ** is invoked.
1487 **
1488 ** <dl>
1489 ** [[SQLITE_CONFIG_SINGLETHREAD]] <dt>SQLITE_CONFIG_SINGLETHREAD</dt>
1490 ** <dd>There are no arguments to this option.  ^This option sets the
1491 ** [threading mode] to Single-thread.  In other words, it disables
1492 ** all mutexing and puts SQLite into a mode where it can only be used
1493 ** by a single thread.   ^If SQLite is compiled with
1494 ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
1495 ** it is not possible to change the [threading mode] from its default
1496 ** value of Single-thread and so [sqlite3_config()] will return
1497 ** [SQLITE_ERROR] if called with the SQLITE_CONFIG_SINGLETHREAD
1498 ** configuration option.</dd>
1499 **
1500 ** [[SQLITE_CONFIG_MULTITHREAD]] <dt>SQLITE_CONFIG_MULTITHREAD</dt>
1501 ** <dd>There are no arguments to this option.  ^This option sets the
1502 ** [threading mode] to Multi-thread.  In other words, it disables
1503 ** mutexing on [database connection] and [prepared statement] objects.
1504 ** The application is responsible for serializing access to
1505 ** [database connections] and [prepared statements].  But other mutexes
1506 ** are enabled so that SQLite will be safe to use in a multi-threaded
1507 ** environment as long as no two threads attempt to use the same
1508 ** [database connection] at the same time.  ^If SQLite is compiled with
1509 ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
1510 ** it is not possible to set the Multi-thread [threading mode] and
1511 ** [sqlite3_config()] will return [SQLITE_ERROR] if called with the
1512 ** SQLITE_CONFIG_MULTITHREAD configuration option.</dd>
1513 **
1514 ** [[SQLITE_CONFIG_SERIALIZED]] <dt>SQLITE_CONFIG_SERIALIZED</dt>
1515 ** <dd>There are no arguments to this option.  ^This option sets the
1516 ** [threading mode] to Serialized. In other words, this option enables
1517 ** all mutexes including the recursive
1518 ** mutexes on [database connection] and [prepared statement] objects.
1519 ** In this mode (which is the default when SQLite is compiled with
1520 ** [SQLITE_THREADSAFE=1]) the SQLite library will itself serialize access
1521 ** to [database connections] and [prepared statements] so that the
1522 ** application is free to use the same [database connection] or the
1523 ** same [prepared statement] in different threads at the same time.
1524 ** ^If SQLite is compiled with
1525 ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
1526 ** it is not possible to set the Serialized [threading mode] and
1527 ** [sqlite3_config()] will return [SQLITE_ERROR] if called with the
1528 ** SQLITE_CONFIG_SERIALIZED configuration option.</dd>
1529 **
1530 ** [[SQLITE_CONFIG_MALLOC]] <dt>SQLITE_CONFIG_MALLOC</dt>
1531 ** <dd> ^(This option takes a single argument which is a pointer to an
1532 ** instance of the [sqlite3_mem_methods] structure.  The argument specifies
1533 ** alternative low-level memory allocation routines to be used in place of
1534 ** the memory allocation routines built into SQLite.)^ ^SQLite makes
1535 ** its own private copy of the content of the [sqlite3_mem_methods] structure
1536 ** before the [sqlite3_config()] call returns.</dd>
1537 **
1538 ** [[SQLITE_CONFIG_GETMALLOC]] <dt>SQLITE_CONFIG_GETMALLOC</dt>
1539 ** <dd> ^(This option takes a single argument which is a pointer to an
1540 ** instance of the [sqlite3_mem_methods] structure.  The [sqlite3_mem_methods]
1541 ** structure is filled with the currently defined memory allocation routines.)^
1542 ** This option can be used to overload the default memory allocation
1543 ** routines with a wrapper that simulations memory allocation failure or
1544 ** tracks memory usage, for example. </dd>
1545 **
1546 ** [[SQLITE_CONFIG_MEMSTATUS]] <dt>SQLITE_CONFIG_MEMSTATUS</dt>
1547 ** <dd> ^This option takes single argument of type int, interpreted as a
1548 ** boolean, which enables or disables the collection of memory allocation
1549 ** statistics. ^(When memory allocation statistics are disabled, the
1550 ** following SQLite interfaces become non-operational:
1551 **   <ul>
1552 **   <li> [sqlite3_memory_used()]
1553 **   <li> [sqlite3_memory_highwater()]
1554 **   <li> [sqlite3_soft_heap_limit64()]
1555 **   <li> [sqlite3_status()]
1556 **   </ul>)^
1557 ** ^Memory allocation statistics are enabled by default unless SQLite is
1558 ** compiled with [SQLITE_DEFAULT_MEMSTATUS]=0 in which case memory
1559 ** allocation statistics are disabled by default.
1560 ** </dd>
1561 **
1562 ** [[SQLITE_CONFIG_SCRATCH]] <dt>SQLITE_CONFIG_SCRATCH</dt>
1563 ** <dd> ^This option specifies a static memory buffer that SQLite can use for
1564 ** scratch memory.  There are three arguments:  A pointer an 8-byte
1565 ** aligned memory buffer from which the scratch allocations will be
1566 ** drawn, the size of each scratch allocation (sz),
1567 ** and the maximum number of scratch allocations (N).  The sz
1568 ** argument must be a multiple of 16.
1569 ** The first argument must be a pointer to an 8-byte aligned buffer
1570 ** of at least sz*N bytes of memory.
1571 ** ^SQLite will use no more than two scratch buffers per thread.  So
1572 ** N should be set to twice the expected maximum number of threads.
1573 ** ^SQLite will never require a scratch buffer that is more than 6
1574 ** times the database page size. ^If SQLite needs needs additional
1575 ** scratch memory beyond what is provided by this configuration option, then
1576 ** [sqlite3_malloc()] will be used to obtain the memory needed.</dd>
1577 **
1578 ** [[SQLITE_CONFIG_PAGECACHE]] <dt>SQLITE_CONFIG_PAGECACHE</dt>
1579 ** <dd> ^This option specifies a static memory buffer that SQLite can use for
1580 ** the database page cache with the default page cache implementation.
1581 ** This configuration should not be used if an application-define page
1582 ** cache implementation is loaded using the SQLITE_CONFIG_PCACHE2 option.
1583 ** There are three arguments to this option: A pointer to 8-byte aligned
1584 ** memory, the size of each page buffer (sz), and the number of pages (N).
1585 ** The sz argument should be the size of the largest database page
1586 ** (a power of two between 512 and 32768) plus a little extra for each
1587 ** page header.  ^The page header size is 20 to 40 bytes depending on
1588 ** the host architecture.  ^It is harmless, apart from the wasted memory,
1589 ** to make sz a little too large.  The first
1590 ** argument should point to an allocation of at least sz*N bytes of memory.
1591 ** ^SQLite will use the memory provided by the first argument to satisfy its
1592 ** memory needs for the first N pages that it adds to cache.  ^If additional
1593 ** page cache memory is needed beyond what is provided by this option, then
1594 ** SQLite goes to [sqlite3_malloc()] for the additional storage space.
1595 ** The pointer in the first argument must
1596 ** be aligned to an 8-byte boundary or subsequent behavior of SQLite
1597 ** will be undefined.</dd>
1598 **
1599 ** [[SQLITE_CONFIG_HEAP]] <dt>SQLITE_CONFIG_HEAP</dt>
1600 ** <dd> ^This option specifies a static memory buffer that SQLite will use
1601 ** for all of its dynamic memory allocation needs beyond those provided
1602 ** for by [SQLITE_CONFIG_SCRATCH] and [SQLITE_CONFIG_PAGECACHE].
1603 ** There are three arguments: An 8-byte aligned pointer to the memory,
1604 ** the number of bytes in the memory buffer, and the minimum allocation size.
1605 ** ^If the first pointer (the memory pointer) is NULL, then SQLite reverts
1606 ** to using its default memory allocator (the system malloc() implementation),
1607 ** undoing any prior invocation of [SQLITE_CONFIG_MALLOC].  ^If the
1608 ** memory pointer is not NULL and either [SQLITE_ENABLE_MEMSYS3] or
1609 ** [SQLITE_ENABLE_MEMSYS5] are defined, then the alternative memory
1610 ** allocator is engaged to handle all of SQLites memory allocation needs.
1611 ** The first pointer (the memory pointer) must be aligned to an 8-byte
1612 ** boundary or subsequent behavior of SQLite will be undefined.
1613 ** The minimum allocation size is capped at 2**12. Reasonable values
1614 ** for the minimum allocation size are 2**5 through 2**8.</dd>
1615 **
1616 ** [[SQLITE_CONFIG_MUTEX]] <dt>SQLITE_CONFIG_MUTEX</dt>
1617 ** <dd> ^(This option takes a single argument which is a pointer to an
1618 ** instance of the [sqlite3_mutex_methods] structure.  The argument specifies
1619 ** alternative low-level mutex routines to be used in place
1620 ** the mutex routines built into SQLite.)^  ^SQLite makes a copy of the
1621 ** content of the [sqlite3_mutex_methods] structure before the call to
1622 ** [sqlite3_config()] returns. ^If SQLite is compiled with
1623 ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
1624 ** the entire mutexing subsystem is omitted from the build and hence calls to
1625 ** [sqlite3_config()] with the SQLITE_CONFIG_MUTEX configuration option will
1626 ** return [SQLITE_ERROR].</dd>
1627 **
1628 ** [[SQLITE_CONFIG_GETMUTEX]] <dt>SQLITE_CONFIG_GETMUTEX</dt>
1629 ** <dd> ^(This option takes a single argument which is a pointer to an
1630 ** instance of the [sqlite3_mutex_methods] structure.  The
1631 ** [sqlite3_mutex_methods]
1632 ** structure is filled with the currently defined mutex routines.)^
1633 ** This option can be used to overload the default mutex allocation
1634 ** routines with a wrapper used to track mutex usage for performance
1635 ** profiling or testing, for example.   ^If SQLite is compiled with
1636 ** the [SQLITE_THREADSAFE | SQLITE_THREADSAFE=0] compile-time option then
1637 ** the entire mutexing subsystem is omitted from the build and hence calls to
1638 ** [sqlite3_config()] with the SQLITE_CONFIG_GETMUTEX configuration option will
1639 ** return [SQLITE_ERROR].</dd>
1640 **
1641 ** [[SQLITE_CONFIG_LOOKASIDE]] <dt>SQLITE_CONFIG_LOOKASIDE</dt>
1642 ** <dd> ^(This option takes two arguments that determine the default
1643 ** memory allocation for the lookaside memory allocator on each
1644 ** [database connection].  The first argument is the
1645 ** size of each lookaside buffer slot and the second is the number of
1646 ** slots allocated to each database connection.)^  ^(This option sets the
1647 ** <i>default</i> lookaside size. The [SQLITE_DBCONFIG_LOOKASIDE]
1648 ** verb to [sqlite3_db_config()] can be used to change the lookaside
1649 ** configuration on individual connections.)^ </dd>
1650 **
1651 ** [[SQLITE_CONFIG_PCACHE2]] <dt>SQLITE_CONFIG_PCACHE2</dt>
1652 ** <dd> ^(This option takes a single argument which is a pointer to
1653 ** an [sqlite3_pcache_methods2] object.  This object specifies the interface
1654 ** to a custom page cache implementation.)^  ^SQLite makes a copy of the
1655 ** object and uses it for page cache memory allocations.</dd>
1656 **
1657 ** [[SQLITE_CONFIG_GETPCACHE2]] <dt>SQLITE_CONFIG_GETPCACHE2</dt>
1658 ** <dd> ^(This option takes a single argument which is a pointer to an
1659 ** [sqlite3_pcache_methods2] object.  SQLite copies of the current
1660 ** page cache implementation into that object.)^ </dd>
1661 **
1662 ** [[SQLITE_CONFIG_LOG]] <dt>SQLITE_CONFIG_LOG</dt>
1663 ** <dd> The SQLITE_CONFIG_LOG option is used to configure the SQLite
1664 ** global [error log].
1665 ** (^The SQLITE_CONFIG_LOG option takes two arguments: a pointer to a
1666 ** function with a call signature of void(*)(void*,int,const char*),
1667 ** and a pointer to void. ^If the function pointer is not NULL, it is
1668 ** invoked by [sqlite3_log()] to process each logging event.  ^If the
1669 ** function pointer is NULL, the [sqlite3_log()] interface becomes a no-op.
1670 ** ^The void pointer that is the second argument to SQLITE_CONFIG_LOG is
1671 ** passed through as the first parameter to the application-defined logger
1672 ** function whenever that function is invoked.  ^The second parameter to
1673 ** the logger function is a copy of the first parameter to the corresponding
1674 ** [sqlite3_log()] call and is intended to be a [result code] or an
1675 ** [extended result code].  ^The third parameter passed to the logger is
1676 ** log message after formatting via [sqlite3_snprintf()].
1677 ** The SQLite logging interface is not reentrant; the logger function
1678 ** supplied by the application must not invoke any SQLite interface.
1679 ** In a multi-threaded application, the application-defined logger
1680 ** function must be threadsafe. </dd>
1681 **
1682 ** [[SQLITE_CONFIG_URI]] <dt>SQLITE_CONFIG_URI
1683 ** <dd>^(This option takes a single argument of type int. If non-zero, then
1684 ** URI handling is globally enabled. If the parameter is zero, then URI handling
1685 ** is globally disabled.)^ ^If URI handling is globally enabled, all filenames
1686 ** passed to [sqlite3_open()], [sqlite3_open_v2()], [sqlite3_open16()] or
1687 ** specified as part of [ATTACH] commands are interpreted as URIs, regardless
1688 ** of whether or not the [SQLITE_OPEN_URI] flag is set when the database
1689 ** connection is opened. ^If it is globally disabled, filenames are
1690 ** only interpreted as URIs if the SQLITE_OPEN_URI flag is set when the
1691 ** database connection is opened. ^(By default, URI handling is globally
1692 ** disabled. The default value may be changed by compiling with the
1693 ** [SQLITE_USE_URI] symbol defined.)^
1694 **
1695 ** [[SQLITE_CONFIG_COVERING_INDEX_SCAN]] <dt>SQLITE_CONFIG_COVERING_INDEX_SCAN
1696 ** <dd>^This option takes a single integer argument which is interpreted as
1697 ** a boolean in order to enable or disable the use of covering indices for
1698 ** full table scans in the query optimizer.  ^The default setting is determined
1699 ** by the [SQLITE_ALLOW_COVERING_INDEX_SCAN] compile-time option, or is "on"
1700 ** if that compile-time option is omitted.
1701 ** The ability to disable the use of covering indices for full table scans
1702 ** is because some incorrectly coded legacy applications might malfunction
1703 ** when the optimization is enabled.  Providing the ability to
1704 ** disable the optimization allows the older, buggy application code to work
1705 ** without change even with newer versions of SQLite.
1706 **
1707 ** [[SQLITE_CONFIG_PCACHE]] [[SQLITE_CONFIG_GETPCACHE]]
1708 ** <dt>SQLITE_CONFIG_PCACHE and SQLITE_CONFIG_GETPCACHE
1709 ** <dd> These options are obsolete and should not be used by new code.
1710 ** They are retained for backwards compatibility but are now no-ops.
1711 ** </dd>
1712 **
1713 ** [[SQLITE_CONFIG_SQLLOG]]
1714 ** <dt>SQLITE_CONFIG_SQLLOG
1715 ** <dd>This option is only available if sqlite is compiled with the
1716 ** [SQLITE_ENABLE_SQLLOG] pre-processor macro defined. The first argument should
1717 ** be a pointer to a function of type void(*)(void*,sqlite3*,const char*, int).
1718 ** The second should be of type (void*). The callback is invoked by the library
1719 ** in three separate circumstances, identified by the value passed as the
1720 ** fourth parameter. If the fourth parameter is 0, then the database connection
1721 ** passed as the second argument has just been opened. The third argument
1722 ** points to a buffer containing the name of the main database file. If the
1723 ** fourth parameter is 1, then the SQL statement that the third parameter
1724 ** points to has just been executed. Or, if the fourth parameter is 2, then
1725 ** the connection being passed as the second parameter is being closed. The
1726 ** third parameter is passed NULL In this case.  An example of using this
1727 ** configuration option can be seen in the "test_sqllog.c" source file in
1728 ** the canonical SQLite source tree.</dd>
1729 **
1730 ** [[SQLITE_CONFIG_MMAP_SIZE]]
1731 ** <dt>SQLITE_CONFIG_MMAP_SIZE
1732 ** <dd>^SQLITE_CONFIG_MMAP_SIZE takes two 64-bit integer (sqlite3_int64) values
1733 ** that are the default mmap size limit (the default setting for
1734 ** [PRAGMA mmap_size]) and the maximum allowed mmap size limit.
1735 ** ^The default setting can be overridden by each database connection using
1736 ** either the [PRAGMA mmap_size] command, or by using the
1737 ** [SQLITE_FCNTL_MMAP_SIZE] file control.  ^(The maximum allowed mmap size
1738 ** cannot be changed at run-time.  Nor may the maximum allowed mmap size
1739 ** exceed the compile-time maximum mmap size set by the
1740 ** [SQLITE_MAX_MMAP_SIZE] compile-time option.)^
1741 ** ^If either argument to this option is negative, then that argument is
1742 ** changed to its compile-time default.
1743 **
1744 ** [[SQLITE_CONFIG_WIN32_HEAPSIZE]]
1745 ** <dt>SQLITE_CONFIG_WIN32_HEAPSIZE
1746 ** <dd>^This option is only available if SQLite is compiled for Windows
1747 ** with the [SQLITE_WIN32_MALLOC] pre-processor macro defined.
1748 ** SQLITE_CONFIG_WIN32_HEAPSIZE takes a 32-bit unsigned integer value
1749 ** that specifies the maximum size of the created heap.
1750 ** </dl>
1751 */
1752 #define SQLITE_CONFIG_SINGLETHREAD  1  /* nil */
1753 #define SQLITE_CONFIG_MULTITHREAD   2  /* nil */
1754 #define SQLITE_CONFIG_SERIALIZED    3  /* nil */
1755 #define SQLITE_CONFIG_MALLOC        4  /* sqlite3_mem_methods* */
1756 #define SQLITE_CONFIG_GETMALLOC     5  /* sqlite3_mem_methods* */
1757 #define SQLITE_CONFIG_SCRATCH       6  /* void*, int sz, int N */
1758 #define SQLITE_CONFIG_PAGECACHE     7  /* void*, int sz, int N */
1759 #define SQLITE_CONFIG_HEAP          8  /* void*, int nByte, int min */
1760 #define SQLITE_CONFIG_MEMSTATUS     9  /* boolean */
1761 #define SQLITE_CONFIG_MUTEX        10  /* sqlite3_mutex_methods* */
1762 #define SQLITE_CONFIG_GETMUTEX     11  /* sqlite3_mutex_methods* */
1763 /* previously SQLITE_CONFIG_CHUNKALLOC 12 which is now unused. */
1764 #define SQLITE_CONFIG_LOOKASIDE    13  /* int int */
1765 #define SQLITE_CONFIG_PCACHE       14  /* no-op */
1766 #define SQLITE_CONFIG_GETPCACHE    15  /* no-op */
1767 #define SQLITE_CONFIG_LOG          16  /* xFunc, void* */
1768 #define SQLITE_CONFIG_URI          17  /* int */
1769 #define SQLITE_CONFIG_PCACHE2      18  /* sqlite3_pcache_methods2* */
1770 #define SQLITE_CONFIG_GETPCACHE2   19  /* sqlite3_pcache_methods2* */
1771 #define SQLITE_CONFIG_COVERING_INDEX_SCAN 20  /* int */
1772 #define SQLITE_CONFIG_SQLLOG       21  /* xSqllog, void* */
1773 #define SQLITE_CONFIG_MMAP_SIZE    22  /* sqlite3_int64, sqlite3_int64 */
1774 #define SQLITE_CONFIG_WIN32_HEAPSIZE      23  /* int nByte */
1775 
1776 /*
1777 ** CAPI3REF: Database Connection Configuration Options
1778 **
1779 ** These constants are the available integer configuration options that
1780 ** can be passed as the second argument to the [sqlite3_db_config()] interface.
1781 **
1782 ** New configuration options may be added in future releases of SQLite.
1783 ** Existing configuration options might be discontinued.  Applications
1784 ** should check the return code from [sqlite3_db_config()] to make sure that
1785 ** the call worked.  ^The [sqlite3_db_config()] interface will return a
1786 ** non-zero [error code] if a discontinued or unsupported configuration option
1787 ** is invoked.
1788 **
1789 ** <dl>
1790 ** <dt>SQLITE_DBCONFIG_LOOKASIDE</dt>
1791 ** <dd> ^This option takes three additional arguments that determine the
1792 ** [lookaside memory allocator] configuration for the [database connection].
1793 ** ^The first argument (the third parameter to [sqlite3_db_config()] is a
1794 ** pointer to a memory buffer to use for lookaside memory.
1795 ** ^The first argument after the SQLITE_DBCONFIG_LOOKASIDE verb
1796 ** may be NULL in which case SQLite will allocate the
1797 ** lookaside buffer itself using [sqlite3_malloc()]. ^The second argument is the
1798 ** size of each lookaside buffer slot.  ^The third argument is the number of
1799 ** slots.  The size of the buffer in the first argument must be greater than
1800 ** or equal to the product of the second and third arguments.  The buffer
1801 ** must be aligned to an 8-byte boundary.  ^If the second argument to
1802 ** SQLITE_DBCONFIG_LOOKASIDE is not a multiple of 8, it is internally
1803 ** rounded down to the next smaller multiple of 8.  ^(The lookaside memory
1804 ** configuration for a database connection can only be changed when that
1805 ** connection is not currently using lookaside memory, or in other words
1806 ** when the "current value" returned by
1807 ** [sqlite3_db_status](D,[SQLITE_CONFIG_LOOKASIDE],...) is zero.
1808 ** Any attempt to change the lookaside memory configuration when lookaside
1809 ** memory is in use leaves the configuration unchanged and returns
1810 ** [SQLITE_BUSY].)^</dd>
1811 **
1812 ** <dt>SQLITE_DBCONFIG_ENABLE_FKEY</dt>
1813 ** <dd> ^This option is used to enable or disable the enforcement of
1814 ** [foreign key constraints].  There should be two additional arguments.
1815 ** The first argument is an integer which is 0 to disable FK enforcement,
1816 ** positive to enable FK enforcement or negative to leave FK enforcement
1817 ** unchanged.  The second parameter is a pointer to an integer into which
1818 ** is written 0 or 1 to indicate whether FK enforcement is off or on
1819 ** following this call.  The second parameter may be a NULL pointer, in
1820 ** which case the FK enforcement setting is not reported back. </dd>
1821 **
1822 ** <dt>SQLITE_DBCONFIG_ENABLE_TRIGGER</dt>
1823 ** <dd> ^This option is used to enable or disable [CREATE TRIGGER | triggers].
1824 ** There should be two additional arguments.
1825 ** The first argument is an integer which is 0 to disable triggers,
1826 ** positive to enable triggers or negative to leave the setting unchanged.
1827 ** The second parameter is a pointer to an integer into which
1828 ** is written 0 or 1 to indicate whether triggers are disabled or enabled
1829 ** following this call.  The second parameter may be a NULL pointer, in
1830 ** which case the trigger setting is not reported back. </dd>
1831 **
1832 ** </dl>
1833 */
1834 #define SQLITE_DBCONFIG_LOOKASIDE       1001  /* void* int int */
1835 #define SQLITE_DBCONFIG_ENABLE_FKEY     1002  /* int int* */
1836 #define SQLITE_DBCONFIG_ENABLE_TRIGGER  1003  /* int int* */
1837 
1838 
1839 /*
1840 ** CAPI3REF: Enable Or Disable Extended Result Codes
1841 **
1842 ** ^The sqlite3_extended_result_codes() routine enables or disables the
1843 ** [extended result codes] feature of SQLite. ^The extended result
1844 ** codes are disabled by default for historical compatibility.
1845 */
1846 SQLITE_API int sqlite3_extended_result_codes(sqlite3*, int onoff);
1847 
1848 /*
1849 ** CAPI3REF: Last Insert Rowid
1850 **
1851 ** ^Each entry in most SQLite tables (except for [WITHOUT ROWID] tables)
1852 ** has a unique 64-bit signed
1853 ** integer key called the [ROWID | "rowid"]. ^The rowid is always available
1854 ** as an undeclared column named ROWID, OID, or _ROWID_ as long as those
1855 ** names are not also used by explicitly declared columns. ^If
1856 ** the table has a column of type [INTEGER PRIMARY KEY] then that column
1857 ** is another alias for the rowid.
1858 **
1859 ** ^The sqlite3_last_insert_rowid(D) interface returns the [rowid] of the
1860 ** most recent successful [INSERT] into a rowid table or [virtual table]
1861 ** on database connection D.
1862 ** ^Inserts into [WITHOUT ROWID] tables are not recorded.
1863 ** ^If no successful [INSERT]s into rowid tables
1864 ** have ever occurred on the database connection D,
1865 ** then sqlite3_last_insert_rowid(D) returns zero.
1866 **
1867 ** ^(If an [INSERT] occurs within a trigger or within a [virtual table]
1868 ** method, then this routine will return the [rowid] of the inserted
1869 ** row as long as the trigger or virtual table method is running.
1870 ** But once the trigger or virtual table method ends, the value returned
1871 ** by this routine reverts to what it was before the trigger or virtual
1872 ** table method began.)^
1873 **
1874 ** ^An [INSERT] that fails due to a constraint violation is not a
1875 ** successful [INSERT] and does not change the value returned by this
1876 ** routine.  ^Thus INSERT OR FAIL, INSERT OR IGNORE, INSERT OR ROLLBACK,
1877 ** and INSERT OR ABORT make no changes to the return value of this
1878 ** routine when their insertion fails.  ^(When INSERT OR REPLACE
1879 ** encounters a constraint violation, it does not fail.  The
1880 ** INSERT continues to completion after deleting rows that caused
1881 ** the constraint problem so INSERT OR REPLACE will always change
1882 ** the return value of this interface.)^
1883 **
1884 ** ^For the purposes of this routine, an [INSERT] is considered to
1885 ** be successful even if it is subsequently rolled back.
1886 **
1887 ** This function is accessible to SQL statements via the
1888 ** [last_insert_rowid() SQL function].
1889 **
1890 ** If a separate thread performs a new [INSERT] on the same
1891 ** database connection while the [sqlite3_last_insert_rowid()]
1892 ** function is running and thus changes the last insert [rowid],
1893 ** then the value returned by [sqlite3_last_insert_rowid()] is
1894 ** unpredictable and might not equal either the old or the new
1895 ** last insert [rowid].
1896 */
1897 SQLITE_API sqlite3_int64 sqlite3_last_insert_rowid(sqlite3*);
1898 
1899 /*
1900 ** CAPI3REF: Count The Number Of Rows Modified
1901 **
1902 ** ^This function returns the number of database rows that were changed
1903 ** or inserted or deleted by the most recently completed SQL statement
1904 ** on the [database connection] specified by the first parameter.
1905 ** ^(Only changes that are directly specified by the [INSERT], [UPDATE],
1906 ** or [DELETE] statement are counted.  Auxiliary changes caused by
1907 ** triggers or [foreign key actions] are not counted.)^ Use the
1908 ** [sqlite3_total_changes()] function to find the total number of changes
1909 ** including changes caused by triggers and foreign key actions.
1910 **
1911 ** ^Changes to a view that are simulated by an [INSTEAD OF trigger]
1912 ** are not counted.  Only real table changes are counted.
1913 **
1914 ** ^(A "row change" is a change to a single row of a single table
1915 ** caused by an INSERT, DELETE, or UPDATE statement.  Rows that
1916 ** are changed as side effects of [REPLACE] constraint resolution,
1917 ** rollback, ABORT processing, [DROP TABLE], or by any other
1918 ** mechanisms do not count as direct row changes.)^
1919 **
1920 ** A "trigger context" is a scope of execution that begins and
1921 ** ends with the script of a [CREATE TRIGGER | trigger].
1922 ** Most SQL statements are
1923 ** evaluated outside of any trigger.  This is the "top level"
1924 ** trigger context.  If a trigger fires from the top level, a
1925 ** new trigger context is entered for the duration of that one
1926 ** trigger.  Subtriggers create subcontexts for their duration.
1927 **
1928 ** ^Calling [sqlite3_exec()] or [sqlite3_step()] recursively does
1929 ** not create a new trigger context.
1930 **
1931 ** ^This function returns the number of direct row changes in the
1932 ** most recent INSERT, UPDATE, or DELETE statement within the same
1933 ** trigger context.
1934 **
1935 ** ^Thus, when called from the top level, this function returns the
1936 ** number of changes in the most recent INSERT, UPDATE, or DELETE
1937 ** that also occurred at the top level.  ^(Within the body of a trigger,
1938 ** the sqlite3_changes() interface can be called to find the number of
1939 ** changes in the most recently completed INSERT, UPDATE, or DELETE
1940 ** statement within the body of the same trigger.
1941 ** However, the number returned does not include changes
1942 ** caused by subtriggers since those have their own context.)^
1943 **
1944 ** See also the [sqlite3_total_changes()] interface, the
1945 ** [count_changes pragma], and the [changes() SQL function].
1946 **
1947 ** If a separate thread makes changes on the same database connection
1948 ** while [sqlite3_changes()] is running then the value returned
1949 ** is unpredictable and not meaningful.
1950 */
1951 SQLITE_API int sqlite3_changes(sqlite3*);
1952 
1953 /*
1954 ** CAPI3REF: Total Number Of Rows Modified
1955 **
1956 ** ^This function returns the number of row changes caused by [INSERT],
1957 ** [UPDATE] or [DELETE] statements since the [database connection] was opened.
1958 ** ^(The count returned by sqlite3_total_changes() includes all changes
1959 ** from all [CREATE TRIGGER | trigger] contexts and changes made by
1960 ** [foreign key actions]. However,
1961 ** the count does not include changes used to implement [REPLACE] constraints,
1962 ** do rollbacks or ABORT processing, or [DROP TABLE] processing.  The
1963 ** count does not include rows of views that fire an [INSTEAD OF trigger],
1964 ** though if the INSTEAD OF trigger makes changes of its own, those changes
1965 ** are counted.)^
1966 ** ^The sqlite3_total_changes() function counts the changes as soon as
1967 ** the statement that makes them is completed (when the statement handle
1968 ** is passed to [sqlite3_reset()] or [sqlite3_finalize()]).
1969 **
1970 ** See also the [sqlite3_changes()] interface, the
1971 ** [count_changes pragma], and the [total_changes() SQL function].
1972 **
1973 ** If a separate thread makes changes on the same database connection
1974 ** while [sqlite3_total_changes()] is running then the value
1975 ** returned is unpredictable and not meaningful.
1976 */
1977 SQLITE_API int sqlite3_total_changes(sqlite3*);
1978 
1979 /*
1980 ** CAPI3REF: Interrupt A Long-Running Query
1981 **
1982 ** ^This function causes any pending database operation to abort and
1983 ** return at its earliest opportunity. This routine is typically
1984 ** called in response to a user action such as pressing "Cancel"
1985 ** or Ctrl-C where the user wants a long query operation to halt
1986 ** immediately.
1987 **
1988 ** ^It is safe to call this routine from a thread different from the
1989 ** thread that is currently running the database operation.  But it
1990 ** is not safe to call this routine with a [database connection] that
1991 ** is closed or might close before sqlite3_interrupt() returns.
1992 **
1993 ** ^If an SQL operation is very nearly finished at the time when
1994 ** sqlite3_interrupt() is called, then it might not have an opportunity
1995 ** to be interrupted and might continue to completion.
1996 **
1997 ** ^An SQL operation that is interrupted will return [SQLITE_INTERRUPT].
1998 ** ^If the interrupted SQL operation is an INSERT, UPDATE, or DELETE
1999 ** that is inside an explicit transaction, then the entire transaction
2000 ** will be rolled back automatically.
2001 **
2002 ** ^The sqlite3_interrupt(D) call is in effect until all currently running
2003 ** SQL statements on [database connection] D complete.  ^Any new SQL statements
2004 ** that are started after the sqlite3_interrupt() call and before the
2005 ** running statements reaches zero are interrupted as if they had been
2006 ** running prior to the sqlite3_interrupt() call.  ^New SQL statements
2007 ** that are started after the running statement count reaches zero are
2008 ** not effected by the sqlite3_interrupt().
2009 ** ^A call to sqlite3_interrupt(D) that occurs when there are no running
2010 ** SQL statements is a no-op and has no effect on SQL statements
2011 ** that are started after the sqlite3_interrupt() call returns.
2012 **
2013 ** If the database connection closes while [sqlite3_interrupt()]
2014 ** is running then bad things will likely happen.
2015 */
2016 SQLITE_API void sqlite3_interrupt(sqlite3*);
2017 
2018 /*
2019 ** CAPI3REF: Determine If An SQL Statement Is Complete
2020 **
2021 ** These routines are useful during command-line input to determine if the
2022 ** currently entered text seems to form a complete SQL statement or
2023 ** if additional input is needed before sending the text into
2024 ** SQLite for parsing.  ^These routines return 1 if the input string
2025 ** appears to be a complete SQL statement.  ^A statement is judged to be
2026 ** complete if it ends with a semicolon token and is not a prefix of a
2027 ** well-formed CREATE TRIGGER statement.  ^Semicolons that are embedded within
2028 ** string literals or quoted identifier names or comments are not
2029 ** independent tokens (they are part of the token in which they are
2030 ** embedded) and thus do not count as a statement terminator.  ^Whitespace
2031 ** and comments that follow the final semicolon are ignored.
2032 **
2033 ** ^These routines return 0 if the statement is incomplete.  ^If a
2034 ** memory allocation fails, then SQLITE_NOMEM is returned.
2035 **
2036 ** ^These routines do not parse the SQL statements thus
2037 ** will not detect syntactically incorrect SQL.
2038 **
2039 ** ^(If SQLite has not been initialized using [sqlite3_initialize()] prior
2040 ** to invoking sqlite3_complete16() then sqlite3_initialize() is invoked
2041 ** automatically by sqlite3_complete16().  If that initialization fails,
2042 ** then the return value from sqlite3_complete16() will be non-zero
2043 ** regardless of whether or not the input SQL is complete.)^
2044 **
2045 ** The input to [sqlite3_complete()] must be a zero-terminated
2046 ** UTF-8 string.
2047 **
2048 ** The input to [sqlite3_complete16()] must be a zero-terminated
2049 ** UTF-16 string in native byte order.
2050 */
2051 SQLITE_API int sqlite3_complete(const char *sql);
2052 SQLITE_API int sqlite3_complete16(const void *sql);
2053 
2054 /*
2055 ** CAPI3REF: Register A Callback To Handle SQLITE_BUSY Errors
2056 **
2057 ** ^This routine sets a callback function that might be invoked whenever
2058 ** an attempt is made to open a database table that another thread
2059 ** or process has locked.
2060 **
2061 ** ^If the busy callback is NULL, then [SQLITE_BUSY] or [SQLITE_IOERR_BLOCKED]
2062 ** is returned immediately upon encountering the lock.  ^If the busy callback
2063 ** is not NULL, then the callback might be invoked with two arguments.
2064 **
2065 ** ^The first argument to the busy handler is a copy of the void* pointer which
2066 ** is the third argument to sqlite3_busy_handler().  ^The second argument to
2067 ** the busy handler callback is the number of times that the busy handler has
2068 ** been invoked for this locking event.  ^If the
2069 ** busy callback returns 0, then no additional attempts are made to
2070 ** access the database and [SQLITE_BUSY] or [SQLITE_IOERR_BLOCKED] is returned.
2071 ** ^If the callback returns non-zero, then another attempt
2072 ** is made to open the database for reading and the cycle repeats.
2073 **
2074 ** The presence of a busy handler does not guarantee that it will be invoked
2075 ** when there is lock contention. ^If SQLite determines that invoking the busy
2076 ** handler could result in a deadlock, it will go ahead and return [SQLITE_BUSY]
2077 ** or [SQLITE_IOERR_BLOCKED] instead of invoking the busy handler.
2078 ** Consider a scenario where one process is holding a read lock that
2079 ** it is trying to promote to a reserved lock and
2080 ** a second process is holding a reserved lock that it is trying
2081 ** to promote to an exclusive lock.  The first process cannot proceed
2082 ** because it is blocked by the second and the second process cannot
2083 ** proceed because it is blocked by the first.  If both processes
2084 ** invoke the busy handlers, neither will make any progress.  Therefore,
2085 ** SQLite returns [SQLITE_BUSY] for the first process, hoping that this
2086 ** will induce the first process to release its read lock and allow
2087 ** the second process to proceed.
2088 **
2089 ** ^The default busy callback is NULL.
2090 **
2091 ** ^The [SQLITE_BUSY] error is converted to [SQLITE_IOERR_BLOCKED]
2092 ** when SQLite is in the middle of a large transaction where all the
2093 ** changes will not fit into the in-memory cache.  SQLite will
2094 ** already hold a RESERVED lock on the database file, but it needs
2095 ** to promote this lock to EXCLUSIVE so that it can spill cache
2096 ** pages into the database file without harm to concurrent
2097 ** readers.  ^If it is unable to promote the lock, then the in-memory
2098 ** cache will be left in an inconsistent state and so the error
2099 ** code is promoted from the relatively benign [SQLITE_BUSY] to
2100 ** the more severe [SQLITE_IOERR_BLOCKED].  ^This error code promotion
2101 ** forces an automatic rollback of the changes.  See the
2102 ** <a href="/cvstrac/wiki?p=CorruptionFollowingBusyError">
2103 ** CorruptionFollowingBusyError</a> wiki page for a discussion of why
2104 ** this is important.
2105 **
2106 ** ^(There can only be a single busy handler defined for each
2107 ** [database connection].  Setting a new busy handler clears any
2108 ** previously set handler.)^  ^Note that calling [sqlite3_busy_timeout()]
2109 ** will also set or clear the busy handler.
2110 **
2111 ** The busy callback should not take any actions which modify the
2112 ** database connection that invoked the busy handler.  Any such actions
2113 ** result in undefined behavior.
2114 **
2115 ** A busy handler must not close the database connection
2116 ** or [prepared statement] that invoked the busy handler.
2117 */
2118 SQLITE_API int sqlite3_busy_handler(sqlite3*, int(*)(void*,int), void*);
2119 
2120 /*
2121 ** CAPI3REF: Set A Busy Timeout
2122 **
2123 ** ^This routine sets a [sqlite3_busy_handler | busy handler] that sleeps
2124 ** for a specified amount of time when a table is locked.  ^The handler
2125 ** will sleep multiple times until at least "ms" milliseconds of sleeping
2126 ** have accumulated.  ^After at least "ms" milliseconds of sleeping,
2127 ** the handler returns 0 which causes [sqlite3_step()] to return
2128 ** [SQLITE_BUSY] or [SQLITE_IOERR_BLOCKED].
2129 **
2130 ** ^Calling this routine with an argument less than or equal to zero
2131 ** turns off all busy handlers.
2132 **
2133 ** ^(There can only be a single busy handler for a particular
2134 ** [database connection] any any given moment.  If another busy handler
2135 ** was defined  (using [sqlite3_busy_handler()]) prior to calling
2136 ** this routine, that other busy handler is cleared.)^
2137 */
2138 SQLITE_API int sqlite3_busy_timeout(sqlite3*, int ms);
2139 
2140 /*
2141 ** CAPI3REF: Convenience Routines For Running Queries
2142 **
2143 ** This is a legacy interface that is preserved for backwards compatibility.
2144 ** Use of this interface is not recommended.
2145 **
2146 ** Definition: A <b>result table</b> is memory data structure created by the
2147 ** [sqlite3_get_table()] interface.  A result table records the
2148 ** complete query results from one or more queries.
2149 **
2150 ** The table conceptually has a number of rows and columns.  But
2151 ** these numbers are not part of the result table itself.  These
2152 ** numbers are obtained separately.  Let N be the number of rows
2153 ** and M be the number of columns.
2154 **
2155 ** A result table is an array of pointers to zero-terminated UTF-8 strings.
2156 ** There are (N+1)*M elements in the array.  The first M pointers point
2157 ** to zero-terminated strings that  contain the names of the columns.
2158 ** The remaining entries all point to query results.  NULL values result
2159 ** in NULL pointers.  All other values are in their UTF-8 zero-terminated
2160 ** string representation as returned by [sqlite3_column_text()].
2161 **
2162 ** A result table might consist of one or more memory allocations.
2163 ** It is not safe to pass a result table directly to [sqlite3_free()].
2164 ** A result table should be deallocated using [sqlite3_free_table()].
2165 **
2166 ** ^(As an example of the result table format, suppose a query result
2167 ** is as follows:
2168 **
2169 ** <blockquote><pre>
2170 **        Name        | Age
2171 **        -----------------------
2172 **        Alice       | 43
2173 **        Bob         | 28
2174 **        Cindy       | 21
2175 ** </pre></blockquote>
2176 **
2177 ** There are two column (M==2) and three rows (N==3).  Thus the
2178 ** result table has 8 entries.  Suppose the result table is stored
2179 ** in an array names azResult.  Then azResult holds this content:
2180 **
2181 ** <blockquote><pre>
2182 **        azResult&#91;0] = "Name";
2183 **        azResult&#91;1] = "Age";
2184 **        azResult&#91;2] = "Alice";
2185 **        azResult&#91;3] = "43";
2186 **        azResult&#91;4] = "Bob";
2187 **        azResult&#91;5] = "28";
2188 **        azResult&#91;6] = "Cindy";
2189 **        azResult&#91;7] = "21";
2190 ** </pre></blockquote>)^
2191 **
2192 ** ^The sqlite3_get_table() function evaluates one or more
2193 ** semicolon-separated SQL statements in the zero-terminated UTF-8
2194 ** string of its 2nd parameter and returns a result table to the
2195 ** pointer given in its 3rd parameter.
2196 **
2197 ** After the application has finished with the result from sqlite3_get_table(),
2198 ** it must pass the result table pointer to sqlite3_free_table() in order to
2199 ** release the memory that was malloced.  Because of the way the
2200 ** [sqlite3_malloc()] happens within sqlite3_get_table(), the calling
2201 ** function must not try to call [sqlite3_free()] directly.  Only
2202 ** [sqlite3_free_table()] is able to release the memory properly and safely.
2203 **
2204 ** The sqlite3_get_table() interface is implemented as a wrapper around
2205 ** [sqlite3_exec()].  The sqlite3_get_table() routine does not have access
2206 ** to any internal data structures of SQLite.  It uses only the public
2207 ** interface defined here.  As a consequence, errors that occur in the
2208 ** wrapper layer outside of the internal [sqlite3_exec()] call are not
2209 ** reflected in subsequent calls to [sqlite3_errcode()] or
2210 ** [sqlite3_errmsg()].
2211 */
2212 SQLITE_API int sqlite3_get_table(
2213   sqlite3 *db,          /* An open database */
2214   const char *zSql,     /* SQL to be evaluated */
2215   char ***pazResult,    /* Results of the query */
2216   int *pnRow,           /* Number of result rows written here */
2217   int *pnColumn,        /* Number of result columns written here */
2218   char **pzErrmsg       /* Error msg written here */
2219 );
2220 SQLITE_API void sqlite3_free_table(char **result);
2221 
2222 /*
2223 ** CAPI3REF: Formatted String Printing Functions
2224 **
2225 ** These routines are work-alikes of the "printf()" family of functions
2226 ** from the standard C library.
2227 **
2228 ** ^The sqlite3_mprintf() and sqlite3_vmprintf() routines write their
2229 ** results into memory obtained from [sqlite3_malloc()].
2230 ** The strings returned by these two routines should be
2231 ** released by [sqlite3_free()].  ^Both routines return a
2232 ** NULL pointer if [sqlite3_malloc()] is unable to allocate enough
2233 ** memory to hold the resulting string.
2234 **
2235 ** ^(The sqlite3_snprintf() routine is similar to "snprintf()" from
2236 ** the standard C library.  The result is written into the
2237 ** buffer supplied as the second parameter whose size is given by
2238 ** the first parameter. Note that the order of the
2239 ** first two parameters is reversed from snprintf().)^  This is an
2240 ** historical accident that cannot be fixed without breaking
2241 ** backwards compatibility.  ^(Note also that sqlite3_snprintf()
2242 ** returns a pointer to its buffer instead of the number of
2243 ** characters actually written into the buffer.)^  We admit that
2244 ** the number of characters written would be a more useful return
2245 ** value but we cannot change the implementation of sqlite3_snprintf()
2246 ** now without breaking compatibility.
2247 **
2248 ** ^As long as the buffer size is greater than zero, sqlite3_snprintf()
2249 ** guarantees that the buffer is always zero-terminated.  ^The first
2250 ** parameter "n" is the total size of the buffer, including space for
2251 ** the zero terminator.  So the longest string that can be completely
2252 ** written will be n-1 characters.
2253 **
2254 ** ^The sqlite3_vsnprintf() routine is a varargs version of sqlite3_snprintf().
2255 **
2256 ** These routines all implement some additional formatting
2257 ** options that are useful for constructing SQL statements.
2258 ** All of the usual printf() formatting options apply.  In addition, there
2259 ** is are "%q", "%Q", and "%z" options.
2260 **
2261 ** ^(The %q option works like %s in that it substitutes a nul-terminated
2262 ** string from the argument list.  But %q also doubles every '\'' character.
2263 ** %q is designed for use inside a string literal.)^  By doubling each '\''
2264 ** character it escapes that character and allows it to be inserted into
2265 ** the string.
2266 **
2267 ** For example, assume the string variable zText contains text as follows:
2268 **
2269 ** <blockquote><pre>
2270 **  char *zText = "It's a happy day!";
2271 ** </pre></blockquote>
2272 **
2273 ** One can use this text in an SQL statement as follows:
2274 **
2275 ** <blockquote><pre>
2276 **  char *zSQL = sqlite3_mprintf("INSERT INTO table VALUES('%q')", zText);
2277 **  sqlite3_exec(db, zSQL, 0, 0, 0);
2278 **  sqlite3_free(zSQL);
2279 ** </pre></blockquote>
2280 **
2281 ** Because the %q format string is used, the '\'' character in zText
2282 ** is escaped and the SQL generated is as follows:
2283 **
2284 ** <blockquote><pre>
2285 **  INSERT INTO table1 VALUES('It''s a happy day!')
2286 ** </pre></blockquote>
2287 **
2288 ** This is correct.  Had we used %s instead of %q, the generated SQL
2289 ** would have looked like this:
2290 **
2291 ** <blockquote><pre>
2292 **  INSERT INTO table1 VALUES('It's a happy day!');
2293 ** </pre></blockquote>
2294 **
2295 ** This second example is an SQL syntax error.  As a general rule you should
2296 ** always use %q instead of %s when inserting text into a string literal.
2297 **
2298 ** ^(The %Q option works like %q except it also adds single quotes around
2299 ** the outside of the total string.  Additionally, if the parameter in the
2300 ** argument list is a NULL pointer, %Q substitutes the text "NULL" (without
2301 ** single quotes).)^  So, for example, one could say:
2302 **
2303 ** <blockquote><pre>
2304 **  char *zSQL = sqlite3_mprintf("INSERT INTO table VALUES(%Q)", zText);
2305 **  sqlite3_exec(db, zSQL, 0, 0, 0);
2306 **  sqlite3_free(zSQL);
2307 ** </pre></blockquote>
2308 **
2309 ** The code above will render a correct SQL statement in the zSQL
2310 ** variable even if the zText variable is a NULL pointer.
2311 **
2312 ** ^(The "%z" formatting option works like "%s" but with the
2313 ** addition that after the string has been read and copied into
2314 ** the result, [sqlite3_free()] is called on the input string.)^
2315 */
2316 SQLITE_API char *sqlite3_mprintf(const char*,...);
2317 SQLITE_API char *sqlite3_vmprintf(const char*, va_list);
2318 SQLITE_API char *sqlite3_snprintf(int,char*,const char*, ...);
2319 SQLITE_API char *sqlite3_vsnprintf(int,char*,const char*, va_list);
2320 
2321 /*
2322 ** CAPI3REF: Memory Allocation Subsystem
2323 **
2324 ** The SQLite core uses these three routines for all of its own
2325 ** internal memory allocation needs. "Core" in the previous sentence
2326 ** does not include operating-system specific VFS implementation.  The
2327 ** Windows VFS uses native malloc() and free() for some operations.
2328 **
2329 ** ^The sqlite3_malloc() routine returns a pointer to a block
2330 ** of memory at least N bytes in length, where N is the parameter.
2331 ** ^If sqlite3_malloc() is unable to obtain sufficient free
2332 ** memory, it returns a NULL pointer.  ^If the parameter N to
2333 ** sqlite3_malloc() is zero or negative then sqlite3_malloc() returns
2334 ** a NULL pointer.
2335 **
2336 ** ^Calling sqlite3_free() with a pointer previously returned
2337 ** by sqlite3_malloc() or sqlite3_realloc() releases that memory so
2338 ** that it might be reused.  ^The sqlite3_free() routine is
2339 ** a no-op if is called with a NULL pointer.  Passing a NULL pointer
2340 ** to sqlite3_free() is harmless.  After being freed, memory
2341 ** should neither be read nor written.  Even reading previously freed
2342 ** memory might result in a segmentation fault or other severe error.
2343 ** Memory corruption, a segmentation fault, or other severe error
2344 ** might result if sqlite3_free() is called with a non-NULL pointer that
2345 ** was not obtained from sqlite3_malloc() or sqlite3_realloc().
2346 **
2347 ** ^(The sqlite3_realloc() interface attempts to resize a
2348 ** prior memory allocation to be at least N bytes, where N is the
2349 ** second parameter.  The memory allocation to be resized is the first
2350 ** parameter.)^ ^ If the first parameter to sqlite3_realloc()
2351 ** is a NULL pointer then its behavior is identical to calling
2352 ** sqlite3_malloc(N) where N is the second parameter to sqlite3_realloc().
2353 ** ^If the second parameter to sqlite3_realloc() is zero or
2354 ** negative then the behavior is exactly the same as calling
2355 ** sqlite3_free(P) where P is the first parameter to sqlite3_realloc().
2356 ** ^sqlite3_realloc() returns a pointer to a memory allocation
2357 ** of at least N bytes in size or NULL if sufficient memory is unavailable.
2358 ** ^If M is the size of the prior allocation, then min(N,M) bytes
2359 ** of the prior allocation are copied into the beginning of buffer returned
2360 ** by sqlite3_realloc() and the prior allocation is freed.
2361 ** ^If sqlite3_realloc() returns NULL, then the prior allocation
2362 ** is not freed.
2363 **
2364 ** ^The memory returned by sqlite3_malloc() and sqlite3_realloc()
2365 ** is always aligned to at least an 8 byte boundary, or to a
2366 ** 4 byte boundary if the [SQLITE_4_BYTE_ALIGNED_MALLOC] compile-time
2367 ** option is used.
2368 **
2369 ** In SQLite version 3.5.0 and 3.5.1, it was possible to define
2370 ** the SQLITE_OMIT_MEMORY_ALLOCATION which would cause the built-in
2371 ** implementation of these routines to be omitted.  That capability
2372 ** is no longer provided.  Only built-in memory allocators can be used.
2373 **
2374 ** Prior to SQLite version 3.7.10, the Windows OS interface layer called
2375 ** the system malloc() and free() directly when converting
2376 ** filenames between the UTF-8 encoding used by SQLite
2377 ** and whatever filename encoding is used by the particular Windows
2378 ** installation.  Memory allocation errors were detected, but
2379 ** they were reported back as [SQLITE_CANTOPEN] or
2380 ** [SQLITE_IOERR] rather than [SQLITE_NOMEM].
2381 **
2382 ** The pointer arguments to [sqlite3_free()] and [sqlite3_realloc()]
2383 ** must be either NULL or else pointers obtained from a prior
2384 ** invocation of [sqlite3_malloc()] or [sqlite3_realloc()] that have
2385 ** not yet been released.
2386 **
2387 ** The application must not read or write any part of
2388 ** a block of memory after it has been released using
2389 ** [sqlite3_free()] or [sqlite3_realloc()].
2390 */
2391 SQLITE_API void *sqlite3_malloc(int);
2392 SQLITE_API void *sqlite3_realloc(void*, int);
2393 SQLITE_API void sqlite3_free(void*);
2394 
2395 /*
2396 ** CAPI3REF: Memory Allocator Statistics
2397 **
2398 ** SQLite provides these two interfaces for reporting on the status
2399 ** of the [sqlite3_malloc()], [sqlite3_free()], and [sqlite3_realloc()]
2400 ** routines, which form the built-in memory allocation subsystem.
2401 **
2402 ** ^The [sqlite3_memory_used()] routine returns the number of bytes
2403 ** of memory currently outstanding (malloced but not freed).
2404 ** ^The [sqlite3_memory_highwater()] routine returns the maximum
2405 ** value of [sqlite3_memory_used()] since the high-water mark
2406 ** was last reset.  ^The values returned by [sqlite3_memory_used()] and
2407 ** [sqlite3_memory_highwater()] include any overhead
2408 ** added by SQLite in its implementation of [sqlite3_malloc()],
2409 ** but not overhead added by the any underlying system library
2410 ** routines that [sqlite3_malloc()] may call.
2411 **
2412 ** ^The memory high-water mark is reset to the current value of
2413 ** [sqlite3_memory_used()] if and only if the parameter to
2414 ** [sqlite3_memory_highwater()] is true.  ^The value returned
2415 ** by [sqlite3_memory_highwater(1)] is the high-water mark
2416 ** prior to the reset.
2417 */
2418 SQLITE_API sqlite3_int64 sqlite3_memory_used(void);
2419 SQLITE_API sqlite3_int64 sqlite3_memory_highwater(int resetFlag);
2420 
2421 /*
2422 ** CAPI3REF: Pseudo-Random Number Generator
2423 **
2424 ** SQLite contains a high-quality pseudo-random number generator (PRNG) used to
2425 ** select random [ROWID | ROWIDs] when inserting new records into a table that
2426 ** already uses the largest possible [ROWID].  The PRNG is also used for
2427 ** the build-in random() and randomblob() SQL functions.  This interface allows
2428 ** applications to access the same PRNG for other purposes.
2429 **
2430 ** ^A call to this routine stores N bytes of randomness into buffer P.
2431 ** ^If N is less than one, then P can be a NULL pointer.
2432 **
2433 ** ^If this routine has not been previously called or if the previous
2434 ** call had N less than one, then the PRNG is seeded using randomness
2435 ** obtained from the xRandomness method of the default [sqlite3_vfs] object.
2436 ** ^If the previous call to this routine had an N of 1 or more then
2437 ** the pseudo-randomness is generated
2438 ** internally and without recourse to the [sqlite3_vfs] xRandomness
2439 ** method.
2440 */
2441 SQLITE_API void sqlite3_randomness(int N, void *P);
2442 
2443 /*
2444 ** CAPI3REF: Compile-Time Authorization Callbacks
2445 **
2446 ** ^This routine registers an authorizer callback with a particular
2447 ** [database connection], supplied in the first argument.
2448 ** ^The authorizer callback is invoked as SQL statements are being compiled
2449 ** by [sqlite3_prepare()] or its variants [sqlite3_prepare_v2()],
2450 ** [sqlite3_prepare16()] and [sqlite3_prepare16_v2()].  ^At various
2451 ** points during the compilation process, as logic is being created
2452 ** to perform various actions, the authorizer callback is invoked to
2453 ** see if those actions are allowed.  ^The authorizer callback should
2454 ** return [SQLITE_OK] to allow the action, [SQLITE_IGNORE] to disallow the
2455 ** specific action but allow the SQL statement to continue to be
2456 ** compiled, or [SQLITE_DENY] to cause the entire SQL statement to be
2457 ** rejected with an error.  ^If the authorizer callback returns
2458 ** any value other than [SQLITE_IGNORE], [SQLITE_OK], or [SQLITE_DENY]
2459 ** then the [sqlite3_prepare_v2()] or equivalent call that triggered
2460 ** the authorizer will fail with an error message.
2461 **
2462 ** When the callback returns [SQLITE_OK], that means the operation
2463 ** requested is ok.  ^When the callback returns [SQLITE_DENY], the
2464 ** [sqlite3_prepare_v2()] or equivalent call that triggered the
2465 ** authorizer will fail with an error message explaining that
2466 ** access is denied.
2467 **
2468 ** ^The first parameter to the authorizer callback is a copy of the third
2469 ** parameter to the sqlite3_set_authorizer() interface. ^The second parameter
2470 ** to the callback is an integer [SQLITE_COPY | action code] that specifies
2471 ** the particular action to be authorized. ^The third through sixth parameters
2472 ** to the callback are zero-terminated strings that contain additional
2473 ** details about the action to be authorized.
2474 **
2475 ** ^If the action code is [SQLITE_READ]
2476 ** and the callback returns [SQLITE_IGNORE] then the
2477 ** [prepared statement] statement is constructed to substitute
2478 ** a NULL value in place of the table column that would have
2479 ** been read if [SQLITE_OK] had been returned.  The [SQLITE_IGNORE]
2480 ** return can be used to deny an untrusted user access to individual
2481 ** columns of a table.
2482 ** ^If the action code is [SQLITE_DELETE] and the callback returns
2483 ** [SQLITE_IGNORE] then the [DELETE] operation proceeds but the
2484 ** [truncate optimization] is disabled and all rows are deleted individually.
2485 **
2486 ** An authorizer is used when [sqlite3_prepare | preparing]
2487 ** SQL statements from an untrusted source, to ensure that the SQL statements
2488 ** do not try to access data they are not allowed to see, or that they do not
2489 ** try to execute malicious statements that damage the database.  For
2490 ** example, an application may allow a user to enter arbitrary
2491 ** SQL queries for evaluation by a database.  But the application does
2492 ** not want the user to be able to make arbitrary changes to the
2493 ** database.  An authorizer could then be put in place while the
2494 ** user-entered SQL is being [sqlite3_prepare | prepared] that
2495 ** disallows everything except [SELECT] statements.
2496 **
2497 ** Applications that need to process SQL from untrusted sources
2498 ** might also consider lowering resource limits using [sqlite3_limit()]
2499 ** and limiting database size using the [max_page_count] [PRAGMA]
2500 ** in addition to using an authorizer.
2501 **
2502 ** ^(Only a single authorizer can be in place on a database connection
2503 ** at a time.  Each call to sqlite3_set_authorizer overrides the
2504 ** previous call.)^  ^Disable the authorizer by installing a NULL callback.
2505 ** The authorizer is disabled by default.
2506 **
2507 ** The authorizer callback must not do anything that will modify
2508 ** the database connection that invoked the authorizer callback.
2509 ** Note that [sqlite3_prepare_v2()] and [sqlite3_step()] both modify their
2510 ** database connections for the meaning of "modify" in this paragraph.
2511 **
2512 ** ^When [sqlite3_prepare_v2()] is used to prepare a statement, the
2513 ** statement might be re-prepared during [sqlite3_step()] due to a
2514 ** schema change.  Hence, the application should ensure that the
2515 ** correct authorizer callback remains in place during the [sqlite3_step()].
2516 **
2517 ** ^Note that the authorizer callback is invoked only during
2518 ** [sqlite3_prepare()] or its variants.  Authorization is not
2519 ** performed during statement evaluation in [sqlite3_step()], unless
2520 ** as stated in the previous paragraph, sqlite3_step() invokes
2521 ** sqlite3_prepare_v2() to reprepare a statement after a schema change.
2522 */
2523 SQLITE_API int sqlite3_set_authorizer(
2524   sqlite3*,
2525   int (*xAuth)(void*,int,const char*,const char*,const char*,const char*),
2526   void *pUserData
2527 );
2528 
2529 /*
2530 ** CAPI3REF: Authorizer Return Codes
2531 **
2532 ** The [sqlite3_set_authorizer | authorizer callback function] must
2533 ** return either [SQLITE_OK] or one of these two constants in order
2534 ** to signal SQLite whether or not the action is permitted.  See the
2535 ** [sqlite3_set_authorizer | authorizer documentation] for additional
2536 ** information.
2537 **
2538 ** Note that SQLITE_IGNORE is also used as a [SQLITE_ROLLBACK | return code]
2539 ** from the [sqlite3_vtab_on_conflict()] interface.
2540 */
2541 #define SQLITE_DENY   1   /* Abort the SQL statement with an error */
2542 #define SQLITE_IGNORE 2   /* Don't allow access, but don't generate an error */
2543 
2544 /*
2545 ** CAPI3REF: Authorizer Action Codes
2546 **
2547 ** The [sqlite3_set_authorizer()] interface registers a callback function
2548 ** that is invoked to authorize certain SQL statement actions.  The
2549 ** second parameter to the callback is an integer code that specifies
2550 ** what action is being authorized.  These are the integer action codes that
2551 ** the authorizer callback may be passed.
2552 **
2553 ** These action code values signify what kind of operation is to be
2554 ** authorized.  The 3rd and 4th parameters to the authorization
2555 ** callback function will be parameters or NULL depending on which of these
2556 ** codes is used as the second parameter.  ^(The 5th parameter to the
2557 ** authorizer callback is the name of the database ("main", "temp",
2558 ** etc.) if applicable.)^  ^The 6th parameter to the authorizer callback
2559 ** is the name of the inner-most trigger or view that is responsible for
2560 ** the access attempt or NULL if this access attempt is directly from
2561 ** top-level SQL code.
2562 */
2563 /******************************************* 3rd ************ 4th ***********/
2564 #define SQLITE_CREATE_INDEX          1   /* Index Name      Table Name      */
2565 #define SQLITE_CREATE_TABLE          2   /* Table Name      NULL            */
2566 #define SQLITE_CREATE_TEMP_INDEX     3   /* Index Name      Table Name      */
2567 #define SQLITE_CREATE_TEMP_TABLE     4   /* Table Name      NULL            */
2568 #define SQLITE_CREATE_TEMP_TRIGGER   5   /* Trigger Name    Table Name      */
2569 #define SQLITE_CREATE_TEMP_VIEW      6   /* View Name       NULL            */
2570 #define SQLITE_CREATE_TRIGGER        7   /* Trigger Name    Table Name      */
2571 #define SQLITE_CREATE_VIEW           8   /* View Name       NULL            */
2572 #define SQLITE_DELETE                9   /* Table Name      NULL            */
2573 #define SQLITE_DROP_INDEX           10   /* Index Name      Table Name      */
2574 #define SQLITE_DROP_TABLE           11   /* Table Name      NULL            */
2575 #define SQLITE_DROP_TEMP_INDEX      12   /* Index Name      Table Name      */
2576 #define SQLITE_DROP_TEMP_TABLE      13   /* Table Name      NULL            */
2577 #define SQLITE_DROP_TEMP_TRIGGER    14   /* Trigger Name    Table Name      */
2578 #define SQLITE_DROP_TEMP_VIEW       15   /* View Name       NULL            */
2579 #define SQLITE_DROP_TRIGGER         16   /* Trigger Name    Table Name      */
2580 #define SQLITE_DROP_VIEW            17   /* View Name       NULL            */
2581 #define SQLITE_INSERT               18   /* Table Name      NULL            */
2582 #define SQLITE_PRAGMA               19   /* Pragma Name     1st arg or NULL */
2583 #define SQLITE_READ                 20   /* Table Name      Column Name     */
2584 #define SQLITE_SELECT               21   /* NULL            NULL            */
2585 #define SQLITE_TRANSACTION          22   /* Operation       NULL            */
2586 #define SQLITE_UPDATE               23   /* Table Name      Column Name     */
2587 #define SQLITE_ATTACH               24   /* Filename        NULL            */
2588 #define SQLITE_DETACH               25   /* Database Name   NULL            */
2589 #define SQLITE_ALTER_TABLE          26   /* Database Name   Table Name      */
2590 #define SQLITE_REINDEX              27   /* Index Name      NULL            */
2591 #define SQLITE_ANALYZE              28   /* Table Name      NULL            */
2592 #define SQLITE_CREATE_VTABLE        29   /* Table Name      Module Name     */
2593 #define SQLITE_DROP_VTABLE          30   /* Table Name      Module Name     */
2594 #define SQLITE_FUNCTION             31   /* NULL            Function Name   */
2595 #define SQLITE_SAVEPOINT            32   /* Operation       Savepoint Name  */
2596 #define SQLITE_COPY                  0   /* No longer used */
2597 #define SQLITE_RECURSIVE            33   /* NULL            NULL            */
2598 
2599 /*
2600 ** CAPI3REF: Tracing And Profiling Functions
2601 **
2602 ** These routines register callback functions that can be used for
2603 ** tracing and profiling the execution of SQL statements.
2604 **
2605 ** ^The callback function registered by sqlite3_trace() is invoked at
2606 ** various times when an SQL statement is being run by [sqlite3_step()].
2607 ** ^The sqlite3_trace() callback is invoked with a UTF-8 rendering of the
2608 ** SQL statement text as the statement first begins executing.
2609 ** ^(Additional sqlite3_trace() callbacks might occur
2610 ** as each triggered subprogram is entered.  The callbacks for triggers
2611 ** contain a UTF-8 SQL comment that identifies the trigger.)^
2612 **
2613 ** The [SQLITE_TRACE_SIZE_LIMIT] compile-time option can be used to limit
2614 ** the length of [bound parameter] expansion in the output of sqlite3_trace().
2615 **
2616 ** ^The callback function registered by sqlite3_profile() is invoked
2617 ** as each SQL statement finishes.  ^The profile callback contains
2618 ** the original statement text and an estimate of wall-clock time
2619 ** of how long that statement took to run.  ^The profile callback
2620 ** time is in units of nanoseconds, however the current implementation
2621 ** is only capable of millisecond resolution so the six least significant
2622 ** digits in the time are meaningless.  Future versions of SQLite
2623 ** might provide greater resolution on the profiler callback.  The
2624 ** sqlite3_profile() function is considered experimental and is
2625 ** subject to change in future versions of SQLite.
2626 */
2627 SQLITE_API void *sqlite3_trace(sqlite3*, void(*xTrace)(void*,const char*), void*);
2628 SQLITE_API SQLITE_EXPERIMENTAL void *sqlite3_profile(sqlite3*,
2629    void(*xProfile)(void*,const char*,sqlite3_uint64), void*);
2630 
2631 /*
2632 ** CAPI3REF: Query Progress Callbacks
2633 **
2634 ** ^The sqlite3_progress_handler(D,N,X,P) interface causes the callback
2635 ** function X to be invoked periodically during long running calls to
2636 ** [sqlite3_exec()], [sqlite3_step()] and [sqlite3_get_table()] for
2637 ** database connection D.  An example use for this
2638 ** interface is to keep a GUI updated during a large query.
2639 **
2640 ** ^The parameter P is passed through as the only parameter to the
2641 ** callback function X.  ^The parameter N is the approximate number of
2642 ** [virtual machine instructions] that are evaluated between successive
2643 ** invocations of the callback X.  ^If N is less than one then the progress
2644 ** handler is disabled.
2645 **
2646 ** ^Only a single progress handler may be defined at one time per
2647 ** [database connection]; setting a new progress handler cancels the
2648 ** old one.  ^Setting parameter X to NULL disables the progress handler.
2649 ** ^The progress handler is also disabled by setting N to a value less
2650 ** than 1.
2651 **
2652 ** ^If the progress callback returns non-zero, the operation is
2653 ** interrupted.  This feature can be used to implement a
2654 ** "Cancel" button on a GUI progress dialog box.
2655 **
2656 ** The progress handler callback must not do anything that will modify
2657 ** the database connection that invoked the progress handler.
2658 ** Note that [sqlite3_prepare_v2()] and [sqlite3_step()] both modify their
2659 ** database connections for the meaning of "modify" in this paragraph.
2660 **
2661 */
2662 SQLITE_API void sqlite3_progress_handler(sqlite3*, int, int(*)(void*), void*);
2663 
2664 /*
2665 ** CAPI3REF: Opening A New Database Connection
2666 **
2667 ** ^These routines open an SQLite database file as specified by the
2668 ** filename argument. ^The filename argument is interpreted as UTF-8 for
2669 ** sqlite3_open() and sqlite3_open_v2() and as UTF-16 in the native byte
2670 ** order for sqlite3_open16(). ^(A [database connection] handle is usually
2671 ** returned in *ppDb, even if an error occurs.  The only exception is that
2672 ** if SQLite is unable to allocate memory to hold the [sqlite3] object,
2673 ** a NULL will be written into *ppDb instead of a pointer to the [sqlite3]
2674 ** object.)^ ^(If the database is opened (and/or created) successfully, then
2675 ** [SQLITE_OK] is returned.  Otherwise an [error code] is returned.)^ ^The
2676 ** [sqlite3_errmsg()] or [sqlite3_errmsg16()] routines can be used to obtain
2677 ** an English language description of the error following a failure of any
2678 ** of the sqlite3_open() routines.
2679 **
2680 ** ^The default encoding for the database will be UTF-8 if
2681 ** sqlite3_open() or sqlite3_open_v2() is called and
2682 ** UTF-16 in the native byte order if sqlite3_open16() is used.
2683 **
2684 ** Whether or not an error occurs when it is opened, resources
2685 ** associated with the [database connection] handle should be released by
2686 ** passing it to [sqlite3_close()] when it is no longer required.
2687 **
2688 ** The sqlite3_open_v2() interface works like sqlite3_open()
2689 ** except that it accepts two additional parameters for additional control
2690 ** over the new database connection.  ^(The flags parameter to
2691 ** sqlite3_open_v2() can take one of
2692 ** the following three values, optionally combined with the
2693 ** [SQLITE_OPEN_NOMUTEX], [SQLITE_OPEN_FULLMUTEX], [SQLITE_OPEN_SHAREDCACHE],
2694 ** [SQLITE_OPEN_PRIVATECACHE], and/or [SQLITE_OPEN_URI] flags:)^
2695 **
2696 ** <dl>
2697 ** ^(<dt>[SQLITE_OPEN_READONLY]</dt>
2698 ** <dd>The database is opened in read-only mode.  If the database does not
2699 ** already exist, an error is returned.</dd>)^
2700 **
2701 ** ^(<dt>[SQLITE_OPEN_READWRITE]</dt>
2702 ** <dd>The database is opened for reading and writing if possible, or reading
2703 ** only if the file is write protected by the operating system.  In either
2704 ** case the database must already exist, otherwise an error is returned.</dd>)^
2705 **
2706 ** ^(<dt>[SQLITE_OPEN_READWRITE] | [SQLITE_OPEN_CREATE]</dt>
2707 ** <dd>The database is opened for reading and writing, and is created if
2708 ** it does not already exist. This is the behavior that is always used for
2709 ** sqlite3_open() and sqlite3_open16().</dd>)^
2710 ** </dl>
2711 **
2712 ** If the 3rd parameter to sqlite3_open_v2() is not one of the
2713 ** combinations shown above optionally combined with other
2714 ** [SQLITE_OPEN_READONLY | SQLITE_OPEN_* bits]
2715 ** then the behavior is undefined.
2716 **
2717 ** ^If the [SQLITE_OPEN_NOMUTEX] flag is set, then the database connection
2718 ** opens in the multi-thread [threading mode] as long as the single-thread
2719 ** mode has not been set at compile-time or start-time.  ^If the
2720 ** [SQLITE_OPEN_FULLMUTEX] flag is set then the database connection opens
2721 ** in the serialized [threading mode] unless single-thread was
2722 ** previously selected at compile-time or start-time.
2723 ** ^The [SQLITE_OPEN_SHAREDCACHE] flag causes the database connection to be
2724 ** eligible to use [shared cache mode], regardless of whether or not shared
2725 ** cache is enabled using [sqlite3_enable_shared_cache()].  ^The
2726 ** [SQLITE_OPEN_PRIVATECACHE] flag causes the database connection to not
2727 ** participate in [shared cache mode] even if it is enabled.
2728 **
2729 ** ^The fourth parameter to sqlite3_open_v2() is the name of the
2730 ** [sqlite3_vfs] object that defines the operating system interface that
2731 ** the new database connection should use.  ^If the fourth parameter is
2732 ** a NULL pointer then the default [sqlite3_vfs] object is used.
2733 **
2734 ** ^If the filename is ":memory:", then a private, temporary in-memory database
2735 ** is created for the connection.  ^This in-memory database will vanish when
2736 ** the database connection is closed.  Future versions of SQLite might
2737 ** make use of additional special filenames that begin with the ":" character.
2738 ** It is recommended that when a database filename actually does begin with
2739 ** a ":" character you should prefix the filename with a pathname such as
2740 ** "./" to avoid ambiguity.
2741 **
2742 ** ^If the filename is an empty string, then a private, temporary
2743 ** on-disk database will be created.  ^This private database will be
2744 ** automatically deleted as soon as the database connection is closed.
2745 **
2746 ** [[URI filenames in sqlite3_open()]] <h3>URI Filenames</h3>
2747 **
2748 ** ^If [URI filename] interpretation is enabled, and the filename argument
2749 ** begins with "file:", then the filename is interpreted as a URI. ^URI
2750 ** filename interpretation is enabled if the [SQLITE_OPEN_URI] flag is
2751 ** set in the fourth argument to sqlite3_open_v2(), or if it has
2752 ** been enabled globally using the [SQLITE_CONFIG_URI] option with the
2753 ** [sqlite3_config()] method or by the [SQLITE_USE_URI] compile-time option.
2754 ** As of SQLite version 3.7.7, URI filename interpretation is turned off
2755 ** by default, but future releases of SQLite might enable URI filename
2756 ** interpretation by default.  See "[URI filenames]" for additional
2757 ** information.
2758 **
2759 ** URI filenames are parsed according to RFC 3986. ^If the URI contains an
2760 ** authority, then it must be either an empty string or the string
2761 ** "localhost". ^If the authority is not an empty string or "localhost", an
2762 ** error is returned to the caller. ^The fragment component of a URI, if
2763 ** present, is ignored.
2764 **
2765 ** ^SQLite uses the path component of the URI as the name of the disk file
2766 ** which contains the database. ^If the path begins with a '/' character,
2767 ** then it is interpreted as an absolute path. ^If the path does not begin
2768 ** with a '/' (meaning that the authority section is omitted from the URI)
2769 ** then the path is interpreted as a relative path.
2770 ** ^On windows, the first component of an absolute path
2771 ** is a drive specification (e.g. "C:").
2772 **
2773 ** [[core URI query parameters]]
2774 ** The query component of a URI may contain parameters that are interpreted
2775 ** either by SQLite itself, or by a [VFS | custom VFS implementation].
2776 ** SQLite interprets the following three query parameters:
2777 **
2778 ** <ul>
2779 **   <li> <b>vfs</b>: ^The "vfs" parameter may be used to specify the name of
2780 **     a VFS object that provides the operating system interface that should
2781 **     be used to access the database file on disk. ^If this option is set to
2782 **     an empty string the default VFS object is used. ^Specifying an unknown
2783 **     VFS is an error. ^If sqlite3_open_v2() is used and the vfs option is
2784 **     present, then the VFS specified by the option takes precedence over
2785 **     the value passed as the fourth parameter to sqlite3_open_v2().
2786 **
2787 **   <li> <b>mode</b>: ^(The mode parameter may be set to either "ro", "rw",
2788 **     "rwc", or "memory". Attempting to set it to any other value is
2789 **     an error)^.
2790 **     ^If "ro" is specified, then the database is opened for read-only
2791 **     access, just as if the [SQLITE_OPEN_READONLY] flag had been set in the
2792 **     third argument to sqlite3_open_v2(). ^If the mode option is set to
2793 **     "rw", then the database is opened for read-write (but not create)
2794 **     access, as if SQLITE_OPEN_READWRITE (but not SQLITE_OPEN_CREATE) had
2795 **     been set. ^Value "rwc" is equivalent to setting both
2796 **     SQLITE_OPEN_READWRITE and SQLITE_OPEN_CREATE.  ^If the mode option is
2797 **     set to "memory" then a pure [in-memory database] that never reads
2798 **     or writes from disk is used. ^It is an error to specify a value for
2799 **     the mode parameter that is less restrictive than that specified by
2800 **     the flags passed in the third parameter to sqlite3_open_v2().
2801 **
2802 **   <li> <b>cache</b>: ^The cache parameter may be set to either "shared" or
2803 **     "private". ^Setting it to "shared" is equivalent to setting the
2804 **     SQLITE_OPEN_SHAREDCACHE bit in the flags argument passed to
2805 **     sqlite3_open_v2(). ^Setting the cache parameter to "private" is
2806 **     equivalent to setting the SQLITE_OPEN_PRIVATECACHE bit.
2807 **     ^If sqlite3_open_v2() is used and the "cache" parameter is present in
2808 **     a URI filename, its value overrides any behavior requested by setting
2809 **     SQLITE_OPEN_PRIVATECACHE or SQLITE_OPEN_SHAREDCACHE flag.
2810 ** </ul>
2811 **
2812 ** ^Specifying an unknown parameter in the query component of a URI is not an
2813 ** error.  Future versions of SQLite might understand additional query
2814 ** parameters.  See "[query parameters with special meaning to SQLite]" for
2815 ** additional information.
2816 **
2817 ** [[URI filename examples]] <h3>URI filename examples</h3>
2818 **
2819 ** <table border="1" align=center cellpadding=5>
2820 ** <tr><th> URI filenames <th> Results
2821 ** <tr><td> file:data.db <td>
2822 **          Open the file "data.db" in the current directory.
2823 ** <tr><td> file:/home/fred/data.db<br>
2824 **          file:///home/fred/data.db <br>
2825 **          file://localhost/home/fred/data.db <br> <td>
2826 **          Open the database file "/home/fred/data.db".
2827 ** <tr><td> file://darkstar/home/fred/data.db <td>
2828 **          An error. "darkstar" is not a recognized authority.
2829 ** <tr><td style="white-space:nowrap">
2830 **          file:///C:/Documents%20and%20Settings/fred/Desktop/data.db
2831 **     <td> Windows only: Open the file "data.db" on fred's desktop on drive
2832 **          C:. Note that the %20 escaping in this example is not strictly
2833 **          necessary - space characters can be used literally
2834 **          in URI filenames.
2835 ** <tr><td> file:data.db?mode=ro&cache=private <td>
2836 **          Open file "data.db" in the current directory for read-only access.
2837 **          Regardless of whether or not shared-cache mode is enabled by
2838 **          default, use a private cache.
2839 ** <tr><td> file:/home/fred/data.db?vfs=unix-nolock <td>
2840 **          Open file "/home/fred/data.db". Use the special VFS "unix-nolock".
2841 ** <tr><td> file:data.db?mode=readonly <td>
2842 **          An error. "readonly" is not a valid option for the "mode" parameter.
2843 ** </table>
2844 **
2845 ** ^URI hexadecimal escape sequences (%HH) are supported within the path and
2846 ** query components of a URI. A hexadecimal escape sequence consists of a
2847 ** percent sign - "%" - followed by exactly two hexadecimal digits
2848 ** specifying an octet value. ^Before the path or query components of a
2849 ** URI filename are interpreted, they are encoded using UTF-8 and all
2850 ** hexadecimal escape sequences replaced by a single byte containing the
2851 ** corresponding octet. If this process generates an invalid UTF-8 encoding,
2852 ** the results are undefined.
2853 **
2854 ** <b>Note to Windows users:</b>  The encoding used for the filename argument
2855 ** of sqlite3_open() and sqlite3_open_v2() must be UTF-8, not whatever
2856 ** codepage is currently defined.  Filenames containing international
2857 ** characters must be converted to UTF-8 prior to passing them into
2858 ** sqlite3_open() or sqlite3_open_v2().
2859 **
2860 ** <b>Note to Windows Runtime users:</b>  The temporary directory must be set
2861 ** prior to calling sqlite3_open() or sqlite3_open_v2().  Otherwise, various
2862 ** features that require the use of temporary files may fail.
2863 **
2864 ** See also: [sqlite3_temp_directory]
2865 */
2866 SQLITE_API int sqlite3_open(
2867   const char *filename,   /* Database filename (UTF-8) */
2868   sqlite3 **ppDb          /* OUT: SQLite db handle */
2869 );
2870 SQLITE_API int sqlite3_open16(
2871   const void *filename,   /* Database filename (UTF-16) */
2872   sqlite3 **ppDb          /* OUT: SQLite db handle */
2873 );
2874 SQLITE_API int sqlite3_open_v2(
2875   const char *filename,   /* Database filename (UTF-8) */
2876   sqlite3 **ppDb,         /* OUT: SQLite db handle */
2877   int flags,              /* Flags */
2878   const char *zVfs        /* Name of VFS module to use */
2879 );
2880 
2881 /*
2882 ** CAPI3REF: Obtain Values For URI Parameters
2883 **
2884 ** These are utility routines, useful to VFS implementations, that check
2885 ** to see if a database file was a URI that contained a specific query
2886 ** parameter, and if so obtains the value of that query parameter.
2887 **
2888 ** If F is the database filename pointer passed into the xOpen() method of
2889 ** a VFS implementation when the flags parameter to xOpen() has one or
2890 ** more of the [SQLITE_OPEN_URI] or [SQLITE_OPEN_MAIN_DB] bits set and
2891 ** P is the name of the query parameter, then
2892 ** sqlite3_uri_parameter(F,P) returns the value of the P
2893 ** parameter if it exists or a NULL pointer if P does not appear as a
2894 ** query parameter on F.  If P is a query parameter of F
2895 ** has no explicit value, then sqlite3_uri_parameter(F,P) returns
2896 ** a pointer to an empty string.
2897 **
2898 ** The sqlite3_uri_boolean(F,P,B) routine assumes that P is a boolean
2899 ** parameter and returns true (1) or false (0) according to the value
2900 ** of P.  The sqlite3_uri_boolean(F,P,B) routine returns true (1) if the
2901 ** value of query parameter P is one of "yes", "true", or "on" in any
2902 ** case or if the value begins with a non-zero number.  The
2903 ** sqlite3_uri_boolean(F,P,B) routines returns false (0) if the value of
2904 ** query parameter P is one of "no", "false", or "off" in any case or
2905 ** if the value begins with a numeric zero.  If P is not a query
2906 ** parameter on F or if the value of P is does not match any of the
2907 ** above, then sqlite3_uri_boolean(F,P,B) returns (B!=0).
2908 **
2909 ** The sqlite3_uri_int64(F,P,D) routine converts the value of P into a
2910 ** 64-bit signed integer and returns that integer, or D if P does not
2911 ** exist.  If the value of P is something other than an integer, then
2912 ** zero is returned.
2913 **
2914 ** If F is a NULL pointer, then sqlite3_uri_parameter(F,P) returns NULL and
2915 ** sqlite3_uri_boolean(F,P,B) returns B.  If F is not a NULL pointer and
2916 ** is not a database file pathname pointer that SQLite passed into the xOpen
2917 ** VFS method, then the behavior of this routine is undefined and probably
2918 ** undesirable.
2919 */
2920 SQLITE_API const char *sqlite3_uri_parameter(const char *zFilename, const char *zParam);
2921 SQLITE_API int sqlite3_uri_boolean(const char *zFile, const char *zParam, int bDefault);
2922 SQLITE_API sqlite3_int64 sqlite3_uri_int64(const char*, const char*, sqlite3_int64);
2923 
2924 
2925 /*
2926 ** CAPI3REF: Error Codes And Messages
2927 **
2928 ** ^The sqlite3_errcode() interface returns the numeric [result code] or
2929 ** [extended result code] for the most recent failed sqlite3_* API call
2930 ** associated with a [database connection]. If a prior API call failed
2931 ** but the most recent API call succeeded, the return value from
2932 ** sqlite3_errcode() is undefined.  ^The sqlite3_extended_errcode()
2933 ** interface is the same except that it always returns the
2934 ** [extended result code] even when extended result codes are
2935 ** disabled.
2936 **
2937 ** ^The sqlite3_errmsg() and sqlite3_errmsg16() return English-language
2938 ** text that describes the error, as either UTF-8 or UTF-16 respectively.
2939 ** ^(Memory to hold the error message string is managed internally.
2940 ** The application does not need to worry about freeing the result.
2941 ** However, the error string might be overwritten or deallocated by
2942 ** subsequent calls to other SQLite interface functions.)^
2943 **
2944 ** ^The sqlite3_errstr() interface returns the English-language text
2945 ** that describes the [result code], as UTF-8.
2946 ** ^(Memory to hold the error message string is managed internally
2947 ** and must not be freed by the application)^.
2948 **
2949 ** When the serialized [threading mode] is in use, it might be the
2950 ** case that a second error occurs on a separate thread in between
2951 ** the time of the first error and the call to these interfaces.
2952 ** When that happens, the second error will be reported since these
2953 ** interfaces always report the most recent result.  To avoid
2954 ** this, each thread can obtain exclusive use of the [database connection] D
2955 ** by invoking [sqlite3_mutex_enter]([sqlite3_db_mutex](D)) before beginning
2956 ** to use D and invoking [sqlite3_mutex_leave]([sqlite3_db_mutex](D)) after
2957 ** all calls to the interfaces listed here are completed.
2958 **
2959 ** If an interface fails with SQLITE_MISUSE, that means the interface
2960 ** was invoked incorrectly by the application.  In that case, the
2961 ** error code and message may or may not be set.
2962 */
2963 SQLITE_API int sqlite3_errcode(sqlite3 *db);
2964 SQLITE_API int sqlite3_extended_errcode(sqlite3 *db);
2965 SQLITE_API const char *sqlite3_errmsg(sqlite3*);
2966 SQLITE_API const void *sqlite3_errmsg16(sqlite3*);
2967 SQLITE_API const char *sqlite3_errstr(int);
2968 
2969 /*
2970 ** CAPI3REF: SQL Statement Object
2971 ** KEYWORDS: {prepared statement} {prepared statements}
2972 **
2973 ** An instance of this object represents a single SQL statement.
2974 ** This object is variously known as a "prepared statement" or a
2975 ** "compiled SQL statement" or simply as a "statement".
2976 **
2977 ** The life of a statement object goes something like this:
2978 **
2979 ** <ol>
2980 ** <li> Create the object using [sqlite3_prepare_v2()] or a related
2981 **      function.
2982 ** <li> Bind values to [host parameters] using the sqlite3_bind_*()
2983 **      interfaces.
2984 ** <li> Run the SQL by calling [sqlite3_step()] one or more times.
2985 ** <li> Reset the statement using [sqlite3_reset()] then go back
2986 **      to step 2.  Do this zero or more times.
2987 ** <li> Destroy the object using [sqlite3_finalize()].
2988 ** </ol>
2989 **
2990 ** Refer to documentation on individual methods above for additional
2991 ** information.
2992 */
2993 typedef struct sqlite3_stmt sqlite3_stmt;
2994 
2995 /*
2996 ** CAPI3REF: Run-time Limits
2997 **
2998 ** ^(This interface allows the size of various constructs to be limited
2999 ** on a connection by connection basis.  The first parameter is the
3000 ** [database connection] whose limit is to be set or queried.  The
3001 ** second parameter is one of the [limit categories] that define a
3002 ** class of constructs to be size limited.  The third parameter is the
3003 ** new limit for that construct.)^
3004 **
3005 ** ^If the new limit is a negative number, the limit is unchanged.
3006 ** ^(For each limit category SQLITE_LIMIT_<i>NAME</i> there is a
3007 ** [limits | hard upper bound]
3008 ** set at compile-time by a C preprocessor macro called
3009 ** [limits | SQLITE_MAX_<i>NAME</i>].
3010 ** (The "_LIMIT_" in the name is changed to "_MAX_".))^
3011 ** ^Attempts to increase a limit above its hard upper bound are
3012 ** silently truncated to the hard upper bound.
3013 **
3014 ** ^Regardless of whether or not the limit was changed, the
3015 ** [sqlite3_limit()] interface returns the prior value of the limit.
3016 ** ^Hence, to find the current value of a limit without changing it,
3017 ** simply invoke this interface with the third parameter set to -1.
3018 **
3019 ** Run-time limits are intended for use in applications that manage
3020 ** both their own internal database and also databases that are controlled
3021 ** by untrusted external sources.  An example application might be a
3022 ** web browser that has its own databases for storing history and
3023 ** separate databases controlled by JavaScript applications downloaded
3024 ** off the Internet.  The internal databases can be given the
3025 ** large, default limits.  Databases managed by external sources can
3026 ** be given much smaller limits designed to prevent a denial of service
3027 ** attack.  Developers might also want to use the [sqlite3_set_authorizer()]
3028 ** interface to further control untrusted SQL.  The size of the database
3029 ** created by an untrusted script can be contained using the
3030 ** [max_page_count] [PRAGMA].
3031 **
3032 ** New run-time limit categories may be added in future releases.
3033 */
3034 SQLITE_API int sqlite3_limit(sqlite3*, int id, int newVal);
3035 
3036 /*
3037 ** CAPI3REF: Run-Time Limit Categories
3038 ** KEYWORDS: {limit category} {*limit categories}
3039 **
3040 ** These constants define various performance limits
3041 ** that can be lowered at run-time using [sqlite3_limit()].
3042 ** The synopsis of the meanings of the various limits is shown below.
3043 ** Additional information is available at [limits | Limits in SQLite].
3044 **
3045 ** <dl>
3046 ** [[SQLITE_LIMIT_LENGTH]] ^(<dt>SQLITE_LIMIT_LENGTH</dt>
3047 ** <dd>The maximum size of any string or BLOB or table row, in bytes.<dd>)^
3048 **
3049 ** [[SQLITE_LIMIT_SQL_LENGTH]] ^(<dt>SQLITE_LIMIT_SQL_LENGTH</dt>
3050 ** <dd>The maximum length of an SQL statement, in bytes.</dd>)^
3051 **
3052 ** [[SQLITE_LIMIT_COLUMN]] ^(<dt>SQLITE_LIMIT_COLUMN</dt>
3053 ** <dd>The maximum number of columns in a table definition or in the
3054 ** result set of a [SELECT] or the maximum number of columns in an index
3055 ** or in an ORDER BY or GROUP BY clause.</dd>)^
3056 **
3057 ** [[SQLITE_LIMIT_EXPR_DEPTH]] ^(<dt>SQLITE_LIMIT_EXPR_DEPTH</dt>
3058 ** <dd>The maximum depth of the parse tree on any expression.</dd>)^
3059 **
3060 ** [[SQLITE_LIMIT_COMPOUND_SELECT]] ^(<dt>SQLITE_LIMIT_COMPOUND_SELECT</dt>
3061 ** <dd>The maximum number of terms in a compound SELECT statement.</dd>)^
3062 **
3063 ** [[SQLITE_LIMIT_VDBE_OP]] ^(<dt>SQLITE_LIMIT_VDBE_OP</dt>
3064 ** <dd>The maximum number of instructions in a virtual machine program
3065 ** used to implement an SQL statement.  This limit is not currently
3066 ** enforced, though that might be added in some future release of
3067 ** SQLite.</dd>)^
3068 **
3069 ** [[SQLITE_LIMIT_FUNCTION_ARG]] ^(<dt>SQLITE_LIMIT_FUNCTION_ARG</dt>
3070 ** <dd>The maximum number of arguments on a function.</dd>)^
3071 **
3072 ** [[SQLITE_LIMIT_ATTACHED]] ^(<dt>SQLITE_LIMIT_ATTACHED</dt>
3073 ** <dd>The maximum number of [ATTACH | attached databases].)^</dd>
3074 **
3075 ** [[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]]
3076 ** ^(<dt>SQLITE_LIMIT_LIKE_PATTERN_LENGTH</dt>
3077 ** <dd>The maximum length of the pattern argument to the [LIKE] or
3078 ** [GLOB] operators.</dd>)^
3079 **
3080 ** [[SQLITE_LIMIT_VARIABLE_NUMBER]]
3081 ** ^(<dt>SQLITE_LIMIT_VARIABLE_NUMBER</dt>
3082 ** <dd>The maximum index number of any [parameter] in an SQL statement.)^
3083 **
3084 ** [[SQLITE_LIMIT_TRIGGER_DEPTH]] ^(<dt>SQLITE_LIMIT_TRIGGER_DEPTH</dt>
3085 ** <dd>The maximum depth of recursion for triggers.</dd>)^
3086 ** </dl>
3087 */
3088 #define SQLITE_LIMIT_LENGTH                    0
3089 #define SQLITE_LIMIT_SQL_LENGTH                1
3090 #define SQLITE_LIMIT_COLUMN                    2
3091 #define SQLITE_LIMIT_EXPR_DEPTH                3
3092 #define SQLITE_LIMIT_COMPOUND_SELECT           4
3093 #define SQLITE_LIMIT_VDBE_OP                   5
3094 #define SQLITE_LIMIT_FUNCTION_ARG              6
3095 #define SQLITE_LIMIT_ATTACHED                  7
3096 #define SQLITE_LIMIT_LIKE_PATTERN_LENGTH       8
3097 #define SQLITE_LIMIT_VARIABLE_NUMBER           9
3098 #define SQLITE_LIMIT_TRIGGER_DEPTH            10
3099 
3100 /*
3101 ** CAPI3REF: Compiling An SQL Statement
3102 ** KEYWORDS: {SQL statement compiler}
3103 **
3104 ** To execute an SQL query, it must first be compiled into a byte-code
3105 ** program using one of these routines.
3106 **
3107 ** The first argument, "db", is a [database connection] obtained from a
3108 ** prior successful call to [sqlite3_open()], [sqlite3_open_v2()] or
3109 ** [sqlite3_open16()].  The database connection must not have been closed.
3110 **
3111 ** The second argument, "zSql", is the statement to be compiled, encoded
3112 ** as either UTF-8 or UTF-16.  The sqlite3_prepare() and sqlite3_prepare_v2()
3113 ** interfaces use UTF-8, and sqlite3_prepare16() and sqlite3_prepare16_v2()
3114 ** use UTF-16.
3115 **
3116 ** ^If the nByte argument is less than zero, then zSql is read up to the
3117 ** first zero terminator. ^If nByte is non-negative, then it is the maximum
3118 ** number of  bytes read from zSql.  ^When nByte is non-negative, the
3119 ** zSql string ends at either the first '\000' or '\u0000' character or
3120 ** the nByte-th byte, whichever comes first. If the caller knows
3121 ** that the supplied string is nul-terminated, then there is a small
3122 ** performance advantage to be gained by passing an nByte parameter that
3123 ** is equal to the number of bytes in the input string <i>including</i>
3124 ** the nul-terminator bytes as this saves SQLite from having to
3125 ** make a copy of the input string.
3126 **
3127 ** ^If pzTail is not NULL then *pzTail is made to point to the first byte
3128 ** past the end of the first SQL statement in zSql.  These routines only
3129 ** compile the first statement in zSql, so *pzTail is left pointing to
3130 ** what remains uncompiled.
3131 **
3132 ** ^*ppStmt is left pointing to a compiled [prepared statement] that can be
3133 ** executed using [sqlite3_step()].  ^If there is an error, *ppStmt is set
3134 ** to NULL.  ^If the input text contains no SQL (if the input is an empty
3135 ** string or a comment) then *ppStmt is set to NULL.
3136 ** The calling procedure is responsible for deleting the compiled
3137 ** SQL statement using [sqlite3_finalize()] after it has finished with it.
3138 ** ppStmt may not be NULL.
3139 **
3140 ** ^On success, the sqlite3_prepare() family of routines return [SQLITE_OK];
3141 ** otherwise an [error code] is returned.
3142 **
3143 ** The sqlite3_prepare_v2() and sqlite3_prepare16_v2() interfaces are
3144 ** recommended for all new programs. The two older interfaces are retained
3145 ** for backwards compatibility, but their use is discouraged.
3146 ** ^In the "v2" interfaces, the prepared statement
3147 ** that is returned (the [sqlite3_stmt] object) contains a copy of the
3148 ** original SQL text. This causes the [sqlite3_step()] interface to
3149 ** behave differently in three ways:
3150 **
3151 ** <ol>
3152 ** <li>
3153 ** ^If the database schema changes, instead of returning [SQLITE_SCHEMA] as it
3154 ** always used to do, [sqlite3_step()] will automatically recompile the SQL
3155 ** statement and try to run it again. As many as [SQLITE_MAX_SCHEMA_RETRY]
3156 ** retries will occur before sqlite3_step() gives up and returns an error.
3157 ** </li>
3158 **
3159 ** <li>
3160 ** ^When an error occurs, [sqlite3_step()] will return one of the detailed
3161 ** [error codes] or [extended error codes].  ^The legacy behavior was that
3162 ** [sqlite3_step()] would only return a generic [SQLITE_ERROR] result code
3163 ** and the application would have to make a second call to [sqlite3_reset()]
3164 ** in order to find the underlying cause of the problem. With the "v2" prepare
3165 ** interfaces, the underlying reason for the error is returned immediately.
3166 ** </li>
3167 **
3168 ** <li>
3169 ** ^If the specific value bound to [parameter | host parameter] in the
3170 ** WHERE clause might influence the choice of query plan for a statement,
3171 ** then the statement will be automatically recompiled, as if there had been
3172 ** a schema change, on the first  [sqlite3_step()] call following any change
3173 ** to the [sqlite3_bind_text | bindings] of that [parameter].
3174 ** ^The specific value of WHERE-clause [parameter] might influence the
3175 ** choice of query plan if the parameter is the left-hand side of a [LIKE]
3176 ** or [GLOB] operator or if the parameter is compared to an indexed column
3177 ** and the [SQLITE_ENABLE_STAT3] compile-time option is enabled.
3178 ** </li>
3179 ** </ol>
3180 */
3181 SQLITE_API int sqlite3_prepare(
3182   sqlite3 *db,            /* Database handle */
3183   const char *zSql,       /* SQL statement, UTF-8 encoded */
3184   int nByte,              /* Maximum length of zSql in bytes. */
3185   sqlite3_stmt **ppStmt,  /* OUT: Statement handle */
3186   const char **pzTail     /* OUT: Pointer to unused portion of zSql */
3187 );
3188 SQLITE_API int sqlite3_prepare_v2(
3189   sqlite3 *db,            /* Database handle */
3190   const char *zSql,       /* SQL statement, UTF-8 encoded */
3191   int nByte,              /* Maximum length of zSql in bytes. */
3192   sqlite3_stmt **ppStmt,  /* OUT: Statement handle */
3193   const char **pzTail     /* OUT: Pointer to unused portion of zSql */
3194 );
3195 SQLITE_API int sqlite3_prepare16(
3196   sqlite3 *db,            /* Database handle */
3197   const void *zSql,       /* SQL statement, UTF-16 encoded */
3198   int nByte,              /* Maximum length of zSql in bytes. */
3199   sqlite3_stmt **ppStmt,  /* OUT: Statement handle */
3200   const void **pzTail     /* OUT: Pointer to unused portion of zSql */
3201 );
3202 SQLITE_API int sqlite3_prepare16_v2(
3203   sqlite3 *db,            /* Database handle */
3204   const void *zSql,       /* SQL statement, UTF-16 encoded */
3205   int nByte,              /* Maximum length of zSql in bytes. */
3206   sqlite3_stmt **ppStmt,  /* OUT: Statement handle */
3207   const void **pzTail     /* OUT: Pointer to unused portion of zSql */
3208 );
3209 
3210 /*
3211 ** CAPI3REF: Retrieving Statement SQL
3212 **
3213 ** ^This interface can be used to retrieve a saved copy of the original
3214 ** SQL text used to create a [prepared statement] if that statement was
3215 ** compiled using either [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()].
3216 */
3217 SQLITE_API const char *sqlite3_sql(sqlite3_stmt *pStmt);
3218 
3219 /*
3220 ** CAPI3REF: Determine If An SQL Statement Writes The Database
3221 **
3222 ** ^The sqlite3_stmt_readonly(X) interface returns true (non-zero) if
3223 ** and only if the [prepared statement] X makes no direct changes to
3224 ** the content of the database file.
3225 **
3226 ** Note that [application-defined SQL functions] or
3227 ** [virtual tables] might change the database indirectly as a side effect.
3228 ** ^(For example, if an application defines a function "eval()" that
3229 ** calls [sqlite3_exec()], then the following SQL statement would
3230 ** change the database file through side-effects:
3231 **
3232 ** <blockquote><pre>
3233 **    SELECT eval('DELETE FROM t1') FROM t2;
3234 ** </pre></blockquote>
3235 **
3236 ** But because the [SELECT] statement does not change the database file
3237 ** directly, sqlite3_stmt_readonly() would still return true.)^
3238 **
3239 ** ^Transaction control statements such as [BEGIN], [COMMIT], [ROLLBACK],
3240 ** [SAVEPOINT], and [RELEASE] cause sqlite3_stmt_readonly() to return true,
3241 ** since the statements themselves do not actually modify the database but
3242 ** rather they control the timing of when other statements modify the
3243 ** database.  ^The [ATTACH] and [DETACH] statements also cause
3244 ** sqlite3_stmt_readonly() to return true since, while those statements
3245 ** change the configuration of a database connection, they do not make
3246 ** changes to the content of the database files on disk.
3247 */
3248 SQLITE_API int sqlite3_stmt_readonly(sqlite3_stmt *pStmt);
3249 
3250 /*
3251 ** CAPI3REF: Determine If A Prepared Statement Has Been Reset
3252 **
3253 ** ^The sqlite3_stmt_busy(S) interface returns true (non-zero) if the
3254 ** [prepared statement] S has been stepped at least once using
3255 ** [sqlite3_step(S)] but has not run to completion and/or has not
3256 ** been reset using [sqlite3_reset(S)].  ^The sqlite3_stmt_busy(S)
3257 ** interface returns false if S is a NULL pointer.  If S is not a
3258 ** NULL pointer and is not a pointer to a valid [prepared statement]
3259 ** object, then the behavior is undefined and probably undesirable.
3260 **
3261 ** This interface can be used in combination [sqlite3_next_stmt()]
3262 ** to locate all prepared statements associated with a database
3263 ** connection that are in need of being reset.  This can be used,
3264 ** for example, in diagnostic routines to search for prepared
3265 ** statements that are holding a transaction open.
3266 */
3267 SQLITE_API int sqlite3_stmt_busy(sqlite3_stmt*);
3268 
3269 /*
3270 ** CAPI3REF: Dynamically Typed Value Object
3271 ** KEYWORDS: {protected sqlite3_value} {unprotected sqlite3_value}
3272 **
3273 ** SQLite uses the sqlite3_value object to represent all values
3274 ** that can be stored in a database table. SQLite uses dynamic typing
3275 ** for the values it stores.  ^Values stored in sqlite3_value objects
3276 ** can be integers, floating point values, strings, BLOBs, or NULL.
3277 **
3278 ** An sqlite3_value object may be either "protected" or "unprotected".
3279 ** Some interfaces require a protected sqlite3_value.  Other interfaces
3280 ** will accept either a protected or an unprotected sqlite3_value.
3281 ** Every interface that accepts sqlite3_value arguments specifies
3282 ** whether or not it requires a protected sqlite3_value.
3283 **
3284 ** The terms "protected" and "unprotected" refer to whether or not
3285 ** a mutex is held.  An internal mutex is held for a protected
3286 ** sqlite3_value object but no mutex is held for an unprotected
3287 ** sqlite3_value object.  If SQLite is compiled to be single-threaded
3288 ** (with [SQLITE_THREADSAFE=0] and with [sqlite3_threadsafe()] returning 0)
3289 ** or if SQLite is run in one of reduced mutex modes
3290 ** [SQLITE_CONFIG_SINGLETHREAD] or [SQLITE_CONFIG_MULTITHREAD]
3291 ** then there is no distinction between protected and unprotected
3292 ** sqlite3_value objects and they can be used interchangeably.  However,
3293 ** for maximum code portability it is recommended that applications
3294 ** still make the distinction between protected and unprotected
3295 ** sqlite3_value objects even when not strictly required.
3296 **
3297 ** ^The sqlite3_value objects that are passed as parameters into the
3298 ** implementation of [application-defined SQL functions] are protected.
3299 ** ^The sqlite3_value object returned by
3300 ** [sqlite3_column_value()] is unprotected.
3301 ** Unprotected sqlite3_value objects may only be used with
3302 ** [sqlite3_result_value()] and [sqlite3_bind_value()].
3303 ** The [sqlite3_value_blob | sqlite3_value_type()] family of
3304 ** interfaces require protected sqlite3_value objects.
3305 */
3306 typedef struct Mem sqlite3_value;
3307 
3308 /*
3309 ** CAPI3REF: SQL Function Context Object
3310 **
3311 ** The context in which an SQL function executes is stored in an
3312 ** sqlite3_context object.  ^A pointer to an sqlite3_context object
3313 ** is always first parameter to [application-defined SQL functions].
3314 ** The application-defined SQL function implementation will pass this
3315 ** pointer through into calls to [sqlite3_result_int | sqlite3_result()],
3316 ** [sqlite3_aggregate_context()], [sqlite3_user_data()],
3317 ** [sqlite3_context_db_handle()], [sqlite3_get_auxdata()],
3318 ** and/or [sqlite3_set_auxdata()].
3319 */
3320 typedef struct sqlite3_context sqlite3_context;
3321 
3322 /*
3323 ** CAPI3REF: Binding Values To Prepared Statements
3324 ** KEYWORDS: {host parameter} {host parameters} {host parameter name}
3325 ** KEYWORDS: {SQL parameter} {SQL parameters} {parameter binding}
3326 **
3327 ** ^(In the SQL statement text input to [sqlite3_prepare_v2()] and its variants,
3328 ** literals may be replaced by a [parameter] that matches one of following
3329 ** templates:
3330 **
3331 ** <ul>
3332 ** <li>  ?
3333 ** <li>  ?NNN
3334 ** <li>  :VVV
3335 ** <li>  @VVV
3336 ** <li>  $VVV
3337 ** </ul>
3338 **
3339 ** In the templates above, NNN represents an integer literal,
3340 ** and VVV represents an alphanumeric identifier.)^  ^The values of these
3341 ** parameters (also called "host parameter names" or "SQL parameters")
3342 ** can be set using the sqlite3_bind_*() routines defined here.
3343 **
3344 ** ^The first argument to the sqlite3_bind_*() routines is always
3345 ** a pointer to the [sqlite3_stmt] object returned from
3346 ** [sqlite3_prepare_v2()] or its variants.
3347 **
3348 ** ^The second argument is the index of the SQL parameter to be set.
3349 ** ^The leftmost SQL parameter has an index of 1.  ^When the same named
3350 ** SQL parameter is used more than once, second and subsequent
3351 ** occurrences have the same index as the first occurrence.
3352 ** ^The index for named parameters can be looked up using the
3353 ** [sqlite3_bind_parameter_index()] API if desired.  ^The index
3354 ** for "?NNN" parameters is the value of NNN.
3355 ** ^The NNN value must be between 1 and the [sqlite3_limit()]
3356 ** parameter [SQLITE_LIMIT_VARIABLE_NUMBER] (default value: 999).
3357 **
3358 ** ^The third argument is the value to bind to the parameter.
3359 ** ^If the third parameter to sqlite3_bind_text() or sqlite3_bind_text16()
3360 ** or sqlite3_bind_blob() is a NULL pointer then the fourth parameter
3361 ** is ignored and the end result is the same as sqlite3_bind_null().
3362 **
3363 ** ^(In those routines that have a fourth argument, its value is the
3364 ** number of bytes in the parameter.  To be clear: the value is the
3365 ** number of <u>bytes</u> in the value, not the number of characters.)^
3366 ** ^If the fourth parameter to sqlite3_bind_text() or sqlite3_bind_text16()
3367 ** is negative, then the length of the string is
3368 ** the number of bytes up to the first zero terminator.
3369 ** If the fourth parameter to sqlite3_bind_blob() is negative, then
3370 ** the behavior is undefined.
3371 ** If a non-negative fourth parameter is provided to sqlite3_bind_text()
3372 ** or sqlite3_bind_text16() then that parameter must be the byte offset
3373 ** where the NUL terminator would occur assuming the string were NUL
3374 ** terminated.  If any NUL characters occur at byte offsets less than
3375 ** the value of the fourth parameter then the resulting string value will
3376 ** contain embedded NULs.  The result of expressions involving strings
3377 ** with embedded NULs is undefined.
3378 **
3379 ** ^The fifth argument to sqlite3_bind_blob(), sqlite3_bind_text(), and
3380 ** sqlite3_bind_text16() is a destructor used to dispose of the BLOB or
3381 ** string after SQLite has finished with it.  ^The destructor is called
3382 ** to dispose of the BLOB or string even if the call to sqlite3_bind_blob(),
3383 ** sqlite3_bind_text(), or sqlite3_bind_text16() fails.
3384 ** ^If the fifth argument is
3385 ** the special value [SQLITE_STATIC], then SQLite assumes that the
3386 ** information is in static, unmanaged space and does not need to be freed.
3387 ** ^If the fifth argument has the value [SQLITE_TRANSIENT], then
3388 ** SQLite makes its own private copy of the data immediately, before
3389 ** the sqlite3_bind_*() routine returns.
3390 **
3391 ** ^The sqlite3_bind_zeroblob() routine binds a BLOB of length N that
3392 ** is filled with zeroes.  ^A zeroblob uses a fixed amount of memory
3393 ** (just an integer to hold its size) while it is being processed.
3394 ** Zeroblobs are intended to serve as placeholders for BLOBs whose
3395 ** content is later written using
3396 ** [sqlite3_blob_open | incremental BLOB I/O] routines.
3397 ** ^A negative value for the zeroblob results in a zero-length BLOB.
3398 **
3399 ** ^If any of the sqlite3_bind_*() routines are called with a NULL pointer
3400 ** for the [prepared statement] or with a prepared statement for which
3401 ** [sqlite3_step()] has been called more recently than [sqlite3_reset()],
3402 ** then the call will return [SQLITE_MISUSE].  If any sqlite3_bind_()
3403 ** routine is passed a [prepared statement] that has been finalized, the
3404 ** result is undefined and probably harmful.
3405 **
3406 ** ^Bindings are not cleared by the [sqlite3_reset()] routine.
3407 ** ^Unbound parameters are interpreted as NULL.
3408 **
3409 ** ^The sqlite3_bind_* routines return [SQLITE_OK] on success or an
3410 ** [error code] if anything goes wrong.
3411 ** ^[SQLITE_RANGE] is returned if the parameter
3412 ** index is out of range.  ^[SQLITE_NOMEM] is returned if malloc() fails.
3413 **
3414 ** See also: [sqlite3_bind_parameter_count()],
3415 ** [sqlite3_bind_parameter_name()], and [sqlite3_bind_parameter_index()].
3416 */
3417 SQLITE_API int sqlite3_bind_blob(sqlite3_stmt*, int, const void*, int n, void(*)(void*));
3418 SQLITE_API int sqlite3_bind_double(sqlite3_stmt*, int, double);
3419 SQLITE_API int sqlite3_bind_int(sqlite3_stmt*, int, int);
3420 SQLITE_API int sqlite3_bind_int64(sqlite3_stmt*, int, sqlite3_int64);
3421 SQLITE_API int sqlite3_bind_null(sqlite3_stmt*, int);
3422 SQLITE_API int sqlite3_bind_text(sqlite3_stmt*, int, const char*, int n, void(*)(void*));
3423 SQLITE_API int sqlite3_bind_text16(sqlite3_stmt*, int, const void*, int, void(*)(void*));
3424 SQLITE_API int sqlite3_bind_value(sqlite3_stmt*, int, const sqlite3_value*);
3425 SQLITE_API int sqlite3_bind_zeroblob(sqlite3_stmt*, int, int n);
3426 
3427 /*
3428 ** CAPI3REF: Number Of SQL Parameters
3429 **
3430 ** ^This routine can be used to find the number of [SQL parameters]
3431 ** in a [prepared statement].  SQL parameters are tokens of the
3432 ** form "?", "?NNN", ":AAA", "$AAA", or "@AAA" that serve as
3433 ** placeholders for values that are [sqlite3_bind_blob | bound]
3434 ** to the parameters at a later time.
3435 **
3436 ** ^(This routine actually returns the index of the largest (rightmost)
3437 ** parameter. For all forms except ?NNN, this will correspond to the
3438 ** number of unique parameters.  If parameters of the ?NNN form are used,
3439 ** there may be gaps in the list.)^
3440 **
3441 ** See also: [sqlite3_bind_blob|sqlite3_bind()],
3442 ** [sqlite3_bind_parameter_name()], and
3443 ** [sqlite3_bind_parameter_index()].
3444 */
3445 SQLITE_API int sqlite3_bind_parameter_count(sqlite3_stmt*);
3446 
3447 /*
3448 ** CAPI3REF: Name Of A Host Parameter
3449 **
3450 ** ^The sqlite3_bind_parameter_name(P,N) interface returns
3451 ** the name of the N-th [SQL parameter] in the [prepared statement] P.
3452 ** ^(SQL parameters of the form "?NNN" or ":AAA" or "@AAA" or "$AAA"
3453 ** have a name which is the string "?NNN" or ":AAA" or "@AAA" or "$AAA"
3454 ** respectively.
3455 ** In other words, the initial ":" or "$" or "@" or "?"
3456 ** is included as part of the name.)^
3457 ** ^Parameters of the form "?" without a following integer have no name
3458 ** and are referred to as "nameless" or "anonymous parameters".
3459 **
3460 ** ^The first host parameter has an index of 1, not 0.
3461 **
3462 ** ^If the value N is out of range or if the N-th parameter is
3463 ** nameless, then NULL is returned.  ^The returned string is
3464 ** always in UTF-8 encoding even if the named parameter was
3465 ** originally specified as UTF-16 in [sqlite3_prepare16()] or
3466 ** [sqlite3_prepare16_v2()].
3467 **
3468 ** See also: [sqlite3_bind_blob|sqlite3_bind()],
3469 ** [sqlite3_bind_parameter_count()], and
3470 ** [sqlite3_bind_parameter_index()].
3471 */
3472 SQLITE_API const char *sqlite3_bind_parameter_name(sqlite3_stmt*, int);
3473 
3474 /*
3475 ** CAPI3REF: Index Of A Parameter With A Given Name
3476 **
3477 ** ^Return the index of an SQL parameter given its name.  ^The
3478 ** index value returned is suitable for use as the second
3479 ** parameter to [sqlite3_bind_blob|sqlite3_bind()].  ^A zero
3480 ** is returned if no matching parameter is found.  ^The parameter
3481 ** name must be given in UTF-8 even if the original statement
3482 ** was prepared from UTF-16 text using [sqlite3_prepare16_v2()].
3483 **
3484 ** See also: [sqlite3_bind_blob|sqlite3_bind()],
3485 ** [sqlite3_bind_parameter_count()], and
3486 ** [sqlite3_bind_parameter_index()].
3487 */
3488 SQLITE_API int sqlite3_bind_parameter_index(sqlite3_stmt*, const char *zName);
3489 
3490 /*
3491 ** CAPI3REF: Reset All Bindings On A Prepared Statement
3492 **
3493 ** ^Contrary to the intuition of many, [sqlite3_reset()] does not reset
3494 ** the [sqlite3_bind_blob | bindings] on a [prepared statement].
3495 ** ^Use this routine to reset all host parameters to NULL.
3496 */
3497 SQLITE_API int sqlite3_clear_bindings(sqlite3_stmt*);
3498 
3499 /*
3500 ** CAPI3REF: Number Of Columns In A Result Set
3501 **
3502 ** ^Return the number of columns in the result set returned by the
3503 ** [prepared statement]. ^This routine returns 0 if pStmt is an SQL
3504 ** statement that does not return data (for example an [UPDATE]).
3505 **
3506 ** See also: [sqlite3_data_count()]
3507 */
3508 SQLITE_API int sqlite3_column_count(sqlite3_stmt *pStmt);
3509 
3510 /*
3511 ** CAPI3REF: Column Names In A Result Set
3512 **
3513 ** ^These routines return the name assigned to a particular column
3514 ** in the result set of a [SELECT] statement.  ^The sqlite3_column_name()
3515 ** interface returns a pointer to a zero-terminated UTF-8 string
3516 ** and sqlite3_column_name16() returns a pointer to a zero-terminated
3517 ** UTF-16 string.  ^The first parameter is the [prepared statement]
3518 ** that implements the [SELECT] statement. ^The second parameter is the
3519 ** column number.  ^The leftmost column is number 0.
3520 **
3521 ** ^The returned string pointer is valid until either the [prepared statement]
3522 ** is destroyed by [sqlite3_finalize()] or until the statement is automatically
3523 ** reprepared by the first call to [sqlite3_step()] for a particular run
3524 ** or until the next call to
3525 ** sqlite3_column_name() or sqlite3_column_name16() on the same column.
3526 **
3527 ** ^If sqlite3_malloc() fails during the processing of either routine
3528 ** (for example during a conversion from UTF-8 to UTF-16) then a
3529 ** NULL pointer is returned.
3530 **
3531 ** ^The name of a result column is the value of the "AS" clause for
3532 ** that column, if there is an AS clause.  If there is no AS clause
3533 ** then the name of the column is unspecified and may change from
3534 ** one release of SQLite to the next.
3535 */
3536 SQLITE_API const char *sqlite3_column_name(sqlite3_stmt*, int N);
3537 SQLITE_API const void *sqlite3_column_name16(sqlite3_stmt*, int N);
3538 
3539 /*
3540 ** CAPI3REF: Source Of Data In A Query Result
3541 **
3542 ** ^These routines provide a means to determine the database, table, and
3543 ** table column that is the origin of a particular result column in
3544 ** [SELECT] statement.
3545 ** ^The name of the database or table or column can be returned as
3546 ** either a UTF-8 or UTF-16 string.  ^The _database_ routines return
3547 ** the database name, the _table_ routines return the table name, and
3548 ** the origin_ routines return the column name.
3549 ** ^The returned string is valid until the [prepared statement] is destroyed
3550 ** using [sqlite3_finalize()] or until the statement is automatically
3551 ** reprepared by the first call to [sqlite3_step()] for a particular run
3552 ** or until the same information is requested
3553 ** again in a different encoding.
3554 **
3555 ** ^The names returned are the original un-aliased names of the
3556 ** database, table, and column.
3557 **
3558 ** ^The first argument to these interfaces is a [prepared statement].
3559 ** ^These functions return information about the Nth result column returned by
3560 ** the statement, where N is the second function argument.
3561 ** ^The left-most column is column 0 for these routines.
3562 **
3563 ** ^If the Nth column returned by the statement is an expression or
3564 ** subquery and is not a column value, then all of these functions return
3565 ** NULL.  ^These routine might also return NULL if a memory allocation error
3566 ** occurs.  ^Otherwise, they return the name of the attached database, table,
3567 ** or column that query result column was extracted from.
3568 **
3569 ** ^As with all other SQLite APIs, those whose names end with "16" return
3570 ** UTF-16 encoded strings and the other functions return UTF-8.
3571 **
3572 ** ^These APIs are only available if the library was compiled with the
3573 ** [SQLITE_ENABLE_COLUMN_METADATA] C-preprocessor symbol.
3574 **
3575 ** If two or more threads call one or more of these routines against the same
3576 ** prepared statement and column at the same time then the results are
3577 ** undefined.
3578 **
3579 ** If two or more threads call one or more
3580 ** [sqlite3_column_database_name | column metadata interfaces]
3581 ** for the same [prepared statement] and result column
3582 ** at the same time then the results are undefined.
3583 */
3584 SQLITE_API const char *sqlite3_column_database_name(sqlite3_stmt*,int);
3585 SQLITE_API const void *sqlite3_column_database_name16(sqlite3_stmt*,int);
3586 SQLITE_API const char *sqlite3_column_table_name(sqlite3_stmt*,int);
3587 SQLITE_API const void *sqlite3_column_table_name16(sqlite3_stmt*,int);
3588 SQLITE_API const char *sqlite3_column_origin_name(sqlite3_stmt*,int);
3589 SQLITE_API const void *sqlite3_column_origin_name16(sqlite3_stmt*,int);
3590 
3591 /*
3592 ** CAPI3REF: Declared Datatype Of A Query Result
3593 **
3594 ** ^(The first parameter is a [prepared statement].
3595 ** If this statement is a [SELECT] statement and the Nth column of the
3596 ** returned result set of that [SELECT] is a table column (not an
3597 ** expression or subquery) then the declared type of the table
3598 ** column is returned.)^  ^If the Nth column of the result set is an
3599 ** expression or subquery, then a NULL pointer is returned.
3600 ** ^The returned string is always UTF-8 encoded.
3601 **
3602 ** ^(For example, given the database schema:
3603 **
3604 ** CREATE TABLE t1(c1 VARIANT);
3605 **
3606 ** and the following statement to be compiled:
3607 **
3608 ** SELECT c1 + 1, c1 FROM t1;
3609 **
3610 ** this routine would return the string "VARIANT" for the second result
3611 ** column (i==1), and a NULL pointer for the first result column (i==0).)^
3612 **
3613 ** ^SQLite uses dynamic run-time typing.  ^So just because a column
3614 ** is declared to contain a particular type does not mean that the
3615 ** data stored in that column is of the declared type.  SQLite is
3616 ** strongly typed, but the typing is dynamic not static.  ^Type
3617 ** is associated with individual values, not with the containers
3618 ** used to hold those values.
3619 */
3620 SQLITE_API const char *sqlite3_column_decltype(sqlite3_stmt*,int);
3621 SQLITE_API const void *sqlite3_column_decltype16(sqlite3_stmt*,int);
3622 
3623 /*
3624 ** CAPI3REF: Evaluate An SQL Statement
3625 **
3626 ** After a [prepared statement] has been prepared using either
3627 ** [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()] or one of the legacy
3628 ** interfaces [sqlite3_prepare()] or [sqlite3_prepare16()], this function
3629 ** must be called one or more times to evaluate the statement.
3630 **
3631 ** The details of the behavior of the sqlite3_step() interface depend
3632 ** on whether the statement was prepared using the newer "v2" interface
3633 ** [sqlite3_prepare_v2()] and [sqlite3_prepare16_v2()] or the older legacy
3634 ** interface [sqlite3_prepare()] and [sqlite3_prepare16()].  The use of the
3635 ** new "v2" interface is recommended for new applications but the legacy
3636 ** interface will continue to be supported.
3637 **
3638 ** ^In the legacy interface, the return value will be either [SQLITE_BUSY],
3639 ** [SQLITE_DONE], [SQLITE_ROW], [SQLITE_ERROR], or [SQLITE_MISUSE].
3640 ** ^With the "v2" interface, any of the other [result codes] or
3641 ** [extended result codes] might be returned as well.
3642 **
3643 ** ^[SQLITE_BUSY] means that the database engine was unable to acquire the
3644 ** database locks it needs to do its job.  ^If the statement is a [COMMIT]
3645 ** or occurs outside of an explicit transaction, then you can retry the
3646 ** statement.  If the statement is not a [COMMIT] and occurs within an
3647 ** explicit transaction then you should rollback the transaction before
3648 ** continuing.
3649 **
3650 ** ^[SQLITE_DONE] means that the statement has finished executing
3651 ** successfully.  sqlite3_step() should not be called again on this virtual
3652 ** machine without first calling [sqlite3_reset()] to reset the virtual
3653 ** machine back to its initial state.
3654 **
3655 ** ^If the SQL statement being executed returns any data, then [SQLITE_ROW]
3656 ** is returned each time a new row of data is ready for processing by the
3657 ** caller. The values may be accessed using the [column access functions].
3658 ** sqlite3_step() is called again to retrieve the next row of data.
3659 **
3660 ** ^[SQLITE_ERROR] means that a run-time error (such as a constraint
3661 ** violation) has occurred.  sqlite3_step() should not be called again on
3662 ** the VM. More information may be found by calling [sqlite3_errmsg()].
3663 ** ^With the legacy interface, a more specific error code (for example,
3664 ** [SQLITE_INTERRUPT], [SQLITE_SCHEMA], [SQLITE_CORRUPT], and so forth)
3665 ** can be obtained by calling [sqlite3_reset()] on the
3666 ** [prepared statement].  ^In the "v2" interface,
3667 ** the more specific error code is returned directly by sqlite3_step().
3668 **
3669 ** [SQLITE_MISUSE] means that the this routine was called inappropriately.
3670 ** Perhaps it was called on a [prepared statement] that has
3671 ** already been [sqlite3_finalize | finalized] or on one that had
3672 ** previously returned [SQLITE_ERROR] or [SQLITE_DONE].  Or it could
3673 ** be the case that the same database connection is being used by two or
3674 ** more threads at the same moment in time.
3675 **
3676 ** For all versions of SQLite up to and including 3.6.23.1, a call to
3677 ** [sqlite3_reset()] was required after sqlite3_step() returned anything
3678 ** other than [SQLITE_ROW] before any subsequent invocation of
3679 ** sqlite3_step().  Failure to reset the prepared statement using
3680 ** [sqlite3_reset()] would result in an [SQLITE_MISUSE] return from
3681 ** sqlite3_step().  But after version 3.6.23.1, sqlite3_step() began
3682 ** calling [sqlite3_reset()] automatically in this circumstance rather
3683 ** than returning [SQLITE_MISUSE].  This is not considered a compatibility
3684 ** break because any application that ever receives an SQLITE_MISUSE error
3685 ** is broken by definition.  The [SQLITE_OMIT_AUTORESET] compile-time option
3686 ** can be used to restore the legacy behavior.
3687 **
3688 ** <b>Goofy Interface Alert:</b> In the legacy interface, the sqlite3_step()
3689 ** API always returns a generic error code, [SQLITE_ERROR], following any
3690 ** error other than [SQLITE_BUSY] and [SQLITE_MISUSE].  You must call
3691 ** [sqlite3_reset()] or [sqlite3_finalize()] in order to find one of the
3692 ** specific [error codes] that better describes the error.
3693 ** We admit that this is a goofy design.  The problem has been fixed
3694 ** with the "v2" interface.  If you prepare all of your SQL statements
3695 ** using either [sqlite3_prepare_v2()] or [sqlite3_prepare16_v2()] instead
3696 ** of the legacy [sqlite3_prepare()] and [sqlite3_prepare16()] interfaces,
3697 ** then the more specific [error codes] are returned directly
3698 ** by sqlite3_step().  The use of the "v2" interface is recommended.
3699 */
3700 SQLITE_API int sqlite3_step(sqlite3_stmt*);
3701 
3702 /*
3703 ** CAPI3REF: Number of columns in a result set
3704 **
3705 ** ^The sqlite3_data_count(P) interface returns the number of columns in the
3706 ** current row of the result set of [prepared statement] P.
3707 ** ^If prepared statement P does not have results ready to return
3708 ** (via calls to the [sqlite3_column_int | sqlite3_column_*()] of
3709 ** interfaces) then sqlite3_data_count(P) returns 0.
3710 ** ^The sqlite3_data_count(P) routine also returns 0 if P is a NULL pointer.
3711 ** ^The sqlite3_data_count(P) routine returns 0 if the previous call to
3712 ** [sqlite3_step](P) returned [SQLITE_DONE].  ^The sqlite3_data_count(P)
3713 ** will return non-zero if previous call to [sqlite3_step](P) returned
3714 ** [SQLITE_ROW], except in the case of the [PRAGMA incremental_vacuum]
3715 ** where it always returns zero since each step of that multi-step
3716 ** pragma returns 0 columns of data.
3717 **
3718 ** See also: [sqlite3_column_count()]
3719 */
3720 SQLITE_API int sqlite3_data_count(sqlite3_stmt *pStmt);
3721 
3722 /*
3723 ** CAPI3REF: Fundamental Datatypes
3724 ** KEYWORDS: SQLITE_TEXT
3725 **
3726 ** ^(Every value in SQLite has one of five fundamental datatypes:
3727 **
3728 ** <ul>
3729 ** <li> 64-bit signed integer
3730 ** <li> 64-bit IEEE floating point number
3731 ** <li> string
3732 ** <li> BLOB
3733 ** <li> NULL
3734 ** </ul>)^
3735 **
3736 ** These constants are codes for each of those types.
3737 **
3738 ** Note that the SQLITE_TEXT constant was also used in SQLite version 2
3739 ** for a completely different meaning.  Software that links against both
3740 ** SQLite version 2 and SQLite version 3 should use SQLITE3_TEXT, not
3741 ** SQLITE_TEXT.
3742 */
3743 #define SQLITE_INTEGER  1
3744 #define SQLITE_FLOAT    2
3745 #define SQLITE_BLOB     4
3746 #define SQLITE_NULL     5
3747 #ifdef SQLITE_TEXT
3748 # undef SQLITE_TEXT
3749 #else
3750 # define SQLITE_TEXT     3
3751 #endif
3752 #define SQLITE3_TEXT     3
3753 
3754 /*
3755 ** CAPI3REF: Result Values From A Query
3756 ** KEYWORDS: {column access functions}
3757 **
3758 ** These routines form the "result set" interface.
3759 **
3760 ** ^These routines return information about a single column of the current
3761 ** result row of a query.  ^In every case the first argument is a pointer
3762 ** to the [prepared statement] that is being evaluated (the [sqlite3_stmt*]
3763 ** that was returned from [sqlite3_prepare_v2()] or one of its variants)
3764 ** and the second argument is the index of the column for which information
3765 ** should be returned. ^The leftmost column of the result set has the index 0.
3766 ** ^The number of columns in the result can be determined using
3767 ** [sqlite3_column_count()].
3768 **
3769 ** If the SQL statement does not currently point to a valid row, or if the
3770 ** column index is out of range, the result is undefined.
3771 ** These routines may only be called when the most recent call to
3772 ** [sqlite3_step()] has returned [SQLITE_ROW] and neither
3773 ** [sqlite3_reset()] nor [sqlite3_finalize()] have been called subsequently.
3774 ** If any of these routines are called after [sqlite3_reset()] or
3775 ** [sqlite3_finalize()] or after [sqlite3_step()] has returned
3776 ** something other than [SQLITE_ROW], the results are undefined.
3777 ** If [sqlite3_step()] or [sqlite3_reset()] or [sqlite3_finalize()]
3778 ** are called from a different thread while any of these routines
3779 ** are pending, then the results are undefined.
3780 **
3781 ** ^The sqlite3_column_type() routine returns the
3782 ** [SQLITE_INTEGER | datatype code] for the initial data type
3783 ** of the result column.  ^The returned value is one of [SQLITE_INTEGER],
3784 ** [SQLITE_FLOAT], [SQLITE_TEXT], [SQLITE_BLOB], or [SQLITE_NULL].  The value
3785 ** returned by sqlite3_column_type() is only meaningful if no type
3786 ** conversions have occurred as described below.  After a type conversion,
3787 ** the value returned by sqlite3_column_type() is undefined.  Future
3788 ** versions of SQLite may change the behavior of sqlite3_column_type()
3789 ** following a type conversion.
3790 **
3791 ** ^If the result is a BLOB or UTF-8 string then the sqlite3_column_bytes()
3792 ** routine returns the number of bytes in that BLOB or string.
3793 ** ^If the result is a UTF-16 string, then sqlite3_column_bytes() converts
3794 ** the string to UTF-8 and then returns the number of bytes.
3795 ** ^If the result is a numeric value then sqlite3_column_bytes() uses
3796 ** [sqlite3_snprintf()] to convert that value to a UTF-8 string and returns
3797 ** the number of bytes in that string.
3798 ** ^If the result is NULL, then sqlite3_column_bytes() returns zero.
3799 **
3800 ** ^If the result is a BLOB or UTF-16 string then the sqlite3_column_bytes16()
3801 ** routine returns the number of bytes in that BLOB or string.
3802 ** ^If the result is a UTF-8 string, then sqlite3_column_bytes16() converts
3803 ** the string to UTF-16 and then returns the number of bytes.
3804 ** ^If the result is a numeric value then sqlite3_column_bytes16() uses
3805 ** [sqlite3_snprintf()] to convert that value to a UTF-16 string and returns
3806 ** the number of bytes in that string.
3807 ** ^If the result is NULL, then sqlite3_column_bytes16() returns zero.
3808 **
3809 ** ^The values returned by [sqlite3_column_bytes()] and
3810 ** [sqlite3_column_bytes16()] do not include the zero terminators at the end
3811 ** of the string.  ^For clarity: the values returned by
3812 ** [sqlite3_column_bytes()] and [sqlite3_column_bytes16()] are the number of
3813 ** bytes in the string, not the number of characters.
3814 **
3815 ** ^Strings returned by sqlite3_column_text() and sqlite3_column_text16(),
3816 ** even empty strings, are always zero-terminated.  ^The return
3817 ** value from sqlite3_column_blob() for a zero-length BLOB is a NULL pointer.
3818 **
3819 ** ^The object returned by [sqlite3_column_value()] is an
3820 ** [unprotected sqlite3_value] object.  An unprotected sqlite3_value object
3821 ** may only be used with [sqlite3_bind_value()] and [sqlite3_result_value()].
3822 ** If the [unprotected sqlite3_value] object returned by
3823 ** [sqlite3_column_value()] is used in any other way, including calls
3824 ** to routines like [sqlite3_value_int()], [sqlite3_value_text()],
3825 ** or [sqlite3_value_bytes()], then the behavior is undefined.
3826 **
3827 ** These routines attempt to convert the value where appropriate.  ^For
3828 ** example, if the internal representation is FLOAT and a text result
3829 ** is requested, [sqlite3_snprintf()] is used internally to perform the
3830 ** conversion automatically.  ^(The following table details the conversions
3831 ** that are applied:
3832 **
3833 ** <blockquote>
3834 ** <table border="1">
3835 ** <tr><th> Internal<br>Type <th> Requested<br>Type <th>  Conversion
3836 **
3837 ** <tr><td>  NULL    <td> INTEGER   <td> Result is 0
3838 ** <tr><td>  NULL    <td>  FLOAT    <td> Result is 0.0
3839 ** <tr><td>  NULL    <td>   TEXT    <td> Result is a NULL pointer
3840 ** <tr><td>  NULL    <td>   BLOB    <td> Result is a NULL pointer
3841 ** <tr><td> INTEGER  <td>  FLOAT    <td> Convert from integer to float
3842 ** <tr><td> INTEGER  <td>   TEXT    <td> ASCII rendering of the integer
3843 ** <tr><td> INTEGER  <td>   BLOB    <td> Same as INTEGER->TEXT
3844 ** <tr><td>  FLOAT   <td> INTEGER   <td> [CAST] to INTEGER
3845 ** <tr><td>  FLOAT   <td>   TEXT    <td> ASCII rendering of the float
3846 ** <tr><td>  FLOAT   <td>   BLOB    <td> [CAST] to BLOB
3847 ** <tr><td>  TEXT    <td> INTEGER   <td> [CAST] to INTEGER
3848 ** <tr><td>  TEXT    <td>  FLOAT    <td> [CAST] to REAL
3849 ** <tr><td>  TEXT    <td>   BLOB    <td> No change
3850 ** <tr><td>  BLOB    <td> INTEGER   <td> [CAST] to INTEGER
3851 ** <tr><td>  BLOB    <td>  FLOAT    <td> [CAST] to REAL
3852 ** <tr><td>  BLOB    <td>   TEXT    <td> Add a zero terminator if needed
3853 ** </table>
3854 ** </blockquote>)^
3855 **
3856 ** The table above makes reference to standard C library functions atoi()
3857 ** and atof().  SQLite does not really use these functions.  It has its
3858 ** own equivalent internal routines.  The atoi() and atof() names are
3859 ** used in the table for brevity and because they are familiar to most
3860 ** C programmers.
3861 **
3862 ** Note that when type conversions occur, pointers returned by prior
3863 ** calls to sqlite3_column_blob(), sqlite3_column_text(), and/or
3864 ** sqlite3_column_text16() may be invalidated.
3865 ** Type conversions and pointer invalidations might occur
3866 ** in the following cases:
3867 **
3868 ** <ul>
3869 ** <li> The initial content is a BLOB and sqlite3_column_text() or
3870 **      sqlite3_column_text16() is called.  A zero-terminator might
3871 **      need to be added to the string.</li>
3872 ** <li> The initial content is UTF-8 text and sqlite3_column_bytes16() or
3873 **      sqlite3_column_text16() is called.  The content must be converted
3874 **      to UTF-16.</li>
3875 ** <li> The initial content is UTF-16 text and sqlite3_column_bytes() or
3876 **      sqlite3_column_text() is called.  The content must be converted
3877 **      to UTF-8.</li>
3878 ** </ul>
3879 **
3880 ** ^Conversions between UTF-16be and UTF-16le are always done in place and do
3881 ** not invalidate a prior pointer, though of course the content of the buffer
3882 ** that the prior pointer references will have been modified.  Other kinds
3883 ** of conversion are done in place when it is possible, but sometimes they
3884 ** are not possible and in those cases prior pointers are invalidated.
3885 **
3886 ** The safest and easiest to remember policy is to invoke these routines
3887 ** in one of the following ways:
3888 **
3889 ** <ul>
3890 **  <li>sqlite3_column_text() followed by sqlite3_column_bytes()</li>
3891 **  <li>sqlite3_column_blob() followed by sqlite3_column_bytes()</li>
3892 **  <li>sqlite3_column_text16() followed by sqlite3_column_bytes16()</li>
3893 ** </ul>
3894 **
3895 ** In other words, you should call sqlite3_column_text(),
3896 ** sqlite3_column_blob(), or sqlite3_column_text16() first to force the result
3897 ** into the desired format, then invoke sqlite3_column_bytes() or
3898 ** sqlite3_column_bytes16() to find the size of the result.  Do not mix calls
3899 ** to sqlite3_column_text() or sqlite3_column_blob() with calls to
3900 ** sqlite3_column_bytes16(), and do not mix calls to sqlite3_column_text16()
3901 ** with calls to sqlite3_column_bytes().
3902 **
3903 ** ^The pointers returned are valid until a type conversion occurs as
3904 ** described above, or until [sqlite3_step()] or [sqlite3_reset()] or
3905 ** [sqlite3_finalize()] is called.  ^The memory space used to hold strings
3906 ** and BLOBs is freed automatically.  Do <b>not</b> pass the pointers returned
3907 ** from [sqlite3_column_blob()], [sqlite3_column_text()], etc. into
3908 ** [sqlite3_free()].
3909 **
3910 ** ^(If a memory allocation error occurs during the evaluation of any
3911 ** of these routines, a default value is returned.  The default value
3912 ** is either the integer 0, the floating point number 0.0, or a NULL
3913 ** pointer.  Subsequent calls to [sqlite3_errcode()] will return
3914 ** [SQLITE_NOMEM].)^
3915 */
3916 SQLITE_API const void *sqlite3_column_blob(sqlite3_stmt*, int iCol);
3917 SQLITE_API int sqlite3_column_bytes(sqlite3_stmt*, int iCol);
3918 SQLITE_API int sqlite3_column_bytes16(sqlite3_stmt*, int iCol);
3919 SQLITE_API double sqlite3_column_double(sqlite3_stmt*, int iCol);
3920 SQLITE_API int sqlite3_column_int(sqlite3_stmt*, int iCol);
3921 SQLITE_API sqlite3_int64 sqlite3_column_int64(sqlite3_stmt*, int iCol);
3922 SQLITE_API const unsigned char *sqlite3_column_text(sqlite3_stmt*, int iCol);
3923 SQLITE_API const void *sqlite3_column_text16(sqlite3_stmt*, int iCol);
3924 SQLITE_API int sqlite3_column_type(sqlite3_stmt*, int iCol);
3925 SQLITE_API sqlite3_value *sqlite3_column_value(sqlite3_stmt*, int iCol);
3926 
3927 /*
3928 ** CAPI3REF: Destroy A Prepared Statement Object
3929 **
3930 ** ^The sqlite3_finalize() function is called to delete a [prepared statement].
3931 ** ^If the most recent evaluation of the statement encountered no errors
3932 ** or if the statement is never been evaluated, then sqlite3_finalize() returns
3933 ** SQLITE_OK.  ^If the most recent evaluation of statement S failed, then
3934 ** sqlite3_finalize(S) returns the appropriate [error code] or
3935 ** [extended error code].
3936 **
3937 ** ^The sqlite3_finalize(S) routine can be called at any point during
3938 ** the life cycle of [prepared statement] S:
3939 ** before statement S is ever evaluated, after
3940 ** one or more calls to [sqlite3_reset()], or after any call
3941 ** to [sqlite3_step()] regardless of whether or not the statement has
3942 ** completed execution.
3943 **
3944 ** ^Invoking sqlite3_finalize() on a NULL pointer is a harmless no-op.
3945 **
3946 ** The application must finalize every [prepared statement] in order to avoid
3947 ** resource leaks.  It is a grievous error for the application to try to use
3948 ** a prepared statement after it has been finalized.  Any use of a prepared
3949 ** statement after it has been finalized can result in undefined and
3950 ** undesirable behavior such as segfaults and heap corruption.
3951 */
3952 SQLITE_API int sqlite3_finalize(sqlite3_stmt *pStmt);
3953 
3954 /*
3955 ** CAPI3REF: Reset A Prepared Statement Object
3956 **
3957 ** The sqlite3_reset() function is called to reset a [prepared statement]
3958 ** object back to its initial state, ready to be re-executed.
3959 ** ^Any SQL statement variables that had values bound to them using
3960 ** the [sqlite3_bind_blob | sqlite3_bind_*() API] retain their values.
3961 ** Use [sqlite3_clear_bindings()] to reset the bindings.
3962 **
3963 ** ^The [sqlite3_reset(S)] interface resets the [prepared statement] S
3964 ** back to the beginning of its program.
3965 **
3966 ** ^If the most recent call to [sqlite3_step(S)] for the
3967 ** [prepared statement] S returned [SQLITE_ROW] or [SQLITE_DONE],
3968 ** or if [sqlite3_step(S)] has never before been called on S,
3969 ** then [sqlite3_reset(S)] returns [SQLITE_OK].
3970 **
3971 ** ^If the most recent call to [sqlite3_step(S)] for the
3972 ** [prepared statement] S indicated an error, then
3973 ** [sqlite3_reset(S)] returns an appropriate [error code].
3974 **
3975 ** ^The [sqlite3_reset(S)] interface does not change the values
3976 ** of any [sqlite3_bind_blob|bindings] on the [prepared statement] S.
3977 */
3978 SQLITE_API int sqlite3_reset(sqlite3_stmt *pStmt);
3979 
3980 /*
3981 ** CAPI3REF: Create Or Redefine SQL Functions
3982 ** KEYWORDS: {function creation routines}
3983 ** KEYWORDS: {application-defined SQL function}
3984 ** KEYWORDS: {application-defined SQL functions}
3985 **
3986 ** ^These functions (collectively known as "function creation routines")
3987 ** are used to add SQL functions or aggregates or to redefine the behavior
3988 ** of existing SQL functions or aggregates.  The only differences between
3989 ** these routines are the text encoding expected for
3990 ** the second parameter (the name of the function being created)
3991 ** and the presence or absence of a destructor callback for
3992 ** the application data pointer.
3993 **
3994 ** ^The first parameter is the [database connection] to which the SQL
3995 ** function is to be added.  ^If an application uses more than one database
3996 ** connection then application-defined SQL functions must be added
3997 ** to each database connection separately.
3998 **
3999 ** ^The second parameter is the name of the SQL function to be created or
4000 ** redefined.  ^The length of the name is limited to 255 bytes in a UTF-8
4001 ** representation, exclusive of the zero-terminator.  ^Note that the name
4002 ** length limit is in UTF-8 bytes, not characters nor UTF-16 bytes.
4003 ** ^Any attempt to create a function with a longer name
4004 ** will result in [SQLITE_MISUSE] being returned.
4005 **
4006 ** ^The third parameter (nArg)
4007 ** is the number of arguments that the SQL function or
4008 ** aggregate takes. ^If this parameter is -1, then the SQL function or
4009 ** aggregate may take any number of arguments between 0 and the limit
4010 ** set by [sqlite3_limit]([SQLITE_LIMIT_FUNCTION_ARG]).  If the third
4011 ** parameter is less than -1 or greater than 127 then the behavior is
4012 ** undefined.
4013 **
4014 ** ^The fourth parameter, eTextRep, specifies what
4015 ** [SQLITE_UTF8 | text encoding] this SQL function prefers for
4016 ** its parameters.  The application should set this parameter to
4017 ** [SQLITE_UTF16LE] if the function implementation invokes
4018 ** [sqlite3_value_text16le()] on an input, or [SQLITE_UTF16BE] if the
4019 ** implementation invokes [sqlite3_value_text16be()] on an input, or
4020 ** [SQLITE_UTF16] if [sqlite3_value_text16()] is used, or [SQLITE_UTF8]
4021 ** otherwise.  ^The same SQL function may be registered multiple times using
4022 ** different preferred text encodings, with different implementations for
4023 ** each encoding.
4024 ** ^When multiple implementations of the same function are available, SQLite
4025 ** will pick the one that involves the least amount of data conversion.
4026 **
4027 ** ^The fourth parameter may optionally be ORed with [SQLITE_DETERMINISTIC]
4028 ** to signal that the function will always return the same result given
4029 ** the same inputs within a single SQL statement.  Most SQL functions are
4030 ** deterministic.  The built-in [random()] SQL function is an example of a
4031 ** function that is not deterministic.  The SQLite query planner is able to
4032 ** perform additional optimizations on deterministic functions, so use
4033 ** of the [SQLITE_DETERMINISTIC] flag is recommended where possible.
4034 **
4035 ** ^(The fifth parameter is an arbitrary pointer.  The implementation of the
4036 ** function can gain access to this pointer using [sqlite3_user_data()].)^
4037 **
4038 ** ^The sixth, seventh and eighth parameters, xFunc, xStep and xFinal, are
4039 ** pointers to C-language functions that implement the SQL function or
4040 ** aggregate. ^A scalar SQL function requires an implementation of the xFunc
4041 ** callback only; NULL pointers must be passed as the xStep and xFinal
4042 ** parameters. ^An aggregate SQL function requires an implementation of xStep
4043 ** and xFinal and NULL pointer must be passed for xFunc. ^To delete an existing
4044 ** SQL function or aggregate, pass NULL pointers for all three function
4045 ** callbacks.
4046 **
4047 ** ^(If the ninth parameter to sqlite3_create_function_v2() is not NULL,
4048 ** then it is destructor for the application data pointer.
4049 ** The destructor is invoked when the function is deleted, either by being
4050 ** overloaded or when the database connection closes.)^
4051 ** ^The destructor is also invoked if the call to
4052 ** sqlite3_create_function_v2() fails.
4053 ** ^When the destructor callback of the tenth parameter is invoked, it
4054 ** is passed a single argument which is a copy of the application data
4055 ** pointer which was the fifth parameter to sqlite3_create_function_v2().
4056 **
4057 ** ^It is permitted to register multiple implementations of the same
4058 ** functions with the same name but with either differing numbers of
4059 ** arguments or differing preferred text encodings.  ^SQLite will use
4060 ** the implementation that most closely matches the way in which the
4061 ** SQL function is used.  ^A function implementation with a non-negative
4062 ** nArg parameter is a better match than a function implementation with
4063 ** a negative nArg.  ^A function where the preferred text encoding
4064 ** matches the database encoding is a better
4065 ** match than a function where the encoding is different.
4066 ** ^A function where the encoding difference is between UTF16le and UTF16be
4067 ** is a closer match than a function where the encoding difference is
4068 ** between UTF8 and UTF16.
4069 **
4070 ** ^Built-in functions may be overloaded by new application-defined functions.
4071 **
4072 ** ^An application-defined function is permitted to call other
4073 ** SQLite interfaces.  However, such calls must not
4074 ** close the database connection nor finalize or reset the prepared
4075 ** statement in which the function is running.
4076 */
4077 SQLITE_API int sqlite3_create_function(
4078   sqlite3 *db,
4079   const char *zFunctionName,
4080   int nArg,
4081   int eTextRep,
4082   void *pApp,
4083   void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
4084   void (*xStep)(sqlite3_context*,int,sqlite3_value**),
4085   void (*xFinal)(sqlite3_context*)
4086 );
4087 SQLITE_API int sqlite3_create_function16(
4088   sqlite3 *db,
4089   const void *zFunctionName,
4090   int nArg,
4091   int eTextRep,
4092   void *pApp,
4093   void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
4094   void (*xStep)(sqlite3_context*,int,sqlite3_value**),
4095   void (*xFinal)(sqlite3_context*)
4096 );
4097 SQLITE_API int sqlite3_create_function_v2(
4098   sqlite3 *db,
4099   const char *zFunctionName,
4100   int nArg,
4101   int eTextRep,
4102   void *pApp,
4103   void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
4104   void (*xStep)(sqlite3_context*,int,sqlite3_value**),
4105   void (*xFinal)(sqlite3_context*),
4106   void(*xDestroy)(void*)
4107 );
4108 
4109 /*
4110 ** CAPI3REF: Text Encodings
4111 **
4112 ** These constant define integer codes that represent the various
4113 ** text encodings supported by SQLite.
4114 */
4115 #define SQLITE_UTF8           1
4116 #define SQLITE_UTF16LE        2
4117 #define SQLITE_UTF16BE        3
4118 #define SQLITE_UTF16          4    /* Use native byte order */
4119 #define SQLITE_ANY            5    /* Deprecated */
4120 #define SQLITE_UTF16_ALIGNED  8    /* sqlite3_create_collation only */
4121 
4122 /*
4123 ** CAPI3REF: Function Flags
4124 **
4125 ** These constants may be ORed together with the
4126 ** [SQLITE_UTF8 | preferred text encoding] as the fourth argument
4127 ** to [sqlite3_create_function()], [sqlite3_create_function16()], or
4128 ** [sqlite3_create_function_v2()].
4129 */
4130 #define SQLITE_DETERMINISTIC    0x800
4131 
4132 /*
4133 ** CAPI3REF: Deprecated Functions
4134 ** DEPRECATED
4135 **
4136 ** These functions are [deprecated].  In order to maintain
4137 ** backwards compatibility with older code, these functions continue
4138 ** to be supported.  However, new applications should avoid
4139 ** the use of these functions.  To help encourage people to avoid
4140 ** using these functions, we are not going to tell you what they do.
4141 */
4142 #ifndef SQLITE_OMIT_DEPRECATED
4143 SQLITE_API SQLITE_DEPRECATED int sqlite3_aggregate_count(sqlite3_context*);
4144 SQLITE_API SQLITE_DEPRECATED int sqlite3_expired(sqlite3_stmt*);
4145 SQLITE_API SQLITE_DEPRECATED int sqlite3_transfer_bindings(sqlite3_stmt*, sqlite3_stmt*);
4146 SQLITE_API SQLITE_DEPRECATED int sqlite3_global_recover(void);
4147 SQLITE_API SQLITE_DEPRECATED void sqlite3_thread_cleanup(void);
4148 SQLITE_API SQLITE_DEPRECATED int sqlite3_memory_alarm(void(*)(void*,sqlite3_int64,int),
4149                       void*,sqlite3_int64);
4150 #endif
4151 
4152 /*
4153 ** CAPI3REF: Obtaining SQL Function Parameter Values
4154 **
4155 ** The C-language implementation of SQL functions and aggregates uses
4156 ** this set of interface routines to access the parameter values on
4157 ** the function or aggregate.
4158 **
4159 ** The xFunc (for scalar functions) or xStep (for aggregates) parameters
4160 ** to [sqlite3_create_function()] and [sqlite3_create_function16()]
4161 ** define callbacks that implement the SQL functions and aggregates.
4162 ** The 3rd parameter to these callbacks is an array of pointers to
4163 ** [protected sqlite3_value] objects.  There is one [sqlite3_value] object for
4164 ** each parameter to the SQL function.  These routines are used to
4165 ** extract values from the [sqlite3_value] objects.
4166 **
4167 ** These routines work only with [protected sqlite3_value] objects.
4168 ** Any attempt to use these routines on an [unprotected sqlite3_value]
4169 ** object results in undefined behavior.
4170 **
4171 ** ^These routines work just like the corresponding [column access functions]
4172 ** except that  these routines take a single [protected sqlite3_value] object
4173 ** pointer instead of a [sqlite3_stmt*] pointer and an integer column number.
4174 **
4175 ** ^The sqlite3_value_text16() interface extracts a UTF-16 string
4176 ** in the native byte-order of the host machine.  ^The
4177 ** sqlite3_value_text16be() and sqlite3_value_text16le() interfaces
4178 ** extract UTF-16 strings as big-endian and little-endian respectively.
4179 **
4180 ** ^(The sqlite3_value_numeric_type() interface attempts to apply
4181 ** numeric affinity to the value.  This means that an attempt is
4182 ** made to convert the value to an integer or floating point.  If
4183 ** such a conversion is possible without loss of information (in other
4184 ** words, if the value is a string that looks like a number)
4185 ** then the conversion is performed.  Otherwise no conversion occurs.
4186 ** The [SQLITE_INTEGER | datatype] after conversion is returned.)^
4187 **
4188 ** Please pay particular attention to the fact that the pointer returned
4189 ** from [sqlite3_value_blob()], [sqlite3_value_text()], or
4190 ** [sqlite3_value_text16()] can be invalidated by a subsequent call to
4191 ** [sqlite3_value_bytes()], [sqlite3_value_bytes16()], [sqlite3_value_text()],
4192 ** or [sqlite3_value_text16()].
4193 **
4194 ** These routines must be called from the same thread as
4195 ** the SQL function that supplied the [sqlite3_value*] parameters.
4196 */
4197 SQLITE_API const void *sqlite3_value_blob(sqlite3_value*);
4198 SQLITE_API int sqlite3_value_bytes(sqlite3_value*);
4199 SQLITE_API int sqlite3_value_bytes16(sqlite3_value*);
4200 SQLITE_API double sqlite3_value_double(sqlite3_value*);
4201 SQLITE_API int sqlite3_value_int(sqlite3_value*);
4202 SQLITE_API sqlite3_int64 sqlite3_value_int64(sqlite3_value*);
4203 SQLITE_API const unsigned char *sqlite3_value_text(sqlite3_value*);
4204 SQLITE_API const void *sqlite3_value_text16(sqlite3_value*);
4205 SQLITE_API const void *sqlite3_value_text16le(sqlite3_value*);
4206 SQLITE_API const void *sqlite3_value_text16be(sqlite3_value*);
4207 SQLITE_API int sqlite3_value_type(sqlite3_value*);
4208 SQLITE_API int sqlite3_value_numeric_type(sqlite3_value*);
4209 
4210 /*
4211 ** CAPI3REF: Obtain Aggregate Function Context
4212 **
4213 ** Implementations of aggregate SQL functions use this
4214 ** routine to allocate memory for storing their state.
4215 **
4216 ** ^The first time the sqlite3_aggregate_context(C,N) routine is called
4217 ** for a particular aggregate function, SQLite
4218 ** allocates N of memory, zeroes out that memory, and returns a pointer
4219 ** to the new memory. ^On second and subsequent calls to
4220 ** sqlite3_aggregate_context() for the same aggregate function instance,
4221 ** the same buffer is returned.  Sqlite3_aggregate_context() is normally
4222 ** called once for each invocation of the xStep callback and then one
4223 ** last time when the xFinal callback is invoked.  ^(When no rows match
4224 ** an aggregate query, the xStep() callback of the aggregate function
4225 ** implementation is never called and xFinal() is called exactly once.
4226 ** In those cases, sqlite3_aggregate_context() might be called for the
4227 ** first time from within xFinal().)^
4228 **
4229 ** ^The sqlite3_aggregate_context(C,N) routine returns a NULL pointer
4230 ** when first called if N is less than or equal to zero or if a memory
4231 ** allocate error occurs.
4232 **
4233 ** ^(The amount of space allocated by sqlite3_aggregate_context(C,N) is
4234 ** determined by the N parameter on first successful call.  Changing the
4235 ** value of N in subsequent call to sqlite3_aggregate_context() within
4236 ** the same aggregate function instance will not resize the memory
4237 ** allocation.)^  Within the xFinal callback, it is customary to set
4238 ** N=0 in calls to sqlite3_aggregate_context(C,N) so that no
4239 ** pointless memory allocations occur.
4240 **
4241 ** ^SQLite automatically frees the memory allocated by
4242 ** sqlite3_aggregate_context() when the aggregate query concludes.
4243 **
4244 ** The first parameter must be a copy of the
4245 ** [sqlite3_context | SQL function context] that is the first parameter
4246 ** to the xStep or xFinal callback routine that implements the aggregate
4247 ** function.
4248 **
4249 ** This routine must be called from the same thread in which
4250 ** the aggregate SQL function is running.
4251 */
4252 SQLITE_API void *sqlite3_aggregate_context(sqlite3_context*, int nBytes);
4253 
4254 /*
4255 ** CAPI3REF: User Data For Functions
4256 **
4257 ** ^The sqlite3_user_data() interface returns a copy of
4258 ** the pointer that was the pUserData parameter (the 5th parameter)
4259 ** of the [sqlite3_create_function()]
4260 ** and [sqlite3_create_function16()] routines that originally
4261 ** registered the application defined function.
4262 **
4263 ** This routine must be called from the same thread in which
4264 ** the application-defined function is running.
4265 */
4266 SQLITE_API void *sqlite3_user_data(sqlite3_context*);
4267 
4268 /*
4269 ** CAPI3REF: Database Connection For Functions
4270 **
4271 ** ^The sqlite3_context_db_handle() interface returns a copy of
4272 ** the pointer to the [database connection] (the 1st parameter)
4273 ** of the [sqlite3_create_function()]
4274 ** and [sqlite3_create_function16()] routines that originally
4275 ** registered the application defined function.
4276 */
4277 SQLITE_API sqlite3 *sqlite3_context_db_handle(sqlite3_context*);
4278 
4279 /*
4280 ** CAPI3REF: Function Auxiliary Data
4281 **
4282 ** These functions may be used by (non-aggregate) SQL functions to
4283 ** associate metadata with argument values. If the same value is passed to
4284 ** multiple invocations of the same SQL function during query execution, under
4285 ** some circumstances the associated metadata may be preserved.  An example
4286 ** of where this might be useful is in a regular-expression matching
4287 ** function. The compiled version of the regular expression can be stored as
4288 ** metadata associated with the pattern string.
4289 ** Then as long as the pattern string remains the same,
4290 ** the compiled regular expression can be reused on multiple
4291 ** invocations of the same function.
4292 **
4293 ** ^The sqlite3_get_auxdata() interface returns a pointer to the metadata
4294 ** associated by the sqlite3_set_auxdata() function with the Nth argument
4295 ** value to the application-defined function. ^If there is no metadata
4296 ** associated with the function argument, this sqlite3_get_auxdata() interface
4297 ** returns a NULL pointer.
4298 **
4299 ** ^The sqlite3_set_auxdata(C,N,P,X) interface saves P as metadata for the N-th
4300 ** argument of the application-defined function.  ^Subsequent
4301 ** calls to sqlite3_get_auxdata(C,N) return P from the most recent
4302 ** sqlite3_set_auxdata(C,N,P,X) call if the metadata is still valid or
4303 ** NULL if the metadata has been discarded.
4304 ** ^After each call to sqlite3_set_auxdata(C,N,P,X) where X is not NULL,
4305 ** SQLite will invoke the destructor function X with parameter P exactly
4306 ** once, when the metadata is discarded.
4307 ** SQLite is free to discard the metadata at any time, including: <ul>
4308 ** <li> when the corresponding function parameter changes, or
4309 ** <li> when [sqlite3_reset()] or [sqlite3_finalize()] is called for the
4310 **      SQL statement, or
4311 ** <li> when sqlite3_set_auxdata() is invoked again on the same parameter, or
4312 ** <li> during the original sqlite3_set_auxdata() call when a memory
4313 **      allocation error occurs. </ul>)^
4314 **
4315 ** Note the last bullet in particular.  The destructor X in
4316 ** sqlite3_set_auxdata(C,N,P,X) might be called immediately, before the
4317 ** sqlite3_set_auxdata() interface even returns.  Hence sqlite3_set_auxdata()
4318 ** should be called near the end of the function implementation and the
4319 ** function implementation should not make any use of P after
4320 ** sqlite3_set_auxdata() has been called.
4321 **
4322 ** ^(In practice, metadata is preserved between function calls for
4323 ** function parameters that are compile-time constants, including literal
4324 ** values and [parameters] and expressions composed from the same.)^
4325 **
4326 ** These routines must be called from the same thread in which
4327 ** the SQL function is running.
4328 */
4329 SQLITE_API void *sqlite3_get_auxdata(sqlite3_context*, int N);
4330 SQLITE_API void sqlite3_set_auxdata(sqlite3_context*, int N, void*, void (*)(void*));
4331 
4332 
4333 /*
4334 ** CAPI3REF: Constants Defining Special Destructor Behavior
4335 **
4336 ** These are special values for the destructor that is passed in as the
4337 ** final argument to routines like [sqlite3_result_blob()].  ^If the destructor
4338 ** argument is SQLITE_STATIC, it means that the content pointer is constant
4339 ** and will never change.  It does not need to be destroyed.  ^The
4340 ** SQLITE_TRANSIENT value means that the content will likely change in
4341 ** the near future and that SQLite should make its own private copy of
4342 ** the content before returning.
4343 **
4344 ** The typedef is necessary to work around problems in certain
4345 ** C++ compilers.
4346 */
4347 typedef void (*sqlite3_destructor_type)(void*);
4348 #define SQLITE_STATIC      ((sqlite3_destructor_type)0)
4349 #define SQLITE_TRANSIENT   ((sqlite3_destructor_type)-1)
4350 
4351 /*
4352 ** CAPI3REF: Setting The Result Of An SQL Function
4353 **
4354 ** These routines are used by the xFunc or xFinal callbacks that
4355 ** implement SQL functions and aggregates.  See
4356 ** [sqlite3_create_function()] and [sqlite3_create_function16()]
4357 ** for additional information.
4358 **
4359 ** These functions work very much like the [parameter binding] family of
4360 ** functions used to bind values to host parameters in prepared statements.
4361 ** Refer to the [SQL parameter] documentation for additional information.
4362 **
4363 ** ^The sqlite3_result_blob() interface sets the result from
4364 ** an application-defined function to be the BLOB whose content is pointed
4365 ** to by the second parameter and which is N bytes long where N is the
4366 ** third parameter.
4367 **
4368 ** ^The sqlite3_result_zeroblob() interfaces set the result of
4369 ** the application-defined function to be a BLOB containing all zero
4370 ** bytes and N bytes in size, where N is the value of the 2nd parameter.
4371 **
4372 ** ^The sqlite3_result_double() interface sets the result from
4373 ** an application-defined function to be a floating point value specified
4374 ** by its 2nd argument.
4375 **
4376 ** ^The sqlite3_result_error() and sqlite3_result_error16() functions
4377 ** cause the implemented SQL function to throw an exception.
4378 ** ^SQLite uses the string pointed to by the
4379 ** 2nd parameter of sqlite3_result_error() or sqlite3_result_error16()
4380 ** as the text of an error message.  ^SQLite interprets the error
4381 ** message string from sqlite3_result_error() as UTF-8. ^SQLite
4382 ** interprets the string from sqlite3_result_error16() as UTF-16 in native
4383 ** byte order.  ^If the third parameter to sqlite3_result_error()
4384 ** or sqlite3_result_error16() is negative then SQLite takes as the error
4385 ** message all text up through the first zero character.
4386 ** ^If the third parameter to sqlite3_result_error() or
4387 ** sqlite3_result_error16() is non-negative then SQLite takes that many
4388 ** bytes (not characters) from the 2nd parameter as the error message.
4389 ** ^The sqlite3_result_error() and sqlite3_result_error16()
4390 ** routines make a private copy of the error message text before
4391 ** they return.  Hence, the calling function can deallocate or
4392 ** modify the text after they return without harm.
4393 ** ^The sqlite3_result_error_code() function changes the error code
4394 ** returned by SQLite as a result of an error in a function.  ^By default,
4395 ** the error code is SQLITE_ERROR.  ^A subsequent call to sqlite3_result_error()
4396 ** or sqlite3_result_error16() resets the error code to SQLITE_ERROR.
4397 **
4398 ** ^The sqlite3_result_error_toobig() interface causes SQLite to throw an
4399 ** error indicating that a string or BLOB is too long to represent.
4400 **
4401 ** ^The sqlite3_result_error_nomem() interface causes SQLite to throw an
4402 ** error indicating that a memory allocation failed.
4403 **
4404 ** ^The sqlite3_result_int() interface sets the return value
4405 ** of the application-defined function to be the 32-bit signed integer
4406 ** value given in the 2nd argument.
4407 ** ^The sqlite3_result_int64() interface sets the return value
4408 ** of the application-defined function to be the 64-bit signed integer
4409 ** value given in the 2nd argument.
4410 **
4411 ** ^The sqlite3_result_null() interface sets the return value
4412 ** of the application-defined function to be NULL.
4413 **
4414 ** ^The sqlite3_result_text(), sqlite3_result_text16(),
4415 ** sqlite3_result_text16le(), and sqlite3_result_text16be() interfaces
4416 ** set the return value of the application-defined function to be
4417 ** a text string which is represented as UTF-8, UTF-16 native byte order,
4418 ** UTF-16 little endian, or UTF-16 big endian, respectively.
4419 ** ^SQLite takes the text result from the application from
4420 ** the 2nd parameter of the sqlite3_result_text* interfaces.
4421 ** ^If the 3rd parameter to the sqlite3_result_text* interfaces
4422 ** is negative, then SQLite takes result text from the 2nd parameter
4423 ** through the first zero character.
4424 ** ^If the 3rd parameter to the sqlite3_result_text* interfaces
4425 ** is non-negative, then as many bytes (not characters) of the text
4426 ** pointed to by the 2nd parameter are taken as the application-defined
4427 ** function result.  If the 3rd parameter is non-negative, then it
4428 ** must be the byte offset into the string where the NUL terminator would
4429 ** appear if the string where NUL terminated.  If any NUL characters occur
4430 ** in the string at a byte offset that is less than the value of the 3rd
4431 ** parameter, then the resulting string will contain embedded NULs and the
4432 ** result of expressions operating on strings with embedded NULs is undefined.
4433 ** ^If the 4th parameter to the sqlite3_result_text* interfaces
4434 ** or sqlite3_result_blob is a non-NULL pointer, then SQLite calls that
4435 ** function as the destructor on the text or BLOB result when it has
4436 ** finished using that result.
4437 ** ^If the 4th parameter to the sqlite3_result_text* interfaces or to
4438 ** sqlite3_result_blob is the special constant SQLITE_STATIC, then SQLite
4439 ** assumes that the text or BLOB result is in constant space and does not
4440 ** copy the content of the parameter nor call a destructor on the content
4441 ** when it has finished using that result.
4442 ** ^If the 4th parameter to the sqlite3_result_text* interfaces
4443 ** or sqlite3_result_blob is the special constant SQLITE_TRANSIENT
4444 ** then SQLite makes a copy of the result into space obtained from
4445 ** from [sqlite3_malloc()] before it returns.
4446 **
4447 ** ^The sqlite3_result_value() interface sets the result of
4448 ** the application-defined function to be a copy the
4449 ** [unprotected sqlite3_value] object specified by the 2nd parameter.  ^The
4450 ** sqlite3_result_value() interface makes a copy of the [sqlite3_value]
4451 ** so that the [sqlite3_value] specified in the parameter may change or
4452 ** be deallocated after sqlite3_result_value() returns without harm.
4453 ** ^A [protected sqlite3_value] object may always be used where an
4454 ** [unprotected sqlite3_value] object is required, so either
4455 ** kind of [sqlite3_value] object can be used with this interface.
4456 **
4457 ** If these routines are called from within the different thread
4458 ** than the one containing the application-defined function that received
4459 ** the [sqlite3_context] pointer, the results are undefined.
4460 */
4461 SQLITE_API void sqlite3_result_blob(sqlite3_context*, const void*, int, void(*)(void*));
4462 SQLITE_API void sqlite3_result_double(sqlite3_context*, double);
4463 SQLITE_API void sqlite3_result_error(sqlite3_context*, const char*, int);
4464 SQLITE_API void sqlite3_result_error16(sqlite3_context*, const void*, int);
4465 SQLITE_API void sqlite3_result_error_toobig(sqlite3_context*);
4466 SQLITE_API void sqlite3_result_error_nomem(sqlite3_context*);
4467 SQLITE_API void sqlite3_result_error_code(sqlite3_context*, int);
4468 SQLITE_API void sqlite3_result_int(sqlite3_context*, int);
4469 SQLITE_API void sqlite3_result_int64(sqlite3_context*, sqlite3_int64);
4470 SQLITE_API void sqlite3_result_null(sqlite3_context*);
4471 SQLITE_API void sqlite3_result_text(sqlite3_context*, const char*, int, void(*)(void*));
4472 SQLITE_API void sqlite3_result_text16(sqlite3_context*, const void*, int, void(*)(void*));
4473 SQLITE_API void sqlite3_result_text16le(sqlite3_context*, const void*, int,void(*)(void*));
4474 SQLITE_API void sqlite3_result_text16be(sqlite3_context*, const void*, int,void(*)(void*));
4475 SQLITE_API void sqlite3_result_value(sqlite3_context*, sqlite3_value*);
4476 SQLITE_API void sqlite3_result_zeroblob(sqlite3_context*, int n);
4477 
4478 /*
4479 ** CAPI3REF: Define New Collating Sequences
4480 **
4481 ** ^These functions add, remove, or modify a [collation] associated
4482 ** with the [database connection] specified as the first argument.
4483 **
4484 ** ^The name of the collation is a UTF-8 string
4485 ** for sqlite3_create_collation() and sqlite3_create_collation_v2()
4486 ** and a UTF-16 string in native byte order for sqlite3_create_collation16().
4487 ** ^Collation names that compare equal according to [sqlite3_strnicmp()] are
4488 ** considered to be the same name.
4489 **
4490 ** ^(The third argument (eTextRep) must be one of the constants:
4491 ** <ul>
4492 ** <li> [SQLITE_UTF8],
4493 ** <li> [SQLITE_UTF16LE],
4494 ** <li> [SQLITE_UTF16BE],
4495 ** <li> [SQLITE_UTF16], or
4496 ** <li> [SQLITE_UTF16_ALIGNED].
4497 ** </ul>)^
4498 ** ^The eTextRep argument determines the encoding of strings passed
4499 ** to the collating function callback, xCallback.
4500 ** ^The [SQLITE_UTF16] and [SQLITE_UTF16_ALIGNED] values for eTextRep
4501 ** force strings to be UTF16 with native byte order.
4502 ** ^The [SQLITE_UTF16_ALIGNED] value for eTextRep forces strings to begin
4503 ** on an even byte address.
4504 **
4505 ** ^The fourth argument, pArg, is an application data pointer that is passed
4506 ** through as the first argument to the collating function callback.
4507 **
4508 ** ^The fifth argument, xCallback, is a pointer to the collating function.
4509 ** ^Multiple collating functions can be registered using the same name but
4510 ** with different eTextRep parameters and SQLite will use whichever
4511 ** function requires the least amount of data transformation.
4512 ** ^If the xCallback argument is NULL then the collating function is
4513 ** deleted.  ^When all collating functions having the same name are deleted,
4514 ** that collation is no longer usable.
4515 **
4516 ** ^The collating function callback is invoked with a copy of the pArg
4517 ** application data pointer and with two strings in the encoding specified
4518 ** by the eTextRep argument.  The collating function must return an
4519 ** integer that is negative, zero, or positive
4520 ** if the first string is less than, equal to, or greater than the second,
4521 ** respectively.  A collating function must always return the same answer
4522 ** given the same inputs.  If two or more collating functions are registered
4523 ** to the same collation name (using different eTextRep values) then all
4524 ** must give an equivalent answer when invoked with equivalent strings.
4525 ** The collating function must obey the following properties for all
4526 ** strings A, B, and C:
4527 **
4528 ** <ol>
4529 ** <li> If A==B then B==A.
4530 ** <li> If A==B and B==C then A==C.
4531 ** <li> If A&lt;B THEN B&gt;A.
4532 ** <li> If A&lt;B and B&lt;C then A&lt;C.
4533 ** </ol>
4534 **
4535 ** If a collating function fails any of the above constraints and that
4536 ** collating function is  registered and used, then the behavior of SQLite
4537 ** is undefined.
4538 **
4539 ** ^The sqlite3_create_collation_v2() works like sqlite3_create_collation()
4540 ** with the addition that the xDestroy callback is invoked on pArg when
4541 ** the collating function is deleted.
4542 ** ^Collating functions are deleted when they are overridden by later
4543 ** calls to the collation creation functions or when the
4544 ** [database connection] is closed using [sqlite3_close()].
4545 **
4546 ** ^The xDestroy callback is <u>not</u> called if the
4547 ** sqlite3_create_collation_v2() function fails.  Applications that invoke
4548 ** sqlite3_create_collation_v2() with a non-NULL xDestroy argument should
4549 ** check the return code and dispose of the application data pointer
4550 ** themselves rather than expecting SQLite to deal with it for them.
4551 ** This is different from every other SQLite interface.  The inconsistency
4552 ** is unfortunate but cannot be changed without breaking backwards
4553 ** compatibility.
4554 **
4555 ** See also:  [sqlite3_collation_needed()] and [sqlite3_collation_needed16()].
4556 */
4557 SQLITE_API int sqlite3_create_collation(
4558   sqlite3*,
4559   const char *zName,
4560   int eTextRep,
4561   void *pArg,
4562   int(*xCompare)(void*,int,const void*,int,const void*)
4563 );
4564 SQLITE_API int sqlite3_create_collation_v2(
4565   sqlite3*,
4566   const char *zName,
4567   int eTextRep,
4568   void *pArg,
4569   int(*xCompare)(void*,int,const void*,int,const void*),
4570   void(*xDestroy)(void*)
4571 );
4572 SQLITE_API int sqlite3_create_collation16(
4573   sqlite3*,
4574   const void *zName,
4575   int eTextRep,
4576   void *pArg,
4577   int(*xCompare)(void*,int,const void*,int,const void*)
4578 );
4579 
4580 /*
4581 ** CAPI3REF: Collation Needed Callbacks
4582 **
4583 ** ^To avoid having to register all collation sequences before a database
4584 ** can be used, a single callback function may be registered with the
4585 ** [database connection] to be invoked whenever an undefined collation
4586 ** sequence is required.
4587 **
4588 ** ^If the function is registered using the sqlite3_collation_needed() API,
4589 ** then it is passed the names of undefined collation sequences as strings
4590 ** encoded in UTF-8. ^If sqlite3_collation_needed16() is used,
4591 ** the names are passed as UTF-16 in machine native byte order.
4592 ** ^A call to either function replaces the existing collation-needed callback.
4593 **
4594 ** ^(When the callback is invoked, the first argument passed is a copy
4595 ** of the second argument to sqlite3_collation_needed() or
4596 ** sqlite3_collation_needed16().  The second argument is the database
4597 ** connection.  The third argument is one of [SQLITE_UTF8], [SQLITE_UTF16BE],
4598 ** or [SQLITE_UTF16LE], indicating the most desirable form of the collation
4599 ** sequence function required.  The fourth parameter is the name of the
4600 ** required collation sequence.)^
4601 **
4602 ** The callback function should register the desired collation using
4603 ** [sqlite3_create_collation()], [sqlite3_create_collation16()], or
4604 ** [sqlite3_create_collation_v2()].
4605 */
4606 SQLITE_API int sqlite3_collation_needed(
4607   sqlite3*,
4608   void*,
4609   void(*)(void*,sqlite3*,int eTextRep,const char*)
4610 );
4611 SQLITE_API int sqlite3_collation_needed16(
4612   sqlite3*,
4613   void*,
4614   void(*)(void*,sqlite3*,int eTextRep,const void*)
4615 );
4616 
4617 #ifdef SQLITE_HAS_CODEC
4618 /*
4619 ** Specify the key for an encrypted database.  This routine should be
4620 ** called right after sqlite3_open().
4621 **
4622 ** The code to implement this API is not available in the public release
4623 ** of SQLite.
4624 */
4625 SQLITE_API int sqlite3_key(
4626   sqlite3 *db,                   /* Database to be rekeyed */
4627   const void *pKey, int nKey     /* The key */
4628 );
4629 SQLITE_API int sqlite3_key_v2(
4630   sqlite3 *db,                   /* Database to be rekeyed */
4631   const char *zDbName,           /* Name of the database */
4632   const void *pKey, int nKey     /* The key */
4633 );
4634 
4635 /*
4636 ** Change the key on an open database.  If the current database is not
4637 ** encrypted, this routine will encrypt it.  If pNew==0 or nNew==0, the
4638 ** database is decrypted.
4639 **
4640 ** The code to implement this API is not available in the public release
4641 ** of SQLite.
4642 */
4643 SQLITE_API int sqlite3_rekey(
4644   sqlite3 *db,                   /* Database to be rekeyed */
4645   const void *pKey, int nKey     /* The new key */
4646 );
4647 SQLITE_API int sqlite3_rekey_v2(
4648   sqlite3 *db,                   /* Database to be rekeyed */
4649   const char *zDbName,           /* Name of the database */
4650   const void *pKey, int nKey     /* The new key */
4651 );
4652 
4653 /*
4654 ** Specify the activation key for a SEE database.  Unless
4655 ** activated, none of the SEE routines will work.
4656 */
4657 SQLITE_API void sqlite3_activate_see(
4658   const char *zPassPhrase        /* Activation phrase */
4659 );
4660 #endif
4661 
4662 #ifdef SQLITE_ENABLE_CEROD
4663 /*
4664 ** Specify the activation key for a CEROD database.  Unless
4665 ** activated, none of the CEROD routines will work.
4666 */
4667 SQLITE_API void sqlite3_activate_cerod(
4668   const char *zPassPhrase        /* Activation phrase */
4669 );
4670 #endif
4671 
4672 /*
4673 ** CAPI3REF: Suspend Execution For A Short Time
4674 **
4675 ** The sqlite3_sleep() function causes the current thread to suspend execution
4676 ** for at least a number of milliseconds specified in its parameter.
4677 **
4678 ** If the operating system does not support sleep requests with
4679 ** millisecond time resolution, then the time will be rounded up to
4680 ** the nearest second. The number of milliseconds of sleep actually
4681 ** requested from the operating system is returned.
4682 **
4683 ** ^SQLite implements this interface by calling the xSleep()
4684 ** method of the default [sqlite3_vfs] object.  If the xSleep() method
4685 ** of the default VFS is not implemented correctly, or not implemented at
4686 ** all, then the behavior of sqlite3_sleep() may deviate from the description
4687 ** in the previous paragraphs.
4688 */
4689 SQLITE_API int sqlite3_sleep(int);
4690 
4691 /*
4692 ** CAPI3REF: Name Of The Folder Holding Temporary Files
4693 **
4694 ** ^(If this global variable is made to point to a string which is
4695 ** the name of a folder (a.k.a. directory), then all temporary files
4696 ** created by SQLite when using a built-in [sqlite3_vfs | VFS]
4697 ** will be placed in that directory.)^  ^If this variable
4698 ** is a NULL pointer, then SQLite performs a search for an appropriate
4699 ** temporary file directory.
4700 **
4701 ** It is not safe to read or modify this variable in more than one
4702 ** thread at a time.  It is not safe to read or modify this variable
4703 ** if a [database connection] is being used at the same time in a separate
4704 ** thread.
4705 ** It is intended that this variable be set once
4706 ** as part of process initialization and before any SQLite interface
4707 ** routines have been called and that this variable remain unchanged
4708 ** thereafter.
4709 **
4710 ** ^The [temp_store_directory pragma] may modify this variable and cause
4711 ** it to point to memory obtained from [sqlite3_malloc].  ^Furthermore,
4712 ** the [temp_store_directory pragma] always assumes that any string
4713 ** that this variable points to is held in memory obtained from
4714 ** [sqlite3_malloc] and the pragma may attempt to free that memory
4715 ** using [sqlite3_free].
4716 ** Hence, if this variable is modified directly, either it should be
4717 ** made NULL or made to point to memory obtained from [sqlite3_malloc]
4718 ** or else the use of the [temp_store_directory pragma] should be avoided.
4719 **
4720 ** <b>Note to Windows Runtime users:</b>  The temporary directory must be set
4721 ** prior to calling [sqlite3_open] or [sqlite3_open_v2].  Otherwise, various
4722 ** features that require the use of temporary files may fail.  Here is an
4723 ** example of how to do this using C++ with the Windows Runtime:
4724 **
4725 ** <blockquote><pre>
4726 ** LPCWSTR zPath = Windows::Storage::ApplicationData::Current->
4727 ** &nbsp;     TemporaryFolder->Path->Data();
4728 ** char zPathBuf&#91;MAX_PATH + 1&#93;;
4729 ** memset(zPathBuf, 0, sizeof(zPathBuf));
4730 ** WideCharToMultiByte(CP_UTF8, 0, zPath, -1, zPathBuf, sizeof(zPathBuf),
4731 ** &nbsp;     NULL, NULL);
4732 ** sqlite3_temp_directory = sqlite3_mprintf("%s", zPathBuf);
4733 ** </pre></blockquote>
4734 */
4735 SQLITE_API char *sqlite3_temp_directory;
4736 
4737 /*
4738 ** CAPI3REF: Name Of The Folder Holding Database Files
4739 **
4740 ** ^(If this global variable is made to point to a string which is
4741 ** the name of a folder (a.k.a. directory), then all database files
4742 ** specified with a relative pathname and created or accessed by
4743 ** SQLite when using a built-in windows [sqlite3_vfs | VFS] will be assumed
4744 ** to be relative to that directory.)^ ^If this variable is a NULL
4745 ** pointer, then SQLite assumes that all database files specified
4746 ** with a relative pathname are relative to the current directory
4747 ** for the process.  Only the windows VFS makes use of this global
4748 ** variable; it is ignored by the unix VFS.
4749 **
4750 ** Changing the value of this variable while a database connection is
4751 ** open can result in a corrupt database.
4752 **
4753 ** It is not safe to read or modify this variable in more than one
4754 ** thread at a time.  It is not safe to read or modify this variable
4755 ** if a [database connection] is being used at the same time in a separate
4756 ** thread.
4757 ** It is intended that this variable be set once
4758 ** as part of process initialization and before any SQLite interface
4759 ** routines have been called and that this variable remain unchanged
4760 ** thereafter.
4761 **
4762 ** ^The [data_store_directory pragma] may modify this variable and cause
4763 ** it to point to memory obtained from [sqlite3_malloc].  ^Furthermore,
4764 ** the [data_store_directory pragma] always assumes that any string
4765 ** that this variable points to is held in memory obtained from
4766 ** [sqlite3_malloc] and the pragma may attempt to free that memory
4767 ** using [sqlite3_free].
4768 ** Hence, if this variable is modified directly, either it should be
4769 ** made NULL or made to point to memory obtained from [sqlite3_malloc]
4770 ** or else the use of the [data_store_directory pragma] should be avoided.
4771 */
4772 SQLITE_API char *sqlite3_data_directory;
4773 
4774 /*
4775 ** CAPI3REF: Test For Auto-Commit Mode
4776 ** KEYWORDS: {autocommit mode}
4777 **
4778 ** ^The sqlite3_get_autocommit() interface returns non-zero or
4779 ** zero if the given database connection is or is not in autocommit mode,
4780 ** respectively.  ^Autocommit mode is on by default.
4781 ** ^Autocommit mode is disabled by a [BEGIN] statement.
4782 ** ^Autocommit mode is re-enabled by a [COMMIT] or [ROLLBACK].
4783 **
4784 ** If certain kinds of errors occur on a statement within a multi-statement
4785 ** transaction (errors including [SQLITE_FULL], [SQLITE_IOERR],
4786 ** [SQLITE_NOMEM], [SQLITE_BUSY], and [SQLITE_INTERRUPT]) then the
4787 ** transaction might be rolled back automatically.  The only way to
4788 ** find out whether SQLite automatically rolled back the transaction after
4789 ** an error is to use this function.
4790 **
4791 ** If another thread changes the autocommit status of the database
4792 ** connection while this routine is running, then the return value
4793 ** is undefined.
4794 */
4795 SQLITE_API int sqlite3_get_autocommit(sqlite3*);
4796 
4797 /*
4798 ** CAPI3REF: Find The Database Handle Of A Prepared Statement
4799 **
4800 ** ^The sqlite3_db_handle interface returns the [database connection] handle
4801 ** to which a [prepared statement] belongs.  ^The [database connection]
4802 ** returned by sqlite3_db_handle is the same [database connection]
4803 ** that was the first argument
4804 ** to the [sqlite3_prepare_v2()] call (or its variants) that was used to
4805 ** create the statement in the first place.
4806 */
4807 SQLITE_API sqlite3 *sqlite3_db_handle(sqlite3_stmt*);
4808 
4809 /*
4810 ** CAPI3REF: Return The Filename For A Database Connection
4811 **
4812 ** ^The sqlite3_db_filename(D,N) interface returns a pointer to a filename
4813 ** associated with database N of connection D.  ^The main database file
4814 ** has the name "main".  If there is no attached database N on the database
4815 ** connection D, or if database N is a temporary or in-memory database, then
4816 ** a NULL pointer is returned.
4817 **
4818 ** ^The filename returned by this function is the output of the
4819 ** xFullPathname method of the [VFS].  ^In other words, the filename
4820 ** will be an absolute pathname, even if the filename used
4821 ** to open the database originally was a URI or relative pathname.
4822 */
4823 SQLITE_API const char *sqlite3_db_filename(sqlite3 *db, const char *zDbName);
4824 
4825 /*
4826 ** CAPI3REF: Determine if a database is read-only
4827 **
4828 ** ^The sqlite3_db_readonly(D,N) interface returns 1 if the database N
4829 ** of connection D is read-only, 0 if it is read/write, or -1 if N is not
4830 ** the name of a database on connection D.
4831 */
4832 SQLITE_API int sqlite3_db_readonly(sqlite3 *db, const char *zDbName);
4833 
4834 /*
4835 ** CAPI3REF: Find the next prepared statement
4836 **
4837 ** ^This interface returns a pointer to the next [prepared statement] after
4838 ** pStmt associated with the [database connection] pDb.  ^If pStmt is NULL
4839 ** then this interface returns a pointer to the first prepared statement
4840 ** associated with the database connection pDb.  ^If no prepared statement
4841 ** satisfies the conditions of this routine, it returns NULL.
4842 **
4843 ** The [database connection] pointer D in a call to
4844 ** [sqlite3_next_stmt(D,S)] must refer to an open database
4845 ** connection and in particular must not be a NULL pointer.
4846 */
4847 SQLITE_API sqlite3_stmt *sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt);
4848 
4849 /*
4850 ** CAPI3REF: Commit And Rollback Notification Callbacks
4851 **
4852 ** ^The sqlite3_commit_hook() interface registers a callback
4853 ** function to be invoked whenever a transaction is [COMMIT | committed].
4854 ** ^Any callback set by a previous call to sqlite3_commit_hook()
4855 ** for the same database connection is overridden.
4856 ** ^The sqlite3_rollback_hook() interface registers a callback
4857 ** function to be invoked whenever a transaction is [ROLLBACK | rolled back].
4858 ** ^Any callback set by a previous call to sqlite3_rollback_hook()
4859 ** for the same database connection is overridden.
4860 ** ^The pArg argument is passed through to the callback.
4861 ** ^If the callback on a commit hook function returns non-zero,
4862 ** then the commit is converted into a rollback.
4863 **
4864 ** ^The sqlite3_commit_hook(D,C,P) and sqlite3_rollback_hook(D,C,P) functions
4865 ** return the P argument from the previous call of the same function
4866 ** on the same [database connection] D, or NULL for
4867 ** the first call for each function on D.
4868 **
4869 ** The commit and rollback hook callbacks are not reentrant.
4870 ** The callback implementation must not do anything that will modify
4871 ** the database connection that invoked the callback.  Any actions
4872 ** to modify the database connection must be deferred until after the
4873 ** completion of the [sqlite3_step()] call that triggered the commit
4874 ** or rollback hook in the first place.
4875 ** Note that running any other SQL statements, including SELECT statements,
4876 ** or merely calling [sqlite3_prepare_v2()] and [sqlite3_step()] will modify
4877 ** the database connections for the meaning of "modify" in this paragraph.
4878 **
4879 ** ^Registering a NULL function disables the callback.
4880 **
4881 ** ^When the commit hook callback routine returns zero, the [COMMIT]
4882 ** operation is allowed to continue normally.  ^If the commit hook
4883 ** returns non-zero, then the [COMMIT] is converted into a [ROLLBACK].
4884 ** ^The rollback hook is invoked on a rollback that results from a commit
4885 ** hook returning non-zero, just as it would be with any other rollback.
4886 **
4887 ** ^For the purposes of this API, a transaction is said to have been
4888 ** rolled back if an explicit "ROLLBACK" statement is executed, or
4889 ** an error or constraint causes an implicit rollback to occur.
4890 ** ^The rollback callback is not invoked if a transaction is
4891 ** automatically rolled back because the database connection is closed.
4892 **
4893 ** See also the [sqlite3_update_hook()] interface.
4894 */
4895 SQLITE_API void *sqlite3_commit_hook(sqlite3*, int(*)(void*), void*);
4896 SQLITE_API void *sqlite3_rollback_hook(sqlite3*, void(*)(void *), void*);
4897 
4898 /*
4899 ** CAPI3REF: Data Change Notification Callbacks
4900 **
4901 ** ^The sqlite3_update_hook() interface registers a callback function
4902 ** with the [database connection] identified by the first argument
4903 ** to be invoked whenever a row is updated, inserted or deleted in
4904 ** a rowid table.
4905 ** ^Any callback set by a previous call to this function
4906 ** for the same database connection is overridden.
4907 **
4908 ** ^The second argument is a pointer to the function to invoke when a
4909 ** row is updated, inserted or deleted in a rowid table.
4910 ** ^The first argument to the callback is a copy of the third argument
4911 ** to sqlite3_update_hook().
4912 ** ^The second callback argument is one of [SQLITE_INSERT], [SQLITE_DELETE],
4913 ** or [SQLITE_UPDATE], depending on the operation that caused the callback
4914 ** to be invoked.
4915 ** ^The third and fourth arguments to the callback contain pointers to the
4916 ** database and table name containing the affected row.
4917 ** ^The final callback parameter is the [rowid] of the row.
4918 ** ^In the case of an update, this is the [rowid] after the update takes place.
4919 **
4920 ** ^(The update hook is not invoked when internal system tables are
4921 ** modified (i.e. sqlite_master and sqlite_sequence).)^
4922 ** ^The update hook is not invoked when [WITHOUT ROWID] tables are modified.
4923 **
4924 ** ^In the current implementation, the update hook
4925 ** is not invoked when duplication rows are deleted because of an
4926 ** [ON CONFLICT | ON CONFLICT REPLACE] clause.  ^Nor is the update hook
4927 ** invoked when rows are deleted using the [truncate optimization].
4928 ** The exceptions defined in this paragraph might change in a future
4929 ** release of SQLite.
4930 **
4931 ** The update hook implementation must not do anything that will modify
4932 ** the database connection that invoked the update hook.  Any actions
4933 ** to modify the database connection must be deferred until after the
4934 ** completion of the [sqlite3_step()] call that triggered the update hook.
4935 ** Note that [sqlite3_prepare_v2()] and [sqlite3_step()] both modify their
4936 ** database connections for the meaning of "modify" in this paragraph.
4937 **
4938 ** ^The sqlite3_update_hook(D,C,P) function
4939 ** returns the P argument from the previous call
4940 ** on the same [database connection] D, or NULL for
4941 ** the first call on D.
4942 **
4943 ** See also the [sqlite3_commit_hook()] and [sqlite3_rollback_hook()]
4944 ** interfaces.
4945 */
4946 SQLITE_API void *sqlite3_update_hook(
4947   sqlite3*,
4948   void(*)(void *,int ,char const *,char const *,sqlite3_int64),
4949   void*
4950 );
4951 
4952 /*
4953 ** CAPI3REF: Enable Or Disable Shared Pager Cache
4954 **
4955 ** ^(This routine enables or disables the sharing of the database cache
4956 ** and schema data structures between [database connection | connections]
4957 ** to the same database. Sharing is enabled if the argument is true
4958 ** and disabled if the argument is false.)^
4959 **
4960 ** ^Cache sharing is enabled and disabled for an entire process.
4961 ** This is a change as of SQLite version 3.5.0. In prior versions of SQLite,
4962 ** sharing was enabled or disabled for each thread separately.
4963 **
4964 ** ^(The cache sharing mode set by this interface effects all subsequent
4965 ** calls to [sqlite3_open()], [sqlite3_open_v2()], and [sqlite3_open16()].
4966 ** Existing database connections continue use the sharing mode
4967 ** that was in effect at the time they were opened.)^
4968 **
4969 ** ^(This routine returns [SQLITE_OK] if shared cache was enabled or disabled
4970 ** successfully.  An [error code] is returned otherwise.)^
4971 **
4972 ** ^Shared cache is disabled by default. But this might change in
4973 ** future releases of SQLite.  Applications that care about shared
4974 ** cache setting should set it explicitly.
4975 **
4976 ** This interface is threadsafe on processors where writing a
4977 ** 32-bit integer is atomic.
4978 **
4979 ** See Also:  [SQLite Shared-Cache Mode]
4980 */
4981 SQLITE_API int sqlite3_enable_shared_cache(int);
4982 
4983 /*
4984 ** CAPI3REF: Attempt To Free Heap Memory
4985 **
4986 ** ^The sqlite3_release_memory() interface attempts to free N bytes
4987 ** of heap memory by deallocating non-essential memory allocations
4988 ** held by the database library.   Memory used to cache database
4989 ** pages to improve performance is an example of non-essential memory.
4990 ** ^sqlite3_release_memory() returns the number of bytes actually freed,
4991 ** which might be more or less than the amount requested.
4992 ** ^The sqlite3_release_memory() routine is a no-op returning zero
4993 ** if SQLite is not compiled with [SQLITE_ENABLE_MEMORY_MANAGEMENT].
4994 **
4995 ** See also: [sqlite3_db_release_memory()]
4996 */
4997 SQLITE_API int sqlite3_release_memory(int);
4998 
4999 /*
5000 ** CAPI3REF: Free Memory Used By A Database Connection
5001 **
5002 ** ^The sqlite3_db_release_memory(D) interface attempts to free as much heap
5003 ** memory as possible from database connection D. Unlike the
5004 ** [sqlite3_release_memory()] interface, this interface is in effect even
5005 ** when the [SQLITE_ENABLE_MEMORY_MANAGEMENT] compile-time option is
5006 ** omitted.
5007 **
5008 ** See also: [sqlite3_release_memory()]
5009 */
5010 SQLITE_API int sqlite3_db_release_memory(sqlite3*);
5011 
5012 /*
5013 ** CAPI3REF: Impose A Limit On Heap Size
5014 **
5015 ** ^The sqlite3_soft_heap_limit64() interface sets and/or queries the
5016 ** soft limit on the amount of heap memory that may be allocated by SQLite.
5017 ** ^SQLite strives to keep heap memory utilization below the soft heap
5018 ** limit by reducing the number of pages held in the page cache
5019 ** as heap memory usages approaches the limit.
5020 ** ^The soft heap limit is "soft" because even though SQLite strives to stay
5021 ** below the limit, it will exceed the limit rather than generate
5022 ** an [SQLITE_NOMEM] error.  In other words, the soft heap limit
5023 ** is advisory only.
5024 **
5025 ** ^The return value from sqlite3_soft_heap_limit64() is the size of
5026 ** the soft heap limit prior to the call, or negative in the case of an
5027 ** error.  ^If the argument N is negative
5028 ** then no change is made to the soft heap limit.  Hence, the current
5029 ** size of the soft heap limit can be determined by invoking
5030 ** sqlite3_soft_heap_limit64() with a negative argument.
5031 **
5032 ** ^If the argument N is zero then the soft heap limit is disabled.
5033 **
5034 ** ^(The soft heap limit is not enforced in the current implementation
5035 ** if one or more of following conditions are true:
5036 **
5037 ** <ul>
5038 ** <li> The soft heap limit is set to zero.
5039 ** <li> Memory accounting is disabled using a combination of the
5040 **      [sqlite3_config]([SQLITE_CONFIG_MEMSTATUS],...) start-time option and
5041 **      the [SQLITE_DEFAULT_MEMSTATUS] compile-time option.
5042 ** <li> An alternative page cache implementation is specified using
5043 **      [sqlite3_config]([SQLITE_CONFIG_PCACHE2],...).
5044 ** <li> The page cache allocates from its own memory pool supplied
5045 **      by [sqlite3_config]([SQLITE_CONFIG_PAGECACHE],...) rather than
5046 **      from the heap.
5047 ** </ul>)^
5048 **
5049 ** Beginning with SQLite version 3.7.3, the soft heap limit is enforced
5050 ** regardless of whether or not the [SQLITE_ENABLE_MEMORY_MANAGEMENT]
5051 ** compile-time option is invoked.  With [SQLITE_ENABLE_MEMORY_MANAGEMENT],
5052 ** the soft heap limit is enforced on every memory allocation.  Without
5053 ** [SQLITE_ENABLE_MEMORY_MANAGEMENT], the soft heap limit is only enforced
5054 ** when memory is allocated by the page cache.  Testing suggests that because
5055 ** the page cache is the predominate memory user in SQLite, most
5056 ** applications will achieve adequate soft heap limit enforcement without
5057 ** the use of [SQLITE_ENABLE_MEMORY_MANAGEMENT].
5058 **
5059 ** The circumstances under which SQLite will enforce the soft heap limit may
5060 ** changes in future releases of SQLite.
5061 */
5062 SQLITE_API sqlite3_int64 sqlite3_soft_heap_limit64(sqlite3_int64 N);
5063 
5064 /*
5065 ** CAPI3REF: Deprecated Soft Heap Limit Interface
5066 ** DEPRECATED
5067 **
5068 ** This is a deprecated version of the [sqlite3_soft_heap_limit64()]
5069 ** interface.  This routine is provided for historical compatibility
5070 ** only.  All new applications should use the
5071 ** [sqlite3_soft_heap_limit64()] interface rather than this one.
5072 */
5073 SQLITE_API SQLITE_DEPRECATED void sqlite3_soft_heap_limit(int N);
5074 
5075 
5076 /*
5077 ** CAPI3REF: Extract Metadata About A Column Of A Table
5078 **
5079 ** ^This routine returns metadata about a specific column of a specific
5080 ** database table accessible using the [database connection] handle
5081 ** passed as the first function argument.
5082 **
5083 ** ^The column is identified by the second, third and fourth parameters to
5084 ** this function. ^The second parameter is either the name of the database
5085 ** (i.e. "main", "temp", or an attached database) containing the specified
5086 ** table or NULL. ^If it is NULL, then all attached databases are searched
5087 ** for the table using the same algorithm used by the database engine to
5088 ** resolve unqualified table references.
5089 **
5090 ** ^The third and fourth parameters to this function are the table and column
5091 ** name of the desired column, respectively. Neither of these parameters
5092 ** may be NULL.
5093 **
5094 ** ^Metadata is returned by writing to the memory locations passed as the 5th
5095 ** and subsequent parameters to this function. ^Any of these arguments may be
5096 ** NULL, in which case the corresponding element of metadata is omitted.
5097 **
5098 ** ^(<blockquote>
5099 ** <table border="1">
5100 ** <tr><th> Parameter <th> Output<br>Type <th>  Description
5101 **
5102 ** <tr><td> 5th <td> const char* <td> Data type
5103 ** <tr><td> 6th <td> const char* <td> Name of default collation sequence
5104 ** <tr><td> 7th <td> int         <td> True if column has a NOT NULL constraint
5105 ** <tr><td> 8th <td> int         <td> True if column is part of the PRIMARY KEY
5106 ** <tr><td> 9th <td> int         <td> True if column is [AUTOINCREMENT]
5107 ** </table>
5108 ** </blockquote>)^
5109 **
5110 ** ^The memory pointed to by the character pointers returned for the
5111 ** declaration type and collation sequence is valid only until the next
5112 ** call to any SQLite API function.
5113 **
5114 ** ^If the specified table is actually a view, an [error code] is returned.
5115 **
5116 ** ^If the specified column is "rowid", "oid" or "_rowid_" and an
5117 ** [INTEGER PRIMARY KEY] column has been explicitly declared, then the output
5118 ** parameters are set for the explicitly declared column. ^(If there is no
5119 ** explicitly declared [INTEGER PRIMARY KEY] column, then the output
5120 ** parameters are set as follows:
5121 **
5122 ** <pre>
5123 **     data type: "INTEGER"
5124 **     collation sequence: "BINARY"
5125 **     not null: 0
5126 **     primary key: 1
5127 **     auto increment: 0
5128 ** </pre>)^
5129 **
5130 ** ^(This function may load one or more schemas from database files. If an
5131 ** error occurs during this process, or if the requested table or column
5132 ** cannot be found, an [error code] is returned and an error message left
5133 ** in the [database connection] (to be retrieved using sqlite3_errmsg()).)^
5134 **
5135 ** ^This API is only available if the library was compiled with the
5136 ** [SQLITE_ENABLE_COLUMN_METADATA] C-preprocessor symbol defined.
5137 */
5138 SQLITE_API int sqlite3_table_column_metadata(
5139   sqlite3 *db,                /* Connection handle */
5140   const char *zDbName,        /* Database name or NULL */
5141   const char *zTableName,     /* Table name */
5142   const char *zColumnName,    /* Column name */
5143   char const **pzDataType,    /* OUTPUT: Declared data type */
5144   char const **pzCollSeq,     /* OUTPUT: Collation sequence name */
5145   int *pNotNull,              /* OUTPUT: True if NOT NULL constraint exists */
5146   int *pPrimaryKey,           /* OUTPUT: True if column part of PK */
5147   int *pAutoinc               /* OUTPUT: True if column is auto-increment */
5148 );
5149 
5150 /*
5151 ** CAPI3REF: Load An Extension
5152 **
5153 ** ^This interface loads an SQLite extension library from the named file.
5154 **
5155 ** ^The sqlite3_load_extension() interface attempts to load an
5156 ** [SQLite extension] library contained in the file zFile.  If
5157 ** the file cannot be loaded directly, attempts are made to load
5158 ** with various operating-system specific extensions added.
5159 ** So for example, if "samplelib" cannot be loaded, then names like
5160 ** "samplelib.so" or "samplelib.dylib" or "samplelib.dll" might
5161 ** be tried also.
5162 **
5163 ** ^The entry point is zProc.
5164 ** ^(zProc may be 0, in which case SQLite will try to come up with an
5165 ** entry point name on its own.  It first tries "sqlite3_extension_init".
5166 ** If that does not work, it constructs a name "sqlite3_X_init" where the
5167 ** X is consists of the lower-case equivalent of all ASCII alphabetic
5168 ** characters in the filename from the last "/" to the first following
5169 ** "." and omitting any initial "lib".)^
5170 ** ^The sqlite3_load_extension() interface returns
5171 ** [SQLITE_OK] on success and [SQLITE_ERROR] if something goes wrong.
5172 ** ^If an error occurs and pzErrMsg is not 0, then the
5173 ** [sqlite3_load_extension()] interface shall attempt to
5174 ** fill *pzErrMsg with error message text stored in memory
5175 ** obtained from [sqlite3_malloc()]. The calling function
5176 ** should free this memory by calling [sqlite3_free()].
5177 **
5178 ** ^Extension loading must be enabled using
5179 ** [sqlite3_enable_load_extension()] prior to calling this API,
5180 ** otherwise an error will be returned.
5181 **
5182 ** See also the [load_extension() SQL function].
5183 */
5184 SQLITE_API int sqlite3_load_extension(
5185   sqlite3 *db,          /* Load the extension into this database connection */
5186   const char *zFile,    /* Name of the shared library containing extension */
5187   const char *zProc,    /* Entry point.  Derived from zFile if 0 */
5188   char **pzErrMsg       /* Put error message here if not 0 */
5189 );
5190 
5191 /*
5192 ** CAPI3REF: Enable Or Disable Extension Loading
5193 **
5194 ** ^So as not to open security holes in older applications that are
5195 ** unprepared to deal with [extension loading], and as a means of disabling
5196 ** [extension loading] while evaluating user-entered SQL, the following API
5197 ** is provided to turn the [sqlite3_load_extension()] mechanism on and off.
5198 **
5199 ** ^Extension loading is off by default.
5200 ** ^Call the sqlite3_enable_load_extension() routine with onoff==1
5201 ** to turn extension loading on and call it with onoff==0 to turn
5202 ** it back off again.
5203 */
5204 SQLITE_API int sqlite3_enable_load_extension(sqlite3 *db, int onoff);
5205 
5206 /*
5207 ** CAPI3REF: Automatically Load Statically Linked Extensions
5208 **
5209 ** ^This interface causes the xEntryPoint() function to be invoked for
5210 ** each new [database connection] that is created.  The idea here is that
5211 ** xEntryPoint() is the entry point for a statically linked [SQLite extension]
5212 ** that is to be automatically loaded into all new database connections.
5213 **
5214 ** ^(Even though the function prototype shows that xEntryPoint() takes
5215 ** no arguments and returns void, SQLite invokes xEntryPoint() with three
5216 ** arguments and expects and integer result as if the signature of the
5217 ** entry point where as follows:
5218 **
5219 ** <blockquote><pre>
5220 ** &nbsp;  int xEntryPoint(
5221 ** &nbsp;    sqlite3 *db,
5222 ** &nbsp;    const char **pzErrMsg,
5223 ** &nbsp;    const struct sqlite3_api_routines *pThunk
5224 ** &nbsp;  );
5225 ** </pre></blockquote>)^
5226 **
5227 ** If the xEntryPoint routine encounters an error, it should make *pzErrMsg
5228 ** point to an appropriate error message (obtained from [sqlite3_mprintf()])
5229 ** and return an appropriate [error code].  ^SQLite ensures that *pzErrMsg
5230 ** is NULL before calling the xEntryPoint().  ^SQLite will invoke
5231 ** [sqlite3_free()] on *pzErrMsg after xEntryPoint() returns.  ^If any
5232 ** xEntryPoint() returns an error, the [sqlite3_open()], [sqlite3_open16()],
5233 ** or [sqlite3_open_v2()] call that provoked the xEntryPoint() will fail.
5234 **
5235 ** ^Calling sqlite3_auto_extension(X) with an entry point X that is already
5236 ** on the list of automatic extensions is a harmless no-op. ^No entry point
5237 ** will be called more than once for each database connection that is opened.
5238 **
5239 ** See also: [sqlite3_reset_auto_extension()]
5240 ** and [sqlite3_cancel_auto_extension()]
5241 */
5242 SQLITE_API int sqlite3_auto_extension(void (*xEntryPoint)(void));
5243 
5244 /*
5245 ** CAPI3REF: Cancel Automatic Extension Loading
5246 **
5247 ** ^The [sqlite3_cancel_auto_extension(X)] interface unregisters the
5248 ** initialization routine X that was registered using a prior call to
5249 ** [sqlite3_auto_extension(X)].  ^The [sqlite3_cancel_auto_extension(X)]
5250 ** routine returns 1 if initialization routine X was successfully
5251 ** unregistered and it returns 0 if X was not on the list of initialization
5252 ** routines.
5253 */
5254 SQLITE_API int sqlite3_cancel_auto_extension(void (*xEntryPoint)(void));
5255 
5256 /*
5257 ** CAPI3REF: Reset Automatic Extension Loading
5258 **
5259 ** ^This interface disables all automatic extensions previously
5260 ** registered using [sqlite3_auto_extension()].
5261 */
5262 SQLITE_API void sqlite3_reset_auto_extension(void);
5263 
5264 /*
5265 ** The interface to the virtual-table mechanism is currently considered
5266 ** to be experimental.  The interface might change in incompatible ways.
5267 ** If this is a problem for you, do not use the interface at this time.
5268 **
5269 ** When the virtual-table mechanism stabilizes, we will declare the
5270 ** interface fixed, support it indefinitely, and remove this comment.
5271 */
5272 
5273 /*
5274 ** Structures used by the virtual table interface
5275 */
5276 typedef struct sqlite3_vtab sqlite3_vtab;
5277 typedef struct sqlite3_index_info sqlite3_index_info;
5278 typedef struct sqlite3_vtab_cursor sqlite3_vtab_cursor;
5279 typedef struct sqlite3_module sqlite3_module;
5280 
5281 /*
5282 ** CAPI3REF: Virtual Table Object
5283 ** KEYWORDS: sqlite3_module {virtual table module}
5284 **
5285 ** This structure, sometimes called a "virtual table module",
5286 ** defines the implementation of a [virtual tables].
5287 ** This structure consists mostly of methods for the module.
5288 **
5289 ** ^A virtual table module is created by filling in a persistent
5290 ** instance of this structure and passing a pointer to that instance
5291 ** to [sqlite3_create_module()] or [sqlite3_create_module_v2()].
5292 ** ^The registration remains valid until it is replaced by a different
5293 ** module or until the [database connection] closes.  The content
5294 ** of this structure must not change while it is registered with
5295 ** any database connection.
5296 */
5297 struct sqlite3_module {
5298   int iVersion;
5299   int (*xCreate)(sqlite3*, void *pAux,
5300                int argc, const char *const*argv,
5301                sqlite3_vtab **ppVTab, char**);
5302   int (*xConnect)(sqlite3*, void *pAux,
5303                int argc, const char *const*argv,
5304                sqlite3_vtab **ppVTab, char**);
5305   int (*xBestIndex)(sqlite3_vtab *pVTab, sqlite3_index_info*);
5306   int (*xDisconnect)(sqlite3_vtab *pVTab);
5307   int (*xDestroy)(sqlite3_vtab *pVTab);
5308   int (*xOpen)(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor);
5309   int (*xClose)(sqlite3_vtab_cursor*);
5310   int (*xFilter)(sqlite3_vtab_cursor*, int idxNum, const char *idxStr,
5311                 int argc, sqlite3_value **argv);
5312   int (*xNext)(sqlite3_vtab_cursor*);
5313   int (*xEof)(sqlite3_vtab_cursor*);
5314   int (*xColumn)(sqlite3_vtab_cursor*, sqlite3_context*, int);
5315   int (*xRowid)(sqlite3_vtab_cursor*, sqlite3_int64 *pRowid);
5316   int (*xUpdate)(sqlite3_vtab *, int, sqlite3_value **, sqlite3_int64 *);
5317   int (*xBegin)(sqlite3_vtab *pVTab);
5318   int (*xSync)(sqlite3_vtab *pVTab);
5319   int (*xCommit)(sqlite3_vtab *pVTab);
5320   int (*xRollback)(sqlite3_vtab *pVTab);
5321   int (*xFindFunction)(sqlite3_vtab *pVtab, int nArg, const char *zName,
5322                        void (**pxFunc)(sqlite3_context*,int,sqlite3_value**),
5323                        void **ppArg);
5324   int (*xRename)(sqlite3_vtab *pVtab, const char *zNew);
5325   /* The methods above are in version 1 of the sqlite_module object. Those
5326   ** below are for version 2 and greater. */
5327   int (*xSavepoint)(sqlite3_vtab *pVTab, int);
5328   int (*xRelease)(sqlite3_vtab *pVTab, int);
5329   int (*xRollbackTo)(sqlite3_vtab *pVTab, int);
5330 };
5331 
5332 /*
5333 ** CAPI3REF: Virtual Table Indexing Information
5334 ** KEYWORDS: sqlite3_index_info
5335 **
5336 ** The sqlite3_index_info structure and its substructures is used as part
5337 ** of the [virtual table] interface to
5338 ** pass information into and receive the reply from the [xBestIndex]
5339 ** method of a [virtual table module].  The fields under **Inputs** are the
5340 ** inputs to xBestIndex and are read-only.  xBestIndex inserts its
5341 ** results into the **Outputs** fields.
5342 **
5343 ** ^(The aConstraint[] array records WHERE clause constraints of the form:
5344 **
5345 ** <blockquote>column OP expr</blockquote>
5346 **
5347 ** where OP is =, &lt;, &lt;=, &gt;, or &gt;=.)^  ^(The particular operator is
5348 ** stored in aConstraint[].op using one of the
5349 ** [SQLITE_INDEX_CONSTRAINT_EQ | SQLITE_INDEX_CONSTRAINT_ values].)^
5350 ** ^(The index of the column is stored in
5351 ** aConstraint[].iColumn.)^  ^(aConstraint[].usable is TRUE if the
5352 ** expr on the right-hand side can be evaluated (and thus the constraint
5353 ** is usable) and false if it cannot.)^
5354 **
5355 ** ^The optimizer automatically inverts terms of the form "expr OP column"
5356 ** and makes other simplifications to the WHERE clause in an attempt to
5357 ** get as many WHERE clause terms into the form shown above as possible.
5358 ** ^The aConstraint[] array only reports WHERE clause terms that are
5359 ** relevant to the particular virtual table being queried.
5360 **
5361 ** ^Information about the ORDER BY clause is stored in aOrderBy[].
5362 ** ^Each term of aOrderBy records a column of the ORDER BY clause.
5363 **
5364 ** The [xBestIndex] method must fill aConstraintUsage[] with information
5365 ** about what parameters to pass to xFilter.  ^If argvIndex>0 then
5366 ** the right-hand side of the corresponding aConstraint[] is evaluated
5367 ** and becomes the argvIndex-th entry in argv.  ^(If aConstraintUsage[].omit
5368 ** is true, then the constraint is assumed to be fully handled by the
5369 ** virtual table and is not checked again by SQLite.)^
5370 **
5371 ** ^The idxNum and idxPtr values are recorded and passed into the
5372 ** [xFilter] method.
5373 ** ^[sqlite3_free()] is used to free idxPtr if and only if
5374 ** needToFreeIdxPtr is true.
5375 **
5376 ** ^The orderByConsumed means that output from [xFilter]/[xNext] will occur in
5377 ** the correct order to satisfy the ORDER BY clause so that no separate
5378 ** sorting step is required.
5379 **
5380 ** ^The estimatedCost value is an estimate of the cost of a particular
5381 ** strategy. A cost of N indicates that the cost of the strategy is similar
5382 ** to a linear scan of an SQLite table with N rows. A cost of log(N)
5383 ** indicates that the expense of the operation is similar to that of a
5384 ** binary search on a unique indexed field of an SQLite table with N rows.
5385 **
5386 ** ^The estimatedRows value is an estimate of the number of rows that
5387 ** will be returned by the strategy.
5388 **
5389 ** IMPORTANT: The estimatedRows field was added to the sqlite3_index_info
5390 ** structure for SQLite version 3.8.2. If a virtual table extension is
5391 ** used with an SQLite version earlier than 3.8.2, the results of attempting
5392 ** to read or write the estimatedRows field are undefined (but are likely
5393 ** to included crashing the application). The estimatedRows field should
5394 ** therefore only be used if [sqlite3_libversion_number()] returns a
5395 ** value greater than or equal to 3008002.
5396 */
5397 struct sqlite3_index_info {
5398   /* Inputs */
5399   int nConstraint;           /* Number of entries in aConstraint */
5400   struct sqlite3_index_constraint {
5401      int iColumn;              /* Column on left-hand side of constraint */
5402      unsigned char op;         /* Constraint operator */
5403      unsigned char usable;     /* True if this constraint is usable */
5404      int iTermOffset;          /* Used internally - xBestIndex should ignore */
5405   } *aConstraint;            /* Table of WHERE clause constraints */
5406   int nOrderBy;              /* Number of terms in the ORDER BY clause */
5407   struct sqlite3_index_orderby {
5408      int iColumn;              /* Column number */
5409      unsigned char desc;       /* True for DESC.  False for ASC. */
5410   } *aOrderBy;               /* The ORDER BY clause */
5411   /* Outputs */
5412   struct sqlite3_index_constraint_usage {
5413     int argvIndex;           /* if >0, constraint is part of argv to xFilter */
5414     unsigned char omit;      /* Do not code a test for this constraint */
5415   } *aConstraintUsage;
5416   int idxNum;                /* Number used to identify the index */
5417   char *idxStr;              /* String, possibly obtained from sqlite3_malloc */
5418   int needToFreeIdxStr;      /* Free idxStr using sqlite3_free() if true */
5419   int orderByConsumed;       /* True if output is already ordered */
5420   double estimatedCost;           /* Estimated cost of using this index */
5421   /* Fields below are only available in SQLite 3.8.2 and later */
5422   sqlite3_int64 estimatedRows;    /* Estimated number of rows returned */
5423 };
5424 
5425 /*
5426 ** CAPI3REF: Virtual Table Constraint Operator Codes
5427 **
5428 ** These macros defined the allowed values for the
5429 ** [sqlite3_index_info].aConstraint[].op field.  Each value represents
5430 ** an operator that is part of a constraint term in the wHERE clause of
5431 ** a query that uses a [virtual table].
5432 */
5433 #define SQLITE_INDEX_CONSTRAINT_EQ    2
5434 #define SQLITE_INDEX_CONSTRAINT_GT    4
5435 #define SQLITE_INDEX_CONSTRAINT_LE    8
5436 #define SQLITE_INDEX_CONSTRAINT_LT    16
5437 #define SQLITE_INDEX_CONSTRAINT_GE    32
5438 #define SQLITE_INDEX_CONSTRAINT_MATCH 64
5439 
5440 /*
5441 ** CAPI3REF: Register A Virtual Table Implementation
5442 **
5443 ** ^These routines are used to register a new [virtual table module] name.
5444 ** ^Module names must be registered before
5445 ** creating a new [virtual table] using the module and before using a
5446 ** preexisting [virtual table] for the module.
5447 **
5448 ** ^The module name is registered on the [database connection] specified
5449 ** by the first parameter.  ^The name of the module is given by the
5450 ** second parameter.  ^The third parameter is a pointer to
5451 ** the implementation of the [virtual table module].   ^The fourth
5452 ** parameter is an arbitrary client data pointer that is passed through
5453 ** into the [xCreate] and [xConnect] methods of the virtual table module
5454 ** when a new virtual table is be being created or reinitialized.
5455 **
5456 ** ^The sqlite3_create_module_v2() interface has a fifth parameter which
5457 ** is a pointer to a destructor for the pClientData.  ^SQLite will
5458 ** invoke the destructor function (if it is not NULL) when SQLite
5459 ** no longer needs the pClientData pointer.  ^The destructor will also
5460 ** be invoked if the call to sqlite3_create_module_v2() fails.
5461 ** ^The sqlite3_create_module()
5462 ** interface is equivalent to sqlite3_create_module_v2() with a NULL
5463 ** destructor.
5464 */
5465 SQLITE_API int sqlite3_create_module(
5466   sqlite3 *db,               /* SQLite connection to register module with */
5467   const char *zName,         /* Name of the module */
5468   const sqlite3_module *p,   /* Methods for the module */
5469   void *pClientData          /* Client data for xCreate/xConnect */
5470 );
5471 SQLITE_API int sqlite3_create_module_v2(
5472   sqlite3 *db,               /* SQLite connection to register module with */
5473   const char *zName,         /* Name of the module */
5474   const sqlite3_module *p,   /* Methods for the module */
5475   void *pClientData,         /* Client data for xCreate/xConnect */
5476   void(*xDestroy)(void*)     /* Module destructor function */
5477 );
5478 
5479 /*
5480 ** CAPI3REF: Virtual Table Instance Object
5481 ** KEYWORDS: sqlite3_vtab
5482 **
5483 ** Every [virtual table module] implementation uses a subclass
5484 ** of this object to describe a particular instance
5485 ** of the [virtual table].  Each subclass will
5486 ** be tailored to the specific needs of the module implementation.
5487 ** The purpose of this superclass is to define certain fields that are
5488 ** common to all module implementations.
5489 **
5490 ** ^Virtual tables methods can set an error message by assigning a
5491 ** string obtained from [sqlite3_mprintf()] to zErrMsg.  The method should
5492 ** take care that any prior string is freed by a call to [sqlite3_free()]
5493 ** prior to assigning a new string to zErrMsg.  ^After the error message
5494 ** is delivered up to the client application, the string will be automatically
5495 ** freed by sqlite3_free() and the zErrMsg field will be zeroed.
5496 */
5497 struct sqlite3_vtab {
5498   const sqlite3_module *pModule;  /* The module for this virtual table */
5499   int nRef;                       /* NO LONGER USED */
5500   char *zErrMsg;                  /* Error message from sqlite3_mprintf() */
5501   /* Virtual table implementations will typically add additional fields */
5502 };
5503 
5504 /*
5505 ** CAPI3REF: Virtual Table Cursor Object
5506 ** KEYWORDS: sqlite3_vtab_cursor {virtual table cursor}
5507 **
5508 ** Every [virtual table module] implementation uses a subclass of the
5509 ** following structure to describe cursors that point into the
5510 ** [virtual table] and are used
5511 ** to loop through the virtual table.  Cursors are created using the
5512 ** [sqlite3_module.xOpen | xOpen] method of the module and are destroyed
5513 ** by the [sqlite3_module.xClose | xClose] method.  Cursors are used
5514 ** by the [xFilter], [xNext], [xEof], [xColumn], and [xRowid] methods
5515 ** of the module.  Each module implementation will define
5516 ** the content of a cursor structure to suit its own needs.
5517 **
5518 ** This superclass exists in order to define fields of the cursor that
5519 ** are common to all implementations.
5520 */
5521 struct sqlite3_vtab_cursor {
5522   sqlite3_vtab *pVtab;      /* Virtual table of this cursor */
5523   /* Virtual table implementations will typically add additional fields */
5524 };
5525 
5526 /*
5527 ** CAPI3REF: Declare The Schema Of A Virtual Table
5528 **
5529 ** ^The [xCreate] and [xConnect] methods of a
5530 ** [virtual table module] call this interface
5531 ** to declare the format (the names and datatypes of the columns) of
5532 ** the virtual tables they implement.
5533 */
5534 SQLITE_API int sqlite3_declare_vtab(sqlite3*, const char *zSQL);
5535 
5536 /*
5537 ** CAPI3REF: Overload A Function For A Virtual Table
5538 **
5539 ** ^(Virtual tables can provide alternative implementations of functions
5540 ** using the [xFindFunction] method of the [virtual table module].
5541 ** But global versions of those functions
5542 ** must exist in order to be overloaded.)^
5543 **
5544 ** ^(This API makes sure a global version of a function with a particular
5545 ** name and number of parameters exists.  If no such function exists
5546 ** before this API is called, a new function is created.)^  ^The implementation
5547 ** of the new function always causes an exception to be thrown.  So
5548 ** the new function is not good for anything by itself.  Its only
5549 ** purpose is to be a placeholder function that can be overloaded
5550 ** by a [virtual table].
5551 */
5552 SQLITE_API int sqlite3_overload_function(sqlite3*, const char *zFuncName, int nArg);
5553 
5554 /*
5555 ** The interface to the virtual-table mechanism defined above (back up
5556 ** to a comment remarkably similar to this one) is currently considered
5557 ** to be experimental.  The interface might change in incompatible ways.
5558 ** If this is a problem for you, do not use the interface at this time.
5559 **
5560 ** When the virtual-table mechanism stabilizes, we will declare the
5561 ** interface fixed, support it indefinitely, and remove this comment.
5562 */
5563 
5564 /*
5565 ** CAPI3REF: A Handle To An Open BLOB
5566 ** KEYWORDS: {BLOB handle} {BLOB handles}
5567 **
5568 ** An instance of this object represents an open BLOB on which
5569 ** [sqlite3_blob_open | incremental BLOB I/O] can be performed.
5570 ** ^Objects of this type are created by [sqlite3_blob_open()]
5571 ** and destroyed by [sqlite3_blob_close()].
5572 ** ^The [sqlite3_blob_read()] and [sqlite3_blob_write()] interfaces
5573 ** can be used to read or write small subsections of the BLOB.
5574 ** ^The [sqlite3_blob_bytes()] interface returns the size of the BLOB in bytes.
5575 */
5576 typedef struct sqlite3_blob sqlite3_blob;
5577 
5578 /*
5579 ** CAPI3REF: Open A BLOB For Incremental I/O
5580 **
5581 ** ^(This interfaces opens a [BLOB handle | handle] to the BLOB located
5582 ** in row iRow, column zColumn, table zTable in database zDb;
5583 ** in other words, the same BLOB that would be selected by:
5584 **
5585 ** <pre>
5586 **     SELECT zColumn FROM zDb.zTable WHERE [rowid] = iRow;
5587 ** </pre>)^
5588 **
5589 ** ^If the flags parameter is non-zero, then the BLOB is opened for read
5590 ** and write access. ^If it is zero, the BLOB is opened for read access.
5591 ** ^It is not possible to open a column that is part of an index or primary
5592 ** key for writing. ^If [foreign key constraints] are enabled, it is
5593 ** not possible to open a column that is part of a [child key] for writing.
5594 **
5595 ** ^Note that the database name is not the filename that contains
5596 ** the database but rather the symbolic name of the database that
5597 ** appears after the AS keyword when the database is connected using [ATTACH].
5598 ** ^For the main database file, the database name is "main".
5599 ** ^For TEMP tables, the database name is "temp".
5600 **
5601 ** ^(On success, [SQLITE_OK] is returned and the new [BLOB handle] is written
5602 ** to *ppBlob. Otherwise an [error code] is returned and *ppBlob is set
5603 ** to be a null pointer.)^
5604 ** ^This function sets the [database connection] error code and message
5605 ** accessible via [sqlite3_errcode()] and [sqlite3_errmsg()] and related
5606 ** functions. ^Note that the *ppBlob variable is always initialized in a
5607 ** way that makes it safe to invoke [sqlite3_blob_close()] on *ppBlob
5608 ** regardless of the success or failure of this routine.
5609 **
5610 ** ^(If the row that a BLOB handle points to is modified by an
5611 ** [UPDATE], [DELETE], or by [ON CONFLICT] side-effects
5612 ** then the BLOB handle is marked as "expired".
5613 ** This is true if any column of the row is changed, even a column
5614 ** other than the one the BLOB handle is open on.)^
5615 ** ^Calls to [sqlite3_blob_read()] and [sqlite3_blob_write()] for
5616 ** an expired BLOB handle fail with a return code of [SQLITE_ABORT].
5617 ** ^(Changes written into a BLOB prior to the BLOB expiring are not
5618 ** rolled back by the expiration of the BLOB.  Such changes will eventually
5619 ** commit if the transaction continues to completion.)^
5620 **
5621 ** ^Use the [sqlite3_blob_bytes()] interface to determine the size of
5622 ** the opened blob.  ^The size of a blob may not be changed by this
5623 ** interface.  Use the [UPDATE] SQL command to change the size of a
5624 ** blob.
5625 **
5626 ** ^The [sqlite3_blob_open()] interface will fail for a [WITHOUT ROWID]
5627 ** table.  Incremental BLOB I/O is not possible on [WITHOUT ROWID] tables.
5628 **
5629 ** ^The [sqlite3_bind_zeroblob()] and [sqlite3_result_zeroblob()] interfaces
5630 ** and the built-in [zeroblob] SQL function can be used, if desired,
5631 ** to create an empty, zero-filled blob in which to read or write using
5632 ** this interface.
5633 **
5634 ** To avoid a resource leak, every open [BLOB handle] should eventually
5635 ** be released by a call to [sqlite3_blob_close()].
5636 */
5637 SQLITE_API int sqlite3_blob_open(
5638   sqlite3*,
5639   const char *zDb,
5640   const char *zTable,
5641   const char *zColumn,
5642   sqlite3_int64 iRow,
5643   int flags,
5644   sqlite3_blob **ppBlob
5645 );
5646 
5647 /*
5648 ** CAPI3REF: Move a BLOB Handle to a New Row
5649 **
5650 ** ^This function is used to move an existing blob handle so that it points
5651 ** to a different row of the same database table. ^The new row is identified
5652 ** by the rowid value passed as the second argument. Only the row can be
5653 ** changed. ^The database, table and column on which the blob handle is open
5654 ** remain the same. Moving an existing blob handle to a new row can be
5655 ** faster than closing the existing handle and opening a new one.
5656 **
5657 ** ^(The new row must meet the same criteria as for [sqlite3_blob_open()] -
5658 ** it must exist and there must be either a blob or text value stored in
5659 ** the nominated column.)^ ^If the new row is not present in the table, or if
5660 ** it does not contain a blob or text value, or if another error occurs, an
5661 ** SQLite error code is returned and the blob handle is considered aborted.
5662 ** ^All subsequent calls to [sqlite3_blob_read()], [sqlite3_blob_write()] or
5663 ** [sqlite3_blob_reopen()] on an aborted blob handle immediately return
5664 ** SQLITE_ABORT. ^Calling [sqlite3_blob_bytes()] on an aborted blob handle
5665 ** always returns zero.
5666 **
5667 ** ^This function sets the database handle error code and message.
5668 */
5669 SQLITE_API SQLITE_EXPERIMENTAL int sqlite3_blob_reopen(sqlite3_blob *, sqlite3_int64);
5670 
5671 /*
5672 ** CAPI3REF: Close A BLOB Handle
5673 **
5674 ** ^Closes an open [BLOB handle].
5675 **
5676 ** ^Closing a BLOB shall cause the current transaction to commit
5677 ** if there are no other BLOBs, no pending prepared statements, and the
5678 ** database connection is in [autocommit mode].
5679 ** ^If any writes were made to the BLOB, they might be held in cache
5680 ** until the close operation if they will fit.
5681 **
5682 ** ^(Closing the BLOB often forces the changes
5683 ** out to disk and so if any I/O errors occur, they will likely occur
5684 ** at the time when the BLOB is closed.  Any errors that occur during
5685 ** closing are reported as a non-zero return value.)^
5686 **
5687 ** ^(The BLOB is closed unconditionally.  Even if this routine returns
5688 ** an error code, the BLOB is still closed.)^
5689 **
5690 ** ^Calling this routine with a null pointer (such as would be returned
5691 ** by a failed call to [sqlite3_blob_open()]) is a harmless no-op.
5692 */
5693 SQLITE_API int sqlite3_blob_close(sqlite3_blob *);
5694 
5695 /*
5696 ** CAPI3REF: Return The Size Of An Open BLOB
5697 **
5698 ** ^Returns the size in bytes of the BLOB accessible via the
5699 ** successfully opened [BLOB handle] in its only argument.  ^The
5700 ** incremental blob I/O routines can only read or overwriting existing
5701 ** blob content; they cannot change the size of a blob.
5702 **
5703 ** This routine only works on a [BLOB handle] which has been created
5704 ** by a prior successful call to [sqlite3_blob_open()] and which has not
5705 ** been closed by [sqlite3_blob_close()].  Passing any other pointer in
5706 ** to this routine results in undefined and probably undesirable behavior.
5707 */
5708 SQLITE_API int sqlite3_blob_bytes(sqlite3_blob *);
5709 
5710 /*
5711 ** CAPI3REF: Read Data From A BLOB Incrementally
5712 **
5713 ** ^(This function is used to read data from an open [BLOB handle] into a
5714 ** caller-supplied buffer. N bytes of data are copied into buffer Z
5715 ** from the open BLOB, starting at offset iOffset.)^
5716 **
5717 ** ^If offset iOffset is less than N bytes from the end of the BLOB,
5718 ** [SQLITE_ERROR] is returned and no data is read.  ^If N or iOffset is
5719 ** less than zero, [SQLITE_ERROR] is returned and no data is read.
5720 ** ^The size of the blob (and hence the maximum value of N+iOffset)
5721 ** can be determined using the [sqlite3_blob_bytes()] interface.
5722 **
5723 ** ^An attempt to read from an expired [BLOB handle] fails with an
5724 ** error code of [SQLITE_ABORT].
5725 **
5726 ** ^(On success, sqlite3_blob_read() returns SQLITE_OK.
5727 ** Otherwise, an [error code] or an [extended error code] is returned.)^
5728 **
5729 ** This routine only works on a [BLOB handle] which has been created
5730 ** by a prior successful call to [sqlite3_blob_open()] and which has not
5731 ** been closed by [sqlite3_blob_close()].  Passing any other pointer in
5732 ** to this routine results in undefined and probably undesirable behavior.
5733 **
5734 ** See also: [sqlite3_blob_write()].
5735 */
5736 SQLITE_API int sqlite3_blob_read(sqlite3_blob *, void *Z, int N, int iOffset);
5737 
5738 /*
5739 ** CAPI3REF: Write Data Into A BLOB Incrementally
5740 **
5741 ** ^This function is used to write data into an open [BLOB handle] from a
5742 ** caller-supplied buffer. ^N bytes of data are copied from the buffer Z
5743 ** into the open BLOB, starting at offset iOffset.
5744 **
5745 ** ^If the [BLOB handle] passed as the first argument was not opened for
5746 ** writing (the flags parameter to [sqlite3_blob_open()] was zero),
5747 ** this function returns [SQLITE_READONLY].
5748 **
5749 ** ^This function may only modify the contents of the BLOB; it is
5750 ** not possible to increase the size of a BLOB using this API.
5751 ** ^If offset iOffset is less than N bytes from the end of the BLOB,
5752 ** [SQLITE_ERROR] is returned and no data is written.  ^If N is
5753 ** less than zero [SQLITE_ERROR] is returned and no data is written.
5754 ** The size of the BLOB (and hence the maximum value of N+iOffset)
5755 ** can be determined using the [sqlite3_blob_bytes()] interface.
5756 **
5757 ** ^An attempt to write to an expired [BLOB handle] fails with an
5758 ** error code of [SQLITE_ABORT].  ^Writes to the BLOB that occurred
5759 ** before the [BLOB handle] expired are not rolled back by the
5760 ** expiration of the handle, though of course those changes might
5761 ** have been overwritten by the statement that expired the BLOB handle
5762 ** or by other independent statements.
5763 **
5764 ** ^(On success, sqlite3_blob_write() returns SQLITE_OK.
5765 ** Otherwise, an  [error code] or an [extended error code] is returned.)^
5766 **
5767 ** This routine only works on a [BLOB handle] which has been created
5768 ** by a prior successful call to [sqlite3_blob_open()] and which has not
5769 ** been closed by [sqlite3_blob_close()].  Passing any other pointer in
5770 ** to this routine results in undefined and probably undesirable behavior.
5771 **
5772 ** See also: [sqlite3_blob_read()].
5773 */
5774 SQLITE_API int sqlite3_blob_write(sqlite3_blob *, const void *z, int n, int iOffset);
5775 
5776 /*
5777 ** CAPI3REF: Virtual File System Objects
5778 **
5779 ** A virtual filesystem (VFS) is an [sqlite3_vfs] object
5780 ** that SQLite uses to interact
5781 ** with the underlying operating system.  Most SQLite builds come with a
5782 ** single default VFS that is appropriate for the host computer.
5783 ** New VFSes can be registered and existing VFSes can be unregistered.
5784 ** The following interfaces are provided.
5785 **
5786 ** ^The sqlite3_vfs_find() interface returns a pointer to a VFS given its name.
5787 ** ^Names are case sensitive.
5788 ** ^Names are zero-terminated UTF-8 strings.
5789 ** ^If there is no match, a NULL pointer is returned.
5790 ** ^If zVfsName is NULL then the default VFS is returned.
5791 **
5792 ** ^New VFSes are registered with sqlite3_vfs_register().
5793 ** ^Each new VFS becomes the default VFS if the makeDflt flag is set.
5794 ** ^The same VFS can be registered multiple times without injury.
5795 ** ^To make an existing VFS into the default VFS, register it again
5796 ** with the makeDflt flag set.  If two different VFSes with the
5797 ** same name are registered, the behavior is undefined.  If a
5798 ** VFS is registered with a name that is NULL or an empty string,
5799 ** then the behavior is undefined.
5800 **
5801 ** ^Unregister a VFS with the sqlite3_vfs_unregister() interface.
5802 ** ^(If the default VFS is unregistered, another VFS is chosen as
5803 ** the default.  The choice for the new VFS is arbitrary.)^
5804 */
5805 SQLITE_API sqlite3_vfs *sqlite3_vfs_find(const char *zVfsName);
5806 SQLITE_API int sqlite3_vfs_register(sqlite3_vfs*, int makeDflt);
5807 SQLITE_API int sqlite3_vfs_unregister(sqlite3_vfs*);
5808 
5809 /*
5810 ** CAPI3REF: Mutexes
5811 **
5812 ** The SQLite core uses these routines for thread
5813 ** synchronization. Though they are intended for internal
5814 ** use by SQLite, code that links against SQLite is
5815 ** permitted to use any of these routines.
5816 **
5817 ** The SQLite source code contains multiple implementations
5818 ** of these mutex routines.  An appropriate implementation
5819 ** is selected automatically at compile-time.  ^(The following
5820 ** implementations are available in the SQLite core:
5821 **
5822 ** <ul>
5823 ** <li>   SQLITE_MUTEX_PTHREADS
5824 ** <li>   SQLITE_MUTEX_W32
5825 ** <li>   SQLITE_MUTEX_NOOP
5826 ** </ul>)^
5827 **
5828 ** ^The SQLITE_MUTEX_NOOP implementation is a set of routines
5829 ** that does no real locking and is appropriate for use in
5830 ** a single-threaded application.  ^The SQLITE_MUTEX_PTHREADS and
5831 ** SQLITE_MUTEX_W32 implementations are appropriate for use on Unix
5832 ** and Windows.
5833 **
5834 ** ^(If SQLite is compiled with the SQLITE_MUTEX_APPDEF preprocessor
5835 ** macro defined (with "-DSQLITE_MUTEX_APPDEF=1"), then no mutex
5836 ** implementation is included with the library. In this case the
5837 ** application must supply a custom mutex implementation using the
5838 ** [SQLITE_CONFIG_MUTEX] option of the sqlite3_config() function
5839 ** before calling sqlite3_initialize() or any other public sqlite3_
5840 ** function that calls sqlite3_initialize().)^
5841 **
5842 ** ^The sqlite3_mutex_alloc() routine allocates a new
5843 ** mutex and returns a pointer to it. ^If it returns NULL
5844 ** that means that a mutex could not be allocated.  ^SQLite
5845 ** will unwind its stack and return an error.  ^(The argument
5846 ** to sqlite3_mutex_alloc() is one of these integer constants:
5847 **
5848 ** <ul>
5849 ** <li>  SQLITE_MUTEX_FAST
5850 ** <li>  SQLITE_MUTEX_RECURSIVE
5851 ** <li>  SQLITE_MUTEX_STATIC_MASTER
5852 ** <li>  SQLITE_MUTEX_STATIC_MEM
5853 ** <li>  SQLITE_MUTEX_STATIC_MEM2
5854 ** <li>  SQLITE_MUTEX_STATIC_PRNG
5855 ** <li>  SQLITE_MUTEX_STATIC_LRU
5856 ** <li>  SQLITE_MUTEX_STATIC_LRU2
5857 ** </ul>)^
5858 **
5859 ** ^The first two constants (SQLITE_MUTEX_FAST and SQLITE_MUTEX_RECURSIVE)
5860 ** cause sqlite3_mutex_alloc() to create
5861 ** a new mutex.  ^The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
5862 ** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
5863 ** The mutex implementation does not need to make a distinction
5864 ** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does
5865 ** not want to.  ^SQLite will only request a recursive mutex in
5866 ** cases where it really needs one.  ^If a faster non-recursive mutex
5867 ** implementation is available on the host platform, the mutex subsystem
5868 ** might return such a mutex in response to SQLITE_MUTEX_FAST.
5869 **
5870 ** ^The other allowed parameters to sqlite3_mutex_alloc() (anything other
5871 ** than SQLITE_MUTEX_FAST and SQLITE_MUTEX_RECURSIVE) each return
5872 ** a pointer to a static preexisting mutex.  ^Six static mutexes are
5873 ** used by the current version of SQLite.  Future versions of SQLite
5874 ** may add additional static mutexes.  Static mutexes are for internal
5875 ** use by SQLite only.  Applications that use SQLite mutexes should
5876 ** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or
5877 ** SQLITE_MUTEX_RECURSIVE.
5878 **
5879 ** ^Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
5880 ** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
5881 ** returns a different mutex on every call.  ^But for the static
5882 ** mutex types, the same mutex is returned on every call that has
5883 ** the same type number.
5884 **
5885 ** ^The sqlite3_mutex_free() routine deallocates a previously
5886 ** allocated dynamic mutex.  ^SQLite is careful to deallocate every
5887 ** dynamic mutex that it allocates.  The dynamic mutexes must not be in
5888 ** use when they are deallocated.  Attempting to deallocate a static
5889 ** mutex results in undefined behavior.  ^SQLite never deallocates
5890 ** a static mutex.
5891 **
5892 ** ^The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
5893 ** to enter a mutex.  ^If another thread is already within the mutex,
5894 ** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
5895 ** SQLITE_BUSY.  ^The sqlite3_mutex_try() interface returns [SQLITE_OK]
5896 ** upon successful entry.  ^(Mutexes created using
5897 ** SQLITE_MUTEX_RECURSIVE can be entered multiple times by the same thread.
5898 ** In such cases the,
5899 ** mutex must be exited an equal number of times before another thread
5900 ** can enter.)^  ^(If the same thread tries to enter any other
5901 ** kind of mutex more than once, the behavior is undefined.
5902 ** SQLite will never exhibit
5903 ** such behavior in its own use of mutexes.)^
5904 **
5905 ** ^(Some systems (for example, Windows 95) do not support the operation
5906 ** implemented by sqlite3_mutex_try().  On those systems, sqlite3_mutex_try()
5907 ** will always return SQLITE_BUSY.  The SQLite core only ever uses
5908 ** sqlite3_mutex_try() as an optimization so this is acceptable behavior.)^
5909 **
5910 ** ^The sqlite3_mutex_leave() routine exits a mutex that was
5911 ** previously entered by the same thread.   ^(The behavior
5912 ** is undefined if the mutex is not currently entered by the
5913 ** calling thread or is not currently allocated.  SQLite will
5914 ** never do either.)^
5915 **
5916 ** ^If the argument to sqlite3_mutex_enter(), sqlite3_mutex_try(), or
5917 ** sqlite3_mutex_leave() is a NULL pointer, then all three routines
5918 ** behave as no-ops.
5919 **
5920 ** See also: [sqlite3_mutex_held()] and [sqlite3_mutex_notheld()].
5921 */
5922 SQLITE_API sqlite3_mutex *sqlite3_mutex_alloc(int);
5923 SQLITE_API void sqlite3_mutex_free(sqlite3_mutex*);
5924 SQLITE_API void sqlite3_mutex_enter(sqlite3_mutex*);
5925 SQLITE_API int sqlite3_mutex_try(sqlite3_mutex*);
5926 SQLITE_API void sqlite3_mutex_leave(sqlite3_mutex*);
5927 
5928 /*
5929 ** CAPI3REF: Mutex Methods Object
5930 **
5931 ** An instance of this structure defines the low-level routines
5932 ** used to allocate and use mutexes.
5933 **
5934 ** Usually, the default mutex implementations provided by SQLite are
5935 ** sufficient, however the user has the option of substituting a custom
5936 ** implementation for specialized deployments or systems for which SQLite
5937 ** does not provide a suitable implementation. In this case, the user
5938 ** creates and populates an instance of this structure to pass
5939 ** to sqlite3_config() along with the [SQLITE_CONFIG_MUTEX] option.
5940 ** Additionally, an instance of this structure can be used as an
5941 ** output variable when querying the system for the current mutex
5942 ** implementation, using the [SQLITE_CONFIG_GETMUTEX] option.
5943 **
5944 ** ^The xMutexInit method defined by this structure is invoked as
5945 ** part of system initialization by the sqlite3_initialize() function.
5946 ** ^The xMutexInit routine is called by SQLite exactly once for each
5947 ** effective call to [sqlite3_initialize()].
5948 **
5949 ** ^The xMutexEnd method defined by this structure is invoked as
5950 ** part of system shutdown by the sqlite3_shutdown() function. The
5951 ** implementation of this method is expected to release all outstanding
5952 ** resources obtained by the mutex methods implementation, especially
5953 ** those obtained by the xMutexInit method.  ^The xMutexEnd()
5954 ** interface is invoked exactly once for each call to [sqlite3_shutdown()].
5955 **
5956 ** ^(The remaining seven methods defined by this structure (xMutexAlloc,
5957 ** xMutexFree, xMutexEnter, xMutexTry, xMutexLeave, xMutexHeld and
5958 ** xMutexNotheld) implement the following interfaces (respectively):
5959 **
5960 ** <ul>
5961 **   <li>  [sqlite3_mutex_alloc()] </li>
5962 **   <li>  [sqlite3_mutex_free()] </li>
5963 **   <li>  [sqlite3_mutex_enter()] </li>
5964 **   <li>  [sqlite3_mutex_try()] </li>
5965 **   <li>  [sqlite3_mutex_leave()] </li>
5966 **   <li>  [sqlite3_mutex_held()] </li>
5967 **   <li>  [sqlite3_mutex_notheld()] </li>
5968 ** </ul>)^
5969 **
5970 ** The only difference is that the public sqlite3_XXX functions enumerated
5971 ** above silently ignore any invocations that pass a NULL pointer instead
5972 ** of a valid mutex handle. The implementations of the methods defined
5973 ** by this structure are not required to handle this case, the results
5974 ** of passing a NULL pointer instead of a valid mutex handle are undefined
5975 ** (i.e. it is acceptable to provide an implementation that segfaults if
5976 ** it is passed a NULL pointer).
5977 **
5978 ** The xMutexInit() method must be threadsafe.  ^It must be harmless to
5979 ** invoke xMutexInit() multiple times within the same process and without
5980 ** intervening calls to xMutexEnd().  Second and subsequent calls to
5981 ** xMutexInit() must be no-ops.
5982 **
5983 ** ^xMutexInit() must not use SQLite memory allocation ([sqlite3_malloc()]
5984 ** and its associates).  ^Similarly, xMutexAlloc() must not use SQLite memory
5985 ** allocation for a static mutex.  ^However xMutexAlloc() may use SQLite
5986 ** memory allocation for a fast or recursive mutex.
5987 **
5988 ** ^SQLite will invoke the xMutexEnd() method when [sqlite3_shutdown()] is
5989 ** called, but only if the prior call to xMutexInit returned SQLITE_OK.
5990 ** If xMutexInit fails in any way, it is expected to clean up after itself
5991 ** prior to returning.
5992 */
5993 typedef struct sqlite3_mutex_methods sqlite3_mutex_methods;
5994 struct sqlite3_mutex_methods {
5995   int (*xMutexInit)(void);
5996   int (*xMutexEnd)(void);
5997   sqlite3_mutex *(*xMutexAlloc)(int);
5998   void (*xMutexFree)(sqlite3_mutex *);
5999   void (*xMutexEnter)(sqlite3_mutex *);
6000   int (*xMutexTry)(sqlite3_mutex *);
6001   void (*xMutexLeave)(sqlite3_mutex *);
6002   int (*xMutexHeld)(sqlite3_mutex *);
6003   int (*xMutexNotheld)(sqlite3_mutex *);
6004 };
6005 
6006 /*
6007 ** CAPI3REF: Mutex Verification Routines
6008 **
6009 ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routines
6010 ** are intended for use inside assert() statements.  ^The SQLite core
6011 ** never uses these routines except inside an assert() and applications
6012 ** are advised to follow the lead of the core.  ^The SQLite core only
6013 ** provides implementations for these routines when it is compiled
6014 ** with the SQLITE_DEBUG flag.  ^External mutex implementations
6015 ** are only required to provide these routines if SQLITE_DEBUG is
6016 ** defined and if NDEBUG is not defined.
6017 **
6018 ** ^These routines should return true if the mutex in their argument
6019 ** is held or not held, respectively, by the calling thread.
6020 **
6021 ** ^The implementation is not required to provide versions of these
6022 ** routines that actually work. If the implementation does not provide working
6023 ** versions of these routines, it should at least provide stubs that always
6024 ** return true so that one does not get spurious assertion failures.
6025 **
6026 ** ^If the argument to sqlite3_mutex_held() is a NULL pointer then
6027 ** the routine should return 1.   This seems counter-intuitive since
6028 ** clearly the mutex cannot be held if it does not exist.  But
6029 ** the reason the mutex does not exist is because the build is not
6030 ** using mutexes.  And we do not want the assert() containing the
6031 ** call to sqlite3_mutex_held() to fail, so a non-zero return is
6032 ** the appropriate thing to do.  ^The sqlite3_mutex_notheld()
6033 ** interface should also return 1 when given a NULL pointer.
6034 */
6035 #ifndef NDEBUG
6036 SQLITE_API int sqlite3_mutex_held(sqlite3_mutex*);
6037 SQLITE_API int sqlite3_mutex_notheld(sqlite3_mutex*);
6038 #endif
6039 
6040 /*
6041 ** CAPI3REF: Mutex Types
6042 **
6043 ** The [sqlite3_mutex_alloc()] interface takes a single argument
6044 ** which is one of these integer constants.
6045 **
6046 ** The set of static mutexes may change from one SQLite release to the
6047 ** next.  Applications that override the built-in mutex logic must be
6048 ** prepared to accommodate additional static mutexes.
6049 */
6050 #define SQLITE_MUTEX_FAST             0
6051 #define SQLITE_MUTEX_RECURSIVE        1
6052 #define SQLITE_MUTEX_STATIC_MASTER    2
6053 #define SQLITE_MUTEX_STATIC_MEM       3  /* sqlite3_malloc() */
6054 #define SQLITE_MUTEX_STATIC_MEM2      4  /* NOT USED */
6055 #define SQLITE_MUTEX_STATIC_OPEN      4  /* sqlite3BtreeOpen() */
6056 #define SQLITE_MUTEX_STATIC_PRNG      5  /* sqlite3_random() */
6057 #define SQLITE_MUTEX_STATIC_LRU       6  /* lru page list */
6058 #define SQLITE_MUTEX_STATIC_LRU2      7  /* NOT USED */
6059 #define SQLITE_MUTEX_STATIC_PMEM      7  /* sqlite3PageMalloc() */
6060 
6061 /*
6062 ** CAPI3REF: Retrieve the mutex for a database connection
6063 **
6064 ** ^This interface returns a pointer the [sqlite3_mutex] object that
6065 ** serializes access to the [database connection] given in the argument
6066 ** when the [threading mode] is Serialized.
6067 ** ^If the [threading mode] is Single-thread or Multi-thread then this
6068 ** routine returns a NULL pointer.
6069 */
6070 SQLITE_API sqlite3_mutex *sqlite3_db_mutex(sqlite3*);
6071 
6072 /*
6073 ** CAPI3REF: Low-Level Control Of Database Files
6074 **
6075 ** ^The [sqlite3_file_control()] interface makes a direct call to the
6076 ** xFileControl method for the [sqlite3_io_methods] object associated
6077 ** with a particular database identified by the second argument. ^The
6078 ** name of the database is "main" for the main database or "temp" for the
6079 ** TEMP database, or the name that appears after the AS keyword for
6080 ** databases that are added using the [ATTACH] SQL command.
6081 ** ^A NULL pointer can be used in place of "main" to refer to the
6082 ** main database file.
6083 ** ^The third and fourth parameters to this routine
6084 ** are passed directly through to the second and third parameters of
6085 ** the xFileControl method.  ^The return value of the xFileControl
6086 ** method becomes the return value of this routine.
6087 **
6088 ** ^The SQLITE_FCNTL_FILE_POINTER value for the op parameter causes
6089 ** a pointer to the underlying [sqlite3_file] object to be written into
6090 ** the space pointed to by the 4th parameter.  ^The SQLITE_FCNTL_FILE_POINTER
6091 ** case is a short-circuit path which does not actually invoke the
6092 ** underlying sqlite3_io_methods.xFileControl method.
6093 **
6094 ** ^If the second parameter (zDbName) does not match the name of any
6095 ** open database file, then SQLITE_ERROR is returned.  ^This error
6096 ** code is not remembered and will not be recalled by [sqlite3_errcode()]
6097 ** or [sqlite3_errmsg()].  The underlying xFileControl method might
6098 ** also return SQLITE_ERROR.  There is no way to distinguish between
6099 ** an incorrect zDbName and an SQLITE_ERROR return from the underlying
6100 ** xFileControl method.
6101 **
6102 ** See also: [SQLITE_FCNTL_LOCKSTATE]
6103 */
6104 SQLITE_API int sqlite3_file_control(sqlite3*, const char *zDbName, int op, void*);
6105 
6106 /*
6107 ** CAPI3REF: Testing Interface
6108 **
6109 ** ^The sqlite3_test_control() interface is used to read out internal
6110 ** state of SQLite and to inject faults into SQLite for testing
6111 ** purposes.  ^The first parameter is an operation code that determines
6112 ** the number, meaning, and operation of all subsequent parameters.
6113 **
6114 ** This interface is not for use by applications.  It exists solely
6115 ** for verifying the correct operation of the SQLite library.  Depending
6116 ** on how the SQLite library is compiled, this interface might not exist.
6117 **
6118 ** The details of the operation codes, their meanings, the parameters
6119 ** they take, and what they do are all subject to change without notice.
6120 ** Unlike most of the SQLite API, this function is not guaranteed to
6121 ** operate consistently from one release to the next.
6122 */
6123 SQLITE_API int sqlite3_test_control(int op, ...);
6124 
6125 /*
6126 ** CAPI3REF: Testing Interface Operation Codes
6127 **
6128 ** These constants are the valid operation code parameters used
6129 ** as the first argument to [sqlite3_test_control()].
6130 **
6131 ** These parameters and their meanings are subject to change
6132 ** without notice.  These values are for testing purposes only.
6133 ** Applications should not use any of these parameters or the
6134 ** [sqlite3_test_control()] interface.
6135 */
6136 #define SQLITE_TESTCTRL_FIRST                    5
6137 #define SQLITE_TESTCTRL_PRNG_SAVE                5
6138 #define SQLITE_TESTCTRL_PRNG_RESTORE             6
6139 #define SQLITE_TESTCTRL_PRNG_RESET               7
6140 #define SQLITE_TESTCTRL_BITVEC_TEST              8
6141 #define SQLITE_TESTCTRL_FAULT_INSTALL            9
6142 #define SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS     10
6143 #define SQLITE_TESTCTRL_PENDING_BYTE            11
6144 #define SQLITE_TESTCTRL_ASSERT                  12
6145 #define SQLITE_TESTCTRL_ALWAYS                  13
6146 #define SQLITE_TESTCTRL_RESERVE                 14
6147 #define SQLITE_TESTCTRL_OPTIMIZATIONS           15
6148 #define SQLITE_TESTCTRL_ISKEYWORD               16
6149 #define SQLITE_TESTCTRL_SCRATCHMALLOC           17
6150 #define SQLITE_TESTCTRL_LOCALTIME_FAULT         18
6151 #define SQLITE_TESTCTRL_EXPLAIN_STMT            19
6152 #define SQLITE_TESTCTRL_NEVER_CORRUPT           20
6153 #define SQLITE_TESTCTRL_LAST                    20
6154 
6155 /*
6156 ** CAPI3REF: SQLite Runtime Status
6157 **
6158 ** ^This interface is used to retrieve runtime status information
6159 ** about the performance of SQLite, and optionally to reset various
6160 ** highwater marks.  ^The first argument is an integer code for
6161 ** the specific parameter to measure.  ^(Recognized integer codes
6162 ** are of the form [status parameters | SQLITE_STATUS_...].)^
6163 ** ^The current value of the parameter is returned into *pCurrent.
6164 ** ^The highest recorded value is returned in *pHighwater.  ^If the
6165 ** resetFlag is true, then the highest record value is reset after
6166 ** *pHighwater is written.  ^(Some parameters do not record the highest
6167 ** value.  For those parameters
6168 ** nothing is written into *pHighwater and the resetFlag is ignored.)^
6169 ** ^(Other parameters record only the highwater mark and not the current
6170 ** value.  For these latter parameters nothing is written into *pCurrent.)^
6171 **
6172 ** ^The sqlite3_status() routine returns SQLITE_OK on success and a
6173 ** non-zero [error code] on failure.
6174 **
6175 ** This routine is threadsafe but is not atomic.  This routine can be
6176 ** called while other threads are running the same or different SQLite
6177 ** interfaces.  However the values returned in *pCurrent and
6178 ** *pHighwater reflect the status of SQLite at different points in time
6179 ** and it is possible that another thread might change the parameter
6180 ** in between the times when *pCurrent and *pHighwater are written.
6181 **
6182 ** See also: [sqlite3_db_status()]
6183 */
6184 SQLITE_API int sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag);
6185 
6186 
6187 /*
6188 ** CAPI3REF: Status Parameters
6189 ** KEYWORDS: {status parameters}
6190 **
6191 ** These integer constants designate various run-time status parameters
6192 ** that can be returned by [sqlite3_status()].
6193 **
6194 ** <dl>
6195 ** [[SQLITE_STATUS_MEMORY_USED]] ^(<dt>SQLITE_STATUS_MEMORY_USED</dt>
6196 ** <dd>This parameter is the current amount of memory checked out
6197 ** using [sqlite3_malloc()], either directly or indirectly.  The
6198 ** figure includes calls made to [sqlite3_malloc()] by the application
6199 ** and internal memory usage by the SQLite library.  Scratch memory
6200 ** controlled by [SQLITE_CONFIG_SCRATCH] and auxiliary page-cache
6201 ** memory controlled by [SQLITE_CONFIG_PAGECACHE] is not included in
6202 ** this parameter.  The amount returned is the sum of the allocation
6203 ** sizes as reported by the xSize method in [sqlite3_mem_methods].</dd>)^
6204 **
6205 ** [[SQLITE_STATUS_MALLOC_SIZE]] ^(<dt>SQLITE_STATUS_MALLOC_SIZE</dt>
6206 ** <dd>This parameter records the largest memory allocation request
6207 ** handed to [sqlite3_malloc()] or [sqlite3_realloc()] (or their
6208 ** internal equivalents).  Only the value returned in the
6209 ** *pHighwater parameter to [sqlite3_status()] is of interest.
6210 ** The value written into the *pCurrent parameter is undefined.</dd>)^
6211 **
6212 ** [[SQLITE_STATUS_MALLOC_COUNT]] ^(<dt>SQLITE_STATUS_MALLOC_COUNT</dt>
6213 ** <dd>This parameter records the number of separate memory allocations
6214 ** currently checked out.</dd>)^
6215 **
6216 ** [[SQLITE_STATUS_PAGECACHE_USED]] ^(<dt>SQLITE_STATUS_PAGECACHE_USED</dt>
6217 ** <dd>This parameter returns the number of pages used out of the
6218 ** [pagecache memory allocator] that was configured using
6219 ** [SQLITE_CONFIG_PAGECACHE].  The
6220 ** value returned is in pages, not in bytes.</dd>)^
6221 **
6222 ** [[SQLITE_STATUS_PAGECACHE_OVERFLOW]]
6223 ** ^(<dt>SQLITE_STATUS_PAGECACHE_OVERFLOW</dt>
6224 ** <dd>This parameter returns the number of bytes of page cache
6225 ** allocation which could not be satisfied by the [SQLITE_CONFIG_PAGECACHE]
6226 ** buffer and where forced to overflow to [sqlite3_malloc()].  The
6227 ** returned value includes allocations that overflowed because they
6228 ** where too large (they were larger than the "sz" parameter to
6229 ** [SQLITE_CONFIG_PAGECACHE]) and allocations that overflowed because
6230 ** no space was left in the page cache.</dd>)^
6231 **
6232 ** [[SQLITE_STATUS_PAGECACHE_SIZE]] ^(<dt>SQLITE_STATUS_PAGECACHE_SIZE</dt>
6233 ** <dd>This parameter records the largest memory allocation request
6234 ** handed to [pagecache memory allocator].  Only the value returned in the
6235 ** *pHighwater parameter to [sqlite3_status()] is of interest.
6236 ** The value written into the *pCurrent parameter is undefined.</dd>)^
6237 **
6238 ** [[SQLITE_STATUS_SCRATCH_USED]] ^(<dt>SQLITE_STATUS_SCRATCH_USED</dt>
6239 ** <dd>This parameter returns the number of allocations used out of the
6240 ** [scratch memory allocator] configured using
6241 ** [SQLITE_CONFIG_SCRATCH].  The value returned is in allocations, not
6242 ** in bytes.  Since a single thread may only have one scratch allocation
6243 ** outstanding at time, this parameter also reports the number of threads
6244 ** using scratch memory at the same time.</dd>)^
6245 **
6246 ** [[SQLITE_STATUS_SCRATCH_OVERFLOW]] ^(<dt>SQLITE_STATUS_SCRATCH_OVERFLOW</dt>
6247 ** <dd>This parameter returns the number of bytes of scratch memory
6248 ** allocation which could not be satisfied by the [SQLITE_CONFIG_SCRATCH]
6249 ** buffer and where forced to overflow to [sqlite3_malloc()].  The values
6250 ** returned include overflows because the requested allocation was too
6251 ** larger (that is, because the requested allocation was larger than the
6252 ** "sz" parameter to [SQLITE_CONFIG_SCRATCH]) and because no scratch buffer
6253 ** slots were available.
6254 ** </dd>)^
6255 **
6256 ** [[SQLITE_STATUS_SCRATCH_SIZE]] ^(<dt>SQLITE_STATUS_SCRATCH_SIZE</dt>
6257 ** <dd>This parameter records the largest memory allocation request
6258 ** handed to [scratch memory allocator].  Only the value returned in the
6259 ** *pHighwater parameter to [sqlite3_status()] is of interest.
6260 ** The value written into the *pCurrent parameter is undefined.</dd>)^
6261 **
6262 ** [[SQLITE_STATUS_PARSER_STACK]] ^(<dt>SQLITE_STATUS_PARSER_STACK</dt>
6263 ** <dd>This parameter records the deepest parser stack.  It is only
6264 ** meaningful if SQLite is compiled with [YYTRACKMAXSTACKDEPTH].</dd>)^
6265 ** </dl>
6266 **
6267 ** New status parameters may be added from time to time.
6268 */
6269 #define SQLITE_STATUS_MEMORY_USED          0
6270 #define SQLITE_STATUS_PAGECACHE_USED       1
6271 #define SQLITE_STATUS_PAGECACHE_OVERFLOW   2
6272 #define SQLITE_STATUS_SCRATCH_USED         3
6273 #define SQLITE_STATUS_SCRATCH_OVERFLOW     4
6274 #define SQLITE_STATUS_MALLOC_SIZE          5
6275 #define SQLITE_STATUS_PARSER_STACK         6
6276 #define SQLITE_STATUS_PAGECACHE_SIZE       7
6277 #define SQLITE_STATUS_SCRATCH_SIZE         8
6278 #define SQLITE_STATUS_MALLOC_COUNT         9
6279 
6280 /*
6281 ** CAPI3REF: Database Connection Status
6282 **
6283 ** ^This interface is used to retrieve runtime status information
6284 ** about a single [database connection].  ^The first argument is the
6285 ** database connection object to be interrogated.  ^The second argument
6286 ** is an integer constant, taken from the set of
6287 ** [SQLITE_DBSTATUS options], that
6288 ** determines the parameter to interrogate.  The set of
6289 ** [SQLITE_DBSTATUS options] is likely
6290 ** to grow in future releases of SQLite.
6291 **
6292 ** ^The current value of the requested parameter is written into *pCur
6293 ** and the highest instantaneous value is written into *pHiwtr.  ^If
6294 ** the resetFlg is true, then the highest instantaneous value is
6295 ** reset back down to the current value.
6296 **
6297 ** ^The sqlite3_db_status() routine returns SQLITE_OK on success and a
6298 ** non-zero [error code] on failure.
6299 **
6300 ** See also: [sqlite3_status()] and [sqlite3_stmt_status()].
6301 */
6302 SQLITE_API int sqlite3_db_status(sqlite3*, int op, int *pCur, int *pHiwtr, int resetFlg);
6303 
6304 /*
6305 ** CAPI3REF: Status Parameters for database connections
6306 ** KEYWORDS: {SQLITE_DBSTATUS options}
6307 **
6308 ** These constants are the available integer "verbs" that can be passed as
6309 ** the second argument to the [sqlite3_db_status()] interface.
6310 **
6311 ** New verbs may be added in future releases of SQLite. Existing verbs
6312 ** might be discontinued. Applications should check the return code from
6313 ** [sqlite3_db_status()] to make sure that the call worked.
6314 ** The [sqlite3_db_status()] interface will return a non-zero error code
6315 ** if a discontinued or unsupported verb is invoked.
6316 **
6317 ** <dl>
6318 ** [[SQLITE_DBSTATUS_LOOKASIDE_USED]] ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_USED</dt>
6319 ** <dd>This parameter returns the number of lookaside memory slots currently
6320 ** checked out.</dd>)^
6321 **
6322 ** [[SQLITE_DBSTATUS_LOOKASIDE_HIT]] ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_HIT</dt>
6323 ** <dd>This parameter returns the number malloc attempts that were
6324 ** satisfied using lookaside memory. Only the high-water value is meaningful;
6325 ** the current value is always zero.)^
6326 **
6327 ** [[SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE]]
6328 ** ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE</dt>
6329 ** <dd>This parameter returns the number malloc attempts that might have
6330 ** been satisfied using lookaside memory but failed due to the amount of
6331 ** memory requested being larger than the lookaside slot size.
6332 ** Only the high-water value is meaningful;
6333 ** the current value is always zero.)^
6334 **
6335 ** [[SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL]]
6336 ** ^(<dt>SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL</dt>
6337 ** <dd>This parameter returns the number malloc attempts that might have
6338 ** been satisfied using lookaside memory but failed due to all lookaside
6339 ** memory already being in use.
6340 ** Only the high-water value is meaningful;
6341 ** the current value is always zero.)^
6342 **
6343 ** [[SQLITE_DBSTATUS_CACHE_USED]] ^(<dt>SQLITE_DBSTATUS_CACHE_USED</dt>
6344 ** <dd>This parameter returns the approximate number of of bytes of heap
6345 ** memory used by all pager caches associated with the database connection.)^
6346 ** ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_USED is always 0.
6347 **
6348 ** [[SQLITE_DBSTATUS_SCHEMA_USED]] ^(<dt>SQLITE_DBSTATUS_SCHEMA_USED</dt>
6349 ** <dd>This parameter returns the approximate number of of bytes of heap
6350 ** memory used to store the schema for all databases associated
6351 ** with the connection - main, temp, and any [ATTACH]-ed databases.)^
6352 ** ^The full amount of memory used by the schemas is reported, even if the
6353 ** schema memory is shared with other database connections due to
6354 ** [shared cache mode] being enabled.
6355 ** ^The highwater mark associated with SQLITE_DBSTATUS_SCHEMA_USED is always 0.
6356 **
6357 ** [[SQLITE_DBSTATUS_STMT_USED]] ^(<dt>SQLITE_DBSTATUS_STMT_USED</dt>
6358 ** <dd>This parameter returns the approximate number of of bytes of heap
6359 ** and lookaside memory used by all prepared statements associated with
6360 ** the database connection.)^
6361 ** ^The highwater mark associated with SQLITE_DBSTATUS_STMT_USED is always 0.
6362 ** </dd>
6363 **
6364 ** [[SQLITE_DBSTATUS_CACHE_HIT]] ^(<dt>SQLITE_DBSTATUS_CACHE_HIT</dt>
6365 ** <dd>This parameter returns the number of pager cache hits that have
6366 ** occurred.)^ ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_HIT
6367 ** is always 0.
6368 ** </dd>
6369 **
6370 ** [[SQLITE_DBSTATUS_CACHE_MISS]] ^(<dt>SQLITE_DBSTATUS_CACHE_MISS</dt>
6371 ** <dd>This parameter returns the number of pager cache misses that have
6372 ** occurred.)^ ^The highwater mark associated with SQLITE_DBSTATUS_CACHE_MISS
6373 ** is always 0.
6374 ** </dd>
6375 **
6376 ** [[SQLITE_DBSTATUS_CACHE_WRITE]] ^(<dt>SQLITE_DBSTATUS_CACHE_WRITE</dt>
6377 ** <dd>This parameter returns the number of dirty cache entries that have
6378 ** been written to disk. Specifically, the number of pages written to the
6379 ** wal file in wal mode databases, or the number of pages written to the
6380 ** database file in rollback mode databases. Any pages written as part of
6381 ** transaction rollback or database recovery operations are not included.
6382 ** If an IO or other error occurs while writing a page to disk, the effect
6383 ** on subsequent SQLITE_DBSTATUS_CACHE_WRITE requests is undefined.)^ ^The
6384 ** highwater mark associated with SQLITE_DBSTATUS_CACHE_WRITE is always 0.
6385 ** </dd>
6386 **
6387 ** [[SQLITE_DBSTATUS_DEFERRED_FKS]] ^(<dt>SQLITE_DBSTATUS_DEFERRED_FKS</dt>
6388 ** <dd>This parameter returns zero for the current value if and only if
6389 ** all foreign key constraints (deferred or immediate) have been
6390 ** resolved.)^  ^The highwater mark is always 0.
6391 ** </dd>
6392 ** </dl>
6393 */
6394 #define SQLITE_DBSTATUS_LOOKASIDE_USED       0
6395 #define SQLITE_DBSTATUS_CACHE_USED           1
6396 #define SQLITE_DBSTATUS_SCHEMA_USED          2
6397 #define SQLITE_DBSTATUS_STMT_USED            3
6398 #define SQLITE_DBSTATUS_LOOKASIDE_HIT        4
6399 #define SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE  5
6400 #define SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL  6
6401 #define SQLITE_DBSTATUS_CACHE_HIT            7
6402 #define SQLITE_DBSTATUS_CACHE_MISS           8
6403 #define SQLITE_DBSTATUS_CACHE_WRITE          9
6404 #define SQLITE_DBSTATUS_DEFERRED_FKS        10
6405 #define SQLITE_DBSTATUS_MAX                 10   /* Largest defined DBSTATUS */
6406 
6407 
6408 /*
6409 ** CAPI3REF: Prepared Statement Status
6410 **
6411 ** ^(Each prepared statement maintains various
6412 ** [SQLITE_STMTSTATUS counters] that measure the number
6413 ** of times it has performed specific operations.)^  These counters can
6414 ** be used to monitor the performance characteristics of the prepared
6415 ** statements.  For example, if the number of table steps greatly exceeds
6416 ** the number of table searches or result rows, that would tend to indicate
6417 ** that the prepared statement is using a full table scan rather than
6418 ** an index.
6419 **
6420 ** ^(This interface is used to retrieve and reset counter values from
6421 ** a [prepared statement].  The first argument is the prepared statement
6422 ** object to be interrogated.  The second argument
6423 ** is an integer code for a specific [SQLITE_STMTSTATUS counter]
6424 ** to be interrogated.)^
6425 ** ^The current value of the requested counter is returned.
6426 ** ^If the resetFlg is true, then the counter is reset to zero after this
6427 ** interface call returns.
6428 **
6429 ** See also: [sqlite3_status()] and [sqlite3_db_status()].
6430 */
6431 SQLITE_API int sqlite3_stmt_status(sqlite3_stmt*, int op,int resetFlg);
6432 
6433 /*
6434 ** CAPI3REF: Status Parameters for prepared statements
6435 ** KEYWORDS: {SQLITE_STMTSTATUS counter} {SQLITE_STMTSTATUS counters}
6436 **
6437 ** These preprocessor macros define integer codes that name counter
6438 ** values associated with the [sqlite3_stmt_status()] interface.
6439 ** The meanings of the various counters are as follows:
6440 **
6441 ** <dl>
6442 ** [[SQLITE_STMTSTATUS_FULLSCAN_STEP]] <dt>SQLITE_STMTSTATUS_FULLSCAN_STEP</dt>
6443 ** <dd>^This is the number of times that SQLite has stepped forward in
6444 ** a table as part of a full table scan.  Large numbers for this counter
6445 ** may indicate opportunities for performance improvement through
6446 ** careful use of indices.</dd>
6447 **
6448 ** [[SQLITE_STMTSTATUS_SORT]] <dt>SQLITE_STMTSTATUS_SORT</dt>
6449 ** <dd>^This is the number of sort operations that have occurred.
6450 ** A non-zero value in this counter may indicate an opportunity to
6451 ** improvement performance through careful use of indices.</dd>
6452 **
6453 ** [[SQLITE_STMTSTATUS_AUTOINDEX]] <dt>SQLITE_STMTSTATUS_AUTOINDEX</dt>
6454 ** <dd>^This is the number of rows inserted into transient indices that
6455 ** were created automatically in order to help joins run faster.
6456 ** A non-zero value in this counter may indicate an opportunity to
6457 ** improvement performance by adding permanent indices that do not
6458 ** need to be reinitialized each time the statement is run.</dd>
6459 **
6460 ** [[SQLITE_STMTSTATUS_VM_STEP]] <dt>SQLITE_STMTSTATUS_VM_STEP</dt>
6461 ** <dd>^This is the number of virtual machine operations executed
6462 ** by the prepared statement if that number is less than or equal
6463 ** to 2147483647.  The number of virtual machine operations can be
6464 ** used as a proxy for the total work done by the prepared statement.
6465 ** If the number of virtual machine operations exceeds 2147483647
6466 ** then the value returned by this statement status code is undefined.
6467 ** </dd>
6468 ** </dl>
6469 */
6470 #define SQLITE_STMTSTATUS_FULLSCAN_STEP     1
6471 #define SQLITE_STMTSTATUS_SORT              2
6472 #define SQLITE_STMTSTATUS_AUTOINDEX         3
6473 #define SQLITE_STMTSTATUS_VM_STEP           4
6474 
6475 /*
6476 ** CAPI3REF: Custom Page Cache Object
6477 **
6478 ** The sqlite3_pcache type is opaque.  It is implemented by
6479 ** the pluggable module.  The SQLite core has no knowledge of
6480 ** its size or internal structure and never deals with the
6481 ** sqlite3_pcache object except by holding and passing pointers
6482 ** to the object.
6483 **
6484 ** See [sqlite3_pcache_methods2] for additional information.
6485 */
6486 typedef struct sqlite3_pcache sqlite3_pcache;
6487 
6488 /*
6489 ** CAPI3REF: Custom Page Cache Object
6490 **
6491 ** The sqlite3_pcache_page object represents a single page in the
6492 ** page cache.  The page cache will allocate instances of this
6493 ** object.  Various methods of the page cache use pointers to instances
6494 ** of this object as parameters or as their return value.
6495 **
6496 ** See [sqlite3_pcache_methods2] for additional information.
6497 */
6498 typedef struct sqlite3_pcache_page sqlite3_pcache_page;
6499 struct sqlite3_pcache_page {
6500   void *pBuf;        /* The content of the page */
6501   void *pExtra;      /* Extra information associated with the page */
6502 };
6503 
6504 /*
6505 ** CAPI3REF: Application Defined Page Cache.
6506 ** KEYWORDS: {page cache}
6507 **
6508 ** ^(The [sqlite3_config]([SQLITE_CONFIG_PCACHE2], ...) interface can
6509 ** register an alternative page cache implementation by passing in an
6510 ** instance of the sqlite3_pcache_methods2 structure.)^
6511 ** In many applications, most of the heap memory allocated by
6512 ** SQLite is used for the page cache.
6513 ** By implementing a
6514 ** custom page cache using this API, an application can better control
6515 ** the amount of memory consumed by SQLite, the way in which
6516 ** that memory is allocated and released, and the policies used to
6517 ** determine exactly which parts of a database file are cached and for
6518 ** how long.
6519 **
6520 ** The alternative page cache mechanism is an
6521 ** extreme measure that is only needed by the most demanding applications.
6522 ** The built-in page cache is recommended for most uses.
6523 **
6524 ** ^(The contents of the sqlite3_pcache_methods2 structure are copied to an
6525 ** internal buffer by SQLite within the call to [sqlite3_config].  Hence
6526 ** the application may discard the parameter after the call to
6527 ** [sqlite3_config()] returns.)^
6528 **
6529 ** [[the xInit() page cache method]]
6530 ** ^(The xInit() method is called once for each effective
6531 ** call to [sqlite3_initialize()])^
6532 ** (usually only once during the lifetime of the process). ^(The xInit()
6533 ** method is passed a copy of the sqlite3_pcache_methods2.pArg value.)^
6534 ** The intent of the xInit() method is to set up global data structures
6535 ** required by the custom page cache implementation.
6536 ** ^(If the xInit() method is NULL, then the
6537 ** built-in default page cache is used instead of the application defined
6538 ** page cache.)^
6539 **
6540 ** [[the xShutdown() page cache method]]
6541 ** ^The xShutdown() method is called by [sqlite3_shutdown()].
6542 ** It can be used to clean up
6543 ** any outstanding resources before process shutdown, if required.
6544 ** ^The xShutdown() method may be NULL.
6545 **
6546 ** ^SQLite automatically serializes calls to the xInit method,
6547 ** so the xInit method need not be threadsafe.  ^The
6548 ** xShutdown method is only called from [sqlite3_shutdown()] so it does
6549 ** not need to be threadsafe either.  All other methods must be threadsafe
6550 ** in multithreaded applications.
6551 **
6552 ** ^SQLite will never invoke xInit() more than once without an intervening
6553 ** call to xShutdown().
6554 **
6555 ** [[the xCreate() page cache methods]]
6556 ** ^SQLite invokes the xCreate() method to construct a new cache instance.
6557 ** SQLite will typically create one cache instance for each open database file,
6558 ** though this is not guaranteed. ^The
6559 ** first parameter, szPage, is the size in bytes of the pages that must
6560 ** be allocated by the cache.  ^szPage will always a power of two.  ^The
6561 ** second parameter szExtra is a number of bytes of extra storage
6562 ** associated with each page cache entry.  ^The szExtra parameter will
6563 ** a number less than 250.  SQLite will use the
6564 ** extra szExtra bytes on each page to store metadata about the underlying
6565 ** database page on disk.  The value passed into szExtra depends
6566 ** on the SQLite version, the target platform, and how SQLite was compiled.
6567 ** ^The third argument to xCreate(), bPurgeable, is true if the cache being
6568 ** created will be used to cache database pages of a file stored on disk, or
6569 ** false if it is used for an in-memory database. The cache implementation
6570 ** does not have to do anything special based with the value of bPurgeable;
6571 ** it is purely advisory.  ^On a cache where bPurgeable is false, SQLite will
6572 ** never invoke xUnpin() except to deliberately delete a page.
6573 ** ^In other words, calls to xUnpin() on a cache with bPurgeable set to
6574 ** false will always have the "discard" flag set to true.
6575 ** ^Hence, a cache created with bPurgeable false will
6576 ** never contain any unpinned pages.
6577 **
6578 ** [[the xCachesize() page cache method]]
6579 ** ^(The xCachesize() method may be called at any time by SQLite to set the
6580 ** suggested maximum cache-size (number of pages stored by) the cache
6581 ** instance passed as the first argument. This is the value configured using
6582 ** the SQLite "[PRAGMA cache_size]" command.)^  As with the bPurgeable
6583 ** parameter, the implementation is not required to do anything with this
6584 ** value; it is advisory only.
6585 **
6586 ** [[the xPagecount() page cache methods]]
6587 ** The xPagecount() method must return the number of pages currently
6588 ** stored in the cache, both pinned and unpinned.
6589 **
6590 ** [[the xFetch() page cache methods]]
6591 ** The xFetch() method locates a page in the cache and returns a pointer to
6592 ** an sqlite3_pcache_page object associated with that page, or a NULL pointer.
6593 ** The pBuf element of the returned sqlite3_pcache_page object will be a
6594 ** pointer to a buffer of szPage bytes used to store the content of a
6595 ** single database page.  The pExtra element of sqlite3_pcache_page will be
6596 ** a pointer to the szExtra bytes of extra storage that SQLite has requested
6597 ** for each entry in the page cache.
6598 **
6599 ** The page to be fetched is determined by the key. ^The minimum key value
6600 ** is 1.  After it has been retrieved using xFetch, the page is considered
6601 ** to be "pinned".
6602 **
6603 ** If the requested page is already in the page cache, then the page cache
6604 ** implementation must return a pointer to the page buffer with its content
6605 ** intact.  If the requested page is not already in the cache, then the
6606 ** cache implementation should use the value of the createFlag
6607 ** parameter to help it determined what action to take:
6608 **
6609 ** <table border=1 width=85% align=center>
6610 ** <tr><th> createFlag <th> Behavior when page is not already in cache
6611 ** <tr><td> 0 <td> Do not allocate a new page.  Return NULL.
6612 ** <tr><td> 1 <td> Allocate a new page if it easy and convenient to do so.
6613 **                 Otherwise return NULL.
6614 ** <tr><td> 2 <td> Make every effort to allocate a new page.  Only return
6615 **                 NULL if allocating a new page is effectively impossible.
6616 ** </table>
6617 **
6618 ** ^(SQLite will normally invoke xFetch() with a createFlag of 0 or 1.  SQLite
6619 ** will only use a createFlag of 2 after a prior call with a createFlag of 1
6620 ** failed.)^  In between the to xFetch() calls, SQLite may
6621 ** attempt to unpin one or more cache pages by spilling the content of
6622 ** pinned pages to disk and synching the operating system disk cache.
6623 **
6624 ** [[the xUnpin() page cache method]]
6625 ** ^xUnpin() is called by SQLite with a pointer to a currently pinned page
6626 ** as its second argument.  If the third parameter, discard, is non-zero,
6627 ** then the page must be evicted from the cache.
6628 ** ^If the discard parameter is
6629 ** zero, then the page may be discarded or retained at the discretion of
6630 ** page cache implementation. ^The page cache implementation
6631 ** may choose to evict unpinned pages at any time.
6632 **
6633 ** The cache must not perform any reference counting. A single
6634 ** call to xUnpin() unpins the page regardless of the number of prior calls
6635 ** to xFetch().
6636 **
6637 ** [[the xRekey() page cache methods]]
6638 ** The xRekey() method is used to change the key value associated with the
6639 ** page passed as the second argument. If the cache
6640 ** previously contains an entry associated with newKey, it must be
6641 ** discarded. ^Any prior cache entry associated with newKey is guaranteed not
6642 ** to be pinned.
6643 **
6644 ** When SQLite calls the xTruncate() method, the cache must discard all
6645 ** existing cache entries with page numbers (keys) greater than or equal
6646 ** to the value of the iLimit parameter passed to xTruncate(). If any
6647 ** of these pages are pinned, they are implicitly unpinned, meaning that
6648 ** they can be safely discarded.
6649 **
6650 ** [[the xDestroy() page cache method]]
6651 ** ^The xDestroy() method is used to delete a cache allocated by xCreate().
6652 ** All resources associated with the specified cache should be freed. ^After
6653 ** calling the xDestroy() method, SQLite considers the [sqlite3_pcache*]
6654 ** handle invalid, and will not use it with any other sqlite3_pcache_methods2
6655 ** functions.
6656 **
6657 ** [[the xShrink() page cache method]]
6658 ** ^SQLite invokes the xShrink() method when it wants the page cache to
6659 ** free up as much of heap memory as possible.  The page cache implementation
6660 ** is not obligated to free any memory, but well-behaved implementations should
6661 ** do their best.
6662 */
6663 typedef struct sqlite3_pcache_methods2 sqlite3_pcache_methods2;
6664 struct sqlite3_pcache_methods2 {
6665   int iVersion;
6666   void *pArg;
6667   int (*xInit)(void*);
6668   void (*xShutdown)(void*);
6669   sqlite3_pcache *(*xCreate)(int szPage, int szExtra, int bPurgeable);
6670   void (*xCachesize)(sqlite3_pcache*, int nCachesize);
6671   int (*xPagecount)(sqlite3_pcache*);
6672   sqlite3_pcache_page *(*xFetch)(sqlite3_pcache*, unsigned key, int createFlag);
6673   void (*xUnpin)(sqlite3_pcache*, sqlite3_pcache_page*, int discard);
6674   void (*xRekey)(sqlite3_pcache*, sqlite3_pcache_page*,
6675       unsigned oldKey, unsigned newKey);
6676   void (*xTruncate)(sqlite3_pcache*, unsigned iLimit);
6677   void (*xDestroy)(sqlite3_pcache*);
6678   void (*xShrink)(sqlite3_pcache*);
6679 };
6680 
6681 /*
6682 ** This is the obsolete pcache_methods object that has now been replaced
6683 ** by sqlite3_pcache_methods2.  This object is not used by SQLite.  It is
6684 ** retained in the header file for backwards compatibility only.
6685 */
6686 typedef struct sqlite3_pcache_methods sqlite3_pcache_methods;
6687 struct sqlite3_pcache_methods {
6688   void *pArg;
6689   int (*xInit)(void*);
6690   void (*xShutdown)(void*);
6691   sqlite3_pcache *(*xCreate)(int szPage, int bPurgeable);
6692   void (*xCachesize)(sqlite3_pcache*, int nCachesize);
6693   int (*xPagecount)(sqlite3_pcache*);
6694   void *(*xFetch)(sqlite3_pcache*, unsigned key, int createFlag);
6695   void (*xUnpin)(sqlite3_pcache*, void*, int discard);
6696   void (*xRekey)(sqlite3_pcache*, void*, unsigned oldKey, unsigned newKey);
6697   void (*xTruncate)(sqlite3_pcache*, unsigned iLimit);
6698   void (*xDestroy)(sqlite3_pcache*);
6699 };
6700 
6701 
6702 /*
6703 ** CAPI3REF: Online Backup Object
6704 **
6705 ** The sqlite3_backup object records state information about an ongoing
6706 ** online backup operation.  ^The sqlite3_backup object is created by
6707 ** a call to [sqlite3_backup_init()] and is destroyed by a call to
6708 ** [sqlite3_backup_finish()].
6709 **
6710 ** See Also: [Using the SQLite Online Backup API]
6711 */
6712 typedef struct sqlite3_backup sqlite3_backup;
6713 
6714 /*
6715 ** CAPI3REF: Online Backup API.
6716 **
6717 ** The backup API copies the content of one database into another.
6718 ** It is useful either for creating backups of databases or
6719 ** for copying in-memory databases to or from persistent files.
6720 **
6721 ** See Also: [Using the SQLite Online Backup API]
6722 **
6723 ** ^SQLite holds a write transaction open on the destination database file
6724 ** for the duration of the backup operation.
6725 ** ^The source database is read-locked only while it is being read;
6726 ** it is not locked continuously for the entire backup operation.
6727 ** ^Thus, the backup may be performed on a live source database without
6728 ** preventing other database connections from
6729 ** reading or writing to the source database while the backup is underway.
6730 **
6731 ** ^(To perform a backup operation:
6732 **   <ol>
6733 **     <li><b>sqlite3_backup_init()</b> is called once to initialize the
6734 **         backup,
6735 **     <li><b>sqlite3_backup_step()</b> is called one or more times to transfer
6736 **         the data between the two databases, and finally
6737 **     <li><b>sqlite3_backup_finish()</b> is called to release all resources
6738 **         associated with the backup operation.
6739 **   </ol>)^
6740 ** There should be exactly one call to sqlite3_backup_finish() for each
6741 ** successful call to sqlite3_backup_init().
6742 **
6743 ** [[sqlite3_backup_init()]] <b>sqlite3_backup_init()</b>
6744 **
6745 ** ^The D and N arguments to sqlite3_backup_init(D,N,S,M) are the
6746 ** [database connection] associated with the destination database
6747 ** and the database name, respectively.
6748 ** ^The database name is "main" for the main database, "temp" for the
6749 ** temporary database, or the name specified after the AS keyword in
6750 ** an [ATTACH] statement for an attached database.
6751 ** ^The S and M arguments passed to
6752 ** sqlite3_backup_init(D,N,S,M) identify the [database connection]
6753 ** and database name of the source database, respectively.
6754 ** ^The source and destination [database connections] (parameters S and D)
6755 ** must be different or else sqlite3_backup_init(D,N,S,M) will fail with
6756 ** an error.
6757 **
6758 ** ^If an error occurs within sqlite3_backup_init(D,N,S,M), then NULL is
6759 ** returned and an error code and error message are stored in the
6760 ** destination [database connection] D.
6761 ** ^The error code and message for the failed call to sqlite3_backup_init()
6762 ** can be retrieved using the [sqlite3_errcode()], [sqlite3_errmsg()], and/or
6763 ** [sqlite3_errmsg16()] functions.
6764 ** ^A successful call to sqlite3_backup_init() returns a pointer to an
6765 ** [sqlite3_backup] object.
6766 ** ^The [sqlite3_backup] object may be used with the sqlite3_backup_step() and
6767 ** sqlite3_backup_finish() functions to perform the specified backup
6768 ** operation.
6769 **
6770 ** [[sqlite3_backup_step()]] <b>sqlite3_backup_step()</b>
6771 **
6772 ** ^Function sqlite3_backup_step(B,N) will copy up to N pages between
6773 ** the source and destination databases specified by [sqlite3_backup] object B.
6774 ** ^If N is negative, all remaining source pages are copied.
6775 ** ^If sqlite3_backup_step(B,N) successfully copies N pages and there
6776 ** are still more pages to be copied, then the function returns [SQLITE_OK].
6777 ** ^If sqlite3_backup_step(B,N) successfully finishes copying all pages
6778 ** from source to destination, then it returns [SQLITE_DONE].
6779 ** ^If an error occurs while running sqlite3_backup_step(B,N),
6780 ** then an [error code] is returned. ^As well as [SQLITE_OK] and
6781 ** [SQLITE_DONE], a call to sqlite3_backup_step() may return [SQLITE_READONLY],
6782 ** [SQLITE_NOMEM], [SQLITE_BUSY], [SQLITE_LOCKED], or an
6783 ** [SQLITE_IOERR_ACCESS | SQLITE_IOERR_XXX] extended error code.
6784 **
6785 ** ^(The sqlite3_backup_step() might return [SQLITE_READONLY] if
6786 ** <ol>
6787 ** <li> the destination database was opened read-only, or
6788 ** <li> the destination database is using write-ahead-log journaling
6789 ** and the destination and source page sizes differ, or
6790 ** <li> the destination database is an in-memory database and the
6791 ** destination and source page sizes differ.
6792 ** </ol>)^
6793 **
6794 ** ^If sqlite3_backup_step() cannot obtain a required file-system lock, then
6795 ** the [sqlite3_busy_handler | busy-handler function]
6796 ** is invoked (if one is specified). ^If the
6797 ** busy-handler returns non-zero before the lock is available, then
6798 ** [SQLITE_BUSY] is returned to the caller. ^In this case the call to
6799 ** sqlite3_backup_step() can be retried later. ^If the source
6800 ** [database connection]
6801 ** is being used to write to the source database when sqlite3_backup_step()
6802 ** is called, then [SQLITE_LOCKED] is returned immediately. ^Again, in this
6803 ** case the call to sqlite3_backup_step() can be retried later on. ^(If
6804 ** [SQLITE_IOERR_ACCESS | SQLITE_IOERR_XXX], [SQLITE_NOMEM], or
6805 ** [SQLITE_READONLY] is returned, then
6806 ** there is no point in retrying the call to sqlite3_backup_step(). These
6807 ** errors are considered fatal.)^  The application must accept
6808 ** that the backup operation has failed and pass the backup operation handle
6809 ** to the sqlite3_backup_finish() to release associated resources.
6810 **
6811 ** ^The first call to sqlite3_backup_step() obtains an exclusive lock
6812 ** on the destination file. ^The exclusive lock is not released until either
6813 ** sqlite3_backup_finish() is called or the backup operation is complete
6814 ** and sqlite3_backup_step() returns [SQLITE_DONE].  ^Every call to
6815 ** sqlite3_backup_step() obtains a [shared lock] on the source database that
6816 ** lasts for the duration of the sqlite3_backup_step() call.
6817 ** ^Because the source database is not locked between calls to
6818 ** sqlite3_backup_step(), the source database may be modified mid-way
6819 ** through the backup process.  ^If the source database is modified by an
6820 ** external process or via a database connection other than the one being
6821 ** used by the backup operation, then the backup will be automatically
6822 ** restarted by the next call to sqlite3_backup_step(). ^If the source
6823 ** database is modified by the using the same database connection as is used
6824 ** by the backup operation, then the backup database is automatically
6825 ** updated at the same time.
6826 **
6827 ** [[sqlite3_backup_finish()]] <b>sqlite3_backup_finish()</b>
6828 **
6829 ** When sqlite3_backup_step() has returned [SQLITE_DONE], or when the
6830 ** application wishes to abandon the backup operation, the application
6831 ** should destroy the [sqlite3_backup] by passing it to sqlite3_backup_finish().
6832 ** ^The sqlite3_backup_finish() interfaces releases all
6833 ** resources associated with the [sqlite3_backup] object.
6834 ** ^If sqlite3_backup_step() has not yet returned [SQLITE_DONE], then any
6835 ** active write-transaction on the destination database is rolled back.
6836 ** The [sqlite3_backup] object is invalid
6837 ** and may not be used following a call to sqlite3_backup_finish().
6838 **
6839 ** ^The value returned by sqlite3_backup_finish is [SQLITE_OK] if no
6840 ** sqlite3_backup_step() errors occurred, regardless or whether or not
6841 ** sqlite3_backup_step() completed.
6842 ** ^If an out-of-memory condition or IO error occurred during any prior
6843 ** sqlite3_backup_step() call on the same [sqlite3_backup] object, then
6844 ** sqlite3_backup_finish() returns the corresponding [error code].
6845 **
6846 ** ^A return of [SQLITE_BUSY] or [SQLITE_LOCKED] from sqlite3_backup_step()
6847 ** is not a permanent error and does not affect the return value of
6848 ** sqlite3_backup_finish().
6849 **
6850 ** [[sqlite3_backup__remaining()]] [[sqlite3_backup_pagecount()]]
6851 ** <b>sqlite3_backup_remaining() and sqlite3_backup_pagecount()</b>
6852 **
6853 ** ^Each call to sqlite3_backup_step() sets two values inside
6854 ** the [sqlite3_backup] object: the number of pages still to be backed
6855 ** up and the total number of pages in the source database file.
6856 ** The sqlite3_backup_remaining() and sqlite3_backup_pagecount() interfaces
6857 ** retrieve these two values, respectively.
6858 **
6859 ** ^The values returned by these functions are only updated by
6860 ** sqlite3_backup_step(). ^If the source database is modified during a backup
6861 ** operation, then the values are not updated to account for any extra
6862 ** pages that need to be updated or the size of the source database file
6863 ** changing.
6864 **
6865 ** <b>Concurrent Usage of Database Handles</b>
6866 **
6867 ** ^The source [database connection] may be used by the application for other
6868 ** purposes while a backup operation is underway or being initialized.
6869 ** ^If SQLite is compiled and configured to support threadsafe database
6870 ** connections, then the source database connection may be used concurrently
6871 ** from within other threads.
6872 **
6873 ** However, the application must guarantee that the destination
6874 ** [database connection] is not passed to any other API (by any thread) after
6875 ** sqlite3_backup_init() is called and before the corresponding call to
6876 ** sqlite3_backup_finish().  SQLite does not currently check to see
6877 ** if the application incorrectly accesses the destination [database connection]
6878 ** and so no error code is reported, but the operations may malfunction
6879 ** nevertheless.  Use of the destination database connection while a
6880 ** backup is in progress might also also cause a mutex deadlock.
6881 **
6882 ** If running in [shared cache mode], the application must
6883 ** guarantee that the shared cache used by the destination database
6884 ** is not accessed while the backup is running. In practice this means
6885 ** that the application must guarantee that the disk file being
6886 ** backed up to is not accessed by any connection within the process,
6887 ** not just the specific connection that was passed to sqlite3_backup_init().
6888 **
6889 ** The [sqlite3_backup] object itself is partially threadsafe. Multiple
6890 ** threads may safely make multiple concurrent calls to sqlite3_backup_step().
6891 ** However, the sqlite3_backup_remaining() and sqlite3_backup_pagecount()
6892 ** APIs are not strictly speaking threadsafe. If they are invoked at the
6893 ** same time as another thread is invoking sqlite3_backup_step() it is
6894 ** possible that they return invalid values.
6895 */
6896 SQLITE_API sqlite3_backup *sqlite3_backup_init(
6897   sqlite3 *pDest,                        /* Destination database handle */
6898   const char *zDestName,                 /* Destination database name */
6899   sqlite3 *pSource,                      /* Source database handle */
6900   const char *zSourceName                /* Source database name */
6901 );
6902 SQLITE_API int sqlite3_backup_step(sqlite3_backup *p, int nPage);
6903 SQLITE_API int sqlite3_backup_finish(sqlite3_backup *p);
6904 SQLITE_API int sqlite3_backup_remaining(sqlite3_backup *p);
6905 SQLITE_API int sqlite3_backup_pagecount(sqlite3_backup *p);
6906 
6907 /*
6908 ** CAPI3REF: Unlock Notification
6909 **
6910 ** ^When running in shared-cache mode, a database operation may fail with
6911 ** an [SQLITE_LOCKED] error if the required locks on the shared-cache or
6912 ** individual tables within the shared-cache cannot be obtained. See
6913 ** [SQLite Shared-Cache Mode] for a description of shared-cache locking.
6914 ** ^This API may be used to register a callback that SQLite will invoke
6915 ** when the connection currently holding the required lock relinquishes it.
6916 ** ^This API is only available if the library was compiled with the
6917 ** [SQLITE_ENABLE_UNLOCK_NOTIFY] C-preprocessor symbol defined.
6918 **
6919 ** See Also: [Using the SQLite Unlock Notification Feature].
6920 **
6921 ** ^Shared-cache locks are released when a database connection concludes
6922 ** its current transaction, either by committing it or rolling it back.
6923 **
6924 ** ^When a connection (known as the blocked connection) fails to obtain a
6925 ** shared-cache lock and SQLITE_LOCKED is returned to the caller, the
6926 ** identity of the database connection (the blocking connection) that
6927 ** has locked the required resource is stored internally. ^After an
6928 ** application receives an SQLITE_LOCKED error, it may call the
6929 ** sqlite3_unlock_notify() method with the blocked connection handle as
6930 ** the first argument to register for a callback that will be invoked
6931 ** when the blocking connections current transaction is concluded. ^The
6932 ** callback is invoked from within the [sqlite3_step] or [sqlite3_close]
6933 ** call that concludes the blocking connections transaction.
6934 **
6935 ** ^(If sqlite3_unlock_notify() is called in a multi-threaded application,
6936 ** there is a chance that the blocking connection will have already
6937 ** concluded its transaction by the time sqlite3_unlock_notify() is invoked.
6938 ** If this happens, then the specified callback is invoked immediately,
6939 ** from within the call to sqlite3_unlock_notify().)^
6940 **
6941 ** ^If the blocked connection is attempting to obtain a write-lock on a
6942 ** shared-cache table, and more than one other connection currently holds
6943 ** a read-lock on the same table, then SQLite arbitrarily selects one of
6944 ** the other connections to use as the blocking connection.
6945 **
6946 ** ^(There may be at most one unlock-notify callback registered by a
6947 ** blocked connection. If sqlite3_unlock_notify() is called when the
6948 ** blocked connection already has a registered unlock-notify callback,
6949 ** then the new callback replaces the old.)^ ^If sqlite3_unlock_notify() is
6950 ** called with a NULL pointer as its second argument, then any existing
6951 ** unlock-notify callback is canceled. ^The blocked connections
6952 ** unlock-notify callback may also be canceled by closing the blocked
6953 ** connection using [sqlite3_close()].
6954 **
6955 ** The unlock-notify callback is not reentrant. If an application invokes
6956 ** any sqlite3_xxx API functions from within an unlock-notify callback, a
6957 ** crash or deadlock may be the result.
6958 **
6959 ** ^Unless deadlock is detected (see below), sqlite3_unlock_notify() always
6960 ** returns SQLITE_OK.
6961 **
6962 ** <b>Callback Invocation Details</b>
6963 **
6964 ** When an unlock-notify callback is registered, the application provides a
6965 ** single void* pointer that is passed to the callback when it is invoked.
6966 ** However, the signature of the callback function allows SQLite to pass
6967 ** it an array of void* context pointers. The first argument passed to
6968 ** an unlock-notify callback is a pointer to an array of void* pointers,
6969 ** and the second is the number of entries in the array.
6970 **
6971 ** When a blocking connections transaction is concluded, there may be
6972 ** more than one blocked connection that has registered for an unlock-notify
6973 ** callback. ^If two or more such blocked connections have specified the
6974 ** same callback function, then instead of invoking the callback function
6975 ** multiple times, it is invoked once with the set of void* context pointers
6976 ** specified by the blocked connections bundled together into an array.
6977 ** This gives the application an opportunity to prioritize any actions
6978 ** related to the set of unblocked database connections.
6979 **
6980 ** <b>Deadlock Detection</b>
6981 **
6982 ** Assuming that after registering for an unlock-notify callback a
6983 ** database waits for the callback to be issued before taking any further
6984 ** action (a reasonable assumption), then using this API may cause the
6985 ** application to deadlock. For example, if connection X is waiting for
6986 ** connection Y's transaction to be concluded, and similarly connection
6987 ** Y is waiting on connection X's transaction, then neither connection
6988 ** will proceed and the system may remain deadlocked indefinitely.
6989 **
6990 ** To avoid this scenario, the sqlite3_unlock_notify() performs deadlock
6991 ** detection. ^If a given call to sqlite3_unlock_notify() would put the
6992 ** system in a deadlocked state, then SQLITE_LOCKED is returned and no
6993 ** unlock-notify callback is registered. The system is said to be in
6994 ** a deadlocked state if connection A has registered for an unlock-notify
6995 ** callback on the conclusion of connection B's transaction, and connection
6996 ** B has itself registered for an unlock-notify callback when connection
6997 ** A's transaction is concluded. ^Indirect deadlock is also detected, so
6998 ** the system is also considered to be deadlocked if connection B has
6999 ** registered for an unlock-notify callback on the conclusion of connection
7000 ** C's transaction, where connection C is waiting on connection A. ^Any
7001 ** number of levels of indirection are allowed.
7002 **
7003 ** <b>The "DROP TABLE" Exception</b>
7004 **
7005 ** When a call to [sqlite3_step()] returns SQLITE_LOCKED, it is almost
7006 ** always appropriate to call sqlite3_unlock_notify(). There is however,
7007 ** one exception. When executing a "DROP TABLE" or "DROP INDEX" statement,
7008 ** SQLite checks if there are any currently executing SELECT statements
7009 ** that belong to the same connection. If there are, SQLITE_LOCKED is
7010 ** returned. In this case there is no "blocking connection", so invoking
7011 ** sqlite3_unlock_notify() results in the unlock-notify callback being
7012 ** invoked immediately. If the application then re-attempts the "DROP TABLE"
7013 ** or "DROP INDEX" query, an infinite loop might be the result.
7014 **
7015 ** One way around this problem is to check the extended error code returned
7016 ** by an sqlite3_step() call. ^(If there is a blocking connection, then the
7017 ** extended error code is set to SQLITE_LOCKED_SHAREDCACHE. Otherwise, in
7018 ** the special "DROP TABLE/INDEX" case, the extended error code is just
7019 ** SQLITE_LOCKED.)^
7020 */
7021 SQLITE_API int sqlite3_unlock_notify(
7022   sqlite3 *pBlocked,                          /* Waiting connection */
7023   void (*xNotify)(void **apArg, int nArg),    /* Callback function to invoke */
7024   void *pNotifyArg                            /* Argument to pass to xNotify */
7025 );
7026 
7027 
7028 /*
7029 ** CAPI3REF: String Comparison
7030 **
7031 ** ^The [sqlite3_stricmp()] and [sqlite3_strnicmp()] APIs allow applications
7032 ** and extensions to compare the contents of two buffers containing UTF-8
7033 ** strings in a case-independent fashion, using the same definition of "case
7034 ** independence" that SQLite uses internally when comparing identifiers.
7035 */
7036 SQLITE_API int sqlite3_stricmp(const char *, const char *);
7037 SQLITE_API int sqlite3_strnicmp(const char *, const char *, int);
7038 
7039 /*
7040 ** CAPI3REF: String Globbing
7041 *
7042 ** ^The [sqlite3_strglob(P,X)] interface returns zero if string X matches
7043 ** the glob pattern P, and it returns non-zero if string X does not match
7044 ** the glob pattern P.  ^The definition of glob pattern matching used in
7045 ** [sqlite3_strglob(P,X)] is the same as for the "X GLOB P" operator in the
7046 ** SQL dialect used by SQLite.  ^The sqlite3_strglob(P,X) function is case
7047 ** sensitive.
7048 **
7049 ** Note that this routine returns zero on a match and non-zero if the strings
7050 ** do not match, the same as [sqlite3_stricmp()] and [sqlite3_strnicmp()].
7051 */
7052 SQLITE_API int sqlite3_strglob(const char *zGlob, const char *zStr);
7053 
7054 /*
7055 ** CAPI3REF: Error Logging Interface
7056 **
7057 ** ^The [sqlite3_log()] interface writes a message into the [error log]
7058 ** established by the [SQLITE_CONFIG_LOG] option to [sqlite3_config()].
7059 ** ^If logging is enabled, the zFormat string and subsequent arguments are
7060 ** used with [sqlite3_snprintf()] to generate the final output string.
7061 **
7062 ** The sqlite3_log() interface is intended for use by extensions such as
7063 ** virtual tables, collating functions, and SQL functions.  While there is
7064 ** nothing to prevent an application from calling sqlite3_log(), doing so
7065 ** is considered bad form.
7066 **
7067 ** The zFormat string must not be NULL.
7068 **
7069 ** To avoid deadlocks and other threading problems, the sqlite3_log() routine
7070 ** will not use dynamically allocated memory.  The log message is stored in
7071 ** a fixed-length buffer on the stack.  If the log message is longer than
7072 ** a few hundred characters, it will be truncated to the length of the
7073 ** buffer.
7074 */
7075 SQLITE_API void sqlite3_log(int iErrCode, const char *zFormat, ...);
7076 
7077 /*
7078 ** CAPI3REF: Write-Ahead Log Commit Hook
7079 **
7080 ** ^The [sqlite3_wal_hook()] function is used to register a callback that
7081 ** will be invoked each time a database connection commits data to a
7082 ** [write-ahead log] (i.e. whenever a transaction is committed in
7083 ** [journal_mode | journal_mode=WAL mode]).
7084 **
7085 ** ^The callback is invoked by SQLite after the commit has taken place and
7086 ** the associated write-lock on the database released, so the implementation
7087 ** may read, write or [checkpoint] the database as required.
7088 **
7089 ** ^The first parameter passed to the callback function when it is invoked
7090 ** is a copy of the third parameter passed to sqlite3_wal_hook() when
7091 ** registering the callback. ^The second is a copy of the database handle.
7092 ** ^The third parameter is the name of the database that was written to -
7093 ** either "main" or the name of an [ATTACH]-ed database. ^The fourth parameter
7094 ** is the number of pages currently in the write-ahead log file,
7095 ** including those that were just committed.
7096 **
7097 ** The callback function should normally return [SQLITE_OK].  ^If an error
7098 ** code is returned, that error will propagate back up through the
7099 ** SQLite code base to cause the statement that provoked the callback
7100 ** to report an error, though the commit will have still occurred. If the
7101 ** callback returns [SQLITE_ROW] or [SQLITE_DONE], or if it returns a value
7102 ** that does not correspond to any valid SQLite error code, the results
7103 ** are undefined.
7104 **
7105 ** A single database handle may have at most a single write-ahead log callback
7106 ** registered at one time. ^Calling [sqlite3_wal_hook()] replaces any
7107 ** previously registered write-ahead log callback. ^Note that the
7108 ** [sqlite3_wal_autocheckpoint()] interface and the
7109 ** [wal_autocheckpoint pragma] both invoke [sqlite3_wal_hook()] and will
7110 ** those overwrite any prior [sqlite3_wal_hook()] settings.
7111 */
7112 SQLITE_API void *sqlite3_wal_hook(
7113   sqlite3*,
7114   int(*)(void *,sqlite3*,const char*,int),
7115   void*
7116 );
7117 
7118 /*
7119 ** CAPI3REF: Configure an auto-checkpoint
7120 **
7121 ** ^The [sqlite3_wal_autocheckpoint(D,N)] is a wrapper around
7122 ** [sqlite3_wal_hook()] that causes any database on [database connection] D
7123 ** to automatically [checkpoint]
7124 ** after committing a transaction if there are N or
7125 ** more frames in the [write-ahead log] file.  ^Passing zero or
7126 ** a negative value as the nFrame parameter disables automatic
7127 ** checkpoints entirely.
7128 **
7129 ** ^The callback registered by this function replaces any existing callback
7130 ** registered using [sqlite3_wal_hook()].  ^Likewise, registering a callback
7131 ** using [sqlite3_wal_hook()] disables the automatic checkpoint mechanism
7132 ** configured by this function.
7133 **
7134 ** ^The [wal_autocheckpoint pragma] can be used to invoke this interface
7135 ** from SQL.
7136 **
7137 ** ^Every new [database connection] defaults to having the auto-checkpoint
7138 ** enabled with a threshold of 1000 or [SQLITE_DEFAULT_WAL_AUTOCHECKPOINT]
7139 ** pages.  The use of this interface
7140 ** is only necessary if the default setting is found to be suboptimal
7141 ** for a particular application.
7142 */
7143 SQLITE_API int sqlite3_wal_autocheckpoint(sqlite3 *db, int N);
7144 
7145 /*
7146 ** CAPI3REF: Checkpoint a database
7147 **
7148 ** ^The [sqlite3_wal_checkpoint(D,X)] interface causes database named X
7149 ** on [database connection] D to be [checkpointed].  ^If X is NULL or an
7150 ** empty string, then a checkpoint is run on all databases of
7151 ** connection D.  ^If the database connection D is not in
7152 ** [WAL | write-ahead log mode] then this interface is a harmless no-op.
7153 **
7154 ** ^The [wal_checkpoint pragma] can be used to invoke this interface
7155 ** from SQL.  ^The [sqlite3_wal_autocheckpoint()] interface and the
7156 ** [wal_autocheckpoint pragma] can be used to cause this interface to be
7157 ** run whenever the WAL reaches a certain size threshold.
7158 **
7159 ** See also: [sqlite3_wal_checkpoint_v2()]
7160 */
7161 SQLITE_API int sqlite3_wal_checkpoint(sqlite3 *db, const char *zDb);
7162 
7163 /*
7164 ** CAPI3REF: Checkpoint a database
7165 **
7166 ** Run a checkpoint operation on WAL database zDb attached to database
7167 ** handle db. The specific operation is determined by the value of the
7168 ** eMode parameter:
7169 **
7170 ** <dl>
7171 ** <dt>SQLITE_CHECKPOINT_PASSIVE<dd>
7172 **   Checkpoint as many frames as possible without waiting for any database
7173 **   readers or writers to finish. Sync the db file if all frames in the log
7174 **   are checkpointed. This mode is the same as calling
7175 **   sqlite3_wal_checkpoint(). The busy-handler callback is never invoked.
7176 **
7177 ** <dt>SQLITE_CHECKPOINT_FULL<dd>
7178 **   This mode blocks (calls the busy-handler callback) until there is no
7179 **   database writer and all readers are reading from the most recent database
7180 **   snapshot. It then checkpoints all frames in the log file and syncs the
7181 **   database file. This call blocks database writers while it is running,
7182 **   but not database readers.
7183 **
7184 ** <dt>SQLITE_CHECKPOINT_RESTART<dd>
7185 **   This mode works the same way as SQLITE_CHECKPOINT_FULL, except after
7186 **   checkpointing the log file it blocks (calls the busy-handler callback)
7187 **   until all readers are reading from the database file only. This ensures
7188 **   that the next client to write to the database file restarts the log file
7189 **   from the beginning. This call blocks database writers while it is running,
7190 **   but not database readers.
7191 ** </dl>
7192 **
7193 ** If pnLog is not NULL, then *pnLog is set to the total number of frames in
7194 ** the log file before returning. If pnCkpt is not NULL, then *pnCkpt is set to
7195 ** the total number of checkpointed frames (including any that were already
7196 ** checkpointed when this function is called). *pnLog and *pnCkpt may be
7197 ** populated even if sqlite3_wal_checkpoint_v2() returns other than SQLITE_OK.
7198 ** If no values are available because of an error, they are both set to -1
7199 ** before returning to communicate this to the caller.
7200 **
7201 ** All calls obtain an exclusive "checkpoint" lock on the database file. If
7202 ** any other process is running a checkpoint operation at the same time, the
7203 ** lock cannot be obtained and SQLITE_BUSY is returned. Even if there is a
7204 ** busy-handler configured, it will not be invoked in this case.
7205 **
7206 ** The SQLITE_CHECKPOINT_FULL and RESTART modes also obtain the exclusive
7207 ** "writer" lock on the database file. If the writer lock cannot be obtained
7208 ** immediately, and a busy-handler is configured, it is invoked and the writer
7209 ** lock retried until either the busy-handler returns 0 or the lock is
7210 ** successfully obtained. The busy-handler is also invoked while waiting for
7211 ** database readers as described above. If the busy-handler returns 0 before
7212 ** the writer lock is obtained or while waiting for database readers, the
7213 ** checkpoint operation proceeds from that point in the same way as
7214 ** SQLITE_CHECKPOINT_PASSIVE - checkpointing as many frames as possible
7215 ** without blocking any further. SQLITE_BUSY is returned in this case.
7216 **
7217 ** If parameter zDb is NULL or points to a zero length string, then the
7218 ** specified operation is attempted on all WAL databases. In this case the
7219 ** values written to output parameters *pnLog and *pnCkpt are undefined. If
7220 ** an SQLITE_BUSY error is encountered when processing one or more of the
7221 ** attached WAL databases, the operation is still attempted on any remaining
7222 ** attached databases and SQLITE_BUSY is returned to the caller. If any other
7223 ** error occurs while processing an attached database, processing is abandoned
7224 ** and the error code returned to the caller immediately. If no error
7225 ** (SQLITE_BUSY or otherwise) is encountered while processing the attached
7226 ** databases, SQLITE_OK is returned.
7227 **
7228 ** If database zDb is the name of an attached database that is not in WAL
7229 ** mode, SQLITE_OK is returned and both *pnLog and *pnCkpt set to -1. If
7230 ** zDb is not NULL (or a zero length string) and is not the name of any
7231 ** attached database, SQLITE_ERROR is returned to the caller.
7232 */
7233 SQLITE_API int sqlite3_wal_checkpoint_v2(
7234   sqlite3 *db,                    /* Database handle */
7235   const char *zDb,                /* Name of attached database (or NULL) */
7236   int eMode,                      /* SQLITE_CHECKPOINT_* value */
7237   int *pnLog,                     /* OUT: Size of WAL log in frames */
7238   int *pnCkpt                     /* OUT: Total number of frames checkpointed */
7239 );
7240 
7241 /*
7242 ** CAPI3REF: Checkpoint operation parameters
7243 **
7244 ** These constants can be used as the 3rd parameter to
7245 ** [sqlite3_wal_checkpoint_v2()].  See the [sqlite3_wal_checkpoint_v2()]
7246 ** documentation for additional information about the meaning and use of
7247 ** each of these values.
7248 */
7249 #define SQLITE_CHECKPOINT_PASSIVE 0
7250 #define SQLITE_CHECKPOINT_FULL    1
7251 #define SQLITE_CHECKPOINT_RESTART 2
7252 
7253 /*
7254 ** CAPI3REF: Virtual Table Interface Configuration
7255 **
7256 ** This function may be called by either the [xConnect] or [xCreate] method
7257 ** of a [virtual table] implementation to configure
7258 ** various facets of the virtual table interface.
7259 **
7260 ** If this interface is invoked outside the context of an xConnect or
7261 ** xCreate virtual table method then the behavior is undefined.
7262 **
7263 ** At present, there is only one option that may be configured using
7264 ** this function. (See [SQLITE_VTAB_CONSTRAINT_SUPPORT].)  Further options
7265 ** may be added in the future.
7266 */
7267 SQLITE_API int sqlite3_vtab_config(sqlite3*, int op, ...);
7268 
7269 /*
7270 ** CAPI3REF: Virtual Table Configuration Options
7271 **
7272 ** These macros define the various options to the
7273 ** [sqlite3_vtab_config()] interface that [virtual table] implementations
7274 ** can use to customize and optimize their behavior.
7275 **
7276 ** <dl>
7277 ** <dt>SQLITE_VTAB_CONSTRAINT_SUPPORT
7278 ** <dd>Calls of the form
7279 ** [sqlite3_vtab_config](db,SQLITE_VTAB_CONSTRAINT_SUPPORT,X) are supported,
7280 ** where X is an integer.  If X is zero, then the [virtual table] whose
7281 ** [xCreate] or [xConnect] method invoked [sqlite3_vtab_config()] does not
7282 ** support constraints.  In this configuration (which is the default) if
7283 ** a call to the [xUpdate] method returns [SQLITE_CONSTRAINT], then the entire
7284 ** statement is rolled back as if [ON CONFLICT | OR ABORT] had been
7285 ** specified as part of the users SQL statement, regardless of the actual
7286 ** ON CONFLICT mode specified.
7287 **
7288 ** If X is non-zero, then the virtual table implementation guarantees
7289 ** that if [xUpdate] returns [SQLITE_CONSTRAINT], it will do so before
7290 ** any modifications to internal or persistent data structures have been made.
7291 ** If the [ON CONFLICT] mode is ABORT, FAIL, IGNORE or ROLLBACK, SQLite
7292 ** is able to roll back a statement or database transaction, and abandon
7293 ** or continue processing the current SQL statement as appropriate.
7294 ** If the ON CONFLICT mode is REPLACE and the [xUpdate] method returns
7295 ** [SQLITE_CONSTRAINT], SQLite handles this as if the ON CONFLICT mode
7296 ** had been ABORT.
7297 **
7298 ** Virtual table implementations that are required to handle OR REPLACE
7299 ** must do so within the [xUpdate] method. If a call to the
7300 ** [sqlite3_vtab_on_conflict()] function indicates that the current ON
7301 ** CONFLICT policy is REPLACE, the virtual table implementation should
7302 ** silently replace the appropriate rows within the xUpdate callback and
7303 ** return SQLITE_OK. Or, if this is not possible, it may return
7304 ** SQLITE_CONSTRAINT, in which case SQLite falls back to OR ABORT
7305 ** constraint handling.
7306 ** </dl>
7307 */
7308 #define SQLITE_VTAB_CONSTRAINT_SUPPORT 1
7309 
7310 /*
7311 ** CAPI3REF: Determine The Virtual Table Conflict Policy
7312 **
7313 ** This function may only be called from within a call to the [xUpdate] method
7314 ** of a [virtual table] implementation for an INSERT or UPDATE operation. ^The
7315 ** value returned is one of [SQLITE_ROLLBACK], [SQLITE_IGNORE], [SQLITE_FAIL],
7316 ** [SQLITE_ABORT], or [SQLITE_REPLACE], according to the [ON CONFLICT] mode
7317 ** of the SQL statement that triggered the call to the [xUpdate] method of the
7318 ** [virtual table].
7319 */
7320 SQLITE_API int sqlite3_vtab_on_conflict(sqlite3 *);
7321 
7322 /*
7323 ** CAPI3REF: Conflict resolution modes
7324 **
7325 ** These constants are returned by [sqlite3_vtab_on_conflict()] to
7326 ** inform a [virtual table] implementation what the [ON CONFLICT] mode
7327 ** is for the SQL statement being evaluated.
7328 **
7329 ** Note that the [SQLITE_IGNORE] constant is also used as a potential
7330 ** return value from the [sqlite3_set_authorizer()] callback and that
7331 ** [SQLITE_ABORT] is also a [result code].
7332 */
7333 #define SQLITE_ROLLBACK 1
7334 /* #define SQLITE_IGNORE 2 // Also used by sqlite3_authorizer() callback */
7335 #define SQLITE_FAIL     3
7336 /* #define SQLITE_ABORT 4  // Also an error code */
7337 #define SQLITE_REPLACE  5
7338 
7339 
7340 
7341 /*
7342 ** Undo the hack that converts floating point types to integer for
7343 ** builds on processors without floating point support.
7344 */
7345 #ifdef SQLITE_OMIT_FLOATING_POINT
7346 # undef double
7347 #endif
7348 
7349 #if 0
7350 }  /* End of the 'extern "C"' block */
7351 #endif
7352 #endif /* _SQLITE3_H_ */
7353 
7354 /*
7355 ** 2010 August 30
7356 **
7357 ** The author disclaims copyright to this source code.  In place of
7358 ** a legal notice, here is a blessing:
7359 **
7360 **    May you do good and not evil.
7361 **    May you find forgiveness for yourself and forgive others.
7362 **    May you share freely, never taking more than you give.
7363 **
7364 *************************************************************************
7365 */
7366 
7367 #ifndef _SQLITE3RTREE_H_
7368 #define _SQLITE3RTREE_H_
7369 
7370 
7371 #if 0
7372 extern "C" {
7373 #endif
7374 
7375 typedef struct sqlite3_rtree_geometry sqlite3_rtree_geometry;
7376 
7377 /*
7378 ** Register a geometry callback named zGeom that can be used as part of an
7379 ** R-Tree geometry query as follows:
7380 **
7381 **   SELECT ... FROM <rtree> WHERE <rtree col> MATCH $zGeom(... params ...)
7382 */
7383 SQLITE_API int sqlite3_rtree_geometry_callback(
7384   sqlite3 *db,
7385   const char *zGeom,
7386 #ifdef SQLITE_RTREE_INT_ONLY
7387   int (*xGeom)(sqlite3_rtree_geometry*, int n, sqlite3_int64 *a, int *pRes),
7388 #else
7389   int (*xGeom)(sqlite3_rtree_geometry*, int n, double *a, int *pRes),
7390 #endif
7391   void *pContext
7392 );
7393 
7394 
7395 /*
7396 ** A pointer to a structure of the following type is passed as the first
7397 ** argument to callbacks registered using rtree_geometry_callback().
7398 */
7399 struct sqlite3_rtree_geometry {
7400   void *pContext;                 /* Copy of pContext passed to s_r_g_c() */
7401   int nParam;                     /* Size of array aParam[] */
7402   double *aParam;                 /* Parameters passed to SQL geom function */
7403   void *pUser;                    /* Callback implementation user data */
7404   void (*xDelUser)(void *);       /* Called by SQLite to clean up pUser */
7405 };
7406 
7407 
7408 #if 0
7409 }  /* end of the 'extern "C"' block */
7410 #endif
7411 
7412 #endif  /* ifndef _SQLITE3RTREE_H_ */
7413 
7414 
7415 /************** End of sqlite3.h *********************************************/
7416 /************** Begin file sqliteInt.h ***************************************/
7417 /*
7418 ** 2001 September 15
7419 **
7420 ** The author disclaims copyright to this source code.  In place of
7421 ** a legal notice, here is a blessing:
7422 **
7423 **    May you do good and not evil.
7424 **    May you find forgiveness for yourself and forgive others.
7425 **    May you share freely, never taking more than you give.
7426 **
7427 *************************************************************************
7428 ** Internal interface definitions for SQLite.
7429 **
7430 */
7431 #ifndef _SQLITEINT_H_
7432 #define _SQLITEINT_H_
7433 
7434 /*
7435 ** These #defines should enable >2GB file support on POSIX if the
7436 ** underlying operating system supports it.  If the OS lacks
7437 ** large file support, or if the OS is windows, these should be no-ops.
7438 **
7439 ** Ticket #2739:  The _LARGEFILE_SOURCE macro must appear before any
7440 ** system #includes.  Hence, this block of code must be the very first
7441 ** code in all source files.
7442 **
7443 ** Large file support can be disabled using the -DSQLITE_DISABLE_LFS switch
7444 ** on the compiler command line.  This is necessary if you are compiling
7445 ** on a recent machine (ex: Red Hat 7.2) but you want your code to work
7446 ** on an older machine (ex: Red Hat 6.0).  If you compile on Red Hat 7.2
7447 ** without this option, LFS is enable.  But LFS does not exist in the kernel
7448 ** in Red Hat 6.0, so the code won't work.  Hence, for maximum binary
7449 ** portability you should omit LFS.
7450 **
7451 ** Similar is true for Mac OS X.  LFS is only supported on Mac OS X 9 and later.
7452 */
7453 #ifndef SQLITE_DISABLE_LFS
7454 # define _LARGE_FILE       1
7455 # ifndef _FILE_OFFSET_BITS
7456 #   define _FILE_OFFSET_BITS 64
7457 # endif
7458 # define _LARGEFILE_SOURCE 1
7459 #endif
7460 
7461 /*
7462 ** Include the configuration header output by 'configure' if we're using the
7463 ** autoconf-based build
7464 */
7465 #ifdef _HAVE_SQLITE_CONFIG_H
7466 #include "config.h"
7467 #endif
7468 
7469 /************** Include sqliteLimit.h in the middle of sqliteInt.h ***********/
7470 /************** Begin file sqliteLimit.h *************************************/
7471 /*
7472 ** 2007 May 7
7473 **
7474 ** The author disclaims copyright to this source code.  In place of
7475 ** a legal notice, here is a blessing:
7476 **
7477 **    May you do good and not evil.
7478 **    May you find forgiveness for yourself and forgive others.
7479 **    May you share freely, never taking more than you give.
7480 **
7481 *************************************************************************
7482 **
7483 ** This file defines various limits of what SQLite can process.
7484 */
7485 
7486 /*
7487 ** The maximum length of a TEXT or BLOB in bytes.   This also
7488 ** limits the size of a row in a table or index.
7489 **
7490 ** The hard limit is the ability of a 32-bit signed integer
7491 ** to count the size: 2^31-1 or 2147483647.
7492 */
7493 #ifndef SQLITE_MAX_LENGTH
7494 # define SQLITE_MAX_LENGTH 1000000000
7495 #endif
7496 
7497 /*
7498 ** This is the maximum number of
7499 **
7500 **    * Columns in a table
7501 **    * Columns in an index
7502 **    * Columns in a view
7503 **    * Terms in the SET clause of an UPDATE statement
7504 **    * Terms in the result set of a SELECT statement
7505 **    * Terms in the GROUP BY or ORDER BY clauses of a SELECT statement.
7506 **    * Terms in the VALUES clause of an INSERT statement
7507 **
7508 ** The hard upper limit here is 32676.  Most database people will
7509 ** tell you that in a well-normalized database, you usually should
7510 ** not have more than a dozen or so columns in any table.  And if
7511 ** that is the case, there is no point in having more than a few
7512 ** dozen values in any of the other situations described above.
7513 */
7514 #ifndef SQLITE_MAX_COLUMN
7515 # define SQLITE_MAX_COLUMN 2000
7516 #endif
7517 
7518 /*
7519 ** The maximum length of a single SQL statement in bytes.
7520 **
7521 ** It used to be the case that setting this value to zero would
7522 ** turn the limit off.  That is no longer true.  It is not possible
7523 ** to turn this limit off.
7524 */
7525 #ifdef SQLITE_OMIT_FLOATING_POINT
7526 # define double sqlite_int64
7527 # define float sqlite_int64
7528 # define LONGDOUBLE_TYPE sqlite_int64
7529 # ifndef SQLITE_BIG_DBL
7530 #   define SQLITE_BIG_DBL (((sqlite3_int64)1)<<50)
7531 # endif
7532 # define SQLITE_OMIT_DATETIME_FUNCS 1
7533 # define SQLITE_OMIT_TRACE 1
7534 # undef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
7535 # undef SQLITE_HAVE_ISNAN
7536 #else
7537 # ifdef __vax__
7538 #  include <float.h>
7539 #  define SQLITE_BIG_DBL DBL_MAX
7540 #  define SQLITE_HUGE_DBL DBL_MAX
7541 #  define SQLITE_HUGE_COST 1e38
7542 # endif
7543 #endif
7544 #ifndef SQLITE_BIG_DBL
7545 # define SQLITE_BIG_DBL (1e99)
7546 #endif
7547 #ifndef SQLITE_HUGE_DBL
7548 # define SQLITE_HUGE_DBL (1.0e+308)
7549 #endif
7550 #ifndef SQLITE_MAX_SQL_LENGTH
7551 # define SQLITE_MAX_SQL_LENGTH 1000000000
7552 #endif
7553 #ifndef SQLITE_HUGE_COST
7554 # define SQLITE_HUGE_COST 1e50
7555 #endif
7556 
7557 /*
7558 ** The maximum depth of an expression tree. This is limited to
7559 ** some extent by SQLITE_MAX_SQL_LENGTH. But sometime you might
7560 ** want to place more severe limits on the complexity of an
7561 ** expression.
7562 **
7563 ** A value of 0 used to mean that the limit was not enforced.
7564 ** But that is no longer true.  The limit is now strictly enforced
7565 ** at all times.
7566 */
7567 #ifndef SQLITE_MAX_EXPR_DEPTH
7568 # define SQLITE_MAX_EXPR_DEPTH 1000
7569 #endif
7570 
7571 /*
7572 ** The maximum number of terms in a compound SELECT statement.
7573 ** The code generator for compound SELECT statements does one
7574 ** level of recursion for each term.  A stack overflow can result
7575 ** if the number of terms is too large.  In practice, most SQL
7576 ** never has more than 3 or 4 terms.  Use a value of 0 to disable
7577 ** any limit on the number of terms in a compount SELECT.
7578 */
7579 #ifndef SQLITE_MAX_COMPOUND_SELECT
7580 # define SQLITE_MAX_COMPOUND_SELECT 500
7581 #endif
7582 
7583 /*
7584 ** The maximum number of opcodes in a VDBE program.
7585 ** Not currently enforced.
7586 */
7587 #ifndef SQLITE_MAX_VDBE_OP
7588 # define SQLITE_MAX_VDBE_OP 25000
7589 #endif
7590 
7591 /*
7592 ** The maximum number of arguments to an SQL function.
7593 */
7594 #ifndef SQLITE_MAX_FUNCTION_ARG
7595 # define SQLITE_MAX_FUNCTION_ARG 127
7596 #endif
7597 
7598 /*
7599 ** The maximum number of in-memory pages to use for the main database
7600 ** table and for temporary tables.  The SQLITE_DEFAULT_CACHE_SIZE
7601 */
7602 #ifndef SQLITE_DEFAULT_CACHE_SIZE
7603 # define SQLITE_DEFAULT_CACHE_SIZE  2000
7604 #endif
7605 #ifndef SQLITE_DEFAULT_TEMP_CACHE_SIZE
7606 # define SQLITE_DEFAULT_TEMP_CACHE_SIZE  500
7607 #endif
7608 
7609 /*
7610 ** The default number of frames to accumulate in the log file before
7611 ** checkpointing the database in WAL mode.
7612 */
7613 #ifndef SQLITE_DEFAULT_WAL_AUTOCHECKPOINT
7614 # define SQLITE_DEFAULT_WAL_AUTOCHECKPOINT  1000
7615 #endif
7616 
7617 /*
7618 ** The maximum number of attached databases.  This must be between 0
7619 ** and 62.  The upper bound on 62 is because a 64-bit integer bitmap
7620 ** is used internally to track attached databases.
7621 */
7622 #ifndef SQLITE_MAX_ATTACHED
7623 # define SQLITE_MAX_ATTACHED 10
7624 #endif
7625 
7626 
7627 /*
7628 ** The maximum value of a ?nnn wildcard that the parser will accept.
7629 */
7630 #ifndef SQLITE_MAX_VARIABLE_NUMBER
7631 # define SQLITE_MAX_VARIABLE_NUMBER 999
7632 #endif
7633 
7634 /* Maximum page size.  The upper bound on this value is 65536.  This a limit
7635 ** imposed by the use of 16-bit offsets within each page.
7636 **
7637 ** Earlier versions of SQLite allowed the user to change this value at
7638 ** compile time. This is no longer permitted, on the grounds that it creates
7639 ** a library that is technically incompatible with an SQLite library
7640 ** compiled with a different limit. If a process operating on a database
7641 ** with a page-size of 65536 bytes crashes, then an instance of SQLite
7642 ** compiled with the default page-size limit will not be able to rollback
7643 ** the aborted transaction. This could lead to database corruption.
7644 */
7645 #ifdef SQLITE_MAX_PAGE_SIZE
7646 # undef SQLITE_MAX_PAGE_SIZE
7647 #endif
7648 #define SQLITE_MAX_PAGE_SIZE 65536
7649 
7650 
7651 /*
7652 ** The default size of a database page.
7653 */
7654 #ifndef SQLITE_DEFAULT_PAGE_SIZE
7655 # define SQLITE_DEFAULT_PAGE_SIZE 1024
7656 #endif
7657 #if SQLITE_DEFAULT_PAGE_SIZE>SQLITE_MAX_PAGE_SIZE
7658 # undef SQLITE_DEFAULT_PAGE_SIZE
7659 # define SQLITE_DEFAULT_PAGE_SIZE SQLITE_MAX_PAGE_SIZE
7660 #endif
7661 
7662 /*
7663 ** Ordinarily, if no value is explicitly provided, SQLite creates databases
7664 ** with page size SQLITE_DEFAULT_PAGE_SIZE. However, based on certain
7665 ** device characteristics (sector-size and atomic write() support),
7666 ** SQLite may choose a larger value. This constant is the maximum value
7667 ** SQLite will choose on its own.
7668 */
7669 #ifndef SQLITE_MAX_DEFAULT_PAGE_SIZE
7670 # define SQLITE_MAX_DEFAULT_PAGE_SIZE 8192
7671 #endif
7672 #if SQLITE_MAX_DEFAULT_PAGE_SIZE>SQLITE_MAX_PAGE_SIZE
7673 # undef SQLITE_MAX_DEFAULT_PAGE_SIZE
7674 # define SQLITE_MAX_DEFAULT_PAGE_SIZE SQLITE_MAX_PAGE_SIZE
7675 #endif
7676 
7677 
7678 /*
7679 ** Maximum number of pages in one database file.
7680 **
7681 ** This is really just the default value for the max_page_count pragma.
7682 ** This value can be lowered (or raised) at run-time using that the
7683 ** max_page_count macro.
7684 */
7685 #ifndef SQLITE_MAX_PAGE_COUNT
7686 # define SQLITE_MAX_PAGE_COUNT 1073741823
7687 #endif
7688 
7689 /*
7690 ** Maximum length (in bytes) of the pattern in a LIKE or GLOB
7691 ** operator.
7692 */
7693 #ifndef SQLITE_MAX_LIKE_PATTERN_LENGTH
7694 # define SQLITE_MAX_LIKE_PATTERN_LENGTH 50000
7695 #endif
7696 
7697 /*
7698 ** Maximum depth of recursion for triggers.
7699 **
7700 ** A value of 1 means that a trigger program will not be able to itself
7701 ** fire any triggers. A value of 0 means that no trigger programs at all
7702 ** may be executed.
7703 */
7704 #ifndef SQLITE_MAX_TRIGGER_DEPTH
7705 # define SQLITE_MAX_TRIGGER_DEPTH 1000
7706 #endif
7707 
7708 /************** End of sqliteLimit.h *****************************************/
7709 /************** Continuing where we left off in sqliteInt.h ******************/
7710 
7711 /* Disable nuisance warnings on Borland compilers */
7712 #if defined(__BORLANDC__)
7713 #pragma warn -rch /* unreachable code */
7714 #pragma warn -ccc /* Condition is always true or false */
7715 #pragma warn -aus /* Assigned value is never used */
7716 #pragma warn -csu /* Comparing signed and unsigned */
7717 #pragma warn -spa /* Suspicious pointer arithmetic */
7718 #endif
7719 
7720 /* Needed for various definitions... */
7721 #ifndef _GNU_SOURCE
7722 # define _GNU_SOURCE
7723 #endif
7724 
7725 #if defined(__OpenBSD__) && !defined(_BSD_SOURCE)
7726 # define _BSD_SOURCE
7727 #endif
7728 
7729 /*
7730 ** Include standard header files as necessary
7731 */
7732 #ifdef HAVE_STDINT_H
7733 #include <stdint.h>
7734 #endif
7735 #ifdef HAVE_INTTYPES_H
7736 #include <inttypes.h>
7737 #endif
7738 
7739 /*
7740 ** The following macros are used to cast pointers to integers and
7741 ** integers to pointers.  The way you do this varies from one compiler
7742 ** to the next, so we have developed the following set of #if statements
7743 ** to generate appropriate macros for a wide range of compilers.
7744 **
7745 ** The correct "ANSI" way to do this is to use the intptr_t type.
7746 ** Unfortunately, that typedef is not available on all compilers, or
7747 ** if it is available, it requires an #include of specific headers
7748 ** that vary from one machine to the next.
7749 **
7750 ** Ticket #3860:  The llvm-gcc-4.2 compiler from Apple chokes on
7751 ** the ((void*)&((char*)0)[X]) construct.  But MSVC chokes on ((void*)(X)).
7752 ** So we have to define the macros in different ways depending on the
7753 ** compiler.
7754 */
7755 #if defined(__PTRDIFF_TYPE__)  /* This case should work for GCC */
7756 # define SQLITE_INT_TO_PTR(X)  ((void*)(__PTRDIFF_TYPE__)(X))
7757 # define SQLITE_PTR_TO_INT(X)  ((int)(__PTRDIFF_TYPE__)(X))
7758 #elif !defined(__GNUC__)       /* Works for compilers other than LLVM */
7759 # define SQLITE_INT_TO_PTR(X)  ((void*)&((char*)0)[X])
7760 # define SQLITE_PTR_TO_INT(X)  ((int)(((char*)X)-(char*)0))
7761 #elif defined(HAVE_STDINT_H)   /* Use this case if we have ANSI headers */
7762 # define SQLITE_INT_TO_PTR(X)  ((void*)(intptr_t)(X))
7763 # define SQLITE_PTR_TO_INT(X)  ((int)(intptr_t)(X))
7764 #else                          /* Generates a warning - but it always works */
7765 # define SQLITE_INT_TO_PTR(X)  ((void*)(X))
7766 # define SQLITE_PTR_TO_INT(X)  ((int)(X))
7767 #endif
7768 
7769 /*
7770 ** The SQLITE_THREADSAFE macro must be defined as 0, 1, or 2.
7771 ** 0 means mutexes are permanently disable and the library is never
7772 ** threadsafe.  1 means the library is serialized which is the highest
7773 ** level of threadsafety.  2 means the library is multithreaded - multiple
7774 ** threads can use SQLite as long as no two threads try to use the same
7775 ** database connection at the same time.
7776 **
7777 ** Older versions of SQLite used an optional THREADSAFE macro.
7778 ** We support that for legacy.
7779 */
7780 #if !defined(SQLITE_THREADSAFE)
7781 # if defined(THREADSAFE)
7782 #   define SQLITE_THREADSAFE THREADSAFE
7783 # else
7784 #   define SQLITE_THREADSAFE 1 /* IMP: R-07272-22309 */
7785 # endif
7786 #endif
7787 
7788 /*
7789 ** Powersafe overwrite is on by default.  But can be turned off using
7790 ** the -DSQLITE_POWERSAFE_OVERWRITE=0 command-line option.
7791 */
7792 #ifndef SQLITE_POWERSAFE_OVERWRITE
7793 # define SQLITE_POWERSAFE_OVERWRITE 1
7794 #endif
7795 
7796 /*
7797 ** The SQLITE_DEFAULT_MEMSTATUS macro must be defined as either 0 or 1.
7798 ** It determines whether or not the features related to
7799 ** SQLITE_CONFIG_MEMSTATUS are available by default or not. This value can
7800 ** be overridden at runtime using the sqlite3_config() API.
7801 */
7802 #if !defined(SQLITE_DEFAULT_MEMSTATUS)
7803 # define SQLITE_DEFAULT_MEMSTATUS 1
7804 #endif
7805 
7806 /*
7807 ** Exactly one of the following macros must be defined in order to
7808 ** specify which memory allocation subsystem to use.
7809 **
7810 **     SQLITE_SYSTEM_MALLOC          // Use normal system malloc()
7811 **     SQLITE_WIN32_MALLOC           // Use Win32 native heap API
7812 **     SQLITE_ZERO_MALLOC            // Use a stub allocator that always fails
7813 **     SQLITE_MEMDEBUG               // Debugging version of system malloc()
7814 **
7815 ** On Windows, if the SQLITE_WIN32_MALLOC_VALIDATE macro is defined and the
7816 ** assert() macro is enabled, each call into the Win32 native heap subsystem
7817 ** will cause HeapValidate to be called.  If heap validation should fail, an
7818 ** assertion will be triggered.
7819 **
7820 ** If none of the above are defined, then set SQLITE_SYSTEM_MALLOC as
7821 ** the default.
7822 */
7823 #if defined(SQLITE_SYSTEM_MALLOC) \
7824   + defined(SQLITE_WIN32_MALLOC) \
7825   + defined(SQLITE_ZERO_MALLOC) \
7826   + defined(SQLITE_MEMDEBUG)>1
7827 # error "Two or more of the following compile-time configuration options\
7828  are defined but at most one is allowed:\
7829  SQLITE_SYSTEM_MALLOC, SQLITE_WIN32_MALLOC, SQLITE_MEMDEBUG,\
7830  SQLITE_ZERO_MALLOC"
7831 #endif
7832 #if defined(SQLITE_SYSTEM_MALLOC) \
7833   + defined(SQLITE_WIN32_MALLOC) \
7834   + defined(SQLITE_ZERO_MALLOC) \
7835   + defined(SQLITE_MEMDEBUG)==0
7836 # define SQLITE_SYSTEM_MALLOC 1
7837 #endif
7838 
7839 /*
7840 ** If SQLITE_MALLOC_SOFT_LIMIT is not zero, then try to keep the
7841 ** sizes of memory allocations below this value where possible.
7842 */
7843 #if !defined(SQLITE_MALLOC_SOFT_LIMIT)
7844 # define SQLITE_MALLOC_SOFT_LIMIT 1024
7845 #endif
7846 
7847 /*
7848 ** We need to define _XOPEN_SOURCE as follows in order to enable
7849 ** recursive mutexes on most Unix systems and fchmod() on OpenBSD.
7850 ** But _XOPEN_SOURCE define causes problems for Mac OS X, so omit
7851 ** it.
7852 */
7853 #if !defined(_XOPEN_SOURCE) && !defined(__DARWIN__) && !defined(__APPLE__)
7854 #  define _XOPEN_SOURCE 600
7855 #endif
7856 
7857 /*
7858 ** NDEBUG and SQLITE_DEBUG are opposites.  It should always be true that
7859 ** defined(NDEBUG)==!defined(SQLITE_DEBUG).  If this is not currently true,
7860 ** make it true by defining or undefining NDEBUG.
7861 **
7862 ** Setting NDEBUG makes the code smaller and faster by disabling the
7863 ** assert() statements in the code.  So we want the default action
7864 ** to be for NDEBUG to be set and NDEBUG to be undefined only if SQLITE_DEBUG
7865 ** is set.  Thus NDEBUG becomes an opt-in rather than an opt-out
7866 ** feature.
7867 */
7868 #if !defined(NDEBUG) && !defined(SQLITE_DEBUG)
7869 # define NDEBUG 1
7870 #endif
7871 #if defined(NDEBUG) && defined(SQLITE_DEBUG)
7872 # undef NDEBUG
7873 #endif
7874 
7875 /*
7876 ** Enable SQLITE_ENABLE_EXPLAIN_COMMENTS if SQLITE_DEBUG is turned on.
7877 */
7878 #if !defined(SQLITE_ENABLE_EXPLAIN_COMMENTS) && defined(SQLITE_DEBUG)
7879 # define SQLITE_ENABLE_EXPLAIN_COMMENTS 1
7880 #endif
7881 
7882 /*
7883 ** The testcase() macro is used to aid in coverage testing.  When
7884 ** doing coverage testing, the condition inside the argument to
7885 ** testcase() must be evaluated both true and false in order to
7886 ** get full branch coverage.  The testcase() macro is inserted
7887 ** to help ensure adequate test coverage in places where simple
7888 ** condition/decision coverage is inadequate.  For example, testcase()
7889 ** can be used to make sure boundary values are tested.  For
7890 ** bitmask tests, testcase() can be used to make sure each bit
7891 ** is significant and used at least once.  On switch statements
7892 ** where multiple cases go to the same block of code, testcase()
7893 ** can insure that all cases are evaluated.
7894 **
7895 */
7896 #ifdef SQLITE_COVERAGE_TEST
7897 SQLITE_PRIVATE   void sqlite3Coverage(int);
7898 # define testcase(X)  if( X ){ sqlite3Coverage(__LINE__); }
7899 #else
7900 # define testcase(X)
7901 #endif
7902 
7903 /*
7904 ** The TESTONLY macro is used to enclose variable declarations or
7905 ** other bits of code that are needed to support the arguments
7906 ** within testcase() and assert() macros.
7907 */
7908 #if !defined(NDEBUG) || defined(SQLITE_COVERAGE_TEST)
7909 # define TESTONLY(X)  X
7910 #else
7911 # define TESTONLY(X)
7912 #endif
7913 
7914 /*
7915 ** Sometimes we need a small amount of code such as a variable initialization
7916 ** to setup for a later assert() statement.  We do not want this code to
7917 ** appear when assert() is disabled.  The following macro is therefore
7918 ** used to contain that setup code.  The "VVA" acronym stands for
7919 ** "Verification, Validation, and Accreditation".  In other words, the
7920 ** code within VVA_ONLY() will only run during verification processes.
7921 */
7922 #ifndef NDEBUG
7923 # define VVA_ONLY(X)  X
7924 #else
7925 # define VVA_ONLY(X)
7926 #endif
7927 
7928 /*
7929 ** The ALWAYS and NEVER macros surround boolean expressions which
7930 ** are intended to always be true or false, respectively.  Such
7931 ** expressions could be omitted from the code completely.  But they
7932 ** are included in a few cases in order to enhance the resilience
7933 ** of SQLite to unexpected behavior - to make the code "self-healing"
7934 ** or "ductile" rather than being "brittle" and crashing at the first
7935 ** hint of unplanned behavior.
7936 **
7937 ** In other words, ALWAYS and NEVER are added for defensive code.
7938 **
7939 ** When doing coverage testing ALWAYS and NEVER are hard-coded to
7940 ** be true and false so that the unreachable code they specify will
7941 ** not be counted as untested code.
7942 */
7943 #if defined(SQLITE_COVERAGE_TEST)
7944 # define ALWAYS(X)      (1)
7945 # define NEVER(X)       (0)
7946 #elif !defined(NDEBUG)
7947 # define ALWAYS(X)      ((X)?1:(assert(0),0))
7948 # define NEVER(X)       ((X)?(assert(0),1):0)
7949 #else
7950 # define ALWAYS(X)      (X)
7951 # define NEVER(X)       (X)
7952 #endif
7953 
7954 /*
7955 ** Return true (non-zero) if the input is a integer that is too large
7956 ** to fit in 32-bits.  This macro is used inside of various testcase()
7957 ** macros to verify that we have tested SQLite for large-file support.
7958 */
7959 #define IS_BIG_INT(X)  (((X)&~(i64)0xffffffff)!=0)
7960 
7961 /*
7962 ** The macro unlikely() is a hint that surrounds a boolean
7963 ** expression that is usually false.  Macro likely() surrounds
7964 ** a boolean expression that is usually true.  These hints could,
7965 ** in theory, be used by the compiler to generate better code, but
7966 ** currently they are just comments for human readers.
7967 */
7968 #define likely(X)    (X)
7969 #define unlikely(X)  (X)
7970 
7971 /************** Include hash.h in the middle of sqliteInt.h ******************/
7972 /************** Begin file hash.h ********************************************/
7973 /*
7974 ** 2001 September 22
7975 **
7976 ** The author disclaims copyright to this source code.  In place of
7977 ** a legal notice, here is a blessing:
7978 **
7979 **    May you do good and not evil.
7980 **    May you find forgiveness for yourself and forgive others.
7981 **    May you share freely, never taking more than you give.
7982 **
7983 *************************************************************************
7984 ** This is the header file for the generic hash-table implementation
7985 ** used in SQLite.
7986 */
7987 #ifndef _SQLITE_HASH_H_
7988 #define _SQLITE_HASH_H_
7989 
7990 /* Forward declarations of structures. */
7991 typedef struct Hash Hash;
7992 typedef struct HashElem HashElem;
7993 
7994 /* A complete hash table is an instance of the following structure.
7995 ** The internals of this structure are intended to be opaque -- client
7996 ** code should not attempt to access or modify the fields of this structure
7997 ** directly.  Change this structure only by using the routines below.
7998 ** However, some of the "procedures" and "functions" for modifying and
7999 ** accessing this structure are really macros, so we can't really make
8000 ** this structure opaque.
8001 **
8002 ** All elements of the hash table are on a single doubly-linked list.
8003 ** Hash.first points to the head of this list.
8004 **
8005 ** There are Hash.htsize buckets.  Each bucket points to a spot in
8006 ** the global doubly-linked list.  The contents of the bucket are the
8007 ** element pointed to plus the next _ht.count-1 elements in the list.
8008 **
8009 ** Hash.htsize and Hash.ht may be zero.  In that case lookup is done
8010 ** by a linear search of the global list.  For small tables, the
8011 ** Hash.ht table is never allocated because if there are few elements
8012 ** in the table, it is faster to do a linear search than to manage
8013 ** the hash table.
8014 */
8015 struct Hash {
8016   unsigned int htsize;      /* Number of buckets in the hash table */
8017   unsigned int count;       /* Number of entries in this table */
8018   HashElem *first;          /* The first element of the array */
8019   struct _ht {              /* the hash table */
8020     int count;                 /* Number of entries with this hash */
8021     HashElem *chain;           /* Pointer to first entry with this hash */
8022   } *ht;
8023 };
8024 
8025 /* Each element in the hash table is an instance of the following
8026 ** structure.  All elements are stored on a single doubly-linked list.
8027 **
8028 ** Again, this structure is intended to be opaque, but it can't really
8029 ** be opaque because it is used by macros.
8030 */
8031 struct HashElem {
8032   HashElem *next, *prev;       /* Next and previous elements in the table */
8033   void *data;                  /* Data associated with this element */
8034   const char *pKey; int nKey;  /* Key associated with this element */
8035 };
8036 
8037 /*
8038 ** Access routines.  To delete, insert a NULL pointer.
8039 */
8040 SQLITE_PRIVATE void sqlite3HashInit(Hash*);
8041 SQLITE_PRIVATE void *sqlite3HashInsert(Hash*, const char *pKey, int nKey, void *pData);
8042 SQLITE_PRIVATE void *sqlite3HashFind(const Hash*, const char *pKey, int nKey);
8043 SQLITE_PRIVATE void sqlite3HashClear(Hash*);
8044 
8045 /*
8046 ** Macros for looping over all elements of a hash table.  The idiom is
8047 ** like this:
8048 **
8049 **   Hash h;
8050 **   HashElem *p;
8051 **   ...
8052 **   for(p=sqliteHashFirst(&h); p; p=sqliteHashNext(p)){
8053 **     SomeStructure *pData = sqliteHashData(p);
8054 **     // do something with pData
8055 **   }
8056 */
8057 #define sqliteHashFirst(H)  ((H)->first)
8058 #define sqliteHashNext(E)   ((E)->next)
8059 #define sqliteHashData(E)   ((E)->data)
8060 /* #define sqliteHashKey(E)    ((E)->pKey) // NOT USED */
8061 /* #define sqliteHashKeysize(E) ((E)->nKey)  // NOT USED */
8062 
8063 /*
8064 ** Number of entries in a hash table
8065 */
8066 /* #define sqliteHashCount(H)  ((H)->count) // NOT USED */
8067 
8068 #endif /* _SQLITE_HASH_H_ */
8069 
8070 /************** End of hash.h ************************************************/
8071 /************** Continuing where we left off in sqliteInt.h ******************/
8072 /************** Include parse.h in the middle of sqliteInt.h *****************/
8073 /************** Begin file parse.h *******************************************/
8074 #define TK_SEMI                             1
8075 #define TK_EXPLAIN                          2
8076 #define TK_QUERY                            3
8077 #define TK_PLAN                             4
8078 #define TK_BEGIN                            5
8079 #define TK_TRANSACTION                      6
8080 #define TK_DEFERRED                         7
8081 #define TK_IMMEDIATE                        8
8082 #define TK_EXCLUSIVE                        9
8083 #define TK_COMMIT                          10
8084 #define TK_END                             11
8085 #define TK_ROLLBACK                        12
8086 #define TK_SAVEPOINT                       13
8087 #define TK_RELEASE                         14
8088 #define TK_TO                              15
8089 #define TK_TABLE                           16
8090 #define TK_CREATE                          17
8091 #define TK_IF                              18
8092 #define TK_NOT                             19
8093 #define TK_EXISTS                          20
8094 #define TK_TEMP                            21
8095 #define TK_LP                              22
8096 #define TK_RP                              23
8097 #define TK_AS                              24
8098 #define TK_WITHOUT                         25
8099 #define TK_COMMA                           26
8100 #define TK_ID                              27
8101 #define TK_INDEXED                         28
8102 #define TK_ABORT                           29
8103 #define TK_ACTION                          30
8104 #define TK_AFTER                           31
8105 #define TK_ANALYZE                         32
8106 #define TK_ASC                             33
8107 #define TK_ATTACH                          34
8108 #define TK_BEFORE                          35
8109 #define TK_BY                              36
8110 #define TK_CASCADE                         37
8111 #define TK_CAST                            38
8112 #define TK_COLUMNKW                        39
8113 #define TK_CONFLICT                        40
8114 #define TK_DATABASE                        41
8115 #define TK_DESC                            42
8116 #define TK_DETACH                          43
8117 #define TK_EACH                            44
8118 #define TK_FAIL                            45
8119 #define TK_FOR                             46
8120 #define TK_IGNORE                          47
8121 #define TK_INITIALLY                       48
8122 #define TK_INSTEAD                         49
8123 #define TK_LIKE_KW                         50
8124 #define TK_MATCH                           51
8125 #define TK_NO                              52
8126 #define TK_KEY                             53
8127 #define TK_OF                              54
8128 #define TK_OFFSET                          55
8129 #define TK_PRAGMA                          56
8130 #define TK_RAISE                           57
8131 #define TK_RECURSIVE                       58
8132 #define TK_REPLACE                         59
8133 #define TK_RESTRICT                        60
8134 #define TK_ROW                             61
8135 #define TK_TRIGGER                         62
8136 #define TK_VACUUM                          63
8137 #define TK_VIEW                            64
8138 #define TK_VIRTUAL                         65
8139 #define TK_WITH                            66
8140 #define TK_REINDEX                         67
8141 #define TK_RENAME                          68
8142 #define TK_CTIME_KW                        69
8143 #define TK_ANY                             70
8144 #define TK_OR                              71
8145 #define TK_AND                             72
8146 #define TK_IS                              73
8147 #define TK_BETWEEN                         74
8148 #define TK_IN                              75
8149 #define TK_ISNULL                          76
8150 #define TK_NOTNULL                         77
8151 #define TK_NE                              78
8152 #define TK_EQ                              79
8153 #define TK_GT                              80
8154 #define TK_LE                              81
8155 #define TK_LT                              82
8156 #define TK_GE                              83
8157 #define TK_ESCAPE                          84
8158 #define TK_BITAND                          85
8159 #define TK_BITOR                           86
8160 #define TK_LSHIFT                          87
8161 #define TK_RSHIFT                          88
8162 #define TK_PLUS                            89
8163 #define TK_MINUS                           90
8164 #define TK_STAR                            91
8165 #define TK_SLASH                           92
8166 #define TK_REM                             93
8167 #define TK_CONCAT                          94
8168 #define TK_COLLATE                         95
8169 #define TK_BITNOT                          96
8170 #define TK_STRING                          97
8171 #define TK_JOIN_KW                         98
8172 #define TK_CONSTRAINT                      99
8173 #define TK_DEFAULT                        100
8174 #define TK_NULL                           101
8175 #define TK_PRIMARY                        102
8176 #define TK_UNIQUE                         103
8177 #define TK_CHECK                          104
8178 #define TK_REFERENCES                     105
8179 #define TK_AUTOINCR                       106
8180 #define TK_ON                             107
8181 #define TK_INSERT                         108
8182 #define TK_DELETE                         109
8183 #define TK_UPDATE                         110
8184 #define TK_SET                            111
8185 #define TK_DEFERRABLE                     112
8186 #define TK_FOREIGN                        113
8187 #define TK_DROP                           114
8188 #define TK_UNION                          115
8189 #define TK_ALL                            116
8190 #define TK_EXCEPT                         117
8191 #define TK_INTERSECT                      118
8192 #define TK_SELECT                         119
8193 #define TK_VALUES                         120
8194 #define TK_DISTINCT                       121
8195 #define TK_DOT                            122
8196 #define TK_FROM                           123
8197 #define TK_JOIN                           124
8198 #define TK_USING                          125
8199 #define TK_ORDER                          126
8200 #define TK_GROUP                          127
8201 #define TK_HAVING                         128
8202 #define TK_LIMIT                          129
8203 #define TK_WHERE                          130
8204 #define TK_INTO                           131
8205 #define TK_INTEGER                        132
8206 #define TK_FLOAT                          133
8207 #define TK_BLOB                           134
8208 #define TK_VARIABLE                       135
8209 #define TK_CASE                           136
8210 #define TK_WHEN                           137
8211 #define TK_THEN                           138
8212 #define TK_ELSE                           139
8213 #define TK_INDEX                          140
8214 #define TK_ALTER                          141
8215 #define TK_ADD                            142
8216 #define TK_TO_TEXT                        143
8217 #define TK_TO_BLOB                        144
8218 #define TK_TO_NUMERIC                     145
8219 #define TK_TO_INT                         146
8220 #define TK_TO_REAL                        147
8221 #define TK_ISNOT                          148
8222 #define TK_END_OF_FILE                    149
8223 #define TK_ILLEGAL                        150
8224 #define TK_SPACE                          151
8225 #define TK_UNCLOSED_STRING                152
8226 #define TK_FUNCTION                       153
8227 #define TK_COLUMN                         154
8228 #define TK_AGG_FUNCTION                   155
8229 #define TK_AGG_COLUMN                     156
8230 #define TK_UMINUS                         157
8231 #define TK_UPLUS                          158
8232 #define TK_REGISTER                       159
8233 
8234 /************** End of parse.h ***********************************************/
8235 /************** Continuing where we left off in sqliteInt.h ******************/
8236 #include <stdio.h>
8237 #include <stdlib.h>
8238 #include <string.h>
8239 #include <assert.h>
8240 #include <stddef.h>
8241 
8242 /*
8243 ** If compiling for a processor that lacks floating point support,
8244 ** substitute integer for floating-point
8245 */
8246 #ifdef SQLITE_OMIT_FLOATING_POINT
8247 # define double sqlite_int64
8248 # define float sqlite_int64
8249 # define LONGDOUBLE_TYPE sqlite_int64
8250 # ifndef SQLITE_BIG_DBL
8251 #   define SQLITE_BIG_DBL (((sqlite3_int64)1)<<50)
8252 # endif
8253 # define SQLITE_OMIT_DATETIME_FUNCS 1
8254 # define SQLITE_OMIT_TRACE 1
8255 # undef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
8256 # undef SQLITE_HAVE_ISNAN
8257 #endif
8258 #ifndef SQLITE_BIG_DBL
8259 # define SQLITE_BIG_DBL (1e99)
8260 #endif
8261 
8262 /*
8263 ** OMIT_TEMPDB is set to 1 if SQLITE_OMIT_TEMPDB is defined, or 0
8264 ** afterward. Having this macro allows us to cause the C compiler
8265 ** to omit code used by TEMP tables without messy #ifndef statements.
8266 */
8267 #ifdef SQLITE_OMIT_TEMPDB
8268 #define OMIT_TEMPDB 1
8269 #else
8270 #define OMIT_TEMPDB 0
8271 #endif
8272 
8273 /*
8274 ** The "file format" number is an integer that is incremented whenever
8275 ** the VDBE-level file format changes.  The following macros define the
8276 ** the default file format for new databases and the maximum file format
8277 ** that the library can read.
8278 */
8279 #define SQLITE_MAX_FILE_FORMAT 4
8280 #ifndef SQLITE_DEFAULT_FILE_FORMAT
8281 # define SQLITE_DEFAULT_FILE_FORMAT 4
8282 #endif
8283 
8284 /*
8285 ** Determine whether triggers are recursive by default.  This can be
8286 ** changed at run-time using a pragma.
8287 */
8288 #ifndef SQLITE_DEFAULT_RECURSIVE_TRIGGERS
8289 # define SQLITE_DEFAULT_RECURSIVE_TRIGGERS 0
8290 #endif
8291 
8292 /*
8293 ** Provide a default value for SQLITE_TEMP_STORE in case it is not specified
8294 ** on the command-line
8295 */
8296 #ifndef SQLITE_TEMP_STORE
8297 # define SQLITE_TEMP_STORE 1
8298 # define SQLITE_TEMP_STORE_xc 1  /* Exclude from ctime.c */
8299 #endif
8300 
8301 /*
8302 ** GCC does not define the offsetof() macro so we'll have to do it
8303 ** ourselves.
8304 */
8305 #ifndef offsetof
8306 #define offsetof(STRUCTURE,FIELD) ((int)((char*)&((STRUCTURE*)0)->FIELD))
8307 #endif
8308 
8309 /*
8310 ** Macros to compute minimum and maximum of two numbers.
8311 */
8312 #define MIN(A,B) ((A)<(B)?(A):(B))
8313 #define MAX(A,B) ((A)>(B)?(A):(B))
8314 
8315 /*
8316 ** Check to see if this machine uses EBCDIC.  (Yes, believe it or
8317 ** not, there are still machines out there that use EBCDIC.)
8318 */
8319 #if 'A' == '\301'
8320 # define SQLITE_EBCDIC 1
8321 #else
8322 # define SQLITE_ASCII 1
8323 #endif
8324 
8325 /*
8326 ** Integers of known sizes.  These typedefs might change for architectures
8327 ** where the sizes very.  Preprocessor macros are available so that the
8328 ** types can be conveniently redefined at compile-type.  Like this:
8329 **
8330 **         cc '-DUINTPTR_TYPE=long long int' ...
8331 */
8332 #ifndef UINT32_TYPE
8333 # ifdef HAVE_UINT32_T
8334 #  define UINT32_TYPE uint32_t
8335 # else
8336 #  define UINT32_TYPE unsigned int
8337 # endif
8338 #endif
8339 #ifndef UINT16_TYPE
8340 # ifdef HAVE_UINT16_T
8341 #  define UINT16_TYPE uint16_t
8342 # else
8343 #  define UINT16_TYPE unsigned short int
8344 # endif
8345 #endif
8346 #ifndef INT16_TYPE
8347 # ifdef HAVE_INT16_T
8348 #  define INT16_TYPE int16_t
8349 # else
8350 #  define INT16_TYPE short int
8351 # endif
8352 #endif
8353 #ifndef UINT8_TYPE
8354 # ifdef HAVE_UINT8_T
8355 #  define UINT8_TYPE uint8_t
8356 # else
8357 #  define UINT8_TYPE unsigned char
8358 # endif
8359 #endif
8360 #ifndef INT8_TYPE
8361 # ifdef HAVE_INT8_T
8362 #  define INT8_TYPE int8_t
8363 # else
8364 #  define INT8_TYPE signed char
8365 # endif
8366 #endif
8367 #ifndef LONGDOUBLE_TYPE
8368 # define LONGDOUBLE_TYPE long double
8369 #endif
8370 typedef sqlite_int64 i64;          /* 8-byte signed integer */
8371 typedef sqlite_uint64 u64;         /* 8-byte unsigned integer */
8372 typedef UINT32_TYPE u32;           /* 4-byte unsigned integer */
8373 typedef UINT16_TYPE u16;           /* 2-byte unsigned integer */
8374 typedef INT16_TYPE i16;            /* 2-byte signed integer */
8375 typedef UINT8_TYPE u8;             /* 1-byte unsigned integer */
8376 typedef INT8_TYPE i8;              /* 1-byte signed integer */
8377 
8378 /*
8379 ** SQLITE_MAX_U32 is a u64 constant that is the maximum u64 value
8380 ** that can be stored in a u32 without loss of data.  The value
8381 ** is 0x00000000ffffffff.  But because of quirks of some compilers, we
8382 ** have to specify the value in the less intuitive manner shown:
8383 */
8384 #define SQLITE_MAX_U32  ((((u64)1)<<32)-1)
8385 
8386 /*
8387 ** The datatype used to store estimates of the number of rows in a
8388 ** table or index.  This is an unsigned integer type.  For 99.9% of
8389 ** the world, a 32-bit integer is sufficient.  But a 64-bit integer
8390 ** can be used at compile-time if desired.
8391 */
8392 #ifdef SQLITE_64BIT_STATS
8393  typedef u64 tRowcnt;    /* 64-bit only if requested at compile-time */
8394 #else
8395  typedef u32 tRowcnt;    /* 32-bit is the default */
8396 #endif
8397 
8398 /*
8399 ** Estimated quantities used for query planning are stored as 16-bit
8400 ** logarithms.  For quantity X, the value stored is 10*log2(X).  This
8401 ** gives a possible range of values of approximately 1.0e986 to 1e-986.
8402 ** But the allowed values are "grainy".  Not every value is representable.
8403 ** For example, quantities 16 and 17 are both represented by a LogEst
8404 ** of 40.  However, since LogEst quantatites are suppose to be estimates,
8405 ** not exact values, this imprecision is not a problem.
8406 **
8407 ** "LogEst" is short for "Logarithimic Estimate".
8408 **
8409 ** Examples:
8410 **      1 -> 0              20 -> 43          10000 -> 132
8411 **      2 -> 10             25 -> 46          25000 -> 146
8412 **      3 -> 16            100 -> 66        1000000 -> 199
8413 **      4 -> 20           1000 -> 99        1048576 -> 200
8414 **     10 -> 33           1024 -> 100    4294967296 -> 320
8415 **
8416 ** The LogEst can be negative to indicate fractional values.
8417 ** Examples:
8418 **
8419 **    0.5 -> -10           0.1 -> -33        0.0625 -> -40
8420 */
8421 typedef INT16_TYPE LogEst;
8422 
8423 /*
8424 ** Macros to determine whether the machine is big or little endian,
8425 ** evaluated at runtime.
8426 */
8427 #ifdef SQLITE_AMALGAMATION
8428 SQLITE_PRIVATE const int sqlite3one = 1;
8429 #else
8430 SQLITE_PRIVATE const int sqlite3one;
8431 #endif
8432 #if defined(i386) || defined(__i386__) || defined(_M_IX86)\
8433                              || defined(__x86_64) || defined(__x86_64__)
8434 # define SQLITE_BIGENDIAN    0
8435 # define SQLITE_LITTLEENDIAN 1
8436 # define SQLITE_UTF16NATIVE  SQLITE_UTF16LE
8437 #else
8438 # define SQLITE_BIGENDIAN    (*(char *)(&sqlite3one)==0)
8439 # define SQLITE_LITTLEENDIAN (*(char *)(&sqlite3one)==1)
8440 # define SQLITE_UTF16NATIVE (SQLITE_BIGENDIAN?SQLITE_UTF16BE:SQLITE_UTF16LE)
8441 #endif
8442 
8443 /*
8444 ** Constants for the largest and smallest possible 64-bit signed integers.
8445 ** These macros are designed to work correctly on both 32-bit and 64-bit
8446 ** compilers.
8447 */
8448 #define LARGEST_INT64  (0xffffffff|(((i64)0x7fffffff)<<32))
8449 #define SMALLEST_INT64 (((i64)-1) - LARGEST_INT64)
8450 
8451 /*
8452 ** Round up a number to the next larger multiple of 8.  This is used
8453 ** to force 8-byte alignment on 64-bit architectures.
8454 */
8455 #define ROUND8(x)     (((x)+7)&~7)
8456 
8457 /*
8458 ** Round down to the nearest multiple of 8
8459 */
8460 #define ROUNDDOWN8(x) ((x)&~7)
8461 
8462 /*
8463 ** Assert that the pointer X is aligned to an 8-byte boundary.  This
8464 ** macro is used only within assert() to verify that the code gets
8465 ** all alignment restrictions correct.
8466 **
8467 ** Except, if SQLITE_4_BYTE_ALIGNED_MALLOC is defined, then the
8468 ** underlying malloc() implemention might return us 4-byte aligned
8469 ** pointers.  In that case, only verify 4-byte alignment.
8470 */
8471 #ifdef SQLITE_4_BYTE_ALIGNED_MALLOC
8472 # define EIGHT_BYTE_ALIGNMENT(X)   ((((char*)(X) - (char*)0)&3)==0)
8473 #else
8474 # define EIGHT_BYTE_ALIGNMENT(X)   ((((char*)(X) - (char*)0)&7)==0)
8475 #endif
8476 
8477 /*
8478 ** Disable MMAP on platforms where it is known to not work
8479 */
8480 #if defined(__OpenBSD__) || defined(__QNXNTO__)
8481 # undef SQLITE_MAX_MMAP_SIZE
8482 # define SQLITE_MAX_MMAP_SIZE 0
8483 #endif
8484 
8485 /*
8486 ** Default maximum size of memory used by memory-mapped I/O in the VFS
8487 */
8488 #ifdef __APPLE__
8489 # include <TargetConditionals.h>
8490 # if TARGET_OS_IPHONE
8491 #   undef SQLITE_MAX_MMAP_SIZE
8492 #   define SQLITE_MAX_MMAP_SIZE 0
8493 # endif
8494 #endif
8495 #ifndef SQLITE_MAX_MMAP_SIZE
8496 # if defined(__linux__) \
8497   || defined(_WIN32) \
8498   || (defined(__APPLE__) && defined(__MACH__)) \
8499   || defined(__sun)
8500 #   define SQLITE_MAX_MMAP_SIZE 0x7fff0000  /* 2147418112 */
8501 # else
8502 #   define SQLITE_MAX_MMAP_SIZE 0
8503 # endif
8504 # define SQLITE_MAX_MMAP_SIZE_xc 1 /* exclude from ctime.c */
8505 #endif
8506 
8507 /*
8508 ** The default MMAP_SIZE is zero on all platforms.  Or, even if a larger
8509 ** default MMAP_SIZE is specified at compile-time, make sure that it does
8510 ** not exceed the maximum mmap size.
8511 */
8512 #ifndef SQLITE_DEFAULT_MMAP_SIZE
8513 # define SQLITE_DEFAULT_MMAP_SIZE 0
8514 # define SQLITE_DEFAULT_MMAP_SIZE_xc 1  /* Exclude from ctime.c */
8515 #endif
8516 #if SQLITE_DEFAULT_MMAP_SIZE>SQLITE_MAX_MMAP_SIZE
8517 # undef SQLITE_DEFAULT_MMAP_SIZE
8518 # define SQLITE_DEFAULT_MMAP_SIZE SQLITE_MAX_MMAP_SIZE
8519 #endif
8520 
8521 /*
8522 ** Only one of SQLITE_ENABLE_STAT3 or SQLITE_ENABLE_STAT4 can be defined.
8523 ** Priority is given to SQLITE_ENABLE_STAT4.  If either are defined, also
8524 ** define SQLITE_ENABLE_STAT3_OR_STAT4
8525 */
8526 #ifdef SQLITE_ENABLE_STAT4
8527 # undef SQLITE_ENABLE_STAT3
8528 # define SQLITE_ENABLE_STAT3_OR_STAT4 1
8529 #elif SQLITE_ENABLE_STAT3
8530 # define SQLITE_ENABLE_STAT3_OR_STAT4 1
8531 #elif SQLITE_ENABLE_STAT3_OR_STAT4
8532 # undef SQLITE_ENABLE_STAT3_OR_STAT4
8533 #endif
8534 
8535 /*
8536 ** An instance of the following structure is used to store the busy-handler
8537 ** callback for a given sqlite handle.
8538 **
8539 ** The sqlite.busyHandler member of the sqlite struct contains the busy
8540 ** callback for the database handle. Each pager opened via the sqlite
8541 ** handle is passed a pointer to sqlite.busyHandler. The busy-handler
8542 ** callback is currently invoked only from within pager.c.
8543 */
8544 typedef struct BusyHandler BusyHandler;
8545 struct BusyHandler {
8546   int (*xFunc)(void *,int);  /* The busy callback */
8547   void *pArg;                /* First arg to busy callback */
8548   int nBusy;                 /* Incremented with each busy call */
8549 };
8550 
8551 /*
8552 ** Name of the master database table.  The master database table
8553 ** is a special table that holds the names and attributes of all
8554 ** user tables and indices.
8555 */
8556 #define MASTER_NAME       "sqlite_master"
8557 #define TEMP_MASTER_NAME  "sqlite_temp_master"
8558 
8559 /*
8560 ** The root-page of the master database table.
8561 */
8562 #define MASTER_ROOT       1
8563 
8564 /*
8565 ** The name of the schema table.
8566 */
8567 #define SCHEMA_TABLE(x)  ((!OMIT_TEMPDB)&&(x==1)?TEMP_MASTER_NAME:MASTER_NAME)
8568 
8569 /*
8570 ** A convenience macro that returns the number of elements in
8571 ** an array.
8572 */
8573 #define ArraySize(X)    ((int)(sizeof(X)/sizeof(X[0])))
8574 
8575 /*
8576 ** Determine if the argument is a power of two
8577 */
8578 #define IsPowerOfTwo(X) (((X)&((X)-1))==0)
8579 
8580 /*
8581 ** The following value as a destructor means to use sqlite3DbFree().
8582 ** The sqlite3DbFree() routine requires two parameters instead of the
8583 ** one parameter that destructors normally want.  So we have to introduce
8584 ** this magic value that the code knows to handle differently.  Any
8585 ** pointer will work here as long as it is distinct from SQLITE_STATIC
8586 ** and SQLITE_TRANSIENT.
8587 */
8588 #define SQLITE_DYNAMIC   ((sqlite3_destructor_type)sqlite3MallocSize)
8589 
8590 /*
8591 ** When SQLITE_OMIT_WSD is defined, it means that the target platform does
8592 ** not support Writable Static Data (WSD) such as global and static variables.
8593 ** All variables must either be on the stack or dynamically allocated from
8594 ** the heap.  When WSD is unsupported, the variable declarations scattered
8595 ** throughout the SQLite code must become constants instead.  The SQLITE_WSD
8596 ** macro is used for this purpose.  And instead of referencing the variable
8597 ** directly, we use its constant as a key to lookup the run-time allocated
8598 ** buffer that holds real variable.  The constant is also the initializer
8599 ** for the run-time allocated buffer.
8600 **
8601 ** In the usual case where WSD is supported, the SQLITE_WSD and GLOBAL
8602 ** macros become no-ops and have zero performance impact.
8603 */
8604 #ifdef SQLITE_OMIT_WSD
8605   #define SQLITE_WSD const
8606   #define GLOBAL(t,v) (*(t*)sqlite3_wsd_find((void*)&(v), sizeof(v)))
8607   #define sqlite3GlobalConfig GLOBAL(struct Sqlite3Config, sqlite3Config)
8608 SQLITE_API   int sqlite3_wsd_init(int N, int J);
8609 SQLITE_API   void *sqlite3_wsd_find(void *K, int L);
8610 #else
8611   #define SQLITE_WSD
8612   #define GLOBAL(t,v) v
8613   #define sqlite3GlobalConfig sqlite3Config
8614 #endif
8615 
8616 /*
8617 ** The following macros are used to suppress compiler warnings and to
8618 ** make it clear to human readers when a function parameter is deliberately
8619 ** left unused within the body of a function. This usually happens when
8620 ** a function is called via a function pointer. For example the
8621 ** implementation of an SQL aggregate step callback may not use the
8622 ** parameter indicating the number of arguments passed to the aggregate,
8623 ** if it knows that this is enforced elsewhere.
8624 **
8625 ** When a function parameter is not used at all within the body of a function,
8626 ** it is generally named "NotUsed" or "NotUsed2" to make things even clearer.
8627 ** However, these macros may also be used to suppress warnings related to
8628 ** parameters that may or may not be used depending on compilation options.
8629 ** For example those parameters only used in assert() statements. In these
8630 ** cases the parameters are named as per the usual conventions.
8631 */
8632 #define UNUSED_PARAMETER(x) (void)(x)
8633 #define UNUSED_PARAMETER2(x,y) UNUSED_PARAMETER(x),UNUSED_PARAMETER(y)
8634 
8635 /*
8636 ** Forward references to structures
8637 */
8638 typedef struct AggInfo AggInfo;
8639 typedef struct AuthContext AuthContext;
8640 typedef struct AutoincInfo AutoincInfo;
8641 typedef struct Bitvec Bitvec;
8642 typedef struct CollSeq CollSeq;
8643 typedef struct Column Column;
8644 typedef struct Db Db;
8645 typedef struct Schema Schema;
8646 typedef struct Expr Expr;
8647 typedef struct ExprList ExprList;
8648 typedef struct ExprSpan ExprSpan;
8649 typedef struct FKey FKey;
8650 typedef struct FuncDestructor FuncDestructor;
8651 typedef struct FuncDef FuncDef;
8652 typedef struct FuncDefHash FuncDefHash;
8653 typedef struct IdList IdList;
8654 typedef struct Index Index;
8655 typedef struct IndexSample IndexSample;
8656 typedef struct KeyClass KeyClass;
8657 typedef struct KeyInfo KeyInfo;
8658 typedef struct Lookaside Lookaside;
8659 typedef struct LookasideSlot LookasideSlot;
8660 typedef struct Module Module;
8661 typedef struct NameContext NameContext;
8662 typedef struct Parse Parse;
8663 typedef struct PrintfArguments PrintfArguments;
8664 typedef struct RowSet RowSet;
8665 typedef struct Savepoint Savepoint;
8666 typedef struct Select Select;
8667 typedef struct SelectDest SelectDest;
8668 typedef struct SrcList SrcList;
8669 typedef struct StrAccum StrAccum;
8670 typedef struct Table Table;
8671 typedef struct TableLock TableLock;
8672 typedef struct Token Token;
8673 typedef struct Trigger Trigger;
8674 typedef struct TriggerPrg TriggerPrg;
8675 typedef struct TriggerStep TriggerStep;
8676 typedef struct UnpackedRecord UnpackedRecord;
8677 typedef struct VTable VTable;
8678 typedef struct VtabCtx VtabCtx;
8679 typedef struct Walker Walker;
8680 typedef struct WhereInfo WhereInfo;
8681 typedef struct With With;
8682 
8683 /*
8684 ** Defer sourcing vdbe.h and btree.h until after the "u8" and
8685 ** "BusyHandler" typedefs. vdbe.h also requires a few of the opaque
8686 ** pointer types (i.e. FuncDef) defined above.
8687 */
8688 /************** Include btree.h in the middle of sqliteInt.h *****************/
8689 /************** Begin file btree.h *******************************************/
8690 /*
8691 ** 2001 September 15
8692 **
8693 ** The author disclaims copyright to this source code.  In place of
8694 ** a legal notice, here is a blessing:
8695 **
8696 **    May you do good and not evil.
8697 **    May you find forgiveness for yourself and forgive others.
8698 **    May you share freely, never taking more than you give.
8699 **
8700 *************************************************************************
8701 ** This header file defines the interface that the sqlite B-Tree file
8702 ** subsystem.  See comments in the source code for a detailed description
8703 ** of what each interface routine does.
8704 */
8705 #ifndef _BTREE_H_
8706 #define _BTREE_H_
8707 
8708 /* TODO: This definition is just included so other modules compile. It
8709 ** needs to be revisited.
8710 */
8711 #define SQLITE_N_BTREE_META 10
8712 
8713 /*
8714 ** If defined as non-zero, auto-vacuum is enabled by default. Otherwise
8715 ** it must be turned on for each database using "PRAGMA auto_vacuum = 1".
8716 */
8717 #ifndef SQLITE_DEFAULT_AUTOVACUUM
8718   #define SQLITE_DEFAULT_AUTOVACUUM 0
8719 #endif
8720 
8721 #define BTREE_AUTOVACUUM_NONE 0        /* Do not do auto-vacuum */
8722 #define BTREE_AUTOVACUUM_FULL 1        /* Do full auto-vacuum */
8723 #define BTREE_AUTOVACUUM_INCR 2        /* Incremental vacuum */
8724 
8725 /*
8726 ** Forward declarations of structure
8727 */
8728 typedef struct Btree Btree;
8729 typedef struct BtCursor BtCursor;
8730 typedef struct BtShared BtShared;
8731 
8732 
8733 SQLITE_PRIVATE int sqlite3BtreeOpen(
8734   sqlite3_vfs *pVfs,       /* VFS to use with this b-tree */
8735   const char *zFilename,   /* Name of database file to open */
8736   sqlite3 *db,             /* Associated database connection */
8737   Btree **ppBtree,         /* Return open Btree* here */
8738   int flags,               /* Flags */
8739   int vfsFlags             /* Flags passed through to VFS open */
8740 );
8741 
8742 /* The flags parameter to sqlite3BtreeOpen can be the bitwise or of the
8743 ** following values.
8744 **
8745 ** NOTE:  These values must match the corresponding PAGER_ values in
8746 ** pager.h.
8747 */
8748 #define BTREE_OMIT_JOURNAL  1  /* Do not create or use a rollback journal */
8749 #define BTREE_MEMORY        2  /* This is an in-memory DB */
8750 #define BTREE_SINGLE        4  /* The file contains at most 1 b-tree */
8751 #define BTREE_UNORDERED     8  /* Use of a hash implementation is OK */
8752 
8753 SQLITE_PRIVATE int sqlite3BtreeClose(Btree*);
8754 SQLITE_PRIVATE int sqlite3BtreeSetCacheSize(Btree*,int);
8755 SQLITE_PRIVATE int sqlite3BtreeSetMmapLimit(Btree*,sqlite3_int64);
8756 SQLITE_PRIVATE int sqlite3BtreeSetPagerFlags(Btree*,unsigned);
8757 SQLITE_PRIVATE int sqlite3BtreeSyncDisabled(Btree*);
8758 SQLITE_PRIVATE int sqlite3BtreeSetPageSize(Btree *p, int nPagesize, int nReserve, int eFix);
8759 SQLITE_PRIVATE int sqlite3BtreeGetPageSize(Btree*);
8760 SQLITE_PRIVATE int sqlite3BtreeMaxPageCount(Btree*,int);
8761 SQLITE_PRIVATE u32 sqlite3BtreeLastPage(Btree*);
8762 SQLITE_PRIVATE int sqlite3BtreeSecureDelete(Btree*,int);
8763 SQLITE_PRIVATE int sqlite3BtreeGetReserve(Btree*);
8764 #if defined(SQLITE_HAS_CODEC) || defined(SQLITE_DEBUG)
8765 SQLITE_PRIVATE int sqlite3BtreeGetReserveNoMutex(Btree *p);
8766 #endif
8767 SQLITE_PRIVATE int sqlite3BtreeSetAutoVacuum(Btree *, int);
8768 SQLITE_PRIVATE int sqlite3BtreeGetAutoVacuum(Btree *);
8769 SQLITE_PRIVATE int sqlite3BtreeBeginTrans(Btree*,int);
8770 SQLITE_PRIVATE int sqlite3BtreeCommitPhaseOne(Btree*, const char *zMaster);
8771 SQLITE_PRIVATE int sqlite3BtreeCommitPhaseTwo(Btree*, int);
8772 SQLITE_PRIVATE int sqlite3BtreeCommit(Btree*);
8773 SQLITE_PRIVATE int sqlite3BtreeRollback(Btree*,int);
8774 SQLITE_PRIVATE int sqlite3BtreeBeginStmt(Btree*,int);
8775 SQLITE_PRIVATE int sqlite3BtreeCreateTable(Btree*, int*, int flags);
8776 SQLITE_PRIVATE int sqlite3BtreeIsInTrans(Btree*);
8777 SQLITE_PRIVATE int sqlite3BtreeIsInReadTrans(Btree*);
8778 SQLITE_PRIVATE int sqlite3BtreeIsInBackup(Btree*);
8779 SQLITE_PRIVATE void *sqlite3BtreeSchema(Btree *, int, void(*)(void *));
8780 SQLITE_PRIVATE int sqlite3BtreeSchemaLocked(Btree *pBtree);
8781 SQLITE_PRIVATE int sqlite3BtreeLockTable(Btree *pBtree, int iTab, u8 isWriteLock);
8782 SQLITE_PRIVATE int sqlite3BtreeSavepoint(Btree *, int, int);
8783 
8784 SQLITE_PRIVATE const char *sqlite3BtreeGetFilename(Btree *);
8785 SQLITE_PRIVATE const char *sqlite3BtreeGetJournalname(Btree *);
8786 SQLITE_PRIVATE int sqlite3BtreeCopyFile(Btree *, Btree *);
8787 
8788 SQLITE_PRIVATE int sqlite3BtreeIncrVacuum(Btree *);
8789 
8790 /* The flags parameter to sqlite3BtreeCreateTable can be the bitwise OR
8791 ** of the flags shown below.
8792 **
8793 ** Every SQLite table must have either BTREE_INTKEY or BTREE_BLOBKEY set.
8794 ** With BTREE_INTKEY, the table key is a 64-bit integer and arbitrary data
8795 ** is stored in the leaves.  (BTREE_INTKEY is used for SQL tables.)  With
8796 ** BTREE_BLOBKEY, the key is an arbitrary BLOB and no content is stored
8797 ** anywhere - the key is the content.  (BTREE_BLOBKEY is used for SQL
8798 ** indices.)
8799 */
8800 #define BTREE_INTKEY     1    /* Table has only 64-bit signed integer keys */
8801 #define BTREE_BLOBKEY    2    /* Table has keys only - no data */
8802 
8803 SQLITE_PRIVATE int sqlite3BtreeDropTable(Btree*, int, int*);
8804 SQLITE_PRIVATE int sqlite3BtreeClearTable(Btree*, int, int*);
8805 SQLITE_PRIVATE void sqlite3BtreeTripAllCursors(Btree*, int);
8806 
8807 SQLITE_PRIVATE void sqlite3BtreeGetMeta(Btree *pBtree, int idx, u32 *pValue);
8808 SQLITE_PRIVATE int sqlite3BtreeUpdateMeta(Btree*, int idx, u32 value);
8809 
8810 SQLITE_PRIVATE int sqlite3BtreeNewDb(Btree *p);
8811 
8812 /*
8813 ** The second parameter to sqlite3BtreeGetMeta or sqlite3BtreeUpdateMeta
8814 ** should be one of the following values. The integer values are assigned
8815 ** to constants so that the offset of the corresponding field in an
8816 ** SQLite database header may be found using the following formula:
8817 **
8818 **   offset = 36 + (idx * 4)
8819 **
8820 ** For example, the free-page-count field is located at byte offset 36 of
8821 ** the database file header. The incr-vacuum-flag field is located at
8822 ** byte offset 64 (== 36+4*7).
8823 */
8824 #define BTREE_FREE_PAGE_COUNT     0
8825 #define BTREE_SCHEMA_VERSION      1
8826 #define BTREE_FILE_FORMAT         2
8827 #define BTREE_DEFAULT_CACHE_SIZE  3
8828 #define BTREE_LARGEST_ROOT_PAGE   4
8829 #define BTREE_TEXT_ENCODING       5
8830 #define BTREE_USER_VERSION        6
8831 #define BTREE_INCR_VACUUM         7
8832 #define BTREE_APPLICATION_ID      8
8833 
8834 /*
8835 ** Values that may be OR'd together to form the second argument of an
8836 ** sqlite3BtreeCursorHints() call.
8837 */
8838 #define BTREE_BULKLOAD 0x00000001
8839 
8840 SQLITE_PRIVATE int sqlite3BtreeCursor(
8841   Btree*,                              /* BTree containing table to open */
8842   int iTable,                          /* Index of root page */
8843   int wrFlag,                          /* 1 for writing.  0 for read-only */
8844   struct KeyInfo*,                     /* First argument to compare function */
8845   BtCursor *pCursor                    /* Space to write cursor structure */
8846 );
8847 SQLITE_PRIVATE int sqlite3BtreeCursorSize(void);
8848 SQLITE_PRIVATE void sqlite3BtreeCursorZero(BtCursor*);
8849 
8850 SQLITE_PRIVATE int sqlite3BtreeCloseCursor(BtCursor*);
8851 SQLITE_PRIVATE int sqlite3BtreeMovetoUnpacked(
8852   BtCursor*,
8853   UnpackedRecord *pUnKey,
8854   i64 intKey,
8855   int bias,
8856   int *pRes
8857 );
8858 SQLITE_PRIVATE int sqlite3BtreeCursorHasMoved(BtCursor*, int*);
8859 SQLITE_PRIVATE int sqlite3BtreeDelete(BtCursor*);
8860 SQLITE_PRIVATE int sqlite3BtreeInsert(BtCursor*, const void *pKey, i64 nKey,
8861                                   const void *pData, int nData,
8862                                   int nZero, int bias, int seekResult);
8863 SQLITE_PRIVATE int sqlite3BtreeFirst(BtCursor*, int *pRes);
8864 SQLITE_PRIVATE int sqlite3BtreeLast(BtCursor*, int *pRes);
8865 SQLITE_PRIVATE int sqlite3BtreeNext(BtCursor*, int *pRes);
8866 SQLITE_PRIVATE int sqlite3BtreeEof(BtCursor*);
8867 SQLITE_PRIVATE int sqlite3BtreePrevious(BtCursor*, int *pRes);
8868 SQLITE_PRIVATE int sqlite3BtreeKeySize(BtCursor*, i64 *pSize);
8869 SQLITE_PRIVATE int sqlite3BtreeKey(BtCursor*, u32 offset, u32 amt, void*);
8870 SQLITE_PRIVATE const void *sqlite3BtreeKeyFetch(BtCursor*, u32 *pAmt);
8871 SQLITE_PRIVATE const void *sqlite3BtreeDataFetch(BtCursor*, u32 *pAmt);
8872 SQLITE_PRIVATE int sqlite3BtreeDataSize(BtCursor*, u32 *pSize);
8873 SQLITE_PRIVATE int sqlite3BtreeData(BtCursor*, u32 offset, u32 amt, void*);
8874 SQLITE_PRIVATE void sqlite3BtreeSetCachedRowid(BtCursor*, sqlite3_int64);
8875 SQLITE_PRIVATE sqlite3_int64 sqlite3BtreeGetCachedRowid(BtCursor*);
8876 
8877 SQLITE_PRIVATE char *sqlite3BtreeIntegrityCheck(Btree*, int *aRoot, int nRoot, int, int*);
8878 SQLITE_PRIVATE struct Pager *sqlite3BtreePager(Btree*);
8879 
8880 SQLITE_PRIVATE int sqlite3BtreePutData(BtCursor*, u32 offset, u32 amt, void*);
8881 SQLITE_PRIVATE void sqlite3BtreeCacheOverflow(BtCursor *);
8882 SQLITE_PRIVATE void sqlite3BtreeClearCursor(BtCursor *);
8883 SQLITE_PRIVATE int sqlite3BtreeSetVersion(Btree *pBt, int iVersion);
8884 SQLITE_PRIVATE void sqlite3BtreeCursorHints(BtCursor *, unsigned int mask);
8885 
8886 #ifndef NDEBUG
8887 SQLITE_PRIVATE int sqlite3BtreeCursorIsValid(BtCursor*);
8888 #endif
8889 
8890 #ifndef SQLITE_OMIT_BTREECOUNT
8891 SQLITE_PRIVATE int sqlite3BtreeCount(BtCursor *, i64 *);
8892 #endif
8893 
8894 #ifdef SQLITE_TEST
8895 SQLITE_PRIVATE int sqlite3BtreeCursorInfo(BtCursor*, int*, int);
8896 SQLITE_PRIVATE void sqlite3BtreeCursorList(Btree*);
8897 #endif
8898 
8899 #ifndef SQLITE_OMIT_WAL
8900 SQLITE_PRIVATE   int sqlite3BtreeCheckpoint(Btree*, int, int *, int *);
8901 #endif
8902 
8903 /*
8904 ** If we are not using shared cache, then there is no need to
8905 ** use mutexes to access the BtShared structures.  So make the
8906 ** Enter and Leave procedures no-ops.
8907 */
8908 #ifndef SQLITE_OMIT_SHARED_CACHE
8909 SQLITE_PRIVATE   void sqlite3BtreeEnter(Btree*);
8910 SQLITE_PRIVATE   void sqlite3BtreeEnterAll(sqlite3*);
8911 #else
8912 # define sqlite3BtreeEnter(X)
8913 # define sqlite3BtreeEnterAll(X)
8914 #endif
8915 
8916 #if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE
8917 SQLITE_PRIVATE   int sqlite3BtreeSharable(Btree*);
8918 SQLITE_PRIVATE   void sqlite3BtreeLeave(Btree*);
8919 SQLITE_PRIVATE   void sqlite3BtreeEnterCursor(BtCursor*);
8920 SQLITE_PRIVATE   void sqlite3BtreeLeaveCursor(BtCursor*);
8921 SQLITE_PRIVATE   void sqlite3BtreeLeaveAll(sqlite3*);
8922 #ifndef NDEBUG
8923   /* These routines are used inside assert() statements only. */
8924 SQLITE_PRIVATE   int sqlite3BtreeHoldsMutex(Btree*);
8925 SQLITE_PRIVATE   int sqlite3BtreeHoldsAllMutexes(sqlite3*);
8926 SQLITE_PRIVATE   int sqlite3SchemaMutexHeld(sqlite3*,int,Schema*);
8927 #endif
8928 #else
8929 
8930 # define sqlite3BtreeSharable(X) 0
8931 # define sqlite3BtreeLeave(X)
8932 # define sqlite3BtreeEnterCursor(X)
8933 # define sqlite3BtreeLeaveCursor(X)
8934 # define sqlite3BtreeLeaveAll(X)
8935 
8936 # define sqlite3BtreeHoldsMutex(X) 1
8937 # define sqlite3BtreeHoldsAllMutexes(X) 1
8938 # define sqlite3SchemaMutexHeld(X,Y,Z) 1
8939 #endif
8940 
8941 
8942 #endif /* _BTREE_H_ */
8943 
8944 /************** End of btree.h ***********************************************/
8945 /************** Continuing where we left off in sqliteInt.h ******************/
8946 /************** Include vdbe.h in the middle of sqliteInt.h ******************/
8947 /************** Begin file vdbe.h ********************************************/
8948 /*
8949 ** 2001 September 15
8950 **
8951 ** The author disclaims copyright to this source code.  In place of
8952 ** a legal notice, here is a blessing:
8953 **
8954 **    May you do good and not evil.
8955 **    May you find forgiveness for yourself and forgive others.
8956 **    May you share freely, never taking more than you give.
8957 **
8958 *************************************************************************
8959 ** Header file for the Virtual DataBase Engine (VDBE)
8960 **
8961 ** This header defines the interface to the virtual database engine
8962 ** or VDBE.  The VDBE implements an abstract machine that runs a
8963 ** simple program to access and modify the underlying database.
8964 */
8965 #ifndef _SQLITE_VDBE_H_
8966 #define _SQLITE_VDBE_H_
8967 /* #include <stdio.h> */
8968 
8969 /*
8970 ** A single VDBE is an opaque structure named "Vdbe".  Only routines
8971 ** in the source file sqliteVdbe.c are allowed to see the insides
8972 ** of this structure.
8973 */
8974 typedef struct Vdbe Vdbe;
8975 
8976 /*
8977 ** The names of the following types declared in vdbeInt.h are required
8978 ** for the VdbeOp definition.
8979 */
8980 typedef struct Mem Mem;
8981 typedef struct SubProgram SubProgram;
8982 
8983 /*
8984 ** A single instruction of the virtual machine has an opcode
8985 ** and as many as three operands.  The instruction is recorded
8986 ** as an instance of the following structure:
8987 */
8988 struct VdbeOp {
8989   u8 opcode;          /* What operation to perform */
8990   signed char p4type; /* One of the P4_xxx constants for p4 */
8991   u8 opflags;         /* Mask of the OPFLG_* flags in opcodes.h */
8992   u8 p5;              /* Fifth parameter is an unsigned character */
8993   int p1;             /* First operand */
8994   int p2;             /* Second parameter (often the jump destination) */
8995   int p3;             /* The third parameter */
8996   union {             /* fourth parameter */
8997     int i;                 /* Integer value if p4type==P4_INT32 */
8998     void *p;               /* Generic pointer */
8999     char *z;               /* Pointer to data for string (char array) types */
9000     i64 *pI64;             /* Used when p4type is P4_INT64 */
9001     double *pReal;         /* Used when p4type is P4_REAL */
9002     FuncDef *pFunc;        /* Used when p4type is P4_FUNCDEF */
9003     CollSeq *pColl;        /* Used when p4type is P4_COLLSEQ */
9004     Mem *pMem;             /* Used when p4type is P4_MEM */
9005     VTable *pVtab;         /* Used when p4type is P4_VTAB */
9006     KeyInfo *pKeyInfo;     /* Used when p4type is P4_KEYINFO */
9007     int *ai;               /* Used when p4type is P4_INTARRAY */
9008     SubProgram *pProgram;  /* Used when p4type is P4_SUBPROGRAM */
9009     int (*xAdvance)(BtCursor *, int *);
9010   } p4;
9011 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
9012   char *zComment;          /* Comment to improve readability */
9013 #endif
9014 #ifdef VDBE_PROFILE
9015   int cnt;                 /* Number of times this instruction was executed */
9016   u64 cycles;              /* Total time spent executing this instruction */
9017 #endif
9018 };
9019 typedef struct VdbeOp VdbeOp;
9020 
9021 
9022 /*
9023 ** A sub-routine used to implement a trigger program.
9024 */
9025 struct SubProgram {
9026   VdbeOp *aOp;                  /* Array of opcodes for sub-program */
9027   int nOp;                      /* Elements in aOp[] */
9028   int nMem;                     /* Number of memory cells required */
9029   int nCsr;                     /* Number of cursors required */
9030   int nOnce;                    /* Number of OP_Once instructions */
9031   void *token;                  /* id that may be used to recursive triggers */
9032   SubProgram *pNext;            /* Next sub-program already visited */
9033 };
9034 
9035 /*
9036 ** A smaller version of VdbeOp used for the VdbeAddOpList() function because
9037 ** it takes up less space.
9038 */
9039 struct VdbeOpList {
9040   u8 opcode;          /* What operation to perform */
9041   signed char p1;     /* First operand */
9042   signed char p2;     /* Second parameter (often the jump destination) */
9043   signed char p3;     /* Third parameter */
9044 };
9045 typedef struct VdbeOpList VdbeOpList;
9046 
9047 /*
9048 ** Allowed values of VdbeOp.p4type
9049 */
9050 #define P4_NOTUSED    0   /* The P4 parameter is not used */
9051 #define P4_DYNAMIC  (-1)  /* Pointer to a string obtained from sqliteMalloc() */
9052 #define P4_STATIC   (-2)  /* Pointer to a static string */
9053 #define P4_COLLSEQ  (-4)  /* P4 is a pointer to a CollSeq structure */
9054 #define P4_FUNCDEF  (-5)  /* P4 is a pointer to a FuncDef structure */
9055 #define P4_KEYINFO  (-6)  /* P4 is a pointer to a KeyInfo structure */
9056 #define P4_MEM      (-8)  /* P4 is a pointer to a Mem*    structure */
9057 #define P4_TRANSIENT  0   /* P4 is a pointer to a transient string */
9058 #define P4_VTAB     (-10) /* P4 is a pointer to an sqlite3_vtab structure */
9059 #define P4_MPRINTF  (-11) /* P4 is a string obtained from sqlite3_mprintf() */
9060 #define P4_REAL     (-12) /* P4 is a 64-bit floating point value */
9061 #define P4_INT64    (-13) /* P4 is a 64-bit signed integer */
9062 #define P4_INT32    (-14) /* P4 is a 32-bit signed integer */
9063 #define P4_INTARRAY (-15) /* P4 is a vector of 32-bit integers */
9064 #define P4_SUBPROGRAM  (-18) /* P4 is a pointer to a SubProgram structure */
9065 #define P4_ADVANCE  (-19) /* P4 is a pointer to BtreeNext() or BtreePrev() */
9066 
9067 /* Error message codes for OP_Halt */
9068 #define P5_ConstraintNotNull 1
9069 #define P5_ConstraintUnique  2
9070 #define P5_ConstraintCheck   3
9071 #define P5_ConstraintFK      4
9072 
9073 /*
9074 ** The Vdbe.aColName array contains 5n Mem structures, where n is the
9075 ** number of columns of data returned by the statement.
9076 */
9077 #define COLNAME_NAME     0
9078 #define COLNAME_DECLTYPE 1
9079 #define COLNAME_DATABASE 2
9080 #define COLNAME_TABLE    3
9081 #define COLNAME_COLUMN   4
9082 #ifdef SQLITE_ENABLE_COLUMN_METADATA
9083 # define COLNAME_N        5      /* Number of COLNAME_xxx symbols */
9084 #else
9085 # ifdef SQLITE_OMIT_DECLTYPE
9086 #   define COLNAME_N      1      /* Store only the name */
9087 # else
9088 #   define COLNAME_N      2      /* Store the name and decltype */
9089 # endif
9090 #endif
9091 
9092 /*
9093 ** The following macro converts a relative address in the p2 field
9094 ** of a VdbeOp structure into a negative number so that
9095 ** sqlite3VdbeAddOpList() knows that the address is relative.  Calling
9096 ** the macro again restores the address.
9097 */
9098 #define ADDR(X)  (-1-(X))
9099 
9100 /*
9101 ** The makefile scans the vdbe.c source file and creates the "opcodes.h"
9102 ** header file that defines a number for each opcode used by the VDBE.
9103 */
9104 /************** Include opcodes.h in the middle of vdbe.h ********************/
9105 /************** Begin file opcodes.h *****************************************/
9106 /* Automatically generated.  Do not edit */
9107 /* See the mkopcodeh.awk script for details */
9108 #define OP_Function        1 /* synopsis: r[P3]=func(r[P2@P5])             */
9109 #define OP_Savepoint       2
9110 #define OP_AutoCommit      3
9111 #define OP_Transaction     4
9112 #define OP_SorterNext      5
9113 #define OP_PrevIfOpen      6
9114 #define OP_NextIfOpen      7
9115 #define OP_Prev            8
9116 #define OP_Next            9
9117 #define OP_AggStep        10 /* synopsis: accum=r[P3] step(r[P2@P5])       */
9118 #define OP_Checkpoint     11
9119 #define OP_JournalMode    12
9120 #define OP_Vacuum         13
9121 #define OP_VFilter        14 /* synopsis: iPlan=r[P3] zPlan='P4'           */
9122 #define OP_VUpdate        15 /* synopsis: data=r[P3@P2]                    */
9123 #define OP_Goto           16
9124 #define OP_Gosub          17
9125 #define OP_Return         18
9126 #define OP_Not            19 /* same as TK_NOT, synopsis: r[P2]= !r[P1]    */
9127 #define OP_Yield          20
9128 #define OP_HaltIfNull     21 /* synopsis: if r[P3] null then halt          */
9129 #define OP_Halt           22
9130 #define OP_Integer        23 /* synopsis: r[P2]=P1                         */
9131 #define OP_Int64          24 /* synopsis: r[P2]=P4                         */
9132 #define OP_String         25 /* synopsis: r[P2]='P4' (len=P1)              */
9133 #define OP_Null           26 /* synopsis: r[P2..P3]=NULL                   */
9134 #define OP_Blob           27 /* synopsis: r[P2]=P4 (len=P1)                */
9135 #define OP_Variable       28 /* synopsis: r[P2]=parameter(P1,P4)           */
9136 #define OP_Move           29 /* synopsis: r[P2@P3]=r[P1@P3]                */
9137 #define OP_Copy           30 /* synopsis: r[P2@P3+1]=r[P1@P3+1]            */
9138 #define OP_SCopy          31 /* synopsis: r[P2]=r[P1]                      */
9139 #define OP_ResultRow      32 /* synopsis: output=r[P1@P2]                  */
9140 #define OP_CollSeq        33
9141 #define OP_AddImm         34 /* synopsis: r[P1]=r[P1]+P2                   */
9142 #define OP_MustBeInt      35
9143 #define OP_RealAffinity   36
9144 #define OP_Permutation    37
9145 #define OP_Compare        38
9146 #define OP_Jump           39
9147 #define OP_Once           40
9148 #define OP_If             41
9149 #define OP_IfNot          42
9150 #define OP_Column         43 /* synopsis: r[P3]=PX                         */
9151 #define OP_Affinity       44 /* synopsis: affinity(r[P1@P2])               */
9152 #define OP_MakeRecord     45 /* synopsis: r[P3]=mkrec(r[P1@P2])            */
9153 #define OP_Count          46 /* synopsis: r[P2]=count()                    */
9154 #define OP_ReadCookie     47
9155 #define OP_SetCookie      48
9156 #define OP_VerifyCookie   49
9157 #define OP_OpenRead       50 /* synopsis: root=P2 iDb=P3                   */
9158 #define OP_OpenWrite      51 /* synopsis: root=P2 iDb=P3                   */
9159 #define OP_OpenAutoindex  52 /* synopsis: nColumn=P2                       */
9160 #define OP_OpenEphemeral  53 /* synopsis: nColumn=P2                       */
9161 #define OP_SorterOpen     54
9162 #define OP_OpenPseudo     55 /* synopsis: content in r[P2@P3]              */
9163 #define OP_Close          56
9164 #define OP_SeekLt         57 /* synopsis: key=r[P3@P4]                     */
9165 #define OP_SeekLe         58 /* synopsis: key=r[P3@P4]                     */
9166 #define OP_SeekGe         59 /* synopsis: key=r[P3@P4]                     */
9167 #define OP_SeekGt         60 /* synopsis: key=r[P3@P4]                     */
9168 #define OP_Seek           61 /* synopsis: intkey=r[P2]                     */
9169 #define OP_NoConflict     62 /* synopsis: key=r[P3@P4]                     */
9170 #define OP_NotFound       63 /* synopsis: key=r[P3@P4]                     */
9171 #define OP_Found          64 /* synopsis: key=r[P3@P4]                     */
9172 #define OP_NotExists      65 /* synopsis: intkey=r[P3]                     */
9173 #define OP_Sequence       66 /* synopsis: r[P2]=rowid                      */
9174 #define OP_NewRowid       67 /* synopsis: r[P2]=rowid                      */
9175 #define OP_Insert         68 /* synopsis: intkey=r[P3] data=r[P2]          */
9176 #define OP_InsertInt      69 /* synopsis: intkey=P3 data=r[P2]             */
9177 #define OP_Delete         70
9178 #define OP_Or             71 /* same as TK_OR, synopsis: r[P3]=(r[P1] || r[P2]) */
9179 #define OP_And            72 /* same as TK_AND, synopsis: r[P3]=(r[P1] && r[P2]) */
9180 #define OP_ResetCount     73
9181 #define OP_SorterCompare  74 /* synopsis: if key(P1)!=rtrim(r[P3],P4) goto P2 */
9182 #define OP_SorterData     75 /* synopsis: r[P2]=data                       */
9183 #define OP_IsNull         76 /* same as TK_ISNULL, synopsis: if r[P1]==NULL goto P2 */
9184 #define OP_NotNull        77 /* same as TK_NOTNULL, synopsis: if r[P1]!=NULL goto P2 */
9185 #define OP_Ne             78 /* same as TK_NE, synopsis: if r[P1]!=r[P3] goto P2 */
9186 #define OP_Eq             79 /* same as TK_EQ, synopsis: if r[P1]==r[P3] goto P2 */
9187 #define OP_Gt             80 /* same as TK_GT, synopsis: if r[P1]>r[P3] goto P2 */
9188 #define OP_Le             81 /* same as TK_LE, synopsis: if r[P1]<=r[P3] goto P2 */
9189 #define OP_Lt             82 /* same as TK_LT, synopsis: if r[P1]<r[P3] goto P2 */
9190 #define OP_Ge             83 /* same as TK_GE, synopsis: if r[P1]>=r[P3] goto P2 */
9191 #define OP_RowKey         84 /* synopsis: r[P2]=key                        */
9192 #define OP_BitAnd         85 /* same as TK_BITAND, synopsis: r[P3]=r[P1]&r[P2] */
9193 #define OP_BitOr          86 /* same as TK_BITOR, synopsis: r[P3]=r[P1]|r[P2] */
9194 #define OP_ShiftLeft      87 /* same as TK_LSHIFT, synopsis: r[P3]=r[P2]<<r[P1] */
9195 #define OP_ShiftRight     88 /* same as TK_RSHIFT, synopsis: r[P3]=r[P2]>>r[P1] */
9196 #define OP_Add            89 /* same as TK_PLUS, synopsis: r[P3]=r[P1]+r[P2] */
9197 #define OP_Subtract       90 /* same as TK_MINUS, synopsis: r[P3]=r[P2]-r[P1] */
9198 #define OP_Multiply       91 /* same as TK_STAR, synopsis: r[P3]=r[P1]*r[P2] */
9199 #define OP_Divide         92 /* same as TK_SLASH, synopsis: r[P3]=r[P2]/r[P1] */
9200 #define OP_Remainder      93 /* same as TK_REM, synopsis: r[P3]=r[P2]%r[P1] */
9201 #define OP_Concat         94 /* same as TK_CONCAT, synopsis: r[P3]=r[P2]+r[P1] */
9202 #define OP_RowData        95 /* synopsis: r[P2]=data                       */
9203 #define OP_BitNot         96 /* same as TK_BITNOT, synopsis: r[P1]= ~r[P1] */
9204 #define OP_String8        97 /* same as TK_STRING, synopsis: r[P2]='P4'    */
9205 #define OP_Rowid          98 /* synopsis: r[P2]=rowid                      */
9206 #define OP_NullRow        99
9207 #define OP_Last          100
9208 #define OP_SorterSort    101
9209 #define OP_Sort          102
9210 #define OP_Rewind        103
9211 #define OP_SorterInsert  104
9212 #define OP_IdxInsert     105 /* synopsis: key=r[P2]                        */
9213 #define OP_IdxDelete     106 /* synopsis: key=r[P2@P3]                     */
9214 #define OP_IdxRowid      107 /* synopsis: r[P2]=rowid                      */
9215 #define OP_IdxLT         108 /* synopsis: key=r[P3@P4]                     */
9216 #define OP_IdxGE         109 /* synopsis: key=r[P3@P4]                     */
9217 #define OP_Destroy       110
9218 #define OP_Clear         111
9219 #define OP_CreateIndex   112 /* synopsis: r[P2]=root iDb=P1                */
9220 #define OP_CreateTable   113 /* synopsis: r[P2]=root iDb=P1                */
9221 #define OP_ParseSchema   114
9222 #define OP_LoadAnalysis  115
9223 #define OP_DropTable     116
9224 #define OP_DropIndex     117
9225 #define OP_DropTrigger   118
9226 #define OP_IntegrityCk   119
9227 #define OP_RowSetAdd     120 /* synopsis: rowset(P1)=r[P2]                 */
9228 #define OP_RowSetRead    121 /* synopsis: r[P3]=rowset(P1)                 */
9229 #define OP_RowSetTest    122 /* synopsis: if r[P3] in rowset(P1) goto P2   */
9230 #define OP_Program       123
9231 #define OP_Param         124
9232 #define OP_FkCounter     125 /* synopsis: fkctr[P1]+=P2                    */
9233 #define OP_FkIfZero      126 /* synopsis: if fkctr[P1]==0 goto P2          */
9234 #define OP_MemMax        127 /* synopsis: r[P1]=max(r[P1],r[P2])           */
9235 #define OP_IfPos         128 /* synopsis: if r[P1]>0 goto P2               */
9236 #define OP_IfNeg         129 /* synopsis: if r[P1]<0 goto P2               */
9237 #define OP_IfZero        130 /* synopsis: r[P1]+=P3, if r[P1]==0 goto P2   */
9238 #define OP_AggFinal      131 /* synopsis: accum=r[P1] N=P2                 */
9239 #define OP_IncrVacuum    132
9240 #define OP_Real          133 /* same as TK_FLOAT, synopsis: r[P2]=P4       */
9241 #define OP_Expire        134
9242 #define OP_TableLock     135 /* synopsis: iDb=P1 root=P2 write=P3          */
9243 #define OP_VBegin        136
9244 #define OP_VCreate       137
9245 #define OP_VDestroy      138
9246 #define OP_VOpen         139
9247 #define OP_VColumn       140 /* synopsis: r[P3]=vcolumn(P2)                */
9248 #define OP_VNext         141
9249 #define OP_VRename       142
9250 #define OP_ToText        143 /* same as TK_TO_TEXT                         */
9251 #define OP_ToBlob        144 /* same as TK_TO_BLOB                         */
9252 #define OP_ToNumeric     145 /* same as TK_TO_NUMERIC                      */
9253 #define OP_ToInt         146 /* same as TK_TO_INT                          */
9254 #define OP_ToReal        147 /* same as TK_TO_REAL                         */
9255 #define OP_Pagecount     148
9256 #define OP_MaxPgcnt      149
9257 #define OP_Trace         150
9258 #define OP_Noop          151
9259 #define OP_Explain       152
9260 
9261 
9262 /* Properties such as "out2" or "jump" that are specified in
9263 ** comments following the "case" for each opcode in the vdbe.c
9264 ** are encoded into bitvectors as follows:
9265 */
9266 #define OPFLG_JUMP            0x0001  /* jump:  P2 holds jmp target */
9267 #define OPFLG_OUT2_PRERELEASE 0x0002  /* out2-prerelease: */
9268 #define OPFLG_IN1             0x0004  /* in1:   P1 is an input */
9269 #define OPFLG_IN2             0x0008  /* in2:   P2 is an input */
9270 #define OPFLG_IN3             0x0010  /* in3:   P3 is an input */
9271 #define OPFLG_OUT2            0x0020  /* out2:  P2 is an output */
9272 #define OPFLG_OUT3            0x0040  /* out3:  P3 is an output */
9273 #define OPFLG_INITIALIZER {\
9274 /*   0 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01,\
9275 /*   8 */ 0x01, 0x01, 0x00, 0x00, 0x02, 0x00, 0x01, 0x00,\
9276 /*  16 */ 0x01, 0x01, 0x04, 0x24, 0x04, 0x10, 0x00, 0x02,\
9277 /*  24 */ 0x02, 0x02, 0x02, 0x02, 0x02, 0x00, 0x00, 0x20,\
9278 /*  32 */ 0x00, 0x00, 0x04, 0x05, 0x04, 0x00, 0x00, 0x01,\
9279 /*  40 */ 0x01, 0x05, 0x05, 0x00, 0x00, 0x00, 0x02, 0x02,\
9280 /*  48 */ 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
9281 /*  56 */ 0x00, 0x11, 0x11, 0x11, 0x11, 0x08, 0x11, 0x11,\
9282 /*  64 */ 0x11, 0x11, 0x02, 0x02, 0x00, 0x00, 0x00, 0x4c,\
9283 /*  72 */ 0x4c, 0x00, 0x00, 0x00, 0x05, 0x05, 0x15, 0x15,\
9284 /*  80 */ 0x15, 0x15, 0x15, 0x15, 0x00, 0x4c, 0x4c, 0x4c,\
9285 /*  88 */ 0x4c, 0x4c, 0x4c, 0x4c, 0x4c, 0x4c, 0x4c, 0x00,\
9286 /*  96 */ 0x24, 0x02, 0x02, 0x00, 0x01, 0x01, 0x01, 0x01,\
9287 /* 104 */ 0x08, 0x08, 0x00, 0x02, 0x01, 0x01, 0x02, 0x00,\
9288 /* 112 */ 0x02, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,\
9289 /* 120 */ 0x0c, 0x45, 0x15, 0x01, 0x02, 0x00, 0x01, 0x08,\
9290 /* 128 */ 0x05, 0x05, 0x05, 0x00, 0x01, 0x02, 0x00, 0x00,\
9291 /* 136 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x04,\
9292 /* 144 */ 0x04, 0x04, 0x04, 0x04, 0x02, 0x02, 0x00, 0x00,\
9293 /* 152 */ 0x00,}
9294 
9295 /************** End of opcodes.h *********************************************/
9296 /************** Continuing where we left off in vdbe.h ***********************/
9297 
9298 /*
9299 ** Prototypes for the VDBE interface.  See comments on the implementation
9300 ** for a description of what each of these routines does.
9301 */
9302 SQLITE_PRIVATE Vdbe *sqlite3VdbeCreate(Parse*);
9303 SQLITE_PRIVATE int sqlite3VdbeAddOp0(Vdbe*,int);
9304 SQLITE_PRIVATE int sqlite3VdbeAddOp1(Vdbe*,int,int);
9305 SQLITE_PRIVATE int sqlite3VdbeAddOp2(Vdbe*,int,int,int);
9306 SQLITE_PRIVATE int sqlite3VdbeAddOp3(Vdbe*,int,int,int,int);
9307 SQLITE_PRIVATE int sqlite3VdbeAddOp4(Vdbe*,int,int,int,int,const char *zP4,int);
9308 SQLITE_PRIVATE int sqlite3VdbeAddOp4Int(Vdbe*,int,int,int,int,int);
9309 SQLITE_PRIVATE int sqlite3VdbeAddOpList(Vdbe*, int nOp, VdbeOpList const *aOp);
9310 SQLITE_PRIVATE void sqlite3VdbeAddParseSchemaOp(Vdbe*,int,char*);
9311 SQLITE_PRIVATE void sqlite3VdbeChangeP1(Vdbe*, u32 addr, int P1);
9312 SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe*, u32 addr, int P2);
9313 SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe*, u32 addr, int P3);
9314 SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe*, u8 P5);
9315 SQLITE_PRIVATE void sqlite3VdbeJumpHere(Vdbe*, int addr);
9316 SQLITE_PRIVATE void sqlite3VdbeChangeToNoop(Vdbe*, int addr);
9317 SQLITE_PRIVATE int sqlite3VdbeDeletePriorOpcode(Vdbe*, u8 op);
9318 SQLITE_PRIVATE void sqlite3VdbeChangeP4(Vdbe*, int addr, const char *zP4, int N);
9319 SQLITE_PRIVATE void sqlite3VdbeSetP4KeyInfo(Parse*, Index*);
9320 SQLITE_PRIVATE void sqlite3VdbeUsesBtree(Vdbe*, int);
9321 SQLITE_PRIVATE VdbeOp *sqlite3VdbeGetOp(Vdbe*, int);
9322 SQLITE_PRIVATE int sqlite3VdbeMakeLabel(Vdbe*);
9323 SQLITE_PRIVATE void sqlite3VdbeRunOnlyOnce(Vdbe*);
9324 SQLITE_PRIVATE void sqlite3VdbeDelete(Vdbe*);
9325 SQLITE_PRIVATE void sqlite3VdbeClearObject(sqlite3*,Vdbe*);
9326 SQLITE_PRIVATE void sqlite3VdbeMakeReady(Vdbe*,Parse*);
9327 SQLITE_PRIVATE int sqlite3VdbeFinalize(Vdbe*);
9328 SQLITE_PRIVATE void sqlite3VdbeResolveLabel(Vdbe*, int);
9329 SQLITE_PRIVATE int sqlite3VdbeCurrentAddr(Vdbe*);
9330 #ifdef SQLITE_DEBUG
9331 SQLITE_PRIVATE   int sqlite3VdbeAssertMayAbort(Vdbe *, int);
9332 #endif
9333 SQLITE_PRIVATE void sqlite3VdbeResetStepResult(Vdbe*);
9334 SQLITE_PRIVATE void sqlite3VdbeRewind(Vdbe*);
9335 SQLITE_PRIVATE int sqlite3VdbeReset(Vdbe*);
9336 SQLITE_PRIVATE void sqlite3VdbeSetNumCols(Vdbe*,int);
9337 SQLITE_PRIVATE int sqlite3VdbeSetColName(Vdbe*, int, int, const char *, void(*)(void*));
9338 SQLITE_PRIVATE void sqlite3VdbeCountChanges(Vdbe*);
9339 SQLITE_PRIVATE sqlite3 *sqlite3VdbeDb(Vdbe*);
9340 SQLITE_PRIVATE void sqlite3VdbeSetSql(Vdbe*, const char *z, int n, int);
9341 SQLITE_PRIVATE void sqlite3VdbeSwap(Vdbe*,Vdbe*);
9342 SQLITE_PRIVATE VdbeOp *sqlite3VdbeTakeOpArray(Vdbe*, int*, int*);
9343 SQLITE_PRIVATE sqlite3_value *sqlite3VdbeGetBoundValue(Vdbe*, int, u8);
9344 SQLITE_PRIVATE void sqlite3VdbeSetVarmask(Vdbe*, int);
9345 #ifndef SQLITE_OMIT_TRACE
9346 SQLITE_PRIVATE   char *sqlite3VdbeExpandSql(Vdbe*, const char*);
9347 #endif
9348 
9349 SQLITE_PRIVATE void sqlite3VdbeRecordUnpack(KeyInfo*,int,const void*,UnpackedRecord*);
9350 SQLITE_PRIVATE int sqlite3VdbeRecordCompare(int,const void*,UnpackedRecord*);
9351 SQLITE_PRIVATE UnpackedRecord *sqlite3VdbeAllocUnpackedRecord(KeyInfo *, char *, int, char **);
9352 
9353 #ifndef SQLITE_OMIT_TRIGGER
9354 SQLITE_PRIVATE void sqlite3VdbeLinkSubProgram(Vdbe *, SubProgram *);
9355 #endif
9356 
9357 /* Use SQLITE_ENABLE_COMMENTS to enable generation of extra comments on
9358 ** each VDBE opcode.
9359 **
9360 ** Use the SQLITE_ENABLE_MODULE_COMMENTS macro to see some extra no-op
9361 ** comments in VDBE programs that show key decision points in the code
9362 ** generator.
9363 */
9364 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
9365 SQLITE_PRIVATE   void sqlite3VdbeComment(Vdbe*, const char*, ...);
9366 # define VdbeComment(X)  sqlite3VdbeComment X
9367 SQLITE_PRIVATE   void sqlite3VdbeNoopComment(Vdbe*, const char*, ...);
9368 # define VdbeNoopComment(X)  sqlite3VdbeNoopComment X
9369 # ifdef SQLITE_ENABLE_MODULE_COMMENTS
9370 #   define VdbeModuleComment(X)  sqlite3VdbeNoopComment X
9371 # else
9372 #   define VdbeModuleComment(X)
9373 # endif
9374 #else
9375 # define VdbeComment(X)
9376 # define VdbeNoopComment(X)
9377 # define VdbeModuleComment(X)
9378 #endif
9379 
9380 #endif
9381 
9382 /************** End of vdbe.h ************************************************/
9383 /************** Continuing where we left off in sqliteInt.h ******************/
9384 /************** Include pager.h in the middle of sqliteInt.h *****************/
9385 /************** Begin file pager.h *******************************************/
9386 /*
9387 ** 2001 September 15
9388 **
9389 ** The author disclaims copyright to this source code.  In place of
9390 ** a legal notice, here is a blessing:
9391 **
9392 **    May you do good and not evil.
9393 **    May you find forgiveness for yourself and forgive others.
9394 **    May you share freely, never taking more than you give.
9395 **
9396 *************************************************************************
9397 ** This header file defines the interface that the sqlite page cache
9398 ** subsystem.  The page cache subsystem reads and writes a file a page
9399 ** at a time and provides a journal for rollback.
9400 */
9401 
9402 #ifndef _PAGER_H_
9403 #define _PAGER_H_
9404 
9405 /*
9406 ** Default maximum size for persistent journal files. A negative
9407 ** value means no limit. This value may be overridden using the
9408 ** sqlite3PagerJournalSizeLimit() API. See also "PRAGMA journal_size_limit".
9409 */
9410 #ifndef SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT
9411   #define SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT -1
9412 #endif
9413 
9414 /*
9415 ** The type used to represent a page number.  The first page in a file
9416 ** is called page 1.  0 is used to represent "not a page".
9417 */
9418 typedef u32 Pgno;
9419 
9420 /*
9421 ** Each open file is managed by a separate instance of the "Pager" structure.
9422 */
9423 typedef struct Pager Pager;
9424 
9425 /*
9426 ** Handle type for pages.
9427 */
9428 typedef struct PgHdr DbPage;
9429 
9430 /*
9431 ** Page number PAGER_MJ_PGNO is never used in an SQLite database (it is
9432 ** reserved for working around a windows/posix incompatibility). It is
9433 ** used in the journal to signify that the remainder of the journal file
9434 ** is devoted to storing a master journal name - there are no more pages to
9435 ** roll back. See comments for function writeMasterJournal() in pager.c
9436 ** for details.
9437 */
9438 #define PAGER_MJ_PGNO(x) ((Pgno)((PENDING_BYTE/((x)->pageSize))+1))
9439 
9440 /*
9441 ** Allowed values for the flags parameter to sqlite3PagerOpen().
9442 **
9443 ** NOTE: These values must match the corresponding BTREE_ values in btree.h.
9444 */
9445 #define PAGER_OMIT_JOURNAL  0x0001    /* Do not use a rollback journal */
9446 #define PAGER_MEMORY        0x0002    /* In-memory database */
9447 
9448 /*
9449 ** Valid values for the second argument to sqlite3PagerLockingMode().
9450 */
9451 #define PAGER_LOCKINGMODE_QUERY      -1
9452 #define PAGER_LOCKINGMODE_NORMAL      0
9453 #define PAGER_LOCKINGMODE_EXCLUSIVE   1
9454 
9455 /*
9456 ** Numeric constants that encode the journalmode.
9457 */
9458 #define PAGER_JOURNALMODE_QUERY     (-1)  /* Query the value of journalmode */
9459 #define PAGER_JOURNALMODE_DELETE      0   /* Commit by deleting journal file */
9460 #define PAGER_JOURNALMODE_PERSIST     1   /* Commit by zeroing journal header */
9461 #define PAGER_JOURNALMODE_OFF         2   /* Journal omitted.  */
9462 #define PAGER_JOURNALMODE_TRUNCATE    3   /* Commit by truncating journal */
9463 #define PAGER_JOURNALMODE_MEMORY      4   /* In-memory journal file */
9464 #define PAGER_JOURNALMODE_WAL         5   /* Use write-ahead logging */
9465 
9466 /*
9467 ** Flags that make up the mask passed to sqlite3PagerAcquire().
9468 */
9469 #define PAGER_GET_NOCONTENT     0x01  /* Do not load data from disk */
9470 #define PAGER_GET_READONLY      0x02  /* Read-only page is acceptable */
9471 
9472 /*
9473 ** Flags for sqlite3PagerSetFlags()
9474 */
9475 #define PAGER_SYNCHRONOUS_OFF       0x01  /* PRAGMA synchronous=OFF */
9476 #define PAGER_SYNCHRONOUS_NORMAL    0x02  /* PRAGMA synchronous=NORMAL */
9477 #define PAGER_SYNCHRONOUS_FULL      0x03  /* PRAGMA synchronous=FULL */
9478 #define PAGER_SYNCHRONOUS_MASK      0x03  /* Mask for three values above */
9479 #define PAGER_FULLFSYNC             0x04  /* PRAGMA fullfsync=ON */
9480 #define PAGER_CKPT_FULLFSYNC        0x08  /* PRAGMA checkpoint_fullfsync=ON */
9481 #define PAGER_CACHESPILL            0x10  /* PRAGMA cache_spill=ON */
9482 #define PAGER_FLAGS_MASK            0x1c  /* All above except SYNCHRONOUS */
9483 
9484 /*
9485 ** The remainder of this file contains the declarations of the functions
9486 ** that make up the Pager sub-system API. See source code comments for
9487 ** a detailed description of each routine.
9488 */
9489 
9490 /* Open and close a Pager connection. */
9491 SQLITE_PRIVATE int sqlite3PagerOpen(
9492   sqlite3_vfs*,
9493   Pager **ppPager,
9494   const char*,
9495   int,
9496   int,
9497   int,
9498   void(*)(DbPage*)
9499 );
9500 SQLITE_PRIVATE int sqlite3PagerClose(Pager *pPager);
9501 SQLITE_PRIVATE int sqlite3PagerReadFileheader(Pager*, int, unsigned char*);
9502 
9503 /* Functions used to configure a Pager object. */
9504 SQLITE_PRIVATE void sqlite3PagerSetBusyhandler(Pager*, int(*)(void *), void *);
9505 SQLITE_PRIVATE int sqlite3PagerSetPagesize(Pager*, u32*, int);
9506 SQLITE_PRIVATE int sqlite3PagerMaxPageCount(Pager*, int);
9507 SQLITE_PRIVATE void sqlite3PagerSetCachesize(Pager*, int);
9508 SQLITE_PRIVATE void sqlite3PagerSetMmapLimit(Pager *, sqlite3_int64);
9509 SQLITE_PRIVATE void sqlite3PagerShrink(Pager*);
9510 SQLITE_PRIVATE void sqlite3PagerSetFlags(Pager*,unsigned);
9511 SQLITE_PRIVATE int sqlite3PagerLockingMode(Pager *, int);
9512 SQLITE_PRIVATE int sqlite3PagerSetJournalMode(Pager *, int);
9513 SQLITE_PRIVATE int sqlite3PagerGetJournalMode(Pager*);
9514 SQLITE_PRIVATE int sqlite3PagerOkToChangeJournalMode(Pager*);
9515 SQLITE_PRIVATE i64 sqlite3PagerJournalSizeLimit(Pager *, i64);
9516 SQLITE_PRIVATE sqlite3_backup **sqlite3PagerBackupPtr(Pager*);
9517 
9518 /* Functions used to obtain and release page references. */
9519 SQLITE_PRIVATE int sqlite3PagerAcquire(Pager *pPager, Pgno pgno, DbPage **ppPage, int clrFlag);
9520 #define sqlite3PagerGet(A,B,C) sqlite3PagerAcquire(A,B,C,0)
9521 SQLITE_PRIVATE DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno);
9522 SQLITE_PRIVATE void sqlite3PagerRef(DbPage*);
9523 SQLITE_PRIVATE void sqlite3PagerUnref(DbPage*);
9524 SQLITE_PRIVATE void sqlite3PagerUnrefNotNull(DbPage*);
9525 
9526 /* Operations on page references. */
9527 SQLITE_PRIVATE int sqlite3PagerWrite(DbPage*);
9528 SQLITE_PRIVATE void sqlite3PagerDontWrite(DbPage*);
9529 SQLITE_PRIVATE int sqlite3PagerMovepage(Pager*,DbPage*,Pgno,int);
9530 SQLITE_PRIVATE int sqlite3PagerPageRefcount(DbPage*);
9531 SQLITE_PRIVATE void *sqlite3PagerGetData(DbPage *);
9532 SQLITE_PRIVATE void *sqlite3PagerGetExtra(DbPage *);
9533 
9534 /* Functions used to manage pager transactions and savepoints. */
9535 SQLITE_PRIVATE void sqlite3PagerPagecount(Pager*, int*);
9536 SQLITE_PRIVATE int sqlite3PagerBegin(Pager*, int exFlag, int);
9537 SQLITE_PRIVATE int sqlite3PagerCommitPhaseOne(Pager*,const char *zMaster, int);
9538 SQLITE_PRIVATE int sqlite3PagerExclusiveLock(Pager*);
9539 SQLITE_PRIVATE int sqlite3PagerSync(Pager *pPager, const char *zMaster);
9540 SQLITE_PRIVATE int sqlite3PagerCommitPhaseTwo(Pager*);
9541 SQLITE_PRIVATE int sqlite3PagerRollback(Pager*);
9542 SQLITE_PRIVATE int sqlite3PagerOpenSavepoint(Pager *pPager, int n);
9543 SQLITE_PRIVATE int sqlite3PagerSavepoint(Pager *pPager, int op, int iSavepoint);
9544 SQLITE_PRIVATE int sqlite3PagerSharedLock(Pager *pPager);
9545 
9546 #ifndef SQLITE_OMIT_WAL
9547 SQLITE_PRIVATE   int sqlite3PagerCheckpoint(Pager *pPager, int, int*, int*);
9548 SQLITE_PRIVATE   int sqlite3PagerWalSupported(Pager *pPager);
9549 SQLITE_PRIVATE   int sqlite3PagerWalCallback(Pager *pPager);
9550 SQLITE_PRIVATE   int sqlite3PagerOpenWal(Pager *pPager, int *pisOpen);
9551 SQLITE_PRIVATE   int sqlite3PagerCloseWal(Pager *pPager);
9552 #endif
9553 
9554 #ifdef SQLITE_ENABLE_ZIPVFS
9555 SQLITE_PRIVATE   int sqlite3PagerWalFramesize(Pager *pPager);
9556 #endif
9557 
9558 /* Functions used to query pager state and configuration. */
9559 SQLITE_PRIVATE u8 sqlite3PagerIsreadonly(Pager*);
9560 SQLITE_PRIVATE int sqlite3PagerRefcount(Pager*);
9561 SQLITE_PRIVATE int sqlite3PagerMemUsed(Pager*);
9562 SQLITE_PRIVATE const char *sqlite3PagerFilename(Pager*, int);
9563 SQLITE_PRIVATE const sqlite3_vfs *sqlite3PagerVfs(Pager*);
9564 SQLITE_PRIVATE sqlite3_file *sqlite3PagerFile(Pager*);
9565 SQLITE_PRIVATE const char *sqlite3PagerJournalname(Pager*);
9566 SQLITE_PRIVATE int sqlite3PagerNosync(Pager*);
9567 SQLITE_PRIVATE void *sqlite3PagerTempSpace(Pager*);
9568 SQLITE_PRIVATE int sqlite3PagerIsMemdb(Pager*);
9569 SQLITE_PRIVATE void sqlite3PagerCacheStat(Pager *, int, int, int *);
9570 SQLITE_PRIVATE void sqlite3PagerClearCache(Pager *);
9571 SQLITE_PRIVATE int sqlite3SectorSize(sqlite3_file *);
9572 
9573 /* Functions used to truncate the database file. */
9574 SQLITE_PRIVATE void sqlite3PagerTruncateImage(Pager*,Pgno);
9575 
9576 #if defined(SQLITE_HAS_CODEC) && !defined(SQLITE_OMIT_WAL)
9577 SQLITE_PRIVATE void *sqlite3PagerCodec(DbPage *);
9578 #endif
9579 
9580 /* Functions to support testing and debugging. */
9581 #if !defined(NDEBUG) || defined(SQLITE_TEST)
9582 SQLITE_PRIVATE   Pgno sqlite3PagerPagenumber(DbPage*);
9583 SQLITE_PRIVATE   int sqlite3PagerIswriteable(DbPage*);
9584 #endif
9585 #ifdef SQLITE_TEST
9586 SQLITE_PRIVATE   int *sqlite3PagerStats(Pager*);
9587 SQLITE_PRIVATE   void sqlite3PagerRefdump(Pager*);
9588   void disable_simulated_io_errors(void);
9589   void enable_simulated_io_errors(void);
9590 #else
9591 # define disable_simulated_io_errors()
9592 # define enable_simulated_io_errors()
9593 #endif
9594 
9595 #endif /* _PAGER_H_ */
9596 
9597 /************** End of pager.h ***********************************************/
9598 /************** Continuing where we left off in sqliteInt.h ******************/
9599 /************** Include pcache.h in the middle of sqliteInt.h ****************/
9600 /************** Begin file pcache.h ******************************************/
9601 /*
9602 ** 2008 August 05
9603 **
9604 ** The author disclaims copyright to this source code.  In place of
9605 ** a legal notice, here is a blessing:
9606 **
9607 **    May you do good and not evil.
9608 **    May you find forgiveness for yourself and forgive others.
9609 **    May you share freely, never taking more than you give.
9610 **
9611 *************************************************************************
9612 ** This header file defines the interface that the sqlite page cache
9613 ** subsystem.
9614 */
9615 
9616 #ifndef _PCACHE_H_
9617 
9618 typedef struct PgHdr PgHdr;
9619 typedef struct PCache PCache;
9620 
9621 /*
9622 ** Every page in the cache is controlled by an instance of the following
9623 ** structure.
9624 */
9625 struct PgHdr {
9626   sqlite3_pcache_page *pPage;    /* Pcache object page handle */
9627   void *pData;                   /* Page data */
9628   void *pExtra;                  /* Extra content */
9629   PgHdr *pDirty;                 /* Transient list of dirty pages */
9630   Pager *pPager;                 /* The pager this page is part of */
9631   Pgno pgno;                     /* Page number for this page */
9632 #ifdef SQLITE_CHECK_PAGES
9633   u32 pageHash;                  /* Hash of page content */
9634 #endif
9635   u16 flags;                     /* PGHDR flags defined below */
9636 
9637   /**********************************************************************
9638   ** Elements above are public.  All that follows is private to pcache.c
9639   ** and should not be accessed by other modules.
9640   */
9641   i16 nRef;                      /* Number of users of this page */
9642   PCache *pCache;                /* Cache that owns this page */
9643 
9644   PgHdr *pDirtyNext;             /* Next element in list of dirty pages */
9645   PgHdr *pDirtyPrev;             /* Previous element in list of dirty pages */
9646 };
9647 
9648 /* Bit values for PgHdr.flags */
9649 #define PGHDR_DIRTY             0x002  /* Page has changed */
9650 #define PGHDR_NEED_SYNC         0x004  /* Fsync the rollback journal before
9651                                        ** writing this page to the database */
9652 #define PGHDR_NEED_READ         0x008  /* Content is unread */
9653 #define PGHDR_REUSE_UNLIKELY    0x010  /* A hint that reuse is unlikely */
9654 #define PGHDR_DONT_WRITE        0x020  /* Do not write content to disk */
9655 
9656 #define PGHDR_MMAP              0x040  /* This is an mmap page object */
9657 
9658 /* Initialize and shutdown the page cache subsystem */
9659 SQLITE_PRIVATE int sqlite3PcacheInitialize(void);
9660 SQLITE_PRIVATE void sqlite3PcacheShutdown(void);
9661 
9662 /* Page cache buffer management:
9663 ** These routines implement SQLITE_CONFIG_PAGECACHE.
9664 */
9665 SQLITE_PRIVATE void sqlite3PCacheBufferSetup(void *, int sz, int n);
9666 
9667 /* Create a new pager cache.
9668 ** Under memory stress, invoke xStress to try to make pages clean.
9669 ** Only clean and unpinned pages can be reclaimed.
9670 */
9671 SQLITE_PRIVATE void sqlite3PcacheOpen(
9672   int szPage,                    /* Size of every page */
9673   int szExtra,                   /* Extra space associated with each page */
9674   int bPurgeable,                /* True if pages are on backing store */
9675   int (*xStress)(void*, PgHdr*), /* Call to try to make pages clean */
9676   void *pStress,                 /* Argument to xStress */
9677   PCache *pToInit                /* Preallocated space for the PCache */
9678 );
9679 
9680 /* Modify the page-size after the cache has been created. */
9681 SQLITE_PRIVATE void sqlite3PcacheSetPageSize(PCache *, int);
9682 
9683 /* Return the size in bytes of a PCache object.  Used to preallocate
9684 ** storage space.
9685 */
9686 SQLITE_PRIVATE int sqlite3PcacheSize(void);
9687 
9688 /* One release per successful fetch.  Page is pinned until released.
9689 ** Reference counted.
9690 */
9691 SQLITE_PRIVATE int sqlite3PcacheFetch(PCache*, Pgno, int createFlag, PgHdr**);
9692 SQLITE_PRIVATE void sqlite3PcacheRelease(PgHdr*);
9693 
9694 SQLITE_PRIVATE void sqlite3PcacheDrop(PgHdr*);         /* Remove page from cache */
9695 SQLITE_PRIVATE void sqlite3PcacheMakeDirty(PgHdr*);    /* Make sure page is marked dirty */
9696 SQLITE_PRIVATE void sqlite3PcacheMakeClean(PgHdr*);    /* Mark a single page as clean */
9697 SQLITE_PRIVATE void sqlite3PcacheCleanAll(PCache*);    /* Mark all dirty list pages as clean */
9698 
9699 /* Change a page number.  Used by incr-vacuum. */
9700 SQLITE_PRIVATE void sqlite3PcacheMove(PgHdr*, Pgno);
9701 
9702 /* Remove all pages with pgno>x.  Reset the cache if x==0 */
9703 SQLITE_PRIVATE void sqlite3PcacheTruncate(PCache*, Pgno x);
9704 
9705 /* Get a list of all dirty pages in the cache, sorted by page number */
9706 SQLITE_PRIVATE PgHdr *sqlite3PcacheDirtyList(PCache*);
9707 
9708 /* Reset and close the cache object */
9709 SQLITE_PRIVATE void sqlite3PcacheClose(PCache*);
9710 
9711 /* Clear flags from pages of the page cache */
9712 SQLITE_PRIVATE void sqlite3PcacheClearSyncFlags(PCache *);
9713 
9714 /* Discard the contents of the cache */
9715 SQLITE_PRIVATE void sqlite3PcacheClear(PCache*);
9716 
9717 /* Return the total number of outstanding page references */
9718 SQLITE_PRIVATE int sqlite3PcacheRefCount(PCache*);
9719 
9720 /* Increment the reference count of an existing page */
9721 SQLITE_PRIVATE void sqlite3PcacheRef(PgHdr*);
9722 
9723 SQLITE_PRIVATE int sqlite3PcachePageRefcount(PgHdr*);
9724 
9725 /* Return the total number of pages stored in the cache */
9726 SQLITE_PRIVATE int sqlite3PcachePagecount(PCache*);
9727 
9728 #if defined(SQLITE_CHECK_PAGES) || defined(SQLITE_DEBUG)
9729 /* Iterate through all dirty pages currently stored in the cache. This
9730 ** interface is only available if SQLITE_CHECK_PAGES is defined when the
9731 ** library is built.
9732 */
9733 SQLITE_PRIVATE void sqlite3PcacheIterateDirty(PCache *pCache, void (*xIter)(PgHdr *));
9734 #endif
9735 
9736 /* Set and get the suggested cache-size for the specified pager-cache.
9737 **
9738 ** If no global maximum is configured, then the system attempts to limit
9739 ** the total number of pages cached by purgeable pager-caches to the sum
9740 ** of the suggested cache-sizes.
9741 */
9742 SQLITE_PRIVATE void sqlite3PcacheSetCachesize(PCache *, int);
9743 #ifdef SQLITE_TEST
9744 SQLITE_PRIVATE int sqlite3PcacheGetCachesize(PCache *);
9745 #endif
9746 
9747 /* Free up as much memory as possible from the page cache */
9748 SQLITE_PRIVATE void sqlite3PcacheShrink(PCache*);
9749 
9750 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
9751 /* Try to return memory used by the pcache module to the main memory heap */
9752 SQLITE_PRIVATE int sqlite3PcacheReleaseMemory(int);
9753 #endif
9754 
9755 #ifdef SQLITE_TEST
9756 SQLITE_PRIVATE void sqlite3PcacheStats(int*,int*,int*,int*);
9757 #endif
9758 
9759 SQLITE_PRIVATE void sqlite3PCacheSetDefault(void);
9760 
9761 #endif /* _PCACHE_H_ */
9762 
9763 /************** End of pcache.h **********************************************/
9764 /************** Continuing where we left off in sqliteInt.h ******************/
9765 
9766 /************** Include os.h in the middle of sqliteInt.h ********************/
9767 /************** Begin file os.h **********************************************/
9768 /*
9769 ** 2001 September 16
9770 **
9771 ** The author disclaims copyright to this source code.  In place of
9772 ** a legal notice, here is a blessing:
9773 **
9774 **    May you do good and not evil.
9775 **    May you find forgiveness for yourself and forgive others.
9776 **    May you share freely, never taking more than you give.
9777 **
9778 ******************************************************************************
9779 **
9780 ** This header file (together with is companion C source-code file
9781 ** "os.c") attempt to abstract the underlying operating system so that
9782 ** the SQLite library will work on both POSIX and windows systems.
9783 **
9784 ** This header file is #include-ed by sqliteInt.h and thus ends up
9785 ** being included by every source file.
9786 */
9787 #ifndef _SQLITE_OS_H_
9788 #define _SQLITE_OS_H_
9789 
9790 /*
9791 ** Figure out if we are dealing with Unix, Windows, or some other
9792 ** operating system.  After the following block of preprocess macros,
9793 ** all of SQLITE_OS_UNIX, SQLITE_OS_WIN, and SQLITE_OS_OTHER
9794 ** will defined to either 1 or 0.  One of the four will be 1.  The other
9795 ** three will be 0.
9796 */
9797 #if defined(SQLITE_OS_OTHER)
9798 # if SQLITE_OS_OTHER==1
9799 #   undef SQLITE_OS_UNIX
9800 #   define SQLITE_OS_UNIX 0
9801 #   undef SQLITE_OS_WIN
9802 #   define SQLITE_OS_WIN 0
9803 # else
9804 #   undef SQLITE_OS_OTHER
9805 # endif
9806 #endif
9807 #if !defined(SQLITE_OS_UNIX) && !defined(SQLITE_OS_OTHER)
9808 # define SQLITE_OS_OTHER 0
9809 # ifndef SQLITE_OS_WIN
9810 #   if defined(_WIN32) || defined(WIN32) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__BORLANDC__)
9811 #     define SQLITE_OS_WIN 1
9812 #     define SQLITE_OS_UNIX 0
9813 #   else
9814 #     define SQLITE_OS_WIN 0
9815 #     define SQLITE_OS_UNIX 1
9816 #  endif
9817 # else
9818 #  define SQLITE_OS_UNIX 0
9819 # endif
9820 #else
9821 # ifndef SQLITE_OS_WIN
9822 #  define SQLITE_OS_WIN 0
9823 # endif
9824 #endif
9825 
9826 #if SQLITE_OS_WIN
9827 # include <windows.h>
9828 #endif
9829 
9830 /*
9831 ** Determine if we are dealing with Windows NT.
9832 **
9833 ** We ought to be able to determine if we are compiling for win98 or winNT
9834 ** using the _WIN32_WINNT macro as follows:
9835 **
9836 ** #if defined(_WIN32_WINNT)
9837 ** # define SQLITE_OS_WINNT 1
9838 ** #else
9839 ** # define SQLITE_OS_WINNT 0
9840 ** #endif
9841 **
9842 ** However, vs2005 does not set _WIN32_WINNT by default, as it ought to,
9843 ** so the above test does not work.  We'll just assume that everything is
9844 ** winNT unless the programmer explicitly says otherwise by setting
9845 ** SQLITE_OS_WINNT to 0.
9846 */
9847 #if SQLITE_OS_WIN && !defined(SQLITE_OS_WINNT)
9848 # define SQLITE_OS_WINNT 1
9849 #endif
9850 
9851 /*
9852 ** Determine if we are dealing with WindowsCE - which has a much
9853 ** reduced API.
9854 */
9855 #if defined(_WIN32_WCE)
9856 # define SQLITE_OS_WINCE 1
9857 #else
9858 # define SQLITE_OS_WINCE 0
9859 #endif
9860 
9861 /*
9862 ** Determine if we are dealing with WinRT, which provides only a subset of
9863 ** the full Win32 API.
9864 */
9865 #if !defined(SQLITE_OS_WINRT)
9866 # define SQLITE_OS_WINRT 0
9867 #endif
9868 
9869 /* If the SET_FULLSYNC macro is not defined above, then make it
9870 ** a no-op
9871 */
9872 #ifndef SET_FULLSYNC
9873 # define SET_FULLSYNC(x,y)
9874 #endif
9875 
9876 /*
9877 ** The default size of a disk sector
9878 */
9879 #ifndef SQLITE_DEFAULT_SECTOR_SIZE
9880 # define SQLITE_DEFAULT_SECTOR_SIZE 4096
9881 #endif
9882 
9883 /*
9884 ** Temporary files are named starting with this prefix followed by 16 random
9885 ** alphanumeric characters, and no file extension. They are stored in the
9886 ** OS's standard temporary file directory, and are deleted prior to exit.
9887 ** If sqlite is being embedded in another program, you may wish to change the
9888 ** prefix to reflect your program's name, so that if your program exits
9889 ** prematurely, old temporary files can be easily identified. This can be done
9890 ** using -DSQLITE_TEMP_FILE_PREFIX=myprefix_ on the compiler command line.
9891 **
9892 ** 2006-10-31:  The default prefix used to be "sqlite_".  But then
9893 ** Mcafee started using SQLite in their anti-virus product and it
9894 ** started putting files with the "sqlite" name in the c:/temp folder.
9895 ** This annoyed many windows users.  Those users would then do a
9896 ** Google search for "sqlite", find the telephone numbers of the
9897 ** developers and call to wake them up at night and complain.
9898 ** For this reason, the default name prefix is changed to be "sqlite"
9899 ** spelled backwards.  So the temp files are still identified, but
9900 ** anybody smart enough to figure out the code is also likely smart
9901 ** enough to know that calling the developer will not help get rid
9902 ** of the file.
9903 */
9904 #ifndef SQLITE_TEMP_FILE_PREFIX
9905 # define SQLITE_TEMP_FILE_PREFIX "etilqs_"
9906 #endif
9907 
9908 /*
9909 ** The following values may be passed as the second argument to
9910 ** sqlite3OsLock(). The various locks exhibit the following semantics:
9911 **
9912 ** SHARED:    Any number of processes may hold a SHARED lock simultaneously.
9913 ** RESERVED:  A single process may hold a RESERVED lock on a file at
9914 **            any time. Other processes may hold and obtain new SHARED locks.
9915 ** PENDING:   A single process may hold a PENDING lock on a file at
9916 **            any one time. Existing SHARED locks may persist, but no new
9917 **            SHARED locks may be obtained by other processes.
9918 ** EXCLUSIVE: An EXCLUSIVE lock precludes all other locks.
9919 **
9920 ** PENDING_LOCK may not be passed directly to sqlite3OsLock(). Instead, a
9921 ** process that requests an EXCLUSIVE lock may actually obtain a PENDING
9922 ** lock. This can be upgraded to an EXCLUSIVE lock by a subsequent call to
9923 ** sqlite3OsLock().
9924 */
9925 #define NO_LOCK         0
9926 #define SHARED_LOCK     1
9927 #define RESERVED_LOCK   2
9928 #define PENDING_LOCK    3
9929 #define EXCLUSIVE_LOCK  4
9930 
9931 /*
9932 ** File Locking Notes:  (Mostly about windows but also some info for Unix)
9933 **
9934 ** We cannot use LockFileEx() or UnlockFileEx() on Win95/98/ME because
9935 ** those functions are not available.  So we use only LockFile() and
9936 ** UnlockFile().
9937 **
9938 ** LockFile() prevents not just writing but also reading by other processes.
9939 ** A SHARED_LOCK is obtained by locking a single randomly-chosen
9940 ** byte out of a specific range of bytes. The lock byte is obtained at
9941 ** random so two separate readers can probably access the file at the
9942 ** same time, unless they are unlucky and choose the same lock byte.
9943 ** An EXCLUSIVE_LOCK is obtained by locking all bytes in the range.
9944 ** There can only be one writer.  A RESERVED_LOCK is obtained by locking
9945 ** a single byte of the file that is designated as the reserved lock byte.
9946 ** A PENDING_LOCK is obtained by locking a designated byte different from
9947 ** the RESERVED_LOCK byte.
9948 **
9949 ** On WinNT/2K/XP systems, LockFileEx() and UnlockFileEx() are available,
9950 ** which means we can use reader/writer locks.  When reader/writer locks
9951 ** are used, the lock is placed on the same range of bytes that is used
9952 ** for probabilistic locking in Win95/98/ME.  Hence, the locking scheme
9953 ** will support two or more Win95 readers or two or more WinNT readers.
9954 ** But a single Win95 reader will lock out all WinNT readers and a single
9955 ** WinNT reader will lock out all other Win95 readers.
9956 **
9957 ** The following #defines specify the range of bytes used for locking.
9958 ** SHARED_SIZE is the number of bytes available in the pool from which
9959 ** a random byte is selected for a shared lock.  The pool of bytes for
9960 ** shared locks begins at SHARED_FIRST.
9961 **
9962 ** The same locking strategy and
9963 ** byte ranges are used for Unix.  This leaves open the possiblity of having
9964 ** clients on win95, winNT, and unix all talking to the same shared file
9965 ** and all locking correctly.  To do so would require that samba (or whatever
9966 ** tool is being used for file sharing) implements locks correctly between
9967 ** windows and unix.  I'm guessing that isn't likely to happen, but by
9968 ** using the same locking range we are at least open to the possibility.
9969 **
9970 ** Locking in windows is manditory.  For this reason, we cannot store
9971 ** actual data in the bytes used for locking.  The pager never allocates
9972 ** the pages involved in locking therefore.  SHARED_SIZE is selected so
9973 ** that all locks will fit on a single page even at the minimum page size.
9974 ** PENDING_BYTE defines the beginning of the locks.  By default PENDING_BYTE
9975 ** is set high so that we don't have to allocate an unused page except
9976 ** for very large databases.  But one should test the page skipping logic
9977 ** by setting PENDING_BYTE low and running the entire regression suite.
9978 **
9979 ** Changing the value of PENDING_BYTE results in a subtly incompatible
9980 ** file format.  Depending on how it is changed, you might not notice
9981 ** the incompatibility right away, even running a full regression test.
9982 ** The default location of PENDING_BYTE is the first byte past the
9983 ** 1GB boundary.
9984 **
9985 */
9986 #ifdef SQLITE_OMIT_WSD
9987 # define PENDING_BYTE     (0x40000000)
9988 #else
9989 # define PENDING_BYTE      sqlite3PendingByte
9990 #endif
9991 #define RESERVED_BYTE     (PENDING_BYTE+1)
9992 #define SHARED_FIRST      (PENDING_BYTE+2)
9993 #define SHARED_SIZE       510
9994 
9995 /*
9996 ** Wrapper around OS specific sqlite3_os_init() function.
9997 */
9998 SQLITE_PRIVATE int sqlite3OsInit(void);
9999 
10000 /*
10001 ** Functions for accessing sqlite3_file methods
10002 */
10003 SQLITE_PRIVATE int sqlite3OsClose(sqlite3_file*);
10004 SQLITE_PRIVATE int sqlite3OsRead(sqlite3_file*, void*, int amt, i64 offset);
10005 SQLITE_PRIVATE int sqlite3OsWrite(sqlite3_file*, const void*, int amt, i64 offset);
10006 SQLITE_PRIVATE int sqlite3OsTruncate(sqlite3_file*, i64 size);
10007 SQLITE_PRIVATE int sqlite3OsSync(sqlite3_file*, int);
10008 SQLITE_PRIVATE int sqlite3OsFileSize(sqlite3_file*, i64 *pSize);
10009 SQLITE_PRIVATE int sqlite3OsLock(sqlite3_file*, int);
10010 SQLITE_PRIVATE int sqlite3OsUnlock(sqlite3_file*, int);
10011 SQLITE_PRIVATE int sqlite3OsCheckReservedLock(sqlite3_file *id, int *pResOut);
10012 SQLITE_PRIVATE int sqlite3OsFileControl(sqlite3_file*,int,void*);
10013 SQLITE_PRIVATE void sqlite3OsFileControlHint(sqlite3_file*,int,void*);
10014 #define SQLITE_FCNTL_DB_UNCHANGED 0xca093fa0
10015 SQLITE_PRIVATE int sqlite3OsSectorSize(sqlite3_file *id);
10016 SQLITE_PRIVATE int sqlite3OsDeviceCharacteristics(sqlite3_file *id);
10017 SQLITE_PRIVATE int sqlite3OsShmMap(sqlite3_file *,int,int,int,void volatile **);
10018 SQLITE_PRIVATE int sqlite3OsShmLock(sqlite3_file *id, int, int, int);
10019 SQLITE_PRIVATE void sqlite3OsShmBarrier(sqlite3_file *id);
10020 SQLITE_PRIVATE int sqlite3OsShmUnmap(sqlite3_file *id, int);
10021 SQLITE_PRIVATE int sqlite3OsFetch(sqlite3_file *id, i64, int, void **);
10022 SQLITE_PRIVATE int sqlite3OsUnfetch(sqlite3_file *, i64, void *);
10023 
10024 
10025 /*
10026 ** Functions for accessing sqlite3_vfs methods
10027 */
10028 SQLITE_PRIVATE int sqlite3OsOpen(sqlite3_vfs *, const char *, sqlite3_file*, int, int *);
10029 SQLITE_PRIVATE int sqlite3OsDelete(sqlite3_vfs *, const char *, int);
10030 SQLITE_PRIVATE int sqlite3OsAccess(sqlite3_vfs *, const char *, int, int *pResOut);
10031 SQLITE_PRIVATE int sqlite3OsFullPathname(sqlite3_vfs *, const char *, int, char *);
10032 #ifndef SQLITE_OMIT_LOAD_EXTENSION
10033 SQLITE_PRIVATE void *sqlite3OsDlOpen(sqlite3_vfs *, const char *);
10034 SQLITE_PRIVATE void sqlite3OsDlError(sqlite3_vfs *, int, char *);
10035 SQLITE_PRIVATE void (*sqlite3OsDlSym(sqlite3_vfs *, void *, const char *))(void);
10036 SQLITE_PRIVATE void sqlite3OsDlClose(sqlite3_vfs *, void *);
10037 #endif /* SQLITE_OMIT_LOAD_EXTENSION */
10038 SQLITE_PRIVATE int sqlite3OsRandomness(sqlite3_vfs *, int, char *);
10039 SQLITE_PRIVATE int sqlite3OsSleep(sqlite3_vfs *, int);
10040 SQLITE_PRIVATE int sqlite3OsCurrentTimeInt64(sqlite3_vfs *, sqlite3_int64*);
10041 
10042 /*
10043 ** Convenience functions for opening and closing files using
10044 ** sqlite3_malloc() to obtain space for the file-handle structure.
10045 */
10046 SQLITE_PRIVATE int sqlite3OsOpenMalloc(sqlite3_vfs *, const char *, sqlite3_file **, int,int*);
10047 SQLITE_PRIVATE int sqlite3OsCloseFree(sqlite3_file *);
10048 
10049 #endif /* _SQLITE_OS_H_ */
10050 
10051 /************** End of os.h **************************************************/
10052 /************** Continuing where we left off in sqliteInt.h ******************/
10053 /************** Include mutex.h in the middle of sqliteInt.h *****************/
10054 /************** Begin file mutex.h *******************************************/
10055 /*
10056 ** 2007 August 28
10057 **
10058 ** The author disclaims copyright to this source code.  In place of
10059 ** a legal notice, here is a blessing:
10060 **
10061 **    May you do good and not evil.
10062 **    May you find forgiveness for yourself and forgive others.
10063 **    May you share freely, never taking more than you give.
10064 **
10065 *************************************************************************
10066 **
10067 ** This file contains the common header for all mutex implementations.
10068 ** The sqliteInt.h header #includes this file so that it is available
10069 ** to all source files.  We break it out in an effort to keep the code
10070 ** better organized.
10071 **
10072 ** NOTE:  source files should *not* #include this header file directly.
10073 ** Source files should #include the sqliteInt.h file and let that file
10074 ** include this one indirectly.
10075 */
10076 
10077 
10078 /*
10079 ** Figure out what version of the code to use.  The choices are
10080 **
10081 **   SQLITE_MUTEX_OMIT         No mutex logic.  Not even stubs.  The
10082 **                             mutexes implemention cannot be overridden
10083 **                             at start-time.
10084 **
10085 **   SQLITE_MUTEX_NOOP         For single-threaded applications.  No
10086 **                             mutual exclusion is provided.  But this
10087 **                             implementation can be overridden at
10088 **                             start-time.
10089 **
10090 **   SQLITE_MUTEX_PTHREADS     For multi-threaded applications on Unix.
10091 **
10092 **   SQLITE_MUTEX_W32          For multi-threaded applications on Win32.
10093 */
10094 #if !SQLITE_THREADSAFE
10095 # define SQLITE_MUTEX_OMIT
10096 #endif
10097 #if SQLITE_THREADSAFE && !defined(SQLITE_MUTEX_NOOP)
10098 #  if SQLITE_OS_UNIX
10099 #    define SQLITE_MUTEX_PTHREADS
10100 #  elif SQLITE_OS_WIN
10101 #    define SQLITE_MUTEX_W32
10102 #  else
10103 #    define SQLITE_MUTEX_NOOP
10104 #  endif
10105 #endif
10106 
10107 #ifdef SQLITE_MUTEX_OMIT
10108 /*
10109 ** If this is a no-op implementation, implement everything as macros.
10110 */
10111 #define sqlite3_mutex_alloc(X)    ((sqlite3_mutex*)8)
10112 #define sqlite3_mutex_free(X)
10113 #define sqlite3_mutex_enter(X)
10114 #define sqlite3_mutex_try(X)      SQLITE_OK
10115 #define sqlite3_mutex_leave(X)
10116 #define sqlite3_mutex_held(X)     ((void)(X),1)
10117 #define sqlite3_mutex_notheld(X)  ((void)(X),1)
10118 #define sqlite3MutexAlloc(X)      ((sqlite3_mutex*)8)
10119 #define sqlite3MutexInit()        SQLITE_OK
10120 #define sqlite3MutexEnd()
10121 #define MUTEX_LOGIC(X)
10122 #else
10123 #define MUTEX_LOGIC(X)            X
10124 #endif /* defined(SQLITE_MUTEX_OMIT) */
10125 
10126 /************** End of mutex.h ***********************************************/
10127 /************** Continuing where we left off in sqliteInt.h ******************/
10128 
10129 
10130 /*
10131 ** Each database file to be accessed by the system is an instance
10132 ** of the following structure.  There are normally two of these structures
10133 ** in the sqlite.aDb[] array.  aDb[0] is the main database file and
10134 ** aDb[1] is the database file used to hold temporary tables.  Additional
10135 ** databases may be attached.
10136 */
10137 struct Db {
10138   char *zName;         /* Name of this database */
10139   Btree *pBt;          /* The B*Tree structure for this database file */
10140   u8 safety_level;     /* How aggressive at syncing data to disk */
10141   Schema *pSchema;     /* Pointer to database schema (possibly shared) */
10142 };
10143 
10144 /*
10145 ** An instance of the following structure stores a database schema.
10146 **
10147 ** Most Schema objects are associated with a Btree.  The exception is
10148 ** the Schema for the TEMP databaes (sqlite3.aDb[1]) which is free-standing.
10149 ** In shared cache mode, a single Schema object can be shared by multiple
10150 ** Btrees that refer to the same underlying BtShared object.
10151 **
10152 ** Schema objects are automatically deallocated when the last Btree that
10153 ** references them is destroyed.   The TEMP Schema is manually freed by
10154 ** sqlite3_close().
10155 *
10156 ** A thread must be holding a mutex on the corresponding Btree in order
10157 ** to access Schema content.  This implies that the thread must also be
10158 ** holding a mutex on the sqlite3 connection pointer that owns the Btree.
10159 ** For a TEMP Schema, only the connection mutex is required.
10160 */
10161 struct Schema {
10162   int schema_cookie;   /* Database schema version number for this file */
10163   int iGeneration;     /* Generation counter.  Incremented with each change */
10164   Hash tblHash;        /* All tables indexed by name */
10165   Hash idxHash;        /* All (named) indices indexed by name */
10166   Hash trigHash;       /* All triggers indexed by name */
10167   Hash fkeyHash;       /* All foreign keys by referenced table name */
10168   Table *pSeqTab;      /* The sqlite_sequence table used by AUTOINCREMENT */
10169   u8 file_format;      /* Schema format version for this file */
10170   u8 enc;              /* Text encoding used by this database */
10171   u16 flags;           /* Flags associated with this schema */
10172   int cache_size;      /* Number of pages to use in the cache */
10173 };
10174 
10175 /*
10176 ** These macros can be used to test, set, or clear bits in the
10177 ** Db.pSchema->flags field.
10178 */
10179 #define DbHasProperty(D,I,P)     (((D)->aDb[I].pSchema->flags&(P))==(P))
10180 #define DbHasAnyProperty(D,I,P)  (((D)->aDb[I].pSchema->flags&(P))!=0)
10181 #define DbSetProperty(D,I,P)     (D)->aDb[I].pSchema->flags|=(P)
10182 #define DbClearProperty(D,I,P)   (D)->aDb[I].pSchema->flags&=~(P)
10183 
10184 /*
10185 ** Allowed values for the DB.pSchema->flags field.
10186 **
10187 ** The DB_SchemaLoaded flag is set after the database schema has been
10188 ** read into internal hash tables.
10189 **
10190 ** DB_UnresetViews means that one or more views have column names that
10191 ** have been filled out.  If the schema changes, these column names might
10192 ** changes and so the view will need to be reset.
10193 */
10194 #define DB_SchemaLoaded    0x0001  /* The schema has been loaded */
10195 #define DB_UnresetViews    0x0002  /* Some views have defined column names */
10196 #define DB_Empty           0x0004  /* The file is empty (length 0 bytes) */
10197 
10198 /*
10199 ** The number of different kinds of things that can be limited
10200 ** using the sqlite3_limit() interface.
10201 */
10202 #define SQLITE_N_LIMIT (SQLITE_LIMIT_TRIGGER_DEPTH+1)
10203 
10204 /*
10205 ** Lookaside malloc is a set of fixed-size buffers that can be used
10206 ** to satisfy small transient memory allocation requests for objects
10207 ** associated with a particular database connection.  The use of
10208 ** lookaside malloc provides a significant performance enhancement
10209 ** (approx 10%) by avoiding numerous malloc/free requests while parsing
10210 ** SQL statements.
10211 **
10212 ** The Lookaside structure holds configuration information about the
10213 ** lookaside malloc subsystem.  Each available memory allocation in
10214 ** the lookaside subsystem is stored on a linked list of LookasideSlot
10215 ** objects.
10216 **
10217 ** Lookaside allocations are only allowed for objects that are associated
10218 ** with a particular database connection.  Hence, schema information cannot
10219 ** be stored in lookaside because in shared cache mode the schema information
10220 ** is shared by multiple database connections.  Therefore, while parsing
10221 ** schema information, the Lookaside.bEnabled flag is cleared so that
10222 ** lookaside allocations are not used to construct the schema objects.
10223 */
10224 struct Lookaside {
10225   u16 sz;                 /* Size of each buffer in bytes */
10226   u8 bEnabled;            /* False to disable new lookaside allocations */
10227   u8 bMalloced;           /* True if pStart obtained from sqlite3_malloc() */
10228   int nOut;               /* Number of buffers currently checked out */
10229   int mxOut;              /* Highwater mark for nOut */
10230   int anStat[3];          /* 0: hits.  1: size misses.  2: full misses */
10231   LookasideSlot *pFree;   /* List of available buffers */
10232   void *pStart;           /* First byte of available memory space */
10233   void *pEnd;             /* First byte past end of available space */
10234 };
10235 struct LookasideSlot {
10236   LookasideSlot *pNext;    /* Next buffer in the list of free buffers */
10237 };
10238 
10239 /*
10240 ** A hash table for function definitions.
10241 **
10242 ** Hash each FuncDef structure into one of the FuncDefHash.a[] slots.
10243 ** Collisions are on the FuncDef.pHash chain.
10244 */
10245 struct FuncDefHash {
10246   FuncDef *a[23];       /* Hash table for functions */
10247 };
10248 
10249 /*
10250 ** Each database connection is an instance of the following structure.
10251 */
10252 struct sqlite3 {
10253   sqlite3_vfs *pVfs;            /* OS Interface */
10254   struct Vdbe *pVdbe;           /* List of active virtual machines */
10255   CollSeq *pDfltColl;           /* The default collating sequence (BINARY) */
10256   sqlite3_mutex *mutex;         /* Connection mutex */
10257   Db *aDb;                      /* All backends */
10258   int nDb;                      /* Number of backends currently in use */
10259   int flags;                    /* Miscellaneous flags. See below */
10260   i64 lastRowid;                /* ROWID of most recent insert (see above) */
10261   i64 szMmap;                   /* Default mmap_size setting */
10262   unsigned int openFlags;       /* Flags passed to sqlite3_vfs.xOpen() */
10263   int errCode;                  /* Most recent error code (SQLITE_*) */
10264   int errMask;                  /* & result codes with this before returning */
10265   u16 dbOptFlags;               /* Flags to enable/disable optimizations */
10266   u8 autoCommit;                /* The auto-commit flag. */
10267   u8 temp_store;                /* 1: file 2: memory 0: default */
10268   u8 mallocFailed;              /* True if we have seen a malloc failure */
10269   u8 dfltLockMode;              /* Default locking-mode for attached dbs */
10270   signed char nextAutovac;      /* Autovac setting after VACUUM if >=0 */
10271   u8 suppressErr;               /* Do not issue error messages if true */
10272   u8 vtabOnConflict;            /* Value to return for s3_vtab_on_conflict() */
10273   u8 isTransactionSavepoint;    /* True if the outermost savepoint is a TS */
10274   int nextPagesize;             /* Pagesize after VACUUM if >0 */
10275   u32 magic;                    /* Magic number for detect library misuse */
10276   int nChange;                  /* Value returned by sqlite3_changes() */
10277   int nTotalChange;             /* Value returned by sqlite3_total_changes() */
10278   int aLimit[SQLITE_N_LIMIT];   /* Limits */
10279   struct sqlite3InitInfo {      /* Information used during initialization */
10280     int newTnum;                /* Rootpage of table being initialized */
10281     u8 iDb;                     /* Which db file is being initialized */
10282     u8 busy;                    /* TRUE if currently initializing */
10283     u8 orphanTrigger;           /* Last statement is orphaned TEMP trigger */
10284   } init;
10285   int nVdbeActive;              /* Number of VDBEs currently running */
10286   int nVdbeRead;                /* Number of active VDBEs that read or write */
10287   int nVdbeWrite;               /* Number of active VDBEs that read and write */
10288   int nVdbeExec;                /* Number of nested calls to VdbeExec() */
10289   int nExtension;               /* Number of loaded extensions */
10290   void **aExtension;            /* Array of shared library handles */
10291   void (*xTrace)(void*,const char*);        /* Trace function */
10292   void *pTraceArg;                          /* Argument to the trace function */
10293   void (*xProfile)(void*,const char*,u64);  /* Profiling function */
10294   void *pProfileArg;                        /* Argument to profile function */
10295   void *pCommitArg;                 /* Argument to xCommitCallback() */
10296   int (*xCommitCallback)(void*);    /* Invoked at every commit. */
10297   void *pRollbackArg;               /* Argument to xRollbackCallback() */
10298   void (*xRollbackCallback)(void*); /* Invoked at every commit. */
10299   void *pUpdateArg;
10300   void (*xUpdateCallback)(void*,int, const char*,const char*,sqlite_int64);
10301 #ifndef SQLITE_OMIT_WAL
10302   int (*xWalCallback)(void *, sqlite3 *, const char *, int);
10303   void *pWalArg;
10304 #endif
10305   void(*xCollNeeded)(void*,sqlite3*,int eTextRep,const char*);
10306   void(*xCollNeeded16)(void*,sqlite3*,int eTextRep,const void*);
10307   void *pCollNeededArg;
10308   sqlite3_value *pErr;          /* Most recent error message */
10309   union {
10310     volatile int isInterrupted; /* True if sqlite3_interrupt has been called */
10311     double notUsed1;            /* Spacer */
10312   } u1;
10313   Lookaside lookaside;          /* Lookaside malloc configuration */
10314 #ifndef SQLITE_OMIT_AUTHORIZATION
10315   int (*xAuth)(void*,int,const char*,const char*,const char*,const char*);
10316                                 /* Access authorization function */
10317   void *pAuthArg;               /* 1st argument to the access auth function */
10318 #endif
10319 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
10320   int (*xProgress)(void *);     /* The progress callback */
10321   void *pProgressArg;           /* Argument to the progress callback */
10322   unsigned nProgressOps;        /* Number of opcodes for progress callback */
10323 #endif
10324 #ifndef SQLITE_OMIT_VIRTUALTABLE
10325   int nVTrans;                  /* Allocated size of aVTrans */
10326   Hash aModule;                 /* populated by sqlite3_create_module() */
10327   VtabCtx *pVtabCtx;            /* Context for active vtab connect/create */
10328   VTable **aVTrans;             /* Virtual tables with open transactions */
10329   VTable *pDisconnect;    /* Disconnect these in next sqlite3_prepare() */
10330 #endif
10331   FuncDefHash aFunc;            /* Hash table of connection functions */
10332   Hash aCollSeq;                /* All collating sequences */
10333   BusyHandler busyHandler;      /* Busy callback */
10334   Db aDbStatic[2];              /* Static space for the 2 default backends */
10335   Savepoint *pSavepoint;        /* List of active savepoints */
10336   int busyTimeout;              /* Busy handler timeout, in msec */
10337   int nSavepoint;               /* Number of non-transaction savepoints */
10338   int nStatement;               /* Number of nested statement-transactions  */
10339   i64 nDeferredCons;            /* Net deferred constraints this transaction. */
10340   i64 nDeferredImmCons;         /* Net deferred immediate constraints */
10341   int *pnBytesFreed;            /* If not NULL, increment this in DbFree() */
10342 
10343 #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
10344   /* The following variables are all protected by the STATIC_MASTER
10345   ** mutex, not by sqlite3.mutex. They are used by code in notify.c.
10346   **
10347   ** When X.pUnlockConnection==Y, that means that X is waiting for Y to
10348   ** unlock so that it can proceed.
10349   **
10350   ** When X.pBlockingConnection==Y, that means that something that X tried
10351   ** tried to do recently failed with an SQLITE_LOCKED error due to locks
10352   ** held by Y.
10353   */
10354   sqlite3 *pBlockingConnection; /* Connection that caused SQLITE_LOCKED */
10355   sqlite3 *pUnlockConnection;           /* Connection to watch for unlock */
10356   void *pUnlockArg;                     /* Argument to xUnlockNotify */
10357   void (*xUnlockNotify)(void **, int);  /* Unlock notify callback */
10358   sqlite3 *pNextBlocked;        /* Next in list of all blocked connections */
10359 #endif
10360 };
10361 
10362 /*
10363 ** A macro to discover the encoding of a database.
10364 */
10365 #define ENC(db) ((db)->aDb[0].pSchema->enc)
10366 
10367 /*
10368 ** Possible values for the sqlite3.flags.
10369 */
10370 #define SQLITE_VdbeTrace      0x00000001  /* True to trace VDBE execution */
10371 #define SQLITE_InternChanges  0x00000002  /* Uncommitted Hash table changes */
10372 #define SQLITE_FullFSync      0x00000004  /* Use full fsync on the backend */
10373 #define SQLITE_CkptFullFSync  0x00000008  /* Use full fsync for checkpoint */
10374 #define SQLITE_CacheSpill     0x00000010  /* OK to spill pager cache */
10375 #define SQLITE_FullColNames   0x00000020  /* Show full column names on SELECT */
10376 #define SQLITE_ShortColNames  0x00000040  /* Show short columns names */
10377 #define SQLITE_CountRows      0x00000080  /* Count rows changed by INSERT, */
10378                                           /*   DELETE, or UPDATE and return */
10379                                           /*   the count using a callback. */
10380 #define SQLITE_NullCallback   0x00000100  /* Invoke the callback once if the */
10381                                           /*   result set is empty */
10382 #define SQLITE_SqlTrace       0x00000200  /* Debug print SQL as it executes */
10383 #define SQLITE_VdbeListing    0x00000400  /* Debug listings of VDBE programs */
10384 #define SQLITE_WriteSchema    0x00000800  /* OK to update SQLITE_MASTER */
10385 #define SQLITE_VdbeAddopTrace 0x00001000  /* Trace sqlite3VdbeAddOp() calls */
10386 #define SQLITE_IgnoreChecks   0x00002000  /* Do not enforce check constraints */
10387 #define SQLITE_ReadUncommitted 0x0004000  /* For shared-cache mode */
10388 #define SQLITE_LegacyFileFmt  0x00008000  /* Create new databases in format 1 */
10389 #define SQLITE_RecoveryMode   0x00010000  /* Ignore schema errors */
10390 #define SQLITE_ReverseOrder   0x00020000  /* Reverse unordered SELECTs */
10391 #define SQLITE_RecTriggers    0x00040000  /* Enable recursive triggers */
10392 #define SQLITE_ForeignKeys    0x00080000  /* Enforce foreign key constraints  */
10393 #define SQLITE_AutoIndex      0x00100000  /* Enable automatic indexes */
10394 #define SQLITE_PreferBuiltin  0x00200000  /* Preference to built-in funcs */
10395 #define SQLITE_LoadExtension  0x00400000  /* Enable load_extension */
10396 #define SQLITE_EnableTrigger  0x00800000  /* True to enable triggers */
10397 #define SQLITE_DeferFKs       0x01000000  /* Defer all FK constraints */
10398 #define SQLITE_QueryOnly      0x02000000  /* Disable database changes */
10399 #define SQLITE_VdbeEQP        0x04000000  /* Debug EXPLAIN QUERY PLAN */
10400 
10401 
10402 /*
10403 ** Bits of the sqlite3.dbOptFlags field that are used by the
10404 ** sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS,...) interface to
10405 ** selectively disable various optimizations.
10406 */
10407 #define SQLITE_QueryFlattener 0x0001   /* Query flattening */
10408 #define SQLITE_ColumnCache    0x0002   /* Column cache */
10409 #define SQLITE_GroupByOrder   0x0004   /* GROUPBY cover of ORDERBY */
10410 #define SQLITE_FactorOutConst 0x0008   /* Constant factoring */
10411 /*                not used    0x0010   // Was: SQLITE_IdxRealAsInt */
10412 #define SQLITE_DistinctOpt    0x0020   /* DISTINCT using indexes */
10413 #define SQLITE_CoverIdxScan   0x0040   /* Covering index scans */
10414 #define SQLITE_OrderByIdxJoin 0x0080   /* ORDER BY of joins via index */
10415 #define SQLITE_SubqCoroutine  0x0100   /* Evaluate subqueries as coroutines */
10416 #define SQLITE_Transitive     0x0200   /* Transitive constraints */
10417 #define SQLITE_OmitNoopJoin   0x0400   /* Omit unused tables in joins */
10418 #define SQLITE_Stat3          0x0800   /* Use the SQLITE_STAT3 table */
10419 #define SQLITE_AdjustOutEst   0x1000   /* Adjust output estimates using WHERE */
10420 #define SQLITE_AllOpts        0xffff   /* All optimizations */
10421 
10422 /*
10423 ** Macros for testing whether or not optimizations are enabled or disabled.
10424 */
10425 #ifndef SQLITE_OMIT_BUILTIN_TEST
10426 #define OptimizationDisabled(db, mask)  (((db)->dbOptFlags&(mask))!=0)
10427 #define OptimizationEnabled(db, mask)   (((db)->dbOptFlags&(mask))==0)
10428 #else
10429 #define OptimizationDisabled(db, mask)  0
10430 #define OptimizationEnabled(db, mask)   1
10431 #endif
10432 
10433 /*
10434 ** Return true if it OK to factor constant expressions into the initialization
10435 ** code. The argument is a Parse object for the code generator.
10436 */
10437 #define ConstFactorOk(P) \
10438   ((P)->cookieGoto>0 && OptimizationEnabled((P)->db,SQLITE_FactorOutConst))
10439 
10440 /*
10441 ** Possible values for the sqlite.magic field.
10442 ** The numbers are obtained at random and have no special meaning, other
10443 ** than being distinct from one another.
10444 */
10445 #define SQLITE_MAGIC_OPEN     0xa029a697  /* Database is open */
10446 #define SQLITE_MAGIC_CLOSED   0x9f3c2d33  /* Database is closed */
10447 #define SQLITE_MAGIC_SICK     0x4b771290  /* Error and awaiting close */
10448 #define SQLITE_MAGIC_BUSY     0xf03b7906  /* Database currently in use */
10449 #define SQLITE_MAGIC_ERROR    0xb5357930  /* An SQLITE_MISUSE error occurred */
10450 #define SQLITE_MAGIC_ZOMBIE   0x64cffc7f  /* Close with last statement close */
10451 
10452 /*
10453 ** Each SQL function is defined by an instance of the following
10454 ** structure.  A pointer to this structure is stored in the sqlite.aFunc
10455 ** hash table.  When multiple functions have the same name, the hash table
10456 ** points to a linked list of these structures.
10457 */
10458 struct FuncDef {
10459   i16 nArg;            /* Number of arguments.  -1 means unlimited */
10460   u16 funcFlags;       /* Some combination of SQLITE_FUNC_* */
10461   void *pUserData;     /* User data parameter */
10462   FuncDef *pNext;      /* Next function with same name */
10463   void (*xFunc)(sqlite3_context*,int,sqlite3_value**); /* Regular function */
10464   void (*xStep)(sqlite3_context*,int,sqlite3_value**); /* Aggregate step */
10465   void (*xFinalize)(sqlite3_context*);                /* Aggregate finalizer */
10466   char *zName;         /* SQL name of the function. */
10467   FuncDef *pHash;      /* Next with a different name but the same hash */
10468   FuncDestructor *pDestructor;   /* Reference counted destructor function */
10469 };
10470 
10471 /*
10472 ** This structure encapsulates a user-function destructor callback (as
10473 ** configured using create_function_v2()) and a reference counter. When
10474 ** create_function_v2() is called to create a function with a destructor,
10475 ** a single object of this type is allocated. FuncDestructor.nRef is set to
10476 ** the number of FuncDef objects created (either 1 or 3, depending on whether
10477 ** or not the specified encoding is SQLITE_ANY). The FuncDef.pDestructor
10478 ** member of each of the new FuncDef objects is set to point to the allocated
10479 ** FuncDestructor.
10480 **
10481 ** Thereafter, when one of the FuncDef objects is deleted, the reference
10482 ** count on this object is decremented. When it reaches 0, the destructor
10483 ** is invoked and the FuncDestructor structure freed.
10484 */
10485 struct FuncDestructor {
10486   int nRef;
10487   void (*xDestroy)(void *);
10488   void *pUserData;
10489 };
10490 
10491 /*
10492 ** Possible values for FuncDef.flags.  Note that the _LENGTH and _TYPEOF
10493 ** values must correspond to OPFLAG_LENGTHARG and OPFLAG_TYPEOFARG.  There
10494 ** are assert() statements in the code to verify this.
10495 */
10496 #define SQLITE_FUNC_ENCMASK  0x003 /* SQLITE_UTF8, SQLITE_UTF16BE or UTF16LE */
10497 #define SQLITE_FUNC_LIKE     0x004 /* Candidate for the LIKE optimization */
10498 #define SQLITE_FUNC_CASE     0x008 /* Case-sensitive LIKE-type function */
10499 #define SQLITE_FUNC_EPHEM    0x010 /* Ephemeral.  Delete with VDBE */
10500 #define SQLITE_FUNC_NEEDCOLL 0x020 /* sqlite3GetFuncCollSeq() might be called */
10501 #define SQLITE_FUNC_LENGTH   0x040 /* Built-in length() function */
10502 #define SQLITE_FUNC_TYPEOF   0x080 /* Built-in typeof() function */
10503 #define SQLITE_FUNC_COUNT    0x100 /* Built-in count(*) aggregate */
10504 #define SQLITE_FUNC_COALESCE 0x200 /* Built-in coalesce() or ifnull() */
10505 #define SQLITE_FUNC_UNLIKELY 0x400 /* Built-in unlikely() function */
10506 #define SQLITE_FUNC_CONSTANT 0x800 /* Constant inputs give a constant output */
10507 
10508 /*
10509 ** The following three macros, FUNCTION(), LIKEFUNC() and AGGREGATE() are
10510 ** used to create the initializers for the FuncDef structures.
10511 **
10512 **   FUNCTION(zName, nArg, iArg, bNC, xFunc)
10513 **     Used to create a scalar function definition of a function zName
10514 **     implemented by C function xFunc that accepts nArg arguments. The
10515 **     value passed as iArg is cast to a (void*) and made available
10516 **     as the user-data (sqlite3_user_data()) for the function. If
10517 **     argument bNC is true, then the SQLITE_FUNC_NEEDCOLL flag is set.
10518 **
10519 **   VFUNCTION(zName, nArg, iArg, bNC, xFunc)
10520 **     Like FUNCTION except it omits the SQLITE_FUNC_CONSTANT flag.
10521 **
10522 **   AGGREGATE(zName, nArg, iArg, bNC, xStep, xFinal)
10523 **     Used to create an aggregate function definition implemented by
10524 **     the C functions xStep and xFinal. The first four parameters
10525 **     are interpreted in the same way as the first 4 parameters to
10526 **     FUNCTION().
10527 **
10528 **   LIKEFUNC(zName, nArg, pArg, flags)
10529 **     Used to create a scalar function definition of a function zName
10530 **     that accepts nArg arguments and is implemented by a call to C
10531 **     function likeFunc. Argument pArg is cast to a (void *) and made
10532 **     available as the function user-data (sqlite3_user_data()). The
10533 **     FuncDef.flags variable is set to the value passed as the flags
10534 **     parameter.
10535 */
10536 #define FUNCTION(zName, nArg, iArg, bNC, xFunc) \
10537   {nArg, SQLITE_FUNC_CONSTANT|SQLITE_UTF8|(bNC*SQLITE_FUNC_NEEDCOLL), \
10538    SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, 0, #zName, 0, 0}
10539 #define VFUNCTION(zName, nArg, iArg, bNC, xFunc) \
10540   {nArg, SQLITE_UTF8|(bNC*SQLITE_FUNC_NEEDCOLL), \
10541    SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, 0, #zName, 0, 0}
10542 #define FUNCTION2(zName, nArg, iArg, bNC, xFunc, extraFlags) \
10543   {nArg,SQLITE_FUNC_CONSTANT|SQLITE_UTF8|(bNC*SQLITE_FUNC_NEEDCOLL)|extraFlags,\
10544    SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, 0, #zName, 0, 0}
10545 #define STR_FUNCTION(zName, nArg, pArg, bNC, xFunc) \
10546   {nArg, SQLITE_FUNC_CONSTANT|SQLITE_UTF8|(bNC*SQLITE_FUNC_NEEDCOLL), \
10547    pArg, 0, xFunc, 0, 0, #zName, 0, 0}
10548 #define LIKEFUNC(zName, nArg, arg, flags) \
10549   {nArg, SQLITE_FUNC_CONSTANT|SQLITE_UTF8|flags, \
10550    (void *)arg, 0, likeFunc, 0, 0, #zName, 0, 0}
10551 #define AGGREGATE(zName, nArg, arg, nc, xStep, xFinal) \
10552   {nArg, SQLITE_UTF8|(nc*SQLITE_FUNC_NEEDCOLL), \
10553    SQLITE_INT_TO_PTR(arg), 0, 0, xStep,xFinal,#zName,0,0}
10554 
10555 /*
10556 ** All current savepoints are stored in a linked list starting at
10557 ** sqlite3.pSavepoint. The first element in the list is the most recently
10558 ** opened savepoint. Savepoints are added to the list by the vdbe
10559 ** OP_Savepoint instruction.
10560 */
10561 struct Savepoint {
10562   char *zName;                        /* Savepoint name (nul-terminated) */
10563   i64 nDeferredCons;                  /* Number of deferred fk violations */
10564   i64 nDeferredImmCons;               /* Number of deferred imm fk. */
10565   Savepoint *pNext;                   /* Parent savepoint (if any) */
10566 };
10567 
10568 /*
10569 ** The following are used as the second parameter to sqlite3Savepoint(),
10570 ** and as the P1 argument to the OP_Savepoint instruction.
10571 */
10572 #define SAVEPOINT_BEGIN      0
10573 #define SAVEPOINT_RELEASE    1
10574 #define SAVEPOINT_ROLLBACK   2
10575 
10576 
10577 /*
10578 ** Each SQLite module (virtual table definition) is defined by an
10579 ** instance of the following structure, stored in the sqlite3.aModule
10580 ** hash table.
10581 */
10582 struct Module {
10583   const sqlite3_module *pModule;       /* Callback pointers */
10584   const char *zName;                   /* Name passed to create_module() */
10585   void *pAux;                          /* pAux passed to create_module() */
10586   void (*xDestroy)(void *);            /* Module destructor function */
10587 };
10588 
10589 /*
10590 ** information about each column of an SQL table is held in an instance
10591 ** of this structure.
10592 */
10593 struct Column {
10594   char *zName;     /* Name of this column */
10595   Expr *pDflt;     /* Default value of this column */
10596   char *zDflt;     /* Original text of the default value */
10597   char *zType;     /* Data type for this column */
10598   char *zColl;     /* Collating sequence.  If NULL, use the default */
10599   u8 notNull;      /* An OE_ code for handling a NOT NULL constraint */
10600   char affinity;   /* One of the SQLITE_AFF_... values */
10601   u8 szEst;        /* Estimated size of this column.  INT==1 */
10602   u8 colFlags;     /* Boolean properties.  See COLFLAG_ defines below */
10603 };
10604 
10605 /* Allowed values for Column.colFlags:
10606 */
10607 #define COLFLAG_PRIMKEY  0x0001    /* Column is part of the primary key */
10608 #define COLFLAG_HIDDEN   0x0002    /* A hidden column in a virtual table */
10609 
10610 /*
10611 ** A "Collating Sequence" is defined by an instance of the following
10612 ** structure. Conceptually, a collating sequence consists of a name and
10613 ** a comparison routine that defines the order of that sequence.
10614 **
10615 ** If CollSeq.xCmp is NULL, it means that the
10616 ** collating sequence is undefined.  Indices built on an undefined
10617 ** collating sequence may not be read or written.
10618 */
10619 struct CollSeq {
10620   char *zName;          /* Name of the collating sequence, UTF-8 encoded */
10621   u8 enc;               /* Text encoding handled by xCmp() */
10622   void *pUser;          /* First argument to xCmp() */
10623   int (*xCmp)(void*,int, const void*, int, const void*);
10624   void (*xDel)(void*);  /* Destructor for pUser */
10625 };
10626 
10627 /*
10628 ** A sort order can be either ASC or DESC.
10629 */
10630 #define SQLITE_SO_ASC       0  /* Sort in ascending order */
10631 #define SQLITE_SO_DESC      1  /* Sort in ascending order */
10632 
10633 /*
10634 ** Column affinity types.
10635 **
10636 ** These used to have mnemonic name like 'i' for SQLITE_AFF_INTEGER and
10637 ** 't' for SQLITE_AFF_TEXT.  But we can save a little space and improve
10638 ** the speed a little by numbering the values consecutively.
10639 **
10640 ** But rather than start with 0 or 1, we begin with 'a'.  That way,
10641 ** when multiple affinity types are concatenated into a string and
10642 ** used as the P4 operand, they will be more readable.
10643 **
10644 ** Note also that the numeric types are grouped together so that testing
10645 ** for a numeric type is a single comparison.
10646 */
10647 #define SQLITE_AFF_TEXT     'a'
10648 #define SQLITE_AFF_NONE     'b'
10649 #define SQLITE_AFF_NUMERIC  'c'
10650 #define SQLITE_AFF_INTEGER  'd'
10651 #define SQLITE_AFF_REAL     'e'
10652 
10653 #define sqlite3IsNumericAffinity(X)  ((X)>=SQLITE_AFF_NUMERIC)
10654 
10655 /*
10656 ** The SQLITE_AFF_MASK values masks off the significant bits of an
10657 ** affinity value.
10658 */
10659 #define SQLITE_AFF_MASK     0x67
10660 
10661 /*
10662 ** Additional bit values that can be ORed with an affinity without
10663 ** changing the affinity.
10664 */
10665 #define SQLITE_JUMPIFNULL   0x08  /* jumps if either operand is NULL */
10666 #define SQLITE_STOREP2      0x10  /* Store result in reg[P2] rather than jump */
10667 #define SQLITE_NULLEQ       0x80  /* NULL=NULL */
10668 
10669 /*
10670 ** An object of this type is created for each virtual table present in
10671 ** the database schema.
10672 **
10673 ** If the database schema is shared, then there is one instance of this
10674 ** structure for each database connection (sqlite3*) that uses the shared
10675 ** schema. This is because each database connection requires its own unique
10676 ** instance of the sqlite3_vtab* handle used to access the virtual table
10677 ** implementation. sqlite3_vtab* handles can not be shared between
10678 ** database connections, even when the rest of the in-memory database
10679 ** schema is shared, as the implementation often stores the database
10680 ** connection handle passed to it via the xConnect() or xCreate() method
10681 ** during initialization internally. This database connection handle may
10682 ** then be used by the virtual table implementation to access real tables
10683 ** within the database. So that they appear as part of the callers
10684 ** transaction, these accesses need to be made via the same database
10685 ** connection as that used to execute SQL operations on the virtual table.
10686 **
10687 ** All VTable objects that correspond to a single table in a shared
10688 ** database schema are initially stored in a linked-list pointed to by
10689 ** the Table.pVTable member variable of the corresponding Table object.
10690 ** When an sqlite3_prepare() operation is required to access the virtual
10691 ** table, it searches the list for the VTable that corresponds to the
10692 ** database connection doing the preparing so as to use the correct
10693 ** sqlite3_vtab* handle in the compiled query.
10694 **
10695 ** When an in-memory Table object is deleted (for example when the
10696 ** schema is being reloaded for some reason), the VTable objects are not
10697 ** deleted and the sqlite3_vtab* handles are not xDisconnect()ed
10698 ** immediately. Instead, they are moved from the Table.pVTable list to
10699 ** another linked list headed by the sqlite3.pDisconnect member of the
10700 ** corresponding sqlite3 structure. They are then deleted/xDisconnected
10701 ** next time a statement is prepared using said sqlite3*. This is done
10702 ** to avoid deadlock issues involving multiple sqlite3.mutex mutexes.
10703 ** Refer to comments above function sqlite3VtabUnlockList() for an
10704 ** explanation as to why it is safe to add an entry to an sqlite3.pDisconnect
10705 ** list without holding the corresponding sqlite3.mutex mutex.
10706 **
10707 ** The memory for objects of this type is always allocated by
10708 ** sqlite3DbMalloc(), using the connection handle stored in VTable.db as
10709 ** the first argument.
10710 */
10711 struct VTable {
10712   sqlite3 *db;              /* Database connection associated with this table */
10713   Module *pMod;             /* Pointer to module implementation */
10714   sqlite3_vtab *pVtab;      /* Pointer to vtab instance */
10715   int nRef;                 /* Number of pointers to this structure */
10716   u8 bConstraint;           /* True if constraints are supported */
10717   int iSavepoint;           /* Depth of the SAVEPOINT stack */
10718   VTable *pNext;            /* Next in linked list (see above) */
10719 };
10720 
10721 /*
10722 ** Each SQL table is represented in memory by an instance of the
10723 ** following structure.
10724 **
10725 ** Table.zName is the name of the table.  The case of the original
10726 ** CREATE TABLE statement is stored, but case is not significant for
10727 ** comparisons.
10728 **
10729 ** Table.nCol is the number of columns in this table.  Table.aCol is a
10730 ** pointer to an array of Column structures, one for each column.
10731 **
10732 ** If the table has an INTEGER PRIMARY KEY, then Table.iPKey is the index of
10733 ** the column that is that key.   Otherwise Table.iPKey is negative.  Note
10734 ** that the datatype of the PRIMARY KEY must be INTEGER for this field to
10735 ** be set.  An INTEGER PRIMARY KEY is used as the rowid for each row of
10736 ** the table.  If a table has no INTEGER PRIMARY KEY, then a random rowid
10737 ** is generated for each row of the table.  TF_HasPrimaryKey is set if
10738 ** the table has any PRIMARY KEY, INTEGER or otherwise.
10739 **
10740 ** Table.tnum is the page number for the root BTree page of the table in the
10741 ** database file.  If Table.iDb is the index of the database table backend
10742 ** in sqlite.aDb[].  0 is for the main database and 1 is for the file that
10743 ** holds temporary tables and indices.  If TF_Ephemeral is set
10744 ** then the table is stored in a file that is automatically deleted
10745 ** when the VDBE cursor to the table is closed.  In this case Table.tnum
10746 ** refers VDBE cursor number that holds the table open, not to the root
10747 ** page number.  Transient tables are used to hold the results of a
10748 ** sub-query that appears instead of a real table name in the FROM clause
10749 ** of a SELECT statement.
10750 */
10751 struct Table {
10752   char *zName;         /* Name of the table or view */
10753   Column *aCol;        /* Information about each column */
10754   Index *pIndex;       /* List of SQL indexes on this table. */
10755   Select *pSelect;     /* NULL for tables.  Points to definition if a view. */
10756   FKey *pFKey;         /* Linked list of all foreign keys in this table */
10757   char *zColAff;       /* String defining the affinity of each column */
10758 #ifndef SQLITE_OMIT_CHECK
10759   ExprList *pCheck;    /* All CHECK constraints */
10760 #endif
10761   tRowcnt nRowEst;     /* Estimated rows in table - from sqlite_stat1 table */
10762   int tnum;            /* Root BTree node for this table (see note above) */
10763   i16 iPKey;           /* If not negative, use aCol[iPKey] as the primary key */
10764   i16 nCol;            /* Number of columns in this table */
10765   u16 nRef;            /* Number of pointers to this Table */
10766   LogEst szTabRow;     /* Estimated size of each table row in bytes */
10767   u8 tabFlags;         /* Mask of TF_* values */
10768   u8 keyConf;          /* What to do in case of uniqueness conflict on iPKey */
10769 #ifndef SQLITE_OMIT_ALTERTABLE
10770   int addColOffset;    /* Offset in CREATE TABLE stmt to add a new column */
10771 #endif
10772 #ifndef SQLITE_OMIT_VIRTUALTABLE
10773   int nModuleArg;      /* Number of arguments to the module */
10774   char **azModuleArg;  /* Text of all module args. [0] is module name */
10775   VTable *pVTable;     /* List of VTable objects. */
10776 #endif
10777   Trigger *pTrigger;   /* List of triggers stored in pSchema */
10778   Schema *pSchema;     /* Schema that contains this table */
10779   Table *pNextZombie;  /* Next on the Parse.pZombieTab list */
10780 };
10781 
10782 /*
10783 ** Allowed values for Table.tabFlags.
10784 */
10785 #define TF_Readonly        0x01    /* Read-only system table */
10786 #define TF_Ephemeral       0x02    /* An ephemeral table */
10787 #define TF_HasPrimaryKey   0x04    /* Table has a primary key */
10788 #define TF_Autoincrement   0x08    /* Integer primary key is autoincrement */
10789 #define TF_Virtual         0x10    /* Is a virtual table */
10790 #define TF_WithoutRowid    0x20    /* No rowid used. PRIMARY KEY is the key */
10791 
10792 
10793 /*
10794 ** Test to see whether or not a table is a virtual table.  This is
10795 ** done as a macro so that it will be optimized out when virtual
10796 ** table support is omitted from the build.
10797 */
10798 #ifndef SQLITE_OMIT_VIRTUALTABLE
10799 #  define IsVirtual(X)      (((X)->tabFlags & TF_Virtual)!=0)
10800 #  define IsHiddenColumn(X) (((X)->colFlags & COLFLAG_HIDDEN)!=0)
10801 #else
10802 #  define IsVirtual(X)      0
10803 #  define IsHiddenColumn(X) 0
10804 #endif
10805 
10806 /* Does the table have a rowid */
10807 #define HasRowid(X)     (((X)->tabFlags & TF_WithoutRowid)==0)
10808 
10809 /*
10810 ** Each foreign key constraint is an instance of the following structure.
10811 **
10812 ** A foreign key is associated with two tables.  The "from" table is
10813 ** the table that contains the REFERENCES clause that creates the foreign
10814 ** key.  The "to" table is the table that is named in the REFERENCES clause.
10815 ** Consider this example:
10816 **
10817 **     CREATE TABLE ex1(
10818 **       a INTEGER PRIMARY KEY,
10819 **       b INTEGER CONSTRAINT fk1 REFERENCES ex2(x)
10820 **     );
10821 **
10822 ** For foreign key "fk1", the from-table is "ex1" and the to-table is "ex2".
10823 ** Equivalent names:
10824 **
10825 **     from-table == child-table
10826 **       to-table == parent-table
10827 **
10828 ** Each REFERENCES clause generates an instance of the following structure
10829 ** which is attached to the from-table.  The to-table need not exist when
10830 ** the from-table is created.  The existence of the to-table is not checked.
10831 **
10832 ** The list of all parents for child Table X is held at X.pFKey.
10833 **
10834 ** A list of all children for a table named Z (which might not even exist)
10835 ** is held in Schema.fkeyHash with a hash key of Z.
10836 */
10837 struct FKey {
10838   Table *pFrom;     /* Table containing the REFERENCES clause (aka: Child) */
10839   FKey *pNextFrom;  /* Next FKey with the same in pFrom. Next parent of pFrom */
10840   char *zTo;        /* Name of table that the key points to (aka: Parent) */
10841   FKey *pNextTo;    /* Next with the same zTo. Next child of zTo. */
10842   FKey *pPrevTo;    /* Previous with the same zTo */
10843   int nCol;         /* Number of columns in this key */
10844   /* EV: R-30323-21917 */
10845   u8 isDeferred;       /* True if constraint checking is deferred till COMMIT */
10846   u8 aAction[2];        /* ON DELETE and ON UPDATE actions, respectively */
10847   Trigger *apTrigger[2];/* Triggers for aAction[] actions */
10848   struct sColMap {      /* Mapping of columns in pFrom to columns in zTo */
10849     int iFrom;            /* Index of column in pFrom */
10850     char *zCol;           /* Name of column in zTo.  If NULL use PRIMARY KEY */
10851   } aCol[1];            /* One entry for each of nCol columns */
10852 };
10853 
10854 /*
10855 ** SQLite supports many different ways to resolve a constraint
10856 ** error.  ROLLBACK processing means that a constraint violation
10857 ** causes the operation in process to fail and for the current transaction
10858 ** to be rolled back.  ABORT processing means the operation in process
10859 ** fails and any prior changes from that one operation are backed out,
10860 ** but the transaction is not rolled back.  FAIL processing means that
10861 ** the operation in progress stops and returns an error code.  But prior
10862 ** changes due to the same operation are not backed out and no rollback
10863 ** occurs.  IGNORE means that the particular row that caused the constraint
10864 ** error is not inserted or updated.  Processing continues and no error
10865 ** is returned.  REPLACE means that preexisting database rows that caused
10866 ** a UNIQUE constraint violation are removed so that the new insert or
10867 ** update can proceed.  Processing continues and no error is reported.
10868 **
10869 ** RESTRICT, SETNULL, and CASCADE actions apply only to foreign keys.
10870 ** RESTRICT is the same as ABORT for IMMEDIATE foreign keys and the
10871 ** same as ROLLBACK for DEFERRED keys.  SETNULL means that the foreign
10872 ** key is set to NULL.  CASCADE means that a DELETE or UPDATE of the
10873 ** referenced table row is propagated into the row that holds the
10874 ** foreign key.
10875 **
10876 ** The following symbolic values are used to record which type
10877 ** of action to take.
10878 */
10879 #define OE_None     0   /* There is no constraint to check */
10880 #define OE_Rollback 1   /* Fail the operation and rollback the transaction */
10881 #define OE_Abort    2   /* Back out changes but do no rollback transaction */
10882 #define OE_Fail     3   /* Stop the operation but leave all prior changes */
10883 #define OE_Ignore   4   /* Ignore the error. Do not do the INSERT or UPDATE */
10884 #define OE_Replace  5   /* Delete existing record, then do INSERT or UPDATE */
10885 
10886 #define OE_Restrict 6   /* OE_Abort for IMMEDIATE, OE_Rollback for DEFERRED */
10887 #define OE_SetNull  7   /* Set the foreign key value to NULL */
10888 #define OE_SetDflt  8   /* Set the foreign key value to its default */
10889 #define OE_Cascade  9   /* Cascade the changes */
10890 
10891 #define OE_Default  10  /* Do whatever the default action is */
10892 
10893 
10894 /*
10895 ** An instance of the following structure is passed as the first
10896 ** argument to sqlite3VdbeKeyCompare and is used to control the
10897 ** comparison of the two index keys.
10898 **
10899 ** Note that aSortOrder[] and aColl[] have nField+1 slots.  There
10900 ** are nField slots for the columns of an index then one extra slot
10901 ** for the rowid at the end.
10902 */
10903 struct KeyInfo {
10904   u32 nRef;           /* Number of references to this KeyInfo object */
10905   u8 enc;             /* Text encoding - one of the SQLITE_UTF* values */
10906   u16 nField;         /* Number of key columns in the index */
10907   u16 nXField;        /* Number of columns beyond the key columns */
10908   sqlite3 *db;        /* The database connection */
10909   u8 *aSortOrder;     /* Sort order for each column. */
10910   CollSeq *aColl[1];  /* Collating sequence for each term of the key */
10911 };
10912 
10913 /*
10914 ** An instance of the following structure holds information about a
10915 ** single index record that has already been parsed out into individual
10916 ** values.
10917 **
10918 ** A record is an object that contains one or more fields of data.
10919 ** Records are used to store the content of a table row and to store
10920 ** the key of an index.  A blob encoding of a record is created by
10921 ** the OP_MakeRecord opcode of the VDBE and is disassembled by the
10922 ** OP_Column opcode.
10923 **
10924 ** This structure holds a record that has already been disassembled
10925 ** into its constituent fields.
10926 */
10927 struct UnpackedRecord {
10928   KeyInfo *pKeyInfo;  /* Collation and sort-order information */
10929   u16 nField;         /* Number of entries in apMem[] */
10930   u8 flags;           /* Boolean settings.  UNPACKED_... below */
10931   Mem *aMem;          /* Values */
10932 };
10933 
10934 /*
10935 ** Allowed values of UnpackedRecord.flags
10936 */
10937 #define UNPACKED_INCRKEY       0x01  /* Make this key an epsilon larger */
10938 #define UNPACKED_PREFIX_MATCH  0x02  /* A prefix match is considered OK */
10939 
10940 /*
10941 ** Each SQL index is represented in memory by an
10942 ** instance of the following structure.
10943 **
10944 ** The columns of the table that are to be indexed are described
10945 ** by the aiColumn[] field of this structure.  For example, suppose
10946 ** we have the following table and index:
10947 **
10948 **     CREATE TABLE Ex1(c1 int, c2 int, c3 text);
10949 **     CREATE INDEX Ex2 ON Ex1(c3,c1);
10950 **
10951 ** In the Table structure describing Ex1, nCol==3 because there are
10952 ** three columns in the table.  In the Index structure describing
10953 ** Ex2, nColumn==2 since 2 of the 3 columns of Ex1 are indexed.
10954 ** The value of aiColumn is {2, 0}.  aiColumn[0]==2 because the
10955 ** first column to be indexed (c3) has an index of 2 in Ex1.aCol[].
10956 ** The second column to be indexed (c1) has an index of 0 in
10957 ** Ex1.aCol[], hence Ex2.aiColumn[1]==0.
10958 **
10959 ** The Index.onError field determines whether or not the indexed columns
10960 ** must be unique and what to do if they are not.  When Index.onError=OE_None,
10961 ** it means this is not a unique index.  Otherwise it is a unique index
10962 ** and the value of Index.onError indicate the which conflict resolution
10963 ** algorithm to employ whenever an attempt is made to insert a non-unique
10964 ** element.
10965 */
10966 struct Index {
10967   char *zName;             /* Name of this index */
10968   i16 *aiColumn;           /* Which columns are used by this index.  1st is 0 */
10969   tRowcnt *aiRowEst;       /* From ANALYZE: Est. rows selected by each column */
10970   Table *pTable;           /* The SQL table being indexed */
10971   char *zColAff;           /* String defining the affinity of each column */
10972   Index *pNext;            /* The next index associated with the same table */
10973   Schema *pSchema;         /* Schema containing this index */
10974   u8 *aSortOrder;          /* for each column: True==DESC, False==ASC */
10975   char **azColl;           /* Array of collation sequence names for index */
10976   Expr *pPartIdxWhere;     /* WHERE clause for partial indices */
10977   KeyInfo *pKeyInfo;       /* A KeyInfo object suitable for this index */
10978   int tnum;                /* DB Page containing root of this index */
10979   LogEst szIdxRow;         /* Estimated average row size in bytes */
10980   u16 nKeyCol;             /* Number of columns forming the key */
10981   u16 nColumn;             /* Number of columns stored in the index */
10982   u8 onError;              /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
10983   unsigned autoIndex:2;    /* 1==UNIQUE, 2==PRIMARY KEY, 0==CREATE INDEX */
10984   unsigned bUnordered:1;   /* Use this index for == or IN queries only */
10985   unsigned uniqNotNull:1;  /* True if UNIQUE and NOT NULL for all columns */
10986   unsigned isResized:1;    /* True if resizeIndexObject() has been called */
10987   unsigned isCovering:1;   /* True if this is a covering index */
10988 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
10989   int nSample;             /* Number of elements in aSample[] */
10990   int nSampleCol;          /* Size of IndexSample.anEq[] and so on */
10991   tRowcnt *aAvgEq;         /* Average nEq values for keys not in aSample */
10992   IndexSample *aSample;    /* Samples of the left-most key */
10993 #endif
10994 };
10995 
10996 /*
10997 ** Each sample stored in the sqlite_stat3 table is represented in memory
10998 ** using a structure of this type.  See documentation at the top of the
10999 ** analyze.c source file for additional information.
11000 */
11001 struct IndexSample {
11002   void *p;          /* Pointer to sampled record */
11003   int n;            /* Size of record in bytes */
11004   tRowcnt *anEq;    /* Est. number of rows where the key equals this sample */
11005   tRowcnt *anLt;    /* Est. number of rows where key is less than this sample */
11006   tRowcnt *anDLt;   /* Est. number of distinct keys less than this sample */
11007 };
11008 
11009 /*
11010 ** Each token coming out of the lexer is an instance of
11011 ** this structure.  Tokens are also used as part of an expression.
11012 **
11013 ** Note if Token.z==0 then Token.dyn and Token.n are undefined and
11014 ** may contain random values.  Do not make any assumptions about Token.dyn
11015 ** and Token.n when Token.z==0.
11016 */
11017 struct Token {
11018   const char *z;     /* Text of the token.  Not NULL-terminated! */
11019   unsigned int n;    /* Number of characters in this token */
11020 };
11021 
11022 /*
11023 ** An instance of this structure contains information needed to generate
11024 ** code for a SELECT that contains aggregate functions.
11025 **
11026 ** If Expr.op==TK_AGG_COLUMN or TK_AGG_FUNCTION then Expr.pAggInfo is a
11027 ** pointer to this structure.  The Expr.iColumn field is the index in
11028 ** AggInfo.aCol[] or AggInfo.aFunc[] of information needed to generate
11029 ** code for that node.
11030 **
11031 ** AggInfo.pGroupBy and AggInfo.aFunc.pExpr point to fields within the
11032 ** original Select structure that describes the SELECT statement.  These
11033 ** fields do not need to be freed when deallocating the AggInfo structure.
11034 */
11035 struct AggInfo {
11036   u8 directMode;          /* Direct rendering mode means take data directly
11037                           ** from source tables rather than from accumulators */
11038   u8 useSortingIdx;       /* In direct mode, reference the sorting index rather
11039                           ** than the source table */
11040   int sortingIdx;         /* Cursor number of the sorting index */
11041   int sortingIdxPTab;     /* Cursor number of pseudo-table */
11042   int nSortingColumn;     /* Number of columns in the sorting index */
11043   int mnReg, mxReg;       /* Range of registers allocated for aCol and aFunc */
11044   ExprList *pGroupBy;     /* The group by clause */
11045   struct AggInfo_col {    /* For each column used in source tables */
11046     Table *pTab;             /* Source table */
11047     int iTable;              /* Cursor number of the source table */
11048     int iColumn;             /* Column number within the source table */
11049     int iSorterColumn;       /* Column number in the sorting index */
11050     int iMem;                /* Memory location that acts as accumulator */
11051     Expr *pExpr;             /* The original expression */
11052   } *aCol;
11053   int nColumn;            /* Number of used entries in aCol[] */
11054   int nAccumulator;       /* Number of columns that show through to the output.
11055                           ** Additional columns are used only as parameters to
11056                           ** aggregate functions */
11057   struct AggInfo_func {   /* For each aggregate function */
11058     Expr *pExpr;             /* Expression encoding the function */
11059     FuncDef *pFunc;          /* The aggregate function implementation */
11060     int iMem;                /* Memory location that acts as accumulator */
11061     int iDistinct;           /* Ephemeral table used to enforce DISTINCT */
11062   } *aFunc;
11063   int nFunc;              /* Number of entries in aFunc[] */
11064 };
11065 
11066 /*
11067 ** The datatype ynVar is a signed integer, either 16-bit or 32-bit.
11068 ** Usually it is 16-bits.  But if SQLITE_MAX_VARIABLE_NUMBER is greater
11069 ** than 32767 we have to make it 32-bit.  16-bit is preferred because
11070 ** it uses less memory in the Expr object, which is a big memory user
11071 ** in systems with lots of prepared statements.  And few applications
11072 ** need more than about 10 or 20 variables.  But some extreme users want
11073 ** to have prepared statements with over 32767 variables, and for them
11074 ** the option is available (at compile-time).
11075 */
11076 #if SQLITE_MAX_VARIABLE_NUMBER<=32767
11077 typedef i16 ynVar;
11078 #else
11079 typedef int ynVar;
11080 #endif
11081 
11082 /*
11083 ** Each node of an expression in the parse tree is an instance
11084 ** of this structure.
11085 **
11086 ** Expr.op is the opcode. The integer parser token codes are reused
11087 ** as opcodes here. For example, the parser defines TK_GE to be an integer
11088 ** code representing the ">=" operator. This same integer code is reused
11089 ** to represent the greater-than-or-equal-to operator in the expression
11090 ** tree.
11091 **
11092 ** If the expression is an SQL literal (TK_INTEGER, TK_FLOAT, TK_BLOB,
11093 ** or TK_STRING), then Expr.token contains the text of the SQL literal. If
11094 ** the expression is a variable (TK_VARIABLE), then Expr.token contains the
11095 ** variable name. Finally, if the expression is an SQL function (TK_FUNCTION),
11096 ** then Expr.token contains the name of the function.
11097 **
11098 ** Expr.pRight and Expr.pLeft are the left and right subexpressions of a
11099 ** binary operator. Either or both may be NULL.
11100 **
11101 ** Expr.x.pList is a list of arguments if the expression is an SQL function,
11102 ** a CASE expression or an IN expression of the form "<lhs> IN (<y>, <z>...)".
11103 ** Expr.x.pSelect is used if the expression is a sub-select or an expression of
11104 ** the form "<lhs> IN (SELECT ...)". If the EP_xIsSelect bit is set in the
11105 ** Expr.flags mask, then Expr.x.pSelect is valid. Otherwise, Expr.x.pList is
11106 ** valid.
11107 **
11108 ** An expression of the form ID or ID.ID refers to a column in a table.
11109 ** For such expressions, Expr.op is set to TK_COLUMN and Expr.iTable is
11110 ** the integer cursor number of a VDBE cursor pointing to that table and
11111 ** Expr.iColumn is the column number for the specific column.  If the
11112 ** expression is used as a result in an aggregate SELECT, then the
11113 ** value is also stored in the Expr.iAgg column in the aggregate so that
11114 ** it can be accessed after all aggregates are computed.
11115 **
11116 ** If the expression is an unbound variable marker (a question mark
11117 ** character '?' in the original SQL) then the Expr.iTable holds the index
11118 ** number for that variable.
11119 **
11120 ** If the expression is a subquery then Expr.iColumn holds an integer
11121 ** register number containing the result of the subquery.  If the
11122 ** subquery gives a constant result, then iTable is -1.  If the subquery
11123 ** gives a different answer at different times during statement processing
11124 ** then iTable is the address of a subroutine that computes the subquery.
11125 **
11126 ** If the Expr is of type OP_Column, and the table it is selecting from
11127 ** is a disk table or the "old.*" pseudo-table, then pTab points to the
11128 ** corresponding table definition.
11129 **
11130 ** ALLOCATION NOTES:
11131 **
11132 ** Expr objects can use a lot of memory space in database schema.  To
11133 ** help reduce memory requirements, sometimes an Expr object will be
11134 ** truncated.  And to reduce the number of memory allocations, sometimes
11135 ** two or more Expr objects will be stored in a single memory allocation,
11136 ** together with Expr.zToken strings.
11137 **
11138 ** If the EP_Reduced and EP_TokenOnly flags are set when
11139 ** an Expr object is truncated.  When EP_Reduced is set, then all
11140 ** the child Expr objects in the Expr.pLeft and Expr.pRight subtrees
11141 ** are contained within the same memory allocation.  Note, however, that
11142 ** the subtrees in Expr.x.pList or Expr.x.pSelect are always separately
11143 ** allocated, regardless of whether or not EP_Reduced is set.
11144 */
11145 struct Expr {
11146   u8 op;                 /* Operation performed by this node */
11147   char affinity;         /* The affinity of the column or 0 if not a column */
11148   u32 flags;             /* Various flags.  EP_* See below */
11149   union {
11150     char *zToken;          /* Token value. Zero terminated and dequoted */
11151     int iValue;            /* Non-negative integer value if EP_IntValue */
11152   } u;
11153 
11154   /* If the EP_TokenOnly flag is set in the Expr.flags mask, then no
11155   ** space is allocated for the fields below this point. An attempt to
11156   ** access them will result in a segfault or malfunction.
11157   *********************************************************************/
11158 
11159   Expr *pLeft;           /* Left subnode */
11160   Expr *pRight;          /* Right subnode */
11161   union {
11162     ExprList *pList;     /* op = IN, EXISTS, SELECT, CASE, FUNCTION, BETWEEN */
11163     Select *pSelect;     /* EP_xIsSelect and op = IN, EXISTS, SELECT */
11164   } x;
11165 
11166   /* If the EP_Reduced flag is set in the Expr.flags mask, then no
11167   ** space is allocated for the fields below this point. An attempt to
11168   ** access them will result in a segfault or malfunction.
11169   *********************************************************************/
11170 
11171 #if SQLITE_MAX_EXPR_DEPTH>0
11172   int nHeight;           /* Height of the tree headed by this node */
11173 #endif
11174   int iTable;            /* TK_COLUMN: cursor number of table holding column
11175                          ** TK_REGISTER: register number
11176                          ** TK_TRIGGER: 1 -> new, 0 -> old
11177                          ** EP_Unlikely:  1000 times likelihood */
11178   ynVar iColumn;         /* TK_COLUMN: column index.  -1 for rowid.
11179                          ** TK_VARIABLE: variable number (always >= 1). */
11180   i16 iAgg;              /* Which entry in pAggInfo->aCol[] or ->aFunc[] */
11181   i16 iRightJoinTable;   /* If EP_FromJoin, the right table of the join */
11182   u8 op2;                /* TK_REGISTER: original value of Expr.op
11183                          ** TK_COLUMN: the value of p5 for OP_Column
11184                          ** TK_AGG_FUNCTION: nesting depth */
11185   AggInfo *pAggInfo;     /* Used by TK_AGG_COLUMN and TK_AGG_FUNCTION */
11186   Table *pTab;           /* Table for TK_COLUMN expressions. */
11187 };
11188 
11189 /*
11190 ** The following are the meanings of bits in the Expr.flags field.
11191 */
11192 #define EP_FromJoin  0x000001 /* Originated in ON or USING clause of a join */
11193 #define EP_Agg       0x000002 /* Contains one or more aggregate functions */
11194 #define EP_Resolved  0x000004 /* IDs have been resolved to COLUMNs */
11195 #define EP_Error     0x000008 /* Expression contains one or more errors */
11196 #define EP_Distinct  0x000010 /* Aggregate function with DISTINCT keyword */
11197 #define EP_VarSelect 0x000020 /* pSelect is correlated, not constant */
11198 #define EP_DblQuoted 0x000040 /* token.z was originally in "..." */
11199 #define EP_InfixFunc 0x000080 /* True for an infix function: LIKE, GLOB, etc */
11200 #define EP_Collate   0x000100 /* Tree contains a TK_COLLATE opeartor */
11201       /* unused      0x000200 */
11202 #define EP_IntValue  0x000400 /* Integer value contained in u.iValue */
11203 #define EP_xIsSelect 0x000800 /* x.pSelect is valid (otherwise x.pList is) */
11204 #define EP_Skip      0x001000 /* COLLATE, AS, or UNLIKELY */
11205 #define EP_Reduced   0x002000 /* Expr struct EXPR_REDUCEDSIZE bytes only */
11206 #define EP_TokenOnly 0x004000 /* Expr struct EXPR_TOKENONLYSIZE bytes only */
11207 #define EP_Static    0x008000 /* Held in memory not obtained from malloc() */
11208 #define EP_MemToken  0x010000 /* Need to sqlite3DbFree() Expr.zToken */
11209 #define EP_NoReduce  0x020000 /* Cannot EXPRDUP_REDUCE this Expr */
11210 #define EP_Unlikely  0x040000 /* unlikely() or likelihood() function */
11211 #define EP_Constant  0x080000 /* Node is a constant */
11212 
11213 /*
11214 ** These macros can be used to test, set, or clear bits in the
11215 ** Expr.flags field.
11216 */
11217 #define ExprHasProperty(E,P)     (((E)->flags&(P))!=0)
11218 #define ExprHasAllProperty(E,P)  (((E)->flags&(P))==(P))
11219 #define ExprSetProperty(E,P)     (E)->flags|=(P)
11220 #define ExprClearProperty(E,P)   (E)->flags&=~(P)
11221 
11222 /* The ExprSetVVAProperty() macro is used for Verification, Validation,
11223 ** and Accreditation only.  It works like ExprSetProperty() during VVA
11224 ** processes but is a no-op for delivery.
11225 */
11226 #ifdef SQLITE_DEBUG
11227 # define ExprSetVVAProperty(E,P)  (E)->flags|=(P)
11228 #else
11229 # define ExprSetVVAProperty(E,P)
11230 #endif
11231 
11232 /*
11233 ** Macros to determine the number of bytes required by a normal Expr
11234 ** struct, an Expr struct with the EP_Reduced flag set in Expr.flags
11235 ** and an Expr struct with the EP_TokenOnly flag set.
11236 */
11237 #define EXPR_FULLSIZE           sizeof(Expr)           /* Full size */
11238 #define EXPR_REDUCEDSIZE        offsetof(Expr,iTable)  /* Common features */
11239 #define EXPR_TOKENONLYSIZE      offsetof(Expr,pLeft)   /* Fewer features */
11240 
11241 /*
11242 ** Flags passed to the sqlite3ExprDup() function. See the header comment
11243 ** above sqlite3ExprDup() for details.
11244 */
11245 #define EXPRDUP_REDUCE         0x0001  /* Used reduced-size Expr nodes */
11246 
11247 /*
11248 ** A list of expressions.  Each expression may optionally have a
11249 ** name.  An expr/name combination can be used in several ways, such
11250 ** as the list of "expr AS ID" fields following a "SELECT" or in the
11251 ** list of "ID = expr" items in an UPDATE.  A list of expressions can
11252 ** also be used as the argument to a function, in which case the a.zName
11253 ** field is not used.
11254 **
11255 ** By default the Expr.zSpan field holds a human-readable description of
11256 ** the expression that is used in the generation of error messages and
11257 ** column labels.  In this case, Expr.zSpan is typically the text of a
11258 ** column expression as it exists in a SELECT statement.  However, if
11259 ** the bSpanIsTab flag is set, then zSpan is overloaded to mean the name
11260 ** of the result column in the form: DATABASE.TABLE.COLUMN.  This later
11261 ** form is used for name resolution with nested FROM clauses.
11262 */
11263 struct ExprList {
11264   int nExpr;             /* Number of expressions on the list */
11265   int iECursor;          /* VDBE Cursor associated with this ExprList */
11266   struct ExprList_item { /* For each expression in the list */
11267     Expr *pExpr;            /* The list of expressions */
11268     char *zName;            /* Token associated with this expression */
11269     char *zSpan;            /* Original text of the expression */
11270     u8 sortOrder;           /* 1 for DESC or 0 for ASC */
11271     unsigned done :1;       /* A flag to indicate when processing is finished */
11272     unsigned bSpanIsTab :1; /* zSpan holds DB.TABLE.COLUMN */
11273     unsigned reusable :1;   /* Constant expression is reusable */
11274     union {
11275       struct {
11276         u16 iOrderByCol;      /* For ORDER BY, column number in result set */
11277         u16 iAlias;           /* Index into Parse.aAlias[] for zName */
11278       } x;
11279       int iConstExprReg;      /* Register in which Expr value is cached */
11280     } u;
11281   } *a;                  /* Alloc a power of two greater or equal to nExpr */
11282 };
11283 
11284 /*
11285 ** An instance of this structure is used by the parser to record both
11286 ** the parse tree for an expression and the span of input text for an
11287 ** expression.
11288 */
11289 struct ExprSpan {
11290   Expr *pExpr;          /* The expression parse tree */
11291   const char *zStart;   /* First character of input text */
11292   const char *zEnd;     /* One character past the end of input text */
11293 };
11294 
11295 /*
11296 ** An instance of this structure can hold a simple list of identifiers,
11297 ** such as the list "a,b,c" in the following statements:
11298 **
11299 **      INSERT INTO t(a,b,c) VALUES ...;
11300 **      CREATE INDEX idx ON t(a,b,c);
11301 **      CREATE TRIGGER trig BEFORE UPDATE ON t(a,b,c) ...;
11302 **
11303 ** The IdList.a.idx field is used when the IdList represents the list of
11304 ** column names after a table name in an INSERT statement.  In the statement
11305 **
11306 **     INSERT INTO t(a,b,c) ...
11307 **
11308 ** If "a" is the k-th column of table "t", then IdList.a[0].idx==k.
11309 */
11310 struct IdList {
11311   struct IdList_item {
11312     char *zName;      /* Name of the identifier */
11313     int idx;          /* Index in some Table.aCol[] of a column named zName */
11314   } *a;
11315   int nId;         /* Number of identifiers on the list */
11316 };
11317 
11318 /*
11319 ** The bitmask datatype defined below is used for various optimizations.
11320 **
11321 ** Changing this from a 64-bit to a 32-bit type limits the number of
11322 ** tables in a join to 32 instead of 64.  But it also reduces the size
11323 ** of the library by 738 bytes on ix86.
11324 */
11325 typedef u64 Bitmask;
11326 
11327 /*
11328 ** The number of bits in a Bitmask.  "BMS" means "BitMask Size".
11329 */
11330 #define BMS  ((int)(sizeof(Bitmask)*8))
11331 
11332 /*
11333 ** A bit in a Bitmask
11334 */
11335 #define MASKBIT(n)   (((Bitmask)1)<<(n))
11336 #define MASKBIT32(n) (((unsigned int)1)<<(n))
11337 
11338 /*
11339 ** The following structure describes the FROM clause of a SELECT statement.
11340 ** Each table or subquery in the FROM clause is a separate element of
11341 ** the SrcList.a[] array.
11342 **
11343 ** With the addition of multiple database support, the following structure
11344 ** can also be used to describe a particular table such as the table that
11345 ** is modified by an INSERT, DELETE, or UPDATE statement.  In standard SQL,
11346 ** such a table must be a simple name: ID.  But in SQLite, the table can
11347 ** now be identified by a database name, a dot, then the table name: ID.ID.
11348 **
11349 ** The jointype starts out showing the join type between the current table
11350 ** and the next table on the list.  The parser builds the list this way.
11351 ** But sqlite3SrcListShiftJoinType() later shifts the jointypes so that each
11352 ** jointype expresses the join between the table and the previous table.
11353 **
11354 ** In the colUsed field, the high-order bit (bit 63) is set if the table
11355 ** contains more than 63 columns and the 64-th or later column is used.
11356 */
11357 struct SrcList {
11358   u8 nSrc;        /* Number of tables or subqueries in the FROM clause */
11359   u8 nAlloc;      /* Number of entries allocated in a[] below */
11360   struct SrcList_item {
11361     Schema *pSchema;  /* Schema to which this item is fixed */
11362     char *zDatabase;  /* Name of database holding this table */
11363     char *zName;      /* Name of the table */
11364     char *zAlias;     /* The "B" part of a "A AS B" phrase.  zName is the "A" */
11365     Table *pTab;      /* An SQL table corresponding to zName */
11366     Select *pSelect;  /* A SELECT statement used in place of a table name */
11367     int addrFillSub;  /* Address of subroutine to manifest a subquery */
11368     int regReturn;    /* Register holding return address of addrFillSub */
11369     u8 jointype;      /* Type of join between this able and the previous */
11370     unsigned notIndexed :1;    /* True if there is a NOT INDEXED clause */
11371     unsigned isCorrelated :1;  /* True if sub-query is correlated */
11372     unsigned viaCoroutine :1;  /* Implemented as a co-routine */
11373     unsigned isRecursive :1;   /* True for recursive reference in WITH */
11374 #ifndef SQLITE_OMIT_EXPLAIN
11375     u8 iSelectId;     /* If pSelect!=0, the id of the sub-select in EQP */
11376 #endif
11377     int iCursor;      /* The VDBE cursor number used to access this table */
11378     Expr *pOn;        /* The ON clause of a join */
11379     IdList *pUsing;   /* The USING clause of a join */
11380     Bitmask colUsed;  /* Bit N (1<<N) set if column N of pTab is used */
11381     char *zIndex;     /* Identifier from "INDEXED BY <zIndex>" clause */
11382     Index *pIndex;    /* Index structure corresponding to zIndex, if any */
11383   } a[1];             /* One entry for each identifier on the list */
11384 };
11385 
11386 /*
11387 ** Permitted values of the SrcList.a.jointype field
11388 */
11389 #define JT_INNER     0x0001    /* Any kind of inner or cross join */
11390 #define JT_CROSS     0x0002    /* Explicit use of the CROSS keyword */
11391 #define JT_NATURAL   0x0004    /* True for a "natural" join */
11392 #define JT_LEFT      0x0008    /* Left outer join */
11393 #define JT_RIGHT     0x0010    /* Right outer join */
11394 #define JT_OUTER     0x0020    /* The "OUTER" keyword is present */
11395 #define JT_ERROR     0x0040    /* unknown or unsupported join type */
11396 
11397 
11398 /*
11399 ** Flags appropriate for the wctrlFlags parameter of sqlite3WhereBegin()
11400 ** and the WhereInfo.wctrlFlags member.
11401 */
11402 #define WHERE_ORDERBY_NORMAL   0x0000 /* No-op */
11403 #define WHERE_ORDERBY_MIN      0x0001 /* ORDER BY processing for min() func */
11404 #define WHERE_ORDERBY_MAX      0x0002 /* ORDER BY processing for max() func */
11405 #define WHERE_ONEPASS_DESIRED  0x0004 /* Want to do one-pass UPDATE/DELETE */
11406 #define WHERE_DUPLICATES_OK    0x0008 /* Ok to return a row more than once */
11407 #define WHERE_OMIT_OPEN_CLOSE  0x0010 /* Table cursors are already open */
11408 #define WHERE_FORCE_TABLE      0x0020 /* Do not use an index-only search */
11409 #define WHERE_ONETABLE_ONLY    0x0040 /* Only code the 1st table in pTabList */
11410 #define WHERE_AND_ONLY         0x0080 /* Don't use indices for OR terms */
11411 #define WHERE_GROUPBY          0x0100 /* pOrderBy is really a GROUP BY */
11412 #define WHERE_DISTINCTBY       0x0200 /* pOrderby is really a DISTINCT clause */
11413 #define WHERE_WANT_DISTINCT    0x0400 /* All output needs to be distinct */
11414 
11415 /* Allowed return values from sqlite3WhereIsDistinct()
11416 */
11417 #define WHERE_DISTINCT_NOOP      0  /* DISTINCT keyword not used */
11418 #define WHERE_DISTINCT_UNIQUE    1  /* No duplicates */
11419 #define WHERE_DISTINCT_ORDERED   2  /* All duplicates are adjacent */
11420 #define WHERE_DISTINCT_UNORDERED 3  /* Duplicates are scattered */
11421 
11422 /*
11423 ** A NameContext defines a context in which to resolve table and column
11424 ** names.  The context consists of a list of tables (the pSrcList) field and
11425 ** a list of named expression (pEList).  The named expression list may
11426 ** be NULL.  The pSrc corresponds to the FROM clause of a SELECT or
11427 ** to the table being operated on by INSERT, UPDATE, or DELETE.  The
11428 ** pEList corresponds to the result set of a SELECT and is NULL for
11429 ** other statements.
11430 **
11431 ** NameContexts can be nested.  When resolving names, the inner-most
11432 ** context is searched first.  If no match is found, the next outer
11433 ** context is checked.  If there is still no match, the next context
11434 ** is checked.  This process continues until either a match is found
11435 ** or all contexts are check.  When a match is found, the nRef member of
11436 ** the context containing the match is incremented.
11437 **
11438 ** Each subquery gets a new NameContext.  The pNext field points to the
11439 ** NameContext in the parent query.  Thus the process of scanning the
11440 ** NameContext list corresponds to searching through successively outer
11441 ** subqueries looking for a match.
11442 */
11443 struct NameContext {
11444   Parse *pParse;       /* The parser */
11445   SrcList *pSrcList;   /* One or more tables used to resolve names */
11446   ExprList *pEList;    /* Optional list of result-set columns */
11447   AggInfo *pAggInfo;   /* Information about aggregates at this level */
11448   NameContext *pNext;  /* Next outer name context.  NULL for outermost */
11449   int nRef;            /* Number of names resolved by this context */
11450   int nErr;            /* Number of errors encountered while resolving names */
11451   u8 ncFlags;          /* Zero or more NC_* flags defined below */
11452 };
11453 
11454 /*
11455 ** Allowed values for the NameContext, ncFlags field.
11456 */
11457 #define NC_AllowAgg  0x01    /* Aggregate functions are allowed here */
11458 #define NC_HasAgg    0x02    /* One or more aggregate functions seen */
11459 #define NC_IsCheck   0x04    /* True if resolving names in a CHECK constraint */
11460 #define NC_InAggFunc 0x08    /* True if analyzing arguments to an agg func */
11461 #define NC_PartIdx   0x10    /* True if resolving a partial index WHERE */
11462 
11463 /*
11464 ** An instance of the following structure contains all information
11465 ** needed to generate code for a single SELECT statement.
11466 **
11467 ** nLimit is set to -1 if there is no LIMIT clause.  nOffset is set to 0.
11468 ** If there is a LIMIT clause, the parser sets nLimit to the value of the
11469 ** limit and nOffset to the value of the offset (or 0 if there is not
11470 ** offset).  But later on, nLimit and nOffset become the memory locations
11471 ** in the VDBE that record the limit and offset counters.
11472 **
11473 ** addrOpenEphm[] entries contain the address of OP_OpenEphemeral opcodes.
11474 ** These addresses must be stored so that we can go back and fill in
11475 ** the P4_KEYINFO and P2 parameters later.  Neither the KeyInfo nor
11476 ** the number of columns in P2 can be computed at the same time
11477 ** as the OP_OpenEphm instruction is coded because not
11478 ** enough information about the compound query is known at that point.
11479 ** The KeyInfo for addrOpenTran[0] and [1] contains collating sequences
11480 ** for the result set.  The KeyInfo for addrOpenEphm[2] contains collating
11481 ** sequences for the ORDER BY clause.
11482 */
11483 struct Select {
11484   ExprList *pEList;      /* The fields of the result */
11485   u8 op;                 /* One of: TK_UNION TK_ALL TK_INTERSECT TK_EXCEPT */
11486   u16 selFlags;          /* Various SF_* values */
11487   int iLimit, iOffset;   /* Memory registers holding LIMIT & OFFSET counters */
11488   int addrOpenEphm[3];   /* OP_OpenEphem opcodes related to this select */
11489   u64 nSelectRow;        /* Estimated number of result rows */
11490   SrcList *pSrc;         /* The FROM clause */
11491   Expr *pWhere;          /* The WHERE clause */
11492   ExprList *pGroupBy;    /* The GROUP BY clause */
11493   Expr *pHaving;         /* The HAVING clause */
11494   ExprList *pOrderBy;    /* The ORDER BY clause */
11495   Select *pPrior;        /* Prior select in a compound select statement */
11496   Select *pNext;         /* Next select to the left in a compound */
11497   Select *pRightmost;    /* Right-most select in a compound select statement */
11498   Expr *pLimit;          /* LIMIT expression. NULL means not used. */
11499   Expr *pOffset;         /* OFFSET expression. NULL means not used. */
11500   With *pWith;           /* WITH clause attached to this select. Or NULL. */
11501 };
11502 
11503 /*
11504 ** Allowed values for Select.selFlags.  The "SF" prefix stands for
11505 ** "Select Flag".
11506 */
11507 #define SF_Distinct        0x0001  /* Output should be DISTINCT */
11508 #define SF_Resolved        0x0002  /* Identifiers have been resolved */
11509 #define SF_Aggregate       0x0004  /* Contains aggregate functions */
11510 #define SF_UsesEphemeral   0x0008  /* Uses the OpenEphemeral opcode */
11511 #define SF_Expanded        0x0010  /* sqlite3SelectExpand() called on this */
11512 #define SF_HasTypeInfo     0x0020  /* FROM subqueries have Table metadata */
11513 #define SF_UseSorter       0x0040  /* Sort using a sorter */
11514 #define SF_Values          0x0080  /* Synthesized from VALUES clause */
11515 #define SF_Materialize     0x0100  /* Force materialization of views */
11516 #define SF_NestedFrom      0x0200  /* Part of a parenthesized FROM clause */
11517 #define SF_MaybeConvert    0x0400  /* Need convertCompoundSelectToSubquery() */
11518 #define SF_Recursive       0x0800  /* The recursive part of a recursive CTE */
11519 
11520 
11521 /*
11522 ** The results of a SELECT can be distributed in several ways, as defined
11523 ** by one of the following macros.  The "SRT" prefix means "SELECT Result
11524 ** Type".
11525 **
11526 **     SRT_Union       Store results as a key in a temporary index
11527 **                     identified by pDest->iSDParm.
11528 **
11529 **     SRT_Except      Remove results from the temporary index pDest->iSDParm.
11530 **
11531 **     SRT_Exists      Store a 1 in memory cell pDest->iSDParm if the result
11532 **                     set is not empty.
11533 **
11534 **     SRT_Discard     Throw the results away.  This is used by SELECT
11535 **                     statements within triggers whose only purpose is
11536 **                     the side-effects of functions.
11537 **
11538 ** All of the above are free to ignore their ORDER BY clause. Those that
11539 ** follow must honor the ORDER BY clause.
11540 **
11541 **     SRT_Output      Generate a row of output (using the OP_ResultRow
11542 **                     opcode) for each row in the result set.
11543 **
11544 **     SRT_Mem         Only valid if the result is a single column.
11545 **                     Store the first column of the first result row
11546 **                     in register pDest->iSDParm then abandon the rest
11547 **                     of the query.  This destination implies "LIMIT 1".
11548 **
11549 **     SRT_Set         The result must be a single column.  Store each
11550 **                     row of result as the key in table pDest->iSDParm.
11551 **                     Apply the affinity pDest->affSdst before storing
11552 **                     results.  Used to implement "IN (SELECT ...)".
11553 **
11554 **     SRT_EphemTab    Create an temporary table pDest->iSDParm and store
11555 **                     the result there. The cursor is left open after
11556 **                     returning.  This is like SRT_Table except that
11557 **                     this destination uses OP_OpenEphemeral to create
11558 **                     the table first.
11559 **
11560 **     SRT_Coroutine   Generate a co-routine that returns a new row of
11561 **                     results each time it is invoked.  The entry point
11562 **                     of the co-routine is stored in register pDest->iSDParm
11563 **                     and the result row is stored in pDest->nDest registers
11564 **                     starting with pDest->iSdst.
11565 **
11566 **     SRT_Table       Store results in temporary table pDest->iSDParm.
11567 **                     This is like SRT_EphemTab except that the table
11568 **                     is assumed to already be open.
11569 **
11570 **     SRT_DistTable   Store results in a temporary table pDest->iSDParm.
11571 **                     But also use temporary table pDest->iSDParm+1 as
11572 **                     a record of all prior results and ignore any duplicate
11573 **                     rows.  Name means:  "Distinct Table".
11574 **
11575 **     SRT_Queue       Store results in priority queue pDest->iSDParm (really
11576 **                     an index).  Append a sequence number so that all entries
11577 **                     are distinct.
11578 **
11579 **     SRT_DistQueue   Store results in priority queue pDest->iSDParm only if
11580 **                     the same record has never been stored before.  The
11581 **                     index at pDest->iSDParm+1 hold all prior stores.
11582 */
11583 #define SRT_Union        1  /* Store result as keys in an index */
11584 #define SRT_Except       2  /* Remove result from a UNION index */
11585 #define SRT_Exists       3  /* Store 1 if the result is not empty */
11586 #define SRT_Discard      4  /* Do not save the results anywhere */
11587 
11588 /* The ORDER BY clause is ignored for all of the above */
11589 #define IgnorableOrderby(X) ((X->eDest)<=SRT_Discard)
11590 
11591 #define SRT_Output       5  /* Output each row of result */
11592 #define SRT_Mem          6  /* Store result in a memory cell */
11593 #define SRT_Set          7  /* Store results as keys in an index */
11594 #define SRT_EphemTab     8  /* Create transient tab and store like SRT_Table */
11595 #define SRT_Coroutine    9  /* Generate a single row of result */
11596 #define SRT_Table       10  /* Store result as data with an automatic rowid */
11597 #define SRT_DistTable   11  /* Like SRT_Table, but unique results only */
11598 #define SRT_Queue       12  /* Store result in an queue */
11599 #define SRT_DistQueue   13  /* Like SRT_Queue, but unique results only */
11600 
11601 /*
11602 ** An instance of this object describes where to put of the results of
11603 ** a SELECT statement.
11604 */
11605 struct SelectDest {
11606   u8 eDest;            /* How to dispose of the results.  On of SRT_* above. */
11607   char affSdst;        /* Affinity used when eDest==SRT_Set */
11608   int iSDParm;         /* A parameter used by the eDest disposal method */
11609   int iSdst;           /* Base register where results are written */
11610   int nSdst;           /* Number of registers allocated */
11611   ExprList *pOrderBy;  /* Key columns for SRT_Queue and SRT_DistQueue */
11612 };
11613 
11614 /*
11615 ** During code generation of statements that do inserts into AUTOINCREMENT
11616 ** tables, the following information is attached to the Table.u.autoInc.p
11617 ** pointer of each autoincrement table to record some side information that
11618 ** the code generator needs.  We have to keep per-table autoincrement
11619 ** information in case inserts are down within triggers.  Triggers do not
11620 ** normally coordinate their activities, but we do need to coordinate the
11621 ** loading and saving of autoincrement information.
11622 */
11623 struct AutoincInfo {
11624   AutoincInfo *pNext;   /* Next info block in a list of them all */
11625   Table *pTab;          /* Table this info block refers to */
11626   int iDb;              /* Index in sqlite3.aDb[] of database holding pTab */
11627   int regCtr;           /* Memory register holding the rowid counter */
11628 };
11629 
11630 /*
11631 ** Size of the column cache
11632 */
11633 #ifndef SQLITE_N_COLCACHE
11634 # define SQLITE_N_COLCACHE 10
11635 #endif
11636 
11637 /*
11638 ** At least one instance of the following structure is created for each
11639 ** trigger that may be fired while parsing an INSERT, UPDATE or DELETE
11640 ** statement. All such objects are stored in the linked list headed at
11641 ** Parse.pTriggerPrg and deleted once statement compilation has been
11642 ** completed.
11643 **
11644 ** A Vdbe sub-program that implements the body and WHEN clause of trigger
11645 ** TriggerPrg.pTrigger, assuming a default ON CONFLICT clause of
11646 ** TriggerPrg.orconf, is stored in the TriggerPrg.pProgram variable.
11647 ** The Parse.pTriggerPrg list never contains two entries with the same
11648 ** values for both pTrigger and orconf.
11649 **
11650 ** The TriggerPrg.aColmask[0] variable is set to a mask of old.* columns
11651 ** accessed (or set to 0 for triggers fired as a result of INSERT
11652 ** statements). Similarly, the TriggerPrg.aColmask[1] variable is set to
11653 ** a mask of new.* columns used by the program.
11654 */
11655 struct TriggerPrg {
11656   Trigger *pTrigger;      /* Trigger this program was coded from */
11657   TriggerPrg *pNext;      /* Next entry in Parse.pTriggerPrg list */
11658   SubProgram *pProgram;   /* Program implementing pTrigger/orconf */
11659   int orconf;             /* Default ON CONFLICT policy */
11660   u32 aColmask[2];        /* Masks of old.*, new.* columns accessed */
11661 };
11662 
11663 /*
11664 ** The yDbMask datatype for the bitmask of all attached databases.
11665 */
11666 #if SQLITE_MAX_ATTACHED>30
11667   typedef sqlite3_uint64 yDbMask;
11668 #else
11669   typedef unsigned int yDbMask;
11670 #endif
11671 
11672 /*
11673 ** An SQL parser context.  A copy of this structure is passed through
11674 ** the parser and down into all the parser action routine in order to
11675 ** carry around information that is global to the entire parse.
11676 **
11677 ** The structure is divided into two parts.  When the parser and code
11678 ** generate call themselves recursively, the first part of the structure
11679 ** is constant but the second part is reset at the beginning and end of
11680 ** each recursion.
11681 **
11682 ** The nTableLock and aTableLock variables are only used if the shared-cache
11683 ** feature is enabled (if sqlite3Tsd()->useSharedData is true). They are
11684 ** used to store the set of table-locks required by the statement being
11685 ** compiled. Function sqlite3TableLock() is used to add entries to the
11686 ** list.
11687 */
11688 struct Parse {
11689   sqlite3 *db;         /* The main database structure */
11690   char *zErrMsg;       /* An error message */
11691   Vdbe *pVdbe;         /* An engine for executing database bytecode */
11692   int rc;              /* Return code from execution */
11693   u8 colNamesSet;      /* TRUE after OP_ColumnName has been issued to pVdbe */
11694   u8 checkSchema;      /* Causes schema cookie check after an error */
11695   u8 nested;           /* Number of nested calls to the parser/code generator */
11696   u8 nTempReg;         /* Number of temporary registers in aTempReg[] */
11697   u8 nTempInUse;       /* Number of aTempReg[] currently checked out */
11698   u8 nColCache;        /* Number of entries in aColCache[] */
11699   u8 iColCache;        /* Next entry in aColCache[] to replace */
11700   u8 isMultiWrite;     /* True if statement may modify/insert multiple rows */
11701   u8 mayAbort;         /* True if statement may throw an ABORT exception */
11702   u8 hasCompound;      /* Need to invoke convertCompoundSelectToSubquery() */
11703   int aTempReg[8];     /* Holding area for temporary registers */
11704   int nRangeReg;       /* Size of the temporary register block */
11705   int iRangeReg;       /* First register in temporary register block */
11706   int nErr;            /* Number of errors seen */
11707   int nTab;            /* Number of previously allocated VDBE cursors */
11708   int nMem;            /* Number of memory cells used so far */
11709   int nSet;            /* Number of sets used so far */
11710   int nOnce;           /* Number of OP_Once instructions so far */
11711   int nOpAlloc;        /* Number of slots allocated for Vdbe.aOp[] */
11712   int nLabel;          /* Number of labels used */
11713   int *aLabel;         /* Space to hold the labels */
11714   int iFixedOp;        /* Never back out opcodes iFixedOp-1 or earlier */
11715   int ckBase;          /* Base register of data during check constraints */
11716   int iPartIdxTab;     /* Table corresponding to a partial index */
11717   int iCacheLevel;     /* ColCache valid when aColCache[].iLevel<=iCacheLevel */
11718   int iCacheCnt;       /* Counter used to generate aColCache[].lru values */
11719   struct yColCache {
11720     int iTable;           /* Table cursor number */
11721     int iColumn;          /* Table column number */
11722     u8 tempReg;           /* iReg is a temp register that needs to be freed */
11723     int iLevel;           /* Nesting level */
11724     int iReg;             /* Reg with value of this column. 0 means none. */
11725     int lru;              /* Least recently used entry has the smallest value */
11726   } aColCache[SQLITE_N_COLCACHE];  /* One for each column cache entry */
11727   ExprList *pConstExpr;/* Constant expressions */
11728   yDbMask writeMask;   /* Start a write transaction on these databases */
11729   yDbMask cookieMask;  /* Bitmask of schema verified databases */
11730   int cookieGoto;      /* Address of OP_Goto to cookie verifier subroutine */
11731   int cookieValue[SQLITE_MAX_ATTACHED+2];  /* Values of cookies to verify */
11732   int regRowid;        /* Register holding rowid of CREATE TABLE entry */
11733   int regRoot;         /* Register holding root page number for new objects */
11734   int nMaxArg;         /* Max args passed to user function by sub-program */
11735   Token constraintName;/* Name of the constraint currently being parsed */
11736 #ifndef SQLITE_OMIT_SHARED_CACHE
11737   int nTableLock;        /* Number of locks in aTableLock */
11738   TableLock *aTableLock; /* Required table locks for shared-cache mode */
11739 #endif
11740   AutoincInfo *pAinc;  /* Information about AUTOINCREMENT counters */
11741 
11742   /* Information used while coding trigger programs. */
11743   Parse *pToplevel;    /* Parse structure for main program (or NULL) */
11744   Table *pTriggerTab;  /* Table triggers are being coded for */
11745   int addrCrTab;       /* Address of OP_CreateTable opcode on CREATE TABLE */
11746   int addrSkipPK;      /* Address of instruction to skip PRIMARY KEY index */
11747   u32 nQueryLoop;      /* Est number of iterations of a query (10*log2(N)) */
11748   u32 oldmask;         /* Mask of old.* columns referenced */
11749   u32 newmask;         /* Mask of new.* columns referenced */
11750   u8 eTriggerOp;       /* TK_UPDATE, TK_INSERT or TK_DELETE */
11751   u8 eOrconf;          /* Default ON CONFLICT policy for trigger steps */
11752   u8 disableTriggers;  /* True to disable triggers */
11753 
11754   /* Above is constant between recursions.  Below is reset before and after
11755   ** each recursion */
11756 
11757   int nVar;                 /* Number of '?' variables seen in the SQL so far */
11758   int nzVar;                /* Number of available slots in azVar[] */
11759   u8 iPkSortOrder;          /* ASC or DESC for INTEGER PRIMARY KEY */
11760   u8 explain;               /* True if the EXPLAIN flag is found on the query */
11761 #ifndef SQLITE_OMIT_VIRTUALTABLE
11762   u8 declareVtab;           /* True if inside sqlite3_declare_vtab() */
11763   int nVtabLock;            /* Number of virtual tables to lock */
11764 #endif
11765   int nAlias;               /* Number of aliased result set columns */
11766   int nHeight;              /* Expression tree height of current sub-select */
11767 #ifndef SQLITE_OMIT_EXPLAIN
11768   int iSelectId;            /* ID of current select for EXPLAIN output */
11769   int iNextSelectId;        /* Next available select ID for EXPLAIN output */
11770 #endif
11771   char **azVar;             /* Pointers to names of parameters */
11772   Vdbe *pReprepare;         /* VM being reprepared (sqlite3Reprepare()) */
11773   const char *zTail;        /* All SQL text past the last semicolon parsed */
11774   Table *pNewTable;         /* A table being constructed by CREATE TABLE */
11775   Trigger *pNewTrigger;     /* Trigger under construct by a CREATE TRIGGER */
11776   const char *zAuthContext; /* The 6th parameter to db->xAuth callbacks */
11777   Token sNameToken;         /* Token with unqualified schema object name */
11778   Token sLastToken;         /* The last token parsed */
11779 #ifndef SQLITE_OMIT_VIRTUALTABLE
11780   Token sArg;               /* Complete text of a module argument */
11781   Table **apVtabLock;       /* Pointer to virtual tables needing locking */
11782 #endif
11783   Table *pZombieTab;        /* List of Table objects to delete after code gen */
11784   TriggerPrg *pTriggerPrg;  /* Linked list of coded triggers */
11785   With *pWith;              /* Current WITH clause, or NULL */
11786   u8 bFreeWith;             /* True if pWith should be freed with parser */
11787 };
11788 
11789 /*
11790 ** Return true if currently inside an sqlite3_declare_vtab() call.
11791 */
11792 #ifdef SQLITE_OMIT_VIRTUALTABLE
11793   #define IN_DECLARE_VTAB 0
11794 #else
11795   #define IN_DECLARE_VTAB (pParse->declareVtab)
11796 #endif
11797 
11798 /*
11799 ** An instance of the following structure can be declared on a stack and used
11800 ** to save the Parse.zAuthContext value so that it can be restored later.
11801 */
11802 struct AuthContext {
11803   const char *zAuthContext;   /* Put saved Parse.zAuthContext here */
11804   Parse *pParse;              /* The Parse structure */
11805 };
11806 
11807 /*
11808 ** Bitfield flags for P5 value in various opcodes.
11809 */
11810 #define OPFLAG_NCHANGE       0x01    /* Set to update db->nChange */
11811 #define OPFLAG_LASTROWID     0x02    /* Set to update db->lastRowid */
11812 #define OPFLAG_ISUPDATE      0x04    /* This OP_Insert is an sql UPDATE */
11813 #define OPFLAG_APPEND        0x08    /* This is likely to be an append */
11814 #define OPFLAG_USESEEKRESULT 0x10    /* Try to avoid a seek in BtreeInsert() */
11815 #define OPFLAG_CLEARCACHE    0x20    /* Clear pseudo-table cache in OP_Column */
11816 #define OPFLAG_LENGTHARG     0x40    /* OP_Column only used for length() */
11817 #define OPFLAG_TYPEOFARG     0x80    /* OP_Column only used for typeof() */
11818 #define OPFLAG_BULKCSR       0x01    /* OP_Open** used to open bulk cursor */
11819 #define OPFLAG_P2ISREG       0x02    /* P2 to OP_Open** is a register number */
11820 #define OPFLAG_PERMUTE       0x01    /* OP_Compare: use the permutation */
11821 
11822 /*
11823  * Each trigger present in the database schema is stored as an instance of
11824  * struct Trigger.
11825  *
11826  * Pointers to instances of struct Trigger are stored in two ways.
11827  * 1. In the "trigHash" hash table (part of the sqlite3* that represents the
11828  *    database). This allows Trigger structures to be retrieved by name.
11829  * 2. All triggers associated with a single table form a linked list, using the
11830  *    pNext member of struct Trigger. A pointer to the first element of the
11831  *    linked list is stored as the "pTrigger" member of the associated
11832  *    struct Table.
11833  *
11834  * The "step_list" member points to the first element of a linked list
11835  * containing the SQL statements specified as the trigger program.
11836  */
11837 struct Trigger {
11838   char *zName;            /* The name of the trigger                        */
11839   char *table;            /* The table or view to which the trigger applies */
11840   u8 op;                  /* One of TK_DELETE, TK_UPDATE, TK_INSERT         */
11841   u8 tr_tm;               /* One of TRIGGER_BEFORE, TRIGGER_AFTER */
11842   Expr *pWhen;            /* The WHEN clause of the expression (may be NULL) */
11843   IdList *pColumns;       /* If this is an UPDATE OF <column-list> trigger,
11844                              the <column-list> is stored here */
11845   Schema *pSchema;        /* Schema containing the trigger */
11846   Schema *pTabSchema;     /* Schema containing the table */
11847   TriggerStep *step_list; /* Link list of trigger program steps             */
11848   Trigger *pNext;         /* Next trigger associated with the table */
11849 };
11850 
11851 /*
11852 ** A trigger is either a BEFORE or an AFTER trigger.  The following constants
11853 ** determine which.
11854 **
11855 ** If there are multiple triggers, you might of some BEFORE and some AFTER.
11856 ** In that cases, the constants below can be ORed together.
11857 */
11858 #define TRIGGER_BEFORE  1
11859 #define TRIGGER_AFTER   2
11860 
11861 /*
11862  * An instance of struct TriggerStep is used to store a single SQL statement
11863  * that is a part of a trigger-program.
11864  *
11865  * Instances of struct TriggerStep are stored in a singly linked list (linked
11866  * using the "pNext" member) referenced by the "step_list" member of the
11867  * associated struct Trigger instance. The first element of the linked list is
11868  * the first step of the trigger-program.
11869  *
11870  * The "op" member indicates whether this is a "DELETE", "INSERT", "UPDATE" or
11871  * "SELECT" statement. The meanings of the other members is determined by the
11872  * value of "op" as follows:
11873  *
11874  * (op == TK_INSERT)
11875  * orconf    -> stores the ON CONFLICT algorithm
11876  * pSelect   -> If this is an INSERT INTO ... SELECT ... statement, then
11877  *              this stores a pointer to the SELECT statement. Otherwise NULL.
11878  * target    -> A token holding the quoted name of the table to insert into.
11879  * pExprList -> If this is an INSERT INTO ... VALUES ... statement, then
11880  *              this stores values to be inserted. Otherwise NULL.
11881  * pIdList   -> If this is an INSERT INTO ... (<column-names>) VALUES ...
11882  *              statement, then this stores the column-names to be
11883  *              inserted into.
11884  *
11885  * (op == TK_DELETE)
11886  * target    -> A token holding the quoted name of the table to delete from.
11887  * pWhere    -> The WHERE clause of the DELETE statement if one is specified.
11888  *              Otherwise NULL.
11889  *
11890  * (op == TK_UPDATE)
11891  * target    -> A token holding the quoted name of the table to update rows of.
11892  * pWhere    -> The WHERE clause of the UPDATE statement if one is specified.
11893  *              Otherwise NULL.
11894  * pExprList -> A list of the columns to update and the expressions to update
11895  *              them to. See sqlite3Update() documentation of "pChanges"
11896  *              argument.
11897  *
11898  */
11899 struct TriggerStep {
11900   u8 op;               /* One of TK_DELETE, TK_UPDATE, TK_INSERT, TK_SELECT */
11901   u8 orconf;           /* OE_Rollback etc. */
11902   Trigger *pTrig;      /* The trigger that this step is a part of */
11903   Select *pSelect;     /* SELECT statment or RHS of INSERT INTO .. SELECT ... */
11904   Token target;        /* Target table for DELETE, UPDATE, INSERT */
11905   Expr *pWhere;        /* The WHERE clause for DELETE or UPDATE steps */
11906   ExprList *pExprList; /* SET clause for UPDATE. */
11907   IdList *pIdList;     /* Column names for INSERT */
11908   TriggerStep *pNext;  /* Next in the link-list */
11909   TriggerStep *pLast;  /* Last element in link-list. Valid for 1st elem only */
11910 };
11911 
11912 /*
11913 ** The following structure contains information used by the sqliteFix...
11914 ** routines as they walk the parse tree to make database references
11915 ** explicit.
11916 */
11917 typedef struct DbFixer DbFixer;
11918 struct DbFixer {
11919   Parse *pParse;      /* The parsing context.  Error messages written here */
11920   Schema *pSchema;    /* Fix items to this schema */
11921   int bVarOnly;       /* Check for variable references only */
11922   const char *zDb;    /* Make sure all objects are contained in this database */
11923   const char *zType;  /* Type of the container - used for error messages */
11924   const Token *pName; /* Name of the container - used for error messages */
11925 };
11926 
11927 /*
11928 ** An objected used to accumulate the text of a string where we
11929 ** do not necessarily know how big the string will be in the end.
11930 */
11931 struct StrAccum {
11932   sqlite3 *db;         /* Optional database for lookaside.  Can be NULL */
11933   char *zBase;         /* A base allocation.  Not from malloc. */
11934   char *zText;         /* The string collected so far */
11935   int  nChar;          /* Length of the string so far */
11936   int  nAlloc;         /* Amount of space allocated in zText */
11937   int  mxAlloc;        /* Maximum allowed string length */
11938   u8   useMalloc;      /* 0: none,  1: sqlite3DbMalloc,  2: sqlite3_malloc */
11939   u8   accError;       /* STRACCUM_NOMEM or STRACCUM_TOOBIG */
11940 };
11941 #define STRACCUM_NOMEM   1
11942 #define STRACCUM_TOOBIG  2
11943 
11944 /*
11945 ** A pointer to this structure is used to communicate information
11946 ** from sqlite3Init and OP_ParseSchema into the sqlite3InitCallback.
11947 */
11948 typedef struct {
11949   sqlite3 *db;        /* The database being initialized */
11950   char **pzErrMsg;    /* Error message stored here */
11951   int iDb;            /* 0 for main database.  1 for TEMP, 2.. for ATTACHed */
11952   int rc;             /* Result code stored here */
11953 } InitData;
11954 
11955 /*
11956 ** Structure containing global configuration data for the SQLite library.
11957 **
11958 ** This structure also contains some state information.
11959 */
11960 struct Sqlite3Config {
11961   int bMemstat;                     /* True to enable memory status */
11962   int bCoreMutex;                   /* True to enable core mutexing */
11963   int bFullMutex;                   /* True to enable full mutexing */
11964   int bOpenUri;                     /* True to interpret filenames as URIs */
11965   int bUseCis;                      /* Use covering indices for full-scans */
11966   int mxStrlen;                     /* Maximum string length */
11967   int neverCorrupt;                 /* Database is always well-formed */
11968   int szLookaside;                  /* Default lookaside buffer size */
11969   int nLookaside;                   /* Default lookaside buffer count */
11970   sqlite3_mem_methods m;            /* Low-level memory allocation interface */
11971   sqlite3_mutex_methods mutex;      /* Low-level mutex interface */
11972   sqlite3_pcache_methods2 pcache2;  /* Low-level page-cache interface */
11973   void *pHeap;                      /* Heap storage space */
11974   int nHeap;                        /* Size of pHeap[] */
11975   int mnReq, mxReq;                 /* Min and max heap requests sizes */
11976   sqlite3_int64 szMmap;             /* mmap() space per open file */
11977   sqlite3_int64 mxMmap;             /* Maximum value for szMmap */
11978   void *pScratch;                   /* Scratch memory */
11979   int szScratch;                    /* Size of each scratch buffer */
11980   int nScratch;                     /* Number of scratch buffers */
11981   void *pPage;                      /* Page cache memory */
11982   int szPage;                       /* Size of each page in pPage[] */
11983   int nPage;                        /* Number of pages in pPage[] */
11984   int mxParserStack;                /* maximum depth of the parser stack */
11985   int sharedCacheEnabled;           /* true if shared-cache mode enabled */
11986   /* The above might be initialized to non-zero.  The following need to always
11987   ** initially be zero, however. */
11988   int isInit;                       /* True after initialization has finished */
11989   int inProgress;                   /* True while initialization in progress */
11990   int isMutexInit;                  /* True after mutexes are initialized */
11991   int isMallocInit;                 /* True after malloc is initialized */
11992   int isPCacheInit;                 /* True after malloc is initialized */
11993   sqlite3_mutex *pInitMutex;        /* Mutex used by sqlite3_initialize() */
11994   int nRefInitMutex;                /* Number of users of pInitMutex */
11995   void (*xLog)(void*,int,const char*); /* Function for logging */
11996   void *pLogArg;                       /* First argument to xLog() */
11997   int bLocaltimeFault;              /* True to fail localtime() calls */
11998 #ifdef SQLITE_ENABLE_SQLLOG
11999   void(*xSqllog)(void*,sqlite3*,const char*, int);
12000   void *pSqllogArg;
12001 #endif
12002 };
12003 
12004 /*
12005 ** This macro is used inside of assert() statements to indicate that
12006 ** the assert is only valid on a well-formed database.  Instead of:
12007 **
12008 **     assert( X );
12009 **
12010 ** One writes:
12011 **
12012 **     assert( X || CORRUPT_DB );
12013 **
12014 ** CORRUPT_DB is true during normal operation.  CORRUPT_DB does not indicate
12015 ** that the database is definitely corrupt, only that it might be corrupt.
12016 ** For most test cases, CORRUPT_DB is set to false using a special
12017 ** sqlite3_test_control().  This enables assert() statements to prove
12018 ** things that are always true for well-formed databases.
12019 */
12020 #define CORRUPT_DB  (sqlite3Config.neverCorrupt==0)
12021 
12022 /*
12023 ** Context pointer passed down through the tree-walk.
12024 */
12025 struct Walker {
12026   int (*xExprCallback)(Walker*, Expr*);     /* Callback for expressions */
12027   int (*xSelectCallback)(Walker*,Select*);  /* Callback for SELECTs */
12028   void (*xSelectCallback2)(Walker*,Select*);/* Second callback for SELECTs */
12029   Parse *pParse;                            /* Parser context.  */
12030   int walkerDepth;                          /* Number of subqueries */
12031   union {                                   /* Extra data for callback */
12032     NameContext *pNC;                          /* Naming context */
12033     int i;                                     /* Integer value */
12034     SrcList *pSrcList;                         /* FROM clause */
12035     struct SrcCount *pSrcCount;                /* Counting column references */
12036   } u;
12037 };
12038 
12039 /* Forward declarations */
12040 SQLITE_PRIVATE int sqlite3WalkExpr(Walker*, Expr*);
12041 SQLITE_PRIVATE int sqlite3WalkExprList(Walker*, ExprList*);
12042 SQLITE_PRIVATE int sqlite3WalkSelect(Walker*, Select*);
12043 SQLITE_PRIVATE int sqlite3WalkSelectExpr(Walker*, Select*);
12044 SQLITE_PRIVATE int sqlite3WalkSelectFrom(Walker*, Select*);
12045 
12046 /*
12047 ** Return code from the parse-tree walking primitives and their
12048 ** callbacks.
12049 */
12050 #define WRC_Continue    0   /* Continue down into children */
12051 #define WRC_Prune       1   /* Omit children but continue walking siblings */
12052 #define WRC_Abort       2   /* Abandon the tree walk */
12053 
12054 /*
12055 ** An instance of this structure represents a set of one or more CTEs
12056 ** (common table expressions) created by a single WITH clause.
12057 */
12058 struct With {
12059   int nCte;                       /* Number of CTEs in the WITH clause */
12060   With *pOuter;                   /* Containing WITH clause, or NULL */
12061   struct Cte {                    /* For each CTE in the WITH clause.... */
12062     char *zName;                    /* Name of this CTE */
12063     ExprList *pCols;                /* List of explicit column names, or NULL */
12064     Select *pSelect;                /* The definition of this CTE */
12065     const char *zErr;               /* Error message for circular references */
12066   } a[1];
12067 };
12068 
12069 /*
12070 ** Assuming zIn points to the first byte of a UTF-8 character,
12071 ** advance zIn to point to the first byte of the next UTF-8 character.
12072 */
12073 #define SQLITE_SKIP_UTF8(zIn) {                        \
12074   if( (*(zIn++))>=0xc0 ){                              \
12075     while( (*zIn & 0xc0)==0x80 ){ zIn++; }             \
12076   }                                                    \
12077 }
12078 
12079 /*
12080 ** The SQLITE_*_BKPT macros are substitutes for the error codes with
12081 ** the same name but without the _BKPT suffix.  These macros invoke
12082 ** routines that report the line-number on which the error originated
12083 ** using sqlite3_log().  The routines also provide a convenient place
12084 ** to set a debugger breakpoint.
12085 */
12086 SQLITE_PRIVATE int sqlite3CorruptError(int);
12087 SQLITE_PRIVATE int sqlite3MisuseError(int);
12088 SQLITE_PRIVATE int sqlite3CantopenError(int);
12089 #define SQLITE_CORRUPT_BKPT sqlite3CorruptError(__LINE__)
12090 #define SQLITE_MISUSE_BKPT sqlite3MisuseError(__LINE__)
12091 #define SQLITE_CANTOPEN_BKPT sqlite3CantopenError(__LINE__)
12092 
12093 
12094 /*
12095 ** FTS4 is really an extension for FTS3.  It is enabled using the
12096 ** SQLITE_ENABLE_FTS3 macro.  But to avoid confusion we also all
12097 ** the SQLITE_ENABLE_FTS4 macro to serve as an alisse for SQLITE_ENABLE_FTS3.
12098 */
12099 #if defined(SQLITE_ENABLE_FTS4) && !defined(SQLITE_ENABLE_FTS3)
12100 # define SQLITE_ENABLE_FTS3
12101 #endif
12102 
12103 /*
12104 ** The ctype.h header is needed for non-ASCII systems.  It is also
12105 ** needed by FTS3 when FTS3 is included in the amalgamation.
12106 */
12107 #if !defined(SQLITE_ASCII) || \
12108     (defined(SQLITE_ENABLE_FTS3) && defined(SQLITE_AMALGAMATION))
12109 # include <ctype.h>
12110 #endif
12111 
12112 /*
12113 ** The following macros mimic the standard library functions toupper(),
12114 ** isspace(), isalnum(), isdigit() and isxdigit(), respectively. The
12115 ** sqlite versions only work for ASCII characters, regardless of locale.
12116 */
12117 #ifdef SQLITE_ASCII
12118 # define sqlite3Toupper(x)  ((x)&~(sqlite3CtypeMap[(unsigned char)(x)]&0x20))
12119 # define sqlite3Isspace(x)   (sqlite3CtypeMap[(unsigned char)(x)]&0x01)
12120 # define sqlite3Isalnum(x)   (sqlite3CtypeMap[(unsigned char)(x)]&0x06)
12121 # define sqlite3Isalpha(x)   (sqlite3CtypeMap[(unsigned char)(x)]&0x02)
12122 # define sqlite3Isdigit(x)   (sqlite3CtypeMap[(unsigned char)(x)]&0x04)
12123 # define sqlite3Isxdigit(x)  (sqlite3CtypeMap[(unsigned char)(x)]&0x08)
12124 # define sqlite3Tolower(x)   (sqlite3UpperToLower[(unsigned char)(x)])
12125 #else
12126 # define sqlite3Toupper(x)   toupper((unsigned char)(x))
12127 # define sqlite3Isspace(x)   isspace((unsigned char)(x))
12128 # define sqlite3Isalnum(x)   isalnum((unsigned char)(x))
12129 # define sqlite3Isalpha(x)   isalpha((unsigned char)(x))
12130 # define sqlite3Isdigit(x)   isdigit((unsigned char)(x))
12131 # define sqlite3Isxdigit(x)  isxdigit((unsigned char)(x))
12132 # define sqlite3Tolower(x)   tolower((unsigned char)(x))
12133 #endif
12134 
12135 /*
12136 ** Internal function prototypes
12137 */
12138 #define sqlite3StrICmp sqlite3_stricmp
12139 SQLITE_PRIVATE int sqlite3Strlen30(const char*);
12140 #define sqlite3StrNICmp sqlite3_strnicmp
12141 
12142 SQLITE_PRIVATE int sqlite3MallocInit(void);
12143 SQLITE_PRIVATE void sqlite3MallocEnd(void);
12144 SQLITE_PRIVATE void *sqlite3Malloc(int);
12145 SQLITE_PRIVATE void *sqlite3MallocZero(int);
12146 SQLITE_PRIVATE void *sqlite3DbMallocZero(sqlite3*, int);
12147 SQLITE_PRIVATE void *sqlite3DbMallocRaw(sqlite3*, int);
12148 SQLITE_PRIVATE char *sqlite3DbStrDup(sqlite3*,const char*);
12149 SQLITE_PRIVATE char *sqlite3DbStrNDup(sqlite3*,const char*, int);
12150 SQLITE_PRIVATE void *sqlite3Realloc(void*, int);
12151 SQLITE_PRIVATE void *sqlite3DbReallocOrFree(sqlite3 *, void *, int);
12152 SQLITE_PRIVATE void *sqlite3DbRealloc(sqlite3 *, void *, int);
12153 SQLITE_PRIVATE void sqlite3DbFree(sqlite3*, void*);
12154 SQLITE_PRIVATE int sqlite3MallocSize(void*);
12155 SQLITE_PRIVATE int sqlite3DbMallocSize(sqlite3*, void*);
12156 SQLITE_PRIVATE void *sqlite3ScratchMalloc(int);
12157 SQLITE_PRIVATE void sqlite3ScratchFree(void*);
12158 SQLITE_PRIVATE void *sqlite3PageMalloc(int);
12159 SQLITE_PRIVATE void sqlite3PageFree(void*);
12160 SQLITE_PRIVATE void sqlite3MemSetDefault(void);
12161 SQLITE_PRIVATE void sqlite3BenignMallocHooks(void (*)(void), void (*)(void));
12162 SQLITE_PRIVATE int sqlite3HeapNearlyFull(void);
12163 
12164 /*
12165 ** On systems with ample stack space and that support alloca(), make
12166 ** use of alloca() to obtain space for large automatic objects.  By default,
12167 ** obtain space from malloc().
12168 **
12169 ** The alloca() routine never returns NULL.  This will cause code paths
12170 ** that deal with sqlite3StackAlloc() failures to be unreachable.
12171 */
12172 #ifdef SQLITE_USE_ALLOCA
12173 # define sqlite3StackAllocRaw(D,N)   alloca(N)
12174 # define sqlite3StackAllocZero(D,N)  memset(alloca(N), 0, N)
12175 # define sqlite3StackFree(D,P)
12176 #else
12177 # define sqlite3StackAllocRaw(D,N)   sqlite3DbMallocRaw(D,N)
12178 # define sqlite3StackAllocZero(D,N)  sqlite3DbMallocZero(D,N)
12179 # define sqlite3StackFree(D,P)       sqlite3DbFree(D,P)
12180 #endif
12181 
12182 #ifdef SQLITE_ENABLE_MEMSYS3
12183 SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys3(void);
12184 #endif
12185 #ifdef SQLITE_ENABLE_MEMSYS5
12186 SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys5(void);
12187 #endif
12188 
12189 
12190 #ifndef SQLITE_MUTEX_OMIT
12191 SQLITE_PRIVATE   sqlite3_mutex_methods const *sqlite3DefaultMutex(void);
12192 SQLITE_PRIVATE   sqlite3_mutex_methods const *sqlite3NoopMutex(void);
12193 SQLITE_PRIVATE   sqlite3_mutex *sqlite3MutexAlloc(int);
12194 SQLITE_PRIVATE   int sqlite3MutexInit(void);
12195 SQLITE_PRIVATE   int sqlite3MutexEnd(void);
12196 #endif
12197 
12198 SQLITE_PRIVATE int sqlite3StatusValue(int);
12199 SQLITE_PRIVATE void sqlite3StatusAdd(int, int);
12200 SQLITE_PRIVATE void sqlite3StatusSet(int, int);
12201 
12202 #ifndef SQLITE_OMIT_FLOATING_POINT
12203 SQLITE_PRIVATE   int sqlite3IsNaN(double);
12204 #else
12205 # define sqlite3IsNaN(X)  0
12206 #endif
12207 
12208 /*
12209 ** An instance of the following structure holds information about SQL
12210 ** functions arguments that are the parameters to the printf() function.
12211 */
12212 struct PrintfArguments {
12213   int nArg;                /* Total number of arguments */
12214   int nUsed;               /* Number of arguments used so far */
12215   sqlite3_value **apArg;   /* The argument values */
12216 };
12217 
12218 #define SQLITE_PRINTF_INTERNAL 0x01
12219 #define SQLITE_PRINTF_SQLFUNC  0x02
12220 SQLITE_PRIVATE void sqlite3VXPrintf(StrAccum*, u32, const char*, va_list);
12221 SQLITE_PRIVATE void sqlite3XPrintf(StrAccum*, u32, const char*, ...);
12222 SQLITE_PRIVATE char *sqlite3MPrintf(sqlite3*,const char*, ...);
12223 SQLITE_PRIVATE char *sqlite3VMPrintf(sqlite3*,const char*, va_list);
12224 SQLITE_PRIVATE char *sqlite3MAppendf(sqlite3*,char*,const char*,...);
12225 #if defined(SQLITE_TEST) || defined(SQLITE_DEBUG)
12226 SQLITE_PRIVATE   void sqlite3DebugPrintf(const char*, ...);
12227 #endif
12228 #if defined(SQLITE_TEST)
12229 SQLITE_PRIVATE   void *sqlite3TestTextToPtr(const char*);
12230 #endif
12231 
12232 /* Output formatting for SQLITE_TESTCTRL_EXPLAIN */
12233 #if defined(SQLITE_ENABLE_TREE_EXPLAIN)
12234 SQLITE_PRIVATE   void sqlite3ExplainBegin(Vdbe*);
12235 SQLITE_PRIVATE   void sqlite3ExplainPrintf(Vdbe*, const char*, ...);
12236 SQLITE_PRIVATE   void sqlite3ExplainNL(Vdbe*);
12237 SQLITE_PRIVATE   void sqlite3ExplainPush(Vdbe*);
12238 SQLITE_PRIVATE   void sqlite3ExplainPop(Vdbe*);
12239 SQLITE_PRIVATE   void sqlite3ExplainFinish(Vdbe*);
12240 SQLITE_PRIVATE   void sqlite3ExplainSelect(Vdbe*, Select*);
12241 SQLITE_PRIVATE   void sqlite3ExplainExpr(Vdbe*, Expr*);
12242 SQLITE_PRIVATE   void sqlite3ExplainExprList(Vdbe*, ExprList*);
12243 SQLITE_PRIVATE   const char *sqlite3VdbeExplanation(Vdbe*);
12244 #else
12245 # define sqlite3ExplainBegin(X)
12246 # define sqlite3ExplainSelect(A,B)
12247 # define sqlite3ExplainExpr(A,B)
12248 # define sqlite3ExplainExprList(A,B)
12249 # define sqlite3ExplainFinish(X)
12250 # define sqlite3VdbeExplanation(X) 0
12251 #endif
12252 
12253 
12254 SQLITE_PRIVATE void sqlite3SetString(char **, sqlite3*, const char*, ...);
12255 SQLITE_PRIVATE void sqlite3ErrorMsg(Parse*, const char*, ...);
12256 SQLITE_PRIVATE int sqlite3Dequote(char*);
12257 SQLITE_PRIVATE int sqlite3KeywordCode(const unsigned char*, int);
12258 SQLITE_PRIVATE int sqlite3RunParser(Parse*, const char*, char **);
12259 SQLITE_PRIVATE void sqlite3FinishCoding(Parse*);
12260 SQLITE_PRIVATE int sqlite3GetTempReg(Parse*);
12261 SQLITE_PRIVATE void sqlite3ReleaseTempReg(Parse*,int);
12262 SQLITE_PRIVATE int sqlite3GetTempRange(Parse*,int);
12263 SQLITE_PRIVATE void sqlite3ReleaseTempRange(Parse*,int,int);
12264 SQLITE_PRIVATE void sqlite3ClearTempRegCache(Parse*);
12265 SQLITE_PRIVATE Expr *sqlite3ExprAlloc(sqlite3*,int,const Token*,int);
12266 SQLITE_PRIVATE Expr *sqlite3Expr(sqlite3*,int,const char*);
12267 SQLITE_PRIVATE void sqlite3ExprAttachSubtrees(sqlite3*,Expr*,Expr*,Expr*);
12268 SQLITE_PRIVATE Expr *sqlite3PExpr(Parse*, int, Expr*, Expr*, const Token*);
12269 SQLITE_PRIVATE Expr *sqlite3ExprAnd(sqlite3*,Expr*, Expr*);
12270 SQLITE_PRIVATE Expr *sqlite3ExprFunction(Parse*,ExprList*, Token*);
12271 SQLITE_PRIVATE void sqlite3ExprAssignVarNumber(Parse*, Expr*);
12272 SQLITE_PRIVATE void sqlite3ExprDelete(sqlite3*, Expr*);
12273 SQLITE_PRIVATE ExprList *sqlite3ExprListAppend(Parse*,ExprList*,Expr*);
12274 SQLITE_PRIVATE void sqlite3ExprListSetName(Parse*,ExprList*,Token*,int);
12275 SQLITE_PRIVATE void sqlite3ExprListSetSpan(Parse*,ExprList*,ExprSpan*);
12276 SQLITE_PRIVATE void sqlite3ExprListDelete(sqlite3*, ExprList*);
12277 SQLITE_PRIVATE int sqlite3Init(sqlite3*, char**);
12278 SQLITE_PRIVATE int sqlite3InitCallback(void*, int, char**, char**);
12279 SQLITE_PRIVATE void sqlite3Pragma(Parse*,Token*,Token*,Token*,int);
12280 SQLITE_PRIVATE void sqlite3ResetAllSchemasOfConnection(sqlite3*);
12281 SQLITE_PRIVATE void sqlite3ResetOneSchema(sqlite3*,int);
12282 SQLITE_PRIVATE void sqlite3CollapseDatabaseArray(sqlite3*);
12283 SQLITE_PRIVATE void sqlite3BeginParse(Parse*,int);
12284 SQLITE_PRIVATE void sqlite3CommitInternalChanges(sqlite3*);
12285 SQLITE_PRIVATE Table *sqlite3ResultSetOfSelect(Parse*,Select*);
12286 SQLITE_PRIVATE void sqlite3OpenMasterTable(Parse *, int);
12287 SQLITE_PRIVATE Index *sqlite3PrimaryKeyIndex(Table*);
12288 SQLITE_PRIVATE i16 sqlite3ColumnOfIndex(Index*, i16);
12289 SQLITE_PRIVATE void sqlite3StartTable(Parse*,Token*,Token*,int,int,int,int);
12290 SQLITE_PRIVATE void sqlite3AddColumn(Parse*,Token*);
12291 SQLITE_PRIVATE void sqlite3AddNotNull(Parse*, int);
12292 SQLITE_PRIVATE void sqlite3AddPrimaryKey(Parse*, ExprList*, int, int, int);
12293 SQLITE_PRIVATE void sqlite3AddCheckConstraint(Parse*, Expr*);
12294 SQLITE_PRIVATE void sqlite3AddColumnType(Parse*,Token*);
12295 SQLITE_PRIVATE void sqlite3AddDefaultValue(Parse*,ExprSpan*);
12296 SQLITE_PRIVATE void sqlite3AddCollateType(Parse*, Token*);
12297 SQLITE_PRIVATE void sqlite3EndTable(Parse*,Token*,Token*,u8,Select*);
12298 SQLITE_PRIVATE int sqlite3ParseUri(const char*,const char*,unsigned int*,
12299                     sqlite3_vfs**,char**,char **);
12300 SQLITE_PRIVATE Btree *sqlite3DbNameToBtree(sqlite3*,const char*);
12301 SQLITE_PRIVATE int sqlite3CodeOnce(Parse *);
12302 
12303 SQLITE_PRIVATE Bitvec *sqlite3BitvecCreate(u32);
12304 SQLITE_PRIVATE int sqlite3BitvecTest(Bitvec*, u32);
12305 SQLITE_PRIVATE int sqlite3BitvecSet(Bitvec*, u32);
12306 SQLITE_PRIVATE void sqlite3BitvecClear(Bitvec*, u32, void*);
12307 SQLITE_PRIVATE void sqlite3BitvecDestroy(Bitvec*);
12308 SQLITE_PRIVATE u32 sqlite3BitvecSize(Bitvec*);
12309 SQLITE_PRIVATE int sqlite3BitvecBuiltinTest(int,int*);
12310 
12311 SQLITE_PRIVATE RowSet *sqlite3RowSetInit(sqlite3*, void*, unsigned int);
12312 SQLITE_PRIVATE void sqlite3RowSetClear(RowSet*);
12313 SQLITE_PRIVATE void sqlite3RowSetInsert(RowSet*, i64);
12314 SQLITE_PRIVATE int sqlite3RowSetTest(RowSet*, u8 iBatch, i64);
12315 SQLITE_PRIVATE int sqlite3RowSetNext(RowSet*, i64*);
12316 
12317 SQLITE_PRIVATE void sqlite3CreateView(Parse*,Token*,Token*,Token*,Select*,int,int);
12318 
12319 #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
12320 SQLITE_PRIVATE   int sqlite3ViewGetColumnNames(Parse*,Table*);
12321 #else
12322 # define sqlite3ViewGetColumnNames(A,B) 0
12323 #endif
12324 
12325 SQLITE_PRIVATE void sqlite3DropTable(Parse*, SrcList*, int, int);
12326 SQLITE_PRIVATE void sqlite3CodeDropTable(Parse*, Table*, int, int);
12327 SQLITE_PRIVATE void sqlite3DeleteTable(sqlite3*, Table*);
12328 #ifndef SQLITE_OMIT_AUTOINCREMENT
12329 SQLITE_PRIVATE   void sqlite3AutoincrementBegin(Parse *pParse);
12330 SQLITE_PRIVATE   void sqlite3AutoincrementEnd(Parse *pParse);
12331 #else
12332 # define sqlite3AutoincrementBegin(X)
12333 # define sqlite3AutoincrementEnd(X)
12334 #endif
12335 SQLITE_PRIVATE int sqlite3CodeCoroutine(Parse*, Select*, SelectDest*);
12336 SQLITE_PRIVATE void sqlite3Insert(Parse*, SrcList*, Select*, IdList*, int);
12337 SQLITE_PRIVATE void *sqlite3ArrayAllocate(sqlite3*,void*,int,int*,int*);
12338 SQLITE_PRIVATE IdList *sqlite3IdListAppend(sqlite3*, IdList*, Token*);
12339 SQLITE_PRIVATE int sqlite3IdListIndex(IdList*,const char*);
12340 SQLITE_PRIVATE SrcList *sqlite3SrcListEnlarge(sqlite3*, SrcList*, int, int);
12341 SQLITE_PRIVATE SrcList *sqlite3SrcListAppend(sqlite3*, SrcList*, Token*, Token*);
12342 SQLITE_PRIVATE SrcList *sqlite3SrcListAppendFromTerm(Parse*, SrcList*, Token*, Token*,
12343                                       Token*, Select*, Expr*, IdList*);
12344 SQLITE_PRIVATE void sqlite3SrcListIndexedBy(Parse *, SrcList *, Token *);
12345 SQLITE_PRIVATE int sqlite3IndexedByLookup(Parse *, struct SrcList_item *);
12346 SQLITE_PRIVATE void sqlite3SrcListShiftJoinType(SrcList*);
12347 SQLITE_PRIVATE void sqlite3SrcListAssignCursors(Parse*, SrcList*);
12348 SQLITE_PRIVATE void sqlite3IdListDelete(sqlite3*, IdList*);
12349 SQLITE_PRIVATE void sqlite3SrcListDelete(sqlite3*, SrcList*);
12350 SQLITE_PRIVATE Index *sqlite3AllocateIndexObject(sqlite3*,i16,int,char**);
12351 SQLITE_PRIVATE Index *sqlite3CreateIndex(Parse*,Token*,Token*,SrcList*,ExprList*,int,Token*,
12352                           Expr*, int, int);
12353 SQLITE_PRIVATE void sqlite3DropIndex(Parse*, SrcList*, int);
12354 SQLITE_PRIVATE int sqlite3Select(Parse*, Select*, SelectDest*);
12355 SQLITE_PRIVATE Select *sqlite3SelectNew(Parse*,ExprList*,SrcList*,Expr*,ExprList*,
12356                          Expr*,ExprList*,u16,Expr*,Expr*);
12357 SQLITE_PRIVATE void sqlite3SelectDelete(sqlite3*, Select*);
12358 SQLITE_PRIVATE Table *sqlite3SrcListLookup(Parse*, SrcList*);
12359 SQLITE_PRIVATE int sqlite3IsReadOnly(Parse*, Table*, int);
12360 SQLITE_PRIVATE void sqlite3OpenTable(Parse*, int iCur, int iDb, Table*, int);
12361 #if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY)
12362 SQLITE_PRIVATE Expr *sqlite3LimitWhere(Parse*,SrcList*,Expr*,ExprList*,Expr*,Expr*,char*);
12363 #endif
12364 SQLITE_PRIVATE void sqlite3DeleteFrom(Parse*, SrcList*, Expr*);
12365 SQLITE_PRIVATE void sqlite3Update(Parse*, SrcList*, ExprList*, Expr*, int);
12366 SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(Parse*,SrcList*,Expr*,ExprList*,ExprList*,u16,int);
12367 SQLITE_PRIVATE void sqlite3WhereEnd(WhereInfo*);
12368 SQLITE_PRIVATE u64 sqlite3WhereOutputRowCount(WhereInfo*);
12369 SQLITE_PRIVATE int sqlite3WhereIsDistinct(WhereInfo*);
12370 SQLITE_PRIVATE int sqlite3WhereIsOrdered(WhereInfo*);
12371 SQLITE_PRIVATE int sqlite3WhereContinueLabel(WhereInfo*);
12372 SQLITE_PRIVATE int sqlite3WhereBreakLabel(WhereInfo*);
12373 SQLITE_PRIVATE int sqlite3WhereOkOnePass(WhereInfo*, int*);
12374 SQLITE_PRIVATE int sqlite3ExprCodeGetColumn(Parse*, Table*, int, int, int, u8);
12375 SQLITE_PRIVATE void sqlite3ExprCodeGetColumnOfTable(Vdbe*, Table*, int, int, int);
12376 SQLITE_PRIVATE void sqlite3ExprCodeMove(Parse*, int, int, int);
12377 SQLITE_PRIVATE void sqlite3ExprCacheStore(Parse*, int, int, int);
12378 SQLITE_PRIVATE void sqlite3ExprCachePush(Parse*);
12379 SQLITE_PRIVATE void sqlite3ExprCachePop(Parse*, int);
12380 SQLITE_PRIVATE void sqlite3ExprCacheRemove(Parse*, int, int);
12381 SQLITE_PRIVATE void sqlite3ExprCacheClear(Parse*);
12382 SQLITE_PRIVATE void sqlite3ExprCacheAffinityChange(Parse*, int, int);
12383 SQLITE_PRIVATE int sqlite3ExprCode(Parse*, Expr*, int);
12384 SQLITE_PRIVATE void sqlite3ExprCodeAtInit(Parse*, Expr*, int, u8);
12385 SQLITE_PRIVATE int sqlite3ExprCodeTemp(Parse*, Expr*, int*);
12386 SQLITE_PRIVATE int sqlite3ExprCodeTarget(Parse*, Expr*, int);
12387 SQLITE_PRIVATE int sqlite3ExprCodeAndCache(Parse*, Expr*, int);
12388 SQLITE_PRIVATE int sqlite3ExprCodeExprList(Parse*, ExprList*, int, u8);
12389 #define SQLITE_ECEL_DUP      0x01  /* Deep, not shallow copies */
12390 #define SQLITE_ECEL_FACTOR   0x02  /* Factor out constant terms */
12391 SQLITE_PRIVATE void sqlite3ExprIfTrue(Parse*, Expr*, int, int);
12392 SQLITE_PRIVATE void sqlite3ExprIfFalse(Parse*, Expr*, int, int);
12393 SQLITE_PRIVATE Table *sqlite3FindTable(sqlite3*,const char*, const char*);
12394 SQLITE_PRIVATE Table *sqlite3LocateTable(Parse*,int isView,const char*, const char*);
12395 SQLITE_PRIVATE Table *sqlite3LocateTableItem(Parse*,int isView,struct SrcList_item *);
12396 SQLITE_PRIVATE Index *sqlite3FindIndex(sqlite3*,const char*, const char*);
12397 SQLITE_PRIVATE void sqlite3UnlinkAndDeleteTable(sqlite3*,int,const char*);
12398 SQLITE_PRIVATE void sqlite3UnlinkAndDeleteIndex(sqlite3*,int,const char*);
12399 SQLITE_PRIVATE void sqlite3Vacuum(Parse*);
12400 SQLITE_PRIVATE int sqlite3RunVacuum(char**, sqlite3*);
12401 SQLITE_PRIVATE char *sqlite3NameFromToken(sqlite3*, Token*);
12402 SQLITE_PRIVATE int sqlite3ExprCompare(Expr*, Expr*, int);
12403 SQLITE_PRIVATE int sqlite3ExprListCompare(ExprList*, ExprList*, int);
12404 SQLITE_PRIVATE int sqlite3ExprImpliesExpr(Expr*, Expr*, int);
12405 SQLITE_PRIVATE void sqlite3ExprAnalyzeAggregates(NameContext*, Expr*);
12406 SQLITE_PRIVATE void sqlite3ExprAnalyzeAggList(NameContext*,ExprList*);
12407 SQLITE_PRIVATE int sqlite3FunctionUsesThisSrc(Expr*, SrcList*);
12408 SQLITE_PRIVATE Vdbe *sqlite3GetVdbe(Parse*);
12409 SQLITE_PRIVATE void sqlite3PrngSaveState(void);
12410 SQLITE_PRIVATE void sqlite3PrngRestoreState(void);
12411 SQLITE_PRIVATE void sqlite3RollbackAll(sqlite3*,int);
12412 SQLITE_PRIVATE void sqlite3CodeVerifySchema(Parse*, int);
12413 SQLITE_PRIVATE void sqlite3CodeVerifyNamedSchema(Parse*, const char *zDb);
12414 SQLITE_PRIVATE void sqlite3BeginTransaction(Parse*, int);
12415 SQLITE_PRIVATE void sqlite3CommitTransaction(Parse*);
12416 SQLITE_PRIVATE void sqlite3RollbackTransaction(Parse*);
12417 SQLITE_PRIVATE void sqlite3Savepoint(Parse*, int, Token*);
12418 SQLITE_PRIVATE void sqlite3CloseSavepoints(sqlite3 *);
12419 SQLITE_PRIVATE void sqlite3LeaveMutexAndCloseZombie(sqlite3*);
12420 SQLITE_PRIVATE int sqlite3ExprIsConstant(Expr*);
12421 SQLITE_PRIVATE int sqlite3ExprIsConstantNotJoin(Expr*);
12422 SQLITE_PRIVATE int sqlite3ExprIsConstantOrFunction(Expr*);
12423 SQLITE_PRIVATE int sqlite3ExprIsInteger(Expr*, int*);
12424 SQLITE_PRIVATE int sqlite3ExprCanBeNull(const Expr*);
12425 SQLITE_PRIVATE void sqlite3ExprCodeIsNullJump(Vdbe*, const Expr*, int, int);
12426 SQLITE_PRIVATE int sqlite3ExprNeedsNoAffinityChange(const Expr*, char);
12427 SQLITE_PRIVATE int sqlite3IsRowid(const char*);
12428 SQLITE_PRIVATE void sqlite3GenerateRowDelete(Parse*,Table*,Trigger*,int,int,int,i16,u8,u8,u8);
12429 SQLITE_PRIVATE void sqlite3GenerateRowIndexDelete(Parse*, Table*, int, int, int*);
12430 SQLITE_PRIVATE int sqlite3GenerateIndexKey(Parse*, Index*, int, int, int, int*,Index*,int);
12431 SQLITE_PRIVATE void sqlite3GenerateConstraintChecks(Parse*,Table*,int*,int,int,int,int,
12432                                      u8,u8,int,int*);
12433 SQLITE_PRIVATE void sqlite3CompleteInsertion(Parse*,Table*,int,int,int,int*,int,int,int);
12434 SQLITE_PRIVATE int sqlite3OpenTableAndIndices(Parse*, Table*, int, int, u8*, int*, int*);
12435 SQLITE_PRIVATE void sqlite3BeginWriteOperation(Parse*, int, int);
12436 SQLITE_PRIVATE void sqlite3MultiWrite(Parse*);
12437 SQLITE_PRIVATE void sqlite3MayAbort(Parse*);
12438 SQLITE_PRIVATE void sqlite3HaltConstraint(Parse*, int, int, char*, i8, u8);
12439 SQLITE_PRIVATE void sqlite3UniqueConstraint(Parse*, int, Index*);
12440 SQLITE_PRIVATE void sqlite3RowidConstraint(Parse*, int, Table*);
12441 SQLITE_PRIVATE Expr *sqlite3ExprDup(sqlite3*,Expr*,int);
12442 SQLITE_PRIVATE ExprList *sqlite3ExprListDup(sqlite3*,ExprList*,int);
12443 SQLITE_PRIVATE SrcList *sqlite3SrcListDup(sqlite3*,SrcList*,int);
12444 SQLITE_PRIVATE IdList *sqlite3IdListDup(sqlite3*,IdList*);
12445 SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3*,Select*,int);
12446 SQLITE_PRIVATE void sqlite3FuncDefInsert(FuncDefHash*, FuncDef*);
12447 SQLITE_PRIVATE FuncDef *sqlite3FindFunction(sqlite3*,const char*,int,int,u8,u8);
12448 SQLITE_PRIVATE void sqlite3RegisterBuiltinFunctions(sqlite3*);
12449 SQLITE_PRIVATE void sqlite3RegisterDateTimeFunctions(void);
12450 SQLITE_PRIVATE void sqlite3RegisterGlobalFunctions(void);
12451 SQLITE_PRIVATE int sqlite3SafetyCheckOk(sqlite3*);
12452 SQLITE_PRIVATE int sqlite3SafetyCheckSickOrOk(sqlite3*);
12453 SQLITE_PRIVATE void sqlite3ChangeCookie(Parse*, int);
12454 
12455 #if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
12456 SQLITE_PRIVATE void sqlite3MaterializeView(Parse*, Table*, Expr*, int);
12457 #endif
12458 
12459 #ifndef SQLITE_OMIT_TRIGGER
12460 SQLITE_PRIVATE   void sqlite3BeginTrigger(Parse*, Token*,Token*,int,int,IdList*,SrcList*,
12461                            Expr*,int, int);
12462 SQLITE_PRIVATE   void sqlite3FinishTrigger(Parse*, TriggerStep*, Token*);
12463 SQLITE_PRIVATE   void sqlite3DropTrigger(Parse*, SrcList*, int);
12464 SQLITE_PRIVATE   void sqlite3DropTriggerPtr(Parse*, Trigger*);
12465 SQLITE_PRIVATE   Trigger *sqlite3TriggersExist(Parse *, Table*, int, ExprList*, int *pMask);
12466 SQLITE_PRIVATE   Trigger *sqlite3TriggerList(Parse *, Table *);
12467 SQLITE_PRIVATE   void sqlite3CodeRowTrigger(Parse*, Trigger *, int, ExprList*, int, Table *,
12468                             int, int, int);
12469 SQLITE_PRIVATE   void sqlite3CodeRowTriggerDirect(Parse *, Trigger *, Table *, int, int, int);
12470   void sqliteViewTriggers(Parse*, Table*, Expr*, int, ExprList*);
12471 SQLITE_PRIVATE   void sqlite3DeleteTriggerStep(sqlite3*, TriggerStep*);
12472 SQLITE_PRIVATE   TriggerStep *sqlite3TriggerSelectStep(sqlite3*,Select*);
12473 SQLITE_PRIVATE   TriggerStep *sqlite3TriggerInsertStep(sqlite3*,Token*, IdList*,
12474                                         Select*,u8);
12475 SQLITE_PRIVATE   TriggerStep *sqlite3TriggerUpdateStep(sqlite3*,Token*,ExprList*, Expr*, u8);
12476 SQLITE_PRIVATE   TriggerStep *sqlite3TriggerDeleteStep(sqlite3*,Token*, Expr*);
12477 SQLITE_PRIVATE   void sqlite3DeleteTrigger(sqlite3*, Trigger*);
12478 SQLITE_PRIVATE   void sqlite3UnlinkAndDeleteTrigger(sqlite3*,int,const char*);
12479 SQLITE_PRIVATE   u32 sqlite3TriggerColmask(Parse*,Trigger*,ExprList*,int,int,Table*,int);
12480 # define sqlite3ParseToplevel(p) ((p)->pToplevel ? (p)->pToplevel : (p))
12481 #else
12482 # define sqlite3TriggersExist(B,C,D,E,F) 0
12483 # define sqlite3DeleteTrigger(A,B)
12484 # define sqlite3DropTriggerPtr(A,B)
12485 # define sqlite3UnlinkAndDeleteTrigger(A,B,C)
12486 # define sqlite3CodeRowTrigger(A,B,C,D,E,F,G,H,I)
12487 # define sqlite3CodeRowTriggerDirect(A,B,C,D,E,F)
12488 # define sqlite3TriggerList(X, Y) 0
12489 # define sqlite3ParseToplevel(p) p
12490 # define sqlite3TriggerColmask(A,B,C,D,E,F,G) 0
12491 #endif
12492 
12493 SQLITE_PRIVATE int sqlite3JoinType(Parse*, Token*, Token*, Token*);
12494 SQLITE_PRIVATE void sqlite3CreateForeignKey(Parse*, ExprList*, Token*, ExprList*, int);
12495 SQLITE_PRIVATE void sqlite3DeferForeignKey(Parse*, int);
12496 #ifndef SQLITE_OMIT_AUTHORIZATION
12497 SQLITE_PRIVATE   void sqlite3AuthRead(Parse*,Expr*,Schema*,SrcList*);
12498 SQLITE_PRIVATE   int sqlite3AuthCheck(Parse*,int, const char*, const char*, const char*);
12499 SQLITE_PRIVATE   void sqlite3AuthContextPush(Parse*, AuthContext*, const char*);
12500 SQLITE_PRIVATE   void sqlite3AuthContextPop(AuthContext*);
12501 SQLITE_PRIVATE   int sqlite3AuthReadCol(Parse*, const char *, const char *, int);
12502 #else
12503 # define sqlite3AuthRead(a,b,c,d)
12504 # define sqlite3AuthCheck(a,b,c,d,e)    SQLITE_OK
12505 # define sqlite3AuthContextPush(a,b,c)
12506 # define sqlite3AuthContextPop(a)  ((void)(a))
12507 #endif
12508 SQLITE_PRIVATE void sqlite3Attach(Parse*, Expr*, Expr*, Expr*);
12509 SQLITE_PRIVATE void sqlite3Detach(Parse*, Expr*);
12510 SQLITE_PRIVATE void sqlite3FixInit(DbFixer*, Parse*, int, const char*, const Token*);
12511 SQLITE_PRIVATE int sqlite3FixSrcList(DbFixer*, SrcList*);
12512 SQLITE_PRIVATE int sqlite3FixSelect(DbFixer*, Select*);
12513 SQLITE_PRIVATE int sqlite3FixExpr(DbFixer*, Expr*);
12514 SQLITE_PRIVATE int sqlite3FixExprList(DbFixer*, ExprList*);
12515 SQLITE_PRIVATE int sqlite3FixTriggerStep(DbFixer*, TriggerStep*);
12516 SQLITE_PRIVATE int sqlite3AtoF(const char *z, double*, int, u8);
12517 SQLITE_PRIVATE int sqlite3GetInt32(const char *, int*);
12518 SQLITE_PRIVATE int sqlite3Atoi(const char*);
12519 SQLITE_PRIVATE int sqlite3Utf16ByteLen(const void *pData, int nChar);
12520 SQLITE_PRIVATE int sqlite3Utf8CharLen(const char *pData, int nByte);
12521 SQLITE_PRIVATE u32 sqlite3Utf8Read(const u8**);
12522 SQLITE_PRIVATE LogEst sqlite3LogEst(u64);
12523 SQLITE_PRIVATE LogEst sqlite3LogEstAdd(LogEst,LogEst);
12524 #ifndef SQLITE_OMIT_VIRTUALTABLE
12525 SQLITE_PRIVATE LogEst sqlite3LogEstFromDouble(double);
12526 #endif
12527 SQLITE_PRIVATE u64 sqlite3LogEstToInt(LogEst);
12528 
12529 /*
12530 ** Routines to read and write variable-length integers.  These used to
12531 ** be defined locally, but now we use the varint routines in the util.c
12532 ** file.  Code should use the MACRO forms below, as the Varint32 versions
12533 ** are coded to assume the single byte case is already handled (which
12534 ** the MACRO form does).
12535 */
12536 SQLITE_PRIVATE int sqlite3PutVarint(unsigned char*, u64);
12537 SQLITE_PRIVATE int sqlite3PutVarint32(unsigned char*, u32);
12538 SQLITE_PRIVATE u8 sqlite3GetVarint(const unsigned char *, u64 *);
12539 SQLITE_PRIVATE u8 sqlite3GetVarint32(const unsigned char *, u32 *);
12540 SQLITE_PRIVATE int sqlite3VarintLen(u64 v);
12541 
12542 /*
12543 ** The header of a record consists of a sequence variable-length integers.
12544 ** These integers are almost always small and are encoded as a single byte.
12545 ** The following macros take advantage this fact to provide a fast encode
12546 ** and decode of the integers in a record header.  It is faster for the common
12547 ** case where the integer is a single byte.  It is a little slower when the
12548 ** integer is two or more bytes.  But overall it is faster.
12549 **
12550 ** The following expressions are equivalent:
12551 **
12552 **     x = sqlite3GetVarint32( A, &B );
12553 **     x = sqlite3PutVarint32( A, B );
12554 **
12555 **     x = getVarint32( A, B );
12556 **     x = putVarint32( A, B );
12557 **
12558 */
12559 #define getVarint32(A,B)  \
12560   (u8)((*(A)<(u8)0x80)?((B)=(u32)*(A)),1:sqlite3GetVarint32((A),(u32 *)&(B)))
12561 #define putVarint32(A,B)  \
12562   (u8)(((u32)(B)<(u32)0x80)?(*(A)=(unsigned char)(B)),1:\
12563   sqlite3PutVarint32((A),(B)))
12564 #define getVarint    sqlite3GetVarint
12565 #define putVarint    sqlite3PutVarint
12566 
12567 
12568 SQLITE_PRIVATE const char *sqlite3IndexAffinityStr(Vdbe *, Index *);
12569 SQLITE_PRIVATE void sqlite3TableAffinityStr(Vdbe *, Table *);
12570 SQLITE_PRIVATE char sqlite3CompareAffinity(Expr *pExpr, char aff2);
12571 SQLITE_PRIVATE int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity);
12572 SQLITE_PRIVATE char sqlite3ExprAffinity(Expr *pExpr);
12573 SQLITE_PRIVATE int sqlite3Atoi64(const char*, i64*, int, u8);
12574 SQLITE_PRIVATE void sqlite3Error(sqlite3*, int, const char*,...);
12575 SQLITE_PRIVATE void *sqlite3HexToBlob(sqlite3*, const char *z, int n);
12576 SQLITE_PRIVATE u8 sqlite3HexToInt(int h);
12577 SQLITE_PRIVATE int sqlite3TwoPartName(Parse *, Token *, Token *, Token **);
12578 
12579 #if defined(SQLITE_TEST)
12580 SQLITE_PRIVATE const char *sqlite3ErrName(int);
12581 #endif
12582 
12583 SQLITE_PRIVATE const char *sqlite3ErrStr(int);
12584 SQLITE_PRIVATE int sqlite3ReadSchema(Parse *pParse);
12585 SQLITE_PRIVATE CollSeq *sqlite3FindCollSeq(sqlite3*,u8 enc, const char*,int);
12586 SQLITE_PRIVATE CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char*zName);
12587 SQLITE_PRIVATE CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr);
12588 SQLITE_PRIVATE Expr *sqlite3ExprAddCollateToken(Parse *pParse, Expr*, Token*);
12589 SQLITE_PRIVATE Expr *sqlite3ExprAddCollateString(Parse*,Expr*,const char*);
12590 SQLITE_PRIVATE Expr *sqlite3ExprSkipCollate(Expr*);
12591 SQLITE_PRIVATE int sqlite3CheckCollSeq(Parse *, CollSeq *);
12592 SQLITE_PRIVATE int sqlite3CheckObjectName(Parse *, const char *);
12593 SQLITE_PRIVATE void sqlite3VdbeSetChanges(sqlite3 *, int);
12594 SQLITE_PRIVATE int sqlite3AddInt64(i64*,i64);
12595 SQLITE_PRIVATE int sqlite3SubInt64(i64*,i64);
12596 SQLITE_PRIVATE int sqlite3MulInt64(i64*,i64);
12597 SQLITE_PRIVATE int sqlite3AbsInt32(int);
12598 #ifdef SQLITE_ENABLE_8_3_NAMES
12599 SQLITE_PRIVATE void sqlite3FileSuffix3(const char*, char*);
12600 #else
12601 # define sqlite3FileSuffix3(X,Y)
12602 #endif
12603 SQLITE_PRIVATE u8 sqlite3GetBoolean(const char *z,int);
12604 
12605 SQLITE_PRIVATE const void *sqlite3ValueText(sqlite3_value*, u8);
12606 SQLITE_PRIVATE int sqlite3ValueBytes(sqlite3_value*, u8);
12607 SQLITE_PRIVATE void sqlite3ValueSetStr(sqlite3_value*, int, const void *,u8,
12608                         void(*)(void*));
12609 SQLITE_PRIVATE void sqlite3ValueSetNull(sqlite3_value*);
12610 SQLITE_PRIVATE void sqlite3ValueFree(sqlite3_value*);
12611 SQLITE_PRIVATE sqlite3_value *sqlite3ValueNew(sqlite3 *);
12612 SQLITE_PRIVATE char *sqlite3Utf16to8(sqlite3 *, const void*, int, u8);
12613 SQLITE_PRIVATE int sqlite3ValueFromExpr(sqlite3 *, Expr *, u8, u8, sqlite3_value **);
12614 SQLITE_PRIVATE void sqlite3ValueApplyAffinity(sqlite3_value *, u8, u8);
12615 #ifndef SQLITE_AMALGAMATION
12616 SQLITE_PRIVATE const unsigned char sqlite3OpcodeProperty[];
12617 SQLITE_PRIVATE const unsigned char sqlite3UpperToLower[];
12618 SQLITE_PRIVATE const unsigned char sqlite3CtypeMap[];
12619 SQLITE_PRIVATE const Token sqlite3IntTokens[];
12620 SQLITE_PRIVATE SQLITE_WSD struct Sqlite3Config sqlite3Config;
12621 SQLITE_PRIVATE SQLITE_WSD FuncDefHash sqlite3GlobalFunctions;
12622 #ifndef SQLITE_OMIT_WSD
12623 SQLITE_PRIVATE int sqlite3PendingByte;
12624 #endif
12625 #endif
12626 SQLITE_PRIVATE void sqlite3RootPageMoved(sqlite3*, int, int, int);
12627 SQLITE_PRIVATE void sqlite3Reindex(Parse*, Token*, Token*);
12628 SQLITE_PRIVATE void sqlite3AlterFunctions(void);
12629 SQLITE_PRIVATE void sqlite3AlterRenameTable(Parse*, SrcList*, Token*);
12630 SQLITE_PRIVATE int sqlite3GetToken(const unsigned char *, int *);
12631 SQLITE_PRIVATE void sqlite3NestedParse(Parse*, const char*, ...);
12632 SQLITE_PRIVATE void sqlite3ExpirePreparedStatements(sqlite3*);
12633 SQLITE_PRIVATE int sqlite3CodeSubselect(Parse *, Expr *, int, int);
12634 SQLITE_PRIVATE void sqlite3SelectPrep(Parse*, Select*, NameContext*);
12635 SQLITE_PRIVATE int sqlite3MatchSpanName(const char*, const char*, const char*, const char*);
12636 SQLITE_PRIVATE int sqlite3ResolveExprNames(NameContext*, Expr*);
12637 SQLITE_PRIVATE void sqlite3ResolveSelectNames(Parse*, Select*, NameContext*);
12638 SQLITE_PRIVATE void sqlite3ResolveSelfReference(Parse*,Table*,int,Expr*,ExprList*);
12639 SQLITE_PRIVATE int sqlite3ResolveOrderGroupBy(Parse*, Select*, ExprList*, const char*);
12640 SQLITE_PRIVATE void sqlite3ColumnDefault(Vdbe *, Table *, int, int);
12641 SQLITE_PRIVATE void sqlite3AlterFinishAddColumn(Parse *, Token *);
12642 SQLITE_PRIVATE void sqlite3AlterBeginAddColumn(Parse *, SrcList *);
12643 SQLITE_PRIVATE CollSeq *sqlite3GetCollSeq(Parse*, u8, CollSeq *, const char*);
12644 SQLITE_PRIVATE char sqlite3AffinityType(const char*, u8*);
12645 SQLITE_PRIVATE void sqlite3Analyze(Parse*, Token*, Token*);
12646 SQLITE_PRIVATE int sqlite3InvokeBusyHandler(BusyHandler*);
12647 SQLITE_PRIVATE int sqlite3FindDb(sqlite3*, Token*);
12648 SQLITE_PRIVATE int sqlite3FindDbName(sqlite3 *, const char *);
12649 SQLITE_PRIVATE int sqlite3AnalysisLoad(sqlite3*,int iDB);
12650 SQLITE_PRIVATE void sqlite3DeleteIndexSamples(sqlite3*,Index*);
12651 SQLITE_PRIVATE void sqlite3DefaultRowEst(Index*);
12652 SQLITE_PRIVATE void sqlite3RegisterLikeFunctions(sqlite3*, int);
12653 SQLITE_PRIVATE int sqlite3IsLikeFunction(sqlite3*,Expr*,int*,char*);
12654 SQLITE_PRIVATE void sqlite3MinimumFileFormat(Parse*, int, int);
12655 SQLITE_PRIVATE void sqlite3SchemaClear(void *);
12656 SQLITE_PRIVATE Schema *sqlite3SchemaGet(sqlite3 *, Btree *);
12657 SQLITE_PRIVATE int sqlite3SchemaToIndex(sqlite3 *db, Schema *);
12658 SQLITE_PRIVATE KeyInfo *sqlite3KeyInfoAlloc(sqlite3*,int,int);
12659 SQLITE_PRIVATE void sqlite3KeyInfoUnref(KeyInfo*);
12660 SQLITE_PRIVATE KeyInfo *sqlite3KeyInfoRef(KeyInfo*);
12661 SQLITE_PRIVATE KeyInfo *sqlite3KeyInfoOfIndex(Parse*, Index*);
12662 #ifdef SQLITE_DEBUG
12663 SQLITE_PRIVATE int sqlite3KeyInfoIsWriteable(KeyInfo*);
12664 #endif
12665 SQLITE_PRIVATE int sqlite3CreateFunc(sqlite3 *, const char *, int, int, void *,
12666   void (*)(sqlite3_context*,int,sqlite3_value **),
12667   void (*)(sqlite3_context*,int,sqlite3_value **), void (*)(sqlite3_context*),
12668   FuncDestructor *pDestructor
12669 );
12670 SQLITE_PRIVATE int sqlite3ApiExit(sqlite3 *db, int);
12671 SQLITE_PRIVATE int sqlite3OpenTempDatabase(Parse *);
12672 
12673 SQLITE_PRIVATE void sqlite3StrAccumInit(StrAccum*, char*, int, int);
12674 SQLITE_PRIVATE void sqlite3StrAccumAppend(StrAccum*,const char*,int);
12675 SQLITE_PRIVATE void sqlite3StrAccumAppendAll(StrAccum*,const char*);
12676 SQLITE_PRIVATE void sqlite3AppendSpace(StrAccum*,int);
12677 SQLITE_PRIVATE char *sqlite3StrAccumFinish(StrAccum*);
12678 SQLITE_PRIVATE void sqlite3StrAccumReset(StrAccum*);
12679 SQLITE_PRIVATE void sqlite3SelectDestInit(SelectDest*,int,int);
12680 SQLITE_PRIVATE Expr *sqlite3CreateColumnExpr(sqlite3 *, SrcList *, int, int);
12681 
12682 SQLITE_PRIVATE void sqlite3BackupRestart(sqlite3_backup *);
12683 SQLITE_PRIVATE void sqlite3BackupUpdate(sqlite3_backup *, Pgno, const u8 *);
12684 
12685 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
12686 SQLITE_PRIVATE void sqlite3AnalyzeFunctions(void);
12687 SQLITE_PRIVATE int sqlite3Stat4ProbeSetValue(Parse*,Index*,UnpackedRecord**,Expr*,u8,int,int*);
12688 SQLITE_PRIVATE void sqlite3Stat4ProbeFree(UnpackedRecord*);
12689 #endif
12690 
12691 /*
12692 ** The interface to the LEMON-generated parser
12693 */
12694 SQLITE_PRIVATE void *sqlite3ParserAlloc(void*(*)(size_t));
12695 SQLITE_PRIVATE void sqlite3ParserFree(void*, void(*)(void*));
12696 SQLITE_PRIVATE void sqlite3Parser(void*, int, Token, Parse*);
12697 #ifdef YYTRACKMAXSTACKDEPTH
12698 SQLITE_PRIVATE   int sqlite3ParserStackPeak(void*);
12699 #endif
12700 
12701 SQLITE_PRIVATE void sqlite3AutoLoadExtensions(sqlite3*);
12702 #ifndef SQLITE_OMIT_LOAD_EXTENSION
12703 SQLITE_PRIVATE   void sqlite3CloseExtensions(sqlite3*);
12704 #else
12705 # define sqlite3CloseExtensions(X)
12706 #endif
12707 
12708 #ifndef SQLITE_OMIT_SHARED_CACHE
12709 SQLITE_PRIVATE   void sqlite3TableLock(Parse *, int, int, u8, const char *);
12710 #else
12711   #define sqlite3TableLock(v,w,x,y,z)
12712 #endif
12713 
12714 #ifdef SQLITE_TEST
12715 SQLITE_PRIVATE   int sqlite3Utf8To8(unsigned char*);
12716 #endif
12717 
12718 #ifdef SQLITE_OMIT_VIRTUALTABLE
12719 #  define sqlite3VtabClear(Y)
12720 #  define sqlite3VtabSync(X,Y) SQLITE_OK
12721 #  define sqlite3VtabRollback(X)
12722 #  define sqlite3VtabCommit(X)
12723 #  define sqlite3VtabInSync(db) 0
12724 #  define sqlite3VtabLock(X)
12725 #  define sqlite3VtabUnlock(X)
12726 #  define sqlite3VtabUnlockList(X)
12727 #  define sqlite3VtabSavepoint(X, Y, Z) SQLITE_OK
12728 #  define sqlite3GetVTable(X,Y)  ((VTable*)0)
12729 #else
12730 SQLITE_PRIVATE    void sqlite3VtabClear(sqlite3 *db, Table*);
12731 SQLITE_PRIVATE    void sqlite3VtabDisconnect(sqlite3 *db, Table *p);
12732 SQLITE_PRIVATE    int sqlite3VtabSync(sqlite3 *db, Vdbe*);
12733 SQLITE_PRIVATE    int sqlite3VtabRollback(sqlite3 *db);
12734 SQLITE_PRIVATE    int sqlite3VtabCommit(sqlite3 *db);
12735 SQLITE_PRIVATE    void sqlite3VtabLock(VTable *);
12736 SQLITE_PRIVATE    void sqlite3VtabUnlock(VTable *);
12737 SQLITE_PRIVATE    void sqlite3VtabUnlockList(sqlite3*);
12738 SQLITE_PRIVATE    int sqlite3VtabSavepoint(sqlite3 *, int, int);
12739 SQLITE_PRIVATE    void sqlite3VtabImportErrmsg(Vdbe*, sqlite3_vtab*);
12740 SQLITE_PRIVATE    VTable *sqlite3GetVTable(sqlite3*, Table*);
12741 #  define sqlite3VtabInSync(db) ((db)->nVTrans>0 && (db)->aVTrans==0)
12742 #endif
12743 SQLITE_PRIVATE void sqlite3VtabMakeWritable(Parse*,Table*);
12744 SQLITE_PRIVATE void sqlite3VtabBeginParse(Parse*, Token*, Token*, Token*, int);
12745 SQLITE_PRIVATE void sqlite3VtabFinishParse(Parse*, Token*);
12746 SQLITE_PRIVATE void sqlite3VtabArgInit(Parse*);
12747 SQLITE_PRIVATE void sqlite3VtabArgExtend(Parse*, Token*);
12748 SQLITE_PRIVATE int sqlite3VtabCallCreate(sqlite3*, int, const char *, char **);
12749 SQLITE_PRIVATE int sqlite3VtabCallConnect(Parse*, Table*);
12750 SQLITE_PRIVATE int sqlite3VtabCallDestroy(sqlite3*, int, const char *);
12751 SQLITE_PRIVATE int sqlite3VtabBegin(sqlite3 *, VTable *);
12752 SQLITE_PRIVATE FuncDef *sqlite3VtabOverloadFunction(sqlite3 *,FuncDef*, int nArg, Expr*);
12753 SQLITE_PRIVATE void sqlite3InvalidFunction(sqlite3_context*,int,sqlite3_value**);
12754 SQLITE_PRIVATE sqlite3_int64 sqlite3StmtCurrentTime(sqlite3_context*);
12755 SQLITE_PRIVATE int sqlite3VdbeParameterIndex(Vdbe*, const char*, int);
12756 SQLITE_PRIVATE int sqlite3TransferBindings(sqlite3_stmt *, sqlite3_stmt *);
12757 SQLITE_PRIVATE void sqlite3ParserReset(Parse*);
12758 SQLITE_PRIVATE int sqlite3Reprepare(Vdbe*);
12759 SQLITE_PRIVATE void sqlite3ExprListCheckLength(Parse*, ExprList*, const char*);
12760 SQLITE_PRIVATE CollSeq *sqlite3BinaryCompareCollSeq(Parse *, Expr *, Expr *);
12761 SQLITE_PRIVATE int sqlite3TempInMemory(const sqlite3*);
12762 SQLITE_PRIVATE const char *sqlite3JournalModename(int);
12763 #ifndef SQLITE_OMIT_WAL
12764 SQLITE_PRIVATE   int sqlite3Checkpoint(sqlite3*, int, int, int*, int*);
12765 SQLITE_PRIVATE   int sqlite3WalDefaultHook(void*,sqlite3*,const char*,int);
12766 #endif
12767 #ifndef SQLITE_OMIT_CTE
12768 SQLITE_PRIVATE   With *sqlite3WithAdd(Parse*,With*,Token*,ExprList*,Select*);
12769 SQLITE_PRIVATE   void sqlite3WithDelete(sqlite3*,With*);
12770 SQLITE_PRIVATE   void sqlite3WithPush(Parse*, With*, u8);
12771 #else
12772 #define sqlite3WithPush(x,y,z)
12773 #define sqlite3WithDelete(x,y)
12774 #endif
12775 
12776 /* Declarations for functions in fkey.c. All of these are replaced by
12777 ** no-op macros if OMIT_FOREIGN_KEY is defined. In this case no foreign
12778 ** key functionality is available. If OMIT_TRIGGER is defined but
12779 ** OMIT_FOREIGN_KEY is not, only some of the functions are no-oped. In
12780 ** this case foreign keys are parsed, but no other functionality is
12781 ** provided (enforcement of FK constraints requires the triggers sub-system).
12782 */
12783 #if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
12784 SQLITE_PRIVATE   void sqlite3FkCheck(Parse*, Table*, int, int, int*, int);
12785 SQLITE_PRIVATE   void sqlite3FkDropTable(Parse*, SrcList *, Table*);
12786 SQLITE_PRIVATE   void sqlite3FkActions(Parse*, Table*, ExprList*, int, int*, int);
12787 SQLITE_PRIVATE   int sqlite3FkRequired(Parse*, Table*, int*, int);
12788 SQLITE_PRIVATE   u32 sqlite3FkOldmask(Parse*, Table*);
12789 SQLITE_PRIVATE   FKey *sqlite3FkReferences(Table *);
12790 #else
12791   #define sqlite3FkActions(a,b,c,d,e,f)
12792   #define sqlite3FkCheck(a,b,c,d,e,f)
12793   #define sqlite3FkDropTable(a,b,c)
12794   #define sqlite3FkOldmask(a,b)         0
12795   #define sqlite3FkRequired(a,b,c,d)    0
12796 #endif
12797 #ifndef SQLITE_OMIT_FOREIGN_KEY
12798 SQLITE_PRIVATE   void sqlite3FkDelete(sqlite3 *, Table*);
12799 SQLITE_PRIVATE   int sqlite3FkLocateIndex(Parse*,Table*,FKey*,Index**,int**);
12800 #else
12801   #define sqlite3FkDelete(a,b)
12802   #define sqlite3FkLocateIndex(a,b,c,d,e)
12803 #endif
12804 
12805 
12806 /*
12807 ** Available fault injectors.  Should be numbered beginning with 0.
12808 */
12809 #define SQLITE_FAULTINJECTOR_MALLOC     0
12810 #define SQLITE_FAULTINJECTOR_COUNT      1
12811 
12812 /*
12813 ** The interface to the code in fault.c used for identifying "benign"
12814 ** malloc failures. This is only present if SQLITE_OMIT_BUILTIN_TEST
12815 ** is not defined.
12816 */
12817 #ifndef SQLITE_OMIT_BUILTIN_TEST
12818 SQLITE_PRIVATE   void sqlite3BeginBenignMalloc(void);
12819 SQLITE_PRIVATE   void sqlite3EndBenignMalloc(void);
12820 #else
12821   #define sqlite3BeginBenignMalloc()
12822   #define sqlite3EndBenignMalloc()
12823 #endif
12824 
12825 #define IN_INDEX_ROWID           1
12826 #define IN_INDEX_EPH             2
12827 #define IN_INDEX_INDEX_ASC       3
12828 #define IN_INDEX_INDEX_DESC      4
12829 SQLITE_PRIVATE int sqlite3FindInIndex(Parse *, Expr *, int*);
12830 
12831 #ifdef SQLITE_ENABLE_ATOMIC_WRITE
12832 SQLITE_PRIVATE   int sqlite3JournalOpen(sqlite3_vfs *, const char *, sqlite3_file *, int, int);
12833 SQLITE_PRIVATE   int sqlite3JournalSize(sqlite3_vfs *);
12834 SQLITE_PRIVATE   int sqlite3JournalCreate(sqlite3_file *);
12835 SQLITE_PRIVATE   int sqlite3JournalExists(sqlite3_file *p);
12836 #else
12837   #define sqlite3JournalSize(pVfs) ((pVfs)->szOsFile)
12838   #define sqlite3JournalExists(p) 1
12839 #endif
12840 
12841 SQLITE_PRIVATE void sqlite3MemJournalOpen(sqlite3_file *);
12842 SQLITE_PRIVATE int sqlite3MemJournalSize(void);
12843 SQLITE_PRIVATE int sqlite3IsMemJournal(sqlite3_file *);
12844 
12845 #if SQLITE_MAX_EXPR_DEPTH>0
12846 SQLITE_PRIVATE   void sqlite3ExprSetHeight(Parse *pParse, Expr *p);
12847 SQLITE_PRIVATE   int sqlite3SelectExprHeight(Select *);
12848 SQLITE_PRIVATE   int sqlite3ExprCheckHeight(Parse*, int);
12849 #else
12850   #define sqlite3ExprSetHeight(x,y)
12851   #define sqlite3SelectExprHeight(x) 0
12852   #define sqlite3ExprCheckHeight(x,y)
12853 #endif
12854 
12855 SQLITE_PRIVATE u32 sqlite3Get4byte(const u8*);
12856 SQLITE_PRIVATE void sqlite3Put4byte(u8*, u32);
12857 
12858 #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
12859 SQLITE_PRIVATE   void sqlite3ConnectionBlocked(sqlite3 *, sqlite3 *);
12860 SQLITE_PRIVATE   void sqlite3ConnectionUnlocked(sqlite3 *db);
12861 SQLITE_PRIVATE   void sqlite3ConnectionClosed(sqlite3 *db);
12862 #else
12863   #define sqlite3ConnectionBlocked(x,y)
12864   #define sqlite3ConnectionUnlocked(x)
12865   #define sqlite3ConnectionClosed(x)
12866 #endif
12867 
12868 #ifdef SQLITE_DEBUG
12869 SQLITE_PRIVATE   void sqlite3ParserTrace(FILE*, char *);
12870 #endif
12871 
12872 /*
12873 ** If the SQLITE_ENABLE IOTRACE exists then the global variable
12874 ** sqlite3IoTrace is a pointer to a printf-like routine used to
12875 ** print I/O tracing messages.
12876 */
12877 #ifdef SQLITE_ENABLE_IOTRACE
12878 # define IOTRACE(A)  if( sqlite3IoTrace ){ sqlite3IoTrace A; }
12879 SQLITE_PRIVATE   void sqlite3VdbeIOTraceSql(Vdbe*);
12880 SQLITE_PRIVATE void (*sqlite3IoTrace)(const char*,...);
12881 #else
12882 # define IOTRACE(A)
12883 # define sqlite3VdbeIOTraceSql(X)
12884 #endif
12885 
12886 /*
12887 ** These routines are available for the mem2.c debugging memory allocator
12888 ** only.  They are used to verify that different "types" of memory
12889 ** allocations are properly tracked by the system.
12890 **
12891 ** sqlite3MemdebugSetType() sets the "type" of an allocation to one of
12892 ** the MEMTYPE_* macros defined below.  The type must be a bitmask with
12893 ** a single bit set.
12894 **
12895 ** sqlite3MemdebugHasType() returns true if any of the bits in its second
12896 ** argument match the type set by the previous sqlite3MemdebugSetType().
12897 ** sqlite3MemdebugHasType() is intended for use inside assert() statements.
12898 **
12899 ** sqlite3MemdebugNoType() returns true if none of the bits in its second
12900 ** argument match the type set by the previous sqlite3MemdebugSetType().
12901 **
12902 ** Perhaps the most important point is the difference between MEMTYPE_HEAP
12903 ** and MEMTYPE_LOOKASIDE.  If an allocation is MEMTYPE_LOOKASIDE, that means
12904 ** it might have been allocated by lookaside, except the allocation was
12905 ** too large or lookaside was already full.  It is important to verify
12906 ** that allocations that might have been satisfied by lookaside are not
12907 ** passed back to non-lookaside free() routines.  Asserts such as the
12908 ** example above are placed on the non-lookaside free() routines to verify
12909 ** this constraint.
12910 **
12911 ** All of this is no-op for a production build.  It only comes into
12912 ** play when the SQLITE_MEMDEBUG compile-time option is used.
12913 */
12914 #ifdef SQLITE_MEMDEBUG
12915 SQLITE_PRIVATE   void sqlite3MemdebugSetType(void*,u8);
12916 SQLITE_PRIVATE   int sqlite3MemdebugHasType(void*,u8);
12917 SQLITE_PRIVATE   int sqlite3MemdebugNoType(void*,u8);
12918 #else
12919 # define sqlite3MemdebugSetType(X,Y)  /* no-op */
12920 # define sqlite3MemdebugHasType(X,Y)  1
12921 # define sqlite3MemdebugNoType(X,Y)   1
12922 #endif
12923 #define MEMTYPE_HEAP       0x01  /* General heap allocations */
12924 #define MEMTYPE_LOOKASIDE  0x02  /* Might have been lookaside memory */
12925 #define MEMTYPE_SCRATCH    0x04  /* Scratch allocations */
12926 #define MEMTYPE_PCACHE     0x08  /* Page cache allocations */
12927 #define MEMTYPE_DB         0x10  /* Uses sqlite3DbMalloc, not sqlite_malloc */
12928 
12929 #endif /* _SQLITEINT_H_ */
12930 
12931 /************** End of sqliteInt.h *******************************************/
12932 /************** Begin file global.c ******************************************/
12933 /*
12934 ** 2008 June 13
12935 **
12936 ** The author disclaims copyright to this source code.  In place of
12937 ** a legal notice, here is a blessing:
12938 **
12939 **    May you do good and not evil.
12940 **    May you find forgiveness for yourself and forgive others.
12941 **    May you share freely, never taking more than you give.
12942 **
12943 *************************************************************************
12944 **
12945 ** This file contains definitions of global variables and contants.
12946 */
12947 
12948 /* An array to map all upper-case characters into their corresponding
12949 ** lower-case character.
12950 **
12951 ** SQLite only considers US-ASCII (or EBCDIC) characters.  We do not
12952 ** handle case conversions for the UTF character set since the tables
12953 ** involved are nearly as big or bigger than SQLite itself.
12954 */
12955 SQLITE_PRIVATE const unsigned char sqlite3UpperToLower[] = {
12956 #ifdef SQLITE_ASCII
12957       0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17,
12958      18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
12959      36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53,
12960      54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 97, 98, 99,100,101,102,103,
12961     104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,
12962     122, 91, 92, 93, 94, 95, 96, 97, 98, 99,100,101,102,103,104,105,106,107,
12963     108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,
12964     126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,
12965     144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,
12966     162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,
12967     180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,
12968     198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,
12969     216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,
12970     234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,
12971     252,253,254,255
12972 #endif
12973 #ifdef SQLITE_EBCDIC
12974       0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, /* 0x */
12975      16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, /* 1x */
12976      32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, /* 2x */
12977      48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, /* 3x */
12978      64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, /* 4x */
12979      80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, /* 5x */
12980      96, 97, 66, 67, 68, 69, 70, 71, 72, 73,106,107,108,109,110,111, /* 6x */
12981     112, 81, 82, 83, 84, 85, 86, 87, 88, 89,122,123,124,125,126,127, /* 7x */
12982     128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143, /* 8x */
12983     144,145,146,147,148,149,150,151,152,153,154,155,156,157,156,159, /* 9x */
12984     160,161,162,163,164,165,166,167,168,169,170,171,140,141,142,175, /* Ax */
12985     176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191, /* Bx */
12986     192,129,130,131,132,133,134,135,136,137,202,203,204,205,206,207, /* Cx */
12987     208,145,146,147,148,149,150,151,152,153,218,219,220,221,222,223, /* Dx */
12988     224,225,162,163,164,165,166,167,168,169,232,203,204,205,206,207, /* Ex */
12989     239,240,241,242,243,244,245,246,247,248,249,219,220,221,222,255, /* Fx */
12990 #endif
12991 };
12992 
12993 /*
12994 ** The following 256 byte lookup table is used to support SQLites built-in
12995 ** equivalents to the following standard library functions:
12996 **
12997 **   isspace()                        0x01
12998 **   isalpha()                        0x02
12999 **   isdigit()                        0x04
13000 **   isalnum()                        0x06
13001 **   isxdigit()                       0x08
13002 **   toupper()                        0x20
13003 **   SQLite identifier character      0x40
13004 **
13005 ** Bit 0x20 is set if the mapped character requires translation to upper
13006 ** case. i.e. if the character is a lower-case ASCII character.
13007 ** If x is a lower-case ASCII character, then its upper-case equivalent
13008 ** is (x - 0x20). Therefore toupper() can be implemented as:
13009 **
13010 **   (x & ~(map[x]&0x20))
13011 **
13012 ** Standard function tolower() is implemented using the sqlite3UpperToLower[]
13013 ** array. tolower() is used more often than toupper() by SQLite.
13014 **
13015 ** Bit 0x40 is set if the character non-alphanumeric and can be used in an
13016 ** SQLite identifier.  Identifiers are alphanumerics, "_", "$", and any
13017 ** non-ASCII UTF character. Hence the test for whether or not a character is
13018 ** part of an identifier is 0x46.
13019 **
13020 ** SQLite's versions are identical to the standard versions assuming a
13021 ** locale of "C". They are implemented as macros in sqliteInt.h.
13022 */
13023 #ifdef SQLITE_ASCII
13024 SQLITE_PRIVATE const unsigned char sqlite3CtypeMap[256] = {
13025   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 00..07    ........ */
13026   0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00,  /* 08..0f    ........ */
13027   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 10..17    ........ */
13028   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 18..1f    ........ */
13029   0x01, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,  /* 20..27     !"#$%&' */
13030   0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 28..2f    ()*+,-./ */
13031   0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c,  /* 30..37    01234567 */
13032   0x0c, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 38..3f    89:;<=>? */
13033 
13034   0x00, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x02,  /* 40..47    @ABCDEFG */
13035   0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,  /* 48..4f    HIJKLMNO */
13036   0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,  /* 50..57    PQRSTUVW */
13037   0x02, 0x02, 0x02, 0x00, 0x00, 0x00, 0x00, 0x40,  /* 58..5f    XYZ[\]^_ */
13038   0x00, 0x2a, 0x2a, 0x2a, 0x2a, 0x2a, 0x2a, 0x22,  /* 60..67    `abcdefg */
13039   0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,  /* 68..6f    hijklmno */
13040   0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,  /* 70..77    pqrstuvw */
13041   0x22, 0x22, 0x22, 0x00, 0x00, 0x00, 0x00, 0x00,  /* 78..7f    xyz{|}~. */
13042 
13043   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* 80..87    ........ */
13044   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* 88..8f    ........ */
13045   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* 90..97    ........ */
13046   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* 98..9f    ........ */
13047   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* a0..a7    ........ */
13048   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* a8..af    ........ */
13049   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* b0..b7    ........ */
13050   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* b8..bf    ........ */
13051 
13052   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* c0..c7    ........ */
13053   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* c8..cf    ........ */
13054   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* d0..d7    ........ */
13055   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* d8..df    ........ */
13056   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* e0..e7    ........ */
13057   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* e8..ef    ........ */
13058   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,  /* f0..f7    ........ */
13059   0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40   /* f8..ff    ........ */
13060 };
13061 #endif
13062 
13063 #ifndef SQLITE_USE_URI
13064 # define  SQLITE_USE_URI 0
13065 #endif
13066 
13067 #ifndef SQLITE_ALLOW_COVERING_INDEX_SCAN
13068 # define SQLITE_ALLOW_COVERING_INDEX_SCAN 1
13069 #endif
13070 
13071 /*
13072 ** The following singleton contains the global configuration for
13073 ** the SQLite library.
13074 */
13075 SQLITE_PRIVATE SQLITE_WSD struct Sqlite3Config sqlite3Config = {
13076    SQLITE_DEFAULT_MEMSTATUS,  /* bMemstat */
13077    1,                         /* bCoreMutex */
13078    SQLITE_THREADSAFE==1,      /* bFullMutex */
13079    SQLITE_USE_URI,            /* bOpenUri */
13080    SQLITE_ALLOW_COVERING_INDEX_SCAN,   /* bUseCis */
13081    0x7ffffffe,                /* mxStrlen */
13082    0,                         /* neverCorrupt */
13083    128,                       /* szLookaside */
13084    500,                       /* nLookaside */
13085    {0,0,0,0,0,0,0,0},         /* m */
13086    {0,0,0,0,0,0,0,0,0},       /* mutex */
13087    {0,0,0,0,0,0,0,0,0,0,0,0,0},/* pcache2 */
13088    (void*)0,                  /* pHeap */
13089    0,                         /* nHeap */
13090    0, 0,                      /* mnHeap, mxHeap */
13091    SQLITE_DEFAULT_MMAP_SIZE,  /* szMmap */
13092    SQLITE_MAX_MMAP_SIZE,      /* mxMmap */
13093    (void*)0,                  /* pScratch */
13094    0,                         /* szScratch */
13095    0,                         /* nScratch */
13096    (void*)0,                  /* pPage */
13097    0,                         /* szPage */
13098    0,                         /* nPage */
13099    0,                         /* mxParserStack */
13100    0,                         /* sharedCacheEnabled */
13101    /* All the rest should always be initialized to zero */
13102    0,                         /* isInit */
13103    0,                         /* inProgress */
13104    0,                         /* isMutexInit */
13105    0,                         /* isMallocInit */
13106    0,                         /* isPCacheInit */
13107    0,                         /* pInitMutex */
13108    0,                         /* nRefInitMutex */
13109    0,                         /* xLog */
13110    0,                         /* pLogArg */
13111    0,                         /* bLocaltimeFault */
13112 #ifdef SQLITE_ENABLE_SQLLOG
13113    0,                         /* xSqllog */
13114    0                          /* pSqllogArg */
13115 #endif
13116 };
13117 
13118 /*
13119 ** Hash table for global functions - functions common to all
13120 ** database connections.  After initialization, this table is
13121 ** read-only.
13122 */
13123 SQLITE_PRIVATE SQLITE_WSD FuncDefHash sqlite3GlobalFunctions;
13124 
13125 /*
13126 ** Constant tokens for values 0 and 1.
13127 */
13128 SQLITE_PRIVATE const Token sqlite3IntTokens[] = {
13129    { "0", 1 },
13130    { "1", 1 }
13131 };
13132 
13133 
13134 /*
13135 ** The value of the "pending" byte must be 0x40000000 (1 byte past the
13136 ** 1-gibabyte boundary) in a compatible database.  SQLite never uses
13137 ** the database page that contains the pending byte.  It never attempts
13138 ** to read or write that page.  The pending byte page is set assign
13139 ** for use by the VFS layers as space for managing file locks.
13140 **
13141 ** During testing, it is often desirable to move the pending byte to
13142 ** a different position in the file.  This allows code that has to
13143 ** deal with the pending byte to run on files that are much smaller
13144 ** than 1 GiB.  The sqlite3_test_control() interface can be used to
13145 ** move the pending byte.
13146 **
13147 ** IMPORTANT:  Changing the pending byte to any value other than
13148 ** 0x40000000 results in an incompatible database file format!
13149 ** Changing the pending byte during operating results in undefined
13150 ** and dileterious behavior.
13151 */
13152 #ifndef SQLITE_OMIT_WSD
13153 SQLITE_PRIVATE int sqlite3PendingByte = 0x40000000;
13154 #endif
13155 
13156 /*
13157 ** Properties of opcodes.  The OPFLG_INITIALIZER macro is
13158 ** created by mkopcodeh.awk during compilation.  Data is obtained
13159 ** from the comments following the "case OP_xxxx:" statements in
13160 ** the vdbe.c file.
13161 */
13162 SQLITE_PRIVATE const unsigned char sqlite3OpcodeProperty[] = OPFLG_INITIALIZER;
13163 
13164 /************** End of global.c **********************************************/
13165 /************** Begin file ctime.c *******************************************/
13166 /*
13167 ** 2010 February 23
13168 **
13169 ** The author disclaims copyright to this source code.  In place of
13170 ** a legal notice, here is a blessing:
13171 **
13172 **    May you do good and not evil.
13173 **    May you find forgiveness for yourself and forgive others.
13174 **    May you share freely, never taking more than you give.
13175 **
13176 *************************************************************************
13177 **
13178 ** This file implements routines used to report what compile-time options
13179 ** SQLite was built with.
13180 */
13181 
13182 #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
13183 
13184 
13185 /*
13186 ** An array of names of all compile-time options.  This array should
13187 ** be sorted A-Z.
13188 **
13189 ** This array looks large, but in a typical installation actually uses
13190 ** only a handful of compile-time options, so most times this array is usually
13191 ** rather short and uses little memory space.
13192 */
13193 static const char * const azCompileOpt[] = {
13194 
13195 /* These macros are provided to "stringify" the value of the define
13196 ** for those options in which the value is meaningful. */
13197 #define CTIMEOPT_VAL_(opt) #opt
13198 #define CTIMEOPT_VAL(opt) CTIMEOPT_VAL_(opt)
13199 
13200 #ifdef SQLITE_32BIT_ROWID
13201   "32BIT_ROWID",
13202 #endif
13203 #ifdef SQLITE_4_BYTE_ALIGNED_MALLOC
13204   "4_BYTE_ALIGNED_MALLOC",
13205 #endif
13206 #ifdef SQLITE_CASE_SENSITIVE_LIKE
13207   "CASE_SENSITIVE_LIKE",
13208 #endif
13209 #ifdef SQLITE_CHECK_PAGES
13210   "CHECK_PAGES",
13211 #endif
13212 #ifdef SQLITE_COVERAGE_TEST
13213   "COVERAGE_TEST",
13214 #endif
13215 #ifdef SQLITE_DEBUG
13216   "DEBUG",
13217 #endif
13218 #ifdef SQLITE_DEFAULT_LOCKING_MODE
13219   "DEFAULT_LOCKING_MODE=" CTIMEOPT_VAL(SQLITE_DEFAULT_LOCKING_MODE),
13220 #endif
13221 #if defined(SQLITE_DEFAULT_MMAP_SIZE) && !defined(SQLITE_DEFAULT_MMAP_SIZE_xc)
13222   "DEFAULT_MMAP_SIZE=" CTIMEOPT_VAL(SQLITE_DEFAULT_MMAP_SIZE),
13223 #endif
13224 #ifdef SQLITE_DISABLE_DIRSYNC
13225   "DISABLE_DIRSYNC",
13226 #endif
13227 #ifdef SQLITE_DISABLE_LFS
13228   "DISABLE_LFS",
13229 #endif
13230 #ifdef SQLITE_ENABLE_ATOMIC_WRITE
13231   "ENABLE_ATOMIC_WRITE",
13232 #endif
13233 #ifdef SQLITE_ENABLE_CEROD
13234   "ENABLE_CEROD",
13235 #endif
13236 #ifdef SQLITE_ENABLE_COLUMN_METADATA
13237   "ENABLE_COLUMN_METADATA",
13238 #endif
13239 #ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
13240   "ENABLE_EXPENSIVE_ASSERT",
13241 #endif
13242 #ifdef SQLITE_ENABLE_FTS1
13243   "ENABLE_FTS1",
13244 #endif
13245 #ifdef SQLITE_ENABLE_FTS2
13246   "ENABLE_FTS2",
13247 #endif
13248 #ifdef SQLITE_ENABLE_FTS3
13249   "ENABLE_FTS3",
13250 #endif
13251 #ifdef SQLITE_ENABLE_FTS3_PARENTHESIS
13252   "ENABLE_FTS3_PARENTHESIS",
13253 #endif
13254 #ifdef SQLITE_ENABLE_FTS4
13255   "ENABLE_FTS4",
13256 #endif
13257 #ifdef SQLITE_ENABLE_ICU
13258   "ENABLE_ICU",
13259 #endif
13260 #ifdef SQLITE_ENABLE_IOTRACE
13261   "ENABLE_IOTRACE",
13262 #endif
13263 #ifdef SQLITE_ENABLE_LOAD_EXTENSION
13264   "ENABLE_LOAD_EXTENSION",
13265 #endif
13266 #ifdef SQLITE_ENABLE_LOCKING_STYLE
13267   "ENABLE_LOCKING_STYLE=" CTIMEOPT_VAL(SQLITE_ENABLE_LOCKING_STYLE),
13268 #endif
13269 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
13270   "ENABLE_MEMORY_MANAGEMENT",
13271 #endif
13272 #ifdef SQLITE_ENABLE_MEMSYS3
13273   "ENABLE_MEMSYS3",
13274 #endif
13275 #ifdef SQLITE_ENABLE_MEMSYS5
13276   "ENABLE_MEMSYS5",
13277 #endif
13278 #ifdef SQLITE_ENABLE_OVERSIZE_CELL_CHECK
13279   "ENABLE_OVERSIZE_CELL_CHECK",
13280 #endif
13281 #ifdef SQLITE_ENABLE_RTREE
13282   "ENABLE_RTREE",
13283 #endif
13284 #if defined(SQLITE_ENABLE_STAT4)
13285   "ENABLE_STAT4",
13286 #elif defined(SQLITE_ENABLE_STAT3)
13287   "ENABLE_STAT3",
13288 #endif
13289 #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
13290   "ENABLE_UNLOCK_NOTIFY",
13291 #endif
13292 #ifdef SQLITE_ENABLE_UPDATE_DELETE_LIMIT
13293   "ENABLE_UPDATE_DELETE_LIMIT",
13294 #endif
13295 #ifdef SQLITE_HAS_CODEC
13296   "HAS_CODEC",
13297 #endif
13298 #ifdef SQLITE_HAVE_ISNAN
13299   "HAVE_ISNAN",
13300 #endif
13301 #ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
13302   "HOMEGROWN_RECURSIVE_MUTEX",
13303 #endif
13304 #ifdef SQLITE_IGNORE_AFP_LOCK_ERRORS
13305   "IGNORE_AFP_LOCK_ERRORS",
13306 #endif
13307 #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
13308   "IGNORE_FLOCK_LOCK_ERRORS",
13309 #endif
13310 #ifdef SQLITE_INT64_TYPE
13311   "INT64_TYPE",
13312 #endif
13313 #ifdef SQLITE_LOCK_TRACE
13314   "LOCK_TRACE",
13315 #endif
13316 #if defined(SQLITE_MAX_MMAP_SIZE) && !defined(SQLITE_MAX_MMAP_SIZE_xc)
13317   "MAX_MMAP_SIZE=" CTIMEOPT_VAL(SQLITE_MAX_MMAP_SIZE),
13318 #endif
13319 #ifdef SQLITE_MAX_SCHEMA_RETRY
13320   "MAX_SCHEMA_RETRY=" CTIMEOPT_VAL(SQLITE_MAX_SCHEMA_RETRY),
13321 #endif
13322 #ifdef SQLITE_MEMDEBUG
13323   "MEMDEBUG",
13324 #endif
13325 #ifdef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
13326   "MIXED_ENDIAN_64BIT_FLOAT",
13327 #endif
13328 #ifdef SQLITE_NO_SYNC
13329   "NO_SYNC",
13330 #endif
13331 #ifdef SQLITE_OMIT_ALTERTABLE
13332   "OMIT_ALTERTABLE",
13333 #endif
13334 #ifdef SQLITE_OMIT_ANALYZE
13335   "OMIT_ANALYZE",
13336 #endif
13337 #ifdef SQLITE_OMIT_ATTACH
13338   "OMIT_ATTACH",
13339 #endif
13340 #ifdef SQLITE_OMIT_AUTHORIZATION
13341   "OMIT_AUTHORIZATION",
13342 #endif
13343 #ifdef SQLITE_OMIT_AUTOINCREMENT
13344   "OMIT_AUTOINCREMENT",
13345 #endif
13346 #ifdef SQLITE_OMIT_AUTOINIT
13347   "OMIT_AUTOINIT",
13348 #endif
13349 #ifdef SQLITE_OMIT_AUTOMATIC_INDEX
13350   "OMIT_AUTOMATIC_INDEX",
13351 #endif
13352 #ifdef SQLITE_OMIT_AUTORESET
13353   "OMIT_AUTORESET",
13354 #endif
13355 #ifdef SQLITE_OMIT_AUTOVACUUM
13356   "OMIT_AUTOVACUUM",
13357 #endif
13358 #ifdef SQLITE_OMIT_BETWEEN_OPTIMIZATION
13359   "OMIT_BETWEEN_OPTIMIZATION",
13360 #endif
13361 #ifdef SQLITE_OMIT_BLOB_LITERAL
13362   "OMIT_BLOB_LITERAL",
13363 #endif
13364 #ifdef SQLITE_OMIT_BTREECOUNT
13365   "OMIT_BTREECOUNT",
13366 #endif
13367 #ifdef SQLITE_OMIT_BUILTIN_TEST
13368   "OMIT_BUILTIN_TEST",
13369 #endif
13370 #ifdef SQLITE_OMIT_CAST
13371   "OMIT_CAST",
13372 #endif
13373 #ifdef SQLITE_OMIT_CHECK
13374   "OMIT_CHECK",
13375 #endif
13376 #ifdef SQLITE_OMIT_COMPLETE
13377   "OMIT_COMPLETE",
13378 #endif
13379 #ifdef SQLITE_OMIT_COMPOUND_SELECT
13380   "OMIT_COMPOUND_SELECT",
13381 #endif
13382 #ifdef SQLITE_OMIT_DATETIME_FUNCS
13383   "OMIT_DATETIME_FUNCS",
13384 #endif
13385 #ifdef SQLITE_OMIT_DECLTYPE
13386   "OMIT_DECLTYPE",
13387 #endif
13388 #ifdef SQLITE_OMIT_DEPRECATED
13389   "OMIT_DEPRECATED",
13390 #endif
13391 #ifdef SQLITE_OMIT_DISKIO
13392   "OMIT_DISKIO",
13393 #endif
13394 #ifdef SQLITE_OMIT_EXPLAIN
13395   "OMIT_EXPLAIN",
13396 #endif
13397 #ifdef SQLITE_OMIT_FLAG_PRAGMAS
13398   "OMIT_FLAG_PRAGMAS",
13399 #endif
13400 #ifdef SQLITE_OMIT_FLOATING_POINT
13401   "OMIT_FLOATING_POINT",
13402 #endif
13403 #ifdef SQLITE_OMIT_FOREIGN_KEY
13404   "OMIT_FOREIGN_KEY",
13405 #endif
13406 #ifdef SQLITE_OMIT_GET_TABLE
13407   "OMIT_GET_TABLE",
13408 #endif
13409 #ifdef SQLITE_OMIT_INCRBLOB
13410   "OMIT_INCRBLOB",
13411 #endif
13412 #ifdef SQLITE_OMIT_INTEGRITY_CHECK
13413   "OMIT_INTEGRITY_CHECK",
13414 #endif
13415 #ifdef SQLITE_OMIT_LIKE_OPTIMIZATION
13416   "OMIT_LIKE_OPTIMIZATION",
13417 #endif
13418 #ifdef SQLITE_OMIT_LOAD_EXTENSION
13419   "OMIT_LOAD_EXTENSION",
13420 #endif
13421 #ifdef SQLITE_OMIT_LOCALTIME
13422   "OMIT_LOCALTIME",
13423 #endif
13424 #ifdef SQLITE_OMIT_LOOKASIDE
13425   "OMIT_LOOKASIDE",
13426 #endif
13427 #ifdef SQLITE_OMIT_MEMORYDB
13428   "OMIT_MEMORYDB",
13429 #endif
13430 #ifdef SQLITE_OMIT_OR_OPTIMIZATION
13431   "OMIT_OR_OPTIMIZATION",
13432 #endif
13433 #ifdef SQLITE_OMIT_PAGER_PRAGMAS
13434   "OMIT_PAGER_PRAGMAS",
13435 #endif
13436 #ifdef SQLITE_OMIT_PRAGMA
13437   "OMIT_PRAGMA",
13438 #endif
13439 #ifdef SQLITE_OMIT_PROGRESS_CALLBACK
13440   "OMIT_PROGRESS_CALLBACK",
13441 #endif
13442 #ifdef SQLITE_OMIT_QUICKBALANCE
13443   "OMIT_QUICKBALANCE",
13444 #endif
13445 #ifdef SQLITE_OMIT_REINDEX
13446   "OMIT_REINDEX",
13447 #endif
13448 #ifdef SQLITE_OMIT_SCHEMA_PRAGMAS
13449   "OMIT_SCHEMA_PRAGMAS",
13450 #endif
13451 #ifdef SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS
13452   "OMIT_SCHEMA_VERSION_PRAGMAS",
13453 #endif
13454 #ifdef SQLITE_OMIT_SHARED_CACHE
13455   "OMIT_SHARED_CACHE",
13456 #endif
13457 #ifdef SQLITE_OMIT_SUBQUERY
13458   "OMIT_SUBQUERY",
13459 #endif
13460 #ifdef SQLITE_OMIT_TCL_VARIABLE
13461   "OMIT_TCL_VARIABLE",
13462 #endif
13463 #ifdef SQLITE_OMIT_TEMPDB
13464   "OMIT_TEMPDB",
13465 #endif
13466 #ifdef SQLITE_OMIT_TRACE
13467   "OMIT_TRACE",
13468 #endif
13469 #ifdef SQLITE_OMIT_TRIGGER
13470   "OMIT_TRIGGER",
13471 #endif
13472 #ifdef SQLITE_OMIT_TRUNCATE_OPTIMIZATION
13473   "OMIT_TRUNCATE_OPTIMIZATION",
13474 #endif
13475 #ifdef SQLITE_OMIT_UTF16
13476   "OMIT_UTF16",
13477 #endif
13478 #ifdef SQLITE_OMIT_VACUUM
13479   "OMIT_VACUUM",
13480 #endif
13481 #ifdef SQLITE_OMIT_VIEW
13482   "OMIT_VIEW",
13483 #endif
13484 #ifdef SQLITE_OMIT_VIRTUALTABLE
13485   "OMIT_VIRTUALTABLE",
13486 #endif
13487 #ifdef SQLITE_OMIT_WAL
13488   "OMIT_WAL",
13489 #endif
13490 #ifdef SQLITE_OMIT_WSD
13491   "OMIT_WSD",
13492 #endif
13493 #ifdef SQLITE_OMIT_XFER_OPT
13494   "OMIT_XFER_OPT",
13495 #endif
13496 #ifdef SQLITE_PERFORMANCE_TRACE
13497   "PERFORMANCE_TRACE",
13498 #endif
13499 #ifdef SQLITE_PROXY_DEBUG
13500   "PROXY_DEBUG",
13501 #endif
13502 #ifdef SQLITE_RTREE_INT_ONLY
13503   "RTREE_INT_ONLY",
13504 #endif
13505 #ifdef SQLITE_SECURE_DELETE
13506   "SECURE_DELETE",
13507 #endif
13508 #ifdef SQLITE_SMALL_STACK
13509   "SMALL_STACK",
13510 #endif
13511 #ifdef SQLITE_SOUNDEX
13512   "SOUNDEX",
13513 #endif
13514 #ifdef SQLITE_SYSTEM_MALLOC
13515   "SYSTEM_MALLOC",
13516 #endif
13517 #ifdef SQLITE_TCL
13518   "TCL",
13519 #endif
13520 #if defined(SQLITE_TEMP_STORE) && !defined(SQLITE_TEMP_STORE_xc)
13521   "TEMP_STORE=" CTIMEOPT_VAL(SQLITE_TEMP_STORE),
13522 #endif
13523 #ifdef SQLITE_TEST
13524   "TEST",
13525 #endif
13526 #if defined(SQLITE_THREADSAFE)
13527   "THREADSAFE=" CTIMEOPT_VAL(SQLITE_THREADSAFE),
13528 #endif
13529 #ifdef SQLITE_USE_ALLOCA
13530   "USE_ALLOCA",
13531 #endif
13532 #ifdef SQLITE_WIN32_MALLOC
13533   "WIN32_MALLOC",
13534 #endif
13535 #ifdef SQLITE_ZERO_MALLOC
13536   "ZERO_MALLOC"
13537 #endif
13538 };
13539 
13540 /*
13541 ** Given the name of a compile-time option, return true if that option
13542 ** was used and false if not.
13543 **
13544 ** The name can optionally begin with "SQLITE_" but the "SQLITE_" prefix
13545 ** is not required for a match.
13546 */
13547 SQLITE_API int sqlite3_compileoption_used(const char *zOptName){
13548   int i, n;
13549   if( sqlite3StrNICmp(zOptName, "SQLITE_", 7)==0 ) zOptName += 7;
13550   n = sqlite3Strlen30(zOptName);
13551 
13552   /* Since ArraySize(azCompileOpt) is normally in single digits, a
13553   ** linear search is adequate.  No need for a binary search. */
13554   for(i=0; i<ArraySize(azCompileOpt); i++){
13555     if( sqlite3StrNICmp(zOptName, azCompileOpt[i], n)==0
13556      && sqlite3CtypeMap[(unsigned char)azCompileOpt[i][n]]==0
13557     ){
13558       return 1;
13559     }
13560   }
13561   return 0;
13562 }
13563 
13564 /*
13565 ** Return the N-th compile-time option string.  If N is out of range,
13566 ** return a NULL pointer.
13567 */
13568 SQLITE_API const char *sqlite3_compileoption_get(int N){
13569   if( N>=0 && N<ArraySize(azCompileOpt) ){
13570     return azCompileOpt[N];
13571   }
13572   return 0;
13573 }
13574 
13575 #endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
13576 
13577 /************** End of ctime.c ***********************************************/
13578 /************** Begin file status.c ******************************************/
13579 /*
13580 ** 2008 June 18
13581 **
13582 ** The author disclaims copyright to this source code.  In place of
13583 ** a legal notice, here is a blessing:
13584 **
13585 **    May you do good and not evil.
13586 **    May you find forgiveness for yourself and forgive others.
13587 **    May you share freely, never taking more than you give.
13588 **
13589 *************************************************************************
13590 **
13591 ** This module implements the sqlite3_status() interface and related
13592 ** functionality.
13593 */
13594 /************** Include vdbeInt.h in the middle of status.c ******************/
13595 /************** Begin file vdbeInt.h *****************************************/
13596 /*
13597 ** 2003 September 6
13598 **
13599 ** The author disclaims copyright to this source code.  In place of
13600 ** a legal notice, here is a blessing:
13601 **
13602 **    May you do good and not evil.
13603 **    May you find forgiveness for yourself and forgive others.
13604 **    May you share freely, never taking more than you give.
13605 **
13606 *************************************************************************
13607 ** This is the header file for information that is private to the
13608 ** VDBE.  This information used to all be at the top of the single
13609 ** source code file "vdbe.c".  When that file became too big (over
13610 ** 6000 lines long) it was split up into several smaller files and
13611 ** this header information was factored out.
13612 */
13613 #ifndef _VDBEINT_H_
13614 #define _VDBEINT_H_
13615 
13616 /*
13617 ** The maximum number of times that a statement will try to reparse
13618 ** itself before giving up and returning SQLITE_SCHEMA.
13619 */
13620 #ifndef SQLITE_MAX_SCHEMA_RETRY
13621 # define SQLITE_MAX_SCHEMA_RETRY 50
13622 #endif
13623 
13624 /*
13625 ** SQL is translated into a sequence of instructions to be
13626 ** executed by a virtual machine.  Each instruction is an instance
13627 ** of the following structure.
13628 */
13629 typedef struct VdbeOp Op;
13630 
13631 /*
13632 ** Boolean values
13633 */
13634 typedef unsigned Bool;
13635 
13636 /* Opaque type used by code in vdbesort.c */
13637 typedef struct VdbeSorter VdbeSorter;
13638 
13639 /* Opaque type used by the explainer */
13640 typedef struct Explain Explain;
13641 
13642 /* Elements of the linked list at Vdbe.pAuxData */
13643 typedef struct AuxData AuxData;
13644 
13645 /*
13646 ** A cursor is a pointer into a single BTree within a database file.
13647 ** The cursor can seek to a BTree entry with a particular key, or
13648 ** loop over all entries of the Btree.  You can also insert new BTree
13649 ** entries or retrieve the key or data from the entry that the cursor
13650 ** is currently pointing to.
13651 **
13652 ** Cursors can also point to virtual tables, sorters, or "pseudo-tables".
13653 ** A pseudo-table is a single-row table implemented by registers.
13654 **
13655 ** Every cursor that the virtual machine has open is represented by an
13656 ** instance of the following structure.
13657 */
13658 struct VdbeCursor {
13659   BtCursor *pCursor;    /* The cursor structure of the backend */
13660   Btree *pBt;           /* Separate file holding temporary table */
13661   KeyInfo *pKeyInfo;    /* Info about index keys needed by index cursors */
13662   int seekResult;       /* Result of previous sqlite3BtreeMoveto() */
13663   int pseudoTableReg;   /* Register holding pseudotable content. */
13664   i16 nField;           /* Number of fields in the header */
13665   u16 nHdrParsed;       /* Number of header fields parsed so far */
13666   i8 iDb;               /* Index of cursor database in db->aDb[] (or -1) */
13667   u8 nullRow;           /* True if pointing to a row with no data */
13668   u8 rowidIsValid;      /* True if lastRowid is valid */
13669   u8 deferredMoveto;    /* A call to sqlite3BtreeMoveto() is needed */
13670   Bool useRandomRowid:1;/* Generate new record numbers semi-randomly */
13671   Bool isTable:1;       /* True if a table requiring integer keys */
13672   Bool isOrdered:1;     /* True if the underlying table is BTREE_UNORDERED */
13673   Bool multiPseudo:1;   /* Multi-register pseudo-cursor */
13674   sqlite3_vtab_cursor *pVtabCursor;  /* The cursor for a virtual table */
13675   i64 seqCount;         /* Sequence counter */
13676   i64 movetoTarget;     /* Argument to the deferred sqlite3BtreeMoveto() */
13677   i64 lastRowid;        /* Rowid being deleted by OP_Delete */
13678   VdbeSorter *pSorter;  /* Sorter object for OP_SorterOpen cursors */
13679 
13680   /* Cached information about the header for the data record that the
13681   ** cursor is currently pointing to.  Only valid if cacheStatus matches
13682   ** Vdbe.cacheCtr.  Vdbe.cacheCtr will never take on the value of
13683   ** CACHE_STALE and so setting cacheStatus=CACHE_STALE guarantees that
13684   ** the cache is out of date.
13685   **
13686   ** aRow might point to (ephemeral) data for the current row, or it might
13687   ** be NULL.
13688   */
13689   u32 cacheStatus;      /* Cache is valid if this matches Vdbe.cacheCtr */
13690   u32 payloadSize;      /* Total number of bytes in the record */
13691   u32 szRow;            /* Byte available in aRow */
13692   u32 iHdrOffset;       /* Offset to next unparsed byte of the header */
13693   const u8 *aRow;       /* Data for the current row, if all on one page */
13694   u32 aType[1];         /* Type values for all entries in the record */
13695   /* 2*nField extra array elements allocated for aType[], beyond the one
13696   ** static element declared in the structure.  nField total array slots for
13697   ** aType[] and nField+1 array slots for aOffset[] */
13698 };
13699 typedef struct VdbeCursor VdbeCursor;
13700 
13701 /*
13702 ** When a sub-program is executed (OP_Program), a structure of this type
13703 ** is allocated to store the current value of the program counter, as
13704 ** well as the current memory cell array and various other frame specific
13705 ** values stored in the Vdbe struct. When the sub-program is finished,
13706 ** these values are copied back to the Vdbe from the VdbeFrame structure,
13707 ** restoring the state of the VM to as it was before the sub-program
13708 ** began executing.
13709 **
13710 ** The memory for a VdbeFrame object is allocated and managed by a memory
13711 ** cell in the parent (calling) frame. When the memory cell is deleted or
13712 ** overwritten, the VdbeFrame object is not freed immediately. Instead, it
13713 ** is linked into the Vdbe.pDelFrame list. The contents of the Vdbe.pDelFrame
13714 ** list is deleted when the VM is reset in VdbeHalt(). The reason for doing
13715 ** this instead of deleting the VdbeFrame immediately is to avoid recursive
13716 ** calls to sqlite3VdbeMemRelease() when the memory cells belonging to the
13717 ** child frame are released.
13718 **
13719 ** The currently executing frame is stored in Vdbe.pFrame. Vdbe.pFrame is
13720 ** set to NULL if the currently executing frame is the main program.
13721 */
13722 typedef struct VdbeFrame VdbeFrame;
13723 struct VdbeFrame {
13724   Vdbe *v;                /* VM this frame belongs to */
13725   VdbeFrame *pParent;     /* Parent of this frame, or NULL if parent is main */
13726   Op *aOp;                /* Program instructions for parent frame */
13727   Mem *aMem;              /* Array of memory cells for parent frame */
13728   u8 *aOnceFlag;          /* Array of OP_Once flags for parent frame */
13729   VdbeCursor **apCsr;     /* Array of Vdbe cursors for parent frame */
13730   void *token;            /* Copy of SubProgram.token */
13731   i64 lastRowid;          /* Last insert rowid (sqlite3.lastRowid) */
13732   int nCursor;            /* Number of entries in apCsr */
13733   int pc;                 /* Program Counter in parent (calling) frame */
13734   int nOp;                /* Size of aOp array */
13735   int nMem;               /* Number of entries in aMem */
13736   int nOnceFlag;          /* Number of entries in aOnceFlag */
13737   int nChildMem;          /* Number of memory cells for child frame */
13738   int nChildCsr;          /* Number of cursors for child frame */
13739   int nChange;            /* Statement changes (Vdbe.nChanges)     */
13740 };
13741 
13742 #define VdbeFrameMem(p) ((Mem *)&((u8 *)p)[ROUND8(sizeof(VdbeFrame))])
13743 
13744 /*
13745 ** A value for VdbeCursor.cacheValid that means the cache is always invalid.
13746 */
13747 #define CACHE_STALE 0
13748 
13749 /*
13750 ** Internally, the vdbe manipulates nearly all SQL values as Mem
13751 ** structures. Each Mem struct may cache multiple representations (string,
13752 ** integer etc.) of the same value.
13753 */
13754 struct Mem {
13755   sqlite3 *db;        /* The associated database connection */
13756   char *z;            /* String or BLOB value */
13757   double r;           /* Real value */
13758   union {
13759     i64 i;              /* Integer value used when MEM_Int is set in flags */
13760     int nZero;          /* Used when bit MEM_Zero is set in flags */
13761     FuncDef *pDef;      /* Used only when flags==MEM_Agg */
13762     RowSet *pRowSet;    /* Used only when flags==MEM_RowSet */
13763     VdbeFrame *pFrame;  /* Used when flags==MEM_Frame */
13764   } u;
13765   int n;              /* Number of characters in string value, excluding '\0' */
13766   u16 flags;          /* Some combination of MEM_Null, MEM_Str, MEM_Dyn, etc. */
13767   u8  type;           /* One of SQLITE_NULL, SQLITE_TEXT, SQLITE_INTEGER, etc */
13768   u8  enc;            /* SQLITE_UTF8, SQLITE_UTF16BE, SQLITE_UTF16LE */
13769 #ifdef SQLITE_DEBUG
13770   Mem *pScopyFrom;    /* This Mem is a shallow copy of pScopyFrom */
13771   void *pFiller;      /* So that sizeof(Mem) is a multiple of 8 */
13772 #endif
13773   void (*xDel)(void *);  /* If not null, call this function to delete Mem.z */
13774   char *zMalloc;      /* Dynamic buffer allocated by sqlite3_malloc() */
13775 };
13776 
13777 /* One or more of the following flags are set to indicate the validOK
13778 ** representations of the value stored in the Mem struct.
13779 **
13780 ** If the MEM_Null flag is set, then the value is an SQL NULL value.
13781 ** No other flags may be set in this case.
13782 **
13783 ** If the MEM_Str flag is set then Mem.z points at a string representation.
13784 ** Usually this is encoded in the same unicode encoding as the main
13785 ** database (see below for exceptions). If the MEM_Term flag is also
13786 ** set, then the string is nul terminated. The MEM_Int and MEM_Real
13787 ** flags may coexist with the MEM_Str flag.
13788 */
13789 #define MEM_Null      0x0001   /* Value is NULL */
13790 #define MEM_Str       0x0002   /* Value is a string */
13791 #define MEM_Int       0x0004   /* Value is an integer */
13792 #define MEM_Real      0x0008   /* Value is a real number */
13793 #define MEM_Blob      0x0010   /* Value is a BLOB */
13794 #define MEM_RowSet    0x0020   /* Value is a RowSet object */
13795 #define MEM_Frame     0x0040   /* Value is a VdbeFrame object */
13796 #define MEM_Invalid   0x0080   /* Value is undefined */
13797 #define MEM_Cleared   0x0100   /* NULL set by OP_Null, not from data */
13798 #define MEM_TypeMask  0x01ff   /* Mask of type bits */
13799 
13800 
13801 /* Whenever Mem contains a valid string or blob representation, one of
13802 ** the following flags must be set to determine the memory management
13803 ** policy for Mem.z.  The MEM_Term flag tells us whether or not the
13804 ** string is \000 or \u0000 terminated
13805 */
13806 #define MEM_Term      0x0200   /* String rep is nul terminated */
13807 #define MEM_Dyn       0x0400   /* Need to call sqliteFree() on Mem.z */
13808 #define MEM_Static    0x0800   /* Mem.z points to a static string */
13809 #define MEM_Ephem     0x1000   /* Mem.z points to an ephemeral string */
13810 #define MEM_Agg       0x2000   /* Mem.z points to an agg function context */
13811 #define MEM_Zero      0x4000   /* Mem.i contains count of 0s appended to blob */
13812 #ifdef SQLITE_OMIT_INCRBLOB
13813   #undef MEM_Zero
13814   #define MEM_Zero 0x0000
13815 #endif
13816 
13817 /*
13818 ** Clear any existing type flags from a Mem and replace them with f
13819 */
13820 #define MemSetTypeFlag(p, f) \
13821    ((p)->flags = ((p)->flags&~(MEM_TypeMask|MEM_Zero))|f)
13822 
13823 /*
13824 ** Return true if a memory cell is not marked as invalid.  This macro
13825 ** is for use inside assert() statements only.
13826 */
13827 #ifdef SQLITE_DEBUG
13828 #define memIsValid(M)  ((M)->flags & MEM_Invalid)==0
13829 #endif
13830 
13831 /*
13832 ** Each auxilliary data pointer stored by a user defined function
13833 ** implementation calling sqlite3_set_auxdata() is stored in an instance
13834 ** of this structure. All such structures associated with a single VM
13835 ** are stored in a linked list headed at Vdbe.pAuxData. All are destroyed
13836 ** when the VM is halted (if not before).
13837 */
13838 struct AuxData {
13839   int iOp;                        /* Instruction number of OP_Function opcode */
13840   int iArg;                       /* Index of function argument. */
13841   void *pAux;                     /* Aux data pointer */
13842   void (*xDelete)(void *);        /* Destructor for the aux data */
13843   AuxData *pNext;                 /* Next element in list */
13844 };
13845 
13846 /*
13847 ** The "context" argument for a installable function.  A pointer to an
13848 ** instance of this structure is the first argument to the routines used
13849 ** implement the SQL functions.
13850 **
13851 ** There is a typedef for this structure in sqlite.h.  So all routines,
13852 ** even the public interface to SQLite, can use a pointer to this structure.
13853 ** But this file is the only place where the internal details of this
13854 ** structure are known.
13855 **
13856 ** This structure is defined inside of vdbeInt.h because it uses substructures
13857 ** (Mem) which are only defined there.
13858 */
13859 struct sqlite3_context {
13860   FuncDef *pFunc;       /* Pointer to function information.  MUST BE FIRST */
13861   Mem s;                /* The return value is stored here */
13862   Mem *pMem;            /* Memory cell used to store aggregate context */
13863   CollSeq *pColl;       /* Collating sequence */
13864   Vdbe *pVdbe;          /* The VM that owns this context */
13865   int iOp;              /* Instruction number of OP_Function */
13866   int isError;          /* Error code returned by the function. */
13867   u8 skipFlag;          /* Skip skip accumulator loading if true */
13868   u8 fErrorOrAux;       /* isError!=0 or pVdbe->pAuxData modified */
13869 };
13870 
13871 /*
13872 ** An Explain object accumulates indented output which is helpful
13873 ** in describing recursive data structures.
13874 */
13875 struct Explain {
13876   Vdbe *pVdbe;       /* Attach the explanation to this Vdbe */
13877   StrAccum str;      /* The string being accumulated */
13878   int nIndent;       /* Number of elements in aIndent */
13879   u16 aIndent[100];  /* Levels of indentation */
13880   char zBase[100];   /* Initial space */
13881 };
13882 
13883 /* A bitfield type for use inside of structures.  Always follow with :N where
13884 ** N is the number of bits.
13885 */
13886 typedef unsigned bft;  /* Bit Field Type */
13887 
13888 /*
13889 ** An instance of the virtual machine.  This structure contains the complete
13890 ** state of the virtual machine.
13891 **
13892 ** The "sqlite3_stmt" structure pointer that is returned by sqlite3_prepare()
13893 ** is really a pointer to an instance of this structure.
13894 **
13895 ** The Vdbe.inVtabMethod variable is set to non-zero for the duration of
13896 ** any virtual table method invocations made by the vdbe program. It is
13897 ** set to 2 for xDestroy method calls and 1 for all other methods. This
13898 ** variable is used for two purposes: to allow xDestroy methods to execute
13899 ** "DROP TABLE" statements and to prevent some nasty side effects of
13900 ** malloc failure when SQLite is invoked recursively by a virtual table
13901 ** method function.
13902 */
13903 struct Vdbe {
13904   sqlite3 *db;            /* The database connection that owns this statement */
13905   Op *aOp;                /* Space to hold the virtual machine's program */
13906   Mem *aMem;              /* The memory locations */
13907   Mem **apArg;            /* Arguments to currently executing user function */
13908   Mem *aColName;          /* Column names to return */
13909   Mem *pResultSet;        /* Pointer to an array of results */
13910   Parse *pParse;          /* Parsing context used to create this Vdbe */
13911   int nMem;               /* Number of memory locations currently allocated */
13912   int nOp;                /* Number of instructions in the program */
13913   int nCursor;            /* Number of slots in apCsr[] */
13914   u32 magic;              /* Magic number for sanity checking */
13915   char *zErrMsg;          /* Error message written here */
13916   Vdbe *pPrev,*pNext;     /* Linked list of VDBEs with the same Vdbe.db */
13917   VdbeCursor **apCsr;     /* One element of this array for each open cursor */
13918   Mem *aVar;              /* Values for the OP_Variable opcode. */
13919   char **azVar;           /* Name of variables */
13920   ynVar nVar;             /* Number of entries in aVar[] */
13921   ynVar nzVar;            /* Number of entries in azVar[] */
13922   u32 cacheCtr;           /* VdbeCursor row cache generation counter */
13923   int pc;                 /* The program counter */
13924   int rc;                 /* Value to return */
13925   u16 nResColumn;         /* Number of columns in one row of the result set */
13926   u8 errorAction;         /* Recovery action to do in case of an error */
13927   u8 minWriteFileFormat;  /* Minimum file format for writable database files */
13928   bft explain:2;          /* True if EXPLAIN present on SQL command */
13929   bft inVtabMethod:2;     /* See comments above */
13930   bft changeCntOn:1;      /* True to update the change-counter */
13931   bft expired:1;          /* True if the VM needs to be recompiled */
13932   bft runOnlyOnce:1;      /* Automatically expire on reset */
13933   bft usesStmtJournal:1;  /* True if uses a statement journal */
13934   bft readOnly:1;         /* True for statements that do not write */
13935   bft bIsReader:1;        /* True for statements that read */
13936   bft isPrepareV2:1;      /* True if prepared with prepare_v2() */
13937   bft doingRerun:1;       /* True if rerunning after an auto-reprepare */
13938   int nChange;            /* Number of db changes made since last reset */
13939   yDbMask btreeMask;      /* Bitmask of db->aDb[] entries referenced */
13940   yDbMask lockMask;       /* Subset of btreeMask that requires a lock */
13941   int iStatement;         /* Statement number (or 0 if has not opened stmt) */
13942   u32 aCounter[5];        /* Counters used by sqlite3_stmt_status() */
13943 #ifndef SQLITE_OMIT_TRACE
13944   i64 startTime;          /* Time when query started - used for profiling */
13945 #endif
13946   i64 iCurrentTime;       /* Value of julianday('now') for this statement */
13947   i64 nFkConstraint;      /* Number of imm. FK constraints this VM */
13948   i64 nStmtDefCons;       /* Number of def. constraints when stmt started */
13949   i64 nStmtDefImmCons;    /* Number of def. imm constraints when stmt started */
13950   char *zSql;             /* Text of the SQL statement that generated this */
13951   void *pFree;            /* Free this when deleting the vdbe */
13952 #ifdef SQLITE_ENABLE_TREE_EXPLAIN
13953   Explain *pExplain;      /* The explainer */
13954   char *zExplain;         /* Explanation of data structures */
13955 #endif
13956   VdbeFrame *pFrame;      /* Parent frame */
13957   VdbeFrame *pDelFrame;   /* List of frame objects to free on VM reset */
13958   int nFrame;             /* Number of frames in pFrame list */
13959   u32 expmask;            /* Binding to these vars invalidates VM */
13960   SubProgram *pProgram;   /* Linked list of all sub-programs used by VM */
13961   int nOnceFlag;          /* Size of array aOnceFlag[] */
13962   u8 *aOnceFlag;          /* Flags for OP_Once */
13963   AuxData *pAuxData;      /* Linked list of auxdata allocations */
13964 };
13965 
13966 /*
13967 ** The following are allowed values for Vdbe.magic
13968 */
13969 #define VDBE_MAGIC_INIT     0x26bceaa5    /* Building a VDBE program */
13970 #define VDBE_MAGIC_RUN      0xbdf20da3    /* VDBE is ready to execute */
13971 #define VDBE_MAGIC_HALT     0x519c2973    /* VDBE has completed execution */
13972 #define VDBE_MAGIC_DEAD     0xb606c3c8    /* The VDBE has been deallocated */
13973 
13974 /*
13975 ** Function prototypes
13976 */
13977 SQLITE_PRIVATE void sqlite3VdbeFreeCursor(Vdbe *, VdbeCursor*);
13978 void sqliteVdbePopStack(Vdbe*,int);
13979 SQLITE_PRIVATE int sqlite3VdbeCursorMoveto(VdbeCursor*);
13980 #if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
13981 SQLITE_PRIVATE void sqlite3VdbePrintOp(FILE*, int, Op*);
13982 #endif
13983 SQLITE_PRIVATE u32 sqlite3VdbeSerialTypeLen(u32);
13984 SQLITE_PRIVATE u32 sqlite3VdbeSerialType(Mem*, int);
13985 SQLITE_PRIVATE u32 sqlite3VdbeSerialPut(unsigned char*, Mem*, u32);
13986 SQLITE_PRIVATE u32 sqlite3VdbeSerialGet(const unsigned char*, u32, Mem*);
13987 SQLITE_PRIVATE void sqlite3VdbeDeleteAuxData(Vdbe*, int, int);
13988 
13989 int sqlite2BtreeKeyCompare(BtCursor *, const void *, int, int, int *);
13990 SQLITE_PRIVATE int sqlite3VdbeIdxKeyCompare(VdbeCursor*,UnpackedRecord*,int*);
13991 SQLITE_PRIVATE int sqlite3VdbeIdxRowid(sqlite3*, BtCursor *, i64 *);
13992 SQLITE_PRIVATE int sqlite3MemCompare(const Mem*, const Mem*, const CollSeq*);
13993 SQLITE_PRIVATE int sqlite3VdbeExec(Vdbe*);
13994 SQLITE_PRIVATE int sqlite3VdbeList(Vdbe*);
13995 SQLITE_PRIVATE int sqlite3VdbeHalt(Vdbe*);
13996 SQLITE_PRIVATE int sqlite3VdbeChangeEncoding(Mem *, int);
13997 SQLITE_PRIVATE int sqlite3VdbeMemTooBig(Mem*);
13998 SQLITE_PRIVATE int sqlite3VdbeMemCopy(Mem*, const Mem*);
13999 SQLITE_PRIVATE void sqlite3VdbeMemShallowCopy(Mem*, const Mem*, int);
14000 SQLITE_PRIVATE void sqlite3VdbeMemMove(Mem*, Mem*);
14001 SQLITE_PRIVATE int sqlite3VdbeMemNulTerminate(Mem*);
14002 SQLITE_PRIVATE int sqlite3VdbeMemSetStr(Mem*, const char*, int, u8, void(*)(void*));
14003 SQLITE_PRIVATE void sqlite3VdbeMemSetInt64(Mem*, i64);
14004 #ifdef SQLITE_OMIT_FLOATING_POINT
14005 # define sqlite3VdbeMemSetDouble sqlite3VdbeMemSetInt64
14006 #else
14007 SQLITE_PRIVATE   void sqlite3VdbeMemSetDouble(Mem*, double);
14008 #endif
14009 SQLITE_PRIVATE void sqlite3VdbeMemSetNull(Mem*);
14010 SQLITE_PRIVATE void sqlite3VdbeMemSetZeroBlob(Mem*,int);
14011 SQLITE_PRIVATE void sqlite3VdbeMemSetRowSet(Mem*);
14012 SQLITE_PRIVATE int sqlite3VdbeMemMakeWriteable(Mem*);
14013 SQLITE_PRIVATE int sqlite3VdbeMemStringify(Mem*, int);
14014 SQLITE_PRIVATE i64 sqlite3VdbeIntValue(Mem*);
14015 SQLITE_PRIVATE int sqlite3VdbeMemIntegerify(Mem*);
14016 SQLITE_PRIVATE double sqlite3VdbeRealValue(Mem*);
14017 SQLITE_PRIVATE void sqlite3VdbeIntegerAffinity(Mem*);
14018 SQLITE_PRIVATE int sqlite3VdbeMemRealify(Mem*);
14019 SQLITE_PRIVATE int sqlite3VdbeMemNumerify(Mem*);
14020 SQLITE_PRIVATE int sqlite3VdbeMemFromBtree(BtCursor*,u32,u32,int,Mem*);
14021 SQLITE_PRIVATE void sqlite3VdbeMemRelease(Mem *p);
14022 SQLITE_PRIVATE void sqlite3VdbeMemReleaseExternal(Mem *p);
14023 #define VdbeMemRelease(X)  \
14024   if((X)->flags&(MEM_Agg|MEM_Dyn|MEM_RowSet|MEM_Frame)) \
14025     sqlite3VdbeMemReleaseExternal(X);
14026 SQLITE_PRIVATE int sqlite3VdbeMemFinalize(Mem*, FuncDef*);
14027 SQLITE_PRIVATE const char *sqlite3OpcodeName(int);
14028 SQLITE_PRIVATE int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve);
14029 SQLITE_PRIVATE int sqlite3VdbeCloseStatement(Vdbe *, int);
14030 SQLITE_PRIVATE void sqlite3VdbeFrameDelete(VdbeFrame*);
14031 SQLITE_PRIVATE int sqlite3VdbeFrameRestore(VdbeFrame *);
14032 SQLITE_PRIVATE void sqlite3VdbeMemStoreType(Mem *pMem);
14033 SQLITE_PRIVATE int sqlite3VdbeTransferError(Vdbe *p);
14034 
14035 SQLITE_PRIVATE int sqlite3VdbeSorterInit(sqlite3 *, VdbeCursor *);
14036 SQLITE_PRIVATE void sqlite3VdbeSorterClose(sqlite3 *, VdbeCursor *);
14037 SQLITE_PRIVATE int sqlite3VdbeSorterRowkey(const VdbeCursor *, Mem *);
14038 SQLITE_PRIVATE int sqlite3VdbeSorterNext(sqlite3 *, const VdbeCursor *, int *);
14039 SQLITE_PRIVATE int sqlite3VdbeSorterRewind(sqlite3 *, const VdbeCursor *, int *);
14040 SQLITE_PRIVATE int sqlite3VdbeSorterWrite(sqlite3 *, const VdbeCursor *, Mem *);
14041 SQLITE_PRIVATE int sqlite3VdbeSorterCompare(const VdbeCursor *, Mem *, int, int *);
14042 
14043 #if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0
14044 SQLITE_PRIVATE   void sqlite3VdbeEnter(Vdbe*);
14045 SQLITE_PRIVATE   void sqlite3VdbeLeave(Vdbe*);
14046 #else
14047 # define sqlite3VdbeEnter(X)
14048 # define sqlite3VdbeLeave(X)
14049 #endif
14050 
14051 #ifdef SQLITE_DEBUG
14052 SQLITE_PRIVATE void sqlite3VdbeMemAboutToChange(Vdbe*,Mem*);
14053 #endif
14054 
14055 #ifndef SQLITE_OMIT_FOREIGN_KEY
14056 SQLITE_PRIVATE int sqlite3VdbeCheckFk(Vdbe *, int);
14057 #else
14058 # define sqlite3VdbeCheckFk(p,i) 0
14059 #endif
14060 
14061 SQLITE_PRIVATE int sqlite3VdbeMemTranslate(Mem*, u8);
14062 #ifdef SQLITE_DEBUG
14063 SQLITE_PRIVATE   void sqlite3VdbePrintSql(Vdbe*);
14064 SQLITE_PRIVATE   void sqlite3VdbeMemPrettyPrint(Mem *pMem, char *zBuf);
14065 #endif
14066 SQLITE_PRIVATE int sqlite3VdbeMemHandleBom(Mem *pMem);
14067 
14068 #ifndef SQLITE_OMIT_INCRBLOB
14069 SQLITE_PRIVATE   int sqlite3VdbeMemExpandBlob(Mem *);
14070   #define ExpandBlob(P) (((P)->flags&MEM_Zero)?sqlite3VdbeMemExpandBlob(P):0)
14071 #else
14072   #define sqlite3VdbeMemExpandBlob(x) SQLITE_OK
14073   #define ExpandBlob(P) SQLITE_OK
14074 #endif
14075 
14076 #endif /* !defined(_VDBEINT_H_) */
14077 
14078 /************** End of vdbeInt.h *********************************************/
14079 /************** Continuing where we left off in status.c *********************/
14080 
14081 /*
14082 ** Variables in which to record status information.
14083 */
14084 typedef struct sqlite3StatType sqlite3StatType;
14085 static SQLITE_WSD struct sqlite3StatType {
14086   int nowValue[10];         /* Current value */
14087   int mxValue[10];          /* Maximum value */
14088 } sqlite3Stat = { {0,}, {0,} };
14089 
14090 
14091 /* The "wsdStat" macro will resolve to the status information
14092 ** state vector.  If writable static data is unsupported on the target,
14093 ** we have to locate the state vector at run-time.  In the more common
14094 ** case where writable static data is supported, wsdStat can refer directly
14095 ** to the "sqlite3Stat" state vector declared above.
14096 */
14097 #ifdef SQLITE_OMIT_WSD
14098 # define wsdStatInit  sqlite3StatType *x = &GLOBAL(sqlite3StatType,sqlite3Stat)
14099 # define wsdStat x[0]
14100 #else
14101 # define wsdStatInit
14102 # define wsdStat sqlite3Stat
14103 #endif
14104 
14105 /*
14106 ** Return the current value of a status parameter.
14107 */
14108 SQLITE_PRIVATE int sqlite3StatusValue(int op){
14109   wsdStatInit;
14110   assert( op>=0 && op<ArraySize(wsdStat.nowValue) );
14111   return wsdStat.nowValue[op];
14112 }
14113 
14114 /*
14115 ** Add N to the value of a status record.  It is assumed that the
14116 ** caller holds appropriate locks.
14117 */
14118 SQLITE_PRIVATE void sqlite3StatusAdd(int op, int N){
14119   wsdStatInit;
14120   assert( op>=0 && op<ArraySize(wsdStat.nowValue) );
14121   wsdStat.nowValue[op] += N;
14122   if( wsdStat.nowValue[op]>wsdStat.mxValue[op] ){
14123     wsdStat.mxValue[op] = wsdStat.nowValue[op];
14124   }
14125 }
14126 
14127 /*
14128 ** Set the value of a status to X.
14129 */
14130 SQLITE_PRIVATE void sqlite3StatusSet(int op, int X){
14131   wsdStatInit;
14132   assert( op>=0 && op<ArraySize(wsdStat.nowValue) );
14133   wsdStat.nowValue[op] = X;
14134   if( wsdStat.nowValue[op]>wsdStat.mxValue[op] ){
14135     wsdStat.mxValue[op] = wsdStat.nowValue[op];
14136   }
14137 }
14138 
14139 /*
14140 ** Query status information.
14141 **
14142 ** This implementation assumes that reading or writing an aligned
14143 ** 32-bit integer is an atomic operation.  If that assumption is not true,
14144 ** then this routine is not threadsafe.
14145 */
14146 SQLITE_API int sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag){
14147   wsdStatInit;
14148   if( op<0 || op>=ArraySize(wsdStat.nowValue) ){
14149     return SQLITE_MISUSE_BKPT;
14150   }
14151   *pCurrent = wsdStat.nowValue[op];
14152   *pHighwater = wsdStat.mxValue[op];
14153   if( resetFlag ){
14154     wsdStat.mxValue[op] = wsdStat.nowValue[op];
14155   }
14156   return SQLITE_OK;
14157 }
14158 
14159 /*
14160 ** Query status information for a single database connection
14161 */
14162 SQLITE_API int sqlite3_db_status(
14163   sqlite3 *db,          /* The database connection whose status is desired */
14164   int op,               /* Status verb */
14165   int *pCurrent,        /* Write current value here */
14166   int *pHighwater,      /* Write high-water mark here */
14167   int resetFlag         /* Reset high-water mark if true */
14168 ){
14169   int rc = SQLITE_OK;   /* Return code */
14170   sqlite3_mutex_enter(db->mutex);
14171   switch( op ){
14172     case SQLITE_DBSTATUS_LOOKASIDE_USED: {
14173       *pCurrent = db->lookaside.nOut;
14174       *pHighwater = db->lookaside.mxOut;
14175       if( resetFlag ){
14176         db->lookaside.mxOut = db->lookaside.nOut;
14177       }
14178       break;
14179     }
14180 
14181     case SQLITE_DBSTATUS_LOOKASIDE_HIT:
14182     case SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE:
14183     case SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL: {
14184       testcase( op==SQLITE_DBSTATUS_LOOKASIDE_HIT );
14185       testcase( op==SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE );
14186       testcase( op==SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL );
14187       assert( (op-SQLITE_DBSTATUS_LOOKASIDE_HIT)>=0 );
14188       assert( (op-SQLITE_DBSTATUS_LOOKASIDE_HIT)<3 );
14189       *pCurrent = 0;
14190       *pHighwater = db->lookaside.anStat[op - SQLITE_DBSTATUS_LOOKASIDE_HIT];
14191       if( resetFlag ){
14192         db->lookaside.anStat[op - SQLITE_DBSTATUS_LOOKASIDE_HIT] = 0;
14193       }
14194       break;
14195     }
14196 
14197     /*
14198     ** Return an approximation for the amount of memory currently used
14199     ** by all pagers associated with the given database connection.  The
14200     ** highwater mark is meaningless and is returned as zero.
14201     */
14202     case SQLITE_DBSTATUS_CACHE_USED: {
14203       int totalUsed = 0;
14204       int i;
14205       sqlite3BtreeEnterAll(db);
14206       for(i=0; i<db->nDb; i++){
14207         Btree *pBt = db->aDb[i].pBt;
14208         if( pBt ){
14209           Pager *pPager = sqlite3BtreePager(pBt);
14210           totalUsed += sqlite3PagerMemUsed(pPager);
14211         }
14212       }
14213       sqlite3BtreeLeaveAll(db);
14214       *pCurrent = totalUsed;
14215       *pHighwater = 0;
14216       break;
14217     }
14218 
14219     /*
14220     ** *pCurrent gets an accurate estimate of the amount of memory used
14221     ** to store the schema for all databases (main, temp, and any ATTACHed
14222     ** databases.  *pHighwater is set to zero.
14223     */
14224     case SQLITE_DBSTATUS_SCHEMA_USED: {
14225       int i;                      /* Used to iterate through schemas */
14226       int nByte = 0;              /* Used to accumulate return value */
14227 
14228       sqlite3BtreeEnterAll(db);
14229       db->pnBytesFreed = &nByte;
14230       for(i=0; i<db->nDb; i++){
14231         Schema *pSchema = db->aDb[i].pSchema;
14232         if( ALWAYS(pSchema!=0) ){
14233           HashElem *p;
14234 
14235           nByte += sqlite3GlobalConfig.m.xRoundup(sizeof(HashElem)) * (
14236               pSchema->tblHash.count
14237             + pSchema->trigHash.count
14238             + pSchema->idxHash.count
14239             + pSchema->fkeyHash.count
14240           );
14241           nByte += sqlite3MallocSize(pSchema->tblHash.ht);
14242           nByte += sqlite3MallocSize(pSchema->trigHash.ht);
14243           nByte += sqlite3MallocSize(pSchema->idxHash.ht);
14244           nByte += sqlite3MallocSize(pSchema->fkeyHash.ht);
14245 
14246           for(p=sqliteHashFirst(&pSchema->trigHash); p; p=sqliteHashNext(p)){
14247             sqlite3DeleteTrigger(db, (Trigger*)sqliteHashData(p));
14248           }
14249           for(p=sqliteHashFirst(&pSchema->tblHash); p; p=sqliteHashNext(p)){
14250             sqlite3DeleteTable(db, (Table *)sqliteHashData(p));
14251           }
14252         }
14253       }
14254       db->pnBytesFreed = 0;
14255       sqlite3BtreeLeaveAll(db);
14256 
14257       *pHighwater = 0;
14258       *pCurrent = nByte;
14259       break;
14260     }
14261 
14262     /*
14263     ** *pCurrent gets an accurate estimate of the amount of memory used
14264     ** to store all prepared statements.
14265     ** *pHighwater is set to zero.
14266     */
14267     case SQLITE_DBSTATUS_STMT_USED: {
14268       struct Vdbe *pVdbe;         /* Used to iterate through VMs */
14269       int nByte = 0;              /* Used to accumulate return value */
14270 
14271       db->pnBytesFreed = &nByte;
14272       for(pVdbe=db->pVdbe; pVdbe; pVdbe=pVdbe->pNext){
14273         sqlite3VdbeClearObject(db, pVdbe);
14274         sqlite3DbFree(db, pVdbe);
14275       }
14276       db->pnBytesFreed = 0;
14277 
14278       *pHighwater = 0;
14279       *pCurrent = nByte;
14280 
14281       break;
14282     }
14283 
14284     /*
14285     ** Set *pCurrent to the total cache hits or misses encountered by all
14286     ** pagers the database handle is connected to. *pHighwater is always set
14287     ** to zero.
14288     */
14289     case SQLITE_DBSTATUS_CACHE_HIT:
14290     case SQLITE_DBSTATUS_CACHE_MISS:
14291     case SQLITE_DBSTATUS_CACHE_WRITE:{
14292       int i;
14293       int nRet = 0;
14294       assert( SQLITE_DBSTATUS_CACHE_MISS==SQLITE_DBSTATUS_CACHE_HIT+1 );
14295       assert( SQLITE_DBSTATUS_CACHE_WRITE==SQLITE_DBSTATUS_CACHE_HIT+2 );
14296 
14297       for(i=0; i<db->nDb; i++){
14298         if( db->aDb[i].pBt ){
14299           Pager *pPager = sqlite3BtreePager(db->aDb[i].pBt);
14300           sqlite3PagerCacheStat(pPager, op, resetFlag, &nRet);
14301         }
14302       }
14303       *pHighwater = 0;
14304       *pCurrent = nRet;
14305       break;
14306     }
14307 
14308     /* Set *pCurrent to non-zero if there are unresolved deferred foreign
14309     ** key constraints.  Set *pCurrent to zero if all foreign key constraints
14310     ** have been satisfied.  The *pHighwater is always set to zero.
14311     */
14312     case SQLITE_DBSTATUS_DEFERRED_FKS: {
14313       *pHighwater = 0;
14314       *pCurrent = db->nDeferredImmCons>0 || db->nDeferredCons>0;
14315       break;
14316     }
14317 
14318     default: {
14319       rc = SQLITE_ERROR;
14320     }
14321   }
14322   sqlite3_mutex_leave(db->mutex);
14323   return rc;
14324 }
14325 
14326 /************** End of status.c **********************************************/
14327 /************** Begin file date.c ********************************************/
14328 /*
14329 ** 2003 October 31
14330 **
14331 ** The author disclaims copyright to this source code.  In place of
14332 ** a legal notice, here is a blessing:
14333 **
14334 **    May you do good and not evil.
14335 **    May you find forgiveness for yourself and forgive others.
14336 **    May you share freely, never taking more than you give.
14337 **
14338 *************************************************************************
14339 ** This file contains the C functions that implement date and time
14340 ** functions for SQLite.
14341 **
14342 ** There is only one exported symbol in this file - the function
14343 ** sqlite3RegisterDateTimeFunctions() found at the bottom of the file.
14344 ** All other code has file scope.
14345 **
14346 ** SQLite processes all times and dates as Julian Day numbers.  The
14347 ** dates and times are stored as the number of days since noon
14348 ** in Greenwich on November 24, 4714 B.C. according to the Gregorian
14349 ** calendar system.
14350 **
14351 ** 1970-01-01 00:00:00 is JD 2440587.5
14352 ** 2000-01-01 00:00:00 is JD 2451544.5
14353 **
14354 ** This implemention requires years to be expressed as a 4-digit number
14355 ** which means that only dates between 0000-01-01 and 9999-12-31 can
14356 ** be represented, even though julian day numbers allow a much wider
14357 ** range of dates.
14358 **
14359 ** The Gregorian calendar system is used for all dates and times,
14360 ** even those that predate the Gregorian calendar.  Historians usually
14361 ** use the Julian calendar for dates prior to 1582-10-15 and for some
14362 ** dates afterwards, depending on locale.  Beware of this difference.
14363 **
14364 ** The conversion algorithms are implemented based on descriptions
14365 ** in the following text:
14366 **
14367 **      Jean Meeus
14368 **      Astronomical Algorithms, 2nd Edition, 1998
14369 **      ISBM 0-943396-61-1
14370 **      Willmann-Bell, Inc
14371 **      Richmond, Virginia (USA)
14372 */
14373 /* #include <stdlib.h> */
14374 /* #include <assert.h> */
14375 #include <time.h>
14376 
14377 #ifndef SQLITE_OMIT_DATETIME_FUNCS
14378 
14379 
14380 /*
14381 ** A structure for holding a single date and time.
14382 */
14383 typedef struct DateTime DateTime;
14384 struct DateTime {
14385   sqlite3_int64 iJD; /* The julian day number times 86400000 */
14386   int Y, M, D;       /* Year, month, and day */
14387   int h, m;          /* Hour and minutes */
14388   int tz;            /* Timezone offset in minutes */
14389   double s;          /* Seconds */
14390   char validYMD;     /* True (1) if Y,M,D are valid */
14391   char validHMS;     /* True (1) if h,m,s are valid */
14392   char validJD;      /* True (1) if iJD is valid */
14393   char validTZ;      /* True (1) if tz is valid */
14394 };
14395 
14396 
14397 /*
14398 ** Convert zDate into one or more integers.  Additional arguments
14399 ** come in groups of 5 as follows:
14400 **
14401 **       N       number of digits in the integer
14402 **       min     minimum allowed value of the integer
14403 **       max     maximum allowed value of the integer
14404 **       nextC   first character after the integer
14405 **       pVal    where to write the integers value.
14406 **
14407 ** Conversions continue until one with nextC==0 is encountered.
14408 ** The function returns the number of successful conversions.
14409 */
14410 static int getDigits(const char *zDate, ...){
14411   va_list ap;
14412   int val;
14413   int N;
14414   int min;
14415   int max;
14416   int nextC;
14417   int *pVal;
14418   int cnt = 0;
14419   va_start(ap, zDate);
14420   do{
14421     N = va_arg(ap, int);
14422     min = va_arg(ap, int);
14423     max = va_arg(ap, int);
14424     nextC = va_arg(ap, int);
14425     pVal = va_arg(ap, int*);
14426     val = 0;
14427     while( N-- ){
14428       if( !sqlite3Isdigit(*zDate) ){
14429         goto end_getDigits;
14430       }
14431       val = val*10 + *zDate - '0';
14432       zDate++;
14433     }
14434     if( val<min || val>max || (nextC!=0 && nextC!=*zDate) ){
14435       goto end_getDigits;
14436     }
14437     *pVal = val;
14438     zDate++;
14439     cnt++;
14440   }while( nextC );
14441 end_getDigits:
14442   va_end(ap);
14443   return cnt;
14444 }
14445 
14446 /*
14447 ** Parse a timezone extension on the end of a date-time.
14448 ** The extension is of the form:
14449 **
14450 **        (+/-)HH:MM
14451 **
14452 ** Or the "zulu" notation:
14453 **
14454 **        Z
14455 **
14456 ** If the parse is successful, write the number of minutes
14457 ** of change in p->tz and return 0.  If a parser error occurs,
14458 ** return non-zero.
14459 **
14460 ** A missing specifier is not considered an error.
14461 */
14462 static int parseTimezone(const char *zDate, DateTime *p){
14463   int sgn = 0;
14464   int nHr, nMn;
14465   int c;
14466   while( sqlite3Isspace(*zDate) ){ zDate++; }
14467   p->tz = 0;
14468   c = *zDate;
14469   if( c=='-' ){
14470     sgn = -1;
14471   }else if( c=='+' ){
14472     sgn = +1;
14473   }else if( c=='Z' || c=='z' ){
14474     zDate++;
14475     goto zulu_time;
14476   }else{
14477     return c!=0;
14478   }
14479   zDate++;
14480   if( getDigits(zDate, 2, 0, 14, ':', &nHr, 2, 0, 59, 0, &nMn)!=2 ){
14481     return 1;
14482   }
14483   zDate += 5;
14484   p->tz = sgn*(nMn + nHr*60);
14485 zulu_time:
14486   while( sqlite3Isspace(*zDate) ){ zDate++; }
14487   return *zDate!=0;
14488 }
14489 
14490 /*
14491 ** Parse times of the form HH:MM or HH:MM:SS or HH:MM:SS.FFFF.
14492 ** The HH, MM, and SS must each be exactly 2 digits.  The
14493 ** fractional seconds FFFF can be one or more digits.
14494 **
14495 ** Return 1 if there is a parsing error and 0 on success.
14496 */
14497 static int parseHhMmSs(const char *zDate, DateTime *p){
14498   int h, m, s;
14499   double ms = 0.0;
14500   if( getDigits(zDate, 2, 0, 24, ':', &h, 2, 0, 59, 0, &m)!=2 ){
14501     return 1;
14502   }
14503   zDate += 5;
14504   if( *zDate==':' ){
14505     zDate++;
14506     if( getDigits(zDate, 2, 0, 59, 0, &s)!=1 ){
14507       return 1;
14508     }
14509     zDate += 2;
14510     if( *zDate=='.' && sqlite3Isdigit(zDate[1]) ){
14511       double rScale = 1.0;
14512       zDate++;
14513       while( sqlite3Isdigit(*zDate) ){
14514         ms = ms*10.0 + *zDate - '0';
14515         rScale *= 10.0;
14516         zDate++;
14517       }
14518       ms /= rScale;
14519     }
14520   }else{
14521     s = 0;
14522   }
14523   p->validJD = 0;
14524   p->validHMS = 1;
14525   p->h = h;
14526   p->m = m;
14527   p->s = s + ms;
14528   if( parseTimezone(zDate, p) ) return 1;
14529   p->validTZ = (p->tz!=0)?1:0;
14530   return 0;
14531 }
14532 
14533 /*
14534 ** Convert from YYYY-MM-DD HH:MM:SS to julian day.  We always assume
14535 ** that the YYYY-MM-DD is according to the Gregorian calendar.
14536 **
14537 ** Reference:  Meeus page 61
14538 */
14539 static void computeJD(DateTime *p){
14540   int Y, M, D, A, B, X1, X2;
14541 
14542   if( p->validJD ) return;
14543   if( p->validYMD ){
14544     Y = p->Y;
14545     M = p->M;
14546     D = p->D;
14547   }else{
14548     Y = 2000;  /* If no YMD specified, assume 2000-Jan-01 */
14549     M = 1;
14550     D = 1;
14551   }
14552   if( M<=2 ){
14553     Y--;
14554     M += 12;
14555   }
14556   A = Y/100;
14557   B = 2 - A + (A/4);
14558   X1 = 36525*(Y+4716)/100;
14559   X2 = 306001*(M+1)/10000;
14560   p->iJD = (sqlite3_int64)((X1 + X2 + D + B - 1524.5 ) * 86400000);
14561   p->validJD = 1;
14562   if( p->validHMS ){
14563     p->iJD += p->h*3600000 + p->m*60000 + (sqlite3_int64)(p->s*1000);
14564     if( p->validTZ ){
14565       p->iJD -= p->tz*60000;
14566       p->validYMD = 0;
14567       p->validHMS = 0;
14568       p->validTZ = 0;
14569     }
14570   }
14571 }
14572 
14573 /*
14574 ** Parse dates of the form
14575 **
14576 **     YYYY-MM-DD HH:MM:SS.FFF
14577 **     YYYY-MM-DD HH:MM:SS
14578 **     YYYY-MM-DD HH:MM
14579 **     YYYY-MM-DD
14580 **
14581 ** Write the result into the DateTime structure and return 0
14582 ** on success and 1 if the input string is not a well-formed
14583 ** date.
14584 */
14585 static int parseYyyyMmDd(const char *zDate, DateTime *p){
14586   int Y, M, D, neg;
14587 
14588   if( zDate[0]=='-' ){
14589     zDate++;
14590     neg = 1;
14591   }else{
14592     neg = 0;
14593   }
14594   if( getDigits(zDate,4,0,9999,'-',&Y,2,1,12,'-',&M,2,1,31,0,&D)!=3 ){
14595     return 1;
14596   }
14597   zDate += 10;
14598   while( sqlite3Isspace(*zDate) || 'T'==*(u8*)zDate ){ zDate++; }
14599   if( parseHhMmSs(zDate, p)==0 ){
14600     /* We got the time */
14601   }else if( *zDate==0 ){
14602     p->validHMS = 0;
14603   }else{
14604     return 1;
14605   }
14606   p->validJD = 0;
14607   p->validYMD = 1;
14608   p->Y = neg ? -Y : Y;
14609   p->M = M;
14610   p->D = D;
14611   if( p->validTZ ){
14612     computeJD(p);
14613   }
14614   return 0;
14615 }
14616 
14617 /*
14618 ** Set the time to the current time reported by the VFS.
14619 **
14620 ** Return the number of errors.
14621 */
14622 static int setDateTimeToCurrent(sqlite3_context *context, DateTime *p){
14623   p->iJD = sqlite3StmtCurrentTime(context);
14624   if( p->iJD>0 ){
14625     p->validJD = 1;
14626     return 0;
14627   }else{
14628     return 1;
14629   }
14630 }
14631 
14632 /*
14633 ** Attempt to parse the given string into a Julian Day Number.  Return
14634 ** the number of errors.
14635 **
14636 ** The following are acceptable forms for the input string:
14637 **
14638 **      YYYY-MM-DD HH:MM:SS.FFF  +/-HH:MM
14639 **      DDDD.DD
14640 **      now
14641 **
14642 ** In the first form, the +/-HH:MM is always optional.  The fractional
14643 ** seconds extension (the ".FFF") is optional.  The seconds portion
14644 ** (":SS.FFF") is option.  The year and date can be omitted as long
14645 ** as there is a time string.  The time string can be omitted as long
14646 ** as there is a year and date.
14647 */
14648 static int parseDateOrTime(
14649   sqlite3_context *context,
14650   const char *zDate,
14651   DateTime *p
14652 ){
14653   double r;
14654   if( parseYyyyMmDd(zDate,p)==0 ){
14655     return 0;
14656   }else if( parseHhMmSs(zDate, p)==0 ){
14657     return 0;
14658   }else if( sqlite3StrICmp(zDate,"now")==0){
14659     return setDateTimeToCurrent(context, p);
14660   }else if( sqlite3AtoF(zDate, &r, sqlite3Strlen30(zDate), SQLITE_UTF8) ){
14661     p->iJD = (sqlite3_int64)(r*86400000.0 + 0.5);
14662     p->validJD = 1;
14663     return 0;
14664   }
14665   return 1;
14666 }
14667 
14668 /*
14669 ** Compute the Year, Month, and Day from the julian day number.
14670 */
14671 static void computeYMD(DateTime *p){
14672   int Z, A, B, C, D, E, X1;
14673   if( p->validYMD ) return;
14674   if( !p->validJD ){
14675     p->Y = 2000;
14676     p->M = 1;
14677     p->D = 1;
14678   }else{
14679     Z = (int)((p->iJD + 43200000)/86400000);
14680     A = (int)((Z - 1867216.25)/36524.25);
14681     A = Z + 1 + A - (A/4);
14682     B = A + 1524;
14683     C = (int)((B - 122.1)/365.25);
14684     D = (36525*C)/100;
14685     E = (int)((B-D)/30.6001);
14686     X1 = (int)(30.6001*E);
14687     p->D = B - D - X1;
14688     p->M = E<14 ? E-1 : E-13;
14689     p->Y = p->M>2 ? C - 4716 : C - 4715;
14690   }
14691   p->validYMD = 1;
14692 }
14693 
14694 /*
14695 ** Compute the Hour, Minute, and Seconds from the julian day number.
14696 */
14697 static void computeHMS(DateTime *p){
14698   int s;
14699   if( p->validHMS ) return;
14700   computeJD(p);
14701   s = (int)((p->iJD + 43200000) % 86400000);
14702   p->s = s/1000.0;
14703   s = (int)p->s;
14704   p->s -= s;
14705   p->h = s/3600;
14706   s -= p->h*3600;
14707   p->m = s/60;
14708   p->s += s - p->m*60;
14709   p->validHMS = 1;
14710 }
14711 
14712 /*
14713 ** Compute both YMD and HMS
14714 */
14715 static void computeYMD_HMS(DateTime *p){
14716   computeYMD(p);
14717   computeHMS(p);
14718 }
14719 
14720 /*
14721 ** Clear the YMD and HMS and the TZ
14722 */
14723 static void clearYMD_HMS_TZ(DateTime *p){
14724   p->validYMD = 0;
14725   p->validHMS = 0;
14726   p->validTZ = 0;
14727 }
14728 
14729 /*
14730 ** On recent Windows platforms, the localtime_s() function is available
14731 ** as part of the "Secure CRT". It is essentially equivalent to
14732 ** localtime_r() available under most POSIX platforms, except that the
14733 ** order of the parameters is reversed.
14734 **
14735 ** See http://msdn.microsoft.com/en-us/library/a442x3ye(VS.80).aspx.
14736 **
14737 ** If the user has not indicated to use localtime_r() or localtime_s()
14738 ** already, check for an MSVC build environment that provides
14739 ** localtime_s().
14740 */
14741 #if !defined(HAVE_LOCALTIME_R) && !defined(HAVE_LOCALTIME_S) && \
14742      defined(_MSC_VER) && defined(_CRT_INSECURE_DEPRECATE)
14743 #define HAVE_LOCALTIME_S 1
14744 #endif
14745 
14746 #ifndef SQLITE_OMIT_LOCALTIME
14747 /*
14748 ** The following routine implements the rough equivalent of localtime_r()
14749 ** using whatever operating-system specific localtime facility that
14750 ** is available.  This routine returns 0 on success and
14751 ** non-zero on any kind of error.
14752 **
14753 ** If the sqlite3GlobalConfig.bLocaltimeFault variable is true then this
14754 ** routine will always fail.
14755 **
14756 ** EVIDENCE-OF: R-62172-00036 In this implementation, the standard C
14757 ** library function localtime_r() is used to assist in the calculation of
14758 ** local time.
14759 */
14760 static int osLocaltime(time_t *t, struct tm *pTm){
14761   int rc;
14762 #if (!defined(HAVE_LOCALTIME_R) || !HAVE_LOCALTIME_R) \
14763       && (!defined(HAVE_LOCALTIME_S) || !HAVE_LOCALTIME_S)
14764   struct tm *pX;
14765 #if SQLITE_THREADSAFE>0
14766   sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
14767 #endif
14768   sqlite3_mutex_enter(mutex);
14769   pX = localtime(t);
14770 #ifndef SQLITE_OMIT_BUILTIN_TEST
14771   if( sqlite3GlobalConfig.bLocaltimeFault ) pX = 0;
14772 #endif
14773   if( pX ) *pTm = *pX;
14774   sqlite3_mutex_leave(mutex);
14775   rc = pX==0;
14776 #else
14777 #ifndef SQLITE_OMIT_BUILTIN_TEST
14778   if( sqlite3GlobalConfig.bLocaltimeFault ) return 1;
14779 #endif
14780 #if defined(HAVE_LOCALTIME_R) && HAVE_LOCALTIME_R
14781   rc = localtime_r(t, pTm)==0;
14782 #else
14783   rc = localtime_s(pTm, t);
14784 #endif /* HAVE_LOCALTIME_R */
14785 #endif /* HAVE_LOCALTIME_R || HAVE_LOCALTIME_S */
14786   return rc;
14787 }
14788 #endif /* SQLITE_OMIT_LOCALTIME */
14789 
14790 
14791 #ifndef SQLITE_OMIT_LOCALTIME
14792 /*
14793 ** Compute the difference (in milliseconds) between localtime and UTC
14794 ** (a.k.a. GMT) for the time value p where p is in UTC. If no error occurs,
14795 ** return this value and set *pRc to SQLITE_OK.
14796 **
14797 ** Or, if an error does occur, set *pRc to SQLITE_ERROR. The returned value
14798 ** is undefined in this case.
14799 */
14800 static sqlite3_int64 localtimeOffset(
14801   DateTime *p,                    /* Date at which to calculate offset */
14802   sqlite3_context *pCtx,          /* Write error here if one occurs */
14803   int *pRc                        /* OUT: Error code. SQLITE_OK or ERROR */
14804 ){
14805   DateTime x, y;
14806   time_t t;
14807   struct tm sLocal;
14808 
14809   /* Initialize the contents of sLocal to avoid a compiler warning. */
14810   memset(&sLocal, 0, sizeof(sLocal));
14811 
14812   x = *p;
14813   computeYMD_HMS(&x);
14814   if( x.Y<1971 || x.Y>=2038 ){
14815     /* EVIDENCE-OF: R-55269-29598 The localtime_r() C function normally only
14816     ** works for years between 1970 and 2037. For dates outside this range,
14817     ** SQLite attempts to map the year into an equivalent year within this
14818     ** range, do the calculation, then map the year back.
14819     */
14820     x.Y = 2000;
14821     x.M = 1;
14822     x.D = 1;
14823     x.h = 0;
14824     x.m = 0;
14825     x.s = 0.0;
14826   } else {
14827     int s = (int)(x.s + 0.5);
14828     x.s = s;
14829   }
14830   x.tz = 0;
14831   x.validJD = 0;
14832   computeJD(&x);
14833   t = (time_t)(x.iJD/1000 - 21086676*(i64)10000);
14834   if( osLocaltime(&t, &sLocal) ){
14835     sqlite3_result_error(pCtx, "local time unavailable", -1);
14836     *pRc = SQLITE_ERROR;
14837     return 0;
14838   }
14839   y.Y = sLocal.tm_year + 1900;
14840   y.M = sLocal.tm_mon + 1;
14841   y.D = sLocal.tm_mday;
14842   y.h = sLocal.tm_hour;
14843   y.m = sLocal.tm_min;
14844   y.s = sLocal.tm_sec;
14845   y.validYMD = 1;
14846   y.validHMS = 1;
14847   y.validJD = 0;
14848   y.validTZ = 0;
14849   computeJD(&y);
14850   *pRc = SQLITE_OK;
14851   return y.iJD - x.iJD;
14852 }
14853 #endif /* SQLITE_OMIT_LOCALTIME */
14854 
14855 /*
14856 ** Process a modifier to a date-time stamp.  The modifiers are
14857 ** as follows:
14858 **
14859 **     NNN days
14860 **     NNN hours
14861 **     NNN minutes
14862 **     NNN.NNNN seconds
14863 **     NNN months
14864 **     NNN years
14865 **     start of month
14866 **     start of year
14867 **     start of week
14868 **     start of day
14869 **     weekday N
14870 **     unixepoch
14871 **     localtime
14872 **     utc
14873 **
14874 ** Return 0 on success and 1 if there is any kind of error. If the error
14875 ** is in a system call (i.e. localtime()), then an error message is written
14876 ** to context pCtx. If the error is an unrecognized modifier, no error is
14877 ** written to pCtx.
14878 */
14879 static int parseModifier(sqlite3_context *pCtx, const char *zMod, DateTime *p){
14880   int rc = 1;
14881   int n;
14882   double r;
14883   char *z, zBuf[30];
14884   z = zBuf;
14885   for(n=0; n<ArraySize(zBuf)-1 && zMod[n]; n++){
14886     z[n] = (char)sqlite3UpperToLower[(u8)zMod[n]];
14887   }
14888   z[n] = 0;
14889   switch( z[0] ){
14890 #ifndef SQLITE_OMIT_LOCALTIME
14891     case 'l': {
14892       /*    localtime
14893       **
14894       ** Assuming the current time value is UTC (a.k.a. GMT), shift it to
14895       ** show local time.
14896       */
14897       if( strcmp(z, "localtime")==0 ){
14898         computeJD(p);
14899         p->iJD += localtimeOffset(p, pCtx, &rc);
14900         clearYMD_HMS_TZ(p);
14901       }
14902       break;
14903     }
14904 #endif
14905     case 'u': {
14906       /*
14907       **    unixepoch
14908       **
14909       ** Treat the current value of p->iJD as the number of
14910       ** seconds since 1970.  Convert to a real julian day number.
14911       */
14912       if( strcmp(z, "unixepoch")==0 && p->validJD ){
14913         p->iJD = (p->iJD + 43200)/86400 + 21086676*(i64)10000000;
14914         clearYMD_HMS_TZ(p);
14915         rc = 0;
14916       }
14917 #ifndef SQLITE_OMIT_LOCALTIME
14918       else if( strcmp(z, "utc")==0 ){
14919         sqlite3_int64 c1;
14920         computeJD(p);
14921         c1 = localtimeOffset(p, pCtx, &rc);
14922         if( rc==SQLITE_OK ){
14923           p->iJD -= c1;
14924           clearYMD_HMS_TZ(p);
14925           p->iJD += c1 - localtimeOffset(p, pCtx, &rc);
14926         }
14927       }
14928 #endif
14929       break;
14930     }
14931     case 'w': {
14932       /*
14933       **    weekday N
14934       **
14935       ** Move the date to the same time on the next occurrence of
14936       ** weekday N where 0==Sunday, 1==Monday, and so forth.  If the
14937       ** date is already on the appropriate weekday, this is a no-op.
14938       */
14939       if( strncmp(z, "weekday ", 8)==0
14940                && sqlite3AtoF(&z[8], &r, sqlite3Strlen30(&z[8]), SQLITE_UTF8)
14941                && (n=(int)r)==r && n>=0 && r<7 ){
14942         sqlite3_int64 Z;
14943         computeYMD_HMS(p);
14944         p->validTZ = 0;
14945         p->validJD = 0;
14946         computeJD(p);
14947         Z = ((p->iJD + 129600000)/86400000) % 7;
14948         if( Z>n ) Z -= 7;
14949         p->iJD += (n - Z)*86400000;
14950         clearYMD_HMS_TZ(p);
14951         rc = 0;
14952       }
14953       break;
14954     }
14955     case 's': {
14956       /*
14957       **    start of TTTTT
14958       **
14959       ** Move the date backwards to the beginning of the current day,
14960       ** or month or year.
14961       */
14962       if( strncmp(z, "start of ", 9)!=0 ) break;
14963       z += 9;
14964       computeYMD(p);
14965       p->validHMS = 1;
14966       p->h = p->m = 0;
14967       p->s = 0.0;
14968       p->validTZ = 0;
14969       p->validJD = 0;
14970       if( strcmp(z,"month")==0 ){
14971         p->D = 1;
14972         rc = 0;
14973       }else if( strcmp(z,"year")==0 ){
14974         computeYMD(p);
14975         p->M = 1;
14976         p->D = 1;
14977         rc = 0;
14978       }else if( strcmp(z,"day")==0 ){
14979         rc = 0;
14980       }
14981       break;
14982     }
14983     case '+':
14984     case '-':
14985     case '0':
14986     case '1':
14987     case '2':
14988     case '3':
14989     case '4':
14990     case '5':
14991     case '6':
14992     case '7':
14993     case '8':
14994     case '9': {
14995       double rRounder;
14996       for(n=1; z[n] && z[n]!=':' && !sqlite3Isspace(z[n]); n++){}
14997       if( !sqlite3AtoF(z, &r, n, SQLITE_UTF8) ){
14998         rc = 1;
14999         break;
15000       }
15001       if( z[n]==':' ){
15002         /* A modifier of the form (+|-)HH:MM:SS.FFF adds (or subtracts) the
15003         ** specified number of hours, minutes, seconds, and fractional seconds
15004         ** to the time.  The ".FFF" may be omitted.  The ":SS.FFF" may be
15005         ** omitted.
15006         */
15007         const char *z2 = z;
15008         DateTime tx;
15009         sqlite3_int64 day;
15010         if( !sqlite3Isdigit(*z2) ) z2++;
15011         memset(&tx, 0, sizeof(tx));
15012         if( parseHhMmSs(z2, &tx) ) break;
15013         computeJD(&tx);
15014         tx.iJD -= 43200000;
15015         day = tx.iJD/86400000;
15016         tx.iJD -= day*86400000;
15017         if( z[0]=='-' ) tx.iJD = -tx.iJD;
15018         computeJD(p);
15019         clearYMD_HMS_TZ(p);
15020         p->iJD += tx.iJD;
15021         rc = 0;
15022         break;
15023       }
15024       z += n;
15025       while( sqlite3Isspace(*z) ) z++;
15026       n = sqlite3Strlen30(z);
15027       if( n>10 || n<3 ) break;
15028       if( z[n-1]=='s' ){ z[n-1] = 0; n--; }
15029       computeJD(p);
15030       rc = 0;
15031       rRounder = r<0 ? -0.5 : +0.5;
15032       if( n==3 && strcmp(z,"day")==0 ){
15033         p->iJD += (sqlite3_int64)(r*86400000.0 + rRounder);
15034       }else if( n==4 && strcmp(z,"hour")==0 ){
15035         p->iJD += (sqlite3_int64)(r*(86400000.0/24.0) + rRounder);
15036       }else if( n==6 && strcmp(z,"minute")==0 ){
15037         p->iJD += (sqlite3_int64)(r*(86400000.0/(24.0*60.0)) + rRounder);
15038       }else if( n==6 && strcmp(z,"second")==0 ){
15039         p->iJD += (sqlite3_int64)(r*(86400000.0/(24.0*60.0*60.0)) + rRounder);
15040       }else if( n==5 && strcmp(z,"month")==0 ){
15041         int x, y;
15042         computeYMD_HMS(p);
15043         p->M += (int)r;
15044         x = p->M>0 ? (p->M-1)/12 : (p->M-12)/12;
15045         p->Y += x;
15046         p->M -= x*12;
15047         p->validJD = 0;
15048         computeJD(p);
15049         y = (int)r;
15050         if( y!=r ){
15051           p->iJD += (sqlite3_int64)((r - y)*30.0*86400000.0 + rRounder);
15052         }
15053       }else if( n==4 && strcmp(z,"year")==0 ){
15054         int y = (int)r;
15055         computeYMD_HMS(p);
15056         p->Y += y;
15057         p->validJD = 0;
15058         computeJD(p);
15059         if( y!=r ){
15060           p->iJD += (sqlite3_int64)((r - y)*365.0*86400000.0 + rRounder);
15061         }
15062       }else{
15063         rc = 1;
15064       }
15065       clearYMD_HMS_TZ(p);
15066       break;
15067     }
15068     default: {
15069       break;
15070     }
15071   }
15072   return rc;
15073 }
15074 
15075 /*
15076 ** Process time function arguments.  argv[0] is a date-time stamp.
15077 ** argv[1] and following are modifiers.  Parse them all and write
15078 ** the resulting time into the DateTime structure p.  Return 0
15079 ** on success and 1 if there are any errors.
15080 **
15081 ** If there are zero parameters (if even argv[0] is undefined)
15082 ** then assume a default value of "now" for argv[0].
15083 */
15084 static int isDate(
15085   sqlite3_context *context,
15086   int argc,
15087   sqlite3_value **argv,
15088   DateTime *p
15089 ){
15090   int i;
15091   const unsigned char *z;
15092   int eType;
15093   memset(p, 0, sizeof(*p));
15094   if( argc==0 ){
15095     return setDateTimeToCurrent(context, p);
15096   }
15097   if( (eType = sqlite3_value_type(argv[0]))==SQLITE_FLOAT
15098                    || eType==SQLITE_INTEGER ){
15099     p->iJD = (sqlite3_int64)(sqlite3_value_double(argv[0])*86400000.0 + 0.5);
15100     p->validJD = 1;
15101   }else{
15102     z = sqlite3_value_text(argv[0]);
15103     if( !z || parseDateOrTime(context, (char*)z, p) ){
15104       return 1;
15105     }
15106   }
15107   for(i=1; i<argc; i++){
15108     z = sqlite3_value_text(argv[i]);
15109     if( z==0 || parseModifier(context, (char*)z, p) ) return 1;
15110   }
15111   return 0;
15112 }
15113 
15114 
15115 /*
15116 ** The following routines implement the various date and time functions
15117 ** of SQLite.
15118 */
15119 
15120 /*
15121 **    julianday( TIMESTRING, MOD, MOD, ...)
15122 **
15123 ** Return the julian day number of the date specified in the arguments
15124 */
15125 static void juliandayFunc(
15126   sqlite3_context *context,
15127   int argc,
15128   sqlite3_value **argv
15129 ){
15130   DateTime x;
15131   if( isDate(context, argc, argv, &x)==0 ){
15132     computeJD(&x);
15133     sqlite3_result_double(context, x.iJD/86400000.0);
15134   }
15135 }
15136 
15137 /*
15138 **    datetime( TIMESTRING, MOD, MOD, ...)
15139 **
15140 ** Return YYYY-MM-DD HH:MM:SS
15141 */
15142 static void datetimeFunc(
15143   sqlite3_context *context,
15144   int argc,
15145   sqlite3_value **argv
15146 ){
15147   DateTime x;
15148   if( isDate(context, argc, argv, &x)==0 ){
15149     char zBuf[100];
15150     computeYMD_HMS(&x);
15151     sqlite3_snprintf(sizeof(zBuf), zBuf, "%04d-%02d-%02d %02d:%02d:%02d",
15152                      x.Y, x.M, x.D, x.h, x.m, (int)(x.s));
15153     sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
15154   }
15155 }
15156 
15157 /*
15158 **    time( TIMESTRING, MOD, MOD, ...)
15159 **
15160 ** Return HH:MM:SS
15161 */
15162 static void timeFunc(
15163   sqlite3_context *context,
15164   int argc,
15165   sqlite3_value **argv
15166 ){
15167   DateTime x;
15168   if( isDate(context, argc, argv, &x)==0 ){
15169     char zBuf[100];
15170     computeHMS(&x);
15171     sqlite3_snprintf(sizeof(zBuf), zBuf, "%02d:%02d:%02d", x.h, x.m, (int)x.s);
15172     sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
15173   }
15174 }
15175 
15176 /*
15177 **    date( TIMESTRING, MOD, MOD, ...)
15178 **
15179 ** Return YYYY-MM-DD
15180 */
15181 static void dateFunc(
15182   sqlite3_context *context,
15183   int argc,
15184   sqlite3_value **argv
15185 ){
15186   DateTime x;
15187   if( isDate(context, argc, argv, &x)==0 ){
15188     char zBuf[100];
15189     computeYMD(&x);
15190     sqlite3_snprintf(sizeof(zBuf), zBuf, "%04d-%02d-%02d", x.Y, x.M, x.D);
15191     sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
15192   }
15193 }
15194 
15195 /*
15196 **    strftime( FORMAT, TIMESTRING, MOD, MOD, ...)
15197 **
15198 ** Return a string described by FORMAT.  Conversions as follows:
15199 **
15200 **   %d  day of month
15201 **   %f  ** fractional seconds  SS.SSS
15202 **   %H  hour 00-24
15203 **   %j  day of year 000-366
15204 **   %J  ** Julian day number
15205 **   %m  month 01-12
15206 **   %M  minute 00-59
15207 **   %s  seconds since 1970-01-01
15208 **   %S  seconds 00-59
15209 **   %w  day of week 0-6  sunday==0
15210 **   %W  week of year 00-53
15211 **   %Y  year 0000-9999
15212 **   %%  %
15213 */
15214 static void strftimeFunc(
15215   sqlite3_context *context,
15216   int argc,
15217   sqlite3_value **argv
15218 ){
15219   DateTime x;
15220   u64 n;
15221   size_t i,j;
15222   char *z;
15223   sqlite3 *db;
15224   const char *zFmt = (const char*)sqlite3_value_text(argv[0]);
15225   char zBuf[100];
15226   if( zFmt==0 || isDate(context, argc-1, argv+1, &x) ) return;
15227   db = sqlite3_context_db_handle(context);
15228   for(i=0, n=1; zFmt[i]; i++, n++){
15229     if( zFmt[i]=='%' ){
15230       switch( zFmt[i+1] ){
15231         case 'd':
15232         case 'H':
15233         case 'm':
15234         case 'M':
15235         case 'S':
15236         case 'W':
15237           n++;
15238           /* fall thru */
15239         case 'w':
15240         case '%':
15241           break;
15242         case 'f':
15243           n += 8;
15244           break;
15245         case 'j':
15246           n += 3;
15247           break;
15248         case 'Y':
15249           n += 8;
15250           break;
15251         case 's':
15252         case 'J':
15253           n += 50;
15254           break;
15255         default:
15256           return;  /* ERROR.  return a NULL */
15257       }
15258       i++;
15259     }
15260   }
15261   testcase( n==sizeof(zBuf)-1 );
15262   testcase( n==sizeof(zBuf) );
15263   testcase( n==(u64)db->aLimit[SQLITE_LIMIT_LENGTH]+1 );
15264   testcase( n==(u64)db->aLimit[SQLITE_LIMIT_LENGTH] );
15265   if( n<sizeof(zBuf) ){
15266     z = zBuf;
15267   }else if( n>(u64)db->aLimit[SQLITE_LIMIT_LENGTH] ){
15268     sqlite3_result_error_toobig(context);
15269     return;
15270   }else{
15271     z = sqlite3DbMallocRaw(db, (int)n);
15272     if( z==0 ){
15273       sqlite3_result_error_nomem(context);
15274       return;
15275     }
15276   }
15277   computeJD(&x);
15278   computeYMD_HMS(&x);
15279   for(i=j=0; zFmt[i]; i++){
15280     if( zFmt[i]!='%' ){
15281       z[j++] = zFmt[i];
15282     }else{
15283       i++;
15284       switch( zFmt[i] ){
15285         case 'd':  sqlite3_snprintf(3, &z[j],"%02d",x.D); j+=2; break;
15286         case 'f': {
15287           double s = x.s;
15288           if( s>59.999 ) s = 59.999;
15289           sqlite3_snprintf(7, &z[j],"%06.3f", s);
15290           j += sqlite3Strlen30(&z[j]);
15291           break;
15292         }
15293         case 'H':  sqlite3_snprintf(3, &z[j],"%02d",x.h); j+=2; break;
15294         case 'W': /* Fall thru */
15295         case 'j': {
15296           int nDay;             /* Number of days since 1st day of year */
15297           DateTime y = x;
15298           y.validJD = 0;
15299           y.M = 1;
15300           y.D = 1;
15301           computeJD(&y);
15302           nDay = (int)((x.iJD-y.iJD+43200000)/86400000);
15303           if( zFmt[i]=='W' ){
15304             int wd;   /* 0=Monday, 1=Tuesday, ... 6=Sunday */
15305             wd = (int)(((x.iJD+43200000)/86400000)%7);
15306             sqlite3_snprintf(3, &z[j],"%02d",(nDay+7-wd)/7);
15307             j += 2;
15308           }else{
15309             sqlite3_snprintf(4, &z[j],"%03d",nDay+1);
15310             j += 3;
15311           }
15312           break;
15313         }
15314         case 'J': {
15315           sqlite3_snprintf(20, &z[j],"%.16g",x.iJD/86400000.0);
15316           j+=sqlite3Strlen30(&z[j]);
15317           break;
15318         }
15319         case 'm':  sqlite3_snprintf(3, &z[j],"%02d",x.M); j+=2; break;
15320         case 'M':  sqlite3_snprintf(3, &z[j],"%02d",x.m); j+=2; break;
15321         case 's': {
15322           sqlite3_snprintf(30,&z[j],"%lld",
15323                            (i64)(x.iJD/1000 - 21086676*(i64)10000));
15324           j += sqlite3Strlen30(&z[j]);
15325           break;
15326         }
15327         case 'S':  sqlite3_snprintf(3,&z[j],"%02d",(int)x.s); j+=2; break;
15328         case 'w': {
15329           z[j++] = (char)(((x.iJD+129600000)/86400000) % 7) + '0';
15330           break;
15331         }
15332         case 'Y': {
15333           sqlite3_snprintf(5,&z[j],"%04d",x.Y); j+=sqlite3Strlen30(&z[j]);
15334           break;
15335         }
15336         default:   z[j++] = '%'; break;
15337       }
15338     }
15339   }
15340   z[j] = 0;
15341   sqlite3_result_text(context, z, -1,
15342                       z==zBuf ? SQLITE_TRANSIENT : SQLITE_DYNAMIC);
15343 }
15344 
15345 /*
15346 ** current_time()
15347 **
15348 ** This function returns the same value as time('now').
15349 */
15350 static void ctimeFunc(
15351   sqlite3_context *context,
15352   int NotUsed,
15353   sqlite3_value **NotUsed2
15354 ){
15355   UNUSED_PARAMETER2(NotUsed, NotUsed2);
15356   timeFunc(context, 0, 0);
15357 }
15358 
15359 /*
15360 ** current_date()
15361 **
15362 ** This function returns the same value as date('now').
15363 */
15364 static void cdateFunc(
15365   sqlite3_context *context,
15366   int NotUsed,
15367   sqlite3_value **NotUsed2
15368 ){
15369   UNUSED_PARAMETER2(NotUsed, NotUsed2);
15370   dateFunc(context, 0, 0);
15371 }
15372 
15373 /*
15374 ** current_timestamp()
15375 **
15376 ** This function returns the same value as datetime('now').
15377 */
15378 static void ctimestampFunc(
15379   sqlite3_context *context,
15380   int NotUsed,
15381   sqlite3_value **NotUsed2
15382 ){
15383   UNUSED_PARAMETER2(NotUsed, NotUsed2);
15384   datetimeFunc(context, 0, 0);
15385 }
15386 #endif /* !defined(SQLITE_OMIT_DATETIME_FUNCS) */
15387 
15388 #ifdef SQLITE_OMIT_DATETIME_FUNCS
15389 /*
15390 ** If the library is compiled to omit the full-scale date and time
15391 ** handling (to get a smaller binary), the following minimal version
15392 ** of the functions current_time(), current_date() and current_timestamp()
15393 ** are included instead. This is to support column declarations that
15394 ** include "DEFAULT CURRENT_TIME" etc.
15395 **
15396 ** This function uses the C-library functions time(), gmtime()
15397 ** and strftime(). The format string to pass to strftime() is supplied
15398 ** as the user-data for the function.
15399 */
15400 static void currentTimeFunc(
15401   sqlite3_context *context,
15402   int argc,
15403   sqlite3_value **argv
15404 ){
15405   time_t t;
15406   char *zFormat = (char *)sqlite3_user_data(context);
15407   sqlite3 *db;
15408   sqlite3_int64 iT;
15409   struct tm *pTm;
15410   struct tm sNow;
15411   char zBuf[20];
15412 
15413   UNUSED_PARAMETER(argc);
15414   UNUSED_PARAMETER(argv);
15415 
15416   iT = sqlite3StmtCurrentTime(context);
15417   if( iT<=0 ) return;
15418   t = iT/1000 - 10000*(sqlite3_int64)21086676;
15419 #ifdef HAVE_GMTIME_R
15420   pTm = gmtime_r(&t, &sNow);
15421 #else
15422   sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
15423   pTm = gmtime(&t);
15424   if( pTm ) memcpy(&sNow, pTm, sizeof(sNow));
15425   sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
15426 #endif
15427   if( pTm ){
15428     strftime(zBuf, 20, zFormat, &sNow);
15429     sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
15430   }
15431 }
15432 #endif
15433 
15434 /*
15435 ** This function registered all of the above C functions as SQL
15436 ** functions.  This should be the only routine in this file with
15437 ** external linkage.
15438 */
15439 SQLITE_PRIVATE void sqlite3RegisterDateTimeFunctions(void){
15440   static SQLITE_WSD FuncDef aDateTimeFuncs[] = {
15441 #ifndef SQLITE_OMIT_DATETIME_FUNCS
15442     FUNCTION(julianday,        -1, 0, 0, juliandayFunc ),
15443     FUNCTION(date,             -1, 0, 0, dateFunc      ),
15444     FUNCTION(time,             -1, 0, 0, timeFunc      ),
15445     FUNCTION(datetime,         -1, 0, 0, datetimeFunc  ),
15446     FUNCTION(strftime,         -1, 0, 0, strftimeFunc  ),
15447     FUNCTION(current_time,      0, 0, 0, ctimeFunc     ),
15448     FUNCTION(current_timestamp, 0, 0, 0, ctimestampFunc),
15449     FUNCTION(current_date,      0, 0, 0, cdateFunc     ),
15450 #else
15451     STR_FUNCTION(current_time,      0, "%H:%M:%S",          0, currentTimeFunc),
15452     STR_FUNCTION(current_date,      0, "%Y-%m-%d",          0, currentTimeFunc),
15453     STR_FUNCTION(current_timestamp, 0, "%Y-%m-%d %H:%M:%S", 0, currentTimeFunc),
15454 #endif
15455   };
15456   int i;
15457   FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions);
15458   FuncDef *aFunc = (FuncDef*)&GLOBAL(FuncDef, aDateTimeFuncs);
15459 
15460   for(i=0; i<ArraySize(aDateTimeFuncs); i++){
15461     sqlite3FuncDefInsert(pHash, &aFunc[i]);
15462   }
15463 }
15464 
15465 /************** End of date.c ************************************************/
15466 /************** Begin file os.c **********************************************/
15467 /*
15468 ** 2005 November 29
15469 **
15470 ** The author disclaims copyright to this source code.  In place of
15471 ** a legal notice, here is a blessing:
15472 **
15473 **    May you do good and not evil.
15474 **    May you find forgiveness for yourself and forgive others.
15475 **    May you share freely, never taking more than you give.
15476 **
15477 ******************************************************************************
15478 **
15479 ** This file contains OS interface code that is common to all
15480 ** architectures.
15481 */
15482 #define _SQLITE_OS_C_ 1
15483 #undef _SQLITE_OS_C_
15484 
15485 /*
15486 ** The default SQLite sqlite3_vfs implementations do not allocate
15487 ** memory (actually, os_unix.c allocates a small amount of memory
15488 ** from within OsOpen()), but some third-party implementations may.
15489 ** So we test the effects of a malloc() failing and the sqlite3OsXXX()
15490 ** function returning SQLITE_IOERR_NOMEM using the DO_OS_MALLOC_TEST macro.
15491 **
15492 ** The following functions are instrumented for malloc() failure
15493 ** testing:
15494 **
15495 **     sqlite3OsRead()
15496 **     sqlite3OsWrite()
15497 **     sqlite3OsSync()
15498 **     sqlite3OsFileSize()
15499 **     sqlite3OsLock()
15500 **     sqlite3OsCheckReservedLock()
15501 **     sqlite3OsFileControl()
15502 **     sqlite3OsShmMap()
15503 **     sqlite3OsOpen()
15504 **     sqlite3OsDelete()
15505 **     sqlite3OsAccess()
15506 **     sqlite3OsFullPathname()
15507 **
15508 */
15509 #if defined(SQLITE_TEST)
15510 SQLITE_API int sqlite3_memdebug_vfs_oom_test = 1;
15511   #define DO_OS_MALLOC_TEST(x)                                       \
15512   if (sqlite3_memdebug_vfs_oom_test && (!x || !sqlite3IsMemJournal(x))) {  \
15513     void *pTstAlloc = sqlite3Malloc(10);                             \
15514     if (!pTstAlloc) return SQLITE_IOERR_NOMEM;                       \
15515     sqlite3_free(pTstAlloc);                                         \
15516   }
15517 #else
15518   #define DO_OS_MALLOC_TEST(x)
15519 #endif
15520 
15521 /*
15522 ** The following routines are convenience wrappers around methods
15523 ** of the sqlite3_file object.  This is mostly just syntactic sugar. All
15524 ** of this would be completely automatic if SQLite were coded using
15525 ** C++ instead of plain old C.
15526 */
15527 SQLITE_PRIVATE int sqlite3OsClose(sqlite3_file *pId){
15528   int rc = SQLITE_OK;
15529   if( pId->pMethods ){
15530     rc = pId->pMethods->xClose(pId);
15531     pId->pMethods = 0;
15532   }
15533   return rc;
15534 }
15535 SQLITE_PRIVATE int sqlite3OsRead(sqlite3_file *id, void *pBuf, int amt, i64 offset){
15536   DO_OS_MALLOC_TEST(id);
15537   return id->pMethods->xRead(id, pBuf, amt, offset);
15538 }
15539 SQLITE_PRIVATE int sqlite3OsWrite(sqlite3_file *id, const void *pBuf, int amt, i64 offset){
15540   DO_OS_MALLOC_TEST(id);
15541   return id->pMethods->xWrite(id, pBuf, amt, offset);
15542 }
15543 SQLITE_PRIVATE int sqlite3OsTruncate(sqlite3_file *id, i64 size){
15544   return id->pMethods->xTruncate(id, size);
15545 }
15546 SQLITE_PRIVATE int sqlite3OsSync(sqlite3_file *id, int flags){
15547   DO_OS_MALLOC_TEST(id);
15548   return id->pMethods->xSync(id, flags);
15549 }
15550 SQLITE_PRIVATE int sqlite3OsFileSize(sqlite3_file *id, i64 *pSize){
15551   DO_OS_MALLOC_TEST(id);
15552   return id->pMethods->xFileSize(id, pSize);
15553 }
15554 SQLITE_PRIVATE int sqlite3OsLock(sqlite3_file *id, int lockType){
15555   DO_OS_MALLOC_TEST(id);
15556   return id->pMethods->xLock(id, lockType);
15557 }
15558 SQLITE_PRIVATE int sqlite3OsUnlock(sqlite3_file *id, int lockType){
15559   return id->pMethods->xUnlock(id, lockType);
15560 }
15561 SQLITE_PRIVATE int sqlite3OsCheckReservedLock(sqlite3_file *id, int *pResOut){
15562   DO_OS_MALLOC_TEST(id);
15563   return id->pMethods->xCheckReservedLock(id, pResOut);
15564 }
15565 
15566 /*
15567 ** Use sqlite3OsFileControl() when we are doing something that might fail
15568 ** and we need to know about the failures.  Use sqlite3OsFileControlHint()
15569 ** when simply tossing information over the wall to the VFS and we do not
15570 ** really care if the VFS receives and understands the information since it
15571 ** is only a hint and can be safely ignored.  The sqlite3OsFileControlHint()
15572 ** routine has no return value since the return value would be meaningless.
15573 */
15574 SQLITE_PRIVATE int sqlite3OsFileControl(sqlite3_file *id, int op, void *pArg){
15575 #ifdef SQLITE_TEST
15576   if( op!=SQLITE_FCNTL_COMMIT_PHASETWO ){
15577     /* Faults are not injected into COMMIT_PHASETWO because, assuming SQLite
15578     ** is using a regular VFS, it is called after the corresponding
15579     ** transaction has been committed. Injecting a fault at this point
15580     ** confuses the test scripts - the COMMIT comand returns SQLITE_NOMEM
15581     ** but the transaction is committed anyway.
15582     **
15583     ** The core must call OsFileControl() though, not OsFileControlHint(),
15584     ** as if a custom VFS (e.g. zipvfs) returns an error here, it probably
15585     ** means the commit really has failed and an error should be returned
15586     ** to the user.  */
15587     DO_OS_MALLOC_TEST(id);
15588   }
15589 #endif
15590   return id->pMethods->xFileControl(id, op, pArg);
15591 }
15592 SQLITE_PRIVATE void sqlite3OsFileControlHint(sqlite3_file *id, int op, void *pArg){
15593   (void)id->pMethods->xFileControl(id, op, pArg);
15594 }
15595 
15596 SQLITE_PRIVATE int sqlite3OsSectorSize(sqlite3_file *id){
15597   int (*xSectorSize)(sqlite3_file*) = id->pMethods->xSectorSize;
15598   return (xSectorSize ? xSectorSize(id) : SQLITE_DEFAULT_SECTOR_SIZE);
15599 }
15600 SQLITE_PRIVATE int sqlite3OsDeviceCharacteristics(sqlite3_file *id){
15601   return id->pMethods->xDeviceCharacteristics(id);
15602 }
15603 SQLITE_PRIVATE int sqlite3OsShmLock(sqlite3_file *id, int offset, int n, int flags){
15604   return id->pMethods->xShmLock(id, offset, n, flags);
15605 }
15606 SQLITE_PRIVATE void sqlite3OsShmBarrier(sqlite3_file *id){
15607   id->pMethods->xShmBarrier(id);
15608 }
15609 SQLITE_PRIVATE int sqlite3OsShmUnmap(sqlite3_file *id, int deleteFlag){
15610   return id->pMethods->xShmUnmap(id, deleteFlag);
15611 }
15612 SQLITE_PRIVATE int sqlite3OsShmMap(
15613   sqlite3_file *id,               /* Database file handle */
15614   int iPage,
15615   int pgsz,
15616   int bExtend,                    /* True to extend file if necessary */
15617   void volatile **pp              /* OUT: Pointer to mapping */
15618 ){
15619   DO_OS_MALLOC_TEST(id);
15620   return id->pMethods->xShmMap(id, iPage, pgsz, bExtend, pp);
15621 }
15622 
15623 #if SQLITE_MAX_MMAP_SIZE>0
15624 /* The real implementation of xFetch and xUnfetch */
15625 SQLITE_PRIVATE int sqlite3OsFetch(sqlite3_file *id, i64 iOff, int iAmt, void **pp){
15626   DO_OS_MALLOC_TEST(id);
15627   return id->pMethods->xFetch(id, iOff, iAmt, pp);
15628 }
15629 SQLITE_PRIVATE int sqlite3OsUnfetch(sqlite3_file *id, i64 iOff, void *p){
15630   return id->pMethods->xUnfetch(id, iOff, p);
15631 }
15632 #else
15633 /* No-op stubs to use when memory-mapped I/O is disabled */
15634 SQLITE_PRIVATE int sqlite3OsFetch(sqlite3_file *id, i64 iOff, int iAmt, void **pp){
15635   *pp = 0;
15636   return SQLITE_OK;
15637 }
15638 SQLITE_PRIVATE int sqlite3OsUnfetch(sqlite3_file *id, i64 iOff, void *p){
15639   return SQLITE_OK;
15640 }
15641 #endif
15642 
15643 /*
15644 ** The next group of routines are convenience wrappers around the
15645 ** VFS methods.
15646 */
15647 SQLITE_PRIVATE int sqlite3OsOpen(
15648   sqlite3_vfs *pVfs,
15649   const char *zPath,
15650   sqlite3_file *pFile,
15651   int flags,
15652   int *pFlagsOut
15653 ){
15654   int rc;
15655   DO_OS_MALLOC_TEST(0);
15656   /* 0x87f7f is a mask of SQLITE_OPEN_ flags that are valid to be passed
15657   ** down into the VFS layer.  Some SQLITE_OPEN_ flags (for example,
15658   ** SQLITE_OPEN_FULLMUTEX or SQLITE_OPEN_SHAREDCACHE) are blocked before
15659   ** reaching the VFS. */
15660   rc = pVfs->xOpen(pVfs, zPath, pFile, flags & 0x87f7f, pFlagsOut);
15661   assert( rc==SQLITE_OK || pFile->pMethods==0 );
15662   return rc;
15663 }
15664 SQLITE_PRIVATE int sqlite3OsDelete(sqlite3_vfs *pVfs, const char *zPath, int dirSync){
15665   DO_OS_MALLOC_TEST(0);
15666   assert( dirSync==0 || dirSync==1 );
15667   return pVfs->xDelete(pVfs, zPath, dirSync);
15668 }
15669 SQLITE_PRIVATE int sqlite3OsAccess(
15670   sqlite3_vfs *pVfs,
15671   const char *zPath,
15672   int flags,
15673   int *pResOut
15674 ){
15675   DO_OS_MALLOC_TEST(0);
15676   return pVfs->xAccess(pVfs, zPath, flags, pResOut);
15677 }
15678 SQLITE_PRIVATE int sqlite3OsFullPathname(
15679   sqlite3_vfs *pVfs,
15680   const char *zPath,
15681   int nPathOut,
15682   char *zPathOut
15683 ){
15684   DO_OS_MALLOC_TEST(0);
15685   zPathOut[0] = 0;
15686   return pVfs->xFullPathname(pVfs, zPath, nPathOut, zPathOut);
15687 }
15688 #ifndef SQLITE_OMIT_LOAD_EXTENSION
15689 SQLITE_PRIVATE void *sqlite3OsDlOpen(sqlite3_vfs *pVfs, const char *zPath){
15690   return pVfs->xDlOpen(pVfs, zPath);
15691 }
15692 SQLITE_PRIVATE void sqlite3OsDlError(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
15693   pVfs->xDlError(pVfs, nByte, zBufOut);
15694 }
15695 SQLITE_PRIVATE void (*sqlite3OsDlSym(sqlite3_vfs *pVfs, void *pHdle, const char *zSym))(void){
15696   return pVfs->xDlSym(pVfs, pHdle, zSym);
15697 }
15698 SQLITE_PRIVATE void sqlite3OsDlClose(sqlite3_vfs *pVfs, void *pHandle){
15699   pVfs->xDlClose(pVfs, pHandle);
15700 }
15701 #endif /* SQLITE_OMIT_LOAD_EXTENSION */
15702 SQLITE_PRIVATE int sqlite3OsRandomness(sqlite3_vfs *pVfs, int nByte, char *zBufOut){
15703   return pVfs->xRandomness(pVfs, nByte, zBufOut);
15704 }
15705 SQLITE_PRIVATE int sqlite3OsSleep(sqlite3_vfs *pVfs, int nMicro){
15706   return pVfs->xSleep(pVfs, nMicro);
15707 }
15708 SQLITE_PRIVATE int sqlite3OsCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *pTimeOut){
15709   int rc;
15710   /* IMPLEMENTATION-OF: R-49045-42493 SQLite will use the xCurrentTimeInt64()
15711   ** method to get the current date and time if that method is available
15712   ** (if iVersion is 2 or greater and the function pointer is not NULL) and
15713   ** will fall back to xCurrentTime() if xCurrentTimeInt64() is
15714   ** unavailable.
15715   */
15716   if( pVfs->iVersion>=2 && pVfs->xCurrentTimeInt64 ){
15717     rc = pVfs->xCurrentTimeInt64(pVfs, pTimeOut);
15718   }else{
15719     double r;
15720     rc = pVfs->xCurrentTime(pVfs, &r);
15721     *pTimeOut = (sqlite3_int64)(r*86400000.0);
15722   }
15723   return rc;
15724 }
15725 
15726 SQLITE_PRIVATE int sqlite3OsOpenMalloc(
15727   sqlite3_vfs *pVfs,
15728   const char *zFile,
15729   sqlite3_file **ppFile,
15730   int flags,
15731   int *pOutFlags
15732 ){
15733   int rc = SQLITE_NOMEM;
15734   sqlite3_file *pFile;
15735   pFile = (sqlite3_file *)sqlite3MallocZero(pVfs->szOsFile);
15736   if( pFile ){
15737     rc = sqlite3OsOpen(pVfs, zFile, pFile, flags, pOutFlags);
15738     if( rc!=SQLITE_OK ){
15739       sqlite3_free(pFile);
15740     }else{
15741       *ppFile = pFile;
15742     }
15743   }
15744   return rc;
15745 }
15746 SQLITE_PRIVATE int sqlite3OsCloseFree(sqlite3_file *pFile){
15747   int rc = SQLITE_OK;
15748   assert( pFile );
15749   rc = sqlite3OsClose(pFile);
15750   sqlite3_free(pFile);
15751   return rc;
15752 }
15753 
15754 /*
15755 ** This function is a wrapper around the OS specific implementation of
15756 ** sqlite3_os_init(). The purpose of the wrapper is to provide the
15757 ** ability to simulate a malloc failure, so that the handling of an
15758 ** error in sqlite3_os_init() by the upper layers can be tested.
15759 */
15760 SQLITE_PRIVATE int sqlite3OsInit(void){
15761   void *p = sqlite3_malloc(10);
15762   if( p==0 ) return SQLITE_NOMEM;
15763   sqlite3_free(p);
15764   return sqlite3_os_init();
15765 }
15766 
15767 /*
15768 ** The list of all registered VFS implementations.
15769 */
15770 static sqlite3_vfs * SQLITE_WSD vfsList = 0;
15771 #define vfsList GLOBAL(sqlite3_vfs *, vfsList)
15772 
15773 /*
15774 ** Locate a VFS by name.  If no name is given, simply return the
15775 ** first VFS on the list.
15776 */
15777 SQLITE_API sqlite3_vfs *sqlite3_vfs_find(const char *zVfs){
15778   sqlite3_vfs *pVfs = 0;
15779 #if SQLITE_THREADSAFE
15780   sqlite3_mutex *mutex;
15781 #endif
15782 #ifndef SQLITE_OMIT_AUTOINIT
15783   int rc = sqlite3_initialize();
15784   if( rc ) return 0;
15785 #endif
15786 #if SQLITE_THREADSAFE
15787   mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
15788 #endif
15789   sqlite3_mutex_enter(mutex);
15790   for(pVfs = vfsList; pVfs; pVfs=pVfs->pNext){
15791     if( zVfs==0 ) break;
15792     if( strcmp(zVfs, pVfs->zName)==0 ) break;
15793   }
15794   sqlite3_mutex_leave(mutex);
15795   return pVfs;
15796 }
15797 
15798 /*
15799 ** Unlink a VFS from the linked list
15800 */
15801 static void vfsUnlink(sqlite3_vfs *pVfs){
15802   assert( sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)) );
15803   if( pVfs==0 ){
15804     /* No-op */
15805   }else if( vfsList==pVfs ){
15806     vfsList = pVfs->pNext;
15807   }else if( vfsList ){
15808     sqlite3_vfs *p = vfsList;
15809     while( p->pNext && p->pNext!=pVfs ){
15810       p = p->pNext;
15811     }
15812     if( p->pNext==pVfs ){
15813       p->pNext = pVfs->pNext;
15814     }
15815   }
15816 }
15817 
15818 /*
15819 ** Register a VFS with the system.  It is harmless to register the same
15820 ** VFS multiple times.  The new VFS becomes the default if makeDflt is
15821 ** true.
15822 */
15823 SQLITE_API int sqlite3_vfs_register(sqlite3_vfs *pVfs, int makeDflt){
15824   MUTEX_LOGIC(sqlite3_mutex *mutex;)
15825 #ifndef SQLITE_OMIT_AUTOINIT
15826   int rc = sqlite3_initialize();
15827   if( rc ) return rc;
15828 #endif
15829   MUTEX_LOGIC( mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); )
15830   sqlite3_mutex_enter(mutex);
15831   vfsUnlink(pVfs);
15832   if( makeDflt || vfsList==0 ){
15833     pVfs->pNext = vfsList;
15834     vfsList = pVfs;
15835   }else{
15836     pVfs->pNext = vfsList->pNext;
15837     vfsList->pNext = pVfs;
15838   }
15839   assert(vfsList);
15840   sqlite3_mutex_leave(mutex);
15841   return SQLITE_OK;
15842 }
15843 
15844 /*
15845 ** Unregister a VFS so that it is no longer accessible.
15846 */
15847 SQLITE_API int sqlite3_vfs_unregister(sqlite3_vfs *pVfs){
15848 #if SQLITE_THREADSAFE
15849   sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
15850 #endif
15851   sqlite3_mutex_enter(mutex);
15852   vfsUnlink(pVfs);
15853   sqlite3_mutex_leave(mutex);
15854   return SQLITE_OK;
15855 }
15856 
15857 /************** End of os.c **************************************************/
15858 /************** Begin file fault.c *******************************************/
15859 /*
15860 ** 2008 Jan 22
15861 **
15862 ** The author disclaims copyright to this source code.  In place of
15863 ** a legal notice, here is a blessing:
15864 **
15865 **    May you do good and not evil.
15866 **    May you find forgiveness for yourself and forgive others.
15867 **    May you share freely, never taking more than you give.
15868 **
15869 *************************************************************************
15870 **
15871 ** This file contains code to support the concept of "benign"
15872 ** malloc failures (when the xMalloc() or xRealloc() method of the
15873 ** sqlite3_mem_methods structure fails to allocate a block of memory
15874 ** and returns 0).
15875 **
15876 ** Most malloc failures are non-benign. After they occur, SQLite
15877 ** abandons the current operation and returns an error code (usually
15878 ** SQLITE_NOMEM) to the user. However, sometimes a fault is not necessarily
15879 ** fatal. For example, if a malloc fails while resizing a hash table, this
15880 ** is completely recoverable simply by not carrying out the resize. The
15881 ** hash table will continue to function normally.  So a malloc failure
15882 ** during a hash table resize is a benign fault.
15883 */
15884 
15885 
15886 #ifndef SQLITE_OMIT_BUILTIN_TEST
15887 
15888 /*
15889 ** Global variables.
15890 */
15891 typedef struct BenignMallocHooks BenignMallocHooks;
15892 static SQLITE_WSD struct BenignMallocHooks {
15893   void (*xBenignBegin)(void);
15894   void (*xBenignEnd)(void);
15895 } sqlite3Hooks = { 0, 0 };
15896 
15897 /* The "wsdHooks" macro will resolve to the appropriate BenignMallocHooks
15898 ** structure.  If writable static data is unsupported on the target,
15899 ** we have to locate the state vector at run-time.  In the more common
15900 ** case where writable static data is supported, wsdHooks can refer directly
15901 ** to the "sqlite3Hooks" state vector declared above.
15902 */
15903 #ifdef SQLITE_OMIT_WSD
15904 # define wsdHooksInit \
15905   BenignMallocHooks *x = &GLOBAL(BenignMallocHooks,sqlite3Hooks)
15906 # define wsdHooks x[0]
15907 #else
15908 # define wsdHooksInit
15909 # define wsdHooks sqlite3Hooks
15910 #endif
15911 
15912 
15913 /*
15914 ** Register hooks to call when sqlite3BeginBenignMalloc() and
15915 ** sqlite3EndBenignMalloc() are called, respectively.
15916 */
15917 SQLITE_PRIVATE void sqlite3BenignMallocHooks(
15918   void (*xBenignBegin)(void),
15919   void (*xBenignEnd)(void)
15920 ){
15921   wsdHooksInit;
15922   wsdHooks.xBenignBegin = xBenignBegin;
15923   wsdHooks.xBenignEnd = xBenignEnd;
15924 }
15925 
15926 /*
15927 ** This (sqlite3EndBenignMalloc()) is called by SQLite code to indicate that
15928 ** subsequent malloc failures are benign. A call to sqlite3EndBenignMalloc()
15929 ** indicates that subsequent malloc failures are non-benign.
15930 */
15931 SQLITE_PRIVATE void sqlite3BeginBenignMalloc(void){
15932   wsdHooksInit;
15933   if( wsdHooks.xBenignBegin ){
15934     wsdHooks.xBenignBegin();
15935   }
15936 }
15937 SQLITE_PRIVATE void sqlite3EndBenignMalloc(void){
15938   wsdHooksInit;
15939   if( wsdHooks.xBenignEnd ){
15940     wsdHooks.xBenignEnd();
15941   }
15942 }
15943 
15944 #endif   /* #ifndef SQLITE_OMIT_BUILTIN_TEST */
15945 
15946 /************** End of fault.c ***********************************************/
15947 /************** Begin file mem0.c ********************************************/
15948 /*
15949 ** 2008 October 28
15950 **
15951 ** The author disclaims copyright to this source code.  In place of
15952 ** a legal notice, here is a blessing:
15953 **
15954 **    May you do good and not evil.
15955 **    May you find forgiveness for yourself and forgive others.
15956 **    May you share freely, never taking more than you give.
15957 **
15958 *************************************************************************
15959 **
15960 ** This file contains a no-op memory allocation drivers for use when
15961 ** SQLITE_ZERO_MALLOC is defined.  The allocation drivers implemented
15962 ** here always fail.  SQLite will not operate with these drivers.  These
15963 ** are merely placeholders.  Real drivers must be substituted using
15964 ** sqlite3_config() before SQLite will operate.
15965 */
15966 
15967 /*
15968 ** This version of the memory allocator is the default.  It is
15969 ** used when no other memory allocator is specified using compile-time
15970 ** macros.
15971 */
15972 #ifdef SQLITE_ZERO_MALLOC
15973 
15974 /*
15975 ** No-op versions of all memory allocation routines
15976 */
15977 static void *sqlite3MemMalloc(int nByte){ return 0; }
15978 static void sqlite3MemFree(void *pPrior){ return; }
15979 static void *sqlite3MemRealloc(void *pPrior, int nByte){ return 0; }
15980 static int sqlite3MemSize(void *pPrior){ return 0; }
15981 static int sqlite3MemRoundup(int n){ return n; }
15982 static int sqlite3MemInit(void *NotUsed){ return SQLITE_OK; }
15983 static void sqlite3MemShutdown(void *NotUsed){ return; }
15984 
15985 /*
15986 ** This routine is the only routine in this file with external linkage.
15987 **
15988 ** Populate the low-level memory allocation function pointers in
15989 ** sqlite3GlobalConfig.m with pointers to the routines in this file.
15990 */
15991 SQLITE_PRIVATE void sqlite3MemSetDefault(void){
15992   static const sqlite3_mem_methods defaultMethods = {
15993      sqlite3MemMalloc,
15994      sqlite3MemFree,
15995      sqlite3MemRealloc,
15996      sqlite3MemSize,
15997      sqlite3MemRoundup,
15998      sqlite3MemInit,
15999      sqlite3MemShutdown,
16000      0
16001   };
16002   sqlite3_config(SQLITE_CONFIG_MALLOC, &defaultMethods);
16003 }
16004 
16005 #endif /* SQLITE_ZERO_MALLOC */
16006 
16007 /************** End of mem0.c ************************************************/
16008 /************** Begin file mem1.c ********************************************/
16009 /*
16010 ** 2007 August 14
16011 **
16012 ** The author disclaims copyright to this source code.  In place of
16013 ** a legal notice, here is a blessing:
16014 **
16015 **    May you do good and not evil.
16016 **    May you find forgiveness for yourself and forgive others.
16017 **    May you share freely, never taking more than you give.
16018 **
16019 *************************************************************************
16020 **
16021 ** This file contains low-level memory allocation drivers for when
16022 ** SQLite will use the standard C-library malloc/realloc/free interface
16023 ** to obtain the memory it needs.
16024 **
16025 ** This file contains implementations of the low-level memory allocation
16026 ** routines specified in the sqlite3_mem_methods object.  The content of
16027 ** this file is only used if SQLITE_SYSTEM_MALLOC is defined.  The
16028 ** SQLITE_SYSTEM_MALLOC macro is defined automatically if neither the
16029 ** SQLITE_MEMDEBUG nor the SQLITE_WIN32_MALLOC macros are defined.  The
16030 ** default configuration is to use memory allocation routines in this
16031 ** file.
16032 **
16033 ** C-preprocessor macro summary:
16034 **
16035 **    HAVE_MALLOC_USABLE_SIZE     The configure script sets this symbol if
16036 **                                the malloc_usable_size() interface exists
16037 **                                on the target platform.  Or, this symbol
16038 **                                can be set manually, if desired.
16039 **                                If an equivalent interface exists by
16040 **                                a different name, using a separate -D
16041 **                                option to rename it.
16042 **
16043 **    SQLITE_WITHOUT_ZONEMALLOC   Some older macs lack support for the zone
16044 **                                memory allocator.  Set this symbol to enable
16045 **                                building on older macs.
16046 **
16047 **    SQLITE_WITHOUT_MSIZE        Set this symbol to disable the use of
16048 **                                _msize() on windows systems.  This might
16049 **                                be necessary when compiling for Delphi,
16050 **                                for example.
16051 */
16052 
16053 /*
16054 ** This version of the memory allocator is the default.  It is
16055 ** used when no other memory allocator is specified using compile-time
16056 ** macros.
16057 */
16058 #ifdef SQLITE_SYSTEM_MALLOC
16059 #if defined(__APPLE__) && !defined(SQLITE_WITHOUT_ZONEMALLOC)
16060 
16061 /*
16062 ** Use the zone allocator available on apple products unless the
16063 ** SQLITE_WITHOUT_ZONEMALLOC symbol is defined.
16064 */
16065 #include <sys/sysctl.h>
16066 #include <malloc/malloc.h>
16067 #include <libkern/OSAtomic.h>
16068 static malloc_zone_t* _sqliteZone_;
16069 #define SQLITE_MALLOC(x) malloc_zone_malloc(_sqliteZone_, (x))
16070 #define SQLITE_FREE(x) malloc_zone_free(_sqliteZone_, (x));
16071 #define SQLITE_REALLOC(x,y) malloc_zone_realloc(_sqliteZone_, (x), (y))
16072 #define SQLITE_MALLOCSIZE(x) \
16073         (_sqliteZone_ ? _sqliteZone_->size(_sqliteZone_,x) : malloc_size(x))
16074 
16075 #else /* if not __APPLE__ */
16076 
16077 /*
16078 ** Use standard C library malloc and free on non-Apple systems.
16079 ** Also used by Apple systems if SQLITE_WITHOUT_ZONEMALLOC is defined.
16080 */
16081 #define SQLITE_MALLOC(x)             malloc(x)
16082 #define SQLITE_FREE(x)               free(x)
16083 #define SQLITE_REALLOC(x,y)          realloc((x),(y))
16084 
16085 /*
16086 ** The malloc.h header file is needed for malloc_usable_size() function
16087 ** on some systems (e.g. Linux).
16088 */
16089 #if defined(HAVE_MALLOC_H) && defined(HAVE_MALLOC_USABLE_SIZE)
16090 #  define SQLITE_USE_MALLOC_H
16091 #  define SQLITE_USE_MALLOC_USABLE_SIZE
16092 /*
16093 ** The MSVCRT has malloc_usable_size(), but it is called _msize().  The
16094 ** use of _msize() is automatic, but can be disabled by compiling with
16095 ** -DSQLITE_WITHOUT_MSIZE.  Using the _msize() function also requires
16096 ** the malloc.h header file.
16097 */
16098 #elif defined(_MSC_VER) && !defined(SQLITE_WITHOUT_MSIZE)
16099 #  define SQLITE_USE_MALLOC_H
16100 #  define SQLITE_USE_MSIZE
16101 #endif
16102 
16103 /*
16104 ** Include the malloc.h header file, if necessary.  Also set define macro
16105 ** SQLITE_MALLOCSIZE to the appropriate function name, which is _msize()
16106 ** for MSVC and malloc_usable_size() for most other systems (e.g. Linux).
16107 ** The memory size function can always be overridden manually by defining
16108 ** the macro SQLITE_MALLOCSIZE to the desired function name.
16109 */
16110 #if defined(SQLITE_USE_MALLOC_H)
16111 #  include <malloc.h>
16112 #  if defined(SQLITE_USE_MALLOC_USABLE_SIZE)
16113 #    if !defined(SQLITE_MALLOCSIZE)
16114 #      define SQLITE_MALLOCSIZE(x)   malloc_usable_size(x)
16115 #    endif
16116 #  elif defined(SQLITE_USE_MSIZE)
16117 #    if !defined(SQLITE_MALLOCSIZE)
16118 #      define SQLITE_MALLOCSIZE      _msize
16119 #    endif
16120 #  endif
16121 #endif /* defined(SQLITE_USE_MALLOC_H) */
16122 
16123 #endif /* __APPLE__ or not __APPLE__ */
16124 
16125 /*
16126 ** Like malloc(), but remember the size of the allocation
16127 ** so that we can find it later using sqlite3MemSize().
16128 **
16129 ** For this low-level routine, we are guaranteed that nByte>0 because
16130 ** cases of nByte<=0 will be intercepted and dealt with by higher level
16131 ** routines.
16132 */
16133 static void *sqlite3MemMalloc(int nByte){
16134 #ifdef SQLITE_MALLOCSIZE
16135   void *p = SQLITE_MALLOC( nByte );
16136   if( p==0 ){
16137     testcase( sqlite3GlobalConfig.xLog!=0 );
16138     sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes of memory", nByte);
16139   }
16140   return p;
16141 #else
16142   sqlite3_int64 *p;
16143   assert( nByte>0 );
16144   nByte = ROUND8(nByte);
16145   p = SQLITE_MALLOC( nByte+8 );
16146   if( p ){
16147     p[0] = nByte;
16148     p++;
16149   }else{
16150     testcase( sqlite3GlobalConfig.xLog!=0 );
16151     sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes of memory", nByte);
16152   }
16153   return (void *)p;
16154 #endif
16155 }
16156 
16157 /*
16158 ** Like free() but works for allocations obtained from sqlite3MemMalloc()
16159 ** or sqlite3MemRealloc().
16160 **
16161 ** For this low-level routine, we already know that pPrior!=0 since
16162 ** cases where pPrior==0 will have been intecepted and dealt with
16163 ** by higher-level routines.
16164 */
16165 static void sqlite3MemFree(void *pPrior){
16166 #ifdef SQLITE_MALLOCSIZE
16167   SQLITE_FREE(pPrior);
16168 #else
16169   sqlite3_int64 *p = (sqlite3_int64*)pPrior;
16170   assert( pPrior!=0 );
16171   p--;
16172   SQLITE_FREE(p);
16173 #endif
16174 }
16175 
16176 /*
16177 ** Report the allocated size of a prior return from xMalloc()
16178 ** or xRealloc().
16179 */
16180 static int sqlite3MemSize(void *pPrior){
16181 #ifdef SQLITE_MALLOCSIZE
16182   return pPrior ? (int)SQLITE_MALLOCSIZE(pPrior) : 0;
16183 #else
16184   sqlite3_int64 *p;
16185   if( pPrior==0 ) return 0;
16186   p = (sqlite3_int64*)pPrior;
16187   p--;
16188   return (int)p[0];
16189 #endif
16190 }
16191 
16192 /*
16193 ** Like realloc().  Resize an allocation previously obtained from
16194 ** sqlite3MemMalloc().
16195 **
16196 ** For this low-level interface, we know that pPrior!=0.  Cases where
16197 ** pPrior==0 while have been intercepted by higher-level routine and
16198 ** redirected to xMalloc.  Similarly, we know that nByte>0 becauses
16199 ** cases where nByte<=0 will have been intercepted by higher-level
16200 ** routines and redirected to xFree.
16201 */
16202 static void *sqlite3MemRealloc(void *pPrior, int nByte){
16203 #ifdef SQLITE_MALLOCSIZE
16204   void *p = SQLITE_REALLOC(pPrior, nByte);
16205   if( p==0 ){
16206     testcase( sqlite3GlobalConfig.xLog!=0 );
16207     sqlite3_log(SQLITE_NOMEM,
16208       "failed memory resize %u to %u bytes",
16209       SQLITE_MALLOCSIZE(pPrior), nByte);
16210   }
16211   return p;
16212 #else
16213   sqlite3_int64 *p = (sqlite3_int64*)pPrior;
16214   assert( pPrior!=0 && nByte>0 );
16215   assert( nByte==ROUND8(nByte) ); /* EV: R-46199-30249 */
16216   p--;
16217   p = SQLITE_REALLOC(p, nByte+8 );
16218   if( p ){
16219     p[0] = nByte;
16220     p++;
16221   }else{
16222     testcase( sqlite3GlobalConfig.xLog!=0 );
16223     sqlite3_log(SQLITE_NOMEM,
16224       "failed memory resize %u to %u bytes",
16225       sqlite3MemSize(pPrior), nByte);
16226   }
16227   return (void*)p;
16228 #endif
16229 }
16230 
16231 /*
16232 ** Round up a request size to the next valid allocation size.
16233 */
16234 static int sqlite3MemRoundup(int n){
16235   return ROUND8(n);
16236 }
16237 
16238 /*
16239 ** Initialize this module.
16240 */
16241 static int sqlite3MemInit(void *NotUsed){
16242 #if defined(__APPLE__) && !defined(SQLITE_WITHOUT_ZONEMALLOC)
16243   int cpuCount;
16244   size_t len;
16245   if( _sqliteZone_ ){
16246     return SQLITE_OK;
16247   }
16248   len = sizeof(cpuCount);
16249   /* One usually wants to use hw.acctivecpu for MT decisions, but not here */
16250   sysctlbyname("hw.ncpu", &cpuCount, &len, NULL, 0);
16251   if( cpuCount>1 ){
16252     /* defer MT decisions to system malloc */
16253     _sqliteZone_ = malloc_default_zone();
16254   }else{
16255     /* only 1 core, use our own zone to contention over global locks,
16256     ** e.g. we have our own dedicated locks */
16257     bool success;
16258     malloc_zone_t* newzone = malloc_create_zone(4096, 0);
16259     malloc_set_zone_name(newzone, "Sqlite_Heap");
16260     do{
16261       success = OSAtomicCompareAndSwapPtrBarrier(NULL, newzone,
16262                                  (void * volatile *)&_sqliteZone_);
16263     }while(!_sqliteZone_);
16264     if( !success ){
16265       /* somebody registered a zone first */
16266       malloc_destroy_zone(newzone);
16267     }
16268   }
16269 #endif
16270   UNUSED_PARAMETER(NotUsed);
16271   return SQLITE_OK;
16272 }
16273 
16274 /*
16275 ** Deinitialize this module.
16276 */
16277 static void sqlite3MemShutdown(void *NotUsed){
16278   UNUSED_PARAMETER(NotUsed);
16279   return;
16280 }
16281 
16282 /*
16283 ** This routine is the only routine in this file with external linkage.
16284 **
16285 ** Populate the low-level memory allocation function pointers in
16286 ** sqlite3GlobalConfig.m with pointers to the routines in this file.
16287 */
16288 SQLITE_PRIVATE void sqlite3MemSetDefault(void){
16289   static const sqlite3_mem_methods defaultMethods = {
16290      sqlite3MemMalloc,
16291      sqlite3MemFree,
16292      sqlite3MemRealloc,
16293      sqlite3MemSize,
16294      sqlite3MemRoundup,
16295      sqlite3MemInit,
16296      sqlite3MemShutdown,
16297      0
16298   };
16299   sqlite3_config(SQLITE_CONFIG_MALLOC, &defaultMethods);
16300 }
16301 
16302 #endif /* SQLITE_SYSTEM_MALLOC */
16303 
16304 /************** End of mem1.c ************************************************/
16305 /************** Begin file mem2.c ********************************************/
16306 /*
16307 ** 2007 August 15
16308 **
16309 ** The author disclaims copyright to this source code.  In place of
16310 ** a legal notice, here is a blessing:
16311 **
16312 **    May you do good and not evil.
16313 **    May you find forgiveness for yourself and forgive others.
16314 **    May you share freely, never taking more than you give.
16315 **
16316 *************************************************************************
16317 **
16318 ** This file contains low-level memory allocation drivers for when
16319 ** SQLite will use the standard C-library malloc/realloc/free interface
16320 ** to obtain the memory it needs while adding lots of additional debugging
16321 ** information to each allocation in order to help detect and fix memory
16322 ** leaks and memory usage errors.
16323 **
16324 ** This file contains implementations of the low-level memory allocation
16325 ** routines specified in the sqlite3_mem_methods object.
16326 */
16327 
16328 /*
16329 ** This version of the memory allocator is used only if the
16330 ** SQLITE_MEMDEBUG macro is defined
16331 */
16332 #ifdef SQLITE_MEMDEBUG
16333 
16334 /*
16335 ** The backtrace functionality is only available with GLIBC
16336 */
16337 #ifdef __GLIBC__
16338   extern int backtrace(void**,int);
16339   extern void backtrace_symbols_fd(void*const*,int,int);
16340 #else
16341 # define backtrace(A,B) 1
16342 # define backtrace_symbols_fd(A,B,C)
16343 #endif
16344 /* #include <stdio.h> */
16345 
16346 /*
16347 ** Each memory allocation looks like this:
16348 **
16349 **  ------------------------------------------------------------------------
16350 **  | Title |  backtrace pointers |  MemBlockHdr |  allocation |  EndGuard |
16351 **  ------------------------------------------------------------------------
16352 **
16353 ** The application code sees only a pointer to the allocation.  We have
16354 ** to back up from the allocation pointer to find the MemBlockHdr.  The
16355 ** MemBlockHdr tells us the size of the allocation and the number of
16356 ** backtrace pointers.  There is also a guard word at the end of the
16357 ** MemBlockHdr.
16358 */
16359 struct MemBlockHdr {
16360   i64 iSize;                          /* Size of this allocation */
16361   struct MemBlockHdr *pNext, *pPrev;  /* Linked list of all unfreed memory */
16362   char nBacktrace;                    /* Number of backtraces on this alloc */
16363   char nBacktraceSlots;               /* Available backtrace slots */
16364   u8 nTitle;                          /* Bytes of title; includes '\0' */
16365   u8 eType;                           /* Allocation type code */
16366   int iForeGuard;                     /* Guard word for sanity */
16367 };
16368 
16369 /*
16370 ** Guard words
16371 */
16372 #define FOREGUARD 0x80F5E153
16373 #define REARGUARD 0xE4676B53
16374 
16375 /*
16376 ** Number of malloc size increments to track.
16377 */
16378 #define NCSIZE  1000
16379 
16380 /*
16381 ** All of the static variables used by this module are collected
16382 ** into a single structure named "mem".  This is to keep the
16383 ** static variables organized and to reduce namespace pollution
16384 ** when this module is combined with other in the amalgamation.
16385 */
16386 static struct {
16387 
16388   /*
16389   ** Mutex to control access to the memory allocation subsystem.
16390   */
16391   sqlite3_mutex *mutex;
16392 
16393   /*
16394   ** Head and tail of a linked list of all outstanding allocations
16395   */
16396   struct MemBlockHdr *pFirst;
16397   struct MemBlockHdr *pLast;
16398 
16399   /*
16400   ** The number of levels of backtrace to save in new allocations.
16401   */
16402   int nBacktrace;
16403   void (*xBacktrace)(int, int, void **);
16404 
16405   /*
16406   ** Title text to insert in front of each block
16407   */
16408   int nTitle;        /* Bytes of zTitle to save.  Includes '\0' and padding */
16409   char zTitle[100];  /* The title text */
16410 
16411   /*
16412   ** sqlite3MallocDisallow() increments the following counter.
16413   ** sqlite3MallocAllow() decrements it.
16414   */
16415   int disallow; /* Do not allow memory allocation */
16416 
16417   /*
16418   ** Gather statistics on the sizes of memory allocations.
16419   ** nAlloc[i] is the number of allocation attempts of i*8
16420   ** bytes.  i==NCSIZE is the number of allocation attempts for
16421   ** sizes more than NCSIZE*8 bytes.
16422   */
16423   int nAlloc[NCSIZE];      /* Total number of allocations */
16424   int nCurrent[NCSIZE];    /* Current number of allocations */
16425   int mxCurrent[NCSIZE];   /* Highwater mark for nCurrent */
16426 
16427 } mem;
16428 
16429 
16430 /*
16431 ** Adjust memory usage statistics
16432 */
16433 static void adjustStats(int iSize, int increment){
16434   int i = ROUND8(iSize)/8;
16435   if( i>NCSIZE-1 ){
16436     i = NCSIZE - 1;
16437   }
16438   if( increment>0 ){
16439     mem.nAlloc[i]++;
16440     mem.nCurrent[i]++;
16441     if( mem.nCurrent[i]>mem.mxCurrent[i] ){
16442       mem.mxCurrent[i] = mem.nCurrent[i];
16443     }
16444   }else{
16445     mem.nCurrent[i]--;
16446     assert( mem.nCurrent[i]>=0 );
16447   }
16448 }
16449 
16450 /*
16451 ** Given an allocation, find the MemBlockHdr for that allocation.
16452 **
16453 ** This routine checks the guards at either end of the allocation and
16454 ** if they are incorrect it asserts.
16455 */
16456 static struct MemBlockHdr *sqlite3MemsysGetHeader(void *pAllocation){
16457   struct MemBlockHdr *p;
16458   int *pInt;
16459   u8 *pU8;
16460   int nReserve;
16461 
16462   p = (struct MemBlockHdr*)pAllocation;
16463   p--;
16464   assert( p->iForeGuard==(int)FOREGUARD );
16465   nReserve = ROUND8(p->iSize);
16466   pInt = (int*)pAllocation;
16467   pU8 = (u8*)pAllocation;
16468   assert( pInt[nReserve/sizeof(int)]==(int)REARGUARD );
16469   /* This checks any of the "extra" bytes allocated due
16470   ** to rounding up to an 8 byte boundary to ensure
16471   ** they haven't been overwritten.
16472   */
16473   while( nReserve-- > p->iSize ) assert( pU8[nReserve]==0x65 );
16474   return p;
16475 }
16476 
16477 /*
16478 ** Return the number of bytes currently allocated at address p.
16479 */
16480 static int sqlite3MemSize(void *p){
16481   struct MemBlockHdr *pHdr;
16482   if( !p ){
16483     return 0;
16484   }
16485   pHdr = sqlite3MemsysGetHeader(p);
16486   return (int)pHdr->iSize;
16487 }
16488 
16489 /*
16490 ** Initialize the memory allocation subsystem.
16491 */
16492 static int sqlite3MemInit(void *NotUsed){
16493   UNUSED_PARAMETER(NotUsed);
16494   assert( (sizeof(struct MemBlockHdr)&7) == 0 );
16495   if( !sqlite3GlobalConfig.bMemstat ){
16496     /* If memory status is enabled, then the malloc.c wrapper will already
16497     ** hold the STATIC_MEM mutex when the routines here are invoked. */
16498     mem.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
16499   }
16500   return SQLITE_OK;
16501 }
16502 
16503 /*
16504 ** Deinitialize the memory allocation subsystem.
16505 */
16506 static void sqlite3MemShutdown(void *NotUsed){
16507   UNUSED_PARAMETER(NotUsed);
16508   mem.mutex = 0;
16509 }
16510 
16511 /*
16512 ** Round up a request size to the next valid allocation size.
16513 */
16514 static int sqlite3MemRoundup(int n){
16515   return ROUND8(n);
16516 }
16517 
16518 /*
16519 ** Fill a buffer with pseudo-random bytes.  This is used to preset
16520 ** the content of a new memory allocation to unpredictable values and
16521 ** to clear the content of a freed allocation to unpredictable values.
16522 */
16523 static void randomFill(char *pBuf, int nByte){
16524   unsigned int x, y, r;
16525   x = SQLITE_PTR_TO_INT(pBuf);
16526   y = nByte | 1;
16527   while( nByte >= 4 ){
16528     x = (x>>1) ^ (-(int)(x&1) & 0xd0000001);
16529     y = y*1103515245 + 12345;
16530     r = x ^ y;
16531     *(int*)pBuf = r;
16532     pBuf += 4;
16533     nByte -= 4;
16534   }
16535   while( nByte-- > 0 ){
16536     x = (x>>1) ^ (-(int)(x&1) & 0xd0000001);
16537     y = y*1103515245 + 12345;
16538     r = x ^ y;
16539     *(pBuf++) = r & 0xff;
16540   }
16541 }
16542 
16543 /*
16544 ** Allocate nByte bytes of memory.
16545 */
16546 static void *sqlite3MemMalloc(int nByte){
16547   struct MemBlockHdr *pHdr;
16548   void **pBt;
16549   char *z;
16550   int *pInt;
16551   void *p = 0;
16552   int totalSize;
16553   int nReserve;
16554   sqlite3_mutex_enter(mem.mutex);
16555   assert( mem.disallow==0 );
16556   nReserve = ROUND8(nByte);
16557   totalSize = nReserve + sizeof(*pHdr) + sizeof(int) +
16558                mem.nBacktrace*sizeof(void*) + mem.nTitle;
16559   p = malloc(totalSize);
16560   if( p ){
16561     z = p;
16562     pBt = (void**)&z[mem.nTitle];
16563     pHdr = (struct MemBlockHdr*)&pBt[mem.nBacktrace];
16564     pHdr->pNext = 0;
16565     pHdr->pPrev = mem.pLast;
16566     if( mem.pLast ){
16567       mem.pLast->pNext = pHdr;
16568     }else{
16569       mem.pFirst = pHdr;
16570     }
16571     mem.pLast = pHdr;
16572     pHdr->iForeGuard = FOREGUARD;
16573     pHdr->eType = MEMTYPE_HEAP;
16574     pHdr->nBacktraceSlots = mem.nBacktrace;
16575     pHdr->nTitle = mem.nTitle;
16576     if( mem.nBacktrace ){
16577       void *aAddr[40];
16578       pHdr->nBacktrace = backtrace(aAddr, mem.nBacktrace+1)-1;
16579       memcpy(pBt, &aAddr[1], pHdr->nBacktrace*sizeof(void*));
16580       assert(pBt[0]);
16581       if( mem.xBacktrace ){
16582         mem.xBacktrace(nByte, pHdr->nBacktrace-1, &aAddr[1]);
16583       }
16584     }else{
16585       pHdr->nBacktrace = 0;
16586     }
16587     if( mem.nTitle ){
16588       memcpy(z, mem.zTitle, mem.nTitle);
16589     }
16590     pHdr->iSize = nByte;
16591     adjustStats(nByte, +1);
16592     pInt = (int*)&pHdr[1];
16593     pInt[nReserve/sizeof(int)] = REARGUARD;
16594     randomFill((char*)pInt, nByte);
16595     memset(((char*)pInt)+nByte, 0x65, nReserve-nByte);
16596     p = (void*)pInt;
16597   }
16598   sqlite3_mutex_leave(mem.mutex);
16599   return p;
16600 }
16601 
16602 /*
16603 ** Free memory.
16604 */
16605 static void sqlite3MemFree(void *pPrior){
16606   struct MemBlockHdr *pHdr;
16607   void **pBt;
16608   char *z;
16609   assert( sqlite3GlobalConfig.bMemstat || sqlite3GlobalConfig.bCoreMutex==0
16610        || mem.mutex!=0 );
16611   pHdr = sqlite3MemsysGetHeader(pPrior);
16612   pBt = (void**)pHdr;
16613   pBt -= pHdr->nBacktraceSlots;
16614   sqlite3_mutex_enter(mem.mutex);
16615   if( pHdr->pPrev ){
16616     assert( pHdr->pPrev->pNext==pHdr );
16617     pHdr->pPrev->pNext = pHdr->pNext;
16618   }else{
16619     assert( mem.pFirst==pHdr );
16620     mem.pFirst = pHdr->pNext;
16621   }
16622   if( pHdr->pNext ){
16623     assert( pHdr->pNext->pPrev==pHdr );
16624     pHdr->pNext->pPrev = pHdr->pPrev;
16625   }else{
16626     assert( mem.pLast==pHdr );
16627     mem.pLast = pHdr->pPrev;
16628   }
16629   z = (char*)pBt;
16630   z -= pHdr->nTitle;
16631   adjustStats((int)pHdr->iSize, -1);
16632   randomFill(z, sizeof(void*)*pHdr->nBacktraceSlots + sizeof(*pHdr) +
16633                 (int)pHdr->iSize + sizeof(int) + pHdr->nTitle);
16634   free(z);
16635   sqlite3_mutex_leave(mem.mutex);
16636 }
16637 
16638 /*
16639 ** Change the size of an existing memory allocation.
16640 **
16641 ** For this debugging implementation, we *always* make a copy of the
16642 ** allocation into a new place in memory.  In this way, if the
16643 ** higher level code is using pointer to the old allocation, it is
16644 ** much more likely to break and we are much more liking to find
16645 ** the error.
16646 */
16647 static void *sqlite3MemRealloc(void *pPrior, int nByte){
16648   struct MemBlockHdr *pOldHdr;
16649   void *pNew;
16650   assert( mem.disallow==0 );
16651   assert( (nByte & 7)==0 );     /* EV: R-46199-30249 */
16652   pOldHdr = sqlite3MemsysGetHeader(pPrior);
16653   pNew = sqlite3MemMalloc(nByte);
16654   if( pNew ){
16655     memcpy(pNew, pPrior, (int)(nByte<pOldHdr->iSize ? nByte : pOldHdr->iSize));
16656     if( nByte>pOldHdr->iSize ){
16657       randomFill(&((char*)pNew)[pOldHdr->iSize], nByte - (int)pOldHdr->iSize);
16658     }
16659     sqlite3MemFree(pPrior);
16660   }
16661   return pNew;
16662 }
16663 
16664 /*
16665 ** Populate the low-level memory allocation function pointers in
16666 ** sqlite3GlobalConfig.m with pointers to the routines in this file.
16667 */
16668 SQLITE_PRIVATE void sqlite3MemSetDefault(void){
16669   static const sqlite3_mem_methods defaultMethods = {
16670      sqlite3MemMalloc,
16671      sqlite3MemFree,
16672      sqlite3MemRealloc,
16673      sqlite3MemSize,
16674      sqlite3MemRoundup,
16675      sqlite3MemInit,
16676      sqlite3MemShutdown,
16677      0
16678   };
16679   sqlite3_config(SQLITE_CONFIG_MALLOC, &defaultMethods);
16680 }
16681 
16682 /*
16683 ** Set the "type" of an allocation.
16684 */
16685 SQLITE_PRIVATE void sqlite3MemdebugSetType(void *p, u8 eType){
16686   if( p && sqlite3GlobalConfig.m.xMalloc==sqlite3MemMalloc ){
16687     struct MemBlockHdr *pHdr;
16688     pHdr = sqlite3MemsysGetHeader(p);
16689     assert( pHdr->iForeGuard==FOREGUARD );
16690     pHdr->eType = eType;
16691   }
16692 }
16693 
16694 /*
16695 ** Return TRUE if the mask of type in eType matches the type of the
16696 ** allocation p.  Also return true if p==NULL.
16697 **
16698 ** This routine is designed for use within an assert() statement, to
16699 ** verify the type of an allocation.  For example:
16700 **
16701 **     assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) );
16702 */
16703 SQLITE_PRIVATE int sqlite3MemdebugHasType(void *p, u8 eType){
16704   int rc = 1;
16705   if( p && sqlite3GlobalConfig.m.xMalloc==sqlite3MemMalloc ){
16706     struct MemBlockHdr *pHdr;
16707     pHdr = sqlite3MemsysGetHeader(p);
16708     assert( pHdr->iForeGuard==FOREGUARD );         /* Allocation is valid */
16709     if( (pHdr->eType&eType)==0 ){
16710       rc = 0;
16711     }
16712   }
16713   return rc;
16714 }
16715 
16716 /*
16717 ** Return TRUE if the mask of type in eType matches no bits of the type of the
16718 ** allocation p.  Also return true if p==NULL.
16719 **
16720 ** This routine is designed for use within an assert() statement, to
16721 ** verify the type of an allocation.  For example:
16722 **
16723 **     assert( sqlite3MemdebugNoType(p, MEMTYPE_DB) );
16724 */
16725 SQLITE_PRIVATE int sqlite3MemdebugNoType(void *p, u8 eType){
16726   int rc = 1;
16727   if( p && sqlite3GlobalConfig.m.xMalloc==sqlite3MemMalloc ){
16728     struct MemBlockHdr *pHdr;
16729     pHdr = sqlite3MemsysGetHeader(p);
16730     assert( pHdr->iForeGuard==FOREGUARD );         /* Allocation is valid */
16731     if( (pHdr->eType&eType)!=0 ){
16732       rc = 0;
16733     }
16734   }
16735   return rc;
16736 }
16737 
16738 /*
16739 ** Set the number of backtrace levels kept for each allocation.
16740 ** A value of zero turns off backtracing.  The number is always rounded
16741 ** up to a multiple of 2.
16742 */
16743 SQLITE_PRIVATE void sqlite3MemdebugBacktrace(int depth){
16744   if( depth<0 ){ depth = 0; }
16745   if( depth>20 ){ depth = 20; }
16746   depth = (depth+1)&0xfe;
16747   mem.nBacktrace = depth;
16748 }
16749 
16750 SQLITE_PRIVATE void sqlite3MemdebugBacktraceCallback(void (*xBacktrace)(int, int, void **)){
16751   mem.xBacktrace = xBacktrace;
16752 }
16753 
16754 /*
16755 ** Set the title string for subsequent allocations.
16756 */
16757 SQLITE_PRIVATE void sqlite3MemdebugSettitle(const char *zTitle){
16758   unsigned int n = sqlite3Strlen30(zTitle) + 1;
16759   sqlite3_mutex_enter(mem.mutex);
16760   if( n>=sizeof(mem.zTitle) ) n = sizeof(mem.zTitle)-1;
16761   memcpy(mem.zTitle, zTitle, n);
16762   mem.zTitle[n] = 0;
16763   mem.nTitle = ROUND8(n);
16764   sqlite3_mutex_leave(mem.mutex);
16765 }
16766 
16767 SQLITE_PRIVATE void sqlite3MemdebugSync(){
16768   struct MemBlockHdr *pHdr;
16769   for(pHdr=mem.pFirst; pHdr; pHdr=pHdr->pNext){
16770     void **pBt = (void**)pHdr;
16771     pBt -= pHdr->nBacktraceSlots;
16772     mem.xBacktrace((int)pHdr->iSize, pHdr->nBacktrace-1, &pBt[1]);
16773   }
16774 }
16775 
16776 /*
16777 ** Open the file indicated and write a log of all unfreed memory
16778 ** allocations into that log.
16779 */
16780 SQLITE_PRIVATE void sqlite3MemdebugDump(const char *zFilename){
16781   FILE *out;
16782   struct MemBlockHdr *pHdr;
16783   void **pBt;
16784   int i;
16785   out = fopen(zFilename, "w");
16786   if( out==0 ){
16787     fprintf(stderr, "** Unable to output memory debug output log: %s **\n",
16788                     zFilename);
16789     return;
16790   }
16791   for(pHdr=mem.pFirst; pHdr; pHdr=pHdr->pNext){
16792     char *z = (char*)pHdr;
16793     z -= pHdr->nBacktraceSlots*sizeof(void*) + pHdr->nTitle;
16794     fprintf(out, "**** %lld bytes at %p from %s ****\n",
16795             pHdr->iSize, &pHdr[1], pHdr->nTitle ? z : "???");
16796     if( pHdr->nBacktrace ){
16797       fflush(out);
16798       pBt = (void**)pHdr;
16799       pBt -= pHdr->nBacktraceSlots;
16800       backtrace_symbols_fd(pBt, pHdr->nBacktrace, fileno(out));
16801       fprintf(out, "\n");
16802     }
16803   }
16804   fprintf(out, "COUNTS:\n");
16805   for(i=0; i<NCSIZE-1; i++){
16806     if( mem.nAlloc[i] ){
16807       fprintf(out, "   %5d: %10d %10d %10d\n",
16808             i*8, mem.nAlloc[i], mem.nCurrent[i], mem.mxCurrent[i]);
16809     }
16810   }
16811   if( mem.nAlloc[NCSIZE-1] ){
16812     fprintf(out, "   %5d: %10d %10d %10d\n",
16813              NCSIZE*8-8, mem.nAlloc[NCSIZE-1],
16814              mem.nCurrent[NCSIZE-1], mem.mxCurrent[NCSIZE-1]);
16815   }
16816   fclose(out);
16817 }
16818 
16819 /*
16820 ** Return the number of times sqlite3MemMalloc() has been called.
16821 */
16822 SQLITE_PRIVATE int sqlite3MemdebugMallocCount(){
16823   int i;
16824   int nTotal = 0;
16825   for(i=0; i<NCSIZE; i++){
16826     nTotal += mem.nAlloc[i];
16827   }
16828   return nTotal;
16829 }
16830 
16831 
16832 #endif /* SQLITE_MEMDEBUG */
16833 
16834 /************** End of mem2.c ************************************************/
16835 /************** Begin file mem3.c ********************************************/
16836 /*
16837 ** 2007 October 14
16838 **
16839 ** The author disclaims copyright to this source code.  In place of
16840 ** a legal notice, here is a blessing:
16841 **
16842 **    May you do good and not evil.
16843 **    May you find forgiveness for yourself and forgive others.
16844 **    May you share freely, never taking more than you give.
16845 **
16846 *************************************************************************
16847 ** This file contains the C functions that implement a memory
16848 ** allocation subsystem for use by SQLite.
16849 **
16850 ** This version of the memory allocation subsystem omits all
16851 ** use of malloc(). The SQLite user supplies a block of memory
16852 ** before calling sqlite3_initialize() from which allocations
16853 ** are made and returned by the xMalloc() and xRealloc()
16854 ** implementations. Once sqlite3_initialize() has been called,
16855 ** the amount of memory available to SQLite is fixed and cannot
16856 ** be changed.
16857 **
16858 ** This version of the memory allocation subsystem is included
16859 ** in the build only if SQLITE_ENABLE_MEMSYS3 is defined.
16860 */
16861 
16862 /*
16863 ** This version of the memory allocator is only built into the library
16864 ** SQLITE_ENABLE_MEMSYS3 is defined. Defining this symbol does not
16865 ** mean that the library will use a memory-pool by default, just that
16866 ** it is available. The mempool allocator is activated by calling
16867 ** sqlite3_config().
16868 */
16869 #ifdef SQLITE_ENABLE_MEMSYS3
16870 
16871 /*
16872 ** Maximum size (in Mem3Blocks) of a "small" chunk.
16873 */
16874 #define MX_SMALL 10
16875 
16876 
16877 /*
16878 ** Number of freelist hash slots
16879 */
16880 #define N_HASH  61
16881 
16882 /*
16883 ** A memory allocation (also called a "chunk") consists of two or
16884 ** more blocks where each block is 8 bytes.  The first 8 bytes are
16885 ** a header that is not returned to the user.
16886 **
16887 ** A chunk is two or more blocks that is either checked out or
16888 ** free.  The first block has format u.hdr.  u.hdr.size4x is 4 times the
16889 ** size of the allocation in blocks if the allocation is free.
16890 ** The u.hdr.size4x&1 bit is true if the chunk is checked out and
16891 ** false if the chunk is on the freelist.  The u.hdr.size4x&2 bit
16892 ** is true if the previous chunk is checked out and false if the
16893 ** previous chunk is free.  The u.hdr.prevSize field is the size of
16894 ** the previous chunk in blocks if the previous chunk is on the
16895 ** freelist. If the previous chunk is checked out, then
16896 ** u.hdr.prevSize can be part of the data for that chunk and should
16897 ** not be read or written.
16898 **
16899 ** We often identify a chunk by its index in mem3.aPool[].  When
16900 ** this is done, the chunk index refers to the second block of
16901 ** the chunk.  In this way, the first chunk has an index of 1.
16902 ** A chunk index of 0 means "no such chunk" and is the equivalent
16903 ** of a NULL pointer.
16904 **
16905 ** The second block of free chunks is of the form u.list.  The
16906 ** two fields form a double-linked list of chunks of related sizes.
16907 ** Pointers to the head of the list are stored in mem3.aiSmall[]
16908 ** for smaller chunks and mem3.aiHash[] for larger chunks.
16909 **
16910 ** The second block of a chunk is user data if the chunk is checked
16911 ** out.  If a chunk is checked out, the user data may extend into
16912 ** the u.hdr.prevSize value of the following chunk.
16913 */
16914 typedef struct Mem3Block Mem3Block;
16915 struct Mem3Block {
16916   union {
16917     struct {
16918       u32 prevSize;   /* Size of previous chunk in Mem3Block elements */
16919       u32 size4x;     /* 4x the size of current chunk in Mem3Block elements */
16920     } hdr;
16921     struct {
16922       u32 next;       /* Index in mem3.aPool[] of next free chunk */
16923       u32 prev;       /* Index in mem3.aPool[] of previous free chunk */
16924     } list;
16925   } u;
16926 };
16927 
16928 /*
16929 ** All of the static variables used by this module are collected
16930 ** into a single structure named "mem3".  This is to keep the
16931 ** static variables organized and to reduce namespace pollution
16932 ** when this module is combined with other in the amalgamation.
16933 */
16934 static SQLITE_WSD struct Mem3Global {
16935   /*
16936   ** Memory available for allocation. nPool is the size of the array
16937   ** (in Mem3Blocks) pointed to by aPool less 2.
16938   */
16939   u32 nPool;
16940   Mem3Block *aPool;
16941 
16942   /*
16943   ** True if we are evaluating an out-of-memory callback.
16944   */
16945   int alarmBusy;
16946 
16947   /*
16948   ** Mutex to control access to the memory allocation subsystem.
16949   */
16950   sqlite3_mutex *mutex;
16951 
16952   /*
16953   ** The minimum amount of free space that we have seen.
16954   */
16955   u32 mnMaster;
16956 
16957   /*
16958   ** iMaster is the index of the master chunk.  Most new allocations
16959   ** occur off of this chunk.  szMaster is the size (in Mem3Blocks)
16960   ** of the current master.  iMaster is 0 if there is not master chunk.
16961   ** The master chunk is not in either the aiHash[] or aiSmall[].
16962   */
16963   u32 iMaster;
16964   u32 szMaster;
16965 
16966   /*
16967   ** Array of lists of free blocks according to the block size
16968   ** for smaller chunks, or a hash on the block size for larger
16969   ** chunks.
16970   */
16971   u32 aiSmall[MX_SMALL-1];   /* For sizes 2 through MX_SMALL, inclusive */
16972   u32 aiHash[N_HASH];        /* For sizes MX_SMALL+1 and larger */
16973 } mem3 = { 97535575 };
16974 
16975 #define mem3 GLOBAL(struct Mem3Global, mem3)
16976 
16977 /*
16978 ** Unlink the chunk at mem3.aPool[i] from list it is currently
16979 ** on.  *pRoot is the list that i is a member of.
16980 */
16981 static void memsys3UnlinkFromList(u32 i, u32 *pRoot){
16982   u32 next = mem3.aPool[i].u.list.next;
16983   u32 prev = mem3.aPool[i].u.list.prev;
16984   assert( sqlite3_mutex_held(mem3.mutex) );
16985   if( prev==0 ){
16986     *pRoot = next;
16987   }else{
16988     mem3.aPool[prev].u.list.next = next;
16989   }
16990   if( next ){
16991     mem3.aPool[next].u.list.prev = prev;
16992   }
16993   mem3.aPool[i].u.list.next = 0;
16994   mem3.aPool[i].u.list.prev = 0;
16995 }
16996 
16997 /*
16998 ** Unlink the chunk at index i from
16999 ** whatever list is currently a member of.
17000 */
17001 static void memsys3Unlink(u32 i){
17002   u32 size, hash;
17003   assert( sqlite3_mutex_held(mem3.mutex) );
17004   assert( (mem3.aPool[i-1].u.hdr.size4x & 1)==0 );
17005   assert( i>=1 );
17006   size = mem3.aPool[i-1].u.hdr.size4x/4;
17007   assert( size==mem3.aPool[i+size-1].u.hdr.prevSize );
17008   assert( size>=2 );
17009   if( size <= MX_SMALL ){
17010     memsys3UnlinkFromList(i, &mem3.aiSmall[size-2]);
17011   }else{
17012     hash = size % N_HASH;
17013     memsys3UnlinkFromList(i, &mem3.aiHash[hash]);
17014   }
17015 }
17016 
17017 /*
17018 ** Link the chunk at mem3.aPool[i] so that is on the list rooted
17019 ** at *pRoot.
17020 */
17021 static void memsys3LinkIntoList(u32 i, u32 *pRoot){
17022   assert( sqlite3_mutex_held(mem3.mutex) );
17023   mem3.aPool[i].u.list.next = *pRoot;
17024   mem3.aPool[i].u.list.prev = 0;
17025   if( *pRoot ){
17026     mem3.aPool[*pRoot].u.list.prev = i;
17027   }
17028   *pRoot = i;
17029 }
17030 
17031 /*
17032 ** Link the chunk at index i into either the appropriate
17033 ** small chunk list, or into the large chunk hash table.
17034 */
17035 static void memsys3Link(u32 i){
17036   u32 size, hash;
17037   assert( sqlite3_mutex_held(mem3.mutex) );
17038   assert( i>=1 );
17039   assert( (mem3.aPool[i-1].u.hdr.size4x & 1)==0 );
17040   size = mem3.aPool[i-1].u.hdr.size4x/4;
17041   assert( size==mem3.aPool[i+size-1].u.hdr.prevSize );
17042   assert( size>=2 );
17043   if( size <= MX_SMALL ){
17044     memsys3LinkIntoList(i, &mem3.aiSmall[size-2]);
17045   }else{
17046     hash = size % N_HASH;
17047     memsys3LinkIntoList(i, &mem3.aiHash[hash]);
17048   }
17049 }
17050 
17051 /*
17052 ** If the STATIC_MEM mutex is not already held, obtain it now. The mutex
17053 ** will already be held (obtained by code in malloc.c) if
17054 ** sqlite3GlobalConfig.bMemStat is true.
17055 */
17056 static void memsys3Enter(void){
17057   if( sqlite3GlobalConfig.bMemstat==0 && mem3.mutex==0 ){
17058     mem3.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
17059   }
17060   sqlite3_mutex_enter(mem3.mutex);
17061 }
17062 static void memsys3Leave(void){
17063   sqlite3_mutex_leave(mem3.mutex);
17064 }
17065 
17066 /*
17067 ** Called when we are unable to satisfy an allocation of nBytes.
17068 */
17069 static void memsys3OutOfMemory(int nByte){
17070   if( !mem3.alarmBusy ){
17071     mem3.alarmBusy = 1;
17072     assert( sqlite3_mutex_held(mem3.mutex) );
17073     sqlite3_mutex_leave(mem3.mutex);
17074     sqlite3_release_memory(nByte);
17075     sqlite3_mutex_enter(mem3.mutex);
17076     mem3.alarmBusy = 0;
17077   }
17078 }
17079 
17080 
17081 /*
17082 ** Chunk i is a free chunk that has been unlinked.  Adjust its
17083 ** size parameters for check-out and return a pointer to the
17084 ** user portion of the chunk.
17085 */
17086 static void *memsys3Checkout(u32 i, u32 nBlock){
17087   u32 x;
17088   assert( sqlite3_mutex_held(mem3.mutex) );
17089   assert( i>=1 );
17090   assert( mem3.aPool[i-1].u.hdr.size4x/4==nBlock );
17091   assert( mem3.aPool[i+nBlock-1].u.hdr.prevSize==nBlock );
17092   x = mem3.aPool[i-1].u.hdr.size4x;
17093   mem3.aPool[i-1].u.hdr.size4x = nBlock*4 | 1 | (x&2);
17094   mem3.aPool[i+nBlock-1].u.hdr.prevSize = nBlock;
17095   mem3.aPool[i+nBlock-1].u.hdr.size4x |= 2;
17096   return &mem3.aPool[i];
17097 }
17098 
17099 /*
17100 ** Carve a piece off of the end of the mem3.iMaster free chunk.
17101 ** Return a pointer to the new allocation.  Or, if the master chunk
17102 ** is not large enough, return 0.
17103 */
17104 static void *memsys3FromMaster(u32 nBlock){
17105   assert( sqlite3_mutex_held(mem3.mutex) );
17106   assert( mem3.szMaster>=nBlock );
17107   if( nBlock>=mem3.szMaster-1 ){
17108     /* Use the entire master */
17109     void *p = memsys3Checkout(mem3.iMaster, mem3.szMaster);
17110     mem3.iMaster = 0;
17111     mem3.szMaster = 0;
17112     mem3.mnMaster = 0;
17113     return p;
17114   }else{
17115     /* Split the master block.  Return the tail. */
17116     u32 newi, x;
17117     newi = mem3.iMaster + mem3.szMaster - nBlock;
17118     assert( newi > mem3.iMaster+1 );
17119     mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.prevSize = nBlock;
17120     mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.size4x |= 2;
17121     mem3.aPool[newi-1].u.hdr.size4x = nBlock*4 + 1;
17122     mem3.szMaster -= nBlock;
17123     mem3.aPool[newi-1].u.hdr.prevSize = mem3.szMaster;
17124     x = mem3.aPool[mem3.iMaster-1].u.hdr.size4x & 2;
17125     mem3.aPool[mem3.iMaster-1].u.hdr.size4x = mem3.szMaster*4 | x;
17126     if( mem3.szMaster < mem3.mnMaster ){
17127       mem3.mnMaster = mem3.szMaster;
17128     }
17129     return (void*)&mem3.aPool[newi];
17130   }
17131 }
17132 
17133 /*
17134 ** *pRoot is the head of a list of free chunks of the same size
17135 ** or same size hash.  In other words, *pRoot is an entry in either
17136 ** mem3.aiSmall[] or mem3.aiHash[].
17137 **
17138 ** This routine examines all entries on the given list and tries
17139 ** to coalesce each entries with adjacent free chunks.
17140 **
17141 ** If it sees a chunk that is larger than mem3.iMaster, it replaces
17142 ** the current mem3.iMaster with the new larger chunk.  In order for
17143 ** this mem3.iMaster replacement to work, the master chunk must be
17144 ** linked into the hash tables.  That is not the normal state of
17145 ** affairs, of course.  The calling routine must link the master
17146 ** chunk before invoking this routine, then must unlink the (possibly
17147 ** changed) master chunk once this routine has finished.
17148 */
17149 static void memsys3Merge(u32 *pRoot){
17150   u32 iNext, prev, size, i, x;
17151 
17152   assert( sqlite3_mutex_held(mem3.mutex) );
17153   for(i=*pRoot; i>0; i=iNext){
17154     iNext = mem3.aPool[i].u.list.next;
17155     size = mem3.aPool[i-1].u.hdr.size4x;
17156     assert( (size&1)==0 );
17157     if( (size&2)==0 ){
17158       memsys3UnlinkFromList(i, pRoot);
17159       assert( i > mem3.aPool[i-1].u.hdr.prevSize );
17160       prev = i - mem3.aPool[i-1].u.hdr.prevSize;
17161       if( prev==iNext ){
17162         iNext = mem3.aPool[prev].u.list.next;
17163       }
17164       memsys3Unlink(prev);
17165       size = i + size/4 - prev;
17166       x = mem3.aPool[prev-1].u.hdr.size4x & 2;
17167       mem3.aPool[prev-1].u.hdr.size4x = size*4 | x;
17168       mem3.aPool[prev+size-1].u.hdr.prevSize = size;
17169       memsys3Link(prev);
17170       i = prev;
17171     }else{
17172       size /= 4;
17173     }
17174     if( size>mem3.szMaster ){
17175       mem3.iMaster = i;
17176       mem3.szMaster = size;
17177     }
17178   }
17179 }
17180 
17181 /*
17182 ** Return a block of memory of at least nBytes in size.
17183 ** Return NULL if unable.
17184 **
17185 ** This function assumes that the necessary mutexes, if any, are
17186 ** already held by the caller. Hence "Unsafe".
17187 */
17188 static void *memsys3MallocUnsafe(int nByte){
17189   u32 i;
17190   u32 nBlock;
17191   u32 toFree;
17192 
17193   assert( sqlite3_mutex_held(mem3.mutex) );
17194   assert( sizeof(Mem3Block)==8 );
17195   if( nByte<=12 ){
17196     nBlock = 2;
17197   }else{
17198     nBlock = (nByte + 11)/8;
17199   }
17200   assert( nBlock>=2 );
17201 
17202   /* STEP 1:
17203   ** Look for an entry of the correct size in either the small
17204   ** chunk table or in the large chunk hash table.  This is
17205   ** successful most of the time (about 9 times out of 10).
17206   */
17207   if( nBlock <= MX_SMALL ){
17208     i = mem3.aiSmall[nBlock-2];
17209     if( i>0 ){
17210       memsys3UnlinkFromList(i, &mem3.aiSmall[nBlock-2]);
17211       return memsys3Checkout(i, nBlock);
17212     }
17213   }else{
17214     int hash = nBlock % N_HASH;
17215     for(i=mem3.aiHash[hash]; i>0; i=mem3.aPool[i].u.list.next){
17216       if( mem3.aPool[i-1].u.hdr.size4x/4==nBlock ){
17217         memsys3UnlinkFromList(i, &mem3.aiHash[hash]);
17218         return memsys3Checkout(i, nBlock);
17219       }
17220     }
17221   }
17222 
17223   /* STEP 2:
17224   ** Try to satisfy the allocation by carving a piece off of the end
17225   ** of the master chunk.  This step usually works if step 1 fails.
17226   */
17227   if( mem3.szMaster>=nBlock ){
17228     return memsys3FromMaster(nBlock);
17229   }
17230 
17231 
17232   /* STEP 3:
17233   ** Loop through the entire memory pool.  Coalesce adjacent free
17234   ** chunks.  Recompute the master chunk as the largest free chunk.
17235   ** Then try again to satisfy the allocation by carving a piece off
17236   ** of the end of the master chunk.  This step happens very
17237   ** rarely (we hope!)
17238   */
17239   for(toFree=nBlock*16; toFree<(mem3.nPool*16); toFree *= 2){
17240     memsys3OutOfMemory(toFree);
17241     if( mem3.iMaster ){
17242       memsys3Link(mem3.iMaster);
17243       mem3.iMaster = 0;
17244       mem3.szMaster = 0;
17245     }
17246     for(i=0; i<N_HASH; i++){
17247       memsys3Merge(&mem3.aiHash[i]);
17248     }
17249     for(i=0; i<MX_SMALL-1; i++){
17250       memsys3Merge(&mem3.aiSmall[i]);
17251     }
17252     if( mem3.szMaster ){
17253       memsys3Unlink(mem3.iMaster);
17254       if( mem3.szMaster>=nBlock ){
17255         return memsys3FromMaster(nBlock);
17256       }
17257     }
17258   }
17259 
17260   /* If none of the above worked, then we fail. */
17261   return 0;
17262 }
17263 
17264 /*
17265 ** Free an outstanding memory allocation.
17266 **
17267 ** This function assumes that the necessary mutexes, if any, are
17268 ** already held by the caller. Hence "Unsafe".
17269 */
17270 static void memsys3FreeUnsafe(void *pOld){
17271   Mem3Block *p = (Mem3Block*)pOld;
17272   int i;
17273   u32 size, x;
17274   assert( sqlite3_mutex_held(mem3.mutex) );
17275   assert( p>mem3.aPool && p<&mem3.aPool[mem3.nPool] );
17276   i = p - mem3.aPool;
17277   assert( (mem3.aPool[i-1].u.hdr.size4x&1)==1 );
17278   size = mem3.aPool[i-1].u.hdr.size4x/4;
17279   assert( i+size<=mem3.nPool+1 );
17280   mem3.aPool[i-1].u.hdr.size4x &= ~1;
17281   mem3.aPool[i+size-1].u.hdr.prevSize = size;
17282   mem3.aPool[i+size-1].u.hdr.size4x &= ~2;
17283   memsys3Link(i);
17284 
17285   /* Try to expand the master using the newly freed chunk */
17286   if( mem3.iMaster ){
17287     while( (mem3.aPool[mem3.iMaster-1].u.hdr.size4x&2)==0 ){
17288       size = mem3.aPool[mem3.iMaster-1].u.hdr.prevSize;
17289       mem3.iMaster -= size;
17290       mem3.szMaster += size;
17291       memsys3Unlink(mem3.iMaster);
17292       x = mem3.aPool[mem3.iMaster-1].u.hdr.size4x & 2;
17293       mem3.aPool[mem3.iMaster-1].u.hdr.size4x = mem3.szMaster*4 | x;
17294       mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.prevSize = mem3.szMaster;
17295     }
17296     x = mem3.aPool[mem3.iMaster-1].u.hdr.size4x & 2;
17297     while( (mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.size4x&1)==0 ){
17298       memsys3Unlink(mem3.iMaster+mem3.szMaster);
17299       mem3.szMaster += mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.size4x/4;
17300       mem3.aPool[mem3.iMaster-1].u.hdr.size4x = mem3.szMaster*4 | x;
17301       mem3.aPool[mem3.iMaster+mem3.szMaster-1].u.hdr.prevSize = mem3.szMaster;
17302     }
17303   }
17304 }
17305 
17306 /*
17307 ** Return the size of an outstanding allocation, in bytes.  The
17308 ** size returned omits the 8-byte header overhead.  This only
17309 ** works for chunks that are currently checked out.
17310 */
17311 static int memsys3Size(void *p){
17312   Mem3Block *pBlock;
17313   if( p==0 ) return 0;
17314   pBlock = (Mem3Block*)p;
17315   assert( (pBlock[-1].u.hdr.size4x&1)!=0 );
17316   return (pBlock[-1].u.hdr.size4x&~3)*2 - 4;
17317 }
17318 
17319 /*
17320 ** Round up a request size to the next valid allocation size.
17321 */
17322 static int memsys3Roundup(int n){
17323   if( n<=12 ){
17324     return 12;
17325   }else{
17326     return ((n+11)&~7) - 4;
17327   }
17328 }
17329 
17330 /*
17331 ** Allocate nBytes of memory.
17332 */
17333 static void *memsys3Malloc(int nBytes){
17334   sqlite3_int64 *p;
17335   assert( nBytes>0 );          /* malloc.c filters out 0 byte requests */
17336   memsys3Enter();
17337   p = memsys3MallocUnsafe(nBytes);
17338   memsys3Leave();
17339   return (void*)p;
17340 }
17341 
17342 /*
17343 ** Free memory.
17344 */
17345 static void memsys3Free(void *pPrior){
17346   assert( pPrior );
17347   memsys3Enter();
17348   memsys3FreeUnsafe(pPrior);
17349   memsys3Leave();
17350 }
17351 
17352 /*
17353 ** Change the size of an existing memory allocation
17354 */
17355 static void *memsys3Realloc(void *pPrior, int nBytes){
17356   int nOld;
17357   void *p;
17358   if( pPrior==0 ){
17359     return sqlite3_malloc(nBytes);
17360   }
17361   if( nBytes<=0 ){
17362     sqlite3_free(pPrior);
17363     return 0;
17364   }
17365   nOld = memsys3Size(pPrior);
17366   if( nBytes<=nOld && nBytes>=nOld-128 ){
17367     return pPrior;
17368   }
17369   memsys3Enter();
17370   p = memsys3MallocUnsafe(nBytes);
17371   if( p ){
17372     if( nOld<nBytes ){
17373       memcpy(p, pPrior, nOld);
17374     }else{
17375       memcpy(p, pPrior, nBytes);
17376     }
17377     memsys3FreeUnsafe(pPrior);
17378   }
17379   memsys3Leave();
17380   return p;
17381 }
17382 
17383 /*
17384 ** Initialize this module.
17385 */
17386 static int memsys3Init(void *NotUsed){
17387   UNUSED_PARAMETER(NotUsed);
17388   if( !sqlite3GlobalConfig.pHeap ){
17389     return SQLITE_ERROR;
17390   }
17391 
17392   /* Store a pointer to the memory block in global structure mem3. */
17393   assert( sizeof(Mem3Block)==8 );
17394   mem3.aPool = (Mem3Block *)sqlite3GlobalConfig.pHeap;
17395   mem3.nPool = (sqlite3GlobalConfig.nHeap / sizeof(Mem3Block)) - 2;
17396 
17397   /* Initialize the master block. */
17398   mem3.szMaster = mem3.nPool;
17399   mem3.mnMaster = mem3.szMaster;
17400   mem3.iMaster = 1;
17401   mem3.aPool[0].u.hdr.size4x = (mem3.szMaster<<2) + 2;
17402   mem3.aPool[mem3.nPool].u.hdr.prevSize = mem3.nPool;
17403   mem3.aPool[mem3.nPool].u.hdr.size4x = 1;
17404 
17405   return SQLITE_OK;
17406 }
17407 
17408 /*
17409 ** Deinitialize this module.
17410 */
17411 static void memsys3Shutdown(void *NotUsed){
17412   UNUSED_PARAMETER(NotUsed);
17413   mem3.mutex = 0;
17414   return;
17415 }
17416 
17417 
17418 
17419 /*
17420 ** Open the file indicated and write a log of all unfreed memory
17421 ** allocations into that log.
17422 */
17423 SQLITE_PRIVATE void sqlite3Memsys3Dump(const char *zFilename){
17424 #ifdef SQLITE_DEBUG
17425   FILE *out;
17426   u32 i, j;
17427   u32 size;
17428   if( zFilename==0 || zFilename[0]==0 ){
17429     out = stdout;
17430   }else{
17431     out = fopen(zFilename, "w");
17432     if( out==0 ){
17433       fprintf(stderr, "** Unable to output memory debug output log: %s **\n",
17434                       zFilename);
17435       return;
17436     }
17437   }
17438   memsys3Enter();
17439   fprintf(out, "CHUNKS:\n");
17440   for(i=1; i<=mem3.nPool; i+=size/4){
17441     size = mem3.aPool[i-1].u.hdr.size4x;
17442     if( size/4<=1 ){
17443       fprintf(out, "%p size error\n", &mem3.aPool[i]);
17444       assert( 0 );
17445       break;
17446     }
17447     if( (size&1)==0 && mem3.aPool[i+size/4-1].u.hdr.prevSize!=size/4 ){
17448       fprintf(out, "%p tail size does not match\n", &mem3.aPool[i]);
17449       assert( 0 );
17450       break;
17451     }
17452     if( ((mem3.aPool[i+size/4-1].u.hdr.size4x&2)>>1)!=(size&1) ){
17453       fprintf(out, "%p tail checkout bit is incorrect\n", &mem3.aPool[i]);
17454       assert( 0 );
17455       break;
17456     }
17457     if( size&1 ){
17458       fprintf(out, "%p %6d bytes checked out\n", &mem3.aPool[i], (size/4)*8-8);
17459     }else{
17460       fprintf(out, "%p %6d bytes free%s\n", &mem3.aPool[i], (size/4)*8-8,
17461                   i==mem3.iMaster ? " **master**" : "");
17462     }
17463   }
17464   for(i=0; i<MX_SMALL-1; i++){
17465     if( mem3.aiSmall[i]==0 ) continue;
17466     fprintf(out, "small(%2d):", i);
17467     for(j = mem3.aiSmall[i]; j>0; j=mem3.aPool[j].u.list.next){
17468       fprintf(out, " %p(%d)", &mem3.aPool[j],
17469               (mem3.aPool[j-1].u.hdr.size4x/4)*8-8);
17470     }
17471     fprintf(out, "\n");
17472   }
17473   for(i=0; i<N_HASH; i++){
17474     if( mem3.aiHash[i]==0 ) continue;
17475     fprintf(out, "hash(%2d):", i);
17476     for(j = mem3.aiHash[i]; j>0; j=mem3.aPool[j].u.list.next){
17477       fprintf(out, " %p(%d)", &mem3.aPool[j],
17478               (mem3.aPool[j-1].u.hdr.size4x/4)*8-8);
17479     }
17480     fprintf(out, "\n");
17481   }
17482   fprintf(out, "master=%d\n", mem3.iMaster);
17483   fprintf(out, "nowUsed=%d\n", mem3.nPool*8 - mem3.szMaster*8);
17484   fprintf(out, "mxUsed=%d\n", mem3.nPool*8 - mem3.mnMaster*8);
17485   sqlite3_mutex_leave(mem3.mutex);
17486   if( out==stdout ){
17487     fflush(stdout);
17488   }else{
17489     fclose(out);
17490   }
17491 #else
17492   UNUSED_PARAMETER(zFilename);
17493 #endif
17494 }
17495 
17496 /*
17497 ** This routine is the only routine in this file with external
17498 ** linkage.
17499 **
17500 ** Populate the low-level memory allocation function pointers in
17501 ** sqlite3GlobalConfig.m with pointers to the routines in this file. The
17502 ** arguments specify the block of memory to manage.
17503 **
17504 ** This routine is only called by sqlite3_config(), and therefore
17505 ** is not required to be threadsafe (it is not).
17506 */
17507 SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys3(void){
17508   static const sqlite3_mem_methods mempoolMethods = {
17509      memsys3Malloc,
17510      memsys3Free,
17511      memsys3Realloc,
17512      memsys3Size,
17513      memsys3Roundup,
17514      memsys3Init,
17515      memsys3Shutdown,
17516      0
17517   };
17518   return &mempoolMethods;
17519 }
17520 
17521 #endif /* SQLITE_ENABLE_MEMSYS3 */
17522 
17523 /************** End of mem3.c ************************************************/
17524 /************** Begin file mem5.c ********************************************/
17525 /*
17526 ** 2007 October 14
17527 **
17528 ** The author disclaims copyright to this source code.  In place of
17529 ** a legal notice, here is a blessing:
17530 **
17531 **    May you do good and not evil.
17532 **    May you find forgiveness for yourself and forgive others.
17533 **    May you share freely, never taking more than you give.
17534 **
17535 *************************************************************************
17536 ** This file contains the C functions that implement a memory
17537 ** allocation subsystem for use by SQLite.
17538 **
17539 ** This version of the memory allocation subsystem omits all
17540 ** use of malloc(). The application gives SQLite a block of memory
17541 ** before calling sqlite3_initialize() from which allocations
17542 ** are made and returned by the xMalloc() and xRealloc()
17543 ** implementations. Once sqlite3_initialize() has been called,
17544 ** the amount of memory available to SQLite is fixed and cannot
17545 ** be changed.
17546 **
17547 ** This version of the memory allocation subsystem is included
17548 ** in the build only if SQLITE_ENABLE_MEMSYS5 is defined.
17549 **
17550 ** This memory allocator uses the following algorithm:
17551 **
17552 **   1.  All memory allocations sizes are rounded up to a power of 2.
17553 **
17554 **   2.  If two adjacent free blocks are the halves of a larger block,
17555 **       then the two blocks are coalesed into the single larger block.
17556 **
17557 **   3.  New memory is allocated from the first available free block.
17558 **
17559 ** This algorithm is described in: J. M. Robson. "Bounds for Some Functions
17560 ** Concerning Dynamic Storage Allocation". Journal of the Association for
17561 ** Computing Machinery, Volume 21, Number 8, July 1974, pages 491-499.
17562 **
17563 ** Let n be the size of the largest allocation divided by the minimum
17564 ** allocation size (after rounding all sizes up to a power of 2.)  Let M
17565 ** be the maximum amount of memory ever outstanding at one time.  Let
17566 ** N be the total amount of memory available for allocation.  Robson
17567 ** proved that this memory allocator will never breakdown due to
17568 ** fragmentation as long as the following constraint holds:
17569 **
17570 **      N >=  M*(1 + log2(n)/2) - n + 1
17571 **
17572 ** The sqlite3_status() logic tracks the maximum values of n and M so
17573 ** that an application can, at any time, verify this constraint.
17574 */
17575 
17576 /*
17577 ** This version of the memory allocator is used only when
17578 ** SQLITE_ENABLE_MEMSYS5 is defined.
17579 */
17580 #ifdef SQLITE_ENABLE_MEMSYS5
17581 
17582 /*
17583 ** A minimum allocation is an instance of the following structure.
17584 ** Larger allocations are an array of these structures where the
17585 ** size of the array is a power of 2.
17586 **
17587 ** The size of this object must be a power of two.  That fact is
17588 ** verified in memsys5Init().
17589 */
17590 typedef struct Mem5Link Mem5Link;
17591 struct Mem5Link {
17592   int next;       /* Index of next free chunk */
17593   int prev;       /* Index of previous free chunk */
17594 };
17595 
17596 /*
17597 ** Maximum size of any allocation is ((1<<LOGMAX)*mem5.szAtom). Since
17598 ** mem5.szAtom is always at least 8 and 32-bit integers are used,
17599 ** it is not actually possible to reach this limit.
17600 */
17601 #define LOGMAX 30
17602 
17603 /*
17604 ** Masks used for mem5.aCtrl[] elements.
17605 */
17606 #define CTRL_LOGSIZE  0x1f    /* Log2 Size of this block */
17607 #define CTRL_FREE     0x20    /* True if not checked out */
17608 
17609 /*
17610 ** All of the static variables used by this module are collected
17611 ** into a single structure named "mem5".  This is to keep the
17612 ** static variables organized and to reduce namespace pollution
17613 ** when this module is combined with other in the amalgamation.
17614 */
17615 static SQLITE_WSD struct Mem5Global {
17616   /*
17617   ** Memory available for allocation
17618   */
17619   int szAtom;      /* Smallest possible allocation in bytes */
17620   int nBlock;      /* Number of szAtom sized blocks in zPool */
17621   u8 *zPool;       /* Memory available to be allocated */
17622 
17623   /*
17624   ** Mutex to control access to the memory allocation subsystem.
17625   */
17626   sqlite3_mutex *mutex;
17627 
17628   /*
17629   ** Performance statistics
17630   */
17631   u64 nAlloc;         /* Total number of calls to malloc */
17632   u64 totalAlloc;     /* Total of all malloc calls - includes internal frag */
17633   u64 totalExcess;    /* Total internal fragmentation */
17634   u32 currentOut;     /* Current checkout, including internal fragmentation */
17635   u32 currentCount;   /* Current number of distinct checkouts */
17636   u32 maxOut;         /* Maximum instantaneous currentOut */
17637   u32 maxCount;       /* Maximum instantaneous currentCount */
17638   u32 maxRequest;     /* Largest allocation (exclusive of internal frag) */
17639 
17640   /*
17641   ** Lists of free blocks.  aiFreelist[0] is a list of free blocks of
17642   ** size mem5.szAtom.  aiFreelist[1] holds blocks of size szAtom*2.
17643   ** and so forth.
17644   */
17645   int aiFreelist[LOGMAX+1];
17646 
17647   /*
17648   ** Space for tracking which blocks are checked out and the size
17649   ** of each block.  One byte per block.
17650   */
17651   u8 *aCtrl;
17652 
17653 } mem5;
17654 
17655 /*
17656 ** Access the static variable through a macro for SQLITE_OMIT_WSD.
17657 */
17658 #define mem5 GLOBAL(struct Mem5Global, mem5)
17659 
17660 /*
17661 ** Assuming mem5.zPool is divided up into an array of Mem5Link
17662 ** structures, return a pointer to the idx-th such link.
17663 */
17664 #define MEM5LINK(idx) ((Mem5Link *)(&mem5.zPool[(idx)*mem5.szAtom]))
17665 
17666 /*
17667 ** Unlink the chunk at mem5.aPool[i] from list it is currently
17668 ** on.  It should be found on mem5.aiFreelist[iLogsize].
17669 */
17670 static void memsys5Unlink(int i, int iLogsize){
17671   int next, prev;
17672   assert( i>=0 && i<mem5.nBlock );
17673   assert( iLogsize>=0 && iLogsize<=LOGMAX );
17674   assert( (mem5.aCtrl[i] & CTRL_LOGSIZE)==iLogsize );
17675 
17676   next = MEM5LINK(i)->next;
17677   prev = MEM5LINK(i)->prev;
17678   if( prev<0 ){
17679     mem5.aiFreelist[iLogsize] = next;
17680   }else{
17681     MEM5LINK(prev)->next = next;
17682   }
17683   if( next>=0 ){
17684     MEM5LINK(next)->prev = prev;
17685   }
17686 }
17687 
17688 /*
17689 ** Link the chunk at mem5.aPool[i] so that is on the iLogsize
17690 ** free list.
17691 */
17692 static void memsys5Link(int i, int iLogsize){
17693   int x;
17694   assert( sqlite3_mutex_held(mem5.mutex) );
17695   assert( i>=0 && i<mem5.nBlock );
17696   assert( iLogsize>=0 && iLogsize<=LOGMAX );
17697   assert( (mem5.aCtrl[i] & CTRL_LOGSIZE)==iLogsize );
17698 
17699   x = MEM5LINK(i)->next = mem5.aiFreelist[iLogsize];
17700   MEM5LINK(i)->prev = -1;
17701   if( x>=0 ){
17702     assert( x<mem5.nBlock );
17703     MEM5LINK(x)->prev = i;
17704   }
17705   mem5.aiFreelist[iLogsize] = i;
17706 }
17707 
17708 /*
17709 ** If the STATIC_MEM mutex is not already held, obtain it now. The mutex
17710 ** will already be held (obtained by code in malloc.c) if
17711 ** sqlite3GlobalConfig.bMemStat is true.
17712 */
17713 static void memsys5Enter(void){
17714   sqlite3_mutex_enter(mem5.mutex);
17715 }
17716 static void memsys5Leave(void){
17717   sqlite3_mutex_leave(mem5.mutex);
17718 }
17719 
17720 /*
17721 ** Return the size of an outstanding allocation, in bytes.  The
17722 ** size returned omits the 8-byte header overhead.  This only
17723 ** works for chunks that are currently checked out.
17724 */
17725 static int memsys5Size(void *p){
17726   int iSize = 0;
17727   if( p ){
17728     int i = (int)(((u8 *)p-mem5.zPool)/mem5.szAtom);
17729     assert( i>=0 && i<mem5.nBlock );
17730     iSize = mem5.szAtom * (1 << (mem5.aCtrl[i]&CTRL_LOGSIZE));
17731   }
17732   return iSize;
17733 }
17734 
17735 /*
17736 ** Return a block of memory of at least nBytes in size.
17737 ** Return NULL if unable.  Return NULL if nBytes==0.
17738 **
17739 ** The caller guarantees that nByte is positive.
17740 **
17741 ** The caller has obtained a mutex prior to invoking this
17742 ** routine so there is never any chance that two or more
17743 ** threads can be in this routine at the same time.
17744 */
17745 static void *memsys5MallocUnsafe(int nByte){
17746   int i;           /* Index of a mem5.aPool[] slot */
17747   int iBin;        /* Index into mem5.aiFreelist[] */
17748   int iFullSz;     /* Size of allocation rounded up to power of 2 */
17749   int iLogsize;    /* Log2 of iFullSz/POW2_MIN */
17750 
17751   /* nByte must be a positive */
17752   assert( nByte>0 );
17753 
17754   /* Keep track of the maximum allocation request.  Even unfulfilled
17755   ** requests are counted */
17756   if( (u32)nByte>mem5.maxRequest ){
17757     mem5.maxRequest = nByte;
17758   }
17759 
17760   /* Abort if the requested allocation size is larger than the largest
17761   ** power of two that we can represent using 32-bit signed integers.
17762   */
17763   if( nByte > 0x40000000 ){
17764     return 0;
17765   }
17766 
17767   /* Round nByte up to the next valid power of two */
17768   for(iFullSz=mem5.szAtom, iLogsize=0; iFullSz<nByte; iFullSz *= 2, iLogsize++){}
17769 
17770   /* Make sure mem5.aiFreelist[iLogsize] contains at least one free
17771   ** block.  If not, then split a block of the next larger power of
17772   ** two in order to create a new free block of size iLogsize.
17773   */
17774   for(iBin=iLogsize; mem5.aiFreelist[iBin]<0 && iBin<=LOGMAX; iBin++){}
17775   if( iBin>LOGMAX ){
17776     testcase( sqlite3GlobalConfig.xLog!=0 );
17777     sqlite3_log(SQLITE_NOMEM, "failed to allocate %u bytes", nByte);
17778     return 0;
17779   }
17780   i = mem5.aiFreelist[iBin];
17781   memsys5Unlink(i, iBin);
17782   while( iBin>iLogsize ){
17783     int newSize;
17784 
17785     iBin--;
17786     newSize = 1 << iBin;
17787     mem5.aCtrl[i+newSize] = CTRL_FREE | iBin;
17788     memsys5Link(i+newSize, iBin);
17789   }
17790   mem5.aCtrl[i] = iLogsize;
17791 
17792   /* Update allocator performance statistics. */
17793   mem5.nAlloc++;
17794   mem5.totalAlloc += iFullSz;
17795   mem5.totalExcess += iFullSz - nByte;
17796   mem5.currentCount++;
17797   mem5.currentOut += iFullSz;
17798   if( mem5.maxCount<mem5.currentCount ) mem5.maxCount = mem5.currentCount;
17799   if( mem5.maxOut<mem5.currentOut ) mem5.maxOut = mem5.currentOut;
17800 
17801   /* Return a pointer to the allocated memory. */
17802   return (void*)&mem5.zPool[i*mem5.szAtom];
17803 }
17804 
17805 /*
17806 ** Free an outstanding memory allocation.
17807 */
17808 static void memsys5FreeUnsafe(void *pOld){
17809   u32 size, iLogsize;
17810   int iBlock;
17811 
17812   /* Set iBlock to the index of the block pointed to by pOld in
17813   ** the array of mem5.szAtom byte blocks pointed to by mem5.zPool.
17814   */
17815   iBlock = (int)(((u8 *)pOld-mem5.zPool)/mem5.szAtom);
17816 
17817   /* Check that the pointer pOld points to a valid, non-free block. */
17818   assert( iBlock>=0 && iBlock<mem5.nBlock );
17819   assert( ((u8 *)pOld-mem5.zPool)%mem5.szAtom==0 );
17820   assert( (mem5.aCtrl[iBlock] & CTRL_FREE)==0 );
17821 
17822   iLogsize = mem5.aCtrl[iBlock] & CTRL_LOGSIZE;
17823   size = 1<<iLogsize;
17824   assert( iBlock+size-1<(u32)mem5.nBlock );
17825 
17826   mem5.aCtrl[iBlock] |= CTRL_FREE;
17827   mem5.aCtrl[iBlock+size-1] |= CTRL_FREE;
17828   assert( mem5.currentCount>0 );
17829   assert( mem5.currentOut>=(size*mem5.szAtom) );
17830   mem5.currentCount--;
17831   mem5.currentOut -= size*mem5.szAtom;
17832   assert( mem5.currentOut>0 || mem5.currentCount==0 );
17833   assert( mem5.currentCount>0 || mem5.currentOut==0 );
17834 
17835   mem5.aCtrl[iBlock] = CTRL_FREE | iLogsize;
17836   while( ALWAYS(iLogsize<LOGMAX) ){
17837     int iBuddy;
17838     if( (iBlock>>iLogsize) & 1 ){
17839       iBuddy = iBlock - size;
17840     }else{
17841       iBuddy = iBlock + size;
17842     }
17843     assert( iBuddy>=0 );
17844     if( (iBuddy+(1<<iLogsize))>mem5.nBlock ) break;
17845     if( mem5.aCtrl[iBuddy]!=(CTRL_FREE | iLogsize) ) break;
17846     memsys5Unlink(iBuddy, iLogsize);
17847     iLogsize++;
17848     if( iBuddy<iBlock ){
17849       mem5.aCtrl[iBuddy] = CTRL_FREE | iLogsize;
17850       mem5.aCtrl[iBlock] = 0;
17851       iBlock = iBuddy;
17852     }else{
17853       mem5.aCtrl[iBlock] = CTRL_FREE | iLogsize;
17854       mem5.aCtrl[iBuddy] = 0;
17855     }
17856     size *= 2;
17857   }
17858   memsys5Link(iBlock, iLogsize);
17859 }
17860 
17861 /*
17862 ** Allocate nBytes of memory.
17863 */
17864 static void *memsys5Malloc(int nBytes){
17865   sqlite3_int64 *p = 0;
17866   if( nBytes>0 ){
17867     memsys5Enter();
17868     p = memsys5MallocUnsafe(nBytes);
17869     memsys5Leave();
17870   }
17871   return (void*)p;
17872 }
17873 
17874 /*
17875 ** Free memory.
17876 **
17877 ** The outer layer memory allocator prevents this routine from
17878 ** being called with pPrior==0.
17879 */
17880 static void memsys5Free(void *pPrior){
17881   assert( pPrior!=0 );
17882   memsys5Enter();
17883   memsys5FreeUnsafe(pPrior);
17884   memsys5Leave();
17885 }
17886 
17887 /*
17888 ** Change the size of an existing memory allocation.
17889 **
17890 ** The outer layer memory allocator prevents this routine from
17891 ** being called with pPrior==0.
17892 **
17893 ** nBytes is always a value obtained from a prior call to
17894 ** memsys5Round().  Hence nBytes is always a non-negative power
17895 ** of two.  If nBytes==0 that means that an oversize allocation
17896 ** (an allocation larger than 0x40000000) was requested and this
17897 ** routine should return 0 without freeing pPrior.
17898 */
17899 static void *memsys5Realloc(void *pPrior, int nBytes){
17900   int nOld;
17901   void *p;
17902   assert( pPrior!=0 );
17903   assert( (nBytes&(nBytes-1))==0 );  /* EV: R-46199-30249 */
17904   assert( nBytes>=0 );
17905   if( nBytes==0 ){
17906     return 0;
17907   }
17908   nOld = memsys5Size(pPrior);
17909   if( nBytes<=nOld ){
17910     return pPrior;
17911   }
17912   memsys5Enter();
17913   p = memsys5MallocUnsafe(nBytes);
17914   if( p ){
17915     memcpy(p, pPrior, nOld);
17916     memsys5FreeUnsafe(pPrior);
17917   }
17918   memsys5Leave();
17919   return p;
17920 }
17921 
17922 /*
17923 ** Round up a request size to the next valid allocation size.  If
17924 ** the allocation is too large to be handled by this allocation system,
17925 ** return 0.
17926 **
17927 ** All allocations must be a power of two and must be expressed by a
17928 ** 32-bit signed integer.  Hence the largest allocation is 0x40000000
17929 ** or 1073741824 bytes.
17930 */
17931 static int memsys5Roundup(int n){
17932   int iFullSz;
17933   if( n > 0x40000000 ) return 0;
17934   for(iFullSz=mem5.szAtom; iFullSz<n; iFullSz *= 2);
17935   return iFullSz;
17936 }
17937 
17938 /*
17939 ** Return the ceiling of the logarithm base 2 of iValue.
17940 **
17941 ** Examples:   memsys5Log(1) -> 0
17942 **             memsys5Log(2) -> 1
17943 **             memsys5Log(4) -> 2
17944 **             memsys5Log(5) -> 3
17945 **             memsys5Log(8) -> 3
17946 **             memsys5Log(9) -> 4
17947 */
17948 static int memsys5Log(int iValue){
17949   int iLog;
17950   for(iLog=0; (iLog<(int)((sizeof(int)*8)-1)) && (1<<iLog)<iValue; iLog++);
17951   return iLog;
17952 }
17953 
17954 /*
17955 ** Initialize the memory allocator.
17956 **
17957 ** This routine is not threadsafe.  The caller must be holding a mutex
17958 ** to prevent multiple threads from entering at the same time.
17959 */
17960 static int memsys5Init(void *NotUsed){
17961   int ii;            /* Loop counter */
17962   int nByte;         /* Number of bytes of memory available to this allocator */
17963   u8 *zByte;         /* Memory usable by this allocator */
17964   int nMinLog;       /* Log base 2 of minimum allocation size in bytes */
17965   int iOffset;       /* An offset into mem5.aCtrl[] */
17966 
17967   UNUSED_PARAMETER(NotUsed);
17968 
17969   /* For the purposes of this routine, disable the mutex */
17970   mem5.mutex = 0;
17971 
17972   /* The size of a Mem5Link object must be a power of two.  Verify that
17973   ** this is case.
17974   */
17975   assert( (sizeof(Mem5Link)&(sizeof(Mem5Link)-1))==0 );
17976 
17977   nByte = sqlite3GlobalConfig.nHeap;
17978   zByte = (u8*)sqlite3GlobalConfig.pHeap;
17979   assert( zByte!=0 );  /* sqlite3_config() does not allow otherwise */
17980 
17981   /* boundaries on sqlite3GlobalConfig.mnReq are enforced in sqlite3_config() */
17982   nMinLog = memsys5Log(sqlite3GlobalConfig.mnReq);
17983   mem5.szAtom = (1<<nMinLog);
17984   while( (int)sizeof(Mem5Link)>mem5.szAtom ){
17985     mem5.szAtom = mem5.szAtom << 1;
17986   }
17987 
17988   mem5.nBlock = (nByte / (mem5.szAtom+sizeof(u8)));
17989   mem5.zPool = zByte;
17990   mem5.aCtrl = (u8 *)&mem5.zPool[mem5.nBlock*mem5.szAtom];
17991 
17992   for(ii=0; ii<=LOGMAX; ii++){
17993     mem5.aiFreelist[ii] = -1;
17994   }
17995 
17996   iOffset = 0;
17997   for(ii=LOGMAX; ii>=0; ii--){
17998     int nAlloc = (1<<ii);
17999     if( (iOffset+nAlloc)<=mem5.nBlock ){
18000       mem5.aCtrl[iOffset] = ii | CTRL_FREE;
18001       memsys5Link(iOffset, ii);
18002       iOffset += nAlloc;
18003     }
18004     assert((iOffset+nAlloc)>mem5.nBlock);
18005   }
18006 
18007   /* If a mutex is required for normal operation, allocate one */
18008   if( sqlite3GlobalConfig.bMemstat==0 ){
18009     mem5.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
18010   }
18011 
18012   return SQLITE_OK;
18013 }
18014 
18015 /*
18016 ** Deinitialize this module.
18017 */
18018 static void memsys5Shutdown(void *NotUsed){
18019   UNUSED_PARAMETER(NotUsed);
18020   mem5.mutex = 0;
18021   return;
18022 }
18023 
18024 #ifdef SQLITE_TEST
18025 /*
18026 ** Open the file indicated and write a log of all unfreed memory
18027 ** allocations into that log.
18028 */
18029 SQLITE_PRIVATE void sqlite3Memsys5Dump(const char *zFilename){
18030   FILE *out;
18031   int i, j, n;
18032   int nMinLog;
18033 
18034   if( zFilename==0 || zFilename[0]==0 ){
18035     out = stdout;
18036   }else{
18037     out = fopen(zFilename, "w");
18038     if( out==0 ){
18039       fprintf(stderr, "** Unable to output memory debug output log: %s **\n",
18040                       zFilename);
18041       return;
18042     }
18043   }
18044   memsys5Enter();
18045   nMinLog = memsys5Log(mem5.szAtom);
18046   for(i=0; i<=LOGMAX && i+nMinLog<32; i++){
18047     for(n=0, j=mem5.aiFreelist[i]; j>=0; j = MEM5LINK(j)->next, n++){}
18048     fprintf(out, "freelist items of size %d: %d\n", mem5.szAtom << i, n);
18049   }
18050   fprintf(out, "mem5.nAlloc       = %llu\n", mem5.nAlloc);
18051   fprintf(out, "mem5.totalAlloc   = %llu\n", mem5.totalAlloc);
18052   fprintf(out, "mem5.totalExcess  = %llu\n", mem5.totalExcess);
18053   fprintf(out, "mem5.currentOut   = %u\n", mem5.currentOut);
18054   fprintf(out, "mem5.currentCount = %u\n", mem5.currentCount);
18055   fprintf(out, "mem5.maxOut       = %u\n", mem5.maxOut);
18056   fprintf(out, "mem5.maxCount     = %u\n", mem5.maxCount);
18057   fprintf(out, "mem5.maxRequest   = %u\n", mem5.maxRequest);
18058   memsys5Leave();
18059   if( out==stdout ){
18060     fflush(stdout);
18061   }else{
18062     fclose(out);
18063   }
18064 }
18065 #endif
18066 
18067 /*
18068 ** This routine is the only routine in this file with external
18069 ** linkage. It returns a pointer to a static sqlite3_mem_methods
18070 ** struct populated with the memsys5 methods.
18071 */
18072 SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetMemsys5(void){
18073   static const sqlite3_mem_methods memsys5Methods = {
18074      memsys5Malloc,
18075      memsys5Free,
18076      memsys5Realloc,
18077      memsys5Size,
18078      memsys5Roundup,
18079      memsys5Init,
18080      memsys5Shutdown,
18081      0
18082   };
18083   return &memsys5Methods;
18084 }
18085 
18086 #endif /* SQLITE_ENABLE_MEMSYS5 */
18087 
18088 /************** End of mem5.c ************************************************/
18089 /************** Begin file mutex.c *******************************************/
18090 /*
18091 ** 2007 August 14
18092 **
18093 ** The author disclaims copyright to this source code.  In place of
18094 ** a legal notice, here is a blessing:
18095 **
18096 **    May you do good and not evil.
18097 **    May you find forgiveness for yourself and forgive others.
18098 **    May you share freely, never taking more than you give.
18099 **
18100 *************************************************************************
18101 ** This file contains the C functions that implement mutexes.
18102 **
18103 ** This file contains code that is common across all mutex implementations.
18104 */
18105 
18106 #if defined(SQLITE_DEBUG) && !defined(SQLITE_MUTEX_OMIT)
18107 /*
18108 ** For debugging purposes, record when the mutex subsystem is initialized
18109 ** and uninitialized so that we can assert() if there is an attempt to
18110 ** allocate a mutex while the system is uninitialized.
18111 */
18112 static SQLITE_WSD int mutexIsInit = 0;
18113 #endif /* SQLITE_DEBUG */
18114 
18115 
18116 #ifndef SQLITE_MUTEX_OMIT
18117 /*
18118 ** Initialize the mutex system.
18119 */
18120 SQLITE_PRIVATE int sqlite3MutexInit(void){
18121   int rc = SQLITE_OK;
18122   if( !sqlite3GlobalConfig.mutex.xMutexAlloc ){
18123     /* If the xMutexAlloc method has not been set, then the user did not
18124     ** install a mutex implementation via sqlite3_config() prior to
18125     ** sqlite3_initialize() being called. This block copies pointers to
18126     ** the default implementation into the sqlite3GlobalConfig structure.
18127     */
18128     sqlite3_mutex_methods const *pFrom;
18129     sqlite3_mutex_methods *pTo = &sqlite3GlobalConfig.mutex;
18130 
18131     if( sqlite3GlobalConfig.bCoreMutex ){
18132       pFrom = sqlite3DefaultMutex();
18133     }else{
18134       pFrom = sqlite3NoopMutex();
18135     }
18136     memcpy(pTo, pFrom, offsetof(sqlite3_mutex_methods, xMutexAlloc));
18137     memcpy(&pTo->xMutexFree, &pFrom->xMutexFree,
18138            sizeof(*pTo) - offsetof(sqlite3_mutex_methods, xMutexFree));
18139     pTo->xMutexAlloc = pFrom->xMutexAlloc;
18140   }
18141   rc = sqlite3GlobalConfig.mutex.xMutexInit();
18142 
18143 #ifdef SQLITE_DEBUG
18144   GLOBAL(int, mutexIsInit) = 1;
18145 #endif
18146 
18147   return rc;
18148 }
18149 
18150 /*
18151 ** Shutdown the mutex system. This call frees resources allocated by
18152 ** sqlite3MutexInit().
18153 */
18154 SQLITE_PRIVATE int sqlite3MutexEnd(void){
18155   int rc = SQLITE_OK;
18156   if( sqlite3GlobalConfig.mutex.xMutexEnd ){
18157     rc = sqlite3GlobalConfig.mutex.xMutexEnd();
18158   }
18159 
18160 #ifdef SQLITE_DEBUG
18161   GLOBAL(int, mutexIsInit) = 0;
18162 #endif
18163 
18164   return rc;
18165 }
18166 
18167 /*
18168 ** Retrieve a pointer to a static mutex or allocate a new dynamic one.
18169 */
18170 SQLITE_API sqlite3_mutex *sqlite3_mutex_alloc(int id){
18171 #ifndef SQLITE_OMIT_AUTOINIT
18172   if( sqlite3_initialize() ) return 0;
18173 #endif
18174   return sqlite3GlobalConfig.mutex.xMutexAlloc(id);
18175 }
18176 
18177 SQLITE_PRIVATE sqlite3_mutex *sqlite3MutexAlloc(int id){
18178   if( !sqlite3GlobalConfig.bCoreMutex ){
18179     return 0;
18180   }
18181   assert( GLOBAL(int, mutexIsInit) );
18182   return sqlite3GlobalConfig.mutex.xMutexAlloc(id);
18183 }
18184 
18185 /*
18186 ** Free a dynamic mutex.
18187 */
18188 SQLITE_API void sqlite3_mutex_free(sqlite3_mutex *p){
18189   if( p ){
18190     sqlite3GlobalConfig.mutex.xMutexFree(p);
18191   }
18192 }
18193 
18194 /*
18195 ** Obtain the mutex p. If some other thread already has the mutex, block
18196 ** until it can be obtained.
18197 */
18198 SQLITE_API void sqlite3_mutex_enter(sqlite3_mutex *p){
18199   if( p ){
18200     sqlite3GlobalConfig.mutex.xMutexEnter(p);
18201   }
18202 }
18203 
18204 /*
18205 ** Obtain the mutex p. If successful, return SQLITE_OK. Otherwise, if another
18206 ** thread holds the mutex and it cannot be obtained, return SQLITE_BUSY.
18207 */
18208 SQLITE_API int sqlite3_mutex_try(sqlite3_mutex *p){
18209   int rc = SQLITE_OK;
18210   if( p ){
18211     return sqlite3GlobalConfig.mutex.xMutexTry(p);
18212   }
18213   return rc;
18214 }
18215 
18216 /*
18217 ** The sqlite3_mutex_leave() routine exits a mutex that was previously
18218 ** entered by the same thread.  The behavior is undefined if the mutex
18219 ** is not currently entered. If a NULL pointer is passed as an argument
18220 ** this function is a no-op.
18221 */
18222 SQLITE_API void sqlite3_mutex_leave(sqlite3_mutex *p){
18223   if( p ){
18224     sqlite3GlobalConfig.mutex.xMutexLeave(p);
18225   }
18226 }
18227 
18228 #ifndef NDEBUG
18229 /*
18230 ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
18231 ** intended for use inside assert() statements.
18232 */
18233 SQLITE_API int sqlite3_mutex_held(sqlite3_mutex *p){
18234   return p==0 || sqlite3GlobalConfig.mutex.xMutexHeld(p);
18235 }
18236 SQLITE_API int sqlite3_mutex_notheld(sqlite3_mutex *p){
18237   return p==0 || sqlite3GlobalConfig.mutex.xMutexNotheld(p);
18238 }
18239 #endif
18240 
18241 #endif /* !defined(SQLITE_MUTEX_OMIT) */
18242 
18243 /************** End of mutex.c ***********************************************/
18244 /************** Begin file mutex_noop.c **************************************/
18245 /*
18246 ** 2008 October 07
18247 **
18248 ** The author disclaims copyright to this source code.  In place of
18249 ** a legal notice, here is a blessing:
18250 **
18251 **    May you do good and not evil.
18252 **    May you find forgiveness for yourself and forgive others.
18253 **    May you share freely, never taking more than you give.
18254 **
18255 *************************************************************************
18256 ** This file contains the C functions that implement mutexes.
18257 **
18258 ** This implementation in this file does not provide any mutual
18259 ** exclusion and is thus suitable for use only in applications
18260 ** that use SQLite in a single thread.  The routines defined
18261 ** here are place-holders.  Applications can substitute working
18262 ** mutex routines at start-time using the
18263 **
18264 **     sqlite3_config(SQLITE_CONFIG_MUTEX,...)
18265 **
18266 ** interface.
18267 **
18268 ** If compiled with SQLITE_DEBUG, then additional logic is inserted
18269 ** that does error checking on mutexes to make sure they are being
18270 ** called correctly.
18271 */
18272 
18273 #ifndef SQLITE_MUTEX_OMIT
18274 
18275 #ifndef SQLITE_DEBUG
18276 /*
18277 ** Stub routines for all mutex methods.
18278 **
18279 ** This routines provide no mutual exclusion or error checking.
18280 */
18281 static int noopMutexInit(void){ return SQLITE_OK; }
18282 static int noopMutexEnd(void){ return SQLITE_OK; }
18283 static sqlite3_mutex *noopMutexAlloc(int id){
18284   UNUSED_PARAMETER(id);
18285   return (sqlite3_mutex*)8;
18286 }
18287 static void noopMutexFree(sqlite3_mutex *p){ UNUSED_PARAMETER(p); return; }
18288 static void noopMutexEnter(sqlite3_mutex *p){ UNUSED_PARAMETER(p); return; }
18289 static int noopMutexTry(sqlite3_mutex *p){
18290   UNUSED_PARAMETER(p);
18291   return SQLITE_OK;
18292 }
18293 static void noopMutexLeave(sqlite3_mutex *p){ UNUSED_PARAMETER(p); return; }
18294 
18295 SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3NoopMutex(void){
18296   static const sqlite3_mutex_methods sMutex = {
18297     noopMutexInit,
18298     noopMutexEnd,
18299     noopMutexAlloc,
18300     noopMutexFree,
18301     noopMutexEnter,
18302     noopMutexTry,
18303     noopMutexLeave,
18304 
18305     0,
18306     0,
18307   };
18308 
18309   return &sMutex;
18310 }
18311 #endif /* !SQLITE_DEBUG */
18312 
18313 #ifdef SQLITE_DEBUG
18314 /*
18315 ** In this implementation, error checking is provided for testing
18316 ** and debugging purposes.  The mutexes still do not provide any
18317 ** mutual exclusion.
18318 */
18319 
18320 /*
18321 ** The mutex object
18322 */
18323 typedef struct sqlite3_debug_mutex {
18324   int id;     /* The mutex type */
18325   int cnt;    /* Number of entries without a matching leave */
18326 } sqlite3_debug_mutex;
18327 
18328 /*
18329 ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
18330 ** intended for use inside assert() statements.
18331 */
18332 static int debugMutexHeld(sqlite3_mutex *pX){
18333   sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
18334   return p==0 || p->cnt>0;
18335 }
18336 static int debugMutexNotheld(sqlite3_mutex *pX){
18337   sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
18338   return p==0 || p->cnt==0;
18339 }
18340 
18341 /*
18342 ** Initialize and deinitialize the mutex subsystem.
18343 */
18344 static int debugMutexInit(void){ return SQLITE_OK; }
18345 static int debugMutexEnd(void){ return SQLITE_OK; }
18346 
18347 /*
18348 ** The sqlite3_mutex_alloc() routine allocates a new
18349 ** mutex and returns a pointer to it.  If it returns NULL
18350 ** that means that a mutex could not be allocated.
18351 */
18352 static sqlite3_mutex *debugMutexAlloc(int id){
18353   static sqlite3_debug_mutex aStatic[6];
18354   sqlite3_debug_mutex *pNew = 0;
18355   switch( id ){
18356     case SQLITE_MUTEX_FAST:
18357     case SQLITE_MUTEX_RECURSIVE: {
18358       pNew = sqlite3Malloc(sizeof(*pNew));
18359       if( pNew ){
18360         pNew->id = id;
18361         pNew->cnt = 0;
18362       }
18363       break;
18364     }
18365     default: {
18366       assert( id-2 >= 0 );
18367       assert( id-2 < (int)(sizeof(aStatic)/sizeof(aStatic[0])) );
18368       pNew = &aStatic[id-2];
18369       pNew->id = id;
18370       break;
18371     }
18372   }
18373   return (sqlite3_mutex*)pNew;
18374 }
18375 
18376 /*
18377 ** This routine deallocates a previously allocated mutex.
18378 */
18379 static void debugMutexFree(sqlite3_mutex *pX){
18380   sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
18381   assert( p->cnt==0 );
18382   assert( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE );
18383   sqlite3_free(p);
18384 }
18385 
18386 /*
18387 ** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
18388 ** to enter a mutex.  If another thread is already within the mutex,
18389 ** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
18390 ** SQLITE_BUSY.  The sqlite3_mutex_try() interface returns SQLITE_OK
18391 ** upon successful entry.  Mutexes created using SQLITE_MUTEX_RECURSIVE can
18392 ** be entered multiple times by the same thread.  In such cases the,
18393 ** mutex must be exited an equal number of times before another thread
18394 ** can enter.  If the same thread tries to enter any other kind of mutex
18395 ** more than once, the behavior is undefined.
18396 */
18397 static void debugMutexEnter(sqlite3_mutex *pX){
18398   sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
18399   assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) );
18400   p->cnt++;
18401 }
18402 static int debugMutexTry(sqlite3_mutex *pX){
18403   sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
18404   assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) );
18405   p->cnt++;
18406   return SQLITE_OK;
18407 }
18408 
18409 /*
18410 ** The sqlite3_mutex_leave() routine exits a mutex that was
18411 ** previously entered by the same thread.  The behavior
18412 ** is undefined if the mutex is not currently entered or
18413 ** is not currently allocated.  SQLite will never do either.
18414 */
18415 static void debugMutexLeave(sqlite3_mutex *pX){
18416   sqlite3_debug_mutex *p = (sqlite3_debug_mutex*)pX;
18417   assert( debugMutexHeld(pX) );
18418   p->cnt--;
18419   assert( p->id==SQLITE_MUTEX_RECURSIVE || debugMutexNotheld(pX) );
18420 }
18421 
18422 SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3NoopMutex(void){
18423   static const sqlite3_mutex_methods sMutex = {
18424     debugMutexInit,
18425     debugMutexEnd,
18426     debugMutexAlloc,
18427     debugMutexFree,
18428     debugMutexEnter,
18429     debugMutexTry,
18430     debugMutexLeave,
18431 
18432     debugMutexHeld,
18433     debugMutexNotheld
18434   };
18435 
18436   return &sMutex;
18437 }
18438 #endif /* SQLITE_DEBUG */
18439 
18440 /*
18441 ** If compiled with SQLITE_MUTEX_NOOP, then the no-op mutex implementation
18442 ** is used regardless of the run-time threadsafety setting.
18443 */
18444 #ifdef SQLITE_MUTEX_NOOP
18445 SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
18446   return sqlite3NoopMutex();
18447 }
18448 #endif /* defined(SQLITE_MUTEX_NOOP) */
18449 #endif /* !defined(SQLITE_MUTEX_OMIT) */
18450 
18451 /************** End of mutex_noop.c ******************************************/
18452 /************** Begin file mutex_unix.c **************************************/
18453 /*
18454 ** 2007 August 28
18455 **
18456 ** The author disclaims copyright to this source code.  In place of
18457 ** a legal notice, here is a blessing:
18458 **
18459 **    May you do good and not evil.
18460 **    May you find forgiveness for yourself and forgive others.
18461 **    May you share freely, never taking more than you give.
18462 **
18463 *************************************************************************
18464 ** This file contains the C functions that implement mutexes for pthreads
18465 */
18466 
18467 /*
18468 ** The code in this file is only used if we are compiling threadsafe
18469 ** under unix with pthreads.
18470 **
18471 ** Note that this implementation requires a version of pthreads that
18472 ** supports recursive mutexes.
18473 */
18474 #ifdef SQLITE_MUTEX_PTHREADS
18475 
18476 #include <pthread.h>
18477 
18478 /*
18479 ** The sqlite3_mutex.id, sqlite3_mutex.nRef, and sqlite3_mutex.owner fields
18480 ** are necessary under two condidtions:  (1) Debug builds and (2) using
18481 ** home-grown mutexes.  Encapsulate these conditions into a single #define.
18482 */
18483 #if defined(SQLITE_DEBUG) || defined(SQLITE_HOMEGROWN_RECURSIVE_MUTEX)
18484 # define SQLITE_MUTEX_NREF 1
18485 #else
18486 # define SQLITE_MUTEX_NREF 0
18487 #endif
18488 
18489 /*
18490 ** Each recursive mutex is an instance of the following structure.
18491 */
18492 struct sqlite3_mutex {
18493   pthread_mutex_t mutex;     /* Mutex controlling the lock */
18494 #if SQLITE_MUTEX_NREF
18495   int id;                    /* Mutex type */
18496   volatile int nRef;         /* Number of entrances */
18497   volatile pthread_t owner;  /* Thread that is within this mutex */
18498   int trace;                 /* True to trace changes */
18499 #endif
18500 };
18501 #if SQLITE_MUTEX_NREF
18502 #define SQLITE3_MUTEX_INITIALIZER { PTHREAD_MUTEX_INITIALIZER, 0, 0, (pthread_t)0, 0 }
18503 #else
18504 #define SQLITE3_MUTEX_INITIALIZER { PTHREAD_MUTEX_INITIALIZER }
18505 #endif
18506 
18507 /*
18508 ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
18509 ** intended for use only inside assert() statements.  On some platforms,
18510 ** there might be race conditions that can cause these routines to
18511 ** deliver incorrect results.  In particular, if pthread_equal() is
18512 ** not an atomic operation, then these routines might delivery
18513 ** incorrect results.  On most platforms, pthread_equal() is a
18514 ** comparison of two integers and is therefore atomic.  But we are
18515 ** told that HPUX is not such a platform.  If so, then these routines
18516 ** will not always work correctly on HPUX.
18517 **
18518 ** On those platforms where pthread_equal() is not atomic, SQLite
18519 ** should be compiled without -DSQLITE_DEBUG and with -DNDEBUG to
18520 ** make sure no assert() statements are evaluated and hence these
18521 ** routines are never called.
18522 */
18523 #if !defined(NDEBUG) || defined(SQLITE_DEBUG)
18524 static int pthreadMutexHeld(sqlite3_mutex *p){
18525   return (p->nRef!=0 && pthread_equal(p->owner, pthread_self()));
18526 }
18527 static int pthreadMutexNotheld(sqlite3_mutex *p){
18528   return p->nRef==0 || pthread_equal(p->owner, pthread_self())==0;
18529 }
18530 #endif
18531 
18532 /*
18533 ** Initialize and deinitialize the mutex subsystem.
18534 */
18535 static int pthreadMutexInit(void){ return SQLITE_OK; }
18536 static int pthreadMutexEnd(void){ return SQLITE_OK; }
18537 
18538 /*
18539 ** The sqlite3_mutex_alloc() routine allocates a new
18540 ** mutex and returns a pointer to it.  If it returns NULL
18541 ** that means that a mutex could not be allocated.  SQLite
18542 ** will unwind its stack and return an error.  The argument
18543 ** to sqlite3_mutex_alloc() is one of these integer constants:
18544 **
18545 ** <ul>
18546 ** <li>  SQLITE_MUTEX_FAST
18547 ** <li>  SQLITE_MUTEX_RECURSIVE
18548 ** <li>  SQLITE_MUTEX_STATIC_MASTER
18549 ** <li>  SQLITE_MUTEX_STATIC_MEM
18550 ** <li>  SQLITE_MUTEX_STATIC_MEM2
18551 ** <li>  SQLITE_MUTEX_STATIC_PRNG
18552 ** <li>  SQLITE_MUTEX_STATIC_LRU
18553 ** <li>  SQLITE_MUTEX_STATIC_PMEM
18554 ** </ul>
18555 **
18556 ** The first two constants cause sqlite3_mutex_alloc() to create
18557 ** a new mutex.  The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
18558 ** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
18559 ** The mutex implementation does not need to make a distinction
18560 ** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does
18561 ** not want to.  But SQLite will only request a recursive mutex in
18562 ** cases where it really needs one.  If a faster non-recursive mutex
18563 ** implementation is available on the host platform, the mutex subsystem
18564 ** might return such a mutex in response to SQLITE_MUTEX_FAST.
18565 **
18566 ** The other allowed parameters to sqlite3_mutex_alloc() each return
18567 ** a pointer to a static preexisting mutex.  Six static mutexes are
18568 ** used by the current version of SQLite.  Future versions of SQLite
18569 ** may add additional static mutexes.  Static mutexes are for internal
18570 ** use by SQLite only.  Applications that use SQLite mutexes should
18571 ** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or
18572 ** SQLITE_MUTEX_RECURSIVE.
18573 **
18574 ** Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
18575 ** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
18576 ** returns a different mutex on every call.  But for the static
18577 ** mutex types, the same mutex is returned on every call that has
18578 ** the same type number.
18579 */
18580 static sqlite3_mutex *pthreadMutexAlloc(int iType){
18581   static sqlite3_mutex staticMutexes[] = {
18582     SQLITE3_MUTEX_INITIALIZER,
18583     SQLITE3_MUTEX_INITIALIZER,
18584     SQLITE3_MUTEX_INITIALIZER,
18585     SQLITE3_MUTEX_INITIALIZER,
18586     SQLITE3_MUTEX_INITIALIZER,
18587     SQLITE3_MUTEX_INITIALIZER
18588   };
18589   sqlite3_mutex *p;
18590   switch( iType ){
18591     case SQLITE_MUTEX_RECURSIVE: {
18592       p = sqlite3MallocZero( sizeof(*p) );
18593       if( p ){
18594 #ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
18595         /* If recursive mutexes are not available, we will have to
18596         ** build our own.  See below. */
18597         pthread_mutex_init(&p->mutex, 0);
18598 #else
18599         /* Use a recursive mutex if it is available */
18600         pthread_mutexattr_t recursiveAttr;
18601         pthread_mutexattr_init(&recursiveAttr);
18602         pthread_mutexattr_settype(&recursiveAttr, PTHREAD_MUTEX_RECURSIVE);
18603         pthread_mutex_init(&p->mutex, &recursiveAttr);
18604         pthread_mutexattr_destroy(&recursiveAttr);
18605 #endif
18606 #if SQLITE_MUTEX_NREF
18607         p->id = iType;
18608 #endif
18609       }
18610       break;
18611     }
18612     case SQLITE_MUTEX_FAST: {
18613       p = sqlite3MallocZero( sizeof(*p) );
18614       if( p ){
18615 #if SQLITE_MUTEX_NREF
18616         p->id = iType;
18617 #endif
18618         pthread_mutex_init(&p->mutex, 0);
18619       }
18620       break;
18621     }
18622     default: {
18623       assert( iType-2 >= 0 );
18624       assert( iType-2 < ArraySize(staticMutexes) );
18625       p = &staticMutexes[iType-2];
18626 #if SQLITE_MUTEX_NREF
18627       p->id = iType;
18628 #endif
18629       break;
18630     }
18631   }
18632   return p;
18633 }
18634 
18635 
18636 /*
18637 ** This routine deallocates a previously
18638 ** allocated mutex.  SQLite is careful to deallocate every
18639 ** mutex that it allocates.
18640 */
18641 static void pthreadMutexFree(sqlite3_mutex *p){
18642   assert( p->nRef==0 );
18643   assert( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE );
18644   pthread_mutex_destroy(&p->mutex);
18645   sqlite3_free(p);
18646 }
18647 
18648 /*
18649 ** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
18650 ** to enter a mutex.  If another thread is already within the mutex,
18651 ** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
18652 ** SQLITE_BUSY.  The sqlite3_mutex_try() interface returns SQLITE_OK
18653 ** upon successful entry.  Mutexes created using SQLITE_MUTEX_RECURSIVE can
18654 ** be entered multiple times by the same thread.  In such cases the,
18655 ** mutex must be exited an equal number of times before another thread
18656 ** can enter.  If the same thread tries to enter any other kind of mutex
18657 ** more than once, the behavior is undefined.
18658 */
18659 static void pthreadMutexEnter(sqlite3_mutex *p){
18660   assert( p->id==SQLITE_MUTEX_RECURSIVE || pthreadMutexNotheld(p) );
18661 
18662 #ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
18663   /* If recursive mutexes are not available, then we have to grow
18664   ** our own.  This implementation assumes that pthread_equal()
18665   ** is atomic - that it cannot be deceived into thinking self
18666   ** and p->owner are equal if p->owner changes between two values
18667   ** that are not equal to self while the comparison is taking place.
18668   ** This implementation also assumes a coherent cache - that
18669   ** separate processes cannot read different values from the same
18670   ** address at the same time.  If either of these two conditions
18671   ** are not met, then the mutexes will fail and problems will result.
18672   */
18673   {
18674     pthread_t self = pthread_self();
18675     if( p->nRef>0 && pthread_equal(p->owner, self) ){
18676       p->nRef++;
18677     }else{
18678       pthread_mutex_lock(&p->mutex);
18679       assert( p->nRef==0 );
18680       p->owner = self;
18681       p->nRef = 1;
18682     }
18683   }
18684 #else
18685   /* Use the built-in recursive mutexes if they are available.
18686   */
18687   pthread_mutex_lock(&p->mutex);
18688 #if SQLITE_MUTEX_NREF
18689   assert( p->nRef>0 || p->owner==0 );
18690   p->owner = pthread_self();
18691   p->nRef++;
18692 #endif
18693 #endif
18694 
18695 #ifdef SQLITE_DEBUG
18696   if( p->trace ){
18697     printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
18698   }
18699 #endif
18700 }
18701 static int pthreadMutexTry(sqlite3_mutex *p){
18702   int rc;
18703   assert( p->id==SQLITE_MUTEX_RECURSIVE || pthreadMutexNotheld(p) );
18704 
18705 #ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
18706   /* If recursive mutexes are not available, then we have to grow
18707   ** our own.  This implementation assumes that pthread_equal()
18708   ** is atomic - that it cannot be deceived into thinking self
18709   ** and p->owner are equal if p->owner changes between two values
18710   ** that are not equal to self while the comparison is taking place.
18711   ** This implementation also assumes a coherent cache - that
18712   ** separate processes cannot read different values from the same
18713   ** address at the same time.  If either of these two conditions
18714   ** are not met, then the mutexes will fail and problems will result.
18715   */
18716   {
18717     pthread_t self = pthread_self();
18718     if( p->nRef>0 && pthread_equal(p->owner, self) ){
18719       p->nRef++;
18720       rc = SQLITE_OK;
18721     }else if( pthread_mutex_trylock(&p->mutex)==0 ){
18722       assert( p->nRef==0 );
18723       p->owner = self;
18724       p->nRef = 1;
18725       rc = SQLITE_OK;
18726     }else{
18727       rc = SQLITE_BUSY;
18728     }
18729   }
18730 #else
18731   /* Use the built-in recursive mutexes if they are available.
18732   */
18733   if( pthread_mutex_trylock(&p->mutex)==0 ){
18734 #if SQLITE_MUTEX_NREF
18735     p->owner = pthread_self();
18736     p->nRef++;
18737 #endif
18738     rc = SQLITE_OK;
18739   }else{
18740     rc = SQLITE_BUSY;
18741   }
18742 #endif
18743 
18744 #ifdef SQLITE_DEBUG
18745   if( rc==SQLITE_OK && p->trace ){
18746     printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
18747   }
18748 #endif
18749   return rc;
18750 }
18751 
18752 /*
18753 ** The sqlite3_mutex_leave() routine exits a mutex that was
18754 ** previously entered by the same thread.  The behavior
18755 ** is undefined if the mutex is not currently entered or
18756 ** is not currently allocated.  SQLite will never do either.
18757 */
18758 static void pthreadMutexLeave(sqlite3_mutex *p){
18759   assert( pthreadMutexHeld(p) );
18760 #if SQLITE_MUTEX_NREF
18761   p->nRef--;
18762   if( p->nRef==0 ) p->owner = 0;
18763 #endif
18764   assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE );
18765 
18766 #ifdef SQLITE_HOMEGROWN_RECURSIVE_MUTEX
18767   if( p->nRef==0 ){
18768     pthread_mutex_unlock(&p->mutex);
18769   }
18770 #else
18771   pthread_mutex_unlock(&p->mutex);
18772 #endif
18773 
18774 #ifdef SQLITE_DEBUG
18775   if( p->trace ){
18776     printf("leave mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
18777   }
18778 #endif
18779 }
18780 
18781 SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
18782   static const sqlite3_mutex_methods sMutex = {
18783     pthreadMutexInit,
18784     pthreadMutexEnd,
18785     pthreadMutexAlloc,
18786     pthreadMutexFree,
18787     pthreadMutexEnter,
18788     pthreadMutexTry,
18789     pthreadMutexLeave,
18790 #ifdef SQLITE_DEBUG
18791     pthreadMutexHeld,
18792     pthreadMutexNotheld
18793 #else
18794     0,
18795     0
18796 #endif
18797   };
18798 
18799   return &sMutex;
18800 }
18801 
18802 #endif /* SQLITE_MUTEX_PTHREADS */
18803 
18804 /************** End of mutex_unix.c ******************************************/
18805 /************** Begin file mutex_w32.c ***************************************/
18806 /*
18807 ** 2007 August 14
18808 **
18809 ** The author disclaims copyright to this source code.  In place of
18810 ** a legal notice, here is a blessing:
18811 **
18812 **    May you do good and not evil.
18813 **    May you find forgiveness for yourself and forgive others.
18814 **    May you share freely, never taking more than you give.
18815 **
18816 *************************************************************************
18817 ** This file contains the C functions that implement mutexes for win32
18818 */
18819 
18820 /*
18821 ** The code in this file is only used if we are compiling multithreaded
18822 ** on a win32 system.
18823 */
18824 #ifdef SQLITE_MUTEX_W32
18825 
18826 /*
18827 ** Each recursive mutex is an instance of the following structure.
18828 */
18829 struct sqlite3_mutex {
18830   CRITICAL_SECTION mutex;    /* Mutex controlling the lock */
18831   int id;                    /* Mutex type */
18832 #ifdef SQLITE_DEBUG
18833   volatile int nRef;         /* Number of enterances */
18834   volatile DWORD owner;      /* Thread holding this mutex */
18835   int trace;                 /* True to trace changes */
18836 #endif
18837 };
18838 #define SQLITE_W32_MUTEX_INITIALIZER { 0 }
18839 #ifdef SQLITE_DEBUG
18840 #define SQLITE3_MUTEX_INITIALIZER { SQLITE_W32_MUTEX_INITIALIZER, 0, 0L, (DWORD)0, 0 }
18841 #else
18842 #define SQLITE3_MUTEX_INITIALIZER { SQLITE_W32_MUTEX_INITIALIZER, 0 }
18843 #endif
18844 
18845 /*
18846 ** Return true (non-zero) if we are running under WinNT, Win2K, WinXP,
18847 ** or WinCE.  Return false (zero) for Win95, Win98, or WinME.
18848 **
18849 ** Here is an interesting observation:  Win95, Win98, and WinME lack
18850 ** the LockFileEx() API.  But we can still statically link against that
18851 ** API as long as we don't call it win running Win95/98/ME.  A call to
18852 ** this routine is used to determine if the host is Win95/98/ME or
18853 ** WinNT/2K/XP so that we will know whether or not we can safely call
18854 ** the LockFileEx() API.
18855 **
18856 ** mutexIsNT() is only used for the TryEnterCriticalSection() API call,
18857 ** which is only available if your application was compiled with
18858 ** _WIN32_WINNT defined to a value >= 0x0400.  Currently, the only
18859 ** call to TryEnterCriticalSection() is #ifdef'ed out, so #ifdef
18860 ** this out as well.
18861 */
18862 #if 0
18863 #if SQLITE_OS_WINCE || SQLITE_OS_WINRT
18864 # define mutexIsNT()  (1)
18865 #else
18866   static int mutexIsNT(void){
18867     static int osType = 0;
18868     if( osType==0 ){
18869       OSVERSIONINFO sInfo;
18870       sInfo.dwOSVersionInfoSize = sizeof(sInfo);
18871       GetVersionEx(&sInfo);
18872       osType = sInfo.dwPlatformId==VER_PLATFORM_WIN32_NT ? 2 : 1;
18873     }
18874     return osType==2;
18875   }
18876 #endif /* SQLITE_OS_WINCE || SQLITE_OS_WINRT */
18877 #endif
18878 
18879 #ifdef SQLITE_DEBUG
18880 /*
18881 ** The sqlite3_mutex_held() and sqlite3_mutex_notheld() routine are
18882 ** intended for use only inside assert() statements.
18883 */
18884 static int winMutexHeld(sqlite3_mutex *p){
18885   return p->nRef!=0 && p->owner==GetCurrentThreadId();
18886 }
18887 static int winMutexNotheld2(sqlite3_mutex *p, DWORD tid){
18888   return p->nRef==0 || p->owner!=tid;
18889 }
18890 static int winMutexNotheld(sqlite3_mutex *p){
18891   DWORD tid = GetCurrentThreadId();
18892   return winMutexNotheld2(p, tid);
18893 }
18894 #endif
18895 
18896 
18897 /*
18898 ** Initialize and deinitialize the mutex subsystem.
18899 */
18900 static sqlite3_mutex winMutex_staticMutexes[6] = {
18901   SQLITE3_MUTEX_INITIALIZER,
18902   SQLITE3_MUTEX_INITIALIZER,
18903   SQLITE3_MUTEX_INITIALIZER,
18904   SQLITE3_MUTEX_INITIALIZER,
18905   SQLITE3_MUTEX_INITIALIZER,
18906   SQLITE3_MUTEX_INITIALIZER
18907 };
18908 static int winMutex_isInit = 0;
18909 /* As winMutexInit() and winMutexEnd() are called as part
18910 ** of the sqlite3_initialize and sqlite3_shutdown()
18911 ** processing, the "interlocked" magic is probably not
18912 ** strictly necessary.
18913 */
18914 static LONG winMutex_lock = 0;
18915 
18916 SQLITE_API void sqlite3_win32_sleep(DWORD milliseconds); /* os_win.c */
18917 
18918 static int winMutexInit(void){
18919   /* The first to increment to 1 does actual initialization */
18920   if( InterlockedCompareExchange(&winMutex_lock, 1, 0)==0 ){
18921     int i;
18922     for(i=0; i<ArraySize(winMutex_staticMutexes); i++){
18923 #if SQLITE_OS_WINRT
18924       InitializeCriticalSectionEx(&winMutex_staticMutexes[i].mutex, 0, 0);
18925 #else
18926       InitializeCriticalSection(&winMutex_staticMutexes[i].mutex);
18927 #endif
18928     }
18929     winMutex_isInit = 1;
18930   }else{
18931     /* Someone else is in the process of initing the static mutexes */
18932     while( !winMutex_isInit ){
18933       sqlite3_win32_sleep(1);
18934     }
18935   }
18936   return SQLITE_OK;
18937 }
18938 
18939 static int winMutexEnd(void){
18940   /* The first to decrement to 0 does actual shutdown
18941   ** (which should be the last to shutdown.) */
18942   if( InterlockedCompareExchange(&winMutex_lock, 0, 1)==1 ){
18943     if( winMutex_isInit==1 ){
18944       int i;
18945       for(i=0; i<ArraySize(winMutex_staticMutexes); i++){
18946         DeleteCriticalSection(&winMutex_staticMutexes[i].mutex);
18947       }
18948       winMutex_isInit = 0;
18949     }
18950   }
18951   return SQLITE_OK;
18952 }
18953 
18954 /*
18955 ** The sqlite3_mutex_alloc() routine allocates a new
18956 ** mutex and returns a pointer to it.  If it returns NULL
18957 ** that means that a mutex could not be allocated.  SQLite
18958 ** will unwind its stack and return an error.  The argument
18959 ** to sqlite3_mutex_alloc() is one of these integer constants:
18960 **
18961 ** <ul>
18962 ** <li>  SQLITE_MUTEX_FAST
18963 ** <li>  SQLITE_MUTEX_RECURSIVE
18964 ** <li>  SQLITE_MUTEX_STATIC_MASTER
18965 ** <li>  SQLITE_MUTEX_STATIC_MEM
18966 ** <li>  SQLITE_MUTEX_STATIC_MEM2
18967 ** <li>  SQLITE_MUTEX_STATIC_PRNG
18968 ** <li>  SQLITE_MUTEX_STATIC_LRU
18969 ** <li>  SQLITE_MUTEX_STATIC_PMEM
18970 ** </ul>
18971 **
18972 ** The first two constants cause sqlite3_mutex_alloc() to create
18973 ** a new mutex.  The new mutex is recursive when SQLITE_MUTEX_RECURSIVE
18974 ** is used but not necessarily so when SQLITE_MUTEX_FAST is used.
18975 ** The mutex implementation does not need to make a distinction
18976 ** between SQLITE_MUTEX_RECURSIVE and SQLITE_MUTEX_FAST if it does
18977 ** not want to.  But SQLite will only request a recursive mutex in
18978 ** cases where it really needs one.  If a faster non-recursive mutex
18979 ** implementation is available on the host platform, the mutex subsystem
18980 ** might return such a mutex in response to SQLITE_MUTEX_FAST.
18981 **
18982 ** The other allowed parameters to sqlite3_mutex_alloc() each return
18983 ** a pointer to a static preexisting mutex.  Six static mutexes are
18984 ** used by the current version of SQLite.  Future versions of SQLite
18985 ** may add additional static mutexes.  Static mutexes are for internal
18986 ** use by SQLite only.  Applications that use SQLite mutexes should
18987 ** use only the dynamic mutexes returned by SQLITE_MUTEX_FAST or
18988 ** SQLITE_MUTEX_RECURSIVE.
18989 **
18990 ** Note that if one of the dynamic mutex parameters (SQLITE_MUTEX_FAST
18991 ** or SQLITE_MUTEX_RECURSIVE) is used then sqlite3_mutex_alloc()
18992 ** returns a different mutex on every call.  But for the static
18993 ** mutex types, the same mutex is returned on every call that has
18994 ** the same type number.
18995 */
18996 static sqlite3_mutex *winMutexAlloc(int iType){
18997   sqlite3_mutex *p;
18998 
18999   switch( iType ){
19000     case SQLITE_MUTEX_FAST:
19001     case SQLITE_MUTEX_RECURSIVE: {
19002       p = sqlite3MallocZero( sizeof(*p) );
19003       if( p ){
19004 #ifdef SQLITE_DEBUG
19005         p->id = iType;
19006 #endif
19007 #if SQLITE_OS_WINRT
19008         InitializeCriticalSectionEx(&p->mutex, 0, 0);
19009 #else
19010         InitializeCriticalSection(&p->mutex);
19011 #endif
19012       }
19013       break;
19014     }
19015     default: {
19016       assert( winMutex_isInit==1 );
19017       assert( iType-2 >= 0 );
19018       assert( iType-2 < ArraySize(winMutex_staticMutexes) );
19019       p = &winMutex_staticMutexes[iType-2];
19020 #ifdef SQLITE_DEBUG
19021       p->id = iType;
19022 #endif
19023       break;
19024     }
19025   }
19026   return p;
19027 }
19028 
19029 
19030 /*
19031 ** This routine deallocates a previously
19032 ** allocated mutex.  SQLite is careful to deallocate every
19033 ** mutex that it allocates.
19034 */
19035 static void winMutexFree(sqlite3_mutex *p){
19036   assert( p );
19037   assert( p->nRef==0 && p->owner==0 );
19038   assert( p->id==SQLITE_MUTEX_FAST || p->id==SQLITE_MUTEX_RECURSIVE );
19039   DeleteCriticalSection(&p->mutex);
19040   sqlite3_free(p);
19041 }
19042 
19043 /*
19044 ** The sqlite3_mutex_enter() and sqlite3_mutex_try() routines attempt
19045 ** to enter a mutex.  If another thread is already within the mutex,
19046 ** sqlite3_mutex_enter() will block and sqlite3_mutex_try() will return
19047 ** SQLITE_BUSY.  The sqlite3_mutex_try() interface returns SQLITE_OK
19048 ** upon successful entry.  Mutexes created using SQLITE_MUTEX_RECURSIVE can
19049 ** be entered multiple times by the same thread.  In such cases the,
19050 ** mutex must be exited an equal number of times before another thread
19051 ** can enter.  If the same thread tries to enter any other kind of mutex
19052 ** more than once, the behavior is undefined.
19053 */
19054 static void winMutexEnter(sqlite3_mutex *p){
19055 #ifdef SQLITE_DEBUG
19056   DWORD tid = GetCurrentThreadId();
19057   assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld2(p, tid) );
19058 #endif
19059   EnterCriticalSection(&p->mutex);
19060 #ifdef SQLITE_DEBUG
19061   assert( p->nRef>0 || p->owner==0 );
19062   p->owner = tid;
19063   p->nRef++;
19064   if( p->trace ){
19065     printf("enter mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
19066   }
19067 #endif
19068 }
19069 static int winMutexTry(sqlite3_mutex *p){
19070 #ifndef NDEBUG
19071   DWORD tid = GetCurrentThreadId();
19072 #endif
19073   int rc = SQLITE_BUSY;
19074   assert( p->id==SQLITE_MUTEX_RECURSIVE || winMutexNotheld2(p, tid) );
19075   /*
19076   ** The sqlite3_mutex_try() routine is very rarely used, and when it
19077   ** is used it is merely an optimization.  So it is OK for it to always
19078   ** fail.
19079   **
19080   ** The TryEnterCriticalSection() interface is only available on WinNT.
19081   ** And some windows compilers complain if you try to use it without
19082   ** first doing some #defines that prevent SQLite from building on Win98.
19083   ** For that reason, we will omit this optimization for now.  See
19084   ** ticket #2685.
19085   */
19086 #if 0
19087   if( mutexIsNT() && TryEnterCriticalSection(&p->mutex) ){
19088     p->owner = tid;
19089     p->nRef++;
19090     rc = SQLITE_OK;
19091   }
19092 #else
19093   UNUSED_PARAMETER(p);
19094 #endif
19095 #ifdef SQLITE_DEBUG
19096   if( rc==SQLITE_OK && p->trace ){
19097     printf("try mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
19098   }
19099 #endif
19100   return rc;
19101 }
19102 
19103 /*
19104 ** The sqlite3_mutex_leave() routine exits a mutex that was
19105 ** previously entered by the same thread.  The behavior
19106 ** is undefined if the mutex is not currently entered or
19107 ** is not currently allocated.  SQLite will never do either.
19108 */
19109 static void winMutexLeave(sqlite3_mutex *p){
19110 #ifndef NDEBUG
19111   DWORD tid = GetCurrentThreadId();
19112   assert( p->nRef>0 );
19113   assert( p->owner==tid );
19114   p->nRef--;
19115   if( p->nRef==0 ) p->owner = 0;
19116   assert( p->nRef==0 || p->id==SQLITE_MUTEX_RECURSIVE );
19117 #endif
19118   LeaveCriticalSection(&p->mutex);
19119 #ifdef SQLITE_DEBUG
19120   if( p->trace ){
19121     printf("leave mutex %p (%d) with nRef=%d\n", p, p->trace, p->nRef);
19122   }
19123 #endif
19124 }
19125 
19126 SQLITE_PRIVATE sqlite3_mutex_methods const *sqlite3DefaultMutex(void){
19127   static const sqlite3_mutex_methods sMutex = {
19128     winMutexInit,
19129     winMutexEnd,
19130     winMutexAlloc,
19131     winMutexFree,
19132     winMutexEnter,
19133     winMutexTry,
19134     winMutexLeave,
19135 #ifdef SQLITE_DEBUG
19136     winMutexHeld,
19137     winMutexNotheld
19138 #else
19139     0,
19140     0
19141 #endif
19142   };
19143 
19144   return &sMutex;
19145 }
19146 #endif /* SQLITE_MUTEX_W32 */
19147 
19148 /************** End of mutex_w32.c *******************************************/
19149 /************** Begin file malloc.c ******************************************/
19150 /*
19151 ** 2001 September 15
19152 **
19153 ** The author disclaims copyright to this source code.  In place of
19154 ** a legal notice, here is a blessing:
19155 **
19156 **    May you do good and not evil.
19157 **    May you find forgiveness for yourself and forgive others.
19158 **    May you share freely, never taking more than you give.
19159 **
19160 *************************************************************************
19161 **
19162 ** Memory allocation functions used throughout sqlite.
19163 */
19164 /* #include <stdarg.h> */
19165 
19166 /*
19167 ** Attempt to release up to n bytes of non-essential memory currently
19168 ** held by SQLite. An example of non-essential memory is memory used to
19169 ** cache database pages that are not currently in use.
19170 */
19171 SQLITE_API int sqlite3_release_memory(int n){
19172 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
19173   return sqlite3PcacheReleaseMemory(n);
19174 #else
19175   /* IMPLEMENTATION-OF: R-34391-24921 The sqlite3_release_memory() routine
19176   ** is a no-op returning zero if SQLite is not compiled with
19177   ** SQLITE_ENABLE_MEMORY_MANAGEMENT. */
19178   UNUSED_PARAMETER(n);
19179   return 0;
19180 #endif
19181 }
19182 
19183 /*
19184 ** An instance of the following object records the location of
19185 ** each unused scratch buffer.
19186 */
19187 typedef struct ScratchFreeslot {
19188   struct ScratchFreeslot *pNext;   /* Next unused scratch buffer */
19189 } ScratchFreeslot;
19190 
19191 /*
19192 ** State information local to the memory allocation subsystem.
19193 */
19194 static SQLITE_WSD struct Mem0Global {
19195   sqlite3_mutex *mutex;         /* Mutex to serialize access */
19196 
19197   /*
19198   ** The alarm callback and its arguments.  The mem0.mutex lock will
19199   ** be held while the callback is running.  Recursive calls into
19200   ** the memory subsystem are allowed, but no new callbacks will be
19201   ** issued.
19202   */
19203   sqlite3_int64 alarmThreshold;
19204   void (*alarmCallback)(void*, sqlite3_int64,int);
19205   void *alarmArg;
19206 
19207   /*
19208   ** Pointers to the end of sqlite3GlobalConfig.pScratch memory
19209   ** (so that a range test can be used to determine if an allocation
19210   ** being freed came from pScratch) and a pointer to the list of
19211   ** unused scratch allocations.
19212   */
19213   void *pScratchEnd;
19214   ScratchFreeslot *pScratchFree;
19215   u32 nScratchFree;
19216 
19217   /*
19218   ** True if heap is nearly "full" where "full" is defined by the
19219   ** sqlite3_soft_heap_limit() setting.
19220   */
19221   int nearlyFull;
19222 } mem0 = { 0, 0, 0, 0, 0, 0, 0, 0 };
19223 
19224 #define mem0 GLOBAL(struct Mem0Global, mem0)
19225 
19226 /*
19227 ** This routine runs when the memory allocator sees that the
19228 ** total memory allocation is about to exceed the soft heap
19229 ** limit.
19230 */
19231 static void softHeapLimitEnforcer(
19232   void *NotUsed,
19233   sqlite3_int64 NotUsed2,
19234   int allocSize
19235 ){
19236   UNUSED_PARAMETER2(NotUsed, NotUsed2);
19237   sqlite3_release_memory(allocSize);
19238 }
19239 
19240 /*
19241 ** Change the alarm callback
19242 */
19243 static int sqlite3MemoryAlarm(
19244   void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
19245   void *pArg,
19246   sqlite3_int64 iThreshold
19247 ){
19248   int nUsed;
19249   sqlite3_mutex_enter(mem0.mutex);
19250   mem0.alarmCallback = xCallback;
19251   mem0.alarmArg = pArg;
19252   mem0.alarmThreshold = iThreshold;
19253   nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
19254   mem0.nearlyFull = (iThreshold>0 && iThreshold<=nUsed);
19255   sqlite3_mutex_leave(mem0.mutex);
19256   return SQLITE_OK;
19257 }
19258 
19259 #ifndef SQLITE_OMIT_DEPRECATED
19260 /*
19261 ** Deprecated external interface.  Internal/core SQLite code
19262 ** should call sqlite3MemoryAlarm.
19263 */
19264 SQLITE_API int sqlite3_memory_alarm(
19265   void(*xCallback)(void *pArg, sqlite3_int64 used,int N),
19266   void *pArg,
19267   sqlite3_int64 iThreshold
19268 ){
19269   return sqlite3MemoryAlarm(xCallback, pArg, iThreshold);
19270 }
19271 #endif
19272 
19273 /*
19274 ** Set the soft heap-size limit for the library. Passing a zero or
19275 ** negative value indicates no limit.
19276 */
19277 SQLITE_API sqlite3_int64 sqlite3_soft_heap_limit64(sqlite3_int64 n){
19278   sqlite3_int64 priorLimit;
19279   sqlite3_int64 excess;
19280 #ifndef SQLITE_OMIT_AUTOINIT
19281   int rc = sqlite3_initialize();
19282   if( rc ) return -1;
19283 #endif
19284   sqlite3_mutex_enter(mem0.mutex);
19285   priorLimit = mem0.alarmThreshold;
19286   sqlite3_mutex_leave(mem0.mutex);
19287   if( n<0 ) return priorLimit;
19288   if( n>0 ){
19289     sqlite3MemoryAlarm(softHeapLimitEnforcer, 0, n);
19290   }else{
19291     sqlite3MemoryAlarm(0, 0, 0);
19292   }
19293   excess = sqlite3_memory_used() - n;
19294   if( excess>0 ) sqlite3_release_memory((int)(excess & 0x7fffffff));
19295   return priorLimit;
19296 }
19297 SQLITE_API void sqlite3_soft_heap_limit(int n){
19298   if( n<0 ) n = 0;
19299   sqlite3_soft_heap_limit64(n);
19300 }
19301 
19302 /*
19303 ** Initialize the memory allocation subsystem.
19304 */
19305 SQLITE_PRIVATE int sqlite3MallocInit(void){
19306   if( sqlite3GlobalConfig.m.xMalloc==0 ){
19307     sqlite3MemSetDefault();
19308   }
19309   memset(&mem0, 0, sizeof(mem0));
19310   if( sqlite3GlobalConfig.bCoreMutex ){
19311     mem0.mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM);
19312   }
19313   if( sqlite3GlobalConfig.pScratch && sqlite3GlobalConfig.szScratch>=100
19314       && sqlite3GlobalConfig.nScratch>0 ){
19315     int i, n, sz;
19316     ScratchFreeslot *pSlot;
19317     sz = ROUNDDOWN8(sqlite3GlobalConfig.szScratch);
19318     sqlite3GlobalConfig.szScratch = sz;
19319     pSlot = (ScratchFreeslot*)sqlite3GlobalConfig.pScratch;
19320     n = sqlite3GlobalConfig.nScratch;
19321     mem0.pScratchFree = pSlot;
19322     mem0.nScratchFree = n;
19323     for(i=0; i<n-1; i++){
19324       pSlot->pNext = (ScratchFreeslot*)(sz+(char*)pSlot);
19325       pSlot = pSlot->pNext;
19326     }
19327     pSlot->pNext = 0;
19328     mem0.pScratchEnd = (void*)&pSlot[1];
19329   }else{
19330     mem0.pScratchEnd = 0;
19331     sqlite3GlobalConfig.pScratch = 0;
19332     sqlite3GlobalConfig.szScratch = 0;
19333     sqlite3GlobalConfig.nScratch = 0;
19334   }
19335   if( sqlite3GlobalConfig.pPage==0 || sqlite3GlobalConfig.szPage<512
19336       || sqlite3GlobalConfig.nPage<1 ){
19337     sqlite3GlobalConfig.pPage = 0;
19338     sqlite3GlobalConfig.szPage = 0;
19339     sqlite3GlobalConfig.nPage = 0;
19340   }
19341   return sqlite3GlobalConfig.m.xInit(sqlite3GlobalConfig.m.pAppData);
19342 }
19343 
19344 /*
19345 ** Return true if the heap is currently under memory pressure - in other
19346 ** words if the amount of heap used is close to the limit set by
19347 ** sqlite3_soft_heap_limit().
19348 */
19349 SQLITE_PRIVATE int sqlite3HeapNearlyFull(void){
19350   return mem0.nearlyFull;
19351 }
19352 
19353 /*
19354 ** Deinitialize the memory allocation subsystem.
19355 */
19356 SQLITE_PRIVATE void sqlite3MallocEnd(void){
19357   if( sqlite3GlobalConfig.m.xShutdown ){
19358     sqlite3GlobalConfig.m.xShutdown(sqlite3GlobalConfig.m.pAppData);
19359   }
19360   memset(&mem0, 0, sizeof(mem0));
19361 }
19362 
19363 /*
19364 ** Return the amount of memory currently checked out.
19365 */
19366 SQLITE_API sqlite3_int64 sqlite3_memory_used(void){
19367   int n, mx;
19368   sqlite3_int64 res;
19369   sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, 0);
19370   res = (sqlite3_int64)n;  /* Work around bug in Borland C. Ticket #3216 */
19371   return res;
19372 }
19373 
19374 /*
19375 ** Return the maximum amount of memory that has ever been
19376 ** checked out since either the beginning of this process
19377 ** or since the most recent reset.
19378 */
19379 SQLITE_API sqlite3_int64 sqlite3_memory_highwater(int resetFlag){
19380   int n, mx;
19381   sqlite3_int64 res;
19382   sqlite3_status(SQLITE_STATUS_MEMORY_USED, &n, &mx, resetFlag);
19383   res = (sqlite3_int64)mx;  /* Work around bug in Borland C. Ticket #3216 */
19384   return res;
19385 }
19386 
19387 /*
19388 ** Trigger the alarm
19389 */
19390 static void sqlite3MallocAlarm(int nByte){
19391   void (*xCallback)(void*,sqlite3_int64,int);
19392   sqlite3_int64 nowUsed;
19393   void *pArg;
19394   if( mem0.alarmCallback==0 ) return;
19395   xCallback = mem0.alarmCallback;
19396   nowUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
19397   pArg = mem0.alarmArg;
19398   mem0.alarmCallback = 0;
19399   sqlite3_mutex_leave(mem0.mutex);
19400   xCallback(pArg, nowUsed, nByte);
19401   sqlite3_mutex_enter(mem0.mutex);
19402   mem0.alarmCallback = xCallback;
19403   mem0.alarmArg = pArg;
19404 }
19405 
19406 /*
19407 ** Do a memory allocation with statistics and alarms.  Assume the
19408 ** lock is already held.
19409 */
19410 static int mallocWithAlarm(int n, void **pp){
19411   int nFull;
19412   void *p;
19413   assert( sqlite3_mutex_held(mem0.mutex) );
19414   nFull = sqlite3GlobalConfig.m.xRoundup(n);
19415   sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, n);
19416   if( mem0.alarmCallback!=0 ){
19417     int nUsed = sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED);
19418     if( nUsed >= mem0.alarmThreshold - nFull ){
19419       mem0.nearlyFull = 1;
19420       sqlite3MallocAlarm(nFull);
19421     }else{
19422       mem0.nearlyFull = 0;
19423     }
19424   }
19425   p = sqlite3GlobalConfig.m.xMalloc(nFull);
19426 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
19427   if( p==0 && mem0.alarmCallback ){
19428     sqlite3MallocAlarm(nFull);
19429     p = sqlite3GlobalConfig.m.xMalloc(nFull);
19430   }
19431 #endif
19432   if( p ){
19433     nFull = sqlite3MallocSize(p);
19434     sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nFull);
19435     sqlite3StatusAdd(SQLITE_STATUS_MALLOC_COUNT, 1);
19436   }
19437   *pp = p;
19438   return nFull;
19439 }
19440 
19441 /*
19442 ** Allocate memory.  This routine is like sqlite3_malloc() except that it
19443 ** assumes the memory subsystem has already been initialized.
19444 */
19445 SQLITE_PRIVATE void *sqlite3Malloc(int n){
19446   void *p;
19447   if( n<=0               /* IMP: R-65312-04917 */
19448    || n>=0x7fffff00
19449   ){
19450     /* A memory allocation of a number of bytes which is near the maximum
19451     ** signed integer value might cause an integer overflow inside of the
19452     ** xMalloc().  Hence we limit the maximum size to 0x7fffff00, giving
19453     ** 255 bytes of overhead.  SQLite itself will never use anything near
19454     ** this amount.  The only way to reach the limit is with sqlite3_malloc() */
19455     p = 0;
19456   }else if( sqlite3GlobalConfig.bMemstat ){
19457     sqlite3_mutex_enter(mem0.mutex);
19458     mallocWithAlarm(n, &p);
19459     sqlite3_mutex_leave(mem0.mutex);
19460   }else{
19461     p = sqlite3GlobalConfig.m.xMalloc(n);
19462   }
19463   assert( EIGHT_BYTE_ALIGNMENT(p) );  /* IMP: R-04675-44850 */
19464   return p;
19465 }
19466 
19467 /*
19468 ** This version of the memory allocation is for use by the application.
19469 ** First make sure the memory subsystem is initialized, then do the
19470 ** allocation.
19471 */
19472 SQLITE_API void *sqlite3_malloc(int n){
19473 #ifndef SQLITE_OMIT_AUTOINIT
19474   if( sqlite3_initialize() ) return 0;
19475 #endif
19476   return sqlite3Malloc(n);
19477 }
19478 
19479 /*
19480 ** Each thread may only have a single outstanding allocation from
19481 ** xScratchMalloc().  We verify this constraint in the single-threaded
19482 ** case by setting scratchAllocOut to 1 when an allocation
19483 ** is outstanding clearing it when the allocation is freed.
19484 */
19485 #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
19486 static int scratchAllocOut = 0;
19487 #endif
19488 
19489 
19490 /*
19491 ** Allocate memory that is to be used and released right away.
19492 ** This routine is similar to alloca() in that it is not intended
19493 ** for situations where the memory might be held long-term.  This
19494 ** routine is intended to get memory to old large transient data
19495 ** structures that would not normally fit on the stack of an
19496 ** embedded processor.
19497 */
19498 SQLITE_PRIVATE void *sqlite3ScratchMalloc(int n){
19499   void *p;
19500   assert( n>0 );
19501 
19502   sqlite3_mutex_enter(mem0.mutex);
19503   if( mem0.nScratchFree && sqlite3GlobalConfig.szScratch>=n ){
19504     p = mem0.pScratchFree;
19505     mem0.pScratchFree = mem0.pScratchFree->pNext;
19506     mem0.nScratchFree--;
19507     sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, 1);
19508     sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n);
19509     sqlite3_mutex_leave(mem0.mutex);
19510   }else{
19511     if( sqlite3GlobalConfig.bMemstat ){
19512       sqlite3StatusSet(SQLITE_STATUS_SCRATCH_SIZE, n);
19513       n = mallocWithAlarm(n, &p);
19514       if( p ) sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, n);
19515       sqlite3_mutex_leave(mem0.mutex);
19516     }else{
19517       sqlite3_mutex_leave(mem0.mutex);
19518       p = sqlite3GlobalConfig.m.xMalloc(n);
19519     }
19520     sqlite3MemdebugSetType(p, MEMTYPE_SCRATCH);
19521   }
19522   assert( sqlite3_mutex_notheld(mem0.mutex) );
19523 
19524 
19525 #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
19526   /* Verify that no more than two scratch allocations per thread
19527   ** are outstanding at one time.  (This is only checked in the
19528   ** single-threaded case since checking in the multi-threaded case
19529   ** would be much more complicated.) */
19530   assert( scratchAllocOut<=1 );
19531   if( p ) scratchAllocOut++;
19532 #endif
19533 
19534   return p;
19535 }
19536 SQLITE_PRIVATE void sqlite3ScratchFree(void *p){
19537   if( p ){
19538 
19539 #if SQLITE_THREADSAFE==0 && !defined(NDEBUG)
19540     /* Verify that no more than two scratch allocation per thread
19541     ** is outstanding at one time.  (This is only checked in the
19542     ** single-threaded case since checking in the multi-threaded case
19543     ** would be much more complicated.) */
19544     assert( scratchAllocOut>=1 && scratchAllocOut<=2 );
19545     scratchAllocOut--;
19546 #endif
19547 
19548     if( p>=sqlite3GlobalConfig.pScratch && p<mem0.pScratchEnd ){
19549       /* Release memory from the SQLITE_CONFIG_SCRATCH allocation */
19550       ScratchFreeslot *pSlot;
19551       pSlot = (ScratchFreeslot*)p;
19552       sqlite3_mutex_enter(mem0.mutex);
19553       pSlot->pNext = mem0.pScratchFree;
19554       mem0.pScratchFree = pSlot;
19555       mem0.nScratchFree++;
19556       assert( mem0.nScratchFree <= (u32)sqlite3GlobalConfig.nScratch );
19557       sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_USED, -1);
19558       sqlite3_mutex_leave(mem0.mutex);
19559     }else{
19560       /* Release memory back to the heap */
19561       assert( sqlite3MemdebugHasType(p, MEMTYPE_SCRATCH) );
19562       assert( sqlite3MemdebugNoType(p, ~MEMTYPE_SCRATCH) );
19563       sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
19564       if( sqlite3GlobalConfig.bMemstat ){
19565         int iSize = sqlite3MallocSize(p);
19566         sqlite3_mutex_enter(mem0.mutex);
19567         sqlite3StatusAdd(SQLITE_STATUS_SCRATCH_OVERFLOW, -iSize);
19568         sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -iSize);
19569         sqlite3StatusAdd(SQLITE_STATUS_MALLOC_COUNT, -1);
19570         sqlite3GlobalConfig.m.xFree(p);
19571         sqlite3_mutex_leave(mem0.mutex);
19572       }else{
19573         sqlite3GlobalConfig.m.xFree(p);
19574       }
19575     }
19576   }
19577 }
19578 
19579 /*
19580 ** TRUE if p is a lookaside memory allocation from db
19581 */
19582 #ifndef SQLITE_OMIT_LOOKASIDE
19583 static int isLookaside(sqlite3 *db, void *p){
19584   return p>=db->lookaside.pStart && p<db->lookaside.pEnd;
19585 }
19586 #else
19587 #define isLookaside(A,B) 0
19588 #endif
19589 
19590 /*
19591 ** Return the size of a memory allocation previously obtained from
19592 ** sqlite3Malloc() or sqlite3_malloc().
19593 */
19594 SQLITE_PRIVATE int sqlite3MallocSize(void *p){
19595   assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
19596   assert( sqlite3MemdebugNoType(p, MEMTYPE_DB) );
19597   return sqlite3GlobalConfig.m.xSize(p);
19598 }
19599 SQLITE_PRIVATE int sqlite3DbMallocSize(sqlite3 *db, void *p){
19600   assert( db!=0 );
19601   assert( sqlite3_mutex_held(db->mutex) );
19602   if( isLookaside(db, p) ){
19603     return db->lookaside.sz;
19604   }else{
19605     assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) );
19606     assert( sqlite3MemdebugHasType(p, MEMTYPE_LOOKASIDE|MEMTYPE_HEAP) );
19607     assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) );
19608     return sqlite3GlobalConfig.m.xSize(p);
19609   }
19610 }
19611 
19612 /*
19613 ** Free memory previously obtained from sqlite3Malloc().
19614 */
19615 SQLITE_API void sqlite3_free(void *p){
19616   if( p==0 ) return;  /* IMP: R-49053-54554 */
19617   assert( sqlite3MemdebugNoType(p, MEMTYPE_DB) );
19618   assert( sqlite3MemdebugHasType(p, MEMTYPE_HEAP) );
19619   if( sqlite3GlobalConfig.bMemstat ){
19620     sqlite3_mutex_enter(mem0.mutex);
19621     sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, -sqlite3MallocSize(p));
19622     sqlite3StatusAdd(SQLITE_STATUS_MALLOC_COUNT, -1);
19623     sqlite3GlobalConfig.m.xFree(p);
19624     sqlite3_mutex_leave(mem0.mutex);
19625   }else{
19626     sqlite3GlobalConfig.m.xFree(p);
19627   }
19628 }
19629 
19630 /*
19631 ** Free memory that might be associated with a particular database
19632 ** connection.
19633 */
19634 SQLITE_PRIVATE void sqlite3DbFree(sqlite3 *db, void *p){
19635   assert( db==0 || sqlite3_mutex_held(db->mutex) );
19636   if( p==0 ) return;
19637   if( db ){
19638     if( db->pnBytesFreed ){
19639       *db->pnBytesFreed += sqlite3DbMallocSize(db, p);
19640       return;
19641     }
19642     if( isLookaside(db, p) ){
19643       LookasideSlot *pBuf = (LookasideSlot*)p;
19644 #if SQLITE_DEBUG
19645       /* Trash all content in the buffer being freed */
19646       memset(p, 0xaa, db->lookaside.sz);
19647 #endif
19648       pBuf->pNext = db->lookaside.pFree;
19649       db->lookaside.pFree = pBuf;
19650       db->lookaside.nOut--;
19651       return;
19652     }
19653   }
19654   assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) );
19655   assert( sqlite3MemdebugHasType(p, MEMTYPE_LOOKASIDE|MEMTYPE_HEAP) );
19656   assert( db!=0 || sqlite3MemdebugNoType(p, MEMTYPE_LOOKASIDE) );
19657   sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
19658   sqlite3_free(p);
19659 }
19660 
19661 /*
19662 ** Change the size of an existing memory allocation
19663 */
19664 SQLITE_PRIVATE void *sqlite3Realloc(void *pOld, int nBytes){
19665   int nOld, nNew, nDiff;
19666   void *pNew;
19667   if( pOld==0 ){
19668     return sqlite3Malloc(nBytes); /* IMP: R-28354-25769 */
19669   }
19670   if( nBytes<=0 ){
19671     sqlite3_free(pOld); /* IMP: R-31593-10574 */
19672     return 0;
19673   }
19674   if( nBytes>=0x7fffff00 ){
19675     /* The 0x7ffff00 limit term is explained in comments on sqlite3Malloc() */
19676     return 0;
19677   }
19678   nOld = sqlite3MallocSize(pOld);
19679   /* IMPLEMENTATION-OF: R-46199-30249 SQLite guarantees that the second
19680   ** argument to xRealloc is always a value returned by a prior call to
19681   ** xRoundup. */
19682   nNew = sqlite3GlobalConfig.m.xRoundup(nBytes);
19683   if( nOld==nNew ){
19684     pNew = pOld;
19685   }else if( sqlite3GlobalConfig.bMemstat ){
19686     sqlite3_mutex_enter(mem0.mutex);
19687     sqlite3StatusSet(SQLITE_STATUS_MALLOC_SIZE, nBytes);
19688     nDiff = nNew - nOld;
19689     if( sqlite3StatusValue(SQLITE_STATUS_MEMORY_USED) >=
19690           mem0.alarmThreshold-nDiff ){
19691       sqlite3MallocAlarm(nDiff);
19692     }
19693     assert( sqlite3MemdebugHasType(pOld, MEMTYPE_HEAP) );
19694     assert( sqlite3MemdebugNoType(pOld, ~MEMTYPE_HEAP) );
19695     pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
19696     if( pNew==0 && mem0.alarmCallback ){
19697       sqlite3MallocAlarm(nBytes);
19698       pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
19699     }
19700     if( pNew ){
19701       nNew = sqlite3MallocSize(pNew);
19702       sqlite3StatusAdd(SQLITE_STATUS_MEMORY_USED, nNew-nOld);
19703     }
19704     sqlite3_mutex_leave(mem0.mutex);
19705   }else{
19706     pNew = sqlite3GlobalConfig.m.xRealloc(pOld, nNew);
19707   }
19708   assert( EIGHT_BYTE_ALIGNMENT(pNew) ); /* IMP: R-04675-44850 */
19709   return pNew;
19710 }
19711 
19712 /*
19713 ** The public interface to sqlite3Realloc.  Make sure that the memory
19714 ** subsystem is initialized prior to invoking sqliteRealloc.
19715 */
19716 SQLITE_API void *sqlite3_realloc(void *pOld, int n){
19717 #ifndef SQLITE_OMIT_AUTOINIT
19718   if( sqlite3_initialize() ) return 0;
19719 #endif
19720   return sqlite3Realloc(pOld, n);
19721 }
19722 
19723 
19724 /*
19725 ** Allocate and zero memory.
19726 */
19727 SQLITE_PRIVATE void *sqlite3MallocZero(int n){
19728   void *p = sqlite3Malloc(n);
19729   if( p ){
19730     memset(p, 0, n);
19731   }
19732   return p;
19733 }
19734 
19735 /*
19736 ** Allocate and zero memory.  If the allocation fails, make
19737 ** the mallocFailed flag in the connection pointer.
19738 */
19739 SQLITE_PRIVATE void *sqlite3DbMallocZero(sqlite3 *db, int n){
19740   void *p = sqlite3DbMallocRaw(db, n);
19741   if( p ){
19742     memset(p, 0, n);
19743   }
19744   return p;
19745 }
19746 
19747 /*
19748 ** Allocate and zero memory.  If the allocation fails, make
19749 ** the mallocFailed flag in the connection pointer.
19750 **
19751 ** If db!=0 and db->mallocFailed is true (indicating a prior malloc
19752 ** failure on the same database connection) then always return 0.
19753 ** Hence for a particular database connection, once malloc starts
19754 ** failing, it fails consistently until mallocFailed is reset.
19755 ** This is an important assumption.  There are many places in the
19756 ** code that do things like this:
19757 **
19758 **         int *a = (int*)sqlite3DbMallocRaw(db, 100);
19759 **         int *b = (int*)sqlite3DbMallocRaw(db, 200);
19760 **         if( b ) a[10] = 9;
19761 **
19762 ** In other words, if a subsequent malloc (ex: "b") worked, it is assumed
19763 ** that all prior mallocs (ex: "a") worked too.
19764 */
19765 SQLITE_PRIVATE void *sqlite3DbMallocRaw(sqlite3 *db, int n){
19766   void *p;
19767   assert( db==0 || sqlite3_mutex_held(db->mutex) );
19768   assert( db==0 || db->pnBytesFreed==0 );
19769 #ifndef SQLITE_OMIT_LOOKASIDE
19770   if( db ){
19771     LookasideSlot *pBuf;
19772     if( db->mallocFailed ){
19773       return 0;
19774     }
19775     if( db->lookaside.bEnabled ){
19776       if( n>db->lookaside.sz ){
19777         db->lookaside.anStat[1]++;
19778       }else if( (pBuf = db->lookaside.pFree)==0 ){
19779         db->lookaside.anStat[2]++;
19780       }else{
19781         db->lookaside.pFree = pBuf->pNext;
19782         db->lookaside.nOut++;
19783         db->lookaside.anStat[0]++;
19784         if( db->lookaside.nOut>db->lookaside.mxOut ){
19785           db->lookaside.mxOut = db->lookaside.nOut;
19786         }
19787         return (void*)pBuf;
19788       }
19789     }
19790   }
19791 #else
19792   if( db && db->mallocFailed ){
19793     return 0;
19794   }
19795 #endif
19796   p = sqlite3Malloc(n);
19797   if( !p && db ){
19798     db->mallocFailed = 1;
19799   }
19800   sqlite3MemdebugSetType(p, MEMTYPE_DB |
19801          ((db && db->lookaside.bEnabled) ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP));
19802   return p;
19803 }
19804 
19805 /*
19806 ** Resize the block of memory pointed to by p to n bytes. If the
19807 ** resize fails, set the mallocFailed flag in the connection object.
19808 */
19809 SQLITE_PRIVATE void *sqlite3DbRealloc(sqlite3 *db, void *p, int n){
19810   void *pNew = 0;
19811   assert( db!=0 );
19812   assert( sqlite3_mutex_held(db->mutex) );
19813   if( db->mallocFailed==0 ){
19814     if( p==0 ){
19815       return sqlite3DbMallocRaw(db, n);
19816     }
19817     if( isLookaside(db, p) ){
19818       if( n<=db->lookaside.sz ){
19819         return p;
19820       }
19821       pNew = sqlite3DbMallocRaw(db, n);
19822       if( pNew ){
19823         memcpy(pNew, p, db->lookaside.sz);
19824         sqlite3DbFree(db, p);
19825       }
19826     }else{
19827       assert( sqlite3MemdebugHasType(p, MEMTYPE_DB) );
19828       assert( sqlite3MemdebugHasType(p, MEMTYPE_LOOKASIDE|MEMTYPE_HEAP) );
19829       sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
19830       pNew = sqlite3_realloc(p, n);
19831       if( !pNew ){
19832         sqlite3MemdebugSetType(p, MEMTYPE_DB|MEMTYPE_HEAP);
19833         db->mallocFailed = 1;
19834       }
19835       sqlite3MemdebugSetType(pNew, MEMTYPE_DB |
19836             (db->lookaside.bEnabled ? MEMTYPE_LOOKASIDE : MEMTYPE_HEAP));
19837     }
19838   }
19839   return pNew;
19840 }
19841 
19842 /*
19843 ** Attempt to reallocate p.  If the reallocation fails, then free p
19844 ** and set the mallocFailed flag in the database connection.
19845 */
19846 SQLITE_PRIVATE void *sqlite3DbReallocOrFree(sqlite3 *db, void *p, int n){
19847   void *pNew;
19848   pNew = sqlite3DbRealloc(db, p, n);
19849   if( !pNew ){
19850     sqlite3DbFree(db, p);
19851   }
19852   return pNew;
19853 }
19854 
19855 /*
19856 ** Make a copy of a string in memory obtained from sqliteMalloc(). These
19857 ** functions call sqlite3MallocRaw() directly instead of sqliteMalloc(). This
19858 ** is because when memory debugging is turned on, these two functions are
19859 ** called via macros that record the current file and line number in the
19860 ** ThreadData structure.
19861 */
19862 SQLITE_PRIVATE char *sqlite3DbStrDup(sqlite3 *db, const char *z){
19863   char *zNew;
19864   size_t n;
19865   if( z==0 ){
19866     return 0;
19867   }
19868   n = sqlite3Strlen30(z) + 1;
19869   assert( (n&0x7fffffff)==n );
19870   zNew = sqlite3DbMallocRaw(db, (int)n);
19871   if( zNew ){
19872     memcpy(zNew, z, n);
19873   }
19874   return zNew;
19875 }
19876 SQLITE_PRIVATE char *sqlite3DbStrNDup(sqlite3 *db, const char *z, int n){
19877   char *zNew;
19878   if( z==0 ){
19879     return 0;
19880   }
19881   assert( (n&0x7fffffff)==n );
19882   zNew = sqlite3DbMallocRaw(db, n+1);
19883   if( zNew ){
19884     memcpy(zNew, z, n);
19885     zNew[n] = 0;
19886   }
19887   return zNew;
19888 }
19889 
19890 /*
19891 ** Create a string from the zFromat argument and the va_list that follows.
19892 ** Store the string in memory obtained from sqliteMalloc() and make *pz
19893 ** point to that string.
19894 */
19895 SQLITE_PRIVATE void sqlite3SetString(char **pz, sqlite3 *db, const char *zFormat, ...){
19896   va_list ap;
19897   char *z;
19898 
19899   va_start(ap, zFormat);
19900   z = sqlite3VMPrintf(db, zFormat, ap);
19901   va_end(ap);
19902   sqlite3DbFree(db, *pz);
19903   *pz = z;
19904 }
19905 
19906 
19907 /*
19908 ** This function must be called before exiting any API function (i.e.
19909 ** returning control to the user) that has called sqlite3_malloc or
19910 ** sqlite3_realloc.
19911 **
19912 ** The returned value is normally a copy of the second argument to this
19913 ** function. However, if a malloc() failure has occurred since the previous
19914 ** invocation SQLITE_NOMEM is returned instead.
19915 **
19916 ** If the first argument, db, is not NULL and a malloc() error has occurred,
19917 ** then the connection error-code (the value returned by sqlite3_errcode())
19918 ** is set to SQLITE_NOMEM.
19919 */
19920 SQLITE_PRIVATE int sqlite3ApiExit(sqlite3* db, int rc){
19921   /* If the db handle is not NULL, then we must hold the connection handle
19922   ** mutex here. Otherwise the read (and possible write) of db->mallocFailed
19923   ** is unsafe, as is the call to sqlite3Error().
19924   */
19925   assert( !db || sqlite3_mutex_held(db->mutex) );
19926   if( db && (db->mallocFailed || rc==SQLITE_IOERR_NOMEM) ){
19927     sqlite3Error(db, SQLITE_NOMEM, 0);
19928     db->mallocFailed = 0;
19929     rc = SQLITE_NOMEM;
19930   }
19931   return rc & (db ? db->errMask : 0xff);
19932 }
19933 
19934 /************** End of malloc.c **********************************************/
19935 /************** Begin file printf.c ******************************************/
19936 /*
19937 ** The "printf" code that follows dates from the 1980's.  It is in
19938 ** the public domain.  The original comments are included here for
19939 ** completeness.  They are very out-of-date but might be useful as
19940 ** an historical reference.  Most of the "enhancements" have been backed
19941 ** out so that the functionality is now the same as standard printf().
19942 **
19943 **************************************************************************
19944 **
19945 ** This file contains code for a set of "printf"-like routines.  These
19946 ** routines format strings much like the printf() from the standard C
19947 ** library, though the implementation here has enhancements to support
19948 ** SQLlite.
19949 */
19950 
19951 /*
19952 ** Conversion types fall into various categories as defined by the
19953 ** following enumeration.
19954 */
19955 #define etRADIX       1 /* Integer types.  %d, %x, %o, and so forth */
19956 #define etFLOAT       2 /* Floating point.  %f */
19957 #define etEXP         3 /* Exponentional notation. %e and %E */
19958 #define etGENERIC     4 /* Floating or exponential, depending on exponent. %g */
19959 #define etSIZE        5 /* Return number of characters processed so far. %n */
19960 #define etSTRING      6 /* Strings. %s */
19961 #define etDYNSTRING   7 /* Dynamically allocated strings. %z */
19962 #define etPERCENT     8 /* Percent symbol. %% */
19963 #define etCHARX       9 /* Characters. %c */
19964 /* The rest are extensions, not normally found in printf() */
19965 #define etSQLESCAPE  10 /* Strings with '\'' doubled.  %q */
19966 #define etSQLESCAPE2 11 /* Strings with '\'' doubled and enclosed in '',
19967                           NULL pointers replaced by SQL NULL.  %Q */
19968 #define etTOKEN      12 /* a pointer to a Token structure */
19969 #define etSRCLIST    13 /* a pointer to a SrcList */
19970 #define etPOINTER    14 /* The %p conversion */
19971 #define etSQLESCAPE3 15 /* %w -> Strings with '\"' doubled */
19972 #define etORDINAL    16 /* %r -> 1st, 2nd, 3rd, 4th, etc.  English only */
19973 
19974 #define etINVALID     0 /* Any unrecognized conversion type */
19975 
19976 
19977 /*
19978 ** An "etByte" is an 8-bit unsigned value.
19979 */
19980 typedef unsigned char etByte;
19981 
19982 /*
19983 ** Each builtin conversion character (ex: the 'd' in "%d") is described
19984 ** by an instance of the following structure
19985 */
19986 typedef struct et_info {   /* Information about each format field */
19987   char fmttype;            /* The format field code letter */
19988   etByte base;             /* The base for radix conversion */
19989   etByte flags;            /* One or more of FLAG_ constants below */
19990   etByte type;             /* Conversion paradigm */
19991   etByte charset;          /* Offset into aDigits[] of the digits string */
19992   etByte prefix;           /* Offset into aPrefix[] of the prefix string */
19993 } et_info;
19994 
19995 /*
19996 ** Allowed values for et_info.flags
19997 */
19998 #define FLAG_SIGNED  1     /* True if the value to convert is signed */
19999 #define FLAG_INTERN  2     /* True if for internal use only */
20000 #define FLAG_STRING  4     /* Allow infinity precision */
20001 
20002 
20003 /*
20004 ** The following table is searched linearly, so it is good to put the
20005 ** most frequently used conversion types first.
20006 */
20007 static const char aDigits[] = "0123456789ABCDEF0123456789abcdef";
20008 static const char aPrefix[] = "-x0\000X0";
20009 static const et_info fmtinfo[] = {
20010   {  'd', 10, 1, etRADIX,      0,  0 },
20011   {  's',  0, 4, etSTRING,     0,  0 },
20012   {  'g',  0, 1, etGENERIC,    30, 0 },
20013   {  'z',  0, 4, etDYNSTRING,  0,  0 },
20014   {  'q',  0, 4, etSQLESCAPE,  0,  0 },
20015   {  'Q',  0, 4, etSQLESCAPE2, 0,  0 },
20016   {  'w',  0, 4, etSQLESCAPE3, 0,  0 },
20017   {  'c',  0, 0, etCHARX,      0,  0 },
20018   {  'o',  8, 0, etRADIX,      0,  2 },
20019   {  'u', 10, 0, etRADIX,      0,  0 },
20020   {  'x', 16, 0, etRADIX,      16, 1 },
20021   {  'X', 16, 0, etRADIX,      0,  4 },
20022 #ifndef SQLITE_OMIT_FLOATING_POINT
20023   {  'f',  0, 1, etFLOAT,      0,  0 },
20024   {  'e',  0, 1, etEXP,        30, 0 },
20025   {  'E',  0, 1, etEXP,        14, 0 },
20026   {  'G',  0, 1, etGENERIC,    14, 0 },
20027 #endif
20028   {  'i', 10, 1, etRADIX,      0,  0 },
20029   {  'n',  0, 0, etSIZE,       0,  0 },
20030   {  '%',  0, 0, etPERCENT,    0,  0 },
20031   {  'p', 16, 0, etPOINTER,    0,  1 },
20032 
20033 /* All the rest have the FLAG_INTERN bit set and are thus for internal
20034 ** use only */
20035   {  'T',  0, 2, etTOKEN,      0,  0 },
20036   {  'S',  0, 2, etSRCLIST,    0,  0 },
20037   {  'r', 10, 3, etORDINAL,    0,  0 },
20038 };
20039 
20040 /*
20041 ** If SQLITE_OMIT_FLOATING_POINT is defined, then none of the floating point
20042 ** conversions will work.
20043 */
20044 #ifndef SQLITE_OMIT_FLOATING_POINT
20045 /*
20046 ** "*val" is a double such that 0.1 <= *val < 10.0
20047 ** Return the ascii code for the leading digit of *val, then
20048 ** multiply "*val" by 10.0 to renormalize.
20049 **
20050 ** Example:
20051 **     input:     *val = 3.14159
20052 **     output:    *val = 1.4159    function return = '3'
20053 **
20054 ** The counter *cnt is incremented each time.  After counter exceeds
20055 ** 16 (the number of significant digits in a 64-bit float) '0' is
20056 ** always returned.
20057 */
20058 static char et_getdigit(LONGDOUBLE_TYPE *val, int *cnt){
20059   int digit;
20060   LONGDOUBLE_TYPE d;
20061   if( (*cnt)<=0 ) return '0';
20062   (*cnt)--;
20063   digit = (int)*val;
20064   d = digit;
20065   digit += '0';
20066   *val = (*val - d)*10.0;
20067   return (char)digit;
20068 }
20069 #endif /* SQLITE_OMIT_FLOATING_POINT */
20070 
20071 /*
20072 ** Append N space characters to the given string buffer.
20073 */
20074 SQLITE_PRIVATE void sqlite3AppendSpace(StrAccum *pAccum, int N){
20075   static const char zSpaces[] = "                             ";
20076   while( N>=(int)sizeof(zSpaces)-1 ){
20077     sqlite3StrAccumAppend(pAccum, zSpaces, sizeof(zSpaces)-1);
20078     N -= sizeof(zSpaces)-1;
20079   }
20080   if( N>0 ){
20081     sqlite3StrAccumAppend(pAccum, zSpaces, N);
20082   }
20083 }
20084 
20085 /*
20086 ** Set the StrAccum object to an error mode.
20087 */
20088 static void setStrAccumError(StrAccum *p, u8 eError){
20089   p->accError = eError;
20090   p->nAlloc = 0;
20091 }
20092 
20093 /*
20094 ** Extra argument values from a PrintfArguments object
20095 */
20096 static sqlite3_int64 getIntArg(PrintfArguments *p){
20097   if( p->nArg<=p->nUsed ) return 0;
20098   return sqlite3_value_int64(p->apArg[p->nUsed++]);
20099 }
20100 static double getDoubleArg(PrintfArguments *p){
20101   if( p->nArg<=p->nUsed ) return 0.0;
20102   return sqlite3_value_double(p->apArg[p->nUsed++]);
20103 }
20104 static char *getTextArg(PrintfArguments *p){
20105   if( p->nArg<=p->nUsed ) return 0;
20106   return (char*)sqlite3_value_text(p->apArg[p->nUsed++]);
20107 }
20108 
20109 
20110 /*
20111 ** On machines with a small stack size, you can redefine the
20112 ** SQLITE_PRINT_BUF_SIZE to be something smaller, if desired.
20113 */
20114 #ifndef SQLITE_PRINT_BUF_SIZE
20115 # define SQLITE_PRINT_BUF_SIZE 70
20116 #endif
20117 #define etBUFSIZE SQLITE_PRINT_BUF_SIZE  /* Size of the output buffer */
20118 
20119 /*
20120 ** Render a string given by "fmt" into the StrAccum object.
20121 */
20122 SQLITE_PRIVATE void sqlite3VXPrintf(
20123   StrAccum *pAccum,          /* Accumulate results here */
20124   u32 bFlags,                /* SQLITE_PRINTF_* flags */
20125   const char *fmt,           /* Format string */
20126   va_list ap                 /* arguments */
20127 ){
20128   int c;                     /* Next character in the format string */
20129   char *bufpt;               /* Pointer to the conversion buffer */
20130   int precision;             /* Precision of the current field */
20131   int length;                /* Length of the field */
20132   int idx;                   /* A general purpose loop counter */
20133   int width;                 /* Width of the current field */
20134   etByte flag_leftjustify;   /* True if "-" flag is present */
20135   etByte flag_plussign;      /* True if "+" flag is present */
20136   etByte flag_blanksign;     /* True if " " flag is present */
20137   etByte flag_alternateform; /* True if "#" flag is present */
20138   etByte flag_altform2;      /* True if "!" flag is present */
20139   etByte flag_zeropad;       /* True if field width constant starts with zero */
20140   etByte flag_long;          /* True if "l" flag is present */
20141   etByte flag_longlong;      /* True if the "ll" flag is present */
20142   etByte done;               /* Loop termination flag */
20143   etByte xtype = 0;          /* Conversion paradigm */
20144   u8 bArgList;               /* True for SQLITE_PRINTF_SQLFUNC */
20145   u8 useIntern;              /* Ok to use internal conversions (ex: %T) */
20146   char prefix;               /* Prefix character.  "+" or "-" or " " or '\0'. */
20147   sqlite_uint64 longvalue;   /* Value for integer types */
20148   LONGDOUBLE_TYPE realvalue; /* Value for real types */
20149   const et_info *infop;      /* Pointer to the appropriate info structure */
20150   char *zOut;                /* Rendering buffer */
20151   int nOut;                  /* Size of the rendering buffer */
20152   char *zExtra;              /* Malloced memory used by some conversion */
20153 #ifndef SQLITE_OMIT_FLOATING_POINT
20154   int  exp, e2;              /* exponent of real numbers */
20155   int nsd;                   /* Number of significant digits returned */
20156   double rounder;            /* Used for rounding floating point values */
20157   etByte flag_dp;            /* True if decimal point should be shown */
20158   etByte flag_rtz;           /* True if trailing zeros should be removed */
20159 #endif
20160   PrintfArguments *pArgList = 0; /* Arguments for SQLITE_PRINTF_SQLFUNC */
20161   char buf[etBUFSIZE];       /* Conversion buffer */
20162 
20163   bufpt = 0;
20164   if( bFlags ){
20165     if( (bArgList = (bFlags & SQLITE_PRINTF_SQLFUNC))!=0 ){
20166       pArgList = va_arg(ap, PrintfArguments*);
20167     }
20168     useIntern = bFlags & SQLITE_PRINTF_INTERNAL;
20169   }else{
20170     bArgList = useIntern = 0;
20171   }
20172   for(; (c=(*fmt))!=0; ++fmt){
20173     if( c!='%' ){
20174       int amt;
20175       bufpt = (char *)fmt;
20176       amt = 1;
20177       while( (c=(*++fmt))!='%' && c!=0 ) amt++;
20178       sqlite3StrAccumAppend(pAccum, bufpt, amt);
20179       if( c==0 ) break;
20180     }
20181     if( (c=(*++fmt))==0 ){
20182       sqlite3StrAccumAppend(pAccum, "%", 1);
20183       break;
20184     }
20185     /* Find out what flags are present */
20186     flag_leftjustify = flag_plussign = flag_blanksign =
20187      flag_alternateform = flag_altform2 = flag_zeropad = 0;
20188     done = 0;
20189     do{
20190       switch( c ){
20191         case '-':   flag_leftjustify = 1;     break;
20192         case '+':   flag_plussign = 1;        break;
20193         case ' ':   flag_blanksign = 1;       break;
20194         case '#':   flag_alternateform = 1;   break;
20195         case '!':   flag_altform2 = 1;        break;
20196         case '0':   flag_zeropad = 1;         break;
20197         default:    done = 1;                 break;
20198       }
20199     }while( !done && (c=(*++fmt))!=0 );
20200     /* Get the field width */
20201     width = 0;
20202     if( c=='*' ){
20203       if( bArgList ){
20204         width = (int)getIntArg(pArgList);
20205       }else{
20206         width = va_arg(ap,int);
20207       }
20208       if( width<0 ){
20209         flag_leftjustify = 1;
20210         width = -width;
20211       }
20212       c = *++fmt;
20213     }else{
20214       while( c>='0' && c<='9' ){
20215         width = width*10 + c - '0';
20216         c = *++fmt;
20217       }
20218     }
20219     /* Get the precision */
20220     if( c=='.' ){
20221       precision = 0;
20222       c = *++fmt;
20223       if( c=='*' ){
20224         if( bArgList ){
20225           precision = (int)getIntArg(pArgList);
20226         }else{
20227           precision = va_arg(ap,int);
20228         }
20229         if( precision<0 ) precision = -precision;
20230         c = *++fmt;
20231       }else{
20232         while( c>='0' && c<='9' ){
20233           precision = precision*10 + c - '0';
20234           c = *++fmt;
20235         }
20236       }
20237     }else{
20238       precision = -1;
20239     }
20240     /* Get the conversion type modifier */
20241     if( c=='l' ){
20242       flag_long = 1;
20243       c = *++fmt;
20244       if( c=='l' ){
20245         flag_longlong = 1;
20246         c = *++fmt;
20247       }else{
20248         flag_longlong = 0;
20249       }
20250     }else{
20251       flag_long = flag_longlong = 0;
20252     }
20253     /* Fetch the info entry for the field */
20254     infop = &fmtinfo[0];
20255     xtype = etINVALID;
20256     for(idx=0; idx<ArraySize(fmtinfo); idx++){
20257       if( c==fmtinfo[idx].fmttype ){
20258         infop = &fmtinfo[idx];
20259         if( useIntern || (infop->flags & FLAG_INTERN)==0 ){
20260           xtype = infop->type;
20261         }else{
20262           return;
20263         }
20264         break;
20265       }
20266     }
20267     zExtra = 0;
20268 
20269     /*
20270     ** At this point, variables are initialized as follows:
20271     **
20272     **   flag_alternateform          TRUE if a '#' is present.
20273     **   flag_altform2               TRUE if a '!' is present.
20274     **   flag_plussign               TRUE if a '+' is present.
20275     **   flag_leftjustify            TRUE if a '-' is present or if the
20276     **                               field width was negative.
20277     **   flag_zeropad                TRUE if the width began with 0.
20278     **   flag_long                   TRUE if the letter 'l' (ell) prefixed
20279     **                               the conversion character.
20280     **   flag_longlong               TRUE if the letter 'll' (ell ell) prefixed
20281     **                               the conversion character.
20282     **   flag_blanksign              TRUE if a ' ' is present.
20283     **   width                       The specified field width.  This is
20284     **                               always non-negative.  Zero is the default.
20285     **   precision                   The specified precision.  The default
20286     **                               is -1.
20287     **   xtype                       The class of the conversion.
20288     **   infop                       Pointer to the appropriate info struct.
20289     */
20290     switch( xtype ){
20291       case etPOINTER:
20292         flag_longlong = sizeof(char*)==sizeof(i64);
20293         flag_long = sizeof(char*)==sizeof(long int);
20294         /* Fall through into the next case */
20295       case etORDINAL:
20296       case etRADIX:
20297         if( infop->flags & FLAG_SIGNED ){
20298           i64 v;
20299           if( bArgList ){
20300             v = getIntArg(pArgList);
20301           }else if( flag_longlong ){
20302             v = va_arg(ap,i64);
20303           }else if( flag_long ){
20304             v = va_arg(ap,long int);
20305           }else{
20306             v = va_arg(ap,int);
20307           }
20308           if( v<0 ){
20309             if( v==SMALLEST_INT64 ){
20310               longvalue = ((u64)1)<<63;
20311             }else{
20312               longvalue = -v;
20313             }
20314             prefix = '-';
20315           }else{
20316             longvalue = v;
20317             if( flag_plussign )        prefix = '+';
20318             else if( flag_blanksign )  prefix = ' ';
20319             else                       prefix = 0;
20320           }
20321         }else{
20322           if( bArgList ){
20323             longvalue = (u64)getIntArg(pArgList);
20324           }else if( flag_longlong ){
20325             longvalue = va_arg(ap,u64);
20326           }else if( flag_long ){
20327             longvalue = va_arg(ap,unsigned long int);
20328           }else{
20329             longvalue = va_arg(ap,unsigned int);
20330           }
20331           prefix = 0;
20332         }
20333         if( longvalue==0 ) flag_alternateform = 0;
20334         if( flag_zeropad && precision<width-(prefix!=0) ){
20335           precision = width-(prefix!=0);
20336         }
20337         if( precision<etBUFSIZE-10 ){
20338           nOut = etBUFSIZE;
20339           zOut = buf;
20340         }else{
20341           nOut = precision + 10;
20342           zOut = zExtra = sqlite3Malloc( nOut );
20343           if( zOut==0 ){
20344             setStrAccumError(pAccum, STRACCUM_NOMEM);
20345             return;
20346           }
20347         }
20348         bufpt = &zOut[nOut-1];
20349         if( xtype==etORDINAL ){
20350           static const char zOrd[] = "thstndrd";
20351           int x = (int)(longvalue % 10);
20352           if( x>=4 || (longvalue/10)%10==1 ){
20353             x = 0;
20354           }
20355           *(--bufpt) = zOrd[x*2+1];
20356           *(--bufpt) = zOrd[x*2];
20357         }
20358         {
20359           register const char *cset;      /* Use registers for speed */
20360           register int base;
20361           cset = &aDigits[infop->charset];
20362           base = infop->base;
20363           do{                                           /* Convert to ascii */
20364             *(--bufpt) = cset[longvalue%base];
20365             longvalue = longvalue/base;
20366           }while( longvalue>0 );
20367         }
20368         length = (int)(&zOut[nOut-1]-bufpt);
20369         for(idx=precision-length; idx>0; idx--){
20370           *(--bufpt) = '0';                             /* Zero pad */
20371         }
20372         if( prefix ) *(--bufpt) = prefix;               /* Add sign */
20373         if( flag_alternateform && infop->prefix ){      /* Add "0" or "0x" */
20374           const char *pre;
20375           char x;
20376           pre = &aPrefix[infop->prefix];
20377           for(; (x=(*pre))!=0; pre++) *(--bufpt) = x;
20378         }
20379         length = (int)(&zOut[nOut-1]-bufpt);
20380         break;
20381       case etFLOAT:
20382       case etEXP:
20383       case etGENERIC:
20384         if( bArgList ){
20385           realvalue = getDoubleArg(pArgList);
20386         }else{
20387           realvalue = va_arg(ap,double);
20388         }
20389 #ifdef SQLITE_OMIT_FLOATING_POINT
20390         length = 0;
20391 #else
20392         if( precision<0 ) precision = 6;         /* Set default precision */
20393         if( realvalue<0.0 ){
20394           realvalue = -realvalue;
20395           prefix = '-';
20396         }else{
20397           if( flag_plussign )          prefix = '+';
20398           else if( flag_blanksign )    prefix = ' ';
20399           else                         prefix = 0;
20400         }
20401         if( xtype==etGENERIC && precision>0 ) precision--;
20402         for(idx=precision, rounder=0.5; idx>0; idx--, rounder*=0.1){}
20403         if( xtype==etFLOAT ) realvalue += rounder;
20404         /* Normalize realvalue to within 10.0 > realvalue >= 1.0 */
20405         exp = 0;
20406         if( sqlite3IsNaN((double)realvalue) ){
20407           bufpt = "NaN";
20408           length = 3;
20409           break;
20410         }
20411         if( realvalue>0.0 ){
20412           LONGDOUBLE_TYPE scale = 1.0;
20413 #if __DBL_MAX_10_EXP__ > 100
20414           while( realvalue>=1e100*scale && exp<=350 ){ scale *= 1e100;exp+=100;}
20415 #endif
20416 #if __DBL_MAX_10_EXP__ > 64
20417           while( realvalue>=1e64*scale && exp<=350 ){ scale *= 1e64; exp+=64; }
20418 #endif
20419           while( realvalue>=1e8*scale && exp<=350 ){ scale *= 1e8; exp+=8; }
20420           while( realvalue>=10.0*scale && exp<=350 ){ scale *= 10.0; exp++; }
20421           realvalue /= scale;
20422           while( realvalue<1e-8 ){ realvalue *= 1e8; exp-=8; }
20423           while( realvalue<1.0 ){ realvalue *= 10.0; exp--; }
20424           if( exp>350 ){
20425             if( prefix=='-' ){
20426               bufpt = "-Inf";
20427             }else if( prefix=='+' ){
20428               bufpt = "+Inf";
20429             }else{
20430               bufpt = "Inf";
20431             }
20432             length = sqlite3Strlen30(bufpt);
20433             break;
20434           }
20435         }
20436         bufpt = buf;
20437         /*
20438         ** If the field type is etGENERIC, then convert to either etEXP
20439         ** or etFLOAT, as appropriate.
20440         */
20441         if( xtype!=etFLOAT ){
20442           realvalue += rounder;
20443           if( realvalue>=10.0 ){ realvalue *= 0.1; exp++; }
20444         }
20445         if( xtype==etGENERIC ){
20446           flag_rtz = !flag_alternateform;
20447           if( exp<-4 || exp>precision ){
20448             xtype = etEXP;
20449           }else{
20450             precision = precision - exp;
20451             xtype = etFLOAT;
20452           }
20453         }else{
20454           flag_rtz = flag_altform2;
20455         }
20456         if( xtype==etEXP ){
20457           e2 = 0;
20458         }else{
20459           e2 = exp;
20460         }
20461         if( MAX(e2,0)+precision+width > etBUFSIZE - 15 ){
20462           bufpt = zExtra = sqlite3Malloc( MAX(e2,0)+precision+width+15 );
20463           if( bufpt==0 ){
20464             setStrAccumError(pAccum, STRACCUM_NOMEM);
20465             return;
20466           }
20467         }
20468         zOut = bufpt;
20469         nsd = 16 + flag_altform2*10;
20470         flag_dp = (precision>0 ?1:0) | flag_alternateform | flag_altform2;
20471         /* The sign in front of the number */
20472         if( prefix ){
20473           *(bufpt++) = prefix;
20474         }
20475         /* Digits prior to the decimal point */
20476         if( e2<0 ){
20477           *(bufpt++) = '0';
20478         }else{
20479           for(; e2>=0; e2--){
20480             *(bufpt++) = et_getdigit(&realvalue,&nsd);
20481           }
20482         }
20483         /* The decimal point */
20484         if( flag_dp ){
20485           *(bufpt++) = '.';
20486         }
20487         /* "0" digits after the decimal point but before the first
20488         ** significant digit of the number */
20489         for(e2++; e2<0; precision--, e2++){
20490           assert( precision>0 );
20491           *(bufpt++) = '0';
20492         }
20493         /* Significant digits after the decimal point */
20494         while( (precision--)>0 ){
20495           *(bufpt++) = et_getdigit(&realvalue,&nsd);
20496         }
20497         /* Remove trailing zeros and the "." if no digits follow the "." */
20498         if( flag_rtz && flag_dp ){
20499           while( bufpt[-1]=='0' ) *(--bufpt) = 0;
20500           assert( bufpt>zOut );
20501           if( bufpt[-1]=='.' ){
20502             if( flag_altform2 ){
20503               *(bufpt++) = '0';
20504             }else{
20505               *(--bufpt) = 0;
20506             }
20507           }
20508         }
20509         /* Add the "eNNN" suffix */
20510         if( xtype==etEXP ){
20511           *(bufpt++) = aDigits[infop->charset];
20512           if( exp<0 ){
20513             *(bufpt++) = '-'; exp = -exp;
20514           }else{
20515             *(bufpt++) = '+';
20516           }
20517           if( exp>=100 ){
20518             *(bufpt++) = (char)((exp/100)+'0');        /* 100's digit */
20519             exp %= 100;
20520           }
20521           *(bufpt++) = (char)(exp/10+'0');             /* 10's digit */
20522           *(bufpt++) = (char)(exp%10+'0');             /* 1's digit */
20523         }
20524         *bufpt = 0;
20525 
20526         /* The converted number is in buf[] and zero terminated. Output it.
20527         ** Note that the number is in the usual order, not reversed as with
20528         ** integer conversions. */
20529         length = (int)(bufpt-zOut);
20530         bufpt = zOut;
20531 
20532         /* Special case:  Add leading zeros if the flag_zeropad flag is
20533         ** set and we are not left justified */
20534         if( flag_zeropad && !flag_leftjustify && length < width){
20535           int i;
20536           int nPad = width - length;
20537           for(i=width; i>=nPad; i--){
20538             bufpt[i] = bufpt[i-nPad];
20539           }
20540           i = prefix!=0;
20541           while( nPad-- ) bufpt[i++] = '0';
20542           length = width;
20543         }
20544 #endif /* !defined(SQLITE_OMIT_FLOATING_POINT) */
20545         break;
20546       case etSIZE:
20547         if( !bArgList ){
20548           *(va_arg(ap,int*)) = pAccum->nChar;
20549         }
20550         length = width = 0;
20551         break;
20552       case etPERCENT:
20553         buf[0] = '%';
20554         bufpt = buf;
20555         length = 1;
20556         break;
20557       case etCHARX:
20558         if( bArgList ){
20559           bufpt = getTextArg(pArgList);
20560           c = bufpt ? bufpt[0] : 0;
20561         }else{
20562           c = va_arg(ap,int);
20563         }
20564         buf[0] = (char)c;
20565         if( precision>=0 ){
20566           for(idx=1; idx<precision; idx++) buf[idx] = (char)c;
20567           length = precision;
20568         }else{
20569           length =1;
20570         }
20571         bufpt = buf;
20572         break;
20573       case etSTRING:
20574       case etDYNSTRING:
20575         if( bArgList ){
20576           bufpt = getTextArg(pArgList);
20577         }else{
20578           bufpt = va_arg(ap,char*);
20579         }
20580         if( bufpt==0 ){
20581           bufpt = "";
20582         }else if( xtype==etDYNSTRING && !bArgList ){
20583           zExtra = bufpt;
20584         }
20585         if( precision>=0 ){
20586           for(length=0; length<precision && bufpt[length]; length++){}
20587         }else{
20588           length = sqlite3Strlen30(bufpt);
20589         }
20590         break;
20591       case etSQLESCAPE:
20592       case etSQLESCAPE2:
20593       case etSQLESCAPE3: {
20594         int i, j, k, n, isnull;
20595         int needQuote;
20596         char ch;
20597         char q = ((xtype==etSQLESCAPE3)?'"':'\'');   /* Quote character */
20598         char *escarg;
20599 
20600         if( bArgList ){
20601           escarg = getTextArg(pArgList);
20602         }else{
20603           escarg = va_arg(ap,char*);
20604         }
20605         isnull = escarg==0;
20606         if( isnull ) escarg = (xtype==etSQLESCAPE2 ? "NULL" : "(NULL)");
20607         k = precision;
20608         for(i=n=0; k!=0 && (ch=escarg[i])!=0; i++, k--){
20609           if( ch==q )  n++;
20610         }
20611         needQuote = !isnull && xtype==etSQLESCAPE2;
20612         n += i + 1 + needQuote*2;
20613         if( n>etBUFSIZE ){
20614           bufpt = zExtra = sqlite3Malloc( n );
20615           if( bufpt==0 ){
20616             setStrAccumError(pAccum, STRACCUM_NOMEM);
20617             return;
20618           }
20619         }else{
20620           bufpt = buf;
20621         }
20622         j = 0;
20623         if( needQuote ) bufpt[j++] = q;
20624         k = i;
20625         for(i=0; i<k; i++){
20626           bufpt[j++] = ch = escarg[i];
20627           if( ch==q ) bufpt[j++] = ch;
20628         }
20629         if( needQuote ) bufpt[j++] = q;
20630         bufpt[j] = 0;
20631         length = j;
20632         /* The precision in %q and %Q means how many input characters to
20633         ** consume, not the length of the output...
20634         ** if( precision>=0 && precision<length ) length = precision; */
20635         break;
20636       }
20637       case etTOKEN: {
20638         Token *pToken = va_arg(ap, Token*);
20639         assert( bArgList==0 );
20640         if( pToken && pToken->n ){
20641           sqlite3StrAccumAppend(pAccum, (const char*)pToken->z, pToken->n);
20642         }
20643         length = width = 0;
20644         break;
20645       }
20646       case etSRCLIST: {
20647         SrcList *pSrc = va_arg(ap, SrcList*);
20648         int k = va_arg(ap, int);
20649         struct SrcList_item *pItem = &pSrc->a[k];
20650         assert( bArgList==0 );
20651         assert( k>=0 && k<pSrc->nSrc );
20652         if( pItem->zDatabase ){
20653           sqlite3StrAccumAppendAll(pAccum, pItem->zDatabase);
20654           sqlite3StrAccumAppend(pAccum, ".", 1);
20655         }
20656         sqlite3StrAccumAppendAll(pAccum, pItem->zName);
20657         length = width = 0;
20658         break;
20659       }
20660       default: {
20661         assert( xtype==etINVALID );
20662         return;
20663       }
20664     }/* End switch over the format type */
20665     /*
20666     ** The text of the conversion is pointed to by "bufpt" and is
20667     ** "length" characters long.  The field width is "width".  Do
20668     ** the output.
20669     */
20670     if( !flag_leftjustify ){
20671       register int nspace;
20672       nspace = width-length;
20673       if( nspace>0 ){
20674         sqlite3AppendSpace(pAccum, nspace);
20675       }
20676     }
20677     if( length>0 ){
20678       sqlite3StrAccumAppend(pAccum, bufpt, length);
20679     }
20680     if( flag_leftjustify ){
20681       register int nspace;
20682       nspace = width-length;
20683       if( nspace>0 ){
20684         sqlite3AppendSpace(pAccum, nspace);
20685       }
20686     }
20687     if( zExtra ) sqlite3_free(zExtra);
20688   }/* End for loop over the format string */
20689 } /* End of function */
20690 
20691 /*
20692 ** Append N bytes of text from z to the StrAccum object.
20693 */
20694 SQLITE_PRIVATE void sqlite3StrAccumAppend(StrAccum *p, const char *z, int N){
20695   assert( z!=0 );
20696   assert( p->zText!=0 || p->nChar==0 || p->accError );
20697   assert( N>=0 );
20698   assert( p->accError==0 || p->nAlloc==0 );
20699   if( p->nChar+N >= p->nAlloc ){
20700     char *zNew;
20701     if( p->accError ){
20702       testcase(p->accError==STRACCUM_TOOBIG);
20703       testcase(p->accError==STRACCUM_NOMEM);
20704       return;
20705     }
20706     if( !p->useMalloc ){
20707       N = p->nAlloc - p->nChar - 1;
20708       setStrAccumError(p, STRACCUM_TOOBIG);
20709       if( N<=0 ){
20710         return;
20711       }
20712     }else{
20713       char *zOld = (p->zText==p->zBase ? 0 : p->zText);
20714       i64 szNew = p->nChar;
20715       szNew += N + 1;
20716       if( szNew > p->mxAlloc ){
20717         sqlite3StrAccumReset(p);
20718         setStrAccumError(p, STRACCUM_TOOBIG);
20719         return;
20720       }else{
20721         p->nAlloc = (int)szNew;
20722       }
20723       if( p->useMalloc==1 ){
20724         zNew = sqlite3DbRealloc(p->db, zOld, p->nAlloc);
20725       }else{
20726         zNew = sqlite3_realloc(zOld, p->nAlloc);
20727       }
20728       if( zNew ){
20729         if( zOld==0 && p->nChar>0 ) memcpy(zNew, p->zText, p->nChar);
20730         p->zText = zNew;
20731       }else{
20732         sqlite3StrAccumReset(p);
20733         setStrAccumError(p, STRACCUM_NOMEM);
20734         return;
20735       }
20736     }
20737   }
20738   assert( p->zText );
20739   memcpy(&p->zText[p->nChar], z, N);
20740   p->nChar += N;
20741 }
20742 
20743 /*
20744 ** Append the complete text of zero-terminated string z[] to the p string.
20745 */
20746 SQLITE_PRIVATE void sqlite3StrAccumAppendAll(StrAccum *p, const char *z){
20747   sqlite3StrAccumAppend(p, z, sqlite3Strlen30(z));
20748 }
20749 
20750 
20751 /*
20752 ** Finish off a string by making sure it is zero-terminated.
20753 ** Return a pointer to the resulting string.  Return a NULL
20754 ** pointer if any kind of error was encountered.
20755 */
20756 SQLITE_PRIVATE char *sqlite3StrAccumFinish(StrAccum *p){
20757   if( p->zText ){
20758     p->zText[p->nChar] = 0;
20759     if( p->useMalloc && p->zText==p->zBase ){
20760       if( p->useMalloc==1 ){
20761         p->zText = sqlite3DbMallocRaw(p->db, p->nChar+1 );
20762       }else{
20763         p->zText = sqlite3_malloc(p->nChar+1);
20764       }
20765       if( p->zText ){
20766         memcpy(p->zText, p->zBase, p->nChar+1);
20767       }else{
20768         setStrAccumError(p, STRACCUM_NOMEM);
20769       }
20770     }
20771   }
20772   return p->zText;
20773 }
20774 
20775 /*
20776 ** Reset an StrAccum string.  Reclaim all malloced memory.
20777 */
20778 SQLITE_PRIVATE void sqlite3StrAccumReset(StrAccum *p){
20779   if( p->zText!=p->zBase ){
20780     if( p->useMalloc==1 ){
20781       sqlite3DbFree(p->db, p->zText);
20782     }else{
20783       sqlite3_free(p->zText);
20784     }
20785   }
20786   p->zText = 0;
20787 }
20788 
20789 /*
20790 ** Initialize a string accumulator
20791 */
20792 SQLITE_PRIVATE void sqlite3StrAccumInit(StrAccum *p, char *zBase, int n, int mx){
20793   p->zText = p->zBase = zBase;
20794   p->db = 0;
20795   p->nChar = 0;
20796   p->nAlloc = n;
20797   p->mxAlloc = mx;
20798   p->useMalloc = 1;
20799   p->accError = 0;
20800 }
20801 
20802 /*
20803 ** Print into memory obtained from sqliteMalloc().  Use the internal
20804 ** %-conversion extensions.
20805 */
20806 SQLITE_PRIVATE char *sqlite3VMPrintf(sqlite3 *db, const char *zFormat, va_list ap){
20807   char *z;
20808   char zBase[SQLITE_PRINT_BUF_SIZE];
20809   StrAccum acc;
20810   assert( db!=0 );
20811   sqlite3StrAccumInit(&acc, zBase, sizeof(zBase),
20812                       db->aLimit[SQLITE_LIMIT_LENGTH]);
20813   acc.db = db;
20814   sqlite3VXPrintf(&acc, SQLITE_PRINTF_INTERNAL, zFormat, ap);
20815   z = sqlite3StrAccumFinish(&acc);
20816   if( acc.accError==STRACCUM_NOMEM ){
20817     db->mallocFailed = 1;
20818   }
20819   return z;
20820 }
20821 
20822 /*
20823 ** Print into memory obtained from sqliteMalloc().  Use the internal
20824 ** %-conversion extensions.
20825 */
20826 SQLITE_PRIVATE char *sqlite3MPrintf(sqlite3 *db, const char *zFormat, ...){
20827   va_list ap;
20828   char *z;
20829   va_start(ap, zFormat);
20830   z = sqlite3VMPrintf(db, zFormat, ap);
20831   va_end(ap);
20832   return z;
20833 }
20834 
20835 /*
20836 ** Like sqlite3MPrintf(), but call sqlite3DbFree() on zStr after formatting
20837 ** the string and before returnning.  This routine is intended to be used
20838 ** to modify an existing string.  For example:
20839 **
20840 **       x = sqlite3MPrintf(db, x, "prefix %s suffix", x);
20841 **
20842 */
20843 SQLITE_PRIVATE char *sqlite3MAppendf(sqlite3 *db, char *zStr, const char *zFormat, ...){
20844   va_list ap;
20845   char *z;
20846   va_start(ap, zFormat);
20847   z = sqlite3VMPrintf(db, zFormat, ap);
20848   va_end(ap);
20849   sqlite3DbFree(db, zStr);
20850   return z;
20851 }
20852 
20853 /*
20854 ** Print into memory obtained from sqlite3_malloc().  Omit the internal
20855 ** %-conversion extensions.
20856 */
20857 SQLITE_API char *sqlite3_vmprintf(const char *zFormat, va_list ap){
20858   char *z;
20859   char zBase[SQLITE_PRINT_BUF_SIZE];
20860   StrAccum acc;
20861 #ifndef SQLITE_OMIT_AUTOINIT
20862   if( sqlite3_initialize() ) return 0;
20863 #endif
20864   sqlite3StrAccumInit(&acc, zBase, sizeof(zBase), SQLITE_MAX_LENGTH);
20865   acc.useMalloc = 2;
20866   sqlite3VXPrintf(&acc, 0, zFormat, ap);
20867   z = sqlite3StrAccumFinish(&acc);
20868   return z;
20869 }
20870 
20871 /*
20872 ** Print into memory obtained from sqlite3_malloc()().  Omit the internal
20873 ** %-conversion extensions.
20874 */
20875 SQLITE_API char *sqlite3_mprintf(const char *zFormat, ...){
20876   va_list ap;
20877   char *z;
20878 #ifndef SQLITE_OMIT_AUTOINIT
20879   if( sqlite3_initialize() ) return 0;
20880 #endif
20881   va_start(ap, zFormat);
20882   z = sqlite3_vmprintf(zFormat, ap);
20883   va_end(ap);
20884   return z;
20885 }
20886 
20887 /*
20888 ** sqlite3_snprintf() works like snprintf() except that it ignores the
20889 ** current locale settings.  This is important for SQLite because we
20890 ** are not able to use a "," as the decimal point in place of "." as
20891 ** specified by some locales.
20892 **
20893 ** Oops:  The first two arguments of sqlite3_snprintf() are backwards
20894 ** from the snprintf() standard.  Unfortunately, it is too late to change
20895 ** this without breaking compatibility, so we just have to live with the
20896 ** mistake.
20897 **
20898 ** sqlite3_vsnprintf() is the varargs version.
20899 */
20900 SQLITE_API char *sqlite3_vsnprintf(int n, char *zBuf, const char *zFormat, va_list ap){
20901   StrAccum acc;
20902   if( n<=0 ) return zBuf;
20903   sqlite3StrAccumInit(&acc, zBuf, n, 0);
20904   acc.useMalloc = 0;
20905   sqlite3VXPrintf(&acc, 0, zFormat, ap);
20906   return sqlite3StrAccumFinish(&acc);
20907 }
20908 SQLITE_API char *sqlite3_snprintf(int n, char *zBuf, const char *zFormat, ...){
20909   char *z;
20910   va_list ap;
20911   va_start(ap,zFormat);
20912   z = sqlite3_vsnprintf(n, zBuf, zFormat, ap);
20913   va_end(ap);
20914   return z;
20915 }
20916 
20917 /*
20918 ** This is the routine that actually formats the sqlite3_log() message.
20919 ** We house it in a separate routine from sqlite3_log() to avoid using
20920 ** stack space on small-stack systems when logging is disabled.
20921 **
20922 ** sqlite3_log() must render into a static buffer.  It cannot dynamically
20923 ** allocate memory because it might be called while the memory allocator
20924 ** mutex is held.
20925 */
20926 static void renderLogMsg(int iErrCode, const char *zFormat, va_list ap){
20927   StrAccum acc;                          /* String accumulator */
20928   char zMsg[SQLITE_PRINT_BUF_SIZE*3];    /* Complete log message */
20929 
20930   sqlite3StrAccumInit(&acc, zMsg, sizeof(zMsg), 0);
20931   acc.useMalloc = 0;
20932   sqlite3VXPrintf(&acc, 0, zFormat, ap);
20933   sqlite3GlobalConfig.xLog(sqlite3GlobalConfig.pLogArg, iErrCode,
20934                            sqlite3StrAccumFinish(&acc));
20935 }
20936 
20937 /*
20938 ** Format and write a message to the log if logging is enabled.
20939 */
20940 SQLITE_API void sqlite3_log(int iErrCode, const char *zFormat, ...){
20941   va_list ap;                             /* Vararg list */
20942   if( sqlite3GlobalConfig.xLog ){
20943     va_start(ap, zFormat);
20944     renderLogMsg(iErrCode, zFormat, ap);
20945     va_end(ap);
20946   }
20947 }
20948 
20949 #if defined(SQLITE_DEBUG)
20950 /*
20951 ** A version of printf() that understands %lld.  Used for debugging.
20952 ** The printf() built into some versions of windows does not understand %lld
20953 ** and segfaults if you give it a long long int.
20954 */
20955 SQLITE_PRIVATE void sqlite3DebugPrintf(const char *zFormat, ...){
20956   va_list ap;
20957   StrAccum acc;
20958   char zBuf[500];
20959   sqlite3StrAccumInit(&acc, zBuf, sizeof(zBuf), 0);
20960   acc.useMalloc = 0;
20961   va_start(ap,zFormat);
20962   sqlite3VXPrintf(&acc, 0, zFormat, ap);
20963   va_end(ap);
20964   sqlite3StrAccumFinish(&acc);
20965   fprintf(stdout,"%s", zBuf);
20966   fflush(stdout);
20967 }
20968 #endif
20969 
20970 /*
20971 ** variable-argument wrapper around sqlite3VXPrintf().
20972 */
20973 SQLITE_PRIVATE void sqlite3XPrintf(StrAccum *p, u32 bFlags, const char *zFormat, ...){
20974   va_list ap;
20975   va_start(ap,zFormat);
20976   sqlite3VXPrintf(p, bFlags, zFormat, ap);
20977   va_end(ap);
20978 }
20979 
20980 /************** End of printf.c **********************************************/
20981 /************** Begin file random.c ******************************************/
20982 /*
20983 ** 2001 September 15
20984 **
20985 ** The author disclaims copyright to this source code.  In place of
20986 ** a legal notice, here is a blessing:
20987 **
20988 **    May you do good and not evil.
20989 **    May you find forgiveness for yourself and forgive others.
20990 **    May you share freely, never taking more than you give.
20991 **
20992 *************************************************************************
20993 ** This file contains code to implement a pseudo-random number
20994 ** generator (PRNG) for SQLite.
20995 **
20996 ** Random numbers are used by some of the database backends in order
20997 ** to generate random integer keys for tables or random filenames.
20998 */
20999 
21000 
21001 /* All threads share a single random number generator.
21002 ** This structure is the current state of the generator.
21003 */
21004 static SQLITE_WSD struct sqlite3PrngType {
21005   unsigned char isInit;          /* True if initialized */
21006   unsigned char i, j;            /* State variables */
21007   unsigned char s[256];          /* State variables */
21008 } sqlite3Prng;
21009 
21010 /*
21011 ** Return N random bytes.
21012 */
21013 SQLITE_API void sqlite3_randomness(int N, void *pBuf){
21014   unsigned char t;
21015   unsigned char *zBuf = pBuf;
21016 
21017   /* The "wsdPrng" macro will resolve to the pseudo-random number generator
21018   ** state vector.  If writable static data is unsupported on the target,
21019   ** we have to locate the state vector at run-time.  In the more common
21020   ** case where writable static data is supported, wsdPrng can refer directly
21021   ** to the "sqlite3Prng" state vector declared above.
21022   */
21023 #ifdef SQLITE_OMIT_WSD
21024   struct sqlite3PrngType *p = &GLOBAL(struct sqlite3PrngType, sqlite3Prng);
21025 # define wsdPrng p[0]
21026 #else
21027 # define wsdPrng sqlite3Prng
21028 #endif
21029 
21030 #if SQLITE_THREADSAFE
21031   sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_PRNG);
21032   sqlite3_mutex_enter(mutex);
21033 #endif
21034 
21035   if( N<=0 ){
21036     wsdPrng.isInit = 0;
21037     sqlite3_mutex_leave(mutex);
21038     return;
21039   }
21040 
21041   /* Initialize the state of the random number generator once,
21042   ** the first time this routine is called.  The seed value does
21043   ** not need to contain a lot of randomness since we are not
21044   ** trying to do secure encryption or anything like that...
21045   **
21046   ** Nothing in this file or anywhere else in SQLite does any kind of
21047   ** encryption.  The RC4 algorithm is being used as a PRNG (pseudo-random
21048   ** number generator) not as an encryption device.
21049   */
21050   if( !wsdPrng.isInit ){
21051     int i;
21052     char k[256];
21053     wsdPrng.j = 0;
21054     wsdPrng.i = 0;
21055     sqlite3OsRandomness(sqlite3_vfs_find(0), 256, k);
21056     for(i=0; i<256; i++){
21057       wsdPrng.s[i] = (u8)i;
21058     }
21059     for(i=0; i<256; i++){
21060       wsdPrng.j += wsdPrng.s[i] + k[i];
21061       t = wsdPrng.s[wsdPrng.j];
21062       wsdPrng.s[wsdPrng.j] = wsdPrng.s[i];
21063       wsdPrng.s[i] = t;
21064     }
21065     wsdPrng.isInit = 1;
21066   }
21067 
21068   assert( N>0 );
21069   do{
21070     wsdPrng.i++;
21071     t = wsdPrng.s[wsdPrng.i];
21072     wsdPrng.j += t;
21073     wsdPrng.s[wsdPrng.i] = wsdPrng.s[wsdPrng.j];
21074     wsdPrng.s[wsdPrng.j] = t;
21075     t += wsdPrng.s[wsdPrng.i];
21076     *(zBuf++) = wsdPrng.s[t];
21077   }while( --N );
21078   sqlite3_mutex_leave(mutex);
21079 }
21080 
21081 #ifndef SQLITE_OMIT_BUILTIN_TEST
21082 /*
21083 ** For testing purposes, we sometimes want to preserve the state of
21084 ** PRNG and restore the PRNG to its saved state at a later time, or
21085 ** to reset the PRNG to its initial state.  These routines accomplish
21086 ** those tasks.
21087 **
21088 ** The sqlite3_test_control() interface calls these routines to
21089 ** control the PRNG.
21090 */
21091 static SQLITE_WSD struct sqlite3PrngType sqlite3SavedPrng;
21092 SQLITE_PRIVATE void sqlite3PrngSaveState(void){
21093   memcpy(
21094     &GLOBAL(struct sqlite3PrngType, sqlite3SavedPrng),
21095     &GLOBAL(struct sqlite3PrngType, sqlite3Prng),
21096     sizeof(sqlite3Prng)
21097   );
21098 }
21099 SQLITE_PRIVATE void sqlite3PrngRestoreState(void){
21100   memcpy(
21101     &GLOBAL(struct sqlite3PrngType, sqlite3Prng),
21102     &GLOBAL(struct sqlite3PrngType, sqlite3SavedPrng),
21103     sizeof(sqlite3Prng)
21104   );
21105 }
21106 #endif /* SQLITE_OMIT_BUILTIN_TEST */
21107 
21108 /************** End of random.c **********************************************/
21109 /************** Begin file utf.c *********************************************/
21110 /*
21111 ** 2004 April 13
21112 **
21113 ** The author disclaims copyright to this source code.  In place of
21114 ** a legal notice, here is a blessing:
21115 **
21116 **    May you do good and not evil.
21117 **    May you find forgiveness for yourself and forgive others.
21118 **    May you share freely, never taking more than you give.
21119 **
21120 *************************************************************************
21121 ** This file contains routines used to translate between UTF-8,
21122 ** UTF-16, UTF-16BE, and UTF-16LE.
21123 **
21124 ** Notes on UTF-8:
21125 **
21126 **   Byte-0    Byte-1    Byte-2    Byte-3    Value
21127 **  0xxxxxxx                                 00000000 00000000 0xxxxxxx
21128 **  110yyyyy  10xxxxxx                       00000000 00000yyy yyxxxxxx
21129 **  1110zzzz  10yyyyyy  10xxxxxx             00000000 zzzzyyyy yyxxxxxx
21130 **  11110uuu  10uuzzzz  10yyyyyy  10xxxxxx   000uuuuu zzzzyyyy yyxxxxxx
21131 **
21132 **
21133 ** Notes on UTF-16:  (with wwww+1==uuuuu)
21134 **
21135 **      Word-0               Word-1          Value
21136 **  110110ww wwzzzzyy   110111yy yyxxxxxx    000uuuuu zzzzyyyy yyxxxxxx
21137 **  zzzzyyyy yyxxxxxx                        00000000 zzzzyyyy yyxxxxxx
21138 **
21139 **
21140 ** BOM or Byte Order Mark:
21141 **     0xff 0xfe   little-endian utf-16 follows
21142 **     0xfe 0xff   big-endian utf-16 follows
21143 **
21144 */
21145 /* #include <assert.h> */
21146 
21147 #ifndef SQLITE_AMALGAMATION
21148 /*
21149 ** The following constant value is used by the SQLITE_BIGENDIAN and
21150 ** SQLITE_LITTLEENDIAN macros.
21151 */
21152 SQLITE_PRIVATE const int sqlite3one = 1;
21153 #endif /* SQLITE_AMALGAMATION */
21154 
21155 /*
21156 ** This lookup table is used to help decode the first byte of
21157 ** a multi-byte UTF8 character.
21158 */
21159 static const unsigned char sqlite3Utf8Trans1[] = {
21160   0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
21161   0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
21162   0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
21163   0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
21164   0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
21165   0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
21166   0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
21167   0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x00, 0x00,
21168 };
21169 
21170 
21171 #define WRITE_UTF8(zOut, c) {                          \
21172   if( c<0x00080 ){                                     \
21173     *zOut++ = (u8)(c&0xFF);                            \
21174   }                                                    \
21175   else if( c<0x00800 ){                                \
21176     *zOut++ = 0xC0 + (u8)((c>>6)&0x1F);                \
21177     *zOut++ = 0x80 + (u8)(c & 0x3F);                   \
21178   }                                                    \
21179   else if( c<0x10000 ){                                \
21180     *zOut++ = 0xE0 + (u8)((c>>12)&0x0F);               \
21181     *zOut++ = 0x80 + (u8)((c>>6) & 0x3F);              \
21182     *zOut++ = 0x80 + (u8)(c & 0x3F);                   \
21183   }else{                                               \
21184     *zOut++ = 0xF0 + (u8)((c>>18) & 0x07);             \
21185     *zOut++ = 0x80 + (u8)((c>>12) & 0x3F);             \
21186     *zOut++ = 0x80 + (u8)((c>>6) & 0x3F);              \
21187     *zOut++ = 0x80 + (u8)(c & 0x3F);                   \
21188   }                                                    \
21189 }
21190 
21191 #define WRITE_UTF16LE(zOut, c) {                                    \
21192   if( c<=0xFFFF ){                                                  \
21193     *zOut++ = (u8)(c&0x00FF);                                       \
21194     *zOut++ = (u8)((c>>8)&0x00FF);                                  \
21195   }else{                                                            \
21196     *zOut++ = (u8)(((c>>10)&0x003F) + (((c-0x10000)>>10)&0x00C0));  \
21197     *zOut++ = (u8)(0x00D8 + (((c-0x10000)>>18)&0x03));              \
21198     *zOut++ = (u8)(c&0x00FF);                                       \
21199     *zOut++ = (u8)(0x00DC + ((c>>8)&0x03));                         \
21200   }                                                                 \
21201 }
21202 
21203 #define WRITE_UTF16BE(zOut, c) {                                    \
21204   if( c<=0xFFFF ){                                                  \
21205     *zOut++ = (u8)((c>>8)&0x00FF);                                  \
21206     *zOut++ = (u8)(c&0x00FF);                                       \
21207   }else{                                                            \
21208     *zOut++ = (u8)(0x00D8 + (((c-0x10000)>>18)&0x03));              \
21209     *zOut++ = (u8)(((c>>10)&0x003F) + (((c-0x10000)>>10)&0x00C0));  \
21210     *zOut++ = (u8)(0x00DC + ((c>>8)&0x03));                         \
21211     *zOut++ = (u8)(c&0x00FF);                                       \
21212   }                                                                 \
21213 }
21214 
21215 #define READ_UTF16LE(zIn, TERM, c){                                   \
21216   c = (*zIn++);                                                       \
21217   c += ((*zIn++)<<8);                                                 \
21218   if( c>=0xD800 && c<0xE000 && TERM ){                                \
21219     int c2 = (*zIn++);                                                \
21220     c2 += ((*zIn++)<<8);                                              \
21221     c = (c2&0x03FF) + ((c&0x003F)<<10) + (((c&0x03C0)+0x0040)<<10);   \
21222   }                                                                   \
21223 }
21224 
21225 #define READ_UTF16BE(zIn, TERM, c){                                   \
21226   c = ((*zIn++)<<8);                                                  \
21227   c += (*zIn++);                                                      \
21228   if( c>=0xD800 && c<0xE000 && TERM ){                                \
21229     int c2 = ((*zIn++)<<8);                                           \
21230     c2 += (*zIn++);                                                   \
21231     c = (c2&0x03FF) + ((c&0x003F)<<10) + (((c&0x03C0)+0x0040)<<10);   \
21232   }                                                                   \
21233 }
21234 
21235 /*
21236 ** Translate a single UTF-8 character.  Return the unicode value.
21237 **
21238 ** During translation, assume that the byte that zTerm points
21239 ** is a 0x00.
21240 **
21241 ** Write a pointer to the next unread byte back into *pzNext.
21242 **
21243 ** Notes On Invalid UTF-8:
21244 **
21245 **  *  This routine never allows a 7-bit character (0x00 through 0x7f) to
21246 **     be encoded as a multi-byte character.  Any multi-byte character that
21247 **     attempts to encode a value between 0x00 and 0x7f is rendered as 0xfffd.
21248 **
21249 **  *  This routine never allows a UTF16 surrogate value to be encoded.
21250 **     If a multi-byte character attempts to encode a value between
21251 **     0xd800 and 0xe000 then it is rendered as 0xfffd.
21252 **
21253 **  *  Bytes in the range of 0x80 through 0xbf which occur as the first
21254 **     byte of a character are interpreted as single-byte characters
21255 **     and rendered as themselves even though they are technically
21256 **     invalid characters.
21257 **
21258 **  *  This routine accepts an infinite number of different UTF8 encodings
21259 **     for unicode values 0x80 and greater.  It do not change over-length
21260 **     encodings to 0xfffd as some systems recommend.
21261 */
21262 #define READ_UTF8(zIn, zTerm, c)                           \
21263   c = *(zIn++);                                            \
21264   if( c>=0xc0 ){                                           \
21265     c = sqlite3Utf8Trans1[c-0xc0];                         \
21266     while( zIn!=zTerm && (*zIn & 0xc0)==0x80 ){            \
21267       c = (c<<6) + (0x3f & *(zIn++));                      \
21268     }                                                      \
21269     if( c<0x80                                             \
21270         || (c&0xFFFFF800)==0xD800                          \
21271         || (c&0xFFFFFFFE)==0xFFFE ){  c = 0xFFFD; }        \
21272   }
21273 SQLITE_PRIVATE u32 sqlite3Utf8Read(
21274   const unsigned char **pz    /* Pointer to string from which to read char */
21275 ){
21276   unsigned int c;
21277 
21278   /* Same as READ_UTF8() above but without the zTerm parameter.
21279   ** For this routine, we assume the UTF8 string is always zero-terminated.
21280   */
21281   c = *((*pz)++);
21282   if( c>=0xc0 ){
21283     c = sqlite3Utf8Trans1[c-0xc0];
21284     while( (*(*pz) & 0xc0)==0x80 ){
21285       c = (c<<6) + (0x3f & *((*pz)++));
21286     }
21287     if( c<0x80
21288         || (c&0xFFFFF800)==0xD800
21289         || (c&0xFFFFFFFE)==0xFFFE ){  c = 0xFFFD; }
21290   }
21291   return c;
21292 }
21293 
21294 
21295 
21296 
21297 /*
21298 ** If the TRANSLATE_TRACE macro is defined, the value of each Mem is
21299 ** printed on stderr on the way into and out of sqlite3VdbeMemTranslate().
21300 */
21301 /* #define TRANSLATE_TRACE 1 */
21302 
21303 #ifndef SQLITE_OMIT_UTF16
21304 /*
21305 ** This routine transforms the internal text encoding used by pMem to
21306 ** desiredEnc. It is an error if the string is already of the desired
21307 ** encoding, or if *pMem does not contain a string value.
21308 */
21309 SQLITE_PRIVATE int sqlite3VdbeMemTranslate(Mem *pMem, u8 desiredEnc){
21310   int len;                    /* Maximum length of output string in bytes */
21311   unsigned char *zOut;                  /* Output buffer */
21312   unsigned char *zIn;                   /* Input iterator */
21313   unsigned char *zTerm;                 /* End of input */
21314   unsigned char *z;                     /* Output iterator */
21315   unsigned int c;
21316 
21317   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
21318   assert( pMem->flags&MEM_Str );
21319   assert( pMem->enc!=desiredEnc );
21320   assert( pMem->enc!=0 );
21321   assert( pMem->n>=0 );
21322 
21323 #if defined(TRANSLATE_TRACE) && defined(SQLITE_DEBUG)
21324   {
21325     char zBuf[100];
21326     sqlite3VdbeMemPrettyPrint(pMem, zBuf);
21327     fprintf(stderr, "INPUT:  %s\n", zBuf);
21328   }
21329 #endif
21330 
21331   /* If the translation is between UTF-16 little and big endian, then
21332   ** all that is required is to swap the byte order. This case is handled
21333   ** differently from the others.
21334   */
21335   if( pMem->enc!=SQLITE_UTF8 && desiredEnc!=SQLITE_UTF8 ){
21336     u8 temp;
21337     int rc;
21338     rc = sqlite3VdbeMemMakeWriteable(pMem);
21339     if( rc!=SQLITE_OK ){
21340       assert( rc==SQLITE_NOMEM );
21341       return SQLITE_NOMEM;
21342     }
21343     zIn = (u8*)pMem->z;
21344     zTerm = &zIn[pMem->n&~1];
21345     while( zIn<zTerm ){
21346       temp = *zIn;
21347       *zIn = *(zIn+1);
21348       zIn++;
21349       *zIn++ = temp;
21350     }
21351     pMem->enc = desiredEnc;
21352     goto translate_out;
21353   }
21354 
21355   /* Set len to the maximum number of bytes required in the output buffer. */
21356   if( desiredEnc==SQLITE_UTF8 ){
21357     /* When converting from UTF-16, the maximum growth results from
21358     ** translating a 2-byte character to a 4-byte UTF-8 character.
21359     ** A single byte is required for the output string
21360     ** nul-terminator.
21361     */
21362     pMem->n &= ~1;
21363     len = pMem->n * 2 + 1;
21364   }else{
21365     /* When converting from UTF-8 to UTF-16 the maximum growth is caused
21366     ** when a 1-byte UTF-8 character is translated into a 2-byte UTF-16
21367     ** character. Two bytes are required in the output buffer for the
21368     ** nul-terminator.
21369     */
21370     len = pMem->n * 2 + 2;
21371   }
21372 
21373   /* Set zIn to point at the start of the input buffer and zTerm to point 1
21374   ** byte past the end.
21375   **
21376   ** Variable zOut is set to point at the output buffer, space obtained
21377   ** from sqlite3_malloc().
21378   */
21379   zIn = (u8*)pMem->z;
21380   zTerm = &zIn[pMem->n];
21381   zOut = sqlite3DbMallocRaw(pMem->db, len);
21382   if( !zOut ){
21383     return SQLITE_NOMEM;
21384   }
21385   z = zOut;
21386 
21387   if( pMem->enc==SQLITE_UTF8 ){
21388     if( desiredEnc==SQLITE_UTF16LE ){
21389       /* UTF-8 -> UTF-16 Little-endian */
21390       while( zIn<zTerm ){
21391         READ_UTF8(zIn, zTerm, c);
21392         WRITE_UTF16LE(z, c);
21393       }
21394     }else{
21395       assert( desiredEnc==SQLITE_UTF16BE );
21396       /* UTF-8 -> UTF-16 Big-endian */
21397       while( zIn<zTerm ){
21398         READ_UTF8(zIn, zTerm, c);
21399         WRITE_UTF16BE(z, c);
21400       }
21401     }
21402     pMem->n = (int)(z - zOut);
21403     *z++ = 0;
21404   }else{
21405     assert( desiredEnc==SQLITE_UTF8 );
21406     if( pMem->enc==SQLITE_UTF16LE ){
21407       /* UTF-16 Little-endian -> UTF-8 */
21408       while( zIn<zTerm ){
21409         READ_UTF16LE(zIn, zIn<zTerm, c);
21410         WRITE_UTF8(z, c);
21411       }
21412     }else{
21413       /* UTF-16 Big-endian -> UTF-8 */
21414       while( zIn<zTerm ){
21415         READ_UTF16BE(zIn, zIn<zTerm, c);
21416         WRITE_UTF8(z, c);
21417       }
21418     }
21419     pMem->n = (int)(z - zOut);
21420   }
21421   *z = 0;
21422   assert( (pMem->n+(desiredEnc==SQLITE_UTF8?1:2))<=len );
21423 
21424   sqlite3VdbeMemRelease(pMem);
21425   pMem->flags &= ~(MEM_Static|MEM_Dyn|MEM_Ephem);
21426   pMem->enc = desiredEnc;
21427   pMem->flags |= (MEM_Term|MEM_Dyn);
21428   pMem->z = (char*)zOut;
21429   pMem->zMalloc = pMem->z;
21430 
21431 translate_out:
21432 #if defined(TRANSLATE_TRACE) && defined(SQLITE_DEBUG)
21433   {
21434     char zBuf[100];
21435     sqlite3VdbeMemPrettyPrint(pMem, zBuf);
21436     fprintf(stderr, "OUTPUT: %s\n", zBuf);
21437   }
21438 #endif
21439   return SQLITE_OK;
21440 }
21441 
21442 /*
21443 ** This routine checks for a byte-order mark at the beginning of the
21444 ** UTF-16 string stored in *pMem. If one is present, it is removed and
21445 ** the encoding of the Mem adjusted. This routine does not do any
21446 ** byte-swapping, it just sets Mem.enc appropriately.
21447 **
21448 ** The allocation (static, dynamic etc.) and encoding of the Mem may be
21449 ** changed by this function.
21450 */
21451 SQLITE_PRIVATE int sqlite3VdbeMemHandleBom(Mem *pMem){
21452   int rc = SQLITE_OK;
21453   u8 bom = 0;
21454 
21455   assert( pMem->n>=0 );
21456   if( pMem->n>1 ){
21457     u8 b1 = *(u8 *)pMem->z;
21458     u8 b2 = *(((u8 *)pMem->z) + 1);
21459     if( b1==0xFE && b2==0xFF ){
21460       bom = SQLITE_UTF16BE;
21461     }
21462     if( b1==0xFF && b2==0xFE ){
21463       bom = SQLITE_UTF16LE;
21464     }
21465   }
21466 
21467   if( bom ){
21468     rc = sqlite3VdbeMemMakeWriteable(pMem);
21469     if( rc==SQLITE_OK ){
21470       pMem->n -= 2;
21471       memmove(pMem->z, &pMem->z[2], pMem->n);
21472       pMem->z[pMem->n] = '\0';
21473       pMem->z[pMem->n+1] = '\0';
21474       pMem->flags |= MEM_Term;
21475       pMem->enc = bom;
21476     }
21477   }
21478   return rc;
21479 }
21480 #endif /* SQLITE_OMIT_UTF16 */
21481 
21482 /*
21483 ** pZ is a UTF-8 encoded unicode string. If nByte is less than zero,
21484 ** return the number of unicode characters in pZ up to (but not including)
21485 ** the first 0x00 byte. If nByte is not less than zero, return the
21486 ** number of unicode characters in the first nByte of pZ (or up to
21487 ** the first 0x00, whichever comes first).
21488 */
21489 SQLITE_PRIVATE int sqlite3Utf8CharLen(const char *zIn, int nByte){
21490   int r = 0;
21491   const u8 *z = (const u8*)zIn;
21492   const u8 *zTerm;
21493   if( nByte>=0 ){
21494     zTerm = &z[nByte];
21495   }else{
21496     zTerm = (const u8*)(-1);
21497   }
21498   assert( z<=zTerm );
21499   while( *z!=0 && z<zTerm ){
21500     SQLITE_SKIP_UTF8(z);
21501     r++;
21502   }
21503   return r;
21504 }
21505 
21506 /* This test function is not currently used by the automated test-suite.
21507 ** Hence it is only available in debug builds.
21508 */
21509 #if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
21510 /*
21511 ** Translate UTF-8 to UTF-8.
21512 **
21513 ** This has the effect of making sure that the string is well-formed
21514 ** UTF-8.  Miscoded characters are removed.
21515 **
21516 ** The translation is done in-place and aborted if the output
21517 ** overruns the input.
21518 */
21519 SQLITE_PRIVATE int sqlite3Utf8To8(unsigned char *zIn){
21520   unsigned char *zOut = zIn;
21521   unsigned char *zStart = zIn;
21522   u32 c;
21523 
21524   while( zIn[0] && zOut<=zIn ){
21525     c = sqlite3Utf8Read((const u8**)&zIn);
21526     if( c!=0xfffd ){
21527       WRITE_UTF8(zOut, c);
21528     }
21529   }
21530   *zOut = 0;
21531   return (int)(zOut - zStart);
21532 }
21533 #endif
21534 
21535 #ifndef SQLITE_OMIT_UTF16
21536 /*
21537 ** Convert a UTF-16 string in the native encoding into a UTF-8 string.
21538 ** Memory to hold the UTF-8 string is obtained from sqlite3_malloc and must
21539 ** be freed by the calling function.
21540 **
21541 ** NULL is returned if there is an allocation error.
21542 */
21543 SQLITE_PRIVATE char *sqlite3Utf16to8(sqlite3 *db, const void *z, int nByte, u8 enc){
21544   Mem m;
21545   memset(&m, 0, sizeof(m));
21546   m.db = db;
21547   sqlite3VdbeMemSetStr(&m, z, nByte, enc, SQLITE_STATIC);
21548   sqlite3VdbeChangeEncoding(&m, SQLITE_UTF8);
21549   if( db->mallocFailed ){
21550     sqlite3VdbeMemRelease(&m);
21551     m.z = 0;
21552   }
21553   assert( (m.flags & MEM_Term)!=0 || db->mallocFailed );
21554   assert( (m.flags & MEM_Str)!=0 || db->mallocFailed );
21555   assert( (m.flags & MEM_Dyn)!=0 || db->mallocFailed );
21556   assert( m.z || db->mallocFailed );
21557   return m.z;
21558 }
21559 
21560 /*
21561 ** zIn is a UTF-16 encoded unicode string at least nChar characters long.
21562 ** Return the number of bytes in the first nChar unicode characters
21563 ** in pZ.  nChar must be non-negative.
21564 */
21565 SQLITE_PRIVATE int sqlite3Utf16ByteLen(const void *zIn, int nChar){
21566   int c;
21567   unsigned char const *z = zIn;
21568   int n = 0;
21569 
21570   if( SQLITE_UTF16NATIVE==SQLITE_UTF16BE ){
21571     while( n<nChar ){
21572       READ_UTF16BE(z, 1, c);
21573       n++;
21574     }
21575   }else{
21576     while( n<nChar ){
21577       READ_UTF16LE(z, 1, c);
21578       n++;
21579     }
21580   }
21581   return (int)(z-(unsigned char const *)zIn);
21582 }
21583 
21584 #if defined(SQLITE_TEST)
21585 /*
21586 ** This routine is called from the TCL test function "translate_selftest".
21587 ** It checks that the primitives for serializing and deserializing
21588 ** characters in each encoding are inverses of each other.
21589 */
21590 SQLITE_PRIVATE void sqlite3UtfSelfTest(void){
21591   unsigned int i, t;
21592   unsigned char zBuf[20];
21593   unsigned char *z;
21594   int n;
21595   unsigned int c;
21596 
21597   for(i=0; i<0x00110000; i++){
21598     z = zBuf;
21599     WRITE_UTF8(z, i);
21600     n = (int)(z-zBuf);
21601     assert( n>0 && n<=4 );
21602     z[0] = 0;
21603     z = zBuf;
21604     c = sqlite3Utf8Read((const u8**)&z);
21605     t = i;
21606     if( i>=0xD800 && i<=0xDFFF ) t = 0xFFFD;
21607     if( (i&0xFFFFFFFE)==0xFFFE ) t = 0xFFFD;
21608     assert( c==t );
21609     assert( (z-zBuf)==n );
21610   }
21611   for(i=0; i<0x00110000; i++){
21612     if( i>=0xD800 && i<0xE000 ) continue;
21613     z = zBuf;
21614     WRITE_UTF16LE(z, i);
21615     n = (int)(z-zBuf);
21616     assert( n>0 && n<=4 );
21617     z[0] = 0;
21618     z = zBuf;
21619     READ_UTF16LE(z, 1, c);
21620     assert( c==i );
21621     assert( (z-zBuf)==n );
21622   }
21623   for(i=0; i<0x00110000; i++){
21624     if( i>=0xD800 && i<0xE000 ) continue;
21625     z = zBuf;
21626     WRITE_UTF16BE(z, i);
21627     n = (int)(z-zBuf);
21628     assert( n>0 && n<=4 );
21629     z[0] = 0;
21630     z = zBuf;
21631     READ_UTF16BE(z, 1, c);
21632     assert( c==i );
21633     assert( (z-zBuf)==n );
21634   }
21635 }
21636 #endif /* SQLITE_TEST */
21637 #endif /* SQLITE_OMIT_UTF16 */
21638 
21639 /************** End of utf.c *************************************************/
21640 /************** Begin file util.c ********************************************/
21641 /*
21642 ** 2001 September 15
21643 **
21644 ** The author disclaims copyright to this source code.  In place of
21645 ** a legal notice, here is a blessing:
21646 **
21647 **    May you do good and not evil.
21648 **    May you find forgiveness for yourself and forgive others.
21649 **    May you share freely, never taking more than you give.
21650 **
21651 *************************************************************************
21652 ** Utility functions used throughout sqlite.
21653 **
21654 ** This file contains functions for allocating memory, comparing
21655 ** strings, and stuff like that.
21656 **
21657 */
21658 /* #include <stdarg.h> */
21659 #ifdef SQLITE_HAVE_ISNAN
21660 # include <math.h>
21661 #endif
21662 
21663 /*
21664 ** Routine needed to support the testcase() macro.
21665 */
21666 #ifdef SQLITE_COVERAGE_TEST
21667 SQLITE_PRIVATE void sqlite3Coverage(int x){
21668   static unsigned dummy = 0;
21669   dummy += (unsigned)x;
21670 }
21671 #endif
21672 
21673 #ifndef SQLITE_OMIT_FLOATING_POINT
21674 /*
21675 ** Return true if the floating point value is Not a Number (NaN).
21676 **
21677 ** Use the math library isnan() function if compiled with SQLITE_HAVE_ISNAN.
21678 ** Otherwise, we have our own implementation that works on most systems.
21679 */
21680 SQLITE_PRIVATE int sqlite3IsNaN(double x){
21681   int rc;   /* The value return */
21682 #if !defined(SQLITE_HAVE_ISNAN)
21683   /*
21684   ** Systems that support the isnan() library function should probably
21685   ** make use of it by compiling with -DSQLITE_HAVE_ISNAN.  But we have
21686   ** found that many systems do not have a working isnan() function so
21687   ** this implementation is provided as an alternative.
21688   **
21689   ** This NaN test sometimes fails if compiled on GCC with -ffast-math.
21690   ** On the other hand, the use of -ffast-math comes with the following
21691   ** warning:
21692   **
21693   **      This option [-ffast-math] should never be turned on by any
21694   **      -O option since it can result in incorrect output for programs
21695   **      which depend on an exact implementation of IEEE or ISO
21696   **      rules/specifications for math functions.
21697   **
21698   ** Under MSVC, this NaN test may fail if compiled with a floating-
21699   ** point precision mode other than /fp:precise.  From the MSDN
21700   ** documentation:
21701   **
21702   **      The compiler [with /fp:precise] will properly handle comparisons
21703   **      involving NaN. For example, x != x evaluates to true if x is NaN
21704   **      ...
21705   */
21706 #ifdef __FAST_MATH__
21707 # error SQLite will not work correctly with the -ffast-math option of GCC.
21708 #endif
21709   volatile double y = x;
21710   volatile double z = y;
21711   rc = (y!=z);
21712 #else  /* if defined(SQLITE_HAVE_ISNAN) */
21713   rc = isnan(x);
21714 #endif /* SQLITE_HAVE_ISNAN */
21715   testcase( rc );
21716   return rc;
21717 }
21718 #endif /* SQLITE_OMIT_FLOATING_POINT */
21719 
21720 /*
21721 ** Compute a string length that is limited to what can be stored in
21722 ** lower 30 bits of a 32-bit signed integer.
21723 **
21724 ** The value returned will never be negative.  Nor will it ever be greater
21725 ** than the actual length of the string.  For very long strings (greater
21726 ** than 1GiB) the value returned might be less than the true string length.
21727 */
21728 SQLITE_PRIVATE int sqlite3Strlen30(const char *z){
21729   const char *z2 = z;
21730   if( z==0 ) return 0;
21731   while( *z2 ){ z2++; }
21732   return 0x3fffffff & (int)(z2 - z);
21733 }
21734 
21735 /*
21736 ** Set the most recent error code and error string for the sqlite
21737 ** handle "db". The error code is set to "err_code".
21738 **
21739 ** If it is not NULL, string zFormat specifies the format of the
21740 ** error string in the style of the printf functions: The following
21741 ** format characters are allowed:
21742 **
21743 **      %s      Insert a string
21744 **      %z      A string that should be freed after use
21745 **      %d      Insert an integer
21746 **      %T      Insert a token
21747 **      %S      Insert the first element of a SrcList
21748 **
21749 ** zFormat and any string tokens that follow it are assumed to be
21750 ** encoded in UTF-8.
21751 **
21752 ** To clear the most recent error for sqlite handle "db", sqlite3Error
21753 ** should be called with err_code set to SQLITE_OK and zFormat set
21754 ** to NULL.
21755 */
21756 SQLITE_PRIVATE void sqlite3Error(sqlite3 *db, int err_code, const char *zFormat, ...){
21757   assert( db!=0 );
21758   db->errCode = err_code;
21759   if( zFormat && (db->pErr || (db->pErr = sqlite3ValueNew(db))!=0) ){
21760     char *z;
21761     va_list ap;
21762     va_start(ap, zFormat);
21763     z = sqlite3VMPrintf(db, zFormat, ap);
21764     va_end(ap);
21765     sqlite3ValueSetStr(db->pErr, -1, z, SQLITE_UTF8, SQLITE_DYNAMIC);
21766   }else if( db->pErr ){
21767     sqlite3ValueSetNull(db->pErr);
21768   }
21769 }
21770 
21771 /*
21772 ** Add an error message to pParse->zErrMsg and increment pParse->nErr.
21773 ** The following formatting characters are allowed:
21774 **
21775 **      %s      Insert a string
21776 **      %z      A string that should be freed after use
21777 **      %d      Insert an integer
21778 **      %T      Insert a token
21779 **      %S      Insert the first element of a SrcList
21780 **
21781 ** This function should be used to report any error that occurs whilst
21782 ** compiling an SQL statement (i.e. within sqlite3_prepare()). The
21783 ** last thing the sqlite3_prepare() function does is copy the error
21784 ** stored by this function into the database handle using sqlite3Error().
21785 ** Function sqlite3Error() should be used during statement execution
21786 ** (sqlite3_step() etc.).
21787 */
21788 SQLITE_PRIVATE void sqlite3ErrorMsg(Parse *pParse, const char *zFormat, ...){
21789   char *zMsg;
21790   va_list ap;
21791   sqlite3 *db = pParse->db;
21792   va_start(ap, zFormat);
21793   zMsg = sqlite3VMPrintf(db, zFormat, ap);
21794   va_end(ap);
21795   if( db->suppressErr ){
21796     sqlite3DbFree(db, zMsg);
21797   }else{
21798     pParse->nErr++;
21799     sqlite3DbFree(db, pParse->zErrMsg);
21800     pParse->zErrMsg = zMsg;
21801     pParse->rc = SQLITE_ERROR;
21802   }
21803 }
21804 
21805 /*
21806 ** Convert an SQL-style quoted string into a normal string by removing
21807 ** the quote characters.  The conversion is done in-place.  If the
21808 ** input does not begin with a quote character, then this routine
21809 ** is a no-op.
21810 **
21811 ** The input string must be zero-terminated.  A new zero-terminator
21812 ** is added to the dequoted string.
21813 **
21814 ** The return value is -1 if no dequoting occurs or the length of the
21815 ** dequoted string, exclusive of the zero terminator, if dequoting does
21816 ** occur.
21817 **
21818 ** 2002-Feb-14: This routine is extended to remove MS-Access style
21819 ** brackets from around identifers.  For example:  "[a-b-c]" becomes
21820 ** "a-b-c".
21821 */
21822 SQLITE_PRIVATE int sqlite3Dequote(char *z){
21823   char quote;
21824   int i, j;
21825   if( z==0 ) return -1;
21826   quote = z[0];
21827   switch( quote ){
21828     case '\'':  break;
21829     case '"':   break;
21830     case '`':   break;                /* For MySQL compatibility */
21831     case '[':   quote = ']';  break;  /* For MS SqlServer compatibility */
21832     default:    return -1;
21833   }
21834   for(i=1, j=0;; i++){
21835     assert( z[i] );
21836     if( z[i]==quote ){
21837       if( z[i+1]==quote ){
21838         z[j++] = quote;
21839         i++;
21840       }else{
21841         break;
21842       }
21843     }else{
21844       z[j++] = z[i];
21845     }
21846   }
21847   z[j] = 0;
21848   return j;
21849 }
21850 
21851 /* Convenient short-hand */
21852 #define UpperToLower sqlite3UpperToLower
21853 
21854 /*
21855 ** Some systems have stricmp().  Others have strcasecmp().  Because
21856 ** there is no consistency, we will define our own.
21857 **
21858 ** IMPLEMENTATION-OF: R-30243-02494 The sqlite3_stricmp() and
21859 ** sqlite3_strnicmp() APIs allow applications and extensions to compare
21860 ** the contents of two buffers containing UTF-8 strings in a
21861 ** case-independent fashion, using the same definition of "case
21862 ** independence" that SQLite uses internally when comparing identifiers.
21863 */
21864 SQLITE_API int sqlite3_stricmp(const char *zLeft, const char *zRight){
21865   register unsigned char *a, *b;
21866   a = (unsigned char *)zLeft;
21867   b = (unsigned char *)zRight;
21868   while( *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
21869   return UpperToLower[*a] - UpperToLower[*b];
21870 }
21871 SQLITE_API int sqlite3_strnicmp(const char *zLeft, const char *zRight, int N){
21872   register unsigned char *a, *b;
21873   a = (unsigned char *)zLeft;
21874   b = (unsigned char *)zRight;
21875   while( N-- > 0 && *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
21876   return N<0 ? 0 : UpperToLower[*a] - UpperToLower[*b];
21877 }
21878 
21879 /*
21880 ** The string z[] is an text representation of a real number.
21881 ** Convert this string to a double and write it into *pResult.
21882 **
21883 ** The string z[] is length bytes in length (bytes, not characters) and
21884 ** uses the encoding enc.  The string is not necessarily zero-terminated.
21885 **
21886 ** Return TRUE if the result is a valid real number (or integer) and FALSE
21887 ** if the string is empty or contains extraneous text.  Valid numbers
21888 ** are in one of these formats:
21889 **
21890 **    [+-]digits[E[+-]digits]
21891 **    [+-]digits.[digits][E[+-]digits]
21892 **    [+-].digits[E[+-]digits]
21893 **
21894 ** Leading and trailing whitespace is ignored for the purpose of determining
21895 ** validity.
21896 **
21897 ** If some prefix of the input string is a valid number, this routine
21898 ** returns FALSE but it still converts the prefix and writes the result
21899 ** into *pResult.
21900 */
21901 SQLITE_PRIVATE int sqlite3AtoF(const char *z, double *pResult, int length, u8 enc){
21902 #ifndef SQLITE_OMIT_FLOATING_POINT
21903   int incr;
21904   const char *zEnd = z + length;
21905   /* sign * significand * (10 ^ (esign * exponent)) */
21906   int sign = 1;    /* sign of significand */
21907   i64 s = 0;       /* significand */
21908   int d = 0;       /* adjust exponent for shifting decimal point */
21909   int esign = 1;   /* sign of exponent */
21910   int e = 0;       /* exponent */
21911   int eValid = 1;  /* True exponent is either not used or is well-formed */
21912   double result;
21913   int nDigits = 0;
21914   int nonNum = 0;
21915 
21916   assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE );
21917   *pResult = 0.0;   /* Default return value, in case of an error */
21918 
21919   if( enc==SQLITE_UTF8 ){
21920     incr = 1;
21921   }else{
21922     int i;
21923     incr = 2;
21924     assert( SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 );
21925     for(i=3-enc; i<length && z[i]==0; i+=2){}
21926     nonNum = i<length;
21927     zEnd = z+i+enc-3;
21928     z += (enc&1);
21929   }
21930 
21931   /* skip leading spaces */
21932   while( z<zEnd && sqlite3Isspace(*z) ) z+=incr;
21933   if( z>=zEnd ) return 0;
21934 
21935   /* get sign of significand */
21936   if( *z=='-' ){
21937     sign = -1;
21938     z+=incr;
21939   }else if( *z=='+' ){
21940     z+=incr;
21941   }
21942 
21943   /* skip leading zeroes */
21944   while( z<zEnd && z[0]=='0' ) z+=incr, nDigits++;
21945 
21946   /* copy max significant digits to significand */
21947   while( z<zEnd && sqlite3Isdigit(*z) && s<((LARGEST_INT64-9)/10) ){
21948     s = s*10 + (*z - '0');
21949     z+=incr, nDigits++;
21950   }
21951 
21952   /* skip non-significant significand digits
21953   ** (increase exponent by d to shift decimal left) */
21954   while( z<zEnd && sqlite3Isdigit(*z) ) z+=incr, nDigits++, d++;
21955   if( z>=zEnd ) goto do_atof_calc;
21956 
21957   /* if decimal point is present */
21958   if( *z=='.' ){
21959     z+=incr;
21960     /* copy digits from after decimal to significand
21961     ** (decrease exponent by d to shift decimal right) */
21962     while( z<zEnd && sqlite3Isdigit(*z) && s<((LARGEST_INT64-9)/10) ){
21963       s = s*10 + (*z - '0');
21964       z+=incr, nDigits++, d--;
21965     }
21966     /* skip non-significant digits */
21967     while( z<zEnd && sqlite3Isdigit(*z) ) z+=incr, nDigits++;
21968   }
21969   if( z>=zEnd ) goto do_atof_calc;
21970 
21971   /* if exponent is present */
21972   if( *z=='e' || *z=='E' ){
21973     z+=incr;
21974     eValid = 0;
21975     if( z>=zEnd ) goto do_atof_calc;
21976     /* get sign of exponent */
21977     if( *z=='-' ){
21978       esign = -1;
21979       z+=incr;
21980     }else if( *z=='+' ){
21981       z+=incr;
21982     }
21983     /* copy digits to exponent */
21984     while( z<zEnd && sqlite3Isdigit(*z) ){
21985       e = e<10000 ? (e*10 + (*z - '0')) : 10000;
21986       z+=incr;
21987       eValid = 1;
21988     }
21989   }
21990 
21991   /* skip trailing spaces */
21992   if( nDigits && eValid ){
21993     while( z<zEnd && sqlite3Isspace(*z) ) z+=incr;
21994   }
21995 
21996 do_atof_calc:
21997   /* adjust exponent by d, and update sign */
21998   e = (e*esign) + d;
21999   if( e<0 ) {
22000     esign = -1;
22001     e *= -1;
22002   } else {
22003     esign = 1;
22004   }
22005 
22006   /* if 0 significand */
22007   if( !s ) {
22008     /* In the IEEE 754 standard, zero is signed.
22009     ** Add the sign if we've seen at least one digit */
22010     result = (sign<0 && nDigits) ? -(double)0 : (double)0;
22011   } else {
22012     /* attempt to reduce exponent */
22013     if( esign>0 ){
22014       while( s<(LARGEST_INT64/10) && e>0 ) e--,s*=10;
22015     }else{
22016       while( !(s%10) && e>0 ) e--,s/=10;
22017     }
22018 
22019     /* adjust the sign of significand */
22020     s = sign<0 ? -s : s;
22021 
22022     /* if exponent, scale significand as appropriate
22023     ** and store in result. */
22024     if( e ){
22025       LONGDOUBLE_TYPE scale = 1.0;
22026 #ifndef __vax__
22027       /* attempt to handle extremely small/large numbers better */
22028       if( e>307 && e<342 ){
22029         while( e%308 ) { scale *= 1.0e+1; e -= 1; }
22030         if( esign<0 ){
22031           result = s / scale;
22032           result /= SQLITE_HUGE_DBL;
22033         }else{
22034           result = s * scale;
22035           result *= SQLITE_HUGE_DBL;
22036         }
22037       }else if( e>=342 ){
22038         if( esign<0 ){
22039           result = 0.0*s;
22040         }else{
22041           result = SQLITE_HUGE_DBL*SQLITE_HUGE_DBL*s;  /* Infinity */
22042         }
22043       }else
22044 #endif
22045       {
22046         /* 1.0e+22 is the largest power of 10 than can be
22047         ** represented exactly. */
22048         while( e%22 ) { scale *= 1.0e+1; e -= 1; }
22049         while( e>0 ) { scale *= 1.0e+22; e -= 22; }
22050         if( esign<0 ){
22051           result = s / scale;
22052         }else{
22053           result = s * scale;
22054         }
22055       }
22056     } else {
22057       result = (double)s;
22058     }
22059   }
22060 
22061   /* store the result */
22062   *pResult = result;
22063 
22064   /* return true if number and no extra non-whitespace chracters after */
22065   return z>=zEnd && nDigits>0 && eValid && nonNum==0;
22066 #else
22067   return !sqlite3Atoi64(z, pResult, length, enc);
22068 #endif /* SQLITE_OMIT_FLOATING_POINT */
22069 }
22070 
22071 /*
22072 ** Compare the 19-character string zNum against the text representation
22073 ** value 2^63:  9223372036854775808.  Return negative, zero, or positive
22074 ** if zNum is less than, equal to, or greater than the string.
22075 ** Note that zNum must contain exactly 19 characters.
22076 **
22077 ** Unlike memcmp() this routine is guaranteed to return the difference
22078 ** in the values of the last digit if the only difference is in the
22079 ** last digit.  So, for example,
22080 **
22081 **      compare2pow63("9223372036854775800", 1)
22082 **
22083 ** will return -8.
22084 */
22085 static int compare2pow63(const char *zNum, int incr){
22086   int c = 0;
22087   int i;
22088                     /* 012345678901234567 */
22089   const char *pow63 = "922337203685477580";
22090   for(i=0; c==0 && i<18; i++){
22091     c = (zNum[i*incr]-pow63[i])*10;
22092   }
22093   if( c==0 ){
22094     c = zNum[18*incr] - '8';
22095     testcase( c==(-1) );
22096     testcase( c==0 );
22097     testcase( c==(+1) );
22098   }
22099   return c;
22100 }
22101 
22102 
22103 /*
22104 ** Convert zNum to a 64-bit signed integer.
22105 **
22106 ** If the zNum value is representable as a 64-bit twos-complement
22107 ** integer, then write that value into *pNum and return 0.
22108 **
22109 ** If zNum is exactly 9223372036854775808, return 2.  This special
22110 ** case is broken out because while 9223372036854775808 cannot be a
22111 ** signed 64-bit integer, its negative -9223372036854775808 can be.
22112 **
22113 ** If zNum is too big for a 64-bit integer and is not
22114 ** 9223372036854775808  or if zNum contains any non-numeric text,
22115 ** then return 1.
22116 **
22117 ** length is the number of bytes in the string (bytes, not characters).
22118 ** The string is not necessarily zero-terminated.  The encoding is
22119 ** given by enc.
22120 */
22121 SQLITE_PRIVATE int sqlite3Atoi64(const char *zNum, i64 *pNum, int length, u8 enc){
22122   int incr;
22123   u64 u = 0;
22124   int neg = 0; /* assume positive */
22125   int i;
22126   int c = 0;
22127   int nonNum = 0;
22128   const char *zStart;
22129   const char *zEnd = zNum + length;
22130   assert( enc==SQLITE_UTF8 || enc==SQLITE_UTF16LE || enc==SQLITE_UTF16BE );
22131   if( enc==SQLITE_UTF8 ){
22132     incr = 1;
22133   }else{
22134     incr = 2;
22135     assert( SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 );
22136     for(i=3-enc; i<length && zNum[i]==0; i+=2){}
22137     nonNum = i<length;
22138     zEnd = zNum+i+enc-3;
22139     zNum += (enc&1);
22140   }
22141   while( zNum<zEnd && sqlite3Isspace(*zNum) ) zNum+=incr;
22142   if( zNum<zEnd ){
22143     if( *zNum=='-' ){
22144       neg = 1;
22145       zNum+=incr;
22146     }else if( *zNum=='+' ){
22147       zNum+=incr;
22148     }
22149   }
22150   zStart = zNum;
22151   while( zNum<zEnd && zNum[0]=='0' ){ zNum+=incr; } /* Skip leading zeros. */
22152   for(i=0; &zNum[i]<zEnd && (c=zNum[i])>='0' && c<='9'; i+=incr){
22153     u = u*10 + c - '0';
22154   }
22155   if( u>LARGEST_INT64 ){
22156     *pNum = neg ? SMALLEST_INT64 : LARGEST_INT64;
22157   }else if( neg ){
22158     *pNum = -(i64)u;
22159   }else{
22160     *pNum = (i64)u;
22161   }
22162   testcase( i==18 );
22163   testcase( i==19 );
22164   testcase( i==20 );
22165   if( (c!=0 && &zNum[i]<zEnd) || (i==0 && zStart==zNum) || i>19*incr || nonNum ){
22166     /* zNum is empty or contains non-numeric text or is longer
22167     ** than 19 digits (thus guaranteeing that it is too large) */
22168     return 1;
22169   }else if( i<19*incr ){
22170     /* Less than 19 digits, so we know that it fits in 64 bits */
22171     assert( u<=LARGEST_INT64 );
22172     return 0;
22173   }else{
22174     /* zNum is a 19-digit numbers.  Compare it against 9223372036854775808. */
22175     c = compare2pow63(zNum, incr);
22176     if( c<0 ){
22177       /* zNum is less than 9223372036854775808 so it fits */
22178       assert( u<=LARGEST_INT64 );
22179       return 0;
22180     }else if( c>0 ){
22181       /* zNum is greater than 9223372036854775808 so it overflows */
22182       return 1;
22183     }else{
22184       /* zNum is exactly 9223372036854775808.  Fits if negative.  The
22185       ** special case 2 overflow if positive */
22186       assert( u-1==LARGEST_INT64 );
22187       return neg ? 0 : 2;
22188     }
22189   }
22190 }
22191 
22192 /*
22193 ** If zNum represents an integer that will fit in 32-bits, then set
22194 ** *pValue to that integer and return true.  Otherwise return false.
22195 **
22196 ** Any non-numeric characters that following zNum are ignored.
22197 ** This is different from sqlite3Atoi64() which requires the
22198 ** input number to be zero-terminated.
22199 */
22200 SQLITE_PRIVATE int sqlite3GetInt32(const char *zNum, int *pValue){
22201   sqlite_int64 v = 0;
22202   int i, c;
22203   int neg = 0;
22204   if( zNum[0]=='-' ){
22205     neg = 1;
22206     zNum++;
22207   }else if( zNum[0]=='+' ){
22208     zNum++;
22209   }
22210   while( zNum[0]=='0' ) zNum++;
22211   for(i=0; i<11 && (c = zNum[i] - '0')>=0 && c<=9; i++){
22212     v = v*10 + c;
22213   }
22214 
22215   /* The longest decimal representation of a 32 bit integer is 10 digits:
22216   **
22217   **             1234567890
22218   **     2^31 -> 2147483648
22219   */
22220   testcase( i==10 );
22221   if( i>10 ){
22222     return 0;
22223   }
22224   testcase( v-neg==2147483647 );
22225   if( v-neg>2147483647 ){
22226     return 0;
22227   }
22228   if( neg ){
22229     v = -v;
22230   }
22231   *pValue = (int)v;
22232   return 1;
22233 }
22234 
22235 /*
22236 ** Return a 32-bit integer value extracted from a string.  If the
22237 ** string is not an integer, just return 0.
22238 */
22239 SQLITE_PRIVATE int sqlite3Atoi(const char *z){
22240   int x = 0;
22241   if( z ) sqlite3GetInt32(z, &x);
22242   return x;
22243 }
22244 
22245 /*
22246 ** The variable-length integer encoding is as follows:
22247 **
22248 ** KEY:
22249 **         A = 0xxxxxxx    7 bits of data and one flag bit
22250 **         B = 1xxxxxxx    7 bits of data and one flag bit
22251 **         C = xxxxxxxx    8 bits of data
22252 **
22253 **  7 bits - A
22254 ** 14 bits - BA
22255 ** 21 bits - BBA
22256 ** 28 bits - BBBA
22257 ** 35 bits - BBBBA
22258 ** 42 bits - BBBBBA
22259 ** 49 bits - BBBBBBA
22260 ** 56 bits - BBBBBBBA
22261 ** 64 bits - BBBBBBBBC
22262 */
22263 
22264 /*
22265 ** Write a 64-bit variable-length integer to memory starting at p[0].
22266 ** The length of data write will be between 1 and 9 bytes.  The number
22267 ** of bytes written is returned.
22268 **
22269 ** A variable-length integer consists of the lower 7 bits of each byte
22270 ** for all bytes that have the 8th bit set and one byte with the 8th
22271 ** bit clear.  Except, if we get to the 9th byte, it stores the full
22272 ** 8 bits and is the last byte.
22273 */
22274 SQLITE_PRIVATE int sqlite3PutVarint(unsigned char *p, u64 v){
22275   int i, j, n;
22276   u8 buf[10];
22277   if( v & (((u64)0xff000000)<<32) ){
22278     p[8] = (u8)v;
22279     v >>= 8;
22280     for(i=7; i>=0; i--){
22281       p[i] = (u8)((v & 0x7f) | 0x80);
22282       v >>= 7;
22283     }
22284     return 9;
22285   }
22286   n = 0;
22287   do{
22288     buf[n++] = (u8)((v & 0x7f) | 0x80);
22289     v >>= 7;
22290   }while( v!=0 );
22291   buf[0] &= 0x7f;
22292   assert( n<=9 );
22293   for(i=0, j=n-1; j>=0; j--, i++){
22294     p[i] = buf[j];
22295   }
22296   return n;
22297 }
22298 
22299 /*
22300 ** This routine is a faster version of sqlite3PutVarint() that only
22301 ** works for 32-bit positive integers and which is optimized for
22302 ** the common case of small integers.  A MACRO version, putVarint32,
22303 ** is provided which inlines the single-byte case.  All code should use
22304 ** the MACRO version as this function assumes the single-byte case has
22305 ** already been handled.
22306 */
22307 SQLITE_PRIVATE int sqlite3PutVarint32(unsigned char *p, u32 v){
22308 #ifndef putVarint32
22309   if( (v & ~0x7f)==0 ){
22310     p[0] = v;
22311     return 1;
22312   }
22313 #endif
22314   if( (v & ~0x3fff)==0 ){
22315     p[0] = (u8)((v>>7) | 0x80);
22316     p[1] = (u8)(v & 0x7f);
22317     return 2;
22318   }
22319   return sqlite3PutVarint(p, v);
22320 }
22321 
22322 /*
22323 ** Bitmasks used by sqlite3GetVarint().  These precomputed constants
22324 ** are defined here rather than simply putting the constant expressions
22325 ** inline in order to work around bugs in the RVT compiler.
22326 **
22327 ** SLOT_2_0     A mask for  (0x7f<<14) | 0x7f
22328 **
22329 ** SLOT_4_2_0   A mask for  (0x7f<<28) | SLOT_2_0
22330 */
22331 #define SLOT_2_0     0x001fc07f
22332 #define SLOT_4_2_0   0xf01fc07f
22333 
22334 
22335 /*
22336 ** Read a 64-bit variable-length integer from memory starting at p[0].
22337 ** Return the number of bytes read.  The value is stored in *v.
22338 */
22339 SQLITE_PRIVATE u8 sqlite3GetVarint(const unsigned char *p, u64 *v){
22340   u32 a,b,s;
22341 
22342   a = *p;
22343   /* a: p0 (unmasked) */
22344   if (!(a&0x80))
22345   {
22346     *v = a;
22347     return 1;
22348   }
22349 
22350   p++;
22351   b = *p;
22352   /* b: p1 (unmasked) */
22353   if (!(b&0x80))
22354   {
22355     a &= 0x7f;
22356     a = a<<7;
22357     a |= b;
22358     *v = a;
22359     return 2;
22360   }
22361 
22362   /* Verify that constants are precomputed correctly */
22363   assert( SLOT_2_0 == ((0x7f<<14) | (0x7f)) );
22364   assert( SLOT_4_2_0 == ((0xfU<<28) | (0x7f<<14) | (0x7f)) );
22365 
22366   p++;
22367   a = a<<14;
22368   a |= *p;
22369   /* a: p0<<14 | p2 (unmasked) */
22370   if (!(a&0x80))
22371   {
22372     a &= SLOT_2_0;
22373     b &= 0x7f;
22374     b = b<<7;
22375     a |= b;
22376     *v = a;
22377     return 3;
22378   }
22379 
22380   /* CSE1 from below */
22381   a &= SLOT_2_0;
22382   p++;
22383   b = b<<14;
22384   b |= *p;
22385   /* b: p1<<14 | p3 (unmasked) */
22386   if (!(b&0x80))
22387   {
22388     b &= SLOT_2_0;
22389     /* moved CSE1 up */
22390     /* a &= (0x7f<<14)|(0x7f); */
22391     a = a<<7;
22392     a |= b;
22393     *v = a;
22394     return 4;
22395   }
22396 
22397   /* a: p0<<14 | p2 (masked) */
22398   /* b: p1<<14 | p3 (unmasked) */
22399   /* 1:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
22400   /* moved CSE1 up */
22401   /* a &= (0x7f<<14)|(0x7f); */
22402   b &= SLOT_2_0;
22403   s = a;
22404   /* s: p0<<14 | p2 (masked) */
22405 
22406   p++;
22407   a = a<<14;
22408   a |= *p;
22409   /* a: p0<<28 | p2<<14 | p4 (unmasked) */
22410   if (!(a&0x80))
22411   {
22412     /* we can skip these cause they were (effectively) done above in calc'ing s */
22413     /* a &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
22414     /* b &= (0x7f<<14)|(0x7f); */
22415     b = b<<7;
22416     a |= b;
22417     s = s>>18;
22418     *v = ((u64)s)<<32 | a;
22419     return 5;
22420   }
22421 
22422   /* 2:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
22423   s = s<<7;
22424   s |= b;
22425   /* s: p0<<21 | p1<<14 | p2<<7 | p3 (masked) */
22426 
22427   p++;
22428   b = b<<14;
22429   b |= *p;
22430   /* b: p1<<28 | p3<<14 | p5 (unmasked) */
22431   if (!(b&0x80))
22432   {
22433     /* we can skip this cause it was (effectively) done above in calc'ing s */
22434     /* b &= (0x7f<<28)|(0x7f<<14)|(0x7f); */
22435     a &= SLOT_2_0;
22436     a = a<<7;
22437     a |= b;
22438     s = s>>18;
22439     *v = ((u64)s)<<32 | a;
22440     return 6;
22441   }
22442 
22443   p++;
22444   a = a<<14;
22445   a |= *p;
22446   /* a: p2<<28 | p4<<14 | p6 (unmasked) */
22447   if (!(a&0x80))
22448   {
22449     a &= SLOT_4_2_0;
22450     b &= SLOT_2_0;
22451     b = b<<7;
22452     a |= b;
22453     s = s>>11;
22454     *v = ((u64)s)<<32 | a;
22455     return 7;
22456   }
22457 
22458   /* CSE2 from below */
22459   a &= SLOT_2_0;
22460   p++;
22461   b = b<<14;
22462   b |= *p;
22463   /* b: p3<<28 | p5<<14 | p7 (unmasked) */
22464   if (!(b&0x80))
22465   {
22466     b &= SLOT_4_2_0;
22467     /* moved CSE2 up */
22468     /* a &= (0x7f<<14)|(0x7f); */
22469     a = a<<7;
22470     a |= b;
22471     s = s>>4;
22472     *v = ((u64)s)<<32 | a;
22473     return 8;
22474   }
22475 
22476   p++;
22477   a = a<<15;
22478   a |= *p;
22479   /* a: p4<<29 | p6<<15 | p8 (unmasked) */
22480 
22481   /* moved CSE2 up */
22482   /* a &= (0x7f<<29)|(0x7f<<15)|(0xff); */
22483   b &= SLOT_2_0;
22484   b = b<<8;
22485   a |= b;
22486 
22487   s = s<<4;
22488   b = p[-4];
22489   b &= 0x7f;
22490   b = b>>3;
22491   s |= b;
22492 
22493   *v = ((u64)s)<<32 | a;
22494 
22495   return 9;
22496 }
22497 
22498 /*
22499 ** Read a 32-bit variable-length integer from memory starting at p[0].
22500 ** Return the number of bytes read.  The value is stored in *v.
22501 **
22502 ** If the varint stored in p[0] is larger than can fit in a 32-bit unsigned
22503 ** integer, then set *v to 0xffffffff.
22504 **
22505 ** A MACRO version, getVarint32, is provided which inlines the
22506 ** single-byte case.  All code should use the MACRO version as
22507 ** this function assumes the single-byte case has already been handled.
22508 */
22509 SQLITE_PRIVATE u8 sqlite3GetVarint32(const unsigned char *p, u32 *v){
22510   u32 a,b;
22511 
22512   /* The 1-byte case.  Overwhelmingly the most common.  Handled inline
22513   ** by the getVarin32() macro */
22514   a = *p;
22515   /* a: p0 (unmasked) */
22516 #ifndef getVarint32
22517   if (!(a&0x80))
22518   {
22519     /* Values between 0 and 127 */
22520     *v = a;
22521     return 1;
22522   }
22523 #endif
22524 
22525   /* The 2-byte case */
22526   p++;
22527   b = *p;
22528   /* b: p1 (unmasked) */
22529   if (!(b&0x80))
22530   {
22531     /* Values between 128 and 16383 */
22532     a &= 0x7f;
22533     a = a<<7;
22534     *v = a | b;
22535     return 2;
22536   }
22537 
22538   /* The 3-byte case */
22539   p++;
22540   a = a<<14;
22541   a |= *p;
22542   /* a: p0<<14 | p2 (unmasked) */
22543   if (!(a&0x80))
22544   {
22545     /* Values between 16384 and 2097151 */
22546     a &= (0x7f<<14)|(0x7f);
22547     b &= 0x7f;
22548     b = b<<7;
22549     *v = a | b;
22550     return 3;
22551   }
22552 
22553   /* A 32-bit varint is used to store size information in btrees.
22554   ** Objects are rarely larger than 2MiB limit of a 3-byte varint.
22555   ** A 3-byte varint is sufficient, for example, to record the size
22556   ** of a 1048569-byte BLOB or string.
22557   **
22558   ** We only unroll the first 1-, 2-, and 3- byte cases.  The very
22559   ** rare larger cases can be handled by the slower 64-bit varint
22560   ** routine.
22561   */
22562 #if 1
22563   {
22564     u64 v64;
22565     u8 n;
22566 
22567     p -= 2;
22568     n = sqlite3GetVarint(p, &v64);
22569     assert( n>3 && n<=9 );
22570     if( (v64 & SQLITE_MAX_U32)!=v64 ){
22571       *v = 0xffffffff;
22572     }else{
22573       *v = (u32)v64;
22574     }
22575     return n;
22576   }
22577 
22578 #else
22579   /* For following code (kept for historical record only) shows an
22580   ** unrolling for the 3- and 4-byte varint cases.  This code is
22581   ** slightly faster, but it is also larger and much harder to test.
22582   */
22583   p++;
22584   b = b<<14;
22585   b |= *p;
22586   /* b: p1<<14 | p3 (unmasked) */
22587   if (!(b&0x80))
22588   {
22589     /* Values between 2097152 and 268435455 */
22590     b &= (0x7f<<14)|(0x7f);
22591     a &= (0x7f<<14)|(0x7f);
22592     a = a<<7;
22593     *v = a | b;
22594     return 4;
22595   }
22596 
22597   p++;
22598   a = a<<14;
22599   a |= *p;
22600   /* a: p0<<28 | p2<<14 | p4 (unmasked) */
22601   if (!(a&0x80))
22602   {
22603     /* Values  between 268435456 and 34359738367 */
22604     a &= SLOT_4_2_0;
22605     b &= SLOT_4_2_0;
22606     b = b<<7;
22607     *v = a | b;
22608     return 5;
22609   }
22610 
22611   /* We can only reach this point when reading a corrupt database
22612   ** file.  In that case we are not in any hurry.  Use the (relatively
22613   ** slow) general-purpose sqlite3GetVarint() routine to extract the
22614   ** value. */
22615   {
22616     u64 v64;
22617     u8 n;
22618 
22619     p -= 4;
22620     n = sqlite3GetVarint(p, &v64);
22621     assert( n>5 && n<=9 );
22622     *v = (u32)v64;
22623     return n;
22624   }
22625 #endif
22626 }
22627 
22628 /*
22629 ** Return the number of bytes that will be needed to store the given
22630 ** 64-bit integer.
22631 */
22632 SQLITE_PRIVATE int sqlite3VarintLen(u64 v){
22633   int i = 0;
22634   do{
22635     i++;
22636     v >>= 7;
22637   }while( v!=0 && ALWAYS(i<9) );
22638   return i;
22639 }
22640 
22641 
22642 /*
22643 ** Read or write a four-byte big-endian integer value.
22644 */
22645 SQLITE_PRIVATE u32 sqlite3Get4byte(const u8 *p){
22646   testcase( p[0]&0x80 );
22647   return ((unsigned)p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3];
22648 }
22649 SQLITE_PRIVATE void sqlite3Put4byte(unsigned char *p, u32 v){
22650   p[0] = (u8)(v>>24);
22651   p[1] = (u8)(v>>16);
22652   p[2] = (u8)(v>>8);
22653   p[3] = (u8)v;
22654 }
22655 
22656 
22657 
22658 /*
22659 ** Translate a single byte of Hex into an integer.
22660 ** This routine only works if h really is a valid hexadecimal
22661 ** character:  0..9a..fA..F
22662 */
22663 SQLITE_PRIVATE u8 sqlite3HexToInt(int h){
22664   assert( (h>='0' && h<='9') ||  (h>='a' && h<='f') ||  (h>='A' && h<='F') );
22665 #ifdef SQLITE_ASCII
22666   h += 9*(1&(h>>6));
22667 #endif
22668 #ifdef SQLITE_EBCDIC
22669   h += 9*(1&~(h>>4));
22670 #endif
22671   return (u8)(h & 0xf);
22672 }
22673 
22674 #if !defined(SQLITE_OMIT_BLOB_LITERAL) || defined(SQLITE_HAS_CODEC)
22675 /*
22676 ** Convert a BLOB literal of the form "x'hhhhhh'" into its binary
22677 ** value.  Return a pointer to its binary value.  Space to hold the
22678 ** binary value has been obtained from malloc and must be freed by
22679 ** the calling routine.
22680 */
22681 SQLITE_PRIVATE void *sqlite3HexToBlob(sqlite3 *db, const char *z, int n){
22682   char *zBlob;
22683   int i;
22684 
22685   zBlob = (char *)sqlite3DbMallocRaw(db, n/2 + 1);
22686   n--;
22687   if( zBlob ){
22688     for(i=0; i<n; i+=2){
22689       zBlob[i/2] = (sqlite3HexToInt(z[i])<<4) | sqlite3HexToInt(z[i+1]);
22690     }
22691     zBlob[i/2] = 0;
22692   }
22693   return zBlob;
22694 }
22695 #endif /* !SQLITE_OMIT_BLOB_LITERAL || SQLITE_HAS_CODEC */
22696 
22697 /*
22698 ** Log an error that is an API call on a connection pointer that should
22699 ** not have been used.  The "type" of connection pointer is given as the
22700 ** argument.  The zType is a word like "NULL" or "closed" or "invalid".
22701 */
22702 static void logBadConnection(const char *zType){
22703   sqlite3_log(SQLITE_MISUSE,
22704      "API call with %s database connection pointer",
22705      zType
22706   );
22707 }
22708 
22709 /*
22710 ** Check to make sure we have a valid db pointer.  This test is not
22711 ** foolproof but it does provide some measure of protection against
22712 ** misuse of the interface such as passing in db pointers that are
22713 ** NULL or which have been previously closed.  If this routine returns
22714 ** 1 it means that the db pointer is valid and 0 if it should not be
22715 ** dereferenced for any reason.  The calling function should invoke
22716 ** SQLITE_MISUSE immediately.
22717 **
22718 ** sqlite3SafetyCheckOk() requires that the db pointer be valid for
22719 ** use.  sqlite3SafetyCheckSickOrOk() allows a db pointer that failed to
22720 ** open properly and is not fit for general use but which can be
22721 ** used as an argument to sqlite3_errmsg() or sqlite3_close().
22722 */
22723 SQLITE_PRIVATE int sqlite3SafetyCheckOk(sqlite3 *db){
22724   u32 magic;
22725   if( db==0 ){
22726     logBadConnection("NULL");
22727     return 0;
22728   }
22729   magic = db->magic;
22730   if( magic!=SQLITE_MAGIC_OPEN ){
22731     if( sqlite3SafetyCheckSickOrOk(db) ){
22732       testcase( sqlite3GlobalConfig.xLog!=0 );
22733       logBadConnection("unopened");
22734     }
22735     return 0;
22736   }else{
22737     return 1;
22738   }
22739 }
22740 SQLITE_PRIVATE int sqlite3SafetyCheckSickOrOk(sqlite3 *db){
22741   u32 magic;
22742   magic = db->magic;
22743   if( magic!=SQLITE_MAGIC_SICK &&
22744       magic!=SQLITE_MAGIC_OPEN &&
22745       magic!=SQLITE_MAGIC_BUSY ){
22746     testcase( sqlite3GlobalConfig.xLog!=0 );
22747     logBadConnection("invalid");
22748     return 0;
22749   }else{
22750     return 1;
22751   }
22752 }
22753 
22754 /*
22755 ** Attempt to add, substract, or multiply the 64-bit signed value iB against
22756 ** the other 64-bit signed integer at *pA and store the result in *pA.
22757 ** Return 0 on success.  Or if the operation would have resulted in an
22758 ** overflow, leave *pA unchanged and return 1.
22759 */
22760 SQLITE_PRIVATE int sqlite3AddInt64(i64 *pA, i64 iB){
22761   i64 iA = *pA;
22762   testcase( iA==0 ); testcase( iA==1 );
22763   testcase( iB==-1 ); testcase( iB==0 );
22764   if( iB>=0 ){
22765     testcase( iA>0 && LARGEST_INT64 - iA == iB );
22766     testcase( iA>0 && LARGEST_INT64 - iA == iB - 1 );
22767     if( iA>0 && LARGEST_INT64 - iA < iB ) return 1;
22768     *pA += iB;
22769   }else{
22770     testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 1 );
22771     testcase( iA<0 && -(iA + LARGEST_INT64) == iB + 2 );
22772     if( iA<0 && -(iA + LARGEST_INT64) > iB + 1 ) return 1;
22773     *pA += iB;
22774   }
22775   return 0;
22776 }
22777 SQLITE_PRIVATE int sqlite3SubInt64(i64 *pA, i64 iB){
22778   testcase( iB==SMALLEST_INT64+1 );
22779   if( iB==SMALLEST_INT64 ){
22780     testcase( (*pA)==(-1) ); testcase( (*pA)==0 );
22781     if( (*pA)>=0 ) return 1;
22782     *pA -= iB;
22783     return 0;
22784   }else{
22785     return sqlite3AddInt64(pA, -iB);
22786   }
22787 }
22788 #define TWOPOWER32 (((i64)1)<<32)
22789 #define TWOPOWER31 (((i64)1)<<31)
22790 SQLITE_PRIVATE int sqlite3MulInt64(i64 *pA, i64 iB){
22791   i64 iA = *pA;
22792   i64 iA1, iA0, iB1, iB0, r;
22793 
22794   iA1 = iA/TWOPOWER32;
22795   iA0 = iA % TWOPOWER32;
22796   iB1 = iB/TWOPOWER32;
22797   iB0 = iB % TWOPOWER32;
22798   if( iA1*iB1 != 0 ) return 1;
22799   assert( iA1*iB0==0 || iA0*iB1==0 );
22800   r = iA1*iB0 + iA0*iB1;
22801   testcase( r==(-TWOPOWER31)-1 );
22802   testcase( r==(-TWOPOWER31) );
22803   testcase( r==TWOPOWER31 );
22804   testcase( r==TWOPOWER31-1 );
22805   if( r<(-TWOPOWER31) || r>=TWOPOWER31 ) return 1;
22806   r *= TWOPOWER32;
22807   if( sqlite3AddInt64(&r, iA0*iB0) ) return 1;
22808   *pA = r;
22809   return 0;
22810 }
22811 
22812 /*
22813 ** Compute the absolute value of a 32-bit signed integer, of possible.  Or
22814 ** if the integer has a value of -2147483648, return +2147483647
22815 */
22816 SQLITE_PRIVATE int sqlite3AbsInt32(int x){
22817   if( x>=0 ) return x;
22818   if( x==(int)0x80000000 ) return 0x7fffffff;
22819   return -x;
22820 }
22821 
22822 #ifdef SQLITE_ENABLE_8_3_NAMES
22823 /*
22824 ** If SQLITE_ENABLE_8_3_NAMES is set at compile-time and if the database
22825 ** filename in zBaseFilename is a URI with the "8_3_names=1" parameter and
22826 ** if filename in z[] has a suffix (a.k.a. "extension") that is longer than
22827 ** three characters, then shorten the suffix on z[] to be the last three
22828 ** characters of the original suffix.
22829 **
22830 ** If SQLITE_ENABLE_8_3_NAMES is set to 2 at compile-time, then always
22831 ** do the suffix shortening regardless of URI parameter.
22832 **
22833 ** Examples:
22834 **
22835 **     test.db-journal    =>   test.nal
22836 **     test.db-wal        =>   test.wal
22837 **     test.db-shm        =>   test.shm
22838 **     test.db-mj7f3319fa =>   test.9fa
22839 */
22840 SQLITE_PRIVATE void sqlite3FileSuffix3(const char *zBaseFilename, char *z){
22841 #if SQLITE_ENABLE_8_3_NAMES<2
22842   if( sqlite3_uri_boolean(zBaseFilename, "8_3_names", 0) )
22843 #endif
22844   {
22845     int i, sz;
22846     sz = sqlite3Strlen30(z);
22847     for(i=sz-1; i>0 && z[i]!='/' && z[i]!='.'; i--){}
22848     if( z[i]=='.' && ALWAYS(sz>i+4) ) memmove(&z[i+1], &z[sz-3], 4);
22849   }
22850 }
22851 #endif
22852 
22853 /*
22854 ** Find (an approximate) sum of two LogEst values.  This computation is
22855 ** not a simple "+" operator because LogEst is stored as a logarithmic
22856 ** value.
22857 **
22858 */
22859 SQLITE_PRIVATE LogEst sqlite3LogEstAdd(LogEst a, LogEst b){
22860   static const unsigned char x[] = {
22861      10, 10,                         /* 0,1 */
22862       9, 9,                          /* 2,3 */
22863       8, 8,                          /* 4,5 */
22864       7, 7, 7,                       /* 6,7,8 */
22865       6, 6, 6,                       /* 9,10,11 */
22866       5, 5, 5,                       /* 12-14 */
22867       4, 4, 4, 4,                    /* 15-18 */
22868       3, 3, 3, 3, 3, 3,              /* 19-24 */
22869       2, 2, 2, 2, 2, 2, 2,           /* 25-31 */
22870   };
22871   if( a>=b ){
22872     if( a>b+49 ) return a;
22873     if( a>b+31 ) return a+1;
22874     return a+x[a-b];
22875   }else{
22876     if( b>a+49 ) return b;
22877     if( b>a+31 ) return b+1;
22878     return b+x[b-a];
22879   }
22880 }
22881 
22882 /*
22883 ** Convert an integer into a LogEst.  In other words, compute a
22884 ** good approximatation for 10*log2(x).
22885 */
22886 SQLITE_PRIVATE LogEst sqlite3LogEst(u64 x){
22887   static LogEst a[] = { 0, 2, 3, 5, 6, 7, 8, 9 };
22888   LogEst y = 40;
22889   if( x<8 ){
22890     if( x<2 ) return 0;
22891     while( x<8 ){  y -= 10; x <<= 1; }
22892   }else{
22893     while( x>255 ){ y += 40; x >>= 4; }
22894     while( x>15 ){  y += 10; x >>= 1; }
22895   }
22896   return a[x&7] + y - 10;
22897 }
22898 
22899 #ifndef SQLITE_OMIT_VIRTUALTABLE
22900 /*
22901 ** Convert a double into a LogEst
22902 ** In other words, compute an approximation for 10*log2(x).
22903 */
22904 SQLITE_PRIVATE LogEst sqlite3LogEstFromDouble(double x){
22905   u64 a;
22906   LogEst e;
22907   assert( sizeof(x)==8 && sizeof(a)==8 );
22908   if( x<=1 ) return 0;
22909   if( x<=2000000000 ) return sqlite3LogEst((u64)x);
22910   memcpy(&a, &x, 8);
22911   e = (a>>52) - 1022;
22912   return e*10;
22913 }
22914 #endif /* SQLITE_OMIT_VIRTUALTABLE */
22915 
22916 /*
22917 ** Convert a LogEst into an integer.
22918 */
22919 SQLITE_PRIVATE u64 sqlite3LogEstToInt(LogEst x){
22920   u64 n;
22921   if( x<10 ) return 1;
22922   n = x%10;
22923   x /= 10;
22924   if( n>=5 ) n -= 2;
22925   else if( n>=1 ) n -= 1;
22926   if( x>=3 ){
22927     return x>60 ? (u64)LARGEST_INT64 : (n+8)<<(x-3);
22928   }
22929   return (n+8)>>(3-x);
22930 }
22931 
22932 /************** End of util.c ************************************************/
22933 /************** Begin file hash.c ********************************************/
22934 /*
22935 ** 2001 September 22
22936 **
22937 ** The author disclaims copyright to this source code.  In place of
22938 ** a legal notice, here is a blessing:
22939 **
22940 **    May you do good and not evil.
22941 **    May you find forgiveness for yourself and forgive others.
22942 **    May you share freely, never taking more than you give.
22943 **
22944 *************************************************************************
22945 ** This is the implementation of generic hash-tables
22946 ** used in SQLite.
22947 */
22948 /* #include <assert.h> */
22949 
22950 /* Turn bulk memory into a hash table object by initializing the
22951 ** fields of the Hash structure.
22952 **
22953 ** "pNew" is a pointer to the hash table that is to be initialized.
22954 */
22955 SQLITE_PRIVATE void sqlite3HashInit(Hash *pNew){
22956   assert( pNew!=0 );
22957   pNew->first = 0;
22958   pNew->count = 0;
22959   pNew->htsize = 0;
22960   pNew->ht = 0;
22961 }
22962 
22963 /* Remove all entries from a hash table.  Reclaim all memory.
22964 ** Call this routine to delete a hash table or to reset a hash table
22965 ** to the empty state.
22966 */
22967 SQLITE_PRIVATE void sqlite3HashClear(Hash *pH){
22968   HashElem *elem;         /* For looping over all elements of the table */
22969 
22970   assert( pH!=0 );
22971   elem = pH->first;
22972   pH->first = 0;
22973   sqlite3_free(pH->ht);
22974   pH->ht = 0;
22975   pH->htsize = 0;
22976   while( elem ){
22977     HashElem *next_elem = elem->next;
22978     sqlite3_free(elem);
22979     elem = next_elem;
22980   }
22981   pH->count = 0;
22982 }
22983 
22984 /*
22985 ** The hashing function.
22986 */
22987 static unsigned int strHash(const char *z, int nKey){
22988   unsigned int h = 0;
22989   assert( nKey>=0 );
22990   while( nKey > 0  ){
22991     h = (h<<3) ^ h ^ sqlite3UpperToLower[(unsigned char)*z++];
22992     nKey--;
22993   }
22994   return h;
22995 }
22996 
22997 
22998 /* Link pNew element into the hash table pH.  If pEntry!=0 then also
22999 ** insert pNew into the pEntry hash bucket.
23000 */
23001 static void insertElement(
23002   Hash *pH,              /* The complete hash table */
23003   struct _ht *pEntry,    /* The entry into which pNew is inserted */
23004   HashElem *pNew         /* The element to be inserted */
23005 ){
23006   HashElem *pHead;       /* First element already in pEntry */
23007   if( pEntry ){
23008     pHead = pEntry->count ? pEntry->chain : 0;
23009     pEntry->count++;
23010     pEntry->chain = pNew;
23011   }else{
23012     pHead = 0;
23013   }
23014   if( pHead ){
23015     pNew->next = pHead;
23016     pNew->prev = pHead->prev;
23017     if( pHead->prev ){ pHead->prev->next = pNew; }
23018     else             { pH->first = pNew; }
23019     pHead->prev = pNew;
23020   }else{
23021     pNew->next = pH->first;
23022     if( pH->first ){ pH->first->prev = pNew; }
23023     pNew->prev = 0;
23024     pH->first = pNew;
23025   }
23026 }
23027 
23028 
23029 /* Resize the hash table so that it cantains "new_size" buckets.
23030 **
23031 ** The hash table might fail to resize if sqlite3_malloc() fails or
23032 ** if the new size is the same as the prior size.
23033 ** Return TRUE if the resize occurs and false if not.
23034 */
23035 static int rehash(Hash *pH, unsigned int new_size){
23036   struct _ht *new_ht;            /* The new hash table */
23037   HashElem *elem, *next_elem;    /* For looping over existing elements */
23038 
23039 #if SQLITE_MALLOC_SOFT_LIMIT>0
23040   if( new_size*sizeof(struct _ht)>SQLITE_MALLOC_SOFT_LIMIT ){
23041     new_size = SQLITE_MALLOC_SOFT_LIMIT/sizeof(struct _ht);
23042   }
23043   if( new_size==pH->htsize ) return 0;
23044 #endif
23045 
23046   /* The inability to allocates space for a larger hash table is
23047   ** a performance hit but it is not a fatal error.  So mark the
23048   ** allocation as a benign. Use sqlite3Malloc()/memset(0) instead of
23049   ** sqlite3MallocZero() to make the allocation, as sqlite3MallocZero()
23050   ** only zeroes the requested number of bytes whereas this module will
23051   ** use the actual amount of space allocated for the hash table (which
23052   ** may be larger than the requested amount).
23053   */
23054   sqlite3BeginBenignMalloc();
23055   new_ht = (struct _ht *)sqlite3Malloc( new_size*sizeof(struct _ht) );
23056   sqlite3EndBenignMalloc();
23057 
23058   if( new_ht==0 ) return 0;
23059   sqlite3_free(pH->ht);
23060   pH->ht = new_ht;
23061   pH->htsize = new_size = sqlite3MallocSize(new_ht)/sizeof(struct _ht);
23062   memset(new_ht, 0, new_size*sizeof(struct _ht));
23063   for(elem=pH->first, pH->first=0; elem; elem = next_elem){
23064     unsigned int h = strHash(elem->pKey, elem->nKey) % new_size;
23065     next_elem = elem->next;
23066     insertElement(pH, &new_ht[h], elem);
23067   }
23068   return 1;
23069 }
23070 
23071 /* This function (for internal use only) locates an element in an
23072 ** hash table that matches the given key.  The hash for this key has
23073 ** already been computed and is passed as the 4th parameter.
23074 */
23075 static HashElem *findElementGivenHash(
23076   const Hash *pH,     /* The pH to be searched */
23077   const char *pKey,   /* The key we are searching for */
23078   int nKey,           /* Bytes in key (not counting zero terminator) */
23079   unsigned int h      /* The hash for this key. */
23080 ){
23081   HashElem *elem;                /* Used to loop thru the element list */
23082   int count;                     /* Number of elements left to test */
23083 
23084   if( pH->ht ){
23085     struct _ht *pEntry = &pH->ht[h];
23086     elem = pEntry->chain;
23087     count = pEntry->count;
23088   }else{
23089     elem = pH->first;
23090     count = pH->count;
23091   }
23092   while( count-- && ALWAYS(elem) ){
23093     if( elem->nKey==nKey && sqlite3StrNICmp(elem->pKey,pKey,nKey)==0 ){
23094       return elem;
23095     }
23096     elem = elem->next;
23097   }
23098   return 0;
23099 }
23100 
23101 /* Remove a single entry from the hash table given a pointer to that
23102 ** element and a hash on the element's key.
23103 */
23104 static void removeElementGivenHash(
23105   Hash *pH,         /* The pH containing "elem" */
23106   HashElem* elem,   /* The element to be removed from the pH */
23107   unsigned int h    /* Hash value for the element */
23108 ){
23109   struct _ht *pEntry;
23110   if( elem->prev ){
23111     elem->prev->next = elem->next;
23112   }else{
23113     pH->first = elem->next;
23114   }
23115   if( elem->next ){
23116     elem->next->prev = elem->prev;
23117   }
23118   if( pH->ht ){
23119     pEntry = &pH->ht[h];
23120     if( pEntry->chain==elem ){
23121       pEntry->chain = elem->next;
23122     }
23123     pEntry->count--;
23124     assert( pEntry->count>=0 );
23125   }
23126   sqlite3_free( elem );
23127   pH->count--;
23128   if( pH->count==0 ){
23129     assert( pH->first==0 );
23130     assert( pH->count==0 );
23131     sqlite3HashClear(pH);
23132   }
23133 }
23134 
23135 /* Attempt to locate an element of the hash table pH with a key
23136 ** that matches pKey,nKey.  Return the data for this element if it is
23137 ** found, or NULL if there is no match.
23138 */
23139 SQLITE_PRIVATE void *sqlite3HashFind(const Hash *pH, const char *pKey, int nKey){
23140   HashElem *elem;    /* The element that matches key */
23141   unsigned int h;    /* A hash on key */
23142 
23143   assert( pH!=0 );
23144   assert( pKey!=0 );
23145   assert( nKey>=0 );
23146   if( pH->ht ){
23147     h = strHash(pKey, nKey) % pH->htsize;
23148   }else{
23149     h = 0;
23150   }
23151   elem = findElementGivenHash(pH, pKey, nKey, h);
23152   return elem ? elem->data : 0;
23153 }
23154 
23155 /* Insert an element into the hash table pH.  The key is pKey,nKey
23156 ** and the data is "data".
23157 **
23158 ** If no element exists with a matching key, then a new
23159 ** element is created and NULL is returned.
23160 **
23161 ** If another element already exists with the same key, then the
23162 ** new data replaces the old data and the old data is returned.
23163 ** The key is not copied in this instance.  If a malloc fails, then
23164 ** the new data is returned and the hash table is unchanged.
23165 **
23166 ** If the "data" parameter to this function is NULL, then the
23167 ** element corresponding to "key" is removed from the hash table.
23168 */
23169 SQLITE_PRIVATE void *sqlite3HashInsert(Hash *pH, const char *pKey, int nKey, void *data){
23170   unsigned int h;       /* the hash of the key modulo hash table size */
23171   HashElem *elem;       /* Used to loop thru the element list */
23172   HashElem *new_elem;   /* New element added to the pH */
23173 
23174   assert( pH!=0 );
23175   assert( pKey!=0 );
23176   assert( nKey>=0 );
23177   if( pH->htsize ){
23178     h = strHash(pKey, nKey) % pH->htsize;
23179   }else{
23180     h = 0;
23181   }
23182   elem = findElementGivenHash(pH,pKey,nKey,h);
23183   if( elem ){
23184     void *old_data = elem->data;
23185     if( data==0 ){
23186       removeElementGivenHash(pH,elem,h);
23187     }else{
23188       elem->data = data;
23189       elem->pKey = pKey;
23190       assert(nKey==elem->nKey);
23191     }
23192     return old_data;
23193   }
23194   if( data==0 ) return 0;
23195   new_elem = (HashElem*)sqlite3Malloc( sizeof(HashElem) );
23196   if( new_elem==0 ) return data;
23197   new_elem->pKey = pKey;
23198   new_elem->nKey = nKey;
23199   new_elem->data = data;
23200   pH->count++;
23201   if( pH->count>=10 && pH->count > 2*pH->htsize ){
23202     if( rehash(pH, pH->count*2) ){
23203       assert( pH->htsize>0 );
23204       h = strHash(pKey, nKey) % pH->htsize;
23205     }
23206   }
23207   if( pH->ht ){
23208     insertElement(pH, &pH->ht[h], new_elem);
23209   }else{
23210     insertElement(pH, 0, new_elem);
23211   }
23212   return 0;
23213 }
23214 
23215 /************** End of hash.c ************************************************/
23216 /************** Begin file opcodes.c *****************************************/
23217 /* Automatically generated.  Do not edit */
23218 /* See the mkopcodec.awk script for details. */
23219 #if !defined(SQLITE_OMIT_EXPLAIN) || defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
23220 #if defined(SQLITE_ENABLE_EXPLAIN_COMMENTS) || defined(SQLITE_DEBUG)
23221 # define OpHelp(X) "\0" X
23222 #else
23223 # define OpHelp(X)
23224 #endif
23225 SQLITE_PRIVATE const char *sqlite3OpcodeName(int i){
23226  static const char *const azName[] = { "?",
23227      /*   1 */ "Function"         OpHelp("r[P3]=func(r[P2@P5])"),
23228      /*   2 */ "Savepoint"        OpHelp(""),
23229      /*   3 */ "AutoCommit"       OpHelp(""),
23230      /*   4 */ "Transaction"      OpHelp(""),
23231      /*   5 */ "SorterNext"       OpHelp(""),
23232      /*   6 */ "PrevIfOpen"       OpHelp(""),
23233      /*   7 */ "NextIfOpen"       OpHelp(""),
23234      /*   8 */ "Prev"             OpHelp(""),
23235      /*   9 */ "Next"             OpHelp(""),
23236      /*  10 */ "AggStep"          OpHelp("accum=r[P3] step(r[P2@P5])"),
23237      /*  11 */ "Checkpoint"       OpHelp(""),
23238      /*  12 */ "JournalMode"      OpHelp(""),
23239      /*  13 */ "Vacuum"           OpHelp(""),
23240      /*  14 */ "VFilter"          OpHelp("iPlan=r[P3] zPlan='P4'"),
23241      /*  15 */ "VUpdate"          OpHelp("data=r[P3@P2]"),
23242      /*  16 */ "Goto"             OpHelp(""),
23243      /*  17 */ "Gosub"            OpHelp(""),
23244      /*  18 */ "Return"           OpHelp(""),
23245      /*  19 */ "Not"              OpHelp("r[P2]= !r[P1]"),
23246      /*  20 */ "Yield"            OpHelp(""),
23247      /*  21 */ "HaltIfNull"       OpHelp("if r[P3] null then halt"),
23248      /*  22 */ "Halt"             OpHelp(""),
23249      /*  23 */ "Integer"          OpHelp("r[P2]=P1"),
23250      /*  24 */ "Int64"            OpHelp("r[P2]=P4"),
23251      /*  25 */ "String"           OpHelp("r[P2]='P4' (len=P1)"),
23252      /*  26 */ "Null"             OpHelp("r[P2..P3]=NULL"),
23253      /*  27 */ "Blob"             OpHelp("r[P2]=P4 (len=P1)"),
23254      /*  28 */ "Variable"         OpHelp("r[P2]=parameter(P1,P4)"),
23255      /*  29 */ "Move"             OpHelp("r[P2@P3]=r[P1@P3]"),
23256      /*  30 */ "Copy"             OpHelp("r[P2@P3+1]=r[P1@P3+1]"),
23257      /*  31 */ "SCopy"            OpHelp("r[P2]=r[P1]"),
23258      /*  32 */ "ResultRow"        OpHelp("output=r[P1@P2]"),
23259      /*  33 */ "CollSeq"          OpHelp(""),
23260      /*  34 */ "AddImm"           OpHelp("r[P1]=r[P1]+P2"),
23261      /*  35 */ "MustBeInt"        OpHelp(""),
23262      /*  36 */ "RealAffinity"     OpHelp(""),
23263      /*  37 */ "Permutation"      OpHelp(""),
23264      /*  38 */ "Compare"          OpHelp(""),
23265      /*  39 */ "Jump"             OpHelp(""),
23266      /*  40 */ "Once"             OpHelp(""),
23267      /*  41 */ "If"               OpHelp(""),
23268      /*  42 */ "IfNot"            OpHelp(""),
23269      /*  43 */ "Column"           OpHelp("r[P3]=PX"),
23270      /*  44 */ "Affinity"         OpHelp("affinity(r[P1@P2])"),
23271      /*  45 */ "MakeRecord"       OpHelp("r[P3]=mkrec(r[P1@P2])"),
23272      /*  46 */ "Count"            OpHelp("r[P2]=count()"),
23273      /*  47 */ "ReadCookie"       OpHelp(""),
23274      /*  48 */ "SetCookie"        OpHelp(""),
23275      /*  49 */ "VerifyCookie"     OpHelp(""),
23276      /*  50 */ "OpenRead"         OpHelp("root=P2 iDb=P3"),
23277      /*  51 */ "OpenWrite"        OpHelp("root=P2 iDb=P3"),
23278      /*  52 */ "OpenAutoindex"    OpHelp("nColumn=P2"),
23279      /*  53 */ "OpenEphemeral"    OpHelp("nColumn=P2"),
23280      /*  54 */ "SorterOpen"       OpHelp(""),
23281      /*  55 */ "OpenPseudo"       OpHelp("content in r[P2@P3]"),
23282      /*  56 */ "Close"            OpHelp(""),
23283      /*  57 */ "SeekLt"           OpHelp("key=r[P3@P4]"),
23284      /*  58 */ "SeekLe"           OpHelp("key=r[P3@P4]"),
23285      /*  59 */ "SeekGe"           OpHelp("key=r[P3@P4]"),
23286      /*  60 */ "SeekGt"           OpHelp("key=r[P3@P4]"),
23287      /*  61 */ "Seek"             OpHelp("intkey=r[P2]"),
23288      /*  62 */ "NoConflict"       OpHelp("key=r[P3@P4]"),
23289      /*  63 */ "NotFound"         OpHelp("key=r[P3@P4]"),
23290      /*  64 */ "Found"            OpHelp("key=r[P3@P4]"),
23291      /*  65 */ "NotExists"        OpHelp("intkey=r[P3]"),
23292      /*  66 */ "Sequence"         OpHelp("r[P2]=rowid"),
23293      /*  67 */ "NewRowid"         OpHelp("r[P2]=rowid"),
23294      /*  68 */ "Insert"           OpHelp("intkey=r[P3] data=r[P2]"),
23295      /*  69 */ "InsertInt"        OpHelp("intkey=P3 data=r[P2]"),
23296      /*  70 */ "Delete"           OpHelp(""),
23297      /*  71 */ "Or"               OpHelp("r[P3]=(r[P1] || r[P2])"),
23298      /*  72 */ "And"              OpHelp("r[P3]=(r[P1] && r[P2])"),
23299      /*  73 */ "ResetCount"       OpHelp(""),
23300      /*  74 */ "SorterCompare"    OpHelp("if key(P1)!=rtrim(r[P3],P4) goto P2"),
23301      /*  75 */ "SorterData"       OpHelp("r[P2]=data"),
23302      /*  76 */ "IsNull"           OpHelp("if r[P1]==NULL goto P2"),
23303      /*  77 */ "NotNull"          OpHelp("if r[P1]!=NULL goto P2"),
23304      /*  78 */ "Ne"               OpHelp("if r[P1]!=r[P3] goto P2"),
23305      /*  79 */ "Eq"               OpHelp("if r[P1]==r[P3] goto P2"),
23306      /*  80 */ "Gt"               OpHelp("if r[P1]>r[P3] goto P2"),
23307      /*  81 */ "Le"               OpHelp("if r[P1]<=r[P3] goto P2"),
23308      /*  82 */ "Lt"               OpHelp("if r[P1]<r[P3] goto P2"),
23309      /*  83 */ "Ge"               OpHelp("if r[P1]>=r[P3] goto P2"),
23310      /*  84 */ "RowKey"           OpHelp("r[P2]=key"),
23311      /*  85 */ "BitAnd"           OpHelp("r[P3]=r[P1]&r[P2]"),
23312      /*  86 */ "BitOr"            OpHelp("r[P3]=r[P1]|r[P2]"),
23313      /*  87 */ "ShiftLeft"        OpHelp("r[P3]=r[P2]<<r[P1]"),
23314      /*  88 */ "ShiftRight"       OpHelp("r[P3]=r[P2]>>r[P1]"),
23315      /*  89 */ "Add"              OpHelp("r[P3]=r[P1]+r[P2]"),
23316      /*  90 */ "Subtract"         OpHelp("r[P3]=r[P2]-r[P1]"),
23317      /*  91 */ "Multiply"         OpHelp("r[P3]=r[P1]*r[P2]"),
23318      /*  92 */ "Divide"           OpHelp("r[P3]=r[P2]/r[P1]"),
23319      /*  93 */ "Remainder"        OpHelp("r[P3]=r[P2]%r[P1]"),
23320      /*  94 */ "Concat"           OpHelp("r[P3]=r[P2]+r[P1]"),
23321      /*  95 */ "RowData"          OpHelp("r[P2]=data"),
23322      /*  96 */ "BitNot"           OpHelp("r[P1]= ~r[P1]"),
23323      /*  97 */ "String8"          OpHelp("r[P2]='P4'"),
23324      /*  98 */ "Rowid"            OpHelp("r[P2]=rowid"),
23325      /*  99 */ "NullRow"          OpHelp(""),
23326      /* 100 */ "Last"             OpHelp(""),
23327      /* 101 */ "SorterSort"       OpHelp(""),
23328      /* 102 */ "Sort"             OpHelp(""),
23329      /* 103 */ "Rewind"           OpHelp(""),
23330      /* 104 */ "SorterInsert"     OpHelp(""),
23331      /* 105 */ "IdxInsert"        OpHelp("key=r[P2]"),
23332      /* 106 */ "IdxDelete"        OpHelp("key=r[P2@P3]"),
23333      /* 107 */ "IdxRowid"         OpHelp("r[P2]=rowid"),
23334      /* 108 */ "IdxLT"            OpHelp("key=r[P3@P4]"),
23335      /* 109 */ "IdxGE"            OpHelp("key=r[P3@P4]"),
23336      /* 110 */ "Destroy"          OpHelp(""),
23337      /* 111 */ "Clear"            OpHelp(""),
23338      /* 112 */ "CreateIndex"      OpHelp("r[P2]=root iDb=P1"),
23339      /* 113 */ "CreateTable"      OpHelp("r[P2]=root iDb=P1"),
23340      /* 114 */ "ParseSchema"      OpHelp(""),
23341      /* 115 */ "LoadAnalysis"     OpHelp(""),
23342      /* 116 */ "DropTable"        OpHelp(""),
23343      /* 117 */ "DropIndex"        OpHelp(""),
23344      /* 118 */ "DropTrigger"      OpHelp(""),
23345      /* 119 */ "IntegrityCk"      OpHelp(""),
23346      /* 120 */ "RowSetAdd"        OpHelp("rowset(P1)=r[P2]"),
23347      /* 121 */ "RowSetRead"       OpHelp("r[P3]=rowset(P1)"),
23348      /* 122 */ "RowSetTest"       OpHelp("if r[P3] in rowset(P1) goto P2"),
23349      /* 123 */ "Program"          OpHelp(""),
23350      /* 124 */ "Param"            OpHelp(""),
23351      /* 125 */ "FkCounter"        OpHelp("fkctr[P1]+=P2"),
23352      /* 126 */ "FkIfZero"         OpHelp("if fkctr[P1]==0 goto P2"),
23353      /* 127 */ "MemMax"           OpHelp("r[P1]=max(r[P1],r[P2])"),
23354      /* 128 */ "IfPos"            OpHelp("if r[P1]>0 goto P2"),
23355      /* 129 */ "IfNeg"            OpHelp("if r[P1]<0 goto P2"),
23356      /* 130 */ "IfZero"           OpHelp("r[P1]+=P3, if r[P1]==0 goto P2"),
23357      /* 131 */ "AggFinal"         OpHelp("accum=r[P1] N=P2"),
23358      /* 132 */ "IncrVacuum"       OpHelp(""),
23359      /* 133 */ "Real"             OpHelp("r[P2]=P4"),
23360      /* 134 */ "Expire"           OpHelp(""),
23361      /* 135 */ "TableLock"        OpHelp("iDb=P1 root=P2 write=P3"),
23362      /* 136 */ "VBegin"           OpHelp(""),
23363      /* 137 */ "VCreate"          OpHelp(""),
23364      /* 138 */ "VDestroy"         OpHelp(""),
23365      /* 139 */ "VOpen"            OpHelp(""),
23366      /* 140 */ "VColumn"          OpHelp("r[P3]=vcolumn(P2)"),
23367      /* 141 */ "VNext"            OpHelp(""),
23368      /* 142 */ "VRename"          OpHelp(""),
23369      /* 143 */ "ToText"           OpHelp(""),
23370      /* 144 */ "ToBlob"           OpHelp(""),
23371      /* 145 */ "ToNumeric"        OpHelp(""),
23372      /* 146 */ "ToInt"            OpHelp(""),
23373      /* 147 */ "ToReal"           OpHelp(""),
23374      /* 148 */ "Pagecount"        OpHelp(""),
23375      /* 149 */ "MaxPgcnt"         OpHelp(""),
23376      /* 150 */ "Trace"            OpHelp(""),
23377      /* 151 */ "Noop"             OpHelp(""),
23378      /* 152 */ "Explain"          OpHelp(""),
23379   };
23380   return azName[i];
23381 }
23382 #endif
23383 
23384 /************** End of opcodes.c *********************************************/
23385 /************** Begin file os_unix.c *****************************************/
23386 /*
23387 ** 2004 May 22
23388 **
23389 ** The author disclaims copyright to this source code.  In place of
23390 ** a legal notice, here is a blessing:
23391 **
23392 **    May you do good and not evil.
23393 **    May you find forgiveness for yourself and forgive others.
23394 **    May you share freely, never taking more than you give.
23395 **
23396 ******************************************************************************
23397 **
23398 ** This file contains the VFS implementation for unix-like operating systems
23399 ** include Linux, MacOSX, *BSD, QNX, VxWorks, AIX, HPUX, and others.
23400 **
23401 ** There are actually several different VFS implementations in this file.
23402 ** The differences are in the way that file locking is done.  The default
23403 ** implementation uses Posix Advisory Locks.  Alternative implementations
23404 ** use flock(), dot-files, various proprietary locking schemas, or simply
23405 ** skip locking all together.
23406 **
23407 ** This source file is organized into divisions where the logic for various
23408 ** subfunctions is contained within the appropriate division.  PLEASE
23409 ** KEEP THE STRUCTURE OF THIS FILE INTACT.  New code should be placed
23410 ** in the correct division and should be clearly labeled.
23411 **
23412 ** The layout of divisions is as follows:
23413 **
23414 **   *  General-purpose declarations and utility functions.
23415 **   *  Unique file ID logic used by VxWorks.
23416 **   *  Various locking primitive implementations (all except proxy locking):
23417 **      + for Posix Advisory Locks
23418 **      + for no-op locks
23419 **      + for dot-file locks
23420 **      + for flock() locking
23421 **      + for named semaphore locks (VxWorks only)
23422 **      + for AFP filesystem locks (MacOSX only)
23423 **   *  sqlite3_file methods not associated with locking.
23424 **   *  Definitions of sqlite3_io_methods objects for all locking
23425 **      methods plus "finder" functions for each locking method.
23426 **   *  sqlite3_vfs method implementations.
23427 **   *  Locking primitives for the proxy uber-locking-method. (MacOSX only)
23428 **   *  Definitions of sqlite3_vfs objects for all locking methods
23429 **      plus implementations of sqlite3_os_init() and sqlite3_os_end().
23430 */
23431 #if SQLITE_OS_UNIX              /* This file is used on unix only */
23432 
23433 /*
23434 ** There are various methods for file locking used for concurrency
23435 ** control:
23436 **
23437 **   1. POSIX locking (the default),
23438 **   2. No locking,
23439 **   3. Dot-file locking,
23440 **   4. flock() locking,
23441 **   5. AFP locking (OSX only),
23442 **   6. Named POSIX semaphores (VXWorks only),
23443 **   7. proxy locking. (OSX only)
23444 **
23445 ** Styles 4, 5, and 7 are only available of SQLITE_ENABLE_LOCKING_STYLE
23446 ** is defined to 1.  The SQLITE_ENABLE_LOCKING_STYLE also enables automatic
23447 ** selection of the appropriate locking style based on the filesystem
23448 ** where the database is located.
23449 */
23450 #if !defined(SQLITE_ENABLE_LOCKING_STYLE)
23451 #  if defined(__APPLE__)
23452 #    define SQLITE_ENABLE_LOCKING_STYLE 1
23453 #  else
23454 #    define SQLITE_ENABLE_LOCKING_STYLE 0
23455 #  endif
23456 #endif
23457 
23458 /*
23459 ** Define the OS_VXWORKS pre-processor macro to 1 if building on
23460 ** vxworks, or 0 otherwise.
23461 */
23462 #ifndef OS_VXWORKS
23463 #  if defined(__RTP__) || defined(_WRS_KERNEL)
23464 #    define OS_VXWORKS 1
23465 #  else
23466 #    define OS_VXWORKS 0
23467 #  endif
23468 #endif
23469 
23470 /*
23471 ** These #defines should enable >2GB file support on Posix if the
23472 ** underlying operating system supports it.  If the OS lacks
23473 ** large file support, these should be no-ops.
23474 **
23475 ** Large file support can be disabled using the -DSQLITE_DISABLE_LFS switch
23476 ** on the compiler command line.  This is necessary if you are compiling
23477 ** on a recent machine (ex: RedHat 7.2) but you want your code to work
23478 ** on an older machine (ex: RedHat 6.0).  If you compile on RedHat 7.2
23479 ** without this option, LFS is enable.  But LFS does not exist in the kernel
23480 ** in RedHat 6.0, so the code won't work.  Hence, for maximum binary
23481 ** portability you should omit LFS.
23482 **
23483 ** The previous paragraph was written in 2005.  (This paragraph is written
23484 ** on 2008-11-28.) These days, all Linux kernels support large files, so
23485 ** you should probably leave LFS enabled.  But some embedded platforms might
23486 ** lack LFS in which case the SQLITE_DISABLE_LFS macro might still be useful.
23487 */
23488 #ifndef SQLITE_DISABLE_LFS
23489 # define _LARGE_FILE       1
23490 # ifndef _FILE_OFFSET_BITS
23491 #   define _FILE_OFFSET_BITS 64
23492 # endif
23493 # define _LARGEFILE_SOURCE 1
23494 #endif
23495 
23496 /*
23497 ** standard include files.
23498 */
23499 #include <sys/types.h>
23500 #include <sys/stat.h>
23501 #include <fcntl.h>
23502 #include <unistd.h>
23503 /* #include <time.h> */
23504 #include <sys/time.h>
23505 #include <errno.h>
23506 #if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0
23507 #include <sys/mman.h>
23508 #endif
23509 
23510 
23511 #if SQLITE_ENABLE_LOCKING_STYLE
23512 # include <sys/ioctl.h>
23513 # if OS_VXWORKS
23514 #  include <semaphore.h>
23515 #  include <limits.h>
23516 # else
23517 #  include <sys/file.h>
23518 #  include <sys/param.h>
23519 # endif
23520 #endif /* SQLITE_ENABLE_LOCKING_STYLE */
23521 
23522 #if defined(__APPLE__) || (SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORKS)
23523 # include <sys/mount.h>
23524 #endif
23525 
23526 #ifdef HAVE_UTIME
23527 # include <utime.h>
23528 #endif
23529 
23530 /*
23531 ** Allowed values of unixFile.fsFlags
23532 */
23533 #define SQLITE_FSFLAGS_IS_MSDOS     0x1
23534 
23535 /*
23536 ** If we are to be thread-safe, include the pthreads header and define
23537 ** the SQLITE_UNIX_THREADS macro.
23538 */
23539 #if SQLITE_THREADSAFE
23540 /* # include <pthread.h> */
23541 # define SQLITE_UNIX_THREADS 1
23542 #endif
23543 
23544 /*
23545 ** Default permissions when creating a new file
23546 */
23547 #ifndef SQLITE_DEFAULT_FILE_PERMISSIONS
23548 # define SQLITE_DEFAULT_FILE_PERMISSIONS 0644
23549 #endif
23550 
23551 /*
23552 ** Default permissions when creating auto proxy dir
23553 */
23554 #ifndef SQLITE_DEFAULT_PROXYDIR_PERMISSIONS
23555 # define SQLITE_DEFAULT_PROXYDIR_PERMISSIONS 0755
23556 #endif
23557 
23558 /*
23559 ** Maximum supported path-length.
23560 */
23561 #define MAX_PATHNAME 512
23562 
23563 /*
23564 ** Only set the lastErrno if the error code is a real error and not
23565 ** a normal expected return code of SQLITE_BUSY or SQLITE_OK
23566 */
23567 #define IS_LOCK_ERROR(x)  ((x != SQLITE_OK) && (x != SQLITE_BUSY))
23568 
23569 /* Forward references */
23570 typedef struct unixShm unixShm;               /* Connection shared memory */
23571 typedef struct unixShmNode unixShmNode;       /* Shared memory instance */
23572 typedef struct unixInodeInfo unixInodeInfo;   /* An i-node */
23573 typedef struct UnixUnusedFd UnixUnusedFd;     /* An unused file descriptor */
23574 
23575 /*
23576 ** Sometimes, after a file handle is closed by SQLite, the file descriptor
23577 ** cannot be closed immediately. In these cases, instances of the following
23578 ** structure are used to store the file descriptor while waiting for an
23579 ** opportunity to either close or reuse it.
23580 */
23581 struct UnixUnusedFd {
23582   int fd;                   /* File descriptor to close */
23583   int flags;                /* Flags this file descriptor was opened with */
23584   UnixUnusedFd *pNext;      /* Next unused file descriptor on same file */
23585 };
23586 
23587 /*
23588 ** The unixFile structure is subclass of sqlite3_file specific to the unix
23589 ** VFS implementations.
23590 */
23591 typedef struct unixFile unixFile;
23592 struct unixFile {
23593   sqlite3_io_methods const *pMethod;  /* Always the first entry */
23594   sqlite3_vfs *pVfs;                  /* The VFS that created this unixFile */
23595   unixInodeInfo *pInode;              /* Info about locks on this inode */
23596   int h;                              /* The file descriptor */
23597   unsigned char eFileLock;            /* The type of lock held on this fd */
23598   unsigned short int ctrlFlags;       /* Behavioral bits.  UNIXFILE_* flags */
23599   int lastErrno;                      /* The unix errno from last I/O error */
23600   void *lockingContext;               /* Locking style specific state */
23601   UnixUnusedFd *pUnused;              /* Pre-allocated UnixUnusedFd */
23602   const char *zPath;                  /* Name of the file */
23603   unixShm *pShm;                      /* Shared memory segment information */
23604   int szChunk;                        /* Configured by FCNTL_CHUNK_SIZE */
23605 #if SQLITE_MAX_MMAP_SIZE>0
23606   int nFetchOut;                      /* Number of outstanding xFetch refs */
23607   sqlite3_int64 mmapSize;             /* Usable size of mapping at pMapRegion */
23608   sqlite3_int64 mmapSizeActual;       /* Actual size of mapping at pMapRegion */
23609   sqlite3_int64 mmapSizeMax;          /* Configured FCNTL_MMAP_SIZE value */
23610   void *pMapRegion;                   /* Memory mapped region */
23611 #endif
23612 #ifdef __QNXNTO__
23613   int sectorSize;                     /* Device sector size */
23614   int deviceCharacteristics;          /* Precomputed device characteristics */
23615 #endif
23616 #if SQLITE_ENABLE_LOCKING_STYLE
23617   int openFlags;                      /* The flags specified at open() */
23618 #endif
23619 #if SQLITE_ENABLE_LOCKING_STYLE || defined(__APPLE__)
23620   unsigned fsFlags;                   /* cached details from statfs() */
23621 #endif
23622 #if OS_VXWORKS
23623   struct vxworksFileId *pId;          /* Unique file ID */
23624 #endif
23625 #ifdef SQLITE_DEBUG
23626   /* The next group of variables are used to track whether or not the
23627   ** transaction counter in bytes 24-27 of database files are updated
23628   ** whenever any part of the database changes.  An assertion fault will
23629   ** occur if a file is updated without also updating the transaction
23630   ** counter.  This test is made to avoid new problems similar to the
23631   ** one described by ticket #3584.
23632   */
23633   unsigned char transCntrChng;   /* True if the transaction counter changed */
23634   unsigned char dbUpdate;        /* True if any part of database file changed */
23635   unsigned char inNormalWrite;   /* True if in a normal write operation */
23636 
23637 #endif
23638 
23639 #ifdef SQLITE_TEST
23640   /* In test mode, increase the size of this structure a bit so that
23641   ** it is larger than the struct CrashFile defined in test6.c.
23642   */
23643   char aPadding[32];
23644 #endif
23645 };
23646 
23647 /* This variable holds the process id (pid) from when the xRandomness()
23648 ** method was called.  If xOpen() is called from a different process id,
23649 ** indicating that a fork() has occurred, the PRNG will be reset.
23650 */
23651 static int randomnessPid = 0;
23652 
23653 /*
23654 ** Allowed values for the unixFile.ctrlFlags bitmask:
23655 */
23656 #define UNIXFILE_EXCL        0x01     /* Connections from one process only */
23657 #define UNIXFILE_RDONLY      0x02     /* Connection is read only */
23658 #define UNIXFILE_PERSIST_WAL 0x04     /* Persistent WAL mode */
23659 #ifndef SQLITE_DISABLE_DIRSYNC
23660 # define UNIXFILE_DIRSYNC    0x08     /* Directory sync needed */
23661 #else
23662 # define UNIXFILE_DIRSYNC    0x00
23663 #endif
23664 #define UNIXFILE_PSOW        0x10     /* SQLITE_IOCAP_POWERSAFE_OVERWRITE */
23665 #define UNIXFILE_DELETE      0x20     /* Delete on close */
23666 #define UNIXFILE_URI         0x40     /* Filename might have query parameters */
23667 #define UNIXFILE_NOLOCK      0x80     /* Do no file locking */
23668 #define UNIXFILE_WARNED    0x0100     /* verifyDbFile() warnings have been issued */
23669 
23670 /*
23671 ** Include code that is common to all os_*.c files
23672 */
23673 /************** Include os_common.h in the middle of os_unix.c ***************/
23674 /************** Begin file os_common.h ***************************************/
23675 /*
23676 ** 2004 May 22
23677 **
23678 ** The author disclaims copyright to this source code.  In place of
23679 ** a legal notice, here is a blessing:
23680 **
23681 **    May you do good and not evil.
23682 **    May you find forgiveness for yourself and forgive others.
23683 **    May you share freely, never taking more than you give.
23684 **
23685 ******************************************************************************
23686 **
23687 ** This file contains macros and a little bit of code that is common to
23688 ** all of the platform-specific files (os_*.c) and is #included into those
23689 ** files.
23690 **
23691 ** This file should be #included by the os_*.c files only.  It is not a
23692 ** general purpose header file.
23693 */
23694 #ifndef _OS_COMMON_H_
23695 #define _OS_COMMON_H_
23696 
23697 /*
23698 ** At least two bugs have slipped in because we changed the MEMORY_DEBUG
23699 ** macro to SQLITE_DEBUG and some older makefiles have not yet made the
23700 ** switch.  The following code should catch this problem at compile-time.
23701 */
23702 #ifdef MEMORY_DEBUG
23703 # error "The MEMORY_DEBUG macro is obsolete.  Use SQLITE_DEBUG instead."
23704 #endif
23705 
23706 #if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
23707 # ifndef SQLITE_DEBUG_OS_TRACE
23708 #   define SQLITE_DEBUG_OS_TRACE 0
23709 # endif
23710   int sqlite3OSTrace = SQLITE_DEBUG_OS_TRACE;
23711 # define OSTRACE(X)          if( sqlite3OSTrace ) sqlite3DebugPrintf X
23712 #else
23713 # define OSTRACE(X)
23714 #endif
23715 
23716 /*
23717 ** Macros for performance tracing.  Normally turned off.  Only works
23718 ** on i486 hardware.
23719 */
23720 #ifdef SQLITE_PERFORMANCE_TRACE
23721 
23722 /*
23723 ** hwtime.h contains inline assembler code for implementing
23724 ** high-performance timing routines.
23725 */
23726 /************** Include hwtime.h in the middle of os_common.h ****************/
23727 /************** Begin file hwtime.h ******************************************/
23728 /*
23729 ** 2008 May 27
23730 **
23731 ** The author disclaims copyright to this source code.  In place of
23732 ** a legal notice, here is a blessing:
23733 **
23734 **    May you do good and not evil.
23735 **    May you find forgiveness for yourself and forgive others.
23736 **    May you share freely, never taking more than you give.
23737 **
23738 ******************************************************************************
23739 **
23740 ** This file contains inline asm code for retrieving "high-performance"
23741 ** counters for x86 class CPUs.
23742 */
23743 #ifndef _HWTIME_H_
23744 #define _HWTIME_H_
23745 
23746 /*
23747 ** The following routine only works on pentium-class (or newer) processors.
23748 ** It uses the RDTSC opcode to read the cycle count value out of the
23749 ** processor and returns that value.  This can be used for high-res
23750 ** profiling.
23751 */
23752 #if (defined(__GNUC__) || defined(_MSC_VER)) && \
23753       (defined(i386) || defined(__i386__) || defined(_M_IX86))
23754 
23755   #if defined(__GNUC__)
23756 
23757   __inline__ sqlite_uint64 sqlite3Hwtime(void){
23758      unsigned int lo, hi;
23759      __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
23760      return (sqlite_uint64)hi << 32 | lo;
23761   }
23762 
23763   #elif defined(_MSC_VER)
23764 
23765   __declspec(naked) __inline sqlite_uint64 __cdecl sqlite3Hwtime(void){
23766      __asm {
23767         rdtsc
23768         ret       ; return value at EDX:EAX
23769      }
23770   }
23771 
23772   #endif
23773 
23774 #elif (defined(__GNUC__) && defined(__x86_64__))
23775 
23776   __inline__ sqlite_uint64 sqlite3Hwtime(void){
23777       unsigned long val;
23778       __asm__ __volatile__ ("rdtsc" : "=A" (val));
23779       return val;
23780   }
23781 
23782 #elif (defined(__GNUC__) && defined(__ppc__))
23783 
23784   __inline__ sqlite_uint64 sqlite3Hwtime(void){
23785       unsigned long long retval;
23786       unsigned long junk;
23787       __asm__ __volatile__ ("\n\
23788           1:      mftbu   %1\n\
23789                   mftb    %L0\n\
23790                   mftbu   %0\n\
23791                   cmpw    %0,%1\n\
23792                   bne     1b"
23793                   : "=r" (retval), "=r" (junk));
23794       return retval;
23795   }
23796 
23797 #else
23798 
23799   #error Need implementation of sqlite3Hwtime() for your platform.
23800 
23801   /*
23802   ** To compile without implementing sqlite3Hwtime() for your platform,
23803   ** you can remove the above #error and use the following
23804   ** stub function.  You will lose timing support for many
23805   ** of the debugging and testing utilities, but it should at
23806   ** least compile and run.
23807   */
23808 SQLITE_PRIVATE   sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); }
23809 
23810 #endif
23811 
23812 #endif /* !defined(_HWTIME_H_) */
23813 
23814 /************** End of hwtime.h **********************************************/
23815 /************** Continuing where we left off in os_common.h ******************/
23816 
23817 static sqlite_uint64 g_start;
23818 static sqlite_uint64 g_elapsed;
23819 #define TIMER_START       g_start=sqlite3Hwtime()
23820 #define TIMER_END         g_elapsed=sqlite3Hwtime()-g_start
23821 #define TIMER_ELAPSED     g_elapsed
23822 #else
23823 #define TIMER_START
23824 #define TIMER_END
23825 #define TIMER_ELAPSED     ((sqlite_uint64)0)
23826 #endif
23827 
23828 /*
23829 ** If we compile with the SQLITE_TEST macro set, then the following block
23830 ** of code will give us the ability to simulate a disk I/O error.  This
23831 ** is used for testing the I/O recovery logic.
23832 */
23833 #ifdef SQLITE_TEST
23834 SQLITE_API int sqlite3_io_error_hit = 0;            /* Total number of I/O Errors */
23835 SQLITE_API int sqlite3_io_error_hardhit = 0;        /* Number of non-benign errors */
23836 SQLITE_API int sqlite3_io_error_pending = 0;        /* Count down to first I/O error */
23837 SQLITE_API int sqlite3_io_error_persist = 0;        /* True if I/O errors persist */
23838 SQLITE_API int sqlite3_io_error_benign = 0;         /* True if errors are benign */
23839 SQLITE_API int sqlite3_diskfull_pending = 0;
23840 SQLITE_API int sqlite3_diskfull = 0;
23841 #define SimulateIOErrorBenign(X) sqlite3_io_error_benign=(X)
23842 #define SimulateIOError(CODE)  \
23843   if( (sqlite3_io_error_persist && sqlite3_io_error_hit) \
23844        || sqlite3_io_error_pending-- == 1 )  \
23845               { local_ioerr(); CODE; }
23846 static void local_ioerr(){
23847   IOTRACE(("IOERR\n"));
23848   sqlite3_io_error_hit++;
23849   if( !sqlite3_io_error_benign ) sqlite3_io_error_hardhit++;
23850 }
23851 #define SimulateDiskfullError(CODE) \
23852    if( sqlite3_diskfull_pending ){ \
23853      if( sqlite3_diskfull_pending == 1 ){ \
23854        local_ioerr(); \
23855        sqlite3_diskfull = 1; \
23856        sqlite3_io_error_hit = 1; \
23857        CODE; \
23858      }else{ \
23859        sqlite3_diskfull_pending--; \
23860      } \
23861    }
23862 #else
23863 #define SimulateIOErrorBenign(X)
23864 #define SimulateIOError(A)
23865 #define SimulateDiskfullError(A)
23866 #endif
23867 
23868 /*
23869 ** When testing, keep a count of the number of open files.
23870 */
23871 #ifdef SQLITE_TEST
23872 SQLITE_API int sqlite3_open_file_count = 0;
23873 #define OpenCounter(X)  sqlite3_open_file_count+=(X)
23874 #else
23875 #define OpenCounter(X)
23876 #endif
23877 
23878 #endif /* !defined(_OS_COMMON_H_) */
23879 
23880 /************** End of os_common.h *******************************************/
23881 /************** Continuing where we left off in os_unix.c ********************/
23882 
23883 /*
23884 ** Define various macros that are missing from some systems.
23885 */
23886 #ifndef O_LARGEFILE
23887 # define O_LARGEFILE 0
23888 #endif
23889 #ifdef SQLITE_DISABLE_LFS
23890 # undef O_LARGEFILE
23891 # define O_LARGEFILE 0
23892 #endif
23893 #ifndef O_NOFOLLOW
23894 # define O_NOFOLLOW 0
23895 #endif
23896 #ifndef O_BINARY
23897 # define O_BINARY 0
23898 #endif
23899 
23900 /*
23901 ** The threadid macro resolves to the thread-id or to 0.  Used for
23902 ** testing and debugging only.
23903 */
23904 #if SQLITE_THREADSAFE
23905 #define threadid pthread_self()
23906 #else
23907 #define threadid 0
23908 #endif
23909 
23910 /*
23911 ** HAVE_MREMAP defaults to true on Linux and false everywhere else.
23912 */
23913 #if !defined(HAVE_MREMAP)
23914 # if defined(__linux__) && defined(_GNU_SOURCE)
23915 #  define HAVE_MREMAP 1
23916 # else
23917 #  define HAVE_MREMAP 0
23918 # endif
23919 #endif
23920 
23921 /*
23922 ** Different Unix systems declare open() in different ways.  Same use
23923 ** open(const char*,int,mode_t).  Others use open(const char*,int,...).
23924 ** The difference is important when using a pointer to the function.
23925 **
23926 ** The safest way to deal with the problem is to always use this wrapper
23927 ** which always has the same well-defined interface.
23928 */
23929 static int posixOpen(const char *zFile, int flags, int mode){
23930   return open(zFile, flags, mode);
23931 }
23932 
23933 /*
23934 ** On some systems, calls to fchown() will trigger a message in a security
23935 ** log if they come from non-root processes.  So avoid calling fchown() if
23936 ** we are not running as root.
23937 */
23938 static int posixFchown(int fd, uid_t uid, gid_t gid){
23939   return geteuid() ? 0 : fchown(fd,uid,gid);
23940 }
23941 
23942 /* Forward reference */
23943 static int openDirectory(const char*, int*);
23944 
23945 /*
23946 ** Many system calls are accessed through pointer-to-functions so that
23947 ** they may be overridden at runtime to facilitate fault injection during
23948 ** testing and sandboxing.  The following array holds the names and pointers
23949 ** to all overrideable system calls.
23950 */
23951 static struct unix_syscall {
23952   const char *zName;            /* Name of the system call */
23953   sqlite3_syscall_ptr pCurrent; /* Current value of the system call */
23954   sqlite3_syscall_ptr pDefault; /* Default value */
23955 } aSyscall[] = {
23956   { "open",         (sqlite3_syscall_ptr)posixOpen,  0  },
23957 #define osOpen      ((int(*)(const char*,int,int))aSyscall[0].pCurrent)
23958 
23959   { "close",        (sqlite3_syscall_ptr)close,      0  },
23960 #define osClose     ((int(*)(int))aSyscall[1].pCurrent)
23961 
23962   { "access",       (sqlite3_syscall_ptr)access,     0  },
23963 #define osAccess    ((int(*)(const char*,int))aSyscall[2].pCurrent)
23964 
23965   { "getcwd",       (sqlite3_syscall_ptr)getcwd,     0  },
23966 #define osGetcwd    ((char*(*)(char*,size_t))aSyscall[3].pCurrent)
23967 
23968   { "stat",         (sqlite3_syscall_ptr)stat,       0  },
23969 #define osStat      ((int(*)(const char*,struct stat*))aSyscall[4].pCurrent)
23970 
23971 /*
23972 ** The DJGPP compiler environment looks mostly like Unix, but it
23973 ** lacks the fcntl() system call.  So redefine fcntl() to be something
23974 ** that always succeeds.  This means that locking does not occur under
23975 ** DJGPP.  But it is DOS - what did you expect?
23976 */
23977 #ifdef __DJGPP__
23978   { "fstat",        0,                 0  },
23979 #define osFstat(a,b,c)    0
23980 #else
23981   { "fstat",        (sqlite3_syscall_ptr)fstat,      0  },
23982 #define osFstat     ((int(*)(int,struct stat*))aSyscall[5].pCurrent)
23983 #endif
23984 
23985   { "ftruncate",    (sqlite3_syscall_ptr)ftruncate,  0  },
23986 #define osFtruncate ((int(*)(int,off_t))aSyscall[6].pCurrent)
23987 
23988   { "fcntl",        (sqlite3_syscall_ptr)fcntl,      0  },
23989 #define osFcntl     ((int(*)(int,int,...))aSyscall[7].pCurrent)
23990 
23991   { "read",         (sqlite3_syscall_ptr)read,       0  },
23992 #define osRead      ((ssize_t(*)(int,void*,size_t))aSyscall[8].pCurrent)
23993 
23994 #if defined(USE_PREAD) || SQLITE_ENABLE_LOCKING_STYLE
23995   { "pread",        (sqlite3_syscall_ptr)pread,      0  },
23996 #else
23997   { "pread",        (sqlite3_syscall_ptr)0,          0  },
23998 #endif
23999 #define osPread     ((ssize_t(*)(int,void*,size_t,off_t))aSyscall[9].pCurrent)
24000 
24001 #if defined(USE_PREAD64)
24002   { "pread64",      (sqlite3_syscall_ptr)pread64,    0  },
24003 #else
24004   { "pread64",      (sqlite3_syscall_ptr)0,          0  },
24005 #endif
24006 #define osPread64   ((ssize_t(*)(int,void*,size_t,off_t))aSyscall[10].pCurrent)
24007 
24008   { "write",        (sqlite3_syscall_ptr)write,      0  },
24009 #define osWrite     ((ssize_t(*)(int,const void*,size_t))aSyscall[11].pCurrent)
24010 
24011 #if defined(USE_PREAD) || SQLITE_ENABLE_LOCKING_STYLE
24012   { "pwrite",       (sqlite3_syscall_ptr)pwrite,     0  },
24013 #else
24014   { "pwrite",       (sqlite3_syscall_ptr)0,          0  },
24015 #endif
24016 #define osPwrite    ((ssize_t(*)(int,const void*,size_t,off_t))\
24017                     aSyscall[12].pCurrent)
24018 
24019 #if defined(USE_PREAD64)
24020   { "pwrite64",     (sqlite3_syscall_ptr)pwrite64,   0  },
24021 #else
24022   { "pwrite64",     (sqlite3_syscall_ptr)0,          0  },
24023 #endif
24024 #define osPwrite64  ((ssize_t(*)(int,const void*,size_t,off_t))\
24025                     aSyscall[13].pCurrent)
24026 
24027   { "fchmod",       (sqlite3_syscall_ptr)fchmod,     0  },
24028 #define osFchmod    ((int(*)(int,mode_t))aSyscall[14].pCurrent)
24029 
24030 #if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE
24031   { "fallocate",    (sqlite3_syscall_ptr)posix_fallocate,  0 },
24032 #else
24033   { "fallocate",    (sqlite3_syscall_ptr)0,                0 },
24034 #endif
24035 #define osFallocate ((int(*)(int,off_t,off_t))aSyscall[15].pCurrent)
24036 
24037   { "unlink",       (sqlite3_syscall_ptr)unlink,           0 },
24038 #define osUnlink    ((int(*)(const char*))aSyscall[16].pCurrent)
24039 
24040   { "openDirectory",    (sqlite3_syscall_ptr)openDirectory,      0 },
24041 #define osOpenDirectory ((int(*)(const char*,int*))aSyscall[17].pCurrent)
24042 
24043   { "mkdir",        (sqlite3_syscall_ptr)mkdir,           0 },
24044 #define osMkdir     ((int(*)(const char*,mode_t))aSyscall[18].pCurrent)
24045 
24046   { "rmdir",        (sqlite3_syscall_ptr)rmdir,           0 },
24047 #define osRmdir     ((int(*)(const char*))aSyscall[19].pCurrent)
24048 
24049   { "fchown",       (sqlite3_syscall_ptr)posixFchown,     0 },
24050 #define osFchown    ((int(*)(int,uid_t,gid_t))aSyscall[20].pCurrent)
24051 
24052 #if !defined(SQLITE_OMIT_WAL) || SQLITE_MAX_MMAP_SIZE>0
24053   { "mmap",       (sqlite3_syscall_ptr)mmap,     0 },
24054 #define osMmap ((void*(*)(void*,size_t,int,int,int,off_t))aSyscall[21].pCurrent)
24055 
24056   { "munmap",       (sqlite3_syscall_ptr)munmap,          0 },
24057 #define osMunmap ((void*(*)(void*,size_t))aSyscall[22].pCurrent)
24058 
24059 #if HAVE_MREMAP
24060   { "mremap",       (sqlite3_syscall_ptr)mremap,          0 },
24061 #else
24062   { "mremap",       (sqlite3_syscall_ptr)0,               0 },
24063 #endif
24064 #define osMremap ((void*(*)(void*,size_t,size_t,int,...))aSyscall[23].pCurrent)
24065 #endif
24066 
24067 }; /* End of the overrideable system calls */
24068 
24069 /*
24070 ** This is the xSetSystemCall() method of sqlite3_vfs for all of the
24071 ** "unix" VFSes.  Return SQLITE_OK opon successfully updating the
24072 ** system call pointer, or SQLITE_NOTFOUND if there is no configurable
24073 ** system call named zName.
24074 */
24075 static int unixSetSystemCall(
24076   sqlite3_vfs *pNotUsed,        /* The VFS pointer.  Not used */
24077   const char *zName,            /* Name of system call to override */
24078   sqlite3_syscall_ptr pNewFunc  /* Pointer to new system call value */
24079 ){
24080   unsigned int i;
24081   int rc = SQLITE_NOTFOUND;
24082 
24083   UNUSED_PARAMETER(pNotUsed);
24084   if( zName==0 ){
24085     /* If no zName is given, restore all system calls to their default
24086     ** settings and return NULL
24087     */
24088     rc = SQLITE_OK;
24089     for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
24090       if( aSyscall[i].pDefault ){
24091         aSyscall[i].pCurrent = aSyscall[i].pDefault;
24092       }
24093     }
24094   }else{
24095     /* If zName is specified, operate on only the one system call
24096     ** specified.
24097     */
24098     for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
24099       if( strcmp(zName, aSyscall[i].zName)==0 ){
24100         if( aSyscall[i].pDefault==0 ){
24101           aSyscall[i].pDefault = aSyscall[i].pCurrent;
24102         }
24103         rc = SQLITE_OK;
24104         if( pNewFunc==0 ) pNewFunc = aSyscall[i].pDefault;
24105         aSyscall[i].pCurrent = pNewFunc;
24106         break;
24107       }
24108     }
24109   }
24110   return rc;
24111 }
24112 
24113 /*
24114 ** Return the value of a system call.  Return NULL if zName is not a
24115 ** recognized system call name.  NULL is also returned if the system call
24116 ** is currently undefined.
24117 */
24118 static sqlite3_syscall_ptr unixGetSystemCall(
24119   sqlite3_vfs *pNotUsed,
24120   const char *zName
24121 ){
24122   unsigned int i;
24123 
24124   UNUSED_PARAMETER(pNotUsed);
24125   for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
24126     if( strcmp(zName, aSyscall[i].zName)==0 ) return aSyscall[i].pCurrent;
24127   }
24128   return 0;
24129 }
24130 
24131 /*
24132 ** Return the name of the first system call after zName.  If zName==NULL
24133 ** then return the name of the first system call.  Return NULL if zName
24134 ** is the last system call or if zName is not the name of a valid
24135 ** system call.
24136 */
24137 static const char *unixNextSystemCall(sqlite3_vfs *p, const char *zName){
24138   int i = -1;
24139 
24140   UNUSED_PARAMETER(p);
24141   if( zName ){
24142     for(i=0; i<ArraySize(aSyscall)-1; i++){
24143       if( strcmp(zName, aSyscall[i].zName)==0 ) break;
24144     }
24145   }
24146   for(i++; i<ArraySize(aSyscall); i++){
24147     if( aSyscall[i].pCurrent!=0 ) return aSyscall[i].zName;
24148   }
24149   return 0;
24150 }
24151 
24152 /*
24153 ** Do not accept any file descriptor less than this value, in order to avoid
24154 ** opening database file using file descriptors that are commonly used for
24155 ** standard input, output, and error.
24156 */
24157 #ifndef SQLITE_MINIMUM_FILE_DESCRIPTOR
24158 # define SQLITE_MINIMUM_FILE_DESCRIPTOR 3
24159 #endif
24160 
24161 /*
24162 ** Invoke open().  Do so multiple times, until it either succeeds or
24163 ** fails for some reason other than EINTR.
24164 **
24165 ** If the file creation mode "m" is 0 then set it to the default for
24166 ** SQLite.  The default is SQLITE_DEFAULT_FILE_PERMISSIONS (normally
24167 ** 0644) as modified by the system umask.  If m is not 0, then
24168 ** make the file creation mode be exactly m ignoring the umask.
24169 **
24170 ** The m parameter will be non-zero only when creating -wal, -journal,
24171 ** and -shm files.  We want those files to have *exactly* the same
24172 ** permissions as their original database, unadulterated by the umask.
24173 ** In that way, if a database file is -rw-rw-rw or -rw-rw-r-, and a
24174 ** transaction crashes and leaves behind hot journals, then any
24175 ** process that is able to write to the database will also be able to
24176 ** recover the hot journals.
24177 */
24178 static int robust_open(const char *z, int f, mode_t m){
24179   int fd;
24180   mode_t m2 = m ? m : SQLITE_DEFAULT_FILE_PERMISSIONS;
24181   while(1){
24182 #if defined(O_CLOEXEC)
24183     fd = osOpen(z,f|O_CLOEXEC,m2);
24184 #else
24185     fd = osOpen(z,f,m2);
24186 #endif
24187     if( fd<0 ){
24188       if( errno==EINTR ) continue;
24189       break;
24190     }
24191     if( fd>=SQLITE_MINIMUM_FILE_DESCRIPTOR ) break;
24192     osClose(fd);
24193     sqlite3_log(SQLITE_WARNING,
24194                 "attempt to open \"%s\" as file descriptor %d", z, fd);
24195     fd = -1;
24196     if( osOpen("/dev/null", f, m)<0 ) break;
24197   }
24198   if( fd>=0 ){
24199     if( m!=0 ){
24200       struct stat statbuf;
24201       if( osFstat(fd, &statbuf)==0
24202        && statbuf.st_size==0
24203        && (statbuf.st_mode&0777)!=m
24204       ){
24205         osFchmod(fd, m);
24206       }
24207     }
24208 #if defined(FD_CLOEXEC) && (!defined(O_CLOEXEC) || O_CLOEXEC==0)
24209     osFcntl(fd, F_SETFD, osFcntl(fd, F_GETFD, 0) | FD_CLOEXEC);
24210 #endif
24211   }
24212   return fd;
24213 }
24214 
24215 /*
24216 ** Helper functions to obtain and relinquish the global mutex. The
24217 ** global mutex is used to protect the unixInodeInfo and
24218 ** vxworksFileId objects used by this file, all of which may be
24219 ** shared by multiple threads.
24220 **
24221 ** Function unixMutexHeld() is used to assert() that the global mutex
24222 ** is held when required. This function is only used as part of assert()
24223 ** statements. e.g.
24224 **
24225 **   unixEnterMutex()
24226 **     assert( unixMutexHeld() );
24227 **   unixEnterLeave()
24228 */
24229 static void unixEnterMutex(void){
24230   sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
24231 }
24232 static void unixLeaveMutex(void){
24233   sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
24234 }
24235 #ifdef SQLITE_DEBUG
24236 static int unixMutexHeld(void) {
24237   return sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
24238 }
24239 #endif
24240 
24241 
24242 #if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
24243 /*
24244 ** Helper function for printing out trace information from debugging
24245 ** binaries. This returns the string represetation of the supplied
24246 ** integer lock-type.
24247 */
24248 static const char *azFileLock(int eFileLock){
24249   switch( eFileLock ){
24250     case NO_LOCK: return "NONE";
24251     case SHARED_LOCK: return "SHARED";
24252     case RESERVED_LOCK: return "RESERVED";
24253     case PENDING_LOCK: return "PENDING";
24254     case EXCLUSIVE_LOCK: return "EXCLUSIVE";
24255   }
24256   return "ERROR";
24257 }
24258 #endif
24259 
24260 #ifdef SQLITE_LOCK_TRACE
24261 /*
24262 ** Print out information about all locking operations.
24263 **
24264 ** This routine is used for troubleshooting locks on multithreaded
24265 ** platforms.  Enable by compiling with the -DSQLITE_LOCK_TRACE
24266 ** command-line option on the compiler.  This code is normally
24267 ** turned off.
24268 */
24269 static int lockTrace(int fd, int op, struct flock *p){
24270   char *zOpName, *zType;
24271   int s;
24272   int savedErrno;
24273   if( op==F_GETLK ){
24274     zOpName = "GETLK";
24275   }else if( op==F_SETLK ){
24276     zOpName = "SETLK";
24277   }else{
24278     s = osFcntl(fd, op, p);
24279     sqlite3DebugPrintf("fcntl unknown %d %d %d\n", fd, op, s);
24280     return s;
24281   }
24282   if( p->l_type==F_RDLCK ){
24283     zType = "RDLCK";
24284   }else if( p->l_type==F_WRLCK ){
24285     zType = "WRLCK";
24286   }else if( p->l_type==F_UNLCK ){
24287     zType = "UNLCK";
24288   }else{
24289     assert( 0 );
24290   }
24291   assert( p->l_whence==SEEK_SET );
24292   s = osFcntl(fd, op, p);
24293   savedErrno = errno;
24294   sqlite3DebugPrintf("fcntl %d %d %s %s %d %d %d %d\n",
24295      threadid, fd, zOpName, zType, (int)p->l_start, (int)p->l_len,
24296      (int)p->l_pid, s);
24297   if( s==(-1) && op==F_SETLK && (p->l_type==F_RDLCK || p->l_type==F_WRLCK) ){
24298     struct flock l2;
24299     l2 = *p;
24300     osFcntl(fd, F_GETLK, &l2);
24301     if( l2.l_type==F_RDLCK ){
24302       zType = "RDLCK";
24303     }else if( l2.l_type==F_WRLCK ){
24304       zType = "WRLCK";
24305     }else if( l2.l_type==F_UNLCK ){
24306       zType = "UNLCK";
24307     }else{
24308       assert( 0 );
24309     }
24310     sqlite3DebugPrintf("fcntl-failure-reason: %s %d %d %d\n",
24311        zType, (int)l2.l_start, (int)l2.l_len, (int)l2.l_pid);
24312   }
24313   errno = savedErrno;
24314   return s;
24315 }
24316 #undef osFcntl
24317 #define osFcntl lockTrace
24318 #endif /* SQLITE_LOCK_TRACE */
24319 
24320 /*
24321 ** Retry ftruncate() calls that fail due to EINTR
24322 */
24323 static int robust_ftruncate(int h, sqlite3_int64 sz){
24324   int rc;
24325   do{ rc = osFtruncate(h,sz); }while( rc<0 && errno==EINTR );
24326   return rc;
24327 }
24328 
24329 /*
24330 ** This routine translates a standard POSIX errno code into something
24331 ** useful to the clients of the sqlite3 functions.  Specifically, it is
24332 ** intended to translate a variety of "try again" errors into SQLITE_BUSY
24333 ** and a variety of "please close the file descriptor NOW" errors into
24334 ** SQLITE_IOERR
24335 **
24336 ** Errors during initialization of locks, or file system support for locks,
24337 ** should handle ENOLCK, ENOTSUP, EOPNOTSUPP separately.
24338 */
24339 static int sqliteErrorFromPosixError(int posixError, int sqliteIOErr) {
24340   switch (posixError) {
24341 #if 0
24342   /* At one point this code was not commented out. In theory, this branch
24343   ** should never be hit, as this function should only be called after
24344   ** a locking-related function (i.e. fcntl()) has returned non-zero with
24345   ** the value of errno as the first argument. Since a system call has failed,
24346   ** errno should be non-zero.
24347   **
24348   ** Despite this, if errno really is zero, we still don't want to return
24349   ** SQLITE_OK. The system call failed, and *some* SQLite error should be
24350   ** propagated back to the caller. Commenting this branch out means errno==0
24351   ** will be handled by the "default:" case below.
24352   */
24353   case 0:
24354     return SQLITE_OK;
24355 #endif
24356 
24357   case EAGAIN:
24358   case ETIMEDOUT:
24359   case EBUSY:
24360   case EINTR:
24361   case ENOLCK:
24362     /* random NFS retry error, unless during file system support
24363      * introspection, in which it actually means what it says */
24364     return SQLITE_BUSY;
24365 
24366   case EACCES:
24367     /* EACCES is like EAGAIN during locking operations, but not any other time*/
24368     if( (sqliteIOErr == SQLITE_IOERR_LOCK) ||
24369         (sqliteIOErr == SQLITE_IOERR_UNLOCK) ||
24370         (sqliteIOErr == SQLITE_IOERR_RDLOCK) ||
24371         (sqliteIOErr == SQLITE_IOERR_CHECKRESERVEDLOCK) ){
24372       return SQLITE_BUSY;
24373     }
24374     /* else fall through */
24375   case EPERM:
24376     return SQLITE_PERM;
24377 
24378   /* EDEADLK is only possible if a call to fcntl(F_SETLKW) is made. And
24379   ** this module never makes such a call. And the code in SQLite itself
24380   ** asserts that SQLITE_IOERR_BLOCKED is never returned. For these reasons
24381   ** this case is also commented out. If the system does set errno to EDEADLK,
24382   ** the default SQLITE_IOERR_XXX code will be returned. */
24383 #if 0
24384   case EDEADLK:
24385     return SQLITE_IOERR_BLOCKED;
24386 #endif
24387 
24388 #if EOPNOTSUPP!=ENOTSUP
24389   case EOPNOTSUPP:
24390     /* something went terribly awry, unless during file system support
24391      * introspection, in which it actually means what it says */
24392 #endif
24393 #ifdef ENOTSUP
24394   case ENOTSUP:
24395     /* invalid fd, unless during file system support introspection, in which
24396      * it actually means what it says */
24397 #endif
24398   case EIO:
24399   case EBADF:
24400   case EINVAL:
24401   case ENOTCONN:
24402   case ENODEV:
24403   case ENXIO:
24404   case ENOENT:
24405 #ifdef ESTALE                     /* ESTALE is not defined on Interix systems */
24406   case ESTALE:
24407 #endif
24408   case ENOSYS:
24409     /* these should force the client to close the file and reconnect */
24410 
24411   default:
24412     return sqliteIOErr;
24413   }
24414 }
24415 
24416 
24417 /******************************************************************************
24418 ****************** Begin Unique File ID Utility Used By VxWorks ***************
24419 **
24420 ** On most versions of unix, we can get a unique ID for a file by concatenating
24421 ** the device number and the inode number.  But this does not work on VxWorks.
24422 ** On VxWorks, a unique file id must be based on the canonical filename.
24423 **
24424 ** A pointer to an instance of the following structure can be used as a
24425 ** unique file ID in VxWorks.  Each instance of this structure contains
24426 ** a copy of the canonical filename.  There is also a reference count.
24427 ** The structure is reclaimed when the number of pointers to it drops to
24428 ** zero.
24429 **
24430 ** There are never very many files open at one time and lookups are not
24431 ** a performance-critical path, so it is sufficient to put these
24432 ** structures on a linked list.
24433 */
24434 struct vxworksFileId {
24435   struct vxworksFileId *pNext;  /* Next in a list of them all */
24436   int nRef;                     /* Number of references to this one */
24437   int nName;                    /* Length of the zCanonicalName[] string */
24438   char *zCanonicalName;         /* Canonical filename */
24439 };
24440 
24441 #if OS_VXWORKS
24442 /*
24443 ** All unique filenames are held on a linked list headed by this
24444 ** variable:
24445 */
24446 static struct vxworksFileId *vxworksFileList = 0;
24447 
24448 /*
24449 ** Simplify a filename into its canonical form
24450 ** by making the following changes:
24451 **
24452 **  * removing any trailing and duplicate /
24453 **  * convert /./ into just /
24454 **  * convert /A/../ where A is any simple name into just /
24455 **
24456 ** Changes are made in-place.  Return the new name length.
24457 **
24458 ** The original filename is in z[0..n-1].  Return the number of
24459 ** characters in the simplified name.
24460 */
24461 static int vxworksSimplifyName(char *z, int n){
24462   int i, j;
24463   while( n>1 && z[n-1]=='/' ){ n--; }
24464   for(i=j=0; i<n; i++){
24465     if( z[i]=='/' ){
24466       if( z[i+1]=='/' ) continue;
24467       if( z[i+1]=='.' && i+2<n && z[i+2]=='/' ){
24468         i += 1;
24469         continue;
24470       }
24471       if( z[i+1]=='.' && i+3<n && z[i+2]=='.' && z[i+3]=='/' ){
24472         while( j>0 && z[j-1]!='/' ){ j--; }
24473         if( j>0 ){ j--; }
24474         i += 2;
24475         continue;
24476       }
24477     }
24478     z[j++] = z[i];
24479   }
24480   z[j] = 0;
24481   return j;
24482 }
24483 
24484 /*
24485 ** Find a unique file ID for the given absolute pathname.  Return
24486 ** a pointer to the vxworksFileId object.  This pointer is the unique
24487 ** file ID.
24488 **
24489 ** The nRef field of the vxworksFileId object is incremented before
24490 ** the object is returned.  A new vxworksFileId object is created
24491 ** and added to the global list if necessary.
24492 **
24493 ** If a memory allocation error occurs, return NULL.
24494 */
24495 static struct vxworksFileId *vxworksFindFileId(const char *zAbsoluteName){
24496   struct vxworksFileId *pNew;         /* search key and new file ID */
24497   struct vxworksFileId *pCandidate;   /* For looping over existing file IDs */
24498   int n;                              /* Length of zAbsoluteName string */
24499 
24500   assert( zAbsoluteName[0]=='/' );
24501   n = (int)strlen(zAbsoluteName);
24502   pNew = sqlite3_malloc( sizeof(*pNew) + (n+1) );
24503   if( pNew==0 ) return 0;
24504   pNew->zCanonicalName = (char*)&pNew[1];
24505   memcpy(pNew->zCanonicalName, zAbsoluteName, n+1);
24506   n = vxworksSimplifyName(pNew->zCanonicalName, n);
24507 
24508   /* Search for an existing entry that matching the canonical name.
24509   ** If found, increment the reference count and return a pointer to
24510   ** the existing file ID.
24511   */
24512   unixEnterMutex();
24513   for(pCandidate=vxworksFileList; pCandidate; pCandidate=pCandidate->pNext){
24514     if( pCandidate->nName==n
24515      && memcmp(pCandidate->zCanonicalName, pNew->zCanonicalName, n)==0
24516     ){
24517        sqlite3_free(pNew);
24518        pCandidate->nRef++;
24519        unixLeaveMutex();
24520        return pCandidate;
24521     }
24522   }
24523 
24524   /* No match was found.  We will make a new file ID */
24525   pNew->nRef = 1;
24526   pNew->nName = n;
24527   pNew->pNext = vxworksFileList;
24528   vxworksFileList = pNew;
24529   unixLeaveMutex();
24530   return pNew;
24531 }
24532 
24533 /*
24534 ** Decrement the reference count on a vxworksFileId object.  Free
24535 ** the object when the reference count reaches zero.
24536 */
24537 static void vxworksReleaseFileId(struct vxworksFileId *pId){
24538   unixEnterMutex();
24539   assert( pId->nRef>0 );
24540   pId->nRef--;
24541   if( pId->nRef==0 ){
24542     struct vxworksFileId **pp;
24543     for(pp=&vxworksFileList; *pp && *pp!=pId; pp = &((*pp)->pNext)){}
24544     assert( *pp==pId );
24545     *pp = pId->pNext;
24546     sqlite3_free(pId);
24547   }
24548   unixLeaveMutex();
24549 }
24550 #endif /* OS_VXWORKS */
24551 /*************** End of Unique File ID Utility Used By VxWorks ****************
24552 ******************************************************************************/
24553 
24554 
24555 /******************************************************************************
24556 *************************** Posix Advisory Locking ****************************
24557 **
24558 ** POSIX advisory locks are broken by design.  ANSI STD 1003.1 (1996)
24559 ** section 6.5.2.2 lines 483 through 490 specify that when a process
24560 ** sets or clears a lock, that operation overrides any prior locks set
24561 ** by the same process.  It does not explicitly say so, but this implies
24562 ** that it overrides locks set by the same process using a different
24563 ** file descriptor.  Consider this test case:
24564 **
24565 **       int fd1 = open("./file1", O_RDWR|O_CREAT, 0644);
24566 **       int fd2 = open("./file2", O_RDWR|O_CREAT, 0644);
24567 **
24568 ** Suppose ./file1 and ./file2 are really the same file (because
24569 ** one is a hard or symbolic link to the other) then if you set
24570 ** an exclusive lock on fd1, then try to get an exclusive lock
24571 ** on fd2, it works.  I would have expected the second lock to
24572 ** fail since there was already a lock on the file due to fd1.
24573 ** But not so.  Since both locks came from the same process, the
24574 ** second overrides the first, even though they were on different
24575 ** file descriptors opened on different file names.
24576 **
24577 ** This means that we cannot use POSIX locks to synchronize file access
24578 ** among competing threads of the same process.  POSIX locks will work fine
24579 ** to synchronize access for threads in separate processes, but not
24580 ** threads within the same process.
24581 **
24582 ** To work around the problem, SQLite has to manage file locks internally
24583 ** on its own.  Whenever a new database is opened, we have to find the
24584 ** specific inode of the database file (the inode is determined by the
24585 ** st_dev and st_ino fields of the stat structure that fstat() fills in)
24586 ** and check for locks already existing on that inode.  When locks are
24587 ** created or removed, we have to look at our own internal record of the
24588 ** locks to see if another thread has previously set a lock on that same
24589 ** inode.
24590 **
24591 ** (Aside: The use of inode numbers as unique IDs does not work on VxWorks.
24592 ** For VxWorks, we have to use the alternative unique ID system based on
24593 ** canonical filename and implemented in the previous division.)
24594 **
24595 ** The sqlite3_file structure for POSIX is no longer just an integer file
24596 ** descriptor.  It is now a structure that holds the integer file
24597 ** descriptor and a pointer to a structure that describes the internal
24598 ** locks on the corresponding inode.  There is one locking structure
24599 ** per inode, so if the same inode is opened twice, both unixFile structures
24600 ** point to the same locking structure.  The locking structure keeps
24601 ** a reference count (so we will know when to delete it) and a "cnt"
24602 ** field that tells us its internal lock status.  cnt==0 means the
24603 ** file is unlocked.  cnt==-1 means the file has an exclusive lock.
24604 ** cnt>0 means there are cnt shared locks on the file.
24605 **
24606 ** Any attempt to lock or unlock a file first checks the locking
24607 ** structure.  The fcntl() system call is only invoked to set a
24608 ** POSIX lock if the internal lock structure transitions between
24609 ** a locked and an unlocked state.
24610 **
24611 ** But wait:  there are yet more problems with POSIX advisory locks.
24612 **
24613 ** If you close a file descriptor that points to a file that has locks,
24614 ** all locks on that file that are owned by the current process are
24615 ** released.  To work around this problem, each unixInodeInfo object
24616 ** maintains a count of the number of pending locks on tha inode.
24617 ** When an attempt is made to close an unixFile, if there are
24618 ** other unixFile open on the same inode that are holding locks, the call
24619 ** to close() the file descriptor is deferred until all of the locks clear.
24620 ** The unixInodeInfo structure keeps a list of file descriptors that need to
24621 ** be closed and that list is walked (and cleared) when the last lock
24622 ** clears.
24623 **
24624 ** Yet another problem:  LinuxThreads do not play well with posix locks.
24625 **
24626 ** Many older versions of linux use the LinuxThreads library which is
24627 ** not posix compliant.  Under LinuxThreads, a lock created by thread
24628 ** A cannot be modified or overridden by a different thread B.
24629 ** Only thread A can modify the lock.  Locking behavior is correct
24630 ** if the appliation uses the newer Native Posix Thread Library (NPTL)
24631 ** on linux - with NPTL a lock created by thread A can override locks
24632 ** in thread B.  But there is no way to know at compile-time which
24633 ** threading library is being used.  So there is no way to know at
24634 ** compile-time whether or not thread A can override locks on thread B.
24635 ** One has to do a run-time check to discover the behavior of the
24636 ** current process.
24637 **
24638 ** SQLite used to support LinuxThreads.  But support for LinuxThreads
24639 ** was dropped beginning with version 3.7.0.  SQLite will still work with
24640 ** LinuxThreads provided that (1) there is no more than one connection
24641 ** per database file in the same process and (2) database connections
24642 ** do not move across threads.
24643 */
24644 
24645 /*
24646 ** An instance of the following structure serves as the key used
24647 ** to locate a particular unixInodeInfo object.
24648 */
24649 struct unixFileId {
24650   dev_t dev;                  /* Device number */
24651 #if OS_VXWORKS
24652   struct vxworksFileId *pId;  /* Unique file ID for vxworks. */
24653 #else
24654   ino_t ino;                  /* Inode number */
24655 #endif
24656 };
24657 
24658 /*
24659 ** An instance of the following structure is allocated for each open
24660 ** inode.  Or, on LinuxThreads, there is one of these structures for
24661 ** each inode opened by each thread.
24662 **
24663 ** A single inode can have multiple file descriptors, so each unixFile
24664 ** structure contains a pointer to an instance of this object and this
24665 ** object keeps a count of the number of unixFile pointing to it.
24666 */
24667 struct unixInodeInfo {
24668   struct unixFileId fileId;       /* The lookup key */
24669   int nShared;                    /* Number of SHARED locks held */
24670   unsigned char eFileLock;        /* One of SHARED_LOCK, RESERVED_LOCK etc. */
24671   unsigned char bProcessLock;     /* An exclusive process lock is held */
24672   int nRef;                       /* Number of pointers to this structure */
24673   unixShmNode *pShmNode;          /* Shared memory associated with this inode */
24674   int nLock;                      /* Number of outstanding file locks */
24675   UnixUnusedFd *pUnused;          /* Unused file descriptors to close */
24676   unixInodeInfo *pNext;           /* List of all unixInodeInfo objects */
24677   unixInodeInfo *pPrev;           /*    .... doubly linked */
24678 #if SQLITE_ENABLE_LOCKING_STYLE
24679   unsigned long long sharedByte;  /* for AFP simulated shared lock */
24680 #endif
24681 #if OS_VXWORKS
24682   sem_t *pSem;                    /* Named POSIX semaphore */
24683   char aSemName[MAX_PATHNAME+2];  /* Name of that semaphore */
24684 #endif
24685 };
24686 
24687 /*
24688 ** A lists of all unixInodeInfo objects.
24689 */
24690 static unixInodeInfo *inodeList = 0;
24691 
24692 /*
24693 **
24694 ** This function - unixLogError_x(), is only ever called via the macro
24695 ** unixLogError().
24696 **
24697 ** It is invoked after an error occurs in an OS function and errno has been
24698 ** set. It logs a message using sqlite3_log() containing the current value of
24699 ** errno and, if possible, the human-readable equivalent from strerror() or
24700 ** strerror_r().
24701 **
24702 ** The first argument passed to the macro should be the error code that
24703 ** will be returned to SQLite (e.g. SQLITE_IOERR_DELETE, SQLITE_CANTOPEN).
24704 ** The two subsequent arguments should be the name of the OS function that
24705 ** failed (e.g. "unlink", "open") and the associated file-system path,
24706 ** if any.
24707 */
24708 #define unixLogError(a,b,c)     unixLogErrorAtLine(a,b,c,__LINE__)
24709 static int unixLogErrorAtLine(
24710   int errcode,                    /* SQLite error code */
24711   const char *zFunc,              /* Name of OS function that failed */
24712   const char *zPath,              /* File path associated with error */
24713   int iLine                       /* Source line number where error occurred */
24714 ){
24715   char *zErr;                     /* Message from strerror() or equivalent */
24716   int iErrno = errno;             /* Saved syscall error number */
24717 
24718   /* If this is not a threadsafe build (SQLITE_THREADSAFE==0), then use
24719   ** the strerror() function to obtain the human-readable error message
24720   ** equivalent to errno. Otherwise, use strerror_r().
24721   */
24722 #if SQLITE_THREADSAFE && defined(HAVE_STRERROR_R)
24723   char aErr[80];
24724   memset(aErr, 0, sizeof(aErr));
24725   zErr = aErr;
24726 
24727   /* If STRERROR_R_CHAR_P (set by autoconf scripts) or __USE_GNU is defined,
24728   ** assume that the system provides the GNU version of strerror_r() that
24729   ** returns a pointer to a buffer containing the error message. That pointer
24730   ** may point to aErr[], or it may point to some static storage somewhere.
24731   ** Otherwise, assume that the system provides the POSIX version of
24732   ** strerror_r(), which always writes an error message into aErr[].
24733   **
24734   ** If the code incorrectly assumes that it is the POSIX version that is
24735   ** available, the error message will often be an empty string. Not a
24736   ** huge problem. Incorrectly concluding that the GNU version is available
24737   ** could lead to a segfault though.
24738   */
24739 #if defined(STRERROR_R_CHAR_P) || defined(__USE_GNU)
24740   zErr =
24741 # endif
24742   strerror_r(iErrno, aErr, sizeof(aErr)-1);
24743 
24744 #elif SQLITE_THREADSAFE
24745   /* This is a threadsafe build, but strerror_r() is not available. */
24746   zErr = "";
24747 #else
24748   /* Non-threadsafe build, use strerror(). */
24749   zErr = strerror(iErrno);
24750 #endif
24751 
24752   if( zPath==0 ) zPath = "";
24753   sqlite3_log(errcode,
24754       "os_unix.c:%d: (%d) %s(%s) - %s",
24755       iLine, iErrno, zFunc, zPath, zErr
24756   );
24757 
24758   return errcode;
24759 }
24760 
24761 /*
24762 ** Close a file descriptor.
24763 **
24764 ** We assume that close() almost always works, since it is only in a
24765 ** very sick application or on a very sick platform that it might fail.
24766 ** If it does fail, simply leak the file descriptor, but do log the
24767 ** error.
24768 **
24769 ** Note that it is not safe to retry close() after EINTR since the
24770 ** file descriptor might have already been reused by another thread.
24771 ** So we don't even try to recover from an EINTR.  Just log the error
24772 ** and move on.
24773 */
24774 static void robust_close(unixFile *pFile, int h, int lineno){
24775   if( osClose(h) ){
24776     unixLogErrorAtLine(SQLITE_IOERR_CLOSE, "close",
24777                        pFile ? pFile->zPath : 0, lineno);
24778   }
24779 }
24780 
24781 /*
24782 ** Close all file descriptors accumuated in the unixInodeInfo->pUnused list.
24783 */
24784 static void closePendingFds(unixFile *pFile){
24785   unixInodeInfo *pInode = pFile->pInode;
24786   UnixUnusedFd *p;
24787   UnixUnusedFd *pNext;
24788   for(p=pInode->pUnused; p; p=pNext){
24789     pNext = p->pNext;
24790     robust_close(pFile, p->fd, __LINE__);
24791     sqlite3_free(p);
24792   }
24793   pInode->pUnused = 0;
24794 }
24795 
24796 /*
24797 ** Release a unixInodeInfo structure previously allocated by findInodeInfo().
24798 **
24799 ** The mutex entered using the unixEnterMutex() function must be held
24800 ** when this function is called.
24801 */
24802 static void releaseInodeInfo(unixFile *pFile){
24803   unixInodeInfo *pInode = pFile->pInode;
24804   assert( unixMutexHeld() );
24805   if( ALWAYS(pInode) ){
24806     pInode->nRef--;
24807     if( pInode->nRef==0 ){
24808       assert( pInode->pShmNode==0 );
24809       closePendingFds(pFile);
24810       if( pInode->pPrev ){
24811         assert( pInode->pPrev->pNext==pInode );
24812         pInode->pPrev->pNext = pInode->pNext;
24813       }else{
24814         assert( inodeList==pInode );
24815         inodeList = pInode->pNext;
24816       }
24817       if( pInode->pNext ){
24818         assert( pInode->pNext->pPrev==pInode );
24819         pInode->pNext->pPrev = pInode->pPrev;
24820       }
24821       sqlite3_free(pInode);
24822     }
24823   }
24824 }
24825 
24826 /*
24827 ** Given a file descriptor, locate the unixInodeInfo object that
24828 ** describes that file descriptor.  Create a new one if necessary.  The
24829 ** return value might be uninitialized if an error occurs.
24830 **
24831 ** The mutex entered using the unixEnterMutex() function must be held
24832 ** when this function is called.
24833 **
24834 ** Return an appropriate error code.
24835 */
24836 static int findInodeInfo(
24837   unixFile *pFile,               /* Unix file with file desc used in the key */
24838   unixInodeInfo **ppInode        /* Return the unixInodeInfo object here */
24839 ){
24840   int rc;                        /* System call return code */
24841   int fd;                        /* The file descriptor for pFile */
24842   struct unixFileId fileId;      /* Lookup key for the unixInodeInfo */
24843   struct stat statbuf;           /* Low-level file information */
24844   unixInodeInfo *pInode = 0;     /* Candidate unixInodeInfo object */
24845 
24846   assert( unixMutexHeld() );
24847 
24848   /* Get low-level information about the file that we can used to
24849   ** create a unique name for the file.
24850   */
24851   fd = pFile->h;
24852   rc = osFstat(fd, &statbuf);
24853   if( rc!=0 ){
24854     pFile->lastErrno = errno;
24855 #ifdef EOVERFLOW
24856     if( pFile->lastErrno==EOVERFLOW ) return SQLITE_NOLFS;
24857 #endif
24858     return SQLITE_IOERR;
24859   }
24860 
24861 #ifdef __APPLE__
24862   /* On OS X on an msdos filesystem, the inode number is reported
24863   ** incorrectly for zero-size files.  See ticket #3260.  To work
24864   ** around this problem (we consider it a bug in OS X, not SQLite)
24865   ** we always increase the file size to 1 by writing a single byte
24866   ** prior to accessing the inode number.  The one byte written is
24867   ** an ASCII 'S' character which also happens to be the first byte
24868   ** in the header of every SQLite database.  In this way, if there
24869   ** is a race condition such that another thread has already populated
24870   ** the first page of the database, no damage is done.
24871   */
24872   if( statbuf.st_size==0 && (pFile->fsFlags & SQLITE_FSFLAGS_IS_MSDOS)!=0 ){
24873     do{ rc = osWrite(fd, "S", 1); }while( rc<0 && errno==EINTR );
24874     if( rc!=1 ){
24875       pFile->lastErrno = errno;
24876       return SQLITE_IOERR;
24877     }
24878     rc = osFstat(fd, &statbuf);
24879     if( rc!=0 ){
24880       pFile->lastErrno = errno;
24881       return SQLITE_IOERR;
24882     }
24883   }
24884 #endif
24885 
24886   memset(&fileId, 0, sizeof(fileId));
24887   fileId.dev = statbuf.st_dev;
24888 #if OS_VXWORKS
24889   fileId.pId = pFile->pId;
24890 #else
24891   fileId.ino = statbuf.st_ino;
24892 #endif
24893   pInode = inodeList;
24894   while( pInode && memcmp(&fileId, &pInode->fileId, sizeof(fileId)) ){
24895     pInode = pInode->pNext;
24896   }
24897   if( pInode==0 ){
24898     pInode = sqlite3_malloc( sizeof(*pInode) );
24899     if( pInode==0 ){
24900       return SQLITE_NOMEM;
24901     }
24902     memset(pInode, 0, sizeof(*pInode));
24903     memcpy(&pInode->fileId, &fileId, sizeof(fileId));
24904     pInode->nRef = 1;
24905     pInode->pNext = inodeList;
24906     pInode->pPrev = 0;
24907     if( inodeList ) inodeList->pPrev = pInode;
24908     inodeList = pInode;
24909   }else{
24910     pInode->nRef++;
24911   }
24912   *ppInode = pInode;
24913   return SQLITE_OK;
24914 }
24915 
24916 /*
24917 ** Return TRUE if pFile has been renamed or unlinked since it was first opened.
24918 */
24919 static int fileHasMoved(unixFile *pFile){
24920   struct stat buf;
24921   return pFile->pInode!=0 &&
24922          (osStat(pFile->zPath, &buf)!=0 || buf.st_ino!=pFile->pInode->fileId.ino);
24923 }
24924 
24925 
24926 /*
24927 ** Check a unixFile that is a database.  Verify the following:
24928 **
24929 ** (1) There is exactly one hard link on the file
24930 ** (2) The file is not a symbolic link
24931 ** (3) The file has not been renamed or unlinked
24932 **
24933 ** Issue sqlite3_log(SQLITE_WARNING,...) messages if anything is not right.
24934 */
24935 static void verifyDbFile(unixFile *pFile){
24936   struct stat buf;
24937   int rc;
24938   if( pFile->ctrlFlags & UNIXFILE_WARNED ){
24939     /* One or more of the following warnings have already been issued.  Do not
24940     ** repeat them so as not to clutter the error log */
24941     return;
24942   }
24943   rc = osFstat(pFile->h, &buf);
24944   if( rc!=0 ){
24945     sqlite3_log(SQLITE_WARNING, "cannot fstat db file %s", pFile->zPath);
24946     pFile->ctrlFlags |= UNIXFILE_WARNED;
24947     return;
24948   }
24949   if( buf.st_nlink==0 && (pFile->ctrlFlags & UNIXFILE_DELETE)==0 ){
24950     sqlite3_log(SQLITE_WARNING, "file unlinked while open: %s", pFile->zPath);
24951     pFile->ctrlFlags |= UNIXFILE_WARNED;
24952     return;
24953   }
24954   if( buf.st_nlink>1 ){
24955     sqlite3_log(SQLITE_WARNING, "multiple links to file: %s", pFile->zPath);
24956     pFile->ctrlFlags |= UNIXFILE_WARNED;
24957     return;
24958   }
24959   if( fileHasMoved(pFile) ){
24960     sqlite3_log(SQLITE_WARNING, "file renamed while open: %s", pFile->zPath);
24961     pFile->ctrlFlags |= UNIXFILE_WARNED;
24962     return;
24963   }
24964 }
24965 
24966 
24967 /*
24968 ** This routine checks if there is a RESERVED lock held on the specified
24969 ** file by this or any other process. If such a lock is held, set *pResOut
24970 ** to a non-zero value otherwise *pResOut is set to zero.  The return value
24971 ** is set to SQLITE_OK unless an I/O error occurs during lock checking.
24972 */
24973 static int unixCheckReservedLock(sqlite3_file *id, int *pResOut){
24974   int rc = SQLITE_OK;
24975   int reserved = 0;
24976   unixFile *pFile = (unixFile*)id;
24977 
24978   SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
24979 
24980   assert( pFile );
24981   unixEnterMutex(); /* Because pFile->pInode is shared across threads */
24982 
24983   /* Check if a thread in this process holds such a lock */
24984   if( pFile->pInode->eFileLock>SHARED_LOCK ){
24985     reserved = 1;
24986   }
24987 
24988   /* Otherwise see if some other process holds it.
24989   */
24990 #ifndef __DJGPP__
24991   if( !reserved && !pFile->pInode->bProcessLock ){
24992     struct flock lock;
24993     lock.l_whence = SEEK_SET;
24994     lock.l_start = RESERVED_BYTE;
24995     lock.l_len = 1;
24996     lock.l_type = F_WRLCK;
24997     if( osFcntl(pFile->h, F_GETLK, &lock) ){
24998       rc = SQLITE_IOERR_CHECKRESERVEDLOCK;
24999       pFile->lastErrno = errno;
25000     } else if( lock.l_type!=F_UNLCK ){
25001       reserved = 1;
25002     }
25003   }
25004 #endif
25005 
25006   unixLeaveMutex();
25007   OSTRACE(("TEST WR-LOCK %d %d %d (unix)\n", pFile->h, rc, reserved));
25008 
25009   *pResOut = reserved;
25010   return rc;
25011 }
25012 
25013 /*
25014 ** Attempt to set a system-lock on the file pFile.  The lock is
25015 ** described by pLock.
25016 **
25017 ** If the pFile was opened read/write from unix-excl, then the only lock
25018 ** ever obtained is an exclusive lock, and it is obtained exactly once
25019 ** the first time any lock is attempted.  All subsequent system locking
25020 ** operations become no-ops.  Locking operations still happen internally,
25021 ** in order to coordinate access between separate database connections
25022 ** within this process, but all of that is handled in memory and the
25023 ** operating system does not participate.
25024 **
25025 ** This function is a pass-through to fcntl(F_SETLK) if pFile is using
25026 ** any VFS other than "unix-excl" or if pFile is opened on "unix-excl"
25027 ** and is read-only.
25028 **
25029 ** Zero is returned if the call completes successfully, or -1 if a call
25030 ** to fcntl() fails. In this case, errno is set appropriately (by fcntl()).
25031 */
25032 static int unixFileLock(unixFile *pFile, struct flock *pLock){
25033   int rc;
25034   unixInodeInfo *pInode = pFile->pInode;
25035   assert( unixMutexHeld() );
25036   assert( pInode!=0 );
25037   if( ((pFile->ctrlFlags & UNIXFILE_EXCL)!=0 || pInode->bProcessLock)
25038    && ((pFile->ctrlFlags & UNIXFILE_RDONLY)==0)
25039   ){
25040     if( pInode->bProcessLock==0 ){
25041       struct flock lock;
25042       assert( pInode->nLock==0 );
25043       lock.l_whence = SEEK_SET;
25044       lock.l_start = SHARED_FIRST;
25045       lock.l_len = SHARED_SIZE;
25046       lock.l_type = F_WRLCK;
25047       rc = osFcntl(pFile->h, F_SETLK, &lock);
25048       if( rc<0 ) return rc;
25049       pInode->bProcessLock = 1;
25050       pInode->nLock++;
25051     }else{
25052       rc = 0;
25053     }
25054   }else{
25055     rc = osFcntl(pFile->h, F_SETLK, pLock);
25056   }
25057   return rc;
25058 }
25059 
25060 /*
25061 ** Lock the file with the lock specified by parameter eFileLock - one
25062 ** of the following:
25063 **
25064 **     (1) SHARED_LOCK
25065 **     (2) RESERVED_LOCK
25066 **     (3) PENDING_LOCK
25067 **     (4) EXCLUSIVE_LOCK
25068 **
25069 ** Sometimes when requesting one lock state, additional lock states
25070 ** are inserted in between.  The locking might fail on one of the later
25071 ** transitions leaving the lock state different from what it started but
25072 ** still short of its goal.  The following chart shows the allowed
25073 ** transitions and the inserted intermediate states:
25074 **
25075 **    UNLOCKED -> SHARED
25076 **    SHARED -> RESERVED
25077 **    SHARED -> (PENDING) -> EXCLUSIVE
25078 **    RESERVED -> (PENDING) -> EXCLUSIVE
25079 **    PENDING -> EXCLUSIVE
25080 **
25081 ** This routine will only increase a lock.  Use the sqlite3OsUnlock()
25082 ** routine to lower a locking level.
25083 */
25084 static int unixLock(sqlite3_file *id, int eFileLock){
25085   /* The following describes the implementation of the various locks and
25086   ** lock transitions in terms of the POSIX advisory shared and exclusive
25087   ** lock primitives (called read-locks and write-locks below, to avoid
25088   ** confusion with SQLite lock names). The algorithms are complicated
25089   ** slightly in order to be compatible with windows systems simultaneously
25090   ** accessing the same database file, in case that is ever required.
25091   **
25092   ** Symbols defined in os.h indentify the 'pending byte' and the 'reserved
25093   ** byte', each single bytes at well known offsets, and the 'shared byte
25094   ** range', a range of 510 bytes at a well known offset.
25095   **
25096   ** To obtain a SHARED lock, a read-lock is obtained on the 'pending
25097   ** byte'.  If this is successful, a random byte from the 'shared byte
25098   ** range' is read-locked and the lock on the 'pending byte' released.
25099   **
25100   ** A process may only obtain a RESERVED lock after it has a SHARED lock.
25101   ** A RESERVED lock is implemented by grabbing a write-lock on the
25102   ** 'reserved byte'.
25103   **
25104   ** A process may only obtain a PENDING lock after it has obtained a
25105   ** SHARED lock. A PENDING lock is implemented by obtaining a write-lock
25106   ** on the 'pending byte'. This ensures that no new SHARED locks can be
25107   ** obtained, but existing SHARED locks are allowed to persist. A process
25108   ** does not have to obtain a RESERVED lock on the way to a PENDING lock.
25109   ** This property is used by the algorithm for rolling back a journal file
25110   ** after a crash.
25111   **
25112   ** An EXCLUSIVE lock, obtained after a PENDING lock is held, is
25113   ** implemented by obtaining a write-lock on the entire 'shared byte
25114   ** range'. Since all other locks require a read-lock on one of the bytes
25115   ** within this range, this ensures that no other locks are held on the
25116   ** database.
25117   **
25118   ** The reason a single byte cannot be used instead of the 'shared byte
25119   ** range' is that some versions of windows do not support read-locks. By
25120   ** locking a random byte from a range, concurrent SHARED locks may exist
25121   ** even if the locking primitive used is always a write-lock.
25122   */
25123   int rc = SQLITE_OK;
25124   unixFile *pFile = (unixFile*)id;
25125   unixInodeInfo *pInode;
25126   struct flock lock;
25127   int tErrno = 0;
25128 
25129   assert( pFile );
25130   OSTRACE(("LOCK    %d %s was %s(%s,%d) pid=%d (unix)\n", pFile->h,
25131       azFileLock(eFileLock), azFileLock(pFile->eFileLock),
25132       azFileLock(pFile->pInode->eFileLock), pFile->pInode->nShared , getpid()));
25133 
25134   /* If there is already a lock of this type or more restrictive on the
25135   ** unixFile, do nothing. Don't use the end_lock: exit path, as
25136   ** unixEnterMutex() hasn't been called yet.
25137   */
25138   if( pFile->eFileLock>=eFileLock ){
25139     OSTRACE(("LOCK    %d %s ok (already held) (unix)\n", pFile->h,
25140             azFileLock(eFileLock)));
25141     return SQLITE_OK;
25142   }
25143 
25144   /* Make sure the locking sequence is correct.
25145   **  (1) We never move from unlocked to anything higher than shared lock.
25146   **  (2) SQLite never explicitly requests a pendig lock.
25147   **  (3) A shared lock is always held when a reserve lock is requested.
25148   */
25149   assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK );
25150   assert( eFileLock!=PENDING_LOCK );
25151   assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK );
25152 
25153   /* This mutex is needed because pFile->pInode is shared across threads
25154   */
25155   unixEnterMutex();
25156   pInode = pFile->pInode;
25157 
25158   /* If some thread using this PID has a lock via a different unixFile*
25159   ** handle that precludes the requested lock, return BUSY.
25160   */
25161   if( (pFile->eFileLock!=pInode->eFileLock &&
25162           (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK))
25163   ){
25164     rc = SQLITE_BUSY;
25165     goto end_lock;
25166   }
25167 
25168   /* If a SHARED lock is requested, and some thread using this PID already
25169   ** has a SHARED or RESERVED lock, then increment reference counts and
25170   ** return SQLITE_OK.
25171   */
25172   if( eFileLock==SHARED_LOCK &&
25173       (pInode->eFileLock==SHARED_LOCK || pInode->eFileLock==RESERVED_LOCK) ){
25174     assert( eFileLock==SHARED_LOCK );
25175     assert( pFile->eFileLock==0 );
25176     assert( pInode->nShared>0 );
25177     pFile->eFileLock = SHARED_LOCK;
25178     pInode->nShared++;
25179     pInode->nLock++;
25180     goto end_lock;
25181   }
25182 
25183 
25184   /* A PENDING lock is needed before acquiring a SHARED lock and before
25185   ** acquiring an EXCLUSIVE lock.  For the SHARED lock, the PENDING will
25186   ** be released.
25187   */
25188   lock.l_len = 1L;
25189   lock.l_whence = SEEK_SET;
25190   if( eFileLock==SHARED_LOCK
25191       || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock<PENDING_LOCK)
25192   ){
25193     lock.l_type = (eFileLock==SHARED_LOCK?F_RDLCK:F_WRLCK);
25194     lock.l_start = PENDING_BYTE;
25195     if( unixFileLock(pFile, &lock) ){
25196       tErrno = errno;
25197       rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
25198       if( rc!=SQLITE_BUSY ){
25199         pFile->lastErrno = tErrno;
25200       }
25201       goto end_lock;
25202     }
25203   }
25204 
25205 
25206   /* If control gets to this point, then actually go ahead and make
25207   ** operating system calls for the specified lock.
25208   */
25209   if( eFileLock==SHARED_LOCK ){
25210     assert( pInode->nShared==0 );
25211     assert( pInode->eFileLock==0 );
25212     assert( rc==SQLITE_OK );
25213 
25214     /* Now get the read-lock */
25215     lock.l_start = SHARED_FIRST;
25216     lock.l_len = SHARED_SIZE;
25217     if( unixFileLock(pFile, &lock) ){
25218       tErrno = errno;
25219       rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
25220     }
25221 
25222     /* Drop the temporary PENDING lock */
25223     lock.l_start = PENDING_BYTE;
25224     lock.l_len = 1L;
25225     lock.l_type = F_UNLCK;
25226     if( unixFileLock(pFile, &lock) && rc==SQLITE_OK ){
25227       /* This could happen with a network mount */
25228       tErrno = errno;
25229       rc = SQLITE_IOERR_UNLOCK;
25230     }
25231 
25232     if( rc ){
25233       if( rc!=SQLITE_BUSY ){
25234         pFile->lastErrno = tErrno;
25235       }
25236       goto end_lock;
25237     }else{
25238       pFile->eFileLock = SHARED_LOCK;
25239       pInode->nLock++;
25240       pInode->nShared = 1;
25241     }
25242   }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){
25243     /* We are trying for an exclusive lock but another thread in this
25244     ** same process is still holding a shared lock. */
25245     rc = SQLITE_BUSY;
25246   }else{
25247     /* The request was for a RESERVED or EXCLUSIVE lock.  It is
25248     ** assumed that there is a SHARED or greater lock on the file
25249     ** already.
25250     */
25251     assert( 0!=pFile->eFileLock );
25252     lock.l_type = F_WRLCK;
25253 
25254     assert( eFileLock==RESERVED_LOCK || eFileLock==EXCLUSIVE_LOCK );
25255     if( eFileLock==RESERVED_LOCK ){
25256       lock.l_start = RESERVED_BYTE;
25257       lock.l_len = 1L;
25258     }else{
25259       lock.l_start = SHARED_FIRST;
25260       lock.l_len = SHARED_SIZE;
25261     }
25262 
25263     if( unixFileLock(pFile, &lock) ){
25264       tErrno = errno;
25265       rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
25266       if( rc!=SQLITE_BUSY ){
25267         pFile->lastErrno = tErrno;
25268       }
25269     }
25270   }
25271 
25272 
25273 #ifdef SQLITE_DEBUG
25274   /* Set up the transaction-counter change checking flags when
25275   ** transitioning from a SHARED to a RESERVED lock.  The change
25276   ** from SHARED to RESERVED marks the beginning of a normal
25277   ** write operation (not a hot journal rollback).
25278   */
25279   if( rc==SQLITE_OK
25280    && pFile->eFileLock<=SHARED_LOCK
25281    && eFileLock==RESERVED_LOCK
25282   ){
25283     pFile->transCntrChng = 0;
25284     pFile->dbUpdate = 0;
25285     pFile->inNormalWrite = 1;
25286   }
25287 #endif
25288 
25289 
25290   if( rc==SQLITE_OK ){
25291     pFile->eFileLock = eFileLock;
25292     pInode->eFileLock = eFileLock;
25293   }else if( eFileLock==EXCLUSIVE_LOCK ){
25294     pFile->eFileLock = PENDING_LOCK;
25295     pInode->eFileLock = PENDING_LOCK;
25296   }
25297 
25298 end_lock:
25299   unixLeaveMutex();
25300   OSTRACE(("LOCK    %d %s %s (unix)\n", pFile->h, azFileLock(eFileLock),
25301       rc==SQLITE_OK ? "ok" : "failed"));
25302   return rc;
25303 }
25304 
25305 /*
25306 ** Add the file descriptor used by file handle pFile to the corresponding
25307 ** pUnused list.
25308 */
25309 static void setPendingFd(unixFile *pFile){
25310   unixInodeInfo *pInode = pFile->pInode;
25311   UnixUnusedFd *p = pFile->pUnused;
25312   p->pNext = pInode->pUnused;
25313   pInode->pUnused = p;
25314   pFile->h = -1;
25315   pFile->pUnused = 0;
25316 }
25317 
25318 /*
25319 ** Lower the locking level on file descriptor pFile to eFileLock.  eFileLock
25320 ** must be either NO_LOCK or SHARED_LOCK.
25321 **
25322 ** If the locking level of the file descriptor is already at or below
25323 ** the requested locking level, this routine is a no-op.
25324 **
25325 ** If handleNFSUnlock is true, then on downgrading an EXCLUSIVE_LOCK to SHARED
25326 ** the byte range is divided into 2 parts and the first part is unlocked then
25327 ** set to a read lock, then the other part is simply unlocked.  This works
25328 ** around a bug in BSD NFS lockd (also seen on MacOSX 10.3+) that fails to
25329 ** remove the write lock on a region when a read lock is set.
25330 */
25331 static int posixUnlock(sqlite3_file *id, int eFileLock, int handleNFSUnlock){
25332   unixFile *pFile = (unixFile*)id;
25333   unixInodeInfo *pInode;
25334   struct flock lock;
25335   int rc = SQLITE_OK;
25336 
25337   assert( pFile );
25338   OSTRACE(("UNLOCK  %d %d was %d(%d,%d) pid=%d (unix)\n", pFile->h, eFileLock,
25339       pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared,
25340       getpid()));
25341 
25342   assert( eFileLock<=SHARED_LOCK );
25343   if( pFile->eFileLock<=eFileLock ){
25344     return SQLITE_OK;
25345   }
25346   unixEnterMutex();
25347   pInode = pFile->pInode;
25348   assert( pInode->nShared!=0 );
25349   if( pFile->eFileLock>SHARED_LOCK ){
25350     assert( pInode->eFileLock==pFile->eFileLock );
25351 
25352 #ifdef SQLITE_DEBUG
25353     /* When reducing a lock such that other processes can start
25354     ** reading the database file again, make sure that the
25355     ** transaction counter was updated if any part of the database
25356     ** file changed.  If the transaction counter is not updated,
25357     ** other connections to the same file might not realize that
25358     ** the file has changed and hence might not know to flush their
25359     ** cache.  The use of a stale cache can lead to database corruption.
25360     */
25361     pFile->inNormalWrite = 0;
25362 #endif
25363 
25364     /* downgrading to a shared lock on NFS involves clearing the write lock
25365     ** before establishing the readlock - to avoid a race condition we downgrade
25366     ** the lock in 2 blocks, so that part of the range will be covered by a
25367     ** write lock until the rest is covered by a read lock:
25368     **  1:   [WWWWW]
25369     **  2:   [....W]
25370     **  3:   [RRRRW]
25371     **  4:   [RRRR.]
25372     */
25373     if( eFileLock==SHARED_LOCK ){
25374 
25375 #if !defined(__APPLE__) || !SQLITE_ENABLE_LOCKING_STYLE
25376       (void)handleNFSUnlock;
25377       assert( handleNFSUnlock==0 );
25378 #endif
25379 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
25380       if( handleNFSUnlock ){
25381         int tErrno;               /* Error code from system call errors */
25382         off_t divSize = SHARED_SIZE - 1;
25383 
25384         lock.l_type = F_UNLCK;
25385         lock.l_whence = SEEK_SET;
25386         lock.l_start = SHARED_FIRST;
25387         lock.l_len = divSize;
25388         if( unixFileLock(pFile, &lock)==(-1) ){
25389           tErrno = errno;
25390           rc = SQLITE_IOERR_UNLOCK;
25391           if( IS_LOCK_ERROR(rc) ){
25392             pFile->lastErrno = tErrno;
25393           }
25394           goto end_unlock;
25395         }
25396         lock.l_type = F_RDLCK;
25397         lock.l_whence = SEEK_SET;
25398         lock.l_start = SHARED_FIRST;
25399         lock.l_len = divSize;
25400         if( unixFileLock(pFile, &lock)==(-1) ){
25401           tErrno = errno;
25402           rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_RDLOCK);
25403           if( IS_LOCK_ERROR(rc) ){
25404             pFile->lastErrno = tErrno;
25405           }
25406           goto end_unlock;
25407         }
25408         lock.l_type = F_UNLCK;
25409         lock.l_whence = SEEK_SET;
25410         lock.l_start = SHARED_FIRST+divSize;
25411         lock.l_len = SHARED_SIZE-divSize;
25412         if( unixFileLock(pFile, &lock)==(-1) ){
25413           tErrno = errno;
25414           rc = SQLITE_IOERR_UNLOCK;
25415           if( IS_LOCK_ERROR(rc) ){
25416             pFile->lastErrno = tErrno;
25417           }
25418           goto end_unlock;
25419         }
25420       }else
25421 #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
25422       {
25423         lock.l_type = F_RDLCK;
25424         lock.l_whence = SEEK_SET;
25425         lock.l_start = SHARED_FIRST;
25426         lock.l_len = SHARED_SIZE;
25427         if( unixFileLock(pFile, &lock) ){
25428           /* In theory, the call to unixFileLock() cannot fail because another
25429           ** process is holding an incompatible lock. If it does, this
25430           ** indicates that the other process is not following the locking
25431           ** protocol. If this happens, return SQLITE_IOERR_RDLOCK. Returning
25432           ** SQLITE_BUSY would confuse the upper layer (in practice it causes
25433           ** an assert to fail). */
25434           rc = SQLITE_IOERR_RDLOCK;
25435           pFile->lastErrno = errno;
25436           goto end_unlock;
25437         }
25438       }
25439     }
25440     lock.l_type = F_UNLCK;
25441     lock.l_whence = SEEK_SET;
25442     lock.l_start = PENDING_BYTE;
25443     lock.l_len = 2L;  assert( PENDING_BYTE+1==RESERVED_BYTE );
25444     if( unixFileLock(pFile, &lock)==0 ){
25445       pInode->eFileLock = SHARED_LOCK;
25446     }else{
25447       rc = SQLITE_IOERR_UNLOCK;
25448       pFile->lastErrno = errno;
25449       goto end_unlock;
25450     }
25451   }
25452   if( eFileLock==NO_LOCK ){
25453     /* Decrement the shared lock counter.  Release the lock using an
25454     ** OS call only when all threads in this same process have released
25455     ** the lock.
25456     */
25457     pInode->nShared--;
25458     if( pInode->nShared==0 ){
25459       lock.l_type = F_UNLCK;
25460       lock.l_whence = SEEK_SET;
25461       lock.l_start = lock.l_len = 0L;
25462       if( unixFileLock(pFile, &lock)==0 ){
25463         pInode->eFileLock = NO_LOCK;
25464       }else{
25465         rc = SQLITE_IOERR_UNLOCK;
25466         pFile->lastErrno = errno;
25467         pInode->eFileLock = NO_LOCK;
25468         pFile->eFileLock = NO_LOCK;
25469       }
25470     }
25471 
25472     /* Decrement the count of locks against this same file.  When the
25473     ** count reaches zero, close any other file descriptors whose close
25474     ** was deferred because of outstanding locks.
25475     */
25476     pInode->nLock--;
25477     assert( pInode->nLock>=0 );
25478     if( pInode->nLock==0 ){
25479       closePendingFds(pFile);
25480     }
25481   }
25482 
25483 end_unlock:
25484   unixLeaveMutex();
25485   if( rc==SQLITE_OK ) pFile->eFileLock = eFileLock;
25486   return rc;
25487 }
25488 
25489 /*
25490 ** Lower the locking level on file descriptor pFile to eFileLock.  eFileLock
25491 ** must be either NO_LOCK or SHARED_LOCK.
25492 **
25493 ** If the locking level of the file descriptor is already at or below
25494 ** the requested locking level, this routine is a no-op.
25495 */
25496 static int unixUnlock(sqlite3_file *id, int eFileLock){
25497 #if SQLITE_MAX_MMAP_SIZE>0
25498   assert( eFileLock==SHARED_LOCK || ((unixFile *)id)->nFetchOut==0 );
25499 #endif
25500   return posixUnlock(id, eFileLock, 0);
25501 }
25502 
25503 #if SQLITE_MAX_MMAP_SIZE>0
25504 static int unixMapfile(unixFile *pFd, i64 nByte);
25505 static void unixUnmapfile(unixFile *pFd);
25506 #endif
25507 
25508 /*
25509 ** This function performs the parts of the "close file" operation
25510 ** common to all locking schemes. It closes the directory and file
25511 ** handles, if they are valid, and sets all fields of the unixFile
25512 ** structure to 0.
25513 **
25514 ** It is *not* necessary to hold the mutex when this routine is called,
25515 ** even on VxWorks.  A mutex will be acquired on VxWorks by the
25516 ** vxworksReleaseFileId() routine.
25517 */
25518 static int closeUnixFile(sqlite3_file *id){
25519   unixFile *pFile = (unixFile*)id;
25520 #if SQLITE_MAX_MMAP_SIZE>0
25521   unixUnmapfile(pFile);
25522 #endif
25523   if( pFile->h>=0 ){
25524     robust_close(pFile, pFile->h, __LINE__);
25525     pFile->h = -1;
25526   }
25527 #if OS_VXWORKS
25528   if( pFile->pId ){
25529     if( pFile->ctrlFlags & UNIXFILE_DELETE ){
25530       osUnlink(pFile->pId->zCanonicalName);
25531     }
25532     vxworksReleaseFileId(pFile->pId);
25533     pFile->pId = 0;
25534   }
25535 #endif
25536   OSTRACE(("CLOSE   %-3d\n", pFile->h));
25537   OpenCounter(-1);
25538   sqlite3_free(pFile->pUnused);
25539   memset(pFile, 0, sizeof(unixFile));
25540   return SQLITE_OK;
25541 }
25542 
25543 /*
25544 ** Close a file.
25545 */
25546 static int unixClose(sqlite3_file *id){
25547   int rc = SQLITE_OK;
25548   unixFile *pFile = (unixFile *)id;
25549   verifyDbFile(pFile);
25550   unixUnlock(id, NO_LOCK);
25551   unixEnterMutex();
25552 
25553   /* unixFile.pInode is always valid here. Otherwise, a different close
25554   ** routine (e.g. nolockClose()) would be called instead.
25555   */
25556   assert( pFile->pInode->nLock>0 || pFile->pInode->bProcessLock==0 );
25557   if( ALWAYS(pFile->pInode) && pFile->pInode->nLock ){
25558     /* If there are outstanding locks, do not actually close the file just
25559     ** yet because that would clear those locks.  Instead, add the file
25560     ** descriptor to pInode->pUnused list.  It will be automatically closed
25561     ** when the last lock is cleared.
25562     */
25563     setPendingFd(pFile);
25564   }
25565   releaseInodeInfo(pFile);
25566   rc = closeUnixFile(id);
25567   unixLeaveMutex();
25568   return rc;
25569 }
25570 
25571 /************** End of the posix advisory lock implementation *****************
25572 ******************************************************************************/
25573 
25574 /******************************************************************************
25575 ****************************** No-op Locking **********************************
25576 **
25577 ** Of the various locking implementations available, this is by far the
25578 ** simplest:  locking is ignored.  No attempt is made to lock the database
25579 ** file for reading or writing.
25580 **
25581 ** This locking mode is appropriate for use on read-only databases
25582 ** (ex: databases that are burned into CD-ROM, for example.)  It can
25583 ** also be used if the application employs some external mechanism to
25584 ** prevent simultaneous access of the same database by two or more
25585 ** database connections.  But there is a serious risk of database
25586 ** corruption if this locking mode is used in situations where multiple
25587 ** database connections are accessing the same database file at the same
25588 ** time and one or more of those connections are writing.
25589 */
25590 
25591 static int nolockCheckReservedLock(sqlite3_file *NotUsed, int *pResOut){
25592   UNUSED_PARAMETER(NotUsed);
25593   *pResOut = 0;
25594   return SQLITE_OK;
25595 }
25596 static int nolockLock(sqlite3_file *NotUsed, int NotUsed2){
25597   UNUSED_PARAMETER2(NotUsed, NotUsed2);
25598   return SQLITE_OK;
25599 }
25600 static int nolockUnlock(sqlite3_file *NotUsed, int NotUsed2){
25601   UNUSED_PARAMETER2(NotUsed, NotUsed2);
25602   return SQLITE_OK;
25603 }
25604 
25605 /*
25606 ** Close the file.
25607 */
25608 static int nolockClose(sqlite3_file *id) {
25609   return closeUnixFile(id);
25610 }
25611 
25612 /******************* End of the no-op lock implementation *********************
25613 ******************************************************************************/
25614 
25615 /******************************************************************************
25616 ************************* Begin dot-file Locking ******************************
25617 **
25618 ** The dotfile locking implementation uses the existence of separate lock
25619 ** files (really a directory) to control access to the database.  This works
25620 ** on just about every filesystem imaginable.  But there are serious downsides:
25621 **
25622 **    (1)  There is zero concurrency.  A single reader blocks all other
25623 **         connections from reading or writing the database.
25624 **
25625 **    (2)  An application crash or power loss can leave stale lock files
25626 **         sitting around that need to be cleared manually.
25627 **
25628 ** Nevertheless, a dotlock is an appropriate locking mode for use if no
25629 ** other locking strategy is available.
25630 **
25631 ** Dotfile locking works by creating a subdirectory in the same directory as
25632 ** the database and with the same name but with a ".lock" extension added.
25633 ** The existence of a lock directory implies an EXCLUSIVE lock.  All other
25634 ** lock types (SHARED, RESERVED, PENDING) are mapped into EXCLUSIVE.
25635 */
25636 
25637 /*
25638 ** The file suffix added to the data base filename in order to create the
25639 ** lock directory.
25640 */
25641 #define DOTLOCK_SUFFIX ".lock"
25642 
25643 /*
25644 ** This routine checks if there is a RESERVED lock held on the specified
25645 ** file by this or any other process. If such a lock is held, set *pResOut
25646 ** to a non-zero value otherwise *pResOut is set to zero.  The return value
25647 ** is set to SQLITE_OK unless an I/O error occurs during lock checking.
25648 **
25649 ** In dotfile locking, either a lock exists or it does not.  So in this
25650 ** variation of CheckReservedLock(), *pResOut is set to true if any lock
25651 ** is held on the file and false if the file is unlocked.
25652 */
25653 static int dotlockCheckReservedLock(sqlite3_file *id, int *pResOut) {
25654   int rc = SQLITE_OK;
25655   int reserved = 0;
25656   unixFile *pFile = (unixFile*)id;
25657 
25658   SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
25659 
25660   assert( pFile );
25661 
25662   /* Check if a thread in this process holds such a lock */
25663   if( pFile->eFileLock>SHARED_LOCK ){
25664     /* Either this connection or some other connection in the same process
25665     ** holds a lock on the file.  No need to check further. */
25666     reserved = 1;
25667   }else{
25668     /* The lock is held if and only if the lockfile exists */
25669     const char *zLockFile = (const char*)pFile->lockingContext;
25670     reserved = osAccess(zLockFile, 0)==0;
25671   }
25672   OSTRACE(("TEST WR-LOCK %d %d %d (dotlock)\n", pFile->h, rc, reserved));
25673   *pResOut = reserved;
25674   return rc;
25675 }
25676 
25677 /*
25678 ** Lock the file with the lock specified by parameter eFileLock - one
25679 ** of the following:
25680 **
25681 **     (1) SHARED_LOCK
25682 **     (2) RESERVED_LOCK
25683 **     (3) PENDING_LOCK
25684 **     (4) EXCLUSIVE_LOCK
25685 **
25686 ** Sometimes when requesting one lock state, additional lock states
25687 ** are inserted in between.  The locking might fail on one of the later
25688 ** transitions leaving the lock state different from what it started but
25689 ** still short of its goal.  The following chart shows the allowed
25690 ** transitions and the inserted intermediate states:
25691 **
25692 **    UNLOCKED -> SHARED
25693 **    SHARED -> RESERVED
25694 **    SHARED -> (PENDING) -> EXCLUSIVE
25695 **    RESERVED -> (PENDING) -> EXCLUSIVE
25696 **    PENDING -> EXCLUSIVE
25697 **
25698 ** This routine will only increase a lock.  Use the sqlite3OsUnlock()
25699 ** routine to lower a locking level.
25700 **
25701 ** With dotfile locking, we really only support state (4): EXCLUSIVE.
25702 ** But we track the other locking levels internally.
25703 */
25704 static int dotlockLock(sqlite3_file *id, int eFileLock) {
25705   unixFile *pFile = (unixFile*)id;
25706   char *zLockFile = (char *)pFile->lockingContext;
25707   int rc = SQLITE_OK;
25708 
25709 
25710   /* If we have any lock, then the lock file already exists.  All we have
25711   ** to do is adjust our internal record of the lock level.
25712   */
25713   if( pFile->eFileLock > NO_LOCK ){
25714     pFile->eFileLock = eFileLock;
25715     /* Always update the timestamp on the old file */
25716 #ifdef HAVE_UTIME
25717     utime(zLockFile, NULL);
25718 #else
25719     utimes(zLockFile, NULL);
25720 #endif
25721     return SQLITE_OK;
25722   }
25723 
25724   /* grab an exclusive lock */
25725   rc = osMkdir(zLockFile, 0777);
25726   if( rc<0 ){
25727     /* failed to open/create the lock directory */
25728     int tErrno = errno;
25729     if( EEXIST == tErrno ){
25730       rc = SQLITE_BUSY;
25731     } else {
25732       rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
25733       if( IS_LOCK_ERROR(rc) ){
25734         pFile->lastErrno = tErrno;
25735       }
25736     }
25737     return rc;
25738   }
25739 
25740   /* got it, set the type and return ok */
25741   pFile->eFileLock = eFileLock;
25742   return rc;
25743 }
25744 
25745 /*
25746 ** Lower the locking level on file descriptor pFile to eFileLock.  eFileLock
25747 ** must be either NO_LOCK or SHARED_LOCK.
25748 **
25749 ** If the locking level of the file descriptor is already at or below
25750 ** the requested locking level, this routine is a no-op.
25751 **
25752 ** When the locking level reaches NO_LOCK, delete the lock file.
25753 */
25754 static int dotlockUnlock(sqlite3_file *id, int eFileLock) {
25755   unixFile *pFile = (unixFile*)id;
25756   char *zLockFile = (char *)pFile->lockingContext;
25757   int rc;
25758 
25759   assert( pFile );
25760   OSTRACE(("UNLOCK  %d %d was %d pid=%d (dotlock)\n", pFile->h, eFileLock,
25761            pFile->eFileLock, getpid()));
25762   assert( eFileLock<=SHARED_LOCK );
25763 
25764   /* no-op if possible */
25765   if( pFile->eFileLock==eFileLock ){
25766     return SQLITE_OK;
25767   }
25768 
25769   /* To downgrade to shared, simply update our internal notion of the
25770   ** lock state.  No need to mess with the file on disk.
25771   */
25772   if( eFileLock==SHARED_LOCK ){
25773     pFile->eFileLock = SHARED_LOCK;
25774     return SQLITE_OK;
25775   }
25776 
25777   /* To fully unlock the database, delete the lock file */
25778   assert( eFileLock==NO_LOCK );
25779   rc = osRmdir(zLockFile);
25780   if( rc<0 && errno==ENOTDIR ) rc = osUnlink(zLockFile);
25781   if( rc<0 ){
25782     int tErrno = errno;
25783     rc = 0;
25784     if( ENOENT != tErrno ){
25785       rc = SQLITE_IOERR_UNLOCK;
25786     }
25787     if( IS_LOCK_ERROR(rc) ){
25788       pFile->lastErrno = tErrno;
25789     }
25790     return rc;
25791   }
25792   pFile->eFileLock = NO_LOCK;
25793   return SQLITE_OK;
25794 }
25795 
25796 /*
25797 ** Close a file.  Make sure the lock has been released before closing.
25798 */
25799 static int dotlockClose(sqlite3_file *id) {
25800   int rc = SQLITE_OK;
25801   if( id ){
25802     unixFile *pFile = (unixFile*)id;
25803     dotlockUnlock(id, NO_LOCK);
25804     sqlite3_free(pFile->lockingContext);
25805     rc = closeUnixFile(id);
25806   }
25807   return rc;
25808 }
25809 /****************** End of the dot-file lock implementation *******************
25810 ******************************************************************************/
25811 
25812 /******************************************************************************
25813 ************************** Begin flock Locking ********************************
25814 **
25815 ** Use the flock() system call to do file locking.
25816 **
25817 ** flock() locking is like dot-file locking in that the various
25818 ** fine-grain locking levels supported by SQLite are collapsed into
25819 ** a single exclusive lock.  In other words, SHARED, RESERVED, and
25820 ** PENDING locks are the same thing as an EXCLUSIVE lock.  SQLite
25821 ** still works when you do this, but concurrency is reduced since
25822 ** only a single process can be reading the database at a time.
25823 **
25824 ** Omit this section if SQLITE_ENABLE_LOCKING_STYLE is turned off or if
25825 ** compiling for VXWORKS.
25826 */
25827 #if SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORKS
25828 
25829 /*
25830 ** Retry flock() calls that fail with EINTR
25831 */
25832 #ifdef EINTR
25833 static int robust_flock(int fd, int op){
25834   int rc;
25835   do{ rc = flock(fd,op); }while( rc<0 && errno==EINTR );
25836   return rc;
25837 }
25838 #else
25839 # define robust_flock(a,b) flock(a,b)
25840 #endif
25841 
25842 
25843 /*
25844 ** This routine checks if there is a RESERVED lock held on the specified
25845 ** file by this or any other process. If such a lock is held, set *pResOut
25846 ** to a non-zero value otherwise *pResOut is set to zero.  The return value
25847 ** is set to SQLITE_OK unless an I/O error occurs during lock checking.
25848 */
25849 static int flockCheckReservedLock(sqlite3_file *id, int *pResOut){
25850   int rc = SQLITE_OK;
25851   int reserved = 0;
25852   unixFile *pFile = (unixFile*)id;
25853 
25854   SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
25855 
25856   assert( pFile );
25857 
25858   /* Check if a thread in this process holds such a lock */
25859   if( pFile->eFileLock>SHARED_LOCK ){
25860     reserved = 1;
25861   }
25862 
25863   /* Otherwise see if some other process holds it. */
25864   if( !reserved ){
25865     /* attempt to get the lock */
25866     int lrc = robust_flock(pFile->h, LOCK_EX | LOCK_NB);
25867     if( !lrc ){
25868       /* got the lock, unlock it */
25869       lrc = robust_flock(pFile->h, LOCK_UN);
25870       if ( lrc ) {
25871         int tErrno = errno;
25872         /* unlock failed with an error */
25873         lrc = SQLITE_IOERR_UNLOCK;
25874         if( IS_LOCK_ERROR(lrc) ){
25875           pFile->lastErrno = tErrno;
25876           rc = lrc;
25877         }
25878       }
25879     } else {
25880       int tErrno = errno;
25881       reserved = 1;
25882       /* someone else might have it reserved */
25883       lrc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
25884       if( IS_LOCK_ERROR(lrc) ){
25885         pFile->lastErrno = tErrno;
25886         rc = lrc;
25887       }
25888     }
25889   }
25890   OSTRACE(("TEST WR-LOCK %d %d %d (flock)\n", pFile->h, rc, reserved));
25891 
25892 #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
25893   if( (rc & SQLITE_IOERR) == SQLITE_IOERR ){
25894     rc = SQLITE_OK;
25895     reserved=1;
25896   }
25897 #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
25898   *pResOut = reserved;
25899   return rc;
25900 }
25901 
25902 /*
25903 ** Lock the file with the lock specified by parameter eFileLock - one
25904 ** of the following:
25905 **
25906 **     (1) SHARED_LOCK
25907 **     (2) RESERVED_LOCK
25908 **     (3) PENDING_LOCK
25909 **     (4) EXCLUSIVE_LOCK
25910 **
25911 ** Sometimes when requesting one lock state, additional lock states
25912 ** are inserted in between.  The locking might fail on one of the later
25913 ** transitions leaving the lock state different from what it started but
25914 ** still short of its goal.  The following chart shows the allowed
25915 ** transitions and the inserted intermediate states:
25916 **
25917 **    UNLOCKED -> SHARED
25918 **    SHARED -> RESERVED
25919 **    SHARED -> (PENDING) -> EXCLUSIVE
25920 **    RESERVED -> (PENDING) -> EXCLUSIVE
25921 **    PENDING -> EXCLUSIVE
25922 **
25923 ** flock() only really support EXCLUSIVE locks.  We track intermediate
25924 ** lock states in the sqlite3_file structure, but all locks SHARED or
25925 ** above are really EXCLUSIVE locks and exclude all other processes from
25926 ** access the file.
25927 **
25928 ** This routine will only increase a lock.  Use the sqlite3OsUnlock()
25929 ** routine to lower a locking level.
25930 */
25931 static int flockLock(sqlite3_file *id, int eFileLock) {
25932   int rc = SQLITE_OK;
25933   unixFile *pFile = (unixFile*)id;
25934 
25935   assert( pFile );
25936 
25937   /* if we already have a lock, it is exclusive.
25938   ** Just adjust level and punt on outta here. */
25939   if (pFile->eFileLock > NO_LOCK) {
25940     pFile->eFileLock = eFileLock;
25941     return SQLITE_OK;
25942   }
25943 
25944   /* grab an exclusive lock */
25945 
25946   if (robust_flock(pFile->h, LOCK_EX | LOCK_NB)) {
25947     int tErrno = errno;
25948     /* didn't get, must be busy */
25949     rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK);
25950     if( IS_LOCK_ERROR(rc) ){
25951       pFile->lastErrno = tErrno;
25952     }
25953   } else {
25954     /* got it, set the type and return ok */
25955     pFile->eFileLock = eFileLock;
25956   }
25957   OSTRACE(("LOCK    %d %s %s (flock)\n", pFile->h, azFileLock(eFileLock),
25958            rc==SQLITE_OK ? "ok" : "failed"));
25959 #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
25960   if( (rc & SQLITE_IOERR) == SQLITE_IOERR ){
25961     rc = SQLITE_BUSY;
25962   }
25963 #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
25964   return rc;
25965 }
25966 
25967 
25968 /*
25969 ** Lower the locking level on file descriptor pFile to eFileLock.  eFileLock
25970 ** must be either NO_LOCK or SHARED_LOCK.
25971 **
25972 ** If the locking level of the file descriptor is already at or below
25973 ** the requested locking level, this routine is a no-op.
25974 */
25975 static int flockUnlock(sqlite3_file *id, int eFileLock) {
25976   unixFile *pFile = (unixFile*)id;
25977 
25978   assert( pFile );
25979   OSTRACE(("UNLOCK  %d %d was %d pid=%d (flock)\n", pFile->h, eFileLock,
25980            pFile->eFileLock, getpid()));
25981   assert( eFileLock<=SHARED_LOCK );
25982 
25983   /* no-op if possible */
25984   if( pFile->eFileLock==eFileLock ){
25985     return SQLITE_OK;
25986   }
25987 
25988   /* shared can just be set because we always have an exclusive */
25989   if (eFileLock==SHARED_LOCK) {
25990     pFile->eFileLock = eFileLock;
25991     return SQLITE_OK;
25992   }
25993 
25994   /* no, really, unlock. */
25995   if( robust_flock(pFile->h, LOCK_UN) ){
25996 #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS
25997     return SQLITE_OK;
25998 #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */
25999     return SQLITE_IOERR_UNLOCK;
26000   }else{
26001     pFile->eFileLock = NO_LOCK;
26002     return SQLITE_OK;
26003   }
26004 }
26005 
26006 /*
26007 ** Close a file.
26008 */
26009 static int flockClose(sqlite3_file *id) {
26010   int rc = SQLITE_OK;
26011   if( id ){
26012     flockUnlock(id, NO_LOCK);
26013     rc = closeUnixFile(id);
26014   }
26015   return rc;
26016 }
26017 
26018 #endif /* SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORK */
26019 
26020 /******************* End of the flock lock implementation *********************
26021 ******************************************************************************/
26022 
26023 /******************************************************************************
26024 ************************ Begin Named Semaphore Locking ************************
26025 **
26026 ** Named semaphore locking is only supported on VxWorks.
26027 **
26028 ** Semaphore locking is like dot-lock and flock in that it really only
26029 ** supports EXCLUSIVE locking.  Only a single process can read or write
26030 ** the database file at a time.  This reduces potential concurrency, but
26031 ** makes the lock implementation much easier.
26032 */
26033 #if OS_VXWORKS
26034 
26035 /*
26036 ** This routine checks if there is a RESERVED lock held on the specified
26037 ** file by this or any other process. If such a lock is held, set *pResOut
26038 ** to a non-zero value otherwise *pResOut is set to zero.  The return value
26039 ** is set to SQLITE_OK unless an I/O error occurs during lock checking.
26040 */
26041 static int semCheckReservedLock(sqlite3_file *id, int *pResOut) {
26042   int rc = SQLITE_OK;
26043   int reserved = 0;
26044   unixFile *pFile = (unixFile*)id;
26045 
26046   SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
26047 
26048   assert( pFile );
26049 
26050   /* Check if a thread in this process holds such a lock */
26051   if( pFile->eFileLock>SHARED_LOCK ){
26052     reserved = 1;
26053   }
26054 
26055   /* Otherwise see if some other process holds it. */
26056   if( !reserved ){
26057     sem_t *pSem = pFile->pInode->pSem;
26058     struct stat statBuf;
26059 
26060     if( sem_trywait(pSem)==-1 ){
26061       int tErrno = errno;
26062       if( EAGAIN != tErrno ){
26063         rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_CHECKRESERVEDLOCK);
26064         pFile->lastErrno = tErrno;
26065       } else {
26066         /* someone else has the lock when we are in NO_LOCK */
26067         reserved = (pFile->eFileLock < SHARED_LOCK);
26068       }
26069     }else{
26070       /* we could have it if we want it */
26071       sem_post(pSem);
26072     }
26073   }
26074   OSTRACE(("TEST WR-LOCK %d %d %d (sem)\n", pFile->h, rc, reserved));
26075 
26076   *pResOut = reserved;
26077   return rc;
26078 }
26079 
26080 /*
26081 ** Lock the file with the lock specified by parameter eFileLock - one
26082 ** of the following:
26083 **
26084 **     (1) SHARED_LOCK
26085 **     (2) RESERVED_LOCK
26086 **     (3) PENDING_LOCK
26087 **     (4) EXCLUSIVE_LOCK
26088 **
26089 ** Sometimes when requesting one lock state, additional lock states
26090 ** are inserted in between.  The locking might fail on one of the later
26091 ** transitions leaving the lock state different from what it started but
26092 ** still short of its goal.  The following chart shows the allowed
26093 ** transitions and the inserted intermediate states:
26094 **
26095 **    UNLOCKED -> SHARED
26096 **    SHARED -> RESERVED
26097 **    SHARED -> (PENDING) -> EXCLUSIVE
26098 **    RESERVED -> (PENDING) -> EXCLUSIVE
26099 **    PENDING -> EXCLUSIVE
26100 **
26101 ** Semaphore locks only really support EXCLUSIVE locks.  We track intermediate
26102 ** lock states in the sqlite3_file structure, but all locks SHARED or
26103 ** above are really EXCLUSIVE locks and exclude all other processes from
26104 ** access the file.
26105 **
26106 ** This routine will only increase a lock.  Use the sqlite3OsUnlock()
26107 ** routine to lower a locking level.
26108 */
26109 static int semLock(sqlite3_file *id, int eFileLock) {
26110   unixFile *pFile = (unixFile*)id;
26111   int fd;
26112   sem_t *pSem = pFile->pInode->pSem;
26113   int rc = SQLITE_OK;
26114 
26115   /* if we already have a lock, it is exclusive.
26116   ** Just adjust level and punt on outta here. */
26117   if (pFile->eFileLock > NO_LOCK) {
26118     pFile->eFileLock = eFileLock;
26119     rc = SQLITE_OK;
26120     goto sem_end_lock;
26121   }
26122 
26123   /* lock semaphore now but bail out when already locked. */
26124   if( sem_trywait(pSem)==-1 ){
26125     rc = SQLITE_BUSY;
26126     goto sem_end_lock;
26127   }
26128 
26129   /* got it, set the type and return ok */
26130   pFile->eFileLock = eFileLock;
26131 
26132  sem_end_lock:
26133   return rc;
26134 }
26135 
26136 /*
26137 ** Lower the locking level on file descriptor pFile to eFileLock.  eFileLock
26138 ** must be either NO_LOCK or SHARED_LOCK.
26139 **
26140 ** If the locking level of the file descriptor is already at or below
26141 ** the requested locking level, this routine is a no-op.
26142 */
26143 static int semUnlock(sqlite3_file *id, int eFileLock) {
26144   unixFile *pFile = (unixFile*)id;
26145   sem_t *pSem = pFile->pInode->pSem;
26146 
26147   assert( pFile );
26148   assert( pSem );
26149   OSTRACE(("UNLOCK  %d %d was %d pid=%d (sem)\n", pFile->h, eFileLock,
26150            pFile->eFileLock, getpid()));
26151   assert( eFileLock<=SHARED_LOCK );
26152 
26153   /* no-op if possible */
26154   if( pFile->eFileLock==eFileLock ){
26155     return SQLITE_OK;
26156   }
26157 
26158   /* shared can just be set because we always have an exclusive */
26159   if (eFileLock==SHARED_LOCK) {
26160     pFile->eFileLock = eFileLock;
26161     return SQLITE_OK;
26162   }
26163 
26164   /* no, really unlock. */
26165   if ( sem_post(pSem)==-1 ) {
26166     int rc, tErrno = errno;
26167     rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK);
26168     if( IS_LOCK_ERROR(rc) ){
26169       pFile->lastErrno = tErrno;
26170     }
26171     return rc;
26172   }
26173   pFile->eFileLock = NO_LOCK;
26174   return SQLITE_OK;
26175 }
26176 
26177 /*
26178  ** Close a file.
26179  */
26180 static int semClose(sqlite3_file *id) {
26181   if( id ){
26182     unixFile *pFile = (unixFile*)id;
26183     semUnlock(id, NO_LOCK);
26184     assert( pFile );
26185     unixEnterMutex();
26186     releaseInodeInfo(pFile);
26187     unixLeaveMutex();
26188     closeUnixFile(id);
26189   }
26190   return SQLITE_OK;
26191 }
26192 
26193 #endif /* OS_VXWORKS */
26194 /*
26195 ** Named semaphore locking is only available on VxWorks.
26196 **
26197 *************** End of the named semaphore lock implementation ****************
26198 ******************************************************************************/
26199 
26200 
26201 /******************************************************************************
26202 *************************** Begin AFP Locking *********************************
26203 **
26204 ** AFP is the Apple Filing Protocol.  AFP is a network filesystem found
26205 ** on Apple Macintosh computers - both OS9 and OSX.
26206 **
26207 ** Third-party implementations of AFP are available.  But this code here
26208 ** only works on OSX.
26209 */
26210 
26211 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
26212 /*
26213 ** The afpLockingContext structure contains all afp lock specific state
26214 */
26215 typedef struct afpLockingContext afpLockingContext;
26216 struct afpLockingContext {
26217   int reserved;
26218   const char *dbPath;             /* Name of the open file */
26219 };
26220 
26221 struct ByteRangeLockPB2
26222 {
26223   unsigned long long offset;        /* offset to first byte to lock */
26224   unsigned long long length;        /* nbr of bytes to lock */
26225   unsigned long long retRangeStart; /* nbr of 1st byte locked if successful */
26226   unsigned char unLockFlag;         /* 1 = unlock, 0 = lock */
26227   unsigned char startEndFlag;       /* 1=rel to end of fork, 0=rel to start */
26228   int fd;                           /* file desc to assoc this lock with */
26229 };
26230 
26231 #define afpfsByteRangeLock2FSCTL        _IOWR('z', 23, struct ByteRangeLockPB2)
26232 
26233 /*
26234 ** This is a utility for setting or clearing a bit-range lock on an
26235 ** AFP filesystem.
26236 **
26237 ** Return SQLITE_OK on success, SQLITE_BUSY on failure.
26238 */
26239 static int afpSetLock(
26240   const char *path,              /* Name of the file to be locked or unlocked */
26241   unixFile *pFile,               /* Open file descriptor on path */
26242   unsigned long long offset,     /* First byte to be locked */
26243   unsigned long long length,     /* Number of bytes to lock */
26244   int setLockFlag                /* True to set lock.  False to clear lock */
26245 ){
26246   struct ByteRangeLockPB2 pb;
26247   int err;
26248 
26249   pb.unLockFlag = setLockFlag ? 0 : 1;
26250   pb.startEndFlag = 0;
26251   pb.offset = offset;
26252   pb.length = length;
26253   pb.fd = pFile->h;
26254 
26255   OSTRACE(("AFPSETLOCK [%s] for %d%s in range %llx:%llx\n",
26256     (setLockFlag?"ON":"OFF"), pFile->h, (pb.fd==-1?"[testval-1]":""),
26257     offset, length));
26258   err = fsctl(path, afpfsByteRangeLock2FSCTL, &pb, 0);
26259   if ( err==-1 ) {
26260     int rc;
26261     int tErrno = errno;
26262     OSTRACE(("AFPSETLOCK failed to fsctl() '%s' %d %s\n",
26263              path, tErrno, strerror(tErrno)));
26264 #ifdef SQLITE_IGNORE_AFP_LOCK_ERRORS
26265     rc = SQLITE_BUSY;
26266 #else
26267     rc = sqliteErrorFromPosixError(tErrno,
26268                     setLockFlag ? SQLITE_IOERR_LOCK : SQLITE_IOERR_UNLOCK);
26269 #endif /* SQLITE_IGNORE_AFP_LOCK_ERRORS */
26270     if( IS_LOCK_ERROR(rc) ){
26271       pFile->lastErrno = tErrno;
26272     }
26273     return rc;
26274   } else {
26275     return SQLITE_OK;
26276   }
26277 }
26278 
26279 /*
26280 ** This routine checks if there is a RESERVED lock held on the specified
26281 ** file by this or any other process. If such a lock is held, set *pResOut
26282 ** to a non-zero value otherwise *pResOut is set to zero.  The return value
26283 ** is set to SQLITE_OK unless an I/O error occurs during lock checking.
26284 */
26285 static int afpCheckReservedLock(sqlite3_file *id, int *pResOut){
26286   int rc = SQLITE_OK;
26287   int reserved = 0;
26288   unixFile *pFile = (unixFile*)id;
26289   afpLockingContext *context;
26290 
26291   SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
26292 
26293   assert( pFile );
26294   context = (afpLockingContext *) pFile->lockingContext;
26295   if( context->reserved ){
26296     *pResOut = 1;
26297     return SQLITE_OK;
26298   }
26299   unixEnterMutex(); /* Because pFile->pInode is shared across threads */
26300 
26301   /* Check if a thread in this process holds such a lock */
26302   if( pFile->pInode->eFileLock>SHARED_LOCK ){
26303     reserved = 1;
26304   }
26305 
26306   /* Otherwise see if some other process holds it.
26307    */
26308   if( !reserved ){
26309     /* lock the RESERVED byte */
26310     int lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1);
26311     if( SQLITE_OK==lrc ){
26312       /* if we succeeded in taking the reserved lock, unlock it to restore
26313       ** the original state */
26314       lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0);
26315     } else {
26316       /* if we failed to get the lock then someone else must have it */
26317       reserved = 1;
26318     }
26319     if( IS_LOCK_ERROR(lrc) ){
26320       rc=lrc;
26321     }
26322   }
26323 
26324   unixLeaveMutex();
26325   OSTRACE(("TEST WR-LOCK %d %d %d (afp)\n", pFile->h, rc, reserved));
26326 
26327   *pResOut = reserved;
26328   return rc;
26329 }
26330 
26331 /*
26332 ** Lock the file with the lock specified by parameter eFileLock - one
26333 ** of the following:
26334 **
26335 **     (1) SHARED_LOCK
26336 **     (2) RESERVED_LOCK
26337 **     (3) PENDING_LOCK
26338 **     (4) EXCLUSIVE_LOCK
26339 **
26340 ** Sometimes when requesting one lock state, additional lock states
26341 ** are inserted in between.  The locking might fail on one of the later
26342 ** transitions leaving the lock state different from what it started but
26343 ** still short of its goal.  The following chart shows the allowed
26344 ** transitions and the inserted intermediate states:
26345 **
26346 **    UNLOCKED -> SHARED
26347 **    SHARED -> RESERVED
26348 **    SHARED -> (PENDING) -> EXCLUSIVE
26349 **    RESERVED -> (PENDING) -> EXCLUSIVE
26350 **    PENDING -> EXCLUSIVE
26351 **
26352 ** This routine will only increase a lock.  Use the sqlite3OsUnlock()
26353 ** routine to lower a locking level.
26354 */
26355 static int afpLock(sqlite3_file *id, int eFileLock){
26356   int rc = SQLITE_OK;
26357   unixFile *pFile = (unixFile*)id;
26358   unixInodeInfo *pInode = pFile->pInode;
26359   afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
26360 
26361   assert( pFile );
26362   OSTRACE(("LOCK    %d %s was %s(%s,%d) pid=%d (afp)\n", pFile->h,
26363            azFileLock(eFileLock), azFileLock(pFile->eFileLock),
26364            azFileLock(pInode->eFileLock), pInode->nShared , getpid()));
26365 
26366   /* If there is already a lock of this type or more restrictive on the
26367   ** unixFile, do nothing. Don't use the afp_end_lock: exit path, as
26368   ** unixEnterMutex() hasn't been called yet.
26369   */
26370   if( pFile->eFileLock>=eFileLock ){
26371     OSTRACE(("LOCK    %d %s ok (already held) (afp)\n", pFile->h,
26372            azFileLock(eFileLock)));
26373     return SQLITE_OK;
26374   }
26375 
26376   /* Make sure the locking sequence is correct
26377   **  (1) We never move from unlocked to anything higher than shared lock.
26378   **  (2) SQLite never explicitly requests a pendig lock.
26379   **  (3) A shared lock is always held when a reserve lock is requested.
26380   */
26381   assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK );
26382   assert( eFileLock!=PENDING_LOCK );
26383   assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK );
26384 
26385   /* This mutex is needed because pFile->pInode is shared across threads
26386   */
26387   unixEnterMutex();
26388   pInode = pFile->pInode;
26389 
26390   /* If some thread using this PID has a lock via a different unixFile*
26391   ** handle that precludes the requested lock, return BUSY.
26392   */
26393   if( (pFile->eFileLock!=pInode->eFileLock &&
26394        (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK))
26395      ){
26396     rc = SQLITE_BUSY;
26397     goto afp_end_lock;
26398   }
26399 
26400   /* If a SHARED lock is requested, and some thread using this PID already
26401   ** has a SHARED or RESERVED lock, then increment reference counts and
26402   ** return SQLITE_OK.
26403   */
26404   if( eFileLock==SHARED_LOCK &&
26405      (pInode->eFileLock==SHARED_LOCK || pInode->eFileLock==RESERVED_LOCK) ){
26406     assert( eFileLock==SHARED_LOCK );
26407     assert( pFile->eFileLock==0 );
26408     assert( pInode->nShared>0 );
26409     pFile->eFileLock = SHARED_LOCK;
26410     pInode->nShared++;
26411     pInode->nLock++;
26412     goto afp_end_lock;
26413   }
26414 
26415   /* A PENDING lock is needed before acquiring a SHARED lock and before
26416   ** acquiring an EXCLUSIVE lock.  For the SHARED lock, the PENDING will
26417   ** be released.
26418   */
26419   if( eFileLock==SHARED_LOCK
26420       || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock<PENDING_LOCK)
26421   ){
26422     int failed;
26423     failed = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 1);
26424     if (failed) {
26425       rc = failed;
26426       goto afp_end_lock;
26427     }
26428   }
26429 
26430   /* If control gets to this point, then actually go ahead and make
26431   ** operating system calls for the specified lock.
26432   */
26433   if( eFileLock==SHARED_LOCK ){
26434     int lrc1, lrc2, lrc1Errno = 0;
26435     long lk, mask;
26436 
26437     assert( pInode->nShared==0 );
26438     assert( pInode->eFileLock==0 );
26439 
26440     mask = (sizeof(long)==8) ? LARGEST_INT64 : 0x7fffffff;
26441     /* Now get the read-lock SHARED_LOCK */
26442     /* note that the quality of the randomness doesn't matter that much */
26443     lk = random();
26444     pInode->sharedByte = (lk & mask)%(SHARED_SIZE - 1);
26445     lrc1 = afpSetLock(context->dbPath, pFile,
26446           SHARED_FIRST+pInode->sharedByte, 1, 1);
26447     if( IS_LOCK_ERROR(lrc1) ){
26448       lrc1Errno = pFile->lastErrno;
26449     }
26450     /* Drop the temporary PENDING lock */
26451     lrc2 = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0);
26452 
26453     if( IS_LOCK_ERROR(lrc1) ) {
26454       pFile->lastErrno = lrc1Errno;
26455       rc = lrc1;
26456       goto afp_end_lock;
26457     } else if( IS_LOCK_ERROR(lrc2) ){
26458       rc = lrc2;
26459       goto afp_end_lock;
26460     } else if( lrc1 != SQLITE_OK ) {
26461       rc = lrc1;
26462     } else {
26463       pFile->eFileLock = SHARED_LOCK;
26464       pInode->nLock++;
26465       pInode->nShared = 1;
26466     }
26467   }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){
26468     /* We are trying for an exclusive lock but another thread in this
26469      ** same process is still holding a shared lock. */
26470     rc = SQLITE_BUSY;
26471   }else{
26472     /* The request was for a RESERVED or EXCLUSIVE lock.  It is
26473     ** assumed that there is a SHARED or greater lock on the file
26474     ** already.
26475     */
26476     int failed = 0;
26477     assert( 0!=pFile->eFileLock );
26478     if (eFileLock >= RESERVED_LOCK && pFile->eFileLock < RESERVED_LOCK) {
26479         /* Acquire a RESERVED lock */
26480         failed = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1);
26481       if( !failed ){
26482         context->reserved = 1;
26483       }
26484     }
26485     if (!failed && eFileLock == EXCLUSIVE_LOCK) {
26486       /* Acquire an EXCLUSIVE lock */
26487 
26488       /* Remove the shared lock before trying the range.  we'll need to
26489       ** reestablish the shared lock if we can't get the  afpUnlock
26490       */
26491       if( !(failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST +
26492                          pInode->sharedByte, 1, 0)) ){
26493         int failed2 = SQLITE_OK;
26494         /* now attemmpt to get the exclusive lock range */
26495         failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST,
26496                                SHARED_SIZE, 1);
26497         if( failed && (failed2 = afpSetLock(context->dbPath, pFile,
26498                        SHARED_FIRST + pInode->sharedByte, 1, 1)) ){
26499           /* Can't reestablish the shared lock.  Sqlite can't deal, this is
26500           ** a critical I/O error
26501           */
26502           rc = ((failed & SQLITE_IOERR) == SQLITE_IOERR) ? failed2 :
26503                SQLITE_IOERR_LOCK;
26504           goto afp_end_lock;
26505         }
26506       }else{
26507         rc = failed;
26508       }
26509     }
26510     if( failed ){
26511       rc = failed;
26512     }
26513   }
26514 
26515   if( rc==SQLITE_OK ){
26516     pFile->eFileLock = eFileLock;
26517     pInode->eFileLock = eFileLock;
26518   }else if( eFileLock==EXCLUSIVE_LOCK ){
26519     pFile->eFileLock = PENDING_LOCK;
26520     pInode->eFileLock = PENDING_LOCK;
26521   }
26522 
26523 afp_end_lock:
26524   unixLeaveMutex();
26525   OSTRACE(("LOCK    %d %s %s (afp)\n", pFile->h, azFileLock(eFileLock),
26526          rc==SQLITE_OK ? "ok" : "failed"));
26527   return rc;
26528 }
26529 
26530 /*
26531 ** Lower the locking level on file descriptor pFile to eFileLock.  eFileLock
26532 ** must be either NO_LOCK or SHARED_LOCK.
26533 **
26534 ** If the locking level of the file descriptor is already at or below
26535 ** the requested locking level, this routine is a no-op.
26536 */
26537 static int afpUnlock(sqlite3_file *id, int eFileLock) {
26538   int rc = SQLITE_OK;
26539   unixFile *pFile = (unixFile*)id;
26540   unixInodeInfo *pInode;
26541   afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
26542   int skipShared = 0;
26543 #ifdef SQLITE_TEST
26544   int h = pFile->h;
26545 #endif
26546 
26547   assert( pFile );
26548   OSTRACE(("UNLOCK  %d %d was %d(%d,%d) pid=%d (afp)\n", pFile->h, eFileLock,
26549            pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared,
26550            getpid()));
26551 
26552   assert( eFileLock<=SHARED_LOCK );
26553   if( pFile->eFileLock<=eFileLock ){
26554     return SQLITE_OK;
26555   }
26556   unixEnterMutex();
26557   pInode = pFile->pInode;
26558   assert( pInode->nShared!=0 );
26559   if( pFile->eFileLock>SHARED_LOCK ){
26560     assert( pInode->eFileLock==pFile->eFileLock );
26561     SimulateIOErrorBenign(1);
26562     SimulateIOError( h=(-1) )
26563     SimulateIOErrorBenign(0);
26564 
26565 #ifdef SQLITE_DEBUG
26566     /* When reducing a lock such that other processes can start
26567     ** reading the database file again, make sure that the
26568     ** transaction counter was updated if any part of the database
26569     ** file changed.  If the transaction counter is not updated,
26570     ** other connections to the same file might not realize that
26571     ** the file has changed and hence might not know to flush their
26572     ** cache.  The use of a stale cache can lead to database corruption.
26573     */
26574     assert( pFile->inNormalWrite==0
26575            || pFile->dbUpdate==0
26576            || pFile->transCntrChng==1 );
26577     pFile->inNormalWrite = 0;
26578 #endif
26579 
26580     if( pFile->eFileLock==EXCLUSIVE_LOCK ){
26581       rc = afpSetLock(context->dbPath, pFile, SHARED_FIRST, SHARED_SIZE, 0);
26582       if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1) ){
26583         /* only re-establish the shared lock if necessary */
26584         int sharedLockByte = SHARED_FIRST+pInode->sharedByte;
26585         rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 1);
26586       } else {
26587         skipShared = 1;
26588       }
26589     }
26590     if( rc==SQLITE_OK && pFile->eFileLock>=PENDING_LOCK ){
26591       rc = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0);
26592     }
26593     if( rc==SQLITE_OK && pFile->eFileLock>=RESERVED_LOCK && context->reserved ){
26594       rc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0);
26595       if( !rc ){
26596         context->reserved = 0;
26597       }
26598     }
26599     if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1)){
26600       pInode->eFileLock = SHARED_LOCK;
26601     }
26602   }
26603   if( rc==SQLITE_OK && eFileLock==NO_LOCK ){
26604 
26605     /* Decrement the shared lock counter.  Release the lock using an
26606     ** OS call only when all threads in this same process have released
26607     ** the lock.
26608     */
26609     unsigned long long sharedLockByte = SHARED_FIRST+pInode->sharedByte;
26610     pInode->nShared--;
26611     if( pInode->nShared==0 ){
26612       SimulateIOErrorBenign(1);
26613       SimulateIOError( h=(-1) )
26614       SimulateIOErrorBenign(0);
26615       if( !skipShared ){
26616         rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 0);
26617       }
26618       if( !rc ){
26619         pInode->eFileLock = NO_LOCK;
26620         pFile->eFileLock = NO_LOCK;
26621       }
26622     }
26623     if( rc==SQLITE_OK ){
26624       pInode->nLock--;
26625       assert( pInode->nLock>=0 );
26626       if( pInode->nLock==0 ){
26627         closePendingFds(pFile);
26628       }
26629     }
26630   }
26631 
26632   unixLeaveMutex();
26633   if( rc==SQLITE_OK ) pFile->eFileLock = eFileLock;
26634   return rc;
26635 }
26636 
26637 /*
26638 ** Close a file & cleanup AFP specific locking context
26639 */
26640 static int afpClose(sqlite3_file *id) {
26641   int rc = SQLITE_OK;
26642   if( id ){
26643     unixFile *pFile = (unixFile*)id;
26644     afpUnlock(id, NO_LOCK);
26645     unixEnterMutex();
26646     if( pFile->pInode && pFile->pInode->nLock ){
26647       /* If there are outstanding locks, do not actually close the file just
26648       ** yet because that would clear those locks.  Instead, add the file
26649       ** descriptor to pInode->aPending.  It will be automatically closed when
26650       ** the last lock is cleared.
26651       */
26652       setPendingFd(pFile);
26653     }
26654     releaseInodeInfo(pFile);
26655     sqlite3_free(pFile->lockingContext);
26656     rc = closeUnixFile(id);
26657     unixLeaveMutex();
26658   }
26659   return rc;
26660 }
26661 
26662 #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
26663 /*
26664 ** The code above is the AFP lock implementation.  The code is specific
26665 ** to MacOSX and does not work on other unix platforms.  No alternative
26666 ** is available.  If you don't compile for a mac, then the "unix-afp"
26667 ** VFS is not available.
26668 **
26669 ********************* End of the AFP lock implementation **********************
26670 ******************************************************************************/
26671 
26672 /******************************************************************************
26673 *************************** Begin NFS Locking ********************************/
26674 
26675 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
26676 /*
26677  ** Lower the locking level on file descriptor pFile to eFileLock.  eFileLock
26678  ** must be either NO_LOCK or SHARED_LOCK.
26679  **
26680  ** If the locking level of the file descriptor is already at or below
26681  ** the requested locking level, this routine is a no-op.
26682  */
26683 static int nfsUnlock(sqlite3_file *id, int eFileLock){
26684   return posixUnlock(id, eFileLock, 1);
26685 }
26686 
26687 #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
26688 /*
26689 ** The code above is the NFS lock implementation.  The code is specific
26690 ** to MacOSX and does not work on other unix platforms.  No alternative
26691 ** is available.
26692 **
26693 ********************* End of the NFS lock implementation **********************
26694 ******************************************************************************/
26695 
26696 /******************************************************************************
26697 **************** Non-locking sqlite3_file methods *****************************
26698 **
26699 ** The next division contains implementations for all methods of the
26700 ** sqlite3_file object other than the locking methods.  The locking
26701 ** methods were defined in divisions above (one locking method per
26702 ** division).  Those methods that are common to all locking modes
26703 ** are gather together into this division.
26704 */
26705 
26706 /*
26707 ** Seek to the offset passed as the second argument, then read cnt
26708 ** bytes into pBuf. Return the number of bytes actually read.
26709 **
26710 ** NB:  If you define USE_PREAD or USE_PREAD64, then it might also
26711 ** be necessary to define _XOPEN_SOURCE to be 500.  This varies from
26712 ** one system to another.  Since SQLite does not define USE_PREAD
26713 ** any any form by default, we will not attempt to define _XOPEN_SOURCE.
26714 ** See tickets #2741 and #2681.
26715 **
26716 ** To avoid stomping the errno value on a failed read the lastErrno value
26717 ** is set before returning.
26718 */
26719 static int seekAndRead(unixFile *id, sqlite3_int64 offset, void *pBuf, int cnt){
26720   int got;
26721   int prior = 0;
26722 #if (!defined(USE_PREAD) && !defined(USE_PREAD64))
26723   i64 newOffset;
26724 #endif
26725   TIMER_START;
26726   assert( cnt==(cnt&0x1ffff) );
26727   assert( id->h>2 );
26728   cnt &= 0x1ffff;
26729   do{
26730 #if defined(USE_PREAD)
26731     got = osPread(id->h, pBuf, cnt, offset);
26732     SimulateIOError( got = -1 );
26733 #elif defined(USE_PREAD64)
26734     got = osPread64(id->h, pBuf, cnt, offset);
26735     SimulateIOError( got = -1 );
26736 #else
26737     newOffset = lseek(id->h, offset, SEEK_SET);
26738     SimulateIOError( newOffset-- );
26739     if( newOffset!=offset ){
26740       if( newOffset == -1 ){
26741         ((unixFile*)id)->lastErrno = errno;
26742       }else{
26743         ((unixFile*)id)->lastErrno = 0;
26744       }
26745       return -1;
26746     }
26747     got = osRead(id->h, pBuf, cnt);
26748 #endif
26749     if( got==cnt ) break;
26750     if( got<0 ){
26751       if( errno==EINTR ){ got = 1; continue; }
26752       prior = 0;
26753       ((unixFile*)id)->lastErrno = errno;
26754       break;
26755     }else if( got>0 ){
26756       cnt -= got;
26757       offset += got;
26758       prior += got;
26759       pBuf = (void*)(got + (char*)pBuf);
26760     }
26761   }while( got>0 );
26762   TIMER_END;
26763   OSTRACE(("READ    %-3d %5d %7lld %llu\n",
26764             id->h, got+prior, offset-prior, TIMER_ELAPSED));
26765   return got+prior;
26766 }
26767 
26768 /*
26769 ** Read data from a file into a buffer.  Return SQLITE_OK if all
26770 ** bytes were read successfully and SQLITE_IOERR if anything goes
26771 ** wrong.
26772 */
26773 static int unixRead(
26774   sqlite3_file *id,
26775   void *pBuf,
26776   int amt,
26777   sqlite3_int64 offset
26778 ){
26779   unixFile *pFile = (unixFile *)id;
26780   int got;
26781   assert( id );
26782   assert( offset>=0 );
26783   assert( amt>0 );
26784 
26785   /* If this is a database file (not a journal, master-journal or temp
26786   ** file), the bytes in the locking range should never be read or written. */
26787 #if 0
26788   assert( pFile->pUnused==0
26789        || offset>=PENDING_BYTE+512
26790        || offset+amt<=PENDING_BYTE
26791   );
26792 #endif
26793 
26794 #if SQLITE_MAX_MMAP_SIZE>0
26795   /* Deal with as much of this read request as possible by transfering
26796   ** data from the memory mapping using memcpy().  */
26797   if( offset<pFile->mmapSize ){
26798     if( offset+amt <= pFile->mmapSize ){
26799       memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], amt);
26800       return SQLITE_OK;
26801     }else{
26802       int nCopy = pFile->mmapSize - offset;
26803       memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], nCopy);
26804       pBuf = &((u8 *)pBuf)[nCopy];
26805       amt -= nCopy;
26806       offset += nCopy;
26807     }
26808   }
26809 #endif
26810 
26811   got = seekAndRead(pFile, offset, pBuf, amt);
26812   if( got==amt ){
26813     return SQLITE_OK;
26814   }else if( got<0 ){
26815     /* lastErrno set by seekAndRead */
26816     return SQLITE_IOERR_READ;
26817   }else{
26818     pFile->lastErrno = 0; /* not a system error */
26819     /* Unread parts of the buffer must be zero-filled */
26820     memset(&((char*)pBuf)[got], 0, amt-got);
26821     return SQLITE_IOERR_SHORT_READ;
26822   }
26823 }
26824 
26825 /*
26826 ** Attempt to seek the file-descriptor passed as the first argument to
26827 ** absolute offset iOff, then attempt to write nBuf bytes of data from
26828 ** pBuf to it. If an error occurs, return -1 and set *piErrno. Otherwise,
26829 ** return the actual number of bytes written (which may be less than
26830 ** nBuf).
26831 */
26832 static int seekAndWriteFd(
26833   int fd,                         /* File descriptor to write to */
26834   i64 iOff,                       /* File offset to begin writing at */
26835   const void *pBuf,               /* Copy data from this buffer to the file */
26836   int nBuf,                       /* Size of buffer pBuf in bytes */
26837   int *piErrno                    /* OUT: Error number if error occurs */
26838 ){
26839   int rc = 0;                     /* Value returned by system call */
26840 
26841   assert( nBuf==(nBuf&0x1ffff) );
26842   assert( fd>2 );
26843   nBuf &= 0x1ffff;
26844   TIMER_START;
26845 
26846 #if defined(USE_PREAD)
26847   do{ rc = osPwrite(fd, pBuf, nBuf, iOff); }while( rc<0 && errno==EINTR );
26848 #elif defined(USE_PREAD64)
26849   do{ rc = osPwrite64(fd, pBuf, nBuf, iOff);}while( rc<0 && errno==EINTR);
26850 #else
26851   do{
26852     i64 iSeek = lseek(fd, iOff, SEEK_SET);
26853     SimulateIOError( iSeek-- );
26854 
26855     if( iSeek!=iOff ){
26856       if( piErrno ) *piErrno = (iSeek==-1 ? errno : 0);
26857       return -1;
26858     }
26859     rc = osWrite(fd, pBuf, nBuf);
26860   }while( rc<0 && errno==EINTR );
26861 #endif
26862 
26863   TIMER_END;
26864   OSTRACE(("WRITE   %-3d %5d %7lld %llu\n", fd, rc, iOff, TIMER_ELAPSED));
26865 
26866   if( rc<0 && piErrno ) *piErrno = errno;
26867   return rc;
26868 }
26869 
26870 
26871 /*
26872 ** Seek to the offset in id->offset then read cnt bytes into pBuf.
26873 ** Return the number of bytes actually read.  Update the offset.
26874 **
26875 ** To avoid stomping the errno value on a failed write the lastErrno value
26876 ** is set before returning.
26877 */
26878 static int seekAndWrite(unixFile *id, i64 offset, const void *pBuf, int cnt){
26879   return seekAndWriteFd(id->h, offset, pBuf, cnt, &id->lastErrno);
26880 }
26881 
26882 
26883 /*
26884 ** Write data from a buffer into a file.  Return SQLITE_OK on success
26885 ** or some other error code on failure.
26886 */
26887 static int unixWrite(
26888   sqlite3_file *id,
26889   const void *pBuf,
26890   int amt,
26891   sqlite3_int64 offset
26892 ){
26893   unixFile *pFile = (unixFile*)id;
26894   int wrote = 0;
26895   assert( id );
26896   assert( amt>0 );
26897 
26898   /* If this is a database file (not a journal, master-journal or temp
26899   ** file), the bytes in the locking range should never be read or written. */
26900 #if 0
26901   assert( pFile->pUnused==0
26902        || offset>=PENDING_BYTE+512
26903        || offset+amt<=PENDING_BYTE
26904   );
26905 #endif
26906 
26907 #ifdef SQLITE_DEBUG
26908   /* If we are doing a normal write to a database file (as opposed to
26909   ** doing a hot-journal rollback or a write to some file other than a
26910   ** normal database file) then record the fact that the database
26911   ** has changed.  If the transaction counter is modified, record that
26912   ** fact too.
26913   */
26914   if( pFile->inNormalWrite ){
26915     pFile->dbUpdate = 1;  /* The database has been modified */
26916     if( offset<=24 && offset+amt>=27 ){
26917       int rc;
26918       char oldCntr[4];
26919       SimulateIOErrorBenign(1);
26920       rc = seekAndRead(pFile, 24, oldCntr, 4);
26921       SimulateIOErrorBenign(0);
26922       if( rc!=4 || memcmp(oldCntr, &((char*)pBuf)[24-offset], 4)!=0 ){
26923         pFile->transCntrChng = 1;  /* The transaction counter has changed */
26924       }
26925     }
26926   }
26927 #endif
26928 
26929 #if SQLITE_MAX_MMAP_SIZE>0
26930   /* Deal with as much of this write request as possible by transfering
26931   ** data from the memory mapping using memcpy().  */
26932   if( offset<pFile->mmapSize ){
26933     if( offset+amt <= pFile->mmapSize ){
26934       memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, amt);
26935       return SQLITE_OK;
26936     }else{
26937       int nCopy = pFile->mmapSize - offset;
26938       memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, nCopy);
26939       pBuf = &((u8 *)pBuf)[nCopy];
26940       amt -= nCopy;
26941       offset += nCopy;
26942     }
26943   }
26944 #endif
26945 
26946   while( amt>0 && (wrote = seekAndWrite(pFile, offset, pBuf, amt))>0 ){
26947     amt -= wrote;
26948     offset += wrote;
26949     pBuf = &((char*)pBuf)[wrote];
26950   }
26951   SimulateIOError(( wrote=(-1), amt=1 ));
26952   SimulateDiskfullError(( wrote=0, amt=1 ));
26953 
26954   if( amt>0 ){
26955     if( wrote<0 && pFile->lastErrno!=ENOSPC ){
26956       /* lastErrno set by seekAndWrite */
26957       return SQLITE_IOERR_WRITE;
26958     }else{
26959       pFile->lastErrno = 0; /* not a system error */
26960       return SQLITE_FULL;
26961     }
26962   }
26963 
26964   return SQLITE_OK;
26965 }
26966 
26967 #ifdef SQLITE_TEST
26968 /*
26969 ** Count the number of fullsyncs and normal syncs.  This is used to test
26970 ** that syncs and fullsyncs are occurring at the right times.
26971 */
26972 SQLITE_API int sqlite3_sync_count = 0;
26973 SQLITE_API int sqlite3_fullsync_count = 0;
26974 #endif
26975 
26976 /*
26977 ** We do not trust systems to provide a working fdatasync().  Some do.
26978 ** Others do no.  To be safe, we will stick with the (slightly slower)
26979 ** fsync(). If you know that your system does support fdatasync() correctly,
26980 ** then simply compile with -Dfdatasync=fdatasync
26981 */
26982 #if !defined(fdatasync)
26983 # define fdatasync fsync
26984 #endif
26985 
26986 /*
26987 ** Define HAVE_FULLFSYNC to 0 or 1 depending on whether or not
26988 ** the F_FULLFSYNC macro is defined.  F_FULLFSYNC is currently
26989 ** only available on Mac OS X.  But that could change.
26990 */
26991 #ifdef F_FULLFSYNC
26992 # define HAVE_FULLFSYNC 1
26993 #else
26994 # define HAVE_FULLFSYNC 0
26995 #endif
26996 
26997 
26998 /*
26999 ** The fsync() system call does not work as advertised on many
27000 ** unix systems.  The following procedure is an attempt to make
27001 ** it work better.
27002 **
27003 ** The SQLITE_NO_SYNC macro disables all fsync()s.  This is useful
27004 ** for testing when we want to run through the test suite quickly.
27005 ** You are strongly advised *not* to deploy with SQLITE_NO_SYNC
27006 ** enabled, however, since with SQLITE_NO_SYNC enabled, an OS crash
27007 ** or power failure will likely corrupt the database file.
27008 **
27009 ** SQLite sets the dataOnly flag if the size of the file is unchanged.
27010 ** The idea behind dataOnly is that it should only write the file content
27011 ** to disk, not the inode.  We only set dataOnly if the file size is
27012 ** unchanged since the file size is part of the inode.  However,
27013 ** Ted Ts'o tells us that fdatasync() will also write the inode if the
27014 ** file size has changed.  The only real difference between fdatasync()
27015 ** and fsync(), Ted tells us, is that fdatasync() will not flush the
27016 ** inode if the mtime or owner or other inode attributes have changed.
27017 ** We only care about the file size, not the other file attributes, so
27018 ** as far as SQLite is concerned, an fdatasync() is always adequate.
27019 ** So, we always use fdatasync() if it is available, regardless of
27020 ** the value of the dataOnly flag.
27021 */
27022 static int full_fsync(int fd, int fullSync, int dataOnly){
27023   int rc;
27024 
27025   /* The following "ifdef/elif/else/" block has the same structure as
27026   ** the one below. It is replicated here solely to avoid cluttering
27027   ** up the real code with the UNUSED_PARAMETER() macros.
27028   */
27029 #ifdef SQLITE_NO_SYNC
27030   UNUSED_PARAMETER(fd);
27031   UNUSED_PARAMETER(fullSync);
27032   UNUSED_PARAMETER(dataOnly);
27033 #elif HAVE_FULLFSYNC
27034   UNUSED_PARAMETER(dataOnly);
27035 #else
27036   UNUSED_PARAMETER(fullSync);
27037   UNUSED_PARAMETER(dataOnly);
27038 #endif
27039 
27040   /* Record the number of times that we do a normal fsync() and
27041   ** FULLSYNC.  This is used during testing to verify that this procedure
27042   ** gets called with the correct arguments.
27043   */
27044 #ifdef SQLITE_TEST
27045   if( fullSync ) sqlite3_fullsync_count++;
27046   sqlite3_sync_count++;
27047 #endif
27048 
27049   /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a
27050   ** no-op
27051   */
27052 #ifdef SQLITE_NO_SYNC
27053   rc = SQLITE_OK;
27054 #elif HAVE_FULLFSYNC
27055   if( fullSync ){
27056     rc = osFcntl(fd, F_FULLFSYNC, 0);
27057   }else{
27058     rc = 1;
27059   }
27060   /* If the FULLFSYNC failed, fall back to attempting an fsync().
27061   ** It shouldn't be possible for fullfsync to fail on the local
27062   ** file system (on OSX), so failure indicates that FULLFSYNC
27063   ** isn't supported for this file system. So, attempt an fsync
27064   ** and (for now) ignore the overhead of a superfluous fcntl call.
27065   ** It'd be better to detect fullfsync support once and avoid
27066   ** the fcntl call every time sync is called.
27067   */
27068   if( rc ) rc = fsync(fd);
27069 
27070 #elif defined(__APPLE__)
27071   /* fdatasync() on HFS+ doesn't yet flush the file size if it changed correctly
27072   ** so currently we default to the macro that redefines fdatasync to fsync
27073   */
27074   rc = fsync(fd);
27075 #else
27076   rc = fdatasync(fd);
27077 #if OS_VXWORKS
27078   if( rc==-1 && errno==ENOTSUP ){
27079     rc = fsync(fd);
27080   }
27081 #endif /* OS_VXWORKS */
27082 #endif /* ifdef SQLITE_NO_SYNC elif HAVE_FULLFSYNC */
27083 
27084   if( OS_VXWORKS && rc!= -1 ){
27085     rc = 0;
27086   }
27087   return rc;
27088 }
27089 
27090 /*
27091 ** Open a file descriptor to the directory containing file zFilename.
27092 ** If successful, *pFd is set to the opened file descriptor and
27093 ** SQLITE_OK is returned. If an error occurs, either SQLITE_NOMEM
27094 ** or SQLITE_CANTOPEN is returned and *pFd is set to an undefined
27095 ** value.
27096 **
27097 ** The directory file descriptor is used for only one thing - to
27098 ** fsync() a directory to make sure file creation and deletion events
27099 ** are flushed to disk.  Such fsyncs are not needed on newer
27100 ** journaling filesystems, but are required on older filesystems.
27101 **
27102 ** This routine can be overridden using the xSetSysCall interface.
27103 ** The ability to override this routine was added in support of the
27104 ** chromium sandbox.  Opening a directory is a security risk (we are
27105 ** told) so making it overrideable allows the chromium sandbox to
27106 ** replace this routine with a harmless no-op.  To make this routine
27107 ** a no-op, replace it with a stub that returns SQLITE_OK but leaves
27108 ** *pFd set to a negative number.
27109 **
27110 ** If SQLITE_OK is returned, the caller is responsible for closing
27111 ** the file descriptor *pFd using close().
27112 */
27113 static int openDirectory(const char *zFilename, int *pFd){
27114   int ii;
27115   int fd = -1;
27116   char zDirname[MAX_PATHNAME+1];
27117 
27118   sqlite3_snprintf(MAX_PATHNAME, zDirname, "%s", zFilename);
27119   for(ii=(int)strlen(zDirname); ii>1 && zDirname[ii]!='/'; ii--);
27120   if( ii>0 ){
27121     zDirname[ii] = '\0';
27122     fd = robust_open(zDirname, O_RDONLY|O_BINARY, 0);
27123     if( fd>=0 ){
27124       OSTRACE(("OPENDIR %-3d %s\n", fd, zDirname));
27125     }
27126   }
27127   *pFd = fd;
27128   return (fd>=0?SQLITE_OK:unixLogError(SQLITE_CANTOPEN_BKPT, "open", zDirname));
27129 }
27130 
27131 /*
27132 ** Make sure all writes to a particular file are committed to disk.
27133 **
27134 ** If dataOnly==0 then both the file itself and its metadata (file
27135 ** size, access time, etc) are synced.  If dataOnly!=0 then only the
27136 ** file data is synced.
27137 **
27138 ** Under Unix, also make sure that the directory entry for the file
27139 ** has been created by fsync-ing the directory that contains the file.
27140 ** If we do not do this and we encounter a power failure, the directory
27141 ** entry for the journal might not exist after we reboot.  The next
27142 ** SQLite to access the file will not know that the journal exists (because
27143 ** the directory entry for the journal was never created) and the transaction
27144 ** will not roll back - possibly leading to database corruption.
27145 */
27146 static int unixSync(sqlite3_file *id, int flags){
27147   int rc;
27148   unixFile *pFile = (unixFile*)id;
27149 
27150   int isDataOnly = (flags&SQLITE_SYNC_DATAONLY);
27151   int isFullsync = (flags&0x0F)==SQLITE_SYNC_FULL;
27152 
27153   /* Check that one of SQLITE_SYNC_NORMAL or FULL was passed */
27154   assert((flags&0x0F)==SQLITE_SYNC_NORMAL
27155       || (flags&0x0F)==SQLITE_SYNC_FULL
27156   );
27157 
27158   /* Unix cannot, but some systems may return SQLITE_FULL from here. This
27159   ** line is to test that doing so does not cause any problems.
27160   */
27161   SimulateDiskfullError( return SQLITE_FULL );
27162 
27163   assert( pFile );
27164   OSTRACE(("SYNC    %-3d\n", pFile->h));
27165   rc = full_fsync(pFile->h, isFullsync, isDataOnly);
27166   SimulateIOError( rc=1 );
27167   if( rc ){
27168     pFile->lastErrno = errno;
27169     return unixLogError(SQLITE_IOERR_FSYNC, "full_fsync", pFile->zPath);
27170   }
27171 
27172   /* Also fsync the directory containing the file if the DIRSYNC flag
27173   ** is set.  This is a one-time occurrence.  Many systems (examples: AIX)
27174   ** are unable to fsync a directory, so ignore errors on the fsync.
27175   */
27176   if( pFile->ctrlFlags & UNIXFILE_DIRSYNC ){
27177     int dirfd;
27178     OSTRACE(("DIRSYNC %s (have_fullfsync=%d fullsync=%d)\n", pFile->zPath,
27179             HAVE_FULLFSYNC, isFullsync));
27180     rc = osOpenDirectory(pFile->zPath, &dirfd);
27181     if( rc==SQLITE_OK && dirfd>=0 ){
27182       full_fsync(dirfd, 0, 0);
27183       robust_close(pFile, dirfd, __LINE__);
27184     }else if( rc==SQLITE_CANTOPEN ){
27185       rc = SQLITE_OK;
27186     }
27187     pFile->ctrlFlags &= ~UNIXFILE_DIRSYNC;
27188   }
27189   return rc;
27190 }
27191 
27192 /*
27193 ** Truncate an open file to a specified size
27194 */
27195 static int unixTruncate(sqlite3_file *id, i64 nByte){
27196   unixFile *pFile = (unixFile *)id;
27197   int rc;
27198   assert( pFile );
27199   SimulateIOError( return SQLITE_IOERR_TRUNCATE );
27200 
27201   /* If the user has configured a chunk-size for this file, truncate the
27202   ** file so that it consists of an integer number of chunks (i.e. the
27203   ** actual file size after the operation may be larger than the requested
27204   ** size).
27205   */
27206   if( pFile->szChunk>0 ){
27207     nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
27208   }
27209 
27210   rc = robust_ftruncate(pFile->h, (off_t)nByte);
27211   if( rc ){
27212     pFile->lastErrno = errno;
27213     return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath);
27214   }else{
27215 #ifdef SQLITE_DEBUG
27216     /* If we are doing a normal write to a database file (as opposed to
27217     ** doing a hot-journal rollback or a write to some file other than a
27218     ** normal database file) and we truncate the file to zero length,
27219     ** that effectively updates the change counter.  This might happen
27220     ** when restoring a database using the backup API from a zero-length
27221     ** source.
27222     */
27223     if( pFile->inNormalWrite && nByte==0 ){
27224       pFile->transCntrChng = 1;
27225     }
27226 #endif
27227 
27228 #if SQLITE_MAX_MMAP_SIZE>0
27229     /* If the file was just truncated to a size smaller than the currently
27230     ** mapped region, reduce the effective mapping size as well. SQLite will
27231     ** use read() and write() to access data beyond this point from now on.
27232     */
27233     if( nByte<pFile->mmapSize ){
27234       pFile->mmapSize = nByte;
27235     }
27236 #endif
27237 
27238     return SQLITE_OK;
27239   }
27240 }
27241 
27242 /*
27243 ** Determine the current size of a file in bytes
27244 */
27245 static int unixFileSize(sqlite3_file *id, i64 *pSize){
27246   int rc;
27247   struct stat buf;
27248   assert( id );
27249   rc = osFstat(((unixFile*)id)->h, &buf);
27250   SimulateIOError( rc=1 );
27251   if( rc!=0 ){
27252     ((unixFile*)id)->lastErrno = errno;
27253     return SQLITE_IOERR_FSTAT;
27254   }
27255   *pSize = buf.st_size;
27256 
27257   /* When opening a zero-size database, the findInodeInfo() procedure
27258   ** writes a single byte into that file in order to work around a bug
27259   ** in the OS-X msdos filesystem.  In order to avoid problems with upper
27260   ** layers, we need to report this file size as zero even though it is
27261   ** really 1.   Ticket #3260.
27262   */
27263   if( *pSize==1 ) *pSize = 0;
27264 
27265 
27266   return SQLITE_OK;
27267 }
27268 
27269 #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
27270 /*
27271 ** Handler for proxy-locking file-control verbs.  Defined below in the
27272 ** proxying locking division.
27273 */
27274 static int proxyFileControl(sqlite3_file*,int,void*);
27275 #endif
27276 
27277 /*
27278 ** This function is called to handle the SQLITE_FCNTL_SIZE_HINT
27279 ** file-control operation.  Enlarge the database to nBytes in size
27280 ** (rounded up to the next chunk-size).  If the database is already
27281 ** nBytes or larger, this routine is a no-op.
27282 */
27283 static int fcntlSizeHint(unixFile *pFile, i64 nByte){
27284   if( pFile->szChunk>0 ){
27285     i64 nSize;                    /* Required file size */
27286     struct stat buf;              /* Used to hold return values of fstat() */
27287 
27288     if( osFstat(pFile->h, &buf) ) return SQLITE_IOERR_FSTAT;
27289 
27290     nSize = ((nByte+pFile->szChunk-1) / pFile->szChunk) * pFile->szChunk;
27291     if( nSize>(i64)buf.st_size ){
27292 
27293 #if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE
27294       /* The code below is handling the return value of osFallocate()
27295       ** correctly. posix_fallocate() is defined to "returns zero on success,
27296       ** or an error number on  failure". See the manpage for details. */
27297       int err;
27298       do{
27299         err = osFallocate(pFile->h, buf.st_size, nSize-buf.st_size);
27300       }while( err==EINTR );
27301       if( err ) return SQLITE_IOERR_WRITE;
27302 #else
27303       /* If the OS does not have posix_fallocate(), fake it. First use
27304       ** ftruncate() to set the file size, then write a single byte to
27305       ** the last byte in each block within the extended region. This
27306       ** is the same technique used by glibc to implement posix_fallocate()
27307       ** on systems that do not have a real fallocate() system call.
27308       */
27309       int nBlk = buf.st_blksize;  /* File-system block size */
27310       i64 iWrite;                 /* Next offset to write to */
27311 
27312       if( robust_ftruncate(pFile->h, nSize) ){
27313         pFile->lastErrno = errno;
27314         return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath);
27315       }
27316       iWrite = ((buf.st_size + 2*nBlk - 1)/nBlk)*nBlk-1;
27317       while( iWrite<nSize ){
27318         int nWrite = seekAndWrite(pFile, iWrite, "", 1);
27319         if( nWrite!=1 ) return SQLITE_IOERR_WRITE;
27320         iWrite += nBlk;
27321       }
27322 #endif
27323     }
27324   }
27325 
27326 #if SQLITE_MAX_MMAP_SIZE>0
27327   if( pFile->mmapSizeMax>0 && nByte>pFile->mmapSize ){
27328     int rc;
27329     if( pFile->szChunk<=0 ){
27330       if( robust_ftruncate(pFile->h, nByte) ){
27331         pFile->lastErrno = errno;
27332         return unixLogError(SQLITE_IOERR_TRUNCATE, "ftruncate", pFile->zPath);
27333       }
27334     }
27335 
27336     rc = unixMapfile(pFile, nByte);
27337     return rc;
27338   }
27339 #endif
27340 
27341   return SQLITE_OK;
27342 }
27343 
27344 /*
27345 ** If *pArg is inititially negative then this is a query.  Set *pArg to
27346 ** 1 or 0 depending on whether or not bit mask of pFile->ctrlFlags is set.
27347 **
27348 ** If *pArg is 0 or 1, then clear or set the mask bit of pFile->ctrlFlags.
27349 */
27350 static void unixModeBit(unixFile *pFile, unsigned char mask, int *pArg){
27351   if( *pArg<0 ){
27352     *pArg = (pFile->ctrlFlags & mask)!=0;
27353   }else if( (*pArg)==0 ){
27354     pFile->ctrlFlags &= ~mask;
27355   }else{
27356     pFile->ctrlFlags |= mask;
27357   }
27358 }
27359 
27360 /* Forward declaration */
27361 static int unixGetTempname(int nBuf, char *zBuf);
27362 
27363 /*
27364 ** Information and control of an open file handle.
27365 */
27366 static int unixFileControl(sqlite3_file *id, int op, void *pArg){
27367   unixFile *pFile = (unixFile*)id;
27368   switch( op ){
27369     case SQLITE_FCNTL_LOCKSTATE: {
27370       *(int*)pArg = pFile->eFileLock;
27371       return SQLITE_OK;
27372     }
27373     case SQLITE_LAST_ERRNO: {
27374       *(int*)pArg = pFile->lastErrno;
27375       return SQLITE_OK;
27376     }
27377     case SQLITE_FCNTL_CHUNK_SIZE: {
27378       pFile->szChunk = *(int *)pArg;
27379       return SQLITE_OK;
27380     }
27381     case SQLITE_FCNTL_SIZE_HINT: {
27382       int rc;
27383       SimulateIOErrorBenign(1);
27384       rc = fcntlSizeHint(pFile, *(i64 *)pArg);
27385       SimulateIOErrorBenign(0);
27386       return rc;
27387     }
27388     case SQLITE_FCNTL_PERSIST_WAL: {
27389       unixModeBit(pFile, UNIXFILE_PERSIST_WAL, (int*)pArg);
27390       return SQLITE_OK;
27391     }
27392     case SQLITE_FCNTL_POWERSAFE_OVERWRITE: {
27393       unixModeBit(pFile, UNIXFILE_PSOW, (int*)pArg);
27394       return SQLITE_OK;
27395     }
27396     case SQLITE_FCNTL_VFSNAME: {
27397       *(char**)pArg = sqlite3_mprintf("%s", pFile->pVfs->zName);
27398       return SQLITE_OK;
27399     }
27400     case SQLITE_FCNTL_TEMPFILENAME: {
27401       char *zTFile = sqlite3_malloc( pFile->pVfs->mxPathname );
27402       if( zTFile ){
27403         unixGetTempname(pFile->pVfs->mxPathname, zTFile);
27404         *(char**)pArg = zTFile;
27405       }
27406       return SQLITE_OK;
27407     }
27408     case SQLITE_FCNTL_HAS_MOVED: {
27409       *(int*)pArg = fileHasMoved(pFile);
27410       return SQLITE_OK;
27411     }
27412 #if SQLITE_MAX_MMAP_SIZE>0
27413     case SQLITE_FCNTL_MMAP_SIZE: {
27414       i64 newLimit = *(i64*)pArg;
27415       int rc = SQLITE_OK;
27416       if( newLimit>sqlite3GlobalConfig.mxMmap ){
27417         newLimit = sqlite3GlobalConfig.mxMmap;
27418       }
27419       *(i64*)pArg = pFile->mmapSizeMax;
27420       if( newLimit>=0 && newLimit!=pFile->mmapSizeMax && pFile->nFetchOut==0 ){
27421         pFile->mmapSizeMax = newLimit;
27422         if( pFile->mmapSize>0 ){
27423           unixUnmapfile(pFile);
27424           rc = unixMapfile(pFile, -1);
27425         }
27426       }
27427       return rc;
27428     }
27429 #endif
27430 #ifdef SQLITE_DEBUG
27431     /* The pager calls this method to signal that it has done
27432     ** a rollback and that the database is therefore unchanged and
27433     ** it hence it is OK for the transaction change counter to be
27434     ** unchanged.
27435     */
27436     case SQLITE_FCNTL_DB_UNCHANGED: {
27437       ((unixFile*)id)->dbUpdate = 0;
27438       return SQLITE_OK;
27439     }
27440 #endif
27441 #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
27442     case SQLITE_SET_LOCKPROXYFILE:
27443     case SQLITE_GET_LOCKPROXYFILE: {
27444       return proxyFileControl(id,op,pArg);
27445     }
27446 #endif /* SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) */
27447   }
27448   return SQLITE_NOTFOUND;
27449 }
27450 
27451 /*
27452 ** Return the sector size in bytes of the underlying block device for
27453 ** the specified file. This is almost always 512 bytes, but may be
27454 ** larger for some devices.
27455 **
27456 ** SQLite code assumes this function cannot fail. It also assumes that
27457 ** if two files are created in the same file-system directory (i.e.
27458 ** a database and its journal file) that the sector size will be the
27459 ** same for both.
27460 */
27461 #ifndef __QNXNTO__
27462 static int unixSectorSize(sqlite3_file *NotUsed){
27463   UNUSED_PARAMETER(NotUsed);
27464   return SQLITE_DEFAULT_SECTOR_SIZE;
27465 }
27466 #endif
27467 
27468 /*
27469 ** The following version of unixSectorSize() is optimized for QNX.
27470 */
27471 #ifdef __QNXNTO__
27472 #include <sys/dcmd_blk.h>
27473 #include <sys/statvfs.h>
27474 static int unixSectorSize(sqlite3_file *id){
27475   unixFile *pFile = (unixFile*)id;
27476   if( pFile->sectorSize == 0 ){
27477     struct statvfs fsInfo;
27478 
27479     /* Set defaults for non-supported filesystems */
27480     pFile->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE;
27481     pFile->deviceCharacteristics = 0;
27482     if( fstatvfs(pFile->h, &fsInfo) == -1 ) {
27483       return pFile->sectorSize;
27484     }
27485 
27486     if( !strcmp(fsInfo.f_basetype, "tmp") ) {
27487       pFile->sectorSize = fsInfo.f_bsize;
27488       pFile->deviceCharacteristics =
27489         SQLITE_IOCAP_ATOMIC4K |       /* All ram filesystem writes are atomic */
27490         SQLITE_IOCAP_SAFE_APPEND |    /* growing the file does not occur until
27491                                       ** the write succeeds */
27492         SQLITE_IOCAP_SEQUENTIAL |     /* The ram filesystem has no write behind
27493                                       ** so it is ordered */
27494         0;
27495     }else if( strstr(fsInfo.f_basetype, "etfs") ){
27496       pFile->sectorSize = fsInfo.f_bsize;
27497       pFile->deviceCharacteristics =
27498         /* etfs cluster size writes are atomic */
27499         (pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) |
27500         SQLITE_IOCAP_SAFE_APPEND |    /* growing the file does not occur until
27501                                       ** the write succeeds */
27502         SQLITE_IOCAP_SEQUENTIAL |     /* The ram filesystem has no write behind
27503                                       ** so it is ordered */
27504         0;
27505     }else if( !strcmp(fsInfo.f_basetype, "qnx6") ){
27506       pFile->sectorSize = fsInfo.f_bsize;
27507       pFile->deviceCharacteristics =
27508         SQLITE_IOCAP_ATOMIC |         /* All filesystem writes are atomic */
27509         SQLITE_IOCAP_SAFE_APPEND |    /* growing the file does not occur until
27510                                       ** the write succeeds */
27511         SQLITE_IOCAP_SEQUENTIAL |     /* The ram filesystem has no write behind
27512                                       ** so it is ordered */
27513         0;
27514     }else if( !strcmp(fsInfo.f_basetype, "qnx4") ){
27515       pFile->sectorSize = fsInfo.f_bsize;
27516       pFile->deviceCharacteristics =
27517         /* full bitset of atomics from max sector size and smaller */
27518         ((pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) << 1) - 2 |
27519         SQLITE_IOCAP_SEQUENTIAL |     /* The ram filesystem has no write behind
27520                                       ** so it is ordered */
27521         0;
27522     }else if( strstr(fsInfo.f_basetype, "dos") ){
27523       pFile->sectorSize = fsInfo.f_bsize;
27524       pFile->deviceCharacteristics =
27525         /* full bitset of atomics from max sector size and smaller */
27526         ((pFile->sectorSize / 512 * SQLITE_IOCAP_ATOMIC512) << 1) - 2 |
27527         SQLITE_IOCAP_SEQUENTIAL |     /* The ram filesystem has no write behind
27528                                       ** so it is ordered */
27529         0;
27530     }else{
27531       pFile->deviceCharacteristics =
27532         SQLITE_IOCAP_ATOMIC512 |      /* blocks are atomic */
27533         SQLITE_IOCAP_SAFE_APPEND |    /* growing the file does not occur until
27534                                       ** the write succeeds */
27535         0;
27536     }
27537   }
27538   /* Last chance verification.  If the sector size isn't a multiple of 512
27539   ** then it isn't valid.*/
27540   if( pFile->sectorSize % 512 != 0 ){
27541     pFile->deviceCharacteristics = 0;
27542     pFile->sectorSize = SQLITE_DEFAULT_SECTOR_SIZE;
27543   }
27544   return pFile->sectorSize;
27545 }
27546 #endif /* __QNXNTO__ */
27547 
27548 /*
27549 ** Return the device characteristics for the file.
27550 **
27551 ** This VFS is set up to return SQLITE_IOCAP_POWERSAFE_OVERWRITE by default.
27552 ** However, that choice is contraversial since technically the underlying
27553 ** file system does not always provide powersafe overwrites.  (In other
27554 ** words, after a power-loss event, parts of the file that were never
27555 ** written might end up being altered.)  However, non-PSOW behavior is very,
27556 ** very rare.  And asserting PSOW makes a large reduction in the amount
27557 ** of required I/O for journaling, since a lot of padding is eliminated.
27558 **  Hence, while POWERSAFE_OVERWRITE is on by default, there is a file-control
27559 ** available to turn it off and URI query parameter available to turn it off.
27560 */
27561 static int unixDeviceCharacteristics(sqlite3_file *id){
27562   unixFile *p = (unixFile*)id;
27563   int rc = 0;
27564 #ifdef __QNXNTO__
27565   if( p->sectorSize==0 ) unixSectorSize(id);
27566   rc = p->deviceCharacteristics;
27567 #endif
27568   if( p->ctrlFlags & UNIXFILE_PSOW ){
27569     rc |= SQLITE_IOCAP_POWERSAFE_OVERWRITE;
27570   }
27571   return rc;
27572 }
27573 
27574 #ifndef SQLITE_OMIT_WAL
27575 
27576 
27577 /*
27578 ** Object used to represent an shared memory buffer.
27579 **
27580 ** When multiple threads all reference the same wal-index, each thread
27581 ** has its own unixShm object, but they all point to a single instance
27582 ** of this unixShmNode object.  In other words, each wal-index is opened
27583 ** only once per process.
27584 **
27585 ** Each unixShmNode object is connected to a single unixInodeInfo object.
27586 ** We could coalesce this object into unixInodeInfo, but that would mean
27587 ** every open file that does not use shared memory (in other words, most
27588 ** open files) would have to carry around this extra information.  So
27589 ** the unixInodeInfo object contains a pointer to this unixShmNode object
27590 ** and the unixShmNode object is created only when needed.
27591 **
27592 ** unixMutexHeld() must be true when creating or destroying
27593 ** this object or while reading or writing the following fields:
27594 **
27595 **      nRef
27596 **
27597 ** The following fields are read-only after the object is created:
27598 **
27599 **      fid
27600 **      zFilename
27601 **
27602 ** Either unixShmNode.mutex must be held or unixShmNode.nRef==0 and
27603 ** unixMutexHeld() is true when reading or writing any other field
27604 ** in this structure.
27605 */
27606 struct unixShmNode {
27607   unixInodeInfo *pInode;     /* unixInodeInfo that owns this SHM node */
27608   sqlite3_mutex *mutex;      /* Mutex to access this object */
27609   char *zFilename;           /* Name of the mmapped file */
27610   int h;                     /* Open file descriptor */
27611   int szRegion;              /* Size of shared-memory regions */
27612   u16 nRegion;               /* Size of array apRegion */
27613   u8 isReadonly;             /* True if read-only */
27614   char **apRegion;           /* Array of mapped shared-memory regions */
27615   int nRef;                  /* Number of unixShm objects pointing to this */
27616   unixShm *pFirst;           /* All unixShm objects pointing to this */
27617 #ifdef SQLITE_DEBUG
27618   u8 exclMask;               /* Mask of exclusive locks held */
27619   u8 sharedMask;             /* Mask of shared locks held */
27620   u8 nextShmId;              /* Next available unixShm.id value */
27621 #endif
27622 };
27623 
27624 /*
27625 ** Structure used internally by this VFS to record the state of an
27626 ** open shared memory connection.
27627 **
27628 ** The following fields are initialized when this object is created and
27629 ** are read-only thereafter:
27630 **
27631 **    unixShm.pFile
27632 **    unixShm.id
27633 **
27634 ** All other fields are read/write.  The unixShm.pFile->mutex must be held
27635 ** while accessing any read/write fields.
27636 */
27637 struct unixShm {
27638   unixShmNode *pShmNode;     /* The underlying unixShmNode object */
27639   unixShm *pNext;            /* Next unixShm with the same unixShmNode */
27640   u8 hasMutex;               /* True if holding the unixShmNode mutex */
27641   u8 id;                     /* Id of this connection within its unixShmNode */
27642   u16 sharedMask;            /* Mask of shared locks held */
27643   u16 exclMask;              /* Mask of exclusive locks held */
27644 };
27645 
27646 /*
27647 ** Constants used for locking
27648 */
27649 #define UNIX_SHM_BASE   ((22+SQLITE_SHM_NLOCK)*4)         /* first lock byte */
27650 #define UNIX_SHM_DMS    (UNIX_SHM_BASE+SQLITE_SHM_NLOCK)  /* deadman switch */
27651 
27652 /*
27653 ** Apply posix advisory locks for all bytes from ofst through ofst+n-1.
27654 **
27655 ** Locks block if the mask is exactly UNIX_SHM_C and are non-blocking
27656 ** otherwise.
27657 */
27658 static int unixShmSystemLock(
27659   unixShmNode *pShmNode, /* Apply locks to this open shared-memory segment */
27660   int lockType,          /* F_UNLCK, F_RDLCK, or F_WRLCK */
27661   int ofst,              /* First byte of the locking range */
27662   int n                  /* Number of bytes to lock */
27663 ){
27664   struct flock f;       /* The posix advisory locking structure */
27665   int rc = SQLITE_OK;   /* Result code form fcntl() */
27666 
27667   /* Access to the unixShmNode object is serialized by the caller */
27668   assert( sqlite3_mutex_held(pShmNode->mutex) || pShmNode->nRef==0 );
27669 
27670   /* Shared locks never span more than one byte */
27671   assert( n==1 || lockType!=F_RDLCK );
27672 
27673   /* Locks are within range */
27674   assert( n>=1 && n<SQLITE_SHM_NLOCK );
27675 
27676   if( pShmNode->h>=0 ){
27677     /* Initialize the locking parameters */
27678     memset(&f, 0, sizeof(f));
27679     f.l_type = lockType;
27680     f.l_whence = SEEK_SET;
27681     f.l_start = ofst;
27682     f.l_len = n;
27683 
27684     rc = osFcntl(pShmNode->h, F_SETLK, &f);
27685     rc = (rc!=(-1)) ? SQLITE_OK : SQLITE_BUSY;
27686   }
27687 
27688   /* Update the global lock state and do debug tracing */
27689 #ifdef SQLITE_DEBUG
27690   { u16 mask;
27691   OSTRACE(("SHM-LOCK "));
27692   mask = ofst>31 ? 0xffff : (1<<(ofst+n)) - (1<<ofst);
27693   if( rc==SQLITE_OK ){
27694     if( lockType==F_UNLCK ){
27695       OSTRACE(("unlock %d ok", ofst));
27696       pShmNode->exclMask &= ~mask;
27697       pShmNode->sharedMask &= ~mask;
27698     }else if( lockType==F_RDLCK ){
27699       OSTRACE(("read-lock %d ok", ofst));
27700       pShmNode->exclMask &= ~mask;
27701       pShmNode->sharedMask |= mask;
27702     }else{
27703       assert( lockType==F_WRLCK );
27704       OSTRACE(("write-lock %d ok", ofst));
27705       pShmNode->exclMask |= mask;
27706       pShmNode->sharedMask &= ~mask;
27707     }
27708   }else{
27709     if( lockType==F_UNLCK ){
27710       OSTRACE(("unlock %d failed", ofst));
27711     }else if( lockType==F_RDLCK ){
27712       OSTRACE(("read-lock failed"));
27713     }else{
27714       assert( lockType==F_WRLCK );
27715       OSTRACE(("write-lock %d failed", ofst));
27716     }
27717   }
27718   OSTRACE((" - afterwards %03x,%03x\n",
27719            pShmNode->sharedMask, pShmNode->exclMask));
27720   }
27721 #endif
27722 
27723   return rc;
27724 }
27725 
27726 
27727 /*
27728 ** Purge the unixShmNodeList list of all entries with unixShmNode.nRef==0.
27729 **
27730 ** This is not a VFS shared-memory method; it is a utility function called
27731 ** by VFS shared-memory methods.
27732 */
27733 static void unixShmPurge(unixFile *pFd){
27734   unixShmNode *p = pFd->pInode->pShmNode;
27735   assert( unixMutexHeld() );
27736   if( p && p->nRef==0 ){
27737     int i;
27738     assert( p->pInode==pFd->pInode );
27739     sqlite3_mutex_free(p->mutex);
27740     for(i=0; i<p->nRegion; i++){
27741       if( p->h>=0 ){
27742         osMunmap(p->apRegion[i], p->szRegion);
27743       }else{
27744         sqlite3_free(p->apRegion[i]);
27745       }
27746     }
27747     sqlite3_free(p->apRegion);
27748     if( p->h>=0 ){
27749       robust_close(pFd, p->h, __LINE__);
27750       p->h = -1;
27751     }
27752     p->pInode->pShmNode = 0;
27753     sqlite3_free(p);
27754   }
27755 }
27756 
27757 /*
27758 ** Open a shared-memory area associated with open database file pDbFd.
27759 ** This particular implementation uses mmapped files.
27760 **
27761 ** The file used to implement shared-memory is in the same directory
27762 ** as the open database file and has the same name as the open database
27763 ** file with the "-shm" suffix added.  For example, if the database file
27764 ** is "/home/user1/config.db" then the file that is created and mmapped
27765 ** for shared memory will be called "/home/user1/config.db-shm".
27766 **
27767 ** Another approach to is to use files in /dev/shm or /dev/tmp or an
27768 ** some other tmpfs mount. But if a file in a different directory
27769 ** from the database file is used, then differing access permissions
27770 ** or a chroot() might cause two different processes on the same
27771 ** database to end up using different files for shared memory -
27772 ** meaning that their memory would not really be shared - resulting
27773 ** in database corruption.  Nevertheless, this tmpfs file usage
27774 ** can be enabled at compile-time using -DSQLITE_SHM_DIRECTORY="/dev/shm"
27775 ** or the equivalent.  The use of the SQLITE_SHM_DIRECTORY compile-time
27776 ** option results in an incompatible build of SQLite;  builds of SQLite
27777 ** that with differing SQLITE_SHM_DIRECTORY settings attempt to use the
27778 ** same database file at the same time, database corruption will likely
27779 ** result. The SQLITE_SHM_DIRECTORY compile-time option is considered
27780 ** "unsupported" and may go away in a future SQLite release.
27781 **
27782 ** When opening a new shared-memory file, if no other instances of that
27783 ** file are currently open, in this process or in other processes, then
27784 ** the file must be truncated to zero length or have its header cleared.
27785 **
27786 ** If the original database file (pDbFd) is using the "unix-excl" VFS
27787 ** that means that an exclusive lock is held on the database file and
27788 ** that no other processes are able to read or write the database.  In
27789 ** that case, we do not really need shared memory.  No shared memory
27790 ** file is created.  The shared memory will be simulated with heap memory.
27791 */
27792 static int unixOpenSharedMemory(unixFile *pDbFd){
27793   struct unixShm *p = 0;          /* The connection to be opened */
27794   struct unixShmNode *pShmNode;   /* The underlying mmapped file */
27795   int rc;                         /* Result code */
27796   unixInodeInfo *pInode;          /* The inode of fd */
27797   char *zShmFilename;             /* Name of the file used for SHM */
27798   int nShmFilename;               /* Size of the SHM filename in bytes */
27799 
27800   /* Allocate space for the new unixShm object. */
27801   p = sqlite3_malloc( sizeof(*p) );
27802   if( p==0 ) return SQLITE_NOMEM;
27803   memset(p, 0, sizeof(*p));
27804   assert( pDbFd->pShm==0 );
27805 
27806   /* Check to see if a unixShmNode object already exists. Reuse an existing
27807   ** one if present. Create a new one if necessary.
27808   */
27809   unixEnterMutex();
27810   pInode = pDbFd->pInode;
27811   pShmNode = pInode->pShmNode;
27812   if( pShmNode==0 ){
27813     struct stat sStat;                 /* fstat() info for database file */
27814 
27815     /* Call fstat() to figure out the permissions on the database file. If
27816     ** a new *-shm file is created, an attempt will be made to create it
27817     ** with the same permissions.
27818     */
27819     if( osFstat(pDbFd->h, &sStat) && pInode->bProcessLock==0 ){
27820       rc = SQLITE_IOERR_FSTAT;
27821       goto shm_open_err;
27822     }
27823 
27824 #ifdef SQLITE_SHM_DIRECTORY
27825     nShmFilename = sizeof(SQLITE_SHM_DIRECTORY) + 31;
27826 #else
27827     nShmFilename = 6 + (int)strlen(pDbFd->zPath);
27828 #endif
27829     pShmNode = sqlite3_malloc( sizeof(*pShmNode) + nShmFilename );
27830     if( pShmNode==0 ){
27831       rc = SQLITE_NOMEM;
27832       goto shm_open_err;
27833     }
27834     memset(pShmNode, 0, sizeof(*pShmNode)+nShmFilename);
27835     zShmFilename = pShmNode->zFilename = (char*)&pShmNode[1];
27836 #ifdef SQLITE_SHM_DIRECTORY
27837     sqlite3_snprintf(nShmFilename, zShmFilename,
27838                      SQLITE_SHM_DIRECTORY "/sqlite-shm-%x-%x",
27839                      (u32)sStat.st_ino, (u32)sStat.st_dev);
27840 #else
27841     sqlite3_snprintf(nShmFilename, zShmFilename, "%s-shm", pDbFd->zPath);
27842     sqlite3FileSuffix3(pDbFd->zPath, zShmFilename);
27843 #endif
27844     pShmNode->h = -1;
27845     pDbFd->pInode->pShmNode = pShmNode;
27846     pShmNode->pInode = pDbFd->pInode;
27847     pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
27848     if( pShmNode->mutex==0 ){
27849       rc = SQLITE_NOMEM;
27850       goto shm_open_err;
27851     }
27852 
27853     if( pInode->bProcessLock==0 ){
27854       int openFlags = O_RDWR | O_CREAT;
27855       if( sqlite3_uri_boolean(pDbFd->zPath, "readonly_shm", 0) ){
27856         openFlags = O_RDONLY;
27857         pShmNode->isReadonly = 1;
27858       }
27859       pShmNode->h = robust_open(zShmFilename, openFlags, (sStat.st_mode&0777));
27860       if( pShmNode->h<0 ){
27861         rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zShmFilename);
27862         goto shm_open_err;
27863       }
27864 
27865       /* If this process is running as root, make sure that the SHM file
27866       ** is owned by the same user that owns the original database.  Otherwise,
27867       ** the original owner will not be able to connect.
27868       */
27869       osFchown(pShmNode->h, sStat.st_uid, sStat.st_gid);
27870 
27871       /* Check to see if another process is holding the dead-man switch.
27872       ** If not, truncate the file to zero length.
27873       */
27874       rc = SQLITE_OK;
27875       if( unixShmSystemLock(pShmNode, F_WRLCK, UNIX_SHM_DMS, 1)==SQLITE_OK ){
27876         if( robust_ftruncate(pShmNode->h, 0) ){
27877           rc = unixLogError(SQLITE_IOERR_SHMOPEN, "ftruncate", zShmFilename);
27878         }
27879       }
27880       if( rc==SQLITE_OK ){
27881         rc = unixShmSystemLock(pShmNode, F_RDLCK, UNIX_SHM_DMS, 1);
27882       }
27883       if( rc ) goto shm_open_err;
27884     }
27885   }
27886 
27887   /* Make the new connection a child of the unixShmNode */
27888   p->pShmNode = pShmNode;
27889 #ifdef SQLITE_DEBUG
27890   p->id = pShmNode->nextShmId++;
27891 #endif
27892   pShmNode->nRef++;
27893   pDbFd->pShm = p;
27894   unixLeaveMutex();
27895 
27896   /* The reference count on pShmNode has already been incremented under
27897   ** the cover of the unixEnterMutex() mutex and the pointer from the
27898   ** new (struct unixShm) object to the pShmNode has been set. All that is
27899   ** left to do is to link the new object into the linked list starting
27900   ** at pShmNode->pFirst. This must be done while holding the pShmNode->mutex
27901   ** mutex.
27902   */
27903   sqlite3_mutex_enter(pShmNode->mutex);
27904   p->pNext = pShmNode->pFirst;
27905   pShmNode->pFirst = p;
27906   sqlite3_mutex_leave(pShmNode->mutex);
27907   return SQLITE_OK;
27908 
27909   /* Jump here on any error */
27910 shm_open_err:
27911   unixShmPurge(pDbFd);       /* This call frees pShmNode if required */
27912   sqlite3_free(p);
27913   unixLeaveMutex();
27914   return rc;
27915 }
27916 
27917 /*
27918 ** This function is called to obtain a pointer to region iRegion of the
27919 ** shared-memory associated with the database file fd. Shared-memory regions
27920 ** are numbered starting from zero. Each shared-memory region is szRegion
27921 ** bytes in size.
27922 **
27923 ** If an error occurs, an error code is returned and *pp is set to NULL.
27924 **
27925 ** Otherwise, if the bExtend parameter is 0 and the requested shared-memory
27926 ** region has not been allocated (by any client, including one running in a
27927 ** separate process), then *pp is set to NULL and SQLITE_OK returned. If
27928 ** bExtend is non-zero and the requested shared-memory region has not yet
27929 ** been allocated, it is allocated by this function.
27930 **
27931 ** If the shared-memory region has already been allocated or is allocated by
27932 ** this call as described above, then it is mapped into this processes
27933 ** address space (if it is not already), *pp is set to point to the mapped
27934 ** memory and SQLITE_OK returned.
27935 */
27936 static int unixShmMap(
27937   sqlite3_file *fd,               /* Handle open on database file */
27938   int iRegion,                    /* Region to retrieve */
27939   int szRegion,                   /* Size of regions */
27940   int bExtend,                    /* True to extend file if necessary */
27941   void volatile **pp              /* OUT: Mapped memory */
27942 ){
27943   unixFile *pDbFd = (unixFile*)fd;
27944   unixShm *p;
27945   unixShmNode *pShmNode;
27946   int rc = SQLITE_OK;
27947 
27948   /* If the shared-memory file has not yet been opened, open it now. */
27949   if( pDbFd->pShm==0 ){
27950     rc = unixOpenSharedMemory(pDbFd);
27951     if( rc!=SQLITE_OK ) return rc;
27952   }
27953 
27954   p = pDbFd->pShm;
27955   pShmNode = p->pShmNode;
27956   sqlite3_mutex_enter(pShmNode->mutex);
27957   assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 );
27958   assert( pShmNode->pInode==pDbFd->pInode );
27959   assert( pShmNode->h>=0 || pDbFd->pInode->bProcessLock==1 );
27960   assert( pShmNode->h<0 || pDbFd->pInode->bProcessLock==0 );
27961 
27962   if( pShmNode->nRegion<=iRegion ){
27963     char **apNew;                      /* New apRegion[] array */
27964     int nByte = (iRegion+1)*szRegion;  /* Minimum required file size */
27965     struct stat sStat;                 /* Used by fstat() */
27966 
27967     pShmNode->szRegion = szRegion;
27968 
27969     if( pShmNode->h>=0 ){
27970       /* The requested region is not mapped into this processes address space.
27971       ** Check to see if it has been allocated (i.e. if the wal-index file is
27972       ** large enough to contain the requested region).
27973       */
27974       if( osFstat(pShmNode->h, &sStat) ){
27975         rc = SQLITE_IOERR_SHMSIZE;
27976         goto shmpage_out;
27977       }
27978 
27979       if( sStat.st_size<nByte ){
27980         /* The requested memory region does not exist. If bExtend is set to
27981         ** false, exit early. *pp will be set to NULL and SQLITE_OK returned.
27982         */
27983         if( !bExtend ){
27984           goto shmpage_out;
27985         }
27986 
27987         /* Alternatively, if bExtend is true, extend the file. Do this by
27988         ** writing a single byte to the end of each (OS) page being
27989         ** allocated or extended. Technically, we need only write to the
27990         ** last page in order to extend the file. But writing to all new
27991         ** pages forces the OS to allocate them immediately, which reduces
27992         ** the chances of SIGBUS while accessing the mapped region later on.
27993         */
27994         else{
27995           static const int pgsz = 4096;
27996           int iPg;
27997 
27998           /* Write to the last byte of each newly allocated or extended page */
27999           assert( (nByte % pgsz)==0 );
28000           for(iPg=(sStat.st_size/pgsz); iPg<(nByte/pgsz); iPg++){
28001             if( seekAndWriteFd(pShmNode->h, iPg*pgsz + pgsz-1, "", 1, 0)!=1 ){
28002               const char *zFile = pShmNode->zFilename;
28003               rc = unixLogError(SQLITE_IOERR_SHMSIZE, "write", zFile);
28004               goto shmpage_out;
28005             }
28006           }
28007         }
28008       }
28009     }
28010 
28011     /* Map the requested memory region into this processes address space. */
28012     apNew = (char **)sqlite3_realloc(
28013         pShmNode->apRegion, (iRegion+1)*sizeof(char *)
28014     );
28015     if( !apNew ){
28016       rc = SQLITE_IOERR_NOMEM;
28017       goto shmpage_out;
28018     }
28019     pShmNode->apRegion = apNew;
28020     while(pShmNode->nRegion<=iRegion){
28021       void *pMem;
28022       if( pShmNode->h>=0 ){
28023         pMem = osMmap(0, szRegion,
28024             pShmNode->isReadonly ? PROT_READ : PROT_READ|PROT_WRITE,
28025             MAP_SHARED, pShmNode->h, szRegion*(i64)pShmNode->nRegion
28026         );
28027         if( pMem==MAP_FAILED ){
28028           rc = unixLogError(SQLITE_IOERR_SHMMAP, "mmap", pShmNode->zFilename);
28029           goto shmpage_out;
28030         }
28031       }else{
28032         pMem = sqlite3_malloc(szRegion);
28033         if( pMem==0 ){
28034           rc = SQLITE_NOMEM;
28035           goto shmpage_out;
28036         }
28037         memset(pMem, 0, szRegion);
28038       }
28039       pShmNode->apRegion[pShmNode->nRegion] = pMem;
28040       pShmNode->nRegion++;
28041     }
28042   }
28043 
28044 shmpage_out:
28045   if( pShmNode->nRegion>iRegion ){
28046     *pp = pShmNode->apRegion[iRegion];
28047   }else{
28048     *pp = 0;
28049   }
28050   if( pShmNode->isReadonly && rc==SQLITE_OK ) rc = SQLITE_READONLY;
28051   sqlite3_mutex_leave(pShmNode->mutex);
28052   return rc;
28053 }
28054 
28055 /*
28056 ** Change the lock state for a shared-memory segment.
28057 **
28058 ** Note that the relationship between SHAREd and EXCLUSIVE locks is a little
28059 ** different here than in posix.  In xShmLock(), one can go from unlocked
28060 ** to shared and back or from unlocked to exclusive and back.  But one may
28061 ** not go from shared to exclusive or from exclusive to shared.
28062 */
28063 static int unixShmLock(
28064   sqlite3_file *fd,          /* Database file holding the shared memory */
28065   int ofst,                  /* First lock to acquire or release */
28066   int n,                     /* Number of locks to acquire or release */
28067   int flags                  /* What to do with the lock */
28068 ){
28069   unixFile *pDbFd = (unixFile*)fd;      /* Connection holding shared memory */
28070   unixShm *p = pDbFd->pShm;             /* The shared memory being locked */
28071   unixShm *pX;                          /* For looping over all siblings */
28072   unixShmNode *pShmNode = p->pShmNode;  /* The underlying file iNode */
28073   int rc = SQLITE_OK;                   /* Result code */
28074   u16 mask;                             /* Mask of locks to take or release */
28075 
28076   assert( pShmNode==pDbFd->pInode->pShmNode );
28077   assert( pShmNode->pInode==pDbFd->pInode );
28078   assert( ofst>=0 && ofst+n<=SQLITE_SHM_NLOCK );
28079   assert( n>=1 );
28080   assert( flags==(SQLITE_SHM_LOCK | SQLITE_SHM_SHARED)
28081        || flags==(SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE)
28082        || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED)
28083        || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE) );
28084   assert( n==1 || (flags & SQLITE_SHM_EXCLUSIVE)!=0 );
28085   assert( pShmNode->h>=0 || pDbFd->pInode->bProcessLock==1 );
28086   assert( pShmNode->h<0 || pDbFd->pInode->bProcessLock==0 );
28087 
28088   mask = (1<<(ofst+n)) - (1<<ofst);
28089   assert( n>1 || mask==(1<<ofst) );
28090   sqlite3_mutex_enter(pShmNode->mutex);
28091   if( flags & SQLITE_SHM_UNLOCK ){
28092     u16 allMask = 0; /* Mask of locks held by siblings */
28093 
28094     /* See if any siblings hold this same lock */
28095     for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
28096       if( pX==p ) continue;
28097       assert( (pX->exclMask & (p->exclMask|p->sharedMask))==0 );
28098       allMask |= pX->sharedMask;
28099     }
28100 
28101     /* Unlock the system-level locks */
28102     if( (mask & allMask)==0 ){
28103       rc = unixShmSystemLock(pShmNode, F_UNLCK, ofst+UNIX_SHM_BASE, n);
28104     }else{
28105       rc = SQLITE_OK;
28106     }
28107 
28108     /* Undo the local locks */
28109     if( rc==SQLITE_OK ){
28110       p->exclMask &= ~mask;
28111       p->sharedMask &= ~mask;
28112     }
28113   }else if( flags & SQLITE_SHM_SHARED ){
28114     u16 allShared = 0;  /* Union of locks held by connections other than "p" */
28115 
28116     /* Find out which shared locks are already held by sibling connections.
28117     ** If any sibling already holds an exclusive lock, go ahead and return
28118     ** SQLITE_BUSY.
28119     */
28120     for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
28121       if( (pX->exclMask & mask)!=0 ){
28122         rc = SQLITE_BUSY;
28123         break;
28124       }
28125       allShared |= pX->sharedMask;
28126     }
28127 
28128     /* Get shared locks at the system level, if necessary */
28129     if( rc==SQLITE_OK ){
28130       if( (allShared & mask)==0 ){
28131         rc = unixShmSystemLock(pShmNode, F_RDLCK, ofst+UNIX_SHM_BASE, n);
28132       }else{
28133         rc = SQLITE_OK;
28134       }
28135     }
28136 
28137     /* Get the local shared locks */
28138     if( rc==SQLITE_OK ){
28139       p->sharedMask |= mask;
28140     }
28141   }else{
28142     /* Make sure no sibling connections hold locks that will block this
28143     ** lock.  If any do, return SQLITE_BUSY right away.
28144     */
28145     for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
28146       if( (pX->exclMask & mask)!=0 || (pX->sharedMask & mask)!=0 ){
28147         rc = SQLITE_BUSY;
28148         break;
28149       }
28150     }
28151 
28152     /* Get the exclusive locks at the system level.  Then if successful
28153     ** also mark the local connection as being locked.
28154     */
28155     if( rc==SQLITE_OK ){
28156       rc = unixShmSystemLock(pShmNode, F_WRLCK, ofst+UNIX_SHM_BASE, n);
28157       if( rc==SQLITE_OK ){
28158         assert( (p->sharedMask & mask)==0 );
28159         p->exclMask |= mask;
28160       }
28161     }
28162   }
28163   sqlite3_mutex_leave(pShmNode->mutex);
28164   OSTRACE(("SHM-LOCK shmid-%d, pid-%d got %03x,%03x\n",
28165            p->id, getpid(), p->sharedMask, p->exclMask));
28166   return rc;
28167 }
28168 
28169 /*
28170 ** Implement a memory barrier or memory fence on shared memory.
28171 **
28172 ** All loads and stores begun before the barrier must complete before
28173 ** any load or store begun after the barrier.
28174 */
28175 static void unixShmBarrier(
28176   sqlite3_file *fd                /* Database file holding the shared memory */
28177 ){
28178   UNUSED_PARAMETER(fd);
28179   unixEnterMutex();
28180   unixLeaveMutex();
28181 }
28182 
28183 /*
28184 ** Close a connection to shared-memory.  Delete the underlying
28185 ** storage if deleteFlag is true.
28186 **
28187 ** If there is no shared memory associated with the connection then this
28188 ** routine is a harmless no-op.
28189 */
28190 static int unixShmUnmap(
28191   sqlite3_file *fd,               /* The underlying database file */
28192   int deleteFlag                  /* Delete shared-memory if true */
28193 ){
28194   unixShm *p;                     /* The connection to be closed */
28195   unixShmNode *pShmNode;          /* The underlying shared-memory file */
28196   unixShm **pp;                   /* For looping over sibling connections */
28197   unixFile *pDbFd;                /* The underlying database file */
28198 
28199   pDbFd = (unixFile*)fd;
28200   p = pDbFd->pShm;
28201   if( p==0 ) return SQLITE_OK;
28202   pShmNode = p->pShmNode;
28203 
28204   assert( pShmNode==pDbFd->pInode->pShmNode );
28205   assert( pShmNode->pInode==pDbFd->pInode );
28206 
28207   /* Remove connection p from the set of connections associated
28208   ** with pShmNode */
28209   sqlite3_mutex_enter(pShmNode->mutex);
28210   for(pp=&pShmNode->pFirst; (*pp)!=p; pp = &(*pp)->pNext){}
28211   *pp = p->pNext;
28212 
28213   /* Free the connection p */
28214   sqlite3_free(p);
28215   pDbFd->pShm = 0;
28216   sqlite3_mutex_leave(pShmNode->mutex);
28217 
28218   /* If pShmNode->nRef has reached 0, then close the underlying
28219   ** shared-memory file, too */
28220   unixEnterMutex();
28221   assert( pShmNode->nRef>0 );
28222   pShmNode->nRef--;
28223   if( pShmNode->nRef==0 ){
28224     if( deleteFlag && pShmNode->h>=0 ) osUnlink(pShmNode->zFilename);
28225     unixShmPurge(pDbFd);
28226   }
28227   unixLeaveMutex();
28228 
28229   return SQLITE_OK;
28230 }
28231 
28232 
28233 #else
28234 # define unixShmMap     0
28235 # define unixShmLock    0
28236 # define unixShmBarrier 0
28237 # define unixShmUnmap   0
28238 #endif /* #ifndef SQLITE_OMIT_WAL */
28239 
28240 #if SQLITE_MAX_MMAP_SIZE>0
28241 /*
28242 ** If it is currently memory mapped, unmap file pFd.
28243 */
28244 static void unixUnmapfile(unixFile *pFd){
28245   assert( pFd->nFetchOut==0 );
28246   if( pFd->pMapRegion ){
28247     osMunmap(pFd->pMapRegion, pFd->mmapSizeActual);
28248     pFd->pMapRegion = 0;
28249     pFd->mmapSize = 0;
28250     pFd->mmapSizeActual = 0;
28251   }
28252 }
28253 
28254 /*
28255 ** Return the system page size.
28256 */
28257 static int unixGetPagesize(void){
28258 #if HAVE_MREMAP
28259   return 512;
28260 #elif defined(_BSD_SOURCE)
28261   return getpagesize();
28262 #else
28263   return (int)sysconf(_SC_PAGESIZE);
28264 #endif
28265 }
28266 
28267 /*
28268 ** Attempt to set the size of the memory mapping maintained by file
28269 ** descriptor pFd to nNew bytes. Any existing mapping is discarded.
28270 **
28271 ** If successful, this function sets the following variables:
28272 **
28273 **       unixFile.pMapRegion
28274 **       unixFile.mmapSize
28275 **       unixFile.mmapSizeActual
28276 **
28277 ** If unsuccessful, an error message is logged via sqlite3_log() and
28278 ** the three variables above are zeroed. In this case SQLite should
28279 ** continue accessing the database using the xRead() and xWrite()
28280 ** methods.
28281 */
28282 static void unixRemapfile(
28283   unixFile *pFd,                  /* File descriptor object */
28284   i64 nNew                        /* Required mapping size */
28285 ){
28286   const char *zErr = "mmap";
28287   int h = pFd->h;                      /* File descriptor open on db file */
28288   u8 *pOrig = (u8 *)pFd->pMapRegion;   /* Pointer to current file mapping */
28289   i64 nOrig = pFd->mmapSizeActual;     /* Size of pOrig region in bytes */
28290   u8 *pNew = 0;                        /* Location of new mapping */
28291   int flags = PROT_READ;               /* Flags to pass to mmap() */
28292 
28293   assert( pFd->nFetchOut==0 );
28294   assert( nNew>pFd->mmapSize );
28295   assert( nNew<=pFd->mmapSizeMax );
28296   assert( nNew>0 );
28297   assert( pFd->mmapSizeActual>=pFd->mmapSize );
28298   assert( MAP_FAILED!=0 );
28299 
28300   if( (pFd->ctrlFlags & UNIXFILE_RDONLY)==0 ) flags |= PROT_WRITE;
28301 
28302   if( pOrig ){
28303     const int szSyspage = unixGetPagesize();
28304     i64 nReuse = (pFd->mmapSize & ~(szSyspage-1));
28305     u8 *pReq = &pOrig[nReuse];
28306 
28307     /* Unmap any pages of the existing mapping that cannot be reused. */
28308     if( nReuse!=nOrig ){
28309       osMunmap(pReq, nOrig-nReuse);
28310     }
28311 
28312 #if HAVE_MREMAP
28313     pNew = osMremap(pOrig, nReuse, nNew, MREMAP_MAYMOVE);
28314     zErr = "mremap";
28315 #else
28316     pNew = osMmap(pReq, nNew-nReuse, flags, MAP_SHARED, h, nReuse);
28317     if( pNew!=MAP_FAILED ){
28318       if( pNew!=pReq ){
28319         osMunmap(pNew, nNew - nReuse);
28320         pNew = 0;
28321       }else{
28322         pNew = pOrig;
28323       }
28324     }
28325 #endif
28326 
28327     /* The attempt to extend the existing mapping failed. Free it. */
28328     if( pNew==MAP_FAILED || pNew==0 ){
28329       osMunmap(pOrig, nReuse);
28330     }
28331   }
28332 
28333   /* If pNew is still NULL, try to create an entirely new mapping. */
28334   if( pNew==0 ){
28335     pNew = osMmap(0, nNew, flags, MAP_SHARED, h, 0);
28336   }
28337 
28338   if( pNew==MAP_FAILED ){
28339     pNew = 0;
28340     nNew = 0;
28341     unixLogError(SQLITE_OK, zErr, pFd->zPath);
28342 
28343     /* If the mmap() above failed, assume that all subsequent mmap() calls
28344     ** will probably fail too. Fall back to using xRead/xWrite exclusively
28345     ** in this case.  */
28346     pFd->mmapSizeMax = 0;
28347   }
28348   pFd->pMapRegion = (void *)pNew;
28349   pFd->mmapSize = pFd->mmapSizeActual = nNew;
28350 }
28351 
28352 /*
28353 ** Memory map or remap the file opened by file-descriptor pFd (if the file
28354 ** is already mapped, the existing mapping is replaced by the new). Or, if
28355 ** there already exists a mapping for this file, and there are still
28356 ** outstanding xFetch() references to it, this function is a no-op.
28357 **
28358 ** If parameter nByte is non-negative, then it is the requested size of
28359 ** the mapping to create. Otherwise, if nByte is less than zero, then the
28360 ** requested size is the size of the file on disk. The actual size of the
28361 ** created mapping is either the requested size or the value configured
28362 ** using SQLITE_FCNTL_MMAP_LIMIT, whichever is smaller.
28363 **
28364 ** SQLITE_OK is returned if no error occurs (even if the mapping is not
28365 ** recreated as a result of outstanding references) or an SQLite error
28366 ** code otherwise.
28367 */
28368 static int unixMapfile(unixFile *pFd, i64 nByte){
28369   i64 nMap = nByte;
28370   int rc;
28371 
28372   assert( nMap>=0 || pFd->nFetchOut==0 );
28373   if( pFd->nFetchOut>0 ) return SQLITE_OK;
28374 
28375   if( nMap<0 ){
28376     struct stat statbuf;          /* Low-level file information */
28377     rc = osFstat(pFd->h, &statbuf);
28378     if( rc!=SQLITE_OK ){
28379       return SQLITE_IOERR_FSTAT;
28380     }
28381     nMap = statbuf.st_size;
28382   }
28383   if( nMap>pFd->mmapSizeMax ){
28384     nMap = pFd->mmapSizeMax;
28385   }
28386 
28387   if( nMap!=pFd->mmapSize ){
28388     if( nMap>0 ){
28389       unixRemapfile(pFd, nMap);
28390     }else{
28391       unixUnmapfile(pFd);
28392     }
28393   }
28394 
28395   return SQLITE_OK;
28396 }
28397 #endif /* SQLITE_MAX_MMAP_SIZE>0 */
28398 
28399 /*
28400 ** If possible, return a pointer to a mapping of file fd starting at offset
28401 ** iOff. The mapping must be valid for at least nAmt bytes.
28402 **
28403 ** If such a pointer can be obtained, store it in *pp and return SQLITE_OK.
28404 ** Or, if one cannot but no error occurs, set *pp to 0 and return SQLITE_OK.
28405 ** Finally, if an error does occur, return an SQLite error code. The final
28406 ** value of *pp is undefined in this case.
28407 **
28408 ** If this function does return a pointer, the caller must eventually
28409 ** release the reference by calling unixUnfetch().
28410 */
28411 static int unixFetch(sqlite3_file *fd, i64 iOff, int nAmt, void **pp){
28412 #if SQLITE_MAX_MMAP_SIZE>0
28413   unixFile *pFd = (unixFile *)fd;   /* The underlying database file */
28414 #endif
28415   *pp = 0;
28416 
28417 #if SQLITE_MAX_MMAP_SIZE>0
28418   if( pFd->mmapSizeMax>0 ){
28419     if( pFd->pMapRegion==0 ){
28420       int rc = unixMapfile(pFd, -1);
28421       if( rc!=SQLITE_OK ) return rc;
28422     }
28423     if( pFd->mmapSize >= iOff+nAmt ){
28424       *pp = &((u8 *)pFd->pMapRegion)[iOff];
28425       pFd->nFetchOut++;
28426     }
28427   }
28428 #endif
28429   return SQLITE_OK;
28430 }
28431 
28432 /*
28433 ** If the third argument is non-NULL, then this function releases a
28434 ** reference obtained by an earlier call to unixFetch(). The second
28435 ** argument passed to this function must be the same as the corresponding
28436 ** argument that was passed to the unixFetch() invocation.
28437 **
28438 ** Or, if the third argument is NULL, then this function is being called
28439 ** to inform the VFS layer that, according to POSIX, any existing mapping
28440 ** may now be invalid and should be unmapped.
28441 */
28442 static int unixUnfetch(sqlite3_file *fd, i64 iOff, void *p){
28443 #if SQLITE_MAX_MMAP_SIZE>0
28444   unixFile *pFd = (unixFile *)fd;   /* The underlying database file */
28445   UNUSED_PARAMETER(iOff);
28446 
28447   /* If p==0 (unmap the entire file) then there must be no outstanding
28448   ** xFetch references. Or, if p!=0 (meaning it is an xFetch reference),
28449   ** then there must be at least one outstanding.  */
28450   assert( (p==0)==(pFd->nFetchOut==0) );
28451 
28452   /* If p!=0, it must match the iOff value. */
28453   assert( p==0 || p==&((u8 *)pFd->pMapRegion)[iOff] );
28454 
28455   if( p ){
28456     pFd->nFetchOut--;
28457   }else{
28458     unixUnmapfile(pFd);
28459   }
28460 
28461   assert( pFd->nFetchOut>=0 );
28462 #else
28463   UNUSED_PARAMETER(fd);
28464   UNUSED_PARAMETER(p);
28465   UNUSED_PARAMETER(iOff);
28466 #endif
28467   return SQLITE_OK;
28468 }
28469 
28470 /*
28471 ** Here ends the implementation of all sqlite3_file methods.
28472 **
28473 ********************** End sqlite3_file Methods *******************************
28474 ******************************************************************************/
28475 
28476 /*
28477 ** This division contains definitions of sqlite3_io_methods objects that
28478 ** implement various file locking strategies.  It also contains definitions
28479 ** of "finder" functions.  A finder-function is used to locate the appropriate
28480 ** sqlite3_io_methods object for a particular database file.  The pAppData
28481 ** field of the sqlite3_vfs VFS objects are initialized to be pointers to
28482 ** the correct finder-function for that VFS.
28483 **
28484 ** Most finder functions return a pointer to a fixed sqlite3_io_methods
28485 ** object.  The only interesting finder-function is autolockIoFinder, which
28486 ** looks at the filesystem type and tries to guess the best locking
28487 ** strategy from that.
28488 **
28489 ** For finder-funtion F, two objects are created:
28490 **
28491 **    (1) The real finder-function named "FImpt()".
28492 **
28493 **    (2) A constant pointer to this function named just "F".
28494 **
28495 **
28496 ** A pointer to the F pointer is used as the pAppData value for VFS
28497 ** objects.  We have to do this instead of letting pAppData point
28498 ** directly at the finder-function since C90 rules prevent a void*
28499 ** from be cast into a function pointer.
28500 **
28501 **
28502 ** Each instance of this macro generates two objects:
28503 **
28504 **   *  A constant sqlite3_io_methods object call METHOD that has locking
28505 **      methods CLOSE, LOCK, UNLOCK, CKRESLOCK.
28506 **
28507 **   *  An I/O method finder function called FINDER that returns a pointer
28508 **      to the METHOD object in the previous bullet.
28509 */
28510 #define IOMETHODS(FINDER, METHOD, VERSION, CLOSE, LOCK, UNLOCK, CKLOCK)      \
28511 static const sqlite3_io_methods METHOD = {                                   \
28512    VERSION,                    /* iVersion */                                \
28513    CLOSE,                      /* xClose */                                  \
28514    unixRead,                   /* xRead */                                   \
28515    unixWrite,                  /* xWrite */                                  \
28516    unixTruncate,               /* xTruncate */                               \
28517    unixSync,                   /* xSync */                                   \
28518    unixFileSize,               /* xFileSize */                               \
28519    LOCK,                       /* xLock */                                   \
28520    UNLOCK,                     /* xUnlock */                                 \
28521    CKLOCK,                     /* xCheckReservedLock */                      \
28522    unixFileControl,            /* xFileControl */                            \
28523    unixSectorSize,             /* xSectorSize */                             \
28524    unixDeviceCharacteristics,  /* xDeviceCapabilities */                     \
28525    unixShmMap,                 /* xShmMap */                                 \
28526    unixShmLock,                /* xShmLock */                                \
28527    unixShmBarrier,             /* xShmBarrier */                             \
28528    unixShmUnmap,               /* xShmUnmap */                               \
28529    unixFetch,                  /* xFetch */                                  \
28530    unixUnfetch,                /* xUnfetch */                                \
28531 };                                                                           \
28532 static const sqlite3_io_methods *FINDER##Impl(const char *z, unixFile *p){   \
28533   UNUSED_PARAMETER(z); UNUSED_PARAMETER(p);                                  \
28534   return &METHOD;                                                            \
28535 }                                                                            \
28536 static const sqlite3_io_methods *(*const FINDER)(const char*,unixFile *p)    \
28537     = FINDER##Impl;
28538 
28539 /*
28540 ** Here are all of the sqlite3_io_methods objects for each of the
28541 ** locking strategies.  Functions that return pointers to these methods
28542 ** are also created.
28543 */
28544 IOMETHODS(
28545   posixIoFinder,            /* Finder function name */
28546   posixIoMethods,           /* sqlite3_io_methods object name */
28547   3,                        /* shared memory and mmap are enabled */
28548   unixClose,                /* xClose method */
28549   unixLock,                 /* xLock method */
28550   unixUnlock,               /* xUnlock method */
28551   unixCheckReservedLock     /* xCheckReservedLock method */
28552 )
28553 IOMETHODS(
28554   nolockIoFinder,           /* Finder function name */
28555   nolockIoMethods,          /* sqlite3_io_methods object name */
28556   1,                        /* shared memory is disabled */
28557   nolockClose,              /* xClose method */
28558   nolockLock,               /* xLock method */
28559   nolockUnlock,             /* xUnlock method */
28560   nolockCheckReservedLock   /* xCheckReservedLock method */
28561 )
28562 IOMETHODS(
28563   dotlockIoFinder,          /* Finder function name */
28564   dotlockIoMethods,         /* sqlite3_io_methods object name */
28565   1,                        /* shared memory is disabled */
28566   dotlockClose,             /* xClose method */
28567   dotlockLock,              /* xLock method */
28568   dotlockUnlock,            /* xUnlock method */
28569   dotlockCheckReservedLock  /* xCheckReservedLock method */
28570 )
28571 
28572 #if SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORKS
28573 IOMETHODS(
28574   flockIoFinder,            /* Finder function name */
28575   flockIoMethods,           /* sqlite3_io_methods object name */
28576   1,                        /* shared memory is disabled */
28577   flockClose,               /* xClose method */
28578   flockLock,                /* xLock method */
28579   flockUnlock,              /* xUnlock method */
28580   flockCheckReservedLock    /* xCheckReservedLock method */
28581 )
28582 #endif
28583 
28584 #if OS_VXWORKS
28585 IOMETHODS(
28586   semIoFinder,              /* Finder function name */
28587   semIoMethods,             /* sqlite3_io_methods object name */
28588   1,                        /* shared memory is disabled */
28589   semClose,                 /* xClose method */
28590   semLock,                  /* xLock method */
28591   semUnlock,                /* xUnlock method */
28592   semCheckReservedLock      /* xCheckReservedLock method */
28593 )
28594 #endif
28595 
28596 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
28597 IOMETHODS(
28598   afpIoFinder,              /* Finder function name */
28599   afpIoMethods,             /* sqlite3_io_methods object name */
28600   1,                        /* shared memory is disabled */
28601   afpClose,                 /* xClose method */
28602   afpLock,                  /* xLock method */
28603   afpUnlock,                /* xUnlock method */
28604   afpCheckReservedLock      /* xCheckReservedLock method */
28605 )
28606 #endif
28607 
28608 /*
28609 ** The proxy locking method is a "super-method" in the sense that it
28610 ** opens secondary file descriptors for the conch and lock files and
28611 ** it uses proxy, dot-file, AFP, and flock() locking methods on those
28612 ** secondary files.  For this reason, the division that implements
28613 ** proxy locking is located much further down in the file.  But we need
28614 ** to go ahead and define the sqlite3_io_methods and finder function
28615 ** for proxy locking here.  So we forward declare the I/O methods.
28616 */
28617 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
28618 static int proxyClose(sqlite3_file*);
28619 static int proxyLock(sqlite3_file*, int);
28620 static int proxyUnlock(sqlite3_file*, int);
28621 static int proxyCheckReservedLock(sqlite3_file*, int*);
28622 IOMETHODS(
28623   proxyIoFinder,            /* Finder function name */
28624   proxyIoMethods,           /* sqlite3_io_methods object name */
28625   1,                        /* shared memory is disabled */
28626   proxyClose,               /* xClose method */
28627   proxyLock,                /* xLock method */
28628   proxyUnlock,              /* xUnlock method */
28629   proxyCheckReservedLock    /* xCheckReservedLock method */
28630 )
28631 #endif
28632 
28633 /* nfs lockd on OSX 10.3+ doesn't clear write locks when a read lock is set */
28634 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
28635 IOMETHODS(
28636   nfsIoFinder,               /* Finder function name */
28637   nfsIoMethods,              /* sqlite3_io_methods object name */
28638   1,                         /* shared memory is disabled */
28639   unixClose,                 /* xClose method */
28640   unixLock,                  /* xLock method */
28641   nfsUnlock,                 /* xUnlock method */
28642   unixCheckReservedLock      /* xCheckReservedLock method */
28643 )
28644 #endif
28645 
28646 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
28647 /*
28648 ** This "finder" function attempts to determine the best locking strategy
28649 ** for the database file "filePath".  It then returns the sqlite3_io_methods
28650 ** object that implements that strategy.
28651 **
28652 ** This is for MacOSX only.
28653 */
28654 static const sqlite3_io_methods *autolockIoFinderImpl(
28655   const char *filePath,    /* name of the database file */
28656   unixFile *pNew           /* open file object for the database file */
28657 ){
28658   static const struct Mapping {
28659     const char *zFilesystem;              /* Filesystem type name */
28660     const sqlite3_io_methods *pMethods;   /* Appropriate locking method */
28661   } aMap[] = {
28662     { "hfs",    &posixIoMethods },
28663     { "ufs",    &posixIoMethods },
28664     { "afpfs",  &afpIoMethods },
28665     { "smbfs",  &afpIoMethods },
28666     { "webdav", &nolockIoMethods },
28667     { 0, 0 }
28668   };
28669   int i;
28670   struct statfs fsInfo;
28671   struct flock lockInfo;
28672 
28673   if( !filePath ){
28674     /* If filePath==NULL that means we are dealing with a transient file
28675     ** that does not need to be locked. */
28676     return &nolockIoMethods;
28677   }
28678   if( statfs(filePath, &fsInfo) != -1 ){
28679     if( fsInfo.f_flags & MNT_RDONLY ){
28680       return &nolockIoMethods;
28681     }
28682     for(i=0; aMap[i].zFilesystem; i++){
28683       if( strcmp(fsInfo.f_fstypename, aMap[i].zFilesystem)==0 ){
28684         return aMap[i].pMethods;
28685       }
28686     }
28687   }
28688 
28689   /* Default case. Handles, amongst others, "nfs".
28690   ** Test byte-range lock using fcntl(). If the call succeeds,
28691   ** assume that the file-system supports POSIX style locks.
28692   */
28693   lockInfo.l_len = 1;
28694   lockInfo.l_start = 0;
28695   lockInfo.l_whence = SEEK_SET;
28696   lockInfo.l_type = F_RDLCK;
28697   if( osFcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) {
28698     if( strcmp(fsInfo.f_fstypename, "nfs")==0 ){
28699       return &nfsIoMethods;
28700     } else {
28701       return &posixIoMethods;
28702     }
28703   }else{
28704     return &dotlockIoMethods;
28705   }
28706 }
28707 static const sqlite3_io_methods
28708   *(*const autolockIoFinder)(const char*,unixFile*) = autolockIoFinderImpl;
28709 
28710 #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
28711 
28712 #if OS_VXWORKS && SQLITE_ENABLE_LOCKING_STYLE
28713 /*
28714 ** This "finder" function attempts to determine the best locking strategy
28715 ** for the database file "filePath".  It then returns the sqlite3_io_methods
28716 ** object that implements that strategy.
28717 **
28718 ** This is for VXWorks only.
28719 */
28720 static const sqlite3_io_methods *autolockIoFinderImpl(
28721   const char *filePath,    /* name of the database file */
28722   unixFile *pNew           /* the open file object */
28723 ){
28724   struct flock lockInfo;
28725 
28726   if( !filePath ){
28727     /* If filePath==NULL that means we are dealing with a transient file
28728     ** that does not need to be locked. */
28729     return &nolockIoMethods;
28730   }
28731 
28732   /* Test if fcntl() is supported and use POSIX style locks.
28733   ** Otherwise fall back to the named semaphore method.
28734   */
28735   lockInfo.l_len = 1;
28736   lockInfo.l_start = 0;
28737   lockInfo.l_whence = SEEK_SET;
28738   lockInfo.l_type = F_RDLCK;
28739   if( osFcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) {
28740     return &posixIoMethods;
28741   }else{
28742     return &semIoMethods;
28743   }
28744 }
28745 static const sqlite3_io_methods
28746   *(*const autolockIoFinder)(const char*,unixFile*) = autolockIoFinderImpl;
28747 
28748 #endif /* OS_VXWORKS && SQLITE_ENABLE_LOCKING_STYLE */
28749 
28750 /*
28751 ** An abstract type for a pointer to a IO method finder function:
28752 */
28753 typedef const sqlite3_io_methods *(*finder_type)(const char*,unixFile*);
28754 
28755 
28756 /****************************************************************************
28757 **************************** sqlite3_vfs methods ****************************
28758 **
28759 ** This division contains the implementation of methods on the
28760 ** sqlite3_vfs object.
28761 */
28762 
28763 /*
28764 ** Initialize the contents of the unixFile structure pointed to by pId.
28765 */
28766 static int fillInUnixFile(
28767   sqlite3_vfs *pVfs,      /* Pointer to vfs object */
28768   int h,                  /* Open file descriptor of file being opened */
28769   sqlite3_file *pId,      /* Write to the unixFile structure here */
28770   const char *zFilename,  /* Name of the file being opened */
28771   int ctrlFlags           /* Zero or more UNIXFILE_* values */
28772 ){
28773   const sqlite3_io_methods *pLockingStyle;
28774   unixFile *pNew = (unixFile *)pId;
28775   int rc = SQLITE_OK;
28776 
28777   assert( pNew->pInode==NULL );
28778 
28779   /* Usually the path zFilename should not be a relative pathname. The
28780   ** exception is when opening the proxy "conch" file in builds that
28781   ** include the special Apple locking styles.
28782   */
28783 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
28784   assert( zFilename==0 || zFilename[0]=='/'
28785     || pVfs->pAppData==(void*)&autolockIoFinder );
28786 #else
28787   assert( zFilename==0 || zFilename[0]=='/' );
28788 #endif
28789 
28790   /* No locking occurs in temporary files */
28791   assert( zFilename!=0 || (ctrlFlags & UNIXFILE_NOLOCK)!=0 );
28792 
28793   OSTRACE(("OPEN    %-3d %s\n", h, zFilename));
28794   pNew->h = h;
28795   pNew->pVfs = pVfs;
28796   pNew->zPath = zFilename;
28797   pNew->ctrlFlags = (u8)ctrlFlags;
28798 #if SQLITE_MAX_MMAP_SIZE>0
28799   pNew->mmapSizeMax = sqlite3GlobalConfig.szMmap;
28800 #endif
28801   if( sqlite3_uri_boolean(((ctrlFlags & UNIXFILE_URI) ? zFilename : 0),
28802                            "psow", SQLITE_POWERSAFE_OVERWRITE) ){
28803     pNew->ctrlFlags |= UNIXFILE_PSOW;
28804   }
28805   if( strcmp(pVfs->zName,"unix-excl")==0 ){
28806     pNew->ctrlFlags |= UNIXFILE_EXCL;
28807   }
28808 
28809 #if OS_VXWORKS
28810   pNew->pId = vxworksFindFileId(zFilename);
28811   if( pNew->pId==0 ){
28812     ctrlFlags |= UNIXFILE_NOLOCK;
28813     rc = SQLITE_NOMEM;
28814   }
28815 #endif
28816 
28817   if( ctrlFlags & UNIXFILE_NOLOCK ){
28818     pLockingStyle = &nolockIoMethods;
28819   }else{
28820     pLockingStyle = (**(finder_type*)pVfs->pAppData)(zFilename, pNew);
28821 #if SQLITE_ENABLE_LOCKING_STYLE
28822     /* Cache zFilename in the locking context (AFP and dotlock override) for
28823     ** proxyLock activation is possible (remote proxy is based on db name)
28824     ** zFilename remains valid until file is closed, to support */
28825     pNew->lockingContext = (void*)zFilename;
28826 #endif
28827   }
28828 
28829   if( pLockingStyle == &posixIoMethods
28830 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
28831     || pLockingStyle == &nfsIoMethods
28832 #endif
28833   ){
28834     unixEnterMutex();
28835     rc = findInodeInfo(pNew, &pNew->pInode);
28836     if( rc!=SQLITE_OK ){
28837       /* If an error occurred in findInodeInfo(), close the file descriptor
28838       ** immediately, before releasing the mutex. findInodeInfo() may fail
28839       ** in two scenarios:
28840       **
28841       **   (a) A call to fstat() failed.
28842       **   (b) A malloc failed.
28843       **
28844       ** Scenario (b) may only occur if the process is holding no other
28845       ** file descriptors open on the same file. If there were other file
28846       ** descriptors on this file, then no malloc would be required by
28847       ** findInodeInfo(). If this is the case, it is quite safe to close
28848       ** handle h - as it is guaranteed that no posix locks will be released
28849       ** by doing so.
28850       **
28851       ** If scenario (a) caused the error then things are not so safe. The
28852       ** implicit assumption here is that if fstat() fails, things are in
28853       ** such bad shape that dropping a lock or two doesn't matter much.
28854       */
28855       robust_close(pNew, h, __LINE__);
28856       h = -1;
28857     }
28858     unixLeaveMutex();
28859   }
28860 
28861 #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
28862   else if( pLockingStyle == &afpIoMethods ){
28863     /* AFP locking uses the file path so it needs to be included in
28864     ** the afpLockingContext.
28865     */
28866     afpLockingContext *pCtx;
28867     pNew->lockingContext = pCtx = sqlite3_malloc( sizeof(*pCtx) );
28868     if( pCtx==0 ){
28869       rc = SQLITE_NOMEM;
28870     }else{
28871       /* NB: zFilename exists and remains valid until the file is closed
28872       ** according to requirement F11141.  So we do not need to make a
28873       ** copy of the filename. */
28874       pCtx->dbPath = zFilename;
28875       pCtx->reserved = 0;
28876       srandomdev();
28877       unixEnterMutex();
28878       rc = findInodeInfo(pNew, &pNew->pInode);
28879       if( rc!=SQLITE_OK ){
28880         sqlite3_free(pNew->lockingContext);
28881         robust_close(pNew, h, __LINE__);
28882         h = -1;
28883       }
28884       unixLeaveMutex();
28885     }
28886   }
28887 #endif
28888 
28889   else if( pLockingStyle == &dotlockIoMethods ){
28890     /* Dotfile locking uses the file path so it needs to be included in
28891     ** the dotlockLockingContext
28892     */
28893     char *zLockFile;
28894     int nFilename;
28895     assert( zFilename!=0 );
28896     nFilename = (int)strlen(zFilename) + 6;
28897     zLockFile = (char *)sqlite3_malloc(nFilename);
28898     if( zLockFile==0 ){
28899       rc = SQLITE_NOMEM;
28900     }else{
28901       sqlite3_snprintf(nFilename, zLockFile, "%s" DOTLOCK_SUFFIX, zFilename);
28902     }
28903     pNew->lockingContext = zLockFile;
28904   }
28905 
28906 #if OS_VXWORKS
28907   else if( pLockingStyle == &semIoMethods ){
28908     /* Named semaphore locking uses the file path so it needs to be
28909     ** included in the semLockingContext
28910     */
28911     unixEnterMutex();
28912     rc = findInodeInfo(pNew, &pNew->pInode);
28913     if( (rc==SQLITE_OK) && (pNew->pInode->pSem==NULL) ){
28914       char *zSemName = pNew->pInode->aSemName;
28915       int n;
28916       sqlite3_snprintf(MAX_PATHNAME, zSemName, "/%s.sem",
28917                        pNew->pId->zCanonicalName);
28918       for( n=1; zSemName[n]; n++ )
28919         if( zSemName[n]=='/' ) zSemName[n] = '_';
28920       pNew->pInode->pSem = sem_open(zSemName, O_CREAT, 0666, 1);
28921       if( pNew->pInode->pSem == SEM_FAILED ){
28922         rc = SQLITE_NOMEM;
28923         pNew->pInode->aSemName[0] = '\0';
28924       }
28925     }
28926     unixLeaveMutex();
28927   }
28928 #endif
28929 
28930   pNew->lastErrno = 0;
28931 #if OS_VXWORKS
28932   if( rc!=SQLITE_OK ){
28933     if( h>=0 ) robust_close(pNew, h, __LINE__);
28934     h = -1;
28935     osUnlink(zFilename);
28936     pNew->ctrlFlags |= UNIXFILE_DELETE;
28937   }
28938 #endif
28939   if( rc!=SQLITE_OK ){
28940     if( h>=0 ) robust_close(pNew, h, __LINE__);
28941   }else{
28942     pNew->pMethod = pLockingStyle;
28943     OpenCounter(+1);
28944     verifyDbFile(pNew);
28945   }
28946   return rc;
28947 }
28948 
28949 /*
28950 ** Return the name of a directory in which to put temporary files.
28951 ** If no suitable temporary file directory can be found, return NULL.
28952 */
28953 static const char *unixTempFileDir(void){
28954   static const char *azDirs[] = {
28955      0,
28956      0,
28957      0,
28958      "/var/tmp",
28959      "/usr/tmp",
28960      "/tmp",
28961      0        /* List terminator */
28962   };
28963   unsigned int i;
28964   struct stat buf;
28965   const char *zDir = 0;
28966 
28967   azDirs[0] = sqlite3_temp_directory;
28968   if( !azDirs[1] ) azDirs[1] = getenv("SQLITE_TMPDIR");
28969   if( !azDirs[2] ) azDirs[2] = getenv("TMPDIR");
28970   for(i=0; i<sizeof(azDirs)/sizeof(azDirs[0]); zDir=azDirs[i++]){
28971     if( zDir==0 ) continue;
28972     if( osStat(zDir, &buf) ) continue;
28973     if( !S_ISDIR(buf.st_mode) ) continue;
28974     if( osAccess(zDir, 07) ) continue;
28975     break;
28976   }
28977   return zDir;
28978 }
28979 
28980 /*
28981 ** Create a temporary file name in zBuf.  zBuf must be allocated
28982 ** by the calling process and must be big enough to hold at least
28983 ** pVfs->mxPathname bytes.
28984 */
28985 static int unixGetTempname(int nBuf, char *zBuf){
28986   static const unsigned char zChars[] =
28987     "abcdefghijklmnopqrstuvwxyz"
28988     "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
28989     "0123456789";
28990   unsigned int i, j;
28991   const char *zDir;
28992 
28993   /* It's odd to simulate an io-error here, but really this is just
28994   ** using the io-error infrastructure to test that SQLite handles this
28995   ** function failing.
28996   */
28997   SimulateIOError( return SQLITE_IOERR );
28998 
28999   zDir = unixTempFileDir();
29000   if( zDir==0 ) zDir = ".";
29001 
29002   /* Check that the output buffer is large enough for the temporary file
29003   ** name. If it is not, return SQLITE_ERROR.
29004   */
29005   if( (strlen(zDir) + strlen(SQLITE_TEMP_FILE_PREFIX) + 18) >= (size_t)nBuf ){
29006     return SQLITE_ERROR;
29007   }
29008 
29009   do{
29010     sqlite3_snprintf(nBuf-18, zBuf, "%s/"SQLITE_TEMP_FILE_PREFIX, zDir);
29011     j = (int)strlen(zBuf);
29012     sqlite3_randomness(15, &zBuf[j]);
29013     for(i=0; i<15; i++, j++){
29014       zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
29015     }
29016     zBuf[j] = 0;
29017     zBuf[j+1] = 0;
29018   }while( osAccess(zBuf,0)==0 );
29019   return SQLITE_OK;
29020 }
29021 
29022 #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
29023 /*
29024 ** Routine to transform a unixFile into a proxy-locking unixFile.
29025 ** Implementation in the proxy-lock division, but used by unixOpen()
29026 ** if SQLITE_PREFER_PROXY_LOCKING is defined.
29027 */
29028 static int proxyTransformUnixFile(unixFile*, const char*);
29029 #endif
29030 
29031 /*
29032 ** Search for an unused file descriptor that was opened on the database
29033 ** file (not a journal or master-journal file) identified by pathname
29034 ** zPath with SQLITE_OPEN_XXX flags matching those passed as the second
29035 ** argument to this function.
29036 **
29037 ** Such a file descriptor may exist if a database connection was closed
29038 ** but the associated file descriptor could not be closed because some
29039 ** other file descriptor open on the same file is holding a file-lock.
29040 ** Refer to comments in the unixClose() function and the lengthy comment
29041 ** describing "Posix Advisory Locking" at the start of this file for
29042 ** further details. Also, ticket #4018.
29043 **
29044 ** If a suitable file descriptor is found, then it is returned. If no
29045 ** such file descriptor is located, -1 is returned.
29046 */
29047 static UnixUnusedFd *findReusableFd(const char *zPath, int flags){
29048   UnixUnusedFd *pUnused = 0;
29049 
29050   /* Do not search for an unused file descriptor on vxworks. Not because
29051   ** vxworks would not benefit from the change (it might, we're not sure),
29052   ** but because no way to test it is currently available. It is better
29053   ** not to risk breaking vxworks support for the sake of such an obscure
29054   ** feature.  */
29055 #if !OS_VXWORKS
29056   struct stat sStat;                   /* Results of stat() call */
29057 
29058   /* A stat() call may fail for various reasons. If this happens, it is
29059   ** almost certain that an open() call on the same path will also fail.
29060   ** For this reason, if an error occurs in the stat() call here, it is
29061   ** ignored and -1 is returned. The caller will try to open a new file
29062   ** descriptor on the same path, fail, and return an error to SQLite.
29063   **
29064   ** Even if a subsequent open() call does succeed, the consequences of
29065   ** not searching for a resusable file descriptor are not dire.  */
29066   if( 0==osStat(zPath, &sStat) ){
29067     unixInodeInfo *pInode;
29068 
29069     unixEnterMutex();
29070     pInode = inodeList;
29071     while( pInode && (pInode->fileId.dev!=sStat.st_dev
29072                      || pInode->fileId.ino!=sStat.st_ino) ){
29073        pInode = pInode->pNext;
29074     }
29075     if( pInode ){
29076       UnixUnusedFd **pp;
29077       for(pp=&pInode->pUnused; *pp && (*pp)->flags!=flags; pp=&((*pp)->pNext));
29078       pUnused = *pp;
29079       if( pUnused ){
29080         *pp = pUnused->pNext;
29081       }
29082     }
29083     unixLeaveMutex();
29084   }
29085 #endif    /* if !OS_VXWORKS */
29086   return pUnused;
29087 }
29088 
29089 /*
29090 ** This function is called by unixOpen() to determine the unix permissions
29091 ** to create new files with. If no error occurs, then SQLITE_OK is returned
29092 ** and a value suitable for passing as the third argument to open(2) is
29093 ** written to *pMode. If an IO error occurs, an SQLite error code is
29094 ** returned and the value of *pMode is not modified.
29095 **
29096 ** In most cases cases, this routine sets *pMode to 0, which will become
29097 ** an indication to robust_open() to create the file using
29098 ** SQLITE_DEFAULT_FILE_PERMISSIONS adjusted by the umask.
29099 ** But if the file being opened is a WAL or regular journal file, then
29100 ** this function queries the file-system for the permissions on the
29101 ** corresponding database file and sets *pMode to this value. Whenever
29102 ** possible, WAL and journal files are created using the same permissions
29103 ** as the associated database file.
29104 **
29105 ** If the SQLITE_ENABLE_8_3_NAMES option is enabled, then the
29106 ** original filename is unavailable.  But 8_3_NAMES is only used for
29107 ** FAT filesystems and permissions do not matter there, so just use
29108 ** the default permissions.
29109 */
29110 static int findCreateFileMode(
29111   const char *zPath,              /* Path of file (possibly) being created */
29112   int flags,                      /* Flags passed as 4th argument to xOpen() */
29113   mode_t *pMode,                  /* OUT: Permissions to open file with */
29114   uid_t *pUid,                    /* OUT: uid to set on the file */
29115   gid_t *pGid                     /* OUT: gid to set on the file */
29116 ){
29117   int rc = SQLITE_OK;             /* Return Code */
29118   *pMode = 0;
29119   *pUid = 0;
29120   *pGid = 0;
29121   if( flags & (SQLITE_OPEN_WAL|SQLITE_OPEN_MAIN_JOURNAL) ){
29122     char zDb[MAX_PATHNAME+1];     /* Database file path */
29123     int nDb;                      /* Number of valid bytes in zDb */
29124     struct stat sStat;            /* Output of stat() on database file */
29125 
29126     /* zPath is a path to a WAL or journal file. The following block derives
29127     ** the path to the associated database file from zPath. This block handles
29128     ** the following naming conventions:
29129     **
29130     **   "<path to db>-journal"
29131     **   "<path to db>-wal"
29132     **   "<path to db>-journalNN"
29133     **   "<path to db>-walNN"
29134     **
29135     ** where NN is a decimal number. The NN naming schemes are
29136     ** used by the test_multiplex.c module.
29137     */
29138     nDb = sqlite3Strlen30(zPath) - 1;
29139 #ifdef SQLITE_ENABLE_8_3_NAMES
29140     while( nDb>0 && sqlite3Isalnum(zPath[nDb]) ) nDb--;
29141     if( nDb==0 || zPath[nDb]!='-' ) return SQLITE_OK;
29142 #else
29143     while( zPath[nDb]!='-' ){
29144       assert( nDb>0 );
29145       assert( zPath[nDb]!='\n' );
29146       nDb--;
29147     }
29148 #endif
29149     memcpy(zDb, zPath, nDb);
29150     zDb[nDb] = '\0';
29151 
29152     if( 0==osStat(zDb, &sStat) ){
29153       *pMode = sStat.st_mode & 0777;
29154       *pUid = sStat.st_uid;
29155       *pGid = sStat.st_gid;
29156     }else{
29157       rc = SQLITE_IOERR_FSTAT;
29158     }
29159   }else if( flags & SQLITE_OPEN_DELETEONCLOSE ){
29160     *pMode = 0600;
29161   }
29162   return rc;
29163 }
29164 
29165 /*
29166 ** Open the file zPath.
29167 **
29168 ** Previously, the SQLite OS layer used three functions in place of this
29169 ** one:
29170 **
29171 **     sqlite3OsOpenReadWrite();
29172 **     sqlite3OsOpenReadOnly();
29173 **     sqlite3OsOpenExclusive();
29174 **
29175 ** These calls correspond to the following combinations of flags:
29176 **
29177 **     ReadWrite() ->     (READWRITE | CREATE)
29178 **     ReadOnly()  ->     (READONLY)
29179 **     OpenExclusive() -> (READWRITE | CREATE | EXCLUSIVE)
29180 **
29181 ** The old OpenExclusive() accepted a boolean argument - "delFlag". If
29182 ** true, the file was configured to be automatically deleted when the
29183 ** file handle closed. To achieve the same effect using this new
29184 ** interface, add the DELETEONCLOSE flag to those specified above for
29185 ** OpenExclusive().
29186 */
29187 static int unixOpen(
29188   sqlite3_vfs *pVfs,           /* The VFS for which this is the xOpen method */
29189   const char *zPath,           /* Pathname of file to be opened */
29190   sqlite3_file *pFile,         /* The file descriptor to be filled in */
29191   int flags,                   /* Input flags to control the opening */
29192   int *pOutFlags               /* Output flags returned to SQLite core */
29193 ){
29194   unixFile *p = (unixFile *)pFile;
29195   int fd = -1;                   /* File descriptor returned by open() */
29196   int openFlags = 0;             /* Flags to pass to open() */
29197   int eType = flags&0xFFFFFF00;  /* Type of file to open */
29198   int noLock;                    /* True to omit locking primitives */
29199   int rc = SQLITE_OK;            /* Function Return Code */
29200   int ctrlFlags = 0;             /* UNIXFILE_* flags */
29201 
29202   int isExclusive  = (flags & SQLITE_OPEN_EXCLUSIVE);
29203   int isDelete     = (flags & SQLITE_OPEN_DELETEONCLOSE);
29204   int isCreate     = (flags & SQLITE_OPEN_CREATE);
29205   int isReadonly   = (flags & SQLITE_OPEN_READONLY);
29206   int isReadWrite  = (flags & SQLITE_OPEN_READWRITE);
29207 #if SQLITE_ENABLE_LOCKING_STYLE
29208   int isAutoProxy  = (flags & SQLITE_OPEN_AUTOPROXY);
29209 #endif
29210 #if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE
29211   struct statfs fsInfo;
29212 #endif
29213 
29214   /* If creating a master or main-file journal, this function will open
29215   ** a file-descriptor on the directory too. The first time unixSync()
29216   ** is called the directory file descriptor will be fsync()ed and close()d.
29217   */
29218   int syncDir = (isCreate && (
29219         eType==SQLITE_OPEN_MASTER_JOURNAL
29220      || eType==SQLITE_OPEN_MAIN_JOURNAL
29221      || eType==SQLITE_OPEN_WAL
29222   ));
29223 
29224   /* If argument zPath is a NULL pointer, this function is required to open
29225   ** a temporary file. Use this buffer to store the file name in.
29226   */
29227   char zTmpname[MAX_PATHNAME+2];
29228   const char *zName = zPath;
29229 
29230   /* Check the following statements are true:
29231   **
29232   **   (a) Exactly one of the READWRITE and READONLY flags must be set, and
29233   **   (b) if CREATE is set, then READWRITE must also be set, and
29234   **   (c) if EXCLUSIVE is set, then CREATE must also be set.
29235   **   (d) if DELETEONCLOSE is set, then CREATE must also be set.
29236   */
29237   assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly));
29238   assert(isCreate==0 || isReadWrite);
29239   assert(isExclusive==0 || isCreate);
29240   assert(isDelete==0 || isCreate);
29241 
29242   /* The main DB, main journal, WAL file and master journal are never
29243   ** automatically deleted. Nor are they ever temporary files.  */
29244   assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_DB );
29245   assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_JOURNAL );
29246   assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MASTER_JOURNAL );
29247   assert( (!isDelete && zName) || eType!=SQLITE_OPEN_WAL );
29248 
29249   /* Assert that the upper layer has set one of the "file-type" flags. */
29250   assert( eType==SQLITE_OPEN_MAIN_DB      || eType==SQLITE_OPEN_TEMP_DB
29251        || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL
29252        || eType==SQLITE_OPEN_SUBJOURNAL   || eType==SQLITE_OPEN_MASTER_JOURNAL
29253        || eType==SQLITE_OPEN_TRANSIENT_DB || eType==SQLITE_OPEN_WAL
29254   );
29255 
29256   /* Detect a pid change and reset the PRNG.  There is a race condition
29257   ** here such that two or more threads all trying to open databases at
29258   ** the same instant might all reset the PRNG.  But multiple resets
29259   ** are harmless.
29260   */
29261   if( randomnessPid!=getpid() ){
29262     randomnessPid = getpid();
29263     sqlite3_randomness(0,0);
29264   }
29265 
29266   memset(p, 0, sizeof(unixFile));
29267 
29268   if( eType==SQLITE_OPEN_MAIN_DB ){
29269     UnixUnusedFd *pUnused;
29270     pUnused = findReusableFd(zName, flags);
29271     if( pUnused ){
29272       fd = pUnused->fd;
29273     }else{
29274       pUnused = sqlite3_malloc(sizeof(*pUnused));
29275       if( !pUnused ){
29276         return SQLITE_NOMEM;
29277       }
29278     }
29279     p->pUnused = pUnused;
29280 
29281     /* Database filenames are double-zero terminated if they are not
29282     ** URIs with parameters.  Hence, they can always be passed into
29283     ** sqlite3_uri_parameter(). */
29284     assert( (flags & SQLITE_OPEN_URI) || zName[strlen(zName)+1]==0 );
29285 
29286   }else if( !zName ){
29287     /* If zName is NULL, the upper layer is requesting a temp file. */
29288     assert(isDelete && !syncDir);
29289     rc = unixGetTempname(MAX_PATHNAME+2, zTmpname);
29290     if( rc!=SQLITE_OK ){
29291       return rc;
29292     }
29293     zName = zTmpname;
29294 
29295     /* Generated temporary filenames are always double-zero terminated
29296     ** for use by sqlite3_uri_parameter(). */
29297     assert( zName[strlen(zName)+1]==0 );
29298   }
29299 
29300   /* Determine the value of the flags parameter passed to POSIX function
29301   ** open(). These must be calculated even if open() is not called, as
29302   ** they may be stored as part of the file handle and used by the
29303   ** 'conch file' locking functions later on.  */
29304   if( isReadonly )  openFlags |= O_RDONLY;
29305   if( isReadWrite ) openFlags |= O_RDWR;
29306   if( isCreate )    openFlags |= O_CREAT;
29307   if( isExclusive ) openFlags |= (O_EXCL|O_NOFOLLOW);
29308   openFlags |= (O_LARGEFILE|O_BINARY);
29309 
29310   if( fd<0 ){
29311     mode_t openMode;              /* Permissions to create file with */
29312     uid_t uid;                    /* Userid for the file */
29313     gid_t gid;                    /* Groupid for the file */
29314     rc = findCreateFileMode(zName, flags, &openMode, &uid, &gid);
29315     if( rc!=SQLITE_OK ){
29316       assert( !p->pUnused );
29317       assert( eType==SQLITE_OPEN_WAL || eType==SQLITE_OPEN_MAIN_JOURNAL );
29318       return rc;
29319     }
29320     fd = robust_open(zName, openFlags, openMode);
29321     OSTRACE(("OPENX   %-3d %s 0%o\n", fd, zName, openFlags));
29322     if( fd<0 && errno!=EISDIR && isReadWrite && !isExclusive ){
29323       /* Failed to open the file for read/write access. Try read-only. */
29324       flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE);
29325       openFlags &= ~(O_RDWR|O_CREAT);
29326       flags |= SQLITE_OPEN_READONLY;
29327       openFlags |= O_RDONLY;
29328       isReadonly = 1;
29329       fd = robust_open(zName, openFlags, openMode);
29330     }
29331     if( fd<0 ){
29332       rc = unixLogError(SQLITE_CANTOPEN_BKPT, "open", zName);
29333       goto open_finished;
29334     }
29335 
29336     /* If this process is running as root and if creating a new rollback
29337     ** journal or WAL file, set the ownership of the journal or WAL to be
29338     ** the same as the original database.
29339     */
29340     if( flags & (SQLITE_OPEN_WAL|SQLITE_OPEN_MAIN_JOURNAL) ){
29341       osFchown(fd, uid, gid);
29342     }
29343   }
29344   assert( fd>=0 );
29345   if( pOutFlags ){
29346     *pOutFlags = flags;
29347   }
29348 
29349   if( p->pUnused ){
29350     p->pUnused->fd = fd;
29351     p->pUnused->flags = flags;
29352   }
29353 
29354   if( isDelete ){
29355 #if OS_VXWORKS
29356     zPath = zName;
29357 #else
29358     osUnlink(zName);
29359 #endif
29360   }
29361 #if SQLITE_ENABLE_LOCKING_STYLE
29362   else{
29363     p->openFlags = openFlags;
29364   }
29365 #endif
29366 
29367   noLock = eType!=SQLITE_OPEN_MAIN_DB;
29368 
29369 
29370 #if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE
29371   if( fstatfs(fd, &fsInfo) == -1 ){
29372     ((unixFile*)pFile)->lastErrno = errno;
29373     robust_close(p, fd, __LINE__);
29374     return SQLITE_IOERR_ACCESS;
29375   }
29376   if (0 == strncmp("msdos", fsInfo.f_fstypename, 5)) {
29377     ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS;
29378   }
29379 #endif
29380 
29381   /* Set up appropriate ctrlFlags */
29382   if( isDelete )                ctrlFlags |= UNIXFILE_DELETE;
29383   if( isReadonly )              ctrlFlags |= UNIXFILE_RDONLY;
29384   if( noLock )                  ctrlFlags |= UNIXFILE_NOLOCK;
29385   if( syncDir )                 ctrlFlags |= UNIXFILE_DIRSYNC;
29386   if( flags & SQLITE_OPEN_URI ) ctrlFlags |= UNIXFILE_URI;
29387 
29388 #if SQLITE_ENABLE_LOCKING_STYLE
29389 #if SQLITE_PREFER_PROXY_LOCKING
29390   isAutoProxy = 1;
29391 #endif
29392   if( isAutoProxy && (zPath!=NULL) && (!noLock) && pVfs->xOpen ){
29393     char *envforce = getenv("SQLITE_FORCE_PROXY_LOCKING");
29394     int useProxy = 0;
29395 
29396     /* SQLITE_FORCE_PROXY_LOCKING==1 means force always use proxy, 0 means
29397     ** never use proxy, NULL means use proxy for non-local files only.  */
29398     if( envforce!=NULL ){
29399       useProxy = atoi(envforce)>0;
29400     }else{
29401       if( statfs(zPath, &fsInfo) == -1 ){
29402         /* In theory, the close(fd) call is sub-optimal. If the file opened
29403         ** with fd is a database file, and there are other connections open
29404         ** on that file that are currently holding advisory locks on it,
29405         ** then the call to close() will cancel those locks. In practice,
29406         ** we're assuming that statfs() doesn't fail very often. At least
29407         ** not while other file descriptors opened by the same process on
29408         ** the same file are working.  */
29409         p->lastErrno = errno;
29410         robust_close(p, fd, __LINE__);
29411         rc = SQLITE_IOERR_ACCESS;
29412         goto open_finished;
29413       }
29414       useProxy = !(fsInfo.f_flags&MNT_LOCAL);
29415     }
29416     if( useProxy ){
29417       rc = fillInUnixFile(pVfs, fd, pFile, zPath, ctrlFlags);
29418       if( rc==SQLITE_OK ){
29419         rc = proxyTransformUnixFile((unixFile*)pFile, ":auto:");
29420         if( rc!=SQLITE_OK ){
29421           /* Use unixClose to clean up the resources added in fillInUnixFile
29422           ** and clear all the structure's references.  Specifically,
29423           ** pFile->pMethods will be NULL so sqlite3OsClose will be a no-op
29424           */
29425           unixClose(pFile);
29426           return rc;
29427         }
29428       }
29429       goto open_finished;
29430     }
29431   }
29432 #endif
29433 
29434   rc = fillInUnixFile(pVfs, fd, pFile, zPath, ctrlFlags);
29435 
29436 open_finished:
29437   if( rc!=SQLITE_OK ){
29438     sqlite3_free(p->pUnused);
29439   }
29440   return rc;
29441 }
29442 
29443 
29444 /*
29445 ** Delete the file at zPath. If the dirSync argument is true, fsync()
29446 ** the directory after deleting the file.
29447 */
29448 static int unixDelete(
29449   sqlite3_vfs *NotUsed,     /* VFS containing this as the xDelete method */
29450   const char *zPath,        /* Name of file to be deleted */
29451   int dirSync               /* If true, fsync() directory after deleting file */
29452 ){
29453   int rc = SQLITE_OK;
29454   UNUSED_PARAMETER(NotUsed);
29455   SimulateIOError(return SQLITE_IOERR_DELETE);
29456   if( osUnlink(zPath)==(-1) ){
29457     if( errno==ENOENT ){
29458       rc = SQLITE_IOERR_DELETE_NOENT;
29459     }else{
29460       rc = unixLogError(SQLITE_IOERR_DELETE, "unlink", zPath);
29461     }
29462     return rc;
29463   }
29464 #ifndef SQLITE_DISABLE_DIRSYNC
29465   if( (dirSync & 1)!=0 ){
29466     int fd;
29467     rc = osOpenDirectory(zPath, &fd);
29468     if( rc==SQLITE_OK ){
29469 #if OS_VXWORKS
29470       if( fsync(fd)==-1 )
29471 #else
29472       if( fsync(fd) )
29473 #endif
29474       {
29475         rc = unixLogError(SQLITE_IOERR_DIR_FSYNC, "fsync", zPath);
29476       }
29477       robust_close(0, fd, __LINE__);
29478     }else if( rc==SQLITE_CANTOPEN ){
29479       rc = SQLITE_OK;
29480     }
29481   }
29482 #endif
29483   return rc;
29484 }
29485 
29486 /*
29487 ** Test the existence of or access permissions of file zPath. The
29488 ** test performed depends on the value of flags:
29489 **
29490 **     SQLITE_ACCESS_EXISTS: Return 1 if the file exists
29491 **     SQLITE_ACCESS_READWRITE: Return 1 if the file is read and writable.
29492 **     SQLITE_ACCESS_READONLY: Return 1 if the file is readable.
29493 **
29494 ** Otherwise return 0.
29495 */
29496 static int unixAccess(
29497   sqlite3_vfs *NotUsed,   /* The VFS containing this xAccess method */
29498   const char *zPath,      /* Path of the file to examine */
29499   int flags,              /* What do we want to learn about the zPath file? */
29500   int *pResOut            /* Write result boolean here */
29501 ){
29502   int amode = 0;
29503   UNUSED_PARAMETER(NotUsed);
29504   SimulateIOError( return SQLITE_IOERR_ACCESS; );
29505   switch( flags ){
29506     case SQLITE_ACCESS_EXISTS:
29507       amode = F_OK;
29508       break;
29509     case SQLITE_ACCESS_READWRITE:
29510       amode = W_OK|R_OK;
29511       break;
29512     case SQLITE_ACCESS_READ:
29513       amode = R_OK;
29514       break;
29515 
29516     default:
29517       assert(!"Invalid flags argument");
29518   }
29519   *pResOut = (osAccess(zPath, amode)==0);
29520   if( flags==SQLITE_ACCESS_EXISTS && *pResOut ){
29521     struct stat buf;
29522     if( 0==osStat(zPath, &buf) && buf.st_size==0 ){
29523       *pResOut = 0;
29524     }
29525   }
29526   return SQLITE_OK;
29527 }
29528 
29529 
29530 /*
29531 ** Turn a relative pathname into a full pathname. The relative path
29532 ** is stored as a nul-terminated string in the buffer pointed to by
29533 ** zPath.
29534 **
29535 ** zOut points to a buffer of at least sqlite3_vfs.mxPathname bytes
29536 ** (in this case, MAX_PATHNAME bytes). The full-path is written to
29537 ** this buffer before returning.
29538 */
29539 static int unixFullPathname(
29540   sqlite3_vfs *pVfs,            /* Pointer to vfs object */
29541   const char *zPath,            /* Possibly relative input path */
29542   int nOut,                     /* Size of output buffer in bytes */
29543   char *zOut                    /* Output buffer */
29544 ){
29545 
29546   /* It's odd to simulate an io-error here, but really this is just
29547   ** using the io-error infrastructure to test that SQLite handles this
29548   ** function failing. This function could fail if, for example, the
29549   ** current working directory has been unlinked.
29550   */
29551   SimulateIOError( return SQLITE_ERROR );
29552 
29553   assert( pVfs->mxPathname==MAX_PATHNAME );
29554   UNUSED_PARAMETER(pVfs);
29555 
29556   zOut[nOut-1] = '\0';
29557   if( zPath[0]=='/' ){
29558     sqlite3_snprintf(nOut, zOut, "%s", zPath);
29559   }else{
29560     int nCwd;
29561     if( osGetcwd(zOut, nOut-1)==0 ){
29562       return unixLogError(SQLITE_CANTOPEN_BKPT, "getcwd", zPath);
29563     }
29564     nCwd = (int)strlen(zOut);
29565     sqlite3_snprintf(nOut-nCwd, &zOut[nCwd], "/%s", zPath);
29566   }
29567   return SQLITE_OK;
29568 }
29569 
29570 
29571 #ifndef SQLITE_OMIT_LOAD_EXTENSION
29572 /*
29573 ** Interfaces for opening a shared library, finding entry points
29574 ** within the shared library, and closing the shared library.
29575 */
29576 #include <dlfcn.h>
29577 static void *unixDlOpen(sqlite3_vfs *NotUsed, const char *zFilename){
29578   UNUSED_PARAMETER(NotUsed);
29579   return dlopen(zFilename, RTLD_NOW | RTLD_GLOBAL);
29580 }
29581 
29582 /*
29583 ** SQLite calls this function immediately after a call to unixDlSym() or
29584 ** unixDlOpen() fails (returns a null pointer). If a more detailed error
29585 ** message is available, it is written to zBufOut. If no error message
29586 ** is available, zBufOut is left unmodified and SQLite uses a default
29587 ** error message.
29588 */
29589 static void unixDlError(sqlite3_vfs *NotUsed, int nBuf, char *zBufOut){
29590   const char *zErr;
29591   UNUSED_PARAMETER(NotUsed);
29592   unixEnterMutex();
29593   zErr = dlerror();
29594   if( zErr ){
29595     sqlite3_snprintf(nBuf, zBufOut, "%s", zErr);
29596   }
29597   unixLeaveMutex();
29598 }
29599 static void (*unixDlSym(sqlite3_vfs *NotUsed, void *p, const char*zSym))(void){
29600   /*
29601   ** GCC with -pedantic-errors says that C90 does not allow a void* to be
29602   ** cast into a pointer to a function.  And yet the library dlsym() routine
29603   ** returns a void* which is really a pointer to a function.  So how do we
29604   ** use dlsym() with -pedantic-errors?
29605   **
29606   ** Variable x below is defined to be a pointer to a function taking
29607   ** parameters void* and const char* and returning a pointer to a function.
29608   ** We initialize x by assigning it a pointer to the dlsym() function.
29609   ** (That assignment requires a cast.)  Then we call the function that
29610   ** x points to.
29611   **
29612   ** This work-around is unlikely to work correctly on any system where
29613   ** you really cannot cast a function pointer into void*.  But then, on the
29614   ** other hand, dlsym() will not work on such a system either, so we have
29615   ** not really lost anything.
29616   */
29617   void (*(*x)(void*,const char*))(void);
29618   UNUSED_PARAMETER(NotUsed);
29619   x = (void(*(*)(void*,const char*))(void))dlsym;
29620   return (*x)(p, zSym);
29621 }
29622 static void unixDlClose(sqlite3_vfs *NotUsed, void *pHandle){
29623   UNUSED_PARAMETER(NotUsed);
29624   dlclose(pHandle);
29625 }
29626 #else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */
29627   #define unixDlOpen  0
29628   #define unixDlError 0
29629   #define unixDlSym   0
29630   #define unixDlClose 0
29631 #endif
29632 
29633 /*
29634 ** Write nBuf bytes of random data to the supplied buffer zBuf.
29635 */
29636 static int unixRandomness(sqlite3_vfs *NotUsed, int nBuf, char *zBuf){
29637   UNUSED_PARAMETER(NotUsed);
29638   assert((size_t)nBuf>=(sizeof(time_t)+sizeof(int)));
29639 
29640   /* We have to initialize zBuf to prevent valgrind from reporting
29641   ** errors.  The reports issued by valgrind are incorrect - we would
29642   ** prefer that the randomness be increased by making use of the
29643   ** uninitialized space in zBuf - but valgrind errors tend to worry
29644   ** some users.  Rather than argue, it seems easier just to initialize
29645   ** the whole array and silence valgrind, even if that means less randomness
29646   ** in the random seed.
29647   **
29648   ** When testing, initializing zBuf[] to zero is all we do.  That means
29649   ** that we always use the same random number sequence.  This makes the
29650   ** tests repeatable.
29651   */
29652   memset(zBuf, 0, nBuf);
29653   randomnessPid = getpid();
29654 #if !defined(SQLITE_TEST)
29655   {
29656     int fd, got;
29657     fd = robust_open("/dev/urandom", O_RDONLY, 0);
29658     if( fd<0 ){
29659       time_t t;
29660       time(&t);
29661       memcpy(zBuf, &t, sizeof(t));
29662       memcpy(&zBuf[sizeof(t)], &randomnessPid, sizeof(randomnessPid));
29663       assert( sizeof(t)+sizeof(randomnessPid)<=(size_t)nBuf );
29664       nBuf = sizeof(t) + sizeof(randomnessPid);
29665     }else{
29666       do{ got = osRead(fd, zBuf, nBuf); }while( got<0 && errno==EINTR );
29667       robust_close(0, fd, __LINE__);
29668     }
29669   }
29670 #endif
29671   return nBuf;
29672 }
29673 
29674 
29675 /*
29676 ** Sleep for a little while.  Return the amount of time slept.
29677 ** The argument is the number of microseconds we want to sleep.
29678 ** The return value is the number of microseconds of sleep actually
29679 ** requested from the underlying operating system, a number which
29680 ** might be greater than or equal to the argument, but not less
29681 ** than the argument.
29682 */
29683 static int unixSleep(sqlite3_vfs *NotUsed, int microseconds){
29684 #if OS_VXWORKS
29685   struct timespec sp;
29686 
29687   sp.tv_sec = microseconds / 1000000;
29688   sp.tv_nsec = (microseconds % 1000000) * 1000;
29689   nanosleep(&sp, NULL);
29690   UNUSED_PARAMETER(NotUsed);
29691   return microseconds;
29692 #elif defined(HAVE_USLEEP) && HAVE_USLEEP
29693   usleep(microseconds);
29694   UNUSED_PARAMETER(NotUsed);
29695   return microseconds;
29696 #else
29697   int seconds = (microseconds+999999)/1000000;
29698   sleep(seconds);
29699   UNUSED_PARAMETER(NotUsed);
29700   return seconds*1000000;
29701 #endif
29702 }
29703 
29704 /*
29705 ** The following variable, if set to a non-zero value, is interpreted as
29706 ** the number of seconds since 1970 and is used to set the result of
29707 ** sqlite3OsCurrentTime() during testing.
29708 */
29709 #ifdef SQLITE_TEST
29710 SQLITE_API int sqlite3_current_time = 0;  /* Fake system time in seconds since 1970. */
29711 #endif
29712 
29713 /*
29714 ** Find the current time (in Universal Coordinated Time).  Write into *piNow
29715 ** the current time and date as a Julian Day number times 86_400_000.  In
29716 ** other words, write into *piNow the number of milliseconds since the Julian
29717 ** epoch of noon in Greenwich on November 24, 4714 B.C according to the
29718 ** proleptic Gregorian calendar.
29719 **
29720 ** On success, return SQLITE_OK.  Return SQLITE_ERROR if the time and date
29721 ** cannot be found.
29722 */
29723 static int unixCurrentTimeInt64(sqlite3_vfs *NotUsed, sqlite3_int64 *piNow){
29724   static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000;
29725   int rc = SQLITE_OK;
29726 #if defined(NO_GETTOD)
29727   time_t t;
29728   time(&t);
29729   *piNow = ((sqlite3_int64)t)*1000 + unixEpoch;
29730 #elif OS_VXWORKS
29731   struct timespec sNow;
29732   clock_gettime(CLOCK_REALTIME, &sNow);
29733   *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_nsec/1000000;
29734 #else
29735   struct timeval sNow;
29736   if( gettimeofday(&sNow, 0)==0 ){
29737     *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_usec/1000;
29738   }else{
29739     rc = SQLITE_ERROR;
29740   }
29741 #endif
29742 
29743 #ifdef SQLITE_TEST
29744   if( sqlite3_current_time ){
29745     *piNow = 1000*(sqlite3_int64)sqlite3_current_time + unixEpoch;
29746   }
29747 #endif
29748   UNUSED_PARAMETER(NotUsed);
29749   return rc;
29750 }
29751 
29752 /*
29753 ** Find the current time (in Universal Coordinated Time).  Write the
29754 ** current time and date as a Julian Day number into *prNow and
29755 ** return 0.  Return 1 if the time and date cannot be found.
29756 */
29757 static int unixCurrentTime(sqlite3_vfs *NotUsed, double *prNow){
29758   sqlite3_int64 i = 0;
29759   int rc;
29760   UNUSED_PARAMETER(NotUsed);
29761   rc = unixCurrentTimeInt64(0, &i);
29762   *prNow = i/86400000.0;
29763   return rc;
29764 }
29765 
29766 /*
29767 ** We added the xGetLastError() method with the intention of providing
29768 ** better low-level error messages when operating-system problems come up
29769 ** during SQLite operation.  But so far, none of that has been implemented
29770 ** in the core.  So this routine is never called.  For now, it is merely
29771 ** a place-holder.
29772 */
29773 static int unixGetLastError(sqlite3_vfs *NotUsed, int NotUsed2, char *NotUsed3){
29774   UNUSED_PARAMETER(NotUsed);
29775   UNUSED_PARAMETER(NotUsed2);
29776   UNUSED_PARAMETER(NotUsed3);
29777   return 0;
29778 }
29779 
29780 
29781 /*
29782 ************************ End of sqlite3_vfs methods ***************************
29783 ******************************************************************************/
29784 
29785 /******************************************************************************
29786 ************************** Begin Proxy Locking ********************************
29787 **
29788 ** Proxy locking is a "uber-locking-method" in this sense:  It uses the
29789 ** other locking methods on secondary lock files.  Proxy locking is a
29790 ** meta-layer over top of the primitive locking implemented above.  For
29791 ** this reason, the division that implements of proxy locking is deferred
29792 ** until late in the file (here) after all of the other I/O methods have
29793 ** been defined - so that the primitive locking methods are available
29794 ** as services to help with the implementation of proxy locking.
29795 **
29796 ****
29797 **
29798 ** The default locking schemes in SQLite use byte-range locks on the
29799 ** database file to coordinate safe, concurrent access by multiple readers
29800 ** and writers [http://sqlite.org/lockingv3.html].  The five file locking
29801 ** states (UNLOCKED, PENDING, SHARED, RESERVED, EXCLUSIVE) are implemented
29802 ** as POSIX read & write locks over fixed set of locations (via fsctl),
29803 ** on AFP and SMB only exclusive byte-range locks are available via fsctl
29804 ** with _IOWR('z', 23, struct ByteRangeLockPB2) to track the same 5 states.
29805 ** To simulate a F_RDLCK on the shared range, on AFP a randomly selected
29806 ** address in the shared range is taken for a SHARED lock, the entire
29807 ** shared range is taken for an EXCLUSIVE lock):
29808 **
29809 **      PENDING_BYTE        0x40000000
29810 **      RESERVED_BYTE       0x40000001
29811 **      SHARED_RANGE        0x40000002 -> 0x40000200
29812 **
29813 ** This works well on the local file system, but shows a nearly 100x
29814 ** slowdown in read performance on AFP because the AFP client disables
29815 ** the read cache when byte-range locks are present.  Enabling the read
29816 ** cache exposes a cache coherency problem that is present on all OS X
29817 ** supported network file systems.  NFS and AFP both observe the
29818 ** close-to-open semantics for ensuring cache coherency
29819 ** [http://nfs.sourceforge.net/#faq_a8], which does not effectively
29820 ** address the requirements for concurrent database access by multiple
29821 ** readers and writers
29822 ** [http://www.nabble.com/SQLite-on-NFS-cache-coherency-td15655701.html].
29823 **
29824 ** To address the performance and cache coherency issues, proxy file locking
29825 ** changes the way database access is controlled by limiting access to a
29826 ** single host at a time and moving file locks off of the database file
29827 ** and onto a proxy file on the local file system.
29828 **
29829 **
29830 ** Using proxy locks
29831 ** -----------------
29832 **
29833 ** C APIs
29834 **
29835 **  sqlite3_file_control(db, dbname, SQLITE_SET_LOCKPROXYFILE,
29836 **                       <proxy_path> | ":auto:");
29837 **  sqlite3_file_control(db, dbname, SQLITE_GET_LOCKPROXYFILE, &<proxy_path>);
29838 **
29839 **
29840 ** SQL pragmas
29841 **
29842 **  PRAGMA [database.]lock_proxy_file=<proxy_path> | :auto:
29843 **  PRAGMA [database.]lock_proxy_file
29844 **
29845 ** Specifying ":auto:" means that if there is a conch file with a matching
29846 ** host ID in it, the proxy path in the conch file will be used, otherwise
29847 ** a proxy path based on the user's temp dir
29848 ** (via confstr(_CS_DARWIN_USER_TEMP_DIR,...)) will be used and the
29849 ** actual proxy file name is generated from the name and path of the
29850 ** database file.  For example:
29851 **
29852 **       For database path "/Users/me/foo.db"
29853 **       The lock path will be "<tmpdir>/sqliteplocks/_Users_me_foo.db:auto:")
29854 **
29855 ** Once a lock proxy is configured for a database connection, it can not
29856 ** be removed, however it may be switched to a different proxy path via
29857 ** the above APIs (assuming the conch file is not being held by another
29858 ** connection or process).
29859 **
29860 **
29861 ** How proxy locking works
29862 ** -----------------------
29863 **
29864 ** Proxy file locking relies primarily on two new supporting files:
29865 **
29866 **   *  conch file to limit access to the database file to a single host
29867 **      at a time
29868 **
29869 **   *  proxy file to act as a proxy for the advisory locks normally
29870 **      taken on the database
29871 **
29872 ** The conch file - to use a proxy file, sqlite must first "hold the conch"
29873 ** by taking an sqlite-style shared lock on the conch file, reading the
29874 ** contents and comparing the host's unique host ID (see below) and lock
29875 ** proxy path against the values stored in the conch.  The conch file is
29876 ** stored in the same directory as the database file and the file name
29877 ** is patterned after the database file name as ".<databasename>-conch".
29878 ** If the conch file does not exist, or it's contents do not match the
29879 ** host ID and/or proxy path, then the lock is escalated to an exclusive
29880 ** lock and the conch file contents is updated with the host ID and proxy
29881 ** path and the lock is downgraded to a shared lock again.  If the conch
29882 ** is held by another process (with a shared lock), the exclusive lock
29883 ** will fail and SQLITE_BUSY is returned.
29884 **
29885 ** The proxy file - a single-byte file used for all advisory file locks
29886 ** normally taken on the database file.   This allows for safe sharing
29887 ** of the database file for multiple readers and writers on the same
29888 ** host (the conch ensures that they all use the same local lock file).
29889 **
29890 ** Requesting the lock proxy does not immediately take the conch, it is
29891 ** only taken when the first request to lock database file is made.
29892 ** This matches the semantics of the traditional locking behavior, where
29893 ** opening a connection to a database file does not take a lock on it.
29894 ** The shared lock and an open file descriptor are maintained until
29895 ** the connection to the database is closed.
29896 **
29897 ** The proxy file and the lock file are never deleted so they only need
29898 ** to be created the first time they are used.
29899 **
29900 ** Configuration options
29901 ** ---------------------
29902 **
29903 **  SQLITE_PREFER_PROXY_LOCKING
29904 **
29905 **       Database files accessed on non-local file systems are
29906 **       automatically configured for proxy locking, lock files are
29907 **       named automatically using the same logic as
29908 **       PRAGMA lock_proxy_file=":auto:"
29909 **
29910 **  SQLITE_PROXY_DEBUG
29911 **
29912 **       Enables the logging of error messages during host id file
29913 **       retrieval and creation
29914 **
29915 **  LOCKPROXYDIR
29916 **
29917 **       Overrides the default directory used for lock proxy files that
29918 **       are named automatically via the ":auto:" setting
29919 **
29920 **  SQLITE_DEFAULT_PROXYDIR_PERMISSIONS
29921 **
29922 **       Permissions to use when creating a directory for storing the
29923 **       lock proxy files, only used when LOCKPROXYDIR is not set.
29924 **
29925 **
29926 ** As mentioned above, when compiled with SQLITE_PREFER_PROXY_LOCKING,
29927 ** setting the environment variable SQLITE_FORCE_PROXY_LOCKING to 1 will
29928 ** force proxy locking to be used for every database file opened, and 0
29929 ** will force automatic proxy locking to be disabled for all database
29930 ** files (explicity calling the SQLITE_SET_LOCKPROXYFILE pragma or
29931 ** sqlite_file_control API is not affected by SQLITE_FORCE_PROXY_LOCKING).
29932 */
29933 
29934 /*
29935 ** Proxy locking is only available on MacOSX
29936 */
29937 #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE
29938 
29939 /*
29940 ** The proxyLockingContext has the path and file structures for the remote
29941 ** and local proxy files in it
29942 */
29943 typedef struct proxyLockingContext proxyLockingContext;
29944 struct proxyLockingContext {
29945   unixFile *conchFile;         /* Open conch file */
29946   char *conchFilePath;         /* Name of the conch file */
29947   unixFile *lockProxy;         /* Open proxy lock file */
29948   char *lockProxyPath;         /* Name of the proxy lock file */
29949   char *dbPath;                /* Name of the open file */
29950   int conchHeld;               /* 1 if the conch is held, -1 if lockless */
29951   void *oldLockingContext;     /* Original lockingcontext to restore on close */
29952   sqlite3_io_methods const *pOldMethod;     /* Original I/O methods for close */
29953 };
29954 
29955 /*
29956 ** The proxy lock file path for the database at dbPath is written into lPath,
29957 ** which must point to valid, writable memory large enough for a maxLen length
29958 ** file path.
29959 */
29960 static int proxyGetLockPath(const char *dbPath, char *lPath, size_t maxLen){
29961   int len;
29962   int dbLen;
29963   int i;
29964 
29965 #ifdef LOCKPROXYDIR
29966   len = strlcpy(lPath, LOCKPROXYDIR, maxLen);
29967 #else
29968 # ifdef _CS_DARWIN_USER_TEMP_DIR
29969   {
29970     if( !confstr(_CS_DARWIN_USER_TEMP_DIR, lPath, maxLen) ){
29971       OSTRACE(("GETLOCKPATH  failed %s errno=%d pid=%d\n",
29972                lPath, errno, getpid()));
29973       return SQLITE_IOERR_LOCK;
29974     }
29975     len = strlcat(lPath, "sqliteplocks", maxLen);
29976   }
29977 # else
29978   len = strlcpy(lPath, "/tmp/", maxLen);
29979 # endif
29980 #endif
29981 
29982   if( lPath[len-1]!='/' ){
29983     len = strlcat(lPath, "/", maxLen);
29984   }
29985 
29986   /* transform the db path to a unique cache name */
29987   dbLen = (int)strlen(dbPath);
29988   for( i=0; i<dbLen && (i+len+7)<(int)maxLen; i++){
29989     char c = dbPath[i];
29990     lPath[i+len] = (c=='/')?'_':c;
29991   }
29992   lPath[i+len]='\0';
29993   strlcat(lPath, ":auto:", maxLen);
29994   OSTRACE(("GETLOCKPATH  proxy lock path=%s pid=%d\n", lPath, getpid()));
29995   return SQLITE_OK;
29996 }
29997 
29998 /*
29999  ** Creates the lock file and any missing directories in lockPath
30000  */
30001 static int proxyCreateLockPath(const char *lockPath){
30002   int i, len;
30003   char buf[MAXPATHLEN];
30004   int start = 0;
30005 
30006   assert(lockPath!=NULL);
30007   /* try to create all the intermediate directories */
30008   len = (int)strlen(lockPath);
30009   buf[0] = lockPath[0];
30010   for( i=1; i<len; i++ ){
30011     if( lockPath[i] == '/' && (i - start > 0) ){
30012       /* only mkdir if leaf dir != "." or "/" or ".." */
30013       if( i-start>2 || (i-start==1 && buf[start] != '.' && buf[start] != '/')
30014          || (i-start==2 && buf[start] != '.' && buf[start+1] != '.') ){
30015         buf[i]='\0';
30016         if( osMkdir(buf, SQLITE_DEFAULT_PROXYDIR_PERMISSIONS) ){
30017           int err=errno;
30018           if( err!=EEXIST ) {
30019             OSTRACE(("CREATELOCKPATH  FAILED creating %s, "
30020                      "'%s' proxy lock path=%s pid=%d\n",
30021                      buf, strerror(err), lockPath, getpid()));
30022             return err;
30023           }
30024         }
30025       }
30026       start=i+1;
30027     }
30028     buf[i] = lockPath[i];
30029   }
30030   OSTRACE(("CREATELOCKPATH  proxy lock path=%s pid=%d\n", lockPath, getpid()));
30031   return 0;
30032 }
30033 
30034 /*
30035 ** Create a new VFS file descriptor (stored in memory obtained from
30036 ** sqlite3_malloc) and open the file named "path" in the file descriptor.
30037 **
30038 ** The caller is responsible not only for closing the file descriptor
30039 ** but also for freeing the memory associated with the file descriptor.
30040 */
30041 static int proxyCreateUnixFile(
30042     const char *path,        /* path for the new unixFile */
30043     unixFile **ppFile,       /* unixFile created and returned by ref */
30044     int islockfile           /* if non zero missing dirs will be created */
30045 ) {
30046   int fd = -1;
30047   unixFile *pNew;
30048   int rc = SQLITE_OK;
30049   int openFlags = O_RDWR | O_CREAT;
30050   sqlite3_vfs dummyVfs;
30051   int terrno = 0;
30052   UnixUnusedFd *pUnused = NULL;
30053 
30054   /* 1. first try to open/create the file
30055   ** 2. if that fails, and this is a lock file (not-conch), try creating
30056   ** the parent directories and then try again.
30057   ** 3. if that fails, try to open the file read-only
30058   ** otherwise return BUSY (if lock file) or CANTOPEN for the conch file
30059   */
30060   pUnused = findReusableFd(path, openFlags);
30061   if( pUnused ){
30062     fd = pUnused->fd;
30063   }else{
30064     pUnused = sqlite3_malloc(sizeof(*pUnused));
30065     if( !pUnused ){
30066       return SQLITE_NOMEM;
30067     }
30068   }
30069   if( fd<0 ){
30070     fd = robust_open(path, openFlags, 0);
30071     terrno = errno;
30072     if( fd<0 && errno==ENOENT && islockfile ){
30073       if( proxyCreateLockPath(path) == SQLITE_OK ){
30074         fd = robust_open(path, openFlags, 0);
30075       }
30076     }
30077   }
30078   if( fd<0 ){
30079     openFlags = O_RDONLY;
30080     fd = robust_open(path, openFlags, 0);
30081     terrno = errno;
30082   }
30083   if( fd<0 ){
30084     if( islockfile ){
30085       return SQLITE_BUSY;
30086     }
30087     switch (terrno) {
30088       case EACCES:
30089         return SQLITE_PERM;
30090       case EIO:
30091         return SQLITE_IOERR_LOCK; /* even though it is the conch */
30092       default:
30093         return SQLITE_CANTOPEN_BKPT;
30094     }
30095   }
30096 
30097   pNew = (unixFile *)sqlite3_malloc(sizeof(*pNew));
30098   if( pNew==NULL ){
30099     rc = SQLITE_NOMEM;
30100     goto end_create_proxy;
30101   }
30102   memset(pNew, 0, sizeof(unixFile));
30103   pNew->openFlags = openFlags;
30104   memset(&dummyVfs, 0, sizeof(dummyVfs));
30105   dummyVfs.pAppData = (void*)&autolockIoFinder;
30106   dummyVfs.zName = "dummy";
30107   pUnused->fd = fd;
30108   pUnused->flags = openFlags;
30109   pNew->pUnused = pUnused;
30110 
30111   rc = fillInUnixFile(&dummyVfs, fd, (sqlite3_file*)pNew, path, 0);
30112   if( rc==SQLITE_OK ){
30113     *ppFile = pNew;
30114     return SQLITE_OK;
30115   }
30116 end_create_proxy:
30117   robust_close(pNew, fd, __LINE__);
30118   sqlite3_free(pNew);
30119   sqlite3_free(pUnused);
30120   return rc;
30121 }
30122 
30123 #ifdef SQLITE_TEST
30124 /* simulate multiple hosts by creating unique hostid file paths */
30125 SQLITE_API int sqlite3_hostid_num = 0;
30126 #endif
30127 
30128 #define PROXY_HOSTIDLEN    16  /* conch file host id length */
30129 
30130 /* Not always defined in the headers as it ought to be */
30131 extern int gethostuuid(uuid_t id, const struct timespec *wait);
30132 
30133 /* get the host ID via gethostuuid(), pHostID must point to PROXY_HOSTIDLEN
30134 ** bytes of writable memory.
30135 */
30136 static int proxyGetHostID(unsigned char *pHostID, int *pError){
30137   assert(PROXY_HOSTIDLEN == sizeof(uuid_t));
30138   memset(pHostID, 0, PROXY_HOSTIDLEN);
30139 #if defined(__MAX_OS_X_VERSION_MIN_REQUIRED)\
30140                && __MAC_OS_X_VERSION_MIN_REQUIRED<1050
30141   {
30142     static const struct timespec timeout = {1, 0}; /* 1 sec timeout */
30143     if( gethostuuid(pHostID, &timeout) ){
30144       int err = errno;
30145       if( pError ){
30146         *pError = err;
30147       }
30148       return SQLITE_IOERR;
30149     }
30150   }
30151 #else
30152   UNUSED_PARAMETER(pError);
30153 #endif
30154 #ifdef SQLITE_TEST
30155   /* simulate multiple hosts by creating unique hostid file paths */
30156   if( sqlite3_hostid_num != 0){
30157     pHostID[0] = (char)(pHostID[0] + (char)(sqlite3_hostid_num & 0xFF));
30158   }
30159 #endif
30160 
30161   return SQLITE_OK;
30162 }
30163 
30164 /* The conch file contains the header, host id and lock file path
30165  */
30166 #define PROXY_CONCHVERSION 2   /* 1-byte header, 16-byte host id, path */
30167 #define PROXY_HEADERLEN    1   /* conch file header length */
30168 #define PROXY_PATHINDEX    (PROXY_HEADERLEN+PROXY_HOSTIDLEN)
30169 #define PROXY_MAXCONCHLEN  (PROXY_HEADERLEN+PROXY_HOSTIDLEN+MAXPATHLEN)
30170 
30171 /*
30172 ** Takes an open conch file, copies the contents to a new path and then moves
30173 ** it back.  The newly created file's file descriptor is assigned to the
30174 ** conch file structure and finally the original conch file descriptor is
30175 ** closed.  Returns zero if successful.
30176 */
30177 static int proxyBreakConchLock(unixFile *pFile, uuid_t myHostID){
30178   proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
30179   unixFile *conchFile = pCtx->conchFile;
30180   char tPath[MAXPATHLEN];
30181   char buf[PROXY_MAXCONCHLEN];
30182   char *cPath = pCtx->conchFilePath;
30183   size_t readLen = 0;
30184   size_t pathLen = 0;
30185   char errmsg[64] = "";
30186   int fd = -1;
30187   int rc = -1;
30188   UNUSED_PARAMETER(myHostID);
30189 
30190   /* create a new path by replace the trailing '-conch' with '-break' */
30191   pathLen = strlcpy(tPath, cPath, MAXPATHLEN);
30192   if( pathLen>MAXPATHLEN || pathLen<6 ||
30193      (strlcpy(&tPath[pathLen-5], "break", 6) != 5) ){
30194     sqlite3_snprintf(sizeof(errmsg),errmsg,"path error (len %d)",(int)pathLen);
30195     goto end_breaklock;
30196   }
30197   /* read the conch content */
30198   readLen = osPread(conchFile->h, buf, PROXY_MAXCONCHLEN, 0);
30199   if( readLen<PROXY_PATHINDEX ){
30200     sqlite3_snprintf(sizeof(errmsg),errmsg,"read error (len %d)",(int)readLen);
30201     goto end_breaklock;
30202   }
30203   /* write it out to the temporary break file */
30204   fd = robust_open(tPath, (O_RDWR|O_CREAT|O_EXCL), 0);
30205   if( fd<0 ){
30206     sqlite3_snprintf(sizeof(errmsg), errmsg, "create failed (%d)", errno);
30207     goto end_breaklock;
30208   }
30209   if( osPwrite(fd, buf, readLen, 0) != (ssize_t)readLen ){
30210     sqlite3_snprintf(sizeof(errmsg), errmsg, "write failed (%d)", errno);
30211     goto end_breaklock;
30212   }
30213   if( rename(tPath, cPath) ){
30214     sqlite3_snprintf(sizeof(errmsg), errmsg, "rename failed (%d)", errno);
30215     goto end_breaklock;
30216   }
30217   rc = 0;
30218   fprintf(stderr, "broke stale lock on %s\n", cPath);
30219   robust_close(pFile, conchFile->h, __LINE__);
30220   conchFile->h = fd;
30221   conchFile->openFlags = O_RDWR | O_CREAT;
30222 
30223 end_breaklock:
30224   if( rc ){
30225     if( fd>=0 ){
30226       osUnlink(tPath);
30227       robust_close(pFile, fd, __LINE__);
30228     }
30229     fprintf(stderr, "failed to break stale lock on %s, %s\n", cPath, errmsg);
30230   }
30231   return rc;
30232 }
30233 
30234 /* Take the requested lock on the conch file and break a stale lock if the
30235 ** host id matches.
30236 */
30237 static int proxyConchLock(unixFile *pFile, uuid_t myHostID, int lockType){
30238   proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
30239   unixFile *conchFile = pCtx->conchFile;
30240   int rc = SQLITE_OK;
30241   int nTries = 0;
30242   struct timespec conchModTime;
30243 
30244   memset(&conchModTime, 0, sizeof(conchModTime));
30245   do {
30246     rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType);
30247     nTries ++;
30248     if( rc==SQLITE_BUSY ){
30249       /* If the lock failed (busy):
30250        * 1st try: get the mod time of the conch, wait 0.5s and try again.
30251        * 2nd try: fail if the mod time changed or host id is different, wait
30252        *           10 sec and try again
30253        * 3rd try: break the lock unless the mod time has changed.
30254        */
30255       struct stat buf;
30256       if( osFstat(conchFile->h, &buf) ){
30257         pFile->lastErrno = errno;
30258         return SQLITE_IOERR_LOCK;
30259       }
30260 
30261       if( nTries==1 ){
30262         conchModTime = buf.st_mtimespec;
30263         usleep(500000); /* wait 0.5 sec and try the lock again*/
30264         continue;
30265       }
30266 
30267       assert( nTries>1 );
30268       if( conchModTime.tv_sec != buf.st_mtimespec.tv_sec ||
30269          conchModTime.tv_nsec != buf.st_mtimespec.tv_nsec ){
30270         return SQLITE_BUSY;
30271       }
30272 
30273       if( nTries==2 ){
30274         char tBuf[PROXY_MAXCONCHLEN];
30275         int len = osPread(conchFile->h, tBuf, PROXY_MAXCONCHLEN, 0);
30276         if( len<0 ){
30277           pFile->lastErrno = errno;
30278           return SQLITE_IOERR_LOCK;
30279         }
30280         if( len>PROXY_PATHINDEX && tBuf[0]==(char)PROXY_CONCHVERSION){
30281           /* don't break the lock if the host id doesn't match */
30282           if( 0!=memcmp(&tBuf[PROXY_HEADERLEN], myHostID, PROXY_HOSTIDLEN) ){
30283             return SQLITE_BUSY;
30284           }
30285         }else{
30286           /* don't break the lock on short read or a version mismatch */
30287           return SQLITE_BUSY;
30288         }
30289         usleep(10000000); /* wait 10 sec and try the lock again */
30290         continue;
30291       }
30292 
30293       assert( nTries==3 );
30294       if( 0==proxyBreakConchLock(pFile, myHostID) ){
30295         rc = SQLITE_OK;
30296         if( lockType==EXCLUSIVE_LOCK ){
30297           rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, SHARED_LOCK);
30298         }
30299         if( !rc ){
30300           rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType);
30301         }
30302       }
30303     }
30304   } while( rc==SQLITE_BUSY && nTries<3 );
30305 
30306   return rc;
30307 }
30308 
30309 /* Takes the conch by taking a shared lock and read the contents conch, if
30310 ** lockPath is non-NULL, the host ID and lock file path must match.  A NULL
30311 ** lockPath means that the lockPath in the conch file will be used if the
30312 ** host IDs match, or a new lock path will be generated automatically
30313 ** and written to the conch file.
30314 */
30315 static int proxyTakeConch(unixFile *pFile){
30316   proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
30317 
30318   if( pCtx->conchHeld!=0 ){
30319     return SQLITE_OK;
30320   }else{
30321     unixFile *conchFile = pCtx->conchFile;
30322     uuid_t myHostID;
30323     int pError = 0;
30324     char readBuf[PROXY_MAXCONCHLEN];
30325     char lockPath[MAXPATHLEN];
30326     char *tempLockPath = NULL;
30327     int rc = SQLITE_OK;
30328     int createConch = 0;
30329     int hostIdMatch = 0;
30330     int readLen = 0;
30331     int tryOldLockPath = 0;
30332     int forceNewLockPath = 0;
30333 
30334     OSTRACE(("TAKECONCH  %d for %s pid=%d\n", conchFile->h,
30335              (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"), getpid()));
30336 
30337     rc = proxyGetHostID(myHostID, &pError);
30338     if( (rc&0xff)==SQLITE_IOERR ){
30339       pFile->lastErrno = pError;
30340       goto end_takeconch;
30341     }
30342     rc = proxyConchLock(pFile, myHostID, SHARED_LOCK);
30343     if( rc!=SQLITE_OK ){
30344       goto end_takeconch;
30345     }
30346     /* read the existing conch file */
30347     readLen = seekAndRead((unixFile*)conchFile, 0, readBuf, PROXY_MAXCONCHLEN);
30348     if( readLen<0 ){
30349       /* I/O error: lastErrno set by seekAndRead */
30350       pFile->lastErrno = conchFile->lastErrno;
30351       rc = SQLITE_IOERR_READ;
30352       goto end_takeconch;
30353     }else if( readLen<=(PROXY_HEADERLEN+PROXY_HOSTIDLEN) ||
30354              readBuf[0]!=(char)PROXY_CONCHVERSION ){
30355       /* a short read or version format mismatch means we need to create a new
30356       ** conch file.
30357       */
30358       createConch = 1;
30359     }
30360     /* if the host id matches and the lock path already exists in the conch
30361     ** we'll try to use the path there, if we can't open that path, we'll
30362     ** retry with a new auto-generated path
30363     */
30364     do { /* in case we need to try again for an :auto: named lock file */
30365 
30366       if( !createConch && !forceNewLockPath ){
30367         hostIdMatch = !memcmp(&readBuf[PROXY_HEADERLEN], myHostID,
30368                                   PROXY_HOSTIDLEN);
30369         /* if the conch has data compare the contents */
30370         if( !pCtx->lockProxyPath ){
30371           /* for auto-named local lock file, just check the host ID and we'll
30372            ** use the local lock file path that's already in there
30373            */
30374           if( hostIdMatch ){
30375             size_t pathLen = (readLen - PROXY_PATHINDEX);
30376 
30377             if( pathLen>=MAXPATHLEN ){
30378               pathLen=MAXPATHLEN-1;
30379             }
30380             memcpy(lockPath, &readBuf[PROXY_PATHINDEX], pathLen);
30381             lockPath[pathLen] = 0;
30382             tempLockPath = lockPath;
30383             tryOldLockPath = 1;
30384             /* create a copy of the lock path if the conch is taken */
30385             goto end_takeconch;
30386           }
30387         }else if( hostIdMatch
30388                && !strncmp(pCtx->lockProxyPath, &readBuf[PROXY_PATHINDEX],
30389                            readLen-PROXY_PATHINDEX)
30390         ){
30391           /* conch host and lock path match */
30392           goto end_takeconch;
30393         }
30394       }
30395 
30396       /* if the conch isn't writable and doesn't match, we can't take it */
30397       if( (conchFile->openFlags&O_RDWR) == 0 ){
30398         rc = SQLITE_BUSY;
30399         goto end_takeconch;
30400       }
30401 
30402       /* either the conch didn't match or we need to create a new one */
30403       if( !pCtx->lockProxyPath ){
30404         proxyGetLockPath(pCtx->dbPath, lockPath, MAXPATHLEN);
30405         tempLockPath = lockPath;
30406         /* create a copy of the lock path _only_ if the conch is taken */
30407       }
30408 
30409       /* update conch with host and path (this will fail if other process
30410       ** has a shared lock already), if the host id matches, use the big
30411       ** stick.
30412       */
30413       futimes(conchFile->h, NULL);
30414       if( hostIdMatch && !createConch ){
30415         if( conchFile->pInode && conchFile->pInode->nShared>1 ){
30416           /* We are trying for an exclusive lock but another thread in this
30417            ** same process is still holding a shared lock. */
30418           rc = SQLITE_BUSY;
30419         } else {
30420           rc = proxyConchLock(pFile, myHostID, EXCLUSIVE_LOCK);
30421         }
30422       }else{
30423         rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, EXCLUSIVE_LOCK);
30424       }
30425       if( rc==SQLITE_OK ){
30426         char writeBuffer[PROXY_MAXCONCHLEN];
30427         int writeSize = 0;
30428 
30429         writeBuffer[0] = (char)PROXY_CONCHVERSION;
30430         memcpy(&writeBuffer[PROXY_HEADERLEN], myHostID, PROXY_HOSTIDLEN);
30431         if( pCtx->lockProxyPath!=NULL ){
30432           strlcpy(&writeBuffer[PROXY_PATHINDEX], pCtx->lockProxyPath, MAXPATHLEN);
30433         }else{
30434           strlcpy(&writeBuffer[PROXY_PATHINDEX], tempLockPath, MAXPATHLEN);
30435         }
30436         writeSize = PROXY_PATHINDEX + strlen(&writeBuffer[PROXY_PATHINDEX]);
30437         robust_ftruncate(conchFile->h, writeSize);
30438         rc = unixWrite((sqlite3_file *)conchFile, writeBuffer, writeSize, 0);
30439         fsync(conchFile->h);
30440         /* If we created a new conch file (not just updated the contents of a
30441          ** valid conch file), try to match the permissions of the database
30442          */
30443         if( rc==SQLITE_OK && createConch ){
30444           struct stat buf;
30445           int err = osFstat(pFile->h, &buf);
30446           if( err==0 ){
30447             mode_t cmode = buf.st_mode&(S_IRUSR|S_IWUSR | S_IRGRP|S_IWGRP |
30448                                         S_IROTH|S_IWOTH);
30449             /* try to match the database file R/W permissions, ignore failure */
30450 #ifndef SQLITE_PROXY_DEBUG
30451             osFchmod(conchFile->h, cmode);
30452 #else
30453             do{
30454               rc = osFchmod(conchFile->h, cmode);
30455             }while( rc==(-1) && errno==EINTR );
30456             if( rc!=0 ){
30457               int code = errno;
30458               fprintf(stderr, "fchmod %o FAILED with %d %s\n",
30459                       cmode, code, strerror(code));
30460             } else {
30461               fprintf(stderr, "fchmod %o SUCCEDED\n",cmode);
30462             }
30463           }else{
30464             int code = errno;
30465             fprintf(stderr, "STAT FAILED[%d] with %d %s\n",
30466                     err, code, strerror(code));
30467 #endif
30468           }
30469         }
30470       }
30471       conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, SHARED_LOCK);
30472 
30473     end_takeconch:
30474       OSTRACE(("TRANSPROXY: CLOSE  %d\n", pFile->h));
30475       if( rc==SQLITE_OK && pFile->openFlags ){
30476         int fd;
30477         if( pFile->h>=0 ){
30478           robust_close(pFile, pFile->h, __LINE__);
30479         }
30480         pFile->h = -1;
30481         fd = robust_open(pCtx->dbPath, pFile->openFlags, 0);
30482         OSTRACE(("TRANSPROXY: OPEN  %d\n", fd));
30483         if( fd>=0 ){
30484           pFile->h = fd;
30485         }else{
30486           rc=SQLITE_CANTOPEN_BKPT; /* SQLITE_BUSY? proxyTakeConch called
30487            during locking */
30488         }
30489       }
30490       if( rc==SQLITE_OK && !pCtx->lockProxy ){
30491         char *path = tempLockPath ? tempLockPath : pCtx->lockProxyPath;
30492         rc = proxyCreateUnixFile(path, &pCtx->lockProxy, 1);
30493         if( rc!=SQLITE_OK && rc!=SQLITE_NOMEM && tryOldLockPath ){
30494           /* we couldn't create the proxy lock file with the old lock file path
30495            ** so try again via auto-naming
30496            */
30497           forceNewLockPath = 1;
30498           tryOldLockPath = 0;
30499           continue; /* go back to the do {} while start point, try again */
30500         }
30501       }
30502       if( rc==SQLITE_OK ){
30503         /* Need to make a copy of path if we extracted the value
30504          ** from the conch file or the path was allocated on the stack
30505          */
30506         if( tempLockPath ){
30507           pCtx->lockProxyPath = sqlite3DbStrDup(0, tempLockPath);
30508           if( !pCtx->lockProxyPath ){
30509             rc = SQLITE_NOMEM;
30510           }
30511         }
30512       }
30513       if( rc==SQLITE_OK ){
30514         pCtx->conchHeld = 1;
30515 
30516         if( pCtx->lockProxy->pMethod == &afpIoMethods ){
30517           afpLockingContext *afpCtx;
30518           afpCtx = (afpLockingContext *)pCtx->lockProxy->lockingContext;
30519           afpCtx->dbPath = pCtx->lockProxyPath;
30520         }
30521       } else {
30522         conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK);
30523       }
30524       OSTRACE(("TAKECONCH  %d %s\n", conchFile->h,
30525                rc==SQLITE_OK?"ok":"failed"));
30526       return rc;
30527     } while (1); /* in case we need to retry the :auto: lock file -
30528                  ** we should never get here except via the 'continue' call. */
30529   }
30530 }
30531 
30532 /*
30533 ** If pFile holds a lock on a conch file, then release that lock.
30534 */
30535 static int proxyReleaseConch(unixFile *pFile){
30536   int rc = SQLITE_OK;         /* Subroutine return code */
30537   proxyLockingContext *pCtx;  /* The locking context for the proxy lock */
30538   unixFile *conchFile;        /* Name of the conch file */
30539 
30540   pCtx = (proxyLockingContext *)pFile->lockingContext;
30541   conchFile = pCtx->conchFile;
30542   OSTRACE(("RELEASECONCH  %d for %s pid=%d\n", conchFile->h,
30543            (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"),
30544            getpid()));
30545   if( pCtx->conchHeld>0 ){
30546     rc = conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK);
30547   }
30548   pCtx->conchHeld = 0;
30549   OSTRACE(("RELEASECONCH  %d %s\n", conchFile->h,
30550            (rc==SQLITE_OK ? "ok" : "failed")));
30551   return rc;
30552 }
30553 
30554 /*
30555 ** Given the name of a database file, compute the name of its conch file.
30556 ** Store the conch filename in memory obtained from sqlite3_malloc().
30557 ** Make *pConchPath point to the new name.  Return SQLITE_OK on success
30558 ** or SQLITE_NOMEM if unable to obtain memory.
30559 **
30560 ** The caller is responsible for ensuring that the allocated memory
30561 ** space is eventually freed.
30562 **
30563 ** *pConchPath is set to NULL if a memory allocation error occurs.
30564 */
30565 static int proxyCreateConchPathname(char *dbPath, char **pConchPath){
30566   int i;                        /* Loop counter */
30567   int len = (int)strlen(dbPath); /* Length of database filename - dbPath */
30568   char *conchPath;              /* buffer in which to construct conch name */
30569 
30570   /* Allocate space for the conch filename and initialize the name to
30571   ** the name of the original database file. */
30572   *pConchPath = conchPath = (char *)sqlite3_malloc(len + 8);
30573   if( conchPath==0 ){
30574     return SQLITE_NOMEM;
30575   }
30576   memcpy(conchPath, dbPath, len+1);
30577 
30578   /* now insert a "." before the last / character */
30579   for( i=(len-1); i>=0; i-- ){
30580     if( conchPath[i]=='/' ){
30581       i++;
30582       break;
30583     }
30584   }
30585   conchPath[i]='.';
30586   while ( i<len ){
30587     conchPath[i+1]=dbPath[i];
30588     i++;
30589   }
30590 
30591   /* append the "-conch" suffix to the file */
30592   memcpy(&conchPath[i+1], "-conch", 7);
30593   assert( (int)strlen(conchPath) == len+7 );
30594 
30595   return SQLITE_OK;
30596 }
30597 
30598 
30599 /* Takes a fully configured proxy locking-style unix file and switches
30600 ** the local lock file path
30601 */
30602 static int switchLockProxyPath(unixFile *pFile, const char *path) {
30603   proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext;
30604   char *oldPath = pCtx->lockProxyPath;
30605   int rc = SQLITE_OK;
30606 
30607   if( pFile->eFileLock!=NO_LOCK ){
30608     return SQLITE_BUSY;
30609   }
30610 
30611   /* nothing to do if the path is NULL, :auto: or matches the existing path */
30612   if( !path || path[0]=='\0' || !strcmp(path, ":auto:") ||
30613     (oldPath && !strncmp(oldPath, path, MAXPATHLEN)) ){
30614     return SQLITE_OK;
30615   }else{
30616     unixFile *lockProxy = pCtx->lockProxy;
30617     pCtx->lockProxy=NULL;
30618     pCtx->conchHeld = 0;
30619     if( lockProxy!=NULL ){
30620       rc=lockProxy->pMethod->xClose((sqlite3_file *)lockProxy);
30621       if( rc ) return rc;
30622       sqlite3_free(lockProxy);
30623     }
30624     sqlite3_free(oldPath);
30625     pCtx->lockProxyPath = sqlite3DbStrDup(0, path);
30626   }
30627 
30628   return rc;
30629 }
30630 
30631 /*
30632 ** pFile is a file that has been opened by a prior xOpen call.  dbPath
30633 ** is a string buffer at least MAXPATHLEN+1 characters in size.
30634 **
30635 ** This routine find the filename associated with pFile and writes it
30636 ** int dbPath.
30637 */
30638 static int proxyGetDbPathForUnixFile(unixFile *pFile, char *dbPath){
30639 #if defined(__APPLE__)
30640   if( pFile->pMethod == &afpIoMethods ){
30641     /* afp style keeps a reference to the db path in the filePath field
30642     ** of the struct */
30643     assert( (int)strlen((char*)pFile->lockingContext)<=MAXPATHLEN );
30644     strlcpy(dbPath, ((afpLockingContext *)pFile->lockingContext)->dbPath, MAXPATHLEN);
30645   } else
30646 #endif
30647   if( pFile->pMethod == &dotlockIoMethods ){
30648     /* dot lock style uses the locking context to store the dot lock
30649     ** file path */
30650     int len = strlen((char *)pFile->lockingContext) - strlen(DOTLOCK_SUFFIX);
30651     memcpy(dbPath, (char *)pFile->lockingContext, len + 1);
30652   }else{
30653     /* all other styles use the locking context to store the db file path */
30654     assert( strlen((char*)pFile->lockingContext)<=MAXPATHLEN );
30655     strlcpy(dbPath, (char *)pFile->lockingContext, MAXPATHLEN);
30656   }
30657   return SQLITE_OK;
30658 }
30659 
30660 /*
30661 ** Takes an already filled in unix file and alters it so all file locking
30662 ** will be performed on the local proxy lock file.  The following fields
30663 ** are preserved in the locking context so that they can be restored and
30664 ** the unix structure properly cleaned up at close time:
30665 **  ->lockingContext
30666 **  ->pMethod
30667 */
30668 static int proxyTransformUnixFile(unixFile *pFile, const char *path) {
30669   proxyLockingContext *pCtx;
30670   char dbPath[MAXPATHLEN+1];       /* Name of the database file */
30671   char *lockPath=NULL;
30672   int rc = SQLITE_OK;
30673 
30674   if( pFile->eFileLock!=NO_LOCK ){
30675     return SQLITE_BUSY;
30676   }
30677   proxyGetDbPathForUnixFile(pFile, dbPath);
30678   if( !path || path[0]=='\0' || !strcmp(path, ":auto:") ){
30679     lockPath=NULL;
30680   }else{
30681     lockPath=(char *)path;
30682   }
30683 
30684   OSTRACE(("TRANSPROXY  %d for %s pid=%d\n", pFile->h,
30685            (lockPath ? lockPath : ":auto:"), getpid()));
30686 
30687   pCtx = sqlite3_malloc( sizeof(*pCtx) );
30688   if( pCtx==0 ){
30689     return SQLITE_NOMEM;
30690   }
30691   memset(pCtx, 0, sizeof(*pCtx));
30692 
30693   rc = proxyCreateConchPathname(dbPath, &pCtx->conchFilePath);
30694   if( rc==SQLITE_OK ){
30695     rc = proxyCreateUnixFile(pCtx->conchFilePath, &pCtx->conchFile, 0);
30696     if( rc==SQLITE_CANTOPEN && ((pFile->openFlags&O_RDWR) == 0) ){
30697       /* if (a) the open flags are not O_RDWR, (b) the conch isn't there, and
30698       ** (c) the file system is read-only, then enable no-locking access.
30699       ** Ugh, since O_RDONLY==0x0000 we test for !O_RDWR since unixOpen asserts
30700       ** that openFlags will have only one of O_RDONLY or O_RDWR.
30701       */
30702       struct statfs fsInfo;
30703       struct stat conchInfo;
30704       int goLockless = 0;
30705 
30706       if( osStat(pCtx->conchFilePath, &conchInfo) == -1 ) {
30707         int err = errno;
30708         if( (err==ENOENT) && (statfs(dbPath, &fsInfo) != -1) ){
30709           goLockless = (fsInfo.f_flags&MNT_RDONLY) == MNT_RDONLY;
30710         }
30711       }
30712       if( goLockless ){
30713         pCtx->conchHeld = -1; /* read only FS/ lockless */
30714         rc = SQLITE_OK;
30715       }
30716     }
30717   }
30718   if( rc==SQLITE_OK && lockPath ){
30719     pCtx->lockProxyPath = sqlite3DbStrDup(0, lockPath);
30720   }
30721 
30722   if( rc==SQLITE_OK ){
30723     pCtx->dbPath = sqlite3DbStrDup(0, dbPath);
30724     if( pCtx->dbPath==NULL ){
30725       rc = SQLITE_NOMEM;
30726     }
30727   }
30728   if( rc==SQLITE_OK ){
30729     /* all memory is allocated, proxys are created and assigned,
30730     ** switch the locking context and pMethod then return.
30731     */
30732     pCtx->oldLockingContext = pFile->lockingContext;
30733     pFile->lockingContext = pCtx;
30734     pCtx->pOldMethod = pFile->pMethod;
30735     pFile->pMethod = &proxyIoMethods;
30736   }else{
30737     if( pCtx->conchFile ){
30738       pCtx->conchFile->pMethod->xClose((sqlite3_file *)pCtx->conchFile);
30739       sqlite3_free(pCtx->conchFile);
30740     }
30741     sqlite3DbFree(0, pCtx->lockProxyPath);
30742     sqlite3_free(pCtx->conchFilePath);
30743     sqlite3_free(pCtx);
30744   }
30745   OSTRACE(("TRANSPROXY  %d %s\n", pFile->h,
30746            (rc==SQLITE_OK ? "ok" : "failed")));
30747   return rc;
30748 }
30749 
30750 
30751 /*
30752 ** This routine handles sqlite3_file_control() calls that are specific
30753 ** to proxy locking.
30754 */
30755 static int proxyFileControl(sqlite3_file *id, int op, void *pArg){
30756   switch( op ){
30757     case SQLITE_GET_LOCKPROXYFILE: {
30758       unixFile *pFile = (unixFile*)id;
30759       if( pFile->pMethod == &proxyIoMethods ){
30760         proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext;
30761         proxyTakeConch(pFile);
30762         if( pCtx->lockProxyPath ){
30763           *(const char **)pArg = pCtx->lockProxyPath;
30764         }else{
30765           *(const char **)pArg = ":auto: (not held)";
30766         }
30767       } else {
30768         *(const char **)pArg = NULL;
30769       }
30770       return SQLITE_OK;
30771     }
30772     case SQLITE_SET_LOCKPROXYFILE: {
30773       unixFile *pFile = (unixFile*)id;
30774       int rc = SQLITE_OK;
30775       int isProxyStyle = (pFile->pMethod == &proxyIoMethods);
30776       if( pArg==NULL || (const char *)pArg==0 ){
30777         if( isProxyStyle ){
30778           /* turn off proxy locking - not supported */
30779           rc = SQLITE_ERROR /*SQLITE_PROTOCOL? SQLITE_MISUSE?*/;
30780         }else{
30781           /* turn off proxy locking - already off - NOOP */
30782           rc = SQLITE_OK;
30783         }
30784       }else{
30785         const char *proxyPath = (const char *)pArg;
30786         if( isProxyStyle ){
30787           proxyLockingContext *pCtx =
30788             (proxyLockingContext*)pFile->lockingContext;
30789           if( !strcmp(pArg, ":auto:")
30790            || (pCtx->lockProxyPath &&
30791                !strncmp(pCtx->lockProxyPath, proxyPath, MAXPATHLEN))
30792           ){
30793             rc = SQLITE_OK;
30794           }else{
30795             rc = switchLockProxyPath(pFile, proxyPath);
30796           }
30797         }else{
30798           /* turn on proxy file locking */
30799           rc = proxyTransformUnixFile(pFile, proxyPath);
30800         }
30801       }
30802       return rc;
30803     }
30804     default: {
30805       assert( 0 );  /* The call assures that only valid opcodes are sent */
30806     }
30807   }
30808   /*NOTREACHED*/
30809   return SQLITE_ERROR;
30810 }
30811 
30812 /*
30813 ** Within this division (the proxying locking implementation) the procedures
30814 ** above this point are all utilities.  The lock-related methods of the
30815 ** proxy-locking sqlite3_io_method object follow.
30816 */
30817 
30818 
30819 /*
30820 ** This routine checks if there is a RESERVED lock held on the specified
30821 ** file by this or any other process. If such a lock is held, set *pResOut
30822 ** to a non-zero value otherwise *pResOut is set to zero.  The return value
30823 ** is set to SQLITE_OK unless an I/O error occurs during lock checking.
30824 */
30825 static int proxyCheckReservedLock(sqlite3_file *id, int *pResOut) {
30826   unixFile *pFile = (unixFile*)id;
30827   int rc = proxyTakeConch(pFile);
30828   if( rc==SQLITE_OK ){
30829     proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
30830     if( pCtx->conchHeld>0 ){
30831       unixFile *proxy = pCtx->lockProxy;
30832       return proxy->pMethod->xCheckReservedLock((sqlite3_file*)proxy, pResOut);
30833     }else{ /* conchHeld < 0 is lockless */
30834       pResOut=0;
30835     }
30836   }
30837   return rc;
30838 }
30839 
30840 /*
30841 ** Lock the file with the lock specified by parameter eFileLock - one
30842 ** of the following:
30843 **
30844 **     (1) SHARED_LOCK
30845 **     (2) RESERVED_LOCK
30846 **     (3) PENDING_LOCK
30847 **     (4) EXCLUSIVE_LOCK
30848 **
30849 ** Sometimes when requesting one lock state, additional lock states
30850 ** are inserted in between.  The locking might fail on one of the later
30851 ** transitions leaving the lock state different from what it started but
30852 ** still short of its goal.  The following chart shows the allowed
30853 ** transitions and the inserted intermediate states:
30854 **
30855 **    UNLOCKED -> SHARED
30856 **    SHARED -> RESERVED
30857 **    SHARED -> (PENDING) -> EXCLUSIVE
30858 **    RESERVED -> (PENDING) -> EXCLUSIVE
30859 **    PENDING -> EXCLUSIVE
30860 **
30861 ** This routine will only increase a lock.  Use the sqlite3OsUnlock()
30862 ** routine to lower a locking level.
30863 */
30864 static int proxyLock(sqlite3_file *id, int eFileLock) {
30865   unixFile *pFile = (unixFile*)id;
30866   int rc = proxyTakeConch(pFile);
30867   if( rc==SQLITE_OK ){
30868     proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
30869     if( pCtx->conchHeld>0 ){
30870       unixFile *proxy = pCtx->lockProxy;
30871       rc = proxy->pMethod->xLock((sqlite3_file*)proxy, eFileLock);
30872       pFile->eFileLock = proxy->eFileLock;
30873     }else{
30874       /* conchHeld < 0 is lockless */
30875     }
30876   }
30877   return rc;
30878 }
30879 
30880 
30881 /*
30882 ** Lower the locking level on file descriptor pFile to eFileLock.  eFileLock
30883 ** must be either NO_LOCK or SHARED_LOCK.
30884 **
30885 ** If the locking level of the file descriptor is already at or below
30886 ** the requested locking level, this routine is a no-op.
30887 */
30888 static int proxyUnlock(sqlite3_file *id, int eFileLock) {
30889   unixFile *pFile = (unixFile*)id;
30890   int rc = proxyTakeConch(pFile);
30891   if( rc==SQLITE_OK ){
30892     proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
30893     if( pCtx->conchHeld>0 ){
30894       unixFile *proxy = pCtx->lockProxy;
30895       rc = proxy->pMethod->xUnlock((sqlite3_file*)proxy, eFileLock);
30896       pFile->eFileLock = proxy->eFileLock;
30897     }else{
30898       /* conchHeld < 0 is lockless */
30899     }
30900   }
30901   return rc;
30902 }
30903 
30904 /*
30905 ** Close a file that uses proxy locks.
30906 */
30907 static int proxyClose(sqlite3_file *id) {
30908   if( id ){
30909     unixFile *pFile = (unixFile*)id;
30910     proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext;
30911     unixFile *lockProxy = pCtx->lockProxy;
30912     unixFile *conchFile = pCtx->conchFile;
30913     int rc = SQLITE_OK;
30914 
30915     if( lockProxy ){
30916       rc = lockProxy->pMethod->xUnlock((sqlite3_file*)lockProxy, NO_LOCK);
30917       if( rc ) return rc;
30918       rc = lockProxy->pMethod->xClose((sqlite3_file*)lockProxy);
30919       if( rc ) return rc;
30920       sqlite3_free(lockProxy);
30921       pCtx->lockProxy = 0;
30922     }
30923     if( conchFile ){
30924       if( pCtx->conchHeld ){
30925         rc = proxyReleaseConch(pFile);
30926         if( rc ) return rc;
30927       }
30928       rc = conchFile->pMethod->xClose((sqlite3_file*)conchFile);
30929       if( rc ) return rc;
30930       sqlite3_free(conchFile);
30931     }
30932     sqlite3DbFree(0, pCtx->lockProxyPath);
30933     sqlite3_free(pCtx->conchFilePath);
30934     sqlite3DbFree(0, pCtx->dbPath);
30935     /* restore the original locking context and pMethod then close it */
30936     pFile->lockingContext = pCtx->oldLockingContext;
30937     pFile->pMethod = pCtx->pOldMethod;
30938     sqlite3_free(pCtx);
30939     return pFile->pMethod->xClose(id);
30940   }
30941   return SQLITE_OK;
30942 }
30943 
30944 
30945 
30946 #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */
30947 /*
30948 ** The proxy locking style is intended for use with AFP filesystems.
30949 ** And since AFP is only supported on MacOSX, the proxy locking is also
30950 ** restricted to MacOSX.
30951 **
30952 **
30953 ******************* End of the proxy lock implementation **********************
30954 ******************************************************************************/
30955 
30956 /*
30957 ** Initialize the operating system interface.
30958 **
30959 ** This routine registers all VFS implementations for unix-like operating
30960 ** systems.  This routine, and the sqlite3_os_end() routine that follows,
30961 ** should be the only routines in this file that are visible from other
30962 ** files.
30963 **
30964 ** This routine is called once during SQLite initialization and by a
30965 ** single thread.  The memory allocation and mutex subsystems have not
30966 ** necessarily been initialized when this routine is called, and so they
30967 ** should not be used.
30968 */
30969 SQLITE_API int sqlite3_os_init(void){
30970   /*
30971   ** The following macro defines an initializer for an sqlite3_vfs object.
30972   ** The name of the VFS is NAME.  The pAppData is a pointer to a pointer
30973   ** to the "finder" function.  (pAppData is a pointer to a pointer because
30974   ** silly C90 rules prohibit a void* from being cast to a function pointer
30975   ** and so we have to go through the intermediate pointer to avoid problems
30976   ** when compiling with -pedantic-errors on GCC.)
30977   **
30978   ** The FINDER parameter to this macro is the name of the pointer to the
30979   ** finder-function.  The finder-function returns a pointer to the
30980   ** sqlite_io_methods object that implements the desired locking
30981   ** behaviors.  See the division above that contains the IOMETHODS
30982   ** macro for addition information on finder-functions.
30983   **
30984   ** Most finders simply return a pointer to a fixed sqlite3_io_methods
30985   ** object.  But the "autolockIoFinder" available on MacOSX does a little
30986   ** more than that; it looks at the filesystem type that hosts the
30987   ** database file and tries to choose an locking method appropriate for
30988   ** that filesystem time.
30989   */
30990   #define UNIXVFS(VFSNAME, FINDER) {                        \
30991     3,                    /* iVersion */                    \
30992     sizeof(unixFile),     /* szOsFile */                    \
30993     MAX_PATHNAME,         /* mxPathname */                  \
30994     0,                    /* pNext */                       \
30995     VFSNAME,              /* zName */                       \
30996     (void*)&FINDER,       /* pAppData */                    \
30997     unixOpen,             /* xOpen */                       \
30998     unixDelete,           /* xDelete */                     \
30999     unixAccess,           /* xAccess */                     \
31000     unixFullPathname,     /* xFullPathname */               \
31001     unixDlOpen,           /* xDlOpen */                     \
31002     unixDlError,          /* xDlError */                    \
31003     unixDlSym,            /* xDlSym */                      \
31004     unixDlClose,          /* xDlClose */                    \
31005     unixRandomness,       /* xRandomness */                 \
31006     unixSleep,            /* xSleep */                      \
31007     unixCurrentTime,      /* xCurrentTime */                \
31008     unixGetLastError,     /* xGetLastError */               \
31009     unixCurrentTimeInt64, /* xCurrentTimeInt64 */           \
31010     unixSetSystemCall,    /* xSetSystemCall */              \
31011     unixGetSystemCall,    /* xGetSystemCall */              \
31012     unixNextSystemCall,   /* xNextSystemCall */             \
31013   }
31014 
31015   /*
31016   ** All default VFSes for unix are contained in the following array.
31017   **
31018   ** Note that the sqlite3_vfs.pNext field of the VFS object is modified
31019   ** by the SQLite core when the VFS is registered.  So the following
31020   ** array cannot be const.
31021   */
31022   static sqlite3_vfs aVfs[] = {
31023 #if SQLITE_ENABLE_LOCKING_STYLE && (OS_VXWORKS || defined(__APPLE__))
31024     UNIXVFS("unix",          autolockIoFinder ),
31025 #else
31026     UNIXVFS("unix",          posixIoFinder ),
31027 #endif
31028     UNIXVFS("unix-none",     nolockIoFinder ),
31029     UNIXVFS("unix-dotfile",  dotlockIoFinder ),
31030     UNIXVFS("unix-excl",     posixIoFinder ),
31031 #if OS_VXWORKS
31032     UNIXVFS("unix-namedsem", semIoFinder ),
31033 #endif
31034 #if SQLITE_ENABLE_LOCKING_STYLE
31035     UNIXVFS("unix-posix",    posixIoFinder ),
31036 #if !OS_VXWORKS
31037     UNIXVFS("unix-flock",    flockIoFinder ),
31038 #endif
31039 #endif
31040 #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__)
31041     UNIXVFS("unix-afp",      afpIoFinder ),
31042     UNIXVFS("unix-nfs",      nfsIoFinder ),
31043     UNIXVFS("unix-proxy",    proxyIoFinder ),
31044 #endif
31045   };
31046   unsigned int i;          /* Loop counter */
31047 
31048   /* Double-check that the aSyscall[] array has been constructed
31049   ** correctly.  See ticket [bb3a86e890c8e96ab] */
31050   assert( ArraySize(aSyscall)==24 );
31051 
31052   /* Register all VFSes defined in the aVfs[] array */
31053   for(i=0; i<(sizeof(aVfs)/sizeof(sqlite3_vfs)); i++){
31054     sqlite3_vfs_register(&aVfs[i], i==0);
31055   }
31056   return SQLITE_OK;
31057 }
31058 
31059 /*
31060 ** Shutdown the operating system interface.
31061 **
31062 ** Some operating systems might need to do some cleanup in this routine,
31063 ** to release dynamically allocated objects.  But not on unix.
31064 ** This routine is a no-op for unix.
31065 */
31066 SQLITE_API int sqlite3_os_end(void){
31067   return SQLITE_OK;
31068 }
31069 
31070 #endif /* SQLITE_OS_UNIX */
31071 
31072 /************** End of os_unix.c *********************************************/
31073 /************** Begin file os_win.c ******************************************/
31074 /*
31075 ** 2004 May 22
31076 **
31077 ** The author disclaims copyright to this source code.  In place of
31078 ** a legal notice, here is a blessing:
31079 **
31080 **    May you do good and not evil.
31081 **    May you find forgiveness for yourself and forgive others.
31082 **    May you share freely, never taking more than you give.
31083 **
31084 ******************************************************************************
31085 **
31086 ** This file contains code that is specific to Windows.
31087 */
31088 #if SQLITE_OS_WIN               /* This file is used for Windows only */
31089 
31090 #ifdef __CYGWIN__
31091 # include <sys/cygwin.h>
31092 # include <errno.h> /* amalgamator: keep */
31093 #endif
31094 
31095 /*
31096 ** Include code that is common to all os_*.c files
31097 */
31098 /************** Include os_common.h in the middle of os_win.c ****************/
31099 /************** Begin file os_common.h ***************************************/
31100 /*
31101 ** 2004 May 22
31102 **
31103 ** The author disclaims copyright to this source code.  In place of
31104 ** a legal notice, here is a blessing:
31105 **
31106 **    May you do good and not evil.
31107 **    May you find forgiveness for yourself and forgive others.
31108 **    May you share freely, never taking more than you give.
31109 **
31110 ******************************************************************************
31111 **
31112 ** This file contains macros and a little bit of code that is common to
31113 ** all of the platform-specific files (os_*.c) and is #included into those
31114 ** files.
31115 **
31116 ** This file should be #included by the os_*.c files only.  It is not a
31117 ** general purpose header file.
31118 */
31119 #ifndef _OS_COMMON_H_
31120 #define _OS_COMMON_H_
31121 
31122 /*
31123 ** At least two bugs have slipped in because we changed the MEMORY_DEBUG
31124 ** macro to SQLITE_DEBUG and some older makefiles have not yet made the
31125 ** switch.  The following code should catch this problem at compile-time.
31126 */
31127 #ifdef MEMORY_DEBUG
31128 # error "The MEMORY_DEBUG macro is obsolete.  Use SQLITE_DEBUG instead."
31129 #endif
31130 
31131 #if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
31132 # ifndef SQLITE_DEBUG_OS_TRACE
31133 #   define SQLITE_DEBUG_OS_TRACE 0
31134 # endif
31135   int sqlite3OSTrace = SQLITE_DEBUG_OS_TRACE;
31136 # define OSTRACE(X)          if( sqlite3OSTrace ) sqlite3DebugPrintf X
31137 #else
31138 # define OSTRACE(X)
31139 #endif
31140 
31141 /*
31142 ** Macros for performance tracing.  Normally turned off.  Only works
31143 ** on i486 hardware.
31144 */
31145 #ifdef SQLITE_PERFORMANCE_TRACE
31146 
31147 /*
31148 ** hwtime.h contains inline assembler code for implementing
31149 ** high-performance timing routines.
31150 */
31151 /************** Include hwtime.h in the middle of os_common.h ****************/
31152 /************** Begin file hwtime.h ******************************************/
31153 /*
31154 ** 2008 May 27
31155 **
31156 ** The author disclaims copyright to this source code.  In place of
31157 ** a legal notice, here is a blessing:
31158 **
31159 **    May you do good and not evil.
31160 **    May you find forgiveness for yourself and forgive others.
31161 **    May you share freely, never taking more than you give.
31162 **
31163 ******************************************************************************
31164 **
31165 ** This file contains inline asm code for retrieving "high-performance"
31166 ** counters for x86 class CPUs.
31167 */
31168 #ifndef _HWTIME_H_
31169 #define _HWTIME_H_
31170 
31171 /*
31172 ** The following routine only works on pentium-class (or newer) processors.
31173 ** It uses the RDTSC opcode to read the cycle count value out of the
31174 ** processor and returns that value.  This can be used for high-res
31175 ** profiling.
31176 */
31177 #if (defined(__GNUC__) || defined(_MSC_VER)) && \
31178       (defined(i386) || defined(__i386__) || defined(_M_IX86))
31179 
31180   #if defined(__GNUC__)
31181 
31182   __inline__ sqlite_uint64 sqlite3Hwtime(void){
31183      unsigned int lo, hi;
31184      __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
31185      return (sqlite_uint64)hi << 32 | lo;
31186   }
31187 
31188   #elif defined(_MSC_VER)
31189 
31190   __declspec(naked) __inline sqlite_uint64 __cdecl sqlite3Hwtime(void){
31191      __asm {
31192         rdtsc
31193         ret       ; return value at EDX:EAX
31194      }
31195   }
31196 
31197   #endif
31198 
31199 #elif (defined(__GNUC__) && defined(__x86_64__))
31200 
31201   __inline__ sqlite_uint64 sqlite3Hwtime(void){
31202       unsigned long val;
31203       __asm__ __volatile__ ("rdtsc" : "=A" (val));
31204       return val;
31205   }
31206 
31207 #elif (defined(__GNUC__) && defined(__ppc__))
31208 
31209   __inline__ sqlite_uint64 sqlite3Hwtime(void){
31210       unsigned long long retval;
31211       unsigned long junk;
31212       __asm__ __volatile__ ("\n\
31213           1:      mftbu   %1\n\
31214                   mftb    %L0\n\
31215                   mftbu   %0\n\
31216                   cmpw    %0,%1\n\
31217                   bne     1b"
31218                   : "=r" (retval), "=r" (junk));
31219       return retval;
31220   }
31221 
31222 #else
31223 
31224   #error Need implementation of sqlite3Hwtime() for your platform.
31225 
31226   /*
31227   ** To compile without implementing sqlite3Hwtime() for your platform,
31228   ** you can remove the above #error and use the following
31229   ** stub function.  You will lose timing support for many
31230   ** of the debugging and testing utilities, but it should at
31231   ** least compile and run.
31232   */
31233 SQLITE_PRIVATE   sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); }
31234 
31235 #endif
31236 
31237 #endif /* !defined(_HWTIME_H_) */
31238 
31239 /************** End of hwtime.h **********************************************/
31240 /************** Continuing where we left off in os_common.h ******************/
31241 
31242 static sqlite_uint64 g_start;
31243 static sqlite_uint64 g_elapsed;
31244 #define TIMER_START       g_start=sqlite3Hwtime()
31245 #define TIMER_END         g_elapsed=sqlite3Hwtime()-g_start
31246 #define TIMER_ELAPSED     g_elapsed
31247 #else
31248 #define TIMER_START
31249 #define TIMER_END
31250 #define TIMER_ELAPSED     ((sqlite_uint64)0)
31251 #endif
31252 
31253 /*
31254 ** If we compile with the SQLITE_TEST macro set, then the following block
31255 ** of code will give us the ability to simulate a disk I/O error.  This
31256 ** is used for testing the I/O recovery logic.
31257 */
31258 #ifdef SQLITE_TEST
31259 SQLITE_API int sqlite3_io_error_hit = 0;            /* Total number of I/O Errors */
31260 SQLITE_API int sqlite3_io_error_hardhit = 0;        /* Number of non-benign errors */
31261 SQLITE_API int sqlite3_io_error_pending = 0;        /* Count down to first I/O error */
31262 SQLITE_API int sqlite3_io_error_persist = 0;        /* True if I/O errors persist */
31263 SQLITE_API int sqlite3_io_error_benign = 0;         /* True if errors are benign */
31264 SQLITE_API int sqlite3_diskfull_pending = 0;
31265 SQLITE_API int sqlite3_diskfull = 0;
31266 #define SimulateIOErrorBenign(X) sqlite3_io_error_benign=(X)
31267 #define SimulateIOError(CODE)  \
31268   if( (sqlite3_io_error_persist && sqlite3_io_error_hit) \
31269        || sqlite3_io_error_pending-- == 1 )  \
31270               { local_ioerr(); CODE; }
31271 static void local_ioerr(){
31272   IOTRACE(("IOERR\n"));
31273   sqlite3_io_error_hit++;
31274   if( !sqlite3_io_error_benign ) sqlite3_io_error_hardhit++;
31275 }
31276 #define SimulateDiskfullError(CODE) \
31277    if( sqlite3_diskfull_pending ){ \
31278      if( sqlite3_diskfull_pending == 1 ){ \
31279        local_ioerr(); \
31280        sqlite3_diskfull = 1; \
31281        sqlite3_io_error_hit = 1; \
31282        CODE; \
31283      }else{ \
31284        sqlite3_diskfull_pending--; \
31285      } \
31286    }
31287 #else
31288 #define SimulateIOErrorBenign(X)
31289 #define SimulateIOError(A)
31290 #define SimulateDiskfullError(A)
31291 #endif
31292 
31293 /*
31294 ** When testing, keep a count of the number of open files.
31295 */
31296 #ifdef SQLITE_TEST
31297 SQLITE_API int sqlite3_open_file_count = 0;
31298 #define OpenCounter(X)  sqlite3_open_file_count+=(X)
31299 #else
31300 #define OpenCounter(X)
31301 #endif
31302 
31303 #endif /* !defined(_OS_COMMON_H_) */
31304 
31305 /************** End of os_common.h *******************************************/
31306 /************** Continuing where we left off in os_win.c *********************/
31307 
31308 /*
31309 ** Compiling and using WAL mode requires several APIs that are only
31310 ** available in Windows platforms based on the NT kernel.
31311 */
31312 #if !SQLITE_OS_WINNT && !defined(SQLITE_OMIT_WAL)
31313 #  error "WAL mode requires support from the Windows NT kernel, compile\
31314  with SQLITE_OMIT_WAL."
31315 #endif
31316 
31317 /*
31318 ** Are most of the Win32 ANSI APIs available (i.e. with certain exceptions
31319 ** based on the sub-platform)?
31320 */
31321 #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && !defined(SQLITE_WIN32_NO_ANSI)
31322 #  define SQLITE_WIN32_HAS_ANSI
31323 #endif
31324 
31325 /*
31326 ** Are most of the Win32 Unicode APIs available (i.e. with certain exceptions
31327 ** based on the sub-platform)?
31328 */
31329 #if (SQLITE_OS_WINCE || SQLITE_OS_WINNT || SQLITE_OS_WINRT) && \
31330     !defined(SQLITE_WIN32_NO_WIDE)
31331 #  define SQLITE_WIN32_HAS_WIDE
31332 #endif
31333 
31334 /*
31335 ** Make sure at least one set of Win32 APIs is available.
31336 */
31337 #if !defined(SQLITE_WIN32_HAS_ANSI) && !defined(SQLITE_WIN32_HAS_WIDE)
31338 #  error "At least one of SQLITE_WIN32_HAS_ANSI and SQLITE_WIN32_HAS_WIDE\
31339  must be defined."
31340 #endif
31341 
31342 /*
31343 ** Define the required Windows SDK version constants if they are not
31344 ** already available.
31345 */
31346 #ifndef NTDDI_WIN8
31347 #  define NTDDI_WIN8                        0x06020000
31348 #endif
31349 
31350 #ifndef NTDDI_WINBLUE
31351 #  define NTDDI_WINBLUE                     0x06030000
31352 #endif
31353 
31354 /*
31355 ** Check if the GetVersionEx[AW] functions should be considered deprecated
31356 ** and avoid using them in that case.  It should be noted here that if the
31357 ** value of the SQLITE_WIN32_GETVERSIONEX pre-processor macro is zero
31358 ** (whether via this block or via being manually specified), that implies
31359 ** the underlying operating system will always be based on the Windows NT
31360 ** Kernel.
31361 */
31362 #ifndef SQLITE_WIN32_GETVERSIONEX
31363 #  if defined(NTDDI_VERSION) && NTDDI_VERSION >= NTDDI_WINBLUE
31364 #    define SQLITE_WIN32_GETVERSIONEX   0
31365 #  else
31366 #    define SQLITE_WIN32_GETVERSIONEX   1
31367 #  endif
31368 #endif
31369 
31370 /*
31371 ** This constant should already be defined (in the "WinDef.h" SDK file).
31372 */
31373 #ifndef MAX_PATH
31374 #  define MAX_PATH                      (260)
31375 #endif
31376 
31377 /*
31378 ** Maximum pathname length (in chars) for Win32.  This should normally be
31379 ** MAX_PATH.
31380 */
31381 #ifndef SQLITE_WIN32_MAX_PATH_CHARS
31382 #  define SQLITE_WIN32_MAX_PATH_CHARS   (MAX_PATH)
31383 #endif
31384 
31385 /*
31386 ** This constant should already be defined (in the "WinNT.h" SDK file).
31387 */
31388 #ifndef UNICODE_STRING_MAX_CHARS
31389 #  define UNICODE_STRING_MAX_CHARS      (32767)
31390 #endif
31391 
31392 /*
31393 ** Maximum pathname length (in chars) for WinNT.  This should normally be
31394 ** UNICODE_STRING_MAX_CHARS.
31395 */
31396 #ifndef SQLITE_WINNT_MAX_PATH_CHARS
31397 #  define SQLITE_WINNT_MAX_PATH_CHARS   (UNICODE_STRING_MAX_CHARS)
31398 #endif
31399 
31400 /*
31401 ** Maximum pathname length (in bytes) for Win32.  The MAX_PATH macro is in
31402 ** characters, so we allocate 4 bytes per character assuming worst-case of
31403 ** 4-bytes-per-character for UTF8.
31404 */
31405 #ifndef SQLITE_WIN32_MAX_PATH_BYTES
31406 #  define SQLITE_WIN32_MAX_PATH_BYTES   (SQLITE_WIN32_MAX_PATH_CHARS*4)
31407 #endif
31408 
31409 /*
31410 ** Maximum pathname length (in bytes) for WinNT.  This should normally be
31411 ** UNICODE_STRING_MAX_CHARS * sizeof(WCHAR).
31412 */
31413 #ifndef SQLITE_WINNT_MAX_PATH_BYTES
31414 #  define SQLITE_WINNT_MAX_PATH_BYTES   \
31415                             (sizeof(WCHAR) * SQLITE_WINNT_MAX_PATH_CHARS)
31416 #endif
31417 
31418 /*
31419 ** Maximum error message length (in chars) for WinRT.
31420 */
31421 #ifndef SQLITE_WIN32_MAX_ERRMSG_CHARS
31422 #  define SQLITE_WIN32_MAX_ERRMSG_CHARS (1024)
31423 #endif
31424 
31425 /*
31426 ** Returns non-zero if the character should be treated as a directory
31427 ** separator.
31428 */
31429 #ifndef winIsDirSep
31430 #  define winIsDirSep(a)                (((a) == '/') || ((a) == '\\'))
31431 #endif
31432 
31433 /*
31434 ** This macro is used when a local variable is set to a value that is
31435 ** [sometimes] not used by the code (e.g. via conditional compilation).
31436 */
31437 #ifndef UNUSED_VARIABLE_VALUE
31438 #  define UNUSED_VARIABLE_VALUE(x) (void)(x)
31439 #endif
31440 
31441 /*
31442 ** Returns the character that should be used as the directory separator.
31443 */
31444 #ifndef winGetDirSep
31445 #  define winGetDirSep()                '\\'
31446 #endif
31447 
31448 /*
31449 ** Do we need to manually define the Win32 file mapping APIs for use with WAL
31450 ** mode (e.g. these APIs are available in the Windows CE SDK; however, they
31451 ** are not present in the header file)?
31452 */
31453 #if SQLITE_WIN32_FILEMAPPING_API && !defined(SQLITE_OMIT_WAL)
31454 /*
31455 ** Two of the file mapping APIs are different under WinRT.  Figure out which
31456 ** set we need.
31457 */
31458 #if SQLITE_OS_WINRT
31459 WINBASEAPI HANDLE WINAPI CreateFileMappingFromApp(HANDLE, \
31460         LPSECURITY_ATTRIBUTES, ULONG, ULONG64, LPCWSTR);
31461 
31462 WINBASEAPI LPVOID WINAPI MapViewOfFileFromApp(HANDLE, ULONG, ULONG64, SIZE_T);
31463 #else
31464 #if defined(SQLITE_WIN32_HAS_ANSI)
31465 WINBASEAPI HANDLE WINAPI CreateFileMappingA(HANDLE, LPSECURITY_ATTRIBUTES, \
31466         DWORD, DWORD, DWORD, LPCSTR);
31467 #endif /* defined(SQLITE_WIN32_HAS_ANSI) */
31468 
31469 #if defined(SQLITE_WIN32_HAS_WIDE)
31470 WINBASEAPI HANDLE WINAPI CreateFileMappingW(HANDLE, LPSECURITY_ATTRIBUTES, \
31471         DWORD, DWORD, DWORD, LPCWSTR);
31472 #endif /* defined(SQLITE_WIN32_HAS_WIDE) */
31473 
31474 WINBASEAPI LPVOID WINAPI MapViewOfFile(HANDLE, DWORD, DWORD, DWORD, SIZE_T);
31475 #endif /* SQLITE_OS_WINRT */
31476 
31477 /*
31478 ** This file mapping API is common to both Win32 and WinRT.
31479 */
31480 WINBASEAPI BOOL WINAPI UnmapViewOfFile(LPCVOID);
31481 #endif /* SQLITE_WIN32_FILEMAPPING_API && !defined(SQLITE_OMIT_WAL) */
31482 
31483 /*
31484 ** Some Microsoft compilers lack this definition.
31485 */
31486 #ifndef INVALID_FILE_ATTRIBUTES
31487 # define INVALID_FILE_ATTRIBUTES ((DWORD)-1)
31488 #endif
31489 
31490 #ifndef FILE_FLAG_MASK
31491 # define FILE_FLAG_MASK          (0xFF3C0000)
31492 #endif
31493 
31494 #ifndef FILE_ATTRIBUTE_MASK
31495 # define FILE_ATTRIBUTE_MASK     (0x0003FFF7)
31496 #endif
31497 
31498 #ifndef SQLITE_OMIT_WAL
31499 /* Forward references to structures used for WAL */
31500 typedef struct winShm winShm;           /* A connection to shared-memory */
31501 typedef struct winShmNode winShmNode;   /* A region of shared-memory */
31502 #endif
31503 
31504 /*
31505 ** WinCE lacks native support for file locking so we have to fake it
31506 ** with some code of our own.
31507 */
31508 #if SQLITE_OS_WINCE
31509 typedef struct winceLock {
31510   int nReaders;       /* Number of reader locks obtained */
31511   BOOL bPending;      /* Indicates a pending lock has been obtained */
31512   BOOL bReserved;     /* Indicates a reserved lock has been obtained */
31513   BOOL bExclusive;    /* Indicates an exclusive lock has been obtained */
31514 } winceLock;
31515 #endif
31516 
31517 /*
31518 ** The winFile structure is a subclass of sqlite3_file* specific to the win32
31519 ** portability layer.
31520 */
31521 typedef struct winFile winFile;
31522 struct winFile {
31523   const sqlite3_io_methods *pMethod; /*** Must be first ***/
31524   sqlite3_vfs *pVfs;      /* The VFS used to open this file */
31525   HANDLE h;               /* Handle for accessing the file */
31526   u8 locktype;            /* Type of lock currently held on this file */
31527   short sharedLockByte;   /* Randomly chosen byte used as a shared lock */
31528   u8 ctrlFlags;           /* Flags.  See WINFILE_* below */
31529   DWORD lastErrno;        /* The Windows errno from the last I/O error */
31530 #ifndef SQLITE_OMIT_WAL
31531   winShm *pShm;           /* Instance of shared memory on this file */
31532 #endif
31533   const char *zPath;      /* Full pathname of this file */
31534   int szChunk;            /* Chunk size configured by FCNTL_CHUNK_SIZE */
31535 #if SQLITE_OS_WINCE
31536   LPWSTR zDeleteOnClose;  /* Name of file to delete when closing */
31537   HANDLE hMutex;          /* Mutex used to control access to shared lock */
31538   HANDLE hShared;         /* Shared memory segment used for locking */
31539   winceLock local;        /* Locks obtained by this instance of winFile */
31540   winceLock *shared;      /* Global shared lock memory for the file  */
31541 #endif
31542 #if SQLITE_MAX_MMAP_SIZE>0
31543   int nFetchOut;                /* Number of outstanding xFetch references */
31544   HANDLE hMap;                  /* Handle for accessing memory mapping */
31545   void *pMapRegion;             /* Area memory mapped */
31546   sqlite3_int64 mmapSize;       /* Usable size of mapped region */
31547   sqlite3_int64 mmapSizeActual; /* Actual size of mapped region */
31548   sqlite3_int64 mmapSizeMax;    /* Configured FCNTL_MMAP_SIZE value */
31549 #endif
31550 };
31551 
31552 /*
31553 ** Allowed values for winFile.ctrlFlags
31554 */
31555 #define WINFILE_RDONLY          0x02   /* Connection is read only */
31556 #define WINFILE_PERSIST_WAL     0x04   /* Persistent WAL mode */
31557 #define WINFILE_PSOW            0x10   /* SQLITE_IOCAP_POWERSAFE_OVERWRITE */
31558 
31559 /*
31560  * The size of the buffer used by sqlite3_win32_write_debug().
31561  */
31562 #ifndef SQLITE_WIN32_DBG_BUF_SIZE
31563 #  define SQLITE_WIN32_DBG_BUF_SIZE   ((int)(4096-sizeof(DWORD)))
31564 #endif
31565 
31566 /*
31567  * The value used with sqlite3_win32_set_directory() to specify that
31568  * the data directory should be changed.
31569  */
31570 #ifndef SQLITE_WIN32_DATA_DIRECTORY_TYPE
31571 #  define SQLITE_WIN32_DATA_DIRECTORY_TYPE (1)
31572 #endif
31573 
31574 /*
31575  * The value used with sqlite3_win32_set_directory() to specify that
31576  * the temporary directory should be changed.
31577  */
31578 #ifndef SQLITE_WIN32_TEMP_DIRECTORY_TYPE
31579 #  define SQLITE_WIN32_TEMP_DIRECTORY_TYPE (2)
31580 #endif
31581 
31582 /*
31583  * If compiled with SQLITE_WIN32_MALLOC on Windows, we will use the
31584  * various Win32 API heap functions instead of our own.
31585  */
31586 #ifdef SQLITE_WIN32_MALLOC
31587 
31588 /*
31589  * If this is non-zero, an isolated heap will be created by the native Win32
31590  * allocator subsystem; otherwise, the default process heap will be used.  This
31591  * setting has no effect when compiling for WinRT.  By default, this is enabled
31592  * and an isolated heap will be created to store all allocated data.
31593  *
31594  ******************************************************************************
31595  * WARNING: It is important to note that when this setting is non-zero and the
31596  *          winMemShutdown function is called (e.g. by the sqlite3_shutdown
31597  *          function), all data that was allocated using the isolated heap will
31598  *          be freed immediately and any attempt to access any of that freed
31599  *          data will almost certainly result in an immediate access violation.
31600  ******************************************************************************
31601  */
31602 #ifndef SQLITE_WIN32_HEAP_CREATE
31603 #  define SQLITE_WIN32_HEAP_CREATE    (TRUE)
31604 #endif
31605 
31606 /*
31607  * The initial size of the Win32-specific heap.  This value may be zero.
31608  */
31609 #ifndef SQLITE_WIN32_HEAP_INIT_SIZE
31610 #  define SQLITE_WIN32_HEAP_INIT_SIZE ((SQLITE_DEFAULT_CACHE_SIZE) * \
31611                                        (SQLITE_DEFAULT_PAGE_SIZE) + 4194304)
31612 #endif
31613 
31614 /*
31615  * The maximum size of the Win32-specific heap.  This value may be zero.
31616  */
31617 #ifndef SQLITE_WIN32_HEAP_MAX_SIZE
31618 #  define SQLITE_WIN32_HEAP_MAX_SIZE  (0)
31619 #endif
31620 
31621 /*
31622  * The extra flags to use in calls to the Win32 heap APIs.  This value may be
31623  * zero for the default behavior.
31624  */
31625 #ifndef SQLITE_WIN32_HEAP_FLAGS
31626 #  define SQLITE_WIN32_HEAP_FLAGS     (0)
31627 #endif
31628 
31629 
31630 /*
31631 ** The winMemData structure stores information required by the Win32-specific
31632 ** sqlite3_mem_methods implementation.
31633 */
31634 typedef struct winMemData winMemData;
31635 struct winMemData {
31636 #ifndef NDEBUG
31637   u32 magic1;   /* Magic number to detect structure corruption. */
31638 #endif
31639   HANDLE hHeap; /* The handle to our heap. */
31640   BOOL bOwned;  /* Do we own the heap (i.e. destroy it on shutdown)? */
31641 #ifndef NDEBUG
31642   u32 magic2;   /* Magic number to detect structure corruption. */
31643 #endif
31644 };
31645 
31646 #ifndef NDEBUG
31647 #define WINMEM_MAGIC1     0x42b2830b
31648 #define WINMEM_MAGIC2     0xbd4d7cf4
31649 #endif
31650 
31651 static struct winMemData win_mem_data = {
31652 #ifndef NDEBUG
31653   WINMEM_MAGIC1,
31654 #endif
31655   NULL, FALSE
31656 #ifndef NDEBUG
31657   ,WINMEM_MAGIC2
31658 #endif
31659 };
31660 
31661 #ifndef NDEBUG
31662 #define winMemAssertMagic1() assert( win_mem_data.magic1==WINMEM_MAGIC1 )
31663 #define winMemAssertMagic2() assert( win_mem_data.magic2==WINMEM_MAGIC2 )
31664 #define winMemAssertMagic()  winMemAssertMagic1(); winMemAssertMagic2();
31665 #else
31666 #define winMemAssertMagic()
31667 #endif
31668 
31669 #define winMemGetDataPtr()  &win_mem_data
31670 #define winMemGetHeap()     win_mem_data.hHeap
31671 #define winMemGetOwned()    win_mem_data.bOwned
31672 
31673 static void *winMemMalloc(int nBytes);
31674 static void winMemFree(void *pPrior);
31675 static void *winMemRealloc(void *pPrior, int nBytes);
31676 static int winMemSize(void *p);
31677 static int winMemRoundup(int n);
31678 static int winMemInit(void *pAppData);
31679 static void winMemShutdown(void *pAppData);
31680 
31681 SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetWin32(void);
31682 #endif /* SQLITE_WIN32_MALLOC */
31683 
31684 /*
31685 ** The following variable is (normally) set once and never changes
31686 ** thereafter.  It records whether the operating system is Win9x
31687 ** or WinNT.
31688 **
31689 ** 0:   Operating system unknown.
31690 ** 1:   Operating system is Win9x.
31691 ** 2:   Operating system is WinNT.
31692 **
31693 ** In order to facilitate testing on a WinNT system, the test fixture
31694 ** can manually set this value to 1 to emulate Win98 behavior.
31695 */
31696 #ifdef SQLITE_TEST
31697 SQLITE_API int sqlite3_os_type = 0;
31698 #elif !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && \
31699       defined(SQLITE_WIN32_HAS_ANSI) && defined(SQLITE_WIN32_HAS_WIDE)
31700 static int sqlite3_os_type = 0;
31701 #endif
31702 
31703 #ifndef SYSCALL
31704 #  define SYSCALL sqlite3_syscall_ptr
31705 #endif
31706 
31707 /*
31708 ** This function is not available on Windows CE or WinRT.
31709  */
31710 
31711 #if SQLITE_OS_WINCE || SQLITE_OS_WINRT
31712 #  define osAreFileApisANSI()       1
31713 #endif
31714 
31715 /*
31716 ** Many system calls are accessed through pointer-to-functions so that
31717 ** they may be overridden at runtime to facilitate fault injection during
31718 ** testing and sandboxing.  The following array holds the names and pointers
31719 ** to all overrideable system calls.
31720 */
31721 static struct win_syscall {
31722   const char *zName;            /* Name of the system call */
31723   sqlite3_syscall_ptr pCurrent; /* Current value of the system call */
31724   sqlite3_syscall_ptr pDefault; /* Default value */
31725 } aSyscall[] = {
31726 #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
31727   { "AreFileApisANSI",         (SYSCALL)AreFileApisANSI,         0 },
31728 #else
31729   { "AreFileApisANSI",         (SYSCALL)0,                       0 },
31730 #endif
31731 
31732 #ifndef osAreFileApisANSI
31733 #define osAreFileApisANSI ((BOOL(WINAPI*)(VOID))aSyscall[0].pCurrent)
31734 #endif
31735 
31736 #if SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_WIDE)
31737   { "CharLowerW",              (SYSCALL)CharLowerW,              0 },
31738 #else
31739   { "CharLowerW",              (SYSCALL)0,                       0 },
31740 #endif
31741 
31742 #define osCharLowerW ((LPWSTR(WINAPI*)(LPWSTR))aSyscall[1].pCurrent)
31743 
31744 #if SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_WIDE)
31745   { "CharUpperW",              (SYSCALL)CharUpperW,              0 },
31746 #else
31747   { "CharUpperW",              (SYSCALL)0,                       0 },
31748 #endif
31749 
31750 #define osCharUpperW ((LPWSTR(WINAPI*)(LPWSTR))aSyscall[2].pCurrent)
31751 
31752   { "CloseHandle",             (SYSCALL)CloseHandle,             0 },
31753 
31754 #define osCloseHandle ((BOOL(WINAPI*)(HANDLE))aSyscall[3].pCurrent)
31755 
31756 #if defined(SQLITE_WIN32_HAS_ANSI)
31757   { "CreateFileA",             (SYSCALL)CreateFileA,             0 },
31758 #else
31759   { "CreateFileA",             (SYSCALL)0,                       0 },
31760 #endif
31761 
31762 #define osCreateFileA ((HANDLE(WINAPI*)(LPCSTR,DWORD,DWORD, \
31763         LPSECURITY_ATTRIBUTES,DWORD,DWORD,HANDLE))aSyscall[4].pCurrent)
31764 
31765 #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
31766   { "CreateFileW",             (SYSCALL)CreateFileW,             0 },
31767 #else
31768   { "CreateFileW",             (SYSCALL)0,                       0 },
31769 #endif
31770 
31771 #define osCreateFileW ((HANDLE(WINAPI*)(LPCWSTR,DWORD,DWORD, \
31772         LPSECURITY_ATTRIBUTES,DWORD,DWORD,HANDLE))aSyscall[5].pCurrent)
31773 
31774 #if (!SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_ANSI) && \
31775         !defined(SQLITE_OMIT_WAL))
31776   { "CreateFileMappingA",      (SYSCALL)CreateFileMappingA,      0 },
31777 #else
31778   { "CreateFileMappingA",      (SYSCALL)0,                       0 },
31779 #endif
31780 
31781 #define osCreateFileMappingA ((HANDLE(WINAPI*)(HANDLE,LPSECURITY_ATTRIBUTES, \
31782         DWORD,DWORD,DWORD,LPCSTR))aSyscall[6].pCurrent)
31783 
31784 #if SQLITE_OS_WINCE || (!SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE) && \
31785         !defined(SQLITE_OMIT_WAL))
31786   { "CreateFileMappingW",      (SYSCALL)CreateFileMappingW,      0 },
31787 #else
31788   { "CreateFileMappingW",      (SYSCALL)0,                       0 },
31789 #endif
31790 
31791 #define osCreateFileMappingW ((HANDLE(WINAPI*)(HANDLE,LPSECURITY_ATTRIBUTES, \
31792         DWORD,DWORD,DWORD,LPCWSTR))aSyscall[7].pCurrent)
31793 
31794 #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
31795   { "CreateMutexW",            (SYSCALL)CreateMutexW,            0 },
31796 #else
31797   { "CreateMutexW",            (SYSCALL)0,                       0 },
31798 #endif
31799 
31800 #define osCreateMutexW ((HANDLE(WINAPI*)(LPSECURITY_ATTRIBUTES,BOOL, \
31801         LPCWSTR))aSyscall[8].pCurrent)
31802 
31803 #if defined(SQLITE_WIN32_HAS_ANSI)
31804   { "DeleteFileA",             (SYSCALL)DeleteFileA,             0 },
31805 #else
31806   { "DeleteFileA",             (SYSCALL)0,                       0 },
31807 #endif
31808 
31809 #define osDeleteFileA ((BOOL(WINAPI*)(LPCSTR))aSyscall[9].pCurrent)
31810 
31811 #if defined(SQLITE_WIN32_HAS_WIDE)
31812   { "DeleteFileW",             (SYSCALL)DeleteFileW,             0 },
31813 #else
31814   { "DeleteFileW",             (SYSCALL)0,                       0 },
31815 #endif
31816 
31817 #define osDeleteFileW ((BOOL(WINAPI*)(LPCWSTR))aSyscall[10].pCurrent)
31818 
31819 #if SQLITE_OS_WINCE
31820   { "FileTimeToLocalFileTime", (SYSCALL)FileTimeToLocalFileTime, 0 },
31821 #else
31822   { "FileTimeToLocalFileTime", (SYSCALL)0,                       0 },
31823 #endif
31824 
31825 #define osFileTimeToLocalFileTime ((BOOL(WINAPI*)(CONST FILETIME*, \
31826         LPFILETIME))aSyscall[11].pCurrent)
31827 
31828 #if SQLITE_OS_WINCE
31829   { "FileTimeToSystemTime",    (SYSCALL)FileTimeToSystemTime,    0 },
31830 #else
31831   { "FileTimeToSystemTime",    (SYSCALL)0,                       0 },
31832 #endif
31833 
31834 #define osFileTimeToSystemTime ((BOOL(WINAPI*)(CONST FILETIME*, \
31835         LPSYSTEMTIME))aSyscall[12].pCurrent)
31836 
31837   { "FlushFileBuffers",        (SYSCALL)FlushFileBuffers,        0 },
31838 
31839 #define osFlushFileBuffers ((BOOL(WINAPI*)(HANDLE))aSyscall[13].pCurrent)
31840 
31841 #if defined(SQLITE_WIN32_HAS_ANSI)
31842   { "FormatMessageA",          (SYSCALL)FormatMessageA,          0 },
31843 #else
31844   { "FormatMessageA",          (SYSCALL)0,                       0 },
31845 #endif
31846 
31847 #define osFormatMessageA ((DWORD(WINAPI*)(DWORD,LPCVOID,DWORD,DWORD,LPSTR, \
31848         DWORD,va_list*))aSyscall[14].pCurrent)
31849 
31850 #if defined(SQLITE_WIN32_HAS_WIDE)
31851   { "FormatMessageW",          (SYSCALL)FormatMessageW,          0 },
31852 #else
31853   { "FormatMessageW",          (SYSCALL)0,                       0 },
31854 #endif
31855 
31856 #define osFormatMessageW ((DWORD(WINAPI*)(DWORD,LPCVOID,DWORD,DWORD,LPWSTR, \
31857         DWORD,va_list*))aSyscall[15].pCurrent)
31858 
31859 #if !defined(SQLITE_OMIT_LOAD_EXTENSION)
31860   { "FreeLibrary",             (SYSCALL)FreeLibrary,             0 },
31861 #else
31862   { "FreeLibrary",             (SYSCALL)0,                       0 },
31863 #endif
31864 
31865 #define osFreeLibrary ((BOOL(WINAPI*)(HMODULE))aSyscall[16].pCurrent)
31866 
31867   { "GetCurrentProcessId",     (SYSCALL)GetCurrentProcessId,     0 },
31868 
31869 #define osGetCurrentProcessId ((DWORD(WINAPI*)(VOID))aSyscall[17].pCurrent)
31870 
31871 #if !SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_ANSI)
31872   { "GetDiskFreeSpaceA",       (SYSCALL)GetDiskFreeSpaceA,       0 },
31873 #else
31874   { "GetDiskFreeSpaceA",       (SYSCALL)0,                       0 },
31875 #endif
31876 
31877 #define osGetDiskFreeSpaceA ((BOOL(WINAPI*)(LPCSTR,LPDWORD,LPDWORD,LPDWORD, \
31878         LPDWORD))aSyscall[18].pCurrent)
31879 
31880 #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
31881   { "GetDiskFreeSpaceW",       (SYSCALL)GetDiskFreeSpaceW,       0 },
31882 #else
31883   { "GetDiskFreeSpaceW",       (SYSCALL)0,                       0 },
31884 #endif
31885 
31886 #define osGetDiskFreeSpaceW ((BOOL(WINAPI*)(LPCWSTR,LPDWORD,LPDWORD,LPDWORD, \
31887         LPDWORD))aSyscall[19].pCurrent)
31888 
31889 #if defined(SQLITE_WIN32_HAS_ANSI)
31890   { "GetFileAttributesA",      (SYSCALL)GetFileAttributesA,      0 },
31891 #else
31892   { "GetFileAttributesA",      (SYSCALL)0,                       0 },
31893 #endif
31894 
31895 #define osGetFileAttributesA ((DWORD(WINAPI*)(LPCSTR))aSyscall[20].pCurrent)
31896 
31897 #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
31898   { "GetFileAttributesW",      (SYSCALL)GetFileAttributesW,      0 },
31899 #else
31900   { "GetFileAttributesW",      (SYSCALL)0,                       0 },
31901 #endif
31902 
31903 #define osGetFileAttributesW ((DWORD(WINAPI*)(LPCWSTR))aSyscall[21].pCurrent)
31904 
31905 #if defined(SQLITE_WIN32_HAS_WIDE)
31906   { "GetFileAttributesExW",    (SYSCALL)GetFileAttributesExW,    0 },
31907 #else
31908   { "GetFileAttributesExW",    (SYSCALL)0,                       0 },
31909 #endif
31910 
31911 #define osGetFileAttributesExW ((BOOL(WINAPI*)(LPCWSTR,GET_FILEEX_INFO_LEVELS, \
31912         LPVOID))aSyscall[22].pCurrent)
31913 
31914 #if !SQLITE_OS_WINRT
31915   { "GetFileSize",             (SYSCALL)GetFileSize,             0 },
31916 #else
31917   { "GetFileSize",             (SYSCALL)0,                       0 },
31918 #endif
31919 
31920 #define osGetFileSize ((DWORD(WINAPI*)(HANDLE,LPDWORD))aSyscall[23].pCurrent)
31921 
31922 #if !SQLITE_OS_WINCE && defined(SQLITE_WIN32_HAS_ANSI)
31923   { "GetFullPathNameA",        (SYSCALL)GetFullPathNameA,        0 },
31924 #else
31925   { "GetFullPathNameA",        (SYSCALL)0,                       0 },
31926 #endif
31927 
31928 #define osGetFullPathNameA ((DWORD(WINAPI*)(LPCSTR,DWORD,LPSTR, \
31929         LPSTR*))aSyscall[24].pCurrent)
31930 
31931 #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
31932   { "GetFullPathNameW",        (SYSCALL)GetFullPathNameW,        0 },
31933 #else
31934   { "GetFullPathNameW",        (SYSCALL)0,                       0 },
31935 #endif
31936 
31937 #define osGetFullPathNameW ((DWORD(WINAPI*)(LPCWSTR,DWORD,LPWSTR, \
31938         LPWSTR*))aSyscall[25].pCurrent)
31939 
31940   { "GetLastError",            (SYSCALL)GetLastError,            0 },
31941 
31942 #define osGetLastError ((DWORD(WINAPI*)(VOID))aSyscall[26].pCurrent)
31943 
31944 #if !defined(SQLITE_OMIT_LOAD_EXTENSION)
31945 #if SQLITE_OS_WINCE
31946   /* The GetProcAddressA() routine is only available on Windows CE. */
31947   { "GetProcAddressA",         (SYSCALL)GetProcAddressA,         0 },
31948 #else
31949   /* All other Windows platforms expect GetProcAddress() to take
31950   ** an ANSI string regardless of the _UNICODE setting */
31951   { "GetProcAddressA",         (SYSCALL)GetProcAddress,          0 },
31952 #endif
31953 #else
31954   { "GetProcAddressA",         (SYSCALL)0,                       0 },
31955 #endif
31956 
31957 #define osGetProcAddressA ((FARPROC(WINAPI*)(HMODULE, \
31958         LPCSTR))aSyscall[27].pCurrent)
31959 
31960 #if !SQLITE_OS_WINRT
31961   { "GetSystemInfo",           (SYSCALL)GetSystemInfo,           0 },
31962 #else
31963   { "GetSystemInfo",           (SYSCALL)0,                       0 },
31964 #endif
31965 
31966 #define osGetSystemInfo ((VOID(WINAPI*)(LPSYSTEM_INFO))aSyscall[28].pCurrent)
31967 
31968   { "GetSystemTime",           (SYSCALL)GetSystemTime,           0 },
31969 
31970 #define osGetSystemTime ((VOID(WINAPI*)(LPSYSTEMTIME))aSyscall[29].pCurrent)
31971 
31972 #if !SQLITE_OS_WINCE
31973   { "GetSystemTimeAsFileTime", (SYSCALL)GetSystemTimeAsFileTime, 0 },
31974 #else
31975   { "GetSystemTimeAsFileTime", (SYSCALL)0,                       0 },
31976 #endif
31977 
31978 #define osGetSystemTimeAsFileTime ((VOID(WINAPI*)( \
31979         LPFILETIME))aSyscall[30].pCurrent)
31980 
31981 #if defined(SQLITE_WIN32_HAS_ANSI)
31982   { "GetTempPathA",            (SYSCALL)GetTempPathA,            0 },
31983 #else
31984   { "GetTempPathA",            (SYSCALL)0,                       0 },
31985 #endif
31986 
31987 #define osGetTempPathA ((DWORD(WINAPI*)(DWORD,LPSTR))aSyscall[31].pCurrent)
31988 
31989 #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE)
31990   { "GetTempPathW",            (SYSCALL)GetTempPathW,            0 },
31991 #else
31992   { "GetTempPathW",            (SYSCALL)0,                       0 },
31993 #endif
31994 
31995 #define osGetTempPathW ((DWORD(WINAPI*)(DWORD,LPWSTR))aSyscall[32].pCurrent)
31996 
31997 #if !SQLITE_OS_WINRT
31998   { "GetTickCount",            (SYSCALL)GetTickCount,            0 },
31999 #else
32000   { "GetTickCount",            (SYSCALL)0,                       0 },
32001 #endif
32002 
32003 #define osGetTickCount ((DWORD(WINAPI*)(VOID))aSyscall[33].pCurrent)
32004 
32005 #if defined(SQLITE_WIN32_HAS_ANSI) && defined(SQLITE_WIN32_GETVERSIONEX) && \
32006         SQLITE_WIN32_GETVERSIONEX
32007   { "GetVersionExA",           (SYSCALL)GetVersionExA,           0 },
32008 #else
32009   { "GetVersionExA",           (SYSCALL)0,                       0 },
32010 #endif
32011 
32012 #define osGetVersionExA ((BOOL(WINAPI*)( \
32013         LPOSVERSIONINFOA))aSyscall[34].pCurrent)
32014 
32015 #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE) && \
32016         defined(SQLITE_WIN32_GETVERSIONEX) && SQLITE_WIN32_GETVERSIONEX
32017   { "GetVersionExW",           (SYSCALL)GetVersionExW,           0 },
32018 #else
32019   { "GetVersionExW",           (SYSCALL)0,                       0 },
32020 #endif
32021 
32022 #define osGetVersionExW ((BOOL(WINAPI*)( \
32023         LPOSVERSIONINFOW))aSyscall[35].pCurrent)
32024 
32025   { "HeapAlloc",               (SYSCALL)HeapAlloc,               0 },
32026 
32027 #define osHeapAlloc ((LPVOID(WINAPI*)(HANDLE,DWORD, \
32028         SIZE_T))aSyscall[36].pCurrent)
32029 
32030 #if !SQLITE_OS_WINRT
32031   { "HeapCreate",              (SYSCALL)HeapCreate,              0 },
32032 #else
32033   { "HeapCreate",              (SYSCALL)0,                       0 },
32034 #endif
32035 
32036 #define osHeapCreate ((HANDLE(WINAPI*)(DWORD,SIZE_T, \
32037         SIZE_T))aSyscall[37].pCurrent)
32038 
32039 #if !SQLITE_OS_WINRT
32040   { "HeapDestroy",             (SYSCALL)HeapDestroy,             0 },
32041 #else
32042   { "HeapDestroy",             (SYSCALL)0,                       0 },
32043 #endif
32044 
32045 #define osHeapDestroy ((BOOL(WINAPI*)(HANDLE))aSyscall[38].pCurrent)
32046 
32047   { "HeapFree",                (SYSCALL)HeapFree,                0 },
32048 
32049 #define osHeapFree ((BOOL(WINAPI*)(HANDLE,DWORD,LPVOID))aSyscall[39].pCurrent)
32050 
32051   { "HeapReAlloc",             (SYSCALL)HeapReAlloc,             0 },
32052 
32053 #define osHeapReAlloc ((LPVOID(WINAPI*)(HANDLE,DWORD,LPVOID, \
32054         SIZE_T))aSyscall[40].pCurrent)
32055 
32056   { "HeapSize",                (SYSCALL)HeapSize,                0 },
32057 
32058 #define osHeapSize ((SIZE_T(WINAPI*)(HANDLE,DWORD, \
32059         LPCVOID))aSyscall[41].pCurrent)
32060 
32061 #if !SQLITE_OS_WINRT
32062   { "HeapValidate",            (SYSCALL)HeapValidate,            0 },
32063 #else
32064   { "HeapValidate",            (SYSCALL)0,                       0 },
32065 #endif
32066 
32067 #define osHeapValidate ((BOOL(WINAPI*)(HANDLE,DWORD, \
32068         LPCVOID))aSyscall[42].pCurrent)
32069 
32070 #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
32071   { "HeapCompact",             (SYSCALL)HeapCompact,             0 },
32072 #else
32073   { "HeapCompact",             (SYSCALL)0,                       0 },
32074 #endif
32075 
32076 #define osHeapCompact ((UINT(WINAPI*)(HANDLE,DWORD))aSyscall[43].pCurrent)
32077 
32078 #if defined(SQLITE_WIN32_HAS_ANSI) && !defined(SQLITE_OMIT_LOAD_EXTENSION)
32079   { "LoadLibraryA",            (SYSCALL)LoadLibraryA,            0 },
32080 #else
32081   { "LoadLibraryA",            (SYSCALL)0,                       0 },
32082 #endif
32083 
32084 #define osLoadLibraryA ((HMODULE(WINAPI*)(LPCSTR))aSyscall[44].pCurrent)
32085 
32086 #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_HAS_WIDE) && \
32087         !defined(SQLITE_OMIT_LOAD_EXTENSION)
32088   { "LoadLibraryW",            (SYSCALL)LoadLibraryW,            0 },
32089 #else
32090   { "LoadLibraryW",            (SYSCALL)0,                       0 },
32091 #endif
32092 
32093 #define osLoadLibraryW ((HMODULE(WINAPI*)(LPCWSTR))aSyscall[45].pCurrent)
32094 
32095 #if !SQLITE_OS_WINRT
32096   { "LocalFree",               (SYSCALL)LocalFree,               0 },
32097 #else
32098   { "LocalFree",               (SYSCALL)0,                       0 },
32099 #endif
32100 
32101 #define osLocalFree ((HLOCAL(WINAPI*)(HLOCAL))aSyscall[46].pCurrent)
32102 
32103 #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
32104   { "LockFile",                (SYSCALL)LockFile,                0 },
32105 #else
32106   { "LockFile",                (SYSCALL)0,                       0 },
32107 #endif
32108 
32109 #ifndef osLockFile
32110 #define osLockFile ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
32111         DWORD))aSyscall[47].pCurrent)
32112 #endif
32113 
32114 #if !SQLITE_OS_WINCE
32115   { "LockFileEx",              (SYSCALL)LockFileEx,              0 },
32116 #else
32117   { "LockFileEx",              (SYSCALL)0,                       0 },
32118 #endif
32119 
32120 #ifndef osLockFileEx
32121 #define osLockFileEx ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD,DWORD, \
32122         LPOVERLAPPED))aSyscall[48].pCurrent)
32123 #endif
32124 
32125 #if SQLITE_OS_WINCE || (!SQLITE_OS_WINRT && !defined(SQLITE_OMIT_WAL))
32126   { "MapViewOfFile",           (SYSCALL)MapViewOfFile,           0 },
32127 #else
32128   { "MapViewOfFile",           (SYSCALL)0,                       0 },
32129 #endif
32130 
32131 #define osMapViewOfFile ((LPVOID(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
32132         SIZE_T))aSyscall[49].pCurrent)
32133 
32134   { "MultiByteToWideChar",     (SYSCALL)MultiByteToWideChar,     0 },
32135 
32136 #define osMultiByteToWideChar ((int(WINAPI*)(UINT,DWORD,LPCSTR,int,LPWSTR, \
32137         int))aSyscall[50].pCurrent)
32138 
32139   { "QueryPerformanceCounter", (SYSCALL)QueryPerformanceCounter, 0 },
32140 
32141 #define osQueryPerformanceCounter ((BOOL(WINAPI*)( \
32142         LARGE_INTEGER*))aSyscall[51].pCurrent)
32143 
32144   { "ReadFile",                (SYSCALL)ReadFile,                0 },
32145 
32146 #define osReadFile ((BOOL(WINAPI*)(HANDLE,LPVOID,DWORD,LPDWORD, \
32147         LPOVERLAPPED))aSyscall[52].pCurrent)
32148 
32149   { "SetEndOfFile",            (SYSCALL)SetEndOfFile,            0 },
32150 
32151 #define osSetEndOfFile ((BOOL(WINAPI*)(HANDLE))aSyscall[53].pCurrent)
32152 
32153 #if !SQLITE_OS_WINRT
32154   { "SetFilePointer",          (SYSCALL)SetFilePointer,          0 },
32155 #else
32156   { "SetFilePointer",          (SYSCALL)0,                       0 },
32157 #endif
32158 
32159 #define osSetFilePointer ((DWORD(WINAPI*)(HANDLE,LONG,PLONG, \
32160         DWORD))aSyscall[54].pCurrent)
32161 
32162 #if !SQLITE_OS_WINRT
32163   { "Sleep",                   (SYSCALL)Sleep,                   0 },
32164 #else
32165   { "Sleep",                   (SYSCALL)0,                       0 },
32166 #endif
32167 
32168 #define osSleep ((VOID(WINAPI*)(DWORD))aSyscall[55].pCurrent)
32169 
32170   { "SystemTimeToFileTime",    (SYSCALL)SystemTimeToFileTime,    0 },
32171 
32172 #define osSystemTimeToFileTime ((BOOL(WINAPI*)(CONST SYSTEMTIME*, \
32173         LPFILETIME))aSyscall[56].pCurrent)
32174 
32175 #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
32176   { "UnlockFile",              (SYSCALL)UnlockFile,              0 },
32177 #else
32178   { "UnlockFile",              (SYSCALL)0,                       0 },
32179 #endif
32180 
32181 #ifndef osUnlockFile
32182 #define osUnlockFile ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
32183         DWORD))aSyscall[57].pCurrent)
32184 #endif
32185 
32186 #if !SQLITE_OS_WINCE
32187   { "UnlockFileEx",            (SYSCALL)UnlockFileEx,            0 },
32188 #else
32189   { "UnlockFileEx",            (SYSCALL)0,                       0 },
32190 #endif
32191 
32192 #define osUnlockFileEx ((BOOL(WINAPI*)(HANDLE,DWORD,DWORD,DWORD, \
32193         LPOVERLAPPED))aSyscall[58].pCurrent)
32194 
32195 #if SQLITE_OS_WINCE || !defined(SQLITE_OMIT_WAL)
32196   { "UnmapViewOfFile",         (SYSCALL)UnmapViewOfFile,         0 },
32197 #else
32198   { "UnmapViewOfFile",         (SYSCALL)0,                       0 },
32199 #endif
32200 
32201 #define osUnmapViewOfFile ((BOOL(WINAPI*)(LPCVOID))aSyscall[59].pCurrent)
32202 
32203   { "WideCharToMultiByte",     (SYSCALL)WideCharToMultiByte,     0 },
32204 
32205 #define osWideCharToMultiByte ((int(WINAPI*)(UINT,DWORD,LPCWSTR,int,LPSTR,int, \
32206         LPCSTR,LPBOOL))aSyscall[60].pCurrent)
32207 
32208   { "WriteFile",               (SYSCALL)WriteFile,               0 },
32209 
32210 #define osWriteFile ((BOOL(WINAPI*)(HANDLE,LPCVOID,DWORD,LPDWORD, \
32211         LPOVERLAPPED))aSyscall[61].pCurrent)
32212 
32213 #if SQLITE_OS_WINRT
32214   { "CreateEventExW",          (SYSCALL)CreateEventExW,          0 },
32215 #else
32216   { "CreateEventExW",          (SYSCALL)0,                       0 },
32217 #endif
32218 
32219 #define osCreateEventExW ((HANDLE(WINAPI*)(LPSECURITY_ATTRIBUTES,LPCWSTR, \
32220         DWORD,DWORD))aSyscall[62].pCurrent)
32221 
32222 #if !SQLITE_OS_WINRT
32223   { "WaitForSingleObject",     (SYSCALL)WaitForSingleObject,     0 },
32224 #else
32225   { "WaitForSingleObject",     (SYSCALL)0,                       0 },
32226 #endif
32227 
32228 #define osWaitForSingleObject ((DWORD(WINAPI*)(HANDLE, \
32229         DWORD))aSyscall[63].pCurrent)
32230 
32231 #if SQLITE_OS_WINRT
32232   { "WaitForSingleObjectEx",   (SYSCALL)WaitForSingleObjectEx,   0 },
32233 #else
32234   { "WaitForSingleObjectEx",   (SYSCALL)0,                       0 },
32235 #endif
32236 
32237 #define osWaitForSingleObjectEx ((DWORD(WINAPI*)(HANDLE,DWORD, \
32238         BOOL))aSyscall[64].pCurrent)
32239 
32240 #if SQLITE_OS_WINRT
32241   { "SetFilePointerEx",        (SYSCALL)SetFilePointerEx,        0 },
32242 #else
32243   { "SetFilePointerEx",        (SYSCALL)0,                       0 },
32244 #endif
32245 
32246 #define osSetFilePointerEx ((BOOL(WINAPI*)(HANDLE,LARGE_INTEGER, \
32247         PLARGE_INTEGER,DWORD))aSyscall[65].pCurrent)
32248 
32249 #if SQLITE_OS_WINRT
32250   { "GetFileInformationByHandleEx", (SYSCALL)GetFileInformationByHandleEx, 0 },
32251 #else
32252   { "GetFileInformationByHandleEx", (SYSCALL)0,                  0 },
32253 #endif
32254 
32255 #define osGetFileInformationByHandleEx ((BOOL(WINAPI*)(HANDLE, \
32256         FILE_INFO_BY_HANDLE_CLASS,LPVOID,DWORD))aSyscall[66].pCurrent)
32257 
32258 #if SQLITE_OS_WINRT && !defined(SQLITE_OMIT_WAL)
32259   { "MapViewOfFileFromApp",    (SYSCALL)MapViewOfFileFromApp,    0 },
32260 #else
32261   { "MapViewOfFileFromApp",    (SYSCALL)0,                       0 },
32262 #endif
32263 
32264 #define osMapViewOfFileFromApp ((LPVOID(WINAPI*)(HANDLE,ULONG,ULONG64, \
32265         SIZE_T))aSyscall[67].pCurrent)
32266 
32267 #if SQLITE_OS_WINRT
32268   { "CreateFile2",             (SYSCALL)CreateFile2,             0 },
32269 #else
32270   { "CreateFile2",             (SYSCALL)0,                       0 },
32271 #endif
32272 
32273 #define osCreateFile2 ((HANDLE(WINAPI*)(LPCWSTR,DWORD,DWORD,DWORD, \
32274         LPCREATEFILE2_EXTENDED_PARAMETERS))aSyscall[68].pCurrent)
32275 
32276 #if SQLITE_OS_WINRT && !defined(SQLITE_OMIT_LOAD_EXTENSION)
32277   { "LoadPackagedLibrary",     (SYSCALL)LoadPackagedLibrary,     0 },
32278 #else
32279   { "LoadPackagedLibrary",     (SYSCALL)0,                       0 },
32280 #endif
32281 
32282 #define osLoadPackagedLibrary ((HMODULE(WINAPI*)(LPCWSTR, \
32283         DWORD))aSyscall[69].pCurrent)
32284 
32285 #if SQLITE_OS_WINRT
32286   { "GetTickCount64",          (SYSCALL)GetTickCount64,          0 },
32287 #else
32288   { "GetTickCount64",          (SYSCALL)0,                       0 },
32289 #endif
32290 
32291 #define osGetTickCount64 ((ULONGLONG(WINAPI*)(VOID))aSyscall[70].pCurrent)
32292 
32293 #if SQLITE_OS_WINRT
32294   { "GetNativeSystemInfo",     (SYSCALL)GetNativeSystemInfo,     0 },
32295 #else
32296   { "GetNativeSystemInfo",     (SYSCALL)0,                       0 },
32297 #endif
32298 
32299 #define osGetNativeSystemInfo ((VOID(WINAPI*)( \
32300         LPSYSTEM_INFO))aSyscall[71].pCurrent)
32301 
32302 #if defined(SQLITE_WIN32_HAS_ANSI)
32303   { "OutputDebugStringA",      (SYSCALL)OutputDebugStringA,      0 },
32304 #else
32305   { "OutputDebugStringA",      (SYSCALL)0,                       0 },
32306 #endif
32307 
32308 #define osOutputDebugStringA ((VOID(WINAPI*)(LPCSTR))aSyscall[72].pCurrent)
32309 
32310 #if defined(SQLITE_WIN32_HAS_WIDE)
32311   { "OutputDebugStringW",      (SYSCALL)OutputDebugStringW,      0 },
32312 #else
32313   { "OutputDebugStringW",      (SYSCALL)0,                       0 },
32314 #endif
32315 
32316 #define osOutputDebugStringW ((VOID(WINAPI*)(LPCWSTR))aSyscall[73].pCurrent)
32317 
32318   { "GetProcessHeap",          (SYSCALL)GetProcessHeap,          0 },
32319 
32320 #define osGetProcessHeap ((HANDLE(WINAPI*)(VOID))aSyscall[74].pCurrent)
32321 
32322 #if SQLITE_OS_WINRT && !defined(SQLITE_OMIT_WAL)
32323   { "CreateFileMappingFromApp", (SYSCALL)CreateFileMappingFromApp, 0 },
32324 #else
32325   { "CreateFileMappingFromApp", (SYSCALL)0,                      0 },
32326 #endif
32327 
32328 #define osCreateFileMappingFromApp ((HANDLE(WINAPI*)(HANDLE, \
32329         LPSECURITY_ATTRIBUTES,ULONG,ULONG64,LPCWSTR))aSyscall[75].pCurrent)
32330 
32331 }; /* End of the overrideable system calls */
32332 
32333 /*
32334 ** This is the xSetSystemCall() method of sqlite3_vfs for all of the
32335 ** "win32" VFSes.  Return SQLITE_OK opon successfully updating the
32336 ** system call pointer, or SQLITE_NOTFOUND if there is no configurable
32337 ** system call named zName.
32338 */
32339 static int winSetSystemCall(
32340   sqlite3_vfs *pNotUsed,        /* The VFS pointer.  Not used */
32341   const char *zName,            /* Name of system call to override */
32342   sqlite3_syscall_ptr pNewFunc  /* Pointer to new system call value */
32343 ){
32344   unsigned int i;
32345   int rc = SQLITE_NOTFOUND;
32346 
32347   UNUSED_PARAMETER(pNotUsed);
32348   if( zName==0 ){
32349     /* If no zName is given, restore all system calls to their default
32350     ** settings and return NULL
32351     */
32352     rc = SQLITE_OK;
32353     for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
32354       if( aSyscall[i].pDefault ){
32355         aSyscall[i].pCurrent = aSyscall[i].pDefault;
32356       }
32357     }
32358   }else{
32359     /* If zName is specified, operate on only the one system call
32360     ** specified.
32361     */
32362     for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
32363       if( strcmp(zName, aSyscall[i].zName)==0 ){
32364         if( aSyscall[i].pDefault==0 ){
32365           aSyscall[i].pDefault = aSyscall[i].pCurrent;
32366         }
32367         rc = SQLITE_OK;
32368         if( pNewFunc==0 ) pNewFunc = aSyscall[i].pDefault;
32369         aSyscall[i].pCurrent = pNewFunc;
32370         break;
32371       }
32372     }
32373   }
32374   return rc;
32375 }
32376 
32377 /*
32378 ** Return the value of a system call.  Return NULL if zName is not a
32379 ** recognized system call name.  NULL is also returned if the system call
32380 ** is currently undefined.
32381 */
32382 static sqlite3_syscall_ptr winGetSystemCall(
32383   sqlite3_vfs *pNotUsed,
32384   const char *zName
32385 ){
32386   unsigned int i;
32387 
32388   UNUSED_PARAMETER(pNotUsed);
32389   for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
32390     if( strcmp(zName, aSyscall[i].zName)==0 ) return aSyscall[i].pCurrent;
32391   }
32392   return 0;
32393 }
32394 
32395 /*
32396 ** Return the name of the first system call after zName.  If zName==NULL
32397 ** then return the name of the first system call.  Return NULL if zName
32398 ** is the last system call or if zName is not the name of a valid
32399 ** system call.
32400 */
32401 static const char *winNextSystemCall(sqlite3_vfs *p, const char *zName){
32402   int i = -1;
32403 
32404   UNUSED_PARAMETER(p);
32405   if( zName ){
32406     for(i=0; i<ArraySize(aSyscall)-1; i++){
32407       if( strcmp(zName, aSyscall[i].zName)==0 ) break;
32408     }
32409   }
32410   for(i++; i<ArraySize(aSyscall); i++){
32411     if( aSyscall[i].pCurrent!=0 ) return aSyscall[i].zName;
32412   }
32413   return 0;
32414 }
32415 
32416 #ifdef SQLITE_WIN32_MALLOC
32417 /*
32418 ** If a Win32 native heap has been configured, this function will attempt to
32419 ** compact it.  Upon success, SQLITE_OK will be returned.  Upon failure, one
32420 ** of SQLITE_NOMEM, SQLITE_ERROR, or SQLITE_NOTFOUND will be returned.  The
32421 ** "pnLargest" argument, if non-zero, will be used to return the size of the
32422 ** largest committed free block in the heap, in bytes.
32423 */
32424 SQLITE_API int sqlite3_win32_compact_heap(LPUINT pnLargest){
32425   int rc = SQLITE_OK;
32426   UINT nLargest = 0;
32427   HANDLE hHeap;
32428 
32429   winMemAssertMagic();
32430   hHeap = winMemGetHeap();
32431   assert( hHeap!=0 );
32432   assert( hHeap!=INVALID_HANDLE_VALUE );
32433 #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
32434   assert( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
32435 #endif
32436 #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT
32437   if( (nLargest=osHeapCompact(hHeap, SQLITE_WIN32_HEAP_FLAGS))==0 ){
32438     DWORD lastErrno = osGetLastError();
32439     if( lastErrno==NO_ERROR ){
32440       sqlite3_log(SQLITE_NOMEM, "failed to HeapCompact (no space), heap=%p",
32441                   (void*)hHeap);
32442       rc = SQLITE_NOMEM;
32443     }else{
32444       sqlite3_log(SQLITE_ERROR, "failed to HeapCompact (%lu), heap=%p",
32445                   osGetLastError(), (void*)hHeap);
32446       rc = SQLITE_ERROR;
32447     }
32448   }
32449 #else
32450   sqlite3_log(SQLITE_NOTFOUND, "failed to HeapCompact, heap=%p",
32451               (void*)hHeap);
32452   rc = SQLITE_NOTFOUND;
32453 #endif
32454   if( pnLargest ) *pnLargest = nLargest;
32455   return rc;
32456 }
32457 
32458 /*
32459 ** If a Win32 native heap has been configured, this function will attempt to
32460 ** destroy and recreate it.  If the Win32 native heap is not isolated and/or
32461 ** the sqlite3_memory_used() function does not return zero, SQLITE_BUSY will
32462 ** be returned and no changes will be made to the Win32 native heap.
32463 */
32464 SQLITE_API int sqlite3_win32_reset_heap(){
32465   int rc;
32466   MUTEX_LOGIC( sqlite3_mutex *pMaster; ) /* The main static mutex */
32467   MUTEX_LOGIC( sqlite3_mutex *pMem; )    /* The memsys static mutex */
32468   MUTEX_LOGIC( pMaster = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); )
32469   MUTEX_LOGIC( pMem = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MEM); )
32470   sqlite3_mutex_enter(pMaster);
32471   sqlite3_mutex_enter(pMem);
32472   winMemAssertMagic();
32473   if( winMemGetHeap()!=NULL && winMemGetOwned() && sqlite3_memory_used()==0 ){
32474     /*
32475     ** At this point, there should be no outstanding memory allocations on
32476     ** the heap.  Also, since both the master and memsys locks are currently
32477     ** being held by us, no other function (i.e. from another thread) should
32478     ** be able to even access the heap.  Attempt to destroy and recreate our
32479     ** isolated Win32 native heap now.
32480     */
32481     assert( winMemGetHeap()!=NULL );
32482     assert( winMemGetOwned() );
32483     assert( sqlite3_memory_used()==0 );
32484     winMemShutdown(winMemGetDataPtr());
32485     assert( winMemGetHeap()==NULL );
32486     assert( !winMemGetOwned() );
32487     assert( sqlite3_memory_used()==0 );
32488     rc = winMemInit(winMemGetDataPtr());
32489     assert( rc!=SQLITE_OK || winMemGetHeap()!=NULL );
32490     assert( rc!=SQLITE_OK || winMemGetOwned() );
32491     assert( rc!=SQLITE_OK || sqlite3_memory_used()==0 );
32492   }else{
32493     /*
32494     ** The Win32 native heap cannot be modified because it may be in use.
32495     */
32496     rc = SQLITE_BUSY;
32497   }
32498   sqlite3_mutex_leave(pMem);
32499   sqlite3_mutex_leave(pMaster);
32500   return rc;
32501 }
32502 #endif /* SQLITE_WIN32_MALLOC */
32503 
32504 /*
32505 ** This function outputs the specified (ANSI) string to the Win32 debugger
32506 ** (if available).
32507 */
32508 
32509 SQLITE_API void sqlite3_win32_write_debug(const char *zBuf, int nBuf){
32510   char zDbgBuf[SQLITE_WIN32_DBG_BUF_SIZE];
32511   int nMin = MIN(nBuf, (SQLITE_WIN32_DBG_BUF_SIZE - 1)); /* may be negative. */
32512   if( nMin<-1 ) nMin = -1; /* all negative values become -1. */
32513   assert( nMin==-1 || nMin==0 || nMin<SQLITE_WIN32_DBG_BUF_SIZE );
32514 #if defined(SQLITE_WIN32_HAS_ANSI)
32515   if( nMin>0 ){
32516     memset(zDbgBuf, 0, SQLITE_WIN32_DBG_BUF_SIZE);
32517     memcpy(zDbgBuf, zBuf, nMin);
32518     osOutputDebugStringA(zDbgBuf);
32519   }else{
32520     osOutputDebugStringA(zBuf);
32521   }
32522 #elif defined(SQLITE_WIN32_HAS_WIDE)
32523   memset(zDbgBuf, 0, SQLITE_WIN32_DBG_BUF_SIZE);
32524   if ( osMultiByteToWideChar(
32525           osAreFileApisANSI() ? CP_ACP : CP_OEMCP, 0, zBuf,
32526           nMin, (LPWSTR)zDbgBuf, SQLITE_WIN32_DBG_BUF_SIZE/sizeof(WCHAR))<=0 ){
32527     return;
32528   }
32529   osOutputDebugStringW((LPCWSTR)zDbgBuf);
32530 #else
32531   if( nMin>0 ){
32532     memset(zDbgBuf, 0, SQLITE_WIN32_DBG_BUF_SIZE);
32533     memcpy(zDbgBuf, zBuf, nMin);
32534     fprintf(stderr, "%s", zDbgBuf);
32535   }else{
32536     fprintf(stderr, "%s", zBuf);
32537   }
32538 #endif
32539 }
32540 
32541 /*
32542 ** The following routine suspends the current thread for at least ms
32543 ** milliseconds.  This is equivalent to the Win32 Sleep() interface.
32544 */
32545 #if SQLITE_OS_WINRT
32546 static HANDLE sleepObj = NULL;
32547 #endif
32548 
32549 SQLITE_API void sqlite3_win32_sleep(DWORD milliseconds){
32550 #if SQLITE_OS_WINRT
32551   if ( sleepObj==NULL ){
32552     sleepObj = osCreateEventExW(NULL, NULL, CREATE_EVENT_MANUAL_RESET,
32553                                 SYNCHRONIZE);
32554   }
32555   assert( sleepObj!=NULL );
32556   osWaitForSingleObjectEx(sleepObj, milliseconds, FALSE);
32557 #else
32558   osSleep(milliseconds);
32559 #endif
32560 }
32561 
32562 /*
32563 ** Return true (non-zero) if we are running under WinNT, Win2K, WinXP,
32564 ** or WinCE.  Return false (zero) for Win95, Win98, or WinME.
32565 **
32566 ** Here is an interesting observation:  Win95, Win98, and WinME lack
32567 ** the LockFileEx() API.  But we can still statically link against that
32568 ** API as long as we don't call it when running Win95/98/ME.  A call to
32569 ** this routine is used to determine if the host is Win95/98/ME or
32570 ** WinNT/2K/XP so that we will know whether or not we can safely call
32571 ** the LockFileEx() API.
32572 */
32573 
32574 #if !defined(SQLITE_WIN32_GETVERSIONEX) || !SQLITE_WIN32_GETVERSIONEX
32575 # define osIsNT()  (1)
32576 #elif SQLITE_OS_WINCE || SQLITE_OS_WINRT || !defined(SQLITE_WIN32_HAS_ANSI)
32577 # define osIsNT()  (1)
32578 #elif !defined(SQLITE_WIN32_HAS_WIDE)
32579 # define osIsNT()  (0)
32580 #else
32581   static int osIsNT(void){
32582     if( sqlite3_os_type==0 ){
32583 #if defined(NTDDI_VERSION) && NTDDI_VERSION >= NTDDI_WIN8
32584       OSVERSIONINFOW sInfo;
32585       sInfo.dwOSVersionInfoSize = sizeof(sInfo);
32586       osGetVersionExW(&sInfo);
32587 #else
32588       OSVERSIONINFOA sInfo;
32589       sInfo.dwOSVersionInfoSize = sizeof(sInfo);
32590       osGetVersionExA(&sInfo);
32591 #endif
32592       sqlite3_os_type = sInfo.dwPlatformId==VER_PLATFORM_WIN32_NT ? 2 : 1;
32593     }
32594     return sqlite3_os_type==2;
32595   }
32596 #endif
32597 
32598 #ifdef SQLITE_WIN32_MALLOC
32599 /*
32600 ** Allocate nBytes of memory.
32601 */
32602 static void *winMemMalloc(int nBytes){
32603   HANDLE hHeap;
32604   void *p;
32605 
32606   winMemAssertMagic();
32607   hHeap = winMemGetHeap();
32608   assert( hHeap!=0 );
32609   assert( hHeap!=INVALID_HANDLE_VALUE );
32610 #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
32611   assert( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
32612 #endif
32613   assert( nBytes>=0 );
32614   p = osHeapAlloc(hHeap, SQLITE_WIN32_HEAP_FLAGS, (SIZE_T)nBytes);
32615   if( !p ){
32616     sqlite3_log(SQLITE_NOMEM, "failed to HeapAlloc %u bytes (%lu), heap=%p",
32617                 nBytes, osGetLastError(), (void*)hHeap);
32618   }
32619   return p;
32620 }
32621 
32622 /*
32623 ** Free memory.
32624 */
32625 static void winMemFree(void *pPrior){
32626   HANDLE hHeap;
32627 
32628   winMemAssertMagic();
32629   hHeap = winMemGetHeap();
32630   assert( hHeap!=0 );
32631   assert( hHeap!=INVALID_HANDLE_VALUE );
32632 #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
32633   assert( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior) );
32634 #endif
32635   if( !pPrior ) return; /* Passing NULL to HeapFree is undefined. */
32636   if( !osHeapFree(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior) ){
32637     sqlite3_log(SQLITE_NOMEM, "failed to HeapFree block %p (%lu), heap=%p",
32638                 pPrior, osGetLastError(), (void*)hHeap);
32639   }
32640 }
32641 
32642 /*
32643 ** Change the size of an existing memory allocation
32644 */
32645 static void *winMemRealloc(void *pPrior, int nBytes){
32646   HANDLE hHeap;
32647   void *p;
32648 
32649   winMemAssertMagic();
32650   hHeap = winMemGetHeap();
32651   assert( hHeap!=0 );
32652   assert( hHeap!=INVALID_HANDLE_VALUE );
32653 #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
32654   assert( osHeapValidate(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior) );
32655 #endif
32656   assert( nBytes>=0 );
32657   if( !pPrior ){
32658     p = osHeapAlloc(hHeap, SQLITE_WIN32_HEAP_FLAGS, (SIZE_T)nBytes);
32659   }else{
32660     p = osHeapReAlloc(hHeap, SQLITE_WIN32_HEAP_FLAGS, pPrior, (SIZE_T)nBytes);
32661   }
32662   if( !p ){
32663     sqlite3_log(SQLITE_NOMEM, "failed to %s %u bytes (%lu), heap=%p",
32664                 pPrior ? "HeapReAlloc" : "HeapAlloc", nBytes, osGetLastError(),
32665                 (void*)hHeap);
32666   }
32667   return p;
32668 }
32669 
32670 /*
32671 ** Return the size of an outstanding allocation, in bytes.
32672 */
32673 static int winMemSize(void *p){
32674   HANDLE hHeap;
32675   SIZE_T n;
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, p) );
32683 #endif
32684   if( !p ) return 0;
32685   n = osHeapSize(hHeap, SQLITE_WIN32_HEAP_FLAGS, p);
32686   if( n==(SIZE_T)-1 ){
32687     sqlite3_log(SQLITE_NOMEM, "failed to HeapSize block %p (%lu), heap=%p",
32688                 p, osGetLastError(), (void*)hHeap);
32689     return 0;
32690   }
32691   return (int)n;
32692 }
32693 
32694 /*
32695 ** Round up a request size to the next valid allocation size.
32696 */
32697 static int winMemRoundup(int n){
32698   return n;
32699 }
32700 
32701 /*
32702 ** Initialize this module.
32703 */
32704 static int winMemInit(void *pAppData){
32705   winMemData *pWinMemData = (winMemData *)pAppData;
32706 
32707   if( !pWinMemData ) return SQLITE_ERROR;
32708   assert( pWinMemData->magic1==WINMEM_MAGIC1 );
32709   assert( pWinMemData->magic2==WINMEM_MAGIC2 );
32710 
32711 #if !SQLITE_OS_WINRT && SQLITE_WIN32_HEAP_CREATE
32712   if( !pWinMemData->hHeap ){
32713     DWORD dwInitialSize = SQLITE_WIN32_HEAP_INIT_SIZE;
32714     DWORD dwMaximumSize = (DWORD)sqlite3GlobalConfig.nHeap;
32715     if( dwMaximumSize==0 ){
32716       dwMaximumSize = SQLITE_WIN32_HEAP_MAX_SIZE;
32717     }else if( dwInitialSize>dwMaximumSize ){
32718       dwInitialSize = dwMaximumSize;
32719     }
32720     pWinMemData->hHeap = osHeapCreate(SQLITE_WIN32_HEAP_FLAGS,
32721                                       dwInitialSize, dwMaximumSize);
32722     if( !pWinMemData->hHeap ){
32723       sqlite3_log(SQLITE_NOMEM,
32724           "failed to HeapCreate (%lu), flags=%u, initSize=%lu, maxSize=%lu",
32725           osGetLastError(), SQLITE_WIN32_HEAP_FLAGS, dwInitialSize,
32726           dwMaximumSize);
32727       return SQLITE_NOMEM;
32728     }
32729     pWinMemData->bOwned = TRUE;
32730     assert( pWinMemData->bOwned );
32731   }
32732 #else
32733   pWinMemData->hHeap = osGetProcessHeap();
32734   if( !pWinMemData->hHeap ){
32735     sqlite3_log(SQLITE_NOMEM,
32736         "failed to GetProcessHeap (%lu)", osGetLastError());
32737     return SQLITE_NOMEM;
32738   }
32739   pWinMemData->bOwned = FALSE;
32740   assert( !pWinMemData->bOwned );
32741 #endif
32742   assert( pWinMemData->hHeap!=0 );
32743   assert( pWinMemData->hHeap!=INVALID_HANDLE_VALUE );
32744 #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
32745   assert( osHeapValidate(pWinMemData->hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
32746 #endif
32747   return SQLITE_OK;
32748 }
32749 
32750 /*
32751 ** Deinitialize this module.
32752 */
32753 static void winMemShutdown(void *pAppData){
32754   winMemData *pWinMemData = (winMemData *)pAppData;
32755 
32756   if( !pWinMemData ) return;
32757   assert( pWinMemData->magic1==WINMEM_MAGIC1 );
32758   assert( pWinMemData->magic2==WINMEM_MAGIC2 );
32759 
32760   if( pWinMemData->hHeap ){
32761     assert( pWinMemData->hHeap!=INVALID_HANDLE_VALUE );
32762 #if !SQLITE_OS_WINRT && defined(SQLITE_WIN32_MALLOC_VALIDATE)
32763     assert( osHeapValidate(pWinMemData->hHeap, SQLITE_WIN32_HEAP_FLAGS, NULL) );
32764 #endif
32765     if( pWinMemData->bOwned ){
32766       if( !osHeapDestroy(pWinMemData->hHeap) ){
32767         sqlite3_log(SQLITE_NOMEM, "failed to HeapDestroy (%lu), heap=%p",
32768                     osGetLastError(), (void*)pWinMemData->hHeap);
32769       }
32770       pWinMemData->bOwned = FALSE;
32771     }
32772     pWinMemData->hHeap = NULL;
32773   }
32774 }
32775 
32776 /*
32777 ** Populate the low-level memory allocation function pointers in
32778 ** sqlite3GlobalConfig.m with pointers to the routines in this file. The
32779 ** arguments specify the block of memory to manage.
32780 **
32781 ** This routine is only called by sqlite3_config(), and therefore
32782 ** is not required to be threadsafe (it is not).
32783 */
32784 SQLITE_PRIVATE const sqlite3_mem_methods *sqlite3MemGetWin32(void){
32785   static const sqlite3_mem_methods winMemMethods = {
32786     winMemMalloc,
32787     winMemFree,
32788     winMemRealloc,
32789     winMemSize,
32790     winMemRoundup,
32791     winMemInit,
32792     winMemShutdown,
32793     &win_mem_data
32794   };
32795   return &winMemMethods;
32796 }
32797 
32798 SQLITE_PRIVATE void sqlite3MemSetDefault(void){
32799   sqlite3_config(SQLITE_CONFIG_MALLOC, sqlite3MemGetWin32());
32800 }
32801 #endif /* SQLITE_WIN32_MALLOC */
32802 
32803 /*
32804 ** Convert a UTF-8 string to Microsoft Unicode (UTF-16?).
32805 **
32806 ** Space to hold the returned string is obtained from malloc.
32807 */
32808 static LPWSTR winUtf8ToUnicode(const char *zFilename){
32809   int nChar;
32810   LPWSTR zWideFilename;
32811 
32812   nChar = osMultiByteToWideChar(CP_UTF8, 0, zFilename, -1, NULL, 0);
32813   if( nChar==0 ){
32814     return 0;
32815   }
32816   zWideFilename = sqlite3MallocZero( nChar*sizeof(zWideFilename[0]) );
32817   if( zWideFilename==0 ){
32818     return 0;
32819   }
32820   nChar = osMultiByteToWideChar(CP_UTF8, 0, zFilename, -1, zWideFilename,
32821                                 nChar);
32822   if( nChar==0 ){
32823     sqlite3_free(zWideFilename);
32824     zWideFilename = 0;
32825   }
32826   return zWideFilename;
32827 }
32828 
32829 /*
32830 ** Convert Microsoft Unicode to UTF-8.  Space to hold the returned string is
32831 ** obtained from sqlite3_malloc().
32832 */
32833 static char *winUnicodeToUtf8(LPCWSTR zWideFilename){
32834   int nByte;
32835   char *zFilename;
32836 
32837   nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideFilename, -1, 0, 0, 0, 0);
32838   if( nByte == 0 ){
32839     return 0;
32840   }
32841   zFilename = sqlite3MallocZero( nByte );
32842   if( zFilename==0 ){
32843     return 0;
32844   }
32845   nByte = osWideCharToMultiByte(CP_UTF8, 0, zWideFilename, -1, zFilename, nByte,
32846                                 0, 0);
32847   if( nByte == 0 ){
32848     sqlite3_free(zFilename);
32849     zFilename = 0;
32850   }
32851   return zFilename;
32852 }
32853 
32854 /*
32855 ** Convert an ANSI string to Microsoft Unicode, based on the
32856 ** current codepage settings for file apis.
32857 **
32858 ** Space to hold the returned string is obtained
32859 ** from sqlite3_malloc.
32860 */
32861 static LPWSTR winMbcsToUnicode(const char *zFilename){
32862   int nByte;
32863   LPWSTR zMbcsFilename;
32864   int codepage = osAreFileApisANSI() ? CP_ACP : CP_OEMCP;
32865 
32866   nByte = osMultiByteToWideChar(codepage, 0, zFilename, -1, NULL,
32867                                 0)*sizeof(WCHAR);
32868   if( nByte==0 ){
32869     return 0;
32870   }
32871   zMbcsFilename = sqlite3MallocZero( nByte*sizeof(zMbcsFilename[0]) );
32872   if( zMbcsFilename==0 ){
32873     return 0;
32874   }
32875   nByte = osMultiByteToWideChar(codepage, 0, zFilename, -1, zMbcsFilename,
32876                                 nByte);
32877   if( nByte==0 ){
32878     sqlite3_free(zMbcsFilename);
32879     zMbcsFilename = 0;
32880   }
32881   return zMbcsFilename;
32882 }
32883 
32884 /*
32885 ** Convert Microsoft Unicode to multi-byte character string, based on the
32886 ** user's ANSI codepage.
32887 **
32888 ** Space to hold the returned string is obtained from
32889 ** sqlite3_malloc().
32890 */
32891 static char *winUnicodeToMbcs(LPCWSTR zWideFilename){
32892   int nByte;
32893   char *zFilename;
32894   int codepage = osAreFileApisANSI() ? CP_ACP : CP_OEMCP;
32895 
32896   nByte = osWideCharToMultiByte(codepage, 0, zWideFilename, -1, 0, 0, 0, 0);
32897   if( nByte == 0 ){
32898     return 0;
32899   }
32900   zFilename = sqlite3MallocZero( nByte );
32901   if( zFilename==0 ){
32902     return 0;
32903   }
32904   nByte = osWideCharToMultiByte(codepage, 0, zWideFilename, -1, zFilename,
32905                                 nByte, 0, 0);
32906   if( nByte == 0 ){
32907     sqlite3_free(zFilename);
32908     zFilename = 0;
32909   }
32910   return zFilename;
32911 }
32912 
32913 /*
32914 ** Convert multibyte character string to UTF-8.  Space to hold the
32915 ** returned string is obtained from sqlite3_malloc().
32916 */
32917 SQLITE_API char *sqlite3_win32_mbcs_to_utf8(const char *zFilename){
32918   char *zFilenameUtf8;
32919   LPWSTR zTmpWide;
32920 
32921   zTmpWide = winMbcsToUnicode(zFilename);
32922   if( zTmpWide==0 ){
32923     return 0;
32924   }
32925   zFilenameUtf8 = winUnicodeToUtf8(zTmpWide);
32926   sqlite3_free(zTmpWide);
32927   return zFilenameUtf8;
32928 }
32929 
32930 /*
32931 ** Convert UTF-8 to multibyte character string.  Space to hold the
32932 ** returned string is obtained from sqlite3_malloc().
32933 */
32934 SQLITE_API char *sqlite3_win32_utf8_to_mbcs(const char *zFilename){
32935   char *zFilenameMbcs;
32936   LPWSTR zTmpWide;
32937 
32938   zTmpWide = winUtf8ToUnicode(zFilename);
32939   if( zTmpWide==0 ){
32940     return 0;
32941   }
32942   zFilenameMbcs = winUnicodeToMbcs(zTmpWide);
32943   sqlite3_free(zTmpWide);
32944   return zFilenameMbcs;
32945 }
32946 
32947 /*
32948 ** This function sets the data directory or the temporary directory based on
32949 ** the provided arguments.  The type argument must be 1 in order to set the
32950 ** data directory or 2 in order to set the temporary directory.  The zValue
32951 ** argument is the name of the directory to use.  The return value will be
32952 ** SQLITE_OK if successful.
32953 */
32954 SQLITE_API int sqlite3_win32_set_directory(DWORD type, LPCWSTR zValue){
32955   char **ppDirectory = 0;
32956 #ifndef SQLITE_OMIT_AUTOINIT
32957   int rc = sqlite3_initialize();
32958   if( rc ) return rc;
32959 #endif
32960   if( type==SQLITE_WIN32_DATA_DIRECTORY_TYPE ){
32961     ppDirectory = &sqlite3_data_directory;
32962   }else if( type==SQLITE_WIN32_TEMP_DIRECTORY_TYPE ){
32963     ppDirectory = &sqlite3_temp_directory;
32964   }
32965   assert( !ppDirectory || type==SQLITE_WIN32_DATA_DIRECTORY_TYPE
32966           || type==SQLITE_WIN32_TEMP_DIRECTORY_TYPE
32967   );
32968   assert( !ppDirectory || sqlite3MemdebugHasType(*ppDirectory, MEMTYPE_HEAP) );
32969   if( ppDirectory ){
32970     char *zValueUtf8 = 0;
32971     if( zValue && zValue[0] ){
32972       zValueUtf8 = winUnicodeToUtf8(zValue);
32973       if ( zValueUtf8==0 ){
32974         return SQLITE_NOMEM;
32975       }
32976     }
32977     sqlite3_free(*ppDirectory);
32978     *ppDirectory = zValueUtf8;
32979     return SQLITE_OK;
32980   }
32981   return SQLITE_ERROR;
32982 }
32983 
32984 /*
32985 ** The return value of winGetLastErrorMsg
32986 ** is zero if the error message fits in the buffer, or non-zero
32987 ** otherwise (if the message was truncated).
32988 */
32989 static int winGetLastErrorMsg(DWORD lastErrno, int nBuf, char *zBuf){
32990   /* FormatMessage returns 0 on failure.  Otherwise it
32991   ** returns the number of TCHARs written to the output
32992   ** buffer, excluding the terminating null char.
32993   */
32994   DWORD dwLen = 0;
32995   char *zOut = 0;
32996 
32997   if( osIsNT() ){
32998 #if SQLITE_OS_WINRT
32999     WCHAR zTempWide[SQLITE_WIN32_MAX_ERRMSG_CHARS+1];
33000     dwLen = osFormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM |
33001                              FORMAT_MESSAGE_IGNORE_INSERTS,
33002                              NULL,
33003                              lastErrno,
33004                              0,
33005                              zTempWide,
33006                              SQLITE_WIN32_MAX_ERRMSG_CHARS,
33007                              0);
33008 #else
33009     LPWSTR zTempWide = NULL;
33010     dwLen = osFormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER |
33011                              FORMAT_MESSAGE_FROM_SYSTEM |
33012                              FORMAT_MESSAGE_IGNORE_INSERTS,
33013                              NULL,
33014                              lastErrno,
33015                              0,
33016                              (LPWSTR) &zTempWide,
33017                              0,
33018                              0);
33019 #endif
33020     if( dwLen > 0 ){
33021       /* allocate a buffer and convert to UTF8 */
33022       sqlite3BeginBenignMalloc();
33023       zOut = winUnicodeToUtf8(zTempWide);
33024       sqlite3EndBenignMalloc();
33025 #if !SQLITE_OS_WINRT
33026       /* free the system buffer allocated by FormatMessage */
33027       osLocalFree(zTempWide);
33028 #endif
33029     }
33030   }
33031 #ifdef SQLITE_WIN32_HAS_ANSI
33032   else{
33033     char *zTemp = NULL;
33034     dwLen = osFormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER |
33035                              FORMAT_MESSAGE_FROM_SYSTEM |
33036                              FORMAT_MESSAGE_IGNORE_INSERTS,
33037                              NULL,
33038                              lastErrno,
33039                              0,
33040                              (LPSTR) &zTemp,
33041                              0,
33042                              0);
33043     if( dwLen > 0 ){
33044       /* allocate a buffer and convert to UTF8 */
33045       sqlite3BeginBenignMalloc();
33046       zOut = sqlite3_win32_mbcs_to_utf8(zTemp);
33047       sqlite3EndBenignMalloc();
33048       /* free the system buffer allocated by FormatMessage */
33049       osLocalFree(zTemp);
33050     }
33051   }
33052 #endif
33053   if( 0 == dwLen ){
33054     sqlite3_snprintf(nBuf, zBuf, "OsError 0x%lx (%lu)", lastErrno, lastErrno);
33055   }else{
33056     /* copy a maximum of nBuf chars to output buffer */
33057     sqlite3_snprintf(nBuf, zBuf, "%s", zOut);
33058     /* free the UTF8 buffer */
33059     sqlite3_free(zOut);
33060   }
33061   return 0;
33062 }
33063 
33064 /*
33065 **
33066 ** This function - winLogErrorAtLine() - is only ever called via the macro
33067 ** winLogError().
33068 **
33069 ** This routine is invoked after an error occurs in an OS function.
33070 ** It logs a message using sqlite3_log() containing the current value of
33071 ** error code and, if possible, the human-readable equivalent from
33072 ** FormatMessage.
33073 **
33074 ** The first argument passed to the macro should be the error code that
33075 ** will be returned to SQLite (e.g. SQLITE_IOERR_DELETE, SQLITE_CANTOPEN).
33076 ** The two subsequent arguments should be the name of the OS function that
33077 ** failed and the associated file-system path, if any.
33078 */
33079 #define winLogError(a,b,c,d)   winLogErrorAtLine(a,b,c,d,__LINE__)
33080 static int winLogErrorAtLine(
33081   int errcode,                    /* SQLite error code */
33082   DWORD lastErrno,                /* Win32 last error */
33083   const char *zFunc,              /* Name of OS function that failed */
33084   const char *zPath,              /* File path associated with error */
33085   int iLine                       /* Source line number where error occurred */
33086 ){
33087   char zMsg[500];                 /* Human readable error text */
33088   int i;                          /* Loop counter */
33089 
33090   zMsg[0] = 0;
33091   winGetLastErrorMsg(lastErrno, sizeof(zMsg), zMsg);
33092   assert( errcode!=SQLITE_OK );
33093   if( zPath==0 ) zPath = "";
33094   for(i=0; zMsg[i] && zMsg[i]!='\r' && zMsg[i]!='\n'; i++){}
33095   zMsg[i] = 0;
33096   sqlite3_log(errcode,
33097       "os_win.c:%d: (%lu) %s(%s) - %s",
33098       iLine, lastErrno, zFunc, zPath, zMsg
33099   );
33100 
33101   return errcode;
33102 }
33103 
33104 /*
33105 ** The number of times that a ReadFile(), WriteFile(), and DeleteFile()
33106 ** will be retried following a locking error - probably caused by
33107 ** antivirus software.  Also the initial delay before the first retry.
33108 ** The delay increases linearly with each retry.
33109 */
33110 #ifndef SQLITE_WIN32_IOERR_RETRY
33111 # define SQLITE_WIN32_IOERR_RETRY 10
33112 #endif
33113 #ifndef SQLITE_WIN32_IOERR_RETRY_DELAY
33114 # define SQLITE_WIN32_IOERR_RETRY_DELAY 25
33115 #endif
33116 static int winIoerrRetry = SQLITE_WIN32_IOERR_RETRY;
33117 static int winIoerrRetryDelay = SQLITE_WIN32_IOERR_RETRY_DELAY;
33118 
33119 /*
33120 ** If a ReadFile() or WriteFile() error occurs, invoke this routine
33121 ** to see if it should be retried.  Return TRUE to retry.  Return FALSE
33122 ** to give up with an error.
33123 */
33124 static int winRetryIoerr(int *pnRetry, DWORD *pError){
33125   DWORD e = osGetLastError();
33126   if( *pnRetry>=winIoerrRetry ){
33127     if( pError ){
33128       *pError = e;
33129     }
33130     return 0;
33131   }
33132   if( e==ERROR_ACCESS_DENIED ||
33133       e==ERROR_LOCK_VIOLATION ||
33134       e==ERROR_SHARING_VIOLATION ){
33135     sqlite3_win32_sleep(winIoerrRetryDelay*(1+*pnRetry));
33136     ++*pnRetry;
33137     return 1;
33138   }
33139   if( pError ){
33140     *pError = e;
33141   }
33142   return 0;
33143 }
33144 
33145 /*
33146 ** Log a I/O error retry episode.
33147 */
33148 static void winLogIoerr(int nRetry){
33149   if( nRetry ){
33150     sqlite3_log(SQLITE_IOERR,
33151       "delayed %dms for lock/sharing conflict",
33152       winIoerrRetryDelay*nRetry*(nRetry+1)/2
33153     );
33154   }
33155 }
33156 
33157 #if SQLITE_OS_WINCE
33158 /*************************************************************************
33159 ** This section contains code for WinCE only.
33160 */
33161 #if !defined(SQLITE_MSVC_LOCALTIME_API) || !SQLITE_MSVC_LOCALTIME_API
33162 /*
33163 ** The MSVC CRT on Windows CE may not have a localtime() function.  So
33164 ** create a substitute.
33165 */
33166 /* #include <time.h> */
33167 struct tm *__cdecl localtime(const time_t *t)
33168 {
33169   static struct tm y;
33170   FILETIME uTm, lTm;
33171   SYSTEMTIME pTm;
33172   sqlite3_int64 t64;
33173   t64 = *t;
33174   t64 = (t64 + 11644473600)*10000000;
33175   uTm.dwLowDateTime = (DWORD)(t64 & 0xFFFFFFFF);
33176   uTm.dwHighDateTime= (DWORD)(t64 >> 32);
33177   osFileTimeToLocalFileTime(&uTm,&lTm);
33178   osFileTimeToSystemTime(&lTm,&pTm);
33179   y.tm_year = pTm.wYear - 1900;
33180   y.tm_mon = pTm.wMonth - 1;
33181   y.tm_wday = pTm.wDayOfWeek;
33182   y.tm_mday = pTm.wDay;
33183   y.tm_hour = pTm.wHour;
33184   y.tm_min = pTm.wMinute;
33185   y.tm_sec = pTm.wSecond;
33186   return &y;
33187 }
33188 #endif
33189 
33190 #define HANDLE_TO_WINFILE(a) (winFile*)&((char*)a)[-(int)offsetof(winFile,h)]
33191 
33192 /*
33193 ** Acquire a lock on the handle h
33194 */
33195 static void winceMutexAcquire(HANDLE h){
33196    DWORD dwErr;
33197    do {
33198      dwErr = osWaitForSingleObject(h, INFINITE);
33199    } while (dwErr != WAIT_OBJECT_0 && dwErr != WAIT_ABANDONED);
33200 }
33201 /*
33202 ** Release a lock acquired by winceMutexAcquire()
33203 */
33204 #define winceMutexRelease(h) ReleaseMutex(h)
33205 
33206 /*
33207 ** Create the mutex and shared memory used for locking in the file
33208 ** descriptor pFile
33209 */
33210 static int winceCreateLock(const char *zFilename, winFile *pFile){
33211   LPWSTR zTok;
33212   LPWSTR zName;
33213   DWORD lastErrno;
33214   BOOL bLogged = FALSE;
33215   BOOL bInit = TRUE;
33216 
33217   zName = winUtf8ToUnicode(zFilename);
33218   if( zName==0 ){
33219     /* out of memory */
33220     return SQLITE_IOERR_NOMEM;
33221   }
33222 
33223   /* Initialize the local lockdata */
33224   memset(&pFile->local, 0, sizeof(pFile->local));
33225 
33226   /* Replace the backslashes from the filename and lowercase it
33227   ** to derive a mutex name. */
33228   zTok = osCharLowerW(zName);
33229   for (;*zTok;zTok++){
33230     if (*zTok == '\\') *zTok = '_';
33231   }
33232 
33233   /* Create/open the named mutex */
33234   pFile->hMutex = osCreateMutexW(NULL, FALSE, zName);
33235   if (!pFile->hMutex){
33236     pFile->lastErrno = osGetLastError();
33237     sqlite3_free(zName);
33238     return winLogError(SQLITE_IOERR, pFile->lastErrno,
33239                        "winceCreateLock1", zFilename);
33240   }
33241 
33242   /* Acquire the mutex before continuing */
33243   winceMutexAcquire(pFile->hMutex);
33244 
33245   /* Since the names of named mutexes, semaphores, file mappings etc are
33246   ** case-sensitive, take advantage of that by uppercasing the mutex name
33247   ** and using that as the shared filemapping name.
33248   */
33249   osCharUpperW(zName);
33250   pFile->hShared = osCreateFileMappingW(INVALID_HANDLE_VALUE, NULL,
33251                                         PAGE_READWRITE, 0, sizeof(winceLock),
33252                                         zName);
33253 
33254   /* Set a flag that indicates we're the first to create the memory so it
33255   ** must be zero-initialized */
33256   lastErrno = osGetLastError();
33257   if (lastErrno == ERROR_ALREADY_EXISTS){
33258     bInit = FALSE;
33259   }
33260 
33261   sqlite3_free(zName);
33262 
33263   /* If we succeeded in making the shared memory handle, map it. */
33264   if( pFile->hShared ){
33265     pFile->shared = (winceLock*)osMapViewOfFile(pFile->hShared,
33266              FILE_MAP_READ|FILE_MAP_WRITE, 0, 0, sizeof(winceLock));
33267     /* If mapping failed, close the shared memory handle and erase it */
33268     if( !pFile->shared ){
33269       pFile->lastErrno = osGetLastError();
33270       winLogError(SQLITE_IOERR, pFile->lastErrno,
33271                   "winceCreateLock2", zFilename);
33272       bLogged = TRUE;
33273       osCloseHandle(pFile->hShared);
33274       pFile->hShared = NULL;
33275     }
33276   }
33277 
33278   /* If shared memory could not be created, then close the mutex and fail */
33279   if( pFile->hShared==NULL ){
33280     if( !bLogged ){
33281       pFile->lastErrno = lastErrno;
33282       winLogError(SQLITE_IOERR, pFile->lastErrno,
33283                   "winceCreateLock3", zFilename);
33284       bLogged = TRUE;
33285     }
33286     winceMutexRelease(pFile->hMutex);
33287     osCloseHandle(pFile->hMutex);
33288     pFile->hMutex = NULL;
33289     return SQLITE_IOERR;
33290   }
33291 
33292   /* Initialize the shared memory if we're supposed to */
33293   if( bInit ){
33294     memset(pFile->shared, 0, sizeof(winceLock));
33295   }
33296 
33297   winceMutexRelease(pFile->hMutex);
33298   return SQLITE_OK;
33299 }
33300 
33301 /*
33302 ** Destroy the part of winFile that deals with wince locks
33303 */
33304 static void winceDestroyLock(winFile *pFile){
33305   if (pFile->hMutex){
33306     /* Acquire the mutex */
33307     winceMutexAcquire(pFile->hMutex);
33308 
33309     /* The following blocks should probably assert in debug mode, but they
33310        are to cleanup in case any locks remained open */
33311     if (pFile->local.nReaders){
33312       pFile->shared->nReaders --;
33313     }
33314     if (pFile->local.bReserved){
33315       pFile->shared->bReserved = FALSE;
33316     }
33317     if (pFile->local.bPending){
33318       pFile->shared->bPending = FALSE;
33319     }
33320     if (pFile->local.bExclusive){
33321       pFile->shared->bExclusive = FALSE;
33322     }
33323 
33324     /* De-reference and close our copy of the shared memory handle */
33325     osUnmapViewOfFile(pFile->shared);
33326     osCloseHandle(pFile->hShared);
33327 
33328     /* Done with the mutex */
33329     winceMutexRelease(pFile->hMutex);
33330     osCloseHandle(pFile->hMutex);
33331     pFile->hMutex = NULL;
33332   }
33333 }
33334 
33335 /*
33336 ** An implementation of the LockFile() API of Windows for CE
33337 */
33338 static BOOL winceLockFile(
33339   LPHANDLE phFile,
33340   DWORD dwFileOffsetLow,
33341   DWORD dwFileOffsetHigh,
33342   DWORD nNumberOfBytesToLockLow,
33343   DWORD nNumberOfBytesToLockHigh
33344 ){
33345   winFile *pFile = HANDLE_TO_WINFILE(phFile);
33346   BOOL bReturn = FALSE;
33347 
33348   UNUSED_PARAMETER(dwFileOffsetHigh);
33349   UNUSED_PARAMETER(nNumberOfBytesToLockHigh);
33350 
33351   if (!pFile->hMutex) return TRUE;
33352   winceMutexAcquire(pFile->hMutex);
33353 
33354   /* Wanting an exclusive lock? */
33355   if (dwFileOffsetLow == (DWORD)SHARED_FIRST
33356        && nNumberOfBytesToLockLow == (DWORD)SHARED_SIZE){
33357     if (pFile->shared->nReaders == 0 && pFile->shared->bExclusive == 0){
33358        pFile->shared->bExclusive = TRUE;
33359        pFile->local.bExclusive = TRUE;
33360        bReturn = TRUE;
33361     }
33362   }
33363 
33364   /* Want a read-only lock? */
33365   else if (dwFileOffsetLow == (DWORD)SHARED_FIRST &&
33366            nNumberOfBytesToLockLow == 1){
33367     if (pFile->shared->bExclusive == 0){
33368       pFile->local.nReaders ++;
33369       if (pFile->local.nReaders == 1){
33370         pFile->shared->nReaders ++;
33371       }
33372       bReturn = TRUE;
33373     }
33374   }
33375 
33376   /* Want a pending lock? */
33377   else if (dwFileOffsetLow == (DWORD)PENDING_BYTE
33378            && nNumberOfBytesToLockLow == 1){
33379     /* If no pending lock has been acquired, then acquire it */
33380     if (pFile->shared->bPending == 0) {
33381       pFile->shared->bPending = TRUE;
33382       pFile->local.bPending = TRUE;
33383       bReturn = TRUE;
33384     }
33385   }
33386 
33387   /* Want a reserved lock? */
33388   else if (dwFileOffsetLow == (DWORD)RESERVED_BYTE
33389            && nNumberOfBytesToLockLow == 1){
33390     if (pFile->shared->bReserved == 0) {
33391       pFile->shared->bReserved = TRUE;
33392       pFile->local.bReserved = TRUE;
33393       bReturn = TRUE;
33394     }
33395   }
33396 
33397   winceMutexRelease(pFile->hMutex);
33398   return bReturn;
33399 }
33400 
33401 /*
33402 ** An implementation of the UnlockFile API of Windows for CE
33403 */
33404 static BOOL winceUnlockFile(
33405   LPHANDLE phFile,
33406   DWORD dwFileOffsetLow,
33407   DWORD dwFileOffsetHigh,
33408   DWORD nNumberOfBytesToUnlockLow,
33409   DWORD nNumberOfBytesToUnlockHigh
33410 ){
33411   winFile *pFile = HANDLE_TO_WINFILE(phFile);
33412   BOOL bReturn = FALSE;
33413 
33414   UNUSED_PARAMETER(dwFileOffsetHigh);
33415   UNUSED_PARAMETER(nNumberOfBytesToUnlockHigh);
33416 
33417   if (!pFile->hMutex) return TRUE;
33418   winceMutexAcquire(pFile->hMutex);
33419 
33420   /* Releasing a reader lock or an exclusive lock */
33421   if (dwFileOffsetLow == (DWORD)SHARED_FIRST){
33422     /* Did we have an exclusive lock? */
33423     if (pFile->local.bExclusive){
33424       assert(nNumberOfBytesToUnlockLow == (DWORD)SHARED_SIZE);
33425       pFile->local.bExclusive = FALSE;
33426       pFile->shared->bExclusive = FALSE;
33427       bReturn = TRUE;
33428     }
33429 
33430     /* Did we just have a reader lock? */
33431     else if (pFile->local.nReaders){
33432       assert(nNumberOfBytesToUnlockLow == (DWORD)SHARED_SIZE
33433              || nNumberOfBytesToUnlockLow == 1);
33434       pFile->local.nReaders --;
33435       if (pFile->local.nReaders == 0)
33436       {
33437         pFile->shared->nReaders --;
33438       }
33439       bReturn = TRUE;
33440     }
33441   }
33442 
33443   /* Releasing a pending lock */
33444   else if (dwFileOffsetLow == (DWORD)PENDING_BYTE
33445            && nNumberOfBytesToUnlockLow == 1){
33446     if (pFile->local.bPending){
33447       pFile->local.bPending = FALSE;
33448       pFile->shared->bPending = FALSE;
33449       bReturn = TRUE;
33450     }
33451   }
33452   /* Releasing a reserved lock */
33453   else if (dwFileOffsetLow == (DWORD)RESERVED_BYTE
33454            && nNumberOfBytesToUnlockLow == 1){
33455     if (pFile->local.bReserved) {
33456       pFile->local.bReserved = FALSE;
33457       pFile->shared->bReserved = FALSE;
33458       bReturn = TRUE;
33459     }
33460   }
33461 
33462   winceMutexRelease(pFile->hMutex);
33463   return bReturn;
33464 }
33465 /*
33466 ** End of the special code for wince
33467 *****************************************************************************/
33468 #endif /* SQLITE_OS_WINCE */
33469 
33470 /*
33471 ** Lock a file region.
33472 */
33473 static BOOL winLockFile(
33474   LPHANDLE phFile,
33475   DWORD flags,
33476   DWORD offsetLow,
33477   DWORD offsetHigh,
33478   DWORD numBytesLow,
33479   DWORD numBytesHigh
33480 ){
33481 #if SQLITE_OS_WINCE
33482   /*
33483   ** NOTE: Windows CE is handled differently here due its lack of the Win32
33484   **       API LockFile.
33485   */
33486   return winceLockFile(phFile, offsetLow, offsetHigh,
33487                        numBytesLow, numBytesHigh);
33488 #else
33489   if( osIsNT() ){
33490     OVERLAPPED ovlp;
33491     memset(&ovlp, 0, sizeof(OVERLAPPED));
33492     ovlp.Offset = offsetLow;
33493     ovlp.OffsetHigh = offsetHigh;
33494     return osLockFileEx(*phFile, flags, 0, numBytesLow, numBytesHigh, &ovlp);
33495   }else{
33496     return osLockFile(*phFile, offsetLow, offsetHigh, numBytesLow,
33497                       numBytesHigh);
33498   }
33499 #endif
33500 }
33501 
33502 /*
33503 ** Unlock a file region.
33504  */
33505 static BOOL winUnlockFile(
33506   LPHANDLE phFile,
33507   DWORD offsetLow,
33508   DWORD offsetHigh,
33509   DWORD numBytesLow,
33510   DWORD numBytesHigh
33511 ){
33512 #if SQLITE_OS_WINCE
33513   /*
33514   ** NOTE: Windows CE is handled differently here due its lack of the Win32
33515   **       API UnlockFile.
33516   */
33517   return winceUnlockFile(phFile, offsetLow, offsetHigh,
33518                          numBytesLow, numBytesHigh);
33519 #else
33520   if( osIsNT() ){
33521     OVERLAPPED ovlp;
33522     memset(&ovlp, 0, sizeof(OVERLAPPED));
33523     ovlp.Offset = offsetLow;
33524     ovlp.OffsetHigh = offsetHigh;
33525     return osUnlockFileEx(*phFile, 0, numBytesLow, numBytesHigh, &ovlp);
33526   }else{
33527     return osUnlockFile(*phFile, offsetLow, offsetHigh, numBytesLow,
33528                         numBytesHigh);
33529   }
33530 #endif
33531 }
33532 
33533 /*****************************************************************************
33534 ** The next group of routines implement the I/O methods specified
33535 ** by the sqlite3_io_methods object.
33536 ******************************************************************************/
33537 
33538 /*
33539 ** Some Microsoft compilers lack this definition.
33540 */
33541 #ifndef INVALID_SET_FILE_POINTER
33542 # define INVALID_SET_FILE_POINTER ((DWORD)-1)
33543 #endif
33544 
33545 /*
33546 ** Move the current position of the file handle passed as the first
33547 ** argument to offset iOffset within the file. If successful, return 0.
33548 ** Otherwise, set pFile->lastErrno and return non-zero.
33549 */
33550 static int winSeekFile(winFile *pFile, sqlite3_int64 iOffset){
33551 #if !SQLITE_OS_WINRT
33552   LONG upperBits;                 /* Most sig. 32 bits of new offset */
33553   LONG lowerBits;                 /* Least sig. 32 bits of new offset */
33554   DWORD dwRet;                    /* Value returned by SetFilePointer() */
33555   DWORD lastErrno;                /* Value returned by GetLastError() */
33556 
33557   OSTRACE(("SEEK file=%p, offset=%lld\n", pFile->h, iOffset));
33558 
33559   upperBits = (LONG)((iOffset>>32) & 0x7fffffff);
33560   lowerBits = (LONG)(iOffset & 0xffffffff);
33561 
33562   /* API oddity: If successful, SetFilePointer() returns a dword
33563   ** containing the lower 32-bits of the new file-offset. Or, if it fails,
33564   ** it returns INVALID_SET_FILE_POINTER. However according to MSDN,
33565   ** INVALID_SET_FILE_POINTER may also be a valid new offset. So to determine
33566   ** whether an error has actually occurred, it is also necessary to call
33567   ** GetLastError().
33568   */
33569   dwRet = osSetFilePointer(pFile->h, lowerBits, &upperBits, FILE_BEGIN);
33570 
33571   if( (dwRet==INVALID_SET_FILE_POINTER
33572       && ((lastErrno = osGetLastError())!=NO_ERROR)) ){
33573     pFile->lastErrno = lastErrno;
33574     winLogError(SQLITE_IOERR_SEEK, pFile->lastErrno,
33575                 "winSeekFile", pFile->zPath);
33576     OSTRACE(("SEEK file=%p, rc=SQLITE_IOERR_SEEK\n", pFile->h));
33577     return 1;
33578   }
33579 
33580   OSTRACE(("SEEK file=%p, rc=SQLITE_OK\n", pFile->h));
33581   return 0;
33582 #else
33583   /*
33584   ** Same as above, except that this implementation works for WinRT.
33585   */
33586 
33587   LARGE_INTEGER x;                /* The new offset */
33588   BOOL bRet;                      /* Value returned by SetFilePointerEx() */
33589 
33590   x.QuadPart = iOffset;
33591   bRet = osSetFilePointerEx(pFile->h, x, 0, FILE_BEGIN);
33592 
33593   if(!bRet){
33594     pFile->lastErrno = osGetLastError();
33595     winLogError(SQLITE_IOERR_SEEK, pFile->lastErrno,
33596                 "winSeekFile", pFile->zPath);
33597     OSTRACE(("SEEK file=%p, rc=SQLITE_IOERR_SEEK\n", pFile->h));
33598     return 1;
33599   }
33600 
33601   OSTRACE(("SEEK file=%p, rc=SQLITE_OK\n", pFile->h));
33602   return 0;
33603 #endif
33604 }
33605 
33606 #if SQLITE_MAX_MMAP_SIZE>0
33607 /* Forward references to VFS helper methods used for memory mapped files */
33608 static int winMapfile(winFile*, sqlite3_int64);
33609 static int winUnmapfile(winFile*);
33610 #endif
33611 
33612 /*
33613 ** Close a file.
33614 **
33615 ** It is reported that an attempt to close a handle might sometimes
33616 ** fail.  This is a very unreasonable result, but Windows is notorious
33617 ** for being unreasonable so I do not doubt that it might happen.  If
33618 ** the close fails, we pause for 100 milliseconds and try again.  As
33619 ** many as MX_CLOSE_ATTEMPT attempts to close the handle are made before
33620 ** giving up and returning an error.
33621 */
33622 #define MX_CLOSE_ATTEMPT 3
33623 static int winClose(sqlite3_file *id){
33624   int rc, cnt = 0;
33625   winFile *pFile = (winFile*)id;
33626 
33627   assert( id!=0 );
33628 #ifndef SQLITE_OMIT_WAL
33629   assert( pFile->pShm==0 );
33630 #endif
33631   assert( pFile->h!=NULL && pFile->h!=INVALID_HANDLE_VALUE );
33632   OSTRACE(("CLOSE file=%p\n", pFile->h));
33633 
33634 #if SQLITE_MAX_MMAP_SIZE>0
33635   winUnmapfile(pFile);
33636 #endif
33637 
33638   do{
33639     rc = osCloseHandle(pFile->h);
33640     /* SimulateIOError( rc=0; cnt=MX_CLOSE_ATTEMPT; ); */
33641   }while( rc==0 && ++cnt < MX_CLOSE_ATTEMPT && (sqlite3_win32_sleep(100), 1) );
33642 #if SQLITE_OS_WINCE
33643 #define WINCE_DELETION_ATTEMPTS 3
33644   winceDestroyLock(pFile);
33645   if( pFile->zDeleteOnClose ){
33646     int cnt = 0;
33647     while(
33648            osDeleteFileW(pFile->zDeleteOnClose)==0
33649         && osGetFileAttributesW(pFile->zDeleteOnClose)!=0xffffffff
33650         && cnt++ < WINCE_DELETION_ATTEMPTS
33651     ){
33652        sqlite3_win32_sleep(100);  /* Wait a little before trying again */
33653     }
33654     sqlite3_free(pFile->zDeleteOnClose);
33655   }
33656 #endif
33657   if( rc ){
33658     pFile->h = NULL;
33659   }
33660   OpenCounter(-1);
33661   OSTRACE(("CLOSE file=%p, rc=%s\n", pFile->h, rc ? "ok" : "failed"));
33662   return rc ? SQLITE_OK
33663             : winLogError(SQLITE_IOERR_CLOSE, osGetLastError(),
33664                           "winClose", pFile->zPath);
33665 }
33666 
33667 /*
33668 ** Read data from a file into a buffer.  Return SQLITE_OK if all
33669 ** bytes were read successfully and SQLITE_IOERR if anything goes
33670 ** wrong.
33671 */
33672 static int winRead(
33673   sqlite3_file *id,          /* File to read from */
33674   void *pBuf,                /* Write content into this buffer */
33675   int amt,                   /* Number of bytes to read */
33676   sqlite3_int64 offset       /* Begin reading at this offset */
33677 ){
33678 #if !SQLITE_OS_WINCE
33679   OVERLAPPED overlapped;          /* The offset for ReadFile. */
33680 #endif
33681   winFile *pFile = (winFile*)id;  /* file handle */
33682   DWORD nRead;                    /* Number of bytes actually read from file */
33683   int nRetry = 0;                 /* Number of retrys */
33684 
33685   assert( id!=0 );
33686   assert( amt>0 );
33687   assert( offset>=0 );
33688   SimulateIOError(return SQLITE_IOERR_READ);
33689   OSTRACE(("READ file=%p, buffer=%p, amount=%d, offset=%lld, lock=%d\n",
33690            pFile->h, pBuf, amt, offset, pFile->locktype));
33691 
33692 #if SQLITE_MAX_MMAP_SIZE>0
33693   /* Deal with as much of this read request as possible by transfering
33694   ** data from the memory mapping using memcpy().  */
33695   if( offset<pFile->mmapSize ){
33696     if( offset+amt <= pFile->mmapSize ){
33697       memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], amt);
33698       OSTRACE(("READ-MMAP file=%p, rc=SQLITE_OK\n", pFile->h));
33699       return SQLITE_OK;
33700     }else{
33701       int nCopy = (int)(pFile->mmapSize - offset);
33702       memcpy(pBuf, &((u8 *)(pFile->pMapRegion))[offset], nCopy);
33703       pBuf = &((u8 *)pBuf)[nCopy];
33704       amt -= nCopy;
33705       offset += nCopy;
33706     }
33707   }
33708 #endif
33709 
33710 #if SQLITE_OS_WINCE
33711   if( winSeekFile(pFile, offset) ){
33712     OSTRACE(("READ file=%p, rc=SQLITE_FULL\n", pFile->h));
33713     return SQLITE_FULL;
33714   }
33715   while( !osReadFile(pFile->h, pBuf, amt, &nRead, 0) ){
33716 #else
33717   memset(&overlapped, 0, sizeof(OVERLAPPED));
33718   overlapped.Offset = (LONG)(offset & 0xffffffff);
33719   overlapped.OffsetHigh = (LONG)((offset>>32) & 0x7fffffff);
33720   while( !osReadFile(pFile->h, pBuf, amt, &nRead, &overlapped) &&
33721          osGetLastError()!=ERROR_HANDLE_EOF ){
33722 #endif
33723     DWORD lastErrno;
33724     if( winRetryIoerr(&nRetry, &lastErrno) ) continue;
33725     pFile->lastErrno = lastErrno;
33726     OSTRACE(("READ file=%p, rc=SQLITE_IOERR_READ\n", pFile->h));
33727     return winLogError(SQLITE_IOERR_READ, pFile->lastErrno,
33728                        "winRead", pFile->zPath);
33729   }
33730   winLogIoerr(nRetry);
33731   if( nRead<(DWORD)amt ){
33732     /* Unread parts of the buffer must be zero-filled */
33733     memset(&((char*)pBuf)[nRead], 0, amt-nRead);
33734     OSTRACE(("READ file=%p, rc=SQLITE_IOERR_SHORT_READ\n", pFile->h));
33735     return SQLITE_IOERR_SHORT_READ;
33736   }
33737 
33738   OSTRACE(("READ file=%p, rc=SQLITE_OK\n", pFile->h));
33739   return SQLITE_OK;
33740 }
33741 
33742 /*
33743 ** Write data from a buffer into a file.  Return SQLITE_OK on success
33744 ** or some other error code on failure.
33745 */
33746 static int winWrite(
33747   sqlite3_file *id,               /* File to write into */
33748   const void *pBuf,               /* The bytes to be written */
33749   int amt,                        /* Number of bytes to write */
33750   sqlite3_int64 offset            /* Offset into the file to begin writing at */
33751 ){
33752   int rc = 0;                     /* True if error has occurred, else false */
33753   winFile *pFile = (winFile*)id;  /* File handle */
33754   int nRetry = 0;                 /* Number of retries */
33755 
33756   assert( amt>0 );
33757   assert( pFile );
33758   SimulateIOError(return SQLITE_IOERR_WRITE);
33759   SimulateDiskfullError(return SQLITE_FULL);
33760 
33761   OSTRACE(("WRITE file=%p, buffer=%p, amount=%d, offset=%lld, lock=%d\n",
33762            pFile->h, pBuf, amt, offset, pFile->locktype));
33763 
33764 #if SQLITE_MAX_MMAP_SIZE>0
33765   /* Deal with as much of this write request as possible by transfering
33766   ** data from the memory mapping using memcpy().  */
33767   if( offset<pFile->mmapSize ){
33768     if( offset+amt <= pFile->mmapSize ){
33769       memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, amt);
33770       OSTRACE(("WRITE-MMAP file=%p, rc=SQLITE_OK\n", pFile->h));
33771       return SQLITE_OK;
33772     }else{
33773       int nCopy = (int)(pFile->mmapSize - offset);
33774       memcpy(&((u8 *)(pFile->pMapRegion))[offset], pBuf, nCopy);
33775       pBuf = &((u8 *)pBuf)[nCopy];
33776       amt -= nCopy;
33777       offset += nCopy;
33778     }
33779   }
33780 #endif
33781 
33782 #if SQLITE_OS_WINCE
33783   rc = winSeekFile(pFile, offset);
33784   if( rc==0 ){
33785 #else
33786   {
33787 #endif
33788 #if !SQLITE_OS_WINCE
33789     OVERLAPPED overlapped;        /* The offset for WriteFile. */
33790 #endif
33791     u8 *aRem = (u8 *)pBuf;        /* Data yet to be written */
33792     int nRem = amt;               /* Number of bytes yet to be written */
33793     DWORD nWrite;                 /* Bytes written by each WriteFile() call */
33794     DWORD lastErrno = NO_ERROR;   /* Value returned by GetLastError() */
33795 
33796 #if !SQLITE_OS_WINCE
33797     memset(&overlapped, 0, sizeof(OVERLAPPED));
33798     overlapped.Offset = (LONG)(offset & 0xffffffff);
33799     overlapped.OffsetHigh = (LONG)((offset>>32) & 0x7fffffff);
33800 #endif
33801 
33802     while( nRem>0 ){
33803 #if SQLITE_OS_WINCE
33804       if( !osWriteFile(pFile->h, aRem, nRem, &nWrite, 0) ){
33805 #else
33806       if( !osWriteFile(pFile->h, aRem, nRem, &nWrite, &overlapped) ){
33807 #endif
33808         if( winRetryIoerr(&nRetry, &lastErrno) ) continue;
33809         break;
33810       }
33811       assert( nWrite==0 || nWrite<=(DWORD)nRem );
33812       if( nWrite==0 || nWrite>(DWORD)nRem ){
33813         lastErrno = osGetLastError();
33814         break;
33815       }
33816 #if !SQLITE_OS_WINCE
33817       offset += nWrite;
33818       overlapped.Offset = (LONG)(offset & 0xffffffff);
33819       overlapped.OffsetHigh = (LONG)((offset>>32) & 0x7fffffff);
33820 #endif
33821       aRem += nWrite;
33822       nRem -= nWrite;
33823     }
33824     if( nRem>0 ){
33825       pFile->lastErrno = lastErrno;
33826       rc = 1;
33827     }
33828   }
33829 
33830   if( rc ){
33831     if(   ( pFile->lastErrno==ERROR_HANDLE_DISK_FULL )
33832        || ( pFile->lastErrno==ERROR_DISK_FULL )){
33833       OSTRACE(("WRITE file=%p, rc=SQLITE_FULL\n", pFile->h));
33834       return winLogError(SQLITE_FULL, pFile->lastErrno,
33835                          "winWrite1", pFile->zPath);
33836     }
33837     OSTRACE(("WRITE file=%p, rc=SQLITE_IOERR_WRITE\n", pFile->h));
33838     return winLogError(SQLITE_IOERR_WRITE, pFile->lastErrno,
33839                        "winWrite2", pFile->zPath);
33840   }else{
33841     winLogIoerr(nRetry);
33842   }
33843   OSTRACE(("WRITE file=%p, rc=SQLITE_OK\n", pFile->h));
33844   return SQLITE_OK;
33845 }
33846 
33847 /*
33848 ** Truncate an open file to a specified size
33849 */
33850 static int winTruncate(sqlite3_file *id, sqlite3_int64 nByte){
33851   winFile *pFile = (winFile*)id;  /* File handle object */
33852   int rc = SQLITE_OK;             /* Return code for this function */
33853   DWORD lastErrno;
33854 
33855   assert( pFile );
33856   SimulateIOError(return SQLITE_IOERR_TRUNCATE);
33857   OSTRACE(("TRUNCATE file=%p, size=%lld, lock=%d\n",
33858            pFile->h, nByte, pFile->locktype));
33859 
33860   /* If the user has configured a chunk-size for this file, truncate the
33861   ** file so that it consists of an integer number of chunks (i.e. the
33862   ** actual file size after the operation may be larger than the requested
33863   ** size).
33864   */
33865   if( pFile->szChunk>0 ){
33866     nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk;
33867   }
33868 
33869   /* SetEndOfFile() returns non-zero when successful, or zero when it fails. */
33870   if( winSeekFile(pFile, nByte) ){
33871     rc = winLogError(SQLITE_IOERR_TRUNCATE, pFile->lastErrno,
33872                      "winTruncate1", pFile->zPath);
33873   }else if( 0==osSetEndOfFile(pFile->h) &&
33874             ((lastErrno = osGetLastError())!=ERROR_USER_MAPPED_FILE) ){
33875     pFile->lastErrno = lastErrno;
33876     rc = winLogError(SQLITE_IOERR_TRUNCATE, pFile->lastErrno,
33877                      "winTruncate2", pFile->zPath);
33878   }
33879 
33880 #if SQLITE_MAX_MMAP_SIZE>0
33881   /* If the file was truncated to a size smaller than the currently
33882   ** mapped region, reduce the effective mapping size as well. SQLite will
33883   ** use read() and write() to access data beyond this point from now on.
33884   */
33885   if( pFile->pMapRegion && nByte<pFile->mmapSize ){
33886     pFile->mmapSize = nByte;
33887   }
33888 #endif
33889 
33890   OSTRACE(("TRUNCATE file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc)));
33891   return rc;
33892 }
33893 
33894 #ifdef SQLITE_TEST
33895 /*
33896 ** Count the number of fullsyncs and normal syncs.  This is used to test
33897 ** that syncs and fullsyncs are occuring at the right times.
33898 */
33899 SQLITE_API int sqlite3_sync_count = 0;
33900 SQLITE_API int sqlite3_fullsync_count = 0;
33901 #endif
33902 
33903 /*
33904 ** Make sure all writes to a particular file are committed to disk.
33905 */
33906 static int winSync(sqlite3_file *id, int flags){
33907 #ifndef SQLITE_NO_SYNC
33908   /*
33909   ** Used only when SQLITE_NO_SYNC is not defined.
33910    */
33911   BOOL rc;
33912 #endif
33913 #if !defined(NDEBUG) || !defined(SQLITE_NO_SYNC) || \
33914     (defined(SQLITE_TEST) && defined(SQLITE_DEBUG))
33915   /*
33916   ** Used when SQLITE_NO_SYNC is not defined and by the assert() and/or
33917   ** OSTRACE() macros.
33918    */
33919   winFile *pFile = (winFile*)id;
33920 #else
33921   UNUSED_PARAMETER(id);
33922 #endif
33923 
33924   assert( pFile );
33925   /* Check that one of SQLITE_SYNC_NORMAL or FULL was passed */
33926   assert((flags&0x0F)==SQLITE_SYNC_NORMAL
33927       || (flags&0x0F)==SQLITE_SYNC_FULL
33928   );
33929 
33930   /* Unix cannot, but some systems may return SQLITE_FULL from here. This
33931   ** line is to test that doing so does not cause any problems.
33932   */
33933   SimulateDiskfullError( return SQLITE_FULL );
33934 
33935   OSTRACE(("SYNC file=%p, flags=%x, lock=%d\n",
33936            pFile->h, flags, pFile->locktype));
33937 
33938 #ifndef SQLITE_TEST
33939   UNUSED_PARAMETER(flags);
33940 #else
33941   if( (flags&0x0F)==SQLITE_SYNC_FULL ){
33942     sqlite3_fullsync_count++;
33943   }
33944   sqlite3_sync_count++;
33945 #endif
33946 
33947   /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a
33948   ** no-op
33949   */
33950 #ifdef SQLITE_NO_SYNC
33951   OSTRACE(("SYNC-NOP file=%p, rc=SQLITE_OK\n", pFile->h));
33952   return SQLITE_OK;
33953 #else
33954   rc = osFlushFileBuffers(pFile->h);
33955   SimulateIOError( rc=FALSE );
33956   if( rc ){
33957     OSTRACE(("SYNC file=%p, rc=SQLITE_OK\n", pFile->h));
33958     return SQLITE_OK;
33959   }else{
33960     pFile->lastErrno = osGetLastError();
33961     OSTRACE(("SYNC file=%p, rc=SQLITE_IOERR_FSYNC\n", pFile->h));
33962     return winLogError(SQLITE_IOERR_FSYNC, pFile->lastErrno,
33963                        "winSync", pFile->zPath);
33964   }
33965 #endif
33966 }
33967 
33968 /*
33969 ** Determine the current size of a file in bytes
33970 */
33971 static int winFileSize(sqlite3_file *id, sqlite3_int64 *pSize){
33972   winFile *pFile = (winFile*)id;
33973   int rc = SQLITE_OK;
33974 
33975   assert( id!=0 );
33976   assert( pSize!=0 );
33977   SimulateIOError(return SQLITE_IOERR_FSTAT);
33978   OSTRACE(("SIZE file=%p, pSize=%p\n", pFile->h, pSize));
33979 
33980 #if SQLITE_OS_WINRT
33981   {
33982     FILE_STANDARD_INFO info;
33983     if( osGetFileInformationByHandleEx(pFile->h, FileStandardInfo,
33984                                      &info, sizeof(info)) ){
33985       *pSize = info.EndOfFile.QuadPart;
33986     }else{
33987       pFile->lastErrno = osGetLastError();
33988       rc = winLogError(SQLITE_IOERR_FSTAT, pFile->lastErrno,
33989                        "winFileSize", pFile->zPath);
33990     }
33991   }
33992 #else
33993   {
33994     DWORD upperBits;
33995     DWORD lowerBits;
33996     DWORD lastErrno;
33997 
33998     lowerBits = osGetFileSize(pFile->h, &upperBits);
33999     *pSize = (((sqlite3_int64)upperBits)<<32) + lowerBits;
34000     if(   (lowerBits == INVALID_FILE_SIZE)
34001        && ((lastErrno = osGetLastError())!=NO_ERROR) ){
34002       pFile->lastErrno = lastErrno;
34003       rc = winLogError(SQLITE_IOERR_FSTAT, pFile->lastErrno,
34004                        "winFileSize", pFile->zPath);
34005     }
34006   }
34007 #endif
34008   OSTRACE(("SIZE file=%p, pSize=%p, *pSize=%lld, rc=%s\n",
34009            pFile->h, pSize, *pSize, sqlite3ErrName(rc)));
34010   return rc;
34011 }
34012 
34013 /*
34014 ** LOCKFILE_FAIL_IMMEDIATELY is undefined on some Windows systems.
34015 */
34016 #ifndef LOCKFILE_FAIL_IMMEDIATELY
34017 # define LOCKFILE_FAIL_IMMEDIATELY 1
34018 #endif
34019 
34020 #ifndef LOCKFILE_EXCLUSIVE_LOCK
34021 # define LOCKFILE_EXCLUSIVE_LOCK 2
34022 #endif
34023 
34024 /*
34025 ** Historically, SQLite has used both the LockFile and LockFileEx functions.
34026 ** When the LockFile function was used, it was always expected to fail
34027 ** immediately if the lock could not be obtained.  Also, it always expected to
34028 ** obtain an exclusive lock.  These flags are used with the LockFileEx function
34029 ** and reflect those expectations; therefore, they should not be changed.
34030 */
34031 #ifndef SQLITE_LOCKFILE_FLAGS
34032 # define SQLITE_LOCKFILE_FLAGS   (LOCKFILE_FAIL_IMMEDIATELY | \
34033                                   LOCKFILE_EXCLUSIVE_LOCK)
34034 #endif
34035 
34036 /*
34037 ** Currently, SQLite never calls the LockFileEx function without wanting the
34038 ** call to fail immediately if the lock cannot be obtained.
34039 */
34040 #ifndef SQLITE_LOCKFILEEX_FLAGS
34041 # define SQLITE_LOCKFILEEX_FLAGS (LOCKFILE_FAIL_IMMEDIATELY)
34042 #endif
34043 
34044 /*
34045 ** Acquire a reader lock.
34046 ** Different API routines are called depending on whether or not this
34047 ** is Win9x or WinNT.
34048 */
34049 static int winGetReadLock(winFile *pFile){
34050   int res;
34051   OSTRACE(("READ-LOCK file=%p, lock=%d\n", pFile->h, pFile->locktype));
34052   if( osIsNT() ){
34053 #if SQLITE_OS_WINCE
34054     /*
34055     ** NOTE: Windows CE is handled differently here due its lack of the Win32
34056     **       API LockFileEx.
34057     */
34058     res = winceLockFile(&pFile->h, SHARED_FIRST, 0, 1, 0);
34059 #else
34060     res = winLockFile(&pFile->h, SQLITE_LOCKFILEEX_FLAGS, SHARED_FIRST, 0,
34061                       SHARED_SIZE, 0);
34062 #endif
34063   }
34064 #ifdef SQLITE_WIN32_HAS_ANSI
34065   else{
34066     int lk;
34067     sqlite3_randomness(sizeof(lk), &lk);
34068     pFile->sharedLockByte = (short)((lk & 0x7fffffff)%(SHARED_SIZE - 1));
34069     res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS,
34070                       SHARED_FIRST+pFile->sharedLockByte, 0, 1, 0);
34071   }
34072 #endif
34073   if( res == 0 ){
34074     pFile->lastErrno = osGetLastError();
34075     /* No need to log a failure to lock */
34076   }
34077   OSTRACE(("READ-LOCK file=%p, rc=%s\n", pFile->h, sqlite3ErrName(res)));
34078   return res;
34079 }
34080 
34081 /*
34082 ** Undo a readlock
34083 */
34084 static int winUnlockReadLock(winFile *pFile){
34085   int res;
34086   DWORD lastErrno;
34087   OSTRACE(("READ-UNLOCK file=%p, lock=%d\n", pFile->h, pFile->locktype));
34088   if( osIsNT() ){
34089     res = winUnlockFile(&pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
34090   }
34091 #ifdef SQLITE_WIN32_HAS_ANSI
34092   else{
34093     res = winUnlockFile(&pFile->h, SHARED_FIRST+pFile->sharedLockByte, 0, 1, 0);
34094   }
34095 #endif
34096   if( res==0 && ((lastErrno = osGetLastError())!=ERROR_NOT_LOCKED) ){
34097     pFile->lastErrno = lastErrno;
34098     winLogError(SQLITE_IOERR_UNLOCK, pFile->lastErrno,
34099                 "winUnlockReadLock", pFile->zPath);
34100   }
34101   OSTRACE(("READ-UNLOCK file=%p, rc=%s\n", pFile->h, sqlite3ErrName(res)));
34102   return res;
34103 }
34104 
34105 /*
34106 ** Lock the file with the lock specified by parameter locktype - one
34107 ** of the following:
34108 **
34109 **     (1) SHARED_LOCK
34110 **     (2) RESERVED_LOCK
34111 **     (3) PENDING_LOCK
34112 **     (4) EXCLUSIVE_LOCK
34113 **
34114 ** Sometimes when requesting one lock state, additional lock states
34115 ** are inserted in between.  The locking might fail on one of the later
34116 ** transitions leaving the lock state different from what it started but
34117 ** still short of its goal.  The following chart shows the allowed
34118 ** transitions and the inserted intermediate states:
34119 **
34120 **    UNLOCKED -> SHARED
34121 **    SHARED -> RESERVED
34122 **    SHARED -> (PENDING) -> EXCLUSIVE
34123 **    RESERVED -> (PENDING) -> EXCLUSIVE
34124 **    PENDING -> EXCLUSIVE
34125 **
34126 ** This routine will only increase a lock.  The winUnlock() routine
34127 ** erases all locks at once and returns us immediately to locking level 0.
34128 ** It is not possible to lower the locking level one step at a time.  You
34129 ** must go straight to locking level 0.
34130 */
34131 static int winLock(sqlite3_file *id, int locktype){
34132   int rc = SQLITE_OK;    /* Return code from subroutines */
34133   int res = 1;           /* Result of a Windows lock call */
34134   int newLocktype;       /* Set pFile->locktype to this value before exiting */
34135   int gotPendingLock = 0;/* True if we acquired a PENDING lock this time */
34136   winFile *pFile = (winFile*)id;
34137   DWORD lastErrno = NO_ERROR;
34138 
34139   assert( id!=0 );
34140   OSTRACE(("LOCK file=%p, oldLock=%d(%d), newLock=%d\n",
34141            pFile->h, pFile->locktype, pFile->sharedLockByte, locktype));
34142 
34143   /* If there is already a lock of this type or more restrictive on the
34144   ** OsFile, do nothing. Don't use the end_lock: exit path, as
34145   ** sqlite3OsEnterMutex() hasn't been called yet.
34146   */
34147   if( pFile->locktype>=locktype ){
34148     OSTRACE(("LOCK-HELD file=%p, rc=SQLITE_OK\n", pFile->h));
34149     return SQLITE_OK;
34150   }
34151 
34152   /* Make sure the locking sequence is correct
34153   */
34154   assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK );
34155   assert( locktype!=PENDING_LOCK );
34156   assert( locktype!=RESERVED_LOCK || pFile->locktype==SHARED_LOCK );
34157 
34158   /* Lock the PENDING_LOCK byte if we need to acquire a PENDING lock or
34159   ** a SHARED lock.  If we are acquiring a SHARED lock, the acquisition of
34160   ** the PENDING_LOCK byte is temporary.
34161   */
34162   newLocktype = pFile->locktype;
34163   if(   (pFile->locktype==NO_LOCK)
34164      || (   (locktype==EXCLUSIVE_LOCK)
34165          && (pFile->locktype==RESERVED_LOCK))
34166   ){
34167     int cnt = 3;
34168     while( cnt-->0 && (res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS,
34169                                          PENDING_BYTE, 0, 1, 0))==0 ){
34170       /* Try 3 times to get the pending lock.  This is needed to work
34171       ** around problems caused by indexing and/or anti-virus software on
34172       ** Windows systems.
34173       ** If you are using this code as a model for alternative VFSes, do not
34174       ** copy this retry logic.  It is a hack intended for Windows only.
34175       */
34176       OSTRACE(("LOCK-PENDING-FAIL file=%p, count=%d, rc=%s\n",
34177                pFile->h, cnt, sqlite3ErrName(res)));
34178       if( cnt ) sqlite3_win32_sleep(1);
34179     }
34180     gotPendingLock = res;
34181     if( !res ){
34182       lastErrno = osGetLastError();
34183     }
34184   }
34185 
34186   /* Acquire a shared lock
34187   */
34188   if( locktype==SHARED_LOCK && res ){
34189     assert( pFile->locktype==NO_LOCK );
34190     res = winGetReadLock(pFile);
34191     if( res ){
34192       newLocktype = SHARED_LOCK;
34193     }else{
34194       lastErrno = osGetLastError();
34195     }
34196   }
34197 
34198   /* Acquire a RESERVED lock
34199   */
34200   if( locktype==RESERVED_LOCK && res ){
34201     assert( pFile->locktype==SHARED_LOCK );
34202     res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS, RESERVED_BYTE, 0, 1, 0);
34203     if( res ){
34204       newLocktype = RESERVED_LOCK;
34205     }else{
34206       lastErrno = osGetLastError();
34207     }
34208   }
34209 
34210   /* Acquire a PENDING lock
34211   */
34212   if( locktype==EXCLUSIVE_LOCK && res ){
34213     newLocktype = PENDING_LOCK;
34214     gotPendingLock = 0;
34215   }
34216 
34217   /* Acquire an EXCLUSIVE lock
34218   */
34219   if( locktype==EXCLUSIVE_LOCK && res ){
34220     assert( pFile->locktype>=SHARED_LOCK );
34221     res = winUnlockReadLock(pFile);
34222     res = winLockFile(&pFile->h, SQLITE_LOCKFILE_FLAGS, SHARED_FIRST, 0,
34223                       SHARED_SIZE, 0);
34224     if( res ){
34225       newLocktype = EXCLUSIVE_LOCK;
34226     }else{
34227       lastErrno = osGetLastError();
34228       winGetReadLock(pFile);
34229     }
34230   }
34231 
34232   /* If we are holding a PENDING lock that ought to be released, then
34233   ** release it now.
34234   */
34235   if( gotPendingLock && locktype==SHARED_LOCK ){
34236     winUnlockFile(&pFile->h, PENDING_BYTE, 0, 1, 0);
34237   }
34238 
34239   /* Update the state of the lock has held in the file descriptor then
34240   ** return the appropriate result code.
34241   */
34242   if( res ){
34243     rc = SQLITE_OK;
34244   }else{
34245     pFile->lastErrno = lastErrno;
34246     rc = SQLITE_BUSY;
34247     OSTRACE(("LOCK-FAIL file=%p, wanted=%d, got=%d\n",
34248              pFile->h, locktype, newLocktype));
34249   }
34250   pFile->locktype = (u8)newLocktype;
34251   OSTRACE(("LOCK file=%p, lock=%d, rc=%s\n",
34252            pFile->h, pFile->locktype, sqlite3ErrName(rc)));
34253   return rc;
34254 }
34255 
34256 /*
34257 ** This routine checks if there is a RESERVED lock held on the specified
34258 ** file by this or any other process. If such a lock is held, return
34259 ** non-zero, otherwise zero.
34260 */
34261 static int winCheckReservedLock(sqlite3_file *id, int *pResOut){
34262   int rc;
34263   winFile *pFile = (winFile*)id;
34264 
34265   SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
34266   OSTRACE(("TEST-WR-LOCK file=%p, pResOut=%p\n", pFile->h, pResOut));
34267 
34268   assert( id!=0 );
34269   if( pFile->locktype>=RESERVED_LOCK ){
34270     rc = 1;
34271     OSTRACE(("TEST-WR-LOCK file=%p, rc=%d (local)\n", pFile->h, rc));
34272   }else{
34273     rc = winLockFile(&pFile->h, SQLITE_LOCKFILEEX_FLAGS,RESERVED_BYTE, 0, 1, 0);
34274     if( rc ){
34275       winUnlockFile(&pFile->h, RESERVED_BYTE, 0, 1, 0);
34276     }
34277     rc = !rc;
34278     OSTRACE(("TEST-WR-LOCK file=%p, rc=%d (remote)\n", pFile->h, rc));
34279   }
34280   *pResOut = rc;
34281   OSTRACE(("TEST-WR-LOCK file=%p, pResOut=%p, *pResOut=%d, rc=SQLITE_OK\n",
34282            pFile->h, pResOut, *pResOut));
34283   return SQLITE_OK;
34284 }
34285 
34286 /*
34287 ** Lower the locking level on file descriptor id to locktype.  locktype
34288 ** must be either NO_LOCK or SHARED_LOCK.
34289 **
34290 ** If the locking level of the file descriptor is already at or below
34291 ** the requested locking level, this routine is a no-op.
34292 **
34293 ** It is not possible for this routine to fail if the second argument
34294 ** is NO_LOCK.  If the second argument is SHARED_LOCK then this routine
34295 ** might return SQLITE_IOERR;
34296 */
34297 static int winUnlock(sqlite3_file *id, int locktype){
34298   int type;
34299   winFile *pFile = (winFile*)id;
34300   int rc = SQLITE_OK;
34301   assert( pFile!=0 );
34302   assert( locktype<=SHARED_LOCK );
34303   OSTRACE(("UNLOCK file=%p, oldLock=%d(%d), newLock=%d\n",
34304            pFile->h, pFile->locktype, pFile->sharedLockByte, locktype));
34305   type = pFile->locktype;
34306   if( type>=EXCLUSIVE_LOCK ){
34307     winUnlockFile(&pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
34308     if( locktype==SHARED_LOCK && !winGetReadLock(pFile) ){
34309       /* This should never happen.  We should always be able to
34310       ** reacquire the read lock */
34311       rc = winLogError(SQLITE_IOERR_UNLOCK, osGetLastError(),
34312                        "winUnlock", pFile->zPath);
34313     }
34314   }
34315   if( type>=RESERVED_LOCK ){
34316     winUnlockFile(&pFile->h, RESERVED_BYTE, 0, 1, 0);
34317   }
34318   if( locktype==NO_LOCK && type>=SHARED_LOCK ){
34319     winUnlockReadLock(pFile);
34320   }
34321   if( type>=PENDING_LOCK ){
34322     winUnlockFile(&pFile->h, PENDING_BYTE, 0, 1, 0);
34323   }
34324   pFile->locktype = (u8)locktype;
34325   OSTRACE(("UNLOCK file=%p, lock=%d, rc=%s\n",
34326            pFile->h, pFile->locktype, sqlite3ErrName(rc)));
34327   return rc;
34328 }
34329 
34330 /*
34331 ** If *pArg is inititially negative then this is a query.  Set *pArg to
34332 ** 1 or 0 depending on whether or not bit mask of pFile->ctrlFlags is set.
34333 **
34334 ** If *pArg is 0 or 1, then clear or set the mask bit of pFile->ctrlFlags.
34335 */
34336 static void winModeBit(winFile *pFile, unsigned char mask, int *pArg){
34337   if( *pArg<0 ){
34338     *pArg = (pFile->ctrlFlags & mask)!=0;
34339   }else if( (*pArg)==0 ){
34340     pFile->ctrlFlags &= ~mask;
34341   }else{
34342     pFile->ctrlFlags |= mask;
34343   }
34344 }
34345 
34346 /* Forward references to VFS helper methods used for temporary files */
34347 static int winGetTempname(sqlite3_vfs *, char **);
34348 static int winIsDir(const void *);
34349 static BOOL winIsDriveLetterAndColon(const char *);
34350 
34351 /*
34352 ** Control and query of the open file handle.
34353 */
34354 static int winFileControl(sqlite3_file *id, int op, void *pArg){
34355   winFile *pFile = (winFile*)id;
34356   OSTRACE(("FCNTL file=%p, op=%d, pArg=%p\n", pFile->h, op, pArg));
34357   switch( op ){
34358     case SQLITE_FCNTL_LOCKSTATE: {
34359       *(int*)pArg = pFile->locktype;
34360       OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
34361       return SQLITE_OK;
34362     }
34363     case SQLITE_LAST_ERRNO: {
34364       *(int*)pArg = (int)pFile->lastErrno;
34365       OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
34366       return SQLITE_OK;
34367     }
34368     case SQLITE_FCNTL_CHUNK_SIZE: {
34369       pFile->szChunk = *(int *)pArg;
34370       OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
34371       return SQLITE_OK;
34372     }
34373     case SQLITE_FCNTL_SIZE_HINT: {
34374       if( pFile->szChunk>0 ){
34375         sqlite3_int64 oldSz;
34376         int rc = winFileSize(id, &oldSz);
34377         if( rc==SQLITE_OK ){
34378           sqlite3_int64 newSz = *(sqlite3_int64*)pArg;
34379           if( newSz>oldSz ){
34380             SimulateIOErrorBenign(1);
34381             rc = winTruncate(id, newSz);
34382             SimulateIOErrorBenign(0);
34383           }
34384         }
34385         OSTRACE(("FCNTL file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc)));
34386         return rc;
34387       }
34388       OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
34389       return SQLITE_OK;
34390     }
34391     case SQLITE_FCNTL_PERSIST_WAL: {
34392       winModeBit(pFile, WINFILE_PERSIST_WAL, (int*)pArg);
34393       OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
34394       return SQLITE_OK;
34395     }
34396     case SQLITE_FCNTL_POWERSAFE_OVERWRITE: {
34397       winModeBit(pFile, WINFILE_PSOW, (int*)pArg);
34398       OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
34399       return SQLITE_OK;
34400     }
34401     case SQLITE_FCNTL_VFSNAME: {
34402       *(char**)pArg = sqlite3_mprintf("%s", pFile->pVfs->zName);
34403       OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
34404       return SQLITE_OK;
34405     }
34406     case SQLITE_FCNTL_WIN32_AV_RETRY: {
34407       int *a = (int*)pArg;
34408       if( a[0]>0 ){
34409         winIoerrRetry = a[0];
34410       }else{
34411         a[0] = winIoerrRetry;
34412       }
34413       if( a[1]>0 ){
34414         winIoerrRetryDelay = a[1];
34415       }else{
34416         a[1] = winIoerrRetryDelay;
34417       }
34418       OSTRACE(("FCNTL file=%p, rc=SQLITE_OK\n", pFile->h));
34419       return SQLITE_OK;
34420     }
34421     case SQLITE_FCNTL_TEMPFILENAME: {
34422       char *zTFile = 0;
34423       int rc = winGetTempname(pFile->pVfs, &zTFile);
34424       if( rc==SQLITE_OK ){
34425         *(char**)pArg = zTFile;
34426       }
34427       OSTRACE(("FCNTL file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc)));
34428       return rc;
34429     }
34430 #if SQLITE_MAX_MMAP_SIZE>0
34431     case SQLITE_FCNTL_MMAP_SIZE: {
34432       i64 newLimit = *(i64*)pArg;
34433       int rc = SQLITE_OK;
34434       if( newLimit>sqlite3GlobalConfig.mxMmap ){
34435         newLimit = sqlite3GlobalConfig.mxMmap;
34436       }
34437       *(i64*)pArg = pFile->mmapSizeMax;
34438       if( newLimit>=0 && newLimit!=pFile->mmapSizeMax && pFile->nFetchOut==0 ){
34439         pFile->mmapSizeMax = newLimit;
34440         if( pFile->mmapSize>0 ){
34441           winUnmapfile(pFile);
34442           rc = winMapfile(pFile, -1);
34443         }
34444       }
34445       OSTRACE(("FCNTL file=%p, rc=%s\n", pFile->h, sqlite3ErrName(rc)));
34446       return rc;
34447     }
34448 #endif
34449   }
34450   OSTRACE(("FCNTL file=%p, rc=SQLITE_NOTFOUND\n", pFile->h));
34451   return SQLITE_NOTFOUND;
34452 }
34453 
34454 /*
34455 ** Return the sector size in bytes of the underlying block device for
34456 ** the specified file. This is almost always 512 bytes, but may be
34457 ** larger for some devices.
34458 **
34459 ** SQLite code assumes this function cannot fail. It also assumes that
34460 ** if two files are created in the same file-system directory (i.e.
34461 ** a database and its journal file) that the sector size will be the
34462 ** same for both.
34463 */
34464 static int winSectorSize(sqlite3_file *id){
34465   (void)id;
34466   return SQLITE_DEFAULT_SECTOR_SIZE;
34467 }
34468 
34469 /*
34470 ** Return a vector of device characteristics.
34471 */
34472 static int winDeviceCharacteristics(sqlite3_file *id){
34473   winFile *p = (winFile*)id;
34474   return SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN |
34475          ((p->ctrlFlags & WINFILE_PSOW)?SQLITE_IOCAP_POWERSAFE_OVERWRITE:0);
34476 }
34477 
34478 /*
34479 ** Windows will only let you create file view mappings
34480 ** on allocation size granularity boundaries.
34481 ** During sqlite3_os_init() we do a GetSystemInfo()
34482 ** to get the granularity size.
34483 */
34484 SYSTEM_INFO winSysInfo;
34485 
34486 #ifndef SQLITE_OMIT_WAL
34487 
34488 /*
34489 ** Helper functions to obtain and relinquish the global mutex. The
34490 ** global mutex is used to protect the winLockInfo objects used by
34491 ** this file, all of which may be shared by multiple threads.
34492 **
34493 ** Function winShmMutexHeld() is used to assert() that the global mutex
34494 ** is held when required. This function is only used as part of assert()
34495 ** statements. e.g.
34496 **
34497 **   winShmEnterMutex()
34498 **     assert( winShmMutexHeld() );
34499 **   winShmLeaveMutex()
34500 */
34501 static void winShmEnterMutex(void){
34502   sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
34503 }
34504 static void winShmLeaveMutex(void){
34505   sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
34506 }
34507 #ifndef NDEBUG
34508 static int winShmMutexHeld(void) {
34509   return sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
34510 }
34511 #endif
34512 
34513 /*
34514 ** Object used to represent a single file opened and mmapped to provide
34515 ** shared memory.  When multiple threads all reference the same
34516 ** log-summary, each thread has its own winFile object, but they all
34517 ** point to a single instance of this object.  In other words, each
34518 ** log-summary is opened only once per process.
34519 **
34520 ** winShmMutexHeld() must be true when creating or destroying
34521 ** this object or while reading or writing the following fields:
34522 **
34523 **      nRef
34524 **      pNext
34525 **
34526 ** The following fields are read-only after the object is created:
34527 **
34528 **      fid
34529 **      zFilename
34530 **
34531 ** Either winShmNode.mutex must be held or winShmNode.nRef==0 and
34532 ** winShmMutexHeld() is true when reading or writing any other field
34533 ** in this structure.
34534 **
34535 */
34536 struct winShmNode {
34537   sqlite3_mutex *mutex;      /* Mutex to access this object */
34538   char *zFilename;           /* Name of the file */
34539   winFile hFile;             /* File handle from winOpen */
34540 
34541   int szRegion;              /* Size of shared-memory regions */
34542   int nRegion;               /* Size of array apRegion */
34543   struct ShmRegion {
34544     HANDLE hMap;             /* File handle from CreateFileMapping */
34545     void *pMap;
34546   } *aRegion;
34547   DWORD lastErrno;           /* The Windows errno from the last I/O error */
34548 
34549   int nRef;                  /* Number of winShm objects pointing to this */
34550   winShm *pFirst;            /* All winShm objects pointing to this */
34551   winShmNode *pNext;         /* Next in list of all winShmNode objects */
34552 #ifdef SQLITE_DEBUG
34553   u8 nextShmId;              /* Next available winShm.id value */
34554 #endif
34555 };
34556 
34557 /*
34558 ** A global array of all winShmNode objects.
34559 **
34560 ** The winShmMutexHeld() must be true while reading or writing this list.
34561 */
34562 static winShmNode *winShmNodeList = 0;
34563 
34564 /*
34565 ** Structure used internally by this VFS to record the state of an
34566 ** open shared memory connection.
34567 **
34568 ** The following fields are initialized when this object is created and
34569 ** are read-only thereafter:
34570 **
34571 **    winShm.pShmNode
34572 **    winShm.id
34573 **
34574 ** All other fields are read/write.  The winShm.pShmNode->mutex must be held
34575 ** while accessing any read/write fields.
34576 */
34577 struct winShm {
34578   winShmNode *pShmNode;      /* The underlying winShmNode object */
34579   winShm *pNext;             /* Next winShm with the same winShmNode */
34580   u8 hasMutex;               /* True if holding the winShmNode mutex */
34581   u16 sharedMask;            /* Mask of shared locks held */
34582   u16 exclMask;              /* Mask of exclusive locks held */
34583 #ifdef SQLITE_DEBUG
34584   u8 id;                     /* Id of this connection with its winShmNode */
34585 #endif
34586 };
34587 
34588 /*
34589 ** Constants used for locking
34590 */
34591 #define WIN_SHM_BASE   ((22+SQLITE_SHM_NLOCK)*4)        /* first lock byte */
34592 #define WIN_SHM_DMS    (WIN_SHM_BASE+SQLITE_SHM_NLOCK)  /* deadman switch */
34593 
34594 /*
34595 ** Apply advisory locks for all n bytes beginning at ofst.
34596 */
34597 #define _SHM_UNLCK  1
34598 #define _SHM_RDLCK  2
34599 #define _SHM_WRLCK  3
34600 static int winShmSystemLock(
34601   winShmNode *pFile,    /* Apply locks to this open shared-memory segment */
34602   int lockType,         /* _SHM_UNLCK, _SHM_RDLCK, or _SHM_WRLCK */
34603   int ofst,             /* Offset to first byte to be locked/unlocked */
34604   int nByte             /* Number of bytes to lock or unlock */
34605 ){
34606   int rc = 0;           /* Result code form Lock/UnlockFileEx() */
34607 
34608   /* Access to the winShmNode object is serialized by the caller */
34609   assert( sqlite3_mutex_held(pFile->mutex) || pFile->nRef==0 );
34610 
34611   OSTRACE(("SHM-LOCK file=%p, lock=%d, offset=%d, size=%d\n",
34612            pFile->hFile.h, lockType, ofst, nByte));
34613 
34614   /* Release/Acquire the system-level lock */
34615   if( lockType==_SHM_UNLCK ){
34616     rc = winUnlockFile(&pFile->hFile.h, ofst, 0, nByte, 0);
34617   }else{
34618     /* Initialize the locking parameters */
34619     DWORD dwFlags = LOCKFILE_FAIL_IMMEDIATELY;
34620     if( lockType == _SHM_WRLCK ) dwFlags |= LOCKFILE_EXCLUSIVE_LOCK;
34621     rc = winLockFile(&pFile->hFile.h, dwFlags, ofst, 0, nByte, 0);
34622   }
34623 
34624   if( rc!= 0 ){
34625     rc = SQLITE_OK;
34626   }else{
34627     pFile->lastErrno =  osGetLastError();
34628     rc = SQLITE_BUSY;
34629   }
34630 
34631   OSTRACE(("SHM-LOCK file=%p, func=%s, errno=%lu, rc=%s\n",
34632            pFile->hFile.h, (lockType == _SHM_UNLCK) ? "winUnlockFile" :
34633            "winLockFile", pFile->lastErrno, sqlite3ErrName(rc)));
34634 
34635   return rc;
34636 }
34637 
34638 /* Forward references to VFS methods */
34639 static int winOpen(sqlite3_vfs*,const char*,sqlite3_file*,int,int*);
34640 static int winDelete(sqlite3_vfs *,const char*,int);
34641 
34642 /*
34643 ** Purge the winShmNodeList list of all entries with winShmNode.nRef==0.
34644 **
34645 ** This is not a VFS shared-memory method; it is a utility function called
34646 ** by VFS shared-memory methods.
34647 */
34648 static void winShmPurge(sqlite3_vfs *pVfs, int deleteFlag){
34649   winShmNode **pp;
34650   winShmNode *p;
34651   assert( winShmMutexHeld() );
34652   OSTRACE(("SHM-PURGE pid=%lu, deleteFlag=%d\n",
34653            osGetCurrentProcessId(), deleteFlag));
34654   pp = &winShmNodeList;
34655   while( (p = *pp)!=0 ){
34656     if( p->nRef==0 ){
34657       int i;
34658       if( p->mutex ){ sqlite3_mutex_free(p->mutex); }
34659       for(i=0; i<p->nRegion; i++){
34660         BOOL bRc = osUnmapViewOfFile(p->aRegion[i].pMap);
34661         OSTRACE(("SHM-PURGE-UNMAP pid=%lu, region=%d, rc=%s\n",
34662                  osGetCurrentProcessId(), i, bRc ? "ok" : "failed"));
34663         UNUSED_VARIABLE_VALUE(bRc);
34664         bRc = osCloseHandle(p->aRegion[i].hMap);
34665         OSTRACE(("SHM-PURGE-CLOSE pid=%lu, region=%d, rc=%s\n",
34666                  osGetCurrentProcessId(), i, bRc ? "ok" : "failed"));
34667         UNUSED_VARIABLE_VALUE(bRc);
34668       }
34669       if( p->hFile.h!=NULL && p->hFile.h!=INVALID_HANDLE_VALUE ){
34670         SimulateIOErrorBenign(1);
34671         winClose((sqlite3_file *)&p->hFile);
34672         SimulateIOErrorBenign(0);
34673       }
34674       if( deleteFlag ){
34675         SimulateIOErrorBenign(1);
34676         sqlite3BeginBenignMalloc();
34677         winDelete(pVfs, p->zFilename, 0);
34678         sqlite3EndBenignMalloc();
34679         SimulateIOErrorBenign(0);
34680       }
34681       *pp = p->pNext;
34682       sqlite3_free(p->aRegion);
34683       sqlite3_free(p);
34684     }else{
34685       pp = &p->pNext;
34686     }
34687   }
34688 }
34689 
34690 /*
34691 ** Open the shared-memory area associated with database file pDbFd.
34692 **
34693 ** When opening a new shared-memory file, if no other instances of that
34694 ** file are currently open, in this process or in other processes, then
34695 ** the file must be truncated to zero length or have its header cleared.
34696 */
34697 static int winOpenSharedMemory(winFile *pDbFd){
34698   struct winShm *p;                  /* The connection to be opened */
34699   struct winShmNode *pShmNode = 0;   /* The underlying mmapped file */
34700   int rc;                            /* Result code */
34701   struct winShmNode *pNew;           /* Newly allocated winShmNode */
34702   int nName;                         /* Size of zName in bytes */
34703 
34704   assert( pDbFd->pShm==0 );    /* Not previously opened */
34705 
34706   /* Allocate space for the new sqlite3_shm object.  Also speculatively
34707   ** allocate space for a new winShmNode and filename.
34708   */
34709   p = sqlite3MallocZero( sizeof(*p) );
34710   if( p==0 ) return SQLITE_IOERR_NOMEM;
34711   nName = sqlite3Strlen30(pDbFd->zPath);
34712   pNew = sqlite3MallocZero( sizeof(*pShmNode) + nName + 17 );
34713   if( pNew==0 ){
34714     sqlite3_free(p);
34715     return SQLITE_IOERR_NOMEM;
34716   }
34717   pNew->zFilename = (char*)&pNew[1];
34718   sqlite3_snprintf(nName+15, pNew->zFilename, "%s-shm", pDbFd->zPath);
34719   sqlite3FileSuffix3(pDbFd->zPath, pNew->zFilename);
34720 
34721   /* Look to see if there is an existing winShmNode that can be used.
34722   ** If no matching winShmNode currently exists, create a new one.
34723   */
34724   winShmEnterMutex();
34725   for(pShmNode = winShmNodeList; pShmNode; pShmNode=pShmNode->pNext){
34726     /* TBD need to come up with better match here.  Perhaps
34727     ** use FILE_ID_BOTH_DIR_INFO Structure.
34728     */
34729     if( sqlite3StrICmp(pShmNode->zFilename, pNew->zFilename)==0 ) break;
34730   }
34731   if( pShmNode ){
34732     sqlite3_free(pNew);
34733   }else{
34734     pShmNode = pNew;
34735     pNew = 0;
34736     ((winFile*)(&pShmNode->hFile))->h = INVALID_HANDLE_VALUE;
34737     pShmNode->pNext = winShmNodeList;
34738     winShmNodeList = pShmNode;
34739 
34740     pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
34741     if( pShmNode->mutex==0 ){
34742       rc = SQLITE_IOERR_NOMEM;
34743       goto shm_open_err;
34744     }
34745 
34746     rc = winOpen(pDbFd->pVfs,
34747                  pShmNode->zFilename,             /* Name of the file (UTF-8) */
34748                  (sqlite3_file*)&pShmNode->hFile,  /* File handle here */
34749                  SQLITE_OPEN_WAL | SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE,
34750                  0);
34751     if( SQLITE_OK!=rc ){
34752       goto shm_open_err;
34753     }
34754 
34755     /* Check to see if another process is holding the dead-man switch.
34756     ** If not, truncate the file to zero length.
34757     */
34758     if( winShmSystemLock(pShmNode, _SHM_WRLCK, WIN_SHM_DMS, 1)==SQLITE_OK ){
34759       rc = winTruncate((sqlite3_file *)&pShmNode->hFile, 0);
34760       if( rc!=SQLITE_OK ){
34761         rc = winLogError(SQLITE_IOERR_SHMOPEN, osGetLastError(),
34762                          "winOpenShm", pDbFd->zPath);
34763       }
34764     }
34765     if( rc==SQLITE_OK ){
34766       winShmSystemLock(pShmNode, _SHM_UNLCK, WIN_SHM_DMS, 1);
34767       rc = winShmSystemLock(pShmNode, _SHM_RDLCK, WIN_SHM_DMS, 1);
34768     }
34769     if( rc ) goto shm_open_err;
34770   }
34771 
34772   /* Make the new connection a child of the winShmNode */
34773   p->pShmNode = pShmNode;
34774 #ifdef SQLITE_DEBUG
34775   p->id = pShmNode->nextShmId++;
34776 #endif
34777   pShmNode->nRef++;
34778   pDbFd->pShm = p;
34779   winShmLeaveMutex();
34780 
34781   /* The reference count on pShmNode has already been incremented under
34782   ** the cover of the winShmEnterMutex() mutex and the pointer from the
34783   ** new (struct winShm) object to the pShmNode has been set. All that is
34784   ** left to do is to link the new object into the linked list starting
34785   ** at pShmNode->pFirst. This must be done while holding the pShmNode->mutex
34786   ** mutex.
34787   */
34788   sqlite3_mutex_enter(pShmNode->mutex);
34789   p->pNext = pShmNode->pFirst;
34790   pShmNode->pFirst = p;
34791   sqlite3_mutex_leave(pShmNode->mutex);
34792   return SQLITE_OK;
34793 
34794   /* Jump here on any error */
34795 shm_open_err:
34796   winShmSystemLock(pShmNode, _SHM_UNLCK, WIN_SHM_DMS, 1);
34797   winShmPurge(pDbFd->pVfs, 0);      /* This call frees pShmNode if required */
34798   sqlite3_free(p);
34799   sqlite3_free(pNew);
34800   winShmLeaveMutex();
34801   return rc;
34802 }
34803 
34804 /*
34805 ** Close a connection to shared-memory.  Delete the underlying
34806 ** storage if deleteFlag is true.
34807 */
34808 static int winShmUnmap(
34809   sqlite3_file *fd,          /* Database holding shared memory */
34810   int deleteFlag             /* Delete after closing if true */
34811 ){
34812   winFile *pDbFd;       /* Database holding shared-memory */
34813   winShm *p;            /* The connection to be closed */
34814   winShmNode *pShmNode; /* The underlying shared-memory file */
34815   winShm **pp;          /* For looping over sibling connections */
34816 
34817   pDbFd = (winFile*)fd;
34818   p = pDbFd->pShm;
34819   if( p==0 ) return SQLITE_OK;
34820   pShmNode = p->pShmNode;
34821 
34822   /* Remove connection p from the set of connections associated
34823   ** with pShmNode */
34824   sqlite3_mutex_enter(pShmNode->mutex);
34825   for(pp=&pShmNode->pFirst; (*pp)!=p; pp = &(*pp)->pNext){}
34826   *pp = p->pNext;
34827 
34828   /* Free the connection p */
34829   sqlite3_free(p);
34830   pDbFd->pShm = 0;
34831   sqlite3_mutex_leave(pShmNode->mutex);
34832 
34833   /* If pShmNode->nRef has reached 0, then close the underlying
34834   ** shared-memory file, too */
34835   winShmEnterMutex();
34836   assert( pShmNode->nRef>0 );
34837   pShmNode->nRef--;
34838   if( pShmNode->nRef==0 ){
34839     winShmPurge(pDbFd->pVfs, deleteFlag);
34840   }
34841   winShmLeaveMutex();
34842 
34843   return SQLITE_OK;
34844 }
34845 
34846 /*
34847 ** Change the lock state for a shared-memory segment.
34848 */
34849 static int winShmLock(
34850   sqlite3_file *fd,          /* Database file holding the shared memory */
34851   int ofst,                  /* First lock to acquire or release */
34852   int n,                     /* Number of locks to acquire or release */
34853   int flags                  /* What to do with the lock */
34854 ){
34855   winFile *pDbFd = (winFile*)fd;        /* Connection holding shared memory */
34856   winShm *p = pDbFd->pShm;              /* The shared memory being locked */
34857   winShm *pX;                           /* For looping over all siblings */
34858   winShmNode *pShmNode = p->pShmNode;
34859   int rc = SQLITE_OK;                   /* Result code */
34860   u16 mask;                             /* Mask of locks to take or release */
34861 
34862   assert( ofst>=0 && ofst+n<=SQLITE_SHM_NLOCK );
34863   assert( n>=1 );
34864   assert( flags==(SQLITE_SHM_LOCK | SQLITE_SHM_SHARED)
34865        || flags==(SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE)
34866        || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED)
34867        || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE) );
34868   assert( n==1 || (flags & SQLITE_SHM_EXCLUSIVE)!=0 );
34869 
34870   mask = (u16)((1U<<(ofst+n)) - (1U<<ofst));
34871   assert( n>1 || mask==(1<<ofst) );
34872   sqlite3_mutex_enter(pShmNode->mutex);
34873   if( flags & SQLITE_SHM_UNLOCK ){
34874     u16 allMask = 0; /* Mask of locks held by siblings */
34875 
34876     /* See if any siblings hold this same lock */
34877     for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
34878       if( pX==p ) continue;
34879       assert( (pX->exclMask & (p->exclMask|p->sharedMask))==0 );
34880       allMask |= pX->sharedMask;
34881     }
34882 
34883     /* Unlock the system-level locks */
34884     if( (mask & allMask)==0 ){
34885       rc = winShmSystemLock(pShmNode, _SHM_UNLCK, ofst+WIN_SHM_BASE, n);
34886     }else{
34887       rc = SQLITE_OK;
34888     }
34889 
34890     /* Undo the local locks */
34891     if( rc==SQLITE_OK ){
34892       p->exclMask &= ~mask;
34893       p->sharedMask &= ~mask;
34894     }
34895   }else if( flags & SQLITE_SHM_SHARED ){
34896     u16 allShared = 0;  /* Union of locks held by connections other than "p" */
34897 
34898     /* Find out which shared locks are already held by sibling connections.
34899     ** If any sibling already holds an exclusive lock, go ahead and return
34900     ** SQLITE_BUSY.
34901     */
34902     for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
34903       if( (pX->exclMask & mask)!=0 ){
34904         rc = SQLITE_BUSY;
34905         break;
34906       }
34907       allShared |= pX->sharedMask;
34908     }
34909 
34910     /* Get shared locks at the system level, if necessary */
34911     if( rc==SQLITE_OK ){
34912       if( (allShared & mask)==0 ){
34913         rc = winShmSystemLock(pShmNode, _SHM_RDLCK, ofst+WIN_SHM_BASE, n);
34914       }else{
34915         rc = SQLITE_OK;
34916       }
34917     }
34918 
34919     /* Get the local shared locks */
34920     if( rc==SQLITE_OK ){
34921       p->sharedMask |= mask;
34922     }
34923   }else{
34924     /* Make sure no sibling connections hold locks that will block this
34925     ** lock.  If any do, return SQLITE_BUSY right away.
34926     */
34927     for(pX=pShmNode->pFirst; pX; pX=pX->pNext){
34928       if( (pX->exclMask & mask)!=0 || (pX->sharedMask & mask)!=0 ){
34929         rc = SQLITE_BUSY;
34930         break;
34931       }
34932     }
34933 
34934     /* Get the exclusive locks at the system level.  Then if successful
34935     ** also mark the local connection as being locked.
34936     */
34937     if( rc==SQLITE_OK ){
34938       rc = winShmSystemLock(pShmNode, _SHM_WRLCK, ofst+WIN_SHM_BASE, n);
34939       if( rc==SQLITE_OK ){
34940         assert( (p->sharedMask & mask)==0 );
34941         p->exclMask |= mask;
34942       }
34943     }
34944   }
34945   sqlite3_mutex_leave(pShmNode->mutex);
34946   OSTRACE(("SHM-LOCK pid=%lu, id=%d, sharedMask=%03x, exclMask=%03x, rc=%s\n",
34947            osGetCurrentProcessId(), p->id, p->sharedMask, p->exclMask,
34948            sqlite3ErrName(rc)));
34949   return rc;
34950 }
34951 
34952 /*
34953 ** Implement a memory barrier or memory fence on shared memory.
34954 **
34955 ** All loads and stores begun before the barrier must complete before
34956 ** any load or store begun after the barrier.
34957 */
34958 static void winShmBarrier(
34959   sqlite3_file *fd          /* Database holding the shared memory */
34960 ){
34961   UNUSED_PARAMETER(fd);
34962   /* MemoryBarrier(); // does not work -- do not know why not */
34963   winShmEnterMutex();
34964   winShmLeaveMutex();
34965 }
34966 
34967 /*
34968 ** This function is called to obtain a pointer to region iRegion of the
34969 ** shared-memory associated with the database file fd. Shared-memory regions
34970 ** are numbered starting from zero. Each shared-memory region is szRegion
34971 ** bytes in size.
34972 **
34973 ** If an error occurs, an error code is returned and *pp is set to NULL.
34974 **
34975 ** Otherwise, if the isWrite parameter is 0 and the requested shared-memory
34976 ** region has not been allocated (by any client, including one running in a
34977 ** separate process), then *pp is set to NULL and SQLITE_OK returned. If
34978 ** isWrite is non-zero and the requested shared-memory region has not yet
34979 ** been allocated, it is allocated by this function.
34980 **
34981 ** If the shared-memory region has already been allocated or is allocated by
34982 ** this call as described above, then it is mapped into this processes
34983 ** address space (if it is not already), *pp is set to point to the mapped
34984 ** memory and SQLITE_OK returned.
34985 */
34986 static int winShmMap(
34987   sqlite3_file *fd,               /* Handle open on database file */
34988   int iRegion,                    /* Region to retrieve */
34989   int szRegion,                   /* Size of regions */
34990   int isWrite,                    /* True to extend file if necessary */
34991   void volatile **pp              /* OUT: Mapped memory */
34992 ){
34993   winFile *pDbFd = (winFile*)fd;
34994   winShm *p = pDbFd->pShm;
34995   winShmNode *pShmNode;
34996   int rc = SQLITE_OK;
34997 
34998   if( !p ){
34999     rc = winOpenSharedMemory(pDbFd);
35000     if( rc!=SQLITE_OK ) return rc;
35001     p = pDbFd->pShm;
35002   }
35003   pShmNode = p->pShmNode;
35004 
35005   sqlite3_mutex_enter(pShmNode->mutex);
35006   assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 );
35007 
35008   if( pShmNode->nRegion<=iRegion ){
35009     struct ShmRegion *apNew;           /* New aRegion[] array */
35010     int nByte = (iRegion+1)*szRegion;  /* Minimum required file size */
35011     sqlite3_int64 sz;                  /* Current size of wal-index file */
35012 
35013     pShmNode->szRegion = szRegion;
35014 
35015     /* The requested region is not mapped into this processes address space.
35016     ** Check to see if it has been allocated (i.e. if the wal-index file is
35017     ** large enough to contain the requested region).
35018     */
35019     rc = winFileSize((sqlite3_file *)&pShmNode->hFile, &sz);
35020     if( rc!=SQLITE_OK ){
35021       rc = winLogError(SQLITE_IOERR_SHMSIZE, osGetLastError(),
35022                        "winShmMap1", pDbFd->zPath);
35023       goto shmpage_out;
35024     }
35025 
35026     if( sz<nByte ){
35027       /* The requested memory region does not exist. If isWrite is set to
35028       ** zero, exit early. *pp will be set to NULL and SQLITE_OK returned.
35029       **
35030       ** Alternatively, if isWrite is non-zero, use ftruncate() to allocate
35031       ** the requested memory region.
35032       */
35033       if( !isWrite ) goto shmpage_out;
35034       rc = winTruncate((sqlite3_file *)&pShmNode->hFile, nByte);
35035       if( rc!=SQLITE_OK ){
35036         rc = winLogError(SQLITE_IOERR_SHMSIZE, osGetLastError(),
35037                          "winShmMap2", pDbFd->zPath);
35038         goto shmpage_out;
35039       }
35040     }
35041 
35042     /* Map the requested memory region into this processes address space. */
35043     apNew = (struct ShmRegion *)sqlite3_realloc(
35044         pShmNode->aRegion, (iRegion+1)*sizeof(apNew[0])
35045     );
35046     if( !apNew ){
35047       rc = SQLITE_IOERR_NOMEM;
35048       goto shmpage_out;
35049     }
35050     pShmNode->aRegion = apNew;
35051 
35052     while( pShmNode->nRegion<=iRegion ){
35053       HANDLE hMap = NULL;         /* file-mapping handle */
35054       void *pMap = 0;             /* Mapped memory region */
35055 
35056 #if SQLITE_OS_WINRT
35057       hMap = osCreateFileMappingFromApp(pShmNode->hFile.h,
35058           NULL, PAGE_READWRITE, nByte, NULL
35059       );
35060 #elif defined(SQLITE_WIN32_HAS_WIDE)
35061       hMap = osCreateFileMappingW(pShmNode->hFile.h,
35062           NULL, PAGE_READWRITE, 0, nByte, NULL
35063       );
35064 #elif defined(SQLITE_WIN32_HAS_ANSI)
35065       hMap = osCreateFileMappingA(pShmNode->hFile.h,
35066           NULL, PAGE_READWRITE, 0, nByte, NULL
35067       );
35068 #endif
35069       OSTRACE(("SHM-MAP-CREATE pid=%lu, region=%d, size=%d, rc=%s\n",
35070                osGetCurrentProcessId(), pShmNode->nRegion, nByte,
35071                hMap ? "ok" : "failed"));
35072       if( hMap ){
35073         int iOffset = pShmNode->nRegion*szRegion;
35074         int iOffsetShift = iOffset % winSysInfo.dwAllocationGranularity;
35075 #if SQLITE_OS_WINRT
35076         pMap = osMapViewOfFileFromApp(hMap, FILE_MAP_WRITE | FILE_MAP_READ,
35077             iOffset - iOffsetShift, szRegion + iOffsetShift
35078         );
35079 #else
35080         pMap = osMapViewOfFile(hMap, FILE_MAP_WRITE | FILE_MAP_READ,
35081             0, iOffset - iOffsetShift, szRegion + iOffsetShift
35082         );
35083 #endif
35084         OSTRACE(("SHM-MAP-MAP pid=%lu, region=%d, offset=%d, size=%d, rc=%s\n",
35085                  osGetCurrentProcessId(), pShmNode->nRegion, iOffset,
35086                  szRegion, pMap ? "ok" : "failed"));
35087       }
35088       if( !pMap ){
35089         pShmNode->lastErrno = osGetLastError();
35090         rc = winLogError(SQLITE_IOERR_SHMMAP, pShmNode->lastErrno,
35091                          "winShmMap3", pDbFd->zPath);
35092         if( hMap ) osCloseHandle(hMap);
35093         goto shmpage_out;
35094       }
35095 
35096       pShmNode->aRegion[pShmNode->nRegion].pMap = pMap;
35097       pShmNode->aRegion[pShmNode->nRegion].hMap = hMap;
35098       pShmNode->nRegion++;
35099     }
35100   }
35101 
35102 shmpage_out:
35103   if( pShmNode->nRegion>iRegion ){
35104     int iOffset = iRegion*szRegion;
35105     int iOffsetShift = iOffset % winSysInfo.dwAllocationGranularity;
35106     char *p = (char *)pShmNode->aRegion[iRegion].pMap;
35107     *pp = (void *)&p[iOffsetShift];
35108   }else{
35109     *pp = 0;
35110   }
35111   sqlite3_mutex_leave(pShmNode->mutex);
35112   return rc;
35113 }
35114 
35115 #else
35116 # define winShmMap     0
35117 # define winShmLock    0
35118 # define winShmBarrier 0
35119 # define winShmUnmap   0
35120 #endif /* #ifndef SQLITE_OMIT_WAL */
35121 
35122 /*
35123 ** Cleans up the mapped region of the specified file, if any.
35124 */
35125 #if SQLITE_MAX_MMAP_SIZE>0
35126 static int winUnmapfile(winFile *pFile){
35127   assert( pFile!=0 );
35128   OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, hMap=%p, pMapRegion=%p, "
35129            "mmapSize=%lld, mmapSizeActual=%lld, mmapSizeMax=%lld\n",
35130            osGetCurrentProcessId(), pFile, pFile->hMap, pFile->pMapRegion,
35131            pFile->mmapSize, pFile->mmapSizeActual, pFile->mmapSizeMax));
35132   if( pFile->pMapRegion ){
35133     if( !osUnmapViewOfFile(pFile->pMapRegion) ){
35134       pFile->lastErrno = osGetLastError();
35135       OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, pMapRegion=%p, "
35136                "rc=SQLITE_IOERR_MMAP\n", osGetCurrentProcessId(), pFile,
35137                pFile->pMapRegion));
35138       return winLogError(SQLITE_IOERR_MMAP, pFile->lastErrno,
35139                          "winUnmapfile1", pFile->zPath);
35140     }
35141     pFile->pMapRegion = 0;
35142     pFile->mmapSize = 0;
35143     pFile->mmapSizeActual = 0;
35144   }
35145   if( pFile->hMap!=NULL ){
35146     if( !osCloseHandle(pFile->hMap) ){
35147       pFile->lastErrno = osGetLastError();
35148       OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, hMap=%p, rc=SQLITE_IOERR_MMAP\n",
35149                osGetCurrentProcessId(), pFile, pFile->hMap));
35150       return winLogError(SQLITE_IOERR_MMAP, pFile->lastErrno,
35151                          "winUnmapfile2", pFile->zPath);
35152     }
35153     pFile->hMap = NULL;
35154   }
35155   OSTRACE(("UNMAP-FILE pid=%lu, pFile=%p, rc=SQLITE_OK\n",
35156            osGetCurrentProcessId(), pFile));
35157   return SQLITE_OK;
35158 }
35159 
35160 /*
35161 ** Memory map or remap the file opened by file-descriptor pFd (if the file
35162 ** is already mapped, the existing mapping is replaced by the new). Or, if
35163 ** there already exists a mapping for this file, and there are still
35164 ** outstanding xFetch() references to it, this function is a no-op.
35165 **
35166 ** If parameter nByte is non-negative, then it is the requested size of
35167 ** the mapping to create. Otherwise, if nByte is less than zero, then the
35168 ** requested size is the size of the file on disk. The actual size of the
35169 ** created mapping is either the requested size or the value configured
35170 ** using SQLITE_FCNTL_MMAP_SIZE, whichever is smaller.
35171 **
35172 ** SQLITE_OK is returned if no error occurs (even if the mapping is not
35173 ** recreated as a result of outstanding references) or an SQLite error
35174 ** code otherwise.
35175 */
35176 static int winMapfile(winFile *pFd, sqlite3_int64 nByte){
35177   sqlite3_int64 nMap = nByte;
35178   int rc;
35179 
35180   assert( nMap>=0 || pFd->nFetchOut==0 );
35181   OSTRACE(("MAP-FILE pid=%lu, pFile=%p, size=%lld\n",
35182            osGetCurrentProcessId(), pFd, nByte));
35183 
35184   if( pFd->nFetchOut>0 ) return SQLITE_OK;
35185 
35186   if( nMap<0 ){
35187     rc = winFileSize((sqlite3_file*)pFd, &nMap);
35188     if( rc ){
35189       OSTRACE(("MAP-FILE pid=%lu, pFile=%p, rc=SQLITE_IOERR_FSTAT\n",
35190                osGetCurrentProcessId(), pFd));
35191       return SQLITE_IOERR_FSTAT;
35192     }
35193   }
35194   if( nMap>pFd->mmapSizeMax ){
35195     nMap = pFd->mmapSizeMax;
35196   }
35197   nMap &= ~(sqlite3_int64)(winSysInfo.dwPageSize - 1);
35198 
35199   if( nMap==0 && pFd->mmapSize>0 ){
35200     winUnmapfile(pFd);
35201   }
35202   if( nMap!=pFd->mmapSize ){
35203     void *pNew = 0;
35204     DWORD protect = PAGE_READONLY;
35205     DWORD flags = FILE_MAP_READ;
35206 
35207     winUnmapfile(pFd);
35208     if( (pFd->ctrlFlags & WINFILE_RDONLY)==0 ){
35209       protect = PAGE_READWRITE;
35210       flags |= FILE_MAP_WRITE;
35211     }
35212 #if SQLITE_OS_WINRT
35213     pFd->hMap = osCreateFileMappingFromApp(pFd->h, NULL, protect, nMap, NULL);
35214 #elif defined(SQLITE_WIN32_HAS_WIDE)
35215     pFd->hMap = osCreateFileMappingW(pFd->h, NULL, protect,
35216                                 (DWORD)((nMap>>32) & 0xffffffff),
35217                                 (DWORD)(nMap & 0xffffffff), NULL);
35218 #elif defined(SQLITE_WIN32_HAS_ANSI)
35219     pFd->hMap = osCreateFileMappingA(pFd->h, NULL, protect,
35220                                 (DWORD)((nMap>>32) & 0xffffffff),
35221                                 (DWORD)(nMap & 0xffffffff), NULL);
35222 #endif
35223     if( pFd->hMap==NULL ){
35224       pFd->lastErrno = osGetLastError();
35225       rc = winLogError(SQLITE_IOERR_MMAP, pFd->lastErrno,
35226                        "winMapfile1", pFd->zPath);
35227       /* Log the error, but continue normal operation using xRead/xWrite */
35228       OSTRACE(("MAP-FILE-CREATE pid=%lu, pFile=%p, rc=%s\n",
35229                osGetCurrentProcessId(), pFd, sqlite3ErrName(rc)));
35230       return SQLITE_OK;
35231     }
35232     assert( (nMap % winSysInfo.dwPageSize)==0 );
35233     assert( sizeof(SIZE_T)==sizeof(sqlite3_int64) || nMap<=0xffffffff );
35234 #if SQLITE_OS_WINRT
35235     pNew = osMapViewOfFileFromApp(pFd->hMap, flags, 0, (SIZE_T)nMap);
35236 #else
35237     pNew = osMapViewOfFile(pFd->hMap, flags, 0, 0, (SIZE_T)nMap);
35238 #endif
35239     if( pNew==NULL ){
35240       osCloseHandle(pFd->hMap);
35241       pFd->hMap = NULL;
35242       pFd->lastErrno = osGetLastError();
35243       rc = winLogError(SQLITE_IOERR_MMAP, pFd->lastErrno,
35244                        "winMapfile2", pFd->zPath);
35245       /* Log the error, but continue normal operation using xRead/xWrite */
35246       OSTRACE(("MAP-FILE-MAP pid=%lu, pFile=%p, rc=%s\n",
35247                osGetCurrentProcessId(), pFd, sqlite3ErrName(rc)));
35248       return SQLITE_OK;
35249     }
35250     pFd->pMapRegion = pNew;
35251     pFd->mmapSize = nMap;
35252     pFd->mmapSizeActual = nMap;
35253   }
35254 
35255   OSTRACE(("MAP-FILE pid=%lu, pFile=%p, rc=SQLITE_OK\n",
35256            osGetCurrentProcessId(), pFd));
35257   return SQLITE_OK;
35258 }
35259 #endif /* SQLITE_MAX_MMAP_SIZE>0 */
35260 
35261 /*
35262 ** If possible, return a pointer to a mapping of file fd starting at offset
35263 ** iOff. The mapping must be valid for at least nAmt bytes.
35264 **
35265 ** If such a pointer can be obtained, store it in *pp and return SQLITE_OK.
35266 ** Or, if one cannot but no error occurs, set *pp to 0 and return SQLITE_OK.
35267 ** Finally, if an error does occur, return an SQLite error code. The final
35268 ** value of *pp is undefined in this case.
35269 **
35270 ** If this function does return a pointer, the caller must eventually
35271 ** release the reference by calling winUnfetch().
35272 */
35273 static int winFetch(sqlite3_file *fd, i64 iOff, int nAmt, void **pp){
35274 #if SQLITE_MAX_MMAP_SIZE>0
35275   winFile *pFd = (winFile*)fd;   /* The underlying database file */
35276 #endif
35277   *pp = 0;
35278 
35279   OSTRACE(("FETCH pid=%lu, pFile=%p, offset=%lld, amount=%d, pp=%p\n",
35280            osGetCurrentProcessId(), fd, iOff, nAmt, pp));
35281 
35282 #if SQLITE_MAX_MMAP_SIZE>0
35283   if( pFd->mmapSizeMax>0 ){
35284     if( pFd->pMapRegion==0 ){
35285       int rc = winMapfile(pFd, -1);
35286       if( rc!=SQLITE_OK ){
35287         OSTRACE(("FETCH pid=%lu, pFile=%p, rc=%s\n",
35288                  osGetCurrentProcessId(), pFd, sqlite3ErrName(rc)));
35289         return rc;
35290       }
35291     }
35292     if( pFd->mmapSize >= iOff+nAmt ){
35293       *pp = &((u8 *)pFd->pMapRegion)[iOff];
35294       pFd->nFetchOut++;
35295     }
35296   }
35297 #endif
35298 
35299   OSTRACE(("FETCH pid=%lu, pFile=%p, pp=%p, *pp=%p, rc=SQLITE_OK\n",
35300            osGetCurrentProcessId(), fd, pp, *pp));
35301   return SQLITE_OK;
35302 }
35303 
35304 /*
35305 ** If the third argument is non-NULL, then this function releases a
35306 ** reference obtained by an earlier call to winFetch(). The second
35307 ** argument passed to this function must be the same as the corresponding
35308 ** argument that was passed to the winFetch() invocation.
35309 **
35310 ** Or, if the third argument is NULL, then this function is being called
35311 ** to inform the VFS layer that, according to POSIX, any existing mapping
35312 ** may now be invalid and should be unmapped.
35313 */
35314 static int winUnfetch(sqlite3_file *fd, i64 iOff, void *p){
35315 #if SQLITE_MAX_MMAP_SIZE>0
35316   winFile *pFd = (winFile*)fd;   /* The underlying database file */
35317 
35318   /* If p==0 (unmap the entire file) then there must be no outstanding
35319   ** xFetch references. Or, if p!=0 (meaning it is an xFetch reference),
35320   ** then there must be at least one outstanding.  */
35321   assert( (p==0)==(pFd->nFetchOut==0) );
35322 
35323   /* If p!=0, it must match the iOff value. */
35324   assert( p==0 || p==&((u8 *)pFd->pMapRegion)[iOff] );
35325 
35326   OSTRACE(("UNFETCH pid=%lu, pFile=%p, offset=%lld, p=%p\n",
35327            osGetCurrentProcessId(), pFd, iOff, p));
35328 
35329   if( p ){
35330     pFd->nFetchOut--;
35331   }else{
35332     /* FIXME:  If Windows truly always prevents truncating or deleting a
35333     ** file while a mapping is held, then the following winUnmapfile() call
35334     ** is unnecessary can can be omitted - potentially improving
35335     ** performance.  */
35336     winUnmapfile(pFd);
35337   }
35338 
35339   assert( pFd->nFetchOut>=0 );
35340 #endif
35341 
35342   OSTRACE(("UNFETCH pid=%lu, pFile=%p, rc=SQLITE_OK\n",
35343            osGetCurrentProcessId(), fd));
35344   return SQLITE_OK;
35345 }
35346 
35347 /*
35348 ** Here ends the implementation of all sqlite3_file methods.
35349 **
35350 ********************** End sqlite3_file Methods *******************************
35351 ******************************************************************************/
35352 
35353 /*
35354 ** This vector defines all the methods that can operate on an
35355 ** sqlite3_file for win32.
35356 */
35357 static const sqlite3_io_methods winIoMethod = {
35358   3,                              /* iVersion */
35359   winClose,                       /* xClose */
35360   winRead,                        /* xRead */
35361   winWrite,                       /* xWrite */
35362   winTruncate,                    /* xTruncate */
35363   winSync,                        /* xSync */
35364   winFileSize,                    /* xFileSize */
35365   winLock,                        /* xLock */
35366   winUnlock,                      /* xUnlock */
35367   winCheckReservedLock,           /* xCheckReservedLock */
35368   winFileControl,                 /* xFileControl */
35369   winSectorSize,                  /* xSectorSize */
35370   winDeviceCharacteristics,       /* xDeviceCharacteristics */
35371   winShmMap,                      /* xShmMap */
35372   winShmLock,                     /* xShmLock */
35373   winShmBarrier,                  /* xShmBarrier */
35374   winShmUnmap,                    /* xShmUnmap */
35375   winFetch,                       /* xFetch */
35376   winUnfetch                      /* xUnfetch */
35377 };
35378 
35379 /****************************************************************************
35380 **************************** sqlite3_vfs methods ****************************
35381 **
35382 ** This division contains the implementation of methods on the
35383 ** sqlite3_vfs object.
35384 */
35385 
35386 #if defined(__CYGWIN__)
35387 /*
35388 ** Convert a filename from whatever the underlying operating system
35389 ** supports for filenames into UTF-8.  Space to hold the result is
35390 ** obtained from malloc and must be freed by the calling function.
35391 */
35392 static char *winConvertToUtf8Filename(const void *zFilename){
35393   char *zConverted = 0;
35394   if( osIsNT() ){
35395     zConverted = winUnicodeToUtf8(zFilename);
35396   }
35397 #ifdef SQLITE_WIN32_HAS_ANSI
35398   else{
35399     zConverted = sqlite3_win32_mbcs_to_utf8(zFilename);
35400   }
35401 #endif
35402   /* caller will handle out of memory */
35403   return zConverted;
35404 }
35405 #endif
35406 
35407 /*
35408 ** Convert a UTF-8 filename into whatever form the underlying
35409 ** operating system wants filenames in.  Space to hold the result
35410 ** is obtained from malloc and must be freed by the calling
35411 ** function.
35412 */
35413 static void *winConvertFromUtf8Filename(const char *zFilename){
35414   void *zConverted = 0;
35415   if( osIsNT() ){
35416     zConverted = winUtf8ToUnicode(zFilename);
35417   }
35418 #ifdef SQLITE_WIN32_HAS_ANSI
35419   else{
35420     zConverted = sqlite3_win32_utf8_to_mbcs(zFilename);
35421   }
35422 #endif
35423   /* caller will handle out of memory */
35424   return zConverted;
35425 }
35426 
35427 /*
35428 ** This function returns non-zero if the specified UTF-8 string buffer
35429 ** ends with a directory separator character or one was successfully
35430 ** added to it.
35431 */
35432 static int winMakeEndInDirSep(int nBuf, char *zBuf){
35433   if( zBuf ){
35434     int nLen = sqlite3Strlen30(zBuf);
35435     if( nLen>0 ){
35436       if( winIsDirSep(zBuf[nLen-1]) ){
35437         return 1;
35438       }else if( nLen+1<nBuf ){
35439         zBuf[nLen] = winGetDirSep();
35440         zBuf[nLen+1] = '\0';
35441         return 1;
35442       }
35443     }
35444   }
35445   return 0;
35446 }
35447 
35448 /*
35449 ** Create a temporary file name and store the resulting pointer into pzBuf.
35450 ** The pointer returned in pzBuf must be freed via sqlite3_free().
35451 */
35452 static int winGetTempname(sqlite3_vfs *pVfs, char **pzBuf){
35453   static char zChars[] =
35454     "abcdefghijklmnopqrstuvwxyz"
35455     "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
35456     "0123456789";
35457   size_t i, j;
35458   int nPre = sqlite3Strlen30(SQLITE_TEMP_FILE_PREFIX);
35459   int nMax, nBuf, nDir, nLen;
35460   char *zBuf;
35461 
35462   /* It's odd to simulate an io-error here, but really this is just
35463   ** using the io-error infrastructure to test that SQLite handles this
35464   ** function failing.
35465   */
35466   SimulateIOError( return SQLITE_IOERR );
35467 
35468   /* Allocate a temporary buffer to store the fully qualified file
35469   ** name for the temporary file.  If this fails, we cannot continue.
35470   */
35471   nMax = pVfs->mxPathname; nBuf = nMax + 2;
35472   zBuf = sqlite3MallocZero( nBuf );
35473   if( !zBuf ){
35474     OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
35475     return SQLITE_IOERR_NOMEM;
35476   }
35477 
35478   /* Figure out the effective temporary directory.  First, check if one
35479   ** has been explicitly set by the application; otherwise, use the one
35480   ** configured by the operating system.
35481   */
35482   nDir = nMax - (nPre + 15);
35483   assert( nDir>0 );
35484   if( sqlite3_temp_directory ){
35485     int nDirLen = sqlite3Strlen30(sqlite3_temp_directory);
35486     if( nDirLen>0 ){
35487       if( !winIsDirSep(sqlite3_temp_directory[nDirLen-1]) ){
35488         nDirLen++;
35489       }
35490       if( nDirLen>nDir ){
35491         sqlite3_free(zBuf);
35492         OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n"));
35493         return winLogError(SQLITE_ERROR, 0, "winGetTempname1", 0);
35494       }
35495       sqlite3_snprintf(nMax, zBuf, "%s", sqlite3_temp_directory);
35496     }
35497   }
35498 #if defined(__CYGWIN__)
35499   else{
35500     static const char *azDirs[] = {
35501        0, /* getenv("SQLITE_TMPDIR") */
35502        0, /* getenv("TMPDIR") */
35503        0, /* getenv("TMP") */
35504        0, /* getenv("TEMP") */
35505        0, /* getenv("USERPROFILE") */
35506        "/var/tmp",
35507        "/usr/tmp",
35508        "/tmp",
35509        ".",
35510        0        /* List terminator */
35511     };
35512     unsigned int i;
35513     const char *zDir = 0;
35514 
35515     if( !azDirs[0] ) azDirs[0] = getenv("SQLITE_TMPDIR");
35516     if( !azDirs[1] ) azDirs[1] = getenv("TMPDIR");
35517     if( !azDirs[2] ) azDirs[2] = getenv("TMP");
35518     if( !azDirs[3] ) azDirs[3] = getenv("TEMP");
35519     if( !azDirs[4] ) azDirs[4] = getenv("USERPROFILE");
35520     for(i=0; i<sizeof(azDirs)/sizeof(azDirs[0]); zDir=azDirs[i++]){
35521       void *zConverted;
35522       if( zDir==0 ) continue;
35523       /* If the path starts with a drive letter followed by the colon
35524       ** character, assume it is already a native Win32 path; otherwise,
35525       ** it must be converted to a native Win32 path via the Cygwin API
35526       ** prior to using it.
35527       */
35528       if( winIsDriveLetterAndColon(zDir) ){
35529         zConverted = winConvertFromUtf8Filename(zDir);
35530         if( !zConverted ){
35531           sqlite3_free(zBuf);
35532           OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
35533           return SQLITE_IOERR_NOMEM;
35534         }
35535         if( winIsDir(zConverted) ){
35536           sqlite3_snprintf(nMax, zBuf, "%s", zDir);
35537           sqlite3_free(zConverted);
35538           break;
35539         }
35540         sqlite3_free(zConverted);
35541       }else{
35542         zConverted = sqlite3MallocZero( nMax+1 );
35543         if( !zConverted ){
35544           sqlite3_free(zBuf);
35545           OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
35546           return SQLITE_IOERR_NOMEM;
35547         }
35548         if( cygwin_conv_path(
35549                 osIsNT() ? CCP_POSIX_TO_WIN_W : CCP_POSIX_TO_WIN_A, zDir,
35550                 zConverted, nMax+1)<0 ){
35551           sqlite3_free(zConverted);
35552           sqlite3_free(zBuf);
35553           OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_CONVPATH\n"));
35554           return winLogError(SQLITE_IOERR_CONVPATH, (DWORD)errno,
35555                              "winGetTempname2", zDir);
35556         }
35557         if( winIsDir(zConverted) ){
35558           /* At this point, we know the candidate directory exists and should
35559           ** be used.  However, we may need to convert the string containing
35560           ** its name into UTF-8 (i.e. if it is UTF-16 right now).
35561           */
35562           char *zUtf8 = winConvertToUtf8Filename(zConverted);
35563           if( !zUtf8 ){
35564             sqlite3_free(zConverted);
35565             sqlite3_free(zBuf);
35566             OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
35567             return SQLITE_IOERR_NOMEM;
35568           }
35569           sqlite3_snprintf(nMax, zBuf, "%s", zUtf8);
35570           sqlite3_free(zUtf8);
35571           sqlite3_free(zConverted);
35572           break;
35573         }
35574         sqlite3_free(zConverted);
35575       }
35576     }
35577   }
35578 #elif !SQLITE_OS_WINRT && !defined(__CYGWIN__)
35579   else if( osIsNT() ){
35580     char *zMulti;
35581     LPWSTR zWidePath = sqlite3MallocZero( nMax*sizeof(WCHAR) );
35582     if( !zWidePath ){
35583       sqlite3_free(zBuf);
35584       OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
35585       return SQLITE_IOERR_NOMEM;
35586     }
35587     if( osGetTempPathW(nMax, zWidePath)==0 ){
35588       sqlite3_free(zWidePath);
35589       sqlite3_free(zBuf);
35590       OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_GETTEMPPATH\n"));
35591       return winLogError(SQLITE_IOERR_GETTEMPPATH, osGetLastError(),
35592                          "winGetTempname2", 0);
35593     }
35594     zMulti = winUnicodeToUtf8(zWidePath);
35595     if( zMulti ){
35596       sqlite3_snprintf(nMax, zBuf, "%s", zMulti);
35597       sqlite3_free(zMulti);
35598       sqlite3_free(zWidePath);
35599     }else{
35600       sqlite3_free(zWidePath);
35601       sqlite3_free(zBuf);
35602       OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
35603       return SQLITE_IOERR_NOMEM;
35604     }
35605   }
35606 #ifdef SQLITE_WIN32_HAS_ANSI
35607   else{
35608     char *zUtf8;
35609     char *zMbcsPath = sqlite3MallocZero( nMax );
35610     if( !zMbcsPath ){
35611       sqlite3_free(zBuf);
35612       OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
35613       return SQLITE_IOERR_NOMEM;
35614     }
35615     if( osGetTempPathA(nMax, zMbcsPath)==0 ){
35616       sqlite3_free(zBuf);
35617       OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_GETTEMPPATH\n"));
35618       return winLogError(SQLITE_IOERR_GETTEMPPATH, osGetLastError(),
35619                          "winGetTempname3", 0);
35620     }
35621     zUtf8 = sqlite3_win32_mbcs_to_utf8(zMbcsPath);
35622     if( zUtf8 ){
35623       sqlite3_snprintf(nMax, zBuf, "%s", zUtf8);
35624       sqlite3_free(zUtf8);
35625     }else{
35626       sqlite3_free(zBuf);
35627       OSTRACE(("TEMP-FILENAME rc=SQLITE_IOERR_NOMEM\n"));
35628       return SQLITE_IOERR_NOMEM;
35629     }
35630   }
35631 #endif /* SQLITE_WIN32_HAS_ANSI */
35632 #endif /* !SQLITE_OS_WINRT */
35633 
35634   /*
35635   ** Check to make sure the temporary directory ends with an appropriate
35636   ** separator.  If it does not and there is not enough space left to add
35637   ** one, fail.
35638   */
35639   if( !winMakeEndInDirSep(nDir+1, zBuf) ){
35640     sqlite3_free(zBuf);
35641     OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n"));
35642     return winLogError(SQLITE_ERROR, 0, "winGetTempname4", 0);
35643   }
35644 
35645   /*
35646   ** Check that the output buffer is large enough for the temporary file
35647   ** name in the following format:
35648   **
35649   **   "<temporary_directory>/etilqs_XXXXXXXXXXXXXXX\0\0"
35650   **
35651   ** If not, return SQLITE_ERROR.  The number 17 is used here in order to
35652   ** account for the space used by the 15 character random suffix and the
35653   ** two trailing NUL characters.  The final directory separator character
35654   ** has already added if it was not already present.
35655   */
35656   nLen = sqlite3Strlen30(zBuf);
35657   if( (nLen + nPre + 17) > nBuf ){
35658     sqlite3_free(zBuf);
35659     OSTRACE(("TEMP-FILENAME rc=SQLITE_ERROR\n"));
35660     return winLogError(SQLITE_ERROR, 0, "winGetTempname5", 0);
35661   }
35662 
35663   sqlite3_snprintf(nBuf-16-nLen, zBuf+nLen, SQLITE_TEMP_FILE_PREFIX);
35664 
35665   j = sqlite3Strlen30(zBuf);
35666   sqlite3_randomness(15, &zBuf[j]);
35667   for(i=0; i<15; i++, j++){
35668     zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
35669   }
35670   zBuf[j] = 0;
35671   zBuf[j+1] = 0;
35672   *pzBuf = zBuf;
35673 
35674   OSTRACE(("TEMP-FILENAME name=%s, rc=SQLITE_OK\n", zBuf));
35675   return SQLITE_OK;
35676 }
35677 
35678 /*
35679 ** Return TRUE if the named file is really a directory.  Return false if
35680 ** it is something other than a directory, or if there is any kind of memory
35681 ** allocation failure.
35682 */
35683 static int winIsDir(const void *zConverted){
35684   DWORD attr;
35685   int rc = 0;
35686   DWORD lastErrno;
35687 
35688   if( osIsNT() ){
35689     int cnt = 0;
35690     WIN32_FILE_ATTRIBUTE_DATA sAttrData;
35691     memset(&sAttrData, 0, sizeof(sAttrData));
35692     while( !(rc = osGetFileAttributesExW((LPCWSTR)zConverted,
35693                              GetFileExInfoStandard,
35694                              &sAttrData)) && winRetryIoerr(&cnt, &lastErrno) ){}
35695     if( !rc ){
35696       return 0; /* Invalid name? */
35697     }
35698     attr = sAttrData.dwFileAttributes;
35699 #if SQLITE_OS_WINCE==0
35700   }else{
35701     attr = osGetFileAttributesA((char*)zConverted);
35702 #endif
35703   }
35704   return (attr!=INVALID_FILE_ATTRIBUTES) && (attr&FILE_ATTRIBUTE_DIRECTORY);
35705 }
35706 
35707 /*
35708 ** Open a file.
35709 */
35710 static int winOpen(
35711   sqlite3_vfs *pVfs,        /* Used to get maximum path name length */
35712   const char *zName,        /* Name of the file (UTF-8) */
35713   sqlite3_file *id,         /* Write the SQLite file handle here */
35714   int flags,                /* Open mode flags */
35715   int *pOutFlags            /* Status return flags */
35716 ){
35717   HANDLE h;
35718   DWORD lastErrno = 0;
35719   DWORD dwDesiredAccess;
35720   DWORD dwShareMode;
35721   DWORD dwCreationDisposition;
35722   DWORD dwFlagsAndAttributes = 0;
35723 #if SQLITE_OS_WINCE
35724   int isTemp = 0;
35725 #endif
35726   winFile *pFile = (winFile*)id;
35727   void *zConverted;              /* Filename in OS encoding */
35728   const char *zUtf8Name = zName; /* Filename in UTF-8 encoding */
35729   int cnt = 0;
35730 
35731   /* If argument zPath is a NULL pointer, this function is required to open
35732   ** a temporary file. Use this buffer to store the file name in.
35733   */
35734   char *zTmpname = 0; /* For temporary filename, if necessary. */
35735 
35736   int rc = SQLITE_OK;            /* Function Return Code */
35737 #if !defined(NDEBUG) || SQLITE_OS_WINCE
35738   int eType = flags&0xFFFFFF00;  /* Type of file to open */
35739 #endif
35740 
35741   int isExclusive  = (flags & SQLITE_OPEN_EXCLUSIVE);
35742   int isDelete     = (flags & SQLITE_OPEN_DELETEONCLOSE);
35743   int isCreate     = (flags & SQLITE_OPEN_CREATE);
35744   int isReadonly   = (flags & SQLITE_OPEN_READONLY);
35745   int isReadWrite  = (flags & SQLITE_OPEN_READWRITE);
35746 
35747 #ifndef NDEBUG
35748   int isOpenJournal = (isCreate && (
35749         eType==SQLITE_OPEN_MASTER_JOURNAL
35750      || eType==SQLITE_OPEN_MAIN_JOURNAL
35751      || eType==SQLITE_OPEN_WAL
35752   ));
35753 #endif
35754 
35755   OSTRACE(("OPEN name=%s, pFile=%p, flags=%x, pOutFlags=%p\n",
35756            zUtf8Name, id, flags, pOutFlags));
35757 
35758   /* Check the following statements are true:
35759   **
35760   **   (a) Exactly one of the READWRITE and READONLY flags must be set, and
35761   **   (b) if CREATE is set, then READWRITE must also be set, and
35762   **   (c) if EXCLUSIVE is set, then CREATE must also be set.
35763   **   (d) if DELETEONCLOSE is set, then CREATE must also be set.
35764   */
35765   assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly));
35766   assert(isCreate==0 || isReadWrite);
35767   assert(isExclusive==0 || isCreate);
35768   assert(isDelete==0 || isCreate);
35769 
35770   /* The main DB, main journal, WAL file and master journal are never
35771   ** automatically deleted. Nor are they ever temporary files.  */
35772   assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_DB );
35773   assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_JOURNAL );
35774   assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MASTER_JOURNAL );
35775   assert( (!isDelete && zName) || eType!=SQLITE_OPEN_WAL );
35776 
35777   /* Assert that the upper layer has set one of the "file-type" flags. */
35778   assert( eType==SQLITE_OPEN_MAIN_DB      || eType==SQLITE_OPEN_TEMP_DB
35779        || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL
35780        || eType==SQLITE_OPEN_SUBJOURNAL   || eType==SQLITE_OPEN_MASTER_JOURNAL
35781        || eType==SQLITE_OPEN_TRANSIENT_DB || eType==SQLITE_OPEN_WAL
35782   );
35783 
35784   assert( pFile!=0 );
35785   memset(pFile, 0, sizeof(winFile));
35786   pFile->h = INVALID_HANDLE_VALUE;
35787 
35788 #if SQLITE_OS_WINRT
35789   if( !zUtf8Name && !sqlite3_temp_directory ){
35790     sqlite3_log(SQLITE_ERROR,
35791         "sqlite3_temp_directory variable should be set for WinRT");
35792   }
35793 #endif
35794 
35795   /* If the second argument to this function is NULL, generate a
35796   ** temporary file name to use
35797   */
35798   if( !zUtf8Name ){
35799     assert( isDelete && !isOpenJournal );
35800     rc = winGetTempname(pVfs, &zTmpname);
35801     if( rc!=SQLITE_OK ){
35802       OSTRACE(("OPEN name=%s, rc=%s", zUtf8Name, sqlite3ErrName(rc)));
35803       return rc;
35804     }
35805     zUtf8Name = zTmpname;
35806   }
35807 
35808   /* Database filenames are double-zero terminated if they are not
35809   ** URIs with parameters.  Hence, they can always be passed into
35810   ** sqlite3_uri_parameter().
35811   */
35812   assert( (eType!=SQLITE_OPEN_MAIN_DB) || (flags & SQLITE_OPEN_URI) ||
35813        zUtf8Name[sqlite3Strlen30(zUtf8Name)+1]==0 );
35814 
35815   /* Convert the filename to the system encoding. */
35816   zConverted = winConvertFromUtf8Filename(zUtf8Name);
35817   if( zConverted==0 ){
35818     sqlite3_free(zTmpname);
35819     OSTRACE(("OPEN name=%s, rc=SQLITE_IOERR_NOMEM", zUtf8Name));
35820     return SQLITE_IOERR_NOMEM;
35821   }
35822 
35823   if( winIsDir(zConverted) ){
35824     sqlite3_free(zConverted);
35825     sqlite3_free(zTmpname);
35826     OSTRACE(("OPEN name=%s, rc=SQLITE_CANTOPEN_ISDIR", zUtf8Name));
35827     return SQLITE_CANTOPEN_ISDIR;
35828   }
35829 
35830   if( isReadWrite ){
35831     dwDesiredAccess = GENERIC_READ | GENERIC_WRITE;
35832   }else{
35833     dwDesiredAccess = GENERIC_READ;
35834   }
35835 
35836   /* SQLITE_OPEN_EXCLUSIVE is used to make sure that a new file is
35837   ** created. SQLite doesn't use it to indicate "exclusive access"
35838   ** as it is usually understood.
35839   */
35840   if( isExclusive ){
35841     /* Creates a new file, only if it does not already exist. */
35842     /* If the file exists, it fails. */
35843     dwCreationDisposition = CREATE_NEW;
35844   }else if( isCreate ){
35845     /* Open existing file, or create if it doesn't exist */
35846     dwCreationDisposition = OPEN_ALWAYS;
35847   }else{
35848     /* Opens a file, only if it exists. */
35849     dwCreationDisposition = OPEN_EXISTING;
35850   }
35851 
35852   dwShareMode = FILE_SHARE_READ | FILE_SHARE_WRITE;
35853 
35854   if( isDelete ){
35855 #if SQLITE_OS_WINCE
35856     dwFlagsAndAttributes = FILE_ATTRIBUTE_HIDDEN;
35857     isTemp = 1;
35858 #else
35859     dwFlagsAndAttributes = FILE_ATTRIBUTE_TEMPORARY
35860                                | FILE_ATTRIBUTE_HIDDEN
35861                                | FILE_FLAG_DELETE_ON_CLOSE;
35862 #endif
35863   }else{
35864     dwFlagsAndAttributes = FILE_ATTRIBUTE_NORMAL;
35865   }
35866   /* Reports from the internet are that performance is always
35867   ** better if FILE_FLAG_RANDOM_ACCESS is used.  Ticket #2699. */
35868 #if SQLITE_OS_WINCE
35869   dwFlagsAndAttributes |= FILE_FLAG_RANDOM_ACCESS;
35870 #endif
35871 
35872   if( osIsNT() ){
35873 #if SQLITE_OS_WINRT
35874     CREATEFILE2_EXTENDED_PARAMETERS extendedParameters;
35875     extendedParameters.dwSize = sizeof(CREATEFILE2_EXTENDED_PARAMETERS);
35876     extendedParameters.dwFileAttributes =
35877             dwFlagsAndAttributes & FILE_ATTRIBUTE_MASK;
35878     extendedParameters.dwFileFlags = dwFlagsAndAttributes & FILE_FLAG_MASK;
35879     extendedParameters.dwSecurityQosFlags = SECURITY_ANONYMOUS;
35880     extendedParameters.lpSecurityAttributes = NULL;
35881     extendedParameters.hTemplateFile = NULL;
35882     while( (h = osCreateFile2((LPCWSTR)zConverted,
35883                               dwDesiredAccess,
35884                               dwShareMode,
35885                               dwCreationDisposition,
35886                               &extendedParameters))==INVALID_HANDLE_VALUE &&
35887                               winRetryIoerr(&cnt, &lastErrno) ){
35888                /* Noop */
35889     }
35890 #else
35891     while( (h = osCreateFileW((LPCWSTR)zConverted,
35892                               dwDesiredAccess,
35893                               dwShareMode, NULL,
35894                               dwCreationDisposition,
35895                               dwFlagsAndAttributes,
35896                               NULL))==INVALID_HANDLE_VALUE &&
35897                               winRetryIoerr(&cnt, &lastErrno) ){
35898                /* Noop */
35899     }
35900 #endif
35901   }
35902 #ifdef SQLITE_WIN32_HAS_ANSI
35903   else{
35904     while( (h = osCreateFileA((LPCSTR)zConverted,
35905                               dwDesiredAccess,
35906                               dwShareMode, NULL,
35907                               dwCreationDisposition,
35908                               dwFlagsAndAttributes,
35909                               NULL))==INVALID_HANDLE_VALUE &&
35910                               winRetryIoerr(&cnt, &lastErrno) ){
35911                /* Noop */
35912     }
35913   }
35914 #endif
35915   winLogIoerr(cnt);
35916 
35917   OSTRACE(("OPEN file=%p, name=%s, access=%lx, rc=%s\n", h, zUtf8Name,
35918            dwDesiredAccess, (h==INVALID_HANDLE_VALUE) ? "failed" : "ok"));
35919 
35920   if( h==INVALID_HANDLE_VALUE ){
35921     pFile->lastErrno = lastErrno;
35922     winLogError(SQLITE_CANTOPEN, pFile->lastErrno, "winOpen", zUtf8Name);
35923     sqlite3_free(zConverted);
35924     sqlite3_free(zTmpname);
35925     if( isReadWrite && !isExclusive ){
35926       return winOpen(pVfs, zName, id,
35927          ((flags|SQLITE_OPEN_READONLY) &
35928                      ~(SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE)),
35929          pOutFlags);
35930     }else{
35931       return SQLITE_CANTOPEN_BKPT;
35932     }
35933   }
35934 
35935   if( pOutFlags ){
35936     if( isReadWrite ){
35937       *pOutFlags = SQLITE_OPEN_READWRITE;
35938     }else{
35939       *pOutFlags = SQLITE_OPEN_READONLY;
35940     }
35941   }
35942 
35943   OSTRACE(("OPEN file=%p, name=%s, access=%lx, pOutFlags=%p, *pOutFlags=%d, "
35944            "rc=%s\n", h, zUtf8Name, dwDesiredAccess, pOutFlags, pOutFlags ?
35945            *pOutFlags : 0, (h==INVALID_HANDLE_VALUE) ? "failed" : "ok"));
35946 
35947 #if SQLITE_OS_WINCE
35948   if( isReadWrite && eType==SQLITE_OPEN_MAIN_DB
35949        && (rc = winceCreateLock(zName, pFile))!=SQLITE_OK
35950   ){
35951     osCloseHandle(h);
35952     sqlite3_free(zConverted);
35953     sqlite3_free(zTmpname);
35954     OSTRACE(("OPEN-CE-LOCK name=%s, rc=%s\n", zName, sqlite3ErrName(rc)));
35955     return rc;
35956   }
35957   if( isTemp ){
35958     pFile->zDeleteOnClose = zConverted;
35959   }else
35960 #endif
35961   {
35962     sqlite3_free(zConverted);
35963   }
35964 
35965   sqlite3_free(zTmpname);
35966   pFile->pMethod = &winIoMethod;
35967   pFile->pVfs = pVfs;
35968   pFile->h = h;
35969   if( isReadonly ){
35970     pFile->ctrlFlags |= WINFILE_RDONLY;
35971   }
35972   if( sqlite3_uri_boolean(zName, "psow", SQLITE_POWERSAFE_OVERWRITE) ){
35973     pFile->ctrlFlags |= WINFILE_PSOW;
35974   }
35975   pFile->lastErrno = NO_ERROR;
35976   pFile->zPath = zName;
35977 #if SQLITE_MAX_MMAP_SIZE>0
35978   pFile->hMap = NULL;
35979   pFile->pMapRegion = 0;
35980   pFile->mmapSize = 0;
35981   pFile->mmapSizeActual = 0;
35982   pFile->mmapSizeMax = sqlite3GlobalConfig.szMmap;
35983 #endif
35984 
35985   OpenCounter(+1);
35986   return rc;
35987 }
35988 
35989 /*
35990 ** Delete the named file.
35991 **
35992 ** Note that Windows does not allow a file to be deleted if some other
35993 ** process has it open.  Sometimes a virus scanner or indexing program
35994 ** will open a journal file shortly after it is created in order to do
35995 ** whatever it does.  While this other process is holding the
35996 ** file open, we will be unable to delete it.  To work around this
35997 ** problem, we delay 100 milliseconds and try to delete again.  Up
35998 ** to MX_DELETION_ATTEMPTs deletion attempts are run before giving
35999 ** up and returning an error.
36000 */
36001 static int winDelete(
36002   sqlite3_vfs *pVfs,          /* Not used on win32 */
36003   const char *zFilename,      /* Name of file to delete */
36004   int syncDir                 /* Not used on win32 */
36005 ){
36006   int cnt = 0;
36007   int rc;
36008   DWORD attr;
36009   DWORD lastErrno = 0;
36010   void *zConverted;
36011   UNUSED_PARAMETER(pVfs);
36012   UNUSED_PARAMETER(syncDir);
36013 
36014   SimulateIOError(return SQLITE_IOERR_DELETE);
36015   OSTRACE(("DELETE name=%s, syncDir=%d\n", zFilename, syncDir));
36016 
36017   zConverted = winConvertFromUtf8Filename(zFilename);
36018   if( zConverted==0 ){
36019     OSTRACE(("DELETE name=%s, rc=SQLITE_IOERR_NOMEM\n", zFilename));
36020     return SQLITE_IOERR_NOMEM;
36021   }
36022   if( osIsNT() ){
36023     do {
36024 #if SQLITE_OS_WINRT
36025       WIN32_FILE_ATTRIBUTE_DATA sAttrData;
36026       memset(&sAttrData, 0, sizeof(sAttrData));
36027       if ( osGetFileAttributesExW(zConverted, GetFileExInfoStandard,
36028                                   &sAttrData) ){
36029         attr = sAttrData.dwFileAttributes;
36030       }else{
36031         lastErrno = osGetLastError();
36032         if( lastErrno==ERROR_FILE_NOT_FOUND
36033          || lastErrno==ERROR_PATH_NOT_FOUND ){
36034           rc = SQLITE_IOERR_DELETE_NOENT; /* Already gone? */
36035         }else{
36036           rc = SQLITE_ERROR;
36037         }
36038         break;
36039       }
36040 #else
36041       attr = osGetFileAttributesW(zConverted);
36042 #endif
36043       if ( attr==INVALID_FILE_ATTRIBUTES ){
36044         lastErrno = osGetLastError();
36045         if( lastErrno==ERROR_FILE_NOT_FOUND
36046          || lastErrno==ERROR_PATH_NOT_FOUND ){
36047           rc = SQLITE_IOERR_DELETE_NOENT; /* Already gone? */
36048         }else{
36049           rc = SQLITE_ERROR;
36050         }
36051         break;
36052       }
36053       if ( attr&FILE_ATTRIBUTE_DIRECTORY ){
36054         rc = SQLITE_ERROR; /* Files only. */
36055         break;
36056       }
36057       if ( osDeleteFileW(zConverted) ){
36058         rc = SQLITE_OK; /* Deleted OK. */
36059         break;
36060       }
36061       if ( !winRetryIoerr(&cnt, &lastErrno) ){
36062         rc = SQLITE_ERROR; /* No more retries. */
36063         break;
36064       }
36065     } while(1);
36066   }
36067 #ifdef SQLITE_WIN32_HAS_ANSI
36068   else{
36069     do {
36070       attr = osGetFileAttributesA(zConverted);
36071       if ( attr==INVALID_FILE_ATTRIBUTES ){
36072         lastErrno = osGetLastError();
36073         if( lastErrno==ERROR_FILE_NOT_FOUND
36074          || lastErrno==ERROR_PATH_NOT_FOUND ){
36075           rc = SQLITE_IOERR_DELETE_NOENT; /* Already gone? */
36076         }else{
36077           rc = SQLITE_ERROR;
36078         }
36079         break;
36080       }
36081       if ( attr&FILE_ATTRIBUTE_DIRECTORY ){
36082         rc = SQLITE_ERROR; /* Files only. */
36083         break;
36084       }
36085       if ( osDeleteFileA(zConverted) ){
36086         rc = SQLITE_OK; /* Deleted OK. */
36087         break;
36088       }
36089       if ( !winRetryIoerr(&cnt, &lastErrno) ){
36090         rc = SQLITE_ERROR; /* No more retries. */
36091         break;
36092       }
36093     } while(1);
36094   }
36095 #endif
36096   if( rc && rc!=SQLITE_IOERR_DELETE_NOENT ){
36097     rc = winLogError(SQLITE_IOERR_DELETE, lastErrno, "winDelete", zFilename);
36098   }else{
36099     winLogIoerr(cnt);
36100   }
36101   sqlite3_free(zConverted);
36102   OSTRACE(("DELETE name=%s, rc=%s\n", zFilename, sqlite3ErrName(rc)));
36103   return rc;
36104 }
36105 
36106 /*
36107 ** Check the existence and status of a file.
36108 */
36109 static int winAccess(
36110   sqlite3_vfs *pVfs,         /* Not used on win32 */
36111   const char *zFilename,     /* Name of file to check */
36112   int flags,                 /* Type of test to make on this file */
36113   int *pResOut               /* OUT: Result */
36114 ){
36115   DWORD attr;
36116   int rc = 0;
36117   DWORD lastErrno = 0;
36118   void *zConverted;
36119   UNUSED_PARAMETER(pVfs);
36120 
36121   SimulateIOError( return SQLITE_IOERR_ACCESS; );
36122   OSTRACE(("ACCESS name=%s, flags=%x, pResOut=%p\n",
36123            zFilename, flags, pResOut));
36124 
36125   zConverted = winConvertFromUtf8Filename(zFilename);
36126   if( zConverted==0 ){
36127     OSTRACE(("ACCESS name=%s, rc=SQLITE_IOERR_NOMEM\n", zFilename));
36128     return SQLITE_IOERR_NOMEM;
36129   }
36130   if( osIsNT() ){
36131     int cnt = 0;
36132     WIN32_FILE_ATTRIBUTE_DATA sAttrData;
36133     memset(&sAttrData, 0, sizeof(sAttrData));
36134     while( !(rc = osGetFileAttributesExW((LPCWSTR)zConverted,
36135                              GetFileExInfoStandard,
36136                              &sAttrData)) && winRetryIoerr(&cnt, &lastErrno) ){}
36137     if( rc ){
36138       /* For an SQLITE_ACCESS_EXISTS query, treat a zero-length file
36139       ** as if it does not exist.
36140       */
36141       if(    flags==SQLITE_ACCESS_EXISTS
36142           && sAttrData.nFileSizeHigh==0
36143           && sAttrData.nFileSizeLow==0 ){
36144         attr = INVALID_FILE_ATTRIBUTES;
36145       }else{
36146         attr = sAttrData.dwFileAttributes;
36147       }
36148     }else{
36149       winLogIoerr(cnt);
36150       if( lastErrno!=ERROR_FILE_NOT_FOUND && lastErrno!=ERROR_PATH_NOT_FOUND ){
36151         sqlite3_free(zConverted);
36152         return winLogError(SQLITE_IOERR_ACCESS, lastErrno, "winAccess",
36153                            zFilename);
36154       }else{
36155         attr = INVALID_FILE_ATTRIBUTES;
36156       }
36157     }
36158   }
36159 #ifdef SQLITE_WIN32_HAS_ANSI
36160   else{
36161     attr = osGetFileAttributesA((char*)zConverted);
36162   }
36163 #endif
36164   sqlite3_free(zConverted);
36165   switch( flags ){
36166     case SQLITE_ACCESS_READ:
36167     case SQLITE_ACCESS_EXISTS:
36168       rc = attr!=INVALID_FILE_ATTRIBUTES;
36169       break;
36170     case SQLITE_ACCESS_READWRITE:
36171       rc = attr!=INVALID_FILE_ATTRIBUTES &&
36172              (attr & FILE_ATTRIBUTE_READONLY)==0;
36173       break;
36174     default:
36175       assert(!"Invalid flags argument");
36176   }
36177   *pResOut = rc;
36178   OSTRACE(("ACCESS name=%s, pResOut=%p, *pResOut=%d, rc=SQLITE_OK\n",
36179            zFilename, pResOut, *pResOut));
36180   return SQLITE_OK;
36181 }
36182 
36183 /*
36184 ** Returns non-zero if the specified path name starts with a drive letter
36185 ** followed by a colon character.
36186 */
36187 static BOOL winIsDriveLetterAndColon(
36188   const char *zPathname
36189 ){
36190   return ( sqlite3Isalpha(zPathname[0]) && zPathname[1]==':' );
36191 }
36192 
36193 /*
36194 ** Returns non-zero if the specified path name should be used verbatim.  If
36195 ** non-zero is returned from this function, the calling function must simply
36196 ** use the provided path name verbatim -OR- resolve it into a full path name
36197 ** using the GetFullPathName Win32 API function (if available).
36198 */
36199 static BOOL winIsVerbatimPathname(
36200   const char *zPathname
36201 ){
36202   /*
36203   ** If the path name starts with a forward slash or a backslash, it is either
36204   ** a legal UNC name, a volume relative path, or an absolute path name in the
36205   ** "Unix" format on Windows.  There is no easy way to differentiate between
36206   ** the final two cases; therefore, we return the safer return value of TRUE
36207   ** so that callers of this function will simply use it verbatim.
36208   */
36209   if ( winIsDirSep(zPathname[0]) ){
36210     return TRUE;
36211   }
36212 
36213   /*
36214   ** If the path name starts with a letter and a colon it is either a volume
36215   ** relative path or an absolute path.  Callers of this function must not
36216   ** attempt to treat it as a relative path name (i.e. they should simply use
36217   ** it verbatim).
36218   */
36219   if ( winIsDriveLetterAndColon(zPathname) ){
36220     return TRUE;
36221   }
36222 
36223   /*
36224   ** If we get to this point, the path name should almost certainly be a purely
36225   ** relative one (i.e. not a UNC name, not absolute, and not volume relative).
36226   */
36227   return FALSE;
36228 }
36229 
36230 /*
36231 ** Turn a relative pathname into a full pathname.  Write the full
36232 ** pathname into zOut[].  zOut[] will be at least pVfs->mxPathname
36233 ** bytes in size.
36234 */
36235 static int winFullPathname(
36236   sqlite3_vfs *pVfs,            /* Pointer to vfs object */
36237   const char *zRelative,        /* Possibly relative input path */
36238   int nFull,                    /* Size of output buffer in bytes */
36239   char *zFull                   /* Output buffer */
36240 ){
36241 
36242 #if defined(__CYGWIN__)
36243   SimulateIOError( return SQLITE_ERROR );
36244   UNUSED_PARAMETER(nFull);
36245   assert( nFull>=pVfs->mxPathname );
36246   if ( sqlite3_data_directory && !winIsVerbatimPathname(zRelative) ){
36247     /*
36248     ** NOTE: We are dealing with a relative path name and the data
36249     **       directory has been set.  Therefore, use it as the basis
36250     **       for converting the relative path name to an absolute
36251     **       one by prepending the data directory and a slash.
36252     */
36253     char *zOut = sqlite3MallocZero( pVfs->mxPathname+1 );
36254     if( !zOut ){
36255       return SQLITE_IOERR_NOMEM;
36256     }
36257     if( cygwin_conv_path(
36258             (osIsNT() ? CCP_POSIX_TO_WIN_W : CCP_POSIX_TO_WIN_A) |
36259             CCP_RELATIVE, zRelative, zOut, pVfs->mxPathname+1)<0 ){
36260       sqlite3_free(zOut);
36261       return winLogError(SQLITE_CANTOPEN_CONVPATH, (DWORD)errno,
36262                          "winFullPathname1", zRelative);
36263     }else{
36264       char *zUtf8 = winConvertToUtf8Filename(zOut);
36265       if( !zUtf8 ){
36266         sqlite3_free(zOut);
36267         return SQLITE_IOERR_NOMEM;
36268       }
36269       sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%c%s",
36270                        sqlite3_data_directory, winGetDirSep(), zUtf8);
36271       sqlite3_free(zUtf8);
36272       sqlite3_free(zOut);
36273     }
36274   }else{
36275     char *zOut = sqlite3MallocZero( pVfs->mxPathname+1 );
36276     if( !zOut ){
36277       return SQLITE_IOERR_NOMEM;
36278     }
36279     if( cygwin_conv_path(
36280             (osIsNT() ? CCP_POSIX_TO_WIN_W : CCP_POSIX_TO_WIN_A),
36281             zRelative, zOut, pVfs->mxPathname+1)<0 ){
36282       sqlite3_free(zOut);
36283       return winLogError(SQLITE_CANTOPEN_CONVPATH, (DWORD)errno,
36284                          "winFullPathname2", zRelative);
36285     }else{
36286       char *zUtf8 = winConvertToUtf8Filename(zOut);
36287       if( !zUtf8 ){
36288         sqlite3_free(zOut);
36289         return SQLITE_IOERR_NOMEM;
36290       }
36291       sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zUtf8);
36292       sqlite3_free(zUtf8);
36293       sqlite3_free(zOut);
36294     }
36295   }
36296   return SQLITE_OK;
36297 #endif
36298 
36299 #if (SQLITE_OS_WINCE || SQLITE_OS_WINRT) && !defined(__CYGWIN__)
36300   SimulateIOError( return SQLITE_ERROR );
36301   /* WinCE has no concept of a relative pathname, or so I am told. */
36302   /* WinRT has no way to convert a relative path to an absolute one. */
36303   if ( sqlite3_data_directory && !winIsVerbatimPathname(zRelative) ){
36304     /*
36305     ** NOTE: We are dealing with a relative path name and the data
36306     **       directory has been set.  Therefore, use it as the basis
36307     **       for converting the relative path name to an absolute
36308     **       one by prepending the data directory and a backslash.
36309     */
36310     sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%c%s",
36311                      sqlite3_data_directory, winGetDirSep(), zRelative);
36312   }else{
36313     sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zRelative);
36314   }
36315   return SQLITE_OK;
36316 #endif
36317 
36318 #if !SQLITE_OS_WINCE && !SQLITE_OS_WINRT && !defined(__CYGWIN__)
36319   DWORD nByte;
36320   void *zConverted;
36321   char *zOut;
36322 
36323   /* If this path name begins with "/X:", where "X" is any alphabetic
36324   ** character, discard the initial "/" from the pathname.
36325   */
36326   if( zRelative[0]=='/' && winIsDriveLetterAndColon(zRelative+1) ){
36327     zRelative++;
36328   }
36329 
36330   /* It's odd to simulate an io-error here, but really this is just
36331   ** using the io-error infrastructure to test that SQLite handles this
36332   ** function failing. This function could fail if, for example, the
36333   ** current working directory has been unlinked.
36334   */
36335   SimulateIOError( return SQLITE_ERROR );
36336   if ( sqlite3_data_directory && !winIsVerbatimPathname(zRelative) ){
36337     /*
36338     ** NOTE: We are dealing with a relative path name and the data
36339     **       directory has been set.  Therefore, use it as the basis
36340     **       for converting the relative path name to an absolute
36341     **       one by prepending the data directory and a backslash.
36342     */
36343     sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s%c%s",
36344                      sqlite3_data_directory, winGetDirSep(), zRelative);
36345     return SQLITE_OK;
36346   }
36347   zConverted = winConvertFromUtf8Filename(zRelative);
36348   if( zConverted==0 ){
36349     return SQLITE_IOERR_NOMEM;
36350   }
36351   if( osIsNT() ){
36352     LPWSTR zTemp;
36353     nByte = osGetFullPathNameW((LPCWSTR)zConverted, 0, 0, 0);
36354     if( nByte==0 ){
36355       sqlite3_free(zConverted);
36356       return winLogError(SQLITE_CANTOPEN_FULLPATH, osGetLastError(),
36357                          "winFullPathname1", zRelative);
36358     }
36359     nByte += 3;
36360     zTemp = sqlite3MallocZero( nByte*sizeof(zTemp[0]) );
36361     if( zTemp==0 ){
36362       sqlite3_free(zConverted);
36363       return SQLITE_IOERR_NOMEM;
36364     }
36365     nByte = osGetFullPathNameW((LPCWSTR)zConverted, nByte, zTemp, 0);
36366     if( nByte==0 ){
36367       sqlite3_free(zConverted);
36368       sqlite3_free(zTemp);
36369       return winLogError(SQLITE_CANTOPEN_FULLPATH, osGetLastError(),
36370                          "winFullPathname2", zRelative);
36371     }
36372     sqlite3_free(zConverted);
36373     zOut = winUnicodeToUtf8(zTemp);
36374     sqlite3_free(zTemp);
36375   }
36376 #ifdef SQLITE_WIN32_HAS_ANSI
36377   else{
36378     char *zTemp;
36379     nByte = osGetFullPathNameA((char*)zConverted, 0, 0, 0);
36380     if( nByte==0 ){
36381       sqlite3_free(zConverted);
36382       return winLogError(SQLITE_CANTOPEN_FULLPATH, osGetLastError(),
36383                          "winFullPathname3", zRelative);
36384     }
36385     nByte += 3;
36386     zTemp = sqlite3MallocZero( nByte*sizeof(zTemp[0]) );
36387     if( zTemp==0 ){
36388       sqlite3_free(zConverted);
36389       return SQLITE_IOERR_NOMEM;
36390     }
36391     nByte = osGetFullPathNameA((char*)zConverted, nByte, zTemp, 0);
36392     if( nByte==0 ){
36393       sqlite3_free(zConverted);
36394       sqlite3_free(zTemp);
36395       return winLogError(SQLITE_CANTOPEN_FULLPATH, osGetLastError(),
36396                          "winFullPathname4", zRelative);
36397     }
36398     sqlite3_free(zConverted);
36399     zOut = sqlite3_win32_mbcs_to_utf8(zTemp);
36400     sqlite3_free(zTemp);
36401   }
36402 #endif
36403   if( zOut ){
36404     sqlite3_snprintf(MIN(nFull, pVfs->mxPathname), zFull, "%s", zOut);
36405     sqlite3_free(zOut);
36406     return SQLITE_OK;
36407   }else{
36408     return SQLITE_IOERR_NOMEM;
36409   }
36410 #endif
36411 }
36412 
36413 #ifndef SQLITE_OMIT_LOAD_EXTENSION
36414 /*
36415 ** Interfaces for opening a shared library, finding entry points
36416 ** within the shared library, and closing the shared library.
36417 */
36418 /*
36419 ** Interfaces for opening a shared library, finding entry points
36420 ** within the shared library, and closing the shared library.
36421 */
36422 static void *winDlOpen(sqlite3_vfs *pVfs, const char *zFilename){
36423   HANDLE h;
36424   void *zConverted = winConvertFromUtf8Filename(zFilename);
36425   UNUSED_PARAMETER(pVfs);
36426   if( zConverted==0 ){
36427     return 0;
36428   }
36429   if( osIsNT() ){
36430 #if SQLITE_OS_WINRT
36431     h = osLoadPackagedLibrary((LPCWSTR)zConverted, 0);
36432 #else
36433     h = osLoadLibraryW((LPCWSTR)zConverted);
36434 #endif
36435   }
36436 #ifdef SQLITE_WIN32_HAS_ANSI
36437   else{
36438     h = osLoadLibraryA((char*)zConverted);
36439   }
36440 #endif
36441   sqlite3_free(zConverted);
36442   return (void*)h;
36443 }
36444 static void winDlError(sqlite3_vfs *pVfs, int nBuf, char *zBufOut){
36445   UNUSED_PARAMETER(pVfs);
36446   winGetLastErrorMsg(osGetLastError(), nBuf, zBufOut);
36447 }
36448 static void (*winDlSym(sqlite3_vfs *pVfs,void *pH,const char *zSym))(void){
36449   UNUSED_PARAMETER(pVfs);
36450   return (void(*)(void))osGetProcAddressA((HANDLE)pH, zSym);
36451 }
36452 static void winDlClose(sqlite3_vfs *pVfs, void *pHandle){
36453   UNUSED_PARAMETER(pVfs);
36454   osFreeLibrary((HANDLE)pHandle);
36455 }
36456 #else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */
36457   #define winDlOpen  0
36458   #define winDlError 0
36459   #define winDlSym   0
36460   #define winDlClose 0
36461 #endif
36462 
36463 
36464 /*
36465 ** Write up to nBuf bytes of randomness into zBuf.
36466 */
36467 static int winRandomness(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
36468   int n = 0;
36469   UNUSED_PARAMETER(pVfs);
36470 #if defined(SQLITE_TEST)
36471   n = nBuf;
36472   memset(zBuf, 0, nBuf);
36473 #else
36474   if( sizeof(SYSTEMTIME)<=nBuf-n ){
36475     SYSTEMTIME x;
36476     osGetSystemTime(&x);
36477     memcpy(&zBuf[n], &x, sizeof(x));
36478     n += sizeof(x);
36479   }
36480   if( sizeof(DWORD)<=nBuf-n ){
36481     DWORD pid = osGetCurrentProcessId();
36482     memcpy(&zBuf[n], &pid, sizeof(pid));
36483     n += sizeof(pid);
36484   }
36485 #if SQLITE_OS_WINRT
36486   if( sizeof(ULONGLONG)<=nBuf-n ){
36487     ULONGLONG cnt = osGetTickCount64();
36488     memcpy(&zBuf[n], &cnt, sizeof(cnt));
36489     n += sizeof(cnt);
36490   }
36491 #else
36492   if( sizeof(DWORD)<=nBuf-n ){
36493     DWORD cnt = osGetTickCount();
36494     memcpy(&zBuf[n], &cnt, sizeof(cnt));
36495     n += sizeof(cnt);
36496   }
36497 #endif
36498   if( sizeof(LARGE_INTEGER)<=nBuf-n ){
36499     LARGE_INTEGER i;
36500     osQueryPerformanceCounter(&i);
36501     memcpy(&zBuf[n], &i, sizeof(i));
36502     n += sizeof(i);
36503   }
36504 #endif
36505   return n;
36506 }
36507 
36508 
36509 /*
36510 ** Sleep for a little while.  Return the amount of time slept.
36511 */
36512 static int winSleep(sqlite3_vfs *pVfs, int microsec){
36513   sqlite3_win32_sleep((microsec+999)/1000);
36514   UNUSED_PARAMETER(pVfs);
36515   return ((microsec+999)/1000)*1000;
36516 }
36517 
36518 /*
36519 ** The following variable, if set to a non-zero value, is interpreted as
36520 ** the number of seconds since 1970 and is used to set the result of
36521 ** sqlite3OsCurrentTime() during testing.
36522 */
36523 #ifdef SQLITE_TEST
36524 SQLITE_API int sqlite3_current_time = 0;  /* Fake system time in seconds since 1970. */
36525 #endif
36526 
36527 /*
36528 ** Find the current time (in Universal Coordinated Time).  Write into *piNow
36529 ** the current time and date as a Julian Day number times 86_400_000.  In
36530 ** other words, write into *piNow the number of milliseconds since the Julian
36531 ** epoch of noon in Greenwich on November 24, 4714 B.C according to the
36532 ** proleptic Gregorian calendar.
36533 **
36534 ** On success, return SQLITE_OK.  Return SQLITE_ERROR if the time and date
36535 ** cannot be found.
36536 */
36537 static int winCurrentTimeInt64(sqlite3_vfs *pVfs, sqlite3_int64 *piNow){
36538   /* FILETIME structure is a 64-bit value representing the number of
36539      100-nanosecond intervals since January 1, 1601 (= JD 2305813.5).
36540   */
36541   FILETIME ft;
36542   static const sqlite3_int64 winFiletimeEpoch = 23058135*(sqlite3_int64)8640000;
36543 #ifdef SQLITE_TEST
36544   static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000;
36545 #endif
36546   /* 2^32 - to avoid use of LL and warnings in gcc */
36547   static const sqlite3_int64 max32BitValue =
36548       (sqlite3_int64)2000000000 + (sqlite3_int64)2000000000 +
36549       (sqlite3_int64)294967296;
36550 
36551 #if SQLITE_OS_WINCE
36552   SYSTEMTIME time;
36553   osGetSystemTime(&time);
36554   /* if SystemTimeToFileTime() fails, it returns zero. */
36555   if (!osSystemTimeToFileTime(&time,&ft)){
36556     return SQLITE_ERROR;
36557   }
36558 #else
36559   osGetSystemTimeAsFileTime( &ft );
36560 #endif
36561 
36562   *piNow = winFiletimeEpoch +
36563             ((((sqlite3_int64)ft.dwHighDateTime)*max32BitValue) +
36564                (sqlite3_int64)ft.dwLowDateTime)/(sqlite3_int64)10000;
36565 
36566 #ifdef SQLITE_TEST
36567   if( sqlite3_current_time ){
36568     *piNow = 1000*(sqlite3_int64)sqlite3_current_time + unixEpoch;
36569   }
36570 #endif
36571   UNUSED_PARAMETER(pVfs);
36572   return SQLITE_OK;
36573 }
36574 
36575 /*
36576 ** Find the current time (in Universal Coordinated Time).  Write the
36577 ** current time and date as a Julian Day number into *prNow and
36578 ** return 0.  Return 1 if the time and date cannot be found.
36579 */
36580 static int winCurrentTime(sqlite3_vfs *pVfs, double *prNow){
36581   int rc;
36582   sqlite3_int64 i;
36583   rc = winCurrentTimeInt64(pVfs, &i);
36584   if( !rc ){
36585     *prNow = i/86400000.0;
36586   }
36587   return rc;
36588 }
36589 
36590 /*
36591 ** The idea is that this function works like a combination of
36592 ** GetLastError() and FormatMessage() on Windows (or errno and
36593 ** strerror_r() on Unix). After an error is returned by an OS
36594 ** function, SQLite calls this function with zBuf pointing to
36595 ** a buffer of nBuf bytes. The OS layer should populate the
36596 ** buffer with a nul-terminated UTF-8 encoded error message
36597 ** describing the last IO error to have occurred within the calling
36598 ** thread.
36599 **
36600 ** If the error message is too large for the supplied buffer,
36601 ** it should be truncated. The return value of xGetLastError
36602 ** is zero if the error message fits in the buffer, or non-zero
36603 ** otherwise (if the message was truncated). If non-zero is returned,
36604 ** then it is not necessary to include the nul-terminator character
36605 ** in the output buffer.
36606 **
36607 ** Not supplying an error message will have no adverse effect
36608 ** on SQLite. It is fine to have an implementation that never
36609 ** returns an error message:
36610 **
36611 **   int xGetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
36612 **     assert(zBuf[0]=='\0');
36613 **     return 0;
36614 **   }
36615 **
36616 ** However if an error message is supplied, it will be incorporated
36617 ** by sqlite into the error message available to the user using
36618 ** sqlite3_errmsg(), possibly making IO errors easier to debug.
36619 */
36620 static int winGetLastError(sqlite3_vfs *pVfs, int nBuf, char *zBuf){
36621   UNUSED_PARAMETER(pVfs);
36622   return winGetLastErrorMsg(osGetLastError(), nBuf, zBuf);
36623 }
36624 
36625 /*
36626 ** Initialize and deinitialize the operating system interface.
36627 */
36628 SQLITE_API int sqlite3_os_init(void){
36629   static sqlite3_vfs winVfs = {
36630     3,                   /* iVersion */
36631     sizeof(winFile),     /* szOsFile */
36632     SQLITE_WIN32_MAX_PATH_BYTES, /* mxPathname */
36633     0,                   /* pNext */
36634     "win32",             /* zName */
36635     0,                   /* pAppData */
36636     winOpen,             /* xOpen */
36637     winDelete,           /* xDelete */
36638     winAccess,           /* xAccess */
36639     winFullPathname,     /* xFullPathname */
36640     winDlOpen,           /* xDlOpen */
36641     winDlError,          /* xDlError */
36642     winDlSym,            /* xDlSym */
36643     winDlClose,          /* xDlClose */
36644     winRandomness,       /* xRandomness */
36645     winSleep,            /* xSleep */
36646     winCurrentTime,      /* xCurrentTime */
36647     winGetLastError,     /* xGetLastError */
36648     winCurrentTimeInt64, /* xCurrentTimeInt64 */
36649     winSetSystemCall,    /* xSetSystemCall */
36650     winGetSystemCall,    /* xGetSystemCall */
36651     winNextSystemCall,   /* xNextSystemCall */
36652   };
36653 #if defined(SQLITE_WIN32_HAS_WIDE)
36654   static sqlite3_vfs winLongPathVfs = {
36655     3,                   /* iVersion */
36656     sizeof(winFile),     /* szOsFile */
36657     SQLITE_WINNT_MAX_PATH_BYTES, /* mxPathname */
36658     0,                   /* pNext */
36659     "win32-longpath",    /* zName */
36660     0,                   /* pAppData */
36661     winOpen,             /* xOpen */
36662     winDelete,           /* xDelete */
36663     winAccess,           /* xAccess */
36664     winFullPathname,     /* xFullPathname */
36665     winDlOpen,           /* xDlOpen */
36666     winDlError,          /* xDlError */
36667     winDlSym,            /* xDlSym */
36668     winDlClose,          /* xDlClose */
36669     winRandomness,       /* xRandomness */
36670     winSleep,            /* xSleep */
36671     winCurrentTime,      /* xCurrentTime */
36672     winGetLastError,     /* xGetLastError */
36673     winCurrentTimeInt64, /* xCurrentTimeInt64 */
36674     winSetSystemCall,    /* xSetSystemCall */
36675     winGetSystemCall,    /* xGetSystemCall */
36676     winNextSystemCall,   /* xNextSystemCall */
36677   };
36678 #endif
36679 
36680   /* Double-check that the aSyscall[] array has been constructed
36681   ** correctly.  See ticket [bb3a86e890c8e96ab] */
36682   assert( ArraySize(aSyscall)==76 );
36683 
36684   /* get memory map allocation granularity */
36685   memset(&winSysInfo, 0, sizeof(SYSTEM_INFO));
36686 #if SQLITE_OS_WINRT
36687   osGetNativeSystemInfo(&winSysInfo);
36688 #else
36689   osGetSystemInfo(&winSysInfo);
36690 #endif
36691   assert( winSysInfo.dwAllocationGranularity>0 );
36692   assert( winSysInfo.dwPageSize>0 );
36693 
36694   sqlite3_vfs_register(&winVfs, 1);
36695 
36696 #if defined(SQLITE_WIN32_HAS_WIDE)
36697   sqlite3_vfs_register(&winLongPathVfs, 0);
36698 #endif
36699 
36700   return SQLITE_OK;
36701 }
36702 
36703 SQLITE_API int sqlite3_os_end(void){
36704 #if SQLITE_OS_WINRT
36705   if( sleepObj!=NULL ){
36706     osCloseHandle(sleepObj);
36707     sleepObj = NULL;
36708   }
36709 #endif
36710   return SQLITE_OK;
36711 }
36712 
36713 #endif /* SQLITE_OS_WIN */
36714 
36715 /************** End of os_win.c **********************************************/
36716 /************** Begin file bitvec.c ******************************************/
36717 /*
36718 ** 2008 February 16
36719 **
36720 ** The author disclaims copyright to this source code.  In place of
36721 ** a legal notice, here is a blessing:
36722 **
36723 **    May you do good and not evil.
36724 **    May you find forgiveness for yourself and forgive others.
36725 **    May you share freely, never taking more than you give.
36726 **
36727 *************************************************************************
36728 ** This file implements an object that represents a fixed-length
36729 ** bitmap.  Bits are numbered starting with 1.
36730 **
36731 ** A bitmap is used to record which pages of a database file have been
36732 ** journalled during a transaction, or which pages have the "dont-write"
36733 ** property.  Usually only a few pages are meet either condition.
36734 ** So the bitmap is usually sparse and has low cardinality.
36735 ** But sometimes (for example when during a DROP of a large table) most
36736 ** or all of the pages in a database can get journalled.  In those cases,
36737 ** the bitmap becomes dense with high cardinality.  The algorithm needs
36738 ** to handle both cases well.
36739 **
36740 ** The size of the bitmap is fixed when the object is created.
36741 **
36742 ** All bits are clear when the bitmap is created.  Individual bits
36743 ** may be set or cleared one at a time.
36744 **
36745 ** Test operations are about 100 times more common that set operations.
36746 ** Clear operations are exceedingly rare.  There are usually between
36747 ** 5 and 500 set operations per Bitvec object, though the number of sets can
36748 ** sometimes grow into tens of thousands or larger.  The size of the
36749 ** Bitvec object is the number of pages in the database file at the
36750 ** start of a transaction, and is thus usually less than a few thousand,
36751 ** but can be as large as 2 billion for a really big database.
36752 */
36753 
36754 /* Size of the Bitvec structure in bytes. */
36755 #define BITVEC_SZ        512
36756 
36757 /* Round the union size down to the nearest pointer boundary, since that's how
36758 ** it will be aligned within the Bitvec struct. */
36759 #define BITVEC_USIZE     (((BITVEC_SZ-(3*sizeof(u32)))/sizeof(Bitvec*))*sizeof(Bitvec*))
36760 
36761 /* Type of the array "element" for the bitmap representation.
36762 ** Should be a power of 2, and ideally, evenly divide into BITVEC_USIZE.
36763 ** Setting this to the "natural word" size of your CPU may improve
36764 ** performance. */
36765 #define BITVEC_TELEM     u8
36766 /* Size, in bits, of the bitmap element. */
36767 #define BITVEC_SZELEM    8
36768 /* Number of elements in a bitmap array. */
36769 #define BITVEC_NELEM     (BITVEC_USIZE/sizeof(BITVEC_TELEM))
36770 /* Number of bits in the bitmap array. */
36771 #define BITVEC_NBIT      (BITVEC_NELEM*BITVEC_SZELEM)
36772 
36773 /* Number of u32 values in hash table. */
36774 #define BITVEC_NINT      (BITVEC_USIZE/sizeof(u32))
36775 /* Maximum number of entries in hash table before
36776 ** sub-dividing and re-hashing. */
36777 #define BITVEC_MXHASH    (BITVEC_NINT/2)
36778 /* Hashing function for the aHash representation.
36779 ** Empirical testing showed that the *37 multiplier
36780 ** (an arbitrary prime)in the hash function provided
36781 ** no fewer collisions than the no-op *1. */
36782 #define BITVEC_HASH(X)   (((X)*1)%BITVEC_NINT)
36783 
36784 #define BITVEC_NPTR      (BITVEC_USIZE/sizeof(Bitvec *))
36785 
36786 
36787 /*
36788 ** A bitmap is an instance of the following structure.
36789 **
36790 ** This bitmap records the existence of zero or more bits
36791 ** with values between 1 and iSize, inclusive.
36792 **
36793 ** There are three possible representations of the bitmap.
36794 ** If iSize<=BITVEC_NBIT, then Bitvec.u.aBitmap[] is a straight
36795 ** bitmap.  The least significant bit is bit 1.
36796 **
36797 ** If iSize>BITVEC_NBIT and iDivisor==0 then Bitvec.u.aHash[] is
36798 ** a hash table that will hold up to BITVEC_MXHASH distinct values.
36799 **
36800 ** Otherwise, the value i is redirected into one of BITVEC_NPTR
36801 ** sub-bitmaps pointed to by Bitvec.u.apSub[].  Each subbitmap
36802 ** handles up to iDivisor separate values of i.  apSub[0] holds
36803 ** values between 1 and iDivisor.  apSub[1] holds values between
36804 ** iDivisor+1 and 2*iDivisor.  apSub[N] holds values between
36805 ** N*iDivisor+1 and (N+1)*iDivisor.  Each subbitmap is normalized
36806 ** to hold deal with values between 1 and iDivisor.
36807 */
36808 struct Bitvec {
36809   u32 iSize;      /* Maximum bit index.  Max iSize is 4,294,967,296. */
36810   u32 nSet;       /* Number of bits that are set - only valid for aHash
36811                   ** element.  Max is BITVEC_NINT.  For BITVEC_SZ of 512,
36812                   ** this would be 125. */
36813   u32 iDivisor;   /* Number of bits handled by each apSub[] entry. */
36814                   /* Should >=0 for apSub element. */
36815                   /* Max iDivisor is max(u32) / BITVEC_NPTR + 1.  */
36816                   /* For a BITVEC_SZ of 512, this would be 34,359,739. */
36817   union {
36818     BITVEC_TELEM aBitmap[BITVEC_NELEM];    /* Bitmap representation */
36819     u32 aHash[BITVEC_NINT];      /* Hash table representation */
36820     Bitvec *apSub[BITVEC_NPTR];  /* Recursive representation */
36821   } u;
36822 };
36823 
36824 /*
36825 ** Create a new bitmap object able to handle bits between 0 and iSize,
36826 ** inclusive.  Return a pointer to the new object.  Return NULL if
36827 ** malloc fails.
36828 */
36829 SQLITE_PRIVATE Bitvec *sqlite3BitvecCreate(u32 iSize){
36830   Bitvec *p;
36831   assert( sizeof(*p)==BITVEC_SZ );
36832   p = sqlite3MallocZero( sizeof(*p) );
36833   if( p ){
36834     p->iSize = iSize;
36835   }
36836   return p;
36837 }
36838 
36839 /*
36840 ** Check to see if the i-th bit is set.  Return true or false.
36841 ** If p is NULL (if the bitmap has not been created) or if
36842 ** i is out of range, then return false.
36843 */
36844 SQLITE_PRIVATE int sqlite3BitvecTest(Bitvec *p, u32 i){
36845   if( p==0 ) return 0;
36846   if( i>p->iSize || i==0 ) return 0;
36847   i--;
36848   while( p->iDivisor ){
36849     u32 bin = i/p->iDivisor;
36850     i = i%p->iDivisor;
36851     p = p->u.apSub[bin];
36852     if (!p) {
36853       return 0;
36854     }
36855   }
36856   if( p->iSize<=BITVEC_NBIT ){
36857     return (p->u.aBitmap[i/BITVEC_SZELEM] & (1<<(i&(BITVEC_SZELEM-1))))!=0;
36858   } else{
36859     u32 h = BITVEC_HASH(i++);
36860     while( p->u.aHash[h] ){
36861       if( p->u.aHash[h]==i ) return 1;
36862       h = (h+1) % BITVEC_NINT;
36863     }
36864     return 0;
36865   }
36866 }
36867 
36868 /*
36869 ** Set the i-th bit.  Return 0 on success and an error code if
36870 ** anything goes wrong.
36871 **
36872 ** This routine might cause sub-bitmaps to be allocated.  Failing
36873 ** to get the memory needed to hold the sub-bitmap is the only
36874 ** that can go wrong with an insert, assuming p and i are valid.
36875 **
36876 ** The calling function must ensure that p is a valid Bitvec object
36877 ** and that the value for "i" is within range of the Bitvec object.
36878 ** Otherwise the behavior is undefined.
36879 */
36880 SQLITE_PRIVATE int sqlite3BitvecSet(Bitvec *p, u32 i){
36881   u32 h;
36882   if( p==0 ) return SQLITE_OK;
36883   assert( i>0 );
36884   assert( i<=p->iSize );
36885   i--;
36886   while((p->iSize > BITVEC_NBIT) && p->iDivisor) {
36887     u32 bin = i/p->iDivisor;
36888     i = i%p->iDivisor;
36889     if( p->u.apSub[bin]==0 ){
36890       p->u.apSub[bin] = sqlite3BitvecCreate( p->iDivisor );
36891       if( p->u.apSub[bin]==0 ) return SQLITE_NOMEM;
36892     }
36893     p = p->u.apSub[bin];
36894   }
36895   if( p->iSize<=BITVEC_NBIT ){
36896     p->u.aBitmap[i/BITVEC_SZELEM] |= 1 << (i&(BITVEC_SZELEM-1));
36897     return SQLITE_OK;
36898   }
36899   h = BITVEC_HASH(i++);
36900   /* if there wasn't a hash collision, and this doesn't */
36901   /* completely fill the hash, then just add it without */
36902   /* worring about sub-dividing and re-hashing. */
36903   if( !p->u.aHash[h] ){
36904     if (p->nSet<(BITVEC_NINT-1)) {
36905       goto bitvec_set_end;
36906     } else {
36907       goto bitvec_set_rehash;
36908     }
36909   }
36910   /* there was a collision, check to see if it's already */
36911   /* in hash, if not, try to find a spot for it */
36912   do {
36913     if( p->u.aHash[h]==i ) return SQLITE_OK;
36914     h++;
36915     if( h>=BITVEC_NINT ) h = 0;
36916   } while( p->u.aHash[h] );
36917   /* we didn't find it in the hash.  h points to the first */
36918   /* available free spot. check to see if this is going to */
36919   /* make our hash too "full".  */
36920 bitvec_set_rehash:
36921   if( p->nSet>=BITVEC_MXHASH ){
36922     unsigned int j;
36923     int rc;
36924     u32 *aiValues = sqlite3StackAllocRaw(0, sizeof(p->u.aHash));
36925     if( aiValues==0 ){
36926       return SQLITE_NOMEM;
36927     }else{
36928       memcpy(aiValues, p->u.aHash, sizeof(p->u.aHash));
36929       memset(p->u.apSub, 0, sizeof(p->u.apSub));
36930       p->iDivisor = (p->iSize + BITVEC_NPTR - 1)/BITVEC_NPTR;
36931       rc = sqlite3BitvecSet(p, i);
36932       for(j=0; j<BITVEC_NINT; j++){
36933         if( aiValues[j] ) rc |= sqlite3BitvecSet(p, aiValues[j]);
36934       }
36935       sqlite3StackFree(0, aiValues);
36936       return rc;
36937     }
36938   }
36939 bitvec_set_end:
36940   p->nSet++;
36941   p->u.aHash[h] = i;
36942   return SQLITE_OK;
36943 }
36944 
36945 /*
36946 ** Clear the i-th bit.
36947 **
36948 ** pBuf must be a pointer to at least BITVEC_SZ bytes of temporary storage
36949 ** that BitvecClear can use to rebuilt its hash table.
36950 */
36951 SQLITE_PRIVATE void sqlite3BitvecClear(Bitvec *p, u32 i, void *pBuf){
36952   if( p==0 ) return;
36953   assert( i>0 );
36954   i--;
36955   while( p->iDivisor ){
36956     u32 bin = i/p->iDivisor;
36957     i = i%p->iDivisor;
36958     p = p->u.apSub[bin];
36959     if (!p) {
36960       return;
36961     }
36962   }
36963   if( p->iSize<=BITVEC_NBIT ){
36964     p->u.aBitmap[i/BITVEC_SZELEM] &= ~(1 << (i&(BITVEC_SZELEM-1)));
36965   }else{
36966     unsigned int j;
36967     u32 *aiValues = pBuf;
36968     memcpy(aiValues, p->u.aHash, sizeof(p->u.aHash));
36969     memset(p->u.aHash, 0, sizeof(p->u.aHash));
36970     p->nSet = 0;
36971     for(j=0; j<BITVEC_NINT; j++){
36972       if( aiValues[j] && aiValues[j]!=(i+1) ){
36973         u32 h = BITVEC_HASH(aiValues[j]-1);
36974         p->nSet++;
36975         while( p->u.aHash[h] ){
36976           h++;
36977           if( h>=BITVEC_NINT ) h = 0;
36978         }
36979         p->u.aHash[h] = aiValues[j];
36980       }
36981     }
36982   }
36983 }
36984 
36985 /*
36986 ** Destroy a bitmap object.  Reclaim all memory used.
36987 */
36988 SQLITE_PRIVATE void sqlite3BitvecDestroy(Bitvec *p){
36989   if( p==0 ) return;
36990   if( p->iDivisor ){
36991     unsigned int i;
36992     for(i=0; i<BITVEC_NPTR; i++){
36993       sqlite3BitvecDestroy(p->u.apSub[i]);
36994     }
36995   }
36996   sqlite3_free(p);
36997 }
36998 
36999 /*
37000 ** Return the value of the iSize parameter specified when Bitvec *p
37001 ** was created.
37002 */
37003 SQLITE_PRIVATE u32 sqlite3BitvecSize(Bitvec *p){
37004   return p->iSize;
37005 }
37006 
37007 #ifndef SQLITE_OMIT_BUILTIN_TEST
37008 /*
37009 ** Let V[] be an array of unsigned characters sufficient to hold
37010 ** up to N bits.  Let I be an integer between 0 and N.  0<=I<N.
37011 ** Then the following macros can be used to set, clear, or test
37012 ** individual bits within V.
37013 */
37014 #define SETBIT(V,I)      V[I>>3] |= (1<<(I&7))
37015 #define CLEARBIT(V,I)    V[I>>3] &= ~(1<<(I&7))
37016 #define TESTBIT(V,I)     (V[I>>3]&(1<<(I&7)))!=0
37017 
37018 /*
37019 ** This routine runs an extensive test of the Bitvec code.
37020 **
37021 ** The input is an array of integers that acts as a program
37022 ** to test the Bitvec.  The integers are opcodes followed
37023 ** by 0, 1, or 3 operands, depending on the opcode.  Another
37024 ** opcode follows immediately after the last operand.
37025 **
37026 ** There are 6 opcodes numbered from 0 through 5.  0 is the
37027 ** "halt" opcode and causes the test to end.
37028 **
37029 **    0          Halt and return the number of errors
37030 **    1 N S X    Set N bits beginning with S and incrementing by X
37031 **    2 N S X    Clear N bits beginning with S and incrementing by X
37032 **    3 N        Set N randomly chosen bits
37033 **    4 N        Clear N randomly chosen bits
37034 **    5 N S X    Set N bits from S increment X in array only, not in bitvec
37035 **
37036 ** The opcodes 1 through 4 perform set and clear operations are performed
37037 ** on both a Bitvec object and on a linear array of bits obtained from malloc.
37038 ** Opcode 5 works on the linear array only, not on the Bitvec.
37039 ** Opcode 5 is used to deliberately induce a fault in order to
37040 ** confirm that error detection works.
37041 **
37042 ** At the conclusion of the test the linear array is compared
37043 ** against the Bitvec object.  If there are any differences,
37044 ** an error is returned.  If they are the same, zero is returned.
37045 **
37046 ** If a memory allocation error occurs, return -1.
37047 */
37048 SQLITE_PRIVATE int sqlite3BitvecBuiltinTest(int sz, int *aOp){
37049   Bitvec *pBitvec = 0;
37050   unsigned char *pV = 0;
37051   int rc = -1;
37052   int i, nx, pc, op;
37053   void *pTmpSpace;
37054 
37055   /* Allocate the Bitvec to be tested and a linear array of
37056   ** bits to act as the reference */
37057   pBitvec = sqlite3BitvecCreate( sz );
37058   pV = sqlite3MallocZero( (sz+7)/8 + 1 );
37059   pTmpSpace = sqlite3_malloc(BITVEC_SZ);
37060   if( pBitvec==0 || pV==0 || pTmpSpace==0  ) goto bitvec_end;
37061 
37062   /* NULL pBitvec tests */
37063   sqlite3BitvecSet(0, 1);
37064   sqlite3BitvecClear(0, 1, pTmpSpace);
37065 
37066   /* Run the program */
37067   pc = 0;
37068   while( (op = aOp[pc])!=0 ){
37069     switch( op ){
37070       case 1:
37071       case 2:
37072       case 5: {
37073         nx = 4;
37074         i = aOp[pc+2] - 1;
37075         aOp[pc+2] += aOp[pc+3];
37076         break;
37077       }
37078       case 3:
37079       case 4:
37080       default: {
37081         nx = 2;
37082         sqlite3_randomness(sizeof(i), &i);
37083         break;
37084       }
37085     }
37086     if( (--aOp[pc+1]) > 0 ) nx = 0;
37087     pc += nx;
37088     i = (i & 0x7fffffff)%sz;
37089     if( (op & 1)!=0 ){
37090       SETBIT(pV, (i+1));
37091       if( op!=5 ){
37092         if( sqlite3BitvecSet(pBitvec, i+1) ) goto bitvec_end;
37093       }
37094     }else{
37095       CLEARBIT(pV, (i+1));
37096       sqlite3BitvecClear(pBitvec, i+1, pTmpSpace);
37097     }
37098   }
37099 
37100   /* Test to make sure the linear array exactly matches the
37101   ** Bitvec object.  Start with the assumption that they do
37102   ** match (rc==0).  Change rc to non-zero if a discrepancy
37103   ** is found.
37104   */
37105   rc = sqlite3BitvecTest(0,0) + sqlite3BitvecTest(pBitvec, sz+1)
37106           + sqlite3BitvecTest(pBitvec, 0)
37107           + (sqlite3BitvecSize(pBitvec) - sz);
37108   for(i=1; i<=sz; i++){
37109     if(  (TESTBIT(pV,i))!=sqlite3BitvecTest(pBitvec,i) ){
37110       rc = i;
37111       break;
37112     }
37113   }
37114 
37115   /* Free allocated structure */
37116 bitvec_end:
37117   sqlite3_free(pTmpSpace);
37118   sqlite3_free(pV);
37119   sqlite3BitvecDestroy(pBitvec);
37120   return rc;
37121 }
37122 #endif /* SQLITE_OMIT_BUILTIN_TEST */
37123 
37124 /************** End of bitvec.c **********************************************/
37125 /************** Begin file pcache.c ******************************************/
37126 /*
37127 ** 2008 August 05
37128 **
37129 ** The author disclaims copyright to this source code.  In place of
37130 ** a legal notice, here is a blessing:
37131 **
37132 **    May you do good and not evil.
37133 **    May you find forgiveness for yourself and forgive others.
37134 **    May you share freely, never taking more than you give.
37135 **
37136 *************************************************************************
37137 ** This file implements that page cache.
37138 */
37139 
37140 /*
37141 ** A complete page cache is an instance of this structure.
37142 */
37143 struct PCache {
37144   PgHdr *pDirty, *pDirtyTail;         /* List of dirty pages in LRU order */
37145   PgHdr *pSynced;                     /* Last synced page in dirty page list */
37146   int nRef;                           /* Number of referenced pages */
37147   int szCache;                        /* Configured cache size */
37148   int szPage;                         /* Size of every page in this cache */
37149   int szExtra;                        /* Size of extra space for each page */
37150   int bPurgeable;                     /* True if pages are on backing store */
37151   int (*xStress)(void*,PgHdr*);       /* Call to try make a page clean */
37152   void *pStress;                      /* Argument to xStress */
37153   sqlite3_pcache *pCache;             /* Pluggable cache module */
37154   PgHdr *pPage1;                      /* Reference to page 1 */
37155 };
37156 
37157 /*
37158 ** Some of the assert() macros in this code are too expensive to run
37159 ** even during normal debugging.  Use them only rarely on long-running
37160 ** tests.  Enable the expensive asserts using the
37161 ** -DSQLITE_ENABLE_EXPENSIVE_ASSERT=1 compile-time option.
37162 */
37163 #ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
37164 # define expensive_assert(X)  assert(X)
37165 #else
37166 # define expensive_assert(X)
37167 #endif
37168 
37169 /********************************** Linked List Management ********************/
37170 
37171 #if !defined(NDEBUG) && defined(SQLITE_ENABLE_EXPENSIVE_ASSERT)
37172 /*
37173 ** Check that the pCache->pSynced variable is set correctly. If it
37174 ** is not, either fail an assert or return zero. Otherwise, return
37175 ** non-zero. This is only used in debugging builds, as follows:
37176 **
37177 **   expensive_assert( pcacheCheckSynced(pCache) );
37178 */
37179 static int pcacheCheckSynced(PCache *pCache){
37180   PgHdr *p;
37181   for(p=pCache->pDirtyTail; p!=pCache->pSynced; p=p->pDirtyPrev){
37182     assert( p->nRef || (p->flags&PGHDR_NEED_SYNC) );
37183   }
37184   return (p==0 || p->nRef || (p->flags&PGHDR_NEED_SYNC)==0);
37185 }
37186 #endif /* !NDEBUG && SQLITE_ENABLE_EXPENSIVE_ASSERT */
37187 
37188 /*
37189 ** Remove page pPage from the list of dirty pages.
37190 */
37191 static void pcacheRemoveFromDirtyList(PgHdr *pPage){
37192   PCache *p = pPage->pCache;
37193 
37194   assert( pPage->pDirtyNext || pPage==p->pDirtyTail );
37195   assert( pPage->pDirtyPrev || pPage==p->pDirty );
37196 
37197   /* Update the PCache1.pSynced variable if necessary. */
37198   if( p->pSynced==pPage ){
37199     PgHdr *pSynced = pPage->pDirtyPrev;
37200     while( pSynced && (pSynced->flags&PGHDR_NEED_SYNC) ){
37201       pSynced = pSynced->pDirtyPrev;
37202     }
37203     p->pSynced = pSynced;
37204   }
37205 
37206   if( pPage->pDirtyNext ){
37207     pPage->pDirtyNext->pDirtyPrev = pPage->pDirtyPrev;
37208   }else{
37209     assert( pPage==p->pDirtyTail );
37210     p->pDirtyTail = pPage->pDirtyPrev;
37211   }
37212   if( pPage->pDirtyPrev ){
37213     pPage->pDirtyPrev->pDirtyNext = pPage->pDirtyNext;
37214   }else{
37215     assert( pPage==p->pDirty );
37216     p->pDirty = pPage->pDirtyNext;
37217   }
37218   pPage->pDirtyNext = 0;
37219   pPage->pDirtyPrev = 0;
37220 
37221   expensive_assert( pcacheCheckSynced(p) );
37222 }
37223 
37224 /*
37225 ** Add page pPage to the head of the dirty list (PCache1.pDirty is set to
37226 ** pPage).
37227 */
37228 static void pcacheAddToDirtyList(PgHdr *pPage){
37229   PCache *p = pPage->pCache;
37230 
37231   assert( pPage->pDirtyNext==0 && pPage->pDirtyPrev==0 && p->pDirty!=pPage );
37232 
37233   pPage->pDirtyNext = p->pDirty;
37234   if( pPage->pDirtyNext ){
37235     assert( pPage->pDirtyNext->pDirtyPrev==0 );
37236     pPage->pDirtyNext->pDirtyPrev = pPage;
37237   }
37238   p->pDirty = pPage;
37239   if( !p->pDirtyTail ){
37240     p->pDirtyTail = pPage;
37241   }
37242   if( !p->pSynced && 0==(pPage->flags&PGHDR_NEED_SYNC) ){
37243     p->pSynced = pPage;
37244   }
37245   expensive_assert( pcacheCheckSynced(p) );
37246 }
37247 
37248 /*
37249 ** Wrapper around the pluggable caches xUnpin method. If the cache is
37250 ** being used for an in-memory database, this function is a no-op.
37251 */
37252 static void pcacheUnpin(PgHdr *p){
37253   PCache *pCache = p->pCache;
37254   if( pCache->bPurgeable ){
37255     if( p->pgno==1 ){
37256       pCache->pPage1 = 0;
37257     }
37258     sqlite3GlobalConfig.pcache2.xUnpin(pCache->pCache, p->pPage, 0);
37259   }
37260 }
37261 
37262 /*************************************************** General Interfaces ******
37263 **
37264 ** Initialize and shutdown the page cache subsystem. Neither of these
37265 ** functions are threadsafe.
37266 */
37267 SQLITE_PRIVATE int sqlite3PcacheInitialize(void){
37268   if( sqlite3GlobalConfig.pcache2.xInit==0 ){
37269     /* IMPLEMENTATION-OF: R-26801-64137 If the xInit() method is NULL, then the
37270     ** built-in default page cache is used instead of the application defined
37271     ** page cache. */
37272     sqlite3PCacheSetDefault();
37273   }
37274   return sqlite3GlobalConfig.pcache2.xInit(sqlite3GlobalConfig.pcache2.pArg);
37275 }
37276 SQLITE_PRIVATE void sqlite3PcacheShutdown(void){
37277   if( sqlite3GlobalConfig.pcache2.xShutdown ){
37278     /* IMPLEMENTATION-OF: R-26000-56589 The xShutdown() method may be NULL. */
37279     sqlite3GlobalConfig.pcache2.xShutdown(sqlite3GlobalConfig.pcache2.pArg);
37280   }
37281 }
37282 
37283 /*
37284 ** Return the size in bytes of a PCache object.
37285 */
37286 SQLITE_PRIVATE int sqlite3PcacheSize(void){ return sizeof(PCache); }
37287 
37288 /*
37289 ** Create a new PCache object. Storage space to hold the object
37290 ** has already been allocated and is passed in as the p pointer.
37291 ** The caller discovers how much space needs to be allocated by
37292 ** calling sqlite3PcacheSize().
37293 */
37294 SQLITE_PRIVATE void sqlite3PcacheOpen(
37295   int szPage,                  /* Size of every page */
37296   int szExtra,                 /* Extra space associated with each page */
37297   int bPurgeable,              /* True if pages are on backing store */
37298   int (*xStress)(void*,PgHdr*),/* Call to try to make pages clean */
37299   void *pStress,               /* Argument to xStress */
37300   PCache *p                    /* Preallocated space for the PCache */
37301 ){
37302   memset(p, 0, sizeof(PCache));
37303   p->szPage = szPage;
37304   p->szExtra = szExtra;
37305   p->bPurgeable = bPurgeable;
37306   p->xStress = xStress;
37307   p->pStress = pStress;
37308   p->szCache = 100;
37309 }
37310 
37311 /*
37312 ** Change the page size for PCache object. The caller must ensure that there
37313 ** are no outstanding page references when this function is called.
37314 */
37315 SQLITE_PRIVATE void sqlite3PcacheSetPageSize(PCache *pCache, int szPage){
37316   assert( pCache->nRef==0 && pCache->pDirty==0 );
37317   if( pCache->pCache ){
37318     sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache);
37319     pCache->pCache = 0;
37320     pCache->pPage1 = 0;
37321   }
37322   pCache->szPage = szPage;
37323 }
37324 
37325 /*
37326 ** Compute the number of pages of cache requested.
37327 */
37328 static int numberOfCachePages(PCache *p){
37329   if( p->szCache>=0 ){
37330     return p->szCache;
37331   }else{
37332     return (int)((-1024*(i64)p->szCache)/(p->szPage+p->szExtra));
37333   }
37334 }
37335 
37336 /*
37337 ** Try to obtain a page from the cache.
37338 */
37339 SQLITE_PRIVATE int sqlite3PcacheFetch(
37340   PCache *pCache,       /* Obtain the page from this cache */
37341   Pgno pgno,            /* Page number to obtain */
37342   int createFlag,       /* If true, create page if it does not exist already */
37343   PgHdr **ppPage        /* Write the page here */
37344 ){
37345   sqlite3_pcache_page *pPage = 0;
37346   PgHdr *pPgHdr = 0;
37347   int eCreate;
37348 
37349   assert( pCache!=0 );
37350   assert( createFlag==1 || createFlag==0 );
37351   assert( pgno>0 );
37352 
37353   /* If the pluggable cache (sqlite3_pcache*) has not been allocated,
37354   ** allocate it now.
37355   */
37356   if( !pCache->pCache && createFlag ){
37357     sqlite3_pcache *p;
37358     p = sqlite3GlobalConfig.pcache2.xCreate(
37359         pCache->szPage, pCache->szExtra + sizeof(PgHdr), pCache->bPurgeable
37360     );
37361     if( !p ){
37362       return SQLITE_NOMEM;
37363     }
37364     sqlite3GlobalConfig.pcache2.xCachesize(p, numberOfCachePages(pCache));
37365     pCache->pCache = p;
37366   }
37367 
37368   eCreate = createFlag * (1 + (!pCache->bPurgeable || !pCache->pDirty));
37369   if( pCache->pCache ){
37370     pPage = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, eCreate);
37371   }
37372 
37373   if( !pPage && eCreate==1 ){
37374     PgHdr *pPg;
37375 
37376     /* Find a dirty page to write-out and recycle. First try to find a
37377     ** page that does not require a journal-sync (one with PGHDR_NEED_SYNC
37378     ** cleared), but if that is not possible settle for any other
37379     ** unreferenced dirty page.
37380     */
37381     expensive_assert( pcacheCheckSynced(pCache) );
37382     for(pPg=pCache->pSynced;
37383         pPg && (pPg->nRef || (pPg->flags&PGHDR_NEED_SYNC));
37384         pPg=pPg->pDirtyPrev
37385     );
37386     pCache->pSynced = pPg;
37387     if( !pPg ){
37388       for(pPg=pCache->pDirtyTail; pPg && pPg->nRef; pPg=pPg->pDirtyPrev);
37389     }
37390     if( pPg ){
37391       int rc;
37392 #ifdef SQLITE_LOG_CACHE_SPILL
37393       sqlite3_log(SQLITE_FULL,
37394                   "spill page %d making room for %d - cache used: %d/%d",
37395                   pPg->pgno, pgno,
37396                   sqlite3GlobalConfig.pcache.xPagecount(pCache->pCache),
37397                   numberOfCachePages(pCache));
37398 #endif
37399       rc = pCache->xStress(pCache->pStress, pPg);
37400       if( rc!=SQLITE_OK && rc!=SQLITE_BUSY ){
37401         return rc;
37402       }
37403     }
37404 
37405     pPage = sqlite3GlobalConfig.pcache2.xFetch(pCache->pCache, pgno, 2);
37406   }
37407 
37408   if( pPage ){
37409     pPgHdr = (PgHdr *)pPage->pExtra;
37410 
37411     if( !pPgHdr->pPage ){
37412       memset(pPgHdr, 0, sizeof(PgHdr));
37413       pPgHdr->pPage = pPage;
37414       pPgHdr->pData = pPage->pBuf;
37415       pPgHdr->pExtra = (void *)&pPgHdr[1];
37416       memset(pPgHdr->pExtra, 0, pCache->szExtra);
37417       pPgHdr->pCache = pCache;
37418       pPgHdr->pgno = pgno;
37419     }
37420     assert( pPgHdr->pCache==pCache );
37421     assert( pPgHdr->pgno==pgno );
37422     assert( pPgHdr->pData==pPage->pBuf );
37423     assert( pPgHdr->pExtra==(void *)&pPgHdr[1] );
37424 
37425     if( 0==pPgHdr->nRef ){
37426       pCache->nRef++;
37427     }
37428     pPgHdr->nRef++;
37429     if( pgno==1 ){
37430       pCache->pPage1 = pPgHdr;
37431     }
37432   }
37433   *ppPage = pPgHdr;
37434   return (pPgHdr==0 && eCreate) ? SQLITE_NOMEM : SQLITE_OK;
37435 }
37436 
37437 /*
37438 ** Decrement the reference count on a page. If the page is clean and the
37439 ** reference count drops to 0, then it is made elible for recycling.
37440 */
37441 SQLITE_PRIVATE void sqlite3PcacheRelease(PgHdr *p){
37442   assert( p->nRef>0 );
37443   p->nRef--;
37444   if( p->nRef==0 ){
37445     PCache *pCache = p->pCache;
37446     pCache->nRef--;
37447     if( (p->flags&PGHDR_DIRTY)==0 ){
37448       pcacheUnpin(p);
37449     }else{
37450       /* Move the page to the head of the dirty list. */
37451       pcacheRemoveFromDirtyList(p);
37452       pcacheAddToDirtyList(p);
37453     }
37454   }
37455 }
37456 
37457 /*
37458 ** Increase the reference count of a supplied page by 1.
37459 */
37460 SQLITE_PRIVATE void sqlite3PcacheRef(PgHdr *p){
37461   assert(p->nRef>0);
37462   p->nRef++;
37463 }
37464 
37465 /*
37466 ** Drop a page from the cache. There must be exactly one reference to the
37467 ** page. This function deletes that reference, so after it returns the
37468 ** page pointed to by p is invalid.
37469 */
37470 SQLITE_PRIVATE void sqlite3PcacheDrop(PgHdr *p){
37471   PCache *pCache;
37472   assert( p->nRef==1 );
37473   if( p->flags&PGHDR_DIRTY ){
37474     pcacheRemoveFromDirtyList(p);
37475   }
37476   pCache = p->pCache;
37477   pCache->nRef--;
37478   if( p->pgno==1 ){
37479     pCache->pPage1 = 0;
37480   }
37481   sqlite3GlobalConfig.pcache2.xUnpin(pCache->pCache, p->pPage, 1);
37482 }
37483 
37484 /*
37485 ** Make sure the page is marked as dirty. If it isn't dirty already,
37486 ** make it so.
37487 */
37488 SQLITE_PRIVATE void sqlite3PcacheMakeDirty(PgHdr *p){
37489   p->flags &= ~PGHDR_DONT_WRITE;
37490   assert( p->nRef>0 );
37491   if( 0==(p->flags & PGHDR_DIRTY) ){
37492     p->flags |= PGHDR_DIRTY;
37493     pcacheAddToDirtyList( p);
37494   }
37495 }
37496 
37497 /*
37498 ** Make sure the page is marked as clean. If it isn't clean already,
37499 ** make it so.
37500 */
37501 SQLITE_PRIVATE void sqlite3PcacheMakeClean(PgHdr *p){
37502   if( (p->flags & PGHDR_DIRTY) ){
37503     pcacheRemoveFromDirtyList(p);
37504     p->flags &= ~(PGHDR_DIRTY|PGHDR_NEED_SYNC);
37505     if( p->nRef==0 ){
37506       pcacheUnpin(p);
37507     }
37508   }
37509 }
37510 
37511 /*
37512 ** Make every page in the cache clean.
37513 */
37514 SQLITE_PRIVATE void sqlite3PcacheCleanAll(PCache *pCache){
37515   PgHdr *p;
37516   while( (p = pCache->pDirty)!=0 ){
37517     sqlite3PcacheMakeClean(p);
37518   }
37519 }
37520 
37521 /*
37522 ** Clear the PGHDR_NEED_SYNC flag from all dirty pages.
37523 */
37524 SQLITE_PRIVATE void sqlite3PcacheClearSyncFlags(PCache *pCache){
37525   PgHdr *p;
37526   for(p=pCache->pDirty; p; p=p->pDirtyNext){
37527     p->flags &= ~PGHDR_NEED_SYNC;
37528   }
37529   pCache->pSynced = pCache->pDirtyTail;
37530 }
37531 
37532 /*
37533 ** Change the page number of page p to newPgno.
37534 */
37535 SQLITE_PRIVATE void sqlite3PcacheMove(PgHdr *p, Pgno newPgno){
37536   PCache *pCache = p->pCache;
37537   assert( p->nRef>0 );
37538   assert( newPgno>0 );
37539   sqlite3GlobalConfig.pcache2.xRekey(pCache->pCache, p->pPage, p->pgno,newPgno);
37540   p->pgno = newPgno;
37541   if( (p->flags&PGHDR_DIRTY) && (p->flags&PGHDR_NEED_SYNC) ){
37542     pcacheRemoveFromDirtyList(p);
37543     pcacheAddToDirtyList(p);
37544   }
37545 }
37546 
37547 /*
37548 ** Drop every cache entry whose page number is greater than "pgno". The
37549 ** caller must ensure that there are no outstanding references to any pages
37550 ** other than page 1 with a page number greater than pgno.
37551 **
37552 ** If there is a reference to page 1 and the pgno parameter passed to this
37553 ** function is 0, then the data area associated with page 1 is zeroed, but
37554 ** the page object is not dropped.
37555 */
37556 SQLITE_PRIVATE void sqlite3PcacheTruncate(PCache *pCache, Pgno pgno){
37557   if( pCache->pCache ){
37558     PgHdr *p;
37559     PgHdr *pNext;
37560     for(p=pCache->pDirty; p; p=pNext){
37561       pNext = p->pDirtyNext;
37562       /* This routine never gets call with a positive pgno except right
37563       ** after sqlite3PcacheCleanAll().  So if there are dirty pages,
37564       ** it must be that pgno==0.
37565       */
37566       assert( p->pgno>0 );
37567       if( ALWAYS(p->pgno>pgno) ){
37568         assert( p->flags&PGHDR_DIRTY );
37569         sqlite3PcacheMakeClean(p);
37570       }
37571     }
37572     if( pgno==0 && pCache->pPage1 ){
37573       memset(pCache->pPage1->pData, 0, pCache->szPage);
37574       pgno = 1;
37575     }
37576     sqlite3GlobalConfig.pcache2.xTruncate(pCache->pCache, pgno+1);
37577   }
37578 }
37579 
37580 /*
37581 ** Close a cache.
37582 */
37583 SQLITE_PRIVATE void sqlite3PcacheClose(PCache *pCache){
37584   if( pCache->pCache ){
37585     sqlite3GlobalConfig.pcache2.xDestroy(pCache->pCache);
37586   }
37587 }
37588 
37589 /*
37590 ** Discard the contents of the cache.
37591 */
37592 SQLITE_PRIVATE void sqlite3PcacheClear(PCache *pCache){
37593   sqlite3PcacheTruncate(pCache, 0);
37594 }
37595 
37596 /*
37597 ** Merge two lists of pages connected by pDirty and in pgno order.
37598 ** Do not both fixing the pDirtyPrev pointers.
37599 */
37600 static PgHdr *pcacheMergeDirtyList(PgHdr *pA, PgHdr *pB){
37601   PgHdr result, *pTail;
37602   pTail = &result;
37603   while( pA && pB ){
37604     if( pA->pgno<pB->pgno ){
37605       pTail->pDirty = pA;
37606       pTail = pA;
37607       pA = pA->pDirty;
37608     }else{
37609       pTail->pDirty = pB;
37610       pTail = pB;
37611       pB = pB->pDirty;
37612     }
37613   }
37614   if( pA ){
37615     pTail->pDirty = pA;
37616   }else if( pB ){
37617     pTail->pDirty = pB;
37618   }else{
37619     pTail->pDirty = 0;
37620   }
37621   return result.pDirty;
37622 }
37623 
37624 /*
37625 ** Sort the list of pages in accending order by pgno.  Pages are
37626 ** connected by pDirty pointers.  The pDirtyPrev pointers are
37627 ** corrupted by this sort.
37628 **
37629 ** Since there cannot be more than 2^31 distinct pages in a database,
37630 ** there cannot be more than 31 buckets required by the merge sorter.
37631 ** One extra bucket is added to catch overflow in case something
37632 ** ever changes to make the previous sentence incorrect.
37633 */
37634 #define N_SORT_BUCKET  32
37635 static PgHdr *pcacheSortDirtyList(PgHdr *pIn){
37636   PgHdr *a[N_SORT_BUCKET], *p;
37637   int i;
37638   memset(a, 0, sizeof(a));
37639   while( pIn ){
37640     p = pIn;
37641     pIn = p->pDirty;
37642     p->pDirty = 0;
37643     for(i=0; ALWAYS(i<N_SORT_BUCKET-1); i++){
37644       if( a[i]==0 ){
37645         a[i] = p;
37646         break;
37647       }else{
37648         p = pcacheMergeDirtyList(a[i], p);
37649         a[i] = 0;
37650       }
37651     }
37652     if( NEVER(i==N_SORT_BUCKET-1) ){
37653       /* To get here, there need to be 2^(N_SORT_BUCKET) elements in
37654       ** the input list.  But that is impossible.
37655       */
37656       a[i] = pcacheMergeDirtyList(a[i], p);
37657     }
37658   }
37659   p = a[0];
37660   for(i=1; i<N_SORT_BUCKET; i++){
37661     p = pcacheMergeDirtyList(p, a[i]);
37662   }
37663   return p;
37664 }
37665 
37666 /*
37667 ** Return a list of all dirty pages in the cache, sorted by page number.
37668 */
37669 SQLITE_PRIVATE PgHdr *sqlite3PcacheDirtyList(PCache *pCache){
37670   PgHdr *p;
37671   for(p=pCache->pDirty; p; p=p->pDirtyNext){
37672     p->pDirty = p->pDirtyNext;
37673   }
37674   return pcacheSortDirtyList(pCache->pDirty);
37675 }
37676 
37677 /*
37678 ** Return the total number of referenced pages held by the cache.
37679 */
37680 SQLITE_PRIVATE int sqlite3PcacheRefCount(PCache *pCache){
37681   return pCache->nRef;
37682 }
37683 
37684 /*
37685 ** Return the number of references to the page supplied as an argument.
37686 */
37687 SQLITE_PRIVATE int sqlite3PcachePageRefcount(PgHdr *p){
37688   return p->nRef;
37689 }
37690 
37691 /*
37692 ** Return the total number of pages in the cache.
37693 */
37694 SQLITE_PRIVATE int sqlite3PcachePagecount(PCache *pCache){
37695   int nPage = 0;
37696   if( pCache->pCache ){
37697     nPage = sqlite3GlobalConfig.pcache2.xPagecount(pCache->pCache);
37698   }
37699   return nPage;
37700 }
37701 
37702 #ifdef SQLITE_TEST
37703 /*
37704 ** Get the suggested cache-size value.
37705 */
37706 SQLITE_PRIVATE int sqlite3PcacheGetCachesize(PCache *pCache){
37707   return numberOfCachePages(pCache);
37708 }
37709 #endif
37710 
37711 /*
37712 ** Set the suggested cache-size value.
37713 */
37714 SQLITE_PRIVATE void sqlite3PcacheSetCachesize(PCache *pCache, int mxPage){
37715   pCache->szCache = mxPage;
37716   if( pCache->pCache ){
37717     sqlite3GlobalConfig.pcache2.xCachesize(pCache->pCache,
37718                                            numberOfCachePages(pCache));
37719   }
37720 }
37721 
37722 /*
37723 ** Free up as much memory as possible from the page cache.
37724 */
37725 SQLITE_PRIVATE void sqlite3PcacheShrink(PCache *pCache){
37726   if( pCache->pCache ){
37727     sqlite3GlobalConfig.pcache2.xShrink(pCache->pCache);
37728   }
37729 }
37730 
37731 #if defined(SQLITE_CHECK_PAGES) || defined(SQLITE_DEBUG)
37732 /*
37733 ** For all dirty pages currently in the cache, invoke the specified
37734 ** callback. This is only used if the SQLITE_CHECK_PAGES macro is
37735 ** defined.
37736 */
37737 SQLITE_PRIVATE void sqlite3PcacheIterateDirty(PCache *pCache, void (*xIter)(PgHdr *)){
37738   PgHdr *pDirty;
37739   for(pDirty=pCache->pDirty; pDirty; pDirty=pDirty->pDirtyNext){
37740     xIter(pDirty);
37741   }
37742 }
37743 #endif
37744 
37745 /************** End of pcache.c **********************************************/
37746 /************** Begin file pcache1.c *****************************************/
37747 /*
37748 ** 2008 November 05
37749 **
37750 ** The author disclaims copyright to this source code.  In place of
37751 ** a legal notice, here is a blessing:
37752 **
37753 **    May you do good and not evil.
37754 **    May you find forgiveness for yourself and forgive others.
37755 **    May you share freely, never taking more than you give.
37756 **
37757 *************************************************************************
37758 **
37759 ** This file implements the default page cache implementation (the
37760 ** sqlite3_pcache interface). It also contains part of the implementation
37761 ** of the SQLITE_CONFIG_PAGECACHE and sqlite3_release_memory() features.
37762 ** If the default page cache implementation is overriden, then neither of
37763 ** these two features are available.
37764 */
37765 
37766 
37767 typedef struct PCache1 PCache1;
37768 typedef struct PgHdr1 PgHdr1;
37769 typedef struct PgFreeslot PgFreeslot;
37770 typedef struct PGroup PGroup;
37771 
37772 /* Each page cache (or PCache) belongs to a PGroup.  A PGroup is a set
37773 ** of one or more PCaches that are able to recycle each others unpinned
37774 ** pages when they are under memory pressure.  A PGroup is an instance of
37775 ** the following object.
37776 **
37777 ** This page cache implementation works in one of two modes:
37778 **
37779 **   (1)  Every PCache is the sole member of its own PGroup.  There is
37780 **        one PGroup per PCache.
37781 **
37782 **   (2)  There is a single global PGroup that all PCaches are a member
37783 **        of.
37784 **
37785 ** Mode 1 uses more memory (since PCache instances are not able to rob
37786 ** unused pages from other PCaches) but it also operates without a mutex,
37787 ** and is therefore often faster.  Mode 2 requires a mutex in order to be
37788 ** threadsafe, but recycles pages more efficiently.
37789 **
37790 ** For mode (1), PGroup.mutex is NULL.  For mode (2) there is only a single
37791 ** PGroup which is the pcache1.grp global variable and its mutex is
37792 ** SQLITE_MUTEX_STATIC_LRU.
37793 */
37794 struct PGroup {
37795   sqlite3_mutex *mutex;          /* MUTEX_STATIC_LRU or NULL */
37796   unsigned int nMaxPage;         /* Sum of nMax for purgeable caches */
37797   unsigned int nMinPage;         /* Sum of nMin for purgeable caches */
37798   unsigned int mxPinned;         /* nMaxpage + 10 - nMinPage */
37799   unsigned int nCurrentPage;     /* Number of purgeable pages allocated */
37800   PgHdr1 *pLruHead, *pLruTail;   /* LRU list of unpinned pages */
37801 };
37802 
37803 /* Each page cache is an instance of the following object.  Every
37804 ** open database file (including each in-memory database and each
37805 ** temporary or transient database) has a single page cache which
37806 ** is an instance of this object.
37807 **
37808 ** Pointers to structures of this type are cast and returned as
37809 ** opaque sqlite3_pcache* handles.
37810 */
37811 struct PCache1 {
37812   /* Cache configuration parameters. Page size (szPage) and the purgeable
37813   ** flag (bPurgeable) are set when the cache is created. nMax may be
37814   ** modified at any time by a call to the pcache1Cachesize() method.
37815   ** The PGroup mutex must be held when accessing nMax.
37816   */
37817   PGroup *pGroup;                     /* PGroup this cache belongs to */
37818   int szPage;                         /* Size of allocated pages in bytes */
37819   int szExtra;                        /* Size of extra space in bytes */
37820   int bPurgeable;                     /* True if cache is purgeable */
37821   unsigned int nMin;                  /* Minimum number of pages reserved */
37822   unsigned int nMax;                  /* Configured "cache_size" value */
37823   unsigned int n90pct;                /* nMax*9/10 */
37824   unsigned int iMaxKey;               /* Largest key seen since xTruncate() */
37825 
37826   /* Hash table of all pages. The following variables may only be accessed
37827   ** when the accessor is holding the PGroup mutex.
37828   */
37829   unsigned int nRecyclable;           /* Number of pages in the LRU list */
37830   unsigned int nPage;                 /* Total number of pages in apHash */
37831   unsigned int nHash;                 /* Number of slots in apHash[] */
37832   PgHdr1 **apHash;                    /* Hash table for fast lookup by key */
37833 };
37834 
37835 /*
37836 ** Each cache entry is represented by an instance of the following
37837 ** structure. Unless SQLITE_PCACHE_SEPARATE_HEADER is defined, a buffer of
37838 ** PgHdr1.pCache->szPage bytes is allocated directly before this structure
37839 ** in memory.
37840 */
37841 struct PgHdr1 {
37842   sqlite3_pcache_page page;
37843   unsigned int iKey;             /* Key value (page number) */
37844   u8 isPinned;                   /* Page in use, not on the LRU list */
37845   PgHdr1 *pNext;                 /* Next in hash table chain */
37846   PCache1 *pCache;               /* Cache that currently owns this page */
37847   PgHdr1 *pLruNext;              /* Next in LRU list of unpinned pages */
37848   PgHdr1 *pLruPrev;              /* Previous in LRU list of unpinned pages */
37849 };
37850 
37851 /*
37852 ** Free slots in the allocator used to divide up the buffer provided using
37853 ** the SQLITE_CONFIG_PAGECACHE mechanism.
37854 */
37855 struct PgFreeslot {
37856   PgFreeslot *pNext;  /* Next free slot */
37857 };
37858 
37859 /*
37860 ** Global data used by this cache.
37861 */
37862 static SQLITE_WSD struct PCacheGlobal {
37863   PGroup grp;                    /* The global PGroup for mode (2) */
37864 
37865   /* Variables related to SQLITE_CONFIG_PAGECACHE settings.  The
37866   ** szSlot, nSlot, pStart, pEnd, nReserve, and isInit values are all
37867   ** fixed at sqlite3_initialize() time and do not require mutex protection.
37868   ** The nFreeSlot and pFree values do require mutex protection.
37869   */
37870   int isInit;                    /* True if initialized */
37871   int szSlot;                    /* Size of each free slot */
37872   int nSlot;                     /* The number of pcache slots */
37873   int nReserve;                  /* Try to keep nFreeSlot above this */
37874   void *pStart, *pEnd;           /* Bounds of pagecache malloc range */
37875   /* Above requires no mutex.  Use mutex below for variable that follow. */
37876   sqlite3_mutex *mutex;          /* Mutex for accessing the following: */
37877   PgFreeslot *pFree;             /* Free page blocks */
37878   int nFreeSlot;                 /* Number of unused pcache slots */
37879   /* The following value requires a mutex to change.  We skip the mutex on
37880   ** reading because (1) most platforms read a 32-bit integer atomically and
37881   ** (2) even if an incorrect value is read, no great harm is done since this
37882   ** is really just an optimization. */
37883   int bUnderPressure;            /* True if low on PAGECACHE memory */
37884 } pcache1_g;
37885 
37886 /*
37887 ** All code in this file should access the global structure above via the
37888 ** alias "pcache1". This ensures that the WSD emulation is used when
37889 ** compiling for systems that do not support real WSD.
37890 */
37891 #define pcache1 (GLOBAL(struct PCacheGlobal, pcache1_g))
37892 
37893 /*
37894 ** Macros to enter and leave the PCache LRU mutex.
37895 */
37896 #define pcache1EnterMutex(X) sqlite3_mutex_enter((X)->mutex)
37897 #define pcache1LeaveMutex(X) sqlite3_mutex_leave((X)->mutex)
37898 
37899 /******************************************************************************/
37900 /******** Page Allocation/SQLITE_CONFIG_PCACHE Related Functions **************/
37901 
37902 /*
37903 ** This function is called during initialization if a static buffer is
37904 ** supplied to use for the page-cache by passing the SQLITE_CONFIG_PAGECACHE
37905 ** verb to sqlite3_config(). Parameter pBuf points to an allocation large
37906 ** enough to contain 'n' buffers of 'sz' bytes each.
37907 **
37908 ** This routine is called from sqlite3_initialize() and so it is guaranteed
37909 ** to be serialized already.  There is no need for further mutexing.
37910 */
37911 SQLITE_PRIVATE void sqlite3PCacheBufferSetup(void *pBuf, int sz, int n){
37912   if( pcache1.isInit ){
37913     PgFreeslot *p;
37914     sz = ROUNDDOWN8(sz);
37915     pcache1.szSlot = sz;
37916     pcache1.nSlot = pcache1.nFreeSlot = n;
37917     pcache1.nReserve = n>90 ? 10 : (n/10 + 1);
37918     pcache1.pStart = pBuf;
37919     pcache1.pFree = 0;
37920     pcache1.bUnderPressure = 0;
37921     while( n-- ){
37922       p = (PgFreeslot*)pBuf;
37923       p->pNext = pcache1.pFree;
37924       pcache1.pFree = p;
37925       pBuf = (void*)&((char*)pBuf)[sz];
37926     }
37927     pcache1.pEnd = pBuf;
37928   }
37929 }
37930 
37931 /*
37932 ** Malloc function used within this file to allocate space from the buffer
37933 ** configured using sqlite3_config(SQLITE_CONFIG_PAGECACHE) option. If no
37934 ** such buffer exists or there is no space left in it, this function falls
37935 ** back to sqlite3Malloc().
37936 **
37937 ** Multiple threads can run this routine at the same time.  Global variables
37938 ** in pcache1 need to be protected via mutex.
37939 */
37940 static void *pcache1Alloc(int nByte){
37941   void *p = 0;
37942   assert( sqlite3_mutex_notheld(pcache1.grp.mutex) );
37943   sqlite3StatusSet(SQLITE_STATUS_PAGECACHE_SIZE, nByte);
37944   if( nByte<=pcache1.szSlot ){
37945     sqlite3_mutex_enter(pcache1.mutex);
37946     p = (PgHdr1 *)pcache1.pFree;
37947     if( p ){
37948       pcache1.pFree = pcache1.pFree->pNext;
37949       pcache1.nFreeSlot--;
37950       pcache1.bUnderPressure = pcache1.nFreeSlot<pcache1.nReserve;
37951       assert( pcache1.nFreeSlot>=0 );
37952       sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, 1);
37953     }
37954     sqlite3_mutex_leave(pcache1.mutex);
37955   }
37956   if( p==0 ){
37957     /* Memory is not available in the SQLITE_CONFIG_PAGECACHE pool.  Get
37958     ** it from sqlite3Malloc instead.
37959     */
37960     p = sqlite3Malloc(nByte);
37961 #ifndef SQLITE_DISABLE_PAGECACHE_OVERFLOW_STATS
37962     if( p ){
37963       int sz = sqlite3MallocSize(p);
37964       sqlite3_mutex_enter(pcache1.mutex);
37965       sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, sz);
37966       sqlite3_mutex_leave(pcache1.mutex);
37967     }
37968 #endif
37969     sqlite3MemdebugSetType(p, MEMTYPE_PCACHE);
37970   }
37971   return p;
37972 }
37973 
37974 /*
37975 ** Free an allocated buffer obtained from pcache1Alloc().
37976 */
37977 static int pcache1Free(void *p){
37978   int nFreed = 0;
37979   if( p==0 ) return 0;
37980   if( p>=pcache1.pStart && p<pcache1.pEnd ){
37981     PgFreeslot *pSlot;
37982     sqlite3_mutex_enter(pcache1.mutex);
37983     sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_USED, -1);
37984     pSlot = (PgFreeslot*)p;
37985     pSlot->pNext = pcache1.pFree;
37986     pcache1.pFree = pSlot;
37987     pcache1.nFreeSlot++;
37988     pcache1.bUnderPressure = pcache1.nFreeSlot<pcache1.nReserve;
37989     assert( pcache1.nFreeSlot<=pcache1.nSlot );
37990     sqlite3_mutex_leave(pcache1.mutex);
37991   }else{
37992     assert( sqlite3MemdebugHasType(p, MEMTYPE_PCACHE) );
37993     sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
37994     nFreed = sqlite3MallocSize(p);
37995 #ifndef SQLITE_DISABLE_PAGECACHE_OVERFLOW_STATS
37996     sqlite3_mutex_enter(pcache1.mutex);
37997     sqlite3StatusAdd(SQLITE_STATUS_PAGECACHE_OVERFLOW, -nFreed);
37998     sqlite3_mutex_leave(pcache1.mutex);
37999 #endif
38000     sqlite3_free(p);
38001   }
38002   return nFreed;
38003 }
38004 
38005 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
38006 /*
38007 ** Return the size of a pcache allocation
38008 */
38009 static int pcache1MemSize(void *p){
38010   if( p>=pcache1.pStart && p<pcache1.pEnd ){
38011     return pcache1.szSlot;
38012   }else{
38013     int iSize;
38014     assert( sqlite3MemdebugHasType(p, MEMTYPE_PCACHE) );
38015     sqlite3MemdebugSetType(p, MEMTYPE_HEAP);
38016     iSize = sqlite3MallocSize(p);
38017     sqlite3MemdebugSetType(p, MEMTYPE_PCACHE);
38018     return iSize;
38019   }
38020 }
38021 #endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */
38022 
38023 /*
38024 ** Allocate a new page object initially associated with cache pCache.
38025 */
38026 static PgHdr1 *pcache1AllocPage(PCache1 *pCache){
38027   PgHdr1 *p = 0;
38028   void *pPg;
38029 
38030   /* The group mutex must be released before pcache1Alloc() is called. This
38031   ** is because it may call sqlite3_release_memory(), which assumes that
38032   ** this mutex is not held. */
38033   assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
38034   pcache1LeaveMutex(pCache->pGroup);
38035 #ifdef SQLITE_PCACHE_SEPARATE_HEADER
38036   pPg = pcache1Alloc(pCache->szPage);
38037   p = sqlite3Malloc(sizeof(PgHdr1) + pCache->szExtra);
38038   if( !pPg || !p ){
38039     pcache1Free(pPg);
38040     sqlite3_free(p);
38041     pPg = 0;
38042   }
38043 #else
38044   pPg = pcache1Alloc(sizeof(PgHdr1) + pCache->szPage + pCache->szExtra);
38045   p = (PgHdr1 *)&((u8 *)pPg)[pCache->szPage];
38046 #endif
38047   pcache1EnterMutex(pCache->pGroup);
38048 
38049   if( pPg ){
38050     p->page.pBuf = pPg;
38051     p->page.pExtra = &p[1];
38052     if( pCache->bPurgeable ){
38053       pCache->pGroup->nCurrentPage++;
38054     }
38055     return p;
38056   }
38057   return 0;
38058 }
38059 
38060 /*
38061 ** Free a page object allocated by pcache1AllocPage().
38062 **
38063 ** The pointer is allowed to be NULL, which is prudent.  But it turns out
38064 ** that the current implementation happens to never call this routine
38065 ** with a NULL pointer, so we mark the NULL test with ALWAYS().
38066 */
38067 static void pcache1FreePage(PgHdr1 *p){
38068   if( ALWAYS(p) ){
38069     PCache1 *pCache = p->pCache;
38070     assert( sqlite3_mutex_held(p->pCache->pGroup->mutex) );
38071     pcache1Free(p->page.pBuf);
38072 #ifdef SQLITE_PCACHE_SEPARATE_HEADER
38073     sqlite3_free(p);
38074 #endif
38075     if( pCache->bPurgeable ){
38076       pCache->pGroup->nCurrentPage--;
38077     }
38078   }
38079 }
38080 
38081 /*
38082 ** Malloc function used by SQLite to obtain space from the buffer configured
38083 ** using sqlite3_config(SQLITE_CONFIG_PAGECACHE) option. If no such buffer
38084 ** exists, this function falls back to sqlite3Malloc().
38085 */
38086 SQLITE_PRIVATE void *sqlite3PageMalloc(int sz){
38087   return pcache1Alloc(sz);
38088 }
38089 
38090 /*
38091 ** Free an allocated buffer obtained from sqlite3PageMalloc().
38092 */
38093 SQLITE_PRIVATE void sqlite3PageFree(void *p){
38094   pcache1Free(p);
38095 }
38096 
38097 
38098 /*
38099 ** Return true if it desirable to avoid allocating a new page cache
38100 ** entry.
38101 **
38102 ** If memory was allocated specifically to the page cache using
38103 ** SQLITE_CONFIG_PAGECACHE but that memory has all been used, then
38104 ** it is desirable to avoid allocating a new page cache entry because
38105 ** presumably SQLITE_CONFIG_PAGECACHE was suppose to be sufficient
38106 ** for all page cache needs and we should not need to spill the
38107 ** allocation onto the heap.
38108 **
38109 ** Or, the heap is used for all page cache memory but the heap is
38110 ** under memory pressure, then again it is desirable to avoid
38111 ** allocating a new page cache entry in order to avoid stressing
38112 ** the heap even further.
38113 */
38114 static int pcache1UnderMemoryPressure(PCache1 *pCache){
38115   if( pcache1.nSlot && (pCache->szPage+pCache->szExtra)<=pcache1.szSlot ){
38116     return pcache1.bUnderPressure;
38117   }else{
38118     return sqlite3HeapNearlyFull();
38119   }
38120 }
38121 
38122 /******************************************************************************/
38123 /******** General Implementation Functions ************************************/
38124 
38125 /*
38126 ** This function is used to resize the hash table used by the cache passed
38127 ** as the first argument.
38128 **
38129 ** The PCache mutex must be held when this function is called.
38130 */
38131 static int pcache1ResizeHash(PCache1 *p){
38132   PgHdr1 **apNew;
38133   unsigned int nNew;
38134   unsigned int i;
38135 
38136   assert( sqlite3_mutex_held(p->pGroup->mutex) );
38137 
38138   nNew = p->nHash*2;
38139   if( nNew<256 ){
38140     nNew = 256;
38141   }
38142 
38143   pcache1LeaveMutex(p->pGroup);
38144   if( p->nHash ){ sqlite3BeginBenignMalloc(); }
38145   apNew = (PgHdr1 **)sqlite3MallocZero(sizeof(PgHdr1 *)*nNew);
38146   if( p->nHash ){ sqlite3EndBenignMalloc(); }
38147   pcache1EnterMutex(p->pGroup);
38148   if( apNew ){
38149     for(i=0; i<p->nHash; i++){
38150       PgHdr1 *pPage;
38151       PgHdr1 *pNext = p->apHash[i];
38152       while( (pPage = pNext)!=0 ){
38153         unsigned int h = pPage->iKey % nNew;
38154         pNext = pPage->pNext;
38155         pPage->pNext = apNew[h];
38156         apNew[h] = pPage;
38157       }
38158     }
38159     sqlite3_free(p->apHash);
38160     p->apHash = apNew;
38161     p->nHash = nNew;
38162   }
38163 
38164   return (p->apHash ? SQLITE_OK : SQLITE_NOMEM);
38165 }
38166 
38167 /*
38168 ** This function is used internally to remove the page pPage from the
38169 ** PGroup LRU list, if is part of it. If pPage is not part of the PGroup
38170 ** LRU list, then this function is a no-op.
38171 **
38172 ** The PGroup mutex must be held when this function is called.
38173 */
38174 static void pcache1PinPage(PgHdr1 *pPage){
38175   PCache1 *pCache;
38176   PGroup *pGroup;
38177 
38178   assert( pPage!=0 );
38179   assert( pPage->isPinned==0 );
38180   pCache = pPage->pCache;
38181   pGroup = pCache->pGroup;
38182   assert( pPage->pLruNext || pPage==pGroup->pLruTail );
38183   assert( pPage->pLruPrev || pPage==pGroup->pLruHead );
38184   assert( sqlite3_mutex_held(pGroup->mutex) );
38185   if( pPage->pLruPrev ){
38186     pPage->pLruPrev->pLruNext = pPage->pLruNext;
38187   }else{
38188     pGroup->pLruHead = pPage->pLruNext;
38189   }
38190   if( pPage->pLruNext ){
38191     pPage->pLruNext->pLruPrev = pPage->pLruPrev;
38192   }else{
38193     pGroup->pLruTail = pPage->pLruPrev;
38194   }
38195   pPage->pLruNext = 0;
38196   pPage->pLruPrev = 0;
38197   pPage->isPinned = 1;
38198   pCache->nRecyclable--;
38199 }
38200 
38201 
38202 /*
38203 ** Remove the page supplied as an argument from the hash table
38204 ** (PCache1.apHash structure) that it is currently stored in.
38205 **
38206 ** The PGroup mutex must be held when this function is called.
38207 */
38208 static void pcache1RemoveFromHash(PgHdr1 *pPage){
38209   unsigned int h;
38210   PCache1 *pCache = pPage->pCache;
38211   PgHdr1 **pp;
38212 
38213   assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
38214   h = pPage->iKey % pCache->nHash;
38215   for(pp=&pCache->apHash[h]; (*pp)!=pPage; pp=&(*pp)->pNext);
38216   *pp = (*pp)->pNext;
38217 
38218   pCache->nPage--;
38219 }
38220 
38221 /*
38222 ** If there are currently more than nMaxPage pages allocated, try
38223 ** to recycle pages to reduce the number allocated to nMaxPage.
38224 */
38225 static void pcache1EnforceMaxPage(PGroup *pGroup){
38226   assert( sqlite3_mutex_held(pGroup->mutex) );
38227   while( pGroup->nCurrentPage>pGroup->nMaxPage && pGroup->pLruTail ){
38228     PgHdr1 *p = pGroup->pLruTail;
38229     assert( p->pCache->pGroup==pGroup );
38230     assert( p->isPinned==0 );
38231     pcache1PinPage(p);
38232     pcache1RemoveFromHash(p);
38233     pcache1FreePage(p);
38234   }
38235 }
38236 
38237 /*
38238 ** Discard all pages from cache pCache with a page number (key value)
38239 ** greater than or equal to iLimit. Any pinned pages that meet this
38240 ** criteria are unpinned before they are discarded.
38241 **
38242 ** The PCache mutex must be held when this function is called.
38243 */
38244 static void pcache1TruncateUnsafe(
38245   PCache1 *pCache,             /* The cache to truncate */
38246   unsigned int iLimit          /* Drop pages with this pgno or larger */
38247 ){
38248   TESTONLY( unsigned int nPage = 0; )  /* To assert pCache->nPage is correct */
38249   unsigned int h;
38250   assert( sqlite3_mutex_held(pCache->pGroup->mutex) );
38251   for(h=0; h<pCache->nHash; h++){
38252     PgHdr1 **pp = &pCache->apHash[h];
38253     PgHdr1 *pPage;
38254     while( (pPage = *pp)!=0 ){
38255       if( pPage->iKey>=iLimit ){
38256         pCache->nPage--;
38257         *pp = pPage->pNext;
38258         if( !pPage->isPinned ) pcache1PinPage(pPage);
38259         pcache1FreePage(pPage);
38260       }else{
38261         pp = &pPage->pNext;
38262         TESTONLY( nPage++; )
38263       }
38264     }
38265   }
38266   assert( pCache->nPage==nPage );
38267 }
38268 
38269 /******************************************************************************/
38270 /******** sqlite3_pcache Methods **********************************************/
38271 
38272 /*
38273 ** Implementation of the sqlite3_pcache.xInit method.
38274 */
38275 static int pcache1Init(void *NotUsed){
38276   UNUSED_PARAMETER(NotUsed);
38277   assert( pcache1.isInit==0 );
38278   memset(&pcache1, 0, sizeof(pcache1));
38279   if( sqlite3GlobalConfig.bCoreMutex ){
38280     pcache1.grp.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU);
38281     pcache1.mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_PMEM);
38282   }
38283   pcache1.grp.mxPinned = 10;
38284   pcache1.isInit = 1;
38285   return SQLITE_OK;
38286 }
38287 
38288 /*
38289 ** Implementation of the sqlite3_pcache.xShutdown method.
38290 ** Note that the static mutex allocated in xInit does
38291 ** not need to be freed.
38292 */
38293 static void pcache1Shutdown(void *NotUsed){
38294   UNUSED_PARAMETER(NotUsed);
38295   assert( pcache1.isInit!=0 );
38296   memset(&pcache1, 0, sizeof(pcache1));
38297 }
38298 
38299 /*
38300 ** Implementation of the sqlite3_pcache.xCreate method.
38301 **
38302 ** Allocate a new cache.
38303 */
38304 static sqlite3_pcache *pcache1Create(int szPage, int szExtra, int bPurgeable){
38305   PCache1 *pCache;      /* The newly created page cache */
38306   PGroup *pGroup;       /* The group the new page cache will belong to */
38307   int sz;               /* Bytes of memory required to allocate the new cache */
38308 
38309   /*
38310   ** The separateCache variable is true if each PCache has its own private
38311   ** PGroup.  In other words, separateCache is true for mode (1) where no
38312   ** mutexing is required.
38313   **
38314   **   *  Always use a unified cache (mode-2) if ENABLE_MEMORY_MANAGEMENT
38315   **
38316   **   *  Always use a unified cache in single-threaded applications
38317   **
38318   **   *  Otherwise (if multi-threaded and ENABLE_MEMORY_MANAGEMENT is off)
38319   **      use separate caches (mode-1)
38320   */
38321 #if defined(SQLITE_ENABLE_MEMORY_MANAGEMENT) || SQLITE_THREADSAFE==0
38322   const int separateCache = 0;
38323 #else
38324   int separateCache = sqlite3GlobalConfig.bCoreMutex>0;
38325 #endif
38326 
38327   assert( (szPage & (szPage-1))==0 && szPage>=512 && szPage<=65536 );
38328   assert( szExtra < 300 );
38329 
38330   sz = sizeof(PCache1) + sizeof(PGroup)*separateCache;
38331   pCache = (PCache1 *)sqlite3MallocZero(sz);
38332   if( pCache ){
38333     if( separateCache ){
38334       pGroup = (PGroup*)&pCache[1];
38335       pGroup->mxPinned = 10;
38336     }else{
38337       pGroup = &pcache1.grp;
38338     }
38339     pCache->pGroup = pGroup;
38340     pCache->szPage = szPage;
38341     pCache->szExtra = szExtra;
38342     pCache->bPurgeable = (bPurgeable ? 1 : 0);
38343     if( bPurgeable ){
38344       pCache->nMin = 10;
38345       pcache1EnterMutex(pGroup);
38346       pGroup->nMinPage += pCache->nMin;
38347       pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
38348       pcache1LeaveMutex(pGroup);
38349     }
38350   }
38351   return (sqlite3_pcache *)pCache;
38352 }
38353 
38354 /*
38355 ** Implementation of the sqlite3_pcache.xCachesize method.
38356 **
38357 ** Configure the cache_size limit for a cache.
38358 */
38359 static void pcache1Cachesize(sqlite3_pcache *p, int nMax){
38360   PCache1 *pCache = (PCache1 *)p;
38361   if( pCache->bPurgeable ){
38362     PGroup *pGroup = pCache->pGroup;
38363     pcache1EnterMutex(pGroup);
38364     pGroup->nMaxPage += (nMax - pCache->nMax);
38365     pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
38366     pCache->nMax = nMax;
38367     pCache->n90pct = pCache->nMax*9/10;
38368     pcache1EnforceMaxPage(pGroup);
38369     pcache1LeaveMutex(pGroup);
38370   }
38371 }
38372 
38373 /*
38374 ** Implementation of the sqlite3_pcache.xShrink method.
38375 **
38376 ** Free up as much memory as possible.
38377 */
38378 static void pcache1Shrink(sqlite3_pcache *p){
38379   PCache1 *pCache = (PCache1*)p;
38380   if( pCache->bPurgeable ){
38381     PGroup *pGroup = pCache->pGroup;
38382     int savedMaxPage;
38383     pcache1EnterMutex(pGroup);
38384     savedMaxPage = pGroup->nMaxPage;
38385     pGroup->nMaxPage = 0;
38386     pcache1EnforceMaxPage(pGroup);
38387     pGroup->nMaxPage = savedMaxPage;
38388     pcache1LeaveMutex(pGroup);
38389   }
38390 }
38391 
38392 /*
38393 ** Implementation of the sqlite3_pcache.xPagecount method.
38394 */
38395 static int pcache1Pagecount(sqlite3_pcache *p){
38396   int n;
38397   PCache1 *pCache = (PCache1*)p;
38398   pcache1EnterMutex(pCache->pGroup);
38399   n = pCache->nPage;
38400   pcache1LeaveMutex(pCache->pGroup);
38401   return n;
38402 }
38403 
38404 /*
38405 ** Implementation of the sqlite3_pcache.xFetch method.
38406 **
38407 ** Fetch a page by key value.
38408 **
38409 ** Whether or not a new page may be allocated by this function depends on
38410 ** the value of the createFlag argument.  0 means do not allocate a new
38411 ** page.  1 means allocate a new page if space is easily available.  2
38412 ** means to try really hard to allocate a new page.
38413 **
38414 ** For a non-purgeable cache (a cache used as the storage for an in-memory
38415 ** database) there is really no difference between createFlag 1 and 2.  So
38416 ** the calling function (pcache.c) will never have a createFlag of 1 on
38417 ** a non-purgeable cache.
38418 **
38419 ** There are three different approaches to obtaining space for a page,
38420 ** depending on the value of parameter createFlag (which may be 0, 1 or 2).
38421 **
38422 **   1. Regardless of the value of createFlag, the cache is searched for a
38423 **      copy of the requested page. If one is found, it is returned.
38424 **
38425 **   2. If createFlag==0 and the page is not already in the cache, NULL is
38426 **      returned.
38427 **
38428 **   3. If createFlag is 1, and the page is not already in the cache, then
38429 **      return NULL (do not allocate a new page) if any of the following
38430 **      conditions are true:
38431 **
38432 **       (a) the number of pages pinned by the cache is greater than
38433 **           PCache1.nMax, or
38434 **
38435 **       (b) the number of pages pinned by the cache is greater than
38436 **           the sum of nMax for all purgeable caches, less the sum of
38437 **           nMin for all other purgeable caches, or
38438 **
38439 **   4. If none of the first three conditions apply and the cache is marked
38440 **      as purgeable, and if one of the following is true:
38441 **
38442 **       (a) The number of pages allocated for the cache is already
38443 **           PCache1.nMax, or
38444 **
38445 **       (b) The number of pages allocated for all purgeable caches is
38446 **           already equal to or greater than the sum of nMax for all
38447 **           purgeable caches,
38448 **
38449 **       (c) The system is under memory pressure and wants to avoid
38450 **           unnecessary pages cache entry allocations
38451 **
38452 **      then attempt to recycle a page from the LRU list. If it is the right
38453 **      size, return the recycled buffer. Otherwise, free the buffer and
38454 **      proceed to step 5.
38455 **
38456 **   5. Otherwise, allocate and return a new page buffer.
38457 */
38458 static sqlite3_pcache_page *pcache1Fetch(
38459   sqlite3_pcache *p,
38460   unsigned int iKey,
38461   int createFlag
38462 ){
38463   unsigned int nPinned;
38464   PCache1 *pCache = (PCache1 *)p;
38465   PGroup *pGroup;
38466   PgHdr1 *pPage = 0;
38467 
38468   assert( offsetof(PgHdr1,page)==0 );
38469   assert( pCache->bPurgeable || createFlag!=1 );
38470   assert( pCache->bPurgeable || pCache->nMin==0 );
38471   assert( pCache->bPurgeable==0 || pCache->nMin==10 );
38472   assert( pCache->nMin==0 || pCache->bPurgeable );
38473   pcache1EnterMutex(pGroup = pCache->pGroup);
38474 
38475   /* Step 1: Search the hash table for an existing entry. */
38476   if( pCache->nHash>0 ){
38477     unsigned int h = iKey % pCache->nHash;
38478     for(pPage=pCache->apHash[h]; pPage&&pPage->iKey!=iKey; pPage=pPage->pNext);
38479   }
38480 
38481   /* Step 2: Abort if no existing page is found and createFlag is 0 */
38482   if( pPage ){
38483     if( !pPage->isPinned ) pcache1PinPage(pPage);
38484     goto fetch_out;
38485   }
38486   if( createFlag==0 ){
38487     goto fetch_out;
38488   }
38489 
38490   /* The pGroup local variable will normally be initialized by the
38491   ** pcache1EnterMutex() macro above.  But if SQLITE_MUTEX_OMIT is defined,
38492   ** then pcache1EnterMutex() is a no-op, so we have to initialize the
38493   ** local variable here.  Delaying the initialization of pGroup is an
38494   ** optimization:  The common case is to exit the module before reaching
38495   ** this point.
38496   */
38497 #ifdef SQLITE_MUTEX_OMIT
38498   pGroup = pCache->pGroup;
38499 #endif
38500 
38501   /* Step 3: Abort if createFlag is 1 but the cache is nearly full */
38502   assert( pCache->nPage >= pCache->nRecyclable );
38503   nPinned = pCache->nPage - pCache->nRecyclable;
38504   assert( pGroup->mxPinned == pGroup->nMaxPage + 10 - pGroup->nMinPage );
38505   assert( pCache->n90pct == pCache->nMax*9/10 );
38506   if( createFlag==1 && (
38507         nPinned>=pGroup->mxPinned
38508      || nPinned>=pCache->n90pct
38509      || pcache1UnderMemoryPressure(pCache)
38510   )){
38511     goto fetch_out;
38512   }
38513 
38514   if( pCache->nPage>=pCache->nHash && pcache1ResizeHash(pCache) ){
38515     goto fetch_out;
38516   }
38517   assert( pCache->nHash>0 && pCache->apHash );
38518 
38519   /* Step 4. Try to recycle a page. */
38520   if( pCache->bPurgeable && pGroup->pLruTail && (
38521          (pCache->nPage+1>=pCache->nMax)
38522       || pGroup->nCurrentPage>=pGroup->nMaxPage
38523       || pcache1UnderMemoryPressure(pCache)
38524   )){
38525     PCache1 *pOther;
38526     pPage = pGroup->pLruTail;
38527     assert( pPage->isPinned==0 );
38528     pcache1RemoveFromHash(pPage);
38529     pcache1PinPage(pPage);
38530     pOther = pPage->pCache;
38531 
38532     /* We want to verify that szPage and szExtra are the same for pOther
38533     ** and pCache.  Assert that we can verify this by comparing sums. */
38534     assert( (pCache->szPage & (pCache->szPage-1))==0 && pCache->szPage>=512 );
38535     assert( pCache->szExtra<512 );
38536     assert( (pOther->szPage & (pOther->szPage-1))==0 && pOther->szPage>=512 );
38537     assert( pOther->szExtra<512 );
38538 
38539     if( pOther->szPage+pOther->szExtra != pCache->szPage+pCache->szExtra ){
38540       pcache1FreePage(pPage);
38541       pPage = 0;
38542     }else{
38543       pGroup->nCurrentPage -= (pOther->bPurgeable - pCache->bPurgeable);
38544     }
38545   }
38546 
38547   /* Step 5. If a usable page buffer has still not been found,
38548   ** attempt to allocate a new one.
38549   */
38550   if( !pPage ){
38551     if( createFlag==1 ) sqlite3BeginBenignMalloc();
38552     pPage = pcache1AllocPage(pCache);
38553     if( createFlag==1 ) sqlite3EndBenignMalloc();
38554   }
38555 
38556   if( pPage ){
38557     unsigned int h = iKey % pCache->nHash;
38558     pCache->nPage++;
38559     pPage->iKey = iKey;
38560     pPage->pNext = pCache->apHash[h];
38561     pPage->pCache = pCache;
38562     pPage->pLruPrev = 0;
38563     pPage->pLruNext = 0;
38564     pPage->isPinned = 1;
38565     *(void **)pPage->page.pExtra = 0;
38566     pCache->apHash[h] = pPage;
38567   }
38568 
38569 fetch_out:
38570   if( pPage && iKey>pCache->iMaxKey ){
38571     pCache->iMaxKey = iKey;
38572   }
38573   pcache1LeaveMutex(pGroup);
38574   return (sqlite3_pcache_page*)pPage;
38575 }
38576 
38577 
38578 /*
38579 ** Implementation of the sqlite3_pcache.xUnpin method.
38580 **
38581 ** Mark a page as unpinned (eligible for asynchronous recycling).
38582 */
38583 static void pcache1Unpin(
38584   sqlite3_pcache *p,
38585   sqlite3_pcache_page *pPg,
38586   int reuseUnlikely
38587 ){
38588   PCache1 *pCache = (PCache1 *)p;
38589   PgHdr1 *pPage = (PgHdr1 *)pPg;
38590   PGroup *pGroup = pCache->pGroup;
38591 
38592   assert( pPage->pCache==pCache );
38593   pcache1EnterMutex(pGroup);
38594 
38595   /* It is an error to call this function if the page is already
38596   ** part of the PGroup LRU list.
38597   */
38598   assert( pPage->pLruPrev==0 && pPage->pLruNext==0 );
38599   assert( pGroup->pLruHead!=pPage && pGroup->pLruTail!=pPage );
38600   assert( pPage->isPinned==1 );
38601 
38602   if( reuseUnlikely || pGroup->nCurrentPage>pGroup->nMaxPage ){
38603     pcache1RemoveFromHash(pPage);
38604     pcache1FreePage(pPage);
38605   }else{
38606     /* Add the page to the PGroup LRU list. */
38607     if( pGroup->pLruHead ){
38608       pGroup->pLruHead->pLruPrev = pPage;
38609       pPage->pLruNext = pGroup->pLruHead;
38610       pGroup->pLruHead = pPage;
38611     }else{
38612       pGroup->pLruTail = pPage;
38613       pGroup->pLruHead = pPage;
38614     }
38615     pCache->nRecyclable++;
38616     pPage->isPinned = 0;
38617   }
38618 
38619   pcache1LeaveMutex(pCache->pGroup);
38620 }
38621 
38622 /*
38623 ** Implementation of the sqlite3_pcache.xRekey method.
38624 */
38625 static void pcache1Rekey(
38626   sqlite3_pcache *p,
38627   sqlite3_pcache_page *pPg,
38628   unsigned int iOld,
38629   unsigned int iNew
38630 ){
38631   PCache1 *pCache = (PCache1 *)p;
38632   PgHdr1 *pPage = (PgHdr1 *)pPg;
38633   PgHdr1 **pp;
38634   unsigned int h;
38635   assert( pPage->iKey==iOld );
38636   assert( pPage->pCache==pCache );
38637 
38638   pcache1EnterMutex(pCache->pGroup);
38639 
38640   h = iOld%pCache->nHash;
38641   pp = &pCache->apHash[h];
38642   while( (*pp)!=pPage ){
38643     pp = &(*pp)->pNext;
38644   }
38645   *pp = pPage->pNext;
38646 
38647   h = iNew%pCache->nHash;
38648   pPage->iKey = iNew;
38649   pPage->pNext = pCache->apHash[h];
38650   pCache->apHash[h] = pPage;
38651   if( iNew>pCache->iMaxKey ){
38652     pCache->iMaxKey = iNew;
38653   }
38654 
38655   pcache1LeaveMutex(pCache->pGroup);
38656 }
38657 
38658 /*
38659 ** Implementation of the sqlite3_pcache.xTruncate method.
38660 **
38661 ** Discard all unpinned pages in the cache with a page number equal to
38662 ** or greater than parameter iLimit. Any pinned pages with a page number
38663 ** equal to or greater than iLimit are implicitly unpinned.
38664 */
38665 static void pcache1Truncate(sqlite3_pcache *p, unsigned int iLimit){
38666   PCache1 *pCache = (PCache1 *)p;
38667   pcache1EnterMutex(pCache->pGroup);
38668   if( iLimit<=pCache->iMaxKey ){
38669     pcache1TruncateUnsafe(pCache, iLimit);
38670     pCache->iMaxKey = iLimit-1;
38671   }
38672   pcache1LeaveMutex(pCache->pGroup);
38673 }
38674 
38675 /*
38676 ** Implementation of the sqlite3_pcache.xDestroy method.
38677 **
38678 ** Destroy a cache allocated using pcache1Create().
38679 */
38680 static void pcache1Destroy(sqlite3_pcache *p){
38681   PCache1 *pCache = (PCache1 *)p;
38682   PGroup *pGroup = pCache->pGroup;
38683   assert( pCache->bPurgeable || (pCache->nMax==0 && pCache->nMin==0) );
38684   pcache1EnterMutex(pGroup);
38685   pcache1TruncateUnsafe(pCache, 0);
38686   assert( pGroup->nMaxPage >= pCache->nMax );
38687   pGroup->nMaxPage -= pCache->nMax;
38688   assert( pGroup->nMinPage >= pCache->nMin );
38689   pGroup->nMinPage -= pCache->nMin;
38690   pGroup->mxPinned = pGroup->nMaxPage + 10 - pGroup->nMinPage;
38691   pcache1EnforceMaxPage(pGroup);
38692   pcache1LeaveMutex(pGroup);
38693   sqlite3_free(pCache->apHash);
38694   sqlite3_free(pCache);
38695 }
38696 
38697 /*
38698 ** This function is called during initialization (sqlite3_initialize()) to
38699 ** install the default pluggable cache module, assuming the user has not
38700 ** already provided an alternative.
38701 */
38702 SQLITE_PRIVATE void sqlite3PCacheSetDefault(void){
38703   static const sqlite3_pcache_methods2 defaultMethods = {
38704     1,                       /* iVersion */
38705     0,                       /* pArg */
38706     pcache1Init,             /* xInit */
38707     pcache1Shutdown,         /* xShutdown */
38708     pcache1Create,           /* xCreate */
38709     pcache1Cachesize,        /* xCachesize */
38710     pcache1Pagecount,        /* xPagecount */
38711     pcache1Fetch,            /* xFetch */
38712     pcache1Unpin,            /* xUnpin */
38713     pcache1Rekey,            /* xRekey */
38714     pcache1Truncate,         /* xTruncate */
38715     pcache1Destroy,          /* xDestroy */
38716     pcache1Shrink            /* xShrink */
38717   };
38718   sqlite3_config(SQLITE_CONFIG_PCACHE2, &defaultMethods);
38719 }
38720 
38721 #ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
38722 /*
38723 ** This function is called to free superfluous dynamically allocated memory
38724 ** held by the pager system. Memory in use by any SQLite pager allocated
38725 ** by the current thread may be sqlite3_free()ed.
38726 **
38727 ** nReq is the number of bytes of memory required. Once this much has
38728 ** been released, the function returns. The return value is the total number
38729 ** of bytes of memory released.
38730 */
38731 SQLITE_PRIVATE int sqlite3PcacheReleaseMemory(int nReq){
38732   int nFree = 0;
38733   assert( sqlite3_mutex_notheld(pcache1.grp.mutex) );
38734   assert( sqlite3_mutex_notheld(pcache1.mutex) );
38735   if( pcache1.pStart==0 ){
38736     PgHdr1 *p;
38737     pcache1EnterMutex(&pcache1.grp);
38738     while( (nReq<0 || nFree<nReq) && ((p=pcache1.grp.pLruTail)!=0) ){
38739       nFree += pcache1MemSize(p->page.pBuf);
38740 #ifdef SQLITE_PCACHE_SEPARATE_HEADER
38741       nFree += sqlite3MemSize(p);
38742 #endif
38743       assert( p->isPinned==0 );
38744       pcache1PinPage(p);
38745       pcache1RemoveFromHash(p);
38746       pcache1FreePage(p);
38747     }
38748     pcache1LeaveMutex(&pcache1.grp);
38749   }
38750   return nFree;
38751 }
38752 #endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */
38753 
38754 #ifdef SQLITE_TEST
38755 /*
38756 ** This function is used by test procedures to inspect the internal state
38757 ** of the global cache.
38758 */
38759 SQLITE_PRIVATE void sqlite3PcacheStats(
38760   int *pnCurrent,      /* OUT: Total number of pages cached */
38761   int *pnMax,          /* OUT: Global maximum cache size */
38762   int *pnMin,          /* OUT: Sum of PCache1.nMin for purgeable caches */
38763   int *pnRecyclable    /* OUT: Total number of pages available for recycling */
38764 ){
38765   PgHdr1 *p;
38766   int nRecyclable = 0;
38767   for(p=pcache1.grp.pLruHead; p; p=p->pLruNext){
38768     assert( p->isPinned==0 );
38769     nRecyclable++;
38770   }
38771   *pnCurrent = pcache1.grp.nCurrentPage;
38772   *pnMax = (int)pcache1.grp.nMaxPage;
38773   *pnMin = (int)pcache1.grp.nMinPage;
38774   *pnRecyclable = nRecyclable;
38775 }
38776 #endif
38777 
38778 /************** End of pcache1.c *********************************************/
38779 /************** Begin file rowset.c ******************************************/
38780 /*
38781 ** 2008 December 3
38782 **
38783 ** The author disclaims copyright to this source code.  In place of
38784 ** a legal notice, here is a blessing:
38785 **
38786 **    May you do good and not evil.
38787 **    May you find forgiveness for yourself and forgive others.
38788 **    May you share freely, never taking more than you give.
38789 **
38790 *************************************************************************
38791 **
38792 ** This module implements an object we call a "RowSet".
38793 **
38794 ** The RowSet object is a collection of rowids.  Rowids
38795 ** are inserted into the RowSet in an arbitrary order.  Inserts
38796 ** can be intermixed with tests to see if a given rowid has been
38797 ** previously inserted into the RowSet.
38798 **
38799 ** After all inserts are finished, it is possible to extract the
38800 ** elements of the RowSet in sorted order.  Once this extraction
38801 ** process has started, no new elements may be inserted.
38802 **
38803 ** Hence, the primitive operations for a RowSet are:
38804 **
38805 **    CREATE
38806 **    INSERT
38807 **    TEST
38808 **    SMALLEST
38809 **    DESTROY
38810 **
38811 ** The CREATE and DESTROY primitives are the constructor and destructor,
38812 ** obviously.  The INSERT primitive adds a new element to the RowSet.
38813 ** TEST checks to see if an element is already in the RowSet.  SMALLEST
38814 ** extracts the least value from the RowSet.
38815 **
38816 ** The INSERT primitive might allocate additional memory.  Memory is
38817 ** allocated in chunks so most INSERTs do no allocation.  There is an
38818 ** upper bound on the size of allocated memory.  No memory is freed
38819 ** until DESTROY.
38820 **
38821 ** The TEST primitive includes a "batch" number.  The TEST primitive
38822 ** will only see elements that were inserted before the last change
38823 ** in the batch number.  In other words, if an INSERT occurs between
38824 ** two TESTs where the TESTs have the same batch nubmer, then the
38825 ** value added by the INSERT will not be visible to the second TEST.
38826 ** The initial batch number is zero, so if the very first TEST contains
38827 ** a non-zero batch number, it will see all prior INSERTs.
38828 **
38829 ** No INSERTs may occurs after a SMALLEST.  An assertion will fail if
38830 ** that is attempted.
38831 **
38832 ** The cost of an INSERT is roughly constant.  (Sometime new memory
38833 ** has to be allocated on an INSERT.)  The cost of a TEST with a new
38834 ** batch number is O(NlogN) where N is the number of elements in the RowSet.
38835 ** The cost of a TEST using the same batch number is O(logN).  The cost
38836 ** of the first SMALLEST is O(NlogN).  Second and subsequent SMALLEST
38837 ** primitives are constant time.  The cost of DESTROY is O(N).
38838 **
38839 ** There is an added cost of O(N) when switching between TEST and
38840 ** SMALLEST primitives.
38841 */
38842 
38843 
38844 /*
38845 ** Target size for allocation chunks.
38846 */
38847 #define ROWSET_ALLOCATION_SIZE 1024
38848 
38849 /*
38850 ** The number of rowset entries per allocation chunk.
38851 */
38852 #define ROWSET_ENTRY_PER_CHUNK  \
38853                        ((ROWSET_ALLOCATION_SIZE-8)/sizeof(struct RowSetEntry))
38854 
38855 /*
38856 ** Each entry in a RowSet is an instance of the following object.
38857 **
38858 ** This same object is reused to store a linked list of trees of RowSetEntry
38859 ** objects.  In that alternative use, pRight points to the next entry
38860 ** in the list, pLeft points to the tree, and v is unused.  The
38861 ** RowSet.pForest value points to the head of this forest list.
38862 */
38863 struct RowSetEntry {
38864   i64 v;                        /* ROWID value for this entry */
38865   struct RowSetEntry *pRight;   /* Right subtree (larger entries) or list */
38866   struct RowSetEntry *pLeft;    /* Left subtree (smaller entries) */
38867 };
38868 
38869 /*
38870 ** RowSetEntry objects are allocated in large chunks (instances of the
38871 ** following structure) to reduce memory allocation overhead.  The
38872 ** chunks are kept on a linked list so that they can be deallocated
38873 ** when the RowSet is destroyed.
38874 */
38875 struct RowSetChunk {
38876   struct RowSetChunk *pNextChunk;        /* Next chunk on list of them all */
38877   struct RowSetEntry aEntry[ROWSET_ENTRY_PER_CHUNK]; /* Allocated entries */
38878 };
38879 
38880 /*
38881 ** A RowSet in an instance of the following structure.
38882 **
38883 ** A typedef of this structure if found in sqliteInt.h.
38884 */
38885 struct RowSet {
38886   struct RowSetChunk *pChunk;    /* List of all chunk allocations */
38887   sqlite3 *db;                   /* The database connection */
38888   struct RowSetEntry *pEntry;    /* List of entries using pRight */
38889   struct RowSetEntry *pLast;     /* Last entry on the pEntry list */
38890   struct RowSetEntry *pFresh;    /* Source of new entry objects */
38891   struct RowSetEntry *pForest;   /* List of binary trees of entries */
38892   u16 nFresh;                    /* Number of objects on pFresh */
38893   u8 rsFlags;                    /* Various flags */
38894   u8 iBatch;                     /* Current insert batch */
38895 };
38896 
38897 /*
38898 ** Allowed values for RowSet.rsFlags
38899 */
38900 #define ROWSET_SORTED  0x01   /* True if RowSet.pEntry is sorted */
38901 #define ROWSET_NEXT    0x02   /* True if sqlite3RowSetNext() has been called */
38902 
38903 /*
38904 ** Turn bulk memory into a RowSet object.  N bytes of memory
38905 ** are available at pSpace.  The db pointer is used as a memory context
38906 ** for any subsequent allocations that need to occur.
38907 ** Return a pointer to the new RowSet object.
38908 **
38909 ** It must be the case that N is sufficient to make a Rowset.  If not
38910 ** an assertion fault occurs.
38911 **
38912 ** If N is larger than the minimum, use the surplus as an initial
38913 ** allocation of entries available to be filled.
38914 */
38915 SQLITE_PRIVATE RowSet *sqlite3RowSetInit(sqlite3 *db, void *pSpace, unsigned int N){
38916   RowSet *p;
38917   assert( N >= ROUND8(sizeof(*p)) );
38918   p = pSpace;
38919   p->pChunk = 0;
38920   p->db = db;
38921   p->pEntry = 0;
38922   p->pLast = 0;
38923   p->pForest = 0;
38924   p->pFresh = (struct RowSetEntry*)(ROUND8(sizeof(*p)) + (char*)p);
38925   p->nFresh = (u16)((N - ROUND8(sizeof(*p)))/sizeof(struct RowSetEntry));
38926   p->rsFlags = ROWSET_SORTED;
38927   p->iBatch = 0;
38928   return p;
38929 }
38930 
38931 /*
38932 ** Deallocate all chunks from a RowSet.  This frees all memory that
38933 ** the RowSet has allocated over its lifetime.  This routine is
38934 ** the destructor for the RowSet.
38935 */
38936 SQLITE_PRIVATE void sqlite3RowSetClear(RowSet *p){
38937   struct RowSetChunk *pChunk, *pNextChunk;
38938   for(pChunk=p->pChunk; pChunk; pChunk = pNextChunk){
38939     pNextChunk = pChunk->pNextChunk;
38940     sqlite3DbFree(p->db, pChunk);
38941   }
38942   p->pChunk = 0;
38943   p->nFresh = 0;
38944   p->pEntry = 0;
38945   p->pLast = 0;
38946   p->pForest = 0;
38947   p->rsFlags = ROWSET_SORTED;
38948 }
38949 
38950 /*
38951 ** Allocate a new RowSetEntry object that is associated with the
38952 ** given RowSet.  Return a pointer to the new and completely uninitialized
38953 ** objected.
38954 **
38955 ** In an OOM situation, the RowSet.db->mallocFailed flag is set and this
38956 ** routine returns NULL.
38957 */
38958 static struct RowSetEntry *rowSetEntryAlloc(RowSet *p){
38959   assert( p!=0 );
38960   if( p->nFresh==0 ){
38961     struct RowSetChunk *pNew;
38962     pNew = sqlite3DbMallocRaw(p->db, sizeof(*pNew));
38963     if( pNew==0 ){
38964       return 0;
38965     }
38966     pNew->pNextChunk = p->pChunk;
38967     p->pChunk = pNew;
38968     p->pFresh = pNew->aEntry;
38969     p->nFresh = ROWSET_ENTRY_PER_CHUNK;
38970   }
38971   p->nFresh--;
38972   return p->pFresh++;
38973 }
38974 
38975 /*
38976 ** Insert a new value into a RowSet.
38977 **
38978 ** The mallocFailed flag of the database connection is set if a
38979 ** memory allocation fails.
38980 */
38981 SQLITE_PRIVATE void sqlite3RowSetInsert(RowSet *p, i64 rowid){
38982   struct RowSetEntry *pEntry;  /* The new entry */
38983   struct RowSetEntry *pLast;   /* The last prior entry */
38984 
38985   /* This routine is never called after sqlite3RowSetNext() */
38986   assert( p!=0 && (p->rsFlags & ROWSET_NEXT)==0 );
38987 
38988   pEntry = rowSetEntryAlloc(p);
38989   if( pEntry==0 ) return;
38990   pEntry->v = rowid;
38991   pEntry->pRight = 0;
38992   pLast = p->pLast;
38993   if( pLast ){
38994     if( (p->rsFlags & ROWSET_SORTED)!=0 && rowid<=pLast->v ){
38995       p->rsFlags &= ~ROWSET_SORTED;
38996     }
38997     pLast->pRight = pEntry;
38998   }else{
38999     p->pEntry = pEntry;
39000   }
39001   p->pLast = pEntry;
39002 }
39003 
39004 /*
39005 ** Merge two lists of RowSetEntry objects.  Remove duplicates.
39006 **
39007 ** The input lists are connected via pRight pointers and are
39008 ** assumed to each already be in sorted order.
39009 */
39010 static struct RowSetEntry *rowSetEntryMerge(
39011   struct RowSetEntry *pA,    /* First sorted list to be merged */
39012   struct RowSetEntry *pB     /* Second sorted list to be merged */
39013 ){
39014   struct RowSetEntry head;
39015   struct RowSetEntry *pTail;
39016 
39017   pTail = &head;
39018   while( pA && pB ){
39019     assert( pA->pRight==0 || pA->v<=pA->pRight->v );
39020     assert( pB->pRight==0 || pB->v<=pB->pRight->v );
39021     if( pA->v<pB->v ){
39022       pTail->pRight = pA;
39023       pA = pA->pRight;
39024       pTail = pTail->pRight;
39025     }else if( pB->v<pA->v ){
39026       pTail->pRight = pB;
39027       pB = pB->pRight;
39028       pTail = pTail->pRight;
39029     }else{
39030       pA = pA->pRight;
39031     }
39032   }
39033   if( pA ){
39034     assert( pA->pRight==0 || pA->v<=pA->pRight->v );
39035     pTail->pRight = pA;
39036   }else{
39037     assert( pB==0 || pB->pRight==0 || pB->v<=pB->pRight->v );
39038     pTail->pRight = pB;
39039   }
39040   return head.pRight;
39041 }
39042 
39043 /*
39044 ** Sort all elements on the list of RowSetEntry objects into order of
39045 ** increasing v.
39046 */
39047 static struct RowSetEntry *rowSetEntrySort(struct RowSetEntry *pIn){
39048   unsigned int i;
39049   struct RowSetEntry *pNext, *aBucket[40];
39050 
39051   memset(aBucket, 0, sizeof(aBucket));
39052   while( pIn ){
39053     pNext = pIn->pRight;
39054     pIn->pRight = 0;
39055     for(i=0; aBucket[i]; i++){
39056       pIn = rowSetEntryMerge(aBucket[i], pIn);
39057       aBucket[i] = 0;
39058     }
39059     aBucket[i] = pIn;
39060     pIn = pNext;
39061   }
39062   pIn = 0;
39063   for(i=0; i<sizeof(aBucket)/sizeof(aBucket[0]); i++){
39064     pIn = rowSetEntryMerge(pIn, aBucket[i]);
39065   }
39066   return pIn;
39067 }
39068 
39069 
39070 /*
39071 ** The input, pIn, is a binary tree (or subtree) of RowSetEntry objects.
39072 ** Convert this tree into a linked list connected by the pRight pointers
39073 ** and return pointers to the first and last elements of the new list.
39074 */
39075 static void rowSetTreeToList(
39076   struct RowSetEntry *pIn,         /* Root of the input tree */
39077   struct RowSetEntry **ppFirst,    /* Write head of the output list here */
39078   struct RowSetEntry **ppLast      /* Write tail of the output list here */
39079 ){
39080   assert( pIn!=0 );
39081   if( pIn->pLeft ){
39082     struct RowSetEntry *p;
39083     rowSetTreeToList(pIn->pLeft, ppFirst, &p);
39084     p->pRight = pIn;
39085   }else{
39086     *ppFirst = pIn;
39087   }
39088   if( pIn->pRight ){
39089     rowSetTreeToList(pIn->pRight, &pIn->pRight, ppLast);
39090   }else{
39091     *ppLast = pIn;
39092   }
39093   assert( (*ppLast)->pRight==0 );
39094 }
39095 
39096 
39097 /*
39098 ** Convert a sorted list of elements (connected by pRight) into a binary
39099 ** tree with depth of iDepth.  A depth of 1 means the tree contains a single
39100 ** node taken from the head of *ppList.  A depth of 2 means a tree with
39101 ** three nodes.  And so forth.
39102 **
39103 ** Use as many entries from the input list as required and update the
39104 ** *ppList to point to the unused elements of the list.  If the input
39105 ** list contains too few elements, then construct an incomplete tree
39106 ** and leave *ppList set to NULL.
39107 **
39108 ** Return a pointer to the root of the constructed binary tree.
39109 */
39110 static struct RowSetEntry *rowSetNDeepTree(
39111   struct RowSetEntry **ppList,
39112   int iDepth
39113 ){
39114   struct RowSetEntry *p;         /* Root of the new tree */
39115   struct RowSetEntry *pLeft;     /* Left subtree */
39116   if( *ppList==0 ){
39117     return 0;
39118   }
39119   if( iDepth==1 ){
39120     p = *ppList;
39121     *ppList = p->pRight;
39122     p->pLeft = p->pRight = 0;
39123     return p;
39124   }
39125   pLeft = rowSetNDeepTree(ppList, iDepth-1);
39126   p = *ppList;
39127   if( p==0 ){
39128     return pLeft;
39129   }
39130   p->pLeft = pLeft;
39131   *ppList = p->pRight;
39132   p->pRight = rowSetNDeepTree(ppList, iDepth-1);
39133   return p;
39134 }
39135 
39136 /*
39137 ** Convert a sorted list of elements into a binary tree. Make the tree
39138 ** as deep as it needs to be in order to contain the entire list.
39139 */
39140 static struct RowSetEntry *rowSetListToTree(struct RowSetEntry *pList){
39141   int iDepth;           /* Depth of the tree so far */
39142   struct RowSetEntry *p;       /* Current tree root */
39143   struct RowSetEntry *pLeft;   /* Left subtree */
39144 
39145   assert( pList!=0 );
39146   p = pList;
39147   pList = p->pRight;
39148   p->pLeft = p->pRight = 0;
39149   for(iDepth=1; pList; iDepth++){
39150     pLeft = p;
39151     p = pList;
39152     pList = p->pRight;
39153     p->pLeft = pLeft;
39154     p->pRight = rowSetNDeepTree(&pList, iDepth);
39155   }
39156   return p;
39157 }
39158 
39159 /*
39160 ** Take all the entries on p->pEntry and on the trees in p->pForest and
39161 ** sort them all together into one big ordered list on p->pEntry.
39162 **
39163 ** This routine should only be called once in the life of a RowSet.
39164 */
39165 static void rowSetToList(RowSet *p){
39166 
39167   /* This routine is called only once */
39168   assert( p!=0 && (p->rsFlags & ROWSET_NEXT)==0 );
39169 
39170   if( (p->rsFlags & ROWSET_SORTED)==0 ){
39171     p->pEntry = rowSetEntrySort(p->pEntry);
39172   }
39173 
39174   /* While this module could theoretically support it, sqlite3RowSetNext()
39175   ** is never called after sqlite3RowSetText() for the same RowSet.  So
39176   ** there is never a forest to deal with.  Should this change, simply
39177   ** remove the assert() and the #if 0. */
39178   assert( p->pForest==0 );
39179 #if 0
39180   while( p->pForest ){
39181     struct RowSetEntry *pTree = p->pForest->pLeft;
39182     if( pTree ){
39183       struct RowSetEntry *pHead, *pTail;
39184       rowSetTreeToList(pTree, &pHead, &pTail);
39185       p->pEntry = rowSetEntryMerge(p->pEntry, pHead);
39186     }
39187     p->pForest = p->pForest->pRight;
39188   }
39189 #endif
39190   p->rsFlags |= ROWSET_NEXT;  /* Verify this routine is never called again */
39191 }
39192 
39193 /*
39194 ** Extract the smallest element from the RowSet.
39195 ** Write the element into *pRowid.  Return 1 on success.  Return
39196 ** 0 if the RowSet is already empty.
39197 **
39198 ** After this routine has been called, the sqlite3RowSetInsert()
39199 ** routine may not be called again.
39200 */
39201 SQLITE_PRIVATE int sqlite3RowSetNext(RowSet *p, i64 *pRowid){
39202   assert( p!=0 );
39203 
39204   /* Merge the forest into a single sorted list on first call */
39205   if( (p->rsFlags & ROWSET_NEXT)==0 ) rowSetToList(p);
39206 
39207   /* Return the next entry on the list */
39208   if( p->pEntry ){
39209     *pRowid = p->pEntry->v;
39210     p->pEntry = p->pEntry->pRight;
39211     if( p->pEntry==0 ){
39212       sqlite3RowSetClear(p);
39213     }
39214     return 1;
39215   }else{
39216     return 0;
39217   }
39218 }
39219 
39220 /*
39221 ** Check to see if element iRowid was inserted into the rowset as
39222 ** part of any insert batch prior to iBatch.  Return 1 or 0.
39223 **
39224 ** If this is the first test of a new batch and if there exist entires
39225 ** on pRowSet->pEntry, then sort those entires into the forest at
39226 ** pRowSet->pForest so that they can be tested.
39227 */
39228 SQLITE_PRIVATE int sqlite3RowSetTest(RowSet *pRowSet, u8 iBatch, sqlite3_int64 iRowid){
39229   struct RowSetEntry *p, *pTree;
39230 
39231   /* This routine is never called after sqlite3RowSetNext() */
39232   assert( pRowSet!=0 && (pRowSet->rsFlags & ROWSET_NEXT)==0 );
39233 
39234   /* Sort entries into the forest on the first test of a new batch
39235   */
39236   if( iBatch!=pRowSet->iBatch ){
39237     p = pRowSet->pEntry;
39238     if( p ){
39239       struct RowSetEntry **ppPrevTree = &pRowSet->pForest;
39240       if( (pRowSet->rsFlags & ROWSET_SORTED)==0 ){
39241         p = rowSetEntrySort(p);
39242       }
39243       for(pTree = pRowSet->pForest; pTree; pTree=pTree->pRight){
39244         ppPrevTree = &pTree->pRight;
39245         if( pTree->pLeft==0 ){
39246           pTree->pLeft = rowSetListToTree(p);
39247           break;
39248         }else{
39249           struct RowSetEntry *pAux, *pTail;
39250           rowSetTreeToList(pTree->pLeft, &pAux, &pTail);
39251           pTree->pLeft = 0;
39252           p = rowSetEntryMerge(pAux, p);
39253         }
39254       }
39255       if( pTree==0 ){
39256         *ppPrevTree = pTree = rowSetEntryAlloc(pRowSet);
39257         if( pTree ){
39258           pTree->v = 0;
39259           pTree->pRight = 0;
39260           pTree->pLeft = rowSetListToTree(p);
39261         }
39262       }
39263       pRowSet->pEntry = 0;
39264       pRowSet->pLast = 0;
39265       pRowSet->rsFlags |= ROWSET_SORTED;
39266     }
39267     pRowSet->iBatch = iBatch;
39268   }
39269 
39270   /* Test to see if the iRowid value appears anywhere in the forest.
39271   ** Return 1 if it does and 0 if not.
39272   */
39273   for(pTree = pRowSet->pForest; pTree; pTree=pTree->pRight){
39274     p = pTree->pLeft;
39275     while( p ){
39276       if( p->v<iRowid ){
39277         p = p->pRight;
39278       }else if( p->v>iRowid ){
39279         p = p->pLeft;
39280       }else{
39281         return 1;
39282       }
39283     }
39284   }
39285   return 0;
39286 }
39287 
39288 /************** End of rowset.c **********************************************/
39289 /************** Begin file pager.c *******************************************/
39290 /*
39291 ** 2001 September 15
39292 **
39293 ** The author disclaims copyright to this source code.  In place of
39294 ** a legal notice, here is a blessing:
39295 **
39296 **    May you do good and not evil.
39297 **    May you find forgiveness for yourself and forgive others.
39298 **    May you share freely, never taking more than you give.
39299 **
39300 *************************************************************************
39301 ** This is the implementation of the page cache subsystem or "pager".
39302 **
39303 ** The pager is used to access a database disk file.  It implements
39304 ** atomic commit and rollback through the use of a journal file that
39305 ** is separate from the database file.  The pager also implements file
39306 ** locking to prevent two processes from writing the same database
39307 ** file simultaneously, or one process from reading the database while
39308 ** another is writing.
39309 */
39310 #ifndef SQLITE_OMIT_DISKIO
39311 /************** Include wal.h in the middle of pager.c ***********************/
39312 /************** Begin file wal.h *********************************************/
39313 /*
39314 ** 2010 February 1
39315 **
39316 ** The author disclaims copyright to this source code.  In place of
39317 ** a legal notice, here is a blessing:
39318 **
39319 **    May you do good and not evil.
39320 **    May you find forgiveness for yourself and forgive others.
39321 **    May you share freely, never taking more than you give.
39322 **
39323 *************************************************************************
39324 ** This header file defines the interface to the write-ahead logging
39325 ** system. Refer to the comments below and the header comment attached to
39326 ** the implementation of each function in log.c for further details.
39327 */
39328 
39329 #ifndef _WAL_H_
39330 #define _WAL_H_
39331 
39332 
39333 /* Additional values that can be added to the sync_flags argument of
39334 ** sqlite3WalFrames():
39335 */
39336 #define WAL_SYNC_TRANSACTIONS  0x20   /* Sync at the end of each transaction */
39337 #define SQLITE_SYNC_MASK       0x13   /* Mask off the SQLITE_SYNC_* values */
39338 
39339 #ifdef SQLITE_OMIT_WAL
39340 # define sqlite3WalOpen(x,y,z)                   0
39341 # define sqlite3WalLimit(x,y)
39342 # define sqlite3WalClose(w,x,y,z)                0
39343 # define sqlite3WalBeginReadTransaction(y,z)     0
39344 # define sqlite3WalEndReadTransaction(z)
39345 # define sqlite3WalDbsize(y)                     0
39346 # define sqlite3WalBeginWriteTransaction(y)      0
39347 # define sqlite3WalEndWriteTransaction(x)        0
39348 # define sqlite3WalUndo(x,y,z)                   0
39349 # define sqlite3WalSavepoint(y,z)
39350 # define sqlite3WalSavepointUndo(y,z)            0
39351 # define sqlite3WalFrames(u,v,w,x,y,z)           0
39352 # define sqlite3WalCheckpoint(r,s,t,u,v,w,x,y,z) 0
39353 # define sqlite3WalCallback(z)                   0
39354 # define sqlite3WalExclusiveMode(y,z)            0
39355 # define sqlite3WalHeapMemory(z)                 0
39356 # define sqlite3WalFramesize(z)                  0
39357 # define sqlite3WalFindFrame(x,y,z)              0
39358 #else
39359 
39360 #define WAL_SAVEPOINT_NDATA 4
39361 
39362 /* Connection to a write-ahead log (WAL) file.
39363 ** There is one object of this type for each pager.
39364 */
39365 typedef struct Wal Wal;
39366 
39367 /* Open and close a connection to a write-ahead log. */
39368 SQLITE_PRIVATE int sqlite3WalOpen(sqlite3_vfs*, sqlite3_file*, const char *, int, i64, Wal**);
39369 SQLITE_PRIVATE int sqlite3WalClose(Wal *pWal, int sync_flags, int, u8 *);
39370 
39371 /* Set the limiting size of a WAL file. */
39372 SQLITE_PRIVATE void sqlite3WalLimit(Wal*, i64);
39373 
39374 /* Used by readers to open (lock) and close (unlock) a snapshot.  A
39375 ** snapshot is like a read-transaction.  It is the state of the database
39376 ** at an instant in time.  sqlite3WalOpenSnapshot gets a read lock and
39377 ** preserves the current state even if the other threads or processes
39378 ** write to or checkpoint the WAL.  sqlite3WalCloseSnapshot() closes the
39379 ** transaction and releases the lock.
39380 */
39381 SQLITE_PRIVATE int sqlite3WalBeginReadTransaction(Wal *pWal, int *);
39382 SQLITE_PRIVATE void sqlite3WalEndReadTransaction(Wal *pWal);
39383 
39384 /* Read a page from the write-ahead log, if it is present. */
39385 SQLITE_PRIVATE int sqlite3WalFindFrame(Wal *, Pgno, u32 *);
39386 SQLITE_PRIVATE int sqlite3WalReadFrame(Wal *, u32, int, u8 *);
39387 
39388 /* If the WAL is not empty, return the size of the database. */
39389 SQLITE_PRIVATE Pgno sqlite3WalDbsize(Wal *pWal);
39390 
39391 /* Obtain or release the WRITER lock. */
39392 SQLITE_PRIVATE int sqlite3WalBeginWriteTransaction(Wal *pWal);
39393 SQLITE_PRIVATE int sqlite3WalEndWriteTransaction(Wal *pWal);
39394 
39395 /* Undo any frames written (but not committed) to the log */
39396 SQLITE_PRIVATE int sqlite3WalUndo(Wal *pWal, int (*xUndo)(void *, Pgno), void *pUndoCtx);
39397 
39398 /* Return an integer that records the current (uncommitted) write
39399 ** position in the WAL */
39400 SQLITE_PRIVATE void sqlite3WalSavepoint(Wal *pWal, u32 *aWalData);
39401 
39402 /* Move the write position of the WAL back to iFrame.  Called in
39403 ** response to a ROLLBACK TO command. */
39404 SQLITE_PRIVATE int sqlite3WalSavepointUndo(Wal *pWal, u32 *aWalData);
39405 
39406 /* Write a frame or frames to the log. */
39407 SQLITE_PRIVATE int sqlite3WalFrames(Wal *pWal, int, PgHdr *, Pgno, int, int);
39408 
39409 /* Copy pages from the log to the database file */
39410 SQLITE_PRIVATE int sqlite3WalCheckpoint(
39411   Wal *pWal,                      /* Write-ahead log connection */
39412   int eMode,                      /* One of PASSIVE, FULL and RESTART */
39413   int (*xBusy)(void*),            /* Function to call when busy */
39414   void *pBusyArg,                 /* Context argument for xBusyHandler */
39415   int sync_flags,                 /* Flags to sync db file with (or 0) */
39416   int nBuf,                       /* Size of buffer nBuf */
39417   u8 *zBuf,                       /* Temporary buffer to use */
39418   int *pnLog,                     /* OUT: Number of frames in WAL */
39419   int *pnCkpt                     /* OUT: Number of backfilled frames in WAL */
39420 );
39421 
39422 /* Return the value to pass to a sqlite3_wal_hook callback, the
39423 ** number of frames in the WAL at the point of the last commit since
39424 ** sqlite3WalCallback() was called.  If no commits have occurred since
39425 ** the last call, then return 0.
39426 */
39427 SQLITE_PRIVATE int sqlite3WalCallback(Wal *pWal);
39428 
39429 /* Tell the wal layer that an EXCLUSIVE lock has been obtained (or released)
39430 ** by the pager layer on the database file.
39431 */
39432 SQLITE_PRIVATE int sqlite3WalExclusiveMode(Wal *pWal, int op);
39433 
39434 /* Return true if the argument is non-NULL and the WAL module is using
39435 ** heap-memory for the wal-index. Otherwise, if the argument is NULL or the
39436 ** WAL module is using shared-memory, return false.
39437 */
39438 SQLITE_PRIVATE int sqlite3WalHeapMemory(Wal *pWal);
39439 
39440 #ifdef SQLITE_ENABLE_ZIPVFS
39441 /* If the WAL file is not empty, return the number of bytes of content
39442 ** stored in each frame (i.e. the db page-size when the WAL was created).
39443 */
39444 SQLITE_PRIVATE int sqlite3WalFramesize(Wal *pWal);
39445 #endif
39446 
39447 #endif /* ifndef SQLITE_OMIT_WAL */
39448 #endif /* _WAL_H_ */
39449 
39450 /************** End of wal.h *************************************************/
39451 /************** Continuing where we left off in pager.c **********************/
39452 
39453 
39454 /******************* NOTES ON THE DESIGN OF THE PAGER ************************
39455 **
39456 ** This comment block describes invariants that hold when using a rollback
39457 ** journal.  These invariants do not apply for journal_mode=WAL,
39458 ** journal_mode=MEMORY, or journal_mode=OFF.
39459 **
39460 ** Within this comment block, a page is deemed to have been synced
39461 ** automatically as soon as it is written when PRAGMA synchronous=OFF.
39462 ** Otherwise, the page is not synced until the xSync method of the VFS
39463 ** is called successfully on the file containing the page.
39464 **
39465 ** Definition:  A page of the database file is said to be "overwriteable" if
39466 ** one or more of the following are true about the page:
39467 **
39468 **     (a)  The original content of the page as it was at the beginning of
39469 **          the transaction has been written into the rollback journal and
39470 **          synced.
39471 **
39472 **     (b)  The page was a freelist leaf page at the start of the transaction.
39473 **
39474 **     (c)  The page number is greater than the largest page that existed in
39475 **          the database file at the start of the transaction.
39476 **
39477 ** (1) A page of the database file is never overwritten unless one of the
39478 **     following are true:
39479 **
39480 **     (a) The page and all other pages on the same sector are overwriteable.
39481 **
39482 **     (b) The atomic page write optimization is enabled, and the entire
39483 **         transaction other than the update of the transaction sequence
39484 **         number consists of a single page change.
39485 **
39486 ** (2) The content of a page written into the rollback journal exactly matches
39487 **     both the content in the database when the rollback journal was written
39488 **     and the content in the database at the beginning of the current
39489 **     transaction.
39490 **
39491 ** (3) Writes to the database file are an integer multiple of the page size
39492 **     in length and are aligned on a page boundary.
39493 **
39494 ** (4) Reads from the database file are either aligned on a page boundary and
39495 **     an integer multiple of the page size in length or are taken from the
39496 **     first 100 bytes of the database file.
39497 **
39498 ** (5) All writes to the database file are synced prior to the rollback journal
39499 **     being deleted, truncated, or zeroed.
39500 **
39501 ** (6) If a master journal file is used, then all writes to the database file
39502 **     are synced prior to the master journal being deleted.
39503 **
39504 ** Definition: Two databases (or the same database at two points it time)
39505 ** are said to be "logically equivalent" if they give the same answer to
39506 ** all queries.  Note in particular the content of freelist leaf
39507 ** pages can be changed arbitarily without effecting the logical equivalence
39508 ** of the database.
39509 **
39510 ** (7) At any time, if any subset, including the empty set and the total set,
39511 **     of the unsynced changes to a rollback journal are removed and the
39512 **     journal is rolled back, the resulting database file will be logical
39513 **     equivalent to the database file at the beginning of the transaction.
39514 **
39515 ** (8) When a transaction is rolled back, the xTruncate method of the VFS
39516 **     is called to restore the database file to the same size it was at
39517 **     the beginning of the transaction.  (In some VFSes, the xTruncate
39518 **     method is a no-op, but that does not change the fact the SQLite will
39519 **     invoke it.)
39520 **
39521 ** (9) Whenever the database file is modified, at least one bit in the range
39522 **     of bytes from 24 through 39 inclusive will be changed prior to releasing
39523 **     the EXCLUSIVE lock, thus signaling other connections on the same
39524 **     database to flush their caches.
39525 **
39526 ** (10) The pattern of bits in bytes 24 through 39 shall not repeat in less
39527 **      than one billion transactions.
39528 **
39529 ** (11) A database file is well-formed at the beginning and at the conclusion
39530 **      of every transaction.
39531 **
39532 ** (12) An EXCLUSIVE lock is held on the database file when writing to
39533 **      the database file.
39534 **
39535 ** (13) A SHARED lock is held on the database file while reading any
39536 **      content out of the database file.
39537 **
39538 ******************************************************************************/
39539 
39540 /*
39541 ** Macros for troubleshooting.  Normally turned off
39542 */
39543 #if 0
39544 int sqlite3PagerTrace=1;  /* True to enable tracing */
39545 #define sqlite3DebugPrintf printf
39546 #define PAGERTRACE(X)     if( sqlite3PagerTrace ){ sqlite3DebugPrintf X; }
39547 #else
39548 #define PAGERTRACE(X)
39549 #endif
39550 
39551 /*
39552 ** The following two macros are used within the PAGERTRACE() macros above
39553 ** to print out file-descriptors.
39554 **
39555 ** PAGERID() takes a pointer to a Pager struct as its argument. The
39556 ** associated file-descriptor is returned. FILEHANDLEID() takes an sqlite3_file
39557 ** struct as its argument.
39558 */
39559 #define PAGERID(p) ((int)(p->fd))
39560 #define FILEHANDLEID(fd) ((int)fd)
39561 
39562 /*
39563 ** The Pager.eState variable stores the current 'state' of a pager. A
39564 ** pager may be in any one of the seven states shown in the following
39565 ** state diagram.
39566 **
39567 **                            OPEN <------+------+
39568 **                              |         |      |
39569 **                              V         |      |
39570 **               +---------> READER-------+      |
39571 **               |              |                |
39572 **               |              V                |
39573 **               |<-------WRITER_LOCKED------> ERROR
39574 **               |              |                ^
39575 **               |              V                |
39576 **               |<------WRITER_CACHEMOD-------->|
39577 **               |              |                |
39578 **               |              V                |
39579 **               |<-------WRITER_DBMOD---------->|
39580 **               |              |                |
39581 **               |              V                |
39582 **               +<------WRITER_FINISHED-------->+
39583 **
39584 **
39585 ** List of state transitions and the C [function] that performs each:
39586 **
39587 **   OPEN              -> READER              [sqlite3PagerSharedLock]
39588 **   READER            -> OPEN                [pager_unlock]
39589 **
39590 **   READER            -> WRITER_LOCKED       [sqlite3PagerBegin]
39591 **   WRITER_LOCKED     -> WRITER_CACHEMOD     [pager_open_journal]
39592 **   WRITER_CACHEMOD   -> WRITER_DBMOD        [syncJournal]
39593 **   WRITER_DBMOD      -> WRITER_FINISHED     [sqlite3PagerCommitPhaseOne]
39594 **   WRITER_***        -> READER              [pager_end_transaction]
39595 **
39596 **   WRITER_***        -> ERROR               [pager_error]
39597 **   ERROR             -> OPEN                [pager_unlock]
39598 **
39599 **
39600 **  OPEN:
39601 **
39602 **    The pager starts up in this state. Nothing is guaranteed in this
39603 **    state - the file may or may not be locked and the database size is
39604 **    unknown. The database may not be read or written.
39605 **
39606 **    * No read or write transaction is active.
39607 **    * Any lock, or no lock at all, may be held on the database file.
39608 **    * The dbSize, dbOrigSize and dbFileSize variables may not be trusted.
39609 **
39610 **  READER:
39611 **
39612 **    In this state all the requirements for reading the database in
39613 **    rollback (non-WAL) mode are met. Unless the pager is (or recently
39614 **    was) in exclusive-locking mode, a user-level read transaction is
39615 **    open. The database size is known in this state.
39616 **
39617 **    A connection running with locking_mode=normal enters this state when
39618 **    it opens a read-transaction on the database and returns to state
39619 **    OPEN after the read-transaction is completed. However a connection
39620 **    running in locking_mode=exclusive (including temp databases) remains in
39621 **    this state even after the read-transaction is closed. The only way
39622 **    a locking_mode=exclusive connection can transition from READER to OPEN
39623 **    is via the ERROR state (see below).
39624 **
39625 **    * A read transaction may be active (but a write-transaction cannot).
39626 **    * A SHARED or greater lock is held on the database file.
39627 **    * The dbSize variable may be trusted (even if a user-level read
39628 **      transaction is not active). The dbOrigSize and dbFileSize variables
39629 **      may not be trusted at this point.
39630 **    * If the database is a WAL database, then the WAL connection is open.
39631 **    * Even if a read-transaction is not open, it is guaranteed that
39632 **      there is no hot-journal in the file-system.
39633 **
39634 **  WRITER_LOCKED:
39635 **
39636 **    The pager moves to this state from READER when a write-transaction
39637 **    is first opened on the database. In WRITER_LOCKED state, all locks
39638 **    required to start a write-transaction are held, but no actual
39639 **    modifications to the cache or database have taken place.
39640 **
39641 **    In rollback mode, a RESERVED or (if the transaction was opened with
39642 **    BEGIN EXCLUSIVE) EXCLUSIVE lock is obtained on the database file when
39643 **    moving to this state, but the journal file is not written to or opened
39644 **    to in this state. If the transaction is committed or rolled back while
39645 **    in WRITER_LOCKED state, all that is required is to unlock the database
39646 **    file.
39647 **
39648 **    IN WAL mode, WalBeginWriteTransaction() is called to lock the log file.
39649 **    If the connection is running with locking_mode=exclusive, an attempt
39650 **    is made to obtain an EXCLUSIVE lock on the database file.
39651 **
39652 **    * A write transaction is active.
39653 **    * If the connection is open in rollback-mode, a RESERVED or greater
39654 **      lock is held on the database file.
39655 **    * If the connection is open in WAL-mode, a WAL write transaction
39656 **      is open (i.e. sqlite3WalBeginWriteTransaction() has been successfully
39657 **      called).
39658 **    * The dbSize, dbOrigSize and dbFileSize variables are all valid.
39659 **    * The contents of the pager cache have not been modified.
39660 **    * The journal file may or may not be open.
39661 **    * Nothing (not even the first header) has been written to the journal.
39662 **
39663 **  WRITER_CACHEMOD:
39664 **
39665 **    A pager moves from WRITER_LOCKED state to this state when a page is
39666 **    first modified by the upper layer. In rollback mode the journal file
39667 **    is opened (if it is not already open) and a header written to the
39668 **    start of it. The database file on disk has not been modified.
39669 **
39670 **    * A write transaction is active.
39671 **    * A RESERVED or greater lock is held on the database file.
39672 **    * The journal file is open and the first header has been written
39673 **      to it, but the header has not been synced to disk.
39674 **    * The contents of the page cache have been modified.
39675 **
39676 **  WRITER_DBMOD:
39677 **
39678 **    The pager transitions from WRITER_CACHEMOD into WRITER_DBMOD state
39679 **    when it modifies the contents of the database file. WAL connections
39680 **    never enter this state (since they do not modify the database file,
39681 **    just the log file).
39682 **
39683 **    * A write transaction is active.
39684 **    * An EXCLUSIVE or greater lock is held on the database file.
39685 **    * The journal file is open and the first header has been written
39686 **      and synced to disk.
39687 **    * The contents of the page cache have been modified (and possibly
39688 **      written to disk).
39689 **
39690 **  WRITER_FINISHED:
39691 **
39692 **    It is not possible for a WAL connection to enter this state.
39693 **
39694 **    A rollback-mode pager changes to WRITER_FINISHED state from WRITER_DBMOD
39695 **    state after the entire transaction has been successfully written into the
39696 **    database file. In this state the transaction may be committed simply
39697 **    by finalizing the journal file. Once in WRITER_FINISHED state, it is
39698 **    not possible to modify the database further. At this point, the upper
39699 **    layer must either commit or rollback the transaction.
39700 **
39701 **    * A write transaction is active.
39702 **    * An EXCLUSIVE or greater lock is held on the database file.
39703 **    * All writing and syncing of journal and database data has finished.
39704 **      If no error occurred, all that remains is to finalize the journal to
39705 **      commit the transaction. If an error did occur, the caller will need
39706 **      to rollback the transaction.
39707 **
39708 **  ERROR:
39709 **
39710 **    The ERROR state is entered when an IO or disk-full error (including
39711 **    SQLITE_IOERR_NOMEM) occurs at a point in the code that makes it
39712 **    difficult to be sure that the in-memory pager state (cache contents,
39713 **    db size etc.) are consistent with the contents of the file-system.
39714 **
39715 **    Temporary pager files may enter the ERROR state, but in-memory pagers
39716 **    cannot.
39717 **
39718 **    For example, if an IO error occurs while performing a rollback,
39719 **    the contents of the page-cache may be left in an inconsistent state.
39720 **    At this point it would be dangerous to change back to READER state
39721 **    (as usually happens after a rollback). Any subsequent readers might
39722 **    report database corruption (due to the inconsistent cache), and if
39723 **    they upgrade to writers, they may inadvertently corrupt the database
39724 **    file. To avoid this hazard, the pager switches into the ERROR state
39725 **    instead of READER following such an error.
39726 **
39727 **    Once it has entered the ERROR state, any attempt to use the pager
39728 **    to read or write data returns an error. Eventually, once all
39729 **    outstanding transactions have been abandoned, the pager is able to
39730 **    transition back to OPEN state, discarding the contents of the
39731 **    page-cache and any other in-memory state at the same time. Everything
39732 **    is reloaded from disk (and, if necessary, hot-journal rollback peformed)
39733 **    when a read-transaction is next opened on the pager (transitioning
39734 **    the pager into READER state). At that point the system has recovered
39735 **    from the error.
39736 **
39737 **    Specifically, the pager jumps into the ERROR state if:
39738 **
39739 **      1. An error occurs while attempting a rollback. This happens in
39740 **         function sqlite3PagerRollback().
39741 **
39742 **      2. An error occurs while attempting to finalize a journal file
39743 **         following a commit in function sqlite3PagerCommitPhaseTwo().
39744 **
39745 **      3. An error occurs while attempting to write to the journal or
39746 **         database file in function pagerStress() in order to free up
39747 **         memory.
39748 **
39749 **    In other cases, the error is returned to the b-tree layer. The b-tree
39750 **    layer then attempts a rollback operation. If the error condition
39751 **    persists, the pager enters the ERROR state via condition (1) above.
39752 **
39753 **    Condition (3) is necessary because it can be triggered by a read-only
39754 **    statement executed within a transaction. In this case, if the error
39755 **    code were simply returned to the user, the b-tree layer would not
39756 **    automatically attempt a rollback, as it assumes that an error in a
39757 **    read-only statement cannot leave the pager in an internally inconsistent
39758 **    state.
39759 **
39760 **    * The Pager.errCode variable is set to something other than SQLITE_OK.
39761 **    * There are one or more outstanding references to pages (after the
39762 **      last reference is dropped the pager should move back to OPEN state).
39763 **    * The pager is not an in-memory pager.
39764 **
39765 **
39766 ** Notes:
39767 **
39768 **   * A pager is never in WRITER_DBMOD or WRITER_FINISHED state if the
39769 **     connection is open in WAL mode. A WAL connection is always in one
39770 **     of the first four states.
39771 **
39772 **   * Normally, a connection open in exclusive mode is never in PAGER_OPEN
39773 **     state. There are two exceptions: immediately after exclusive-mode has
39774 **     been turned on (and before any read or write transactions are
39775 **     executed), and when the pager is leaving the "error state".
39776 **
39777 **   * See also: assert_pager_state().
39778 */
39779 #define PAGER_OPEN                  0
39780 #define PAGER_READER                1
39781 #define PAGER_WRITER_LOCKED         2
39782 #define PAGER_WRITER_CACHEMOD       3
39783 #define PAGER_WRITER_DBMOD          4
39784 #define PAGER_WRITER_FINISHED       5
39785 #define PAGER_ERROR                 6
39786 
39787 /*
39788 ** The Pager.eLock variable is almost always set to one of the
39789 ** following locking-states, according to the lock currently held on
39790 ** the database file: NO_LOCK, SHARED_LOCK, RESERVED_LOCK or EXCLUSIVE_LOCK.
39791 ** This variable is kept up to date as locks are taken and released by
39792 ** the pagerLockDb() and pagerUnlockDb() wrappers.
39793 **
39794 ** If the VFS xLock() or xUnlock() returns an error other than SQLITE_BUSY
39795 ** (i.e. one of the SQLITE_IOERR subtypes), it is not clear whether or not
39796 ** the operation was successful. In these circumstances pagerLockDb() and
39797 ** pagerUnlockDb() take a conservative approach - eLock is always updated
39798 ** when unlocking the file, and only updated when locking the file if the
39799 ** VFS call is successful. This way, the Pager.eLock variable may be set
39800 ** to a less exclusive (lower) value than the lock that is actually held
39801 ** at the system level, but it is never set to a more exclusive value.
39802 **
39803 ** This is usually safe. If an xUnlock fails or appears to fail, there may
39804 ** be a few redundant xLock() calls or a lock may be held for longer than
39805 ** required, but nothing really goes wrong.
39806 **
39807 ** The exception is when the database file is unlocked as the pager moves
39808 ** from ERROR to OPEN state. At this point there may be a hot-journal file
39809 ** in the file-system that needs to be rolled back (as part of a OPEN->SHARED
39810 ** transition, by the same pager or any other). If the call to xUnlock()
39811 ** fails at this point and the pager is left holding an EXCLUSIVE lock, this
39812 ** can confuse the call to xCheckReservedLock() call made later as part
39813 ** of hot-journal detection.
39814 **
39815 ** xCheckReservedLock() is defined as returning true "if there is a RESERVED
39816 ** lock held by this process or any others". So xCheckReservedLock may
39817 ** return true because the caller itself is holding an EXCLUSIVE lock (but
39818 ** doesn't know it because of a previous error in xUnlock). If this happens
39819 ** a hot-journal may be mistaken for a journal being created by an active
39820 ** transaction in another process, causing SQLite to read from the database
39821 ** without rolling it back.
39822 **
39823 ** To work around this, if a call to xUnlock() fails when unlocking the
39824 ** database in the ERROR state, Pager.eLock is set to UNKNOWN_LOCK. It
39825 ** is only changed back to a real locking state after a successful call
39826 ** to xLock(EXCLUSIVE). Also, the code to do the OPEN->SHARED state transition
39827 ** omits the check for a hot-journal if Pager.eLock is set to UNKNOWN_LOCK
39828 ** lock. Instead, it assumes a hot-journal exists and obtains an EXCLUSIVE
39829 ** lock on the database file before attempting to roll it back. See function
39830 ** PagerSharedLock() for more detail.
39831 **
39832 ** Pager.eLock may only be set to UNKNOWN_LOCK when the pager is in
39833 ** PAGER_OPEN state.
39834 */
39835 #define UNKNOWN_LOCK                (EXCLUSIVE_LOCK+1)
39836 
39837 /*
39838 ** A macro used for invoking the codec if there is one
39839 */
39840 #ifdef SQLITE_HAS_CODEC
39841 # define CODEC1(P,D,N,X,E) \
39842     if( P->xCodec && P->xCodec(P->pCodec,D,N,X)==0 ){ E; }
39843 # define CODEC2(P,D,N,X,E,O) \
39844     if( P->xCodec==0 ){ O=(char*)D; }else \
39845     if( (O=(char*)(P->xCodec(P->pCodec,D,N,X)))==0 ){ E; }
39846 #else
39847 # define CODEC1(P,D,N,X,E)   /* NO-OP */
39848 # define CODEC2(P,D,N,X,E,O) O=(char*)D
39849 #endif
39850 
39851 /*
39852 ** The maximum allowed sector size. 64KiB. If the xSectorsize() method
39853 ** returns a value larger than this, then MAX_SECTOR_SIZE is used instead.
39854 ** This could conceivably cause corruption following a power failure on
39855 ** such a system. This is currently an undocumented limit.
39856 */
39857 #define MAX_SECTOR_SIZE 0x10000
39858 
39859 /*
39860 ** An instance of the following structure is allocated for each active
39861 ** savepoint and statement transaction in the system. All such structures
39862 ** are stored in the Pager.aSavepoint[] array, which is allocated and
39863 ** resized using sqlite3Realloc().
39864 **
39865 ** When a savepoint is created, the PagerSavepoint.iHdrOffset field is
39866 ** set to 0. If a journal-header is written into the main journal while
39867 ** the savepoint is active, then iHdrOffset is set to the byte offset
39868 ** immediately following the last journal record written into the main
39869 ** journal before the journal-header. This is required during savepoint
39870 ** rollback (see pagerPlaybackSavepoint()).
39871 */
39872 typedef struct PagerSavepoint PagerSavepoint;
39873 struct PagerSavepoint {
39874   i64 iOffset;                 /* Starting offset in main journal */
39875   i64 iHdrOffset;              /* See above */
39876   Bitvec *pInSavepoint;        /* Set of pages in this savepoint */
39877   Pgno nOrig;                  /* Original number of pages in file */
39878   Pgno iSubRec;                /* Index of first record in sub-journal */
39879 #ifndef SQLITE_OMIT_WAL
39880   u32 aWalData[WAL_SAVEPOINT_NDATA];        /* WAL savepoint context */
39881 #endif
39882 };
39883 
39884 /*
39885 ** Bits of the Pager.doNotSpill flag.  See further description below.
39886 */
39887 #define SPILLFLAG_OFF         0x01      /* Never spill cache.  Set via pragma */
39888 #define SPILLFLAG_ROLLBACK    0x02      /* Current rolling back, so do not spill */
39889 #define SPILLFLAG_NOSYNC      0x04      /* Spill is ok, but do not sync */
39890 
39891 /*
39892 ** A open page cache is an instance of struct Pager. A description of
39893 ** some of the more important member variables follows:
39894 **
39895 ** eState
39896 **
39897 **   The current 'state' of the pager object. See the comment and state
39898 **   diagram above for a description of the pager state.
39899 **
39900 ** eLock
39901 **
39902 **   For a real on-disk database, the current lock held on the database file -
39903 **   NO_LOCK, SHARED_LOCK, RESERVED_LOCK or EXCLUSIVE_LOCK.
39904 **
39905 **   For a temporary or in-memory database (neither of which require any
39906 **   locks), this variable is always set to EXCLUSIVE_LOCK. Since such
39907 **   databases always have Pager.exclusiveMode==1, this tricks the pager
39908 **   logic into thinking that it already has all the locks it will ever
39909 **   need (and no reason to release them).
39910 **
39911 **   In some (obscure) circumstances, this variable may also be set to
39912 **   UNKNOWN_LOCK. See the comment above the #define of UNKNOWN_LOCK for
39913 **   details.
39914 **
39915 ** changeCountDone
39916 **
39917 **   This boolean variable is used to make sure that the change-counter
39918 **   (the 4-byte header field at byte offset 24 of the database file) is
39919 **   not updated more often than necessary.
39920 **
39921 **   It is set to true when the change-counter field is updated, which
39922 **   can only happen if an exclusive lock is held on the database file.
39923 **   It is cleared (set to false) whenever an exclusive lock is
39924 **   relinquished on the database file. Each time a transaction is committed,
39925 **   The changeCountDone flag is inspected. If it is true, the work of
39926 **   updating the change-counter is omitted for the current transaction.
39927 **
39928 **   This mechanism means that when running in exclusive mode, a connection
39929 **   need only update the change-counter once, for the first transaction
39930 **   committed.
39931 **
39932 ** setMaster
39933 **
39934 **   When PagerCommitPhaseOne() is called to commit a transaction, it may
39935 **   (or may not) specify a master-journal name to be written into the
39936 **   journal file before it is synced to disk.
39937 **
39938 **   Whether or not a journal file contains a master-journal pointer affects
39939 **   the way in which the journal file is finalized after the transaction is
39940 **   committed or rolled back when running in "journal_mode=PERSIST" mode.
39941 **   If a journal file does not contain a master-journal pointer, it is
39942 **   finalized by overwriting the first journal header with zeroes. If
39943 **   it does contain a master-journal pointer the journal file is finalized
39944 **   by truncating it to zero bytes, just as if the connection were
39945 **   running in "journal_mode=truncate" mode.
39946 **
39947 **   Journal files that contain master journal pointers cannot be finalized
39948 **   simply by overwriting the first journal-header with zeroes, as the
39949 **   master journal pointer could interfere with hot-journal rollback of any
39950 **   subsequently interrupted transaction that reuses the journal file.
39951 **
39952 **   The flag is cleared as soon as the journal file is finalized (either
39953 **   by PagerCommitPhaseTwo or PagerRollback). If an IO error prevents the
39954 **   journal file from being successfully finalized, the setMaster flag
39955 **   is cleared anyway (and the pager will move to ERROR state).
39956 **
39957 ** doNotSpill
39958 **
39959 **   This variables control the behavior of cache-spills  (calls made by
39960 **   the pcache module to the pagerStress() routine to write cached data
39961 **   to the file-system in order to free up memory).
39962 **
39963 **   When bits SPILLFLAG_OFF or SPILLFLAG_ROLLBACK of doNotSpill are set,
39964 **   writing to the database from pagerStress() is disabled altogether.
39965 **   The SPILLFLAG_ROLLBACK case is done in a very obscure case that
39966 **   comes up during savepoint rollback that requires the pcache module
39967 **   to allocate a new page to prevent the journal file from being written
39968 **   while it is being traversed by code in pager_playback().  The SPILLFLAG_OFF
39969 **   case is a user preference.
39970 **
39971 **   If the SPILLFLAG_NOSYNC bit is set, writing to the database from pagerStress()
39972 **   is permitted, but syncing the journal file is not. This flag is set
39973 **   by sqlite3PagerWrite() when the file-system sector-size is larger than
39974 **   the database page-size in order to prevent a journal sync from happening
39975 **   in between the journalling of two pages on the same sector.
39976 **
39977 ** subjInMemory
39978 **
39979 **   This is a boolean variable. If true, then any required sub-journal
39980 **   is opened as an in-memory journal file. If false, then in-memory
39981 **   sub-journals are only used for in-memory pager files.
39982 **
39983 **   This variable is updated by the upper layer each time a new
39984 **   write-transaction is opened.
39985 **
39986 ** dbSize, dbOrigSize, dbFileSize
39987 **
39988 **   Variable dbSize is set to the number of pages in the database file.
39989 **   It is valid in PAGER_READER and higher states (all states except for
39990 **   OPEN and ERROR).
39991 **
39992 **   dbSize is set based on the size of the database file, which may be
39993 **   larger than the size of the database (the value stored at offset
39994 **   28 of the database header by the btree). If the size of the file
39995 **   is not an integer multiple of the page-size, the value stored in
39996 **   dbSize is rounded down (i.e. a 5KB file with 2K page-size has dbSize==2).
39997 **   Except, any file that is greater than 0 bytes in size is considered
39998 **   to have at least one page. (i.e. a 1KB file with 2K page-size leads
39999 **   to dbSize==1).
40000 **
40001 **   During a write-transaction, if pages with page-numbers greater than
40002 **   dbSize are modified in the cache, dbSize is updated accordingly.
40003 **   Similarly, if the database is truncated using PagerTruncateImage(),
40004 **   dbSize is updated.
40005 **
40006 **   Variables dbOrigSize and dbFileSize are valid in states
40007 **   PAGER_WRITER_LOCKED and higher. dbOrigSize is a copy of the dbSize
40008 **   variable at the start of the transaction. It is used during rollback,
40009 **   and to determine whether or not pages need to be journalled before
40010 **   being modified.
40011 **
40012 **   Throughout a write-transaction, dbFileSize contains the size of
40013 **   the file on disk in pages. It is set to a copy of dbSize when the
40014 **   write-transaction is first opened, and updated when VFS calls are made
40015 **   to write or truncate the database file on disk.
40016 **
40017 **   The only reason the dbFileSize variable is required is to suppress
40018 **   unnecessary calls to xTruncate() after committing a transaction. If,
40019 **   when a transaction is committed, the dbFileSize variable indicates
40020 **   that the database file is larger than the database image (Pager.dbSize),
40021 **   pager_truncate() is called. The pager_truncate() call uses xFilesize()
40022 **   to measure the database file on disk, and then truncates it if required.
40023 **   dbFileSize is not used when rolling back a transaction. In this case
40024 **   pager_truncate() is called unconditionally (which means there may be
40025 **   a call to xFilesize() that is not strictly required). In either case,
40026 **   pager_truncate() may cause the file to become smaller or larger.
40027 **
40028 ** dbHintSize
40029 **
40030 **   The dbHintSize variable is used to limit the number of calls made to
40031 **   the VFS xFileControl(FCNTL_SIZE_HINT) method.
40032 **
40033 **   dbHintSize is set to a copy of the dbSize variable when a
40034 **   write-transaction is opened (at the same time as dbFileSize and
40035 **   dbOrigSize). If the xFileControl(FCNTL_SIZE_HINT) method is called,
40036 **   dbHintSize is increased to the number of pages that correspond to the
40037 **   size-hint passed to the method call. See pager_write_pagelist() for
40038 **   details.
40039 **
40040 ** errCode
40041 **
40042 **   The Pager.errCode variable is only ever used in PAGER_ERROR state. It
40043 **   is set to zero in all other states. In PAGER_ERROR state, Pager.errCode
40044 **   is always set to SQLITE_FULL, SQLITE_IOERR or one of the SQLITE_IOERR_XXX
40045 **   sub-codes.
40046 */
40047 struct Pager {
40048   sqlite3_vfs *pVfs;          /* OS functions to use for IO */
40049   u8 exclusiveMode;           /* Boolean. True if locking_mode==EXCLUSIVE */
40050   u8 journalMode;             /* One of the PAGER_JOURNALMODE_* values */
40051   u8 useJournal;              /* Use a rollback journal on this file */
40052   u8 noSync;                  /* Do not sync the journal if true */
40053   u8 fullSync;                /* Do extra syncs of the journal for robustness */
40054   u8 ckptSyncFlags;           /* SYNC_NORMAL or SYNC_FULL for checkpoint */
40055   u8 walSyncFlags;            /* SYNC_NORMAL or SYNC_FULL for wal writes */
40056   u8 syncFlags;               /* SYNC_NORMAL or SYNC_FULL otherwise */
40057   u8 tempFile;                /* zFilename is a temporary file */
40058   u8 readOnly;                /* True for a read-only database */
40059   u8 memDb;                   /* True to inhibit all file I/O */
40060 
40061   /**************************************************************************
40062   ** The following block contains those class members that change during
40063   ** routine opertion.  Class members not in this block are either fixed
40064   ** when the pager is first created or else only change when there is a
40065   ** significant mode change (such as changing the page_size, locking_mode,
40066   ** or the journal_mode).  From another view, these class members describe
40067   ** the "state" of the pager, while other class members describe the
40068   ** "configuration" of the pager.
40069   */
40070   u8 eState;                  /* Pager state (OPEN, READER, WRITER_LOCKED..) */
40071   u8 eLock;                   /* Current lock held on database file */
40072   u8 changeCountDone;         /* Set after incrementing the change-counter */
40073   u8 setMaster;               /* True if a m-j name has been written to jrnl */
40074   u8 doNotSpill;              /* Do not spill the cache when non-zero */
40075   u8 subjInMemory;            /* True to use in-memory sub-journals */
40076   Pgno dbSize;                /* Number of pages in the database */
40077   Pgno dbOrigSize;            /* dbSize before the current transaction */
40078   Pgno dbFileSize;            /* Number of pages in the database file */
40079   Pgno dbHintSize;            /* Value passed to FCNTL_SIZE_HINT call */
40080   int errCode;                /* One of several kinds of errors */
40081   int nRec;                   /* Pages journalled since last j-header written */
40082   u32 cksumInit;              /* Quasi-random value added to every checksum */
40083   u32 nSubRec;                /* Number of records written to sub-journal */
40084   Bitvec *pInJournal;         /* One bit for each page in the database file */
40085   sqlite3_file *fd;           /* File descriptor for database */
40086   sqlite3_file *jfd;          /* File descriptor for main journal */
40087   sqlite3_file *sjfd;         /* File descriptor for sub-journal */
40088   i64 journalOff;             /* Current write offset in the journal file */
40089   i64 journalHdr;             /* Byte offset to previous journal header */
40090   sqlite3_backup *pBackup;    /* Pointer to list of ongoing backup processes */
40091   PagerSavepoint *aSavepoint; /* Array of active savepoints */
40092   int nSavepoint;             /* Number of elements in aSavepoint[] */
40093   char dbFileVers[16];        /* Changes whenever database file changes */
40094 
40095   u8 bUseFetch;               /* True to use xFetch() */
40096   int nMmapOut;               /* Number of mmap pages currently outstanding */
40097   sqlite3_int64 szMmap;       /* Desired maximum mmap size */
40098   PgHdr *pMmapFreelist;       /* List of free mmap page headers (pDirty) */
40099   /*
40100   ** End of the routinely-changing class members
40101   ***************************************************************************/
40102 
40103   u16 nExtra;                 /* Add this many bytes to each in-memory page */
40104   i16 nReserve;               /* Number of unused bytes at end of each page */
40105   u32 vfsFlags;               /* Flags for sqlite3_vfs.xOpen() */
40106   u32 sectorSize;             /* Assumed sector size during rollback */
40107   int pageSize;               /* Number of bytes in a page */
40108   Pgno mxPgno;                /* Maximum allowed size of the database */
40109   i64 journalSizeLimit;       /* Size limit for persistent journal files */
40110   char *zFilename;            /* Name of the database file */
40111   char *zJournal;             /* Name of the journal file */
40112   int (*xBusyHandler)(void*); /* Function to call when busy */
40113   void *pBusyHandlerArg;      /* Context argument for xBusyHandler */
40114   int aStat[3];               /* Total cache hits, misses and writes */
40115 #ifdef SQLITE_TEST
40116   int nRead;                  /* Database pages read */
40117 #endif
40118   void (*xReiniter)(DbPage*); /* Call this routine when reloading pages */
40119 #ifdef SQLITE_HAS_CODEC
40120   void *(*xCodec)(void*,void*,Pgno,int); /* Routine for en/decoding data */
40121   void (*xCodecSizeChng)(void*,int,int); /* Notify of page size changes */
40122   void (*xCodecFree)(void*);             /* Destructor for the codec */
40123   void *pCodec;               /* First argument to xCodec... methods */
40124 #endif
40125   char *pTmpSpace;            /* Pager.pageSize bytes of space for tmp use */
40126   PCache *pPCache;            /* Pointer to page cache object */
40127 #ifndef SQLITE_OMIT_WAL
40128   Wal *pWal;                  /* Write-ahead log used by "journal_mode=wal" */
40129   char *zWal;                 /* File name for write-ahead log */
40130 #endif
40131 };
40132 
40133 /*
40134 ** Indexes for use with Pager.aStat[]. The Pager.aStat[] array contains
40135 ** the values accessed by passing SQLITE_DBSTATUS_CACHE_HIT, CACHE_MISS
40136 ** or CACHE_WRITE to sqlite3_db_status().
40137 */
40138 #define PAGER_STAT_HIT   0
40139 #define PAGER_STAT_MISS  1
40140 #define PAGER_STAT_WRITE 2
40141 
40142 /*
40143 ** The following global variables hold counters used for
40144 ** testing purposes only.  These variables do not exist in
40145 ** a non-testing build.  These variables are not thread-safe.
40146 */
40147 #ifdef SQLITE_TEST
40148 SQLITE_API int sqlite3_pager_readdb_count = 0;    /* Number of full pages read from DB */
40149 SQLITE_API int sqlite3_pager_writedb_count = 0;   /* Number of full pages written to DB */
40150 SQLITE_API int sqlite3_pager_writej_count = 0;    /* Number of pages written to journal */
40151 # define PAGER_INCR(v)  v++
40152 #else
40153 # define PAGER_INCR(v)
40154 #endif
40155 
40156 
40157 
40158 /*
40159 ** Journal files begin with the following magic string.  The data
40160 ** was obtained from /dev/random.  It is used only as a sanity check.
40161 **
40162 ** Since version 2.8.0, the journal format contains additional sanity
40163 ** checking information.  If the power fails while the journal is being
40164 ** written, semi-random garbage data might appear in the journal
40165 ** file after power is restored.  If an attempt is then made
40166 ** to roll the journal back, the database could be corrupted.  The additional
40167 ** sanity checking data is an attempt to discover the garbage in the
40168 ** journal and ignore it.
40169 **
40170 ** The sanity checking information for the new journal format consists
40171 ** of a 32-bit checksum on each page of data.  The checksum covers both
40172 ** the page number and the pPager->pageSize bytes of data for the page.
40173 ** This cksum is initialized to a 32-bit random value that appears in the
40174 ** journal file right after the header.  The random initializer is important,
40175 ** because garbage data that appears at the end of a journal is likely
40176 ** data that was once in other files that have now been deleted.  If the
40177 ** garbage data came from an obsolete journal file, the checksums might
40178 ** be correct.  But by initializing the checksum to random value which
40179 ** is different for every journal, we minimize that risk.
40180 */
40181 static const unsigned char aJournalMagic[] = {
40182   0xd9, 0xd5, 0x05, 0xf9, 0x20, 0xa1, 0x63, 0xd7,
40183 };
40184 
40185 /*
40186 ** The size of the of each page record in the journal is given by
40187 ** the following macro.
40188 */
40189 #define JOURNAL_PG_SZ(pPager)  ((pPager->pageSize) + 8)
40190 
40191 /*
40192 ** The journal header size for this pager. This is usually the same
40193 ** size as a single disk sector. See also setSectorSize().
40194 */
40195 #define JOURNAL_HDR_SZ(pPager) (pPager->sectorSize)
40196 
40197 /*
40198 ** The macro MEMDB is true if we are dealing with an in-memory database.
40199 ** We do this as a macro so that if the SQLITE_OMIT_MEMORYDB macro is set,
40200 ** the value of MEMDB will be a constant and the compiler will optimize
40201 ** out code that would never execute.
40202 */
40203 #ifdef SQLITE_OMIT_MEMORYDB
40204 # define MEMDB 0
40205 #else
40206 # define MEMDB pPager->memDb
40207 #endif
40208 
40209 /*
40210 ** The macro USEFETCH is true if we are allowed to use the xFetch and xUnfetch
40211 ** interfaces to access the database using memory-mapped I/O.
40212 */
40213 #if SQLITE_MAX_MMAP_SIZE>0
40214 # define USEFETCH(x) ((x)->bUseFetch)
40215 #else
40216 # define USEFETCH(x) 0
40217 #endif
40218 
40219 /*
40220 ** The maximum legal page number is (2^31 - 1).
40221 */
40222 #define PAGER_MAX_PGNO 2147483647
40223 
40224 /*
40225 ** The argument to this macro is a file descriptor (type sqlite3_file*).
40226 ** Return 0 if it is not open, or non-zero (but not 1) if it is.
40227 **
40228 ** This is so that expressions can be written as:
40229 **
40230 **   if( isOpen(pPager->jfd) ){ ...
40231 **
40232 ** instead of
40233 **
40234 **   if( pPager->jfd->pMethods ){ ...
40235 */
40236 #define isOpen(pFd) ((pFd)->pMethods)
40237 
40238 /*
40239 ** Return true if this pager uses a write-ahead log instead of the usual
40240 ** rollback journal. Otherwise false.
40241 */
40242 #ifndef SQLITE_OMIT_WAL
40243 static int pagerUseWal(Pager *pPager){
40244   return (pPager->pWal!=0);
40245 }
40246 #else
40247 # define pagerUseWal(x) 0
40248 # define pagerRollbackWal(x) 0
40249 # define pagerWalFrames(v,w,x,y) 0
40250 # define pagerOpenWalIfPresent(z) SQLITE_OK
40251 # define pagerBeginReadTransaction(z) SQLITE_OK
40252 #endif
40253 
40254 #ifndef NDEBUG
40255 /*
40256 ** Usage:
40257 **
40258 **   assert( assert_pager_state(pPager) );
40259 **
40260 ** This function runs many asserts to try to find inconsistencies in
40261 ** the internal state of the Pager object.
40262 */
40263 static int assert_pager_state(Pager *p){
40264   Pager *pPager = p;
40265 
40266   /* State must be valid. */
40267   assert( p->eState==PAGER_OPEN
40268        || p->eState==PAGER_READER
40269        || p->eState==PAGER_WRITER_LOCKED
40270        || p->eState==PAGER_WRITER_CACHEMOD
40271        || p->eState==PAGER_WRITER_DBMOD
40272        || p->eState==PAGER_WRITER_FINISHED
40273        || p->eState==PAGER_ERROR
40274   );
40275 
40276   /* Regardless of the current state, a temp-file connection always behaves
40277   ** as if it has an exclusive lock on the database file. It never updates
40278   ** the change-counter field, so the changeCountDone flag is always set.
40279   */
40280   assert( p->tempFile==0 || p->eLock==EXCLUSIVE_LOCK );
40281   assert( p->tempFile==0 || pPager->changeCountDone );
40282 
40283   /* If the useJournal flag is clear, the journal-mode must be "OFF".
40284   ** And if the journal-mode is "OFF", the journal file must not be open.
40285   */
40286   assert( p->journalMode==PAGER_JOURNALMODE_OFF || p->useJournal );
40287   assert( p->journalMode!=PAGER_JOURNALMODE_OFF || !isOpen(p->jfd) );
40288 
40289   /* Check that MEMDB implies noSync. And an in-memory journal. Since
40290   ** this means an in-memory pager performs no IO at all, it cannot encounter
40291   ** either SQLITE_IOERR or SQLITE_FULL during rollback or while finalizing
40292   ** a journal file. (although the in-memory journal implementation may
40293   ** return SQLITE_IOERR_NOMEM while the journal file is being written). It
40294   ** is therefore not possible for an in-memory pager to enter the ERROR
40295   ** state.
40296   */
40297   if( MEMDB ){
40298     assert( p->noSync );
40299     assert( p->journalMode==PAGER_JOURNALMODE_OFF
40300          || p->journalMode==PAGER_JOURNALMODE_MEMORY
40301     );
40302     assert( p->eState!=PAGER_ERROR && p->eState!=PAGER_OPEN );
40303     assert( pagerUseWal(p)==0 );
40304   }
40305 
40306   /* If changeCountDone is set, a RESERVED lock or greater must be held
40307   ** on the file.
40308   */
40309   assert( pPager->changeCountDone==0 || pPager->eLock>=RESERVED_LOCK );
40310   assert( p->eLock!=PENDING_LOCK );
40311 
40312   switch( p->eState ){
40313     case PAGER_OPEN:
40314       assert( !MEMDB );
40315       assert( pPager->errCode==SQLITE_OK );
40316       assert( sqlite3PcacheRefCount(pPager->pPCache)==0 || pPager->tempFile );
40317       break;
40318 
40319     case PAGER_READER:
40320       assert( pPager->errCode==SQLITE_OK );
40321       assert( p->eLock!=UNKNOWN_LOCK );
40322       assert( p->eLock>=SHARED_LOCK );
40323       break;
40324 
40325     case PAGER_WRITER_LOCKED:
40326       assert( p->eLock!=UNKNOWN_LOCK );
40327       assert( pPager->errCode==SQLITE_OK );
40328       if( !pagerUseWal(pPager) ){
40329         assert( p->eLock>=RESERVED_LOCK );
40330       }
40331       assert( pPager->dbSize==pPager->dbOrigSize );
40332       assert( pPager->dbOrigSize==pPager->dbFileSize );
40333       assert( pPager->dbOrigSize==pPager->dbHintSize );
40334       assert( pPager->setMaster==0 );
40335       break;
40336 
40337     case PAGER_WRITER_CACHEMOD:
40338       assert( p->eLock!=UNKNOWN_LOCK );
40339       assert( pPager->errCode==SQLITE_OK );
40340       if( !pagerUseWal(pPager) ){
40341         /* It is possible that if journal_mode=wal here that neither the
40342         ** journal file nor the WAL file are open. This happens during
40343         ** a rollback transaction that switches from journal_mode=off
40344         ** to journal_mode=wal.
40345         */
40346         assert( p->eLock>=RESERVED_LOCK );
40347         assert( isOpen(p->jfd)
40348              || p->journalMode==PAGER_JOURNALMODE_OFF
40349              || p->journalMode==PAGER_JOURNALMODE_WAL
40350         );
40351       }
40352       assert( pPager->dbOrigSize==pPager->dbFileSize );
40353       assert( pPager->dbOrigSize==pPager->dbHintSize );
40354       break;
40355 
40356     case PAGER_WRITER_DBMOD:
40357       assert( p->eLock==EXCLUSIVE_LOCK );
40358       assert( pPager->errCode==SQLITE_OK );
40359       assert( !pagerUseWal(pPager) );
40360       assert( p->eLock>=EXCLUSIVE_LOCK );
40361       assert( isOpen(p->jfd)
40362            || p->journalMode==PAGER_JOURNALMODE_OFF
40363            || p->journalMode==PAGER_JOURNALMODE_WAL
40364       );
40365       assert( pPager->dbOrigSize<=pPager->dbHintSize );
40366       break;
40367 
40368     case PAGER_WRITER_FINISHED:
40369       assert( p->eLock==EXCLUSIVE_LOCK );
40370       assert( pPager->errCode==SQLITE_OK );
40371       assert( !pagerUseWal(pPager) );
40372       assert( isOpen(p->jfd)
40373            || p->journalMode==PAGER_JOURNALMODE_OFF
40374            || p->journalMode==PAGER_JOURNALMODE_WAL
40375       );
40376       break;
40377 
40378     case PAGER_ERROR:
40379       /* There must be at least one outstanding reference to the pager if
40380       ** in ERROR state. Otherwise the pager should have already dropped
40381       ** back to OPEN state.
40382       */
40383       assert( pPager->errCode!=SQLITE_OK );
40384       assert( sqlite3PcacheRefCount(pPager->pPCache)>0 );
40385       break;
40386   }
40387 
40388   return 1;
40389 }
40390 #endif /* ifndef NDEBUG */
40391 
40392 #ifdef SQLITE_DEBUG
40393 /*
40394 ** Return a pointer to a human readable string in a static buffer
40395 ** containing the state of the Pager object passed as an argument. This
40396 ** is intended to be used within debuggers. For example, as an alternative
40397 ** to "print *pPager" in gdb:
40398 **
40399 ** (gdb) printf "%s", print_pager_state(pPager)
40400 */
40401 static char *print_pager_state(Pager *p){
40402   static char zRet[1024];
40403 
40404   sqlite3_snprintf(1024, zRet,
40405       "Filename:      %s\n"
40406       "State:         %s errCode=%d\n"
40407       "Lock:          %s\n"
40408       "Locking mode:  locking_mode=%s\n"
40409       "Journal mode:  journal_mode=%s\n"
40410       "Backing store: tempFile=%d memDb=%d useJournal=%d\n"
40411       "Journal:       journalOff=%lld journalHdr=%lld\n"
40412       "Size:          dbsize=%d dbOrigSize=%d dbFileSize=%d\n"
40413       , p->zFilename
40414       , p->eState==PAGER_OPEN            ? "OPEN" :
40415         p->eState==PAGER_READER          ? "READER" :
40416         p->eState==PAGER_WRITER_LOCKED   ? "WRITER_LOCKED" :
40417         p->eState==PAGER_WRITER_CACHEMOD ? "WRITER_CACHEMOD" :
40418         p->eState==PAGER_WRITER_DBMOD    ? "WRITER_DBMOD" :
40419         p->eState==PAGER_WRITER_FINISHED ? "WRITER_FINISHED" :
40420         p->eState==PAGER_ERROR           ? "ERROR" : "?error?"
40421       , (int)p->errCode
40422       , p->eLock==NO_LOCK         ? "NO_LOCK" :
40423         p->eLock==RESERVED_LOCK   ? "RESERVED" :
40424         p->eLock==EXCLUSIVE_LOCK  ? "EXCLUSIVE" :
40425         p->eLock==SHARED_LOCK     ? "SHARED" :
40426         p->eLock==UNKNOWN_LOCK    ? "UNKNOWN" : "?error?"
40427       , p->exclusiveMode ? "exclusive" : "normal"
40428       , p->journalMode==PAGER_JOURNALMODE_MEMORY   ? "memory" :
40429         p->journalMode==PAGER_JOURNALMODE_OFF      ? "off" :
40430         p->journalMode==PAGER_JOURNALMODE_DELETE   ? "delete" :
40431         p->journalMode==PAGER_JOURNALMODE_PERSIST  ? "persist" :
40432         p->journalMode==PAGER_JOURNALMODE_TRUNCATE ? "truncate" :
40433         p->journalMode==PAGER_JOURNALMODE_WAL      ? "wal" : "?error?"
40434       , (int)p->tempFile, (int)p->memDb, (int)p->useJournal
40435       , p->journalOff, p->journalHdr
40436       , (int)p->dbSize, (int)p->dbOrigSize, (int)p->dbFileSize
40437   );
40438 
40439   return zRet;
40440 }
40441 #endif
40442 
40443 /*
40444 ** Return true if it is necessary to write page *pPg into the sub-journal.
40445 ** A page needs to be written into the sub-journal if there exists one
40446 ** or more open savepoints for which:
40447 **
40448 **   * The page-number is less than or equal to PagerSavepoint.nOrig, and
40449 **   * The bit corresponding to the page-number is not set in
40450 **     PagerSavepoint.pInSavepoint.
40451 */
40452 static int subjRequiresPage(PgHdr *pPg){
40453   Pager *pPager = pPg->pPager;
40454   PagerSavepoint *p;
40455   Pgno pgno = pPg->pgno;
40456   int i;
40457   for(i=0; i<pPager->nSavepoint; i++){
40458     p = &pPager->aSavepoint[i];
40459     if( p->nOrig>=pgno && 0==sqlite3BitvecTest(p->pInSavepoint, pgno) ){
40460       return 1;
40461     }
40462   }
40463   return 0;
40464 }
40465 
40466 /*
40467 ** Return true if the page is already in the journal file.
40468 */
40469 static int pageInJournal(Pager *pPager, PgHdr *pPg){
40470   return sqlite3BitvecTest(pPager->pInJournal, pPg->pgno);
40471 }
40472 
40473 /*
40474 ** Read a 32-bit integer from the given file descriptor.  Store the integer
40475 ** that is read in *pRes.  Return SQLITE_OK if everything worked, or an
40476 ** error code is something goes wrong.
40477 **
40478 ** All values are stored on disk as big-endian.
40479 */
40480 static int read32bits(sqlite3_file *fd, i64 offset, u32 *pRes){
40481   unsigned char ac[4];
40482   int rc = sqlite3OsRead(fd, ac, sizeof(ac), offset);
40483   if( rc==SQLITE_OK ){
40484     *pRes = sqlite3Get4byte(ac);
40485   }
40486   return rc;
40487 }
40488 
40489 /*
40490 ** Write a 32-bit integer into a string buffer in big-endian byte order.
40491 */
40492 #define put32bits(A,B)  sqlite3Put4byte((u8*)A,B)
40493 
40494 
40495 /*
40496 ** Write a 32-bit integer into the given file descriptor.  Return SQLITE_OK
40497 ** on success or an error code is something goes wrong.
40498 */
40499 static int write32bits(sqlite3_file *fd, i64 offset, u32 val){
40500   char ac[4];
40501   put32bits(ac, val);
40502   return sqlite3OsWrite(fd, ac, 4, offset);
40503 }
40504 
40505 /*
40506 ** Unlock the database file to level eLock, which must be either NO_LOCK
40507 ** or SHARED_LOCK. Regardless of whether or not the call to xUnlock()
40508 ** succeeds, set the Pager.eLock variable to match the (attempted) new lock.
40509 **
40510 ** Except, if Pager.eLock is set to UNKNOWN_LOCK when this function is
40511 ** called, do not modify it. See the comment above the #define of
40512 ** UNKNOWN_LOCK for an explanation of this.
40513 */
40514 static int pagerUnlockDb(Pager *pPager, int eLock){
40515   int rc = SQLITE_OK;
40516 
40517   assert( !pPager->exclusiveMode || pPager->eLock==eLock );
40518   assert( eLock==NO_LOCK || eLock==SHARED_LOCK );
40519   assert( eLock!=NO_LOCK || pagerUseWal(pPager)==0 );
40520   if( isOpen(pPager->fd) ){
40521     assert( pPager->eLock>=eLock );
40522     rc = sqlite3OsUnlock(pPager->fd, eLock);
40523     if( pPager->eLock!=UNKNOWN_LOCK ){
40524       pPager->eLock = (u8)eLock;
40525     }
40526     IOTRACE(("UNLOCK %p %d\n", pPager, eLock))
40527   }
40528   return rc;
40529 }
40530 
40531 /*
40532 ** Lock the database file to level eLock, which must be either SHARED_LOCK,
40533 ** RESERVED_LOCK or EXCLUSIVE_LOCK. If the caller is successful, set the
40534 ** Pager.eLock variable to the new locking state.
40535 **
40536 ** Except, if Pager.eLock is set to UNKNOWN_LOCK when this function is
40537 ** called, do not modify it unless the new locking state is EXCLUSIVE_LOCK.
40538 ** See the comment above the #define of UNKNOWN_LOCK for an explanation
40539 ** of this.
40540 */
40541 static int pagerLockDb(Pager *pPager, int eLock){
40542   int rc = SQLITE_OK;
40543 
40544   assert( eLock==SHARED_LOCK || eLock==RESERVED_LOCK || eLock==EXCLUSIVE_LOCK );
40545   if( pPager->eLock<eLock || pPager->eLock==UNKNOWN_LOCK ){
40546     rc = sqlite3OsLock(pPager->fd, eLock);
40547     if( rc==SQLITE_OK && (pPager->eLock!=UNKNOWN_LOCK||eLock==EXCLUSIVE_LOCK) ){
40548       pPager->eLock = (u8)eLock;
40549       IOTRACE(("LOCK %p %d\n", pPager, eLock))
40550     }
40551   }
40552   return rc;
40553 }
40554 
40555 /*
40556 ** This function determines whether or not the atomic-write optimization
40557 ** can be used with this pager. The optimization can be used if:
40558 **
40559 **  (a) the value returned by OsDeviceCharacteristics() indicates that
40560 **      a database page may be written atomically, and
40561 **  (b) the value returned by OsSectorSize() is less than or equal
40562 **      to the page size.
40563 **
40564 ** The optimization is also always enabled for temporary files. It is
40565 ** an error to call this function if pPager is opened on an in-memory
40566 ** database.
40567 **
40568 ** If the optimization cannot be used, 0 is returned. If it can be used,
40569 ** then the value returned is the size of the journal file when it
40570 ** contains rollback data for exactly one page.
40571 */
40572 #ifdef SQLITE_ENABLE_ATOMIC_WRITE
40573 static int jrnlBufferSize(Pager *pPager){
40574   assert( !MEMDB );
40575   if( !pPager->tempFile ){
40576     int dc;                           /* Device characteristics */
40577     int nSector;                      /* Sector size */
40578     int szPage;                       /* Page size */
40579 
40580     assert( isOpen(pPager->fd) );
40581     dc = sqlite3OsDeviceCharacteristics(pPager->fd);
40582     nSector = pPager->sectorSize;
40583     szPage = pPager->pageSize;
40584 
40585     assert(SQLITE_IOCAP_ATOMIC512==(512>>8));
40586     assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8));
40587     if( 0==(dc&(SQLITE_IOCAP_ATOMIC|(szPage>>8)) || nSector>szPage) ){
40588       return 0;
40589     }
40590   }
40591 
40592   return JOURNAL_HDR_SZ(pPager) + JOURNAL_PG_SZ(pPager);
40593 }
40594 #endif
40595 
40596 /*
40597 ** If SQLITE_CHECK_PAGES is defined then we do some sanity checking
40598 ** on the cache using a hash function.  This is used for testing
40599 ** and debugging only.
40600 */
40601 #ifdef SQLITE_CHECK_PAGES
40602 /*
40603 ** Return a 32-bit hash of the page data for pPage.
40604 */
40605 static u32 pager_datahash(int nByte, unsigned char *pData){
40606   u32 hash = 0;
40607   int i;
40608   for(i=0; i<nByte; i++){
40609     hash = (hash*1039) + pData[i];
40610   }
40611   return hash;
40612 }
40613 static u32 pager_pagehash(PgHdr *pPage){
40614   return pager_datahash(pPage->pPager->pageSize, (unsigned char *)pPage->pData);
40615 }
40616 static void pager_set_pagehash(PgHdr *pPage){
40617   pPage->pageHash = pager_pagehash(pPage);
40618 }
40619 
40620 /*
40621 ** The CHECK_PAGE macro takes a PgHdr* as an argument. If SQLITE_CHECK_PAGES
40622 ** is defined, and NDEBUG is not defined, an assert() statement checks
40623 ** that the page is either dirty or still matches the calculated page-hash.
40624 */
40625 #define CHECK_PAGE(x) checkPage(x)
40626 static void checkPage(PgHdr *pPg){
40627   Pager *pPager = pPg->pPager;
40628   assert( pPager->eState!=PAGER_ERROR );
40629   assert( (pPg->flags&PGHDR_DIRTY) || pPg->pageHash==pager_pagehash(pPg) );
40630 }
40631 
40632 #else
40633 #define pager_datahash(X,Y)  0
40634 #define pager_pagehash(X)  0
40635 #define pager_set_pagehash(X)
40636 #define CHECK_PAGE(x)
40637 #endif  /* SQLITE_CHECK_PAGES */
40638 
40639 /*
40640 ** When this is called the journal file for pager pPager must be open.
40641 ** This function attempts to read a master journal file name from the
40642 ** end of the file and, if successful, copies it into memory supplied
40643 ** by the caller. See comments above writeMasterJournal() for the format
40644 ** used to store a master journal file name at the end of a journal file.
40645 **
40646 ** zMaster must point to a buffer of at least nMaster bytes allocated by
40647 ** the caller. This should be sqlite3_vfs.mxPathname+1 (to ensure there is
40648 ** enough space to write the master journal name). If the master journal
40649 ** name in the journal is longer than nMaster bytes (including a
40650 ** nul-terminator), then this is handled as if no master journal name
40651 ** were present in the journal.
40652 **
40653 ** If a master journal file name is present at the end of the journal
40654 ** file, then it is copied into the buffer pointed to by zMaster. A
40655 ** nul-terminator byte is appended to the buffer following the master
40656 ** journal file name.
40657 **
40658 ** If it is determined that no master journal file name is present
40659 ** zMaster[0] is set to 0 and SQLITE_OK returned.
40660 **
40661 ** If an error occurs while reading from the journal file, an SQLite
40662 ** error code is returned.
40663 */
40664 static int readMasterJournal(sqlite3_file *pJrnl, char *zMaster, u32 nMaster){
40665   int rc;                    /* Return code */
40666   u32 len;                   /* Length in bytes of master journal name */
40667   i64 szJ;                   /* Total size in bytes of journal file pJrnl */
40668   u32 cksum;                 /* MJ checksum value read from journal */
40669   u32 u;                     /* Unsigned loop counter */
40670   unsigned char aMagic[8];   /* A buffer to hold the magic header */
40671   zMaster[0] = '\0';
40672 
40673   if( SQLITE_OK!=(rc = sqlite3OsFileSize(pJrnl, &szJ))
40674    || szJ<16
40675    || SQLITE_OK!=(rc = read32bits(pJrnl, szJ-16, &len))
40676    || len>=nMaster
40677    || len==0
40678    || SQLITE_OK!=(rc = read32bits(pJrnl, szJ-12, &cksum))
40679    || SQLITE_OK!=(rc = sqlite3OsRead(pJrnl, aMagic, 8, szJ-8))
40680    || memcmp(aMagic, aJournalMagic, 8)
40681    || SQLITE_OK!=(rc = sqlite3OsRead(pJrnl, zMaster, len, szJ-16-len))
40682   ){
40683     return rc;
40684   }
40685 
40686   /* See if the checksum matches the master journal name */
40687   for(u=0; u<len; u++){
40688     cksum -= zMaster[u];
40689   }
40690   if( cksum ){
40691     /* If the checksum doesn't add up, then one or more of the disk sectors
40692     ** containing the master journal filename is corrupted. This means
40693     ** definitely roll back, so just return SQLITE_OK and report a (nul)
40694     ** master-journal filename.
40695     */
40696     len = 0;
40697   }
40698   zMaster[len] = '\0';
40699 
40700   return SQLITE_OK;
40701 }
40702 
40703 /*
40704 ** Return the offset of the sector boundary at or immediately
40705 ** following the value in pPager->journalOff, assuming a sector
40706 ** size of pPager->sectorSize bytes.
40707 **
40708 ** i.e for a sector size of 512:
40709 **
40710 **   Pager.journalOff          Return value
40711 **   ---------------------------------------
40712 **   0                         0
40713 **   512                       512
40714 **   100                       512
40715 **   2000                      2048
40716 **
40717 */
40718 static i64 journalHdrOffset(Pager *pPager){
40719   i64 offset = 0;
40720   i64 c = pPager->journalOff;
40721   if( c ){
40722     offset = ((c-1)/JOURNAL_HDR_SZ(pPager) + 1) * JOURNAL_HDR_SZ(pPager);
40723   }
40724   assert( offset%JOURNAL_HDR_SZ(pPager)==0 );
40725   assert( offset>=c );
40726   assert( (offset-c)<JOURNAL_HDR_SZ(pPager) );
40727   return offset;
40728 }
40729 
40730 /*
40731 ** The journal file must be open when this function is called.
40732 **
40733 ** This function is a no-op if the journal file has not been written to
40734 ** within the current transaction (i.e. if Pager.journalOff==0).
40735 **
40736 ** If doTruncate is non-zero or the Pager.journalSizeLimit variable is
40737 ** set to 0, then truncate the journal file to zero bytes in size. Otherwise,
40738 ** zero the 28-byte header at the start of the journal file. In either case,
40739 ** if the pager is not in no-sync mode, sync the journal file immediately
40740 ** after writing or truncating it.
40741 **
40742 ** If Pager.journalSizeLimit is set to a positive, non-zero value, and
40743 ** following the truncation or zeroing described above the size of the
40744 ** journal file in bytes is larger than this value, then truncate the
40745 ** journal file to Pager.journalSizeLimit bytes. The journal file does
40746 ** not need to be synced following this operation.
40747 **
40748 ** If an IO error occurs, abandon processing and return the IO error code.
40749 ** Otherwise, return SQLITE_OK.
40750 */
40751 static int zeroJournalHdr(Pager *pPager, int doTruncate){
40752   int rc = SQLITE_OK;                               /* Return code */
40753   assert( isOpen(pPager->jfd) );
40754   if( pPager->journalOff ){
40755     const i64 iLimit = pPager->journalSizeLimit;    /* Local cache of jsl */
40756 
40757     IOTRACE(("JZEROHDR %p\n", pPager))
40758     if( doTruncate || iLimit==0 ){
40759       rc = sqlite3OsTruncate(pPager->jfd, 0);
40760     }else{
40761       static const char zeroHdr[28] = {0};
40762       rc = sqlite3OsWrite(pPager->jfd, zeroHdr, sizeof(zeroHdr), 0);
40763     }
40764     if( rc==SQLITE_OK && !pPager->noSync ){
40765       rc = sqlite3OsSync(pPager->jfd, SQLITE_SYNC_DATAONLY|pPager->syncFlags);
40766     }
40767 
40768     /* At this point the transaction is committed but the write lock
40769     ** is still held on the file. If there is a size limit configured for
40770     ** the persistent journal and the journal file currently consumes more
40771     ** space than that limit allows for, truncate it now. There is no need
40772     ** to sync the file following this operation.
40773     */
40774     if( rc==SQLITE_OK && iLimit>0 ){
40775       i64 sz;
40776       rc = sqlite3OsFileSize(pPager->jfd, &sz);
40777       if( rc==SQLITE_OK && sz>iLimit ){
40778         rc = sqlite3OsTruncate(pPager->jfd, iLimit);
40779       }
40780     }
40781   }
40782   return rc;
40783 }
40784 
40785 /*
40786 ** The journal file must be open when this routine is called. A journal
40787 ** header (JOURNAL_HDR_SZ bytes) is written into the journal file at the
40788 ** current location.
40789 **
40790 ** The format for the journal header is as follows:
40791 ** - 8 bytes: Magic identifying journal format.
40792 ** - 4 bytes: Number of records in journal, or -1 no-sync mode is on.
40793 ** - 4 bytes: Random number used for page hash.
40794 ** - 4 bytes: Initial database page count.
40795 ** - 4 bytes: Sector size used by the process that wrote this journal.
40796 ** - 4 bytes: Database page size.
40797 **
40798 ** Followed by (JOURNAL_HDR_SZ - 28) bytes of unused space.
40799 */
40800 static int writeJournalHdr(Pager *pPager){
40801   int rc = SQLITE_OK;                 /* Return code */
40802   char *zHeader = pPager->pTmpSpace;  /* Temporary space used to build header */
40803   u32 nHeader = (u32)pPager->pageSize;/* Size of buffer pointed to by zHeader */
40804   u32 nWrite;                         /* Bytes of header sector written */
40805   int ii;                             /* Loop counter */
40806 
40807   assert( isOpen(pPager->jfd) );      /* Journal file must be open. */
40808 
40809   if( nHeader>JOURNAL_HDR_SZ(pPager) ){
40810     nHeader = JOURNAL_HDR_SZ(pPager);
40811   }
40812 
40813   /* If there are active savepoints and any of them were created
40814   ** since the most recent journal header was written, update the
40815   ** PagerSavepoint.iHdrOffset fields now.
40816   */
40817   for(ii=0; ii<pPager->nSavepoint; ii++){
40818     if( pPager->aSavepoint[ii].iHdrOffset==0 ){
40819       pPager->aSavepoint[ii].iHdrOffset = pPager->journalOff;
40820     }
40821   }
40822 
40823   pPager->journalHdr = pPager->journalOff = journalHdrOffset(pPager);
40824 
40825   /*
40826   ** Write the nRec Field - the number of page records that follow this
40827   ** journal header. Normally, zero is written to this value at this time.
40828   ** After the records are added to the journal (and the journal synced,
40829   ** if in full-sync mode), the zero is overwritten with the true number
40830   ** of records (see syncJournal()).
40831   **
40832   ** A faster alternative is to write 0xFFFFFFFF to the nRec field. When
40833   ** reading the journal this value tells SQLite to assume that the
40834   ** rest of the journal file contains valid page records. This assumption
40835   ** is dangerous, as if a failure occurred whilst writing to the journal
40836   ** file it may contain some garbage data. There are two scenarios
40837   ** where this risk can be ignored:
40838   **
40839   **   * When the pager is in no-sync mode. Corruption can follow a
40840   **     power failure in this case anyway.
40841   **
40842   **   * When the SQLITE_IOCAP_SAFE_APPEND flag is set. This guarantees
40843   **     that garbage data is never appended to the journal file.
40844   */
40845   assert( isOpen(pPager->fd) || pPager->noSync );
40846   if( pPager->noSync || (pPager->journalMode==PAGER_JOURNALMODE_MEMORY)
40847    || (sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_SAFE_APPEND)
40848   ){
40849     memcpy(zHeader, aJournalMagic, sizeof(aJournalMagic));
40850     put32bits(&zHeader[sizeof(aJournalMagic)], 0xffffffff);
40851   }else{
40852     memset(zHeader, 0, sizeof(aJournalMagic)+4);
40853   }
40854 
40855   /* The random check-hash initializer */
40856   sqlite3_randomness(sizeof(pPager->cksumInit), &pPager->cksumInit);
40857   put32bits(&zHeader[sizeof(aJournalMagic)+4], pPager->cksumInit);
40858   /* The initial database size */
40859   put32bits(&zHeader[sizeof(aJournalMagic)+8], pPager->dbOrigSize);
40860   /* The assumed sector size for this process */
40861   put32bits(&zHeader[sizeof(aJournalMagic)+12], pPager->sectorSize);
40862 
40863   /* The page size */
40864   put32bits(&zHeader[sizeof(aJournalMagic)+16], pPager->pageSize);
40865 
40866   /* Initializing the tail of the buffer is not necessary.  Everything
40867   ** works find if the following memset() is omitted.  But initializing
40868   ** the memory prevents valgrind from complaining, so we are willing to
40869   ** take the performance hit.
40870   */
40871   memset(&zHeader[sizeof(aJournalMagic)+20], 0,
40872          nHeader-(sizeof(aJournalMagic)+20));
40873 
40874   /* In theory, it is only necessary to write the 28 bytes that the
40875   ** journal header consumes to the journal file here. Then increment the
40876   ** Pager.journalOff variable by JOURNAL_HDR_SZ so that the next
40877   ** record is written to the following sector (leaving a gap in the file
40878   ** that will be implicitly filled in by the OS).
40879   **
40880   ** However it has been discovered that on some systems this pattern can
40881   ** be significantly slower than contiguously writing data to the file,
40882   ** even if that means explicitly writing data to the block of
40883   ** (JOURNAL_HDR_SZ - 28) bytes that will not be used. So that is what
40884   ** is done.
40885   **
40886   ** The loop is required here in case the sector-size is larger than the
40887   ** database page size. Since the zHeader buffer is only Pager.pageSize
40888   ** bytes in size, more than one call to sqlite3OsWrite() may be required
40889   ** to populate the entire journal header sector.
40890   */
40891   for(nWrite=0; rc==SQLITE_OK&&nWrite<JOURNAL_HDR_SZ(pPager); nWrite+=nHeader){
40892     IOTRACE(("JHDR %p %lld %d\n", pPager, pPager->journalHdr, nHeader))
40893     rc = sqlite3OsWrite(pPager->jfd, zHeader, nHeader, pPager->journalOff);
40894     assert( pPager->journalHdr <= pPager->journalOff );
40895     pPager->journalOff += nHeader;
40896   }
40897 
40898   return rc;
40899 }
40900 
40901 /*
40902 ** The journal file must be open when this is called. A journal header file
40903 ** (JOURNAL_HDR_SZ bytes) is read from the current location in the journal
40904 ** file. The current location in the journal file is given by
40905 ** pPager->journalOff. See comments above function writeJournalHdr() for
40906 ** a description of the journal header format.
40907 **
40908 ** If the header is read successfully, *pNRec is set to the number of
40909 ** page records following this header and *pDbSize is set to the size of the
40910 ** database before the transaction began, in pages. Also, pPager->cksumInit
40911 ** is set to the value read from the journal header. SQLITE_OK is returned
40912 ** in this case.
40913 **
40914 ** If the journal header file appears to be corrupted, SQLITE_DONE is
40915 ** returned and *pNRec and *PDbSize are undefined.  If JOURNAL_HDR_SZ bytes
40916 ** cannot be read from the journal file an error code is returned.
40917 */
40918 static int readJournalHdr(
40919   Pager *pPager,               /* Pager object */
40920   int isHot,
40921   i64 journalSize,             /* Size of the open journal file in bytes */
40922   u32 *pNRec,                  /* OUT: Value read from the nRec field */
40923   u32 *pDbSize                 /* OUT: Value of original database size field */
40924 ){
40925   int rc;                      /* Return code */
40926   unsigned char aMagic[8];     /* A buffer to hold the magic header */
40927   i64 iHdrOff;                 /* Offset of journal header being read */
40928 
40929   assert( isOpen(pPager->jfd) );      /* Journal file must be open. */
40930 
40931   /* Advance Pager.journalOff to the start of the next sector. If the
40932   ** journal file is too small for there to be a header stored at this
40933   ** point, return SQLITE_DONE.
40934   */
40935   pPager->journalOff = journalHdrOffset(pPager);
40936   if( pPager->journalOff+JOURNAL_HDR_SZ(pPager) > journalSize ){
40937     return SQLITE_DONE;
40938   }
40939   iHdrOff = pPager->journalOff;
40940 
40941   /* Read in the first 8 bytes of the journal header. If they do not match
40942   ** the  magic string found at the start of each journal header, return
40943   ** SQLITE_DONE. If an IO error occurs, return an error code. Otherwise,
40944   ** proceed.
40945   */
40946   if( isHot || iHdrOff!=pPager->journalHdr ){
40947     rc = sqlite3OsRead(pPager->jfd, aMagic, sizeof(aMagic), iHdrOff);
40948     if( rc ){
40949       return rc;
40950     }
40951     if( memcmp(aMagic, aJournalMagic, sizeof(aMagic))!=0 ){
40952       return SQLITE_DONE;
40953     }
40954   }
40955 
40956   /* Read the first three 32-bit fields of the journal header: The nRec
40957   ** field, the checksum-initializer and the database size at the start
40958   ** of the transaction. Return an error code if anything goes wrong.
40959   */
40960   if( SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+8, pNRec))
40961    || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+12, &pPager->cksumInit))
40962    || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+16, pDbSize))
40963   ){
40964     return rc;
40965   }
40966 
40967   if( pPager->journalOff==0 ){
40968     u32 iPageSize;               /* Page-size field of journal header */
40969     u32 iSectorSize;             /* Sector-size field of journal header */
40970 
40971     /* Read the page-size and sector-size journal header fields. */
40972     if( SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+20, &iSectorSize))
40973      || SQLITE_OK!=(rc = read32bits(pPager->jfd, iHdrOff+24, &iPageSize))
40974     ){
40975       return rc;
40976     }
40977 
40978     /* Versions of SQLite prior to 3.5.8 set the page-size field of the
40979     ** journal header to zero. In this case, assume that the Pager.pageSize
40980     ** variable is already set to the correct page size.
40981     */
40982     if( iPageSize==0 ){
40983       iPageSize = pPager->pageSize;
40984     }
40985 
40986     /* Check that the values read from the page-size and sector-size fields
40987     ** are within range. To be 'in range', both values need to be a power
40988     ** of two greater than or equal to 512 or 32, and not greater than their
40989     ** respective compile time maximum limits.
40990     */
40991     if( iPageSize<512                  || iSectorSize<32
40992      || iPageSize>SQLITE_MAX_PAGE_SIZE || iSectorSize>MAX_SECTOR_SIZE
40993      || ((iPageSize-1)&iPageSize)!=0   || ((iSectorSize-1)&iSectorSize)!=0
40994     ){
40995       /* If the either the page-size or sector-size in the journal-header is
40996       ** invalid, then the process that wrote the journal-header must have
40997       ** crashed before the header was synced. In this case stop reading
40998       ** the journal file here.
40999       */
41000       return SQLITE_DONE;
41001     }
41002 
41003     /* Update the page-size to match the value read from the journal.
41004     ** Use a testcase() macro to make sure that malloc failure within
41005     ** PagerSetPagesize() is tested.
41006     */
41007     rc = sqlite3PagerSetPagesize(pPager, &iPageSize, -1);
41008     testcase( rc!=SQLITE_OK );
41009 
41010     /* Update the assumed sector-size to match the value used by
41011     ** the process that created this journal. If this journal was
41012     ** created by a process other than this one, then this routine
41013     ** is being called from within pager_playback(). The local value
41014     ** of Pager.sectorSize is restored at the end of that routine.
41015     */
41016     pPager->sectorSize = iSectorSize;
41017   }
41018 
41019   pPager->journalOff += JOURNAL_HDR_SZ(pPager);
41020   return rc;
41021 }
41022 
41023 
41024 /*
41025 ** Write the supplied master journal name into the journal file for pager
41026 ** pPager at the current location. The master journal name must be the last
41027 ** thing written to a journal file. If the pager is in full-sync mode, the
41028 ** journal file descriptor is advanced to the next sector boundary before
41029 ** anything is written. The format is:
41030 **
41031 **   + 4 bytes: PAGER_MJ_PGNO.
41032 **   + N bytes: Master journal filename in utf-8.
41033 **   + 4 bytes: N (length of master journal name in bytes, no nul-terminator).
41034 **   + 4 bytes: Master journal name checksum.
41035 **   + 8 bytes: aJournalMagic[].
41036 **
41037 ** The master journal page checksum is the sum of the bytes in the master
41038 ** journal name, where each byte is interpreted as a signed 8-bit integer.
41039 **
41040 ** If zMaster is a NULL pointer (occurs for a single database transaction),
41041 ** this call is a no-op.
41042 */
41043 static int writeMasterJournal(Pager *pPager, const char *zMaster){
41044   int rc;                          /* Return code */
41045   int nMaster;                     /* Length of string zMaster */
41046   i64 iHdrOff;                     /* Offset of header in journal file */
41047   i64 jrnlSize;                    /* Size of journal file on disk */
41048   u32 cksum = 0;                   /* Checksum of string zMaster */
41049 
41050   assert( pPager->setMaster==0 );
41051   assert( !pagerUseWal(pPager) );
41052 
41053   if( !zMaster
41054    || pPager->journalMode==PAGER_JOURNALMODE_MEMORY
41055    || pPager->journalMode==PAGER_JOURNALMODE_OFF
41056   ){
41057     return SQLITE_OK;
41058   }
41059   pPager->setMaster = 1;
41060   assert( isOpen(pPager->jfd) );
41061   assert( pPager->journalHdr <= pPager->journalOff );
41062 
41063   /* Calculate the length in bytes and the checksum of zMaster */
41064   for(nMaster=0; zMaster[nMaster]; nMaster++){
41065     cksum += zMaster[nMaster];
41066   }
41067 
41068   /* If in full-sync mode, advance to the next disk sector before writing
41069   ** the master journal name. This is in case the previous page written to
41070   ** the journal has already been synced.
41071   */
41072   if( pPager->fullSync ){
41073     pPager->journalOff = journalHdrOffset(pPager);
41074   }
41075   iHdrOff = pPager->journalOff;
41076 
41077   /* Write the master journal data to the end of the journal file. If
41078   ** an error occurs, return the error code to the caller.
41079   */
41080   if( (0 != (rc = write32bits(pPager->jfd, iHdrOff, PAGER_MJ_PGNO(pPager))))
41081    || (0 != (rc = sqlite3OsWrite(pPager->jfd, zMaster, nMaster, iHdrOff+4)))
41082    || (0 != (rc = write32bits(pPager->jfd, iHdrOff+4+nMaster, nMaster)))
41083    || (0 != (rc = write32bits(pPager->jfd, iHdrOff+4+nMaster+4, cksum)))
41084    || (0 != (rc = sqlite3OsWrite(pPager->jfd, aJournalMagic, 8, iHdrOff+4+nMaster+8)))
41085   ){
41086     return rc;
41087   }
41088   pPager->journalOff += (nMaster+20);
41089 
41090   /* If the pager is in peristent-journal mode, then the physical
41091   ** journal-file may extend past the end of the master-journal name
41092   ** and 8 bytes of magic data just written to the file. This is
41093   ** dangerous because the code to rollback a hot-journal file
41094   ** will not be able to find the master-journal name to determine
41095   ** whether or not the journal is hot.
41096   **
41097   ** Easiest thing to do in this scenario is to truncate the journal
41098   ** file to the required size.
41099   */
41100   if( SQLITE_OK==(rc = sqlite3OsFileSize(pPager->jfd, &jrnlSize))
41101    && jrnlSize>pPager->journalOff
41102   ){
41103     rc = sqlite3OsTruncate(pPager->jfd, pPager->journalOff);
41104   }
41105   return rc;
41106 }
41107 
41108 /*
41109 ** Find a page in the hash table given its page number. Return
41110 ** a pointer to the page or NULL if the requested page is not
41111 ** already in memory.
41112 */
41113 static PgHdr *pager_lookup(Pager *pPager, Pgno pgno){
41114   PgHdr *p = 0;                     /* Return value */
41115 
41116   /* It is not possible for a call to PcacheFetch() with createFlag==0 to
41117   ** fail, since no attempt to allocate dynamic memory will be made.
41118   */
41119   (void)sqlite3PcacheFetch(pPager->pPCache, pgno, 0, &p);
41120   return p;
41121 }
41122 
41123 /*
41124 ** Discard the entire contents of the in-memory page-cache.
41125 */
41126 static void pager_reset(Pager *pPager){
41127   sqlite3BackupRestart(pPager->pBackup);
41128   sqlite3PcacheClear(pPager->pPCache);
41129 }
41130 
41131 /*
41132 ** Free all structures in the Pager.aSavepoint[] array and set both
41133 ** Pager.aSavepoint and Pager.nSavepoint to zero. Close the sub-journal
41134 ** if it is open and the pager is not in exclusive mode.
41135 */
41136 static void releaseAllSavepoints(Pager *pPager){
41137   int ii;               /* Iterator for looping through Pager.aSavepoint */
41138   for(ii=0; ii<pPager->nSavepoint; ii++){
41139     sqlite3BitvecDestroy(pPager->aSavepoint[ii].pInSavepoint);
41140   }
41141   if( !pPager->exclusiveMode || sqlite3IsMemJournal(pPager->sjfd) ){
41142     sqlite3OsClose(pPager->sjfd);
41143   }
41144   sqlite3_free(pPager->aSavepoint);
41145   pPager->aSavepoint = 0;
41146   pPager->nSavepoint = 0;
41147   pPager->nSubRec = 0;
41148 }
41149 
41150 /*
41151 ** Set the bit number pgno in the PagerSavepoint.pInSavepoint
41152 ** bitvecs of all open savepoints. Return SQLITE_OK if successful
41153 ** or SQLITE_NOMEM if a malloc failure occurs.
41154 */
41155 static int addToSavepointBitvecs(Pager *pPager, Pgno pgno){
41156   int ii;                   /* Loop counter */
41157   int rc = SQLITE_OK;       /* Result code */
41158 
41159   for(ii=0; ii<pPager->nSavepoint; ii++){
41160     PagerSavepoint *p = &pPager->aSavepoint[ii];
41161     if( pgno<=p->nOrig ){
41162       rc |= sqlite3BitvecSet(p->pInSavepoint, pgno);
41163       testcase( rc==SQLITE_NOMEM );
41164       assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
41165     }
41166   }
41167   return rc;
41168 }
41169 
41170 /*
41171 ** This function is a no-op if the pager is in exclusive mode and not
41172 ** in the ERROR state. Otherwise, it switches the pager to PAGER_OPEN
41173 ** state.
41174 **
41175 ** If the pager is not in exclusive-access mode, the database file is
41176 ** completely unlocked. If the file is unlocked and the file-system does
41177 ** not exhibit the UNDELETABLE_WHEN_OPEN property, the journal file is
41178 ** closed (if it is open).
41179 **
41180 ** If the pager is in ERROR state when this function is called, the
41181 ** contents of the pager cache are discarded before switching back to
41182 ** the OPEN state. Regardless of whether the pager is in exclusive-mode
41183 ** or not, any journal file left in the file-system will be treated
41184 ** as a hot-journal and rolled back the next time a read-transaction
41185 ** is opened (by this or by any other connection).
41186 */
41187 static void pager_unlock(Pager *pPager){
41188 
41189   assert( pPager->eState==PAGER_READER
41190        || pPager->eState==PAGER_OPEN
41191        || pPager->eState==PAGER_ERROR
41192   );
41193 
41194   sqlite3BitvecDestroy(pPager->pInJournal);
41195   pPager->pInJournal = 0;
41196   releaseAllSavepoints(pPager);
41197 
41198   if( pagerUseWal(pPager) ){
41199     assert( !isOpen(pPager->jfd) );
41200     sqlite3WalEndReadTransaction(pPager->pWal);
41201     pPager->eState = PAGER_OPEN;
41202   }else if( !pPager->exclusiveMode ){
41203     int rc;                       /* Error code returned by pagerUnlockDb() */
41204     int iDc = isOpen(pPager->fd)?sqlite3OsDeviceCharacteristics(pPager->fd):0;
41205 
41206     /* If the operating system support deletion of open files, then
41207     ** close the journal file when dropping the database lock.  Otherwise
41208     ** another connection with journal_mode=delete might delete the file
41209     ** out from under us.
41210     */
41211     assert( (PAGER_JOURNALMODE_MEMORY   & 5)!=1 );
41212     assert( (PAGER_JOURNALMODE_OFF      & 5)!=1 );
41213     assert( (PAGER_JOURNALMODE_WAL      & 5)!=1 );
41214     assert( (PAGER_JOURNALMODE_DELETE   & 5)!=1 );
41215     assert( (PAGER_JOURNALMODE_TRUNCATE & 5)==1 );
41216     assert( (PAGER_JOURNALMODE_PERSIST  & 5)==1 );
41217     if( 0==(iDc & SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN)
41218      || 1!=(pPager->journalMode & 5)
41219     ){
41220       sqlite3OsClose(pPager->jfd);
41221     }
41222 
41223     /* If the pager is in the ERROR state and the call to unlock the database
41224     ** file fails, set the current lock to UNKNOWN_LOCK. See the comment
41225     ** above the #define for UNKNOWN_LOCK for an explanation of why this
41226     ** is necessary.
41227     */
41228     rc = pagerUnlockDb(pPager, NO_LOCK);
41229     if( rc!=SQLITE_OK && pPager->eState==PAGER_ERROR ){
41230       pPager->eLock = UNKNOWN_LOCK;
41231     }
41232 
41233     /* The pager state may be changed from PAGER_ERROR to PAGER_OPEN here
41234     ** without clearing the error code. This is intentional - the error
41235     ** code is cleared and the cache reset in the block below.
41236     */
41237     assert( pPager->errCode || pPager->eState!=PAGER_ERROR );
41238     pPager->changeCountDone = 0;
41239     pPager->eState = PAGER_OPEN;
41240   }
41241 
41242   /* If Pager.errCode is set, the contents of the pager cache cannot be
41243   ** trusted. Now that there are no outstanding references to the pager,
41244   ** it can safely move back to PAGER_OPEN state. This happens in both
41245   ** normal and exclusive-locking mode.
41246   */
41247   if( pPager->errCode ){
41248     assert( !MEMDB );
41249     pager_reset(pPager);
41250     pPager->changeCountDone = pPager->tempFile;
41251     pPager->eState = PAGER_OPEN;
41252     pPager->errCode = SQLITE_OK;
41253     if( USEFETCH(pPager) ) sqlite3OsUnfetch(pPager->fd, 0, 0);
41254   }
41255 
41256   pPager->journalOff = 0;
41257   pPager->journalHdr = 0;
41258   pPager->setMaster = 0;
41259 }
41260 
41261 /*
41262 ** This function is called whenever an IOERR or FULL error that requires
41263 ** the pager to transition into the ERROR state may ahve occurred.
41264 ** The first argument is a pointer to the pager structure, the second
41265 ** the error-code about to be returned by a pager API function. The
41266 ** value returned is a copy of the second argument to this function.
41267 **
41268 ** If the second argument is SQLITE_FULL, SQLITE_IOERR or one of the
41269 ** IOERR sub-codes, the pager enters the ERROR state and the error code
41270 ** is stored in Pager.errCode. While the pager remains in the ERROR state,
41271 ** all major API calls on the Pager will immediately return Pager.errCode.
41272 **
41273 ** The ERROR state indicates that the contents of the pager-cache
41274 ** cannot be trusted. This state can be cleared by completely discarding
41275 ** the contents of the pager-cache. If a transaction was active when
41276 ** the persistent error occurred, then the rollback journal may need
41277 ** to be replayed to restore the contents of the database file (as if
41278 ** it were a hot-journal).
41279 */
41280 static int pager_error(Pager *pPager, int rc){
41281   int rc2 = rc & 0xff;
41282   assert( rc==SQLITE_OK || !MEMDB );
41283   assert(
41284        pPager->errCode==SQLITE_FULL ||
41285        pPager->errCode==SQLITE_OK ||
41286        (pPager->errCode & 0xff)==SQLITE_IOERR
41287   );
41288   if( rc2==SQLITE_FULL || rc2==SQLITE_IOERR ){
41289     pPager->errCode = rc;
41290     pPager->eState = PAGER_ERROR;
41291   }
41292   return rc;
41293 }
41294 
41295 static int pager_truncate(Pager *pPager, Pgno nPage);
41296 
41297 /*
41298 ** This routine ends a transaction. A transaction is usually ended by
41299 ** either a COMMIT or a ROLLBACK operation. This routine may be called
41300 ** after rollback of a hot-journal, or if an error occurs while opening
41301 ** the journal file or writing the very first journal-header of a
41302 ** database transaction.
41303 **
41304 ** This routine is never called in PAGER_ERROR state. If it is called
41305 ** in PAGER_NONE or PAGER_SHARED state and the lock held is less
41306 ** exclusive than a RESERVED lock, it is a no-op.
41307 **
41308 ** Otherwise, any active savepoints are released.
41309 **
41310 ** If the journal file is open, then it is "finalized". Once a journal
41311 ** file has been finalized it is not possible to use it to roll back a
41312 ** transaction. Nor will it be considered to be a hot-journal by this
41313 ** or any other database connection. Exactly how a journal is finalized
41314 ** depends on whether or not the pager is running in exclusive mode and
41315 ** the current journal-mode (Pager.journalMode value), as follows:
41316 **
41317 **   journalMode==MEMORY
41318 **     Journal file descriptor is simply closed. This destroys an
41319 **     in-memory journal.
41320 **
41321 **   journalMode==TRUNCATE
41322 **     Journal file is truncated to zero bytes in size.
41323 **
41324 **   journalMode==PERSIST
41325 **     The first 28 bytes of the journal file are zeroed. This invalidates
41326 **     the first journal header in the file, and hence the entire journal
41327 **     file. An invalid journal file cannot be rolled back.
41328 **
41329 **   journalMode==DELETE
41330 **     The journal file is closed and deleted using sqlite3OsDelete().
41331 **
41332 **     If the pager is running in exclusive mode, this method of finalizing
41333 **     the journal file is never used. Instead, if the journalMode is
41334 **     DELETE and the pager is in exclusive mode, the method described under
41335 **     journalMode==PERSIST is used instead.
41336 **
41337 ** After the journal is finalized, the pager moves to PAGER_READER state.
41338 ** If running in non-exclusive rollback mode, the lock on the file is
41339 ** downgraded to a SHARED_LOCK.
41340 **
41341 ** SQLITE_OK is returned if no error occurs. If an error occurs during
41342 ** any of the IO operations to finalize the journal file or unlock the
41343 ** database then the IO error code is returned to the user. If the
41344 ** operation to finalize the journal file fails, then the code still
41345 ** tries to unlock the database file if not in exclusive mode. If the
41346 ** unlock operation fails as well, then the first error code related
41347 ** to the first error encountered (the journal finalization one) is
41348 ** returned.
41349 */
41350 static int pager_end_transaction(Pager *pPager, int hasMaster, int bCommit){
41351   int rc = SQLITE_OK;      /* Error code from journal finalization operation */
41352   int rc2 = SQLITE_OK;     /* Error code from db file unlock operation */
41353 
41354   /* Do nothing if the pager does not have an open write transaction
41355   ** or at least a RESERVED lock. This function may be called when there
41356   ** is no write-transaction active but a RESERVED or greater lock is
41357   ** held under two circumstances:
41358   **
41359   **   1. After a successful hot-journal rollback, it is called with
41360   **      eState==PAGER_NONE and eLock==EXCLUSIVE_LOCK.
41361   **
41362   **   2. If a connection with locking_mode=exclusive holding an EXCLUSIVE
41363   **      lock switches back to locking_mode=normal and then executes a
41364   **      read-transaction, this function is called with eState==PAGER_READER
41365   **      and eLock==EXCLUSIVE_LOCK when the read-transaction is closed.
41366   */
41367   assert( assert_pager_state(pPager) );
41368   assert( pPager->eState!=PAGER_ERROR );
41369   if( pPager->eState<PAGER_WRITER_LOCKED && pPager->eLock<RESERVED_LOCK ){
41370     return SQLITE_OK;
41371   }
41372 
41373   releaseAllSavepoints(pPager);
41374   assert( isOpen(pPager->jfd) || pPager->pInJournal==0 );
41375   if( isOpen(pPager->jfd) ){
41376     assert( !pagerUseWal(pPager) );
41377 
41378     /* Finalize the journal file. */
41379     if( sqlite3IsMemJournal(pPager->jfd) ){
41380       assert( pPager->journalMode==PAGER_JOURNALMODE_MEMORY );
41381       sqlite3OsClose(pPager->jfd);
41382     }else if( pPager->journalMode==PAGER_JOURNALMODE_TRUNCATE ){
41383       if( pPager->journalOff==0 ){
41384         rc = SQLITE_OK;
41385       }else{
41386         rc = sqlite3OsTruncate(pPager->jfd, 0);
41387       }
41388       pPager->journalOff = 0;
41389     }else if( pPager->journalMode==PAGER_JOURNALMODE_PERSIST
41390       || (pPager->exclusiveMode && pPager->journalMode!=PAGER_JOURNALMODE_WAL)
41391     ){
41392       rc = zeroJournalHdr(pPager, hasMaster);
41393       pPager->journalOff = 0;
41394     }else{
41395       /* This branch may be executed with Pager.journalMode==MEMORY if
41396       ** a hot-journal was just rolled back. In this case the journal
41397       ** file should be closed and deleted. If this connection writes to
41398       ** the database file, it will do so using an in-memory journal.
41399       */
41400       int bDelete = (!pPager->tempFile && sqlite3JournalExists(pPager->jfd));
41401       assert( pPager->journalMode==PAGER_JOURNALMODE_DELETE
41402            || pPager->journalMode==PAGER_JOURNALMODE_MEMORY
41403            || pPager->journalMode==PAGER_JOURNALMODE_WAL
41404       );
41405       sqlite3OsClose(pPager->jfd);
41406       if( bDelete ){
41407         rc = sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
41408       }
41409     }
41410   }
41411 
41412 #ifdef SQLITE_CHECK_PAGES
41413   sqlite3PcacheIterateDirty(pPager->pPCache, pager_set_pagehash);
41414   if( pPager->dbSize==0 && sqlite3PcacheRefCount(pPager->pPCache)>0 ){
41415     PgHdr *p = pager_lookup(pPager, 1);
41416     if( p ){
41417       p->pageHash = 0;
41418       sqlite3PagerUnrefNotNull(p);
41419     }
41420   }
41421 #endif
41422 
41423   sqlite3BitvecDestroy(pPager->pInJournal);
41424   pPager->pInJournal = 0;
41425   pPager->nRec = 0;
41426   sqlite3PcacheCleanAll(pPager->pPCache);
41427   sqlite3PcacheTruncate(pPager->pPCache, pPager->dbSize);
41428 
41429   if( pagerUseWal(pPager) ){
41430     /* Drop the WAL write-lock, if any. Also, if the connection was in
41431     ** locking_mode=exclusive mode but is no longer, drop the EXCLUSIVE
41432     ** lock held on the database file.
41433     */
41434     rc2 = sqlite3WalEndWriteTransaction(pPager->pWal);
41435     assert( rc2==SQLITE_OK );
41436   }else if( rc==SQLITE_OK && bCommit && pPager->dbFileSize>pPager->dbSize ){
41437     /* This branch is taken when committing a transaction in rollback-journal
41438     ** mode if the database file on disk is larger than the database image.
41439     ** At this point the journal has been finalized and the transaction
41440     ** successfully committed, but the EXCLUSIVE lock is still held on the
41441     ** file. So it is safe to truncate the database file to its minimum
41442     ** required size.  */
41443     assert( pPager->eLock==EXCLUSIVE_LOCK );
41444     rc = pager_truncate(pPager, pPager->dbSize);
41445   }
41446 
41447   if( rc==SQLITE_OK && bCommit && isOpen(pPager->fd) ){
41448     rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_COMMIT_PHASETWO, 0);
41449     if( rc==SQLITE_NOTFOUND ) rc = SQLITE_OK;
41450   }
41451 
41452   if( !pPager->exclusiveMode
41453    && (!pagerUseWal(pPager) || sqlite3WalExclusiveMode(pPager->pWal, 0))
41454   ){
41455     rc2 = pagerUnlockDb(pPager, SHARED_LOCK);
41456     pPager->changeCountDone = 0;
41457   }
41458   pPager->eState = PAGER_READER;
41459   pPager->setMaster = 0;
41460 
41461   return (rc==SQLITE_OK?rc2:rc);
41462 }
41463 
41464 /*
41465 ** Execute a rollback if a transaction is active and unlock the
41466 ** database file.
41467 **
41468 ** If the pager has already entered the ERROR state, do not attempt
41469 ** the rollback at this time. Instead, pager_unlock() is called. The
41470 ** call to pager_unlock() will discard all in-memory pages, unlock
41471 ** the database file and move the pager back to OPEN state. If this
41472 ** means that there is a hot-journal left in the file-system, the next
41473 ** connection to obtain a shared lock on the pager (which may be this one)
41474 ** will roll it back.
41475 **
41476 ** If the pager has not already entered the ERROR state, but an IO or
41477 ** malloc error occurs during a rollback, then this will itself cause
41478 ** the pager to enter the ERROR state. Which will be cleared by the
41479 ** call to pager_unlock(), as described above.
41480 */
41481 static void pagerUnlockAndRollback(Pager *pPager){
41482   if( pPager->eState!=PAGER_ERROR && pPager->eState!=PAGER_OPEN ){
41483     assert( assert_pager_state(pPager) );
41484     if( pPager->eState>=PAGER_WRITER_LOCKED ){
41485       sqlite3BeginBenignMalloc();
41486       sqlite3PagerRollback(pPager);
41487       sqlite3EndBenignMalloc();
41488     }else if( !pPager->exclusiveMode ){
41489       assert( pPager->eState==PAGER_READER );
41490       pager_end_transaction(pPager, 0, 0);
41491     }
41492   }
41493   pager_unlock(pPager);
41494 }
41495 
41496 /*
41497 ** Parameter aData must point to a buffer of pPager->pageSize bytes
41498 ** of data. Compute and return a checksum based ont the contents of the
41499 ** page of data and the current value of pPager->cksumInit.
41500 **
41501 ** This is not a real checksum. It is really just the sum of the
41502 ** random initial value (pPager->cksumInit) and every 200th byte
41503 ** of the page data, starting with byte offset (pPager->pageSize%200).
41504 ** Each byte is interpreted as an 8-bit unsigned integer.
41505 **
41506 ** Changing the formula used to compute this checksum results in an
41507 ** incompatible journal file format.
41508 **
41509 ** If journal corruption occurs due to a power failure, the most likely
41510 ** scenario is that one end or the other of the record will be changed.
41511 ** It is much less likely that the two ends of the journal record will be
41512 ** correct and the middle be corrupt.  Thus, this "checksum" scheme,
41513 ** though fast and simple, catches the mostly likely kind of corruption.
41514 */
41515 static u32 pager_cksum(Pager *pPager, const u8 *aData){
41516   u32 cksum = pPager->cksumInit;         /* Checksum value to return */
41517   int i = pPager->pageSize-200;          /* Loop counter */
41518   while( i>0 ){
41519     cksum += aData[i];
41520     i -= 200;
41521   }
41522   return cksum;
41523 }
41524 
41525 /*
41526 ** Report the current page size and number of reserved bytes back
41527 ** to the codec.
41528 */
41529 #ifdef SQLITE_HAS_CODEC
41530 static void pagerReportSize(Pager *pPager){
41531   if( pPager->xCodecSizeChng ){
41532     pPager->xCodecSizeChng(pPager->pCodec, pPager->pageSize,
41533                            (int)pPager->nReserve);
41534   }
41535 }
41536 #else
41537 # define pagerReportSize(X)     /* No-op if we do not support a codec */
41538 #endif
41539 
41540 /*
41541 ** Read a single page from either the journal file (if isMainJrnl==1) or
41542 ** from the sub-journal (if isMainJrnl==0) and playback that page.
41543 ** The page begins at offset *pOffset into the file. The *pOffset
41544 ** value is increased to the start of the next page in the journal.
41545 **
41546 ** The main rollback journal uses checksums - the statement journal does
41547 ** not.
41548 **
41549 ** If the page number of the page record read from the (sub-)journal file
41550 ** is greater than the current value of Pager.dbSize, then playback is
41551 ** skipped and SQLITE_OK is returned.
41552 **
41553 ** If pDone is not NULL, then it is a record of pages that have already
41554 ** been played back.  If the page at *pOffset has already been played back
41555 ** (if the corresponding pDone bit is set) then skip the playback.
41556 ** Make sure the pDone bit corresponding to the *pOffset page is set
41557 ** prior to returning.
41558 **
41559 ** If the page record is successfully read from the (sub-)journal file
41560 ** and played back, then SQLITE_OK is returned. If an IO error occurs
41561 ** while reading the record from the (sub-)journal file or while writing
41562 ** to the database file, then the IO error code is returned. If data
41563 ** is successfully read from the (sub-)journal file but appears to be
41564 ** corrupted, SQLITE_DONE is returned. Data is considered corrupted in
41565 ** two circumstances:
41566 **
41567 **   * If the record page-number is illegal (0 or PAGER_MJ_PGNO), or
41568 **   * If the record is being rolled back from the main journal file
41569 **     and the checksum field does not match the record content.
41570 **
41571 ** Neither of these two scenarios are possible during a savepoint rollback.
41572 **
41573 ** If this is a savepoint rollback, then memory may have to be dynamically
41574 ** allocated by this function. If this is the case and an allocation fails,
41575 ** SQLITE_NOMEM is returned.
41576 */
41577 static int pager_playback_one_page(
41578   Pager *pPager,                /* The pager being played back */
41579   i64 *pOffset,                 /* Offset of record to playback */
41580   Bitvec *pDone,                /* Bitvec of pages already played back */
41581   int isMainJrnl,               /* 1 -> main journal. 0 -> sub-journal. */
41582   int isSavepnt                 /* True for a savepoint rollback */
41583 ){
41584   int rc;
41585   PgHdr *pPg;                   /* An existing page in the cache */
41586   Pgno pgno;                    /* The page number of a page in journal */
41587   u32 cksum;                    /* Checksum used for sanity checking */
41588   char *aData;                  /* Temporary storage for the page */
41589   sqlite3_file *jfd;            /* The file descriptor for the journal file */
41590   int isSynced;                 /* True if journal page is synced */
41591 
41592   assert( (isMainJrnl&~1)==0 );      /* isMainJrnl is 0 or 1 */
41593   assert( (isSavepnt&~1)==0 );       /* isSavepnt is 0 or 1 */
41594   assert( isMainJrnl || pDone );     /* pDone always used on sub-journals */
41595   assert( isSavepnt || pDone==0 );   /* pDone never used on non-savepoint */
41596 
41597   aData = pPager->pTmpSpace;
41598   assert( aData );         /* Temp storage must have already been allocated */
41599   assert( pagerUseWal(pPager)==0 || (!isMainJrnl && isSavepnt) );
41600 
41601   /* Either the state is greater than PAGER_WRITER_CACHEMOD (a transaction
41602   ** or savepoint rollback done at the request of the caller) or this is
41603   ** a hot-journal rollback. If it is a hot-journal rollback, the pager
41604   ** is in state OPEN and holds an EXCLUSIVE lock. Hot-journal rollback
41605   ** only reads from the main journal, not the sub-journal.
41606   */
41607   assert( pPager->eState>=PAGER_WRITER_CACHEMOD
41608        || (pPager->eState==PAGER_OPEN && pPager->eLock==EXCLUSIVE_LOCK)
41609   );
41610   assert( pPager->eState>=PAGER_WRITER_CACHEMOD || isMainJrnl );
41611 
41612   /* Read the page number and page data from the journal or sub-journal
41613   ** file. Return an error code to the caller if an IO error occurs.
41614   */
41615   jfd = isMainJrnl ? pPager->jfd : pPager->sjfd;
41616   rc = read32bits(jfd, *pOffset, &pgno);
41617   if( rc!=SQLITE_OK ) return rc;
41618   rc = sqlite3OsRead(jfd, (u8*)aData, pPager->pageSize, (*pOffset)+4);
41619   if( rc!=SQLITE_OK ) return rc;
41620   *pOffset += pPager->pageSize + 4 + isMainJrnl*4;
41621 
41622   /* Sanity checking on the page.  This is more important that I originally
41623   ** thought.  If a power failure occurs while the journal is being written,
41624   ** it could cause invalid data to be written into the journal.  We need to
41625   ** detect this invalid data (with high probability) and ignore it.
41626   */
41627   if( pgno==0 || pgno==PAGER_MJ_PGNO(pPager) ){
41628     assert( !isSavepnt );
41629     return SQLITE_DONE;
41630   }
41631   if( pgno>(Pgno)pPager->dbSize || sqlite3BitvecTest(pDone, pgno) ){
41632     return SQLITE_OK;
41633   }
41634   if( isMainJrnl ){
41635     rc = read32bits(jfd, (*pOffset)-4, &cksum);
41636     if( rc ) return rc;
41637     if( !isSavepnt && pager_cksum(pPager, (u8*)aData)!=cksum ){
41638       return SQLITE_DONE;
41639     }
41640   }
41641 
41642   /* If this page has already been played by before during the current
41643   ** rollback, then don't bother to play it back again.
41644   */
41645   if( pDone && (rc = sqlite3BitvecSet(pDone, pgno))!=SQLITE_OK ){
41646     return rc;
41647   }
41648 
41649   /* When playing back page 1, restore the nReserve setting
41650   */
41651   if( pgno==1 && pPager->nReserve!=((u8*)aData)[20] ){
41652     pPager->nReserve = ((u8*)aData)[20];
41653     pagerReportSize(pPager);
41654   }
41655 
41656   /* If the pager is in CACHEMOD state, then there must be a copy of this
41657   ** page in the pager cache. In this case just update the pager cache,
41658   ** not the database file. The page is left marked dirty in this case.
41659   **
41660   ** An exception to the above rule: If the database is in no-sync mode
41661   ** and a page is moved during an incremental vacuum then the page may
41662   ** not be in the pager cache. Later: if a malloc() or IO error occurs
41663   ** during a Movepage() call, then the page may not be in the cache
41664   ** either. So the condition described in the above paragraph is not
41665   ** assert()able.
41666   **
41667   ** If in WRITER_DBMOD, WRITER_FINISHED or OPEN state, then we update the
41668   ** pager cache if it exists and the main file. The page is then marked
41669   ** not dirty. Since this code is only executed in PAGER_OPEN state for
41670   ** a hot-journal rollback, it is guaranteed that the page-cache is empty
41671   ** if the pager is in OPEN state.
41672   **
41673   ** Ticket #1171:  The statement journal might contain page content that is
41674   ** different from the page content at the start of the transaction.
41675   ** This occurs when a page is changed prior to the start of a statement
41676   ** then changed again within the statement.  When rolling back such a
41677   ** statement we must not write to the original database unless we know
41678   ** for certain that original page contents are synced into the main rollback
41679   ** journal.  Otherwise, a power loss might leave modified data in the
41680   ** database file without an entry in the rollback journal that can
41681   ** restore the database to its original form.  Two conditions must be
41682   ** met before writing to the database files. (1) the database must be
41683   ** locked.  (2) we know that the original page content is fully synced
41684   ** in the main journal either because the page is not in cache or else
41685   ** the page is marked as needSync==0.
41686   **
41687   ** 2008-04-14:  When attempting to vacuum a corrupt database file, it
41688   ** is possible to fail a statement on a database that does not yet exist.
41689   ** Do not attempt to write if database file has never been opened.
41690   */
41691   if( pagerUseWal(pPager) ){
41692     pPg = 0;
41693   }else{
41694     pPg = pager_lookup(pPager, pgno);
41695   }
41696   assert( pPg || !MEMDB );
41697   assert( pPager->eState!=PAGER_OPEN || pPg==0 );
41698   PAGERTRACE(("PLAYBACK %d page %d hash(%08x) %s\n",
41699            PAGERID(pPager), pgno, pager_datahash(pPager->pageSize, (u8*)aData),
41700            (isMainJrnl?"main-journal":"sub-journal")
41701   ));
41702   if( isMainJrnl ){
41703     isSynced = pPager->noSync || (*pOffset <= pPager->journalHdr);
41704   }else{
41705     isSynced = (pPg==0 || 0==(pPg->flags & PGHDR_NEED_SYNC));
41706   }
41707   if( isOpen(pPager->fd)
41708    && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
41709    && isSynced
41710   ){
41711     i64 ofst = (pgno-1)*(i64)pPager->pageSize;
41712     testcase( !isSavepnt && pPg!=0 && (pPg->flags&PGHDR_NEED_SYNC)!=0 );
41713     assert( !pagerUseWal(pPager) );
41714     rc = sqlite3OsWrite(pPager->fd, (u8 *)aData, pPager->pageSize, ofst);
41715     if( pgno>pPager->dbFileSize ){
41716       pPager->dbFileSize = pgno;
41717     }
41718     if( pPager->pBackup ){
41719       CODEC1(pPager, aData, pgno, 3, rc=SQLITE_NOMEM);
41720       sqlite3BackupUpdate(pPager->pBackup, pgno, (u8*)aData);
41721       CODEC2(pPager, aData, pgno, 7, rc=SQLITE_NOMEM, aData);
41722     }
41723   }else if( !isMainJrnl && pPg==0 ){
41724     /* If this is a rollback of a savepoint and data was not written to
41725     ** the database and the page is not in-memory, there is a potential
41726     ** problem. When the page is next fetched by the b-tree layer, it
41727     ** will be read from the database file, which may or may not be
41728     ** current.
41729     **
41730     ** There are a couple of different ways this can happen. All are quite
41731     ** obscure. When running in synchronous mode, this can only happen
41732     ** if the page is on the free-list at the start of the transaction, then
41733     ** populated, then moved using sqlite3PagerMovepage().
41734     **
41735     ** The solution is to add an in-memory page to the cache containing
41736     ** the data just read from the sub-journal. Mark the page as dirty
41737     ** and if the pager requires a journal-sync, then mark the page as
41738     ** requiring a journal-sync before it is written.
41739     */
41740     assert( isSavepnt );
41741     assert( (pPager->doNotSpill & SPILLFLAG_ROLLBACK)==0 );
41742     pPager->doNotSpill |= SPILLFLAG_ROLLBACK;
41743     rc = sqlite3PagerAcquire(pPager, pgno, &pPg, 1);
41744     assert( (pPager->doNotSpill & SPILLFLAG_ROLLBACK)!=0 );
41745     pPager->doNotSpill &= ~SPILLFLAG_ROLLBACK;
41746     if( rc!=SQLITE_OK ) return rc;
41747     pPg->flags &= ~PGHDR_NEED_READ;
41748     sqlite3PcacheMakeDirty(pPg);
41749   }
41750   if( pPg ){
41751     /* No page should ever be explicitly rolled back that is in use, except
41752     ** for page 1 which is held in use in order to keep the lock on the
41753     ** database active. However such a page may be rolled back as a result
41754     ** of an internal error resulting in an automatic call to
41755     ** sqlite3PagerRollback().
41756     */
41757     void *pData;
41758     pData = pPg->pData;
41759     memcpy(pData, (u8*)aData, pPager->pageSize);
41760     pPager->xReiniter(pPg);
41761     if( isMainJrnl && (!isSavepnt || *pOffset<=pPager->journalHdr) ){
41762       /* If the contents of this page were just restored from the main
41763       ** journal file, then its content must be as they were when the
41764       ** transaction was first opened. In this case we can mark the page
41765       ** as clean, since there will be no need to write it out to the
41766       ** database.
41767       **
41768       ** There is one exception to this rule. If the page is being rolled
41769       ** back as part of a savepoint (or statement) rollback from an
41770       ** unsynced portion of the main journal file, then it is not safe
41771       ** to mark the page as clean. This is because marking the page as
41772       ** clean will clear the PGHDR_NEED_SYNC flag. Since the page is
41773       ** already in the journal file (recorded in Pager.pInJournal) and
41774       ** the PGHDR_NEED_SYNC flag is cleared, if the page is written to
41775       ** again within this transaction, it will be marked as dirty but
41776       ** the PGHDR_NEED_SYNC flag will not be set. It could then potentially
41777       ** be written out into the database file before its journal file
41778       ** segment is synced. If a crash occurs during or following this,
41779       ** database corruption may ensue.
41780       */
41781       assert( !pagerUseWal(pPager) );
41782       sqlite3PcacheMakeClean(pPg);
41783     }
41784     pager_set_pagehash(pPg);
41785 
41786     /* If this was page 1, then restore the value of Pager.dbFileVers.
41787     ** Do this before any decoding. */
41788     if( pgno==1 ){
41789       memcpy(&pPager->dbFileVers, &((u8*)pData)[24],sizeof(pPager->dbFileVers));
41790     }
41791 
41792     /* Decode the page just read from disk */
41793     CODEC1(pPager, pData, pPg->pgno, 3, rc=SQLITE_NOMEM);
41794     sqlite3PcacheRelease(pPg);
41795   }
41796   return rc;
41797 }
41798 
41799 /*
41800 ** Parameter zMaster is the name of a master journal file. A single journal
41801 ** file that referred to the master journal file has just been rolled back.
41802 ** This routine checks if it is possible to delete the master journal file,
41803 ** and does so if it is.
41804 **
41805 ** Argument zMaster may point to Pager.pTmpSpace. So that buffer is not
41806 ** available for use within this function.
41807 **
41808 ** When a master journal file is created, it is populated with the names
41809 ** of all of its child journals, one after another, formatted as utf-8
41810 ** encoded text. The end of each child journal file is marked with a
41811 ** nul-terminator byte (0x00). i.e. the entire contents of a master journal
41812 ** file for a transaction involving two databases might be:
41813 **
41814 **   "/home/bill/a.db-journal\x00/home/bill/b.db-journal\x00"
41815 **
41816 ** A master journal file may only be deleted once all of its child
41817 ** journals have been rolled back.
41818 **
41819 ** This function reads the contents of the master-journal file into
41820 ** memory and loops through each of the child journal names. For
41821 ** each child journal, it checks if:
41822 **
41823 **   * if the child journal exists, and if so
41824 **   * if the child journal contains a reference to master journal
41825 **     file zMaster
41826 **
41827 ** If a child journal can be found that matches both of the criteria
41828 ** above, this function returns without doing anything. Otherwise, if
41829 ** no such child journal can be found, file zMaster is deleted from
41830 ** the file-system using sqlite3OsDelete().
41831 **
41832 ** If an IO error within this function, an error code is returned. This
41833 ** function allocates memory by calling sqlite3Malloc(). If an allocation
41834 ** fails, SQLITE_NOMEM is returned. Otherwise, if no IO or malloc errors
41835 ** occur, SQLITE_OK is returned.
41836 **
41837 ** TODO: This function allocates a single block of memory to load
41838 ** the entire contents of the master journal file. This could be
41839 ** a couple of kilobytes or so - potentially larger than the page
41840 ** size.
41841 */
41842 static int pager_delmaster(Pager *pPager, const char *zMaster){
41843   sqlite3_vfs *pVfs = pPager->pVfs;
41844   int rc;                   /* Return code */
41845   sqlite3_file *pMaster;    /* Malloc'd master-journal file descriptor */
41846   sqlite3_file *pJournal;   /* Malloc'd child-journal file descriptor */
41847   char *zMasterJournal = 0; /* Contents of master journal file */
41848   i64 nMasterJournal;       /* Size of master journal file */
41849   char *zJournal;           /* Pointer to one journal within MJ file */
41850   char *zMasterPtr;         /* Space to hold MJ filename from a journal file */
41851   int nMasterPtr;           /* Amount of space allocated to zMasterPtr[] */
41852 
41853   /* Allocate space for both the pJournal and pMaster file descriptors.
41854   ** If successful, open the master journal file for reading.
41855   */
41856   pMaster = (sqlite3_file *)sqlite3MallocZero(pVfs->szOsFile * 2);
41857   pJournal = (sqlite3_file *)(((u8 *)pMaster) + pVfs->szOsFile);
41858   if( !pMaster ){
41859     rc = SQLITE_NOMEM;
41860   }else{
41861     const int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MASTER_JOURNAL);
41862     rc = sqlite3OsOpen(pVfs, zMaster, pMaster, flags, 0);
41863   }
41864   if( rc!=SQLITE_OK ) goto delmaster_out;
41865 
41866   /* Load the entire master journal file into space obtained from
41867   ** sqlite3_malloc() and pointed to by zMasterJournal.   Also obtain
41868   ** sufficient space (in zMasterPtr) to hold the names of master
41869   ** journal files extracted from regular rollback-journals.
41870   */
41871   rc = sqlite3OsFileSize(pMaster, &nMasterJournal);
41872   if( rc!=SQLITE_OK ) goto delmaster_out;
41873   nMasterPtr = pVfs->mxPathname+1;
41874   zMasterJournal = sqlite3Malloc((int)nMasterJournal + nMasterPtr + 1);
41875   if( !zMasterJournal ){
41876     rc = SQLITE_NOMEM;
41877     goto delmaster_out;
41878   }
41879   zMasterPtr = &zMasterJournal[nMasterJournal+1];
41880   rc = sqlite3OsRead(pMaster, zMasterJournal, (int)nMasterJournal, 0);
41881   if( rc!=SQLITE_OK ) goto delmaster_out;
41882   zMasterJournal[nMasterJournal] = 0;
41883 
41884   zJournal = zMasterJournal;
41885   while( (zJournal-zMasterJournal)<nMasterJournal ){
41886     int exists;
41887     rc = sqlite3OsAccess(pVfs, zJournal, SQLITE_ACCESS_EXISTS, &exists);
41888     if( rc!=SQLITE_OK ){
41889       goto delmaster_out;
41890     }
41891     if( exists ){
41892       /* One of the journals pointed to by the master journal exists.
41893       ** Open it and check if it points at the master journal. If
41894       ** so, return without deleting the master journal file.
41895       */
41896       int c;
41897       int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MAIN_JOURNAL);
41898       rc = sqlite3OsOpen(pVfs, zJournal, pJournal, flags, 0);
41899       if( rc!=SQLITE_OK ){
41900         goto delmaster_out;
41901       }
41902 
41903       rc = readMasterJournal(pJournal, zMasterPtr, nMasterPtr);
41904       sqlite3OsClose(pJournal);
41905       if( rc!=SQLITE_OK ){
41906         goto delmaster_out;
41907       }
41908 
41909       c = zMasterPtr[0]!=0 && strcmp(zMasterPtr, zMaster)==0;
41910       if( c ){
41911         /* We have a match. Do not delete the master journal file. */
41912         goto delmaster_out;
41913       }
41914     }
41915     zJournal += (sqlite3Strlen30(zJournal)+1);
41916   }
41917 
41918   sqlite3OsClose(pMaster);
41919   rc = sqlite3OsDelete(pVfs, zMaster, 0);
41920 
41921 delmaster_out:
41922   sqlite3_free(zMasterJournal);
41923   if( pMaster ){
41924     sqlite3OsClose(pMaster);
41925     assert( !isOpen(pJournal) );
41926     sqlite3_free(pMaster);
41927   }
41928   return rc;
41929 }
41930 
41931 
41932 /*
41933 ** This function is used to change the actual size of the database
41934 ** file in the file-system. This only happens when committing a transaction,
41935 ** or rolling back a transaction (including rolling back a hot-journal).
41936 **
41937 ** If the main database file is not open, or the pager is not in either
41938 ** DBMOD or OPEN state, this function is a no-op. Otherwise, the size
41939 ** of the file is changed to nPage pages (nPage*pPager->pageSize bytes).
41940 ** If the file on disk is currently larger than nPage pages, then use the VFS
41941 ** xTruncate() method to truncate it.
41942 **
41943 ** Or, it might might be the case that the file on disk is smaller than
41944 ** nPage pages. Some operating system implementations can get confused if
41945 ** you try to truncate a file to some size that is larger than it
41946 ** currently is, so detect this case and write a single zero byte to
41947 ** the end of the new file instead.
41948 **
41949 ** If successful, return SQLITE_OK. If an IO error occurs while modifying
41950 ** the database file, return the error code to the caller.
41951 */
41952 static int pager_truncate(Pager *pPager, Pgno nPage){
41953   int rc = SQLITE_OK;
41954   assert( pPager->eState!=PAGER_ERROR );
41955   assert( pPager->eState!=PAGER_READER );
41956 
41957   if( isOpen(pPager->fd)
41958    && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
41959   ){
41960     i64 currentSize, newSize;
41961     int szPage = pPager->pageSize;
41962     assert( pPager->eLock==EXCLUSIVE_LOCK );
41963     /* TODO: Is it safe to use Pager.dbFileSize here? */
41964     rc = sqlite3OsFileSize(pPager->fd, &currentSize);
41965     newSize = szPage*(i64)nPage;
41966     if( rc==SQLITE_OK && currentSize!=newSize ){
41967       if( currentSize>newSize ){
41968         rc = sqlite3OsTruncate(pPager->fd, newSize);
41969       }else if( (currentSize+szPage)<=newSize ){
41970         char *pTmp = pPager->pTmpSpace;
41971         memset(pTmp, 0, szPage);
41972         testcase( (newSize-szPage) == currentSize );
41973         testcase( (newSize-szPage) >  currentSize );
41974         rc = sqlite3OsWrite(pPager->fd, pTmp, szPage, newSize-szPage);
41975       }
41976       if( rc==SQLITE_OK ){
41977         pPager->dbFileSize = nPage;
41978       }
41979     }
41980   }
41981   return rc;
41982 }
41983 
41984 /*
41985 ** Return a sanitized version of the sector-size of OS file pFile. The
41986 ** return value is guaranteed to lie between 32 and MAX_SECTOR_SIZE.
41987 */
41988 SQLITE_PRIVATE int sqlite3SectorSize(sqlite3_file *pFile){
41989   int iRet = sqlite3OsSectorSize(pFile);
41990   if( iRet<32 ){
41991     iRet = 512;
41992   }else if( iRet>MAX_SECTOR_SIZE ){
41993     assert( MAX_SECTOR_SIZE>=512 );
41994     iRet = MAX_SECTOR_SIZE;
41995   }
41996   return iRet;
41997 }
41998 
41999 /*
42000 ** Set the value of the Pager.sectorSize variable for the given
42001 ** pager based on the value returned by the xSectorSize method
42002 ** of the open database file. The sector size will be used used
42003 ** to determine the size and alignment of journal header and
42004 ** master journal pointers within created journal files.
42005 **
42006 ** For temporary files the effective sector size is always 512 bytes.
42007 **
42008 ** Otherwise, for non-temporary files, the effective sector size is
42009 ** the value returned by the xSectorSize() method rounded up to 32 if
42010 ** it is less than 32, or rounded down to MAX_SECTOR_SIZE if it
42011 ** is greater than MAX_SECTOR_SIZE.
42012 **
42013 ** If the file has the SQLITE_IOCAP_POWERSAFE_OVERWRITE property, then set
42014 ** the effective sector size to its minimum value (512).  The purpose of
42015 ** pPager->sectorSize is to define the "blast radius" of bytes that
42016 ** might change if a crash occurs while writing to a single byte in
42017 ** that range.  But with POWERSAFE_OVERWRITE, the blast radius is zero
42018 ** (that is what POWERSAFE_OVERWRITE means), so we minimize the sector
42019 ** size.  For backwards compatibility of the rollback journal file format,
42020 ** we cannot reduce the effective sector size below 512.
42021 */
42022 static void setSectorSize(Pager *pPager){
42023   assert( isOpen(pPager->fd) || pPager->tempFile );
42024 
42025   if( pPager->tempFile
42026    || (sqlite3OsDeviceCharacteristics(pPager->fd) &
42027               SQLITE_IOCAP_POWERSAFE_OVERWRITE)!=0
42028   ){
42029     /* Sector size doesn't matter for temporary files. Also, the file
42030     ** may not have been opened yet, in which case the OsSectorSize()
42031     ** call will segfault. */
42032     pPager->sectorSize = 512;
42033   }else{
42034     pPager->sectorSize = sqlite3SectorSize(pPager->fd);
42035   }
42036 }
42037 
42038 /*
42039 ** Playback the journal and thus restore the database file to
42040 ** the state it was in before we started making changes.
42041 **
42042 ** The journal file format is as follows:
42043 **
42044 **  (1)  8 byte prefix.  A copy of aJournalMagic[].
42045 **  (2)  4 byte big-endian integer which is the number of valid page records
42046 **       in the journal.  If this value is 0xffffffff, then compute the
42047 **       number of page records from the journal size.
42048 **  (3)  4 byte big-endian integer which is the initial value for the
42049 **       sanity checksum.
42050 **  (4)  4 byte integer which is the number of pages to truncate the
42051 **       database to during a rollback.
42052 **  (5)  4 byte big-endian integer which is the sector size.  The header
42053 **       is this many bytes in size.
42054 **  (6)  4 byte big-endian integer which is the page size.
42055 **  (7)  zero padding out to the next sector size.
42056 **  (8)  Zero or more pages instances, each as follows:
42057 **        +  4 byte page number.
42058 **        +  pPager->pageSize bytes of data.
42059 **        +  4 byte checksum
42060 **
42061 ** When we speak of the journal header, we mean the first 7 items above.
42062 ** Each entry in the journal is an instance of the 8th item.
42063 **
42064 ** Call the value from the second bullet "nRec".  nRec is the number of
42065 ** valid page entries in the journal.  In most cases, you can compute the
42066 ** value of nRec from the size of the journal file.  But if a power
42067 ** failure occurred while the journal was being written, it could be the
42068 ** case that the size of the journal file had already been increased but
42069 ** the extra entries had not yet made it safely to disk.  In such a case,
42070 ** the value of nRec computed from the file size would be too large.  For
42071 ** that reason, we always use the nRec value in the header.
42072 **
42073 ** If the nRec value is 0xffffffff it means that nRec should be computed
42074 ** from the file size.  This value is used when the user selects the
42075 ** no-sync option for the journal.  A power failure could lead to corruption
42076 ** in this case.  But for things like temporary table (which will be
42077 ** deleted when the power is restored) we don't care.
42078 **
42079 ** If the file opened as the journal file is not a well-formed
42080 ** journal file then all pages up to the first corrupted page are rolled
42081 ** back (or no pages if the journal header is corrupted). The journal file
42082 ** is then deleted and SQLITE_OK returned, just as if no corruption had
42083 ** been encountered.
42084 **
42085 ** If an I/O or malloc() error occurs, the journal-file is not deleted
42086 ** and an error code is returned.
42087 **
42088 ** The isHot parameter indicates that we are trying to rollback a journal
42089 ** that might be a hot journal.  Or, it could be that the journal is
42090 ** preserved because of JOURNALMODE_PERSIST or JOURNALMODE_TRUNCATE.
42091 ** If the journal really is hot, reset the pager cache prior rolling
42092 ** back any content.  If the journal is merely persistent, no reset is
42093 ** needed.
42094 */
42095 static int pager_playback(Pager *pPager, int isHot){
42096   sqlite3_vfs *pVfs = pPager->pVfs;
42097   i64 szJ;                 /* Size of the journal file in bytes */
42098   u32 nRec;                /* Number of Records in the journal */
42099   u32 u;                   /* Unsigned loop counter */
42100   Pgno mxPg = 0;           /* Size of the original file in pages */
42101   int rc;                  /* Result code of a subroutine */
42102   int res = 1;             /* Value returned by sqlite3OsAccess() */
42103   char *zMaster = 0;       /* Name of master journal file if any */
42104   int needPagerReset;      /* True to reset page prior to first page rollback */
42105   int nPlayback = 0;       /* Total number of pages restored from journal */
42106 
42107   /* Figure out how many records are in the journal.  Abort early if
42108   ** the journal is empty.
42109   */
42110   assert( isOpen(pPager->jfd) );
42111   rc = sqlite3OsFileSize(pPager->jfd, &szJ);
42112   if( rc!=SQLITE_OK ){
42113     goto end_playback;
42114   }
42115 
42116   /* Read the master journal name from the journal, if it is present.
42117   ** If a master journal file name is specified, but the file is not
42118   ** present on disk, then the journal is not hot and does not need to be
42119   ** played back.
42120   **
42121   ** TODO: Technically the following is an error because it assumes that
42122   ** buffer Pager.pTmpSpace is (mxPathname+1) bytes or larger. i.e. that
42123   ** (pPager->pageSize >= pPager->pVfs->mxPathname+1). Using os_unix.c,
42124   **  mxPathname is 512, which is the same as the minimum allowable value
42125   ** for pageSize.
42126   */
42127   zMaster = pPager->pTmpSpace;
42128   rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1);
42129   if( rc==SQLITE_OK && zMaster[0] ){
42130     rc = sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS, &res);
42131   }
42132   zMaster = 0;
42133   if( rc!=SQLITE_OK || !res ){
42134     goto end_playback;
42135   }
42136   pPager->journalOff = 0;
42137   needPagerReset = isHot;
42138 
42139   /* This loop terminates either when a readJournalHdr() or
42140   ** pager_playback_one_page() call returns SQLITE_DONE or an IO error
42141   ** occurs.
42142   */
42143   while( 1 ){
42144     /* Read the next journal header from the journal file.  If there are
42145     ** not enough bytes left in the journal file for a complete header, or
42146     ** it is corrupted, then a process must have failed while writing it.
42147     ** This indicates nothing more needs to be rolled back.
42148     */
42149     rc = readJournalHdr(pPager, isHot, szJ, &nRec, &mxPg);
42150     if( rc!=SQLITE_OK ){
42151       if( rc==SQLITE_DONE ){
42152         rc = SQLITE_OK;
42153       }
42154       goto end_playback;
42155     }
42156 
42157     /* If nRec is 0xffffffff, then this journal was created by a process
42158     ** working in no-sync mode. This means that the rest of the journal
42159     ** file consists of pages, there are no more journal headers. Compute
42160     ** the value of nRec based on this assumption.
42161     */
42162     if( nRec==0xffffffff ){
42163       assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) );
42164       nRec = (int)((szJ - JOURNAL_HDR_SZ(pPager))/JOURNAL_PG_SZ(pPager));
42165     }
42166 
42167     /* If nRec is 0 and this rollback is of a transaction created by this
42168     ** process and if this is the final header in the journal, then it means
42169     ** that this part of the journal was being filled but has not yet been
42170     ** synced to disk.  Compute the number of pages based on the remaining
42171     ** size of the file.
42172     **
42173     ** The third term of the test was added to fix ticket #2565.
42174     ** When rolling back a hot journal, nRec==0 always means that the next
42175     ** chunk of the journal contains zero pages to be rolled back.  But
42176     ** when doing a ROLLBACK and the nRec==0 chunk is the last chunk in
42177     ** the journal, it means that the journal might contain additional
42178     ** pages that need to be rolled back and that the number of pages
42179     ** should be computed based on the journal file size.
42180     */
42181     if( nRec==0 && !isHot &&
42182         pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff ){
42183       nRec = (int)((szJ - pPager->journalOff) / JOURNAL_PG_SZ(pPager));
42184     }
42185 
42186     /* If this is the first header read from the journal, truncate the
42187     ** database file back to its original size.
42188     */
42189     if( pPager->journalOff==JOURNAL_HDR_SZ(pPager) ){
42190       rc = pager_truncate(pPager, mxPg);
42191       if( rc!=SQLITE_OK ){
42192         goto end_playback;
42193       }
42194       pPager->dbSize = mxPg;
42195     }
42196 
42197     /* Copy original pages out of the journal and back into the
42198     ** database file and/or page cache.
42199     */
42200     for(u=0; u<nRec; u++){
42201       if( needPagerReset ){
42202         pager_reset(pPager);
42203         needPagerReset = 0;
42204       }
42205       rc = pager_playback_one_page(pPager,&pPager->journalOff,0,1,0);
42206       if( rc==SQLITE_OK ){
42207         nPlayback++;
42208       }else{
42209         if( rc==SQLITE_DONE ){
42210           pPager->journalOff = szJ;
42211           break;
42212         }else if( rc==SQLITE_IOERR_SHORT_READ ){
42213           /* If the journal has been truncated, simply stop reading and
42214           ** processing the journal. This might happen if the journal was
42215           ** not completely written and synced prior to a crash.  In that
42216           ** case, the database should have never been written in the
42217           ** first place so it is OK to simply abandon the rollback. */
42218           rc = SQLITE_OK;
42219           goto end_playback;
42220         }else{
42221           /* If we are unable to rollback, quit and return the error
42222           ** code.  This will cause the pager to enter the error state
42223           ** so that no further harm will be done.  Perhaps the next
42224           ** process to come along will be able to rollback the database.
42225           */
42226           goto end_playback;
42227         }
42228       }
42229     }
42230   }
42231   /*NOTREACHED*/
42232   assert( 0 );
42233 
42234 end_playback:
42235   /* Following a rollback, the database file should be back in its original
42236   ** state prior to the start of the transaction, so invoke the
42237   ** SQLITE_FCNTL_DB_UNCHANGED file-control method to disable the
42238   ** assertion that the transaction counter was modified.
42239   */
42240 #ifdef SQLITE_DEBUG
42241   if( pPager->fd->pMethods ){
42242     sqlite3OsFileControlHint(pPager->fd,SQLITE_FCNTL_DB_UNCHANGED,0);
42243   }
42244 #endif
42245 
42246   /* If this playback is happening automatically as a result of an IO or
42247   ** malloc error that occurred after the change-counter was updated but
42248   ** before the transaction was committed, then the change-counter
42249   ** modification may just have been reverted. If this happens in exclusive
42250   ** mode, then subsequent transactions performed by the connection will not
42251   ** update the change-counter at all. This may lead to cache inconsistency
42252   ** problems for other processes at some point in the future. So, just
42253   ** in case this has happened, clear the changeCountDone flag now.
42254   */
42255   pPager->changeCountDone = pPager->tempFile;
42256 
42257   if( rc==SQLITE_OK ){
42258     zMaster = pPager->pTmpSpace;
42259     rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1);
42260     testcase( rc!=SQLITE_OK );
42261   }
42262   if( rc==SQLITE_OK
42263    && (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
42264   ){
42265     rc = sqlite3PagerSync(pPager, 0);
42266   }
42267   if( rc==SQLITE_OK ){
42268     rc = pager_end_transaction(pPager, zMaster[0]!='\0', 0);
42269     testcase( rc!=SQLITE_OK );
42270   }
42271   if( rc==SQLITE_OK && zMaster[0] && res ){
42272     /* If there was a master journal and this routine will return success,
42273     ** see if it is possible to delete the master journal.
42274     */
42275     rc = pager_delmaster(pPager, zMaster);
42276     testcase( rc!=SQLITE_OK );
42277   }
42278   if( isHot && nPlayback ){
42279     sqlite3_log(SQLITE_NOTICE_RECOVER_ROLLBACK, "recovered %d pages from %s",
42280                 nPlayback, pPager->zJournal);
42281   }
42282 
42283   /* The Pager.sectorSize variable may have been updated while rolling
42284   ** back a journal created by a process with a different sector size
42285   ** value. Reset it to the correct value for this process.
42286   */
42287   setSectorSize(pPager);
42288   return rc;
42289 }
42290 
42291 
42292 /*
42293 ** Read the content for page pPg out of the database file and into
42294 ** pPg->pData. A shared lock or greater must be held on the database
42295 ** file before this function is called.
42296 **
42297 ** If page 1 is read, then the value of Pager.dbFileVers[] is set to
42298 ** the value read from the database file.
42299 **
42300 ** If an IO error occurs, then the IO error is returned to the caller.
42301 ** Otherwise, SQLITE_OK is returned.
42302 */
42303 static int readDbPage(PgHdr *pPg, u32 iFrame){
42304   Pager *pPager = pPg->pPager; /* Pager object associated with page pPg */
42305   Pgno pgno = pPg->pgno;       /* Page number to read */
42306   int rc = SQLITE_OK;          /* Return code */
42307   int pgsz = pPager->pageSize; /* Number of bytes to read */
42308 
42309   assert( pPager->eState>=PAGER_READER && !MEMDB );
42310   assert( isOpen(pPager->fd) );
42311 
42312 #ifndef SQLITE_OMIT_WAL
42313   if( iFrame ){
42314     /* Try to pull the page from the write-ahead log. */
42315     rc = sqlite3WalReadFrame(pPager->pWal, iFrame, pgsz, pPg->pData);
42316   }else
42317 #endif
42318   {
42319     i64 iOffset = (pgno-1)*(i64)pPager->pageSize;
42320     rc = sqlite3OsRead(pPager->fd, pPg->pData, pgsz, iOffset);
42321     if( rc==SQLITE_IOERR_SHORT_READ ){
42322       rc = SQLITE_OK;
42323     }
42324   }
42325 
42326   if( pgno==1 ){
42327     if( rc ){
42328       /* If the read is unsuccessful, set the dbFileVers[] to something
42329       ** that will never be a valid file version.  dbFileVers[] is a copy
42330       ** of bytes 24..39 of the database.  Bytes 28..31 should always be
42331       ** zero or the size of the database in page. Bytes 32..35 and 35..39
42332       ** should be page numbers which are never 0xffffffff.  So filling
42333       ** pPager->dbFileVers[] with all 0xff bytes should suffice.
42334       **
42335       ** For an encrypted database, the situation is more complex:  bytes
42336       ** 24..39 of the database are white noise.  But the probability of
42337       ** white noising equaling 16 bytes of 0xff is vanishingly small so
42338       ** we should still be ok.
42339       */
42340       memset(pPager->dbFileVers, 0xff, sizeof(pPager->dbFileVers));
42341     }else{
42342       u8 *dbFileVers = &((u8*)pPg->pData)[24];
42343       memcpy(&pPager->dbFileVers, dbFileVers, sizeof(pPager->dbFileVers));
42344     }
42345   }
42346   CODEC1(pPager, pPg->pData, pgno, 3, rc = SQLITE_NOMEM);
42347 
42348   PAGER_INCR(sqlite3_pager_readdb_count);
42349   PAGER_INCR(pPager->nRead);
42350   IOTRACE(("PGIN %p %d\n", pPager, pgno));
42351   PAGERTRACE(("FETCH %d page %d hash(%08x)\n",
42352                PAGERID(pPager), pgno, pager_pagehash(pPg)));
42353 
42354   return rc;
42355 }
42356 
42357 /*
42358 ** Update the value of the change-counter at offsets 24 and 92 in
42359 ** the header and the sqlite version number at offset 96.
42360 **
42361 ** This is an unconditional update.  See also the pager_incr_changecounter()
42362 ** routine which only updates the change-counter if the update is actually
42363 ** needed, as determined by the pPager->changeCountDone state variable.
42364 */
42365 static void pager_write_changecounter(PgHdr *pPg){
42366   u32 change_counter;
42367 
42368   /* Increment the value just read and write it back to byte 24. */
42369   change_counter = sqlite3Get4byte((u8*)pPg->pPager->dbFileVers)+1;
42370   put32bits(((char*)pPg->pData)+24, change_counter);
42371 
42372   /* Also store the SQLite version number in bytes 96..99 and in
42373   ** bytes 92..95 store the change counter for which the version number
42374   ** is valid. */
42375   put32bits(((char*)pPg->pData)+92, change_counter);
42376   put32bits(((char*)pPg->pData)+96, SQLITE_VERSION_NUMBER);
42377 }
42378 
42379 #ifndef SQLITE_OMIT_WAL
42380 /*
42381 ** This function is invoked once for each page that has already been
42382 ** written into the log file when a WAL transaction is rolled back.
42383 ** Parameter iPg is the page number of said page. The pCtx argument
42384 ** is actually a pointer to the Pager structure.
42385 **
42386 ** If page iPg is present in the cache, and has no outstanding references,
42387 ** it is discarded. Otherwise, if there are one or more outstanding
42388 ** references, the page content is reloaded from the database. If the
42389 ** attempt to reload content from the database is required and fails,
42390 ** return an SQLite error code. Otherwise, SQLITE_OK.
42391 */
42392 static int pagerUndoCallback(void *pCtx, Pgno iPg){
42393   int rc = SQLITE_OK;
42394   Pager *pPager = (Pager *)pCtx;
42395   PgHdr *pPg;
42396 
42397   assert( pagerUseWal(pPager) );
42398   pPg = sqlite3PagerLookup(pPager, iPg);
42399   if( pPg ){
42400     if( sqlite3PcachePageRefcount(pPg)==1 ){
42401       sqlite3PcacheDrop(pPg);
42402     }else{
42403       u32 iFrame = 0;
42404       rc = sqlite3WalFindFrame(pPager->pWal, pPg->pgno, &iFrame);
42405       if( rc==SQLITE_OK ){
42406         rc = readDbPage(pPg, iFrame);
42407       }
42408       if( rc==SQLITE_OK ){
42409         pPager->xReiniter(pPg);
42410       }
42411       sqlite3PagerUnrefNotNull(pPg);
42412     }
42413   }
42414 
42415   /* Normally, if a transaction is rolled back, any backup processes are
42416   ** updated as data is copied out of the rollback journal and into the
42417   ** database. This is not generally possible with a WAL database, as
42418   ** rollback involves simply truncating the log file. Therefore, if one
42419   ** or more frames have already been written to the log (and therefore
42420   ** also copied into the backup databases) as part of this transaction,
42421   ** the backups must be restarted.
42422   */
42423   sqlite3BackupRestart(pPager->pBackup);
42424 
42425   return rc;
42426 }
42427 
42428 /*
42429 ** This function is called to rollback a transaction on a WAL database.
42430 */
42431 static int pagerRollbackWal(Pager *pPager){
42432   int rc;                         /* Return Code */
42433   PgHdr *pList;                   /* List of dirty pages to revert */
42434 
42435   /* For all pages in the cache that are currently dirty or have already
42436   ** been written (but not committed) to the log file, do one of the
42437   ** following:
42438   **
42439   **   + Discard the cached page (if refcount==0), or
42440   **   + Reload page content from the database (if refcount>0).
42441   */
42442   pPager->dbSize = pPager->dbOrigSize;
42443   rc = sqlite3WalUndo(pPager->pWal, pagerUndoCallback, (void *)pPager);
42444   pList = sqlite3PcacheDirtyList(pPager->pPCache);
42445   while( pList && rc==SQLITE_OK ){
42446     PgHdr *pNext = pList->pDirty;
42447     rc = pagerUndoCallback((void *)pPager, pList->pgno);
42448     pList = pNext;
42449   }
42450 
42451   return rc;
42452 }
42453 
42454 /*
42455 ** This function is a wrapper around sqlite3WalFrames(). As well as logging
42456 ** the contents of the list of pages headed by pList (connected by pDirty),
42457 ** this function notifies any active backup processes that the pages have
42458 ** changed.
42459 **
42460 ** The list of pages passed into this routine is always sorted by page number.
42461 ** Hence, if page 1 appears anywhere on the list, it will be the first page.
42462 */
42463 static int pagerWalFrames(
42464   Pager *pPager,                  /* Pager object */
42465   PgHdr *pList,                   /* List of frames to log */
42466   Pgno nTruncate,                 /* Database size after this commit */
42467   int isCommit                    /* True if this is a commit */
42468 ){
42469   int rc;                         /* Return code */
42470   int nList;                      /* Number of pages in pList */
42471 #if defined(SQLITE_DEBUG) || defined(SQLITE_CHECK_PAGES)
42472   PgHdr *p;                       /* For looping over pages */
42473 #endif
42474 
42475   assert( pPager->pWal );
42476   assert( pList );
42477 #ifdef SQLITE_DEBUG
42478   /* Verify that the page list is in accending order */
42479   for(p=pList; p && p->pDirty; p=p->pDirty){
42480     assert( p->pgno < p->pDirty->pgno );
42481   }
42482 #endif
42483 
42484   assert( pList->pDirty==0 || isCommit );
42485   if( isCommit ){
42486     /* If a WAL transaction is being committed, there is no point in writing
42487     ** any pages with page numbers greater than nTruncate into the WAL file.
42488     ** They will never be read by any client. So remove them from the pDirty
42489     ** list here. */
42490     PgHdr *p;
42491     PgHdr **ppNext = &pList;
42492     nList = 0;
42493     for(p=pList; (*ppNext = p)!=0; p=p->pDirty){
42494       if( p->pgno<=nTruncate ){
42495         ppNext = &p->pDirty;
42496         nList++;
42497       }
42498     }
42499     assert( pList );
42500   }else{
42501     nList = 1;
42502   }
42503   pPager->aStat[PAGER_STAT_WRITE] += nList;
42504 
42505   if( pList->pgno==1 ) pager_write_changecounter(pList);
42506   rc = sqlite3WalFrames(pPager->pWal,
42507       pPager->pageSize, pList, nTruncate, isCommit, pPager->walSyncFlags
42508   );
42509   if( rc==SQLITE_OK && pPager->pBackup ){
42510     PgHdr *p;
42511     for(p=pList; p; p=p->pDirty){
42512       sqlite3BackupUpdate(pPager->pBackup, p->pgno, (u8 *)p->pData);
42513     }
42514   }
42515 
42516 #ifdef SQLITE_CHECK_PAGES
42517   pList = sqlite3PcacheDirtyList(pPager->pPCache);
42518   for(p=pList; p; p=p->pDirty){
42519     pager_set_pagehash(p);
42520   }
42521 #endif
42522 
42523   return rc;
42524 }
42525 
42526 /*
42527 ** Begin a read transaction on the WAL.
42528 **
42529 ** This routine used to be called "pagerOpenSnapshot()" because it essentially
42530 ** makes a snapshot of the database at the current point in time and preserves
42531 ** that snapshot for use by the reader in spite of concurrently changes by
42532 ** other writers or checkpointers.
42533 */
42534 static int pagerBeginReadTransaction(Pager *pPager){
42535   int rc;                         /* Return code */
42536   int changed = 0;                /* True if cache must be reset */
42537 
42538   assert( pagerUseWal(pPager) );
42539   assert( pPager->eState==PAGER_OPEN || pPager->eState==PAGER_READER );
42540 
42541   /* sqlite3WalEndReadTransaction() was not called for the previous
42542   ** transaction in locking_mode=EXCLUSIVE.  So call it now.  If we
42543   ** are in locking_mode=NORMAL and EndRead() was previously called,
42544   ** the duplicate call is harmless.
42545   */
42546   sqlite3WalEndReadTransaction(pPager->pWal);
42547 
42548   rc = sqlite3WalBeginReadTransaction(pPager->pWal, &changed);
42549   if( rc!=SQLITE_OK || changed ){
42550     pager_reset(pPager);
42551     if( USEFETCH(pPager) ) sqlite3OsUnfetch(pPager->fd, 0, 0);
42552   }
42553 
42554   return rc;
42555 }
42556 #endif
42557 
42558 /*
42559 ** This function is called as part of the transition from PAGER_OPEN
42560 ** to PAGER_READER state to determine the size of the database file
42561 ** in pages (assuming the page size currently stored in Pager.pageSize).
42562 **
42563 ** If no error occurs, SQLITE_OK is returned and the size of the database
42564 ** in pages is stored in *pnPage. Otherwise, an error code (perhaps
42565 ** SQLITE_IOERR_FSTAT) is returned and *pnPage is left unmodified.
42566 */
42567 static int pagerPagecount(Pager *pPager, Pgno *pnPage){
42568   Pgno nPage;                     /* Value to return via *pnPage */
42569 
42570   /* Query the WAL sub-system for the database size. The WalDbsize()
42571   ** function returns zero if the WAL is not open (i.e. Pager.pWal==0), or
42572   ** if the database size is not available. The database size is not
42573   ** available from the WAL sub-system if the log file is empty or
42574   ** contains no valid committed transactions.
42575   */
42576   assert( pPager->eState==PAGER_OPEN );
42577   assert( pPager->eLock>=SHARED_LOCK );
42578   nPage = sqlite3WalDbsize(pPager->pWal);
42579 
42580   /* If the database size was not available from the WAL sub-system,
42581   ** determine it based on the size of the database file. If the size
42582   ** of the database file is not an integer multiple of the page-size,
42583   ** round down to the nearest page. Except, any file larger than 0
42584   ** bytes in size is considered to contain at least one page.
42585   */
42586   if( nPage==0 ){
42587     i64 n = 0;                    /* Size of db file in bytes */
42588     assert( isOpen(pPager->fd) || pPager->tempFile );
42589     if( isOpen(pPager->fd) ){
42590       int rc = sqlite3OsFileSize(pPager->fd, &n);
42591       if( rc!=SQLITE_OK ){
42592         return rc;
42593       }
42594     }
42595     nPage = (Pgno)((n+pPager->pageSize-1) / pPager->pageSize);
42596   }
42597 
42598   /* If the current number of pages in the file is greater than the
42599   ** configured maximum pager number, increase the allowed limit so
42600   ** that the file can be read.
42601   */
42602   if( nPage>pPager->mxPgno ){
42603     pPager->mxPgno = (Pgno)nPage;
42604   }
42605 
42606   *pnPage = nPage;
42607   return SQLITE_OK;
42608 }
42609 
42610 #ifndef SQLITE_OMIT_WAL
42611 /*
42612 ** Check if the *-wal file that corresponds to the database opened by pPager
42613 ** exists if the database is not empy, or verify that the *-wal file does
42614 ** not exist (by deleting it) if the database file is empty.
42615 **
42616 ** If the database is not empty and the *-wal file exists, open the pager
42617 ** in WAL mode.  If the database is empty or if no *-wal file exists and
42618 ** if no error occurs, make sure Pager.journalMode is not set to
42619 ** PAGER_JOURNALMODE_WAL.
42620 **
42621 ** Return SQLITE_OK or an error code.
42622 **
42623 ** The caller must hold a SHARED lock on the database file to call this
42624 ** function. Because an EXCLUSIVE lock on the db file is required to delete
42625 ** a WAL on a none-empty database, this ensures there is no race condition
42626 ** between the xAccess() below and an xDelete() being executed by some
42627 ** other connection.
42628 */
42629 static int pagerOpenWalIfPresent(Pager *pPager){
42630   int rc = SQLITE_OK;
42631   assert( pPager->eState==PAGER_OPEN );
42632   assert( pPager->eLock>=SHARED_LOCK );
42633 
42634   if( !pPager->tempFile ){
42635     int isWal;                    /* True if WAL file exists */
42636     Pgno nPage;                   /* Size of the database file */
42637 
42638     rc = pagerPagecount(pPager, &nPage);
42639     if( rc ) return rc;
42640     if( nPage==0 ){
42641       rc = sqlite3OsDelete(pPager->pVfs, pPager->zWal, 0);
42642       if( rc==SQLITE_IOERR_DELETE_NOENT ) rc = SQLITE_OK;
42643       isWal = 0;
42644     }else{
42645       rc = sqlite3OsAccess(
42646           pPager->pVfs, pPager->zWal, SQLITE_ACCESS_EXISTS, &isWal
42647       );
42648     }
42649     if( rc==SQLITE_OK ){
42650       if( isWal ){
42651         testcase( sqlite3PcachePagecount(pPager->pPCache)==0 );
42652         rc = sqlite3PagerOpenWal(pPager, 0);
42653       }else if( pPager->journalMode==PAGER_JOURNALMODE_WAL ){
42654         pPager->journalMode = PAGER_JOURNALMODE_DELETE;
42655       }
42656     }
42657   }
42658   return rc;
42659 }
42660 #endif
42661 
42662 /*
42663 ** Playback savepoint pSavepoint. Or, if pSavepoint==NULL, then playback
42664 ** the entire master journal file. The case pSavepoint==NULL occurs when
42665 ** a ROLLBACK TO command is invoked on a SAVEPOINT that is a transaction
42666 ** savepoint.
42667 **
42668 ** When pSavepoint is not NULL (meaning a non-transaction savepoint is
42669 ** being rolled back), then the rollback consists of up to three stages,
42670 ** performed in the order specified:
42671 **
42672 **   * Pages are played back from the main journal starting at byte
42673 **     offset PagerSavepoint.iOffset and continuing to
42674 **     PagerSavepoint.iHdrOffset, or to the end of the main journal
42675 **     file if PagerSavepoint.iHdrOffset is zero.
42676 **
42677 **   * If PagerSavepoint.iHdrOffset is not zero, then pages are played
42678 **     back starting from the journal header immediately following
42679 **     PagerSavepoint.iHdrOffset to the end of the main journal file.
42680 **
42681 **   * Pages are then played back from the sub-journal file, starting
42682 **     with the PagerSavepoint.iSubRec and continuing to the end of
42683 **     the journal file.
42684 **
42685 ** Throughout the rollback process, each time a page is rolled back, the
42686 ** corresponding bit is set in a bitvec structure (variable pDone in the
42687 ** implementation below). This is used to ensure that a page is only
42688 ** rolled back the first time it is encountered in either journal.
42689 **
42690 ** If pSavepoint is NULL, then pages are only played back from the main
42691 ** journal file. There is no need for a bitvec in this case.
42692 **
42693 ** In either case, before playback commences the Pager.dbSize variable
42694 ** is reset to the value that it held at the start of the savepoint
42695 ** (or transaction). No page with a page-number greater than this value
42696 ** is played back. If one is encountered it is simply skipped.
42697 */
42698 static int pagerPlaybackSavepoint(Pager *pPager, PagerSavepoint *pSavepoint){
42699   i64 szJ;                 /* Effective size of the main journal */
42700   i64 iHdrOff;             /* End of first segment of main-journal records */
42701   int rc = SQLITE_OK;      /* Return code */
42702   Bitvec *pDone = 0;       /* Bitvec to ensure pages played back only once */
42703 
42704   assert( pPager->eState!=PAGER_ERROR );
42705   assert( pPager->eState>=PAGER_WRITER_LOCKED );
42706 
42707   /* Allocate a bitvec to use to store the set of pages rolled back */
42708   if( pSavepoint ){
42709     pDone = sqlite3BitvecCreate(pSavepoint->nOrig);
42710     if( !pDone ){
42711       return SQLITE_NOMEM;
42712     }
42713   }
42714 
42715   /* Set the database size back to the value it was before the savepoint
42716   ** being reverted was opened.
42717   */
42718   pPager->dbSize = pSavepoint ? pSavepoint->nOrig : pPager->dbOrigSize;
42719   pPager->changeCountDone = pPager->tempFile;
42720 
42721   if( !pSavepoint && pagerUseWal(pPager) ){
42722     return pagerRollbackWal(pPager);
42723   }
42724 
42725   /* Use pPager->journalOff as the effective size of the main rollback
42726   ** journal.  The actual file might be larger than this in
42727   ** PAGER_JOURNALMODE_TRUNCATE or PAGER_JOURNALMODE_PERSIST.  But anything
42728   ** past pPager->journalOff is off-limits to us.
42729   */
42730   szJ = pPager->journalOff;
42731   assert( pagerUseWal(pPager)==0 || szJ==0 );
42732 
42733   /* Begin by rolling back records from the main journal starting at
42734   ** PagerSavepoint.iOffset and continuing to the next journal header.
42735   ** There might be records in the main journal that have a page number
42736   ** greater than the current database size (pPager->dbSize) but those
42737   ** will be skipped automatically.  Pages are added to pDone as they
42738   ** are played back.
42739   */
42740   if( pSavepoint && !pagerUseWal(pPager) ){
42741     iHdrOff = pSavepoint->iHdrOffset ? pSavepoint->iHdrOffset : szJ;
42742     pPager->journalOff = pSavepoint->iOffset;
42743     while( rc==SQLITE_OK && pPager->journalOff<iHdrOff ){
42744       rc = pager_playback_one_page(pPager, &pPager->journalOff, pDone, 1, 1);
42745     }
42746     assert( rc!=SQLITE_DONE );
42747   }else{
42748     pPager->journalOff = 0;
42749   }
42750 
42751   /* Continue rolling back records out of the main journal starting at
42752   ** the first journal header seen and continuing until the effective end
42753   ** of the main journal file.  Continue to skip out-of-range pages and
42754   ** continue adding pages rolled back to pDone.
42755   */
42756   while( rc==SQLITE_OK && pPager->journalOff<szJ ){
42757     u32 ii;            /* Loop counter */
42758     u32 nJRec = 0;     /* Number of Journal Records */
42759     u32 dummy;
42760     rc = readJournalHdr(pPager, 0, szJ, &nJRec, &dummy);
42761     assert( rc!=SQLITE_DONE );
42762 
42763     /*
42764     ** The "pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff"
42765     ** test is related to ticket #2565.  See the discussion in the
42766     ** pager_playback() function for additional information.
42767     */
42768     if( nJRec==0
42769      && pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff
42770     ){
42771       nJRec = (u32)((szJ - pPager->journalOff)/JOURNAL_PG_SZ(pPager));
42772     }
42773     for(ii=0; rc==SQLITE_OK && ii<nJRec && pPager->journalOff<szJ; ii++){
42774       rc = pager_playback_one_page(pPager, &pPager->journalOff, pDone, 1, 1);
42775     }
42776     assert( rc!=SQLITE_DONE );
42777   }
42778   assert( rc!=SQLITE_OK || pPager->journalOff>=szJ );
42779 
42780   /* Finally,  rollback pages from the sub-journal.  Page that were
42781   ** previously rolled back out of the main journal (and are hence in pDone)
42782   ** will be skipped.  Out-of-range pages are also skipped.
42783   */
42784   if( pSavepoint ){
42785     u32 ii;            /* Loop counter */
42786     i64 offset = (i64)pSavepoint->iSubRec*(4+pPager->pageSize);
42787 
42788     if( pagerUseWal(pPager) ){
42789       rc = sqlite3WalSavepointUndo(pPager->pWal, pSavepoint->aWalData);
42790     }
42791     for(ii=pSavepoint->iSubRec; rc==SQLITE_OK && ii<pPager->nSubRec; ii++){
42792       assert( offset==(i64)ii*(4+pPager->pageSize) );
42793       rc = pager_playback_one_page(pPager, &offset, pDone, 0, 1);
42794     }
42795     assert( rc!=SQLITE_DONE );
42796   }
42797 
42798   sqlite3BitvecDestroy(pDone);
42799   if( rc==SQLITE_OK ){
42800     pPager->journalOff = szJ;
42801   }
42802 
42803   return rc;
42804 }
42805 
42806 /*
42807 ** Change the maximum number of in-memory pages that are allowed.
42808 */
42809 SQLITE_PRIVATE void sqlite3PagerSetCachesize(Pager *pPager, int mxPage){
42810   sqlite3PcacheSetCachesize(pPager->pPCache, mxPage);
42811 }
42812 
42813 /*
42814 ** Invoke SQLITE_FCNTL_MMAP_SIZE based on the current value of szMmap.
42815 */
42816 static void pagerFixMaplimit(Pager *pPager){
42817 #if SQLITE_MAX_MMAP_SIZE>0
42818   sqlite3_file *fd = pPager->fd;
42819   if( isOpen(fd) && fd->pMethods->iVersion>=3 ){
42820     sqlite3_int64 sz;
42821     sz = pPager->szMmap;
42822     pPager->bUseFetch = (sz>0);
42823     sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_MMAP_SIZE, &sz);
42824   }
42825 #endif
42826 }
42827 
42828 /*
42829 ** Change the maximum size of any memory mapping made of the database file.
42830 */
42831 SQLITE_PRIVATE void sqlite3PagerSetMmapLimit(Pager *pPager, sqlite3_int64 szMmap){
42832   pPager->szMmap = szMmap;
42833   pagerFixMaplimit(pPager);
42834 }
42835 
42836 /*
42837 ** Free as much memory as possible from the pager.
42838 */
42839 SQLITE_PRIVATE void sqlite3PagerShrink(Pager *pPager){
42840   sqlite3PcacheShrink(pPager->pPCache);
42841 }
42842 
42843 /*
42844 ** Adjust settings of the pager to those specified in the pgFlags parameter.
42845 **
42846 ** The "level" in pgFlags & PAGER_SYNCHRONOUS_MASK sets the robustness
42847 ** of the database to damage due to OS crashes or power failures by
42848 ** changing the number of syncs()s when writing the journals.
42849 ** There are three levels:
42850 **
42851 **    OFF       sqlite3OsSync() is never called.  This is the default
42852 **              for temporary and transient files.
42853 **
42854 **    NORMAL    The journal is synced once before writes begin on the
42855 **              database.  This is normally adequate protection, but
42856 **              it is theoretically possible, though very unlikely,
42857 **              that an inopertune power failure could leave the journal
42858 **              in a state which would cause damage to the database
42859 **              when it is rolled back.
42860 **
42861 **    FULL      The journal is synced twice before writes begin on the
42862 **              database (with some additional information - the nRec field
42863 **              of the journal header - being written in between the two
42864 **              syncs).  If we assume that writing a
42865 **              single disk sector is atomic, then this mode provides
42866 **              assurance that the journal will not be corrupted to the
42867 **              point of causing damage to the database during rollback.
42868 **
42869 ** The above is for a rollback-journal mode.  For WAL mode, OFF continues
42870 ** to mean that no syncs ever occur.  NORMAL means that the WAL is synced
42871 ** prior to the start of checkpoint and that the database file is synced
42872 ** at the conclusion of the checkpoint if the entire content of the WAL
42873 ** was written back into the database.  But no sync operations occur for
42874 ** an ordinary commit in NORMAL mode with WAL.  FULL means that the WAL
42875 ** file is synced following each commit operation, in addition to the
42876 ** syncs associated with NORMAL.
42877 **
42878 ** Do not confuse synchronous=FULL with SQLITE_SYNC_FULL.  The
42879 ** SQLITE_SYNC_FULL macro means to use the MacOSX-style full-fsync
42880 ** using fcntl(F_FULLFSYNC).  SQLITE_SYNC_NORMAL means to do an
42881 ** ordinary fsync() call.  There is no difference between SQLITE_SYNC_FULL
42882 ** and SQLITE_SYNC_NORMAL on platforms other than MacOSX.  But the
42883 ** synchronous=FULL versus synchronous=NORMAL setting determines when
42884 ** the xSync primitive is called and is relevant to all platforms.
42885 **
42886 ** Numeric values associated with these states are OFF==1, NORMAL=2,
42887 ** and FULL=3.
42888 */
42889 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
42890 SQLITE_PRIVATE void sqlite3PagerSetFlags(
42891   Pager *pPager,        /* The pager to set safety level for */
42892   unsigned pgFlags      /* Various flags */
42893 ){
42894   unsigned level = pgFlags & PAGER_SYNCHRONOUS_MASK;
42895   assert( level>=1 && level<=3 );
42896   pPager->noSync =  (level==1 || pPager->tempFile) ?1:0;
42897   pPager->fullSync = (level==3 && !pPager->tempFile) ?1:0;
42898   if( pPager->noSync ){
42899     pPager->syncFlags = 0;
42900     pPager->ckptSyncFlags = 0;
42901   }else if( pgFlags & PAGER_FULLFSYNC ){
42902     pPager->syncFlags = SQLITE_SYNC_FULL;
42903     pPager->ckptSyncFlags = SQLITE_SYNC_FULL;
42904   }else if( pgFlags & PAGER_CKPT_FULLFSYNC ){
42905     pPager->syncFlags = SQLITE_SYNC_NORMAL;
42906     pPager->ckptSyncFlags = SQLITE_SYNC_FULL;
42907   }else{
42908     pPager->syncFlags = SQLITE_SYNC_NORMAL;
42909     pPager->ckptSyncFlags = SQLITE_SYNC_NORMAL;
42910   }
42911   pPager->walSyncFlags = pPager->syncFlags;
42912   if( pPager->fullSync ){
42913     pPager->walSyncFlags |= WAL_SYNC_TRANSACTIONS;
42914   }
42915   if( pgFlags & PAGER_CACHESPILL ){
42916     pPager->doNotSpill &= ~SPILLFLAG_OFF;
42917   }else{
42918     pPager->doNotSpill |= SPILLFLAG_OFF;
42919   }
42920 }
42921 #endif
42922 
42923 /*
42924 ** The following global variable is incremented whenever the library
42925 ** attempts to open a temporary file.  This information is used for
42926 ** testing and analysis only.
42927 */
42928 #ifdef SQLITE_TEST
42929 SQLITE_API int sqlite3_opentemp_count = 0;
42930 #endif
42931 
42932 /*
42933 ** Open a temporary file.
42934 **
42935 ** Write the file descriptor into *pFile. Return SQLITE_OK on success
42936 ** or some other error code if we fail. The OS will automatically
42937 ** delete the temporary file when it is closed.
42938 **
42939 ** The flags passed to the VFS layer xOpen() call are those specified
42940 ** by parameter vfsFlags ORed with the following:
42941 **
42942 **     SQLITE_OPEN_READWRITE
42943 **     SQLITE_OPEN_CREATE
42944 **     SQLITE_OPEN_EXCLUSIVE
42945 **     SQLITE_OPEN_DELETEONCLOSE
42946 */
42947 static int pagerOpentemp(
42948   Pager *pPager,        /* The pager object */
42949   sqlite3_file *pFile,  /* Write the file descriptor here */
42950   int vfsFlags          /* Flags passed through to the VFS */
42951 ){
42952   int rc;               /* Return code */
42953 
42954 #ifdef SQLITE_TEST
42955   sqlite3_opentemp_count++;  /* Used for testing and analysis only */
42956 #endif
42957 
42958   vfsFlags |=  SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE |
42959             SQLITE_OPEN_EXCLUSIVE | SQLITE_OPEN_DELETEONCLOSE;
42960   rc = sqlite3OsOpen(pPager->pVfs, 0, pFile, vfsFlags, 0);
42961   assert( rc!=SQLITE_OK || isOpen(pFile) );
42962   return rc;
42963 }
42964 
42965 /*
42966 ** Set the busy handler function.
42967 **
42968 ** The pager invokes the busy-handler if sqlite3OsLock() returns
42969 ** SQLITE_BUSY when trying to upgrade from no-lock to a SHARED lock,
42970 ** or when trying to upgrade from a RESERVED lock to an EXCLUSIVE
42971 ** lock. It does *not* invoke the busy handler when upgrading from
42972 ** SHARED to RESERVED, or when upgrading from SHARED to EXCLUSIVE
42973 ** (which occurs during hot-journal rollback). Summary:
42974 **
42975 **   Transition                        | Invokes xBusyHandler
42976 **   --------------------------------------------------------
42977 **   NO_LOCK       -> SHARED_LOCK      | Yes
42978 **   SHARED_LOCK   -> RESERVED_LOCK    | No
42979 **   SHARED_LOCK   -> EXCLUSIVE_LOCK   | No
42980 **   RESERVED_LOCK -> EXCLUSIVE_LOCK   | Yes
42981 **
42982 ** If the busy-handler callback returns non-zero, the lock is
42983 ** retried. If it returns zero, then the SQLITE_BUSY error is
42984 ** returned to the caller of the pager API function.
42985 */
42986 SQLITE_PRIVATE void sqlite3PagerSetBusyhandler(
42987   Pager *pPager,                       /* Pager object */
42988   int (*xBusyHandler)(void *),         /* Pointer to busy-handler function */
42989   void *pBusyHandlerArg                /* Argument to pass to xBusyHandler */
42990 ){
42991   pPager->xBusyHandler = xBusyHandler;
42992   pPager->pBusyHandlerArg = pBusyHandlerArg;
42993 
42994   if( isOpen(pPager->fd) ){
42995     void **ap = (void **)&pPager->xBusyHandler;
42996     assert( ((int(*)(void *))(ap[0]))==xBusyHandler );
42997     assert( ap[1]==pBusyHandlerArg );
42998     sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_BUSYHANDLER, (void *)ap);
42999   }
43000 }
43001 
43002 /*
43003 ** Change the page size used by the Pager object. The new page size
43004 ** is passed in *pPageSize.
43005 **
43006 ** If the pager is in the error state when this function is called, it
43007 ** is a no-op. The value returned is the error state error code (i.e.
43008 ** one of SQLITE_IOERR, an SQLITE_IOERR_xxx sub-code or SQLITE_FULL).
43009 **
43010 ** Otherwise, if all of the following are true:
43011 **
43012 **   * the new page size (value of *pPageSize) is valid (a power
43013 **     of two between 512 and SQLITE_MAX_PAGE_SIZE, inclusive), and
43014 **
43015 **   * there are no outstanding page references, and
43016 **
43017 **   * the database is either not an in-memory database or it is
43018 **     an in-memory database that currently consists of zero pages.
43019 **
43020 ** then the pager object page size is set to *pPageSize.
43021 **
43022 ** If the page size is changed, then this function uses sqlite3PagerMalloc()
43023 ** to obtain a new Pager.pTmpSpace buffer. If this allocation attempt
43024 ** fails, SQLITE_NOMEM is returned and the page size remains unchanged.
43025 ** In all other cases, SQLITE_OK is returned.
43026 **
43027 ** If the page size is not changed, either because one of the enumerated
43028 ** conditions above is not true, the pager was in error state when this
43029 ** function was called, or because the memory allocation attempt failed,
43030 ** then *pPageSize is set to the old, retained page size before returning.
43031 */
43032 SQLITE_PRIVATE int sqlite3PagerSetPagesize(Pager *pPager, u32 *pPageSize, int nReserve){
43033   int rc = SQLITE_OK;
43034 
43035   /* It is not possible to do a full assert_pager_state() here, as this
43036   ** function may be called from within PagerOpen(), before the state
43037   ** of the Pager object is internally consistent.
43038   **
43039   ** At one point this function returned an error if the pager was in
43040   ** PAGER_ERROR state. But since PAGER_ERROR state guarantees that
43041   ** there is at least one outstanding page reference, this function
43042   ** is a no-op for that case anyhow.
43043   */
43044 
43045   u32 pageSize = *pPageSize;
43046   assert( pageSize==0 || (pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE) );
43047   if( (pPager->memDb==0 || pPager->dbSize==0)
43048    && sqlite3PcacheRefCount(pPager->pPCache)==0
43049    && pageSize && pageSize!=(u32)pPager->pageSize
43050   ){
43051     char *pNew = NULL;             /* New temp space */
43052     i64 nByte = 0;
43053 
43054     if( pPager->eState>PAGER_OPEN && isOpen(pPager->fd) ){
43055       rc = sqlite3OsFileSize(pPager->fd, &nByte);
43056     }
43057     if( rc==SQLITE_OK ){
43058       pNew = (char *)sqlite3PageMalloc(pageSize);
43059       if( !pNew ) rc = SQLITE_NOMEM;
43060     }
43061 
43062     if( rc==SQLITE_OK ){
43063       pager_reset(pPager);
43064       pPager->dbSize = (Pgno)((nByte+pageSize-1)/pageSize);
43065       pPager->pageSize = pageSize;
43066       sqlite3PageFree(pPager->pTmpSpace);
43067       pPager->pTmpSpace = pNew;
43068       sqlite3PcacheSetPageSize(pPager->pPCache, pageSize);
43069     }
43070   }
43071 
43072   *pPageSize = pPager->pageSize;
43073   if( rc==SQLITE_OK ){
43074     if( nReserve<0 ) nReserve = pPager->nReserve;
43075     assert( nReserve>=0 && nReserve<1000 );
43076     pPager->nReserve = (i16)nReserve;
43077     pagerReportSize(pPager);
43078     pagerFixMaplimit(pPager);
43079   }
43080   return rc;
43081 }
43082 
43083 /*
43084 ** Return a pointer to the "temporary page" buffer held internally
43085 ** by the pager.  This is a buffer that is big enough to hold the
43086 ** entire content of a database page.  This buffer is used internally
43087 ** during rollback and will be overwritten whenever a rollback
43088 ** occurs.  But other modules are free to use it too, as long as
43089 ** no rollbacks are happening.
43090 */
43091 SQLITE_PRIVATE void *sqlite3PagerTempSpace(Pager *pPager){
43092   return pPager->pTmpSpace;
43093 }
43094 
43095 /*
43096 ** Attempt to set the maximum database page count if mxPage is positive.
43097 ** Make no changes if mxPage is zero or negative.  And never reduce the
43098 ** maximum page count below the current size of the database.
43099 **
43100 ** Regardless of mxPage, return the current maximum page count.
43101 */
43102 SQLITE_PRIVATE int sqlite3PagerMaxPageCount(Pager *pPager, int mxPage){
43103   if( mxPage>0 ){
43104     pPager->mxPgno = mxPage;
43105   }
43106   assert( pPager->eState!=PAGER_OPEN );      /* Called only by OP_MaxPgcnt */
43107   assert( pPager->mxPgno>=pPager->dbSize );  /* OP_MaxPgcnt enforces this */
43108   return pPager->mxPgno;
43109 }
43110 
43111 /*
43112 ** The following set of routines are used to disable the simulated
43113 ** I/O error mechanism.  These routines are used to avoid simulated
43114 ** errors in places where we do not care about errors.
43115 **
43116 ** Unless -DSQLITE_TEST=1 is used, these routines are all no-ops
43117 ** and generate no code.
43118 */
43119 #ifdef SQLITE_TEST
43120 SQLITE_API extern int sqlite3_io_error_pending;
43121 SQLITE_API extern int sqlite3_io_error_hit;
43122 static int saved_cnt;
43123 void disable_simulated_io_errors(void){
43124   saved_cnt = sqlite3_io_error_pending;
43125   sqlite3_io_error_pending = -1;
43126 }
43127 void enable_simulated_io_errors(void){
43128   sqlite3_io_error_pending = saved_cnt;
43129 }
43130 #else
43131 # define disable_simulated_io_errors()
43132 # define enable_simulated_io_errors()
43133 #endif
43134 
43135 /*
43136 ** Read the first N bytes from the beginning of the file into memory
43137 ** that pDest points to.
43138 **
43139 ** If the pager was opened on a transient file (zFilename==""), or
43140 ** opened on a file less than N bytes in size, the output buffer is
43141 ** zeroed and SQLITE_OK returned. The rationale for this is that this
43142 ** function is used to read database headers, and a new transient or
43143 ** zero sized database has a header than consists entirely of zeroes.
43144 **
43145 ** If any IO error apart from SQLITE_IOERR_SHORT_READ is encountered,
43146 ** the error code is returned to the caller and the contents of the
43147 ** output buffer undefined.
43148 */
43149 SQLITE_PRIVATE int sqlite3PagerReadFileheader(Pager *pPager, int N, unsigned char *pDest){
43150   int rc = SQLITE_OK;
43151   memset(pDest, 0, N);
43152   assert( isOpen(pPager->fd) || pPager->tempFile );
43153 
43154   /* This routine is only called by btree immediately after creating
43155   ** the Pager object.  There has not been an opportunity to transition
43156   ** to WAL mode yet.
43157   */
43158   assert( !pagerUseWal(pPager) );
43159 
43160   if( isOpen(pPager->fd) ){
43161     IOTRACE(("DBHDR %p 0 %d\n", pPager, N))
43162     rc = sqlite3OsRead(pPager->fd, pDest, N, 0);
43163     if( rc==SQLITE_IOERR_SHORT_READ ){
43164       rc = SQLITE_OK;
43165     }
43166   }
43167   return rc;
43168 }
43169 
43170 /*
43171 ** This function may only be called when a read-transaction is open on
43172 ** the pager. It returns the total number of pages in the database.
43173 **
43174 ** However, if the file is between 1 and <page-size> bytes in size, then
43175 ** this is considered a 1 page file.
43176 */
43177 SQLITE_PRIVATE void sqlite3PagerPagecount(Pager *pPager, int *pnPage){
43178   assert( pPager->eState>=PAGER_READER );
43179   assert( pPager->eState!=PAGER_WRITER_FINISHED );
43180   *pnPage = (int)pPager->dbSize;
43181 }
43182 
43183 
43184 /*
43185 ** Try to obtain a lock of type locktype on the database file. If
43186 ** a similar or greater lock is already held, this function is a no-op
43187 ** (returning SQLITE_OK immediately).
43188 **
43189 ** Otherwise, attempt to obtain the lock using sqlite3OsLock(). Invoke
43190 ** the busy callback if the lock is currently not available. Repeat
43191 ** until the busy callback returns false or until the attempt to
43192 ** obtain the lock succeeds.
43193 **
43194 ** Return SQLITE_OK on success and an error code if we cannot obtain
43195 ** the lock. If the lock is obtained successfully, set the Pager.state
43196 ** variable to locktype before returning.
43197 */
43198 static int pager_wait_on_lock(Pager *pPager, int locktype){
43199   int rc;                              /* Return code */
43200 
43201   /* Check that this is either a no-op (because the requested lock is
43202   ** already held, or one of the transistions that the busy-handler
43203   ** may be invoked during, according to the comment above
43204   ** sqlite3PagerSetBusyhandler().
43205   */
43206   assert( (pPager->eLock>=locktype)
43207        || (pPager->eLock==NO_LOCK && locktype==SHARED_LOCK)
43208        || (pPager->eLock==RESERVED_LOCK && locktype==EXCLUSIVE_LOCK)
43209   );
43210 
43211   do {
43212     rc = pagerLockDb(pPager, locktype);
43213   }while( rc==SQLITE_BUSY && pPager->xBusyHandler(pPager->pBusyHandlerArg) );
43214   return rc;
43215 }
43216 
43217 /*
43218 ** Function assertTruncateConstraint(pPager) checks that one of the
43219 ** following is true for all dirty pages currently in the page-cache:
43220 **
43221 **   a) The page number is less than or equal to the size of the
43222 **      current database image, in pages, OR
43223 **
43224 **   b) if the page content were written at this time, it would not
43225 **      be necessary to write the current content out to the sub-journal
43226 **      (as determined by function subjRequiresPage()).
43227 **
43228 ** If the condition asserted by this function were not true, and the
43229 ** dirty page were to be discarded from the cache via the pagerStress()
43230 ** routine, pagerStress() would not write the current page content to
43231 ** the database file. If a savepoint transaction were rolled back after
43232 ** this happened, the correct behavior would be to restore the current
43233 ** content of the page. However, since this content is not present in either
43234 ** the database file or the portion of the rollback journal and
43235 ** sub-journal rolled back the content could not be restored and the
43236 ** database image would become corrupt. It is therefore fortunate that
43237 ** this circumstance cannot arise.
43238 */
43239 #if defined(SQLITE_DEBUG)
43240 static void assertTruncateConstraintCb(PgHdr *pPg){
43241   assert( pPg->flags&PGHDR_DIRTY );
43242   assert( !subjRequiresPage(pPg) || pPg->pgno<=pPg->pPager->dbSize );
43243 }
43244 static void assertTruncateConstraint(Pager *pPager){
43245   sqlite3PcacheIterateDirty(pPager->pPCache, assertTruncateConstraintCb);
43246 }
43247 #else
43248 # define assertTruncateConstraint(pPager)
43249 #endif
43250 
43251 /*
43252 ** Truncate the in-memory database file image to nPage pages. This
43253 ** function does not actually modify the database file on disk. It
43254 ** just sets the internal state of the pager object so that the
43255 ** truncation will be done when the current transaction is committed.
43256 **
43257 ** This function is only called right before committing a transaction.
43258 ** Once this function has been called, the transaction must either be
43259 ** rolled back or committed. It is not safe to call this function and
43260 ** then continue writing to the database.
43261 */
43262 SQLITE_PRIVATE void sqlite3PagerTruncateImage(Pager *pPager, Pgno nPage){
43263   assert( pPager->dbSize>=nPage );
43264   assert( pPager->eState>=PAGER_WRITER_CACHEMOD );
43265   pPager->dbSize = nPage;
43266 
43267   /* At one point the code here called assertTruncateConstraint() to
43268   ** ensure that all pages being truncated away by this operation are,
43269   ** if one or more savepoints are open, present in the savepoint
43270   ** journal so that they can be restored if the savepoint is rolled
43271   ** back. This is no longer necessary as this function is now only
43272   ** called right before committing a transaction. So although the
43273   ** Pager object may still have open savepoints (Pager.nSavepoint!=0),
43274   ** they cannot be rolled back. So the assertTruncateConstraint() call
43275   ** is no longer correct. */
43276 }
43277 
43278 
43279 /*
43280 ** This function is called before attempting a hot-journal rollback. It
43281 ** syncs the journal file to disk, then sets pPager->journalHdr to the
43282 ** size of the journal file so that the pager_playback() routine knows
43283 ** that the entire journal file has been synced.
43284 **
43285 ** Syncing a hot-journal to disk before attempting to roll it back ensures
43286 ** that if a power-failure occurs during the rollback, the process that
43287 ** attempts rollback following system recovery sees the same journal
43288 ** content as this process.
43289 **
43290 ** If everything goes as planned, SQLITE_OK is returned. Otherwise,
43291 ** an SQLite error code.
43292 */
43293 static int pagerSyncHotJournal(Pager *pPager){
43294   int rc = SQLITE_OK;
43295   if( !pPager->noSync ){
43296     rc = sqlite3OsSync(pPager->jfd, SQLITE_SYNC_NORMAL);
43297   }
43298   if( rc==SQLITE_OK ){
43299     rc = sqlite3OsFileSize(pPager->jfd, &pPager->journalHdr);
43300   }
43301   return rc;
43302 }
43303 
43304 /*
43305 ** Obtain a reference to a memory mapped page object for page number pgno.
43306 ** The new object will use the pointer pData, obtained from xFetch().
43307 ** If successful, set *ppPage to point to the new page reference
43308 ** and return SQLITE_OK. Otherwise, return an SQLite error code and set
43309 ** *ppPage to zero.
43310 **
43311 ** Page references obtained by calling this function should be released
43312 ** by calling pagerReleaseMapPage().
43313 */
43314 static int pagerAcquireMapPage(
43315   Pager *pPager,                  /* Pager object */
43316   Pgno pgno,                      /* Page number */
43317   void *pData,                    /* xFetch()'d data for this page */
43318   PgHdr **ppPage                  /* OUT: Acquired page object */
43319 ){
43320   PgHdr *p;                       /* Memory mapped page to return */
43321 
43322   if( pPager->pMmapFreelist ){
43323     *ppPage = p = pPager->pMmapFreelist;
43324     pPager->pMmapFreelist = p->pDirty;
43325     p->pDirty = 0;
43326     memset(p->pExtra, 0, pPager->nExtra);
43327   }else{
43328     *ppPage = p = (PgHdr *)sqlite3MallocZero(sizeof(PgHdr) + pPager->nExtra);
43329     if( p==0 ){
43330       sqlite3OsUnfetch(pPager->fd, (i64)(pgno-1) * pPager->pageSize, pData);
43331       return SQLITE_NOMEM;
43332     }
43333     p->pExtra = (void *)&p[1];
43334     p->flags = PGHDR_MMAP;
43335     p->nRef = 1;
43336     p->pPager = pPager;
43337   }
43338 
43339   assert( p->pExtra==(void *)&p[1] );
43340   assert( p->pPage==0 );
43341   assert( p->flags==PGHDR_MMAP );
43342   assert( p->pPager==pPager );
43343   assert( p->nRef==1 );
43344 
43345   p->pgno = pgno;
43346   p->pData = pData;
43347   pPager->nMmapOut++;
43348 
43349   return SQLITE_OK;
43350 }
43351 
43352 /*
43353 ** Release a reference to page pPg. pPg must have been returned by an
43354 ** earlier call to pagerAcquireMapPage().
43355 */
43356 static void pagerReleaseMapPage(PgHdr *pPg){
43357   Pager *pPager = pPg->pPager;
43358   pPager->nMmapOut--;
43359   pPg->pDirty = pPager->pMmapFreelist;
43360   pPager->pMmapFreelist = pPg;
43361 
43362   assert( pPager->fd->pMethods->iVersion>=3 );
43363   sqlite3OsUnfetch(pPager->fd, (i64)(pPg->pgno-1)*pPager->pageSize, pPg->pData);
43364 }
43365 
43366 /*
43367 ** Free all PgHdr objects stored in the Pager.pMmapFreelist list.
43368 */
43369 static void pagerFreeMapHdrs(Pager *pPager){
43370   PgHdr *p;
43371   PgHdr *pNext;
43372   for(p=pPager->pMmapFreelist; p; p=pNext){
43373     pNext = p->pDirty;
43374     sqlite3_free(p);
43375   }
43376 }
43377 
43378 
43379 /*
43380 ** Shutdown the page cache.  Free all memory and close all files.
43381 **
43382 ** If a transaction was in progress when this routine is called, that
43383 ** transaction is rolled back.  All outstanding pages are invalidated
43384 ** and their memory is freed.  Any attempt to use a page associated
43385 ** with this page cache after this function returns will likely
43386 ** result in a coredump.
43387 **
43388 ** This function always succeeds. If a transaction is active an attempt
43389 ** is made to roll it back. If an error occurs during the rollback
43390 ** a hot journal may be left in the filesystem but no error is returned
43391 ** to the caller.
43392 */
43393 SQLITE_PRIVATE int sqlite3PagerClose(Pager *pPager){
43394   u8 *pTmp = (u8 *)pPager->pTmpSpace;
43395 
43396   assert( assert_pager_state(pPager) );
43397   disable_simulated_io_errors();
43398   sqlite3BeginBenignMalloc();
43399   pagerFreeMapHdrs(pPager);
43400   /* pPager->errCode = 0; */
43401   pPager->exclusiveMode = 0;
43402 #ifndef SQLITE_OMIT_WAL
43403   sqlite3WalClose(pPager->pWal, pPager->ckptSyncFlags, pPager->pageSize, pTmp);
43404   pPager->pWal = 0;
43405 #endif
43406   pager_reset(pPager);
43407   if( MEMDB ){
43408     pager_unlock(pPager);
43409   }else{
43410     /* If it is open, sync the journal file before calling UnlockAndRollback.
43411     ** If this is not done, then an unsynced portion of the open journal
43412     ** file may be played back into the database. If a power failure occurs
43413     ** while this is happening, the database could become corrupt.
43414     **
43415     ** If an error occurs while trying to sync the journal, shift the pager
43416     ** into the ERROR state. This causes UnlockAndRollback to unlock the
43417     ** database and close the journal file without attempting to roll it
43418     ** back or finalize it. The next database user will have to do hot-journal
43419     ** rollback before accessing the database file.
43420     */
43421     if( isOpen(pPager->jfd) ){
43422       pager_error(pPager, pagerSyncHotJournal(pPager));
43423     }
43424     pagerUnlockAndRollback(pPager);
43425   }
43426   sqlite3EndBenignMalloc();
43427   enable_simulated_io_errors();
43428   PAGERTRACE(("CLOSE %d\n", PAGERID(pPager)));
43429   IOTRACE(("CLOSE %p\n", pPager))
43430   sqlite3OsClose(pPager->jfd);
43431   sqlite3OsClose(pPager->fd);
43432   sqlite3PageFree(pTmp);
43433   sqlite3PcacheClose(pPager->pPCache);
43434 
43435 #ifdef SQLITE_HAS_CODEC
43436   if( pPager->xCodecFree ) pPager->xCodecFree(pPager->pCodec);
43437 #endif
43438 
43439   assert( !pPager->aSavepoint && !pPager->pInJournal );
43440   assert( !isOpen(pPager->jfd) && !isOpen(pPager->sjfd) );
43441 
43442   sqlite3_free(pPager);
43443   return SQLITE_OK;
43444 }
43445 
43446 #if !defined(NDEBUG) || defined(SQLITE_TEST)
43447 /*
43448 ** Return the page number for page pPg.
43449 */
43450 SQLITE_PRIVATE Pgno sqlite3PagerPagenumber(DbPage *pPg){
43451   return pPg->pgno;
43452 }
43453 #endif
43454 
43455 /*
43456 ** Increment the reference count for page pPg.
43457 */
43458 SQLITE_PRIVATE void sqlite3PagerRef(DbPage *pPg){
43459   sqlite3PcacheRef(pPg);
43460 }
43461 
43462 /*
43463 ** Sync the journal. In other words, make sure all the pages that have
43464 ** been written to the journal have actually reached the surface of the
43465 ** disk and can be restored in the event of a hot-journal rollback.
43466 **
43467 ** If the Pager.noSync flag is set, then this function is a no-op.
43468 ** Otherwise, the actions required depend on the journal-mode and the
43469 ** device characteristics of the file-system, as follows:
43470 **
43471 **   * If the journal file is an in-memory journal file, no action need
43472 **     be taken.
43473 **
43474 **   * Otherwise, if the device does not support the SAFE_APPEND property,
43475 **     then the nRec field of the most recently written journal header
43476 **     is updated to contain the number of journal records that have
43477 **     been written following it. If the pager is operating in full-sync
43478 **     mode, then the journal file is synced before this field is updated.
43479 **
43480 **   * If the device does not support the SEQUENTIAL property, then
43481 **     journal file is synced.
43482 **
43483 ** Or, in pseudo-code:
43484 **
43485 **   if( NOT <in-memory journal> ){
43486 **     if( NOT SAFE_APPEND ){
43487 **       if( <full-sync mode> ) xSync(<journal file>);
43488 **       <update nRec field>
43489 **     }
43490 **     if( NOT SEQUENTIAL ) xSync(<journal file>);
43491 **   }
43492 **
43493 ** If successful, this routine clears the PGHDR_NEED_SYNC flag of every
43494 ** page currently held in memory before returning SQLITE_OK. If an IO
43495 ** error is encountered, then the IO error code is returned to the caller.
43496 */
43497 static int syncJournal(Pager *pPager, int newHdr){
43498   int rc;                         /* Return code */
43499 
43500   assert( pPager->eState==PAGER_WRITER_CACHEMOD
43501        || pPager->eState==PAGER_WRITER_DBMOD
43502   );
43503   assert( assert_pager_state(pPager) );
43504   assert( !pagerUseWal(pPager) );
43505 
43506   rc = sqlite3PagerExclusiveLock(pPager);
43507   if( rc!=SQLITE_OK ) return rc;
43508 
43509   if( !pPager->noSync ){
43510     assert( !pPager->tempFile );
43511     if( isOpen(pPager->jfd) && pPager->journalMode!=PAGER_JOURNALMODE_MEMORY ){
43512       const int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
43513       assert( isOpen(pPager->jfd) );
43514 
43515       if( 0==(iDc&SQLITE_IOCAP_SAFE_APPEND) ){
43516         /* This block deals with an obscure problem. If the last connection
43517         ** that wrote to this database was operating in persistent-journal
43518         ** mode, then the journal file may at this point actually be larger
43519         ** than Pager.journalOff bytes. If the next thing in the journal
43520         ** file happens to be a journal-header (written as part of the
43521         ** previous connection's transaction), and a crash or power-failure
43522         ** occurs after nRec is updated but before this connection writes
43523         ** anything else to the journal file (or commits/rolls back its
43524         ** transaction), then SQLite may become confused when doing the
43525         ** hot-journal rollback following recovery. It may roll back all
43526         ** of this connections data, then proceed to rolling back the old,
43527         ** out-of-date data that follows it. Database corruption.
43528         **
43529         ** To work around this, if the journal file does appear to contain
43530         ** a valid header following Pager.journalOff, then write a 0x00
43531         ** byte to the start of it to prevent it from being recognized.
43532         **
43533         ** Variable iNextHdrOffset is set to the offset at which this
43534         ** problematic header will occur, if it exists. aMagic is used
43535         ** as a temporary buffer to inspect the first couple of bytes of
43536         ** the potential journal header.
43537         */
43538         i64 iNextHdrOffset;
43539         u8 aMagic[8];
43540         u8 zHeader[sizeof(aJournalMagic)+4];
43541 
43542         memcpy(zHeader, aJournalMagic, sizeof(aJournalMagic));
43543         put32bits(&zHeader[sizeof(aJournalMagic)], pPager->nRec);
43544 
43545         iNextHdrOffset = journalHdrOffset(pPager);
43546         rc = sqlite3OsRead(pPager->jfd, aMagic, 8, iNextHdrOffset);
43547         if( rc==SQLITE_OK && 0==memcmp(aMagic, aJournalMagic, 8) ){
43548           static const u8 zerobyte = 0;
43549           rc = sqlite3OsWrite(pPager->jfd, &zerobyte, 1, iNextHdrOffset);
43550         }
43551         if( rc!=SQLITE_OK && rc!=SQLITE_IOERR_SHORT_READ ){
43552           return rc;
43553         }
43554 
43555         /* Write the nRec value into the journal file header. If in
43556         ** full-synchronous mode, sync the journal first. This ensures that
43557         ** all data has really hit the disk before nRec is updated to mark
43558         ** it as a candidate for rollback.
43559         **
43560         ** This is not required if the persistent media supports the
43561         ** SAFE_APPEND property. Because in this case it is not possible
43562         ** for garbage data to be appended to the file, the nRec field
43563         ** is populated with 0xFFFFFFFF when the journal header is written
43564         ** and never needs to be updated.
43565         */
43566         if( pPager->fullSync && 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
43567           PAGERTRACE(("SYNC journal of %d\n", PAGERID(pPager)));
43568           IOTRACE(("JSYNC %p\n", pPager))
43569           rc = sqlite3OsSync(pPager->jfd, pPager->syncFlags);
43570           if( rc!=SQLITE_OK ) return rc;
43571         }
43572         IOTRACE(("JHDR %p %lld\n", pPager, pPager->journalHdr));
43573         rc = sqlite3OsWrite(
43574             pPager->jfd, zHeader, sizeof(zHeader), pPager->journalHdr
43575         );
43576         if( rc!=SQLITE_OK ) return rc;
43577       }
43578       if( 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
43579         PAGERTRACE(("SYNC journal of %d\n", PAGERID(pPager)));
43580         IOTRACE(("JSYNC %p\n", pPager))
43581         rc = sqlite3OsSync(pPager->jfd, pPager->syncFlags|
43582           (pPager->syncFlags==SQLITE_SYNC_FULL?SQLITE_SYNC_DATAONLY:0)
43583         );
43584         if( rc!=SQLITE_OK ) return rc;
43585       }
43586 
43587       pPager->journalHdr = pPager->journalOff;
43588       if( newHdr && 0==(iDc&SQLITE_IOCAP_SAFE_APPEND) ){
43589         pPager->nRec = 0;
43590         rc = writeJournalHdr(pPager);
43591         if( rc!=SQLITE_OK ) return rc;
43592       }
43593     }else{
43594       pPager->journalHdr = pPager->journalOff;
43595     }
43596   }
43597 
43598   /* Unless the pager is in noSync mode, the journal file was just
43599   ** successfully synced. Either way, clear the PGHDR_NEED_SYNC flag on
43600   ** all pages.
43601   */
43602   sqlite3PcacheClearSyncFlags(pPager->pPCache);
43603   pPager->eState = PAGER_WRITER_DBMOD;
43604   assert( assert_pager_state(pPager) );
43605   return SQLITE_OK;
43606 }
43607 
43608 /*
43609 ** The argument is the first in a linked list of dirty pages connected
43610 ** by the PgHdr.pDirty pointer. This function writes each one of the
43611 ** in-memory pages in the list to the database file. The argument may
43612 ** be NULL, representing an empty list. In this case this function is
43613 ** a no-op.
43614 **
43615 ** The pager must hold at least a RESERVED lock when this function
43616 ** is called. Before writing anything to the database file, this lock
43617 ** is upgraded to an EXCLUSIVE lock. If the lock cannot be obtained,
43618 ** SQLITE_BUSY is returned and no data is written to the database file.
43619 **
43620 ** If the pager is a temp-file pager and the actual file-system file
43621 ** is not yet open, it is created and opened before any data is
43622 ** written out.
43623 **
43624 ** Once the lock has been upgraded and, if necessary, the file opened,
43625 ** the pages are written out to the database file in list order. Writing
43626 ** a page is skipped if it meets either of the following criteria:
43627 **
43628 **   * The page number is greater than Pager.dbSize, or
43629 **   * The PGHDR_DONT_WRITE flag is set on the page.
43630 **
43631 ** If writing out a page causes the database file to grow, Pager.dbFileSize
43632 ** is updated accordingly. If page 1 is written out, then the value cached
43633 ** in Pager.dbFileVers[] is updated to match the new value stored in
43634 ** the database file.
43635 **
43636 ** If everything is successful, SQLITE_OK is returned. If an IO error
43637 ** occurs, an IO error code is returned. Or, if the EXCLUSIVE lock cannot
43638 ** be obtained, SQLITE_BUSY is returned.
43639 */
43640 static int pager_write_pagelist(Pager *pPager, PgHdr *pList){
43641   int rc = SQLITE_OK;                  /* Return code */
43642 
43643   /* This function is only called for rollback pagers in WRITER_DBMOD state. */
43644   assert( !pagerUseWal(pPager) );
43645   assert( pPager->eState==PAGER_WRITER_DBMOD );
43646   assert( pPager->eLock==EXCLUSIVE_LOCK );
43647 
43648   /* If the file is a temp-file has not yet been opened, open it now. It
43649   ** is not possible for rc to be other than SQLITE_OK if this branch
43650   ** is taken, as pager_wait_on_lock() is a no-op for temp-files.
43651   */
43652   if( !isOpen(pPager->fd) ){
43653     assert( pPager->tempFile && rc==SQLITE_OK );
43654     rc = pagerOpentemp(pPager, pPager->fd, pPager->vfsFlags);
43655   }
43656 
43657   /* Before the first write, give the VFS a hint of what the final
43658   ** file size will be.
43659   */
43660   assert( rc!=SQLITE_OK || isOpen(pPager->fd) );
43661   if( rc==SQLITE_OK
43662    && pPager->dbHintSize<pPager->dbSize
43663    && (pList->pDirty || pList->pgno>pPager->dbHintSize)
43664   ){
43665     sqlite3_int64 szFile = pPager->pageSize * (sqlite3_int64)pPager->dbSize;
43666     sqlite3OsFileControlHint(pPager->fd, SQLITE_FCNTL_SIZE_HINT, &szFile);
43667     pPager->dbHintSize = pPager->dbSize;
43668   }
43669 
43670   while( rc==SQLITE_OK && pList ){
43671     Pgno pgno = pList->pgno;
43672 
43673     /* If there are dirty pages in the page cache with page numbers greater
43674     ** than Pager.dbSize, this means sqlite3PagerTruncateImage() was called to
43675     ** make the file smaller (presumably by auto-vacuum code). Do not write
43676     ** any such pages to the file.
43677     **
43678     ** Also, do not write out any page that has the PGHDR_DONT_WRITE flag
43679     ** set (set by sqlite3PagerDontWrite()).
43680     */
43681     if( pgno<=pPager->dbSize && 0==(pList->flags&PGHDR_DONT_WRITE) ){
43682       i64 offset = (pgno-1)*(i64)pPager->pageSize;   /* Offset to write */
43683       char *pData;                                   /* Data to write */
43684 
43685       assert( (pList->flags&PGHDR_NEED_SYNC)==0 );
43686       if( pList->pgno==1 ) pager_write_changecounter(pList);
43687 
43688       /* Encode the database */
43689       CODEC2(pPager, pList->pData, pgno, 6, return SQLITE_NOMEM, pData);
43690 
43691       /* Write out the page data. */
43692       rc = sqlite3OsWrite(pPager->fd, pData, pPager->pageSize, offset);
43693 
43694       /* If page 1 was just written, update Pager.dbFileVers to match
43695       ** the value now stored in the database file. If writing this
43696       ** page caused the database file to grow, update dbFileSize.
43697       */
43698       if( pgno==1 ){
43699         memcpy(&pPager->dbFileVers, &pData[24], sizeof(pPager->dbFileVers));
43700       }
43701       if( pgno>pPager->dbFileSize ){
43702         pPager->dbFileSize = pgno;
43703       }
43704       pPager->aStat[PAGER_STAT_WRITE]++;
43705 
43706       /* Update any backup objects copying the contents of this pager. */
43707       sqlite3BackupUpdate(pPager->pBackup, pgno, (u8*)pList->pData);
43708 
43709       PAGERTRACE(("STORE %d page %d hash(%08x)\n",
43710                    PAGERID(pPager), pgno, pager_pagehash(pList)));
43711       IOTRACE(("PGOUT %p %d\n", pPager, pgno));
43712       PAGER_INCR(sqlite3_pager_writedb_count);
43713     }else{
43714       PAGERTRACE(("NOSTORE %d page %d\n", PAGERID(pPager), pgno));
43715     }
43716     pager_set_pagehash(pList);
43717     pList = pList->pDirty;
43718   }
43719 
43720   return rc;
43721 }
43722 
43723 /*
43724 ** Ensure that the sub-journal file is open. If it is already open, this
43725 ** function is a no-op.
43726 **
43727 ** SQLITE_OK is returned if everything goes according to plan. An
43728 ** SQLITE_IOERR_XXX error code is returned if a call to sqlite3OsOpen()
43729 ** fails.
43730 */
43731 static int openSubJournal(Pager *pPager){
43732   int rc = SQLITE_OK;
43733   if( !isOpen(pPager->sjfd) ){
43734     if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY || pPager->subjInMemory ){
43735       sqlite3MemJournalOpen(pPager->sjfd);
43736     }else{
43737       rc = pagerOpentemp(pPager, pPager->sjfd, SQLITE_OPEN_SUBJOURNAL);
43738     }
43739   }
43740   return rc;
43741 }
43742 
43743 /*
43744 ** Append a record of the current state of page pPg to the sub-journal.
43745 ** It is the callers responsibility to use subjRequiresPage() to check
43746 ** that it is really required before calling this function.
43747 **
43748 ** If successful, set the bit corresponding to pPg->pgno in the bitvecs
43749 ** for all open savepoints before returning.
43750 **
43751 ** This function returns SQLITE_OK if everything is successful, an IO
43752 ** error code if the attempt to write to the sub-journal fails, or
43753 ** SQLITE_NOMEM if a malloc fails while setting a bit in a savepoint
43754 ** bitvec.
43755 */
43756 static int subjournalPage(PgHdr *pPg){
43757   int rc = SQLITE_OK;
43758   Pager *pPager = pPg->pPager;
43759   if( pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
43760 
43761     /* Open the sub-journal, if it has not already been opened */
43762     assert( pPager->useJournal );
43763     assert( isOpen(pPager->jfd) || pagerUseWal(pPager) );
43764     assert( isOpen(pPager->sjfd) || pPager->nSubRec==0 );
43765     assert( pagerUseWal(pPager)
43766          || pageInJournal(pPager, pPg)
43767          || pPg->pgno>pPager->dbOrigSize
43768     );
43769     rc = openSubJournal(pPager);
43770 
43771     /* If the sub-journal was opened successfully (or was already open),
43772     ** write the journal record into the file.  */
43773     if( rc==SQLITE_OK ){
43774       void *pData = pPg->pData;
43775       i64 offset = (i64)pPager->nSubRec*(4+pPager->pageSize);
43776       char *pData2;
43777 
43778       CODEC2(pPager, pData, pPg->pgno, 7, return SQLITE_NOMEM, pData2);
43779       PAGERTRACE(("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno));
43780       rc = write32bits(pPager->sjfd, offset, pPg->pgno);
43781       if( rc==SQLITE_OK ){
43782         rc = sqlite3OsWrite(pPager->sjfd, pData2, pPager->pageSize, offset+4);
43783       }
43784     }
43785   }
43786   if( rc==SQLITE_OK ){
43787     pPager->nSubRec++;
43788     assert( pPager->nSavepoint>0 );
43789     rc = addToSavepointBitvecs(pPager, pPg->pgno);
43790   }
43791   return rc;
43792 }
43793 
43794 /*
43795 ** This function is called by the pcache layer when it has reached some
43796 ** soft memory limit. The first argument is a pointer to a Pager object
43797 ** (cast as a void*). The pager is always 'purgeable' (not an in-memory
43798 ** database). The second argument is a reference to a page that is
43799 ** currently dirty but has no outstanding references. The page
43800 ** is always associated with the Pager object passed as the first
43801 ** argument.
43802 **
43803 ** The job of this function is to make pPg clean by writing its contents
43804 ** out to the database file, if possible. This may involve syncing the
43805 ** journal file.
43806 **
43807 ** If successful, sqlite3PcacheMakeClean() is called on the page and
43808 ** SQLITE_OK returned. If an IO error occurs while trying to make the
43809 ** page clean, the IO error code is returned. If the page cannot be
43810 ** made clean for some other reason, but no error occurs, then SQLITE_OK
43811 ** is returned by sqlite3PcacheMakeClean() is not called.
43812 */
43813 static int pagerStress(void *p, PgHdr *pPg){
43814   Pager *pPager = (Pager *)p;
43815   int rc = SQLITE_OK;
43816 
43817   assert( pPg->pPager==pPager );
43818   assert( pPg->flags&PGHDR_DIRTY );
43819 
43820   /* The doNotSpill NOSYNC bit is set during times when doing a sync of
43821   ** journal (and adding a new header) is not allowed.  This occurs
43822   ** during calls to sqlite3PagerWrite() while trying to journal multiple
43823   ** pages belonging to the same sector.
43824   **
43825   ** The doNotSpill ROLLBACK and OFF bits inhibits all cache spilling
43826   ** regardless of whether or not a sync is required.  This is set during
43827   ** a rollback or by user request, respectively.
43828   **
43829   ** Spilling is also prohibited when in an error state since that could
43830   ** lead to database corruption.   In the current implementaton it
43831   ** is impossible for sqlite3PcacheFetch() to be called with createFlag==1
43832   ** while in the error state, hence it is impossible for this routine to
43833   ** be called in the error state.  Nevertheless, we include a NEVER()
43834   ** test for the error state as a safeguard against future changes.
43835   */
43836   if( NEVER(pPager->errCode) ) return SQLITE_OK;
43837   testcase( pPager->doNotSpill & SPILLFLAG_ROLLBACK );
43838   testcase( pPager->doNotSpill & SPILLFLAG_OFF );
43839   testcase( pPager->doNotSpill & SPILLFLAG_NOSYNC );
43840   if( pPager->doNotSpill
43841    && ((pPager->doNotSpill & (SPILLFLAG_ROLLBACK|SPILLFLAG_OFF))!=0
43842       || (pPg->flags & PGHDR_NEED_SYNC)!=0)
43843   ){
43844     return SQLITE_OK;
43845   }
43846 
43847   pPg->pDirty = 0;
43848   if( pagerUseWal(pPager) ){
43849     /* Write a single frame for this page to the log. */
43850     if( subjRequiresPage(pPg) ){
43851       rc = subjournalPage(pPg);
43852     }
43853     if( rc==SQLITE_OK ){
43854       rc = pagerWalFrames(pPager, pPg, 0, 0);
43855     }
43856   }else{
43857 
43858     /* Sync the journal file if required. */
43859     if( pPg->flags&PGHDR_NEED_SYNC
43860      || pPager->eState==PAGER_WRITER_CACHEMOD
43861     ){
43862       rc = syncJournal(pPager, 1);
43863     }
43864 
43865     /* If the page number of this page is larger than the current size of
43866     ** the database image, it may need to be written to the sub-journal.
43867     ** This is because the call to pager_write_pagelist() below will not
43868     ** actually write data to the file in this case.
43869     **
43870     ** Consider the following sequence of events:
43871     **
43872     **   BEGIN;
43873     **     <journal page X>
43874     **     <modify page X>
43875     **     SAVEPOINT sp;
43876     **       <shrink database file to Y pages>
43877     **       pagerStress(page X)
43878     **     ROLLBACK TO sp;
43879     **
43880     ** If (X>Y), then when pagerStress is called page X will not be written
43881     ** out to the database file, but will be dropped from the cache. Then,
43882     ** following the "ROLLBACK TO sp" statement, reading page X will read
43883     ** data from the database file. This will be the copy of page X as it
43884     ** was when the transaction started, not as it was when "SAVEPOINT sp"
43885     ** was executed.
43886     **
43887     ** The solution is to write the current data for page X into the
43888     ** sub-journal file now (if it is not already there), so that it will
43889     ** be restored to its current value when the "ROLLBACK TO sp" is
43890     ** executed.
43891     */
43892     if( NEVER(
43893         rc==SQLITE_OK && pPg->pgno>pPager->dbSize && subjRequiresPage(pPg)
43894     ) ){
43895       rc = subjournalPage(pPg);
43896     }
43897 
43898     /* Write the contents of the page out to the database file. */
43899     if( rc==SQLITE_OK ){
43900       assert( (pPg->flags&PGHDR_NEED_SYNC)==0 );
43901       rc = pager_write_pagelist(pPager, pPg);
43902     }
43903   }
43904 
43905   /* Mark the page as clean. */
43906   if( rc==SQLITE_OK ){
43907     PAGERTRACE(("STRESS %d page %d\n", PAGERID(pPager), pPg->pgno));
43908     sqlite3PcacheMakeClean(pPg);
43909   }
43910 
43911   return pager_error(pPager, rc);
43912 }
43913 
43914 
43915 /*
43916 ** Allocate and initialize a new Pager object and put a pointer to it
43917 ** in *ppPager. The pager should eventually be freed by passing it
43918 ** to sqlite3PagerClose().
43919 **
43920 ** The zFilename argument is the path to the database file to open.
43921 ** If zFilename is NULL then a randomly-named temporary file is created
43922 ** and used as the file to be cached. Temporary files are be deleted
43923 ** automatically when they are closed. If zFilename is ":memory:" then
43924 ** all information is held in cache. It is never written to disk.
43925 ** This can be used to implement an in-memory database.
43926 **
43927 ** The nExtra parameter specifies the number of bytes of space allocated
43928 ** along with each page reference. This space is available to the user
43929 ** via the sqlite3PagerGetExtra() API.
43930 **
43931 ** The flags argument is used to specify properties that affect the
43932 ** operation of the pager. It should be passed some bitwise combination
43933 ** of the PAGER_* flags.
43934 **
43935 ** The vfsFlags parameter is a bitmask to pass to the flags parameter
43936 ** of the xOpen() method of the supplied VFS when opening files.
43937 **
43938 ** If the pager object is allocated and the specified file opened
43939 ** successfully, SQLITE_OK is returned and *ppPager set to point to
43940 ** the new pager object. If an error occurs, *ppPager is set to NULL
43941 ** and error code returned. This function may return SQLITE_NOMEM
43942 ** (sqlite3Malloc() is used to allocate memory), SQLITE_CANTOPEN or
43943 ** various SQLITE_IO_XXX errors.
43944 */
43945 SQLITE_PRIVATE int sqlite3PagerOpen(
43946   sqlite3_vfs *pVfs,       /* The virtual file system to use */
43947   Pager **ppPager,         /* OUT: Return the Pager structure here */
43948   const char *zFilename,   /* Name of the database file to open */
43949   int nExtra,              /* Extra bytes append to each in-memory page */
43950   int flags,               /* flags controlling this file */
43951   int vfsFlags,            /* flags passed through to sqlite3_vfs.xOpen() */
43952   void (*xReinit)(DbPage*) /* Function to reinitialize pages */
43953 ){
43954   u8 *pPtr;
43955   Pager *pPager = 0;       /* Pager object to allocate and return */
43956   int rc = SQLITE_OK;      /* Return code */
43957   int tempFile = 0;        /* True for temp files (incl. in-memory files) */
43958   int memDb = 0;           /* True if this is an in-memory file */
43959   int readOnly = 0;        /* True if this is a read-only file */
43960   int journalFileSize;     /* Bytes to allocate for each journal fd */
43961   char *zPathname = 0;     /* Full path to database file */
43962   int nPathname = 0;       /* Number of bytes in zPathname */
43963   int useJournal = (flags & PAGER_OMIT_JOURNAL)==0; /* False to omit journal */
43964   int pcacheSize = sqlite3PcacheSize();       /* Bytes to allocate for PCache */
43965   u32 szPageDflt = SQLITE_DEFAULT_PAGE_SIZE;  /* Default page size */
43966   const char *zUri = 0;    /* URI args to copy */
43967   int nUri = 0;            /* Number of bytes of URI args at *zUri */
43968 
43969   /* Figure out how much space is required for each journal file-handle
43970   ** (there are two of them, the main journal and the sub-journal). This
43971   ** is the maximum space required for an in-memory journal file handle
43972   ** and a regular journal file-handle. Note that a "regular journal-handle"
43973   ** may be a wrapper capable of caching the first portion of the journal
43974   ** file in memory to implement the atomic-write optimization (see
43975   ** source file journal.c).
43976   */
43977   if( sqlite3JournalSize(pVfs)>sqlite3MemJournalSize() ){
43978     journalFileSize = ROUND8(sqlite3JournalSize(pVfs));
43979   }else{
43980     journalFileSize = ROUND8(sqlite3MemJournalSize());
43981   }
43982 
43983   /* Set the output variable to NULL in case an error occurs. */
43984   *ppPager = 0;
43985 
43986 #ifndef SQLITE_OMIT_MEMORYDB
43987   if( flags & PAGER_MEMORY ){
43988     memDb = 1;
43989     if( zFilename && zFilename[0] ){
43990       zPathname = sqlite3DbStrDup(0, zFilename);
43991       if( zPathname==0  ) return SQLITE_NOMEM;
43992       nPathname = sqlite3Strlen30(zPathname);
43993       zFilename = 0;
43994     }
43995   }
43996 #endif
43997 
43998   /* Compute and store the full pathname in an allocated buffer pointed
43999   ** to by zPathname, length nPathname. Or, if this is a temporary file,
44000   ** leave both nPathname and zPathname set to 0.
44001   */
44002   if( zFilename && zFilename[0] ){
44003     const char *z;
44004     nPathname = pVfs->mxPathname+1;
44005     zPathname = sqlite3DbMallocRaw(0, nPathname*2);
44006     if( zPathname==0 ){
44007       return SQLITE_NOMEM;
44008     }
44009     zPathname[0] = 0; /* Make sure initialized even if FullPathname() fails */
44010     rc = sqlite3OsFullPathname(pVfs, zFilename, nPathname, zPathname);
44011     nPathname = sqlite3Strlen30(zPathname);
44012     z = zUri = &zFilename[sqlite3Strlen30(zFilename)+1];
44013     while( *z ){
44014       z += sqlite3Strlen30(z)+1;
44015       z += sqlite3Strlen30(z)+1;
44016     }
44017     nUri = (int)(&z[1] - zUri);
44018     assert( nUri>=0 );
44019     if( rc==SQLITE_OK && nPathname+8>pVfs->mxPathname ){
44020       /* This branch is taken when the journal path required by
44021       ** the database being opened will be more than pVfs->mxPathname
44022       ** bytes in length. This means the database cannot be opened,
44023       ** as it will not be possible to open the journal file or even
44024       ** check for a hot-journal before reading.
44025       */
44026       rc = SQLITE_CANTOPEN_BKPT;
44027     }
44028     if( rc!=SQLITE_OK ){
44029       sqlite3DbFree(0, zPathname);
44030       return rc;
44031     }
44032   }
44033 
44034   /* Allocate memory for the Pager structure, PCache object, the
44035   ** three file descriptors, the database file name and the journal
44036   ** file name. The layout in memory is as follows:
44037   **
44038   **     Pager object                    (sizeof(Pager) bytes)
44039   **     PCache object                   (sqlite3PcacheSize() bytes)
44040   **     Database file handle            (pVfs->szOsFile bytes)
44041   **     Sub-journal file handle         (journalFileSize bytes)
44042   **     Main journal file handle        (journalFileSize bytes)
44043   **     Database file name              (nPathname+1 bytes)
44044   **     Journal file name               (nPathname+8+1 bytes)
44045   */
44046   pPtr = (u8 *)sqlite3MallocZero(
44047     ROUND8(sizeof(*pPager)) +      /* Pager structure */
44048     ROUND8(pcacheSize) +           /* PCache object */
44049     ROUND8(pVfs->szOsFile) +       /* The main db file */
44050     journalFileSize * 2 +          /* The two journal files */
44051     nPathname + 1 + nUri +         /* zFilename */
44052     nPathname + 8 + 2              /* zJournal */
44053 #ifndef SQLITE_OMIT_WAL
44054     + nPathname + 4 + 2            /* zWal */
44055 #endif
44056   );
44057   assert( EIGHT_BYTE_ALIGNMENT(SQLITE_INT_TO_PTR(journalFileSize)) );
44058   if( !pPtr ){
44059     sqlite3DbFree(0, zPathname);
44060     return SQLITE_NOMEM;
44061   }
44062   pPager =              (Pager*)(pPtr);
44063   pPager->pPCache =    (PCache*)(pPtr += ROUND8(sizeof(*pPager)));
44064   pPager->fd =   (sqlite3_file*)(pPtr += ROUND8(pcacheSize));
44065   pPager->sjfd = (sqlite3_file*)(pPtr += ROUND8(pVfs->szOsFile));
44066   pPager->jfd =  (sqlite3_file*)(pPtr += journalFileSize);
44067   pPager->zFilename =    (char*)(pPtr += journalFileSize);
44068   assert( EIGHT_BYTE_ALIGNMENT(pPager->jfd) );
44069 
44070   /* Fill in the Pager.zFilename and Pager.zJournal buffers, if required. */
44071   if( zPathname ){
44072     assert( nPathname>0 );
44073     pPager->zJournal =   (char*)(pPtr += nPathname + 1 + nUri);
44074     memcpy(pPager->zFilename, zPathname, nPathname);
44075     if( nUri ) memcpy(&pPager->zFilename[nPathname+1], zUri, nUri);
44076     memcpy(pPager->zJournal, zPathname, nPathname);
44077     memcpy(&pPager->zJournal[nPathname], "-journal\000", 8+2);
44078     sqlite3FileSuffix3(pPager->zFilename, pPager->zJournal);
44079 #ifndef SQLITE_OMIT_WAL
44080     pPager->zWal = &pPager->zJournal[nPathname+8+1];
44081     memcpy(pPager->zWal, zPathname, nPathname);
44082     memcpy(&pPager->zWal[nPathname], "-wal\000", 4+1);
44083     sqlite3FileSuffix3(pPager->zFilename, pPager->zWal);
44084 #endif
44085     sqlite3DbFree(0, zPathname);
44086   }
44087   pPager->pVfs = pVfs;
44088   pPager->vfsFlags = vfsFlags;
44089 
44090   /* Open the pager file.
44091   */
44092   if( zFilename && zFilename[0] ){
44093     int fout = 0;                    /* VFS flags returned by xOpen() */
44094     rc = sqlite3OsOpen(pVfs, pPager->zFilename, pPager->fd, vfsFlags, &fout);
44095     assert( !memDb );
44096     readOnly = (fout&SQLITE_OPEN_READONLY);
44097 
44098     /* If the file was successfully opened for read/write access,
44099     ** choose a default page size in case we have to create the
44100     ** database file. The default page size is the maximum of:
44101     **
44102     **    + SQLITE_DEFAULT_PAGE_SIZE,
44103     **    + The value returned by sqlite3OsSectorSize()
44104     **    + The largest page size that can be written atomically.
44105     */
44106     if( rc==SQLITE_OK && !readOnly ){
44107       setSectorSize(pPager);
44108       assert(SQLITE_DEFAULT_PAGE_SIZE<=SQLITE_MAX_DEFAULT_PAGE_SIZE);
44109       if( szPageDflt<pPager->sectorSize ){
44110         if( pPager->sectorSize>SQLITE_MAX_DEFAULT_PAGE_SIZE ){
44111           szPageDflt = SQLITE_MAX_DEFAULT_PAGE_SIZE;
44112         }else{
44113           szPageDflt = (u32)pPager->sectorSize;
44114         }
44115       }
44116 #ifdef SQLITE_ENABLE_ATOMIC_WRITE
44117       {
44118         int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
44119         int ii;
44120         assert(SQLITE_IOCAP_ATOMIC512==(512>>8));
44121         assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8));
44122         assert(SQLITE_MAX_DEFAULT_PAGE_SIZE<=65536);
44123         for(ii=szPageDflt; ii<=SQLITE_MAX_DEFAULT_PAGE_SIZE; ii=ii*2){
44124           if( iDc&(SQLITE_IOCAP_ATOMIC|(ii>>8)) ){
44125             szPageDflt = ii;
44126           }
44127         }
44128       }
44129 #endif
44130     }
44131   }else{
44132     /* If a temporary file is requested, it is not opened immediately.
44133     ** In this case we accept the default page size and delay actually
44134     ** opening the file until the first call to OsWrite().
44135     **
44136     ** This branch is also run for an in-memory database. An in-memory
44137     ** database is the same as a temp-file that is never written out to
44138     ** disk and uses an in-memory rollback journal.
44139     */
44140     tempFile = 1;
44141     pPager->eState = PAGER_READER;
44142     pPager->eLock = EXCLUSIVE_LOCK;
44143     readOnly = (vfsFlags&SQLITE_OPEN_READONLY);
44144   }
44145 
44146   /* The following call to PagerSetPagesize() serves to set the value of
44147   ** Pager.pageSize and to allocate the Pager.pTmpSpace buffer.
44148   */
44149   if( rc==SQLITE_OK ){
44150     assert( pPager->memDb==0 );
44151     rc = sqlite3PagerSetPagesize(pPager, &szPageDflt, -1);
44152     testcase( rc!=SQLITE_OK );
44153   }
44154 
44155   /* If an error occurred in either of the blocks above, free the
44156   ** Pager structure and close the file.
44157   */
44158   if( rc!=SQLITE_OK ){
44159     assert( !pPager->pTmpSpace );
44160     sqlite3OsClose(pPager->fd);
44161     sqlite3_free(pPager);
44162     return rc;
44163   }
44164 
44165   /* Initialize the PCache object. */
44166   assert( nExtra<1000 );
44167   nExtra = ROUND8(nExtra);
44168   sqlite3PcacheOpen(szPageDflt, nExtra, !memDb,
44169                     !memDb?pagerStress:0, (void *)pPager, pPager->pPCache);
44170 
44171   PAGERTRACE(("OPEN %d %s\n", FILEHANDLEID(pPager->fd), pPager->zFilename));
44172   IOTRACE(("OPEN %p %s\n", pPager, pPager->zFilename))
44173 
44174   pPager->useJournal = (u8)useJournal;
44175   /* pPager->stmtOpen = 0; */
44176   /* pPager->stmtInUse = 0; */
44177   /* pPager->nRef = 0; */
44178   /* pPager->stmtSize = 0; */
44179   /* pPager->stmtJSize = 0; */
44180   /* pPager->nPage = 0; */
44181   pPager->mxPgno = SQLITE_MAX_PAGE_COUNT;
44182   /* pPager->state = PAGER_UNLOCK; */
44183 #if 0
44184   assert( pPager->state == (tempFile ? PAGER_EXCLUSIVE : PAGER_UNLOCK) );
44185 #endif
44186   /* pPager->errMask = 0; */
44187   pPager->tempFile = (u8)tempFile;
44188   assert( tempFile==PAGER_LOCKINGMODE_NORMAL
44189           || tempFile==PAGER_LOCKINGMODE_EXCLUSIVE );
44190   assert( PAGER_LOCKINGMODE_EXCLUSIVE==1 );
44191   pPager->exclusiveMode = (u8)tempFile;
44192   pPager->changeCountDone = pPager->tempFile;
44193   pPager->memDb = (u8)memDb;
44194   pPager->readOnly = (u8)readOnly;
44195   assert( useJournal || pPager->tempFile );
44196   pPager->noSync = pPager->tempFile;
44197   if( pPager->noSync ){
44198     assert( pPager->fullSync==0 );
44199     assert( pPager->syncFlags==0 );
44200     assert( pPager->walSyncFlags==0 );
44201     assert( pPager->ckptSyncFlags==0 );
44202   }else{
44203     pPager->fullSync = 1;
44204     pPager->syncFlags = SQLITE_SYNC_NORMAL;
44205     pPager->walSyncFlags = SQLITE_SYNC_NORMAL | WAL_SYNC_TRANSACTIONS;
44206     pPager->ckptSyncFlags = SQLITE_SYNC_NORMAL;
44207   }
44208   /* pPager->pFirst = 0; */
44209   /* pPager->pFirstSynced = 0; */
44210   /* pPager->pLast = 0; */
44211   pPager->nExtra = (u16)nExtra;
44212   pPager->journalSizeLimit = SQLITE_DEFAULT_JOURNAL_SIZE_LIMIT;
44213   assert( isOpen(pPager->fd) || tempFile );
44214   setSectorSize(pPager);
44215   if( !useJournal ){
44216     pPager->journalMode = PAGER_JOURNALMODE_OFF;
44217   }else if( memDb ){
44218     pPager->journalMode = PAGER_JOURNALMODE_MEMORY;
44219   }
44220   /* pPager->xBusyHandler = 0; */
44221   /* pPager->pBusyHandlerArg = 0; */
44222   pPager->xReiniter = xReinit;
44223   /* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */
44224   /* pPager->szMmap = SQLITE_DEFAULT_MMAP_SIZE // will be set by btree.c */
44225 
44226   *ppPager = pPager;
44227   return SQLITE_OK;
44228 }
44229 
44230 
44231 /* Verify that the database file has not be deleted or renamed out from
44232 ** under the pager.  Return SQLITE_OK if the database is still were it ought
44233 ** to be on disk.  Return non-zero (SQLITE_READONLY_DBMOVED or some other error
44234 ** code from sqlite3OsAccess()) if the database has gone missing.
44235 */
44236 static int databaseIsUnmoved(Pager *pPager){
44237   int bHasMoved = 0;
44238   int rc;
44239 
44240   if( pPager->tempFile ) return SQLITE_OK;
44241   if( pPager->dbSize==0 ) return SQLITE_OK;
44242   assert( pPager->zFilename && pPager->zFilename[0] );
44243   rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_HAS_MOVED, &bHasMoved);
44244   if( rc==SQLITE_NOTFOUND ){
44245     /* If the HAS_MOVED file-control is unimplemented, assume that the file
44246     ** has not been moved.  That is the historical behavior of SQLite: prior to
44247     ** version 3.8.3, it never checked */
44248     rc = SQLITE_OK;
44249   }else if( rc==SQLITE_OK && bHasMoved ){
44250     rc = SQLITE_READONLY_DBMOVED;
44251   }
44252   return rc;
44253 }
44254 
44255 
44256 /*
44257 ** This function is called after transitioning from PAGER_UNLOCK to
44258 ** PAGER_SHARED state. It tests if there is a hot journal present in
44259 ** the file-system for the given pager. A hot journal is one that
44260 ** needs to be played back. According to this function, a hot-journal
44261 ** file exists if the following criteria are met:
44262 **
44263 **   * The journal file exists in the file system, and
44264 **   * No process holds a RESERVED or greater lock on the database file, and
44265 **   * The database file itself is greater than 0 bytes in size, and
44266 **   * The first byte of the journal file exists and is not 0x00.
44267 **
44268 ** If the current size of the database file is 0 but a journal file
44269 ** exists, that is probably an old journal left over from a prior
44270 ** database with the same name. In this case the journal file is
44271 ** just deleted using OsDelete, *pExists is set to 0 and SQLITE_OK
44272 ** is returned.
44273 **
44274 ** This routine does not check if there is a master journal filename
44275 ** at the end of the file. If there is, and that master journal file
44276 ** does not exist, then the journal file is not really hot. In this
44277 ** case this routine will return a false-positive. The pager_playback()
44278 ** routine will discover that the journal file is not really hot and
44279 ** will not roll it back.
44280 **
44281 ** If a hot-journal file is found to exist, *pExists is set to 1 and
44282 ** SQLITE_OK returned. If no hot-journal file is present, *pExists is
44283 ** set to 0 and SQLITE_OK returned. If an IO error occurs while trying
44284 ** to determine whether or not a hot-journal file exists, the IO error
44285 ** code is returned and the value of *pExists is undefined.
44286 */
44287 static int hasHotJournal(Pager *pPager, int *pExists){
44288   sqlite3_vfs * const pVfs = pPager->pVfs;
44289   int rc = SQLITE_OK;           /* Return code */
44290   int exists = 1;               /* True if a journal file is present */
44291   int jrnlOpen = !!isOpen(pPager->jfd);
44292 
44293   assert( pPager->useJournal );
44294   assert( isOpen(pPager->fd) );
44295   assert( pPager->eState==PAGER_OPEN );
44296 
44297   assert( jrnlOpen==0 || ( sqlite3OsDeviceCharacteristics(pPager->jfd) &
44298     SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN
44299   ));
44300 
44301   *pExists = 0;
44302   if( !jrnlOpen ){
44303     rc = sqlite3OsAccess(pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS, &exists);
44304   }
44305   if( rc==SQLITE_OK && exists ){
44306     int locked = 0;             /* True if some process holds a RESERVED lock */
44307 
44308     /* Race condition here:  Another process might have been holding the
44309     ** the RESERVED lock and have a journal open at the sqlite3OsAccess()
44310     ** call above, but then delete the journal and drop the lock before
44311     ** we get to the following sqlite3OsCheckReservedLock() call.  If that
44312     ** is the case, this routine might think there is a hot journal when
44313     ** in fact there is none.  This results in a false-positive which will
44314     ** be dealt with by the playback routine.  Ticket #3883.
44315     */
44316     rc = sqlite3OsCheckReservedLock(pPager->fd, &locked);
44317     if( rc==SQLITE_OK && !locked ){
44318       Pgno nPage;                 /* Number of pages in database file */
44319 
44320       /* Check the size of the database file. If it consists of 0 pages,
44321       ** then delete the journal file. See the header comment above for
44322       ** the reasoning here.  Delete the obsolete journal file under
44323       ** a RESERVED lock to avoid race conditions and to avoid violating
44324       ** [H33020].
44325       */
44326       rc = pagerPagecount(pPager, &nPage);
44327       if( rc==SQLITE_OK ){
44328         if( nPage==0 ){
44329           sqlite3BeginBenignMalloc();
44330           if( pagerLockDb(pPager, RESERVED_LOCK)==SQLITE_OK ){
44331             sqlite3OsDelete(pVfs, pPager->zJournal, 0);
44332             if( !pPager->exclusiveMode ) pagerUnlockDb(pPager, SHARED_LOCK);
44333           }
44334           sqlite3EndBenignMalloc();
44335         }else{
44336           /* The journal file exists and no other connection has a reserved
44337           ** or greater lock on the database file. Now check that there is
44338           ** at least one non-zero bytes at the start of the journal file.
44339           ** If there is, then we consider this journal to be hot. If not,
44340           ** it can be ignored.
44341           */
44342           if( !jrnlOpen ){
44343             int f = SQLITE_OPEN_READONLY|SQLITE_OPEN_MAIN_JOURNAL;
44344             rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &f);
44345           }
44346           if( rc==SQLITE_OK ){
44347             u8 first = 0;
44348             rc = sqlite3OsRead(pPager->jfd, (void *)&first, 1, 0);
44349             if( rc==SQLITE_IOERR_SHORT_READ ){
44350               rc = SQLITE_OK;
44351             }
44352             if( !jrnlOpen ){
44353               sqlite3OsClose(pPager->jfd);
44354             }
44355             *pExists = (first!=0);
44356           }else if( rc==SQLITE_CANTOPEN ){
44357             /* If we cannot open the rollback journal file in order to see if
44358             ** its has a zero header, that might be due to an I/O error, or
44359             ** it might be due to the race condition described above and in
44360             ** ticket #3883.  Either way, assume that the journal is hot.
44361             ** This might be a false positive.  But if it is, then the
44362             ** automatic journal playback and recovery mechanism will deal
44363             ** with it under an EXCLUSIVE lock where we do not need to
44364             ** worry so much with race conditions.
44365             */
44366             *pExists = 1;
44367             rc = SQLITE_OK;
44368           }
44369         }
44370       }
44371     }
44372   }
44373 
44374   return rc;
44375 }
44376 
44377 /*
44378 ** This function is called to obtain a shared lock on the database file.
44379 ** It is illegal to call sqlite3PagerAcquire() until after this function
44380 ** has been successfully called. If a shared-lock is already held when
44381 ** this function is called, it is a no-op.
44382 **
44383 ** The following operations are also performed by this function.
44384 **
44385 **   1) If the pager is currently in PAGER_OPEN state (no lock held
44386 **      on the database file), then an attempt is made to obtain a
44387 **      SHARED lock on the database file. Immediately after obtaining
44388 **      the SHARED lock, the file-system is checked for a hot-journal,
44389 **      which is played back if present. Following any hot-journal
44390 **      rollback, the contents of the cache are validated by checking
44391 **      the 'change-counter' field of the database file header and
44392 **      discarded if they are found to be invalid.
44393 **
44394 **   2) If the pager is running in exclusive-mode, and there are currently
44395 **      no outstanding references to any pages, and is in the error state,
44396 **      then an attempt is made to clear the error state by discarding
44397 **      the contents of the page cache and rolling back any open journal
44398 **      file.
44399 **
44400 ** If everything is successful, SQLITE_OK is returned. If an IO error
44401 ** occurs while locking the database, checking for a hot-journal file or
44402 ** rolling back a journal file, the IO error code is returned.
44403 */
44404 SQLITE_PRIVATE int sqlite3PagerSharedLock(Pager *pPager){
44405   int rc = SQLITE_OK;                /* Return code */
44406 
44407   /* This routine is only called from b-tree and only when there are no
44408   ** outstanding pages. This implies that the pager state should either
44409   ** be OPEN or READER. READER is only possible if the pager is or was in
44410   ** exclusive access mode.
44411   */
44412   assert( sqlite3PcacheRefCount(pPager->pPCache)==0 );
44413   assert( assert_pager_state(pPager) );
44414   assert( pPager->eState==PAGER_OPEN || pPager->eState==PAGER_READER );
44415   if( NEVER(MEMDB && pPager->errCode) ){ return pPager->errCode; }
44416 
44417   if( !pagerUseWal(pPager) && pPager->eState==PAGER_OPEN ){
44418     int bHotJournal = 1;          /* True if there exists a hot journal-file */
44419 
44420     assert( !MEMDB );
44421 
44422     rc = pager_wait_on_lock(pPager, SHARED_LOCK);
44423     if( rc!=SQLITE_OK ){
44424       assert( pPager->eLock==NO_LOCK || pPager->eLock==UNKNOWN_LOCK );
44425       goto failed;
44426     }
44427 
44428     /* If a journal file exists, and there is no RESERVED lock on the
44429     ** database file, then it either needs to be played back or deleted.
44430     */
44431     if( pPager->eLock<=SHARED_LOCK ){
44432       rc = hasHotJournal(pPager, &bHotJournal);
44433     }
44434     if( rc!=SQLITE_OK ){
44435       goto failed;
44436     }
44437     if( bHotJournal ){
44438       if( pPager->readOnly ){
44439         rc = SQLITE_READONLY_ROLLBACK;
44440         goto failed;
44441       }
44442 
44443       /* Get an EXCLUSIVE lock on the database file. At this point it is
44444       ** important that a RESERVED lock is not obtained on the way to the
44445       ** EXCLUSIVE lock. If it were, another process might open the
44446       ** database file, detect the RESERVED lock, and conclude that the
44447       ** database is safe to read while this process is still rolling the
44448       ** hot-journal back.
44449       **
44450       ** Because the intermediate RESERVED lock is not requested, any
44451       ** other process attempting to access the database file will get to
44452       ** this point in the code and fail to obtain its own EXCLUSIVE lock
44453       ** on the database file.
44454       **
44455       ** Unless the pager is in locking_mode=exclusive mode, the lock is
44456       ** downgraded to SHARED_LOCK before this function returns.
44457       */
44458       rc = pagerLockDb(pPager, EXCLUSIVE_LOCK);
44459       if( rc!=SQLITE_OK ){
44460         goto failed;
44461       }
44462 
44463       /* If it is not already open and the file exists on disk, open the
44464       ** journal for read/write access. Write access is required because
44465       ** in exclusive-access mode the file descriptor will be kept open
44466       ** and possibly used for a transaction later on. Also, write-access
44467       ** is usually required to finalize the journal in journal_mode=persist
44468       ** mode (and also for journal_mode=truncate on some systems).
44469       **
44470       ** If the journal does not exist, it usually means that some
44471       ** other connection managed to get in and roll it back before
44472       ** this connection obtained the exclusive lock above. Or, it
44473       ** may mean that the pager was in the error-state when this
44474       ** function was called and the journal file does not exist.
44475       */
44476       if( !isOpen(pPager->jfd) ){
44477         sqlite3_vfs * const pVfs = pPager->pVfs;
44478         int bExists;              /* True if journal file exists */
44479         rc = sqlite3OsAccess(
44480             pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS, &bExists);
44481         if( rc==SQLITE_OK && bExists ){
44482           int fout = 0;
44483           int f = SQLITE_OPEN_READWRITE|SQLITE_OPEN_MAIN_JOURNAL;
44484           assert( !pPager->tempFile );
44485           rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &fout);
44486           assert( rc!=SQLITE_OK || isOpen(pPager->jfd) );
44487           if( rc==SQLITE_OK && fout&SQLITE_OPEN_READONLY ){
44488             rc = SQLITE_CANTOPEN_BKPT;
44489             sqlite3OsClose(pPager->jfd);
44490           }
44491         }
44492       }
44493 
44494       /* Playback and delete the journal.  Drop the database write
44495       ** lock and reacquire the read lock. Purge the cache before
44496       ** playing back the hot-journal so that we don't end up with
44497       ** an inconsistent cache.  Sync the hot journal before playing
44498       ** it back since the process that crashed and left the hot journal
44499       ** probably did not sync it and we are required to always sync
44500       ** the journal before playing it back.
44501       */
44502       if( isOpen(pPager->jfd) ){
44503         assert( rc==SQLITE_OK );
44504         rc = pagerSyncHotJournal(pPager);
44505         if( rc==SQLITE_OK ){
44506           rc = pager_playback(pPager, 1);
44507           pPager->eState = PAGER_OPEN;
44508         }
44509       }else if( !pPager->exclusiveMode ){
44510         pagerUnlockDb(pPager, SHARED_LOCK);
44511       }
44512 
44513       if( rc!=SQLITE_OK ){
44514         /* This branch is taken if an error occurs while trying to open
44515         ** or roll back a hot-journal while holding an EXCLUSIVE lock. The
44516         ** pager_unlock() routine will be called before returning to unlock
44517         ** the file. If the unlock attempt fails, then Pager.eLock must be
44518         ** set to UNKNOWN_LOCK (see the comment above the #define for
44519         ** UNKNOWN_LOCK above for an explanation).
44520         **
44521         ** In order to get pager_unlock() to do this, set Pager.eState to
44522         ** PAGER_ERROR now. This is not actually counted as a transition
44523         ** to ERROR state in the state diagram at the top of this file,
44524         ** since we know that the same call to pager_unlock() will very
44525         ** shortly transition the pager object to the OPEN state. Calling
44526         ** assert_pager_state() would fail now, as it should not be possible
44527         ** to be in ERROR state when there are zero outstanding page
44528         ** references.
44529         */
44530         pager_error(pPager, rc);
44531         goto failed;
44532       }
44533 
44534       assert( pPager->eState==PAGER_OPEN );
44535       assert( (pPager->eLock==SHARED_LOCK)
44536            || (pPager->exclusiveMode && pPager->eLock>SHARED_LOCK)
44537       );
44538     }
44539 
44540     if( !pPager->tempFile && (
44541         pPager->pBackup
44542      || sqlite3PcachePagecount(pPager->pPCache)>0
44543      || USEFETCH(pPager)
44544     )){
44545       /* The shared-lock has just been acquired on the database file
44546       ** and there are already pages in the cache (from a previous
44547       ** read or write transaction).  Check to see if the database
44548       ** has been modified.  If the database has changed, flush the
44549       ** cache.
44550       **
44551       ** Database changes is detected by looking at 15 bytes beginning
44552       ** at offset 24 into the file.  The first 4 of these 16 bytes are
44553       ** a 32-bit counter that is incremented with each change.  The
44554       ** other bytes change randomly with each file change when
44555       ** a codec is in use.
44556       **
44557       ** There is a vanishingly small chance that a change will not be
44558       ** detected.  The chance of an undetected change is so small that
44559       ** it can be neglected.
44560       */
44561       Pgno nPage = 0;
44562       char dbFileVers[sizeof(pPager->dbFileVers)];
44563 
44564       rc = pagerPagecount(pPager, &nPage);
44565       if( rc ) goto failed;
44566 
44567       if( nPage>0 ){
44568         IOTRACE(("CKVERS %p %d\n", pPager, sizeof(dbFileVers)));
44569         rc = sqlite3OsRead(pPager->fd, &dbFileVers, sizeof(dbFileVers), 24);
44570         if( rc!=SQLITE_OK && rc!=SQLITE_IOERR_SHORT_READ ){
44571           goto failed;
44572         }
44573       }else{
44574         memset(dbFileVers, 0, sizeof(dbFileVers));
44575       }
44576 
44577       if( memcmp(pPager->dbFileVers, dbFileVers, sizeof(dbFileVers))!=0 ){
44578         pager_reset(pPager);
44579 
44580         /* Unmap the database file. It is possible that external processes
44581         ** may have truncated the database file and then extended it back
44582         ** to its original size while this process was not holding a lock.
44583         ** In this case there may exist a Pager.pMap mapping that appears
44584         ** to be the right size but is not actually valid. Avoid this
44585         ** possibility by unmapping the db here. */
44586         if( USEFETCH(pPager) ){
44587           sqlite3OsUnfetch(pPager->fd, 0, 0);
44588         }
44589       }
44590     }
44591 
44592     /* If there is a WAL file in the file-system, open this database in WAL
44593     ** mode. Otherwise, the following function call is a no-op.
44594     */
44595     rc = pagerOpenWalIfPresent(pPager);
44596 #ifndef SQLITE_OMIT_WAL
44597     assert( pPager->pWal==0 || rc==SQLITE_OK );
44598 #endif
44599   }
44600 
44601   if( pagerUseWal(pPager) ){
44602     assert( rc==SQLITE_OK );
44603     rc = pagerBeginReadTransaction(pPager);
44604   }
44605 
44606   if( pPager->eState==PAGER_OPEN && rc==SQLITE_OK ){
44607     rc = pagerPagecount(pPager, &pPager->dbSize);
44608   }
44609 
44610  failed:
44611   if( rc!=SQLITE_OK ){
44612     assert( !MEMDB );
44613     pager_unlock(pPager);
44614     assert( pPager->eState==PAGER_OPEN );
44615   }else{
44616     pPager->eState = PAGER_READER;
44617   }
44618   return rc;
44619 }
44620 
44621 /*
44622 ** If the reference count has reached zero, rollback any active
44623 ** transaction and unlock the pager.
44624 **
44625 ** Except, in locking_mode=EXCLUSIVE when there is nothing to in
44626 ** the rollback journal, the unlock is not performed and there is
44627 ** nothing to rollback, so this routine is a no-op.
44628 */
44629 static void pagerUnlockIfUnused(Pager *pPager){
44630   if( pPager->nMmapOut==0 && (sqlite3PcacheRefCount(pPager->pPCache)==0) ){
44631     pagerUnlockAndRollback(pPager);
44632   }
44633 }
44634 
44635 /*
44636 ** Acquire a reference to page number pgno in pager pPager (a page
44637 ** reference has type DbPage*). If the requested reference is
44638 ** successfully obtained, it is copied to *ppPage and SQLITE_OK returned.
44639 **
44640 ** If the requested page is already in the cache, it is returned.
44641 ** Otherwise, a new page object is allocated and populated with data
44642 ** read from the database file. In some cases, the pcache module may
44643 ** choose not to allocate a new page object and may reuse an existing
44644 ** object with no outstanding references.
44645 **
44646 ** The extra data appended to a page is always initialized to zeros the
44647 ** first time a page is loaded into memory. If the page requested is
44648 ** already in the cache when this function is called, then the extra
44649 ** data is left as it was when the page object was last used.
44650 **
44651 ** If the database image is smaller than the requested page or if a
44652 ** non-zero value is passed as the noContent parameter and the
44653 ** requested page is not already stored in the cache, then no
44654 ** actual disk read occurs. In this case the memory image of the
44655 ** page is initialized to all zeros.
44656 **
44657 ** If noContent is true, it means that we do not care about the contents
44658 ** of the page. This occurs in two scenarios:
44659 **
44660 **   a) When reading a free-list leaf page from the database, and
44661 **
44662 **   b) When a savepoint is being rolled back and we need to load
44663 **      a new page into the cache to be filled with the data read
44664 **      from the savepoint journal.
44665 **
44666 ** If noContent is true, then the data returned is zeroed instead of
44667 ** being read from the database. Additionally, the bits corresponding
44668 ** to pgno in Pager.pInJournal (bitvec of pages already written to the
44669 ** journal file) and the PagerSavepoint.pInSavepoint bitvecs of any open
44670 ** savepoints are set. This means if the page is made writable at any
44671 ** point in the future, using a call to sqlite3PagerWrite(), its contents
44672 ** will not be journaled. This saves IO.
44673 **
44674 ** The acquisition might fail for several reasons.  In all cases,
44675 ** an appropriate error code is returned and *ppPage is set to NULL.
44676 **
44677 ** See also sqlite3PagerLookup().  Both this routine and Lookup() attempt
44678 ** to find a page in the in-memory cache first.  If the page is not already
44679 ** in memory, this routine goes to disk to read it in whereas Lookup()
44680 ** just returns 0.  This routine acquires a read-lock the first time it
44681 ** has to go to disk, and could also playback an old journal if necessary.
44682 ** Since Lookup() never goes to disk, it never has to deal with locks
44683 ** or journal files.
44684 */
44685 SQLITE_PRIVATE int sqlite3PagerAcquire(
44686   Pager *pPager,      /* The pager open on the database file */
44687   Pgno pgno,          /* Page number to fetch */
44688   DbPage **ppPage,    /* Write a pointer to the page here */
44689   int flags           /* PAGER_GET_XXX flags */
44690 ){
44691   int rc = SQLITE_OK;
44692   PgHdr *pPg = 0;
44693   u32 iFrame = 0;                 /* Frame to read from WAL file */
44694   const int noContent = (flags & PAGER_GET_NOCONTENT);
44695 
44696   /* It is acceptable to use a read-only (mmap) page for any page except
44697   ** page 1 if there is no write-transaction open or the ACQUIRE_READONLY
44698   ** flag was specified by the caller. And so long as the db is not a
44699   ** temporary or in-memory database.  */
44700   const int bMmapOk = (pgno!=1 && USEFETCH(pPager)
44701    && (pPager->eState==PAGER_READER || (flags & PAGER_GET_READONLY))
44702 #ifdef SQLITE_HAS_CODEC
44703    && pPager->xCodec==0
44704 #endif
44705   );
44706 
44707   assert( pPager->eState>=PAGER_READER );
44708   assert( assert_pager_state(pPager) );
44709   assert( noContent==0 || bMmapOk==0 );
44710 
44711   if( pgno==0 ){
44712     return SQLITE_CORRUPT_BKPT;
44713   }
44714 
44715   /* If the pager is in the error state, return an error immediately.
44716   ** Otherwise, request the page from the PCache layer. */
44717   if( pPager->errCode!=SQLITE_OK ){
44718     rc = pPager->errCode;
44719   }else{
44720 
44721     if( bMmapOk && pagerUseWal(pPager) ){
44722       rc = sqlite3WalFindFrame(pPager->pWal, pgno, &iFrame);
44723       if( rc!=SQLITE_OK ) goto pager_acquire_err;
44724     }
44725 
44726     if( bMmapOk && iFrame==0 ){
44727       void *pData = 0;
44728 
44729       rc = sqlite3OsFetch(pPager->fd,
44730           (i64)(pgno-1) * pPager->pageSize, pPager->pageSize, &pData
44731       );
44732 
44733       if( rc==SQLITE_OK && pData ){
44734         if( pPager->eState>PAGER_READER ){
44735           (void)sqlite3PcacheFetch(pPager->pPCache, pgno, 0, &pPg);
44736         }
44737         if( pPg==0 ){
44738           rc = pagerAcquireMapPage(pPager, pgno, pData, &pPg);
44739         }else{
44740           sqlite3OsUnfetch(pPager->fd, (i64)(pgno-1)*pPager->pageSize, pData);
44741         }
44742         if( pPg ){
44743           assert( rc==SQLITE_OK );
44744           *ppPage = pPg;
44745           return SQLITE_OK;
44746         }
44747       }
44748       if( rc!=SQLITE_OK ){
44749         goto pager_acquire_err;
44750       }
44751     }
44752 
44753     rc = sqlite3PcacheFetch(pPager->pPCache, pgno, 1, ppPage);
44754   }
44755 
44756   if( rc!=SQLITE_OK ){
44757     /* Either the call to sqlite3PcacheFetch() returned an error or the
44758     ** pager was already in the error-state when this function was called.
44759     ** Set pPg to 0 and jump to the exception handler.  */
44760     pPg = 0;
44761     goto pager_acquire_err;
44762   }
44763   assert( (*ppPage)->pgno==pgno );
44764   assert( (*ppPage)->pPager==pPager || (*ppPage)->pPager==0 );
44765 
44766   if( (*ppPage)->pPager && !noContent ){
44767     /* In this case the pcache already contains an initialized copy of
44768     ** the page. Return without further ado.  */
44769     assert( pgno<=PAGER_MAX_PGNO && pgno!=PAGER_MJ_PGNO(pPager) );
44770     pPager->aStat[PAGER_STAT_HIT]++;
44771     return SQLITE_OK;
44772 
44773   }else{
44774     /* The pager cache has created a new page. Its content needs to
44775     ** be initialized.  */
44776 
44777     pPg = *ppPage;
44778     pPg->pPager = pPager;
44779 
44780     /* The maximum page number is 2^31. Return SQLITE_CORRUPT if a page
44781     ** number greater than this, or the unused locking-page, is requested. */
44782     if( pgno>PAGER_MAX_PGNO || pgno==PAGER_MJ_PGNO(pPager) ){
44783       rc = SQLITE_CORRUPT_BKPT;
44784       goto pager_acquire_err;
44785     }
44786 
44787     if( MEMDB || pPager->dbSize<pgno || noContent || !isOpen(pPager->fd) ){
44788       if( pgno>pPager->mxPgno ){
44789         rc = SQLITE_FULL;
44790         goto pager_acquire_err;
44791       }
44792       if( noContent ){
44793         /* Failure to set the bits in the InJournal bit-vectors is benign.
44794         ** It merely means that we might do some extra work to journal a
44795         ** page that does not need to be journaled.  Nevertheless, be sure
44796         ** to test the case where a malloc error occurs while trying to set
44797         ** a bit in a bit vector.
44798         */
44799         sqlite3BeginBenignMalloc();
44800         if( pgno<=pPager->dbOrigSize ){
44801           TESTONLY( rc = ) sqlite3BitvecSet(pPager->pInJournal, pgno);
44802           testcase( rc==SQLITE_NOMEM );
44803         }
44804         TESTONLY( rc = ) addToSavepointBitvecs(pPager, pgno);
44805         testcase( rc==SQLITE_NOMEM );
44806         sqlite3EndBenignMalloc();
44807       }
44808       memset(pPg->pData, 0, pPager->pageSize);
44809       IOTRACE(("ZERO %p %d\n", pPager, pgno));
44810     }else{
44811       if( pagerUseWal(pPager) && bMmapOk==0 ){
44812         rc = sqlite3WalFindFrame(pPager->pWal, pgno, &iFrame);
44813         if( rc!=SQLITE_OK ) goto pager_acquire_err;
44814       }
44815       assert( pPg->pPager==pPager );
44816       pPager->aStat[PAGER_STAT_MISS]++;
44817       rc = readDbPage(pPg, iFrame);
44818       if( rc!=SQLITE_OK ){
44819         goto pager_acquire_err;
44820       }
44821     }
44822     pager_set_pagehash(pPg);
44823   }
44824 
44825   return SQLITE_OK;
44826 
44827 pager_acquire_err:
44828   assert( rc!=SQLITE_OK );
44829   if( pPg ){
44830     sqlite3PcacheDrop(pPg);
44831   }
44832   pagerUnlockIfUnused(pPager);
44833 
44834   *ppPage = 0;
44835   return rc;
44836 }
44837 
44838 /*
44839 ** Acquire a page if it is already in the in-memory cache.  Do
44840 ** not read the page from disk.  Return a pointer to the page,
44841 ** or 0 if the page is not in cache.
44842 **
44843 ** See also sqlite3PagerGet().  The difference between this routine
44844 ** and sqlite3PagerGet() is that _get() will go to the disk and read
44845 ** in the page if the page is not already in cache.  This routine
44846 ** returns NULL if the page is not in cache or if a disk I/O error
44847 ** has ever happened.
44848 */
44849 SQLITE_PRIVATE DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno){
44850   PgHdr *pPg = 0;
44851   assert( pPager!=0 );
44852   assert( pgno!=0 );
44853   assert( pPager->pPCache!=0 );
44854   assert( pPager->eState>=PAGER_READER && pPager->eState!=PAGER_ERROR );
44855   sqlite3PcacheFetch(pPager->pPCache, pgno, 0, &pPg);
44856   return pPg;
44857 }
44858 
44859 /*
44860 ** Release a page reference.
44861 **
44862 ** If the number of references to the page drop to zero, then the
44863 ** page is added to the LRU list.  When all references to all pages
44864 ** are released, a rollback occurs and the lock on the database is
44865 ** removed.
44866 */
44867 SQLITE_PRIVATE void sqlite3PagerUnrefNotNull(DbPage *pPg){
44868   Pager *pPager;
44869   assert( pPg!=0 );
44870   pPager = pPg->pPager;
44871   if( pPg->flags & PGHDR_MMAP ){
44872     pagerReleaseMapPage(pPg);
44873   }else{
44874     sqlite3PcacheRelease(pPg);
44875   }
44876   pagerUnlockIfUnused(pPager);
44877 }
44878 SQLITE_PRIVATE void sqlite3PagerUnref(DbPage *pPg){
44879   if( pPg ) sqlite3PagerUnrefNotNull(pPg);
44880 }
44881 
44882 /*
44883 ** This function is called at the start of every write transaction.
44884 ** There must already be a RESERVED or EXCLUSIVE lock on the database
44885 ** file when this routine is called.
44886 **
44887 ** Open the journal file for pager pPager and write a journal header
44888 ** to the start of it. If there are active savepoints, open the sub-journal
44889 ** as well. This function is only used when the journal file is being
44890 ** opened to write a rollback log for a transaction. It is not used
44891 ** when opening a hot journal file to roll it back.
44892 **
44893 ** If the journal file is already open (as it may be in exclusive mode),
44894 ** then this function just writes a journal header to the start of the
44895 ** already open file.
44896 **
44897 ** Whether or not the journal file is opened by this function, the
44898 ** Pager.pInJournal bitvec structure is allocated.
44899 **
44900 ** Return SQLITE_OK if everything is successful. Otherwise, return
44901 ** SQLITE_NOMEM if the attempt to allocate Pager.pInJournal fails, or
44902 ** an IO error code if opening or writing the journal file fails.
44903 */
44904 static int pager_open_journal(Pager *pPager){
44905   int rc = SQLITE_OK;                        /* Return code */
44906   sqlite3_vfs * const pVfs = pPager->pVfs;   /* Local cache of vfs pointer */
44907 
44908   assert( pPager->eState==PAGER_WRITER_LOCKED );
44909   assert( assert_pager_state(pPager) );
44910   assert( pPager->pInJournal==0 );
44911 
44912   /* If already in the error state, this function is a no-op.  But on
44913   ** the other hand, this routine is never called if we are already in
44914   ** an error state. */
44915   if( NEVER(pPager->errCode) ) return pPager->errCode;
44916 
44917   if( !pagerUseWal(pPager) && pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
44918     pPager->pInJournal = sqlite3BitvecCreate(pPager->dbSize);
44919     if( pPager->pInJournal==0 ){
44920       return SQLITE_NOMEM;
44921     }
44922 
44923     /* Open the journal file if it is not already open. */
44924     if( !isOpen(pPager->jfd) ){
44925       if( pPager->journalMode==PAGER_JOURNALMODE_MEMORY ){
44926         sqlite3MemJournalOpen(pPager->jfd);
44927       }else{
44928         const int flags =                   /* VFS flags to open journal file */
44929           SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|
44930           (pPager->tempFile ?
44931             (SQLITE_OPEN_DELETEONCLOSE|SQLITE_OPEN_TEMP_JOURNAL):
44932             (SQLITE_OPEN_MAIN_JOURNAL)
44933           );
44934 
44935         /* Verify that the database still has the same name as it did when
44936         ** it was originally opened. */
44937         rc = databaseIsUnmoved(pPager);
44938         if( rc==SQLITE_OK ){
44939 #ifdef SQLITE_ENABLE_ATOMIC_WRITE
44940           rc = sqlite3JournalOpen(
44941               pVfs, pPager->zJournal, pPager->jfd, flags, jrnlBufferSize(pPager)
44942           );
44943 #else
44944           rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, flags, 0);
44945 #endif
44946         }
44947       }
44948       assert( rc!=SQLITE_OK || isOpen(pPager->jfd) );
44949     }
44950 
44951 
44952     /* Write the first journal header to the journal file and open
44953     ** the sub-journal if necessary.
44954     */
44955     if( rc==SQLITE_OK ){
44956       /* TODO: Check if all of these are really required. */
44957       pPager->nRec = 0;
44958       pPager->journalOff = 0;
44959       pPager->setMaster = 0;
44960       pPager->journalHdr = 0;
44961       rc = writeJournalHdr(pPager);
44962     }
44963   }
44964 
44965   if( rc!=SQLITE_OK ){
44966     sqlite3BitvecDestroy(pPager->pInJournal);
44967     pPager->pInJournal = 0;
44968   }else{
44969     assert( pPager->eState==PAGER_WRITER_LOCKED );
44970     pPager->eState = PAGER_WRITER_CACHEMOD;
44971   }
44972 
44973   return rc;
44974 }
44975 
44976 /*
44977 ** Begin a write-transaction on the specified pager object. If a
44978 ** write-transaction has already been opened, this function is a no-op.
44979 **
44980 ** If the exFlag argument is false, then acquire at least a RESERVED
44981 ** lock on the database file. If exFlag is true, then acquire at least
44982 ** an EXCLUSIVE lock. If such a lock is already held, no locking
44983 ** functions need be called.
44984 **
44985 ** If the subjInMemory argument is non-zero, then any sub-journal opened
44986 ** within this transaction will be opened as an in-memory file. This
44987 ** has no effect if the sub-journal is already opened (as it may be when
44988 ** running in exclusive mode) or if the transaction does not require a
44989 ** sub-journal. If the subjInMemory argument is zero, then any required
44990 ** sub-journal is implemented in-memory if pPager is an in-memory database,
44991 ** or using a temporary file otherwise.
44992 */
44993 SQLITE_PRIVATE int sqlite3PagerBegin(Pager *pPager, int exFlag, int subjInMemory){
44994   int rc = SQLITE_OK;
44995 
44996   if( pPager->errCode ) return pPager->errCode;
44997   assert( pPager->eState>=PAGER_READER && pPager->eState<PAGER_ERROR );
44998   pPager->subjInMemory = (u8)subjInMemory;
44999 
45000   if( ALWAYS(pPager->eState==PAGER_READER) ){
45001     assert( pPager->pInJournal==0 );
45002 
45003     if( pagerUseWal(pPager) ){
45004       /* If the pager is configured to use locking_mode=exclusive, and an
45005       ** exclusive lock on the database is not already held, obtain it now.
45006       */
45007       if( pPager->exclusiveMode && sqlite3WalExclusiveMode(pPager->pWal, -1) ){
45008         rc = pagerLockDb(pPager, EXCLUSIVE_LOCK);
45009         if( rc!=SQLITE_OK ){
45010           return rc;
45011         }
45012         sqlite3WalExclusiveMode(pPager->pWal, 1);
45013       }
45014 
45015       /* Grab the write lock on the log file. If successful, upgrade to
45016       ** PAGER_RESERVED state. Otherwise, return an error code to the caller.
45017       ** The busy-handler is not invoked if another connection already
45018       ** holds the write-lock. If possible, the upper layer will call it.
45019       */
45020       rc = sqlite3WalBeginWriteTransaction(pPager->pWal);
45021     }else{
45022       /* Obtain a RESERVED lock on the database file. If the exFlag parameter
45023       ** is true, then immediately upgrade this to an EXCLUSIVE lock. The
45024       ** busy-handler callback can be used when upgrading to the EXCLUSIVE
45025       ** lock, but not when obtaining the RESERVED lock.
45026       */
45027       rc = pagerLockDb(pPager, RESERVED_LOCK);
45028       if( rc==SQLITE_OK && exFlag ){
45029         rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
45030       }
45031     }
45032 
45033     if( rc==SQLITE_OK ){
45034       /* Change to WRITER_LOCKED state.
45035       **
45036       ** WAL mode sets Pager.eState to PAGER_WRITER_LOCKED or CACHEMOD
45037       ** when it has an open transaction, but never to DBMOD or FINISHED.
45038       ** This is because in those states the code to roll back savepoint
45039       ** transactions may copy data from the sub-journal into the database
45040       ** file as well as into the page cache. Which would be incorrect in
45041       ** WAL mode.
45042       */
45043       pPager->eState = PAGER_WRITER_LOCKED;
45044       pPager->dbHintSize = pPager->dbSize;
45045       pPager->dbFileSize = pPager->dbSize;
45046       pPager->dbOrigSize = pPager->dbSize;
45047       pPager->journalOff = 0;
45048     }
45049 
45050     assert( rc==SQLITE_OK || pPager->eState==PAGER_READER );
45051     assert( rc!=SQLITE_OK || pPager->eState==PAGER_WRITER_LOCKED );
45052     assert( assert_pager_state(pPager) );
45053   }
45054 
45055   PAGERTRACE(("TRANSACTION %d\n", PAGERID(pPager)));
45056   return rc;
45057 }
45058 
45059 /*
45060 ** Mark a single data page as writeable. The page is written into the
45061 ** main journal or sub-journal as required. If the page is written into
45062 ** one of the journals, the corresponding bit is set in the
45063 ** Pager.pInJournal bitvec and the PagerSavepoint.pInSavepoint bitvecs
45064 ** of any open savepoints as appropriate.
45065 */
45066 static int pager_write(PgHdr *pPg){
45067   Pager *pPager = pPg->pPager;
45068   int rc = SQLITE_OK;
45069   int inJournal;
45070 
45071   /* This routine is not called unless a write-transaction has already
45072   ** been started. The journal file may or may not be open at this point.
45073   ** It is never called in the ERROR state.
45074   */
45075   assert( pPager->eState==PAGER_WRITER_LOCKED
45076        || pPager->eState==PAGER_WRITER_CACHEMOD
45077        || pPager->eState==PAGER_WRITER_DBMOD
45078   );
45079   assert( assert_pager_state(pPager) );
45080   assert( pPager->errCode==0 );
45081   assert( pPager->readOnly==0 );
45082 
45083   CHECK_PAGE(pPg);
45084 
45085   /* The journal file needs to be opened. Higher level routines have already
45086   ** obtained the necessary locks to begin the write-transaction, but the
45087   ** rollback journal might not yet be open. Open it now if this is the case.
45088   **
45089   ** This is done before calling sqlite3PcacheMakeDirty() on the page.
45090   ** Otherwise, if it were done after calling sqlite3PcacheMakeDirty(), then
45091   ** an error might occur and the pager would end up in WRITER_LOCKED state
45092   ** with pages marked as dirty in the cache.
45093   */
45094   if( pPager->eState==PAGER_WRITER_LOCKED ){
45095     rc = pager_open_journal(pPager);
45096     if( rc!=SQLITE_OK ) return rc;
45097   }
45098   assert( pPager->eState>=PAGER_WRITER_CACHEMOD );
45099   assert( assert_pager_state(pPager) );
45100 
45101   /* Mark the page as dirty.  If the page has already been written
45102   ** to the journal then we can return right away.
45103   */
45104   sqlite3PcacheMakeDirty(pPg);
45105   inJournal = pageInJournal(pPager, pPg);
45106   if( inJournal && (pPager->nSavepoint==0 || !subjRequiresPage(pPg)) ){
45107     assert( !pagerUseWal(pPager) );
45108   }else{
45109 
45110     /* The transaction journal now exists and we have a RESERVED or an
45111     ** EXCLUSIVE lock on the main database file.  Write the current page to
45112     ** the transaction journal if it is not there already.
45113     */
45114     if( !inJournal && !pagerUseWal(pPager) ){
45115       assert( pagerUseWal(pPager)==0 );
45116       if( pPg->pgno<=pPager->dbOrigSize && isOpen(pPager->jfd) ){
45117         u32 cksum;
45118         char *pData2;
45119         i64 iOff = pPager->journalOff;
45120 
45121         /* We should never write to the journal file the page that
45122         ** contains the database locks.  The following assert verifies
45123         ** that we do not. */
45124         assert( pPg->pgno!=PAGER_MJ_PGNO(pPager) );
45125 
45126         assert( pPager->journalHdr<=pPager->journalOff );
45127         CODEC2(pPager, pPg->pData, pPg->pgno, 7, return SQLITE_NOMEM, pData2);
45128         cksum = pager_cksum(pPager, (u8*)pData2);
45129 
45130         /* Even if an IO or diskfull error occurs while journalling the
45131         ** page in the block above, set the need-sync flag for the page.
45132         ** Otherwise, when the transaction is rolled back, the logic in
45133         ** playback_one_page() will think that the page needs to be restored
45134         ** in the database file. And if an IO error occurs while doing so,
45135         ** then corruption may follow.
45136         */
45137         pPg->flags |= PGHDR_NEED_SYNC;
45138 
45139         rc = write32bits(pPager->jfd, iOff, pPg->pgno);
45140         if( rc!=SQLITE_OK ) return rc;
45141         rc = sqlite3OsWrite(pPager->jfd, pData2, pPager->pageSize, iOff+4);
45142         if( rc!=SQLITE_OK ) return rc;
45143         rc = write32bits(pPager->jfd, iOff+pPager->pageSize+4, cksum);
45144         if( rc!=SQLITE_OK ) return rc;
45145 
45146         IOTRACE(("JOUT %p %d %lld %d\n", pPager, pPg->pgno,
45147                  pPager->journalOff, pPager->pageSize));
45148         PAGER_INCR(sqlite3_pager_writej_count);
45149         PAGERTRACE(("JOURNAL %d page %d needSync=%d hash(%08x)\n",
45150              PAGERID(pPager), pPg->pgno,
45151              ((pPg->flags&PGHDR_NEED_SYNC)?1:0), pager_pagehash(pPg)));
45152 
45153         pPager->journalOff += 8 + pPager->pageSize;
45154         pPager->nRec++;
45155         assert( pPager->pInJournal!=0 );
45156         rc = sqlite3BitvecSet(pPager->pInJournal, pPg->pgno);
45157         testcase( rc==SQLITE_NOMEM );
45158         assert( rc==SQLITE_OK || rc==SQLITE_NOMEM );
45159         rc |= addToSavepointBitvecs(pPager, pPg->pgno);
45160         if( rc!=SQLITE_OK ){
45161           assert( rc==SQLITE_NOMEM );
45162           return rc;
45163         }
45164       }else{
45165         if( pPager->eState!=PAGER_WRITER_DBMOD ){
45166           pPg->flags |= PGHDR_NEED_SYNC;
45167         }
45168         PAGERTRACE(("APPEND %d page %d needSync=%d\n",
45169                 PAGERID(pPager), pPg->pgno,
45170                ((pPg->flags&PGHDR_NEED_SYNC)?1:0)));
45171       }
45172     }
45173 
45174     /* If the statement journal is open and the page is not in it,
45175     ** then write the current page to the statement journal.  Note that
45176     ** the statement journal format differs from the standard journal format
45177     ** in that it omits the checksums and the header.
45178     */
45179     if( pPager->nSavepoint>0 && subjRequiresPage(pPg) ){
45180       rc = subjournalPage(pPg);
45181     }
45182   }
45183 
45184   /* Update the database size and return.
45185   */
45186   if( pPager->dbSize<pPg->pgno ){
45187     pPager->dbSize = pPg->pgno;
45188   }
45189   return rc;
45190 }
45191 
45192 /*
45193 ** Mark a data page as writeable. This routine must be called before
45194 ** making changes to a page. The caller must check the return value
45195 ** of this function and be careful not to change any page data unless
45196 ** this routine returns SQLITE_OK.
45197 **
45198 ** The difference between this function and pager_write() is that this
45199 ** function also deals with the special case where 2 or more pages
45200 ** fit on a single disk sector. In this case all co-resident pages
45201 ** must have been written to the journal file before returning.
45202 **
45203 ** If an error occurs, SQLITE_NOMEM or an IO error code is returned
45204 ** as appropriate. Otherwise, SQLITE_OK.
45205 */
45206 SQLITE_PRIVATE int sqlite3PagerWrite(DbPage *pDbPage){
45207   int rc = SQLITE_OK;
45208 
45209   PgHdr *pPg = pDbPage;
45210   Pager *pPager = pPg->pPager;
45211 
45212   assert( (pPg->flags & PGHDR_MMAP)==0 );
45213   assert( pPager->eState>=PAGER_WRITER_LOCKED );
45214   assert( pPager->eState!=PAGER_ERROR );
45215   assert( assert_pager_state(pPager) );
45216 
45217   if( pPager->sectorSize > (u32)pPager->pageSize ){
45218     Pgno nPageCount;          /* Total number of pages in database file */
45219     Pgno pg1;                 /* First page of the sector pPg is located on. */
45220     int nPage = 0;            /* Number of pages starting at pg1 to journal */
45221     int ii;                   /* Loop counter */
45222     int needSync = 0;         /* True if any page has PGHDR_NEED_SYNC */
45223     Pgno nPagePerSector = (pPager->sectorSize/pPager->pageSize);
45224 
45225     /* Set the doNotSpill NOSYNC bit to 1. This is because we cannot allow
45226     ** a journal header to be written between the pages journaled by
45227     ** this function.
45228     */
45229     assert( !MEMDB );
45230     assert( (pPager->doNotSpill & SPILLFLAG_NOSYNC)==0 );
45231     pPager->doNotSpill |= SPILLFLAG_NOSYNC;
45232 
45233     /* This trick assumes that both the page-size and sector-size are
45234     ** an integer power of 2. It sets variable pg1 to the identifier
45235     ** of the first page of the sector pPg is located on.
45236     */
45237     pg1 = ((pPg->pgno-1) & ~(nPagePerSector-1)) + 1;
45238 
45239     nPageCount = pPager->dbSize;
45240     if( pPg->pgno>nPageCount ){
45241       nPage = (pPg->pgno - pg1)+1;
45242     }else if( (pg1+nPagePerSector-1)>nPageCount ){
45243       nPage = nPageCount+1-pg1;
45244     }else{
45245       nPage = nPagePerSector;
45246     }
45247     assert(nPage>0);
45248     assert(pg1<=pPg->pgno);
45249     assert((pg1+nPage)>pPg->pgno);
45250 
45251     for(ii=0; ii<nPage && rc==SQLITE_OK; ii++){
45252       Pgno pg = pg1+ii;
45253       PgHdr *pPage;
45254       if( pg==pPg->pgno || !sqlite3BitvecTest(pPager->pInJournal, pg) ){
45255         if( pg!=PAGER_MJ_PGNO(pPager) ){
45256           rc = sqlite3PagerGet(pPager, pg, &pPage);
45257           if( rc==SQLITE_OK ){
45258             rc = pager_write(pPage);
45259             if( pPage->flags&PGHDR_NEED_SYNC ){
45260               needSync = 1;
45261             }
45262             sqlite3PagerUnrefNotNull(pPage);
45263           }
45264         }
45265       }else if( (pPage = pager_lookup(pPager, pg))!=0 ){
45266         if( pPage->flags&PGHDR_NEED_SYNC ){
45267           needSync = 1;
45268         }
45269         sqlite3PagerUnrefNotNull(pPage);
45270       }
45271     }
45272 
45273     /* If the PGHDR_NEED_SYNC flag is set for any of the nPage pages
45274     ** starting at pg1, then it needs to be set for all of them. Because
45275     ** writing to any of these nPage pages may damage the others, the
45276     ** journal file must contain sync()ed copies of all of them
45277     ** before any of them can be written out to the database file.
45278     */
45279     if( rc==SQLITE_OK && needSync ){
45280       assert( !MEMDB );
45281       for(ii=0; ii<nPage; ii++){
45282         PgHdr *pPage = pager_lookup(pPager, pg1+ii);
45283         if( pPage ){
45284           pPage->flags |= PGHDR_NEED_SYNC;
45285           sqlite3PagerUnrefNotNull(pPage);
45286         }
45287       }
45288     }
45289 
45290     assert( (pPager->doNotSpill & SPILLFLAG_NOSYNC)!=0 );
45291     pPager->doNotSpill &= ~SPILLFLAG_NOSYNC;
45292   }else{
45293     rc = pager_write(pDbPage);
45294   }
45295   return rc;
45296 }
45297 
45298 /*
45299 ** Return TRUE if the page given in the argument was previously passed
45300 ** to sqlite3PagerWrite().  In other words, return TRUE if it is ok
45301 ** to change the content of the page.
45302 */
45303 #ifndef NDEBUG
45304 SQLITE_PRIVATE int sqlite3PagerIswriteable(DbPage *pPg){
45305   return pPg->flags&PGHDR_DIRTY;
45306 }
45307 #endif
45308 
45309 /*
45310 ** A call to this routine tells the pager that it is not necessary to
45311 ** write the information on page pPg back to the disk, even though
45312 ** that page might be marked as dirty.  This happens, for example, when
45313 ** the page has been added as a leaf of the freelist and so its
45314 ** content no longer matters.
45315 **
45316 ** The overlying software layer calls this routine when all of the data
45317 ** on the given page is unused. The pager marks the page as clean so
45318 ** that it does not get written to disk.
45319 **
45320 ** Tests show that this optimization can quadruple the speed of large
45321 ** DELETE operations.
45322 */
45323 SQLITE_PRIVATE void sqlite3PagerDontWrite(PgHdr *pPg){
45324   Pager *pPager = pPg->pPager;
45325   if( (pPg->flags&PGHDR_DIRTY) && pPager->nSavepoint==0 ){
45326     PAGERTRACE(("DONT_WRITE page %d of %d\n", pPg->pgno, PAGERID(pPager)));
45327     IOTRACE(("CLEAN %p %d\n", pPager, pPg->pgno))
45328     pPg->flags |= PGHDR_DONT_WRITE;
45329     pager_set_pagehash(pPg);
45330   }
45331 }
45332 
45333 /*
45334 ** This routine is called to increment the value of the database file
45335 ** change-counter, stored as a 4-byte big-endian integer starting at
45336 ** byte offset 24 of the pager file.  The secondary change counter at
45337 ** 92 is also updated, as is the SQLite version number at offset 96.
45338 **
45339 ** But this only happens if the pPager->changeCountDone flag is false.
45340 ** To avoid excess churning of page 1, the update only happens once.
45341 ** See also the pager_write_changecounter() routine that does an
45342 ** unconditional update of the change counters.
45343 **
45344 ** If the isDirectMode flag is zero, then this is done by calling
45345 ** sqlite3PagerWrite() on page 1, then modifying the contents of the
45346 ** page data. In this case the file will be updated when the current
45347 ** transaction is committed.
45348 **
45349 ** The isDirectMode flag may only be non-zero if the library was compiled
45350 ** with the SQLITE_ENABLE_ATOMIC_WRITE macro defined. In this case,
45351 ** if isDirect is non-zero, then the database file is updated directly
45352 ** by writing an updated version of page 1 using a call to the
45353 ** sqlite3OsWrite() function.
45354 */
45355 static int pager_incr_changecounter(Pager *pPager, int isDirectMode){
45356   int rc = SQLITE_OK;
45357 
45358   assert( pPager->eState==PAGER_WRITER_CACHEMOD
45359        || pPager->eState==PAGER_WRITER_DBMOD
45360   );
45361   assert( assert_pager_state(pPager) );
45362 
45363   /* Declare and initialize constant integer 'isDirect'. If the
45364   ** atomic-write optimization is enabled in this build, then isDirect
45365   ** is initialized to the value passed as the isDirectMode parameter
45366   ** to this function. Otherwise, it is always set to zero.
45367   **
45368   ** The idea is that if the atomic-write optimization is not
45369   ** enabled at compile time, the compiler can omit the tests of
45370   ** 'isDirect' below, as well as the block enclosed in the
45371   ** "if( isDirect )" condition.
45372   */
45373 #ifndef SQLITE_ENABLE_ATOMIC_WRITE
45374 # define DIRECT_MODE 0
45375   assert( isDirectMode==0 );
45376   UNUSED_PARAMETER(isDirectMode);
45377 #else
45378 # define DIRECT_MODE isDirectMode
45379 #endif
45380 
45381   if( !pPager->changeCountDone && ALWAYS(pPager->dbSize>0) ){
45382     PgHdr *pPgHdr;                /* Reference to page 1 */
45383 
45384     assert( !pPager->tempFile && isOpen(pPager->fd) );
45385 
45386     /* Open page 1 of the file for writing. */
45387     rc = sqlite3PagerGet(pPager, 1, &pPgHdr);
45388     assert( pPgHdr==0 || rc==SQLITE_OK );
45389 
45390     /* If page one was fetched successfully, and this function is not
45391     ** operating in direct-mode, make page 1 writable.  When not in
45392     ** direct mode, page 1 is always held in cache and hence the PagerGet()
45393     ** above is always successful - hence the ALWAYS on rc==SQLITE_OK.
45394     */
45395     if( !DIRECT_MODE && ALWAYS(rc==SQLITE_OK) ){
45396       rc = sqlite3PagerWrite(pPgHdr);
45397     }
45398 
45399     if( rc==SQLITE_OK ){
45400       /* Actually do the update of the change counter */
45401       pager_write_changecounter(pPgHdr);
45402 
45403       /* If running in direct mode, write the contents of page 1 to the file. */
45404       if( DIRECT_MODE ){
45405         const void *zBuf;
45406         assert( pPager->dbFileSize>0 );
45407         CODEC2(pPager, pPgHdr->pData, 1, 6, rc=SQLITE_NOMEM, zBuf);
45408         if( rc==SQLITE_OK ){
45409           rc = sqlite3OsWrite(pPager->fd, zBuf, pPager->pageSize, 0);
45410           pPager->aStat[PAGER_STAT_WRITE]++;
45411         }
45412         if( rc==SQLITE_OK ){
45413           /* Update the pager's copy of the change-counter. Otherwise, the
45414           ** next time a read transaction is opened the cache will be
45415           ** flushed (as the change-counter values will not match).  */
45416           const void *pCopy = (const void *)&((const char *)zBuf)[24];
45417           memcpy(&pPager->dbFileVers, pCopy, sizeof(pPager->dbFileVers));
45418           pPager->changeCountDone = 1;
45419         }
45420       }else{
45421         pPager->changeCountDone = 1;
45422       }
45423     }
45424 
45425     /* Release the page reference. */
45426     sqlite3PagerUnref(pPgHdr);
45427   }
45428   return rc;
45429 }
45430 
45431 /*
45432 ** Sync the database file to disk. This is a no-op for in-memory databases
45433 ** or pages with the Pager.noSync flag set.
45434 **
45435 ** If successful, or if called on a pager for which it is a no-op, this
45436 ** function returns SQLITE_OK. Otherwise, an IO error code is returned.
45437 */
45438 SQLITE_PRIVATE int sqlite3PagerSync(Pager *pPager, const char *zMaster){
45439   int rc = SQLITE_OK;
45440 
45441   if( isOpen(pPager->fd) ){
45442     void *pArg = (void*)zMaster;
45443     rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_SYNC, pArg);
45444     if( rc==SQLITE_NOTFOUND ) rc = SQLITE_OK;
45445   }
45446   if( rc==SQLITE_OK && !pPager->noSync ){
45447     assert( !MEMDB );
45448     rc = sqlite3OsSync(pPager->fd, pPager->syncFlags);
45449   }
45450   return rc;
45451 }
45452 
45453 /*
45454 ** This function may only be called while a write-transaction is active in
45455 ** rollback. If the connection is in WAL mode, this call is a no-op.
45456 ** Otherwise, if the connection does not already have an EXCLUSIVE lock on
45457 ** the database file, an attempt is made to obtain one.
45458 **
45459 ** If the EXCLUSIVE lock is already held or the attempt to obtain it is
45460 ** successful, or the connection is in WAL mode, SQLITE_OK is returned.
45461 ** Otherwise, either SQLITE_BUSY or an SQLITE_IOERR_XXX error code is
45462 ** returned.
45463 */
45464 SQLITE_PRIVATE int sqlite3PagerExclusiveLock(Pager *pPager){
45465   int rc = SQLITE_OK;
45466   assert( pPager->eState==PAGER_WRITER_CACHEMOD
45467        || pPager->eState==PAGER_WRITER_DBMOD
45468        || pPager->eState==PAGER_WRITER_LOCKED
45469   );
45470   assert( assert_pager_state(pPager) );
45471   if( 0==pagerUseWal(pPager) ){
45472     rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
45473   }
45474   return rc;
45475 }
45476 
45477 /*
45478 ** Sync the database file for the pager pPager. zMaster points to the name
45479 ** of a master journal file that should be written into the individual
45480 ** journal file. zMaster may be NULL, which is interpreted as no master
45481 ** journal (a single database transaction).
45482 **
45483 ** This routine ensures that:
45484 **
45485 **   * The database file change-counter is updated,
45486 **   * the journal is synced (unless the atomic-write optimization is used),
45487 **   * all dirty pages are written to the database file,
45488 **   * the database file is truncated (if required), and
45489 **   * the database file synced.
45490 **
45491 ** The only thing that remains to commit the transaction is to finalize
45492 ** (delete, truncate or zero the first part of) the journal file (or
45493 ** delete the master journal file if specified).
45494 **
45495 ** Note that if zMaster==NULL, this does not overwrite a previous value
45496 ** passed to an sqlite3PagerCommitPhaseOne() call.
45497 **
45498 ** If the final parameter - noSync - is true, then the database file itself
45499 ** is not synced. The caller must call sqlite3PagerSync() directly to
45500 ** sync the database file before calling CommitPhaseTwo() to delete the
45501 ** journal file in this case.
45502 */
45503 SQLITE_PRIVATE int sqlite3PagerCommitPhaseOne(
45504   Pager *pPager,                  /* Pager object */
45505   const char *zMaster,            /* If not NULL, the master journal name */
45506   int noSync                      /* True to omit the xSync on the db file */
45507 ){
45508   int rc = SQLITE_OK;             /* Return code */
45509 
45510   assert( pPager->eState==PAGER_WRITER_LOCKED
45511        || pPager->eState==PAGER_WRITER_CACHEMOD
45512        || pPager->eState==PAGER_WRITER_DBMOD
45513        || pPager->eState==PAGER_ERROR
45514   );
45515   assert( assert_pager_state(pPager) );
45516 
45517   /* If a prior error occurred, report that error again. */
45518   if( NEVER(pPager->errCode) ) return pPager->errCode;
45519 
45520   PAGERTRACE(("DATABASE SYNC: File=%s zMaster=%s nSize=%d\n",
45521       pPager->zFilename, zMaster, pPager->dbSize));
45522 
45523   /* If no database changes have been made, return early. */
45524   if( pPager->eState<PAGER_WRITER_CACHEMOD ) return SQLITE_OK;
45525 
45526   if( MEMDB ){
45527     /* If this is an in-memory db, or no pages have been written to, or this
45528     ** function has already been called, it is mostly a no-op.  However, any
45529     ** backup in progress needs to be restarted.
45530     */
45531     sqlite3BackupRestart(pPager->pBackup);
45532   }else{
45533     if( pagerUseWal(pPager) ){
45534       PgHdr *pList = sqlite3PcacheDirtyList(pPager->pPCache);
45535       PgHdr *pPageOne = 0;
45536       if( pList==0 ){
45537         /* Must have at least one page for the WAL commit flag.
45538         ** Ticket [2d1a5c67dfc2363e44f29d9bbd57f] 2011-05-18 */
45539         rc = sqlite3PagerGet(pPager, 1, &pPageOne);
45540         pList = pPageOne;
45541         pList->pDirty = 0;
45542       }
45543       assert( rc==SQLITE_OK );
45544       if( ALWAYS(pList) ){
45545         rc = pagerWalFrames(pPager, pList, pPager->dbSize, 1);
45546       }
45547       sqlite3PagerUnref(pPageOne);
45548       if( rc==SQLITE_OK ){
45549         sqlite3PcacheCleanAll(pPager->pPCache);
45550       }
45551     }else{
45552       /* The following block updates the change-counter. Exactly how it
45553       ** does this depends on whether or not the atomic-update optimization
45554       ** was enabled at compile time, and if this transaction meets the
45555       ** runtime criteria to use the operation:
45556       **
45557       **    * The file-system supports the atomic-write property for
45558       **      blocks of size page-size, and
45559       **    * This commit is not part of a multi-file transaction, and
45560       **    * Exactly one page has been modified and store in the journal file.
45561       **
45562       ** If the optimization was not enabled at compile time, then the
45563       ** pager_incr_changecounter() function is called to update the change
45564       ** counter in 'indirect-mode'. If the optimization is compiled in but
45565       ** is not applicable to this transaction, call sqlite3JournalCreate()
45566       ** to make sure the journal file has actually been created, then call
45567       ** pager_incr_changecounter() to update the change-counter in indirect
45568       ** mode.
45569       **
45570       ** Otherwise, if the optimization is both enabled and applicable,
45571       ** then call pager_incr_changecounter() to update the change-counter
45572       ** in 'direct' mode. In this case the journal file will never be
45573       ** created for this transaction.
45574       */
45575   #ifdef SQLITE_ENABLE_ATOMIC_WRITE
45576       PgHdr *pPg;
45577       assert( isOpen(pPager->jfd)
45578            || pPager->journalMode==PAGER_JOURNALMODE_OFF
45579            || pPager->journalMode==PAGER_JOURNALMODE_WAL
45580       );
45581       if( !zMaster && isOpen(pPager->jfd)
45582        && pPager->journalOff==jrnlBufferSize(pPager)
45583        && pPager->dbSize>=pPager->dbOrigSize
45584        && (0==(pPg = sqlite3PcacheDirtyList(pPager->pPCache)) || 0==pPg->pDirty)
45585       ){
45586         /* Update the db file change counter via the direct-write method. The
45587         ** following call will modify the in-memory representation of page 1
45588         ** to include the updated change counter and then write page 1
45589         ** directly to the database file. Because of the atomic-write
45590         ** property of the host file-system, this is safe.
45591         */
45592         rc = pager_incr_changecounter(pPager, 1);
45593       }else{
45594         rc = sqlite3JournalCreate(pPager->jfd);
45595         if( rc==SQLITE_OK ){
45596           rc = pager_incr_changecounter(pPager, 0);
45597         }
45598       }
45599   #else
45600       rc = pager_incr_changecounter(pPager, 0);
45601   #endif
45602       if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
45603 
45604       /* Write the master journal name into the journal file. If a master
45605       ** journal file name has already been written to the journal file,
45606       ** or if zMaster is NULL (no master journal), then this call is a no-op.
45607       */
45608       rc = writeMasterJournal(pPager, zMaster);
45609       if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
45610 
45611       /* Sync the journal file and write all dirty pages to the database.
45612       ** If the atomic-update optimization is being used, this sync will not
45613       ** create the journal file or perform any real IO.
45614       **
45615       ** Because the change-counter page was just modified, unless the
45616       ** atomic-update optimization is used it is almost certain that the
45617       ** journal requires a sync here. However, in locking_mode=exclusive
45618       ** on a system under memory pressure it is just possible that this is
45619       ** not the case. In this case it is likely enough that the redundant
45620       ** xSync() call will be changed to a no-op by the OS anyhow.
45621       */
45622       rc = syncJournal(pPager, 0);
45623       if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
45624 
45625       rc = pager_write_pagelist(pPager,sqlite3PcacheDirtyList(pPager->pPCache));
45626       if( rc!=SQLITE_OK ){
45627         assert( rc!=SQLITE_IOERR_BLOCKED );
45628         goto commit_phase_one_exit;
45629       }
45630       sqlite3PcacheCleanAll(pPager->pPCache);
45631 
45632       /* If the file on disk is smaller than the database image, use
45633       ** pager_truncate to grow the file here. This can happen if the database
45634       ** image was extended as part of the current transaction and then the
45635       ** last page in the db image moved to the free-list. In this case the
45636       ** last page is never written out to disk, leaving the database file
45637       ** undersized. Fix this now if it is the case.  */
45638       if( pPager->dbSize>pPager->dbFileSize ){
45639         Pgno nNew = pPager->dbSize - (pPager->dbSize==PAGER_MJ_PGNO(pPager));
45640         assert( pPager->eState==PAGER_WRITER_DBMOD );
45641         rc = pager_truncate(pPager, nNew);
45642         if( rc!=SQLITE_OK ) goto commit_phase_one_exit;
45643       }
45644 
45645       /* Finally, sync the database file. */
45646       if( !noSync ){
45647         rc = sqlite3PagerSync(pPager, zMaster);
45648       }
45649       IOTRACE(("DBSYNC %p\n", pPager))
45650     }
45651   }
45652 
45653 commit_phase_one_exit:
45654   if( rc==SQLITE_OK && !pagerUseWal(pPager) ){
45655     pPager->eState = PAGER_WRITER_FINISHED;
45656   }
45657   return rc;
45658 }
45659 
45660 
45661 /*
45662 ** When this function is called, the database file has been completely
45663 ** updated to reflect the changes made by the current transaction and
45664 ** synced to disk. The journal file still exists in the file-system
45665 ** though, and if a failure occurs at this point it will eventually
45666 ** be used as a hot-journal and the current transaction rolled back.
45667 **
45668 ** This function finalizes the journal file, either by deleting,
45669 ** truncating or partially zeroing it, so that it cannot be used
45670 ** for hot-journal rollback. Once this is done the transaction is
45671 ** irrevocably committed.
45672 **
45673 ** If an error occurs, an IO error code is returned and the pager
45674 ** moves into the error state. Otherwise, SQLITE_OK is returned.
45675 */
45676 SQLITE_PRIVATE int sqlite3PagerCommitPhaseTwo(Pager *pPager){
45677   int rc = SQLITE_OK;                  /* Return code */
45678 
45679   /* This routine should not be called if a prior error has occurred.
45680   ** But if (due to a coding error elsewhere in the system) it does get
45681   ** called, just return the same error code without doing anything. */
45682   if( NEVER(pPager->errCode) ) return pPager->errCode;
45683 
45684   assert( pPager->eState==PAGER_WRITER_LOCKED
45685        || pPager->eState==PAGER_WRITER_FINISHED
45686        || (pagerUseWal(pPager) && pPager->eState==PAGER_WRITER_CACHEMOD)
45687   );
45688   assert( assert_pager_state(pPager) );
45689 
45690   /* An optimization. If the database was not actually modified during
45691   ** this transaction, the pager is running in exclusive-mode and is
45692   ** using persistent journals, then this function is a no-op.
45693   **
45694   ** The start of the journal file currently contains a single journal
45695   ** header with the nRec field set to 0. If such a journal is used as
45696   ** a hot-journal during hot-journal rollback, 0 changes will be made
45697   ** to the database file. So there is no need to zero the journal
45698   ** header. Since the pager is in exclusive mode, there is no need
45699   ** to drop any locks either.
45700   */
45701   if( pPager->eState==PAGER_WRITER_LOCKED
45702    && pPager->exclusiveMode
45703    && pPager->journalMode==PAGER_JOURNALMODE_PERSIST
45704   ){
45705     assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) || !pPager->journalOff );
45706     pPager->eState = PAGER_READER;
45707     return SQLITE_OK;
45708   }
45709 
45710   PAGERTRACE(("COMMIT %d\n", PAGERID(pPager)));
45711   rc = pager_end_transaction(pPager, pPager->setMaster, 1);
45712   return pager_error(pPager, rc);
45713 }
45714 
45715 /*
45716 ** If a write transaction is open, then all changes made within the
45717 ** transaction are reverted and the current write-transaction is closed.
45718 ** The pager falls back to PAGER_READER state if successful, or PAGER_ERROR
45719 ** state if an error occurs.
45720 **
45721 ** If the pager is already in PAGER_ERROR state when this function is called,
45722 ** it returns Pager.errCode immediately. No work is performed in this case.
45723 **
45724 ** Otherwise, in rollback mode, this function performs two functions:
45725 **
45726 **   1) It rolls back the journal file, restoring all database file and
45727 **      in-memory cache pages to the state they were in when the transaction
45728 **      was opened, and
45729 **
45730 **   2) It finalizes the journal file, so that it is not used for hot
45731 **      rollback at any point in the future.
45732 **
45733 ** Finalization of the journal file (task 2) is only performed if the
45734 ** rollback is successful.
45735 **
45736 ** In WAL mode, all cache-entries containing data modified within the
45737 ** current transaction are either expelled from the cache or reverted to
45738 ** their pre-transaction state by re-reading data from the database or
45739 ** WAL files. The WAL transaction is then closed.
45740 */
45741 SQLITE_PRIVATE int sqlite3PagerRollback(Pager *pPager){
45742   int rc = SQLITE_OK;                  /* Return code */
45743   PAGERTRACE(("ROLLBACK %d\n", PAGERID(pPager)));
45744 
45745   /* PagerRollback() is a no-op if called in READER or OPEN state. If
45746   ** the pager is already in the ERROR state, the rollback is not
45747   ** attempted here. Instead, the error code is returned to the caller.
45748   */
45749   assert( assert_pager_state(pPager) );
45750   if( pPager->eState==PAGER_ERROR ) return pPager->errCode;
45751   if( pPager->eState<=PAGER_READER ) return SQLITE_OK;
45752 
45753   if( pagerUseWal(pPager) ){
45754     int rc2;
45755     rc = sqlite3PagerSavepoint(pPager, SAVEPOINT_ROLLBACK, -1);
45756     rc2 = pager_end_transaction(pPager, pPager->setMaster, 0);
45757     if( rc==SQLITE_OK ) rc = rc2;
45758   }else if( !isOpen(pPager->jfd) || pPager->eState==PAGER_WRITER_LOCKED ){
45759     int eState = pPager->eState;
45760     rc = pager_end_transaction(pPager, 0, 0);
45761     if( !MEMDB && eState>PAGER_WRITER_LOCKED ){
45762       /* This can happen using journal_mode=off. Move the pager to the error
45763       ** state to indicate that the contents of the cache may not be trusted.
45764       ** Any active readers will get SQLITE_ABORT.
45765       */
45766       pPager->errCode = SQLITE_ABORT;
45767       pPager->eState = PAGER_ERROR;
45768       return rc;
45769     }
45770   }else{
45771     rc = pager_playback(pPager, 0);
45772   }
45773 
45774   assert( pPager->eState==PAGER_READER || rc!=SQLITE_OK );
45775   assert( rc==SQLITE_OK || rc==SQLITE_FULL || rc==SQLITE_CORRUPT
45776           || rc==SQLITE_NOMEM || (rc&0xFF)==SQLITE_IOERR
45777           || rc==SQLITE_CANTOPEN
45778   );
45779 
45780   /* If an error occurs during a ROLLBACK, we can no longer trust the pager
45781   ** cache. So call pager_error() on the way out to make any error persistent.
45782   */
45783   return pager_error(pPager, rc);
45784 }
45785 
45786 /*
45787 ** Return TRUE if the database file is opened read-only.  Return FALSE
45788 ** if the database is (in theory) writable.
45789 */
45790 SQLITE_PRIVATE u8 sqlite3PagerIsreadonly(Pager *pPager){
45791   return pPager->readOnly;
45792 }
45793 
45794 /*
45795 ** Return the number of references to the pager.
45796 */
45797 SQLITE_PRIVATE int sqlite3PagerRefcount(Pager *pPager){
45798   return sqlite3PcacheRefCount(pPager->pPCache);
45799 }
45800 
45801 /*
45802 ** Return the approximate number of bytes of memory currently
45803 ** used by the pager and its associated cache.
45804 */
45805 SQLITE_PRIVATE int sqlite3PagerMemUsed(Pager *pPager){
45806   int perPageSize = pPager->pageSize + pPager->nExtra + sizeof(PgHdr)
45807                                      + 5*sizeof(void*);
45808   return perPageSize*sqlite3PcachePagecount(pPager->pPCache)
45809            + sqlite3MallocSize(pPager)
45810            + pPager->pageSize;
45811 }
45812 
45813 /*
45814 ** Return the number of references to the specified page.
45815 */
45816 SQLITE_PRIVATE int sqlite3PagerPageRefcount(DbPage *pPage){
45817   return sqlite3PcachePageRefcount(pPage);
45818 }
45819 
45820 #ifdef SQLITE_TEST
45821 /*
45822 ** This routine is used for testing and analysis only.
45823 */
45824 SQLITE_PRIVATE int *sqlite3PagerStats(Pager *pPager){
45825   static int a[11];
45826   a[0] = sqlite3PcacheRefCount(pPager->pPCache);
45827   a[1] = sqlite3PcachePagecount(pPager->pPCache);
45828   a[2] = sqlite3PcacheGetCachesize(pPager->pPCache);
45829   a[3] = pPager->eState==PAGER_OPEN ? -1 : (int) pPager->dbSize;
45830   a[4] = pPager->eState;
45831   a[5] = pPager->errCode;
45832   a[6] = pPager->aStat[PAGER_STAT_HIT];
45833   a[7] = pPager->aStat[PAGER_STAT_MISS];
45834   a[8] = 0;  /* Used to be pPager->nOvfl */
45835   a[9] = pPager->nRead;
45836   a[10] = pPager->aStat[PAGER_STAT_WRITE];
45837   return a;
45838 }
45839 #endif
45840 
45841 /*
45842 ** Parameter eStat must be either SQLITE_DBSTATUS_CACHE_HIT or
45843 ** SQLITE_DBSTATUS_CACHE_MISS. Before returning, *pnVal is incremented by the
45844 ** current cache hit or miss count, according to the value of eStat. If the
45845 ** reset parameter is non-zero, the cache hit or miss count is zeroed before
45846 ** returning.
45847 */
45848 SQLITE_PRIVATE void sqlite3PagerCacheStat(Pager *pPager, int eStat, int reset, int *pnVal){
45849 
45850   assert( eStat==SQLITE_DBSTATUS_CACHE_HIT
45851        || eStat==SQLITE_DBSTATUS_CACHE_MISS
45852        || eStat==SQLITE_DBSTATUS_CACHE_WRITE
45853   );
45854 
45855   assert( SQLITE_DBSTATUS_CACHE_HIT+1==SQLITE_DBSTATUS_CACHE_MISS );
45856   assert( SQLITE_DBSTATUS_CACHE_HIT+2==SQLITE_DBSTATUS_CACHE_WRITE );
45857   assert( PAGER_STAT_HIT==0 && PAGER_STAT_MISS==1 && PAGER_STAT_WRITE==2 );
45858 
45859   *pnVal += pPager->aStat[eStat - SQLITE_DBSTATUS_CACHE_HIT];
45860   if( reset ){
45861     pPager->aStat[eStat - SQLITE_DBSTATUS_CACHE_HIT] = 0;
45862   }
45863 }
45864 
45865 /*
45866 ** Return true if this is an in-memory pager.
45867 */
45868 SQLITE_PRIVATE int sqlite3PagerIsMemdb(Pager *pPager){
45869   return MEMDB;
45870 }
45871 
45872 /*
45873 ** Check that there are at least nSavepoint savepoints open. If there are
45874 ** currently less than nSavepoints open, then open one or more savepoints
45875 ** to make up the difference. If the number of savepoints is already
45876 ** equal to nSavepoint, then this function is a no-op.
45877 **
45878 ** If a memory allocation fails, SQLITE_NOMEM is returned. If an error
45879 ** occurs while opening the sub-journal file, then an IO error code is
45880 ** returned. Otherwise, SQLITE_OK.
45881 */
45882 SQLITE_PRIVATE int sqlite3PagerOpenSavepoint(Pager *pPager, int nSavepoint){
45883   int rc = SQLITE_OK;                       /* Return code */
45884   int nCurrent = pPager->nSavepoint;        /* Current number of savepoints */
45885 
45886   assert( pPager->eState>=PAGER_WRITER_LOCKED );
45887   assert( assert_pager_state(pPager) );
45888 
45889   if( nSavepoint>nCurrent && pPager->useJournal ){
45890     int ii;                                 /* Iterator variable */
45891     PagerSavepoint *aNew;                   /* New Pager.aSavepoint array */
45892 
45893     /* Grow the Pager.aSavepoint array using realloc(). Return SQLITE_NOMEM
45894     ** if the allocation fails. Otherwise, zero the new portion in case a
45895     ** malloc failure occurs while populating it in the for(...) loop below.
45896     */
45897     aNew = (PagerSavepoint *)sqlite3Realloc(
45898         pPager->aSavepoint, sizeof(PagerSavepoint)*nSavepoint
45899     );
45900     if( !aNew ){
45901       return SQLITE_NOMEM;
45902     }
45903     memset(&aNew[nCurrent], 0, (nSavepoint-nCurrent) * sizeof(PagerSavepoint));
45904     pPager->aSavepoint = aNew;
45905 
45906     /* Populate the PagerSavepoint structures just allocated. */
45907     for(ii=nCurrent; ii<nSavepoint; ii++){
45908       aNew[ii].nOrig = pPager->dbSize;
45909       if( isOpen(pPager->jfd) && pPager->journalOff>0 ){
45910         aNew[ii].iOffset = pPager->journalOff;
45911       }else{
45912         aNew[ii].iOffset = JOURNAL_HDR_SZ(pPager);
45913       }
45914       aNew[ii].iSubRec = pPager->nSubRec;
45915       aNew[ii].pInSavepoint = sqlite3BitvecCreate(pPager->dbSize);
45916       if( !aNew[ii].pInSavepoint ){
45917         return SQLITE_NOMEM;
45918       }
45919       if( pagerUseWal(pPager) ){
45920         sqlite3WalSavepoint(pPager->pWal, aNew[ii].aWalData);
45921       }
45922       pPager->nSavepoint = ii+1;
45923     }
45924     assert( pPager->nSavepoint==nSavepoint );
45925     assertTruncateConstraint(pPager);
45926   }
45927 
45928   return rc;
45929 }
45930 
45931 /*
45932 ** This function is called to rollback or release (commit) a savepoint.
45933 ** The savepoint to release or rollback need not be the most recently
45934 ** created savepoint.
45935 **
45936 ** Parameter op is always either SAVEPOINT_ROLLBACK or SAVEPOINT_RELEASE.
45937 ** If it is SAVEPOINT_RELEASE, then release and destroy the savepoint with
45938 ** index iSavepoint. If it is SAVEPOINT_ROLLBACK, then rollback all changes
45939 ** that have occurred since the specified savepoint was created.
45940 **
45941 ** The savepoint to rollback or release is identified by parameter
45942 ** iSavepoint. A value of 0 means to operate on the outermost savepoint
45943 ** (the first created). A value of (Pager.nSavepoint-1) means operate
45944 ** on the most recently created savepoint. If iSavepoint is greater than
45945 ** (Pager.nSavepoint-1), then this function is a no-op.
45946 **
45947 ** If a negative value is passed to this function, then the current
45948 ** transaction is rolled back. This is different to calling
45949 ** sqlite3PagerRollback() because this function does not terminate
45950 ** the transaction or unlock the database, it just restores the
45951 ** contents of the database to its original state.
45952 **
45953 ** In any case, all savepoints with an index greater than iSavepoint
45954 ** are destroyed. If this is a release operation (op==SAVEPOINT_RELEASE),
45955 ** then savepoint iSavepoint is also destroyed.
45956 **
45957 ** This function may return SQLITE_NOMEM if a memory allocation fails,
45958 ** or an IO error code if an IO error occurs while rolling back a
45959 ** savepoint. If no errors occur, SQLITE_OK is returned.
45960 */
45961 SQLITE_PRIVATE int sqlite3PagerSavepoint(Pager *pPager, int op, int iSavepoint){
45962   int rc = pPager->errCode;       /* Return code */
45963 
45964   assert( op==SAVEPOINT_RELEASE || op==SAVEPOINT_ROLLBACK );
45965   assert( iSavepoint>=0 || op==SAVEPOINT_ROLLBACK );
45966 
45967   if( rc==SQLITE_OK && iSavepoint<pPager->nSavepoint ){
45968     int ii;            /* Iterator variable */
45969     int nNew;          /* Number of remaining savepoints after this op. */
45970 
45971     /* Figure out how many savepoints will still be active after this
45972     ** operation. Store this value in nNew. Then free resources associated
45973     ** with any savepoints that are destroyed by this operation.
45974     */
45975     nNew = iSavepoint + (( op==SAVEPOINT_RELEASE ) ? 0 : 1);
45976     for(ii=nNew; ii<pPager->nSavepoint; ii++){
45977       sqlite3BitvecDestroy(pPager->aSavepoint[ii].pInSavepoint);
45978     }
45979     pPager->nSavepoint = nNew;
45980 
45981     /* If this is a release of the outermost savepoint, truncate
45982     ** the sub-journal to zero bytes in size. */
45983     if( op==SAVEPOINT_RELEASE ){
45984       if( nNew==0 && isOpen(pPager->sjfd) ){
45985         /* Only truncate if it is an in-memory sub-journal. */
45986         if( sqlite3IsMemJournal(pPager->sjfd) ){
45987           rc = sqlite3OsTruncate(pPager->sjfd, 0);
45988           assert( rc==SQLITE_OK );
45989         }
45990         pPager->nSubRec = 0;
45991       }
45992     }
45993     /* Else this is a rollback operation, playback the specified savepoint.
45994     ** If this is a temp-file, it is possible that the journal file has
45995     ** not yet been opened. In this case there have been no changes to
45996     ** the database file, so the playback operation can be skipped.
45997     */
45998     else if( pagerUseWal(pPager) || isOpen(pPager->jfd) ){
45999       PagerSavepoint *pSavepoint = (nNew==0)?0:&pPager->aSavepoint[nNew-1];
46000       rc = pagerPlaybackSavepoint(pPager, pSavepoint);
46001       assert(rc!=SQLITE_DONE);
46002     }
46003   }
46004 
46005   return rc;
46006 }
46007 
46008 /*
46009 ** Return the full pathname of the database file.
46010 **
46011 ** Except, if the pager is in-memory only, then return an empty string if
46012 ** nullIfMemDb is true.  This routine is called with nullIfMemDb==1 when
46013 ** used to report the filename to the user, for compatibility with legacy
46014 ** behavior.  But when the Btree needs to know the filename for matching to
46015 ** shared cache, it uses nullIfMemDb==0 so that in-memory databases can
46016 ** participate in shared-cache.
46017 */
46018 SQLITE_PRIVATE const char *sqlite3PagerFilename(Pager *pPager, int nullIfMemDb){
46019   return (nullIfMemDb && pPager->memDb) ? "" : pPager->zFilename;
46020 }
46021 
46022 /*
46023 ** Return the VFS structure for the pager.
46024 */
46025 SQLITE_PRIVATE const sqlite3_vfs *sqlite3PagerVfs(Pager *pPager){
46026   return pPager->pVfs;
46027 }
46028 
46029 /*
46030 ** Return the file handle for the database file associated
46031 ** with the pager.  This might return NULL if the file has
46032 ** not yet been opened.
46033 */
46034 SQLITE_PRIVATE sqlite3_file *sqlite3PagerFile(Pager *pPager){
46035   return pPager->fd;
46036 }
46037 
46038 /*
46039 ** Return the full pathname of the journal file.
46040 */
46041 SQLITE_PRIVATE const char *sqlite3PagerJournalname(Pager *pPager){
46042   return pPager->zJournal;
46043 }
46044 
46045 /*
46046 ** Return true if fsync() calls are disabled for this pager.  Return FALSE
46047 ** if fsync()s are executed normally.
46048 */
46049 SQLITE_PRIVATE int sqlite3PagerNosync(Pager *pPager){
46050   return pPager->noSync;
46051 }
46052 
46053 #ifdef SQLITE_HAS_CODEC
46054 /*
46055 ** Set or retrieve the codec for this pager
46056 */
46057 SQLITE_PRIVATE void sqlite3PagerSetCodec(
46058   Pager *pPager,
46059   void *(*xCodec)(void*,void*,Pgno,int),
46060   void (*xCodecSizeChng)(void*,int,int),
46061   void (*xCodecFree)(void*),
46062   void *pCodec
46063 ){
46064   if( pPager->xCodecFree ) pPager->xCodecFree(pPager->pCodec);
46065   pPager->xCodec = pPager->memDb ? 0 : xCodec;
46066   pPager->xCodecSizeChng = xCodecSizeChng;
46067   pPager->xCodecFree = xCodecFree;
46068   pPager->pCodec = pCodec;
46069   pagerReportSize(pPager);
46070 }
46071 SQLITE_PRIVATE void *sqlite3PagerGetCodec(Pager *pPager){
46072   return pPager->pCodec;
46073 }
46074 
46075 /*
46076 ** This function is called by the wal module when writing page content
46077 ** into the log file.
46078 **
46079 ** This function returns a pointer to a buffer containing the encrypted
46080 ** page content. If a malloc fails, this function may return NULL.
46081 */
46082 SQLITE_PRIVATE void *sqlite3PagerCodec(PgHdr *pPg){
46083   void *aData = 0;
46084   CODEC2(pPg->pPager, pPg->pData, pPg->pgno, 6, return 0, aData);
46085   return aData;
46086 }
46087 
46088 /*
46089 ** Return the current pager state
46090 */
46091 SQLITE_PRIVATE int sqlite3PagerState(Pager *pPager){
46092   return pPager->eState;
46093 }
46094 #endif /* SQLITE_HAS_CODEC */
46095 
46096 #ifndef SQLITE_OMIT_AUTOVACUUM
46097 /*
46098 ** Move the page pPg to location pgno in the file.
46099 **
46100 ** There must be no references to the page previously located at
46101 ** pgno (which we call pPgOld) though that page is allowed to be
46102 ** in cache.  If the page previously located at pgno is not already
46103 ** in the rollback journal, it is not put there by by this routine.
46104 **
46105 ** References to the page pPg remain valid. Updating any
46106 ** meta-data associated with pPg (i.e. data stored in the nExtra bytes
46107 ** allocated along with the page) is the responsibility of the caller.
46108 **
46109 ** A transaction must be active when this routine is called. It used to be
46110 ** required that a statement transaction was not active, but this restriction
46111 ** has been removed (CREATE INDEX needs to move a page when a statement
46112 ** transaction is active).
46113 **
46114 ** If the fourth argument, isCommit, is non-zero, then this page is being
46115 ** moved as part of a database reorganization just before the transaction
46116 ** is being committed. In this case, it is guaranteed that the database page
46117 ** pPg refers to will not be written to again within this transaction.
46118 **
46119 ** This function may return SQLITE_NOMEM or an IO error code if an error
46120 ** occurs. Otherwise, it returns SQLITE_OK.
46121 */
46122 SQLITE_PRIVATE int sqlite3PagerMovepage(Pager *pPager, DbPage *pPg, Pgno pgno, int isCommit){
46123   PgHdr *pPgOld;               /* The page being overwritten. */
46124   Pgno needSyncPgno = 0;       /* Old value of pPg->pgno, if sync is required */
46125   int rc;                      /* Return code */
46126   Pgno origPgno;               /* The original page number */
46127 
46128   assert( pPg->nRef>0 );
46129   assert( pPager->eState==PAGER_WRITER_CACHEMOD
46130        || pPager->eState==PAGER_WRITER_DBMOD
46131   );
46132   assert( assert_pager_state(pPager) );
46133 
46134   /* In order to be able to rollback, an in-memory database must journal
46135   ** the page we are moving from.
46136   */
46137   if( MEMDB ){
46138     rc = sqlite3PagerWrite(pPg);
46139     if( rc ) return rc;
46140   }
46141 
46142   /* If the page being moved is dirty and has not been saved by the latest
46143   ** savepoint, then save the current contents of the page into the
46144   ** sub-journal now. This is required to handle the following scenario:
46145   **
46146   **   BEGIN;
46147   **     <journal page X, then modify it in memory>
46148   **     SAVEPOINT one;
46149   **       <Move page X to location Y>
46150   **     ROLLBACK TO one;
46151   **
46152   ** If page X were not written to the sub-journal here, it would not
46153   ** be possible to restore its contents when the "ROLLBACK TO one"
46154   ** statement were is processed.
46155   **
46156   ** subjournalPage() may need to allocate space to store pPg->pgno into
46157   ** one or more savepoint bitvecs. This is the reason this function
46158   ** may return SQLITE_NOMEM.
46159   */
46160   if( pPg->flags&PGHDR_DIRTY
46161    && subjRequiresPage(pPg)
46162    && SQLITE_OK!=(rc = subjournalPage(pPg))
46163   ){
46164     return rc;
46165   }
46166 
46167   PAGERTRACE(("MOVE %d page %d (needSync=%d) moves to %d\n",
46168       PAGERID(pPager), pPg->pgno, (pPg->flags&PGHDR_NEED_SYNC)?1:0, pgno));
46169   IOTRACE(("MOVE %p %d %d\n", pPager, pPg->pgno, pgno))
46170 
46171   /* If the journal needs to be sync()ed before page pPg->pgno can
46172   ** be written to, store pPg->pgno in local variable needSyncPgno.
46173   **
46174   ** If the isCommit flag is set, there is no need to remember that
46175   ** the journal needs to be sync()ed before database page pPg->pgno
46176   ** can be written to. The caller has already promised not to write to it.
46177   */
46178   if( (pPg->flags&PGHDR_NEED_SYNC) && !isCommit ){
46179     needSyncPgno = pPg->pgno;
46180     assert( pPager->journalMode==PAGER_JOURNALMODE_OFF ||
46181             pageInJournal(pPager, pPg) || pPg->pgno>pPager->dbOrigSize );
46182     assert( pPg->flags&PGHDR_DIRTY );
46183   }
46184 
46185   /* If the cache contains a page with page-number pgno, remove it
46186   ** from its hash chain. Also, if the PGHDR_NEED_SYNC flag was set for
46187   ** page pgno before the 'move' operation, it needs to be retained
46188   ** for the page moved there.
46189   */
46190   pPg->flags &= ~PGHDR_NEED_SYNC;
46191   pPgOld = pager_lookup(pPager, pgno);
46192   assert( !pPgOld || pPgOld->nRef==1 );
46193   if( pPgOld ){
46194     pPg->flags |= (pPgOld->flags&PGHDR_NEED_SYNC);
46195     if( MEMDB ){
46196       /* Do not discard pages from an in-memory database since we might
46197       ** need to rollback later.  Just move the page out of the way. */
46198       sqlite3PcacheMove(pPgOld, pPager->dbSize+1);
46199     }else{
46200       sqlite3PcacheDrop(pPgOld);
46201     }
46202   }
46203 
46204   origPgno = pPg->pgno;
46205   sqlite3PcacheMove(pPg, pgno);
46206   sqlite3PcacheMakeDirty(pPg);
46207 
46208   /* For an in-memory database, make sure the original page continues
46209   ** to exist, in case the transaction needs to roll back.  Use pPgOld
46210   ** as the original page since it has already been allocated.
46211   */
46212   if( MEMDB ){
46213     assert( pPgOld );
46214     sqlite3PcacheMove(pPgOld, origPgno);
46215     sqlite3PagerUnrefNotNull(pPgOld);
46216   }
46217 
46218   if( needSyncPgno ){
46219     /* If needSyncPgno is non-zero, then the journal file needs to be
46220     ** sync()ed before any data is written to database file page needSyncPgno.
46221     ** Currently, no such page exists in the page-cache and the
46222     ** "is journaled" bitvec flag has been set. This needs to be remedied by
46223     ** loading the page into the pager-cache and setting the PGHDR_NEED_SYNC
46224     ** flag.
46225     **
46226     ** If the attempt to load the page into the page-cache fails, (due
46227     ** to a malloc() or IO failure), clear the bit in the pInJournal[]
46228     ** array. Otherwise, if the page is loaded and written again in
46229     ** this transaction, it may be written to the database file before
46230     ** it is synced into the journal file. This way, it may end up in
46231     ** the journal file twice, but that is not a problem.
46232     */
46233     PgHdr *pPgHdr;
46234     rc = sqlite3PagerGet(pPager, needSyncPgno, &pPgHdr);
46235     if( rc!=SQLITE_OK ){
46236       if( needSyncPgno<=pPager->dbOrigSize ){
46237         assert( pPager->pTmpSpace!=0 );
46238         sqlite3BitvecClear(pPager->pInJournal, needSyncPgno, pPager->pTmpSpace);
46239       }
46240       return rc;
46241     }
46242     pPgHdr->flags |= PGHDR_NEED_SYNC;
46243     sqlite3PcacheMakeDirty(pPgHdr);
46244     sqlite3PagerUnrefNotNull(pPgHdr);
46245   }
46246 
46247   return SQLITE_OK;
46248 }
46249 #endif
46250 
46251 /*
46252 ** Return a pointer to the data for the specified page.
46253 */
46254 SQLITE_PRIVATE void *sqlite3PagerGetData(DbPage *pPg){
46255   assert( pPg->nRef>0 || pPg->pPager->memDb );
46256   return pPg->pData;
46257 }
46258 
46259 /*
46260 ** Return a pointer to the Pager.nExtra bytes of "extra" space
46261 ** allocated along with the specified page.
46262 */
46263 SQLITE_PRIVATE void *sqlite3PagerGetExtra(DbPage *pPg){
46264   return pPg->pExtra;
46265 }
46266 
46267 /*
46268 ** Get/set the locking-mode for this pager. Parameter eMode must be one
46269 ** of PAGER_LOCKINGMODE_QUERY, PAGER_LOCKINGMODE_NORMAL or
46270 ** PAGER_LOCKINGMODE_EXCLUSIVE. If the parameter is not _QUERY, then
46271 ** the locking-mode is set to the value specified.
46272 **
46273 ** The returned value is either PAGER_LOCKINGMODE_NORMAL or
46274 ** PAGER_LOCKINGMODE_EXCLUSIVE, indicating the current (possibly updated)
46275 ** locking-mode.
46276 */
46277 SQLITE_PRIVATE int sqlite3PagerLockingMode(Pager *pPager, int eMode){
46278   assert( eMode==PAGER_LOCKINGMODE_QUERY
46279             || eMode==PAGER_LOCKINGMODE_NORMAL
46280             || eMode==PAGER_LOCKINGMODE_EXCLUSIVE );
46281   assert( PAGER_LOCKINGMODE_QUERY<0 );
46282   assert( PAGER_LOCKINGMODE_NORMAL>=0 && PAGER_LOCKINGMODE_EXCLUSIVE>=0 );
46283   assert( pPager->exclusiveMode || 0==sqlite3WalHeapMemory(pPager->pWal) );
46284   if( eMode>=0 && !pPager->tempFile && !sqlite3WalHeapMemory(pPager->pWal) ){
46285     pPager->exclusiveMode = (u8)eMode;
46286   }
46287   return (int)pPager->exclusiveMode;
46288 }
46289 
46290 /*
46291 ** Set the journal-mode for this pager. Parameter eMode must be one of:
46292 **
46293 **    PAGER_JOURNALMODE_DELETE
46294 **    PAGER_JOURNALMODE_TRUNCATE
46295 **    PAGER_JOURNALMODE_PERSIST
46296 **    PAGER_JOURNALMODE_OFF
46297 **    PAGER_JOURNALMODE_MEMORY
46298 **    PAGER_JOURNALMODE_WAL
46299 **
46300 ** The journalmode is set to the value specified if the change is allowed.
46301 ** The change may be disallowed for the following reasons:
46302 **
46303 **   *  An in-memory database can only have its journal_mode set to _OFF
46304 **      or _MEMORY.
46305 **
46306 **   *  Temporary databases cannot have _WAL journalmode.
46307 **
46308 ** The returned indicate the current (possibly updated) journal-mode.
46309 */
46310 SQLITE_PRIVATE int sqlite3PagerSetJournalMode(Pager *pPager, int eMode){
46311   u8 eOld = pPager->journalMode;    /* Prior journalmode */
46312 
46313 #ifdef SQLITE_DEBUG
46314   /* The print_pager_state() routine is intended to be used by the debugger
46315   ** only.  We invoke it once here to suppress a compiler warning. */
46316   print_pager_state(pPager);
46317 #endif
46318 
46319 
46320   /* The eMode parameter is always valid */
46321   assert(      eMode==PAGER_JOURNALMODE_DELETE
46322             || eMode==PAGER_JOURNALMODE_TRUNCATE
46323             || eMode==PAGER_JOURNALMODE_PERSIST
46324             || eMode==PAGER_JOURNALMODE_OFF
46325             || eMode==PAGER_JOURNALMODE_WAL
46326             || eMode==PAGER_JOURNALMODE_MEMORY );
46327 
46328   /* This routine is only called from the OP_JournalMode opcode, and
46329   ** the logic there will never allow a temporary file to be changed
46330   ** to WAL mode.
46331   */
46332   assert( pPager->tempFile==0 || eMode!=PAGER_JOURNALMODE_WAL );
46333 
46334   /* Do allow the journalmode of an in-memory database to be set to
46335   ** anything other than MEMORY or OFF
46336   */
46337   if( MEMDB ){
46338     assert( eOld==PAGER_JOURNALMODE_MEMORY || eOld==PAGER_JOURNALMODE_OFF );
46339     if( eMode!=PAGER_JOURNALMODE_MEMORY && eMode!=PAGER_JOURNALMODE_OFF ){
46340       eMode = eOld;
46341     }
46342   }
46343 
46344   if( eMode!=eOld ){
46345 
46346     /* Change the journal mode. */
46347     assert( pPager->eState!=PAGER_ERROR );
46348     pPager->journalMode = (u8)eMode;
46349 
46350     /* When transistioning from TRUNCATE or PERSIST to any other journal
46351     ** mode except WAL, unless the pager is in locking_mode=exclusive mode,
46352     ** delete the journal file.
46353     */
46354     assert( (PAGER_JOURNALMODE_TRUNCATE & 5)==1 );
46355     assert( (PAGER_JOURNALMODE_PERSIST & 5)==1 );
46356     assert( (PAGER_JOURNALMODE_DELETE & 5)==0 );
46357     assert( (PAGER_JOURNALMODE_MEMORY & 5)==4 );
46358     assert( (PAGER_JOURNALMODE_OFF & 5)==0 );
46359     assert( (PAGER_JOURNALMODE_WAL & 5)==5 );
46360 
46361     assert( isOpen(pPager->fd) || pPager->exclusiveMode );
46362     if( !pPager->exclusiveMode && (eOld & 5)==1 && (eMode & 1)==0 ){
46363 
46364       /* In this case we would like to delete the journal file. If it is
46365       ** not possible, then that is not a problem. Deleting the journal file
46366       ** here is an optimization only.
46367       **
46368       ** Before deleting the journal file, obtain a RESERVED lock on the
46369       ** database file. This ensures that the journal file is not deleted
46370       ** while it is in use by some other client.
46371       */
46372       sqlite3OsClose(pPager->jfd);
46373       if( pPager->eLock>=RESERVED_LOCK ){
46374         sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
46375       }else{
46376         int rc = SQLITE_OK;
46377         int state = pPager->eState;
46378         assert( state==PAGER_OPEN || state==PAGER_READER );
46379         if( state==PAGER_OPEN ){
46380           rc = sqlite3PagerSharedLock(pPager);
46381         }
46382         if( pPager->eState==PAGER_READER ){
46383           assert( rc==SQLITE_OK );
46384           rc = pagerLockDb(pPager, RESERVED_LOCK);
46385         }
46386         if( rc==SQLITE_OK ){
46387           sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
46388         }
46389         if( rc==SQLITE_OK && state==PAGER_READER ){
46390           pagerUnlockDb(pPager, SHARED_LOCK);
46391         }else if( state==PAGER_OPEN ){
46392           pager_unlock(pPager);
46393         }
46394         assert( state==pPager->eState );
46395       }
46396     }
46397   }
46398 
46399   /* Return the new journal mode */
46400   return (int)pPager->journalMode;
46401 }
46402 
46403 /*
46404 ** Return the current journal mode.
46405 */
46406 SQLITE_PRIVATE int sqlite3PagerGetJournalMode(Pager *pPager){
46407   return (int)pPager->journalMode;
46408 }
46409 
46410 /*
46411 ** Return TRUE if the pager is in a state where it is OK to change the
46412 ** journalmode.  Journalmode changes can only happen when the database
46413 ** is unmodified.
46414 */
46415 SQLITE_PRIVATE int sqlite3PagerOkToChangeJournalMode(Pager *pPager){
46416   assert( assert_pager_state(pPager) );
46417   if( pPager->eState>=PAGER_WRITER_CACHEMOD ) return 0;
46418   if( NEVER(isOpen(pPager->jfd) && pPager->journalOff>0) ) return 0;
46419   return 1;
46420 }
46421 
46422 /*
46423 ** Get/set the size-limit used for persistent journal files.
46424 **
46425 ** Setting the size limit to -1 means no limit is enforced.
46426 ** An attempt to set a limit smaller than -1 is a no-op.
46427 */
46428 SQLITE_PRIVATE i64 sqlite3PagerJournalSizeLimit(Pager *pPager, i64 iLimit){
46429   if( iLimit>=-1 ){
46430     pPager->journalSizeLimit = iLimit;
46431     sqlite3WalLimit(pPager->pWal, iLimit);
46432   }
46433   return pPager->journalSizeLimit;
46434 }
46435 
46436 /*
46437 ** Return a pointer to the pPager->pBackup variable. The backup module
46438 ** in backup.c maintains the content of this variable. This module
46439 ** uses it opaquely as an argument to sqlite3BackupRestart() and
46440 ** sqlite3BackupUpdate() only.
46441 */
46442 SQLITE_PRIVATE sqlite3_backup **sqlite3PagerBackupPtr(Pager *pPager){
46443   return &pPager->pBackup;
46444 }
46445 
46446 #ifndef SQLITE_OMIT_VACUUM
46447 /*
46448 ** Unless this is an in-memory or temporary database, clear the pager cache.
46449 */
46450 SQLITE_PRIVATE void sqlite3PagerClearCache(Pager *pPager){
46451   if( !MEMDB && pPager->tempFile==0 ) pager_reset(pPager);
46452 }
46453 #endif
46454 
46455 #ifndef SQLITE_OMIT_WAL
46456 /*
46457 ** This function is called when the user invokes "PRAGMA wal_checkpoint",
46458 ** "PRAGMA wal_blocking_checkpoint" or calls the sqlite3_wal_checkpoint()
46459 ** or wal_blocking_checkpoint() API functions.
46460 **
46461 ** Parameter eMode is one of SQLITE_CHECKPOINT_PASSIVE, FULL or RESTART.
46462 */
46463 SQLITE_PRIVATE int sqlite3PagerCheckpoint(Pager *pPager, int eMode, int *pnLog, int *pnCkpt){
46464   int rc = SQLITE_OK;
46465   if( pPager->pWal ){
46466     rc = sqlite3WalCheckpoint(pPager->pWal, eMode,
46467         pPager->xBusyHandler, pPager->pBusyHandlerArg,
46468         pPager->ckptSyncFlags, pPager->pageSize, (u8 *)pPager->pTmpSpace,
46469         pnLog, pnCkpt
46470     );
46471   }
46472   return rc;
46473 }
46474 
46475 SQLITE_PRIVATE int sqlite3PagerWalCallback(Pager *pPager){
46476   return sqlite3WalCallback(pPager->pWal);
46477 }
46478 
46479 /*
46480 ** Return true if the underlying VFS for the given pager supports the
46481 ** primitives necessary for write-ahead logging.
46482 */
46483 SQLITE_PRIVATE int sqlite3PagerWalSupported(Pager *pPager){
46484   const sqlite3_io_methods *pMethods = pPager->fd->pMethods;
46485   return pPager->exclusiveMode || (pMethods->iVersion>=2 && pMethods->xShmMap);
46486 }
46487 
46488 /*
46489 ** Attempt to take an exclusive lock on the database file. If a PENDING lock
46490 ** is obtained instead, immediately release it.
46491 */
46492 static int pagerExclusiveLock(Pager *pPager){
46493   int rc;                         /* Return code */
46494 
46495   assert( pPager->eLock==SHARED_LOCK || pPager->eLock==EXCLUSIVE_LOCK );
46496   rc = pagerLockDb(pPager, EXCLUSIVE_LOCK);
46497   if( rc!=SQLITE_OK ){
46498     /* If the attempt to grab the exclusive lock failed, release the
46499     ** pending lock that may have been obtained instead.  */
46500     pagerUnlockDb(pPager, SHARED_LOCK);
46501   }
46502 
46503   return rc;
46504 }
46505 
46506 /*
46507 ** Call sqlite3WalOpen() to open the WAL handle. If the pager is in
46508 ** exclusive-locking mode when this function is called, take an EXCLUSIVE
46509 ** lock on the database file and use heap-memory to store the wal-index
46510 ** in. Otherwise, use the normal shared-memory.
46511 */
46512 static int pagerOpenWal(Pager *pPager){
46513   int rc = SQLITE_OK;
46514 
46515   assert( pPager->pWal==0 && pPager->tempFile==0 );
46516   assert( pPager->eLock==SHARED_LOCK || pPager->eLock==EXCLUSIVE_LOCK );
46517 
46518   /* If the pager is already in exclusive-mode, the WAL module will use
46519   ** heap-memory for the wal-index instead of the VFS shared-memory
46520   ** implementation. Take the exclusive lock now, before opening the WAL
46521   ** file, to make sure this is safe.
46522   */
46523   if( pPager->exclusiveMode ){
46524     rc = pagerExclusiveLock(pPager);
46525   }
46526 
46527   /* Open the connection to the log file. If this operation fails,
46528   ** (e.g. due to malloc() failure), return an error code.
46529   */
46530   if( rc==SQLITE_OK ){
46531     rc = sqlite3WalOpen(pPager->pVfs,
46532         pPager->fd, pPager->zWal, pPager->exclusiveMode,
46533         pPager->journalSizeLimit, &pPager->pWal
46534     );
46535   }
46536   pagerFixMaplimit(pPager);
46537 
46538   return rc;
46539 }
46540 
46541 
46542 /*
46543 ** The caller must be holding a SHARED lock on the database file to call
46544 ** this function.
46545 **
46546 ** If the pager passed as the first argument is open on a real database
46547 ** file (not a temp file or an in-memory database), and the WAL file
46548 ** is not already open, make an attempt to open it now. If successful,
46549 ** return SQLITE_OK. If an error occurs or the VFS used by the pager does
46550 ** not support the xShmXXX() methods, return an error code. *pbOpen is
46551 ** not modified in either case.
46552 **
46553 ** If the pager is open on a temp-file (or in-memory database), or if
46554 ** the WAL file is already open, set *pbOpen to 1 and return SQLITE_OK
46555 ** without doing anything.
46556 */
46557 SQLITE_PRIVATE int sqlite3PagerOpenWal(
46558   Pager *pPager,                  /* Pager object */
46559   int *pbOpen                     /* OUT: Set to true if call is a no-op */
46560 ){
46561   int rc = SQLITE_OK;             /* Return code */
46562 
46563   assert( assert_pager_state(pPager) );
46564   assert( pPager->eState==PAGER_OPEN   || pbOpen );
46565   assert( pPager->eState==PAGER_READER || !pbOpen );
46566   assert( pbOpen==0 || *pbOpen==0 );
46567   assert( pbOpen!=0 || (!pPager->tempFile && !pPager->pWal) );
46568 
46569   if( !pPager->tempFile && !pPager->pWal ){
46570     if( !sqlite3PagerWalSupported(pPager) ) return SQLITE_CANTOPEN;
46571 
46572     /* Close any rollback journal previously open */
46573     sqlite3OsClose(pPager->jfd);
46574 
46575     rc = pagerOpenWal(pPager);
46576     if( rc==SQLITE_OK ){
46577       pPager->journalMode = PAGER_JOURNALMODE_WAL;
46578       pPager->eState = PAGER_OPEN;
46579     }
46580   }else{
46581     *pbOpen = 1;
46582   }
46583 
46584   return rc;
46585 }
46586 
46587 /*
46588 ** This function is called to close the connection to the log file prior
46589 ** to switching from WAL to rollback mode.
46590 **
46591 ** Before closing the log file, this function attempts to take an
46592 ** EXCLUSIVE lock on the database file. If this cannot be obtained, an
46593 ** error (SQLITE_BUSY) is returned and the log connection is not closed.
46594 ** If successful, the EXCLUSIVE lock is not released before returning.
46595 */
46596 SQLITE_PRIVATE int sqlite3PagerCloseWal(Pager *pPager){
46597   int rc = SQLITE_OK;
46598 
46599   assert( pPager->journalMode==PAGER_JOURNALMODE_WAL );
46600 
46601   /* If the log file is not already open, but does exist in the file-system,
46602   ** it may need to be checkpointed before the connection can switch to
46603   ** rollback mode. Open it now so this can happen.
46604   */
46605   if( !pPager->pWal ){
46606     int logexists = 0;
46607     rc = pagerLockDb(pPager, SHARED_LOCK);
46608     if( rc==SQLITE_OK ){
46609       rc = sqlite3OsAccess(
46610           pPager->pVfs, pPager->zWal, SQLITE_ACCESS_EXISTS, &logexists
46611       );
46612     }
46613     if( rc==SQLITE_OK && logexists ){
46614       rc = pagerOpenWal(pPager);
46615     }
46616   }
46617 
46618   /* Checkpoint and close the log. Because an EXCLUSIVE lock is held on
46619   ** the database file, the log and log-summary files will be deleted.
46620   */
46621   if( rc==SQLITE_OK && pPager->pWal ){
46622     rc = pagerExclusiveLock(pPager);
46623     if( rc==SQLITE_OK ){
46624       rc = sqlite3WalClose(pPager->pWal, pPager->ckptSyncFlags,
46625                            pPager->pageSize, (u8*)pPager->pTmpSpace);
46626       pPager->pWal = 0;
46627       pagerFixMaplimit(pPager);
46628     }
46629   }
46630   return rc;
46631 }
46632 
46633 #endif /* !SQLITE_OMIT_WAL */
46634 
46635 #ifdef SQLITE_ENABLE_ZIPVFS
46636 /*
46637 ** A read-lock must be held on the pager when this function is called. If
46638 ** the pager is in WAL mode and the WAL file currently contains one or more
46639 ** frames, return the size in bytes of the page images stored within the
46640 ** WAL frames. Otherwise, if this is not a WAL database or the WAL file
46641 ** is empty, return 0.
46642 */
46643 SQLITE_PRIVATE int sqlite3PagerWalFramesize(Pager *pPager){
46644   assert( pPager->eState==PAGER_READER );
46645   return sqlite3WalFramesize(pPager->pWal);
46646 }
46647 #endif
46648 
46649 #endif /* SQLITE_OMIT_DISKIO */
46650 
46651 /************** End of pager.c ***********************************************/
46652 /************** Begin file wal.c *********************************************/
46653 /*
46654 ** 2010 February 1
46655 **
46656 ** The author disclaims copyright to this source code.  In place of
46657 ** a legal notice, here is a blessing:
46658 **
46659 **    May you do good and not evil.
46660 **    May you find forgiveness for yourself and forgive others.
46661 **    May you share freely, never taking more than you give.
46662 **
46663 *************************************************************************
46664 **
46665 ** This file contains the implementation of a write-ahead log (WAL) used in
46666 ** "journal_mode=WAL" mode.
46667 **
46668 ** WRITE-AHEAD LOG (WAL) FILE FORMAT
46669 **
46670 ** A WAL file consists of a header followed by zero or more "frames".
46671 ** Each frame records the revised content of a single page from the
46672 ** database file.  All changes to the database are recorded by writing
46673 ** frames into the WAL.  Transactions commit when a frame is written that
46674 ** contains a commit marker.  A single WAL can and usually does record
46675 ** multiple transactions.  Periodically, the content of the WAL is
46676 ** transferred back into the database file in an operation called a
46677 ** "checkpoint".
46678 **
46679 ** A single WAL file can be used multiple times.  In other words, the
46680 ** WAL can fill up with frames and then be checkpointed and then new
46681 ** frames can overwrite the old ones.  A WAL always grows from beginning
46682 ** toward the end.  Checksums and counters attached to each frame are
46683 ** used to determine which frames within the WAL are valid and which
46684 ** are leftovers from prior checkpoints.
46685 **
46686 ** The WAL header is 32 bytes in size and consists of the following eight
46687 ** big-endian 32-bit unsigned integer values:
46688 **
46689 **     0: Magic number.  0x377f0682 or 0x377f0683
46690 **     4: File format version.  Currently 3007000
46691 **     8: Database page size.  Example: 1024
46692 **    12: Checkpoint sequence number
46693 **    16: Salt-1, random integer incremented with each checkpoint
46694 **    20: Salt-2, a different random integer changing with each ckpt
46695 **    24: Checksum-1 (first part of checksum for first 24 bytes of header).
46696 **    28: Checksum-2 (second part of checksum for first 24 bytes of header).
46697 **
46698 ** Immediately following the wal-header are zero or more frames. Each
46699 ** frame consists of a 24-byte frame-header followed by a <page-size> bytes
46700 ** of page data. The frame-header is six big-endian 32-bit unsigned
46701 ** integer values, as follows:
46702 **
46703 **     0: Page number.
46704 **     4: For commit records, the size of the database image in pages
46705 **        after the commit. For all other records, zero.
46706 **     8: Salt-1 (copied from the header)
46707 **    12: Salt-2 (copied from the header)
46708 **    16: Checksum-1.
46709 **    20: Checksum-2.
46710 **
46711 ** A frame is considered valid if and only if the following conditions are
46712 ** true:
46713 **
46714 **    (1) The salt-1 and salt-2 values in the frame-header match
46715 **        salt values in the wal-header
46716 **
46717 **    (2) The checksum values in the final 8 bytes of the frame-header
46718 **        exactly match the checksum computed consecutively on the
46719 **        WAL header and the first 8 bytes and the content of all frames
46720 **        up to and including the current frame.
46721 **
46722 ** The checksum is computed using 32-bit big-endian integers if the
46723 ** magic number in the first 4 bytes of the WAL is 0x377f0683 and it
46724 ** is computed using little-endian if the magic number is 0x377f0682.
46725 ** The checksum values are always stored in the frame header in a
46726 ** big-endian format regardless of which byte order is used to compute
46727 ** the checksum.  The checksum is computed by interpreting the input as
46728 ** an even number of unsigned 32-bit integers: x[0] through x[N].  The
46729 ** algorithm used for the checksum is as follows:
46730 **
46731 **   for i from 0 to n-1 step 2:
46732 **     s0 += x[i] + s1;
46733 **     s1 += x[i+1] + s0;
46734 **   endfor
46735 **
46736 ** Note that s0 and s1 are both weighted checksums using fibonacci weights
46737 ** in reverse order (the largest fibonacci weight occurs on the first element
46738 ** of the sequence being summed.)  The s1 value spans all 32-bit
46739 ** terms of the sequence whereas s0 omits the final term.
46740 **
46741 ** On a checkpoint, the WAL is first VFS.xSync-ed, then valid content of the
46742 ** WAL is transferred into the database, then the database is VFS.xSync-ed.
46743 ** The VFS.xSync operations serve as write barriers - all writes launched
46744 ** before the xSync must complete before any write that launches after the
46745 ** xSync begins.
46746 **
46747 ** After each checkpoint, the salt-1 value is incremented and the salt-2
46748 ** value is randomized.  This prevents old and new frames in the WAL from
46749 ** being considered valid at the same time and being checkpointing together
46750 ** following a crash.
46751 **
46752 ** READER ALGORITHM
46753 **
46754 ** To read a page from the database (call it page number P), a reader
46755 ** first checks the WAL to see if it contains page P.  If so, then the
46756 ** last valid instance of page P that is a followed by a commit frame
46757 ** or is a commit frame itself becomes the value read.  If the WAL
46758 ** contains no copies of page P that are valid and which are a commit
46759 ** frame or are followed by a commit frame, then page P is read from
46760 ** the database file.
46761 **
46762 ** To start a read transaction, the reader records the index of the last
46763 ** valid frame in the WAL.  The reader uses this recorded "mxFrame" value
46764 ** for all subsequent read operations.  New transactions can be appended
46765 ** to the WAL, but as long as the reader uses its original mxFrame value
46766 ** and ignores the newly appended content, it will see a consistent snapshot
46767 ** of the database from a single point in time.  This technique allows
46768 ** multiple concurrent readers to view different versions of the database
46769 ** content simultaneously.
46770 **
46771 ** The reader algorithm in the previous paragraphs works correctly, but
46772 ** because frames for page P can appear anywhere within the WAL, the
46773 ** reader has to scan the entire WAL looking for page P frames.  If the
46774 ** WAL is large (multiple megabytes is typical) that scan can be slow,
46775 ** and read performance suffers.  To overcome this problem, a separate
46776 ** data structure called the wal-index is maintained to expedite the
46777 ** search for frames of a particular page.
46778 **
46779 ** WAL-INDEX FORMAT
46780 **
46781 ** Conceptually, the wal-index is shared memory, though VFS implementations
46782 ** might choose to implement the wal-index using a mmapped file.  Because
46783 ** the wal-index is shared memory, SQLite does not support journal_mode=WAL
46784 ** on a network filesystem.  All users of the database must be able to
46785 ** share memory.
46786 **
46787 ** The wal-index is transient.  After a crash, the wal-index can (and should
46788 ** be) reconstructed from the original WAL file.  In fact, the VFS is required
46789 ** to either truncate or zero the header of the wal-index when the last
46790 ** connection to it closes.  Because the wal-index is transient, it can
46791 ** use an architecture-specific format; it does not have to be cross-platform.
46792 ** Hence, unlike the database and WAL file formats which store all values
46793 ** as big endian, the wal-index can store multi-byte values in the native
46794 ** byte order of the host computer.
46795 **
46796 ** The purpose of the wal-index is to answer this question quickly:  Given
46797 ** a page number P and a maximum frame index M, return the index of the
46798 ** last frame in the wal before frame M for page P in the WAL, or return
46799 ** NULL if there are no frames for page P in the WAL prior to M.
46800 **
46801 ** The wal-index consists of a header region, followed by an one or
46802 ** more index blocks.
46803 **
46804 ** The wal-index header contains the total number of frames within the WAL
46805 ** in the mxFrame field.
46806 **
46807 ** Each index block except for the first contains information on
46808 ** HASHTABLE_NPAGE frames. The first index block contains information on
46809 ** HASHTABLE_NPAGE_ONE frames. The values of HASHTABLE_NPAGE_ONE and
46810 ** HASHTABLE_NPAGE are selected so that together the wal-index header and
46811 ** first index block are the same size as all other index blocks in the
46812 ** wal-index.
46813 **
46814 ** Each index block contains two sections, a page-mapping that contains the
46815 ** database page number associated with each wal frame, and a hash-table
46816 ** that allows readers to query an index block for a specific page number.
46817 ** The page-mapping is an array of HASHTABLE_NPAGE (or HASHTABLE_NPAGE_ONE
46818 ** for the first index block) 32-bit page numbers. The first entry in the
46819 ** first index-block contains the database page number corresponding to the
46820 ** first frame in the WAL file. The first entry in the second index block
46821 ** in the WAL file corresponds to the (HASHTABLE_NPAGE_ONE+1)th frame in
46822 ** the log, and so on.
46823 **
46824 ** The last index block in a wal-index usually contains less than the full
46825 ** complement of HASHTABLE_NPAGE (or HASHTABLE_NPAGE_ONE) page-numbers,
46826 ** depending on the contents of the WAL file. This does not change the
46827 ** allocated size of the page-mapping array - the page-mapping array merely
46828 ** contains unused entries.
46829 **
46830 ** Even without using the hash table, the last frame for page P
46831 ** can be found by scanning the page-mapping sections of each index block
46832 ** starting with the last index block and moving toward the first, and
46833 ** within each index block, starting at the end and moving toward the
46834 ** beginning.  The first entry that equals P corresponds to the frame
46835 ** holding the content for that page.
46836 **
46837 ** The hash table consists of HASHTABLE_NSLOT 16-bit unsigned integers.
46838 ** HASHTABLE_NSLOT = 2*HASHTABLE_NPAGE, and there is one entry in the
46839 ** hash table for each page number in the mapping section, so the hash
46840 ** table is never more than half full.  The expected number of collisions
46841 ** prior to finding a match is 1.  Each entry of the hash table is an
46842 ** 1-based index of an entry in the mapping section of the same
46843 ** index block.   Let K be the 1-based index of the largest entry in
46844 ** the mapping section.  (For index blocks other than the last, K will
46845 ** always be exactly HASHTABLE_NPAGE (4096) and for the last index block
46846 ** K will be (mxFrame%HASHTABLE_NPAGE).)  Unused slots of the hash table
46847 ** contain a value of 0.
46848 **
46849 ** To look for page P in the hash table, first compute a hash iKey on
46850 ** P as follows:
46851 **
46852 **      iKey = (P * 383) % HASHTABLE_NSLOT
46853 **
46854 ** Then start scanning entries of the hash table, starting with iKey
46855 ** (wrapping around to the beginning when the end of the hash table is
46856 ** reached) until an unused hash slot is found. Let the first unused slot
46857 ** be at index iUnused.  (iUnused might be less than iKey if there was
46858 ** wrap-around.) Because the hash table is never more than half full,
46859 ** the search is guaranteed to eventually hit an unused entry.  Let
46860 ** iMax be the value between iKey and iUnused, closest to iUnused,
46861 ** where aHash[iMax]==P.  If there is no iMax entry (if there exists
46862 ** no hash slot such that aHash[i]==p) then page P is not in the
46863 ** current index block.  Otherwise the iMax-th mapping entry of the
46864 ** current index block corresponds to the last entry that references
46865 ** page P.
46866 **
46867 ** A hash search begins with the last index block and moves toward the
46868 ** first index block, looking for entries corresponding to page P.  On
46869 ** average, only two or three slots in each index block need to be
46870 ** examined in order to either find the last entry for page P, or to
46871 ** establish that no such entry exists in the block.  Each index block
46872 ** holds over 4000 entries.  So two or three index blocks are sufficient
46873 ** to cover a typical 10 megabyte WAL file, assuming 1K pages.  8 or 10
46874 ** comparisons (on average) suffice to either locate a frame in the
46875 ** WAL or to establish that the frame does not exist in the WAL.  This
46876 ** is much faster than scanning the entire 10MB WAL.
46877 **
46878 ** Note that entries are added in order of increasing K.  Hence, one
46879 ** reader might be using some value K0 and a second reader that started
46880 ** at a later time (after additional transactions were added to the WAL
46881 ** and to the wal-index) might be using a different value K1, where K1>K0.
46882 ** Both readers can use the same hash table and mapping section to get
46883 ** the correct result.  There may be entries in the hash table with
46884 ** K>K0 but to the first reader, those entries will appear to be unused
46885 ** slots in the hash table and so the first reader will get an answer as
46886 ** if no values greater than K0 had ever been inserted into the hash table
46887 ** in the first place - which is what reader one wants.  Meanwhile, the
46888 ** second reader using K1 will see additional values that were inserted
46889 ** later, which is exactly what reader two wants.
46890 **
46891 ** When a rollback occurs, the value of K is decreased. Hash table entries
46892 ** that correspond to frames greater than the new K value are removed
46893 ** from the hash table at this point.
46894 */
46895 #ifndef SQLITE_OMIT_WAL
46896 
46897 
46898 /*
46899 ** Trace output macros
46900 */
46901 #if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
46902 SQLITE_PRIVATE int sqlite3WalTrace = 0;
46903 # define WALTRACE(X)  if(sqlite3WalTrace) sqlite3DebugPrintf X
46904 #else
46905 # define WALTRACE(X)
46906 #endif
46907 
46908 /*
46909 ** The maximum (and only) versions of the wal and wal-index formats
46910 ** that may be interpreted by this version of SQLite.
46911 **
46912 ** If a client begins recovering a WAL file and finds that (a) the checksum
46913 ** values in the wal-header are correct and (b) the version field is not
46914 ** WAL_MAX_VERSION, recovery fails and SQLite returns SQLITE_CANTOPEN.
46915 **
46916 ** Similarly, if a client successfully reads a wal-index header (i.e. the
46917 ** checksum test is successful) and finds that the version field is not
46918 ** WALINDEX_MAX_VERSION, then no read-transaction is opened and SQLite
46919 ** returns SQLITE_CANTOPEN.
46920 */
46921 #define WAL_MAX_VERSION      3007000
46922 #define WALINDEX_MAX_VERSION 3007000
46923 
46924 /*
46925 ** Indices of various locking bytes.   WAL_NREADER is the number
46926 ** of available reader locks and should be at least 3.
46927 */
46928 #define WAL_WRITE_LOCK         0
46929 #define WAL_ALL_BUT_WRITE      1
46930 #define WAL_CKPT_LOCK          1
46931 #define WAL_RECOVER_LOCK       2
46932 #define WAL_READ_LOCK(I)       (3+(I))
46933 #define WAL_NREADER            (SQLITE_SHM_NLOCK-3)
46934 
46935 
46936 /* Object declarations */
46937 typedef struct WalIndexHdr WalIndexHdr;
46938 typedef struct WalIterator WalIterator;
46939 typedef struct WalCkptInfo WalCkptInfo;
46940 
46941 
46942 /*
46943 ** The following object holds a copy of the wal-index header content.
46944 **
46945 ** The actual header in the wal-index consists of two copies of this
46946 ** object.
46947 **
46948 ** The szPage value can be any power of 2 between 512 and 32768, inclusive.
46949 ** Or it can be 1 to represent a 65536-byte page.  The latter case was
46950 ** added in 3.7.1 when support for 64K pages was added.
46951 */
46952 struct WalIndexHdr {
46953   u32 iVersion;                   /* Wal-index version */
46954   u32 unused;                     /* Unused (padding) field */
46955   u32 iChange;                    /* Counter incremented each transaction */
46956   u8 isInit;                      /* 1 when initialized */
46957   u8 bigEndCksum;                 /* True if checksums in WAL are big-endian */
46958   u16 szPage;                     /* Database page size in bytes. 1==64K */
46959   u32 mxFrame;                    /* Index of last valid frame in the WAL */
46960   u32 nPage;                      /* Size of database in pages */
46961   u32 aFrameCksum[2];             /* Checksum of last frame in log */
46962   u32 aSalt[2];                   /* Two salt values copied from WAL header */
46963   u32 aCksum[2];                  /* Checksum over all prior fields */
46964 };
46965 
46966 /*
46967 ** A copy of the following object occurs in the wal-index immediately
46968 ** following the second copy of the WalIndexHdr.  This object stores
46969 ** information used by checkpoint.
46970 **
46971 ** nBackfill is the number of frames in the WAL that have been written
46972 ** back into the database. (We call the act of moving content from WAL to
46973 ** database "backfilling".)  The nBackfill number is never greater than
46974 ** WalIndexHdr.mxFrame.  nBackfill can only be increased by threads
46975 ** holding the WAL_CKPT_LOCK lock (which includes a recovery thread).
46976 ** However, a WAL_WRITE_LOCK thread can move the value of nBackfill from
46977 ** mxFrame back to zero when the WAL is reset.
46978 **
46979 ** There is one entry in aReadMark[] for each reader lock.  If a reader
46980 ** holds read-lock K, then the value in aReadMark[K] is no greater than
46981 ** the mxFrame for that reader.  The value READMARK_NOT_USED (0xffffffff)
46982 ** for any aReadMark[] means that entry is unused.  aReadMark[0] is
46983 ** a special case; its value is never used and it exists as a place-holder
46984 ** to avoid having to offset aReadMark[] indexs by one.  Readers holding
46985 ** WAL_READ_LOCK(0) always ignore the entire WAL and read all content
46986 ** directly from the database.
46987 **
46988 ** The value of aReadMark[K] may only be changed by a thread that
46989 ** is holding an exclusive lock on WAL_READ_LOCK(K).  Thus, the value of
46990 ** aReadMark[K] cannot changed while there is a reader is using that mark
46991 ** since the reader will be holding a shared lock on WAL_READ_LOCK(K).
46992 **
46993 ** The checkpointer may only transfer frames from WAL to database where
46994 ** the frame numbers are less than or equal to every aReadMark[] that is
46995 ** in use (that is, every aReadMark[j] for which there is a corresponding
46996 ** WAL_READ_LOCK(j)).  New readers (usually) pick the aReadMark[] with the
46997 ** largest value and will increase an unused aReadMark[] to mxFrame if there
46998 ** is not already an aReadMark[] equal to mxFrame.  The exception to the
46999 ** previous sentence is when nBackfill equals mxFrame (meaning that everything
47000 ** in the WAL has been backfilled into the database) then new readers
47001 ** will choose aReadMark[0] which has value 0 and hence such reader will
47002 ** get all their all content directly from the database file and ignore
47003 ** the WAL.
47004 **
47005 ** Writers normally append new frames to the end of the WAL.  However,
47006 ** if nBackfill equals mxFrame (meaning that all WAL content has been
47007 ** written back into the database) and if no readers are using the WAL
47008 ** (in other words, if there are no WAL_READ_LOCK(i) where i>0) then
47009 ** the writer will first "reset" the WAL back to the beginning and start
47010 ** writing new content beginning at frame 1.
47011 **
47012 ** We assume that 32-bit loads are atomic and so no locks are needed in
47013 ** order to read from any aReadMark[] entries.
47014 */
47015 struct WalCkptInfo {
47016   u32 nBackfill;                  /* Number of WAL frames backfilled into DB */
47017   u32 aReadMark[WAL_NREADER];     /* Reader marks */
47018 };
47019 #define READMARK_NOT_USED  0xffffffff
47020 
47021 
47022 /* A block of WALINDEX_LOCK_RESERVED bytes beginning at
47023 ** WALINDEX_LOCK_OFFSET is reserved for locks. Since some systems
47024 ** only support mandatory file-locks, we do not read or write data
47025 ** from the region of the file on which locks are applied.
47026 */
47027 #define WALINDEX_LOCK_OFFSET   (sizeof(WalIndexHdr)*2 + sizeof(WalCkptInfo))
47028 #define WALINDEX_LOCK_RESERVED 16
47029 #define WALINDEX_HDR_SIZE      (WALINDEX_LOCK_OFFSET+WALINDEX_LOCK_RESERVED)
47030 
47031 /* Size of header before each frame in wal */
47032 #define WAL_FRAME_HDRSIZE 24
47033 
47034 /* Size of write ahead log header, including checksum. */
47035 /* #define WAL_HDRSIZE 24 */
47036 #define WAL_HDRSIZE 32
47037 
47038 /* WAL magic value. Either this value, or the same value with the least
47039 ** significant bit also set (WAL_MAGIC | 0x00000001) is stored in 32-bit
47040 ** big-endian format in the first 4 bytes of a WAL file.
47041 **
47042 ** If the LSB is set, then the checksums for each frame within the WAL
47043 ** file are calculated by treating all data as an array of 32-bit
47044 ** big-endian words. Otherwise, they are calculated by interpreting
47045 ** all data as 32-bit little-endian words.
47046 */
47047 #define WAL_MAGIC 0x377f0682
47048 
47049 /*
47050 ** Return the offset of frame iFrame in the write-ahead log file,
47051 ** assuming a database page size of szPage bytes. The offset returned
47052 ** is to the start of the write-ahead log frame-header.
47053 */
47054 #define walFrameOffset(iFrame, szPage) (                               \
47055   WAL_HDRSIZE + ((iFrame)-1)*(i64)((szPage)+WAL_FRAME_HDRSIZE)         \
47056 )
47057 
47058 /*
47059 ** An open write-ahead log file is represented by an instance of the
47060 ** following object.
47061 */
47062 struct Wal {
47063   sqlite3_vfs *pVfs;         /* The VFS used to create pDbFd */
47064   sqlite3_file *pDbFd;       /* File handle for the database file */
47065   sqlite3_file *pWalFd;      /* File handle for WAL file */
47066   u32 iCallback;             /* Value to pass to log callback (or 0) */
47067   i64 mxWalSize;             /* Truncate WAL to this size upon reset */
47068   int nWiData;               /* Size of array apWiData */
47069   int szFirstBlock;          /* Size of first block written to WAL file */
47070   volatile u32 **apWiData;   /* Pointer to wal-index content in memory */
47071   u32 szPage;                /* Database page size */
47072   i16 readLock;              /* Which read lock is being held.  -1 for none */
47073   u8 syncFlags;              /* Flags to use to sync header writes */
47074   u8 exclusiveMode;          /* Non-zero if connection is in exclusive mode */
47075   u8 writeLock;              /* True if in a write transaction */
47076   u8 ckptLock;               /* True if holding a checkpoint lock */
47077   u8 readOnly;               /* WAL_RDWR, WAL_RDONLY, or WAL_SHM_RDONLY */
47078   u8 truncateOnCommit;       /* True to truncate WAL file on commit */
47079   u8 syncHeader;             /* Fsync the WAL header if true */
47080   u8 padToSectorBoundary;    /* Pad transactions out to the next sector */
47081   WalIndexHdr hdr;           /* Wal-index header for current transaction */
47082   const char *zWalName;      /* Name of WAL file */
47083   u32 nCkpt;                 /* Checkpoint sequence counter in the wal-header */
47084 #ifdef SQLITE_DEBUG
47085   u8 lockError;              /* True if a locking error has occurred */
47086 #endif
47087 };
47088 
47089 /*
47090 ** Candidate values for Wal.exclusiveMode.
47091 */
47092 #define WAL_NORMAL_MODE     0
47093 #define WAL_EXCLUSIVE_MODE  1
47094 #define WAL_HEAPMEMORY_MODE 2
47095 
47096 /*
47097 ** Possible values for WAL.readOnly
47098 */
47099 #define WAL_RDWR        0    /* Normal read/write connection */
47100 #define WAL_RDONLY      1    /* The WAL file is readonly */
47101 #define WAL_SHM_RDONLY  2    /* The SHM file is readonly */
47102 
47103 /*
47104 ** Each page of the wal-index mapping contains a hash-table made up of
47105 ** an array of HASHTABLE_NSLOT elements of the following type.
47106 */
47107 typedef u16 ht_slot;
47108 
47109 /*
47110 ** This structure is used to implement an iterator that loops through
47111 ** all frames in the WAL in database page order. Where two or more frames
47112 ** correspond to the same database page, the iterator visits only the
47113 ** frame most recently written to the WAL (in other words, the frame with
47114 ** the largest index).
47115 **
47116 ** The internals of this structure are only accessed by:
47117 **
47118 **   walIteratorInit() - Create a new iterator,
47119 **   walIteratorNext() - Step an iterator,
47120 **   walIteratorFree() - Free an iterator.
47121 **
47122 ** This functionality is used by the checkpoint code (see walCheckpoint()).
47123 */
47124 struct WalIterator {
47125   int iPrior;                     /* Last result returned from the iterator */
47126   int nSegment;                   /* Number of entries in aSegment[] */
47127   struct WalSegment {
47128     int iNext;                    /* Next slot in aIndex[] not yet returned */
47129     ht_slot *aIndex;              /* i0, i1, i2... such that aPgno[iN] ascend */
47130     u32 *aPgno;                   /* Array of page numbers. */
47131     int nEntry;                   /* Nr. of entries in aPgno[] and aIndex[] */
47132     int iZero;                    /* Frame number associated with aPgno[0] */
47133   } aSegment[1];                  /* One for every 32KB page in the wal-index */
47134 };
47135 
47136 /*
47137 ** Define the parameters of the hash tables in the wal-index file. There
47138 ** is a hash-table following every HASHTABLE_NPAGE page numbers in the
47139 ** wal-index.
47140 **
47141 ** Changing any of these constants will alter the wal-index format and
47142 ** create incompatibilities.
47143 */
47144 #define HASHTABLE_NPAGE      4096                 /* Must be power of 2 */
47145 #define HASHTABLE_HASH_1     383                  /* Should be prime */
47146 #define HASHTABLE_NSLOT      (HASHTABLE_NPAGE*2)  /* Must be a power of 2 */
47147 
47148 /*
47149 ** The block of page numbers associated with the first hash-table in a
47150 ** wal-index is smaller than usual. This is so that there is a complete
47151 ** hash-table on each aligned 32KB page of the wal-index.
47152 */
47153 #define HASHTABLE_NPAGE_ONE  (HASHTABLE_NPAGE - (WALINDEX_HDR_SIZE/sizeof(u32)))
47154 
47155 /* The wal-index is divided into pages of WALINDEX_PGSZ bytes each. */
47156 #define WALINDEX_PGSZ   (                                         \
47157     sizeof(ht_slot)*HASHTABLE_NSLOT + HASHTABLE_NPAGE*sizeof(u32) \
47158 )
47159 
47160 /*
47161 ** Obtain a pointer to the iPage'th page of the wal-index. The wal-index
47162 ** is broken into pages of WALINDEX_PGSZ bytes. Wal-index pages are
47163 ** numbered from zero.
47164 **
47165 ** If this call is successful, *ppPage is set to point to the wal-index
47166 ** page and SQLITE_OK is returned. If an error (an OOM or VFS error) occurs,
47167 ** then an SQLite error code is returned and *ppPage is set to 0.
47168 */
47169 static int walIndexPage(Wal *pWal, int iPage, volatile u32 **ppPage){
47170   int rc = SQLITE_OK;
47171 
47172   /* Enlarge the pWal->apWiData[] array if required */
47173   if( pWal->nWiData<=iPage ){
47174     int nByte = sizeof(u32*)*(iPage+1);
47175     volatile u32 **apNew;
47176     apNew = (volatile u32 **)sqlite3_realloc((void *)pWal->apWiData, nByte);
47177     if( !apNew ){
47178       *ppPage = 0;
47179       return SQLITE_NOMEM;
47180     }
47181     memset((void*)&apNew[pWal->nWiData], 0,
47182            sizeof(u32*)*(iPage+1-pWal->nWiData));
47183     pWal->apWiData = apNew;
47184     pWal->nWiData = iPage+1;
47185   }
47186 
47187   /* Request a pointer to the required page from the VFS */
47188   if( pWal->apWiData[iPage]==0 ){
47189     if( pWal->exclusiveMode==WAL_HEAPMEMORY_MODE ){
47190       pWal->apWiData[iPage] = (u32 volatile *)sqlite3MallocZero(WALINDEX_PGSZ);
47191       if( !pWal->apWiData[iPage] ) rc = SQLITE_NOMEM;
47192     }else{
47193       rc = sqlite3OsShmMap(pWal->pDbFd, iPage, WALINDEX_PGSZ,
47194           pWal->writeLock, (void volatile **)&pWal->apWiData[iPage]
47195       );
47196       if( rc==SQLITE_READONLY ){
47197         pWal->readOnly |= WAL_SHM_RDONLY;
47198         rc = SQLITE_OK;
47199       }
47200     }
47201   }
47202 
47203   *ppPage = pWal->apWiData[iPage];
47204   assert( iPage==0 || *ppPage || rc!=SQLITE_OK );
47205   return rc;
47206 }
47207 
47208 /*
47209 ** Return a pointer to the WalCkptInfo structure in the wal-index.
47210 */
47211 static volatile WalCkptInfo *walCkptInfo(Wal *pWal){
47212   assert( pWal->nWiData>0 && pWal->apWiData[0] );
47213   return (volatile WalCkptInfo*)&(pWal->apWiData[0][sizeof(WalIndexHdr)/2]);
47214 }
47215 
47216 /*
47217 ** Return a pointer to the WalIndexHdr structure in the wal-index.
47218 */
47219 static volatile WalIndexHdr *walIndexHdr(Wal *pWal){
47220   assert( pWal->nWiData>0 && pWal->apWiData[0] );
47221   return (volatile WalIndexHdr*)pWal->apWiData[0];
47222 }
47223 
47224 /*
47225 ** The argument to this macro must be of type u32. On a little-endian
47226 ** architecture, it returns the u32 value that results from interpreting
47227 ** the 4 bytes as a big-endian value. On a big-endian architecture, it
47228 ** returns the value that would be produced by intepreting the 4 bytes
47229 ** of the input value as a little-endian integer.
47230 */
47231 #define BYTESWAP32(x) ( \
47232     (((x)&0x000000FF)<<24) + (((x)&0x0000FF00)<<8)  \
47233   + (((x)&0x00FF0000)>>8)  + (((x)&0xFF000000)>>24) \
47234 )
47235 
47236 /*
47237 ** Generate or extend an 8 byte checksum based on the data in
47238 ** array aByte[] and the initial values of aIn[0] and aIn[1] (or
47239 ** initial values of 0 and 0 if aIn==NULL).
47240 **
47241 ** The checksum is written back into aOut[] before returning.
47242 **
47243 ** nByte must be a positive multiple of 8.
47244 */
47245 static void walChecksumBytes(
47246   int nativeCksum, /* True for native byte-order, false for non-native */
47247   u8 *a,           /* Content to be checksummed */
47248   int nByte,       /* Bytes of content in a[].  Must be a multiple of 8. */
47249   const u32 *aIn,  /* Initial checksum value input */
47250   u32 *aOut        /* OUT: Final checksum value output */
47251 ){
47252   u32 s1, s2;
47253   u32 *aData = (u32 *)a;
47254   u32 *aEnd = (u32 *)&a[nByte];
47255 
47256   if( aIn ){
47257     s1 = aIn[0];
47258     s2 = aIn[1];
47259   }else{
47260     s1 = s2 = 0;
47261   }
47262 
47263   assert( nByte>=8 );
47264   assert( (nByte&0x00000007)==0 );
47265 
47266   if( nativeCksum ){
47267     do {
47268       s1 += *aData++ + s2;
47269       s2 += *aData++ + s1;
47270     }while( aData<aEnd );
47271   }else{
47272     do {
47273       s1 += BYTESWAP32(aData[0]) + s2;
47274       s2 += BYTESWAP32(aData[1]) + s1;
47275       aData += 2;
47276     }while( aData<aEnd );
47277   }
47278 
47279   aOut[0] = s1;
47280   aOut[1] = s2;
47281 }
47282 
47283 static void walShmBarrier(Wal *pWal){
47284   if( pWal->exclusiveMode!=WAL_HEAPMEMORY_MODE ){
47285     sqlite3OsShmBarrier(pWal->pDbFd);
47286   }
47287 }
47288 
47289 /*
47290 ** Write the header information in pWal->hdr into the wal-index.
47291 **
47292 ** The checksum on pWal->hdr is updated before it is written.
47293 */
47294 static void walIndexWriteHdr(Wal *pWal){
47295   volatile WalIndexHdr *aHdr = walIndexHdr(pWal);
47296   const int nCksum = offsetof(WalIndexHdr, aCksum);
47297 
47298   assert( pWal->writeLock );
47299   pWal->hdr.isInit = 1;
47300   pWal->hdr.iVersion = WALINDEX_MAX_VERSION;
47301   walChecksumBytes(1, (u8*)&pWal->hdr, nCksum, 0, pWal->hdr.aCksum);
47302   memcpy((void *)&aHdr[1], (void *)&pWal->hdr, sizeof(WalIndexHdr));
47303   walShmBarrier(pWal);
47304   memcpy((void *)&aHdr[0], (void *)&pWal->hdr, sizeof(WalIndexHdr));
47305 }
47306 
47307 /*
47308 ** This function encodes a single frame header and writes it to a buffer
47309 ** supplied by the caller. A frame-header is made up of a series of
47310 ** 4-byte big-endian integers, as follows:
47311 **
47312 **     0: Page number.
47313 **     4: For commit records, the size of the database image in pages
47314 **        after the commit. For all other records, zero.
47315 **     8: Salt-1 (copied from the wal-header)
47316 **    12: Salt-2 (copied from the wal-header)
47317 **    16: Checksum-1.
47318 **    20: Checksum-2.
47319 */
47320 static void walEncodeFrame(
47321   Wal *pWal,                      /* The write-ahead log */
47322   u32 iPage,                      /* Database page number for frame */
47323   u32 nTruncate,                  /* New db size (or 0 for non-commit frames) */
47324   u8 *aData,                      /* Pointer to page data */
47325   u8 *aFrame                      /* OUT: Write encoded frame here */
47326 ){
47327   int nativeCksum;                /* True for native byte-order checksums */
47328   u32 *aCksum = pWal->hdr.aFrameCksum;
47329   assert( WAL_FRAME_HDRSIZE==24 );
47330   sqlite3Put4byte(&aFrame[0], iPage);
47331   sqlite3Put4byte(&aFrame[4], nTruncate);
47332   memcpy(&aFrame[8], pWal->hdr.aSalt, 8);
47333 
47334   nativeCksum = (pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN);
47335   walChecksumBytes(nativeCksum, aFrame, 8, aCksum, aCksum);
47336   walChecksumBytes(nativeCksum, aData, pWal->szPage, aCksum, aCksum);
47337 
47338   sqlite3Put4byte(&aFrame[16], aCksum[0]);
47339   sqlite3Put4byte(&aFrame[20], aCksum[1]);
47340 }
47341 
47342 /*
47343 ** Check to see if the frame with header in aFrame[] and content
47344 ** in aData[] is valid.  If it is a valid frame, fill *piPage and
47345 ** *pnTruncate and return true.  Return if the frame is not valid.
47346 */
47347 static int walDecodeFrame(
47348   Wal *pWal,                      /* The write-ahead log */
47349   u32 *piPage,                    /* OUT: Database page number for frame */
47350   u32 *pnTruncate,                /* OUT: New db size (or 0 if not commit) */
47351   u8 *aData,                      /* Pointer to page data (for checksum) */
47352   u8 *aFrame                      /* Frame data */
47353 ){
47354   int nativeCksum;                /* True for native byte-order checksums */
47355   u32 *aCksum = pWal->hdr.aFrameCksum;
47356   u32 pgno;                       /* Page number of the frame */
47357   assert( WAL_FRAME_HDRSIZE==24 );
47358 
47359   /* A frame is only valid if the salt values in the frame-header
47360   ** match the salt values in the wal-header.
47361   */
47362   if( memcmp(&pWal->hdr.aSalt, &aFrame[8], 8)!=0 ){
47363     return 0;
47364   }
47365 
47366   /* A frame is only valid if the page number is creater than zero.
47367   */
47368   pgno = sqlite3Get4byte(&aFrame[0]);
47369   if( pgno==0 ){
47370     return 0;
47371   }
47372 
47373   /* A frame is only valid if a checksum of the WAL header,
47374   ** all prior frams, the first 16 bytes of this frame-header,
47375   ** and the frame-data matches the checksum in the last 8
47376   ** bytes of this frame-header.
47377   */
47378   nativeCksum = (pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN);
47379   walChecksumBytes(nativeCksum, aFrame, 8, aCksum, aCksum);
47380   walChecksumBytes(nativeCksum, aData, pWal->szPage, aCksum, aCksum);
47381   if( aCksum[0]!=sqlite3Get4byte(&aFrame[16])
47382    || aCksum[1]!=sqlite3Get4byte(&aFrame[20])
47383   ){
47384     /* Checksum failed. */
47385     return 0;
47386   }
47387 
47388   /* If we reach this point, the frame is valid.  Return the page number
47389   ** and the new database size.
47390   */
47391   *piPage = pgno;
47392   *pnTruncate = sqlite3Get4byte(&aFrame[4]);
47393   return 1;
47394 }
47395 
47396 
47397 #if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
47398 /*
47399 ** Names of locks.  This routine is used to provide debugging output and is not
47400 ** a part of an ordinary build.
47401 */
47402 static const char *walLockName(int lockIdx){
47403   if( lockIdx==WAL_WRITE_LOCK ){
47404     return "WRITE-LOCK";
47405   }else if( lockIdx==WAL_CKPT_LOCK ){
47406     return "CKPT-LOCK";
47407   }else if( lockIdx==WAL_RECOVER_LOCK ){
47408     return "RECOVER-LOCK";
47409   }else{
47410     static char zName[15];
47411     sqlite3_snprintf(sizeof(zName), zName, "READ-LOCK[%d]",
47412                      lockIdx-WAL_READ_LOCK(0));
47413     return zName;
47414   }
47415 }
47416 #endif /*defined(SQLITE_TEST) || defined(SQLITE_DEBUG) */
47417 
47418 
47419 /*
47420 ** Set or release locks on the WAL.  Locks are either shared or exclusive.
47421 ** A lock cannot be moved directly between shared and exclusive - it must go
47422 ** through the unlocked state first.
47423 **
47424 ** In locking_mode=EXCLUSIVE, all of these routines become no-ops.
47425 */
47426 static int walLockShared(Wal *pWal, int lockIdx){
47427   int rc;
47428   if( pWal->exclusiveMode ) return SQLITE_OK;
47429   rc = sqlite3OsShmLock(pWal->pDbFd, lockIdx, 1,
47430                         SQLITE_SHM_LOCK | SQLITE_SHM_SHARED);
47431   WALTRACE(("WAL%p: acquire SHARED-%s %s\n", pWal,
47432             walLockName(lockIdx), rc ? "failed" : "ok"));
47433   VVA_ONLY( pWal->lockError = (u8)(rc!=SQLITE_OK && rc!=SQLITE_BUSY); )
47434   return rc;
47435 }
47436 static void walUnlockShared(Wal *pWal, int lockIdx){
47437   if( pWal->exclusiveMode ) return;
47438   (void)sqlite3OsShmLock(pWal->pDbFd, lockIdx, 1,
47439                          SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED);
47440   WALTRACE(("WAL%p: release SHARED-%s\n", pWal, walLockName(lockIdx)));
47441 }
47442 static int walLockExclusive(Wal *pWal, int lockIdx, int n){
47443   int rc;
47444   if( pWal->exclusiveMode ) return SQLITE_OK;
47445   rc = sqlite3OsShmLock(pWal->pDbFd, lockIdx, n,
47446                         SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE);
47447   WALTRACE(("WAL%p: acquire EXCLUSIVE-%s cnt=%d %s\n", pWal,
47448             walLockName(lockIdx), n, rc ? "failed" : "ok"));
47449   VVA_ONLY( pWal->lockError = (u8)(rc!=SQLITE_OK && rc!=SQLITE_BUSY); )
47450   return rc;
47451 }
47452 static void walUnlockExclusive(Wal *pWal, int lockIdx, int n){
47453   if( pWal->exclusiveMode ) return;
47454   (void)sqlite3OsShmLock(pWal->pDbFd, lockIdx, n,
47455                          SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE);
47456   WALTRACE(("WAL%p: release EXCLUSIVE-%s cnt=%d\n", pWal,
47457              walLockName(lockIdx), n));
47458 }
47459 
47460 /*
47461 ** Compute a hash on a page number.  The resulting hash value must land
47462 ** between 0 and (HASHTABLE_NSLOT-1).  The walHashNext() function advances
47463 ** the hash to the next value in the event of a collision.
47464 */
47465 static int walHash(u32 iPage){
47466   assert( iPage>0 );
47467   assert( (HASHTABLE_NSLOT & (HASHTABLE_NSLOT-1))==0 );
47468   return (iPage*HASHTABLE_HASH_1) & (HASHTABLE_NSLOT-1);
47469 }
47470 static int walNextHash(int iPriorHash){
47471   return (iPriorHash+1)&(HASHTABLE_NSLOT-1);
47472 }
47473 
47474 /*
47475 ** Return pointers to the hash table and page number array stored on
47476 ** page iHash of the wal-index. The wal-index is broken into 32KB pages
47477 ** numbered starting from 0.
47478 **
47479 ** Set output variable *paHash to point to the start of the hash table
47480 ** in the wal-index file. Set *piZero to one less than the frame
47481 ** number of the first frame indexed by this hash table. If a
47482 ** slot in the hash table is set to N, it refers to frame number
47483 ** (*piZero+N) in the log.
47484 **
47485 ** Finally, set *paPgno so that *paPgno[1] is the page number of the
47486 ** first frame indexed by the hash table, frame (*piZero+1).
47487 */
47488 static int walHashGet(
47489   Wal *pWal,                      /* WAL handle */
47490   int iHash,                      /* Find the iHash'th table */
47491   volatile ht_slot **paHash,      /* OUT: Pointer to hash index */
47492   volatile u32 **paPgno,          /* OUT: Pointer to page number array */
47493   u32 *piZero                     /* OUT: Frame associated with *paPgno[0] */
47494 ){
47495   int rc;                         /* Return code */
47496   volatile u32 *aPgno;
47497 
47498   rc = walIndexPage(pWal, iHash, &aPgno);
47499   assert( rc==SQLITE_OK || iHash>0 );
47500 
47501   if( rc==SQLITE_OK ){
47502     u32 iZero;
47503     volatile ht_slot *aHash;
47504 
47505     aHash = (volatile ht_slot *)&aPgno[HASHTABLE_NPAGE];
47506     if( iHash==0 ){
47507       aPgno = &aPgno[WALINDEX_HDR_SIZE/sizeof(u32)];
47508       iZero = 0;
47509     }else{
47510       iZero = HASHTABLE_NPAGE_ONE + (iHash-1)*HASHTABLE_NPAGE;
47511     }
47512 
47513     *paPgno = &aPgno[-1];
47514     *paHash = aHash;
47515     *piZero = iZero;
47516   }
47517   return rc;
47518 }
47519 
47520 /*
47521 ** Return the number of the wal-index page that contains the hash-table
47522 ** and page-number array that contain entries corresponding to WAL frame
47523 ** iFrame. The wal-index is broken up into 32KB pages. Wal-index pages
47524 ** are numbered starting from 0.
47525 */
47526 static int walFramePage(u32 iFrame){
47527   int iHash = (iFrame+HASHTABLE_NPAGE-HASHTABLE_NPAGE_ONE-1) / HASHTABLE_NPAGE;
47528   assert( (iHash==0 || iFrame>HASHTABLE_NPAGE_ONE)
47529        && (iHash>=1 || iFrame<=HASHTABLE_NPAGE_ONE)
47530        && (iHash<=1 || iFrame>(HASHTABLE_NPAGE_ONE+HASHTABLE_NPAGE))
47531        && (iHash>=2 || iFrame<=HASHTABLE_NPAGE_ONE+HASHTABLE_NPAGE)
47532        && (iHash<=2 || iFrame>(HASHTABLE_NPAGE_ONE+2*HASHTABLE_NPAGE))
47533   );
47534   return iHash;
47535 }
47536 
47537 /*
47538 ** Return the page number associated with frame iFrame in this WAL.
47539 */
47540 static u32 walFramePgno(Wal *pWal, u32 iFrame){
47541   int iHash = walFramePage(iFrame);
47542   if( iHash==0 ){
47543     return pWal->apWiData[0][WALINDEX_HDR_SIZE/sizeof(u32) + iFrame - 1];
47544   }
47545   return pWal->apWiData[iHash][(iFrame-1-HASHTABLE_NPAGE_ONE)%HASHTABLE_NPAGE];
47546 }
47547 
47548 /*
47549 ** Remove entries from the hash table that point to WAL slots greater
47550 ** than pWal->hdr.mxFrame.
47551 **
47552 ** This function is called whenever pWal->hdr.mxFrame is decreased due
47553 ** to a rollback or savepoint.
47554 **
47555 ** At most only the hash table containing pWal->hdr.mxFrame needs to be
47556 ** updated.  Any later hash tables will be automatically cleared when
47557 ** pWal->hdr.mxFrame advances to the point where those hash tables are
47558 ** actually needed.
47559 */
47560 static void walCleanupHash(Wal *pWal){
47561   volatile ht_slot *aHash = 0;    /* Pointer to hash table to clear */
47562   volatile u32 *aPgno = 0;        /* Page number array for hash table */
47563   u32 iZero = 0;                  /* frame == (aHash[x]+iZero) */
47564   int iLimit = 0;                 /* Zero values greater than this */
47565   int nByte;                      /* Number of bytes to zero in aPgno[] */
47566   int i;                          /* Used to iterate through aHash[] */
47567 
47568   assert( pWal->writeLock );
47569   testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE-1 );
47570   testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE );
47571   testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE+1 );
47572 
47573   if( pWal->hdr.mxFrame==0 ) return;
47574 
47575   /* Obtain pointers to the hash-table and page-number array containing
47576   ** the entry that corresponds to frame pWal->hdr.mxFrame. It is guaranteed
47577   ** that the page said hash-table and array reside on is already mapped.
47578   */
47579   assert( pWal->nWiData>walFramePage(pWal->hdr.mxFrame) );
47580   assert( pWal->apWiData[walFramePage(pWal->hdr.mxFrame)] );
47581   walHashGet(pWal, walFramePage(pWal->hdr.mxFrame), &aHash, &aPgno, &iZero);
47582 
47583   /* Zero all hash-table entries that correspond to frame numbers greater
47584   ** than pWal->hdr.mxFrame.
47585   */
47586   iLimit = pWal->hdr.mxFrame - iZero;
47587   assert( iLimit>0 );
47588   for(i=0; i<HASHTABLE_NSLOT; i++){
47589     if( aHash[i]>iLimit ){
47590       aHash[i] = 0;
47591     }
47592   }
47593 
47594   /* Zero the entries in the aPgno array that correspond to frames with
47595   ** frame numbers greater than pWal->hdr.mxFrame.
47596   */
47597   nByte = (int)((char *)aHash - (char *)&aPgno[iLimit+1]);
47598   memset((void *)&aPgno[iLimit+1], 0, nByte);
47599 
47600 #ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
47601   /* Verify that the every entry in the mapping region is still reachable
47602   ** via the hash table even after the cleanup.
47603   */
47604   if( iLimit ){
47605     int i;           /* Loop counter */
47606     int iKey;        /* Hash key */
47607     for(i=1; i<=iLimit; i++){
47608       for(iKey=walHash(aPgno[i]); aHash[iKey]; iKey=walNextHash(iKey)){
47609         if( aHash[iKey]==i ) break;
47610       }
47611       assert( aHash[iKey]==i );
47612     }
47613   }
47614 #endif /* SQLITE_ENABLE_EXPENSIVE_ASSERT */
47615 }
47616 
47617 
47618 /*
47619 ** Set an entry in the wal-index that will map database page number
47620 ** pPage into WAL frame iFrame.
47621 */
47622 static int walIndexAppend(Wal *pWal, u32 iFrame, u32 iPage){
47623   int rc;                         /* Return code */
47624   u32 iZero = 0;                  /* One less than frame number of aPgno[1] */
47625   volatile u32 *aPgno = 0;        /* Page number array */
47626   volatile ht_slot *aHash = 0;    /* Hash table */
47627 
47628   rc = walHashGet(pWal, walFramePage(iFrame), &aHash, &aPgno, &iZero);
47629 
47630   /* Assuming the wal-index file was successfully mapped, populate the
47631   ** page number array and hash table entry.
47632   */
47633   if( rc==SQLITE_OK ){
47634     int iKey;                     /* Hash table key */
47635     int idx;                      /* Value to write to hash-table slot */
47636     int nCollide;                 /* Number of hash collisions */
47637 
47638     idx = iFrame - iZero;
47639     assert( idx <= HASHTABLE_NSLOT/2 + 1 );
47640 
47641     /* If this is the first entry to be added to this hash-table, zero the
47642     ** entire hash table and aPgno[] array before proceding.
47643     */
47644     if( idx==1 ){
47645       int nByte = (int)((u8 *)&aHash[HASHTABLE_NSLOT] - (u8 *)&aPgno[1]);
47646       memset((void*)&aPgno[1], 0, nByte);
47647     }
47648 
47649     /* If the entry in aPgno[] is already set, then the previous writer
47650     ** must have exited unexpectedly in the middle of a transaction (after
47651     ** writing one or more dirty pages to the WAL to free up memory).
47652     ** Remove the remnants of that writers uncommitted transaction from
47653     ** the hash-table before writing any new entries.
47654     */
47655     if( aPgno[idx] ){
47656       walCleanupHash(pWal);
47657       assert( !aPgno[idx] );
47658     }
47659 
47660     /* Write the aPgno[] array entry and the hash-table slot. */
47661     nCollide = idx;
47662     for(iKey=walHash(iPage); aHash[iKey]; iKey=walNextHash(iKey)){
47663       if( (nCollide--)==0 ) return SQLITE_CORRUPT_BKPT;
47664     }
47665     aPgno[idx] = iPage;
47666     aHash[iKey] = (ht_slot)idx;
47667 
47668 #ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
47669     /* Verify that the number of entries in the hash table exactly equals
47670     ** the number of entries in the mapping region.
47671     */
47672     {
47673       int i;           /* Loop counter */
47674       int nEntry = 0;  /* Number of entries in the hash table */
47675       for(i=0; i<HASHTABLE_NSLOT; i++){ if( aHash[i] ) nEntry++; }
47676       assert( nEntry==idx );
47677     }
47678 
47679     /* Verify that the every entry in the mapping region is reachable
47680     ** via the hash table.  This turns out to be a really, really expensive
47681     ** thing to check, so only do this occasionally - not on every
47682     ** iteration.
47683     */
47684     if( (idx&0x3ff)==0 ){
47685       int i;           /* Loop counter */
47686       for(i=1; i<=idx; i++){
47687         for(iKey=walHash(aPgno[i]); aHash[iKey]; iKey=walNextHash(iKey)){
47688           if( aHash[iKey]==i ) break;
47689         }
47690         assert( aHash[iKey]==i );
47691       }
47692     }
47693 #endif /* SQLITE_ENABLE_EXPENSIVE_ASSERT */
47694   }
47695 
47696 
47697   return rc;
47698 }
47699 
47700 
47701 /*
47702 ** Recover the wal-index by reading the write-ahead log file.
47703 **
47704 ** This routine first tries to establish an exclusive lock on the
47705 ** wal-index to prevent other threads/processes from doing anything
47706 ** with the WAL or wal-index while recovery is running.  The
47707 ** WAL_RECOVER_LOCK is also held so that other threads will know
47708 ** that this thread is running recovery.  If unable to establish
47709 ** the necessary locks, this routine returns SQLITE_BUSY.
47710 */
47711 static int walIndexRecover(Wal *pWal){
47712   int rc;                         /* Return Code */
47713   i64 nSize;                      /* Size of log file */
47714   u32 aFrameCksum[2] = {0, 0};
47715   int iLock;                      /* Lock offset to lock for checkpoint */
47716   int nLock;                      /* Number of locks to hold */
47717 
47718   /* Obtain an exclusive lock on all byte in the locking range not already
47719   ** locked by the caller. The caller is guaranteed to have locked the
47720   ** WAL_WRITE_LOCK byte, and may have also locked the WAL_CKPT_LOCK byte.
47721   ** If successful, the same bytes that are locked here are unlocked before
47722   ** this function returns.
47723   */
47724   assert( pWal->ckptLock==1 || pWal->ckptLock==0 );
47725   assert( WAL_ALL_BUT_WRITE==WAL_WRITE_LOCK+1 );
47726   assert( WAL_CKPT_LOCK==WAL_ALL_BUT_WRITE );
47727   assert( pWal->writeLock );
47728   iLock = WAL_ALL_BUT_WRITE + pWal->ckptLock;
47729   nLock = SQLITE_SHM_NLOCK - iLock;
47730   rc = walLockExclusive(pWal, iLock, nLock);
47731   if( rc ){
47732     return rc;
47733   }
47734   WALTRACE(("WAL%p: recovery begin...\n", pWal));
47735 
47736   memset(&pWal->hdr, 0, sizeof(WalIndexHdr));
47737 
47738   rc = sqlite3OsFileSize(pWal->pWalFd, &nSize);
47739   if( rc!=SQLITE_OK ){
47740     goto recovery_error;
47741   }
47742 
47743   if( nSize>WAL_HDRSIZE ){
47744     u8 aBuf[WAL_HDRSIZE];         /* Buffer to load WAL header into */
47745     u8 *aFrame = 0;               /* Malloc'd buffer to load entire frame */
47746     int szFrame;                  /* Number of bytes in buffer aFrame[] */
47747     u8 *aData;                    /* Pointer to data part of aFrame buffer */
47748     int iFrame;                   /* Index of last frame read */
47749     i64 iOffset;                  /* Next offset to read from log file */
47750     int szPage;                   /* Page size according to the log */
47751     u32 magic;                    /* Magic value read from WAL header */
47752     u32 version;                  /* Magic value read from WAL header */
47753     int isValid;                  /* True if this frame is valid */
47754 
47755     /* Read in the WAL header. */
47756     rc = sqlite3OsRead(pWal->pWalFd, aBuf, WAL_HDRSIZE, 0);
47757     if( rc!=SQLITE_OK ){
47758       goto recovery_error;
47759     }
47760 
47761     /* If the database page size is not a power of two, or is greater than
47762     ** SQLITE_MAX_PAGE_SIZE, conclude that the WAL file contains no valid
47763     ** data. Similarly, if the 'magic' value is invalid, ignore the whole
47764     ** WAL file.
47765     */
47766     magic = sqlite3Get4byte(&aBuf[0]);
47767     szPage = sqlite3Get4byte(&aBuf[8]);
47768     if( (magic&0xFFFFFFFE)!=WAL_MAGIC
47769      || szPage&(szPage-1)
47770      || szPage>SQLITE_MAX_PAGE_SIZE
47771      || szPage<512
47772     ){
47773       goto finished;
47774     }
47775     pWal->hdr.bigEndCksum = (u8)(magic&0x00000001);
47776     pWal->szPage = szPage;
47777     pWal->nCkpt = sqlite3Get4byte(&aBuf[12]);
47778     memcpy(&pWal->hdr.aSalt, &aBuf[16], 8);
47779 
47780     /* Verify that the WAL header checksum is correct */
47781     walChecksumBytes(pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN,
47782         aBuf, WAL_HDRSIZE-2*4, 0, pWal->hdr.aFrameCksum
47783     );
47784     if( pWal->hdr.aFrameCksum[0]!=sqlite3Get4byte(&aBuf[24])
47785      || pWal->hdr.aFrameCksum[1]!=sqlite3Get4byte(&aBuf[28])
47786     ){
47787       goto finished;
47788     }
47789 
47790     /* Verify that the version number on the WAL format is one that
47791     ** are able to understand */
47792     version = sqlite3Get4byte(&aBuf[4]);
47793     if( version!=WAL_MAX_VERSION ){
47794       rc = SQLITE_CANTOPEN_BKPT;
47795       goto finished;
47796     }
47797 
47798     /* Malloc a buffer to read frames into. */
47799     szFrame = szPage + WAL_FRAME_HDRSIZE;
47800     aFrame = (u8 *)sqlite3_malloc(szFrame);
47801     if( !aFrame ){
47802       rc = SQLITE_NOMEM;
47803       goto recovery_error;
47804     }
47805     aData = &aFrame[WAL_FRAME_HDRSIZE];
47806 
47807     /* Read all frames from the log file. */
47808     iFrame = 0;
47809     for(iOffset=WAL_HDRSIZE; (iOffset+szFrame)<=nSize; iOffset+=szFrame){
47810       u32 pgno;                   /* Database page number for frame */
47811       u32 nTruncate;              /* dbsize field from frame header */
47812 
47813       /* Read and decode the next log frame. */
47814       iFrame++;
47815       rc = sqlite3OsRead(pWal->pWalFd, aFrame, szFrame, iOffset);
47816       if( rc!=SQLITE_OK ) break;
47817       isValid = walDecodeFrame(pWal, &pgno, &nTruncate, aData, aFrame);
47818       if( !isValid ) break;
47819       rc = walIndexAppend(pWal, iFrame, pgno);
47820       if( rc!=SQLITE_OK ) break;
47821 
47822       /* If nTruncate is non-zero, this is a commit record. */
47823       if( nTruncate ){
47824         pWal->hdr.mxFrame = iFrame;
47825         pWal->hdr.nPage = nTruncate;
47826         pWal->hdr.szPage = (u16)((szPage&0xff00) | (szPage>>16));
47827         testcase( szPage<=32768 );
47828         testcase( szPage>=65536 );
47829         aFrameCksum[0] = pWal->hdr.aFrameCksum[0];
47830         aFrameCksum[1] = pWal->hdr.aFrameCksum[1];
47831       }
47832     }
47833 
47834     sqlite3_free(aFrame);
47835   }
47836 
47837 finished:
47838   if( rc==SQLITE_OK ){
47839     volatile WalCkptInfo *pInfo;
47840     int i;
47841     pWal->hdr.aFrameCksum[0] = aFrameCksum[0];
47842     pWal->hdr.aFrameCksum[1] = aFrameCksum[1];
47843     walIndexWriteHdr(pWal);
47844 
47845     /* Reset the checkpoint-header. This is safe because this thread is
47846     ** currently holding locks that exclude all other readers, writers and
47847     ** checkpointers.
47848     */
47849     pInfo = walCkptInfo(pWal);
47850     pInfo->nBackfill = 0;
47851     pInfo->aReadMark[0] = 0;
47852     for(i=1; i<WAL_NREADER; i++) pInfo->aReadMark[i] = READMARK_NOT_USED;
47853     if( pWal->hdr.mxFrame ) pInfo->aReadMark[1] = pWal->hdr.mxFrame;
47854 
47855     /* If more than one frame was recovered from the log file, report an
47856     ** event via sqlite3_log(). This is to help with identifying performance
47857     ** problems caused by applications routinely shutting down without
47858     ** checkpointing the log file.
47859     */
47860     if( pWal->hdr.nPage ){
47861       sqlite3_log(SQLITE_NOTICE_RECOVER_WAL,
47862           "recovered %d frames from WAL file %s",
47863           pWal->hdr.mxFrame, pWal->zWalName
47864       );
47865     }
47866   }
47867 
47868 recovery_error:
47869   WALTRACE(("WAL%p: recovery %s\n", pWal, rc ? "failed" : "ok"));
47870   walUnlockExclusive(pWal, iLock, nLock);
47871   return rc;
47872 }
47873 
47874 /*
47875 ** Close an open wal-index.
47876 */
47877 static void walIndexClose(Wal *pWal, int isDelete){
47878   if( pWal->exclusiveMode==WAL_HEAPMEMORY_MODE ){
47879     int i;
47880     for(i=0; i<pWal->nWiData; i++){
47881       sqlite3_free((void *)pWal->apWiData[i]);
47882       pWal->apWiData[i] = 0;
47883     }
47884   }else{
47885     sqlite3OsShmUnmap(pWal->pDbFd, isDelete);
47886   }
47887 }
47888 
47889 /*
47890 ** Open a connection to the WAL file zWalName. The database file must
47891 ** already be opened on connection pDbFd. The buffer that zWalName points
47892 ** to must remain valid for the lifetime of the returned Wal* handle.
47893 **
47894 ** A SHARED lock should be held on the database file when this function
47895 ** is called. The purpose of this SHARED lock is to prevent any other
47896 ** client from unlinking the WAL or wal-index file. If another process
47897 ** were to do this just after this client opened one of these files, the
47898 ** system would be badly broken.
47899 **
47900 ** If the log file is successfully opened, SQLITE_OK is returned and
47901 ** *ppWal is set to point to a new WAL handle. If an error occurs,
47902 ** an SQLite error code is returned and *ppWal is left unmodified.
47903 */
47904 SQLITE_PRIVATE int sqlite3WalOpen(
47905   sqlite3_vfs *pVfs,              /* vfs module to open wal and wal-index */
47906   sqlite3_file *pDbFd,            /* The open database file */
47907   const char *zWalName,           /* Name of the WAL file */
47908   int bNoShm,                     /* True to run in heap-memory mode */
47909   i64 mxWalSize,                  /* Truncate WAL to this size on reset */
47910   Wal **ppWal                     /* OUT: Allocated Wal handle */
47911 ){
47912   int rc;                         /* Return Code */
47913   Wal *pRet;                      /* Object to allocate and return */
47914   int flags;                      /* Flags passed to OsOpen() */
47915 
47916   assert( zWalName && zWalName[0] );
47917   assert( pDbFd );
47918 
47919   /* In the amalgamation, the os_unix.c and os_win.c source files come before
47920   ** this source file.  Verify that the #defines of the locking byte offsets
47921   ** in os_unix.c and os_win.c agree with the WALINDEX_LOCK_OFFSET value.
47922   */
47923 #ifdef WIN_SHM_BASE
47924   assert( WIN_SHM_BASE==WALINDEX_LOCK_OFFSET );
47925 #endif
47926 #ifdef UNIX_SHM_BASE
47927   assert( UNIX_SHM_BASE==WALINDEX_LOCK_OFFSET );
47928 #endif
47929 
47930 
47931   /* Allocate an instance of struct Wal to return. */
47932   *ppWal = 0;
47933   pRet = (Wal*)sqlite3MallocZero(sizeof(Wal) + pVfs->szOsFile);
47934   if( !pRet ){
47935     return SQLITE_NOMEM;
47936   }
47937 
47938   pRet->pVfs = pVfs;
47939   pRet->pWalFd = (sqlite3_file *)&pRet[1];
47940   pRet->pDbFd = pDbFd;
47941   pRet->readLock = -1;
47942   pRet->mxWalSize = mxWalSize;
47943   pRet->zWalName = zWalName;
47944   pRet->syncHeader = 1;
47945   pRet->padToSectorBoundary = 1;
47946   pRet->exclusiveMode = (bNoShm ? WAL_HEAPMEMORY_MODE: WAL_NORMAL_MODE);
47947 
47948   /* Open file handle on the write-ahead log file. */
47949   flags = (SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|SQLITE_OPEN_WAL);
47950   rc = sqlite3OsOpen(pVfs, zWalName, pRet->pWalFd, flags, &flags);
47951   if( rc==SQLITE_OK && flags&SQLITE_OPEN_READONLY ){
47952     pRet->readOnly = WAL_RDONLY;
47953   }
47954 
47955   if( rc!=SQLITE_OK ){
47956     walIndexClose(pRet, 0);
47957     sqlite3OsClose(pRet->pWalFd);
47958     sqlite3_free(pRet);
47959   }else{
47960     int iDC = sqlite3OsDeviceCharacteristics(pRet->pWalFd);
47961     if( iDC & SQLITE_IOCAP_SEQUENTIAL ){ pRet->syncHeader = 0; }
47962     if( iDC & SQLITE_IOCAP_POWERSAFE_OVERWRITE ){
47963       pRet->padToSectorBoundary = 0;
47964     }
47965     *ppWal = pRet;
47966     WALTRACE(("WAL%d: opened\n", pRet));
47967   }
47968   return rc;
47969 }
47970 
47971 /*
47972 ** Change the size to which the WAL file is trucated on each reset.
47973 */
47974 SQLITE_PRIVATE void sqlite3WalLimit(Wal *pWal, i64 iLimit){
47975   if( pWal ) pWal->mxWalSize = iLimit;
47976 }
47977 
47978 /*
47979 ** Find the smallest page number out of all pages held in the WAL that
47980 ** has not been returned by any prior invocation of this method on the
47981 ** same WalIterator object.   Write into *piFrame the frame index where
47982 ** that page was last written into the WAL.  Write into *piPage the page
47983 ** number.
47984 **
47985 ** Return 0 on success.  If there are no pages in the WAL with a page
47986 ** number larger than *piPage, then return 1.
47987 */
47988 static int walIteratorNext(
47989   WalIterator *p,               /* Iterator */
47990   u32 *piPage,                  /* OUT: The page number of the next page */
47991   u32 *piFrame                  /* OUT: Wal frame index of next page */
47992 ){
47993   u32 iMin;                     /* Result pgno must be greater than iMin */
47994   u32 iRet = 0xFFFFFFFF;        /* 0xffffffff is never a valid page number */
47995   int i;                        /* For looping through segments */
47996 
47997   iMin = p->iPrior;
47998   assert( iMin<0xffffffff );
47999   for(i=p->nSegment-1; i>=0; i--){
48000     struct WalSegment *pSegment = &p->aSegment[i];
48001     while( pSegment->iNext<pSegment->nEntry ){
48002       u32 iPg = pSegment->aPgno[pSegment->aIndex[pSegment->iNext]];
48003       if( iPg>iMin ){
48004         if( iPg<iRet ){
48005           iRet = iPg;
48006           *piFrame = pSegment->iZero + pSegment->aIndex[pSegment->iNext];
48007         }
48008         break;
48009       }
48010       pSegment->iNext++;
48011     }
48012   }
48013 
48014   *piPage = p->iPrior = iRet;
48015   return (iRet==0xFFFFFFFF);
48016 }
48017 
48018 /*
48019 ** This function merges two sorted lists into a single sorted list.
48020 **
48021 ** aLeft[] and aRight[] are arrays of indices.  The sort key is
48022 ** aContent[aLeft[]] and aContent[aRight[]].  Upon entry, the following
48023 ** is guaranteed for all J<K:
48024 **
48025 **        aContent[aLeft[J]] < aContent[aLeft[K]]
48026 **        aContent[aRight[J]] < aContent[aRight[K]]
48027 **
48028 ** This routine overwrites aRight[] with a new (probably longer) sequence
48029 ** of indices such that the aRight[] contains every index that appears in
48030 ** either aLeft[] or the old aRight[] and such that the second condition
48031 ** above is still met.
48032 **
48033 ** The aContent[aLeft[X]] values will be unique for all X.  And the
48034 ** aContent[aRight[X]] values will be unique too.  But there might be
48035 ** one or more combinations of X and Y such that
48036 **
48037 **      aLeft[X]!=aRight[Y]  &&  aContent[aLeft[X]] == aContent[aRight[Y]]
48038 **
48039 ** When that happens, omit the aLeft[X] and use the aRight[Y] index.
48040 */
48041 static void walMerge(
48042   const u32 *aContent,            /* Pages in wal - keys for the sort */
48043   ht_slot *aLeft,                 /* IN: Left hand input list */
48044   int nLeft,                      /* IN: Elements in array *paLeft */
48045   ht_slot **paRight,              /* IN/OUT: Right hand input list */
48046   int *pnRight,                   /* IN/OUT: Elements in *paRight */
48047   ht_slot *aTmp                   /* Temporary buffer */
48048 ){
48049   int iLeft = 0;                  /* Current index in aLeft */
48050   int iRight = 0;                 /* Current index in aRight */
48051   int iOut = 0;                   /* Current index in output buffer */
48052   int nRight = *pnRight;
48053   ht_slot *aRight = *paRight;
48054 
48055   assert( nLeft>0 && nRight>0 );
48056   while( iRight<nRight || iLeft<nLeft ){
48057     ht_slot logpage;
48058     Pgno dbpage;
48059 
48060     if( (iLeft<nLeft)
48061      && (iRight>=nRight || aContent[aLeft[iLeft]]<aContent[aRight[iRight]])
48062     ){
48063       logpage = aLeft[iLeft++];
48064     }else{
48065       logpage = aRight[iRight++];
48066     }
48067     dbpage = aContent[logpage];
48068 
48069     aTmp[iOut++] = logpage;
48070     if( iLeft<nLeft && aContent[aLeft[iLeft]]==dbpage ) iLeft++;
48071 
48072     assert( iLeft>=nLeft || aContent[aLeft[iLeft]]>dbpage );
48073     assert( iRight>=nRight || aContent[aRight[iRight]]>dbpage );
48074   }
48075 
48076   *paRight = aLeft;
48077   *pnRight = iOut;
48078   memcpy(aLeft, aTmp, sizeof(aTmp[0])*iOut);
48079 }
48080 
48081 /*
48082 ** Sort the elements in list aList using aContent[] as the sort key.
48083 ** Remove elements with duplicate keys, preferring to keep the
48084 ** larger aList[] values.
48085 **
48086 ** The aList[] entries are indices into aContent[].  The values in
48087 ** aList[] are to be sorted so that for all J<K:
48088 **
48089 **      aContent[aList[J]] < aContent[aList[K]]
48090 **
48091 ** For any X and Y such that
48092 **
48093 **      aContent[aList[X]] == aContent[aList[Y]]
48094 **
48095 ** Keep the larger of the two values aList[X] and aList[Y] and discard
48096 ** the smaller.
48097 */
48098 static void walMergesort(
48099   const u32 *aContent,            /* Pages in wal */
48100   ht_slot *aBuffer,               /* Buffer of at least *pnList items to use */
48101   ht_slot *aList,                 /* IN/OUT: List to sort */
48102   int *pnList                     /* IN/OUT: Number of elements in aList[] */
48103 ){
48104   struct Sublist {
48105     int nList;                    /* Number of elements in aList */
48106     ht_slot *aList;               /* Pointer to sub-list content */
48107   };
48108 
48109   const int nList = *pnList;      /* Size of input list */
48110   int nMerge = 0;                 /* Number of elements in list aMerge */
48111   ht_slot *aMerge = 0;            /* List to be merged */
48112   int iList;                      /* Index into input list */
48113   int iSub = 0;                   /* Index into aSub array */
48114   struct Sublist aSub[13];        /* Array of sub-lists */
48115 
48116   memset(aSub, 0, sizeof(aSub));
48117   assert( nList<=HASHTABLE_NPAGE && nList>0 );
48118   assert( HASHTABLE_NPAGE==(1<<(ArraySize(aSub)-1)) );
48119 
48120   for(iList=0; iList<nList; iList++){
48121     nMerge = 1;
48122     aMerge = &aList[iList];
48123     for(iSub=0; iList & (1<<iSub); iSub++){
48124       struct Sublist *p = &aSub[iSub];
48125       assert( p->aList && p->nList<=(1<<iSub) );
48126       assert( p->aList==&aList[iList&~((2<<iSub)-1)] );
48127       walMerge(aContent, p->aList, p->nList, &aMerge, &nMerge, aBuffer);
48128     }
48129     aSub[iSub].aList = aMerge;
48130     aSub[iSub].nList = nMerge;
48131   }
48132 
48133   for(iSub++; iSub<ArraySize(aSub); iSub++){
48134     if( nList & (1<<iSub) ){
48135       struct Sublist *p = &aSub[iSub];
48136       assert( p->nList<=(1<<iSub) );
48137       assert( p->aList==&aList[nList&~((2<<iSub)-1)] );
48138       walMerge(aContent, p->aList, p->nList, &aMerge, &nMerge, aBuffer);
48139     }
48140   }
48141   assert( aMerge==aList );
48142   *pnList = nMerge;
48143 
48144 #ifdef SQLITE_DEBUG
48145   {
48146     int i;
48147     for(i=1; i<*pnList; i++){
48148       assert( aContent[aList[i]] > aContent[aList[i-1]] );
48149     }
48150   }
48151 #endif
48152 }
48153 
48154 /*
48155 ** Free an iterator allocated by walIteratorInit().
48156 */
48157 static void walIteratorFree(WalIterator *p){
48158   sqlite3ScratchFree(p);
48159 }
48160 
48161 /*
48162 ** Construct a WalInterator object that can be used to loop over all
48163 ** pages in the WAL in ascending order. The caller must hold the checkpoint
48164 ** lock.
48165 **
48166 ** On success, make *pp point to the newly allocated WalInterator object
48167 ** return SQLITE_OK. Otherwise, return an error code. If this routine
48168 ** returns an error, the value of *pp is undefined.
48169 **
48170 ** The calling routine should invoke walIteratorFree() to destroy the
48171 ** WalIterator object when it has finished with it.
48172 */
48173 static int walIteratorInit(Wal *pWal, WalIterator **pp){
48174   WalIterator *p;                 /* Return value */
48175   int nSegment;                   /* Number of segments to merge */
48176   u32 iLast;                      /* Last frame in log */
48177   int nByte;                      /* Number of bytes to allocate */
48178   int i;                          /* Iterator variable */
48179   ht_slot *aTmp;                  /* Temp space used by merge-sort */
48180   int rc = SQLITE_OK;             /* Return Code */
48181 
48182   /* This routine only runs while holding the checkpoint lock. And
48183   ** it only runs if there is actually content in the log (mxFrame>0).
48184   */
48185   assert( pWal->ckptLock && pWal->hdr.mxFrame>0 );
48186   iLast = pWal->hdr.mxFrame;
48187 
48188   /* Allocate space for the WalIterator object. */
48189   nSegment = walFramePage(iLast) + 1;
48190   nByte = sizeof(WalIterator)
48191         + (nSegment-1)*sizeof(struct WalSegment)
48192         + iLast*sizeof(ht_slot);
48193   p = (WalIterator *)sqlite3ScratchMalloc(nByte);
48194   if( !p ){
48195     return SQLITE_NOMEM;
48196   }
48197   memset(p, 0, nByte);
48198   p->nSegment = nSegment;
48199 
48200   /* Allocate temporary space used by the merge-sort routine. This block
48201   ** of memory will be freed before this function returns.
48202   */
48203   aTmp = (ht_slot *)sqlite3ScratchMalloc(
48204       sizeof(ht_slot) * (iLast>HASHTABLE_NPAGE?HASHTABLE_NPAGE:iLast)
48205   );
48206   if( !aTmp ){
48207     rc = SQLITE_NOMEM;
48208   }
48209 
48210   for(i=0; rc==SQLITE_OK && i<nSegment; i++){
48211     volatile ht_slot *aHash;
48212     u32 iZero;
48213     volatile u32 *aPgno;
48214 
48215     rc = walHashGet(pWal, i, &aHash, &aPgno, &iZero);
48216     if( rc==SQLITE_OK ){
48217       int j;                      /* Counter variable */
48218       int nEntry;                 /* Number of entries in this segment */
48219       ht_slot *aIndex;            /* Sorted index for this segment */
48220 
48221       aPgno++;
48222       if( (i+1)==nSegment ){
48223         nEntry = (int)(iLast - iZero);
48224       }else{
48225         nEntry = (int)((u32*)aHash - (u32*)aPgno);
48226       }
48227       aIndex = &((ht_slot *)&p->aSegment[p->nSegment])[iZero];
48228       iZero++;
48229 
48230       for(j=0; j<nEntry; j++){
48231         aIndex[j] = (ht_slot)j;
48232       }
48233       walMergesort((u32 *)aPgno, aTmp, aIndex, &nEntry);
48234       p->aSegment[i].iZero = iZero;
48235       p->aSegment[i].nEntry = nEntry;
48236       p->aSegment[i].aIndex = aIndex;
48237       p->aSegment[i].aPgno = (u32 *)aPgno;
48238     }
48239   }
48240   sqlite3ScratchFree(aTmp);
48241 
48242   if( rc!=SQLITE_OK ){
48243     walIteratorFree(p);
48244   }
48245   *pp = p;
48246   return rc;
48247 }
48248 
48249 /*
48250 ** Attempt to obtain the exclusive WAL lock defined by parameters lockIdx and
48251 ** n. If the attempt fails and parameter xBusy is not NULL, then it is a
48252 ** busy-handler function. Invoke it and retry the lock until either the
48253 ** lock is successfully obtained or the busy-handler returns 0.
48254 */
48255 static int walBusyLock(
48256   Wal *pWal,                      /* WAL connection */
48257   int (*xBusy)(void*),            /* Function to call when busy */
48258   void *pBusyArg,                 /* Context argument for xBusyHandler */
48259   int lockIdx,                    /* Offset of first byte to lock */
48260   int n                           /* Number of bytes to lock */
48261 ){
48262   int rc;
48263   do {
48264     rc = walLockExclusive(pWal, lockIdx, n);
48265   }while( xBusy && rc==SQLITE_BUSY && xBusy(pBusyArg) );
48266   return rc;
48267 }
48268 
48269 /*
48270 ** The cache of the wal-index header must be valid to call this function.
48271 ** Return the page-size in bytes used by the database.
48272 */
48273 static int walPagesize(Wal *pWal){
48274   return (pWal->hdr.szPage&0xfe00) + ((pWal->hdr.szPage&0x0001)<<16);
48275 }
48276 
48277 /*
48278 ** Copy as much content as we can from the WAL back into the database file
48279 ** in response to an sqlite3_wal_checkpoint() request or the equivalent.
48280 **
48281 ** The amount of information copies from WAL to database might be limited
48282 ** by active readers.  This routine will never overwrite a database page
48283 ** that a concurrent reader might be using.
48284 **
48285 ** All I/O barrier operations (a.k.a fsyncs) occur in this routine when
48286 ** SQLite is in WAL-mode in synchronous=NORMAL.  That means that if
48287 ** checkpoints are always run by a background thread or background
48288 ** process, foreground threads will never block on a lengthy fsync call.
48289 **
48290 ** Fsync is called on the WAL before writing content out of the WAL and
48291 ** into the database.  This ensures that if the new content is persistent
48292 ** in the WAL and can be recovered following a power-loss or hard reset.
48293 **
48294 ** Fsync is also called on the database file if (and only if) the entire
48295 ** WAL content is copied into the database file.  This second fsync makes
48296 ** it safe to delete the WAL since the new content will persist in the
48297 ** database file.
48298 **
48299 ** This routine uses and updates the nBackfill field of the wal-index header.
48300 ** This is the only routine tha will increase the value of nBackfill.
48301 ** (A WAL reset or recovery will revert nBackfill to zero, but not increase
48302 ** its value.)
48303 **
48304 ** The caller must be holding sufficient locks to ensure that no other
48305 ** checkpoint is running (in any other thread or process) at the same
48306 ** time.
48307 */
48308 static int walCheckpoint(
48309   Wal *pWal,                      /* Wal connection */
48310   int eMode,                      /* One of PASSIVE, FULL or RESTART */
48311   int (*xBusyCall)(void*),        /* Function to call when busy */
48312   void *pBusyArg,                 /* Context argument for xBusyHandler */
48313   int sync_flags,                 /* Flags for OsSync() (or 0) */
48314   u8 *zBuf                        /* Temporary buffer to use */
48315 ){
48316   int rc;                         /* Return code */
48317   int szPage;                     /* Database page-size */
48318   WalIterator *pIter = 0;         /* Wal iterator context */
48319   u32 iDbpage = 0;                /* Next database page to write */
48320   u32 iFrame = 0;                 /* Wal frame containing data for iDbpage */
48321   u32 mxSafeFrame;                /* Max frame that can be backfilled */
48322   u32 mxPage;                     /* Max database page to write */
48323   int i;                          /* Loop counter */
48324   volatile WalCkptInfo *pInfo;    /* The checkpoint status information */
48325   int (*xBusy)(void*) = 0;        /* Function to call when waiting for locks */
48326 
48327   szPage = walPagesize(pWal);
48328   testcase( szPage<=32768 );
48329   testcase( szPage>=65536 );
48330   pInfo = walCkptInfo(pWal);
48331   if( pInfo->nBackfill>=pWal->hdr.mxFrame ) return SQLITE_OK;
48332 
48333   /* Allocate the iterator */
48334   rc = walIteratorInit(pWal, &pIter);
48335   if( rc!=SQLITE_OK ){
48336     return rc;
48337   }
48338   assert( pIter );
48339 
48340   if( eMode!=SQLITE_CHECKPOINT_PASSIVE ) xBusy = xBusyCall;
48341 
48342   /* Compute in mxSafeFrame the index of the last frame of the WAL that is
48343   ** safe to write into the database.  Frames beyond mxSafeFrame might
48344   ** overwrite database pages that are in use by active readers and thus
48345   ** cannot be backfilled from the WAL.
48346   */
48347   mxSafeFrame = pWal->hdr.mxFrame;
48348   mxPage = pWal->hdr.nPage;
48349   for(i=1; i<WAL_NREADER; i++){
48350     u32 y = pInfo->aReadMark[i];
48351     if( mxSafeFrame>y ){
48352       assert( y<=pWal->hdr.mxFrame );
48353       rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(i), 1);
48354       if( rc==SQLITE_OK ){
48355         pInfo->aReadMark[i] = (i==1 ? mxSafeFrame : READMARK_NOT_USED);
48356         walUnlockExclusive(pWal, WAL_READ_LOCK(i), 1);
48357       }else if( rc==SQLITE_BUSY ){
48358         mxSafeFrame = y;
48359         xBusy = 0;
48360       }else{
48361         goto walcheckpoint_out;
48362       }
48363     }
48364   }
48365 
48366   if( pInfo->nBackfill<mxSafeFrame
48367    && (rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(0), 1))==SQLITE_OK
48368   ){
48369     i64 nSize;                    /* Current size of database file */
48370     u32 nBackfill = pInfo->nBackfill;
48371 
48372     /* Sync the WAL to disk */
48373     if( sync_flags ){
48374       rc = sqlite3OsSync(pWal->pWalFd, sync_flags);
48375     }
48376 
48377     /* If the database may grow as a result of this checkpoint, hint
48378     ** about the eventual size of the db file to the VFS layer.
48379     */
48380     if( rc==SQLITE_OK ){
48381       i64 nReq = ((i64)mxPage * szPage);
48382       rc = sqlite3OsFileSize(pWal->pDbFd, &nSize);
48383       if( rc==SQLITE_OK && nSize<nReq ){
48384         sqlite3OsFileControlHint(pWal->pDbFd, SQLITE_FCNTL_SIZE_HINT, &nReq);
48385       }
48386     }
48387 
48388 
48389     /* Iterate through the contents of the WAL, copying data to the db file. */
48390     while( rc==SQLITE_OK && 0==walIteratorNext(pIter, &iDbpage, &iFrame) ){
48391       i64 iOffset;
48392       assert( walFramePgno(pWal, iFrame)==iDbpage );
48393       if( iFrame<=nBackfill || iFrame>mxSafeFrame || iDbpage>mxPage ) continue;
48394       iOffset = walFrameOffset(iFrame, szPage) + WAL_FRAME_HDRSIZE;
48395       /* testcase( IS_BIG_INT(iOffset) ); // requires a 4GiB WAL file */
48396       rc = sqlite3OsRead(pWal->pWalFd, zBuf, szPage, iOffset);
48397       if( rc!=SQLITE_OK ) break;
48398       iOffset = (iDbpage-1)*(i64)szPage;
48399       testcase( IS_BIG_INT(iOffset) );
48400       rc = sqlite3OsWrite(pWal->pDbFd, zBuf, szPage, iOffset);
48401       if( rc!=SQLITE_OK ) break;
48402     }
48403 
48404     /* If work was actually accomplished... */
48405     if( rc==SQLITE_OK ){
48406       if( mxSafeFrame==walIndexHdr(pWal)->mxFrame ){
48407         i64 szDb = pWal->hdr.nPage*(i64)szPage;
48408         testcase( IS_BIG_INT(szDb) );
48409         rc = sqlite3OsTruncate(pWal->pDbFd, szDb);
48410         if( rc==SQLITE_OK && sync_flags ){
48411           rc = sqlite3OsSync(pWal->pDbFd, sync_flags);
48412         }
48413       }
48414       if( rc==SQLITE_OK ){
48415         pInfo->nBackfill = mxSafeFrame;
48416       }
48417     }
48418 
48419     /* Release the reader lock held while backfilling */
48420     walUnlockExclusive(pWal, WAL_READ_LOCK(0), 1);
48421   }
48422 
48423   if( rc==SQLITE_BUSY ){
48424     /* Reset the return code so as not to report a checkpoint failure
48425     ** just because there are active readers.  */
48426     rc = SQLITE_OK;
48427   }
48428 
48429   /* If this is an SQLITE_CHECKPOINT_RESTART operation, and the entire wal
48430   ** file has been copied into the database file, then block until all
48431   ** readers have finished using the wal file. This ensures that the next
48432   ** process to write to the database restarts the wal file.
48433   */
48434   if( rc==SQLITE_OK && eMode!=SQLITE_CHECKPOINT_PASSIVE ){
48435     assert( pWal->writeLock );
48436     if( pInfo->nBackfill<pWal->hdr.mxFrame ){
48437       rc = SQLITE_BUSY;
48438     }else if( eMode==SQLITE_CHECKPOINT_RESTART ){
48439       assert( mxSafeFrame==pWal->hdr.mxFrame );
48440       rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(1), WAL_NREADER-1);
48441       if( rc==SQLITE_OK ){
48442         walUnlockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
48443       }
48444     }
48445   }
48446 
48447  walcheckpoint_out:
48448   walIteratorFree(pIter);
48449   return rc;
48450 }
48451 
48452 /*
48453 ** If the WAL file is currently larger than nMax bytes in size, truncate
48454 ** it to exactly nMax bytes. If an error occurs while doing so, ignore it.
48455 */
48456 static void walLimitSize(Wal *pWal, i64 nMax){
48457   i64 sz;
48458   int rx;
48459   sqlite3BeginBenignMalloc();
48460   rx = sqlite3OsFileSize(pWal->pWalFd, &sz);
48461   if( rx==SQLITE_OK && (sz > nMax ) ){
48462     rx = sqlite3OsTruncate(pWal->pWalFd, nMax);
48463   }
48464   sqlite3EndBenignMalloc();
48465   if( rx ){
48466     sqlite3_log(rx, "cannot limit WAL size: %s", pWal->zWalName);
48467   }
48468 }
48469 
48470 /*
48471 ** Close a connection to a log file.
48472 */
48473 SQLITE_PRIVATE int sqlite3WalClose(
48474   Wal *pWal,                      /* Wal to close */
48475   int sync_flags,                 /* Flags to pass to OsSync() (or 0) */
48476   int nBuf,
48477   u8 *zBuf                        /* Buffer of at least nBuf bytes */
48478 ){
48479   int rc = SQLITE_OK;
48480   if( pWal ){
48481     int isDelete = 0;             /* True to unlink wal and wal-index files */
48482 
48483     /* If an EXCLUSIVE lock can be obtained on the database file (using the
48484     ** ordinary, rollback-mode locking methods, this guarantees that the
48485     ** connection associated with this log file is the only connection to
48486     ** the database. In this case checkpoint the database and unlink both
48487     ** the wal and wal-index files.
48488     **
48489     ** The EXCLUSIVE lock is not released before returning.
48490     */
48491     rc = sqlite3OsLock(pWal->pDbFd, SQLITE_LOCK_EXCLUSIVE);
48492     if( rc==SQLITE_OK ){
48493       if( pWal->exclusiveMode==WAL_NORMAL_MODE ){
48494         pWal->exclusiveMode = WAL_EXCLUSIVE_MODE;
48495       }
48496       rc = sqlite3WalCheckpoint(
48497           pWal, SQLITE_CHECKPOINT_PASSIVE, 0, 0, sync_flags, nBuf, zBuf, 0, 0
48498       );
48499       if( rc==SQLITE_OK ){
48500         int bPersist = -1;
48501         sqlite3OsFileControlHint(
48502             pWal->pDbFd, SQLITE_FCNTL_PERSIST_WAL, &bPersist
48503         );
48504         if( bPersist!=1 ){
48505           /* Try to delete the WAL file if the checkpoint completed and
48506           ** fsyned (rc==SQLITE_OK) and if we are not in persistent-wal
48507           ** mode (!bPersist) */
48508           isDelete = 1;
48509         }else if( pWal->mxWalSize>=0 ){
48510           /* Try to truncate the WAL file to zero bytes if the checkpoint
48511           ** completed and fsynced (rc==SQLITE_OK) and we are in persistent
48512           ** WAL mode (bPersist) and if the PRAGMA journal_size_limit is a
48513           ** non-negative value (pWal->mxWalSize>=0).  Note that we truncate
48514           ** to zero bytes as truncating to the journal_size_limit might
48515           ** leave a corrupt WAL file on disk. */
48516           walLimitSize(pWal, 0);
48517         }
48518       }
48519     }
48520 
48521     walIndexClose(pWal, isDelete);
48522     sqlite3OsClose(pWal->pWalFd);
48523     if( isDelete ){
48524       sqlite3BeginBenignMalloc();
48525       sqlite3OsDelete(pWal->pVfs, pWal->zWalName, 0);
48526       sqlite3EndBenignMalloc();
48527     }
48528     WALTRACE(("WAL%p: closed\n", pWal));
48529     sqlite3_free((void *)pWal->apWiData);
48530     sqlite3_free(pWal);
48531   }
48532   return rc;
48533 }
48534 
48535 /*
48536 ** Try to read the wal-index header.  Return 0 on success and 1 if
48537 ** there is a problem.
48538 **
48539 ** The wal-index is in shared memory.  Another thread or process might
48540 ** be writing the header at the same time this procedure is trying to
48541 ** read it, which might result in inconsistency.  A dirty read is detected
48542 ** by verifying that both copies of the header are the same and also by
48543 ** a checksum on the header.
48544 **
48545 ** If and only if the read is consistent and the header is different from
48546 ** pWal->hdr, then pWal->hdr is updated to the content of the new header
48547 ** and *pChanged is set to 1.
48548 **
48549 ** If the checksum cannot be verified return non-zero. If the header
48550 ** is read successfully and the checksum verified, return zero.
48551 */
48552 static int walIndexTryHdr(Wal *pWal, int *pChanged){
48553   u32 aCksum[2];                  /* Checksum on the header content */
48554   WalIndexHdr h1, h2;             /* Two copies of the header content */
48555   WalIndexHdr volatile *aHdr;     /* Header in shared memory */
48556 
48557   /* The first page of the wal-index must be mapped at this point. */
48558   assert( pWal->nWiData>0 && pWal->apWiData[0] );
48559 
48560   /* Read the header. This might happen concurrently with a write to the
48561   ** same area of shared memory on a different CPU in a SMP,
48562   ** meaning it is possible that an inconsistent snapshot is read
48563   ** from the file. If this happens, return non-zero.
48564   **
48565   ** There are two copies of the header at the beginning of the wal-index.
48566   ** When reading, read [0] first then [1].  Writes are in the reverse order.
48567   ** Memory barriers are used to prevent the compiler or the hardware from
48568   ** reordering the reads and writes.
48569   */
48570   aHdr = walIndexHdr(pWal);
48571   memcpy(&h1, (void *)&aHdr[0], sizeof(h1));
48572   walShmBarrier(pWal);
48573   memcpy(&h2, (void *)&aHdr[1], sizeof(h2));
48574 
48575   if( memcmp(&h1, &h2, sizeof(h1))!=0 ){
48576     return 1;   /* Dirty read */
48577   }
48578   if( h1.isInit==0 ){
48579     return 1;   /* Malformed header - probably all zeros */
48580   }
48581   walChecksumBytes(1, (u8*)&h1, sizeof(h1)-sizeof(h1.aCksum), 0, aCksum);
48582   if( aCksum[0]!=h1.aCksum[0] || aCksum[1]!=h1.aCksum[1] ){
48583     return 1;   /* Checksum does not match */
48584   }
48585 
48586   if( memcmp(&pWal->hdr, &h1, sizeof(WalIndexHdr)) ){
48587     *pChanged = 1;
48588     memcpy(&pWal->hdr, &h1, sizeof(WalIndexHdr));
48589     pWal->szPage = (pWal->hdr.szPage&0xfe00) + ((pWal->hdr.szPage&0x0001)<<16);
48590     testcase( pWal->szPage<=32768 );
48591     testcase( pWal->szPage>=65536 );
48592   }
48593 
48594   /* The header was successfully read. Return zero. */
48595   return 0;
48596 }
48597 
48598 /*
48599 ** Read the wal-index header from the wal-index and into pWal->hdr.
48600 ** If the wal-header appears to be corrupt, try to reconstruct the
48601 ** wal-index from the WAL before returning.
48602 **
48603 ** Set *pChanged to 1 if the wal-index header value in pWal->hdr is
48604 ** changed by this opertion.  If pWal->hdr is unchanged, set *pChanged
48605 ** to 0.
48606 **
48607 ** If the wal-index header is successfully read, return SQLITE_OK.
48608 ** Otherwise an SQLite error code.
48609 */
48610 static int walIndexReadHdr(Wal *pWal, int *pChanged){
48611   int rc;                         /* Return code */
48612   int badHdr;                     /* True if a header read failed */
48613   volatile u32 *page0;            /* Chunk of wal-index containing header */
48614 
48615   /* Ensure that page 0 of the wal-index (the page that contains the
48616   ** wal-index header) is mapped. Return early if an error occurs here.
48617   */
48618   assert( pChanged );
48619   rc = walIndexPage(pWal, 0, &page0);
48620   if( rc!=SQLITE_OK ){
48621     return rc;
48622   };
48623   assert( page0 || pWal->writeLock==0 );
48624 
48625   /* If the first page of the wal-index has been mapped, try to read the
48626   ** wal-index header immediately, without holding any lock. This usually
48627   ** works, but may fail if the wal-index header is corrupt or currently
48628   ** being modified by another thread or process.
48629   */
48630   badHdr = (page0 ? walIndexTryHdr(pWal, pChanged) : 1);
48631 
48632   /* If the first attempt failed, it might have been due to a race
48633   ** with a writer.  So get a WRITE lock and try again.
48634   */
48635   assert( badHdr==0 || pWal->writeLock==0 );
48636   if( badHdr ){
48637     if( pWal->readOnly & WAL_SHM_RDONLY ){
48638       if( SQLITE_OK==(rc = walLockShared(pWal, WAL_WRITE_LOCK)) ){
48639         walUnlockShared(pWal, WAL_WRITE_LOCK);
48640         rc = SQLITE_READONLY_RECOVERY;
48641       }
48642     }else if( SQLITE_OK==(rc = walLockExclusive(pWal, WAL_WRITE_LOCK, 1)) ){
48643       pWal->writeLock = 1;
48644       if( SQLITE_OK==(rc = walIndexPage(pWal, 0, &page0)) ){
48645         badHdr = walIndexTryHdr(pWal, pChanged);
48646         if( badHdr ){
48647           /* If the wal-index header is still malformed even while holding
48648           ** a WRITE lock, it can only mean that the header is corrupted and
48649           ** needs to be reconstructed.  So run recovery to do exactly that.
48650           */
48651           rc = walIndexRecover(pWal);
48652           *pChanged = 1;
48653         }
48654       }
48655       pWal->writeLock = 0;
48656       walUnlockExclusive(pWal, WAL_WRITE_LOCK, 1);
48657     }
48658   }
48659 
48660   /* If the header is read successfully, check the version number to make
48661   ** sure the wal-index was not constructed with some future format that
48662   ** this version of SQLite cannot understand.
48663   */
48664   if( badHdr==0 && pWal->hdr.iVersion!=WALINDEX_MAX_VERSION ){
48665     rc = SQLITE_CANTOPEN_BKPT;
48666   }
48667 
48668   return rc;
48669 }
48670 
48671 /*
48672 ** This is the value that walTryBeginRead returns when it needs to
48673 ** be retried.
48674 */
48675 #define WAL_RETRY  (-1)
48676 
48677 /*
48678 ** Attempt to start a read transaction.  This might fail due to a race or
48679 ** other transient condition.  When that happens, it returns WAL_RETRY to
48680 ** indicate to the caller that it is safe to retry immediately.
48681 **
48682 ** On success return SQLITE_OK.  On a permanent failure (such an
48683 ** I/O error or an SQLITE_BUSY because another process is running
48684 ** recovery) return a positive error code.
48685 **
48686 ** The useWal parameter is true to force the use of the WAL and disable
48687 ** the case where the WAL is bypassed because it has been completely
48688 ** checkpointed.  If useWal==0 then this routine calls walIndexReadHdr()
48689 ** to make a copy of the wal-index header into pWal->hdr.  If the
48690 ** wal-index header has changed, *pChanged is set to 1 (as an indication
48691 ** to the caller that the local paget cache is obsolete and needs to be
48692 ** flushed.)  When useWal==1, the wal-index header is assumed to already
48693 ** be loaded and the pChanged parameter is unused.
48694 **
48695 ** The caller must set the cnt parameter to the number of prior calls to
48696 ** this routine during the current read attempt that returned WAL_RETRY.
48697 ** This routine will start taking more aggressive measures to clear the
48698 ** race conditions after multiple WAL_RETRY returns, and after an excessive
48699 ** number of errors will ultimately return SQLITE_PROTOCOL.  The
48700 ** SQLITE_PROTOCOL return indicates that some other process has gone rogue
48701 ** and is not honoring the locking protocol.  There is a vanishingly small
48702 ** chance that SQLITE_PROTOCOL could be returned because of a run of really
48703 ** bad luck when there is lots of contention for the wal-index, but that
48704 ** possibility is so small that it can be safely neglected, we believe.
48705 **
48706 ** On success, this routine obtains a read lock on
48707 ** WAL_READ_LOCK(pWal->readLock).  The pWal->readLock integer is
48708 ** in the range 0 <= pWal->readLock < WAL_NREADER.  If pWal->readLock==(-1)
48709 ** that means the Wal does not hold any read lock.  The reader must not
48710 ** access any database page that is modified by a WAL frame up to and
48711 ** including frame number aReadMark[pWal->readLock].  The reader will
48712 ** use WAL frames up to and including pWal->hdr.mxFrame if pWal->readLock>0
48713 ** Or if pWal->readLock==0, then the reader will ignore the WAL
48714 ** completely and get all content directly from the database file.
48715 ** If the useWal parameter is 1 then the WAL will never be ignored and
48716 ** this routine will always set pWal->readLock>0 on success.
48717 ** When the read transaction is completed, the caller must release the
48718 ** lock on WAL_READ_LOCK(pWal->readLock) and set pWal->readLock to -1.
48719 **
48720 ** This routine uses the nBackfill and aReadMark[] fields of the header
48721 ** to select a particular WAL_READ_LOCK() that strives to let the
48722 ** checkpoint process do as much work as possible.  This routine might
48723 ** update values of the aReadMark[] array in the header, but if it does
48724 ** so it takes care to hold an exclusive lock on the corresponding
48725 ** WAL_READ_LOCK() while changing values.
48726 */
48727 static int walTryBeginRead(Wal *pWal, int *pChanged, int useWal, int cnt){
48728   volatile WalCkptInfo *pInfo;    /* Checkpoint information in wal-index */
48729   u32 mxReadMark;                 /* Largest aReadMark[] value */
48730   int mxI;                        /* Index of largest aReadMark[] value */
48731   int i;                          /* Loop counter */
48732   int rc = SQLITE_OK;             /* Return code  */
48733 
48734   assert( pWal->readLock<0 );     /* Not currently locked */
48735 
48736   /* Take steps to avoid spinning forever if there is a protocol error.
48737   **
48738   ** Circumstances that cause a RETRY should only last for the briefest
48739   ** instances of time.  No I/O or other system calls are done while the
48740   ** locks are held, so the locks should not be held for very long. But
48741   ** if we are unlucky, another process that is holding a lock might get
48742   ** paged out or take a page-fault that is time-consuming to resolve,
48743   ** during the few nanoseconds that it is holding the lock.  In that case,
48744   ** it might take longer than normal for the lock to free.
48745   **
48746   ** After 5 RETRYs, we begin calling sqlite3OsSleep().  The first few
48747   ** calls to sqlite3OsSleep() have a delay of 1 microsecond.  Really this
48748   ** is more of a scheduler yield than an actual delay.  But on the 10th
48749   ** an subsequent retries, the delays start becoming longer and longer,
48750   ** so that on the 100th (and last) RETRY we delay for 21 milliseconds.
48751   ** The total delay time before giving up is less than 1 second.
48752   */
48753   if( cnt>5 ){
48754     int nDelay = 1;                      /* Pause time in microseconds */
48755     if( cnt>100 ){
48756       VVA_ONLY( pWal->lockError = 1; )
48757       return SQLITE_PROTOCOL;
48758     }
48759     if( cnt>=10 ) nDelay = (cnt-9)*238;  /* Max delay 21ms. Total delay 996ms */
48760     sqlite3OsSleep(pWal->pVfs, nDelay);
48761   }
48762 
48763   if( !useWal ){
48764     rc = walIndexReadHdr(pWal, pChanged);
48765     if( rc==SQLITE_BUSY ){
48766       /* If there is not a recovery running in another thread or process
48767       ** then convert BUSY errors to WAL_RETRY.  If recovery is known to
48768       ** be running, convert BUSY to BUSY_RECOVERY.  There is a race here
48769       ** which might cause WAL_RETRY to be returned even if BUSY_RECOVERY
48770       ** would be technically correct.  But the race is benign since with
48771       ** WAL_RETRY this routine will be called again and will probably be
48772       ** right on the second iteration.
48773       */
48774       if( pWal->apWiData[0]==0 ){
48775         /* This branch is taken when the xShmMap() method returns SQLITE_BUSY.
48776         ** We assume this is a transient condition, so return WAL_RETRY. The
48777         ** xShmMap() implementation used by the default unix and win32 VFS
48778         ** modules may return SQLITE_BUSY due to a race condition in the
48779         ** code that determines whether or not the shared-memory region
48780         ** must be zeroed before the requested page is returned.
48781         */
48782         rc = WAL_RETRY;
48783       }else if( SQLITE_OK==(rc = walLockShared(pWal, WAL_RECOVER_LOCK)) ){
48784         walUnlockShared(pWal, WAL_RECOVER_LOCK);
48785         rc = WAL_RETRY;
48786       }else if( rc==SQLITE_BUSY ){
48787         rc = SQLITE_BUSY_RECOVERY;
48788       }
48789     }
48790     if( rc!=SQLITE_OK ){
48791       return rc;
48792     }
48793   }
48794 
48795   pInfo = walCkptInfo(pWal);
48796   if( !useWal && pInfo->nBackfill==pWal->hdr.mxFrame ){
48797     /* The WAL has been completely backfilled (or it is empty).
48798     ** and can be safely ignored.
48799     */
48800     rc = walLockShared(pWal, WAL_READ_LOCK(0));
48801     walShmBarrier(pWal);
48802     if( rc==SQLITE_OK ){
48803       if( memcmp((void *)walIndexHdr(pWal), &pWal->hdr, sizeof(WalIndexHdr)) ){
48804         /* It is not safe to allow the reader to continue here if frames
48805         ** may have been appended to the log before READ_LOCK(0) was obtained.
48806         ** When holding READ_LOCK(0), the reader ignores the entire log file,
48807         ** which implies that the database file contains a trustworthy
48808         ** snapshoT. Since holding READ_LOCK(0) prevents a checkpoint from
48809         ** happening, this is usually correct.
48810         **
48811         ** However, if frames have been appended to the log (or if the log
48812         ** is wrapped and written for that matter) before the READ_LOCK(0)
48813         ** is obtained, that is not necessarily true. A checkpointer may
48814         ** have started to backfill the appended frames but crashed before
48815         ** it finished. Leaving a corrupt image in the database file.
48816         */
48817         walUnlockShared(pWal, WAL_READ_LOCK(0));
48818         return WAL_RETRY;
48819       }
48820       pWal->readLock = 0;
48821       return SQLITE_OK;
48822     }else if( rc!=SQLITE_BUSY ){
48823       return rc;
48824     }
48825   }
48826 
48827   /* If we get this far, it means that the reader will want to use
48828   ** the WAL to get at content from recent commits.  The job now is
48829   ** to select one of the aReadMark[] entries that is closest to
48830   ** but not exceeding pWal->hdr.mxFrame and lock that entry.
48831   */
48832   mxReadMark = 0;
48833   mxI = 0;
48834   for(i=1; i<WAL_NREADER; i++){
48835     u32 thisMark = pInfo->aReadMark[i];
48836     if( mxReadMark<=thisMark && thisMark<=pWal->hdr.mxFrame ){
48837       assert( thisMark!=READMARK_NOT_USED );
48838       mxReadMark = thisMark;
48839       mxI = i;
48840     }
48841   }
48842   /* There was once an "if" here. The extra "{" is to preserve indentation. */
48843   {
48844     if( (pWal->readOnly & WAL_SHM_RDONLY)==0
48845      && (mxReadMark<pWal->hdr.mxFrame || mxI==0)
48846     ){
48847       for(i=1; i<WAL_NREADER; i++){
48848         rc = walLockExclusive(pWal, WAL_READ_LOCK(i), 1);
48849         if( rc==SQLITE_OK ){
48850           mxReadMark = pInfo->aReadMark[i] = pWal->hdr.mxFrame;
48851           mxI = i;
48852           walUnlockExclusive(pWal, WAL_READ_LOCK(i), 1);
48853           break;
48854         }else if( rc!=SQLITE_BUSY ){
48855           return rc;
48856         }
48857       }
48858     }
48859     if( mxI==0 ){
48860       assert( rc==SQLITE_BUSY || (pWal->readOnly & WAL_SHM_RDONLY)!=0 );
48861       return rc==SQLITE_BUSY ? WAL_RETRY : SQLITE_READONLY_CANTLOCK;
48862     }
48863 
48864     rc = walLockShared(pWal, WAL_READ_LOCK(mxI));
48865     if( rc ){
48866       return rc==SQLITE_BUSY ? WAL_RETRY : rc;
48867     }
48868     /* Now that the read-lock has been obtained, check that neither the
48869     ** value in the aReadMark[] array or the contents of the wal-index
48870     ** header have changed.
48871     **
48872     ** It is necessary to check that the wal-index header did not change
48873     ** between the time it was read and when the shared-lock was obtained
48874     ** on WAL_READ_LOCK(mxI) was obtained to account for the possibility
48875     ** that the log file may have been wrapped by a writer, or that frames
48876     ** that occur later in the log than pWal->hdr.mxFrame may have been
48877     ** copied into the database by a checkpointer. If either of these things
48878     ** happened, then reading the database with the current value of
48879     ** pWal->hdr.mxFrame risks reading a corrupted snapshot. So, retry
48880     ** instead.
48881     **
48882     ** This does not guarantee that the copy of the wal-index header is up to
48883     ** date before proceeding. That would not be possible without somehow
48884     ** blocking writers. It only guarantees that a dangerous checkpoint or
48885     ** log-wrap (either of which would require an exclusive lock on
48886     ** WAL_READ_LOCK(mxI)) has not occurred since the snapshot was valid.
48887     */
48888     walShmBarrier(pWal);
48889     if( pInfo->aReadMark[mxI]!=mxReadMark
48890      || memcmp((void *)walIndexHdr(pWal), &pWal->hdr, sizeof(WalIndexHdr))
48891     ){
48892       walUnlockShared(pWal, WAL_READ_LOCK(mxI));
48893       return WAL_RETRY;
48894     }else{
48895       assert( mxReadMark<=pWal->hdr.mxFrame );
48896       pWal->readLock = (i16)mxI;
48897     }
48898   }
48899   return rc;
48900 }
48901 
48902 /*
48903 ** Begin a read transaction on the database.
48904 **
48905 ** This routine used to be called sqlite3OpenSnapshot() and with good reason:
48906 ** it takes a snapshot of the state of the WAL and wal-index for the current
48907 ** instant in time.  The current thread will continue to use this snapshot.
48908 ** Other threads might append new content to the WAL and wal-index but
48909 ** that extra content is ignored by the current thread.
48910 **
48911 ** If the database contents have changes since the previous read
48912 ** transaction, then *pChanged is set to 1 before returning.  The
48913 ** Pager layer will use this to know that is cache is stale and
48914 ** needs to be flushed.
48915 */
48916 SQLITE_PRIVATE int sqlite3WalBeginReadTransaction(Wal *pWal, int *pChanged){
48917   int rc;                         /* Return code */
48918   int cnt = 0;                    /* Number of TryBeginRead attempts */
48919 
48920   do{
48921     rc = walTryBeginRead(pWal, pChanged, 0, ++cnt);
48922   }while( rc==WAL_RETRY );
48923   testcase( (rc&0xff)==SQLITE_BUSY );
48924   testcase( (rc&0xff)==SQLITE_IOERR );
48925   testcase( rc==SQLITE_PROTOCOL );
48926   testcase( rc==SQLITE_OK );
48927   return rc;
48928 }
48929 
48930 /*
48931 ** Finish with a read transaction.  All this does is release the
48932 ** read-lock.
48933 */
48934 SQLITE_PRIVATE void sqlite3WalEndReadTransaction(Wal *pWal){
48935   sqlite3WalEndWriteTransaction(pWal);
48936   if( pWal->readLock>=0 ){
48937     walUnlockShared(pWal, WAL_READ_LOCK(pWal->readLock));
48938     pWal->readLock = -1;
48939   }
48940 }
48941 
48942 /*
48943 ** Search the wal file for page pgno. If found, set *piRead to the frame that
48944 ** contains the page. Otherwise, if pgno is not in the wal file, set *piRead
48945 ** to zero.
48946 **
48947 ** Return SQLITE_OK if successful, or an error code if an error occurs. If an
48948 ** error does occur, the final value of *piRead is undefined.
48949 */
48950 SQLITE_PRIVATE int sqlite3WalFindFrame(
48951   Wal *pWal,                      /* WAL handle */
48952   Pgno pgno,                      /* Database page number to read data for */
48953   u32 *piRead                     /* OUT: Frame number (or zero) */
48954 ){
48955   u32 iRead = 0;                  /* If !=0, WAL frame to return data from */
48956   u32 iLast = pWal->hdr.mxFrame;  /* Last page in WAL for this reader */
48957   int iHash;                      /* Used to loop through N hash tables */
48958 
48959   /* This routine is only be called from within a read transaction. */
48960   assert( pWal->readLock>=0 || pWal->lockError );
48961 
48962   /* If the "last page" field of the wal-index header snapshot is 0, then
48963   ** no data will be read from the wal under any circumstances. Return early
48964   ** in this case as an optimization.  Likewise, if pWal->readLock==0,
48965   ** then the WAL is ignored by the reader so return early, as if the
48966   ** WAL were empty.
48967   */
48968   if( iLast==0 || pWal->readLock==0 ){
48969     *piRead = 0;
48970     return SQLITE_OK;
48971   }
48972 
48973   /* Search the hash table or tables for an entry matching page number
48974   ** pgno. Each iteration of the following for() loop searches one
48975   ** hash table (each hash table indexes up to HASHTABLE_NPAGE frames).
48976   **
48977   ** This code might run concurrently to the code in walIndexAppend()
48978   ** that adds entries to the wal-index (and possibly to this hash
48979   ** table). This means the value just read from the hash
48980   ** slot (aHash[iKey]) may have been added before or after the
48981   ** current read transaction was opened. Values added after the
48982   ** read transaction was opened may have been written incorrectly -
48983   ** i.e. these slots may contain garbage data. However, we assume
48984   ** that any slots written before the current read transaction was
48985   ** opened remain unmodified.
48986   **
48987   ** For the reasons above, the if(...) condition featured in the inner
48988   ** loop of the following block is more stringent that would be required
48989   ** if we had exclusive access to the hash-table:
48990   **
48991   **   (aPgno[iFrame]==pgno):
48992   **     This condition filters out normal hash-table collisions.
48993   **
48994   **   (iFrame<=iLast):
48995   **     This condition filters out entries that were added to the hash
48996   **     table after the current read-transaction had started.
48997   */
48998   for(iHash=walFramePage(iLast); iHash>=0 && iRead==0; iHash--){
48999     volatile ht_slot *aHash;      /* Pointer to hash table */
49000     volatile u32 *aPgno;          /* Pointer to array of page numbers */
49001     u32 iZero;                    /* Frame number corresponding to aPgno[0] */
49002     int iKey;                     /* Hash slot index */
49003     int nCollide;                 /* Number of hash collisions remaining */
49004     int rc;                       /* Error code */
49005 
49006     rc = walHashGet(pWal, iHash, &aHash, &aPgno, &iZero);
49007     if( rc!=SQLITE_OK ){
49008       return rc;
49009     }
49010     nCollide = HASHTABLE_NSLOT;
49011     for(iKey=walHash(pgno); aHash[iKey]; iKey=walNextHash(iKey)){
49012       u32 iFrame = aHash[iKey] + iZero;
49013       if( iFrame<=iLast && aPgno[aHash[iKey]]==pgno ){
49014         /* assert( iFrame>iRead ); -- not true if there is corruption */
49015         iRead = iFrame;
49016       }
49017       if( (nCollide--)==0 ){
49018         return SQLITE_CORRUPT_BKPT;
49019       }
49020     }
49021   }
49022 
49023 #ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT
49024   /* If expensive assert() statements are available, do a linear search
49025   ** of the wal-index file content. Make sure the results agree with the
49026   ** result obtained using the hash indexes above.  */
49027   {
49028     u32 iRead2 = 0;
49029     u32 iTest;
49030     for(iTest=iLast; iTest>0; iTest--){
49031       if( walFramePgno(pWal, iTest)==pgno ){
49032         iRead2 = iTest;
49033         break;
49034       }
49035     }
49036     assert( iRead==iRead2 );
49037   }
49038 #endif
49039 
49040   *piRead = iRead;
49041   return SQLITE_OK;
49042 }
49043 
49044 /*
49045 ** Read the contents of frame iRead from the wal file into buffer pOut
49046 ** (which is nOut bytes in size). Return SQLITE_OK if successful, or an
49047 ** error code otherwise.
49048 */
49049 SQLITE_PRIVATE int sqlite3WalReadFrame(
49050   Wal *pWal,                      /* WAL handle */
49051   u32 iRead,                      /* Frame to read */
49052   int nOut,                       /* Size of buffer pOut in bytes */
49053   u8 *pOut                        /* Buffer to write page data to */
49054 ){
49055   int sz;
49056   i64 iOffset;
49057   sz = pWal->hdr.szPage;
49058   sz = (sz&0xfe00) + ((sz&0x0001)<<16);
49059   testcase( sz<=32768 );
49060   testcase( sz>=65536 );
49061   iOffset = walFrameOffset(iRead, sz) + WAL_FRAME_HDRSIZE;
49062   /* testcase( IS_BIG_INT(iOffset) ); // requires a 4GiB WAL */
49063   return sqlite3OsRead(pWal->pWalFd, pOut, (nOut>sz ? sz : nOut), iOffset);
49064 }
49065 
49066 /*
49067 ** Return the size of the database in pages (or zero, if unknown).
49068 */
49069 SQLITE_PRIVATE Pgno sqlite3WalDbsize(Wal *pWal){
49070   if( pWal && ALWAYS(pWal->readLock>=0) ){
49071     return pWal->hdr.nPage;
49072   }
49073   return 0;
49074 }
49075 
49076 
49077 /*
49078 ** This function starts a write transaction on the WAL.
49079 **
49080 ** A read transaction must have already been started by a prior call
49081 ** to sqlite3WalBeginReadTransaction().
49082 **
49083 ** If another thread or process has written into the database since
49084 ** the read transaction was started, then it is not possible for this
49085 ** thread to write as doing so would cause a fork.  So this routine
49086 ** returns SQLITE_BUSY in that case and no write transaction is started.
49087 **
49088 ** There can only be a single writer active at a time.
49089 */
49090 SQLITE_PRIVATE int sqlite3WalBeginWriteTransaction(Wal *pWal){
49091   int rc;
49092 
49093   /* Cannot start a write transaction without first holding a read
49094   ** transaction. */
49095   assert( pWal->readLock>=0 );
49096 
49097   if( pWal->readOnly ){
49098     return SQLITE_READONLY;
49099   }
49100 
49101   /* Only one writer allowed at a time.  Get the write lock.  Return
49102   ** SQLITE_BUSY if unable.
49103   */
49104   rc = walLockExclusive(pWal, WAL_WRITE_LOCK, 1);
49105   if( rc ){
49106     return rc;
49107   }
49108   pWal->writeLock = 1;
49109 
49110   /* If another connection has written to the database file since the
49111   ** time the read transaction on this connection was started, then
49112   ** the write is disallowed.
49113   */
49114   if( memcmp(&pWal->hdr, (void *)walIndexHdr(pWal), sizeof(WalIndexHdr))!=0 ){
49115     walUnlockExclusive(pWal, WAL_WRITE_LOCK, 1);
49116     pWal->writeLock = 0;
49117     rc = SQLITE_BUSY_SNAPSHOT;
49118   }
49119 
49120   return rc;
49121 }
49122 
49123 /*
49124 ** End a write transaction.  The commit has already been done.  This
49125 ** routine merely releases the lock.
49126 */
49127 SQLITE_PRIVATE int sqlite3WalEndWriteTransaction(Wal *pWal){
49128   if( pWal->writeLock ){
49129     walUnlockExclusive(pWal, WAL_WRITE_LOCK, 1);
49130     pWal->writeLock = 0;
49131     pWal->truncateOnCommit = 0;
49132   }
49133   return SQLITE_OK;
49134 }
49135 
49136 /*
49137 ** If any data has been written (but not committed) to the log file, this
49138 ** function moves the write-pointer back to the start of the transaction.
49139 **
49140 ** Additionally, the callback function is invoked for each frame written
49141 ** to the WAL since the start of the transaction. If the callback returns
49142 ** other than SQLITE_OK, it is not invoked again and the error code is
49143 ** returned to the caller.
49144 **
49145 ** Otherwise, if the callback function does not return an error, this
49146 ** function returns SQLITE_OK.
49147 */
49148 SQLITE_PRIVATE int sqlite3WalUndo(Wal *pWal, int (*xUndo)(void *, Pgno), void *pUndoCtx){
49149   int rc = SQLITE_OK;
49150   if( ALWAYS(pWal->writeLock) ){
49151     Pgno iMax = pWal->hdr.mxFrame;
49152     Pgno iFrame;
49153 
49154     /* Restore the clients cache of the wal-index header to the state it
49155     ** was in before the client began writing to the database.
49156     */
49157     memcpy(&pWal->hdr, (void *)walIndexHdr(pWal), sizeof(WalIndexHdr));
49158 
49159     for(iFrame=pWal->hdr.mxFrame+1;
49160         ALWAYS(rc==SQLITE_OK) && iFrame<=iMax;
49161         iFrame++
49162     ){
49163       /* This call cannot fail. Unless the page for which the page number
49164       ** is passed as the second argument is (a) in the cache and
49165       ** (b) has an outstanding reference, then xUndo is either a no-op
49166       ** (if (a) is false) or simply expels the page from the cache (if (b)
49167       ** is false).
49168       **
49169       ** If the upper layer is doing a rollback, it is guaranteed that there
49170       ** are no outstanding references to any page other than page 1. And
49171       ** page 1 is never written to the log until the transaction is
49172       ** committed. As a result, the call to xUndo may not fail.
49173       */
49174       assert( walFramePgno(pWal, iFrame)!=1 );
49175       rc = xUndo(pUndoCtx, walFramePgno(pWal, iFrame));
49176     }
49177     if( iMax!=pWal->hdr.mxFrame ) walCleanupHash(pWal);
49178   }
49179   assert( rc==SQLITE_OK );
49180   return rc;
49181 }
49182 
49183 /*
49184 ** Argument aWalData must point to an array of WAL_SAVEPOINT_NDATA u32
49185 ** values. This function populates the array with values required to
49186 ** "rollback" the write position of the WAL handle back to the current
49187 ** point in the event of a savepoint rollback (via WalSavepointUndo()).
49188 */
49189 SQLITE_PRIVATE void sqlite3WalSavepoint(Wal *pWal, u32 *aWalData){
49190   assert( pWal->writeLock );
49191   aWalData[0] = pWal->hdr.mxFrame;
49192   aWalData[1] = pWal->hdr.aFrameCksum[0];
49193   aWalData[2] = pWal->hdr.aFrameCksum[1];
49194   aWalData[3] = pWal->nCkpt;
49195 }
49196 
49197 /*
49198 ** Move the write position of the WAL back to the point identified by
49199 ** the values in the aWalData[] array. aWalData must point to an array
49200 ** of WAL_SAVEPOINT_NDATA u32 values that has been previously populated
49201 ** by a call to WalSavepoint().
49202 */
49203 SQLITE_PRIVATE int sqlite3WalSavepointUndo(Wal *pWal, u32 *aWalData){
49204   int rc = SQLITE_OK;
49205 
49206   assert( pWal->writeLock );
49207   assert( aWalData[3]!=pWal->nCkpt || aWalData[0]<=pWal->hdr.mxFrame );
49208 
49209   if( aWalData[3]!=pWal->nCkpt ){
49210     /* This savepoint was opened immediately after the write-transaction
49211     ** was started. Right after that, the writer decided to wrap around
49212     ** to the start of the log. Update the savepoint values to match.
49213     */
49214     aWalData[0] = 0;
49215     aWalData[3] = pWal->nCkpt;
49216   }
49217 
49218   if( aWalData[0]<pWal->hdr.mxFrame ){
49219     pWal->hdr.mxFrame = aWalData[0];
49220     pWal->hdr.aFrameCksum[0] = aWalData[1];
49221     pWal->hdr.aFrameCksum[1] = aWalData[2];
49222     walCleanupHash(pWal);
49223   }
49224 
49225   return rc;
49226 }
49227 
49228 
49229 /*
49230 ** This function is called just before writing a set of frames to the log
49231 ** file (see sqlite3WalFrames()). It checks to see if, instead of appending
49232 ** to the current log file, it is possible to overwrite the start of the
49233 ** existing log file with the new frames (i.e. "reset" the log). If so,
49234 ** it sets pWal->hdr.mxFrame to 0. Otherwise, pWal->hdr.mxFrame is left
49235 ** unchanged.
49236 **
49237 ** SQLITE_OK is returned if no error is encountered (regardless of whether
49238 ** or not pWal->hdr.mxFrame is modified). An SQLite error code is returned
49239 ** if an error occurs.
49240 */
49241 static int walRestartLog(Wal *pWal){
49242   int rc = SQLITE_OK;
49243   int cnt;
49244 
49245   if( pWal->readLock==0 ){
49246     volatile WalCkptInfo *pInfo = walCkptInfo(pWal);
49247     assert( pInfo->nBackfill==pWal->hdr.mxFrame );
49248     if( pInfo->nBackfill>0 ){
49249       u32 salt1;
49250       sqlite3_randomness(4, &salt1);
49251       rc = walLockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
49252       if( rc==SQLITE_OK ){
49253         /* If all readers are using WAL_READ_LOCK(0) (in other words if no
49254         ** readers are currently using the WAL), then the transactions
49255         ** frames will overwrite the start of the existing log. Update the
49256         ** wal-index header to reflect this.
49257         **
49258         ** In theory it would be Ok to update the cache of the header only
49259         ** at this point. But updating the actual wal-index header is also
49260         ** safe and means there is no special case for sqlite3WalUndo()
49261         ** to handle if this transaction is rolled back.
49262         */
49263         int i;                    /* Loop counter */
49264         u32 *aSalt = pWal->hdr.aSalt;       /* Big-endian salt values */
49265 
49266         pWal->nCkpt++;
49267         pWal->hdr.mxFrame = 0;
49268         sqlite3Put4byte((u8*)&aSalt[0], 1 + sqlite3Get4byte((u8*)&aSalt[0]));
49269         aSalt[1] = salt1;
49270         walIndexWriteHdr(pWal);
49271         pInfo->nBackfill = 0;
49272         pInfo->aReadMark[1] = 0;
49273         for(i=2; i<WAL_NREADER; i++) pInfo->aReadMark[i] = READMARK_NOT_USED;
49274         assert( pInfo->aReadMark[0]==0 );
49275         walUnlockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
49276       }else if( rc!=SQLITE_BUSY ){
49277         return rc;
49278       }
49279     }
49280     walUnlockShared(pWal, WAL_READ_LOCK(0));
49281     pWal->readLock = -1;
49282     cnt = 0;
49283     do{
49284       int notUsed;
49285       rc = walTryBeginRead(pWal, &notUsed, 1, ++cnt);
49286     }while( rc==WAL_RETRY );
49287     assert( (rc&0xff)!=SQLITE_BUSY ); /* BUSY not possible when useWal==1 */
49288     testcase( (rc&0xff)==SQLITE_IOERR );
49289     testcase( rc==SQLITE_PROTOCOL );
49290     testcase( rc==SQLITE_OK );
49291   }
49292   return rc;
49293 }
49294 
49295 /*
49296 ** Information about the current state of the WAL file and where
49297 ** the next fsync should occur - passed from sqlite3WalFrames() into
49298 ** walWriteToLog().
49299 */
49300 typedef struct WalWriter {
49301   Wal *pWal;                   /* The complete WAL information */
49302   sqlite3_file *pFd;           /* The WAL file to which we write */
49303   sqlite3_int64 iSyncPoint;    /* Fsync at this offset */
49304   int syncFlags;               /* Flags for the fsync */
49305   int szPage;                  /* Size of one page */
49306 } WalWriter;
49307 
49308 /*
49309 ** Write iAmt bytes of content into the WAL file beginning at iOffset.
49310 ** Do a sync when crossing the p->iSyncPoint boundary.
49311 **
49312 ** In other words, if iSyncPoint is in between iOffset and iOffset+iAmt,
49313 ** first write the part before iSyncPoint, then sync, then write the
49314 ** rest.
49315 */
49316 static int walWriteToLog(
49317   WalWriter *p,              /* WAL to write to */
49318   void *pContent,            /* Content to be written */
49319   int iAmt,                  /* Number of bytes to write */
49320   sqlite3_int64 iOffset      /* Start writing at this offset */
49321 ){
49322   int rc;
49323   if( iOffset<p->iSyncPoint && iOffset+iAmt>=p->iSyncPoint ){
49324     int iFirstAmt = (int)(p->iSyncPoint - iOffset);
49325     rc = sqlite3OsWrite(p->pFd, pContent, iFirstAmt, iOffset);
49326     if( rc ) return rc;
49327     iOffset += iFirstAmt;
49328     iAmt -= iFirstAmt;
49329     pContent = (void*)(iFirstAmt + (char*)pContent);
49330     assert( p->syncFlags & (SQLITE_SYNC_NORMAL|SQLITE_SYNC_FULL) );
49331     rc = sqlite3OsSync(p->pFd, p->syncFlags);
49332     if( iAmt==0 || rc ) return rc;
49333   }
49334   rc = sqlite3OsWrite(p->pFd, pContent, iAmt, iOffset);
49335   return rc;
49336 }
49337 
49338 /*
49339 ** Write out a single frame of the WAL
49340 */
49341 static int walWriteOneFrame(
49342   WalWriter *p,               /* Where to write the frame */
49343   PgHdr *pPage,               /* The page of the frame to be written */
49344   int nTruncate,              /* The commit flag.  Usually 0.  >0 for commit */
49345   sqlite3_int64 iOffset       /* Byte offset at which to write */
49346 ){
49347   int rc;                         /* Result code from subfunctions */
49348   void *pData;                    /* Data actually written */
49349   u8 aFrame[WAL_FRAME_HDRSIZE];   /* Buffer to assemble frame-header in */
49350 #if defined(SQLITE_HAS_CODEC)
49351   if( (pData = sqlite3PagerCodec(pPage))==0 ) return SQLITE_NOMEM;
49352 #else
49353   pData = pPage->pData;
49354 #endif
49355   walEncodeFrame(p->pWal, pPage->pgno, nTruncate, pData, aFrame);
49356   rc = walWriteToLog(p, aFrame, sizeof(aFrame), iOffset);
49357   if( rc ) return rc;
49358   /* Write the page data */
49359   rc = walWriteToLog(p, pData, p->szPage, iOffset+sizeof(aFrame));
49360   return rc;
49361 }
49362 
49363 /*
49364 ** Write a set of frames to the log. The caller must hold the write-lock
49365 ** on the log file (obtained using sqlite3WalBeginWriteTransaction()).
49366 */
49367 SQLITE_PRIVATE int sqlite3WalFrames(
49368   Wal *pWal,                      /* Wal handle to write to */
49369   int szPage,                     /* Database page-size in bytes */
49370   PgHdr *pList,                   /* List of dirty pages to write */
49371   Pgno nTruncate,                 /* Database size after this commit */
49372   int isCommit,                   /* True if this is a commit */
49373   int sync_flags                  /* Flags to pass to OsSync() (or 0) */
49374 ){
49375   int rc;                         /* Used to catch return codes */
49376   u32 iFrame;                     /* Next frame address */
49377   PgHdr *p;                       /* Iterator to run through pList with. */
49378   PgHdr *pLast = 0;               /* Last frame in list */
49379   int nExtra = 0;                 /* Number of extra copies of last page */
49380   int szFrame;                    /* The size of a single frame */
49381   i64 iOffset;                    /* Next byte to write in WAL file */
49382   WalWriter w;                    /* The writer */
49383 
49384   assert( pList );
49385   assert( pWal->writeLock );
49386 
49387   /* If this frame set completes a transaction, then nTruncate>0.  If
49388   ** nTruncate==0 then this frame set does not complete the transaction. */
49389   assert( (isCommit!=0)==(nTruncate!=0) );
49390 
49391 #if defined(SQLITE_TEST) && defined(SQLITE_DEBUG)
49392   { int cnt; for(cnt=0, p=pList; p; p=p->pDirty, cnt++){}
49393     WALTRACE(("WAL%p: frame write begin. %d frames. mxFrame=%d. %s\n",
49394               pWal, cnt, pWal->hdr.mxFrame, isCommit ? "Commit" : "Spill"));
49395   }
49396 #endif
49397 
49398   /* See if it is possible to write these frames into the start of the
49399   ** log file, instead of appending to it at pWal->hdr.mxFrame.
49400   */
49401   if( SQLITE_OK!=(rc = walRestartLog(pWal)) ){
49402     return rc;
49403   }
49404 
49405   /* If this is the first frame written into the log, write the WAL
49406   ** header to the start of the WAL file. See comments at the top of
49407   ** this source file for a description of the WAL header format.
49408   */
49409   iFrame = pWal->hdr.mxFrame;
49410   if( iFrame==0 ){
49411     u8 aWalHdr[WAL_HDRSIZE];      /* Buffer to assemble wal-header in */
49412     u32 aCksum[2];                /* Checksum for wal-header */
49413 
49414     sqlite3Put4byte(&aWalHdr[0], (WAL_MAGIC | SQLITE_BIGENDIAN));
49415     sqlite3Put4byte(&aWalHdr[4], WAL_MAX_VERSION);
49416     sqlite3Put4byte(&aWalHdr[8], szPage);
49417     sqlite3Put4byte(&aWalHdr[12], pWal->nCkpt);
49418     if( pWal->nCkpt==0 ) sqlite3_randomness(8, pWal->hdr.aSalt);
49419     memcpy(&aWalHdr[16], pWal->hdr.aSalt, 8);
49420     walChecksumBytes(1, aWalHdr, WAL_HDRSIZE-2*4, 0, aCksum);
49421     sqlite3Put4byte(&aWalHdr[24], aCksum[0]);
49422     sqlite3Put4byte(&aWalHdr[28], aCksum[1]);
49423 
49424     pWal->szPage = szPage;
49425     pWal->hdr.bigEndCksum = SQLITE_BIGENDIAN;
49426     pWal->hdr.aFrameCksum[0] = aCksum[0];
49427     pWal->hdr.aFrameCksum[1] = aCksum[1];
49428     pWal->truncateOnCommit = 1;
49429 
49430     rc = sqlite3OsWrite(pWal->pWalFd, aWalHdr, sizeof(aWalHdr), 0);
49431     WALTRACE(("WAL%p: wal-header write %s\n", pWal, rc ? "failed" : "ok"));
49432     if( rc!=SQLITE_OK ){
49433       return rc;
49434     }
49435 
49436     /* Sync the header (unless SQLITE_IOCAP_SEQUENTIAL is true or unless
49437     ** all syncing is turned off by PRAGMA synchronous=OFF).  Otherwise
49438     ** an out-of-order write following a WAL restart could result in
49439     ** database corruption.  See the ticket:
49440     **
49441     **     http://localhost:591/sqlite/info/ff5be73dee
49442     */
49443     if( pWal->syncHeader && sync_flags ){
49444       rc = sqlite3OsSync(pWal->pWalFd, sync_flags & SQLITE_SYNC_MASK);
49445       if( rc ) return rc;
49446     }
49447   }
49448   assert( (int)pWal->szPage==szPage );
49449 
49450   /* Setup information needed to write frames into the WAL */
49451   w.pWal = pWal;
49452   w.pFd = pWal->pWalFd;
49453   w.iSyncPoint = 0;
49454   w.syncFlags = sync_flags;
49455   w.szPage = szPage;
49456   iOffset = walFrameOffset(iFrame+1, szPage);
49457   szFrame = szPage + WAL_FRAME_HDRSIZE;
49458 
49459   /* Write all frames into the log file exactly once */
49460   for(p=pList; p; p=p->pDirty){
49461     int nDbSize;   /* 0 normally.  Positive == commit flag */
49462     iFrame++;
49463     assert( iOffset==walFrameOffset(iFrame, szPage) );
49464     nDbSize = (isCommit && p->pDirty==0) ? nTruncate : 0;
49465     rc = walWriteOneFrame(&w, p, nDbSize, iOffset);
49466     if( rc ) return rc;
49467     pLast = p;
49468     iOffset += szFrame;
49469   }
49470 
49471   /* If this is the end of a transaction, then we might need to pad
49472   ** the transaction and/or sync the WAL file.
49473   **
49474   ** Padding and syncing only occur if this set of frames complete a
49475   ** transaction and if PRAGMA synchronous=FULL.  If synchronous==NORMAL
49476   ** or synchonous==OFF, then no padding or syncing are needed.
49477   **
49478   ** If SQLITE_IOCAP_POWERSAFE_OVERWRITE is defined, then padding is not
49479   ** needed and only the sync is done.  If padding is needed, then the
49480   ** final frame is repeated (with its commit mark) until the next sector
49481   ** boundary is crossed.  Only the part of the WAL prior to the last
49482   ** sector boundary is synced; the part of the last frame that extends
49483   ** past the sector boundary is written after the sync.
49484   */
49485   if( isCommit && (sync_flags & WAL_SYNC_TRANSACTIONS)!=0 ){
49486     if( pWal->padToSectorBoundary ){
49487       int sectorSize = sqlite3SectorSize(pWal->pWalFd);
49488       w.iSyncPoint = ((iOffset+sectorSize-1)/sectorSize)*sectorSize;
49489       while( iOffset<w.iSyncPoint ){
49490         rc = walWriteOneFrame(&w, pLast, nTruncate, iOffset);
49491         if( rc ) return rc;
49492         iOffset += szFrame;
49493         nExtra++;
49494       }
49495     }else{
49496       rc = sqlite3OsSync(w.pFd, sync_flags & SQLITE_SYNC_MASK);
49497     }
49498   }
49499 
49500   /* If this frame set completes the first transaction in the WAL and
49501   ** if PRAGMA journal_size_limit is set, then truncate the WAL to the
49502   ** journal size limit, if possible.
49503   */
49504   if( isCommit && pWal->truncateOnCommit && pWal->mxWalSize>=0 ){
49505     i64 sz = pWal->mxWalSize;
49506     if( walFrameOffset(iFrame+nExtra+1, szPage)>pWal->mxWalSize ){
49507       sz = walFrameOffset(iFrame+nExtra+1, szPage);
49508     }
49509     walLimitSize(pWal, sz);
49510     pWal->truncateOnCommit = 0;
49511   }
49512 
49513   /* Append data to the wal-index. It is not necessary to lock the
49514   ** wal-index to do this as the SQLITE_SHM_WRITE lock held on the wal-index
49515   ** guarantees that there are no other writers, and no data that may
49516   ** be in use by existing readers is being overwritten.
49517   */
49518   iFrame = pWal->hdr.mxFrame;
49519   for(p=pList; p && rc==SQLITE_OK; p=p->pDirty){
49520     iFrame++;
49521     rc = walIndexAppend(pWal, iFrame, p->pgno);
49522   }
49523   while( rc==SQLITE_OK && nExtra>0 ){
49524     iFrame++;
49525     nExtra--;
49526     rc = walIndexAppend(pWal, iFrame, pLast->pgno);
49527   }
49528 
49529   if( rc==SQLITE_OK ){
49530     /* Update the private copy of the header. */
49531     pWal->hdr.szPage = (u16)((szPage&0xff00) | (szPage>>16));
49532     testcase( szPage<=32768 );
49533     testcase( szPage>=65536 );
49534     pWal->hdr.mxFrame = iFrame;
49535     if( isCommit ){
49536       pWal->hdr.iChange++;
49537       pWal->hdr.nPage = nTruncate;
49538     }
49539     /* If this is a commit, update the wal-index header too. */
49540     if( isCommit ){
49541       walIndexWriteHdr(pWal);
49542       pWal->iCallback = iFrame;
49543     }
49544   }
49545 
49546   WALTRACE(("WAL%p: frame write %s\n", pWal, rc ? "failed" : "ok"));
49547   return rc;
49548 }
49549 
49550 /*
49551 ** This routine is called to implement sqlite3_wal_checkpoint() and
49552 ** related interfaces.
49553 **
49554 ** Obtain a CHECKPOINT lock and then backfill as much information as
49555 ** we can from WAL into the database.
49556 **
49557 ** If parameter xBusy is not NULL, it is a pointer to a busy-handler
49558 ** callback. In this case this function runs a blocking checkpoint.
49559 */
49560 SQLITE_PRIVATE int sqlite3WalCheckpoint(
49561   Wal *pWal,                      /* Wal connection */
49562   int eMode,                      /* PASSIVE, FULL or RESTART */
49563   int (*xBusy)(void*),            /* Function to call when busy */
49564   void *pBusyArg,                 /* Context argument for xBusyHandler */
49565   int sync_flags,                 /* Flags to sync db file with (or 0) */
49566   int nBuf,                       /* Size of temporary buffer */
49567   u8 *zBuf,                       /* Temporary buffer to use */
49568   int *pnLog,                     /* OUT: Number of frames in WAL */
49569   int *pnCkpt                     /* OUT: Number of backfilled frames in WAL */
49570 ){
49571   int rc;                         /* Return code */
49572   int isChanged = 0;              /* True if a new wal-index header is loaded */
49573   int eMode2 = eMode;             /* Mode to pass to walCheckpoint() */
49574 
49575   assert( pWal->ckptLock==0 );
49576   assert( pWal->writeLock==0 );
49577 
49578   if( pWal->readOnly ) return SQLITE_READONLY;
49579   WALTRACE(("WAL%p: checkpoint begins\n", pWal));
49580   rc = walLockExclusive(pWal, WAL_CKPT_LOCK, 1);
49581   if( rc ){
49582     /* Usually this is SQLITE_BUSY meaning that another thread or process
49583     ** is already running a checkpoint, or maybe a recovery.  But it might
49584     ** also be SQLITE_IOERR. */
49585     return rc;
49586   }
49587   pWal->ckptLock = 1;
49588 
49589   /* If this is a blocking-checkpoint, then obtain the write-lock as well
49590   ** to prevent any writers from running while the checkpoint is underway.
49591   ** This has to be done before the call to walIndexReadHdr() below.
49592   **
49593   ** If the writer lock cannot be obtained, then a passive checkpoint is
49594   ** run instead. Since the checkpointer is not holding the writer lock,
49595   ** there is no point in blocking waiting for any readers. Assuming no
49596   ** other error occurs, this function will return SQLITE_BUSY to the caller.
49597   */
49598   if( eMode!=SQLITE_CHECKPOINT_PASSIVE ){
49599     rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_WRITE_LOCK, 1);
49600     if( rc==SQLITE_OK ){
49601       pWal->writeLock = 1;
49602     }else if( rc==SQLITE_BUSY ){
49603       eMode2 = SQLITE_CHECKPOINT_PASSIVE;
49604       rc = SQLITE_OK;
49605     }
49606   }
49607 
49608   /* Read the wal-index header. */
49609   if( rc==SQLITE_OK ){
49610     rc = walIndexReadHdr(pWal, &isChanged);
49611     if( isChanged && pWal->pDbFd->pMethods->iVersion>=3 ){
49612       sqlite3OsUnfetch(pWal->pDbFd, 0, 0);
49613     }
49614   }
49615 
49616   /* Copy data from the log to the database file. */
49617   if( rc==SQLITE_OK ){
49618     if( pWal->hdr.mxFrame && walPagesize(pWal)!=nBuf ){
49619       rc = SQLITE_CORRUPT_BKPT;
49620     }else{
49621       rc = walCheckpoint(pWal, eMode2, xBusy, pBusyArg, sync_flags, zBuf);
49622     }
49623 
49624     /* If no error occurred, set the output variables. */
49625     if( rc==SQLITE_OK || rc==SQLITE_BUSY ){
49626       if( pnLog ) *pnLog = (int)pWal->hdr.mxFrame;
49627       if( pnCkpt ) *pnCkpt = (int)(walCkptInfo(pWal)->nBackfill);
49628     }
49629   }
49630 
49631   if( isChanged ){
49632     /* If a new wal-index header was loaded before the checkpoint was
49633     ** performed, then the pager-cache associated with pWal is now
49634     ** out of date. So zero the cached wal-index header to ensure that
49635     ** next time the pager opens a snapshot on this database it knows that
49636     ** the cache needs to be reset.
49637     */
49638     memset(&pWal->hdr, 0, sizeof(WalIndexHdr));
49639   }
49640 
49641   /* Release the locks. */
49642   sqlite3WalEndWriteTransaction(pWal);
49643   walUnlockExclusive(pWal, WAL_CKPT_LOCK, 1);
49644   pWal->ckptLock = 0;
49645   WALTRACE(("WAL%p: checkpoint %s\n", pWal, rc ? "failed" : "ok"));
49646   return (rc==SQLITE_OK && eMode!=eMode2 ? SQLITE_BUSY : rc);
49647 }
49648 
49649 /* Return the value to pass to a sqlite3_wal_hook callback, the
49650 ** number of frames in the WAL at the point of the last commit since
49651 ** sqlite3WalCallback() was called.  If no commits have occurred since
49652 ** the last call, then return 0.
49653 */
49654 SQLITE_PRIVATE int sqlite3WalCallback(Wal *pWal){
49655   u32 ret = 0;
49656   if( pWal ){
49657     ret = pWal->iCallback;
49658     pWal->iCallback = 0;
49659   }
49660   return (int)ret;
49661 }
49662 
49663 /*
49664 ** This function is called to change the WAL subsystem into or out
49665 ** of locking_mode=EXCLUSIVE.
49666 **
49667 ** If op is zero, then attempt to change from locking_mode=EXCLUSIVE
49668 ** into locking_mode=NORMAL.  This means that we must acquire a lock
49669 ** on the pWal->readLock byte.  If the WAL is already in locking_mode=NORMAL
49670 ** or if the acquisition of the lock fails, then return 0.  If the
49671 ** transition out of exclusive-mode is successful, return 1.  This
49672 ** operation must occur while the pager is still holding the exclusive
49673 ** lock on the main database file.
49674 **
49675 ** If op is one, then change from locking_mode=NORMAL into
49676 ** locking_mode=EXCLUSIVE.  This means that the pWal->readLock must
49677 ** be released.  Return 1 if the transition is made and 0 if the
49678 ** WAL is already in exclusive-locking mode - meaning that this
49679 ** routine is a no-op.  The pager must already hold the exclusive lock
49680 ** on the main database file before invoking this operation.
49681 **
49682 ** If op is negative, then do a dry-run of the op==1 case but do
49683 ** not actually change anything. The pager uses this to see if it
49684 ** should acquire the database exclusive lock prior to invoking
49685 ** the op==1 case.
49686 */
49687 SQLITE_PRIVATE int sqlite3WalExclusiveMode(Wal *pWal, int op){
49688   int rc;
49689   assert( pWal->writeLock==0 );
49690   assert( pWal->exclusiveMode!=WAL_HEAPMEMORY_MODE || op==-1 );
49691 
49692   /* pWal->readLock is usually set, but might be -1 if there was a
49693   ** prior error while attempting to acquire are read-lock. This cannot
49694   ** happen if the connection is actually in exclusive mode (as no xShmLock
49695   ** locks are taken in this case). Nor should the pager attempt to
49696   ** upgrade to exclusive-mode following such an error.
49697   */
49698   assert( pWal->readLock>=0 || pWal->lockError );
49699   assert( pWal->readLock>=0 || (op<=0 && pWal->exclusiveMode==0) );
49700 
49701   if( op==0 ){
49702     if( pWal->exclusiveMode ){
49703       pWal->exclusiveMode = 0;
49704       if( walLockShared(pWal, WAL_READ_LOCK(pWal->readLock))!=SQLITE_OK ){
49705         pWal->exclusiveMode = 1;
49706       }
49707       rc = pWal->exclusiveMode==0;
49708     }else{
49709       /* Already in locking_mode=NORMAL */
49710       rc = 0;
49711     }
49712   }else if( op>0 ){
49713     assert( pWal->exclusiveMode==0 );
49714     assert( pWal->readLock>=0 );
49715     walUnlockShared(pWal, WAL_READ_LOCK(pWal->readLock));
49716     pWal->exclusiveMode = 1;
49717     rc = 1;
49718   }else{
49719     rc = pWal->exclusiveMode==0;
49720   }
49721   return rc;
49722 }
49723 
49724 /*
49725 ** Return true if the argument is non-NULL and the WAL module is using
49726 ** heap-memory for the wal-index. Otherwise, if the argument is NULL or the
49727 ** WAL module is using shared-memory, return false.
49728 */
49729 SQLITE_PRIVATE int sqlite3WalHeapMemory(Wal *pWal){
49730   return (pWal && pWal->exclusiveMode==WAL_HEAPMEMORY_MODE );
49731 }
49732 
49733 #ifdef SQLITE_ENABLE_ZIPVFS
49734 /*
49735 ** If the argument is not NULL, it points to a Wal object that holds a
49736 ** read-lock. This function returns the database page-size if it is known,
49737 ** or zero if it is not (or if pWal is NULL).
49738 */
49739 SQLITE_PRIVATE int sqlite3WalFramesize(Wal *pWal){
49740   assert( pWal==0 || pWal->readLock>=0 );
49741   return (pWal ? pWal->szPage : 0);
49742 }
49743 #endif
49744 
49745 #endif /* #ifndef SQLITE_OMIT_WAL */
49746 
49747 /************** End of wal.c *************************************************/
49748 /************** Begin file btmutex.c *****************************************/
49749 /*
49750 ** 2007 August 27
49751 **
49752 ** The author disclaims copyright to this source code.  In place of
49753 ** a legal notice, here is a blessing:
49754 **
49755 **    May you do good and not evil.
49756 **    May you find forgiveness for yourself and forgive others.
49757 **    May you share freely, never taking more than you give.
49758 **
49759 *************************************************************************
49760 **
49761 ** This file contains code used to implement mutexes on Btree objects.
49762 ** This code really belongs in btree.c.  But btree.c is getting too
49763 ** big and we want to break it down some.  This packaged seemed like
49764 ** a good breakout.
49765 */
49766 /************** Include btreeInt.h in the middle of btmutex.c ****************/
49767 /************** Begin file btreeInt.h ****************************************/
49768 /*
49769 ** 2004 April 6
49770 **
49771 ** The author disclaims copyright to this source code.  In place of
49772 ** a legal notice, here is a blessing:
49773 **
49774 **    May you do good and not evil.
49775 **    May you find forgiveness for yourself and forgive others.
49776 **    May you share freely, never taking more than you give.
49777 **
49778 *************************************************************************
49779 ** This file implements a external (disk-based) database using BTrees.
49780 ** For a detailed discussion of BTrees, refer to
49781 **
49782 **     Donald E. Knuth, THE ART OF COMPUTER PROGRAMMING, Volume 3:
49783 **     "Sorting And Searching", pages 473-480. Addison-Wesley
49784 **     Publishing Company, Reading, Massachusetts.
49785 **
49786 ** The basic idea is that each page of the file contains N database
49787 ** entries and N+1 pointers to subpages.
49788 **
49789 **   ----------------------------------------------------------------
49790 **   |  Ptr(0) | Key(0) | Ptr(1) | Key(1) | ... | Key(N-1) | Ptr(N) |
49791 **   ----------------------------------------------------------------
49792 **
49793 ** All of the keys on the page that Ptr(0) points to have values less
49794 ** than Key(0).  All of the keys on page Ptr(1) and its subpages have
49795 ** values greater than Key(0) and less than Key(1).  All of the keys
49796 ** on Ptr(N) and its subpages have values greater than Key(N-1).  And
49797 ** so forth.
49798 **
49799 ** Finding a particular key requires reading O(log(M)) pages from the
49800 ** disk where M is the number of entries in the tree.
49801 **
49802 ** In this implementation, a single file can hold one or more separate
49803 ** BTrees.  Each BTree is identified by the index of its root page.  The
49804 ** key and data for any entry are combined to form the "payload".  A
49805 ** fixed amount of payload can be carried directly on the database
49806 ** page.  If the payload is larger than the preset amount then surplus
49807 ** bytes are stored on overflow pages.  The payload for an entry
49808 ** and the preceding pointer are combined to form a "Cell".  Each
49809 ** page has a small header which contains the Ptr(N) pointer and other
49810 ** information such as the size of key and data.
49811 **
49812 ** FORMAT DETAILS
49813 **
49814 ** The file is divided into pages.  The first page is called page 1,
49815 ** the second is page 2, and so forth.  A page number of zero indicates
49816 ** "no such page".  The page size can be any power of 2 between 512 and 65536.
49817 ** Each page can be either a btree page, a freelist page, an overflow
49818 ** page, or a pointer-map page.
49819 **
49820 ** The first page is always a btree page.  The first 100 bytes of the first
49821 ** page contain a special header (the "file header") that describes the file.
49822 ** The format of the file header is as follows:
49823 **
49824 **   OFFSET   SIZE    DESCRIPTION
49825 **      0      16     Header string: "SQLite format 3\000"
49826 **     16       2     Page size in bytes.  (1 means 65536)
49827 **     18       1     File format write version
49828 **     19       1     File format read version
49829 **     20       1     Bytes of unused space at the end of each page
49830 **     21       1     Max embedded payload fraction (must be 64)
49831 **     22       1     Min embedded payload fraction (must be 32)
49832 **     23       1     Min leaf payload fraction (must be 32)
49833 **     24       4     File change counter
49834 **     28       4     Reserved for future use
49835 **     32       4     First freelist page
49836 **     36       4     Number of freelist pages in the file
49837 **     40      60     15 4-byte meta values passed to higher layers
49838 **
49839 **     40       4     Schema cookie
49840 **     44       4     File format of schema layer
49841 **     48       4     Size of page cache
49842 **     52       4     Largest root-page (auto/incr_vacuum)
49843 **     56       4     1=UTF-8 2=UTF16le 3=UTF16be
49844 **     60       4     User version
49845 **     64       4     Incremental vacuum mode
49846 **     68       4     Application-ID
49847 **     72      20     unused
49848 **     92       4     The version-valid-for number
49849 **     96       4     SQLITE_VERSION_NUMBER
49850 **
49851 ** All of the integer values are big-endian (most significant byte first).
49852 **
49853 ** The file change counter is incremented when the database is changed
49854 ** This counter allows other processes to know when the file has changed
49855 ** and thus when they need to flush their cache.
49856 **
49857 ** The max embedded payload fraction is the amount of the total usable
49858 ** space in a page that can be consumed by a single cell for standard
49859 ** B-tree (non-LEAFDATA) tables.  A value of 255 means 100%.  The default
49860 ** is to limit the maximum cell size so that at least 4 cells will fit
49861 ** on one page.  Thus the default max embedded payload fraction is 64.
49862 **
49863 ** If the payload for a cell is larger than the max payload, then extra
49864 ** payload is spilled to overflow pages.  Once an overflow page is allocated,
49865 ** as many bytes as possible are moved into the overflow pages without letting
49866 ** the cell size drop below the min embedded payload fraction.
49867 **
49868 ** The min leaf payload fraction is like the min embedded payload fraction
49869 ** except that it applies to leaf nodes in a LEAFDATA tree.  The maximum
49870 ** payload fraction for a LEAFDATA tree is always 100% (or 255) and it
49871 ** not specified in the header.
49872 **
49873 ** Each btree pages is divided into three sections:  The header, the
49874 ** cell pointer array, and the cell content area.  Page 1 also has a 100-byte
49875 ** file header that occurs before the page header.
49876 **
49877 **      |----------------|
49878 **      | file header    |   100 bytes.  Page 1 only.
49879 **      |----------------|
49880 **      | page header    |   8 bytes for leaves.  12 bytes for interior nodes
49881 **      |----------------|
49882 **      | cell pointer   |   |  2 bytes per cell.  Sorted order.
49883 **      | array          |   |  Grows downward
49884 **      |                |   v
49885 **      |----------------|
49886 **      | unallocated    |
49887 **      | space          |
49888 **      |----------------|   ^  Grows upwards
49889 **      | cell content   |   |  Arbitrary order interspersed with freeblocks.
49890 **      | area           |   |  and free space fragments.
49891 **      |----------------|
49892 **
49893 ** The page headers looks like this:
49894 **
49895 **   OFFSET   SIZE     DESCRIPTION
49896 **      0       1      Flags. 1: intkey, 2: zerodata, 4: leafdata, 8: leaf
49897 **      1       2      byte offset to the first freeblock
49898 **      3       2      number of cells on this page
49899 **      5       2      first byte of the cell content area
49900 **      7       1      number of fragmented free bytes
49901 **      8       4      Right child (the Ptr(N) value).  Omitted on leaves.
49902 **
49903 ** The flags define the format of this btree page.  The leaf flag means that
49904 ** this page has no children.  The zerodata flag means that this page carries
49905 ** only keys and no data.  The intkey flag means that the key is a integer
49906 ** which is stored in the key size entry of the cell header rather than in
49907 ** the payload area.
49908 **
49909 ** The cell pointer array begins on the first byte after the page header.
49910 ** The cell pointer array contains zero or more 2-byte numbers which are
49911 ** offsets from the beginning of the page to the cell content in the cell
49912 ** content area.  The cell pointers occur in sorted order.  The system strives
49913 ** to keep free space after the last cell pointer so that new cells can
49914 ** be easily added without having to defragment the page.
49915 **
49916 ** Cell content is stored at the very end of the page and grows toward the
49917 ** beginning of the page.
49918 **
49919 ** Unused space within the cell content area is collected into a linked list of
49920 ** freeblocks.  Each freeblock is at least 4 bytes in size.  The byte offset
49921 ** to the first freeblock is given in the header.  Freeblocks occur in
49922 ** increasing order.  Because a freeblock must be at least 4 bytes in size,
49923 ** any group of 3 or fewer unused bytes in the cell content area cannot
49924 ** exist on the freeblock chain.  A group of 3 or fewer free bytes is called
49925 ** a fragment.  The total number of bytes in all fragments is recorded.
49926 ** in the page header at offset 7.
49927 **
49928 **    SIZE    DESCRIPTION
49929 **      2     Byte offset of the next freeblock
49930 **      2     Bytes in this freeblock
49931 **
49932 ** Cells are of variable length.  Cells are stored in the cell content area at
49933 ** the end of the page.  Pointers to the cells are in the cell pointer array
49934 ** that immediately follows the page header.  Cells is not necessarily
49935 ** contiguous or in order, but cell pointers are contiguous and in order.
49936 **
49937 ** Cell content makes use of variable length integers.  A variable
49938 ** length integer is 1 to 9 bytes where the lower 7 bits of each
49939 ** byte are used.  The integer consists of all bytes that have bit 8 set and
49940 ** the first byte with bit 8 clear.  The most significant byte of the integer
49941 ** appears first.  A variable-length integer may not be more than 9 bytes long.
49942 ** As a special case, all 8 bytes of the 9th byte are used as data.  This
49943 ** allows a 64-bit integer to be encoded in 9 bytes.
49944 **
49945 **    0x00                      becomes  0x00000000
49946 **    0x7f                      becomes  0x0000007f
49947 **    0x81 0x00                 becomes  0x00000080
49948 **    0x82 0x00                 becomes  0x00000100
49949 **    0x80 0x7f                 becomes  0x0000007f
49950 **    0x8a 0x91 0xd1 0xac 0x78  becomes  0x12345678
49951 **    0x81 0x81 0x81 0x81 0x01  becomes  0x10204081
49952 **
49953 ** Variable length integers are used for rowids and to hold the number of
49954 ** bytes of key and data in a btree cell.
49955 **
49956 ** The content of a cell looks like this:
49957 **
49958 **    SIZE    DESCRIPTION
49959 **      4     Page number of the left child. Omitted if leaf flag is set.
49960 **     var    Number of bytes of data. Omitted if the zerodata flag is set.
49961 **     var    Number of bytes of key. Or the key itself if intkey flag is set.
49962 **      *     Payload
49963 **      4     First page of the overflow chain.  Omitted if no overflow
49964 **
49965 ** Overflow pages form a linked list.  Each page except the last is completely
49966 ** filled with data (pagesize - 4 bytes).  The last page can have as little
49967 ** as 1 byte of data.
49968 **
49969 **    SIZE    DESCRIPTION
49970 **      4     Page number of next overflow page
49971 **      *     Data
49972 **
49973 ** Freelist pages come in two subtypes: trunk pages and leaf pages.  The
49974 ** file header points to the first in a linked list of trunk page.  Each trunk
49975 ** page points to multiple leaf pages.  The content of a leaf page is
49976 ** unspecified.  A trunk page looks like this:
49977 **
49978 **    SIZE    DESCRIPTION
49979 **      4     Page number of next trunk page
49980 **      4     Number of leaf pointers on this page
49981 **      *     zero or more pages numbers of leaves
49982 */
49983 
49984 
49985 /* The following value is the maximum cell size assuming a maximum page
49986 ** size give above.
49987 */
49988 #define MX_CELL_SIZE(pBt)  ((int)(pBt->pageSize-8))
49989 
49990 /* The maximum number of cells on a single page of the database.  This
49991 ** assumes a minimum cell size of 6 bytes  (4 bytes for the cell itself
49992 ** plus 2 bytes for the index to the cell in the page header).  Such
49993 ** small cells will be rare, but they are possible.
49994 */
49995 #define MX_CELL(pBt) ((pBt->pageSize-8)/6)
49996 
49997 /* Forward declarations */
49998 typedef struct MemPage MemPage;
49999 typedef struct BtLock BtLock;
50000 
50001 /*
50002 ** This is a magic string that appears at the beginning of every
50003 ** SQLite database in order to identify the file as a real database.
50004 **
50005 ** You can change this value at compile-time by specifying a
50006 ** -DSQLITE_FILE_HEADER="..." on the compiler command-line.  The
50007 ** header must be exactly 16 bytes including the zero-terminator so
50008 ** the string itself should be 15 characters long.  If you change
50009 ** the header, then your custom library will not be able to read
50010 ** databases generated by the standard tools and the standard tools
50011 ** will not be able to read databases created by your custom library.
50012 */
50013 #ifndef SQLITE_FILE_HEADER /* 123456789 123456 */
50014 #  define SQLITE_FILE_HEADER "SQLite format 3"
50015 #endif
50016 
50017 /*
50018 ** Page type flags.  An ORed combination of these flags appear as the
50019 ** first byte of on-disk image of every BTree page.
50020 */
50021 #define PTF_INTKEY    0x01
50022 #define PTF_ZERODATA  0x02
50023 #define PTF_LEAFDATA  0x04
50024 #define PTF_LEAF      0x08
50025 
50026 /*
50027 ** As each page of the file is loaded into memory, an instance of the following
50028 ** structure is appended and initialized to zero.  This structure stores
50029 ** information about the page that is decoded from the raw file page.
50030 **
50031 ** The pParent field points back to the parent page.  This allows us to
50032 ** walk up the BTree from any leaf to the root.  Care must be taken to
50033 ** unref() the parent page pointer when this page is no longer referenced.
50034 ** The pageDestructor() routine handles that chore.
50035 **
50036 ** Access to all fields of this structure is controlled by the mutex
50037 ** stored in MemPage.pBt->mutex.
50038 */
50039 struct MemPage {
50040   u8 isInit;           /* True if previously initialized. MUST BE FIRST! */
50041   u8 nOverflow;        /* Number of overflow cell bodies in aCell[] */
50042   u8 intKey;           /* True if intkey flag is set */
50043   u8 leaf;             /* True if leaf flag is set */
50044   u8 hasData;          /* True if this page stores data */
50045   u8 hdrOffset;        /* 100 for page 1.  0 otherwise */
50046   u8 childPtrSize;     /* 0 if leaf==1.  4 if leaf==0 */
50047   u8 max1bytePayload;  /* min(maxLocal,127) */
50048   u16 maxLocal;        /* Copy of BtShared.maxLocal or BtShared.maxLeaf */
50049   u16 minLocal;        /* Copy of BtShared.minLocal or BtShared.minLeaf */
50050   u16 cellOffset;      /* Index in aData of first cell pointer */
50051   u16 nFree;           /* Number of free bytes on the page */
50052   u16 nCell;           /* Number of cells on this page, local and ovfl */
50053   u16 maskPage;        /* Mask for page offset */
50054   u16 aiOvfl[5];       /* Insert the i-th overflow cell before the aiOvfl-th
50055                        ** non-overflow cell */
50056   u8 *apOvfl[5];       /* Pointers to the body of overflow cells */
50057   BtShared *pBt;       /* Pointer to BtShared that this page is part of */
50058   u8 *aData;           /* Pointer to disk image of the page data */
50059   u8 *aDataEnd;        /* One byte past the end of usable data */
50060   u8 *aCellIdx;        /* The cell index area */
50061   DbPage *pDbPage;     /* Pager page handle */
50062   Pgno pgno;           /* Page number for this page */
50063 };
50064 
50065 /*
50066 ** The in-memory image of a disk page has the auxiliary information appended
50067 ** to the end.  EXTRA_SIZE is the number of bytes of space needed to hold
50068 ** that extra information.
50069 */
50070 #define EXTRA_SIZE sizeof(MemPage)
50071 
50072 /*
50073 ** A linked list of the following structures is stored at BtShared.pLock.
50074 ** Locks are added (or upgraded from READ_LOCK to WRITE_LOCK) when a cursor
50075 ** is opened on the table with root page BtShared.iTable. Locks are removed
50076 ** from this list when a transaction is committed or rolled back, or when
50077 ** a btree handle is closed.
50078 */
50079 struct BtLock {
50080   Btree *pBtree;        /* Btree handle holding this lock */
50081   Pgno iTable;          /* Root page of table */
50082   u8 eLock;             /* READ_LOCK or WRITE_LOCK */
50083   BtLock *pNext;        /* Next in BtShared.pLock list */
50084 };
50085 
50086 /* Candidate values for BtLock.eLock */
50087 #define READ_LOCK     1
50088 #define WRITE_LOCK    2
50089 
50090 /* A Btree handle
50091 **
50092 ** A database connection contains a pointer to an instance of
50093 ** this object for every database file that it has open.  This structure
50094 ** is opaque to the database connection.  The database connection cannot
50095 ** see the internals of this structure and only deals with pointers to
50096 ** this structure.
50097 **
50098 ** For some database files, the same underlying database cache might be
50099 ** shared between multiple connections.  In that case, each connection
50100 ** has it own instance of this object.  But each instance of this object
50101 ** points to the same BtShared object.  The database cache and the
50102 ** schema associated with the database file are all contained within
50103 ** the BtShared object.
50104 **
50105 ** All fields in this structure are accessed under sqlite3.mutex.
50106 ** The pBt pointer itself may not be changed while there exists cursors
50107 ** in the referenced BtShared that point back to this Btree since those
50108 ** cursors have to go through this Btree to find their BtShared and
50109 ** they often do so without holding sqlite3.mutex.
50110 */
50111 struct Btree {
50112   sqlite3 *db;       /* The database connection holding this btree */
50113   BtShared *pBt;     /* Sharable content of this btree */
50114   u8 inTrans;        /* TRANS_NONE, TRANS_READ or TRANS_WRITE */
50115   u8 sharable;       /* True if we can share pBt with another db */
50116   u8 locked;         /* True if db currently has pBt locked */
50117   int wantToLock;    /* Number of nested calls to sqlite3BtreeEnter() */
50118   int nBackup;       /* Number of backup operations reading this btree */
50119   Btree *pNext;      /* List of other sharable Btrees from the same db */
50120   Btree *pPrev;      /* Back pointer of the same list */
50121 #ifndef SQLITE_OMIT_SHARED_CACHE
50122   BtLock lock;       /* Object used to lock page 1 */
50123 #endif
50124 };
50125 
50126 /*
50127 ** Btree.inTrans may take one of the following values.
50128 **
50129 ** If the shared-data extension is enabled, there may be multiple users
50130 ** of the Btree structure. At most one of these may open a write transaction,
50131 ** but any number may have active read transactions.
50132 */
50133 #define TRANS_NONE  0
50134 #define TRANS_READ  1
50135 #define TRANS_WRITE 2
50136 
50137 /*
50138 ** An instance of this object represents a single database file.
50139 **
50140 ** A single database file can be in use at the same time by two
50141 ** or more database connections.  When two or more connections are
50142 ** sharing the same database file, each connection has it own
50143 ** private Btree object for the file and each of those Btrees points
50144 ** to this one BtShared object.  BtShared.nRef is the number of
50145 ** connections currently sharing this database file.
50146 **
50147 ** Fields in this structure are accessed under the BtShared.mutex
50148 ** mutex, except for nRef and pNext which are accessed under the
50149 ** global SQLITE_MUTEX_STATIC_MASTER mutex.  The pPager field
50150 ** may not be modified once it is initially set as long as nRef>0.
50151 ** The pSchema field may be set once under BtShared.mutex and
50152 ** thereafter is unchanged as long as nRef>0.
50153 **
50154 ** isPending:
50155 **
50156 **   If a BtShared client fails to obtain a write-lock on a database
50157 **   table (because there exists one or more read-locks on the table),
50158 **   the shared-cache enters 'pending-lock' state and isPending is
50159 **   set to true.
50160 **
50161 **   The shared-cache leaves the 'pending lock' state when either of
50162 **   the following occur:
50163 **
50164 **     1) The current writer (BtShared.pWriter) concludes its transaction, OR
50165 **     2) The number of locks held by other connections drops to zero.
50166 **
50167 **   while in the 'pending-lock' state, no connection may start a new
50168 **   transaction.
50169 **
50170 **   This feature is included to help prevent writer-starvation.
50171 */
50172 struct BtShared {
50173   Pager *pPager;        /* The page cache */
50174   sqlite3 *db;          /* Database connection currently using this Btree */
50175   BtCursor *pCursor;    /* A list of all open cursors */
50176   MemPage *pPage1;      /* First page of the database */
50177   u8 openFlags;         /* Flags to sqlite3BtreeOpen() */
50178 #ifndef SQLITE_OMIT_AUTOVACUUM
50179   u8 autoVacuum;        /* True if auto-vacuum is enabled */
50180   u8 incrVacuum;        /* True if incr-vacuum is enabled */
50181   u8 bDoTruncate;       /* True to truncate db on commit */
50182 #endif
50183   u8 inTransaction;     /* Transaction state */
50184   u8 max1bytePayload;   /* Maximum first byte of cell for a 1-byte payload */
50185   u16 btsFlags;         /* Boolean parameters.  See BTS_* macros below */
50186   u16 maxLocal;         /* Maximum local payload in non-LEAFDATA tables */
50187   u16 minLocal;         /* Minimum local payload in non-LEAFDATA tables */
50188   u16 maxLeaf;          /* Maximum local payload in a LEAFDATA table */
50189   u16 minLeaf;          /* Minimum local payload in a LEAFDATA table */
50190   u32 pageSize;         /* Total number of bytes on a page */
50191   u32 usableSize;       /* Number of usable bytes on each page */
50192   int nTransaction;     /* Number of open transactions (read + write) */
50193   u32 nPage;            /* Number of pages in the database */
50194   void *pSchema;        /* Pointer to space allocated by sqlite3BtreeSchema() */
50195   void (*xFreeSchema)(void*);  /* Destructor for BtShared.pSchema */
50196   sqlite3_mutex *mutex; /* Non-recursive mutex required to access this object */
50197   Bitvec *pHasContent;  /* Set of pages moved to free-list this transaction */
50198 #ifndef SQLITE_OMIT_SHARED_CACHE
50199   int nRef;             /* Number of references to this structure */
50200   BtShared *pNext;      /* Next on a list of sharable BtShared structs */
50201   BtLock *pLock;        /* List of locks held on this shared-btree struct */
50202   Btree *pWriter;       /* Btree with currently open write transaction */
50203 #endif
50204   u8 *pTmpSpace;        /* BtShared.pageSize bytes of space for tmp use */
50205 };
50206 
50207 /*
50208 ** Allowed values for BtShared.btsFlags
50209 */
50210 #define BTS_READ_ONLY        0x0001   /* Underlying file is readonly */
50211 #define BTS_PAGESIZE_FIXED   0x0002   /* Page size can no longer be changed */
50212 #define BTS_SECURE_DELETE    0x0004   /* PRAGMA secure_delete is enabled */
50213 #define BTS_INITIALLY_EMPTY  0x0008   /* Database was empty at trans start */
50214 #define BTS_NO_WAL           0x0010   /* Do not open write-ahead-log files */
50215 #define BTS_EXCLUSIVE        0x0020   /* pWriter has an exclusive lock */
50216 #define BTS_PENDING          0x0040   /* Waiting for read-locks to clear */
50217 
50218 /*
50219 ** An instance of the following structure is used to hold information
50220 ** about a cell.  The parseCellPtr() function fills in this structure
50221 ** based on information extract from the raw disk page.
50222 */
50223 typedef struct CellInfo CellInfo;
50224 struct CellInfo {
50225   i64 nKey;      /* The key for INTKEY tables, or number of bytes in key */
50226   u8 *pCell;     /* Pointer to the start of cell content */
50227   u32 nData;     /* Number of bytes of data */
50228   u32 nPayload;  /* Total amount of payload */
50229   u16 nHeader;   /* Size of the cell content header in bytes */
50230   u16 nLocal;    /* Amount of payload held locally */
50231   u16 iOverflow; /* Offset to overflow page number.  Zero if no overflow */
50232   u16 nSize;     /* Size of the cell content on the main b-tree page */
50233 };
50234 
50235 /*
50236 ** Maximum depth of an SQLite B-Tree structure. Any B-Tree deeper than
50237 ** this will be declared corrupt. This value is calculated based on a
50238 ** maximum database size of 2^31 pages a minimum fanout of 2 for a
50239 ** root-node and 3 for all other internal nodes.
50240 **
50241 ** If a tree that appears to be taller than this is encountered, it is
50242 ** assumed that the database is corrupt.
50243 */
50244 #define BTCURSOR_MAX_DEPTH 20
50245 
50246 /*
50247 ** A cursor is a pointer to a particular entry within a particular
50248 ** b-tree within a database file.
50249 **
50250 ** The entry is identified by its MemPage and the index in
50251 ** MemPage.aCell[] of the entry.
50252 **
50253 ** A single database file can be shared by two more database connections,
50254 ** but cursors cannot be shared.  Each cursor is associated with a
50255 ** particular database connection identified BtCursor.pBtree.db.
50256 **
50257 ** Fields in this structure are accessed under the BtShared.mutex
50258 ** found at self->pBt->mutex.
50259 */
50260 struct BtCursor {
50261   Btree *pBtree;            /* The Btree to which this cursor belongs */
50262   BtShared *pBt;            /* The BtShared this cursor points to */
50263   BtCursor *pNext, *pPrev;  /* Forms a linked list of all cursors */
50264   struct KeyInfo *pKeyInfo; /* Argument passed to comparison function */
50265 #ifndef SQLITE_OMIT_INCRBLOB
50266   Pgno *aOverflow;          /* Cache of overflow page locations */
50267 #endif
50268   Pgno pgnoRoot;            /* The root page of this tree */
50269   sqlite3_int64 cachedRowid; /* Next rowid cache.  0 means not valid */
50270   CellInfo info;            /* A parse of the cell we are pointing at */
50271   i64 nKey;        /* Size of pKey, or last integer key */
50272   void *pKey;      /* Saved key that was cursor's last known position */
50273   int skipNext;    /* Prev() is noop if negative. Next() is noop if positive */
50274   u8 wrFlag;                /* True if writable */
50275   u8 atLast;                /* Cursor pointing to the last entry */
50276   u8 validNKey;             /* True if info.nKey is valid */
50277   u8 eState;                /* One of the CURSOR_XXX constants (see below) */
50278 #ifndef SQLITE_OMIT_INCRBLOB
50279   u8 isIncrblobHandle;      /* True if this cursor is an incr. io handle */
50280 #endif
50281   u8 hints;                             /* As configured by CursorSetHints() */
50282   i16 iPage;                            /* Index of current page in apPage */
50283   u16 aiIdx[BTCURSOR_MAX_DEPTH];        /* Current index in apPage[i] */
50284   MemPage *apPage[BTCURSOR_MAX_DEPTH];  /* Pages from root to current page */
50285 };
50286 
50287 /*
50288 ** Potential values for BtCursor.eState.
50289 **
50290 ** CURSOR_INVALID:
50291 **   Cursor does not point to a valid entry. This can happen (for example)
50292 **   because the table is empty or because BtreeCursorFirst() has not been
50293 **   called.
50294 **
50295 ** CURSOR_VALID:
50296 **   Cursor points to a valid entry. getPayload() etc. may be called.
50297 **
50298 ** CURSOR_SKIPNEXT:
50299 **   Cursor is valid except that the Cursor.skipNext field is non-zero
50300 **   indicating that the next sqlite3BtreeNext() or sqlite3BtreePrevious()
50301 **   operation should be a no-op.
50302 **
50303 ** CURSOR_REQUIRESEEK:
50304 **   The table that this cursor was opened on still exists, but has been
50305 **   modified since the cursor was last used. The cursor position is saved
50306 **   in variables BtCursor.pKey and BtCursor.nKey. When a cursor is in
50307 **   this state, restoreCursorPosition() can be called to attempt to
50308 **   seek the cursor to the saved position.
50309 **
50310 ** CURSOR_FAULT:
50311 **   A unrecoverable error (an I/O error or a malloc failure) has occurred
50312 **   on a different connection that shares the BtShared cache with this
50313 **   cursor.  The error has left the cache in an inconsistent state.
50314 **   Do nothing else with this cursor.  Any attempt to use the cursor
50315 **   should return the error code stored in BtCursor.skip
50316 */
50317 #define CURSOR_INVALID           0
50318 #define CURSOR_VALID             1
50319 #define CURSOR_SKIPNEXT          2
50320 #define CURSOR_REQUIRESEEK       3
50321 #define CURSOR_FAULT             4
50322 
50323 /*
50324 ** The database page the PENDING_BYTE occupies. This page is never used.
50325 */
50326 # define PENDING_BYTE_PAGE(pBt) PAGER_MJ_PGNO(pBt)
50327 
50328 /*
50329 ** These macros define the location of the pointer-map entry for a
50330 ** database page. The first argument to each is the number of usable
50331 ** bytes on each page of the database (often 1024). The second is the
50332 ** page number to look up in the pointer map.
50333 **
50334 ** PTRMAP_PAGENO returns the database page number of the pointer-map
50335 ** page that stores the required pointer. PTRMAP_PTROFFSET returns
50336 ** the offset of the requested map entry.
50337 **
50338 ** If the pgno argument passed to PTRMAP_PAGENO is a pointer-map page,
50339 ** then pgno is returned. So (pgno==PTRMAP_PAGENO(pgsz, pgno)) can be
50340 ** used to test if pgno is a pointer-map page. PTRMAP_ISPAGE implements
50341 ** this test.
50342 */
50343 #define PTRMAP_PAGENO(pBt, pgno) ptrmapPageno(pBt, pgno)
50344 #define PTRMAP_PTROFFSET(pgptrmap, pgno) (5*(pgno-pgptrmap-1))
50345 #define PTRMAP_ISPAGE(pBt, pgno) (PTRMAP_PAGENO((pBt),(pgno))==(pgno))
50346 
50347 /*
50348 ** The pointer map is a lookup table that identifies the parent page for
50349 ** each child page in the database file.  The parent page is the page that
50350 ** contains a pointer to the child.  Every page in the database contains
50351 ** 0 or 1 parent pages.  (In this context 'database page' refers
50352 ** to any page that is not part of the pointer map itself.)  Each pointer map
50353 ** entry consists of a single byte 'type' and a 4 byte parent page number.
50354 ** The PTRMAP_XXX identifiers below are the valid types.
50355 **
50356 ** The purpose of the pointer map is to facility moving pages from one
50357 ** position in the file to another as part of autovacuum.  When a page
50358 ** is moved, the pointer in its parent must be updated to point to the
50359 ** new location.  The pointer map is used to locate the parent page quickly.
50360 **
50361 ** PTRMAP_ROOTPAGE: The database page is a root-page. The page-number is not
50362 **                  used in this case.
50363 **
50364 ** PTRMAP_FREEPAGE: The database page is an unused (free) page. The page-number
50365 **                  is not used in this case.
50366 **
50367 ** PTRMAP_OVERFLOW1: The database page is the first page in a list of
50368 **                   overflow pages. The page number identifies the page that
50369 **                   contains the cell with a pointer to this overflow page.
50370 **
50371 ** PTRMAP_OVERFLOW2: The database page is the second or later page in a list of
50372 **                   overflow pages. The page-number identifies the previous
50373 **                   page in the overflow page list.
50374 **
50375 ** PTRMAP_BTREE: The database page is a non-root btree page. The page number
50376 **               identifies the parent page in the btree.
50377 */
50378 #define PTRMAP_ROOTPAGE 1
50379 #define PTRMAP_FREEPAGE 2
50380 #define PTRMAP_OVERFLOW1 3
50381 #define PTRMAP_OVERFLOW2 4
50382 #define PTRMAP_BTREE 5
50383 
50384 /* A bunch of assert() statements to check the transaction state variables
50385 ** of handle p (type Btree*) are internally consistent.
50386 */
50387 #define btreeIntegrity(p) \
50388   assert( p->pBt->inTransaction!=TRANS_NONE || p->pBt->nTransaction==0 ); \
50389   assert( p->pBt->inTransaction>=p->inTrans );
50390 
50391 
50392 /*
50393 ** The ISAUTOVACUUM macro is used within balance_nonroot() to determine
50394 ** if the database supports auto-vacuum or not. Because it is used
50395 ** within an expression that is an argument to another macro
50396 ** (sqliteMallocRaw), it is not possible to use conditional compilation.
50397 ** So, this macro is defined instead.
50398 */
50399 #ifndef SQLITE_OMIT_AUTOVACUUM
50400 #define ISAUTOVACUUM (pBt->autoVacuum)
50401 #else
50402 #define ISAUTOVACUUM 0
50403 #endif
50404 
50405 
50406 /*
50407 ** This structure is passed around through all the sanity checking routines
50408 ** in order to keep track of some global state information.
50409 **
50410 ** The aRef[] array is allocated so that there is 1 bit for each page in
50411 ** the database. As the integrity-check proceeds, for each page used in
50412 ** the database the corresponding bit is set. This allows integrity-check to
50413 ** detect pages that are used twice and orphaned pages (both of which
50414 ** indicate corruption).
50415 */
50416 typedef struct IntegrityCk IntegrityCk;
50417 struct IntegrityCk {
50418   BtShared *pBt;    /* The tree being checked out */
50419   Pager *pPager;    /* The associated pager.  Also accessible by pBt->pPager */
50420   u8 *aPgRef;       /* 1 bit per page in the db (see above) */
50421   Pgno nPage;       /* Number of pages in the database */
50422   int mxErr;        /* Stop accumulating errors when this reaches zero */
50423   int nErr;         /* Number of messages written to zErrMsg so far */
50424   int mallocFailed; /* A memory allocation error has occurred */
50425   StrAccum errMsg;  /* Accumulate the error message text here */
50426 };
50427 
50428 /*
50429 ** Routines to read or write a two- and four-byte big-endian integer values.
50430 */
50431 #define get2byte(x)   ((x)[0]<<8 | (x)[1])
50432 #define put2byte(p,v) ((p)[0] = (u8)((v)>>8), (p)[1] = (u8)(v))
50433 #define get4byte sqlite3Get4byte
50434 #define put4byte sqlite3Put4byte
50435 
50436 /************** End of btreeInt.h ********************************************/
50437 /************** Continuing where we left off in btmutex.c ********************/
50438 #ifndef SQLITE_OMIT_SHARED_CACHE
50439 #if SQLITE_THREADSAFE
50440 
50441 /*
50442 ** Obtain the BtShared mutex associated with B-Tree handle p. Also,
50443 ** set BtShared.db to the database handle associated with p and the
50444 ** p->locked boolean to true.
50445 */
50446 static void lockBtreeMutex(Btree *p){
50447   assert( p->locked==0 );
50448   assert( sqlite3_mutex_notheld(p->pBt->mutex) );
50449   assert( sqlite3_mutex_held(p->db->mutex) );
50450 
50451   sqlite3_mutex_enter(p->pBt->mutex);
50452   p->pBt->db = p->db;
50453   p->locked = 1;
50454 }
50455 
50456 /*
50457 ** Release the BtShared mutex associated with B-Tree handle p and
50458 ** clear the p->locked boolean.
50459 */
50460 static void unlockBtreeMutex(Btree *p){
50461   BtShared *pBt = p->pBt;
50462   assert( p->locked==1 );
50463   assert( sqlite3_mutex_held(pBt->mutex) );
50464   assert( sqlite3_mutex_held(p->db->mutex) );
50465   assert( p->db==pBt->db );
50466 
50467   sqlite3_mutex_leave(pBt->mutex);
50468   p->locked = 0;
50469 }
50470 
50471 /*
50472 ** Enter a mutex on the given BTree object.
50473 **
50474 ** If the object is not sharable, then no mutex is ever required
50475 ** and this routine is a no-op.  The underlying mutex is non-recursive.
50476 ** But we keep a reference count in Btree.wantToLock so the behavior
50477 ** of this interface is recursive.
50478 **
50479 ** To avoid deadlocks, multiple Btrees are locked in the same order
50480 ** by all database connections.  The p->pNext is a list of other
50481 ** Btrees belonging to the same database connection as the p Btree
50482 ** which need to be locked after p.  If we cannot get a lock on
50483 ** p, then first unlock all of the others on p->pNext, then wait
50484 ** for the lock to become available on p, then relock all of the
50485 ** subsequent Btrees that desire a lock.
50486 */
50487 SQLITE_PRIVATE void sqlite3BtreeEnter(Btree *p){
50488   Btree *pLater;
50489 
50490   /* Some basic sanity checking on the Btree.  The list of Btrees
50491   ** connected by pNext and pPrev should be in sorted order by
50492   ** Btree.pBt value. All elements of the list should belong to
50493   ** the same connection. Only shared Btrees are on the list. */
50494   assert( p->pNext==0 || p->pNext->pBt>p->pBt );
50495   assert( p->pPrev==0 || p->pPrev->pBt<p->pBt );
50496   assert( p->pNext==0 || p->pNext->db==p->db );
50497   assert( p->pPrev==0 || p->pPrev->db==p->db );
50498   assert( p->sharable || (p->pNext==0 && p->pPrev==0) );
50499 
50500   /* Check for locking consistency */
50501   assert( !p->locked || p->wantToLock>0 );
50502   assert( p->sharable || p->wantToLock==0 );
50503 
50504   /* We should already hold a lock on the database connection */
50505   assert( sqlite3_mutex_held(p->db->mutex) );
50506 
50507   /* Unless the database is sharable and unlocked, then BtShared.db
50508   ** should already be set correctly. */
50509   assert( (p->locked==0 && p->sharable) || p->pBt->db==p->db );
50510 
50511   if( !p->sharable ) return;
50512   p->wantToLock++;
50513   if( p->locked ) return;
50514 
50515   /* In most cases, we should be able to acquire the lock we
50516   ** want without having to go throught the ascending lock
50517   ** procedure that follows.  Just be sure not to block.
50518   */
50519   if( sqlite3_mutex_try(p->pBt->mutex)==SQLITE_OK ){
50520     p->pBt->db = p->db;
50521     p->locked = 1;
50522     return;
50523   }
50524 
50525   /* To avoid deadlock, first release all locks with a larger
50526   ** BtShared address.  Then acquire our lock.  Then reacquire
50527   ** the other BtShared locks that we used to hold in ascending
50528   ** order.
50529   */
50530   for(pLater=p->pNext; pLater; pLater=pLater->pNext){
50531     assert( pLater->sharable );
50532     assert( pLater->pNext==0 || pLater->pNext->pBt>pLater->pBt );
50533     assert( !pLater->locked || pLater->wantToLock>0 );
50534     if( pLater->locked ){
50535       unlockBtreeMutex(pLater);
50536     }
50537   }
50538   lockBtreeMutex(p);
50539   for(pLater=p->pNext; pLater; pLater=pLater->pNext){
50540     if( pLater->wantToLock ){
50541       lockBtreeMutex(pLater);
50542     }
50543   }
50544 }
50545 
50546 /*
50547 ** Exit the recursive mutex on a Btree.
50548 */
50549 SQLITE_PRIVATE void sqlite3BtreeLeave(Btree *p){
50550   if( p->sharable ){
50551     assert( p->wantToLock>0 );
50552     p->wantToLock--;
50553     if( p->wantToLock==0 ){
50554       unlockBtreeMutex(p);
50555     }
50556   }
50557 }
50558 
50559 #ifndef NDEBUG
50560 /*
50561 ** Return true if the BtShared mutex is held on the btree, or if the
50562 ** B-Tree is not marked as sharable.
50563 **
50564 ** This routine is used only from within assert() statements.
50565 */
50566 SQLITE_PRIVATE int sqlite3BtreeHoldsMutex(Btree *p){
50567   assert( p->sharable==0 || p->locked==0 || p->wantToLock>0 );
50568   assert( p->sharable==0 || p->locked==0 || p->db==p->pBt->db );
50569   assert( p->sharable==0 || p->locked==0 || sqlite3_mutex_held(p->pBt->mutex) );
50570   assert( p->sharable==0 || p->locked==0 || sqlite3_mutex_held(p->db->mutex) );
50571 
50572   return (p->sharable==0 || p->locked);
50573 }
50574 #endif
50575 
50576 
50577 #ifndef SQLITE_OMIT_INCRBLOB
50578 /*
50579 ** Enter and leave a mutex on a Btree given a cursor owned by that
50580 ** Btree.  These entry points are used by incremental I/O and can be
50581 ** omitted if that module is not used.
50582 */
50583 SQLITE_PRIVATE void sqlite3BtreeEnterCursor(BtCursor *pCur){
50584   sqlite3BtreeEnter(pCur->pBtree);
50585 }
50586 SQLITE_PRIVATE void sqlite3BtreeLeaveCursor(BtCursor *pCur){
50587   sqlite3BtreeLeave(pCur->pBtree);
50588 }
50589 #endif /* SQLITE_OMIT_INCRBLOB */
50590 
50591 
50592 /*
50593 ** Enter the mutex on every Btree associated with a database
50594 ** connection.  This is needed (for example) prior to parsing
50595 ** a statement since we will be comparing table and column names
50596 ** against all schemas and we do not want those schemas being
50597 ** reset out from under us.
50598 **
50599 ** There is a corresponding leave-all procedures.
50600 **
50601 ** Enter the mutexes in accending order by BtShared pointer address
50602 ** to avoid the possibility of deadlock when two threads with
50603 ** two or more btrees in common both try to lock all their btrees
50604 ** at the same instant.
50605 */
50606 SQLITE_PRIVATE void sqlite3BtreeEnterAll(sqlite3 *db){
50607   int i;
50608   Btree *p;
50609   assert( sqlite3_mutex_held(db->mutex) );
50610   for(i=0; i<db->nDb; i++){
50611     p = db->aDb[i].pBt;
50612     if( p ) sqlite3BtreeEnter(p);
50613   }
50614 }
50615 SQLITE_PRIVATE void sqlite3BtreeLeaveAll(sqlite3 *db){
50616   int i;
50617   Btree *p;
50618   assert( sqlite3_mutex_held(db->mutex) );
50619   for(i=0; i<db->nDb; i++){
50620     p = db->aDb[i].pBt;
50621     if( p ) sqlite3BtreeLeave(p);
50622   }
50623 }
50624 
50625 /*
50626 ** Return true if a particular Btree requires a lock.  Return FALSE if
50627 ** no lock is ever required since it is not sharable.
50628 */
50629 SQLITE_PRIVATE int sqlite3BtreeSharable(Btree *p){
50630   return p->sharable;
50631 }
50632 
50633 #ifndef NDEBUG
50634 /*
50635 ** Return true if the current thread holds the database connection
50636 ** mutex and all required BtShared mutexes.
50637 **
50638 ** This routine is used inside assert() statements only.
50639 */
50640 SQLITE_PRIVATE int sqlite3BtreeHoldsAllMutexes(sqlite3 *db){
50641   int i;
50642   if( !sqlite3_mutex_held(db->mutex) ){
50643     return 0;
50644   }
50645   for(i=0; i<db->nDb; i++){
50646     Btree *p;
50647     p = db->aDb[i].pBt;
50648     if( p && p->sharable &&
50649          (p->wantToLock==0 || !sqlite3_mutex_held(p->pBt->mutex)) ){
50650       return 0;
50651     }
50652   }
50653   return 1;
50654 }
50655 #endif /* NDEBUG */
50656 
50657 #ifndef NDEBUG
50658 /*
50659 ** Return true if the correct mutexes are held for accessing the
50660 ** db->aDb[iDb].pSchema structure.  The mutexes required for schema
50661 ** access are:
50662 **
50663 **   (1) The mutex on db
50664 **   (2) if iDb!=1, then the mutex on db->aDb[iDb].pBt.
50665 **
50666 ** If pSchema is not NULL, then iDb is computed from pSchema and
50667 ** db using sqlite3SchemaToIndex().
50668 */
50669 SQLITE_PRIVATE int sqlite3SchemaMutexHeld(sqlite3 *db, int iDb, Schema *pSchema){
50670   Btree *p;
50671   assert( db!=0 );
50672   if( pSchema ) iDb = sqlite3SchemaToIndex(db, pSchema);
50673   assert( iDb>=0 && iDb<db->nDb );
50674   if( !sqlite3_mutex_held(db->mutex) ) return 0;
50675   if( iDb==1 ) return 1;
50676   p = db->aDb[iDb].pBt;
50677   assert( p!=0 );
50678   return p->sharable==0 || p->locked==1;
50679 }
50680 #endif /* NDEBUG */
50681 
50682 #else /* SQLITE_THREADSAFE>0 above.  SQLITE_THREADSAFE==0 below */
50683 /*
50684 ** The following are special cases for mutex enter routines for use
50685 ** in single threaded applications that use shared cache.  Except for
50686 ** these two routines, all mutex operations are no-ops in that case and
50687 ** are null #defines in btree.h.
50688 **
50689 ** If shared cache is disabled, then all btree mutex routines, including
50690 ** the ones below, are no-ops and are null #defines in btree.h.
50691 */
50692 
50693 SQLITE_PRIVATE void sqlite3BtreeEnter(Btree *p){
50694   p->pBt->db = p->db;
50695 }
50696 SQLITE_PRIVATE void sqlite3BtreeEnterAll(sqlite3 *db){
50697   int i;
50698   for(i=0; i<db->nDb; i++){
50699     Btree *p = db->aDb[i].pBt;
50700     if( p ){
50701       p->pBt->db = p->db;
50702     }
50703   }
50704 }
50705 #endif /* if SQLITE_THREADSAFE */
50706 #endif /* ifndef SQLITE_OMIT_SHARED_CACHE */
50707 
50708 /************** End of btmutex.c *********************************************/
50709 /************** Begin file btree.c *******************************************/
50710 /*
50711 ** 2004 April 6
50712 **
50713 ** The author disclaims copyright to this source code.  In place of
50714 ** a legal notice, here is a blessing:
50715 **
50716 **    May you do good and not evil.
50717 **    May you find forgiveness for yourself and forgive others.
50718 **    May you share freely, never taking more than you give.
50719 **
50720 *************************************************************************
50721 ** This file implements a external (disk-based) database using BTrees.
50722 ** See the header comment on "btreeInt.h" for additional information.
50723 ** Including a description of file format and an overview of operation.
50724 */
50725 
50726 /*
50727 ** The header string that appears at the beginning of every
50728 ** SQLite database.
50729 */
50730 static const char zMagicHeader[] = SQLITE_FILE_HEADER;
50731 
50732 /*
50733 ** Set this global variable to 1 to enable tracing using the TRACE
50734 ** macro.
50735 */
50736 #if 0
50737 int sqlite3BtreeTrace=1;  /* True to enable tracing */
50738 # define TRACE(X)  if(sqlite3BtreeTrace){printf X;fflush(stdout);}
50739 #else
50740 # define TRACE(X)
50741 #endif
50742 
50743 /*
50744 ** Extract a 2-byte big-endian integer from an array of unsigned bytes.
50745 ** But if the value is zero, make it 65536.
50746 **
50747 ** This routine is used to extract the "offset to cell content area" value
50748 ** from the header of a btree page.  If the page size is 65536 and the page
50749 ** is empty, the offset should be 65536, but the 2-byte value stores zero.
50750 ** This routine makes the necessary adjustment to 65536.
50751 */
50752 #define get2byteNotZero(X)  (((((int)get2byte(X))-1)&0xffff)+1)
50753 
50754 /*
50755 ** Values passed as the 5th argument to allocateBtreePage()
50756 */
50757 #define BTALLOC_ANY   0           /* Allocate any page */
50758 #define BTALLOC_EXACT 1           /* Allocate exact page if possible */
50759 #define BTALLOC_LE    2           /* Allocate any page <= the parameter */
50760 
50761 /*
50762 ** Macro IfNotOmitAV(x) returns (x) if SQLITE_OMIT_AUTOVACUUM is not
50763 ** defined, or 0 if it is. For example:
50764 **
50765 **   bIncrVacuum = IfNotOmitAV(pBtShared->incrVacuum);
50766 */
50767 #ifndef SQLITE_OMIT_AUTOVACUUM
50768 #define IfNotOmitAV(expr) (expr)
50769 #else
50770 #define IfNotOmitAV(expr) 0
50771 #endif
50772 
50773 #ifndef SQLITE_OMIT_SHARED_CACHE
50774 /*
50775 ** A list of BtShared objects that are eligible for participation
50776 ** in shared cache.  This variable has file scope during normal builds,
50777 ** but the test harness needs to access it so we make it global for
50778 ** test builds.
50779 **
50780 ** Access to this variable is protected by SQLITE_MUTEX_STATIC_MASTER.
50781 */
50782 #ifdef SQLITE_TEST
50783 SQLITE_PRIVATE BtShared *SQLITE_WSD sqlite3SharedCacheList = 0;
50784 #else
50785 static BtShared *SQLITE_WSD sqlite3SharedCacheList = 0;
50786 #endif
50787 #endif /* SQLITE_OMIT_SHARED_CACHE */
50788 
50789 #ifndef SQLITE_OMIT_SHARED_CACHE
50790 /*
50791 ** Enable or disable the shared pager and schema features.
50792 **
50793 ** This routine has no effect on existing database connections.
50794 ** The shared cache setting effects only future calls to
50795 ** sqlite3_open(), sqlite3_open16(), or sqlite3_open_v2().
50796 */
50797 SQLITE_API int sqlite3_enable_shared_cache(int enable){
50798   sqlite3GlobalConfig.sharedCacheEnabled = enable;
50799   return SQLITE_OK;
50800 }
50801 #endif
50802 
50803 
50804 
50805 #ifdef SQLITE_OMIT_SHARED_CACHE
50806   /*
50807   ** The functions querySharedCacheTableLock(), setSharedCacheTableLock(),
50808   ** and clearAllSharedCacheTableLocks()
50809   ** manipulate entries in the BtShared.pLock linked list used to store
50810   ** shared-cache table level locks. If the library is compiled with the
50811   ** shared-cache feature disabled, then there is only ever one user
50812   ** of each BtShared structure and so this locking is not necessary.
50813   ** So define the lock related functions as no-ops.
50814   */
50815   #define querySharedCacheTableLock(a,b,c) SQLITE_OK
50816   #define setSharedCacheTableLock(a,b,c) SQLITE_OK
50817   #define clearAllSharedCacheTableLocks(a)
50818   #define downgradeAllSharedCacheTableLocks(a)
50819   #define hasSharedCacheTableLock(a,b,c,d) 1
50820   #define hasReadConflicts(a, b) 0
50821 #endif
50822 
50823 #ifndef SQLITE_OMIT_SHARED_CACHE
50824 
50825 #ifdef SQLITE_DEBUG
50826 /*
50827 **** This function is only used as part of an assert() statement. ***
50828 **
50829 ** Check to see if pBtree holds the required locks to read or write to the
50830 ** table with root page iRoot.   Return 1 if it does and 0 if not.
50831 **
50832 ** For example, when writing to a table with root-page iRoot via
50833 ** Btree connection pBtree:
50834 **
50835 **    assert( hasSharedCacheTableLock(pBtree, iRoot, 0, WRITE_LOCK) );
50836 **
50837 ** When writing to an index that resides in a sharable database, the
50838 ** caller should have first obtained a lock specifying the root page of
50839 ** the corresponding table. This makes things a bit more complicated,
50840 ** as this module treats each table as a separate structure. To determine
50841 ** the table corresponding to the index being written, this
50842 ** function has to search through the database schema.
50843 **
50844 ** Instead of a lock on the table/index rooted at page iRoot, the caller may
50845 ** hold a write-lock on the schema table (root page 1). This is also
50846 ** acceptable.
50847 */
50848 static int hasSharedCacheTableLock(
50849   Btree *pBtree,         /* Handle that must hold lock */
50850   Pgno iRoot,            /* Root page of b-tree */
50851   int isIndex,           /* True if iRoot is the root of an index b-tree */
50852   int eLockType          /* Required lock type (READ_LOCK or WRITE_LOCK) */
50853 ){
50854   Schema *pSchema = (Schema *)pBtree->pBt->pSchema;
50855   Pgno iTab = 0;
50856   BtLock *pLock;
50857 
50858   /* If this database is not shareable, or if the client is reading
50859   ** and has the read-uncommitted flag set, then no lock is required.
50860   ** Return true immediately.
50861   */
50862   if( (pBtree->sharable==0)
50863    || (eLockType==READ_LOCK && (pBtree->db->flags & SQLITE_ReadUncommitted))
50864   ){
50865     return 1;
50866   }
50867 
50868   /* If the client is reading  or writing an index and the schema is
50869   ** not loaded, then it is too difficult to actually check to see if
50870   ** the correct locks are held.  So do not bother - just return true.
50871   ** This case does not come up very often anyhow.
50872   */
50873   if( isIndex && (!pSchema || (pSchema->flags&DB_SchemaLoaded)==0) ){
50874     return 1;
50875   }
50876 
50877   /* Figure out the root-page that the lock should be held on. For table
50878   ** b-trees, this is just the root page of the b-tree being read or
50879   ** written. For index b-trees, it is the root page of the associated
50880   ** table.  */
50881   if( isIndex ){
50882     HashElem *p;
50883     for(p=sqliteHashFirst(&pSchema->idxHash); p; p=sqliteHashNext(p)){
50884       Index *pIdx = (Index *)sqliteHashData(p);
50885       if( pIdx->tnum==(int)iRoot ){
50886         iTab = pIdx->pTable->tnum;
50887       }
50888     }
50889   }else{
50890     iTab = iRoot;
50891   }
50892 
50893   /* Search for the required lock. Either a write-lock on root-page iTab, a
50894   ** write-lock on the schema table, or (if the client is reading) a
50895   ** read-lock on iTab will suffice. Return 1 if any of these are found.  */
50896   for(pLock=pBtree->pBt->pLock; pLock; pLock=pLock->pNext){
50897     if( pLock->pBtree==pBtree
50898      && (pLock->iTable==iTab || (pLock->eLock==WRITE_LOCK && pLock->iTable==1))
50899      && pLock->eLock>=eLockType
50900     ){
50901       return 1;
50902     }
50903   }
50904 
50905   /* Failed to find the required lock. */
50906   return 0;
50907 }
50908 #endif /* SQLITE_DEBUG */
50909 
50910 #ifdef SQLITE_DEBUG
50911 /*
50912 **** This function may be used as part of assert() statements only. ****
50913 **
50914 ** Return true if it would be illegal for pBtree to write into the
50915 ** table or index rooted at iRoot because other shared connections are
50916 ** simultaneously reading that same table or index.
50917 **
50918 ** It is illegal for pBtree to write if some other Btree object that
50919 ** shares the same BtShared object is currently reading or writing
50920 ** the iRoot table.  Except, if the other Btree object has the
50921 ** read-uncommitted flag set, then it is OK for the other object to
50922 ** have a read cursor.
50923 **
50924 ** For example, before writing to any part of the table or index
50925 ** rooted at page iRoot, one should call:
50926 **
50927 **    assert( !hasReadConflicts(pBtree, iRoot) );
50928 */
50929 static int hasReadConflicts(Btree *pBtree, Pgno iRoot){
50930   BtCursor *p;
50931   for(p=pBtree->pBt->pCursor; p; p=p->pNext){
50932     if( p->pgnoRoot==iRoot
50933      && p->pBtree!=pBtree
50934      && 0==(p->pBtree->db->flags & SQLITE_ReadUncommitted)
50935     ){
50936       return 1;
50937     }
50938   }
50939   return 0;
50940 }
50941 #endif    /* #ifdef SQLITE_DEBUG */
50942 
50943 /*
50944 ** Query to see if Btree handle p may obtain a lock of type eLock
50945 ** (READ_LOCK or WRITE_LOCK) on the table with root-page iTab. Return
50946 ** SQLITE_OK if the lock may be obtained (by calling
50947 ** setSharedCacheTableLock()), or SQLITE_LOCKED if not.
50948 */
50949 static int querySharedCacheTableLock(Btree *p, Pgno iTab, u8 eLock){
50950   BtShared *pBt = p->pBt;
50951   BtLock *pIter;
50952 
50953   assert( sqlite3BtreeHoldsMutex(p) );
50954   assert( eLock==READ_LOCK || eLock==WRITE_LOCK );
50955   assert( p->db!=0 );
50956   assert( !(p->db->flags&SQLITE_ReadUncommitted)||eLock==WRITE_LOCK||iTab==1 );
50957 
50958   /* If requesting a write-lock, then the Btree must have an open write
50959   ** transaction on this file. And, obviously, for this to be so there
50960   ** must be an open write transaction on the file itself.
50961   */
50962   assert( eLock==READ_LOCK || (p==pBt->pWriter && p->inTrans==TRANS_WRITE) );
50963   assert( eLock==READ_LOCK || pBt->inTransaction==TRANS_WRITE );
50964 
50965   /* This routine is a no-op if the shared-cache is not enabled */
50966   if( !p->sharable ){
50967     return SQLITE_OK;
50968   }
50969 
50970   /* If some other connection is holding an exclusive lock, the
50971   ** requested lock may not be obtained.
50972   */
50973   if( pBt->pWriter!=p && (pBt->btsFlags & BTS_EXCLUSIVE)!=0 ){
50974     sqlite3ConnectionBlocked(p->db, pBt->pWriter->db);
50975     return SQLITE_LOCKED_SHAREDCACHE;
50976   }
50977 
50978   for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
50979     /* The condition (pIter->eLock!=eLock) in the following if(...)
50980     ** statement is a simplification of:
50981     **
50982     **   (eLock==WRITE_LOCK || pIter->eLock==WRITE_LOCK)
50983     **
50984     ** since we know that if eLock==WRITE_LOCK, then no other connection
50985     ** may hold a WRITE_LOCK on any table in this file (since there can
50986     ** only be a single writer).
50987     */
50988     assert( pIter->eLock==READ_LOCK || pIter->eLock==WRITE_LOCK );
50989     assert( eLock==READ_LOCK || pIter->pBtree==p || pIter->eLock==READ_LOCK);
50990     if( pIter->pBtree!=p && pIter->iTable==iTab && pIter->eLock!=eLock ){
50991       sqlite3ConnectionBlocked(p->db, pIter->pBtree->db);
50992       if( eLock==WRITE_LOCK ){
50993         assert( p==pBt->pWriter );
50994         pBt->btsFlags |= BTS_PENDING;
50995       }
50996       return SQLITE_LOCKED_SHAREDCACHE;
50997     }
50998   }
50999   return SQLITE_OK;
51000 }
51001 #endif /* !SQLITE_OMIT_SHARED_CACHE */
51002 
51003 #ifndef SQLITE_OMIT_SHARED_CACHE
51004 /*
51005 ** Add a lock on the table with root-page iTable to the shared-btree used
51006 ** by Btree handle p. Parameter eLock must be either READ_LOCK or
51007 ** WRITE_LOCK.
51008 **
51009 ** This function assumes the following:
51010 **
51011 **   (a) The specified Btree object p is connected to a sharable
51012 **       database (one with the BtShared.sharable flag set), and
51013 **
51014 **   (b) No other Btree objects hold a lock that conflicts
51015 **       with the requested lock (i.e. querySharedCacheTableLock() has
51016 **       already been called and returned SQLITE_OK).
51017 **
51018 ** SQLITE_OK is returned if the lock is added successfully. SQLITE_NOMEM
51019 ** is returned if a malloc attempt fails.
51020 */
51021 static int setSharedCacheTableLock(Btree *p, Pgno iTable, u8 eLock){
51022   BtShared *pBt = p->pBt;
51023   BtLock *pLock = 0;
51024   BtLock *pIter;
51025 
51026   assert( sqlite3BtreeHoldsMutex(p) );
51027   assert( eLock==READ_LOCK || eLock==WRITE_LOCK );
51028   assert( p->db!=0 );
51029 
51030   /* A connection with the read-uncommitted flag set will never try to
51031   ** obtain a read-lock using this function. The only read-lock obtained
51032   ** by a connection in read-uncommitted mode is on the sqlite_master
51033   ** table, and that lock is obtained in BtreeBeginTrans().  */
51034   assert( 0==(p->db->flags&SQLITE_ReadUncommitted) || eLock==WRITE_LOCK );
51035 
51036   /* This function should only be called on a sharable b-tree after it
51037   ** has been determined that no other b-tree holds a conflicting lock.  */
51038   assert( p->sharable );
51039   assert( SQLITE_OK==querySharedCacheTableLock(p, iTable, eLock) );
51040 
51041   /* First search the list for an existing lock on this table. */
51042   for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
51043     if( pIter->iTable==iTable && pIter->pBtree==p ){
51044       pLock = pIter;
51045       break;
51046     }
51047   }
51048 
51049   /* If the above search did not find a BtLock struct associating Btree p
51050   ** with table iTable, allocate one and link it into the list.
51051   */
51052   if( !pLock ){
51053     pLock = (BtLock *)sqlite3MallocZero(sizeof(BtLock));
51054     if( !pLock ){
51055       return SQLITE_NOMEM;
51056     }
51057     pLock->iTable = iTable;
51058     pLock->pBtree = p;
51059     pLock->pNext = pBt->pLock;
51060     pBt->pLock = pLock;
51061   }
51062 
51063   /* Set the BtLock.eLock variable to the maximum of the current lock
51064   ** and the requested lock. This means if a write-lock was already held
51065   ** and a read-lock requested, we don't incorrectly downgrade the lock.
51066   */
51067   assert( WRITE_LOCK>READ_LOCK );
51068   if( eLock>pLock->eLock ){
51069     pLock->eLock = eLock;
51070   }
51071 
51072   return SQLITE_OK;
51073 }
51074 #endif /* !SQLITE_OMIT_SHARED_CACHE */
51075 
51076 #ifndef SQLITE_OMIT_SHARED_CACHE
51077 /*
51078 ** Release all the table locks (locks obtained via calls to
51079 ** the setSharedCacheTableLock() procedure) held by Btree object p.
51080 **
51081 ** This function assumes that Btree p has an open read or write
51082 ** transaction. If it does not, then the BTS_PENDING flag
51083 ** may be incorrectly cleared.
51084 */
51085 static void clearAllSharedCacheTableLocks(Btree *p){
51086   BtShared *pBt = p->pBt;
51087   BtLock **ppIter = &pBt->pLock;
51088 
51089   assert( sqlite3BtreeHoldsMutex(p) );
51090   assert( p->sharable || 0==*ppIter );
51091   assert( p->inTrans>0 );
51092 
51093   while( *ppIter ){
51094     BtLock *pLock = *ppIter;
51095     assert( (pBt->btsFlags & BTS_EXCLUSIVE)==0 || pBt->pWriter==pLock->pBtree );
51096     assert( pLock->pBtree->inTrans>=pLock->eLock );
51097     if( pLock->pBtree==p ){
51098       *ppIter = pLock->pNext;
51099       assert( pLock->iTable!=1 || pLock==&p->lock );
51100       if( pLock->iTable!=1 ){
51101         sqlite3_free(pLock);
51102       }
51103     }else{
51104       ppIter = &pLock->pNext;
51105     }
51106   }
51107 
51108   assert( (pBt->btsFlags & BTS_PENDING)==0 || pBt->pWriter );
51109   if( pBt->pWriter==p ){
51110     pBt->pWriter = 0;
51111     pBt->btsFlags &= ~(BTS_EXCLUSIVE|BTS_PENDING);
51112   }else if( pBt->nTransaction==2 ){
51113     /* This function is called when Btree p is concluding its
51114     ** transaction. If there currently exists a writer, and p is not
51115     ** that writer, then the number of locks held by connections other
51116     ** than the writer must be about to drop to zero. In this case
51117     ** set the BTS_PENDING flag to 0.
51118     **
51119     ** If there is not currently a writer, then BTS_PENDING must
51120     ** be zero already. So this next line is harmless in that case.
51121     */
51122     pBt->btsFlags &= ~BTS_PENDING;
51123   }
51124 }
51125 
51126 /*
51127 ** This function changes all write-locks held by Btree p into read-locks.
51128 */
51129 static void downgradeAllSharedCacheTableLocks(Btree *p){
51130   BtShared *pBt = p->pBt;
51131   if( pBt->pWriter==p ){
51132     BtLock *pLock;
51133     pBt->pWriter = 0;
51134     pBt->btsFlags &= ~(BTS_EXCLUSIVE|BTS_PENDING);
51135     for(pLock=pBt->pLock; pLock; pLock=pLock->pNext){
51136       assert( pLock->eLock==READ_LOCK || pLock->pBtree==p );
51137       pLock->eLock = READ_LOCK;
51138     }
51139   }
51140 }
51141 
51142 #endif /* SQLITE_OMIT_SHARED_CACHE */
51143 
51144 static void releasePage(MemPage *pPage);  /* Forward reference */
51145 
51146 /*
51147 ***** This routine is used inside of assert() only ****
51148 **
51149 ** Verify that the cursor holds the mutex on its BtShared
51150 */
51151 #ifdef SQLITE_DEBUG
51152 static int cursorHoldsMutex(BtCursor *p){
51153   return sqlite3_mutex_held(p->pBt->mutex);
51154 }
51155 #endif
51156 
51157 
51158 #ifndef SQLITE_OMIT_INCRBLOB
51159 /*
51160 ** Invalidate the overflow page-list cache for cursor pCur, if any.
51161 */
51162 static void invalidateOverflowCache(BtCursor *pCur){
51163   assert( cursorHoldsMutex(pCur) );
51164   sqlite3_free(pCur->aOverflow);
51165   pCur->aOverflow = 0;
51166 }
51167 
51168 /*
51169 ** Invalidate the overflow page-list cache for all cursors opened
51170 ** on the shared btree structure pBt.
51171 */
51172 static void invalidateAllOverflowCache(BtShared *pBt){
51173   BtCursor *p;
51174   assert( sqlite3_mutex_held(pBt->mutex) );
51175   for(p=pBt->pCursor; p; p=p->pNext){
51176     invalidateOverflowCache(p);
51177   }
51178 }
51179 
51180 /*
51181 ** This function is called before modifying the contents of a table
51182 ** to invalidate any incrblob cursors that are open on the
51183 ** row or one of the rows being modified.
51184 **
51185 ** If argument isClearTable is true, then the entire contents of the
51186 ** table is about to be deleted. In this case invalidate all incrblob
51187 ** cursors open on any row within the table with root-page pgnoRoot.
51188 **
51189 ** Otherwise, if argument isClearTable is false, then the row with
51190 ** rowid iRow is being replaced or deleted. In this case invalidate
51191 ** only those incrblob cursors open on that specific row.
51192 */
51193 static void invalidateIncrblobCursors(
51194   Btree *pBtree,          /* The database file to check */
51195   i64 iRow,               /* The rowid that might be changing */
51196   int isClearTable        /* True if all rows are being deleted */
51197 ){
51198   BtCursor *p;
51199   BtShared *pBt = pBtree->pBt;
51200   assert( sqlite3BtreeHoldsMutex(pBtree) );
51201   for(p=pBt->pCursor; p; p=p->pNext){
51202     if( p->isIncrblobHandle && (isClearTable || p->info.nKey==iRow) ){
51203       p->eState = CURSOR_INVALID;
51204     }
51205   }
51206 }
51207 
51208 #else
51209   /* Stub functions when INCRBLOB is omitted */
51210   #define invalidateOverflowCache(x)
51211   #define invalidateAllOverflowCache(x)
51212   #define invalidateIncrblobCursors(x,y,z)
51213 #endif /* SQLITE_OMIT_INCRBLOB */
51214 
51215 /*
51216 ** Set bit pgno of the BtShared.pHasContent bitvec. This is called
51217 ** when a page that previously contained data becomes a free-list leaf
51218 ** page.
51219 **
51220 ** The BtShared.pHasContent bitvec exists to work around an obscure
51221 ** bug caused by the interaction of two useful IO optimizations surrounding
51222 ** free-list leaf pages:
51223 **
51224 **   1) When all data is deleted from a page and the page becomes
51225 **      a free-list leaf page, the page is not written to the database
51226 **      (as free-list leaf pages contain no meaningful data). Sometimes
51227 **      such a page is not even journalled (as it will not be modified,
51228 **      why bother journalling it?).
51229 **
51230 **   2) When a free-list leaf page is reused, its content is not read
51231 **      from the database or written to the journal file (why should it
51232 **      be, if it is not at all meaningful?).
51233 **
51234 ** By themselves, these optimizations work fine and provide a handy
51235 ** performance boost to bulk delete or insert operations. However, if
51236 ** a page is moved to the free-list and then reused within the same
51237 ** transaction, a problem comes up. If the page is not journalled when
51238 ** it is moved to the free-list and it is also not journalled when it
51239 ** is extracted from the free-list and reused, then the original data
51240 ** may be lost. In the event of a rollback, it may not be possible
51241 ** to restore the database to its original configuration.
51242 **
51243 ** The solution is the BtShared.pHasContent bitvec. Whenever a page is
51244 ** moved to become a free-list leaf page, the corresponding bit is
51245 ** set in the bitvec. Whenever a leaf page is extracted from the free-list,
51246 ** optimization 2 above is omitted if the corresponding bit is already
51247 ** set in BtShared.pHasContent. The contents of the bitvec are cleared
51248 ** at the end of every transaction.
51249 */
51250 static int btreeSetHasContent(BtShared *pBt, Pgno pgno){
51251   int rc = SQLITE_OK;
51252   if( !pBt->pHasContent ){
51253     assert( pgno<=pBt->nPage );
51254     pBt->pHasContent = sqlite3BitvecCreate(pBt->nPage);
51255     if( !pBt->pHasContent ){
51256       rc = SQLITE_NOMEM;
51257     }
51258   }
51259   if( rc==SQLITE_OK && pgno<=sqlite3BitvecSize(pBt->pHasContent) ){
51260     rc = sqlite3BitvecSet(pBt->pHasContent, pgno);
51261   }
51262   return rc;
51263 }
51264 
51265 /*
51266 ** Query the BtShared.pHasContent vector.
51267 **
51268 ** This function is called when a free-list leaf page is removed from the
51269 ** free-list for reuse. It returns false if it is safe to retrieve the
51270 ** page from the pager layer with the 'no-content' flag set. True otherwise.
51271 */
51272 static int btreeGetHasContent(BtShared *pBt, Pgno pgno){
51273   Bitvec *p = pBt->pHasContent;
51274   return (p && (pgno>sqlite3BitvecSize(p) || sqlite3BitvecTest(p, pgno)));
51275 }
51276 
51277 /*
51278 ** Clear (destroy) the BtShared.pHasContent bitvec. This should be
51279 ** invoked at the conclusion of each write-transaction.
51280 */
51281 static void btreeClearHasContent(BtShared *pBt){
51282   sqlite3BitvecDestroy(pBt->pHasContent);
51283   pBt->pHasContent = 0;
51284 }
51285 
51286 /*
51287 ** Release all of the apPage[] pages for a cursor.
51288 */
51289 static void btreeReleaseAllCursorPages(BtCursor *pCur){
51290   int i;
51291   for(i=0; i<=pCur->iPage; i++){
51292     releasePage(pCur->apPage[i]);
51293     pCur->apPage[i] = 0;
51294   }
51295   pCur->iPage = -1;
51296 }
51297 
51298 
51299 /*
51300 ** Save the current cursor position in the variables BtCursor.nKey
51301 ** and BtCursor.pKey. The cursor's state is set to CURSOR_REQUIRESEEK.
51302 **
51303 ** The caller must ensure that the cursor is valid (has eState==CURSOR_VALID)
51304 ** prior to calling this routine.
51305 */
51306 static int saveCursorPosition(BtCursor *pCur){
51307   int rc;
51308 
51309   assert( CURSOR_VALID==pCur->eState );
51310   assert( 0==pCur->pKey );
51311   assert( cursorHoldsMutex(pCur) );
51312 
51313   rc = sqlite3BtreeKeySize(pCur, &pCur->nKey);
51314   assert( rc==SQLITE_OK );  /* KeySize() cannot fail */
51315 
51316   /* If this is an intKey table, then the above call to BtreeKeySize()
51317   ** stores the integer key in pCur->nKey. In this case this value is
51318   ** all that is required. Otherwise, if pCur is not open on an intKey
51319   ** table, then malloc space for and store the pCur->nKey bytes of key
51320   ** data.
51321   */
51322   if( 0==pCur->apPage[0]->intKey ){
51323     void *pKey = sqlite3Malloc( (int)pCur->nKey );
51324     if( pKey ){
51325       rc = sqlite3BtreeKey(pCur, 0, (int)pCur->nKey, pKey);
51326       if( rc==SQLITE_OK ){
51327         pCur->pKey = pKey;
51328       }else{
51329         sqlite3_free(pKey);
51330       }
51331     }else{
51332       rc = SQLITE_NOMEM;
51333     }
51334   }
51335   assert( !pCur->apPage[0]->intKey || !pCur->pKey );
51336 
51337   if( rc==SQLITE_OK ){
51338     btreeReleaseAllCursorPages(pCur);
51339     pCur->eState = CURSOR_REQUIRESEEK;
51340   }
51341 
51342   invalidateOverflowCache(pCur);
51343   return rc;
51344 }
51345 
51346 /*
51347 ** Save the positions of all cursors (except pExcept) that are open on
51348 ** the table  with root-page iRoot. Usually, this is called just before cursor
51349 ** pExcept is used to modify the table (BtreeDelete() or BtreeInsert()).
51350 */
51351 static int saveAllCursors(BtShared *pBt, Pgno iRoot, BtCursor *pExcept){
51352   BtCursor *p;
51353   assert( sqlite3_mutex_held(pBt->mutex) );
51354   assert( pExcept==0 || pExcept->pBt==pBt );
51355   for(p=pBt->pCursor; p; p=p->pNext){
51356     if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) ){
51357       if( p->eState==CURSOR_VALID ){
51358         int rc = saveCursorPosition(p);
51359         if( SQLITE_OK!=rc ){
51360           return rc;
51361         }
51362       }else{
51363         testcase( p->iPage>0 );
51364         btreeReleaseAllCursorPages(p);
51365       }
51366     }
51367   }
51368   return SQLITE_OK;
51369 }
51370 
51371 /*
51372 ** Clear the current cursor position.
51373 */
51374 SQLITE_PRIVATE void sqlite3BtreeClearCursor(BtCursor *pCur){
51375   assert( cursorHoldsMutex(pCur) );
51376   sqlite3_free(pCur->pKey);
51377   pCur->pKey = 0;
51378   pCur->eState = CURSOR_INVALID;
51379 }
51380 
51381 /*
51382 ** In this version of BtreeMoveto, pKey is a packed index record
51383 ** such as is generated by the OP_MakeRecord opcode.  Unpack the
51384 ** record and then call BtreeMovetoUnpacked() to do the work.
51385 */
51386 static int btreeMoveto(
51387   BtCursor *pCur,     /* Cursor open on the btree to be searched */
51388   const void *pKey,   /* Packed key if the btree is an index */
51389   i64 nKey,           /* Integer key for tables.  Size of pKey for indices */
51390   int bias,           /* Bias search to the high end */
51391   int *pRes           /* Write search results here */
51392 ){
51393   int rc;                    /* Status code */
51394   UnpackedRecord *pIdxKey;   /* Unpacked index key */
51395   char aSpace[200];          /* Temp space for pIdxKey - to avoid a malloc */
51396   char *pFree = 0;
51397 
51398   if( pKey ){
51399     assert( nKey==(i64)(int)nKey );
51400     pIdxKey = sqlite3VdbeAllocUnpackedRecord(
51401         pCur->pKeyInfo, aSpace, sizeof(aSpace), &pFree
51402     );
51403     if( pIdxKey==0 ) return SQLITE_NOMEM;
51404     sqlite3VdbeRecordUnpack(pCur->pKeyInfo, (int)nKey, pKey, pIdxKey);
51405     if( pIdxKey->nField==0 ){
51406       sqlite3DbFree(pCur->pKeyInfo->db, pFree);
51407       return SQLITE_CORRUPT_BKPT;
51408     }
51409   }else{
51410     pIdxKey = 0;
51411   }
51412   rc = sqlite3BtreeMovetoUnpacked(pCur, pIdxKey, nKey, bias, pRes);
51413   if( pFree ){
51414     sqlite3DbFree(pCur->pKeyInfo->db, pFree);
51415   }
51416   return rc;
51417 }
51418 
51419 /*
51420 ** Restore the cursor to the position it was in (or as close to as possible)
51421 ** when saveCursorPosition() was called. Note that this call deletes the
51422 ** saved position info stored by saveCursorPosition(), so there can be
51423 ** at most one effective restoreCursorPosition() call after each
51424 ** saveCursorPosition().
51425 */
51426 static int btreeRestoreCursorPosition(BtCursor *pCur){
51427   int rc;
51428   assert( cursorHoldsMutex(pCur) );
51429   assert( pCur->eState>=CURSOR_REQUIRESEEK );
51430   if( pCur->eState==CURSOR_FAULT ){
51431     return pCur->skipNext;
51432   }
51433   pCur->eState = CURSOR_INVALID;
51434   rc = btreeMoveto(pCur, pCur->pKey, pCur->nKey, 0, &pCur->skipNext);
51435   if( rc==SQLITE_OK ){
51436     sqlite3_free(pCur->pKey);
51437     pCur->pKey = 0;
51438     assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_INVALID );
51439     if( pCur->skipNext && pCur->eState==CURSOR_VALID ){
51440       pCur->eState = CURSOR_SKIPNEXT;
51441     }
51442   }
51443   return rc;
51444 }
51445 
51446 #define restoreCursorPosition(p) \
51447   (p->eState>=CURSOR_REQUIRESEEK ? \
51448          btreeRestoreCursorPosition(p) : \
51449          SQLITE_OK)
51450 
51451 /*
51452 ** Determine whether or not a cursor has moved from the position it
51453 ** was last placed at.  Cursors can move when the row they are pointing
51454 ** at is deleted out from under them.
51455 **
51456 ** This routine returns an error code if something goes wrong.  The
51457 ** integer *pHasMoved is set to one if the cursor has moved and 0 if not.
51458 */
51459 SQLITE_PRIVATE int sqlite3BtreeCursorHasMoved(BtCursor *pCur, int *pHasMoved){
51460   int rc;
51461 
51462   rc = restoreCursorPosition(pCur);
51463   if( rc ){
51464     *pHasMoved = 1;
51465     return rc;
51466   }
51467   if( pCur->eState!=CURSOR_VALID || NEVER(pCur->skipNext!=0) ){
51468     *pHasMoved = 1;
51469   }else{
51470     *pHasMoved = 0;
51471   }
51472   return SQLITE_OK;
51473 }
51474 
51475 #ifndef SQLITE_OMIT_AUTOVACUUM
51476 /*
51477 ** Given a page number of a regular database page, return the page
51478 ** number for the pointer-map page that contains the entry for the
51479 ** input page number.
51480 **
51481 ** Return 0 (not a valid page) for pgno==1 since there is
51482 ** no pointer map associated with page 1.  The integrity_check logic
51483 ** requires that ptrmapPageno(*,1)!=1.
51484 */
51485 static Pgno ptrmapPageno(BtShared *pBt, Pgno pgno){
51486   int nPagesPerMapPage;
51487   Pgno iPtrMap, ret;
51488   assert( sqlite3_mutex_held(pBt->mutex) );
51489   if( pgno<2 ) return 0;
51490   nPagesPerMapPage = (pBt->usableSize/5)+1;
51491   iPtrMap = (pgno-2)/nPagesPerMapPage;
51492   ret = (iPtrMap*nPagesPerMapPage) + 2;
51493   if( ret==PENDING_BYTE_PAGE(pBt) ){
51494     ret++;
51495   }
51496   return ret;
51497 }
51498 
51499 /*
51500 ** Write an entry into the pointer map.
51501 **
51502 ** This routine updates the pointer map entry for page number 'key'
51503 ** so that it maps to type 'eType' and parent page number 'pgno'.
51504 **
51505 ** If *pRC is initially non-zero (non-SQLITE_OK) then this routine is
51506 ** a no-op.  If an error occurs, the appropriate error code is written
51507 ** into *pRC.
51508 */
51509 static void ptrmapPut(BtShared *pBt, Pgno key, u8 eType, Pgno parent, int *pRC){
51510   DbPage *pDbPage;  /* The pointer map page */
51511   u8 *pPtrmap;      /* The pointer map data */
51512   Pgno iPtrmap;     /* The pointer map page number */
51513   int offset;       /* Offset in pointer map page */
51514   int rc;           /* Return code from subfunctions */
51515 
51516   if( *pRC ) return;
51517 
51518   assert( sqlite3_mutex_held(pBt->mutex) );
51519   /* The master-journal page number must never be used as a pointer map page */
51520   assert( 0==PTRMAP_ISPAGE(pBt, PENDING_BYTE_PAGE(pBt)) );
51521 
51522   assert( pBt->autoVacuum );
51523   if( key==0 ){
51524     *pRC = SQLITE_CORRUPT_BKPT;
51525     return;
51526   }
51527   iPtrmap = PTRMAP_PAGENO(pBt, key);
51528   rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage);
51529   if( rc!=SQLITE_OK ){
51530     *pRC = rc;
51531     return;
51532   }
51533   offset = PTRMAP_PTROFFSET(iPtrmap, key);
51534   if( offset<0 ){
51535     *pRC = SQLITE_CORRUPT_BKPT;
51536     goto ptrmap_exit;
51537   }
51538   assert( offset <= (int)pBt->usableSize-5 );
51539   pPtrmap = (u8 *)sqlite3PagerGetData(pDbPage);
51540 
51541   if( eType!=pPtrmap[offset] || get4byte(&pPtrmap[offset+1])!=parent ){
51542     TRACE(("PTRMAP_UPDATE: %d->(%d,%d)\n", key, eType, parent));
51543     *pRC= rc = sqlite3PagerWrite(pDbPage);
51544     if( rc==SQLITE_OK ){
51545       pPtrmap[offset] = eType;
51546       put4byte(&pPtrmap[offset+1], parent);
51547     }
51548   }
51549 
51550 ptrmap_exit:
51551   sqlite3PagerUnref(pDbPage);
51552 }
51553 
51554 /*
51555 ** Read an entry from the pointer map.
51556 **
51557 ** This routine retrieves the pointer map entry for page 'key', writing
51558 ** the type and parent page number to *pEType and *pPgno respectively.
51559 ** An error code is returned if something goes wrong, otherwise SQLITE_OK.
51560 */
51561 static int ptrmapGet(BtShared *pBt, Pgno key, u8 *pEType, Pgno *pPgno){
51562   DbPage *pDbPage;   /* The pointer map page */
51563   int iPtrmap;       /* Pointer map page index */
51564   u8 *pPtrmap;       /* Pointer map page data */
51565   int offset;        /* Offset of entry in pointer map */
51566   int rc;
51567 
51568   assert( sqlite3_mutex_held(pBt->mutex) );
51569 
51570   iPtrmap = PTRMAP_PAGENO(pBt, key);
51571   rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage);
51572   if( rc!=0 ){
51573     return rc;
51574   }
51575   pPtrmap = (u8 *)sqlite3PagerGetData(pDbPage);
51576 
51577   offset = PTRMAP_PTROFFSET(iPtrmap, key);
51578   if( offset<0 ){
51579     sqlite3PagerUnref(pDbPage);
51580     return SQLITE_CORRUPT_BKPT;
51581   }
51582   assert( offset <= (int)pBt->usableSize-5 );
51583   assert( pEType!=0 );
51584   *pEType = pPtrmap[offset];
51585   if( pPgno ) *pPgno = get4byte(&pPtrmap[offset+1]);
51586 
51587   sqlite3PagerUnref(pDbPage);
51588   if( *pEType<1 || *pEType>5 ) return SQLITE_CORRUPT_BKPT;
51589   return SQLITE_OK;
51590 }
51591 
51592 #else /* if defined SQLITE_OMIT_AUTOVACUUM */
51593   #define ptrmapPut(w,x,y,z,rc)
51594   #define ptrmapGet(w,x,y,z) SQLITE_OK
51595   #define ptrmapPutOvflPtr(x, y, rc)
51596 #endif
51597 
51598 /*
51599 ** Given a btree page and a cell index (0 means the first cell on
51600 ** the page, 1 means the second cell, and so forth) return a pointer
51601 ** to the cell content.
51602 **
51603 ** This routine works only for pages that do not contain overflow cells.
51604 */
51605 #define findCell(P,I) \
51606   ((P)->aData + ((P)->maskPage & get2byte(&(P)->aCellIdx[2*(I)])))
51607 #define findCellv2(D,M,O,I) (D+(M&get2byte(D+(O+2*(I)))))
51608 
51609 
51610 /*
51611 ** This a more complex version of findCell() that works for
51612 ** pages that do contain overflow cells.
51613 */
51614 static u8 *findOverflowCell(MemPage *pPage, int iCell){
51615   int i;
51616   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
51617   for(i=pPage->nOverflow-1; i>=0; i--){
51618     int k;
51619     k = pPage->aiOvfl[i];
51620     if( k<=iCell ){
51621       if( k==iCell ){
51622         return pPage->apOvfl[i];
51623       }
51624       iCell--;
51625     }
51626   }
51627   return findCell(pPage, iCell);
51628 }
51629 
51630 /*
51631 ** Parse a cell content block and fill in the CellInfo structure.  There
51632 ** are two versions of this function.  btreeParseCell() takes a
51633 ** cell index as the second argument and btreeParseCellPtr()
51634 ** takes a pointer to the body of the cell as its second argument.
51635 **
51636 ** Within this file, the parseCell() macro can be called instead of
51637 ** btreeParseCellPtr(). Using some compilers, this will be faster.
51638 */
51639 static void btreeParseCellPtr(
51640   MemPage *pPage,         /* Page containing the cell */
51641   u8 *pCell,              /* Pointer to the cell text. */
51642   CellInfo *pInfo         /* Fill in this structure */
51643 ){
51644   u16 n;                  /* Number bytes in cell content header */
51645   u32 nPayload;           /* Number of bytes of cell payload */
51646 
51647   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
51648 
51649   pInfo->pCell = pCell;
51650   assert( pPage->leaf==0 || pPage->leaf==1 );
51651   n = pPage->childPtrSize;
51652   assert( n==4-4*pPage->leaf );
51653   if( pPage->intKey ){
51654     if( pPage->hasData ){
51655       assert( n==0 );
51656       n = getVarint32(pCell, nPayload);
51657     }else{
51658       nPayload = 0;
51659     }
51660     n += getVarint(&pCell[n], (u64*)&pInfo->nKey);
51661     pInfo->nData = nPayload;
51662   }else{
51663     pInfo->nData = 0;
51664     n += getVarint32(&pCell[n], nPayload);
51665     pInfo->nKey = nPayload;
51666   }
51667   pInfo->nPayload = nPayload;
51668   pInfo->nHeader = n;
51669   testcase( nPayload==pPage->maxLocal );
51670   testcase( nPayload==pPage->maxLocal+1 );
51671   if( likely(nPayload<=pPage->maxLocal) ){
51672     /* This is the (easy) common case where the entire payload fits
51673     ** on the local page.  No overflow is required.
51674     */
51675     if( (pInfo->nSize = (u16)(n+nPayload))<4 ) pInfo->nSize = 4;
51676     pInfo->nLocal = (u16)nPayload;
51677     pInfo->iOverflow = 0;
51678   }else{
51679     /* If the payload will not fit completely on the local page, we have
51680     ** to decide how much to store locally and how much to spill onto
51681     ** overflow pages.  The strategy is to minimize the amount of unused
51682     ** space on overflow pages while keeping the amount of local storage
51683     ** in between minLocal and maxLocal.
51684     **
51685     ** Warning:  changing the way overflow payload is distributed in any
51686     ** way will result in an incompatible file format.
51687     */
51688     int minLocal;  /* Minimum amount of payload held locally */
51689     int maxLocal;  /* Maximum amount of payload held locally */
51690     int surplus;   /* Overflow payload available for local storage */
51691 
51692     minLocal = pPage->minLocal;
51693     maxLocal = pPage->maxLocal;
51694     surplus = minLocal + (nPayload - minLocal)%(pPage->pBt->usableSize - 4);
51695     testcase( surplus==maxLocal );
51696     testcase( surplus==maxLocal+1 );
51697     if( surplus <= maxLocal ){
51698       pInfo->nLocal = (u16)surplus;
51699     }else{
51700       pInfo->nLocal = (u16)minLocal;
51701     }
51702     pInfo->iOverflow = (u16)(pInfo->nLocal + n);
51703     pInfo->nSize = pInfo->iOverflow + 4;
51704   }
51705 }
51706 #define parseCell(pPage, iCell, pInfo) \
51707   btreeParseCellPtr((pPage), findCell((pPage), (iCell)), (pInfo))
51708 static void btreeParseCell(
51709   MemPage *pPage,         /* Page containing the cell */
51710   int iCell,              /* The cell index.  First cell is 0 */
51711   CellInfo *pInfo         /* Fill in this structure */
51712 ){
51713   parseCell(pPage, iCell, pInfo);
51714 }
51715 
51716 /*
51717 ** Compute the total number of bytes that a Cell needs in the cell
51718 ** data area of the btree-page.  The return number includes the cell
51719 ** data header and the local payload, but not any overflow page or
51720 ** the space used by the cell pointer.
51721 */
51722 static u16 cellSizePtr(MemPage *pPage, u8 *pCell){
51723   u8 *pIter = &pCell[pPage->childPtrSize];
51724   u32 nSize;
51725 
51726 #ifdef SQLITE_DEBUG
51727   /* The value returned by this function should always be the same as
51728   ** the (CellInfo.nSize) value found by doing a full parse of the
51729   ** cell. If SQLITE_DEBUG is defined, an assert() at the bottom of
51730   ** this function verifies that this invariant is not violated. */
51731   CellInfo debuginfo;
51732   btreeParseCellPtr(pPage, pCell, &debuginfo);
51733 #endif
51734 
51735   if( pPage->intKey ){
51736     u8 *pEnd;
51737     if( pPage->hasData ){
51738       pIter += getVarint32(pIter, nSize);
51739     }else{
51740       nSize = 0;
51741     }
51742 
51743     /* pIter now points at the 64-bit integer key value, a variable length
51744     ** integer. The following block moves pIter to point at the first byte
51745     ** past the end of the key value. */
51746     pEnd = &pIter[9];
51747     while( (*pIter++)&0x80 && pIter<pEnd );
51748   }else{
51749     pIter += getVarint32(pIter, nSize);
51750   }
51751 
51752   testcase( nSize==pPage->maxLocal );
51753   testcase( nSize==pPage->maxLocal+1 );
51754   if( nSize>pPage->maxLocal ){
51755     int minLocal = pPage->minLocal;
51756     nSize = minLocal + (nSize - minLocal) % (pPage->pBt->usableSize - 4);
51757     testcase( nSize==pPage->maxLocal );
51758     testcase( nSize==pPage->maxLocal+1 );
51759     if( nSize>pPage->maxLocal ){
51760       nSize = minLocal;
51761     }
51762     nSize += 4;
51763   }
51764   nSize += (u32)(pIter - pCell);
51765 
51766   /* The minimum size of any cell is 4 bytes. */
51767   if( nSize<4 ){
51768     nSize = 4;
51769   }
51770 
51771   assert( nSize==debuginfo.nSize );
51772   return (u16)nSize;
51773 }
51774 
51775 #ifdef SQLITE_DEBUG
51776 /* This variation on cellSizePtr() is used inside of assert() statements
51777 ** only. */
51778 static u16 cellSize(MemPage *pPage, int iCell){
51779   return cellSizePtr(pPage, findCell(pPage, iCell));
51780 }
51781 #endif
51782 
51783 #ifndef SQLITE_OMIT_AUTOVACUUM
51784 /*
51785 ** If the cell pCell, part of page pPage contains a pointer
51786 ** to an overflow page, insert an entry into the pointer-map
51787 ** for the overflow page.
51788 */
51789 static void ptrmapPutOvflPtr(MemPage *pPage, u8 *pCell, int *pRC){
51790   CellInfo info;
51791   if( *pRC ) return;
51792   assert( pCell!=0 );
51793   btreeParseCellPtr(pPage, pCell, &info);
51794   assert( (info.nData+(pPage->intKey?0:info.nKey))==info.nPayload );
51795   if( info.iOverflow ){
51796     Pgno ovfl = get4byte(&pCell[info.iOverflow]);
51797     ptrmapPut(pPage->pBt, ovfl, PTRMAP_OVERFLOW1, pPage->pgno, pRC);
51798   }
51799 }
51800 #endif
51801 
51802 
51803 /*
51804 ** Defragment the page given.  All Cells are moved to the
51805 ** end of the page and all free space is collected into one
51806 ** big FreeBlk that occurs in between the header and cell
51807 ** pointer array and the cell content area.
51808 */
51809 static int defragmentPage(MemPage *pPage){
51810   int i;                     /* Loop counter */
51811   int pc;                    /* Address of a i-th cell */
51812   int hdr;                   /* Offset to the page header */
51813   int size;                  /* Size of a cell */
51814   int usableSize;            /* Number of usable bytes on a page */
51815   int cellOffset;            /* Offset to the cell pointer array */
51816   int cbrk;                  /* Offset to the cell content area */
51817   int nCell;                 /* Number of cells on the page */
51818   unsigned char *data;       /* The page data */
51819   unsigned char *temp;       /* Temp area for cell content */
51820   int iCellFirst;            /* First allowable cell index */
51821   int iCellLast;             /* Last possible cell index */
51822 
51823 
51824   assert( sqlite3PagerIswriteable(pPage->pDbPage) );
51825   assert( pPage->pBt!=0 );
51826   assert( pPage->pBt->usableSize <= SQLITE_MAX_PAGE_SIZE );
51827   assert( pPage->nOverflow==0 );
51828   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
51829   temp = sqlite3PagerTempSpace(pPage->pBt->pPager);
51830   data = pPage->aData;
51831   hdr = pPage->hdrOffset;
51832   cellOffset = pPage->cellOffset;
51833   nCell = pPage->nCell;
51834   assert( nCell==get2byte(&data[hdr+3]) );
51835   usableSize = pPage->pBt->usableSize;
51836   cbrk = get2byte(&data[hdr+5]);
51837   memcpy(&temp[cbrk], &data[cbrk], usableSize - cbrk);
51838   cbrk = usableSize;
51839   iCellFirst = cellOffset + 2*nCell;
51840   iCellLast = usableSize - 4;
51841   for(i=0; i<nCell; i++){
51842     u8 *pAddr;     /* The i-th cell pointer */
51843     pAddr = &data[cellOffset + i*2];
51844     pc = get2byte(pAddr);
51845     testcase( pc==iCellFirst );
51846     testcase( pc==iCellLast );
51847 #if !defined(SQLITE_ENABLE_OVERSIZE_CELL_CHECK)
51848     /* These conditions have already been verified in btreeInitPage()
51849     ** if SQLITE_ENABLE_OVERSIZE_CELL_CHECK is defined
51850     */
51851     if( pc<iCellFirst || pc>iCellLast ){
51852       return SQLITE_CORRUPT_BKPT;
51853     }
51854 #endif
51855     assert( pc>=iCellFirst && pc<=iCellLast );
51856     size = cellSizePtr(pPage, &temp[pc]);
51857     cbrk -= size;
51858 #if defined(SQLITE_ENABLE_OVERSIZE_CELL_CHECK)
51859     if( cbrk<iCellFirst ){
51860       return SQLITE_CORRUPT_BKPT;
51861     }
51862 #else
51863     if( cbrk<iCellFirst || pc+size>usableSize ){
51864       return SQLITE_CORRUPT_BKPT;
51865     }
51866 #endif
51867     assert( cbrk+size<=usableSize && cbrk>=iCellFirst );
51868     testcase( cbrk+size==usableSize );
51869     testcase( pc+size==usableSize );
51870     memcpy(&data[cbrk], &temp[pc], size);
51871     put2byte(pAddr, cbrk);
51872   }
51873   assert( cbrk>=iCellFirst );
51874   put2byte(&data[hdr+5], cbrk);
51875   data[hdr+1] = 0;
51876   data[hdr+2] = 0;
51877   data[hdr+7] = 0;
51878   memset(&data[iCellFirst], 0, cbrk-iCellFirst);
51879   assert( sqlite3PagerIswriteable(pPage->pDbPage) );
51880   if( cbrk-iCellFirst!=pPage->nFree ){
51881     return SQLITE_CORRUPT_BKPT;
51882   }
51883   return SQLITE_OK;
51884 }
51885 
51886 /*
51887 ** Allocate nByte bytes of space from within the B-Tree page passed
51888 ** as the first argument. Write into *pIdx the index into pPage->aData[]
51889 ** of the first byte of allocated space. Return either SQLITE_OK or
51890 ** an error code (usually SQLITE_CORRUPT).
51891 **
51892 ** The caller guarantees that there is sufficient space to make the
51893 ** allocation.  This routine might need to defragment in order to bring
51894 ** all the space together, however.  This routine will avoid using
51895 ** the first two bytes past the cell pointer area since presumably this
51896 ** allocation is being made in order to insert a new cell, so we will
51897 ** also end up needing a new cell pointer.
51898 */
51899 static int allocateSpace(MemPage *pPage, int nByte, int *pIdx){
51900   const int hdr = pPage->hdrOffset;    /* Local cache of pPage->hdrOffset */
51901   u8 * const data = pPage->aData;      /* Local cache of pPage->aData */
51902   int nFrag;                           /* Number of fragmented bytes on pPage */
51903   int top;                             /* First byte of cell content area */
51904   int gap;        /* First byte of gap between cell pointers and cell content */
51905   int rc;         /* Integer return code */
51906   int usableSize; /* Usable size of the page */
51907 
51908   assert( sqlite3PagerIswriteable(pPage->pDbPage) );
51909   assert( pPage->pBt );
51910   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
51911   assert( nByte>=0 );  /* Minimum cell size is 4 */
51912   assert( pPage->nFree>=nByte );
51913   assert( pPage->nOverflow==0 );
51914   usableSize = pPage->pBt->usableSize;
51915   assert( nByte < usableSize-8 );
51916 
51917   nFrag = data[hdr+7];
51918   assert( pPage->cellOffset == hdr + 12 - 4*pPage->leaf );
51919   gap = pPage->cellOffset + 2*pPage->nCell;
51920   top = get2byteNotZero(&data[hdr+5]);
51921   if( gap>top ) return SQLITE_CORRUPT_BKPT;
51922   testcase( gap+2==top );
51923   testcase( gap+1==top );
51924   testcase( gap==top );
51925 
51926   if( nFrag>=60 ){
51927     /* Always defragment highly fragmented pages */
51928     rc = defragmentPage(pPage);
51929     if( rc ) return rc;
51930     top = get2byteNotZero(&data[hdr+5]);
51931   }else if( gap+2<=top ){
51932     /* Search the freelist looking for a free slot big enough to satisfy
51933     ** the request. The allocation is made from the first free slot in
51934     ** the list that is large enough to accommodate it.
51935     */
51936     int pc, addr;
51937     for(addr=hdr+1; (pc = get2byte(&data[addr]))>0; addr=pc){
51938       int size;            /* Size of the free slot */
51939       if( pc>usableSize-4 || pc<addr+4 ){
51940         return SQLITE_CORRUPT_BKPT;
51941       }
51942       size = get2byte(&data[pc+2]);
51943       if( size>=nByte ){
51944         int x = size - nByte;
51945         testcase( x==4 );
51946         testcase( x==3 );
51947         if( x<4 ){
51948           /* Remove the slot from the free-list. Update the number of
51949           ** fragmented bytes within the page. */
51950           memcpy(&data[addr], &data[pc], 2);
51951           data[hdr+7] = (u8)(nFrag + x);
51952         }else if( size+pc > usableSize ){
51953           return SQLITE_CORRUPT_BKPT;
51954         }else{
51955           /* The slot remains on the free-list. Reduce its size to account
51956           ** for the portion used by the new allocation. */
51957           put2byte(&data[pc+2], x);
51958         }
51959         *pIdx = pc + x;
51960         return SQLITE_OK;
51961       }
51962     }
51963   }
51964 
51965   /* Check to make sure there is enough space in the gap to satisfy
51966   ** the allocation.  If not, defragment.
51967   */
51968   testcase( gap+2+nByte==top );
51969   if( gap+2+nByte>top ){
51970     rc = defragmentPage(pPage);
51971     if( rc ) return rc;
51972     top = get2byteNotZero(&data[hdr+5]);
51973     assert( gap+nByte<=top );
51974   }
51975 
51976 
51977   /* Allocate memory from the gap in between the cell pointer array
51978   ** and the cell content area.  The btreeInitPage() call has already
51979   ** validated the freelist.  Given that the freelist is valid, there
51980   ** is no way that the allocation can extend off the end of the page.
51981   ** The assert() below verifies the previous sentence.
51982   */
51983   top -= nByte;
51984   put2byte(&data[hdr+5], top);
51985   assert( top+nByte <= (int)pPage->pBt->usableSize );
51986   *pIdx = top;
51987   return SQLITE_OK;
51988 }
51989 
51990 /*
51991 ** Return a section of the pPage->aData to the freelist.
51992 ** The first byte of the new free block is pPage->aDisk[start]
51993 ** and the size of the block is "size" bytes.
51994 **
51995 ** Most of the effort here is involved in coalesing adjacent
51996 ** free blocks into a single big free block.
51997 */
51998 static int freeSpace(MemPage *pPage, int start, int size){
51999   int addr, pbegin, hdr;
52000   int iLast;                        /* Largest possible freeblock offset */
52001   unsigned char *data = pPage->aData;
52002 
52003   assert( pPage->pBt!=0 );
52004   assert( sqlite3PagerIswriteable(pPage->pDbPage) );
52005   assert( start>=pPage->hdrOffset+6+pPage->childPtrSize );
52006   assert( (start + size) <= (int)pPage->pBt->usableSize );
52007   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
52008   assert( size>=0 );   /* Minimum cell size is 4 */
52009 
52010   if( pPage->pBt->btsFlags & BTS_SECURE_DELETE ){
52011     /* Overwrite deleted information with zeros when the secure_delete
52012     ** option is enabled */
52013     memset(&data[start], 0, size);
52014   }
52015 
52016   /* Add the space back into the linked list of freeblocks.  Note that
52017   ** even though the freeblock list was checked by btreeInitPage(),
52018   ** btreeInitPage() did not detect overlapping cells or
52019   ** freeblocks that overlapped cells.   Nor does it detect when the
52020   ** cell content area exceeds the value in the page header.  If these
52021   ** situations arise, then subsequent insert operations might corrupt
52022   ** the freelist.  So we do need to check for corruption while scanning
52023   ** the freelist.
52024   */
52025   hdr = pPage->hdrOffset;
52026   addr = hdr + 1;
52027   iLast = pPage->pBt->usableSize - 4;
52028   assert( start<=iLast );
52029   while( (pbegin = get2byte(&data[addr]))<start && pbegin>0 ){
52030     if( pbegin<addr+4 ){
52031       return SQLITE_CORRUPT_BKPT;
52032     }
52033     addr = pbegin;
52034   }
52035   if( pbegin>iLast ){
52036     return SQLITE_CORRUPT_BKPT;
52037   }
52038   assert( pbegin>addr || pbegin==0 );
52039   put2byte(&data[addr], start);
52040   put2byte(&data[start], pbegin);
52041   put2byte(&data[start+2], size);
52042   pPage->nFree = pPage->nFree + (u16)size;
52043 
52044   /* Coalesce adjacent free blocks */
52045   addr = hdr + 1;
52046   while( (pbegin = get2byte(&data[addr]))>0 ){
52047     int pnext, psize, x;
52048     assert( pbegin>addr );
52049     assert( pbegin <= (int)pPage->pBt->usableSize-4 );
52050     pnext = get2byte(&data[pbegin]);
52051     psize = get2byte(&data[pbegin+2]);
52052     if( pbegin + psize + 3 >= pnext && pnext>0 ){
52053       int frag = pnext - (pbegin+psize);
52054       if( (frag<0) || (frag>(int)data[hdr+7]) ){
52055         return SQLITE_CORRUPT_BKPT;
52056       }
52057       data[hdr+7] -= (u8)frag;
52058       x = get2byte(&data[pnext]);
52059       put2byte(&data[pbegin], x);
52060       x = pnext + get2byte(&data[pnext+2]) - pbegin;
52061       put2byte(&data[pbegin+2], x);
52062     }else{
52063       addr = pbegin;
52064     }
52065   }
52066 
52067   /* If the cell content area begins with a freeblock, remove it. */
52068   if( data[hdr+1]==data[hdr+5] && data[hdr+2]==data[hdr+6] ){
52069     int top;
52070     pbegin = get2byte(&data[hdr+1]);
52071     memcpy(&data[hdr+1], &data[pbegin], 2);
52072     top = get2byte(&data[hdr+5]) + get2byte(&data[pbegin+2]);
52073     put2byte(&data[hdr+5], top);
52074   }
52075   assert( sqlite3PagerIswriteable(pPage->pDbPage) );
52076   return SQLITE_OK;
52077 }
52078 
52079 /*
52080 ** Decode the flags byte (the first byte of the header) for a page
52081 ** and initialize fields of the MemPage structure accordingly.
52082 **
52083 ** Only the following combinations are supported.  Anything different
52084 ** indicates a corrupt database files:
52085 **
52086 **         PTF_ZERODATA
52087 **         PTF_ZERODATA | PTF_LEAF
52088 **         PTF_LEAFDATA | PTF_INTKEY
52089 **         PTF_LEAFDATA | PTF_INTKEY | PTF_LEAF
52090 */
52091 static int decodeFlags(MemPage *pPage, int flagByte){
52092   BtShared *pBt;     /* A copy of pPage->pBt */
52093 
52094   assert( pPage->hdrOffset==(pPage->pgno==1 ? 100 : 0) );
52095   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
52096   pPage->leaf = (u8)(flagByte>>3);  assert( PTF_LEAF == 1<<3 );
52097   flagByte &= ~PTF_LEAF;
52098   pPage->childPtrSize = 4-4*pPage->leaf;
52099   pBt = pPage->pBt;
52100   if( flagByte==(PTF_LEAFDATA | PTF_INTKEY) ){
52101     pPage->intKey = 1;
52102     pPage->hasData = pPage->leaf;
52103     pPage->maxLocal = pBt->maxLeaf;
52104     pPage->minLocal = pBt->minLeaf;
52105   }else if( flagByte==PTF_ZERODATA ){
52106     pPage->intKey = 0;
52107     pPage->hasData = 0;
52108     pPage->maxLocal = pBt->maxLocal;
52109     pPage->minLocal = pBt->minLocal;
52110   }else{
52111     return SQLITE_CORRUPT_BKPT;
52112   }
52113   pPage->max1bytePayload = pBt->max1bytePayload;
52114   return SQLITE_OK;
52115 }
52116 
52117 /*
52118 ** Initialize the auxiliary information for a disk block.
52119 **
52120 ** Return SQLITE_OK on success.  If we see that the page does
52121 ** not contain a well-formed database page, then return
52122 ** SQLITE_CORRUPT.  Note that a return of SQLITE_OK does not
52123 ** guarantee that the page is well-formed.  It only shows that
52124 ** we failed to detect any corruption.
52125 */
52126 static int btreeInitPage(MemPage *pPage){
52127 
52128   assert( pPage->pBt!=0 );
52129   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
52130   assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) );
52131   assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) );
52132   assert( pPage->aData == sqlite3PagerGetData(pPage->pDbPage) );
52133 
52134   if( !pPage->isInit ){
52135     u16 pc;            /* Address of a freeblock within pPage->aData[] */
52136     u8 hdr;            /* Offset to beginning of page header */
52137     u8 *data;          /* Equal to pPage->aData */
52138     BtShared *pBt;        /* The main btree structure */
52139     int usableSize;    /* Amount of usable space on each page */
52140     u16 cellOffset;    /* Offset from start of page to first cell pointer */
52141     int nFree;         /* Number of unused bytes on the page */
52142     int top;           /* First byte of the cell content area */
52143     int iCellFirst;    /* First allowable cell or freeblock offset */
52144     int iCellLast;     /* Last possible cell or freeblock offset */
52145 
52146     pBt = pPage->pBt;
52147 
52148     hdr = pPage->hdrOffset;
52149     data = pPage->aData;
52150     if( decodeFlags(pPage, data[hdr]) ) return SQLITE_CORRUPT_BKPT;
52151     assert( pBt->pageSize>=512 && pBt->pageSize<=65536 );
52152     pPage->maskPage = (u16)(pBt->pageSize - 1);
52153     pPage->nOverflow = 0;
52154     usableSize = pBt->usableSize;
52155     pPage->cellOffset = cellOffset = hdr + 12 - 4*pPage->leaf;
52156     pPage->aDataEnd = &data[usableSize];
52157     pPage->aCellIdx = &data[cellOffset];
52158     top = get2byteNotZero(&data[hdr+5]);
52159     pPage->nCell = get2byte(&data[hdr+3]);
52160     if( pPage->nCell>MX_CELL(pBt) ){
52161       /* To many cells for a single page.  The page must be corrupt */
52162       return SQLITE_CORRUPT_BKPT;
52163     }
52164     testcase( pPage->nCell==MX_CELL(pBt) );
52165 
52166     /* A malformed database page might cause us to read past the end
52167     ** of page when parsing a cell.
52168     **
52169     ** The following block of code checks early to see if a cell extends
52170     ** past the end of a page boundary and causes SQLITE_CORRUPT to be
52171     ** returned if it does.
52172     */
52173     iCellFirst = cellOffset + 2*pPage->nCell;
52174     iCellLast = usableSize - 4;
52175 #if defined(SQLITE_ENABLE_OVERSIZE_CELL_CHECK)
52176     {
52177       int i;            /* Index into the cell pointer array */
52178       int sz;           /* Size of a cell */
52179 
52180       if( !pPage->leaf ) iCellLast--;
52181       for(i=0; i<pPage->nCell; i++){
52182         pc = get2byte(&data[cellOffset+i*2]);
52183         testcase( pc==iCellFirst );
52184         testcase( pc==iCellLast );
52185         if( pc<iCellFirst || pc>iCellLast ){
52186           return SQLITE_CORRUPT_BKPT;
52187         }
52188         sz = cellSizePtr(pPage, &data[pc]);
52189         testcase( pc+sz==usableSize );
52190         if( pc+sz>usableSize ){
52191           return SQLITE_CORRUPT_BKPT;
52192         }
52193       }
52194       if( !pPage->leaf ) iCellLast++;
52195     }
52196 #endif
52197 
52198     /* Compute the total free space on the page */
52199     pc = get2byte(&data[hdr+1]);
52200     nFree = data[hdr+7] + top;
52201     while( pc>0 ){
52202       u16 next, size;
52203       if( pc<iCellFirst || pc>iCellLast ){
52204         /* Start of free block is off the page */
52205         return SQLITE_CORRUPT_BKPT;
52206       }
52207       next = get2byte(&data[pc]);
52208       size = get2byte(&data[pc+2]);
52209       if( (next>0 && next<=pc+size+3) || pc+size>usableSize ){
52210         /* Free blocks must be in ascending order. And the last byte of
52211         ** the free-block must lie on the database page.  */
52212         return SQLITE_CORRUPT_BKPT;
52213       }
52214       nFree = nFree + size;
52215       pc = next;
52216     }
52217 
52218     /* At this point, nFree contains the sum of the offset to the start
52219     ** of the cell-content area plus the number of free bytes within
52220     ** the cell-content area. If this is greater than the usable-size
52221     ** of the page, then the page must be corrupted. This check also
52222     ** serves to verify that the offset to the start of the cell-content
52223     ** area, according to the page header, lies within the page.
52224     */
52225     if( nFree>usableSize ){
52226       return SQLITE_CORRUPT_BKPT;
52227     }
52228     pPage->nFree = (u16)(nFree - iCellFirst);
52229     pPage->isInit = 1;
52230   }
52231   return SQLITE_OK;
52232 }
52233 
52234 /*
52235 ** Set up a raw page so that it looks like a database page holding
52236 ** no entries.
52237 */
52238 static void zeroPage(MemPage *pPage, int flags){
52239   unsigned char *data = pPage->aData;
52240   BtShared *pBt = pPage->pBt;
52241   u8 hdr = pPage->hdrOffset;
52242   u16 first;
52243 
52244   assert( sqlite3PagerPagenumber(pPage->pDbPage)==pPage->pgno );
52245   assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
52246   assert( sqlite3PagerGetData(pPage->pDbPage) == data );
52247   assert( sqlite3PagerIswriteable(pPage->pDbPage) );
52248   assert( sqlite3_mutex_held(pBt->mutex) );
52249   if( pBt->btsFlags & BTS_SECURE_DELETE ){
52250     memset(&data[hdr], 0, pBt->usableSize - hdr);
52251   }
52252   data[hdr] = (char)flags;
52253   first = hdr + 8 + 4*((flags&PTF_LEAF)==0 ?1:0);
52254   memset(&data[hdr+1], 0, 4);
52255   data[hdr+7] = 0;
52256   put2byte(&data[hdr+5], pBt->usableSize);
52257   pPage->nFree = (u16)(pBt->usableSize - first);
52258   decodeFlags(pPage, flags);
52259   pPage->hdrOffset = hdr;
52260   pPage->cellOffset = first;
52261   pPage->aDataEnd = &data[pBt->usableSize];
52262   pPage->aCellIdx = &data[first];
52263   pPage->nOverflow = 0;
52264   assert( pBt->pageSize>=512 && pBt->pageSize<=65536 );
52265   pPage->maskPage = (u16)(pBt->pageSize - 1);
52266   pPage->nCell = 0;
52267   pPage->isInit = 1;
52268 }
52269 
52270 
52271 /*
52272 ** Convert a DbPage obtained from the pager into a MemPage used by
52273 ** the btree layer.
52274 */
52275 static MemPage *btreePageFromDbPage(DbPage *pDbPage, Pgno pgno, BtShared *pBt){
52276   MemPage *pPage = (MemPage*)sqlite3PagerGetExtra(pDbPage);
52277   pPage->aData = sqlite3PagerGetData(pDbPage);
52278   pPage->pDbPage = pDbPage;
52279   pPage->pBt = pBt;
52280   pPage->pgno = pgno;
52281   pPage->hdrOffset = pPage->pgno==1 ? 100 : 0;
52282   return pPage;
52283 }
52284 
52285 /*
52286 ** Get a page from the pager.  Initialize the MemPage.pBt and
52287 ** MemPage.aData elements if needed.
52288 **
52289 ** If the noContent flag is set, it means that we do not care about
52290 ** the content of the page at this time.  So do not go to the disk
52291 ** to fetch the content.  Just fill in the content with zeros for now.
52292 ** If in the future we call sqlite3PagerWrite() on this page, that
52293 ** means we have started to be concerned about content and the disk
52294 ** read should occur at that point.
52295 */
52296 static int btreeGetPage(
52297   BtShared *pBt,       /* The btree */
52298   Pgno pgno,           /* Number of the page to fetch */
52299   MemPage **ppPage,    /* Return the page in this parameter */
52300   int flags            /* PAGER_GET_NOCONTENT or PAGER_GET_READONLY */
52301 ){
52302   int rc;
52303   DbPage *pDbPage;
52304 
52305   assert( flags==0 || flags==PAGER_GET_NOCONTENT || flags==PAGER_GET_READONLY );
52306   assert( sqlite3_mutex_held(pBt->mutex) );
52307   rc = sqlite3PagerAcquire(pBt->pPager, pgno, (DbPage**)&pDbPage, flags);
52308   if( rc ) return rc;
52309   *ppPage = btreePageFromDbPage(pDbPage, pgno, pBt);
52310   return SQLITE_OK;
52311 }
52312 
52313 /*
52314 ** Retrieve a page from the pager cache. If the requested page is not
52315 ** already in the pager cache return NULL. Initialize the MemPage.pBt and
52316 ** MemPage.aData elements if needed.
52317 */
52318 static MemPage *btreePageLookup(BtShared *pBt, Pgno pgno){
52319   DbPage *pDbPage;
52320   assert( sqlite3_mutex_held(pBt->mutex) );
52321   pDbPage = sqlite3PagerLookup(pBt->pPager, pgno);
52322   if( pDbPage ){
52323     return btreePageFromDbPage(pDbPage, pgno, pBt);
52324   }
52325   return 0;
52326 }
52327 
52328 /*
52329 ** Return the size of the database file in pages. If there is any kind of
52330 ** error, return ((unsigned int)-1).
52331 */
52332 static Pgno btreePagecount(BtShared *pBt){
52333   return pBt->nPage;
52334 }
52335 SQLITE_PRIVATE u32 sqlite3BtreeLastPage(Btree *p){
52336   assert( sqlite3BtreeHoldsMutex(p) );
52337   assert( ((p->pBt->nPage)&0x8000000)==0 );
52338   return (int)btreePagecount(p->pBt);
52339 }
52340 
52341 /*
52342 ** Get a page from the pager and initialize it.  This routine is just a
52343 ** convenience wrapper around separate calls to btreeGetPage() and
52344 ** btreeInitPage().
52345 **
52346 ** If an error occurs, then the value *ppPage is set to is undefined. It
52347 ** may remain unchanged, or it may be set to an invalid value.
52348 */
52349 static int getAndInitPage(
52350   BtShared *pBt,                  /* The database file */
52351   Pgno pgno,                      /* Number of the page to get */
52352   MemPage **ppPage,               /* Write the page pointer here */
52353   int bReadonly                   /* PAGER_GET_READONLY or 0 */
52354 ){
52355   int rc;
52356   assert( sqlite3_mutex_held(pBt->mutex) );
52357   assert( bReadonly==PAGER_GET_READONLY || bReadonly==0 );
52358 
52359   if( pgno>btreePagecount(pBt) ){
52360     rc = SQLITE_CORRUPT_BKPT;
52361   }else{
52362     rc = btreeGetPage(pBt, pgno, ppPage, bReadonly);
52363     if( rc==SQLITE_OK && (*ppPage)->isInit==0 ){
52364       rc = btreeInitPage(*ppPage);
52365       if( rc!=SQLITE_OK ){
52366         releasePage(*ppPage);
52367       }
52368     }
52369   }
52370 
52371   testcase( pgno==0 );
52372   assert( pgno!=0 || rc==SQLITE_CORRUPT );
52373   return rc;
52374 }
52375 
52376 /*
52377 ** Release a MemPage.  This should be called once for each prior
52378 ** call to btreeGetPage.
52379 */
52380 static void releasePage(MemPage *pPage){
52381   if( pPage ){
52382     assert( pPage->aData );
52383     assert( pPage->pBt );
52384     assert( pPage->pDbPage!=0 );
52385     assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage );
52386     assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData );
52387     assert( sqlite3_mutex_held(pPage->pBt->mutex) );
52388     sqlite3PagerUnrefNotNull(pPage->pDbPage);
52389   }
52390 }
52391 
52392 /*
52393 ** During a rollback, when the pager reloads information into the cache
52394 ** so that the cache is restored to its original state at the start of
52395 ** the transaction, for each page restored this routine is called.
52396 **
52397 ** This routine needs to reset the extra data section at the end of the
52398 ** page to agree with the restored data.
52399 */
52400 static void pageReinit(DbPage *pData){
52401   MemPage *pPage;
52402   pPage = (MemPage *)sqlite3PagerGetExtra(pData);
52403   assert( sqlite3PagerPageRefcount(pData)>0 );
52404   if( pPage->isInit ){
52405     assert( sqlite3_mutex_held(pPage->pBt->mutex) );
52406     pPage->isInit = 0;
52407     if( sqlite3PagerPageRefcount(pData)>1 ){
52408       /* pPage might not be a btree page;  it might be an overflow page
52409       ** or ptrmap page or a free page.  In those cases, the following
52410       ** call to btreeInitPage() will likely return SQLITE_CORRUPT.
52411       ** But no harm is done by this.  And it is very important that
52412       ** btreeInitPage() be called on every btree page so we make
52413       ** the call for every page that comes in for re-initing. */
52414       btreeInitPage(pPage);
52415     }
52416   }
52417 }
52418 
52419 /*
52420 ** Invoke the busy handler for a btree.
52421 */
52422 static int btreeInvokeBusyHandler(void *pArg){
52423   BtShared *pBt = (BtShared*)pArg;
52424   assert( pBt->db );
52425   assert( sqlite3_mutex_held(pBt->db->mutex) );
52426   return sqlite3InvokeBusyHandler(&pBt->db->busyHandler);
52427 }
52428 
52429 /*
52430 ** Open a database file.
52431 **
52432 ** zFilename is the name of the database file.  If zFilename is NULL
52433 ** then an ephemeral database is created.  The ephemeral database might
52434 ** be exclusively in memory, or it might use a disk-based memory cache.
52435 ** Either way, the ephemeral database will be automatically deleted
52436 ** when sqlite3BtreeClose() is called.
52437 **
52438 ** If zFilename is ":memory:" then an in-memory database is created
52439 ** that is automatically destroyed when it is closed.
52440 **
52441 ** The "flags" parameter is a bitmask that might contain bits like
52442 ** BTREE_OMIT_JOURNAL and/or BTREE_MEMORY.
52443 **
52444 ** If the database is already opened in the same database connection
52445 ** and we are in shared cache mode, then the open will fail with an
52446 ** SQLITE_CONSTRAINT error.  We cannot allow two or more BtShared
52447 ** objects in the same database connection since doing so will lead
52448 ** to problems with locking.
52449 */
52450 SQLITE_PRIVATE int sqlite3BtreeOpen(
52451   sqlite3_vfs *pVfs,      /* VFS to use for this b-tree */
52452   const char *zFilename,  /* Name of the file containing the BTree database */
52453   sqlite3 *db,            /* Associated database handle */
52454   Btree **ppBtree,        /* Pointer to new Btree object written here */
52455   int flags,              /* Options */
52456   int vfsFlags            /* Flags passed through to sqlite3_vfs.xOpen() */
52457 ){
52458   BtShared *pBt = 0;             /* Shared part of btree structure */
52459   Btree *p;                      /* Handle to return */
52460   sqlite3_mutex *mutexOpen = 0;  /* Prevents a race condition. Ticket #3537 */
52461   int rc = SQLITE_OK;            /* Result code from this function */
52462   u8 nReserve;                   /* Byte of unused space on each page */
52463   unsigned char zDbHeader[100];  /* Database header content */
52464 
52465   /* True if opening an ephemeral, temporary database */
52466   const int isTempDb = zFilename==0 || zFilename[0]==0;
52467 
52468   /* Set the variable isMemdb to true for an in-memory database, or
52469   ** false for a file-based database.
52470   */
52471 #ifdef SQLITE_OMIT_MEMORYDB
52472   const int isMemdb = 0;
52473 #else
52474   const int isMemdb = (zFilename && strcmp(zFilename, ":memory:")==0)
52475                        || (isTempDb && sqlite3TempInMemory(db))
52476                        || (vfsFlags & SQLITE_OPEN_MEMORY)!=0;
52477 #endif
52478 
52479   assert( db!=0 );
52480   assert( pVfs!=0 );
52481   assert( sqlite3_mutex_held(db->mutex) );
52482   assert( (flags&0xff)==flags );   /* flags fit in 8 bits */
52483 
52484   /* Only a BTREE_SINGLE database can be BTREE_UNORDERED */
52485   assert( (flags & BTREE_UNORDERED)==0 || (flags & BTREE_SINGLE)!=0 );
52486 
52487   /* A BTREE_SINGLE database is always a temporary and/or ephemeral */
52488   assert( (flags & BTREE_SINGLE)==0 || isTempDb );
52489 
52490   if( isMemdb ){
52491     flags |= BTREE_MEMORY;
52492   }
52493   if( (vfsFlags & SQLITE_OPEN_MAIN_DB)!=0 && (isMemdb || isTempDb) ){
52494     vfsFlags = (vfsFlags & ~SQLITE_OPEN_MAIN_DB) | SQLITE_OPEN_TEMP_DB;
52495   }
52496   p = sqlite3MallocZero(sizeof(Btree));
52497   if( !p ){
52498     return SQLITE_NOMEM;
52499   }
52500   p->inTrans = TRANS_NONE;
52501   p->db = db;
52502 #ifndef SQLITE_OMIT_SHARED_CACHE
52503   p->lock.pBtree = p;
52504   p->lock.iTable = 1;
52505 #endif
52506 
52507 #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
52508   /*
52509   ** If this Btree is a candidate for shared cache, try to find an
52510   ** existing BtShared object that we can share with
52511   */
52512   if( isTempDb==0 && (isMemdb==0 || (vfsFlags&SQLITE_OPEN_URI)!=0) ){
52513     if( vfsFlags & SQLITE_OPEN_SHAREDCACHE ){
52514       int nFullPathname = pVfs->mxPathname+1;
52515       char *zFullPathname = sqlite3Malloc(nFullPathname);
52516       MUTEX_LOGIC( sqlite3_mutex *mutexShared; )
52517       p->sharable = 1;
52518       if( !zFullPathname ){
52519         sqlite3_free(p);
52520         return SQLITE_NOMEM;
52521       }
52522       if( isMemdb ){
52523         memcpy(zFullPathname, zFilename, sqlite3Strlen30(zFilename)+1);
52524       }else{
52525         rc = sqlite3OsFullPathname(pVfs, zFilename,
52526                                    nFullPathname, zFullPathname);
52527         if( rc ){
52528           sqlite3_free(zFullPathname);
52529           sqlite3_free(p);
52530           return rc;
52531         }
52532       }
52533 #if SQLITE_THREADSAFE
52534       mutexOpen = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_OPEN);
52535       sqlite3_mutex_enter(mutexOpen);
52536       mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
52537       sqlite3_mutex_enter(mutexShared);
52538 #endif
52539       for(pBt=GLOBAL(BtShared*,sqlite3SharedCacheList); pBt; pBt=pBt->pNext){
52540         assert( pBt->nRef>0 );
52541         if( 0==strcmp(zFullPathname, sqlite3PagerFilename(pBt->pPager, 0))
52542                  && sqlite3PagerVfs(pBt->pPager)==pVfs ){
52543           int iDb;
52544           for(iDb=db->nDb-1; iDb>=0; iDb--){
52545             Btree *pExisting = db->aDb[iDb].pBt;
52546             if( pExisting && pExisting->pBt==pBt ){
52547               sqlite3_mutex_leave(mutexShared);
52548               sqlite3_mutex_leave(mutexOpen);
52549               sqlite3_free(zFullPathname);
52550               sqlite3_free(p);
52551               return SQLITE_CONSTRAINT;
52552             }
52553           }
52554           p->pBt = pBt;
52555           pBt->nRef++;
52556           break;
52557         }
52558       }
52559       sqlite3_mutex_leave(mutexShared);
52560       sqlite3_free(zFullPathname);
52561     }
52562 #ifdef SQLITE_DEBUG
52563     else{
52564       /* In debug mode, we mark all persistent databases as sharable
52565       ** even when they are not.  This exercises the locking code and
52566       ** gives more opportunity for asserts(sqlite3_mutex_held())
52567       ** statements to find locking problems.
52568       */
52569       p->sharable = 1;
52570     }
52571 #endif
52572   }
52573 #endif
52574   if( pBt==0 ){
52575     /*
52576     ** The following asserts make sure that structures used by the btree are
52577     ** the right size.  This is to guard against size changes that result
52578     ** when compiling on a different architecture.
52579     */
52580     assert( sizeof(i64)==8 || sizeof(i64)==4 );
52581     assert( sizeof(u64)==8 || sizeof(u64)==4 );
52582     assert( sizeof(u32)==4 );
52583     assert( sizeof(u16)==2 );
52584     assert( sizeof(Pgno)==4 );
52585 
52586     pBt = sqlite3MallocZero( sizeof(*pBt) );
52587     if( pBt==0 ){
52588       rc = SQLITE_NOMEM;
52589       goto btree_open_out;
52590     }
52591     rc = sqlite3PagerOpen(pVfs, &pBt->pPager, zFilename,
52592                           EXTRA_SIZE, flags, vfsFlags, pageReinit);
52593     if( rc==SQLITE_OK ){
52594       sqlite3PagerSetMmapLimit(pBt->pPager, db->szMmap);
52595       rc = sqlite3PagerReadFileheader(pBt->pPager,sizeof(zDbHeader),zDbHeader);
52596     }
52597     if( rc!=SQLITE_OK ){
52598       goto btree_open_out;
52599     }
52600     pBt->openFlags = (u8)flags;
52601     pBt->db = db;
52602     sqlite3PagerSetBusyhandler(pBt->pPager, btreeInvokeBusyHandler, pBt);
52603     p->pBt = pBt;
52604 
52605     pBt->pCursor = 0;
52606     pBt->pPage1 = 0;
52607     if( sqlite3PagerIsreadonly(pBt->pPager) ) pBt->btsFlags |= BTS_READ_ONLY;
52608 #ifdef SQLITE_SECURE_DELETE
52609     pBt->btsFlags |= BTS_SECURE_DELETE;
52610 #endif
52611     pBt->pageSize = (zDbHeader[16]<<8) | (zDbHeader[17]<<16);
52612     if( pBt->pageSize<512 || pBt->pageSize>SQLITE_MAX_PAGE_SIZE
52613          || ((pBt->pageSize-1)&pBt->pageSize)!=0 ){
52614       pBt->pageSize = 0;
52615 #ifndef SQLITE_OMIT_AUTOVACUUM
52616       /* If the magic name ":memory:" will create an in-memory database, then
52617       ** leave the autoVacuum mode at 0 (do not auto-vacuum), even if
52618       ** SQLITE_DEFAULT_AUTOVACUUM is true. On the other hand, if
52619       ** SQLITE_OMIT_MEMORYDB has been defined, then ":memory:" is just a
52620       ** regular file-name. In this case the auto-vacuum applies as per normal.
52621       */
52622       if( zFilename && !isMemdb ){
52623         pBt->autoVacuum = (SQLITE_DEFAULT_AUTOVACUUM ? 1 : 0);
52624         pBt->incrVacuum = (SQLITE_DEFAULT_AUTOVACUUM==2 ? 1 : 0);
52625       }
52626 #endif
52627       nReserve = 0;
52628     }else{
52629       nReserve = zDbHeader[20];
52630       pBt->btsFlags |= BTS_PAGESIZE_FIXED;
52631 #ifndef SQLITE_OMIT_AUTOVACUUM
52632       pBt->autoVacuum = (get4byte(&zDbHeader[36 + 4*4])?1:0);
52633       pBt->incrVacuum = (get4byte(&zDbHeader[36 + 7*4])?1:0);
52634 #endif
52635     }
52636     rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve);
52637     if( rc ) goto btree_open_out;
52638     pBt->usableSize = pBt->pageSize - nReserve;
52639     assert( (pBt->pageSize & 7)==0 );  /* 8-byte alignment of pageSize */
52640 
52641 #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
52642     /* Add the new BtShared object to the linked list sharable BtShareds.
52643     */
52644     if( p->sharable ){
52645       MUTEX_LOGIC( sqlite3_mutex *mutexShared; )
52646       pBt->nRef = 1;
52647       MUTEX_LOGIC( mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);)
52648       if( SQLITE_THREADSAFE && sqlite3GlobalConfig.bCoreMutex ){
52649         pBt->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_FAST);
52650         if( pBt->mutex==0 ){
52651           rc = SQLITE_NOMEM;
52652           db->mallocFailed = 0;
52653           goto btree_open_out;
52654         }
52655       }
52656       sqlite3_mutex_enter(mutexShared);
52657       pBt->pNext = GLOBAL(BtShared*,sqlite3SharedCacheList);
52658       GLOBAL(BtShared*,sqlite3SharedCacheList) = pBt;
52659       sqlite3_mutex_leave(mutexShared);
52660     }
52661 #endif
52662   }
52663 
52664 #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO)
52665   /* If the new Btree uses a sharable pBtShared, then link the new
52666   ** Btree into the list of all sharable Btrees for the same connection.
52667   ** The list is kept in ascending order by pBt address.
52668   */
52669   if( p->sharable ){
52670     int i;
52671     Btree *pSib;
52672     for(i=0; i<db->nDb; i++){
52673       if( (pSib = db->aDb[i].pBt)!=0 && pSib->sharable ){
52674         while( pSib->pPrev ){ pSib = pSib->pPrev; }
52675         if( p->pBt<pSib->pBt ){
52676           p->pNext = pSib;
52677           p->pPrev = 0;
52678           pSib->pPrev = p;
52679         }else{
52680           while( pSib->pNext && pSib->pNext->pBt<p->pBt ){
52681             pSib = pSib->pNext;
52682           }
52683           p->pNext = pSib->pNext;
52684           p->pPrev = pSib;
52685           if( p->pNext ){
52686             p->pNext->pPrev = p;
52687           }
52688           pSib->pNext = p;
52689         }
52690         break;
52691       }
52692     }
52693   }
52694 #endif
52695   *ppBtree = p;
52696 
52697 btree_open_out:
52698   if( rc!=SQLITE_OK ){
52699     if( pBt && pBt->pPager ){
52700       sqlite3PagerClose(pBt->pPager);
52701     }
52702     sqlite3_free(pBt);
52703     sqlite3_free(p);
52704     *ppBtree = 0;
52705   }else{
52706     /* If the B-Tree was successfully opened, set the pager-cache size to the
52707     ** default value. Except, when opening on an existing shared pager-cache,
52708     ** do not change the pager-cache size.
52709     */
52710     if( sqlite3BtreeSchema(p, 0, 0)==0 ){
52711       sqlite3PagerSetCachesize(p->pBt->pPager, SQLITE_DEFAULT_CACHE_SIZE);
52712     }
52713   }
52714   if( mutexOpen ){
52715     assert( sqlite3_mutex_held(mutexOpen) );
52716     sqlite3_mutex_leave(mutexOpen);
52717   }
52718   return rc;
52719 }
52720 
52721 /*
52722 ** Decrement the BtShared.nRef counter.  When it reaches zero,
52723 ** remove the BtShared structure from the sharing list.  Return
52724 ** true if the BtShared.nRef counter reaches zero and return
52725 ** false if it is still positive.
52726 */
52727 static int removeFromSharingList(BtShared *pBt){
52728 #ifndef SQLITE_OMIT_SHARED_CACHE
52729   MUTEX_LOGIC( sqlite3_mutex *pMaster; )
52730   BtShared *pList;
52731   int removed = 0;
52732 
52733   assert( sqlite3_mutex_notheld(pBt->mutex) );
52734   MUTEX_LOGIC( pMaster = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); )
52735   sqlite3_mutex_enter(pMaster);
52736   pBt->nRef--;
52737   if( pBt->nRef<=0 ){
52738     if( GLOBAL(BtShared*,sqlite3SharedCacheList)==pBt ){
52739       GLOBAL(BtShared*,sqlite3SharedCacheList) = pBt->pNext;
52740     }else{
52741       pList = GLOBAL(BtShared*,sqlite3SharedCacheList);
52742       while( ALWAYS(pList) && pList->pNext!=pBt ){
52743         pList=pList->pNext;
52744       }
52745       if( ALWAYS(pList) ){
52746         pList->pNext = pBt->pNext;
52747       }
52748     }
52749     if( SQLITE_THREADSAFE ){
52750       sqlite3_mutex_free(pBt->mutex);
52751     }
52752     removed = 1;
52753   }
52754   sqlite3_mutex_leave(pMaster);
52755   return removed;
52756 #else
52757   return 1;
52758 #endif
52759 }
52760 
52761 /*
52762 ** Make sure pBt->pTmpSpace points to an allocation of
52763 ** MX_CELL_SIZE(pBt) bytes.
52764 */
52765 static void allocateTempSpace(BtShared *pBt){
52766   if( !pBt->pTmpSpace ){
52767     pBt->pTmpSpace = sqlite3PageMalloc( pBt->pageSize );
52768 
52769     /* One of the uses of pBt->pTmpSpace is to format cells before
52770     ** inserting them into a leaf page (function fillInCell()). If
52771     ** a cell is less than 4 bytes in size, it is rounded up to 4 bytes
52772     ** by the various routines that manipulate binary cells. Which
52773     ** can mean that fillInCell() only initializes the first 2 or 3
52774     ** bytes of pTmpSpace, but that the first 4 bytes are copied from
52775     ** it into a database page. This is not actually a problem, but it
52776     ** does cause a valgrind error when the 1 or 2 bytes of unitialized
52777     ** data is passed to system call write(). So to avoid this error,
52778     ** zero the first 4 bytes of temp space here.  */
52779     if( pBt->pTmpSpace ) memset(pBt->pTmpSpace, 0, 4);
52780   }
52781 }
52782 
52783 /*
52784 ** Free the pBt->pTmpSpace allocation
52785 */
52786 static void freeTempSpace(BtShared *pBt){
52787   sqlite3PageFree( pBt->pTmpSpace);
52788   pBt->pTmpSpace = 0;
52789 }
52790 
52791 /*
52792 ** Close an open database and invalidate all cursors.
52793 */
52794 SQLITE_PRIVATE int sqlite3BtreeClose(Btree *p){
52795   BtShared *pBt = p->pBt;
52796   BtCursor *pCur;
52797 
52798   /* Close all cursors opened via this handle.  */
52799   assert( sqlite3_mutex_held(p->db->mutex) );
52800   sqlite3BtreeEnter(p);
52801   pCur = pBt->pCursor;
52802   while( pCur ){
52803     BtCursor *pTmp = pCur;
52804     pCur = pCur->pNext;
52805     if( pTmp->pBtree==p ){
52806       sqlite3BtreeCloseCursor(pTmp);
52807     }
52808   }
52809 
52810   /* Rollback any active transaction and free the handle structure.
52811   ** The call to sqlite3BtreeRollback() drops any table-locks held by
52812   ** this handle.
52813   */
52814   sqlite3BtreeRollback(p, SQLITE_OK);
52815   sqlite3BtreeLeave(p);
52816 
52817   /* If there are still other outstanding references to the shared-btree
52818   ** structure, return now. The remainder of this procedure cleans
52819   ** up the shared-btree.
52820   */
52821   assert( p->wantToLock==0 && p->locked==0 );
52822   if( !p->sharable || removeFromSharingList(pBt) ){
52823     /* The pBt is no longer on the sharing list, so we can access
52824     ** it without having to hold the mutex.
52825     **
52826     ** Clean out and delete the BtShared object.
52827     */
52828     assert( !pBt->pCursor );
52829     sqlite3PagerClose(pBt->pPager);
52830     if( pBt->xFreeSchema && pBt->pSchema ){
52831       pBt->xFreeSchema(pBt->pSchema);
52832     }
52833     sqlite3DbFree(0, pBt->pSchema);
52834     freeTempSpace(pBt);
52835     sqlite3_free(pBt);
52836   }
52837 
52838 #ifndef SQLITE_OMIT_SHARED_CACHE
52839   assert( p->wantToLock==0 );
52840   assert( p->locked==0 );
52841   if( p->pPrev ) p->pPrev->pNext = p->pNext;
52842   if( p->pNext ) p->pNext->pPrev = p->pPrev;
52843 #endif
52844 
52845   sqlite3_free(p);
52846   return SQLITE_OK;
52847 }
52848 
52849 /*
52850 ** Change the limit on the number of pages allowed in the cache.
52851 **
52852 ** The maximum number of cache pages is set to the absolute
52853 ** value of mxPage.  If mxPage is negative, the pager will
52854 ** operate asynchronously - it will not stop to do fsync()s
52855 ** to insure data is written to the disk surface before
52856 ** continuing.  Transactions still work if synchronous is off,
52857 ** and the database cannot be corrupted if this program
52858 ** crashes.  But if the operating system crashes or there is
52859 ** an abrupt power failure when synchronous is off, the database
52860 ** could be left in an inconsistent and unrecoverable state.
52861 ** Synchronous is on by default so database corruption is not
52862 ** normally a worry.
52863 */
52864 SQLITE_PRIVATE int sqlite3BtreeSetCacheSize(Btree *p, int mxPage){
52865   BtShared *pBt = p->pBt;
52866   assert( sqlite3_mutex_held(p->db->mutex) );
52867   sqlite3BtreeEnter(p);
52868   sqlite3PagerSetCachesize(pBt->pPager, mxPage);
52869   sqlite3BtreeLeave(p);
52870   return SQLITE_OK;
52871 }
52872 
52873 /*
52874 ** Change the limit on the amount of the database file that may be
52875 ** memory mapped.
52876 */
52877 SQLITE_PRIVATE int sqlite3BtreeSetMmapLimit(Btree *p, sqlite3_int64 szMmap){
52878   BtShared *pBt = p->pBt;
52879   assert( sqlite3_mutex_held(p->db->mutex) );
52880   sqlite3BtreeEnter(p);
52881   sqlite3PagerSetMmapLimit(pBt->pPager, szMmap);
52882   sqlite3BtreeLeave(p);
52883   return SQLITE_OK;
52884 }
52885 
52886 /*
52887 ** Change the way data is synced to disk in order to increase or decrease
52888 ** how well the database resists damage due to OS crashes and power
52889 ** failures.  Level 1 is the same as asynchronous (no syncs() occur and
52890 ** there is a high probability of damage)  Level 2 is the default.  There
52891 ** is a very low but non-zero probability of damage.  Level 3 reduces the
52892 ** probability of damage to near zero but with a write performance reduction.
52893 */
52894 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
52895 SQLITE_PRIVATE int sqlite3BtreeSetPagerFlags(
52896   Btree *p,              /* The btree to set the safety level on */
52897   unsigned pgFlags       /* Various PAGER_* flags */
52898 ){
52899   BtShared *pBt = p->pBt;
52900   assert( sqlite3_mutex_held(p->db->mutex) );
52901   sqlite3BtreeEnter(p);
52902   sqlite3PagerSetFlags(pBt->pPager, pgFlags);
52903   sqlite3BtreeLeave(p);
52904   return SQLITE_OK;
52905 }
52906 #endif
52907 
52908 /*
52909 ** Return TRUE if the given btree is set to safety level 1.  In other
52910 ** words, return TRUE if no sync() occurs on the disk files.
52911 */
52912 SQLITE_PRIVATE int sqlite3BtreeSyncDisabled(Btree *p){
52913   BtShared *pBt = p->pBt;
52914   int rc;
52915   assert( sqlite3_mutex_held(p->db->mutex) );
52916   sqlite3BtreeEnter(p);
52917   assert( pBt && pBt->pPager );
52918   rc = sqlite3PagerNosync(pBt->pPager);
52919   sqlite3BtreeLeave(p);
52920   return rc;
52921 }
52922 
52923 /*
52924 ** Change the default pages size and the number of reserved bytes per page.
52925 ** Or, if the page size has already been fixed, return SQLITE_READONLY
52926 ** without changing anything.
52927 **
52928 ** The page size must be a power of 2 between 512 and 65536.  If the page
52929 ** size supplied does not meet this constraint then the page size is not
52930 ** changed.
52931 **
52932 ** Page sizes are constrained to be a power of two so that the region
52933 ** of the database file used for locking (beginning at PENDING_BYTE,
52934 ** the first byte past the 1GB boundary, 0x40000000) needs to occur
52935 ** at the beginning of a page.
52936 **
52937 ** If parameter nReserve is less than zero, then the number of reserved
52938 ** bytes per page is left unchanged.
52939 **
52940 ** If the iFix!=0 then the BTS_PAGESIZE_FIXED flag is set so that the page size
52941 ** and autovacuum mode can no longer be changed.
52942 */
52943 SQLITE_PRIVATE int sqlite3BtreeSetPageSize(Btree *p, int pageSize, int nReserve, int iFix){
52944   int rc = SQLITE_OK;
52945   BtShared *pBt = p->pBt;
52946   assert( nReserve>=-1 && nReserve<=255 );
52947   sqlite3BtreeEnter(p);
52948   if( pBt->btsFlags & BTS_PAGESIZE_FIXED ){
52949     sqlite3BtreeLeave(p);
52950     return SQLITE_READONLY;
52951   }
52952   if( nReserve<0 ){
52953     nReserve = pBt->pageSize - pBt->usableSize;
52954   }
52955   assert( nReserve>=0 && nReserve<=255 );
52956   if( pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE &&
52957         ((pageSize-1)&pageSize)==0 ){
52958     assert( (pageSize & 7)==0 );
52959     assert( !pBt->pPage1 && !pBt->pCursor );
52960     pBt->pageSize = (u32)pageSize;
52961     freeTempSpace(pBt);
52962   }
52963   rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve);
52964   pBt->usableSize = pBt->pageSize - (u16)nReserve;
52965   if( iFix ) pBt->btsFlags |= BTS_PAGESIZE_FIXED;
52966   sqlite3BtreeLeave(p);
52967   return rc;
52968 }
52969 
52970 /*
52971 ** Return the currently defined page size
52972 */
52973 SQLITE_PRIVATE int sqlite3BtreeGetPageSize(Btree *p){
52974   return p->pBt->pageSize;
52975 }
52976 
52977 #if defined(SQLITE_HAS_CODEC) || defined(SQLITE_DEBUG)
52978 /*
52979 ** This function is similar to sqlite3BtreeGetReserve(), except that it
52980 ** may only be called if it is guaranteed that the b-tree mutex is already
52981 ** held.
52982 **
52983 ** This is useful in one special case in the backup API code where it is
52984 ** known that the shared b-tree mutex is held, but the mutex on the
52985 ** database handle that owns *p is not. In this case if sqlite3BtreeEnter()
52986 ** were to be called, it might collide with some other operation on the
52987 ** database handle that owns *p, causing undefined behavior.
52988 */
52989 SQLITE_PRIVATE int sqlite3BtreeGetReserveNoMutex(Btree *p){
52990   assert( sqlite3_mutex_held(p->pBt->mutex) );
52991   return p->pBt->pageSize - p->pBt->usableSize;
52992 }
52993 #endif /* SQLITE_HAS_CODEC || SQLITE_DEBUG */
52994 
52995 #if !defined(SQLITE_OMIT_PAGER_PRAGMAS) || !defined(SQLITE_OMIT_VACUUM)
52996 /*
52997 ** Return the number of bytes of space at the end of every page that
52998 ** are intentually left unused.  This is the "reserved" space that is
52999 ** sometimes used by extensions.
53000 */
53001 SQLITE_PRIVATE int sqlite3BtreeGetReserve(Btree *p){
53002   int n;
53003   sqlite3BtreeEnter(p);
53004   n = p->pBt->pageSize - p->pBt->usableSize;
53005   sqlite3BtreeLeave(p);
53006   return n;
53007 }
53008 
53009 /*
53010 ** Set the maximum page count for a database if mxPage is positive.
53011 ** No changes are made if mxPage is 0 or negative.
53012 ** Regardless of the value of mxPage, return the maximum page count.
53013 */
53014 SQLITE_PRIVATE int sqlite3BtreeMaxPageCount(Btree *p, int mxPage){
53015   int n;
53016   sqlite3BtreeEnter(p);
53017   n = sqlite3PagerMaxPageCount(p->pBt->pPager, mxPage);
53018   sqlite3BtreeLeave(p);
53019   return n;
53020 }
53021 
53022 /*
53023 ** Set the BTS_SECURE_DELETE flag if newFlag is 0 or 1.  If newFlag is -1,
53024 ** then make no changes.  Always return the value of the BTS_SECURE_DELETE
53025 ** setting after the change.
53026 */
53027 SQLITE_PRIVATE int sqlite3BtreeSecureDelete(Btree *p, int newFlag){
53028   int b;
53029   if( p==0 ) return 0;
53030   sqlite3BtreeEnter(p);
53031   if( newFlag>=0 ){
53032     p->pBt->btsFlags &= ~BTS_SECURE_DELETE;
53033     if( newFlag ) p->pBt->btsFlags |= BTS_SECURE_DELETE;
53034   }
53035   b = (p->pBt->btsFlags & BTS_SECURE_DELETE)!=0;
53036   sqlite3BtreeLeave(p);
53037   return b;
53038 }
53039 #endif /* !defined(SQLITE_OMIT_PAGER_PRAGMAS) || !defined(SQLITE_OMIT_VACUUM) */
53040 
53041 /*
53042 ** Change the 'auto-vacuum' property of the database. If the 'autoVacuum'
53043 ** parameter is non-zero, then auto-vacuum mode is enabled. If zero, it
53044 ** is disabled. The default value for the auto-vacuum property is
53045 ** determined by the SQLITE_DEFAULT_AUTOVACUUM macro.
53046 */
53047 SQLITE_PRIVATE int sqlite3BtreeSetAutoVacuum(Btree *p, int autoVacuum){
53048 #ifdef SQLITE_OMIT_AUTOVACUUM
53049   return SQLITE_READONLY;
53050 #else
53051   BtShared *pBt = p->pBt;
53052   int rc = SQLITE_OK;
53053   u8 av = (u8)autoVacuum;
53054 
53055   sqlite3BtreeEnter(p);
53056   if( (pBt->btsFlags & BTS_PAGESIZE_FIXED)!=0 && (av ?1:0)!=pBt->autoVacuum ){
53057     rc = SQLITE_READONLY;
53058   }else{
53059     pBt->autoVacuum = av ?1:0;
53060     pBt->incrVacuum = av==2 ?1:0;
53061   }
53062   sqlite3BtreeLeave(p);
53063   return rc;
53064 #endif
53065 }
53066 
53067 /*
53068 ** Return the value of the 'auto-vacuum' property. If auto-vacuum is
53069 ** enabled 1 is returned. Otherwise 0.
53070 */
53071 SQLITE_PRIVATE int sqlite3BtreeGetAutoVacuum(Btree *p){
53072 #ifdef SQLITE_OMIT_AUTOVACUUM
53073   return BTREE_AUTOVACUUM_NONE;
53074 #else
53075   int rc;
53076   sqlite3BtreeEnter(p);
53077   rc = (
53078     (!p->pBt->autoVacuum)?BTREE_AUTOVACUUM_NONE:
53079     (!p->pBt->incrVacuum)?BTREE_AUTOVACUUM_FULL:
53080     BTREE_AUTOVACUUM_INCR
53081   );
53082   sqlite3BtreeLeave(p);
53083   return rc;
53084 #endif
53085 }
53086 
53087 
53088 /*
53089 ** Get a reference to pPage1 of the database file.  This will
53090 ** also acquire a readlock on that file.
53091 **
53092 ** SQLITE_OK is returned on success.  If the file is not a
53093 ** well-formed database file, then SQLITE_CORRUPT is returned.
53094 ** SQLITE_BUSY is returned if the database is locked.  SQLITE_NOMEM
53095 ** is returned if we run out of memory.
53096 */
53097 static int lockBtree(BtShared *pBt){
53098   int rc;              /* Result code from subfunctions */
53099   MemPage *pPage1;     /* Page 1 of the database file */
53100   int nPage;           /* Number of pages in the database */
53101   int nPageFile = 0;   /* Number of pages in the database file */
53102   int nPageHeader;     /* Number of pages in the database according to hdr */
53103 
53104   assert( sqlite3_mutex_held(pBt->mutex) );
53105   assert( pBt->pPage1==0 );
53106   rc = sqlite3PagerSharedLock(pBt->pPager);
53107   if( rc!=SQLITE_OK ) return rc;
53108   rc = btreeGetPage(pBt, 1, &pPage1, 0);
53109   if( rc!=SQLITE_OK ) return rc;
53110 
53111   /* Do some checking to help insure the file we opened really is
53112   ** a valid database file.
53113   */
53114   nPage = nPageHeader = get4byte(28+(u8*)pPage1->aData);
53115   sqlite3PagerPagecount(pBt->pPager, &nPageFile);
53116   if( nPage==0 || memcmp(24+(u8*)pPage1->aData, 92+(u8*)pPage1->aData,4)!=0 ){
53117     nPage = nPageFile;
53118   }
53119   if( nPage>0 ){
53120     u32 pageSize;
53121     u32 usableSize;
53122     u8 *page1 = pPage1->aData;
53123     rc = SQLITE_NOTADB;
53124     if( memcmp(page1, zMagicHeader, 16)!=0 ){
53125       goto page1_init_failed;
53126     }
53127 
53128 #ifdef SQLITE_OMIT_WAL
53129     if( page1[18]>1 ){
53130       pBt->btsFlags |= BTS_READ_ONLY;
53131     }
53132     if( page1[19]>1 ){
53133       goto page1_init_failed;
53134     }
53135 #else
53136     if( page1[18]>2 ){
53137       pBt->btsFlags |= BTS_READ_ONLY;
53138     }
53139     if( page1[19]>2 ){
53140       goto page1_init_failed;
53141     }
53142 
53143     /* If the write version is set to 2, this database should be accessed
53144     ** in WAL mode. If the log is not already open, open it now. Then
53145     ** return SQLITE_OK and return without populating BtShared.pPage1.
53146     ** The caller detects this and calls this function again. This is
53147     ** required as the version of page 1 currently in the page1 buffer
53148     ** may not be the latest version - there may be a newer one in the log
53149     ** file.
53150     */
53151     if( page1[19]==2 && (pBt->btsFlags & BTS_NO_WAL)==0 ){
53152       int isOpen = 0;
53153       rc = sqlite3PagerOpenWal(pBt->pPager, &isOpen);
53154       if( rc!=SQLITE_OK ){
53155         goto page1_init_failed;
53156       }else if( isOpen==0 ){
53157         releasePage(pPage1);
53158         return SQLITE_OK;
53159       }
53160       rc = SQLITE_NOTADB;
53161     }
53162 #endif
53163 
53164     /* The maximum embedded fraction must be exactly 25%.  And the minimum
53165     ** embedded fraction must be 12.5% for both leaf-data and non-leaf-data.
53166     ** The original design allowed these amounts to vary, but as of
53167     ** version 3.6.0, we require them to be fixed.
53168     */
53169     if( memcmp(&page1[21], "\100\040\040",3)!=0 ){
53170       goto page1_init_failed;
53171     }
53172     pageSize = (page1[16]<<8) | (page1[17]<<16);
53173     if( ((pageSize-1)&pageSize)!=0
53174      || pageSize>SQLITE_MAX_PAGE_SIZE
53175      || pageSize<=256
53176     ){
53177       goto page1_init_failed;
53178     }
53179     assert( (pageSize & 7)==0 );
53180     usableSize = pageSize - page1[20];
53181     if( (u32)pageSize!=pBt->pageSize ){
53182       /* After reading the first page of the database assuming a page size
53183       ** of BtShared.pageSize, we have discovered that the page-size is
53184       ** actually pageSize. Unlock the database, leave pBt->pPage1 at
53185       ** zero and return SQLITE_OK. The caller will call this function
53186       ** again with the correct page-size.
53187       */
53188       releasePage(pPage1);
53189       pBt->usableSize = usableSize;
53190       pBt->pageSize = pageSize;
53191       freeTempSpace(pBt);
53192       rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize,
53193                                    pageSize-usableSize);
53194       return rc;
53195     }
53196     if( (pBt->db->flags & SQLITE_RecoveryMode)==0 && nPage>nPageFile ){
53197       rc = SQLITE_CORRUPT_BKPT;
53198       goto page1_init_failed;
53199     }
53200     if( usableSize<480 ){
53201       goto page1_init_failed;
53202     }
53203     pBt->pageSize = pageSize;
53204     pBt->usableSize = usableSize;
53205 #ifndef SQLITE_OMIT_AUTOVACUUM
53206     pBt->autoVacuum = (get4byte(&page1[36 + 4*4])?1:0);
53207     pBt->incrVacuum = (get4byte(&page1[36 + 7*4])?1:0);
53208 #endif
53209   }
53210 
53211   /* maxLocal is the maximum amount of payload to store locally for
53212   ** a cell.  Make sure it is small enough so that at least minFanout
53213   ** cells can will fit on one page.  We assume a 10-byte page header.
53214   ** Besides the payload, the cell must store:
53215   **     2-byte pointer to the cell
53216   **     4-byte child pointer
53217   **     9-byte nKey value
53218   **     4-byte nData value
53219   **     4-byte overflow page pointer
53220   ** So a cell consists of a 2-byte pointer, a header which is as much as
53221   ** 17 bytes long, 0 to N bytes of payload, and an optional 4 byte overflow
53222   ** page pointer.
53223   */
53224   pBt->maxLocal = (u16)((pBt->usableSize-12)*64/255 - 23);
53225   pBt->minLocal = (u16)((pBt->usableSize-12)*32/255 - 23);
53226   pBt->maxLeaf = (u16)(pBt->usableSize - 35);
53227   pBt->minLeaf = (u16)((pBt->usableSize-12)*32/255 - 23);
53228   if( pBt->maxLocal>127 ){
53229     pBt->max1bytePayload = 127;
53230   }else{
53231     pBt->max1bytePayload = (u8)pBt->maxLocal;
53232   }
53233   assert( pBt->maxLeaf + 23 <= MX_CELL_SIZE(pBt) );
53234   pBt->pPage1 = pPage1;
53235   pBt->nPage = nPage;
53236   return SQLITE_OK;
53237 
53238 page1_init_failed:
53239   releasePage(pPage1);
53240   pBt->pPage1 = 0;
53241   return rc;
53242 }
53243 
53244 #ifndef NDEBUG
53245 /*
53246 ** Return the number of cursors open on pBt. This is for use
53247 ** in assert() expressions, so it is only compiled if NDEBUG is not
53248 ** defined.
53249 **
53250 ** Only write cursors are counted if wrOnly is true.  If wrOnly is
53251 ** false then all cursors are counted.
53252 **
53253 ** For the purposes of this routine, a cursor is any cursor that
53254 ** is capable of reading or writing to the databse.  Cursors that
53255 ** have been tripped into the CURSOR_FAULT state are not counted.
53256 */
53257 static int countValidCursors(BtShared *pBt, int wrOnly){
53258   BtCursor *pCur;
53259   int r = 0;
53260   for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){
53261     if( (wrOnly==0 || pCur->wrFlag) && pCur->eState!=CURSOR_FAULT ) r++;
53262   }
53263   return r;
53264 }
53265 #endif
53266 
53267 /*
53268 ** If there are no outstanding cursors and we are not in the middle
53269 ** of a transaction but there is a read lock on the database, then
53270 ** this routine unrefs the first page of the database file which
53271 ** has the effect of releasing the read lock.
53272 **
53273 ** If there is a transaction in progress, this routine is a no-op.
53274 */
53275 static void unlockBtreeIfUnused(BtShared *pBt){
53276   assert( sqlite3_mutex_held(pBt->mutex) );
53277   assert( countValidCursors(pBt,0)==0 || pBt->inTransaction>TRANS_NONE );
53278   if( pBt->inTransaction==TRANS_NONE && pBt->pPage1!=0 ){
53279     assert( pBt->pPage1->aData );
53280     assert( sqlite3PagerRefcount(pBt->pPager)==1 );
53281     assert( pBt->pPage1->aData );
53282     releasePage(pBt->pPage1);
53283     pBt->pPage1 = 0;
53284   }
53285 }
53286 
53287 /*
53288 ** If pBt points to an empty file then convert that empty file
53289 ** into a new empty database by initializing the first page of
53290 ** the database.
53291 */
53292 static int newDatabase(BtShared *pBt){
53293   MemPage *pP1;
53294   unsigned char *data;
53295   int rc;
53296 
53297   assert( sqlite3_mutex_held(pBt->mutex) );
53298   if( pBt->nPage>0 ){
53299     return SQLITE_OK;
53300   }
53301   pP1 = pBt->pPage1;
53302   assert( pP1!=0 );
53303   data = pP1->aData;
53304   rc = sqlite3PagerWrite(pP1->pDbPage);
53305   if( rc ) return rc;
53306   memcpy(data, zMagicHeader, sizeof(zMagicHeader));
53307   assert( sizeof(zMagicHeader)==16 );
53308   data[16] = (u8)((pBt->pageSize>>8)&0xff);
53309   data[17] = (u8)((pBt->pageSize>>16)&0xff);
53310   data[18] = 1;
53311   data[19] = 1;
53312   assert( pBt->usableSize<=pBt->pageSize && pBt->usableSize+255>=pBt->pageSize);
53313   data[20] = (u8)(pBt->pageSize - pBt->usableSize);
53314   data[21] = 64;
53315   data[22] = 32;
53316   data[23] = 32;
53317   memset(&data[24], 0, 100-24);
53318   zeroPage(pP1, PTF_INTKEY|PTF_LEAF|PTF_LEAFDATA );
53319   pBt->btsFlags |= BTS_PAGESIZE_FIXED;
53320 #ifndef SQLITE_OMIT_AUTOVACUUM
53321   assert( pBt->autoVacuum==1 || pBt->autoVacuum==0 );
53322   assert( pBt->incrVacuum==1 || pBt->incrVacuum==0 );
53323   put4byte(&data[36 + 4*4], pBt->autoVacuum);
53324   put4byte(&data[36 + 7*4], pBt->incrVacuum);
53325 #endif
53326   pBt->nPage = 1;
53327   data[31] = 1;
53328   return SQLITE_OK;
53329 }
53330 
53331 /*
53332 ** Initialize the first page of the database file (creating a database
53333 ** consisting of a single page and no schema objects). Return SQLITE_OK
53334 ** if successful, or an SQLite error code otherwise.
53335 */
53336 SQLITE_PRIVATE int sqlite3BtreeNewDb(Btree *p){
53337   int rc;
53338   sqlite3BtreeEnter(p);
53339   p->pBt->nPage = 0;
53340   rc = newDatabase(p->pBt);
53341   sqlite3BtreeLeave(p);
53342   return rc;
53343 }
53344 
53345 /*
53346 ** Attempt to start a new transaction. A write-transaction
53347 ** is started if the second argument is nonzero, otherwise a read-
53348 ** transaction.  If the second argument is 2 or more and exclusive
53349 ** transaction is started, meaning that no other process is allowed
53350 ** to access the database.  A preexisting transaction may not be
53351 ** upgraded to exclusive by calling this routine a second time - the
53352 ** exclusivity flag only works for a new transaction.
53353 **
53354 ** A write-transaction must be started before attempting any
53355 ** changes to the database.  None of the following routines
53356 ** will work unless a transaction is started first:
53357 **
53358 **      sqlite3BtreeCreateTable()
53359 **      sqlite3BtreeCreateIndex()
53360 **      sqlite3BtreeClearTable()
53361 **      sqlite3BtreeDropTable()
53362 **      sqlite3BtreeInsert()
53363 **      sqlite3BtreeDelete()
53364 **      sqlite3BtreeUpdateMeta()
53365 **
53366 ** If an initial attempt to acquire the lock fails because of lock contention
53367 ** and the database was previously unlocked, then invoke the busy handler
53368 ** if there is one.  But if there was previously a read-lock, do not
53369 ** invoke the busy handler - just return SQLITE_BUSY.  SQLITE_BUSY is
53370 ** returned when there is already a read-lock in order to avoid a deadlock.
53371 **
53372 ** Suppose there are two processes A and B.  A has a read lock and B has
53373 ** a reserved lock.  B tries to promote to exclusive but is blocked because
53374 ** of A's read lock.  A tries to promote to reserved but is blocked by B.
53375 ** One or the other of the two processes must give way or there can be
53376 ** no progress.  By returning SQLITE_BUSY and not invoking the busy callback
53377 ** when A already has a read lock, we encourage A to give up and let B
53378 ** proceed.
53379 */
53380 SQLITE_PRIVATE int sqlite3BtreeBeginTrans(Btree *p, int wrflag){
53381   sqlite3 *pBlock = 0;
53382   BtShared *pBt = p->pBt;
53383   int rc = SQLITE_OK;
53384 
53385   sqlite3BtreeEnter(p);
53386   btreeIntegrity(p);
53387 
53388   /* If the btree is already in a write-transaction, or it
53389   ** is already in a read-transaction and a read-transaction
53390   ** is requested, this is a no-op.
53391   */
53392   if( p->inTrans==TRANS_WRITE || (p->inTrans==TRANS_READ && !wrflag) ){
53393     goto trans_begun;
53394   }
53395   assert( pBt->inTransaction==TRANS_WRITE || IfNotOmitAV(pBt->bDoTruncate)==0 );
53396 
53397   /* Write transactions are not possible on a read-only database */
53398   if( (pBt->btsFlags & BTS_READ_ONLY)!=0 && wrflag ){
53399     rc = SQLITE_READONLY;
53400     goto trans_begun;
53401   }
53402 
53403 #ifndef SQLITE_OMIT_SHARED_CACHE
53404   /* If another database handle has already opened a write transaction
53405   ** on this shared-btree structure and a second write transaction is
53406   ** requested, return SQLITE_LOCKED.
53407   */
53408   if( (wrflag && pBt->inTransaction==TRANS_WRITE)
53409    || (pBt->btsFlags & BTS_PENDING)!=0
53410   ){
53411     pBlock = pBt->pWriter->db;
53412   }else if( wrflag>1 ){
53413     BtLock *pIter;
53414     for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){
53415       if( pIter->pBtree!=p ){
53416         pBlock = pIter->pBtree->db;
53417         break;
53418       }
53419     }
53420   }
53421   if( pBlock ){
53422     sqlite3ConnectionBlocked(p->db, pBlock);
53423     rc = SQLITE_LOCKED_SHAREDCACHE;
53424     goto trans_begun;
53425   }
53426 #endif
53427 
53428   /* Any read-only or read-write transaction implies a read-lock on
53429   ** page 1. So if some other shared-cache client already has a write-lock
53430   ** on page 1, the transaction cannot be opened. */
53431   rc = querySharedCacheTableLock(p, MASTER_ROOT, READ_LOCK);
53432   if( SQLITE_OK!=rc ) goto trans_begun;
53433 
53434   pBt->btsFlags &= ~BTS_INITIALLY_EMPTY;
53435   if( pBt->nPage==0 ) pBt->btsFlags |= BTS_INITIALLY_EMPTY;
53436   do {
53437     /* Call lockBtree() until either pBt->pPage1 is populated or
53438     ** lockBtree() returns something other than SQLITE_OK. lockBtree()
53439     ** may return SQLITE_OK but leave pBt->pPage1 set to 0 if after
53440     ** reading page 1 it discovers that the page-size of the database
53441     ** file is not pBt->pageSize. In this case lockBtree() will update
53442     ** pBt->pageSize to the page-size of the file on disk.
53443     */
53444     while( pBt->pPage1==0 && SQLITE_OK==(rc = lockBtree(pBt)) );
53445 
53446     if( rc==SQLITE_OK && wrflag ){
53447       if( (pBt->btsFlags & BTS_READ_ONLY)!=0 ){
53448         rc = SQLITE_READONLY;
53449       }else{
53450         rc = sqlite3PagerBegin(pBt->pPager,wrflag>1,sqlite3TempInMemory(p->db));
53451         if( rc==SQLITE_OK ){
53452           rc = newDatabase(pBt);
53453         }
53454       }
53455     }
53456 
53457     if( rc!=SQLITE_OK ){
53458       unlockBtreeIfUnused(pBt);
53459     }
53460   }while( (rc&0xFF)==SQLITE_BUSY && pBt->inTransaction==TRANS_NONE &&
53461           btreeInvokeBusyHandler(pBt) );
53462 
53463   if( rc==SQLITE_OK ){
53464     if( p->inTrans==TRANS_NONE ){
53465       pBt->nTransaction++;
53466 #ifndef SQLITE_OMIT_SHARED_CACHE
53467       if( p->sharable ){
53468         assert( p->lock.pBtree==p && p->lock.iTable==1 );
53469         p->lock.eLock = READ_LOCK;
53470         p->lock.pNext = pBt->pLock;
53471         pBt->pLock = &p->lock;
53472       }
53473 #endif
53474     }
53475     p->inTrans = (wrflag?TRANS_WRITE:TRANS_READ);
53476     if( p->inTrans>pBt->inTransaction ){
53477       pBt->inTransaction = p->inTrans;
53478     }
53479     if( wrflag ){
53480       MemPage *pPage1 = pBt->pPage1;
53481 #ifndef SQLITE_OMIT_SHARED_CACHE
53482       assert( !pBt->pWriter );
53483       pBt->pWriter = p;
53484       pBt->btsFlags &= ~BTS_EXCLUSIVE;
53485       if( wrflag>1 ) pBt->btsFlags |= BTS_EXCLUSIVE;
53486 #endif
53487 
53488       /* If the db-size header field is incorrect (as it may be if an old
53489       ** client has been writing the database file), update it now. Doing
53490       ** this sooner rather than later means the database size can safely
53491       ** re-read the database size from page 1 if a savepoint or transaction
53492       ** rollback occurs within the transaction.
53493       */
53494       if( pBt->nPage!=get4byte(&pPage1->aData[28]) ){
53495         rc = sqlite3PagerWrite(pPage1->pDbPage);
53496         if( rc==SQLITE_OK ){
53497           put4byte(&pPage1->aData[28], pBt->nPage);
53498         }
53499       }
53500     }
53501   }
53502 
53503 
53504 trans_begun:
53505   if( rc==SQLITE_OK && wrflag ){
53506     /* This call makes sure that the pager has the correct number of
53507     ** open savepoints. If the second parameter is greater than 0 and
53508     ** the sub-journal is not already open, then it will be opened here.
53509     */
53510     rc = sqlite3PagerOpenSavepoint(pBt->pPager, p->db->nSavepoint);
53511   }
53512 
53513   btreeIntegrity(p);
53514   sqlite3BtreeLeave(p);
53515   return rc;
53516 }
53517 
53518 #ifndef SQLITE_OMIT_AUTOVACUUM
53519 
53520 /*
53521 ** Set the pointer-map entries for all children of page pPage. Also, if
53522 ** pPage contains cells that point to overflow pages, set the pointer
53523 ** map entries for the overflow pages as well.
53524 */
53525 static int setChildPtrmaps(MemPage *pPage){
53526   int i;                             /* Counter variable */
53527   int nCell;                         /* Number of cells in page pPage */
53528   int rc;                            /* Return code */
53529   BtShared *pBt = pPage->pBt;
53530   u8 isInitOrig = pPage->isInit;
53531   Pgno pgno = pPage->pgno;
53532 
53533   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
53534   rc = btreeInitPage(pPage);
53535   if( rc!=SQLITE_OK ){
53536     goto set_child_ptrmaps_out;
53537   }
53538   nCell = pPage->nCell;
53539 
53540   for(i=0; i<nCell; i++){
53541     u8 *pCell = findCell(pPage, i);
53542 
53543     ptrmapPutOvflPtr(pPage, pCell, &rc);
53544 
53545     if( !pPage->leaf ){
53546       Pgno childPgno = get4byte(pCell);
53547       ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno, &rc);
53548     }
53549   }
53550 
53551   if( !pPage->leaf ){
53552     Pgno childPgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
53553     ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno, &rc);
53554   }
53555 
53556 set_child_ptrmaps_out:
53557   pPage->isInit = isInitOrig;
53558   return rc;
53559 }
53560 
53561 /*
53562 ** Somewhere on pPage is a pointer to page iFrom.  Modify this pointer so
53563 ** that it points to iTo. Parameter eType describes the type of pointer to
53564 ** be modified, as  follows:
53565 **
53566 ** PTRMAP_BTREE:     pPage is a btree-page. The pointer points at a child
53567 **                   page of pPage.
53568 **
53569 ** PTRMAP_OVERFLOW1: pPage is a btree-page. The pointer points at an overflow
53570 **                   page pointed to by one of the cells on pPage.
53571 **
53572 ** PTRMAP_OVERFLOW2: pPage is an overflow-page. The pointer points at the next
53573 **                   overflow page in the list.
53574 */
53575 static int modifyPagePointer(MemPage *pPage, Pgno iFrom, Pgno iTo, u8 eType){
53576   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
53577   assert( sqlite3PagerIswriteable(pPage->pDbPage) );
53578   if( eType==PTRMAP_OVERFLOW2 ){
53579     /* The pointer is always the first 4 bytes of the page in this case.  */
53580     if( get4byte(pPage->aData)!=iFrom ){
53581       return SQLITE_CORRUPT_BKPT;
53582     }
53583     put4byte(pPage->aData, iTo);
53584   }else{
53585     u8 isInitOrig = pPage->isInit;
53586     int i;
53587     int nCell;
53588 
53589     btreeInitPage(pPage);
53590     nCell = pPage->nCell;
53591 
53592     for(i=0; i<nCell; i++){
53593       u8 *pCell = findCell(pPage, i);
53594       if( eType==PTRMAP_OVERFLOW1 ){
53595         CellInfo info;
53596         btreeParseCellPtr(pPage, pCell, &info);
53597         if( info.iOverflow
53598          && pCell+info.iOverflow+3<=pPage->aData+pPage->maskPage
53599          && iFrom==get4byte(&pCell[info.iOverflow])
53600         ){
53601           put4byte(&pCell[info.iOverflow], iTo);
53602           break;
53603         }
53604       }else{
53605         if( get4byte(pCell)==iFrom ){
53606           put4byte(pCell, iTo);
53607           break;
53608         }
53609       }
53610     }
53611 
53612     if( i==nCell ){
53613       if( eType!=PTRMAP_BTREE ||
53614           get4byte(&pPage->aData[pPage->hdrOffset+8])!=iFrom ){
53615         return SQLITE_CORRUPT_BKPT;
53616       }
53617       put4byte(&pPage->aData[pPage->hdrOffset+8], iTo);
53618     }
53619 
53620     pPage->isInit = isInitOrig;
53621   }
53622   return SQLITE_OK;
53623 }
53624 
53625 
53626 /*
53627 ** Move the open database page pDbPage to location iFreePage in the
53628 ** database. The pDbPage reference remains valid.
53629 **
53630 ** The isCommit flag indicates that there is no need to remember that
53631 ** the journal needs to be sync()ed before database page pDbPage->pgno
53632 ** can be written to. The caller has already promised not to write to that
53633 ** page.
53634 */
53635 static int relocatePage(
53636   BtShared *pBt,           /* Btree */
53637   MemPage *pDbPage,        /* Open page to move */
53638   u8 eType,                /* Pointer map 'type' entry for pDbPage */
53639   Pgno iPtrPage,           /* Pointer map 'page-no' entry for pDbPage */
53640   Pgno iFreePage,          /* The location to move pDbPage to */
53641   int isCommit             /* isCommit flag passed to sqlite3PagerMovepage */
53642 ){
53643   MemPage *pPtrPage;   /* The page that contains a pointer to pDbPage */
53644   Pgno iDbPage = pDbPage->pgno;
53645   Pager *pPager = pBt->pPager;
53646   int rc;
53647 
53648   assert( eType==PTRMAP_OVERFLOW2 || eType==PTRMAP_OVERFLOW1 ||
53649       eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE );
53650   assert( sqlite3_mutex_held(pBt->mutex) );
53651   assert( pDbPage->pBt==pBt );
53652 
53653   /* Move page iDbPage from its current location to page number iFreePage */
53654   TRACE(("AUTOVACUUM: Moving %d to free page %d (ptr page %d type %d)\n",
53655       iDbPage, iFreePage, iPtrPage, eType));
53656   rc = sqlite3PagerMovepage(pPager, pDbPage->pDbPage, iFreePage, isCommit);
53657   if( rc!=SQLITE_OK ){
53658     return rc;
53659   }
53660   pDbPage->pgno = iFreePage;
53661 
53662   /* If pDbPage was a btree-page, then it may have child pages and/or cells
53663   ** that point to overflow pages. The pointer map entries for all these
53664   ** pages need to be changed.
53665   **
53666   ** If pDbPage is an overflow page, then the first 4 bytes may store a
53667   ** pointer to a subsequent overflow page. If this is the case, then
53668   ** the pointer map needs to be updated for the subsequent overflow page.
53669   */
53670   if( eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE ){
53671     rc = setChildPtrmaps(pDbPage);
53672     if( rc!=SQLITE_OK ){
53673       return rc;
53674     }
53675   }else{
53676     Pgno nextOvfl = get4byte(pDbPage->aData);
53677     if( nextOvfl!=0 ){
53678       ptrmapPut(pBt, nextOvfl, PTRMAP_OVERFLOW2, iFreePage, &rc);
53679       if( rc!=SQLITE_OK ){
53680         return rc;
53681       }
53682     }
53683   }
53684 
53685   /* Fix the database pointer on page iPtrPage that pointed at iDbPage so
53686   ** that it points at iFreePage. Also fix the pointer map entry for
53687   ** iPtrPage.
53688   */
53689   if( eType!=PTRMAP_ROOTPAGE ){
53690     rc = btreeGetPage(pBt, iPtrPage, &pPtrPage, 0);
53691     if( rc!=SQLITE_OK ){
53692       return rc;
53693     }
53694     rc = sqlite3PagerWrite(pPtrPage->pDbPage);
53695     if( rc!=SQLITE_OK ){
53696       releasePage(pPtrPage);
53697       return rc;
53698     }
53699     rc = modifyPagePointer(pPtrPage, iDbPage, iFreePage, eType);
53700     releasePage(pPtrPage);
53701     if( rc==SQLITE_OK ){
53702       ptrmapPut(pBt, iFreePage, eType, iPtrPage, &rc);
53703     }
53704   }
53705   return rc;
53706 }
53707 
53708 /* Forward declaration required by incrVacuumStep(). */
53709 static int allocateBtreePage(BtShared *, MemPage **, Pgno *, Pgno, u8);
53710 
53711 /*
53712 ** Perform a single step of an incremental-vacuum. If successful, return
53713 ** SQLITE_OK. If there is no work to do (and therefore no point in
53714 ** calling this function again), return SQLITE_DONE. Or, if an error
53715 ** occurs, return some other error code.
53716 **
53717 ** More specificly, this function attempts to re-organize the database so
53718 ** that the last page of the file currently in use is no longer in use.
53719 **
53720 ** Parameter nFin is the number of pages that this database would contain
53721 ** were this function called until it returns SQLITE_DONE.
53722 **
53723 ** If the bCommit parameter is non-zero, this function assumes that the
53724 ** caller will keep calling incrVacuumStep() until it returns SQLITE_DONE
53725 ** or an error. bCommit is passed true for an auto-vacuum-on-commmit
53726 ** operation, or false for an incremental vacuum.
53727 */
53728 static int incrVacuumStep(BtShared *pBt, Pgno nFin, Pgno iLastPg, int bCommit){
53729   Pgno nFreeList;           /* Number of pages still on the free-list */
53730   int rc;
53731 
53732   assert( sqlite3_mutex_held(pBt->mutex) );
53733   assert( iLastPg>nFin );
53734 
53735   if( !PTRMAP_ISPAGE(pBt, iLastPg) && iLastPg!=PENDING_BYTE_PAGE(pBt) ){
53736     u8 eType;
53737     Pgno iPtrPage;
53738 
53739     nFreeList = get4byte(&pBt->pPage1->aData[36]);
53740     if( nFreeList==0 ){
53741       return SQLITE_DONE;
53742     }
53743 
53744     rc = ptrmapGet(pBt, iLastPg, &eType, &iPtrPage);
53745     if( rc!=SQLITE_OK ){
53746       return rc;
53747     }
53748     if( eType==PTRMAP_ROOTPAGE ){
53749       return SQLITE_CORRUPT_BKPT;
53750     }
53751 
53752     if( eType==PTRMAP_FREEPAGE ){
53753       if( bCommit==0 ){
53754         /* Remove the page from the files free-list. This is not required
53755         ** if bCommit is non-zero. In that case, the free-list will be
53756         ** truncated to zero after this function returns, so it doesn't
53757         ** matter if it still contains some garbage entries.
53758         */
53759         Pgno iFreePg;
53760         MemPage *pFreePg;
53761         rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, iLastPg, BTALLOC_EXACT);
53762         if( rc!=SQLITE_OK ){
53763           return rc;
53764         }
53765         assert( iFreePg==iLastPg );
53766         releasePage(pFreePg);
53767       }
53768     } else {
53769       Pgno iFreePg;             /* Index of free page to move pLastPg to */
53770       MemPage *pLastPg;
53771       u8 eMode = BTALLOC_ANY;   /* Mode parameter for allocateBtreePage() */
53772       Pgno iNear = 0;           /* nearby parameter for allocateBtreePage() */
53773 
53774       rc = btreeGetPage(pBt, iLastPg, &pLastPg, 0);
53775       if( rc!=SQLITE_OK ){
53776         return rc;
53777       }
53778 
53779       /* If bCommit is zero, this loop runs exactly once and page pLastPg
53780       ** is swapped with the first free page pulled off the free list.
53781       **
53782       ** On the other hand, if bCommit is greater than zero, then keep
53783       ** looping until a free-page located within the first nFin pages
53784       ** of the file is found.
53785       */
53786       if( bCommit==0 ){
53787         eMode = BTALLOC_LE;
53788         iNear = nFin;
53789       }
53790       do {
53791         MemPage *pFreePg;
53792         rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, iNear, eMode);
53793         if( rc!=SQLITE_OK ){
53794           releasePage(pLastPg);
53795           return rc;
53796         }
53797         releasePage(pFreePg);
53798       }while( bCommit && iFreePg>nFin );
53799       assert( iFreePg<iLastPg );
53800 
53801       rc = relocatePage(pBt, pLastPg, eType, iPtrPage, iFreePg, bCommit);
53802       releasePage(pLastPg);
53803       if( rc!=SQLITE_OK ){
53804         return rc;
53805       }
53806     }
53807   }
53808 
53809   if( bCommit==0 ){
53810     do {
53811       iLastPg--;
53812     }while( iLastPg==PENDING_BYTE_PAGE(pBt) || PTRMAP_ISPAGE(pBt, iLastPg) );
53813     pBt->bDoTruncate = 1;
53814     pBt->nPage = iLastPg;
53815   }
53816   return SQLITE_OK;
53817 }
53818 
53819 /*
53820 ** The database opened by the first argument is an auto-vacuum database
53821 ** nOrig pages in size containing nFree free pages. Return the expected
53822 ** size of the database in pages following an auto-vacuum operation.
53823 */
53824 static Pgno finalDbSize(BtShared *pBt, Pgno nOrig, Pgno nFree){
53825   int nEntry;                     /* Number of entries on one ptrmap page */
53826   Pgno nPtrmap;                   /* Number of PtrMap pages to be freed */
53827   Pgno nFin;                      /* Return value */
53828 
53829   nEntry = pBt->usableSize/5;
53830   nPtrmap = (nFree-nOrig+PTRMAP_PAGENO(pBt, nOrig)+nEntry)/nEntry;
53831   nFin = nOrig - nFree - nPtrmap;
53832   if( nOrig>PENDING_BYTE_PAGE(pBt) && nFin<PENDING_BYTE_PAGE(pBt) ){
53833     nFin--;
53834   }
53835   while( PTRMAP_ISPAGE(pBt, nFin) || nFin==PENDING_BYTE_PAGE(pBt) ){
53836     nFin--;
53837   }
53838 
53839   return nFin;
53840 }
53841 
53842 /*
53843 ** A write-transaction must be opened before calling this function.
53844 ** It performs a single unit of work towards an incremental vacuum.
53845 **
53846 ** If the incremental vacuum is finished after this function has run,
53847 ** SQLITE_DONE is returned. If it is not finished, but no error occurred,
53848 ** SQLITE_OK is returned. Otherwise an SQLite error code.
53849 */
53850 SQLITE_PRIVATE int sqlite3BtreeIncrVacuum(Btree *p){
53851   int rc;
53852   BtShared *pBt = p->pBt;
53853 
53854   sqlite3BtreeEnter(p);
53855   assert( pBt->inTransaction==TRANS_WRITE && p->inTrans==TRANS_WRITE );
53856   if( !pBt->autoVacuum ){
53857     rc = SQLITE_DONE;
53858   }else{
53859     Pgno nOrig = btreePagecount(pBt);
53860     Pgno nFree = get4byte(&pBt->pPage1->aData[36]);
53861     Pgno nFin = finalDbSize(pBt, nOrig, nFree);
53862 
53863     if( nOrig<nFin ){
53864       rc = SQLITE_CORRUPT_BKPT;
53865     }else if( nFree>0 ){
53866       rc = saveAllCursors(pBt, 0, 0);
53867       if( rc==SQLITE_OK ){
53868         invalidateAllOverflowCache(pBt);
53869         rc = incrVacuumStep(pBt, nFin, nOrig, 0);
53870       }
53871       if( rc==SQLITE_OK ){
53872         rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
53873         put4byte(&pBt->pPage1->aData[28], pBt->nPage);
53874       }
53875     }else{
53876       rc = SQLITE_DONE;
53877     }
53878   }
53879   sqlite3BtreeLeave(p);
53880   return rc;
53881 }
53882 
53883 /*
53884 ** This routine is called prior to sqlite3PagerCommit when a transaction
53885 ** is committed for an auto-vacuum database.
53886 **
53887 ** If SQLITE_OK is returned, then *pnTrunc is set to the number of pages
53888 ** the database file should be truncated to during the commit process.
53889 ** i.e. the database has been reorganized so that only the first *pnTrunc
53890 ** pages are in use.
53891 */
53892 static int autoVacuumCommit(BtShared *pBt){
53893   int rc = SQLITE_OK;
53894   Pager *pPager = pBt->pPager;
53895   VVA_ONLY( int nRef = sqlite3PagerRefcount(pPager) );
53896 
53897   assert( sqlite3_mutex_held(pBt->mutex) );
53898   invalidateAllOverflowCache(pBt);
53899   assert(pBt->autoVacuum);
53900   if( !pBt->incrVacuum ){
53901     Pgno nFin;         /* Number of pages in database after autovacuuming */
53902     Pgno nFree;        /* Number of pages on the freelist initially */
53903     Pgno iFree;        /* The next page to be freed */
53904     Pgno nOrig;        /* Database size before freeing */
53905 
53906     nOrig = btreePagecount(pBt);
53907     if( PTRMAP_ISPAGE(pBt, nOrig) || nOrig==PENDING_BYTE_PAGE(pBt) ){
53908       /* It is not possible to create a database for which the final page
53909       ** is either a pointer-map page or the pending-byte page. If one
53910       ** is encountered, this indicates corruption.
53911       */
53912       return SQLITE_CORRUPT_BKPT;
53913     }
53914 
53915     nFree = get4byte(&pBt->pPage1->aData[36]);
53916     nFin = finalDbSize(pBt, nOrig, nFree);
53917     if( nFin>nOrig ) return SQLITE_CORRUPT_BKPT;
53918     if( nFin<nOrig ){
53919       rc = saveAllCursors(pBt, 0, 0);
53920     }
53921     for(iFree=nOrig; iFree>nFin && rc==SQLITE_OK; iFree--){
53922       rc = incrVacuumStep(pBt, nFin, iFree, 1);
53923     }
53924     if( (rc==SQLITE_DONE || rc==SQLITE_OK) && nFree>0 ){
53925       rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
53926       put4byte(&pBt->pPage1->aData[32], 0);
53927       put4byte(&pBt->pPage1->aData[36], 0);
53928       put4byte(&pBt->pPage1->aData[28], nFin);
53929       pBt->bDoTruncate = 1;
53930       pBt->nPage = nFin;
53931     }
53932     if( rc!=SQLITE_OK ){
53933       sqlite3PagerRollback(pPager);
53934     }
53935   }
53936 
53937   assert( nRef>=sqlite3PagerRefcount(pPager) );
53938   return rc;
53939 }
53940 
53941 #else /* ifndef SQLITE_OMIT_AUTOVACUUM */
53942 # define setChildPtrmaps(x) SQLITE_OK
53943 #endif
53944 
53945 /*
53946 ** This routine does the first phase of a two-phase commit.  This routine
53947 ** causes a rollback journal to be created (if it does not already exist)
53948 ** and populated with enough information so that if a power loss occurs
53949 ** the database can be restored to its original state by playing back
53950 ** the journal.  Then the contents of the journal are flushed out to
53951 ** the disk.  After the journal is safely on oxide, the changes to the
53952 ** database are written into the database file and flushed to oxide.
53953 ** At the end of this call, the rollback journal still exists on the
53954 ** disk and we are still holding all locks, so the transaction has not
53955 ** committed.  See sqlite3BtreeCommitPhaseTwo() for the second phase of the
53956 ** commit process.
53957 **
53958 ** This call is a no-op if no write-transaction is currently active on pBt.
53959 **
53960 ** Otherwise, sync the database file for the btree pBt. zMaster points to
53961 ** the name of a master journal file that should be written into the
53962 ** individual journal file, or is NULL, indicating no master journal file
53963 ** (single database transaction).
53964 **
53965 ** When this is called, the master journal should already have been
53966 ** created, populated with this journal pointer and synced to disk.
53967 **
53968 ** Once this is routine has returned, the only thing required to commit
53969 ** the write-transaction for this database file is to delete the journal.
53970 */
53971 SQLITE_PRIVATE int sqlite3BtreeCommitPhaseOne(Btree *p, const char *zMaster){
53972   int rc = SQLITE_OK;
53973   if( p->inTrans==TRANS_WRITE ){
53974     BtShared *pBt = p->pBt;
53975     sqlite3BtreeEnter(p);
53976 #ifndef SQLITE_OMIT_AUTOVACUUM
53977     if( pBt->autoVacuum ){
53978       rc = autoVacuumCommit(pBt);
53979       if( rc!=SQLITE_OK ){
53980         sqlite3BtreeLeave(p);
53981         return rc;
53982       }
53983     }
53984     if( pBt->bDoTruncate ){
53985       sqlite3PagerTruncateImage(pBt->pPager, pBt->nPage);
53986     }
53987 #endif
53988     rc = sqlite3PagerCommitPhaseOne(pBt->pPager, zMaster, 0);
53989     sqlite3BtreeLeave(p);
53990   }
53991   return rc;
53992 }
53993 
53994 /*
53995 ** This function is called from both BtreeCommitPhaseTwo() and BtreeRollback()
53996 ** at the conclusion of a transaction.
53997 */
53998 static void btreeEndTransaction(Btree *p){
53999   BtShared *pBt = p->pBt;
54000   sqlite3 *db = p->db;
54001   assert( sqlite3BtreeHoldsMutex(p) );
54002 
54003 #ifndef SQLITE_OMIT_AUTOVACUUM
54004   pBt->bDoTruncate = 0;
54005 #endif
54006   if( p->inTrans>TRANS_NONE && db->nVdbeRead>1 ){
54007     /* If there are other active statements that belong to this database
54008     ** handle, downgrade to a read-only transaction. The other statements
54009     ** may still be reading from the database.  */
54010     downgradeAllSharedCacheTableLocks(p);
54011     p->inTrans = TRANS_READ;
54012   }else{
54013     /* If the handle had any kind of transaction open, decrement the
54014     ** transaction count of the shared btree. If the transaction count
54015     ** reaches 0, set the shared state to TRANS_NONE. The unlockBtreeIfUnused()
54016     ** call below will unlock the pager.  */
54017     if( p->inTrans!=TRANS_NONE ){
54018       clearAllSharedCacheTableLocks(p);
54019       pBt->nTransaction--;
54020       if( 0==pBt->nTransaction ){
54021         pBt->inTransaction = TRANS_NONE;
54022       }
54023     }
54024 
54025     /* Set the current transaction state to TRANS_NONE and unlock the
54026     ** pager if this call closed the only read or write transaction.  */
54027     p->inTrans = TRANS_NONE;
54028     unlockBtreeIfUnused(pBt);
54029   }
54030 
54031   btreeIntegrity(p);
54032 }
54033 
54034 /*
54035 ** Commit the transaction currently in progress.
54036 **
54037 ** This routine implements the second phase of a 2-phase commit.  The
54038 ** sqlite3BtreeCommitPhaseOne() routine does the first phase and should
54039 ** be invoked prior to calling this routine.  The sqlite3BtreeCommitPhaseOne()
54040 ** routine did all the work of writing information out to disk and flushing the
54041 ** contents so that they are written onto the disk platter.  All this
54042 ** routine has to do is delete or truncate or zero the header in the
54043 ** the rollback journal (which causes the transaction to commit) and
54044 ** drop locks.
54045 **
54046 ** Normally, if an error occurs while the pager layer is attempting to
54047 ** finalize the underlying journal file, this function returns an error and
54048 ** the upper layer will attempt a rollback. However, if the second argument
54049 ** is non-zero then this b-tree transaction is part of a multi-file
54050 ** transaction. In this case, the transaction has already been committed
54051 ** (by deleting a master journal file) and the caller will ignore this
54052 ** functions return code. So, even if an error occurs in the pager layer,
54053 ** reset the b-tree objects internal state to indicate that the write
54054 ** transaction has been closed. This is quite safe, as the pager will have
54055 ** transitioned to the error state.
54056 **
54057 ** This will release the write lock on the database file.  If there
54058 ** are no active cursors, it also releases the read lock.
54059 */
54060 SQLITE_PRIVATE int sqlite3BtreeCommitPhaseTwo(Btree *p, int bCleanup){
54061 
54062   if( p->inTrans==TRANS_NONE ) return SQLITE_OK;
54063   sqlite3BtreeEnter(p);
54064   btreeIntegrity(p);
54065 
54066   /* If the handle has a write-transaction open, commit the shared-btrees
54067   ** transaction and set the shared state to TRANS_READ.
54068   */
54069   if( p->inTrans==TRANS_WRITE ){
54070     int rc;
54071     BtShared *pBt = p->pBt;
54072     assert( pBt->inTransaction==TRANS_WRITE );
54073     assert( pBt->nTransaction>0 );
54074     rc = sqlite3PagerCommitPhaseTwo(pBt->pPager);
54075     if( rc!=SQLITE_OK && bCleanup==0 ){
54076       sqlite3BtreeLeave(p);
54077       return rc;
54078     }
54079     pBt->inTransaction = TRANS_READ;
54080     btreeClearHasContent(pBt);
54081   }
54082 
54083   btreeEndTransaction(p);
54084   sqlite3BtreeLeave(p);
54085   return SQLITE_OK;
54086 }
54087 
54088 /*
54089 ** Do both phases of a commit.
54090 */
54091 SQLITE_PRIVATE int sqlite3BtreeCommit(Btree *p){
54092   int rc;
54093   sqlite3BtreeEnter(p);
54094   rc = sqlite3BtreeCommitPhaseOne(p, 0);
54095   if( rc==SQLITE_OK ){
54096     rc = sqlite3BtreeCommitPhaseTwo(p, 0);
54097   }
54098   sqlite3BtreeLeave(p);
54099   return rc;
54100 }
54101 
54102 /*
54103 ** This routine sets the state to CURSOR_FAULT and the error
54104 ** code to errCode for every cursor on BtShared that pBtree
54105 ** references.
54106 **
54107 ** Every cursor is tripped, including cursors that belong
54108 ** to other database connections that happen to be sharing
54109 ** the cache with pBtree.
54110 **
54111 ** This routine gets called when a rollback occurs.
54112 ** All cursors using the same cache must be tripped
54113 ** to prevent them from trying to use the btree after
54114 ** the rollback.  The rollback may have deleted tables
54115 ** or moved root pages, so it is not sufficient to
54116 ** save the state of the cursor.  The cursor must be
54117 ** invalidated.
54118 */
54119 SQLITE_PRIVATE void sqlite3BtreeTripAllCursors(Btree *pBtree, int errCode){
54120   BtCursor *p;
54121   if( pBtree==0 ) return;
54122   sqlite3BtreeEnter(pBtree);
54123   for(p=pBtree->pBt->pCursor; p; p=p->pNext){
54124     int i;
54125     sqlite3BtreeClearCursor(p);
54126     p->eState = CURSOR_FAULT;
54127     p->skipNext = errCode;
54128     for(i=0; i<=p->iPage; i++){
54129       releasePage(p->apPage[i]);
54130       p->apPage[i] = 0;
54131     }
54132   }
54133   sqlite3BtreeLeave(pBtree);
54134 }
54135 
54136 /*
54137 ** Rollback the transaction in progress.  All cursors will be
54138 ** invalided by this operation.  Any attempt to use a cursor
54139 ** that was open at the beginning of this operation will result
54140 ** in an error.
54141 **
54142 ** This will release the write lock on the database file.  If there
54143 ** are no active cursors, it also releases the read lock.
54144 */
54145 SQLITE_PRIVATE int sqlite3BtreeRollback(Btree *p, int tripCode){
54146   int rc;
54147   BtShared *pBt = p->pBt;
54148   MemPage *pPage1;
54149 
54150   sqlite3BtreeEnter(p);
54151   if( tripCode==SQLITE_OK ){
54152     rc = tripCode = saveAllCursors(pBt, 0, 0);
54153   }else{
54154     rc = SQLITE_OK;
54155   }
54156   if( tripCode ){
54157     sqlite3BtreeTripAllCursors(p, tripCode);
54158   }
54159   btreeIntegrity(p);
54160 
54161   if( p->inTrans==TRANS_WRITE ){
54162     int rc2;
54163 
54164     assert( TRANS_WRITE==pBt->inTransaction );
54165     rc2 = sqlite3PagerRollback(pBt->pPager);
54166     if( rc2!=SQLITE_OK ){
54167       rc = rc2;
54168     }
54169 
54170     /* The rollback may have destroyed the pPage1->aData value.  So
54171     ** call btreeGetPage() on page 1 again to make
54172     ** sure pPage1->aData is set correctly. */
54173     if( btreeGetPage(pBt, 1, &pPage1, 0)==SQLITE_OK ){
54174       int nPage = get4byte(28+(u8*)pPage1->aData);
54175       testcase( nPage==0 );
54176       if( nPage==0 ) sqlite3PagerPagecount(pBt->pPager, &nPage);
54177       testcase( pBt->nPage!=nPage );
54178       pBt->nPage = nPage;
54179       releasePage(pPage1);
54180     }
54181     assert( countValidCursors(pBt, 1)==0 );
54182     pBt->inTransaction = TRANS_READ;
54183     btreeClearHasContent(pBt);
54184   }
54185 
54186   btreeEndTransaction(p);
54187   sqlite3BtreeLeave(p);
54188   return rc;
54189 }
54190 
54191 /*
54192 ** Start a statement subtransaction. The subtransaction can can be rolled
54193 ** back independently of the main transaction. You must start a transaction
54194 ** before starting a subtransaction. The subtransaction is ended automatically
54195 ** if the main transaction commits or rolls back.
54196 **
54197 ** Statement subtransactions are used around individual SQL statements
54198 ** that are contained within a BEGIN...COMMIT block.  If a constraint
54199 ** error occurs within the statement, the effect of that one statement
54200 ** can be rolled back without having to rollback the entire transaction.
54201 **
54202 ** A statement sub-transaction is implemented as an anonymous savepoint. The
54203 ** value passed as the second parameter is the total number of savepoints,
54204 ** including the new anonymous savepoint, open on the B-Tree. i.e. if there
54205 ** are no active savepoints and no other statement-transactions open,
54206 ** iStatement is 1. This anonymous savepoint can be released or rolled back
54207 ** using the sqlite3BtreeSavepoint() function.
54208 */
54209 SQLITE_PRIVATE int sqlite3BtreeBeginStmt(Btree *p, int iStatement){
54210   int rc;
54211   BtShared *pBt = p->pBt;
54212   sqlite3BtreeEnter(p);
54213   assert( p->inTrans==TRANS_WRITE );
54214   assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
54215   assert( iStatement>0 );
54216   assert( iStatement>p->db->nSavepoint );
54217   assert( pBt->inTransaction==TRANS_WRITE );
54218   /* At the pager level, a statement transaction is a savepoint with
54219   ** an index greater than all savepoints created explicitly using
54220   ** SQL statements. It is illegal to open, release or rollback any
54221   ** such savepoints while the statement transaction savepoint is active.
54222   */
54223   rc = sqlite3PagerOpenSavepoint(pBt->pPager, iStatement);
54224   sqlite3BtreeLeave(p);
54225   return rc;
54226 }
54227 
54228 /*
54229 ** The second argument to this function, op, is always SAVEPOINT_ROLLBACK
54230 ** or SAVEPOINT_RELEASE. This function either releases or rolls back the
54231 ** savepoint identified by parameter iSavepoint, depending on the value
54232 ** of op.
54233 **
54234 ** Normally, iSavepoint is greater than or equal to zero. However, if op is
54235 ** SAVEPOINT_ROLLBACK, then iSavepoint may also be -1. In this case the
54236 ** contents of the entire transaction are rolled back. This is different
54237 ** from a normal transaction rollback, as no locks are released and the
54238 ** transaction remains open.
54239 */
54240 SQLITE_PRIVATE int sqlite3BtreeSavepoint(Btree *p, int op, int iSavepoint){
54241   int rc = SQLITE_OK;
54242   if( p && p->inTrans==TRANS_WRITE ){
54243     BtShared *pBt = p->pBt;
54244     assert( op==SAVEPOINT_RELEASE || op==SAVEPOINT_ROLLBACK );
54245     assert( iSavepoint>=0 || (iSavepoint==-1 && op==SAVEPOINT_ROLLBACK) );
54246     sqlite3BtreeEnter(p);
54247     rc = sqlite3PagerSavepoint(pBt->pPager, op, iSavepoint);
54248     if( rc==SQLITE_OK ){
54249       if( iSavepoint<0 && (pBt->btsFlags & BTS_INITIALLY_EMPTY)!=0 ){
54250         pBt->nPage = 0;
54251       }
54252       rc = newDatabase(pBt);
54253       pBt->nPage = get4byte(28 + pBt->pPage1->aData);
54254 
54255       /* The database size was written into the offset 28 of the header
54256       ** when the transaction started, so we know that the value at offset
54257       ** 28 is nonzero. */
54258       assert( pBt->nPage>0 );
54259     }
54260     sqlite3BtreeLeave(p);
54261   }
54262   return rc;
54263 }
54264 
54265 /*
54266 ** Create a new cursor for the BTree whose root is on the page
54267 ** iTable. If a read-only cursor is requested, it is assumed that
54268 ** the caller already has at least a read-only transaction open
54269 ** on the database already. If a write-cursor is requested, then
54270 ** the caller is assumed to have an open write transaction.
54271 **
54272 ** If wrFlag==0, then the cursor can only be used for reading.
54273 ** If wrFlag==1, then the cursor can be used for reading or for
54274 ** writing if other conditions for writing are also met.  These
54275 ** are the conditions that must be met in order for writing to
54276 ** be allowed:
54277 **
54278 ** 1:  The cursor must have been opened with wrFlag==1
54279 **
54280 ** 2:  Other database connections that share the same pager cache
54281 **     but which are not in the READ_UNCOMMITTED state may not have
54282 **     cursors open with wrFlag==0 on the same table.  Otherwise
54283 **     the changes made by this write cursor would be visible to
54284 **     the read cursors in the other database connection.
54285 **
54286 ** 3:  The database must be writable (not on read-only media)
54287 **
54288 ** 4:  There must be an active transaction.
54289 **
54290 ** No checking is done to make sure that page iTable really is the
54291 ** root page of a b-tree.  If it is not, then the cursor acquired
54292 ** will not work correctly.
54293 **
54294 ** It is assumed that the sqlite3BtreeCursorZero() has been called
54295 ** on pCur to initialize the memory space prior to invoking this routine.
54296 */
54297 static int btreeCursor(
54298   Btree *p,                              /* The btree */
54299   int iTable,                            /* Root page of table to open */
54300   int wrFlag,                            /* 1 to write. 0 read-only */
54301   struct KeyInfo *pKeyInfo,              /* First arg to comparison function */
54302   BtCursor *pCur                         /* Space for new cursor */
54303 ){
54304   BtShared *pBt = p->pBt;                /* Shared b-tree handle */
54305 
54306   assert( sqlite3BtreeHoldsMutex(p) );
54307   assert( wrFlag==0 || wrFlag==1 );
54308 
54309   /* The following assert statements verify that if this is a sharable
54310   ** b-tree database, the connection is holding the required table locks,
54311   ** and that no other connection has any open cursor that conflicts with
54312   ** this lock.  */
54313   assert( hasSharedCacheTableLock(p, iTable, pKeyInfo!=0, wrFlag+1) );
54314   assert( wrFlag==0 || !hasReadConflicts(p, iTable) );
54315 
54316   /* Assert that the caller has opened the required transaction. */
54317   assert( p->inTrans>TRANS_NONE );
54318   assert( wrFlag==0 || p->inTrans==TRANS_WRITE );
54319   assert( pBt->pPage1 && pBt->pPage1->aData );
54320 
54321   if( NEVER(wrFlag && (pBt->btsFlags & BTS_READ_ONLY)!=0) ){
54322     return SQLITE_READONLY;
54323   }
54324   if( iTable==1 && btreePagecount(pBt)==0 ){
54325     assert( wrFlag==0 );
54326     iTable = 0;
54327   }
54328 
54329   /* Now that no other errors can occur, finish filling in the BtCursor
54330   ** variables and link the cursor into the BtShared list.  */
54331   pCur->pgnoRoot = (Pgno)iTable;
54332   pCur->iPage = -1;
54333   pCur->pKeyInfo = pKeyInfo;
54334   pCur->pBtree = p;
54335   pCur->pBt = pBt;
54336   pCur->wrFlag = (u8)wrFlag;
54337   pCur->pNext = pBt->pCursor;
54338   if( pCur->pNext ){
54339     pCur->pNext->pPrev = pCur;
54340   }
54341   pBt->pCursor = pCur;
54342   pCur->eState = CURSOR_INVALID;
54343   pCur->cachedRowid = 0;
54344   return SQLITE_OK;
54345 }
54346 SQLITE_PRIVATE int sqlite3BtreeCursor(
54347   Btree *p,                                   /* The btree */
54348   int iTable,                                 /* Root page of table to open */
54349   int wrFlag,                                 /* 1 to write. 0 read-only */
54350   struct KeyInfo *pKeyInfo,                   /* First arg to xCompare() */
54351   BtCursor *pCur                              /* Write new cursor here */
54352 ){
54353   int rc;
54354   sqlite3BtreeEnter(p);
54355   rc = btreeCursor(p, iTable, wrFlag, pKeyInfo, pCur);
54356   sqlite3BtreeLeave(p);
54357   return rc;
54358 }
54359 
54360 /*
54361 ** Return the size of a BtCursor object in bytes.
54362 **
54363 ** This interfaces is needed so that users of cursors can preallocate
54364 ** sufficient storage to hold a cursor.  The BtCursor object is opaque
54365 ** to users so they cannot do the sizeof() themselves - they must call
54366 ** this routine.
54367 */
54368 SQLITE_PRIVATE int sqlite3BtreeCursorSize(void){
54369   return ROUND8(sizeof(BtCursor));
54370 }
54371 
54372 /*
54373 ** Initialize memory that will be converted into a BtCursor object.
54374 **
54375 ** The simple approach here would be to memset() the entire object
54376 ** to zero.  But it turns out that the apPage[] and aiIdx[] arrays
54377 ** do not need to be zeroed and they are large, so we can save a lot
54378 ** of run-time by skipping the initialization of those elements.
54379 */
54380 SQLITE_PRIVATE void sqlite3BtreeCursorZero(BtCursor *p){
54381   memset(p, 0, offsetof(BtCursor, iPage));
54382 }
54383 
54384 /*
54385 ** Set the cached rowid value of every cursor in the same database file
54386 ** as pCur and having the same root page number as pCur.  The value is
54387 ** set to iRowid.
54388 **
54389 ** Only positive rowid values are considered valid for this cache.
54390 ** The cache is initialized to zero, indicating an invalid cache.
54391 ** A btree will work fine with zero or negative rowids.  We just cannot
54392 ** cache zero or negative rowids, which means tables that use zero or
54393 ** negative rowids might run a little slower.  But in practice, zero
54394 ** or negative rowids are very uncommon so this should not be a problem.
54395 */
54396 SQLITE_PRIVATE void sqlite3BtreeSetCachedRowid(BtCursor *pCur, sqlite3_int64 iRowid){
54397   BtCursor *p;
54398   for(p=pCur->pBt->pCursor; p; p=p->pNext){
54399     if( p->pgnoRoot==pCur->pgnoRoot ) p->cachedRowid = iRowid;
54400   }
54401   assert( pCur->cachedRowid==iRowid );
54402 }
54403 
54404 /*
54405 ** Return the cached rowid for the given cursor.  A negative or zero
54406 ** return value indicates that the rowid cache is invalid and should be
54407 ** ignored.  If the rowid cache has never before been set, then a
54408 ** zero is returned.
54409 */
54410 SQLITE_PRIVATE sqlite3_int64 sqlite3BtreeGetCachedRowid(BtCursor *pCur){
54411   return pCur->cachedRowid;
54412 }
54413 
54414 /*
54415 ** Close a cursor.  The read lock on the database file is released
54416 ** when the last cursor is closed.
54417 */
54418 SQLITE_PRIVATE int sqlite3BtreeCloseCursor(BtCursor *pCur){
54419   Btree *pBtree = pCur->pBtree;
54420   if( pBtree ){
54421     int i;
54422     BtShared *pBt = pCur->pBt;
54423     sqlite3BtreeEnter(pBtree);
54424     sqlite3BtreeClearCursor(pCur);
54425     if( pCur->pPrev ){
54426       pCur->pPrev->pNext = pCur->pNext;
54427     }else{
54428       pBt->pCursor = pCur->pNext;
54429     }
54430     if( pCur->pNext ){
54431       pCur->pNext->pPrev = pCur->pPrev;
54432     }
54433     for(i=0; i<=pCur->iPage; i++){
54434       releasePage(pCur->apPage[i]);
54435     }
54436     unlockBtreeIfUnused(pBt);
54437     invalidateOverflowCache(pCur);
54438     /* sqlite3_free(pCur); */
54439     sqlite3BtreeLeave(pBtree);
54440   }
54441   return SQLITE_OK;
54442 }
54443 
54444 /*
54445 ** Make sure the BtCursor* given in the argument has a valid
54446 ** BtCursor.info structure.  If it is not already valid, call
54447 ** btreeParseCell() to fill it in.
54448 **
54449 ** BtCursor.info is a cache of the information in the current cell.
54450 ** Using this cache reduces the number of calls to btreeParseCell().
54451 **
54452 ** 2007-06-25:  There is a bug in some versions of MSVC that cause the
54453 ** compiler to crash when getCellInfo() is implemented as a macro.
54454 ** But there is a measureable speed advantage to using the macro on gcc
54455 ** (when less compiler optimizations like -Os or -O0 are used and the
54456 ** compiler is not doing agressive inlining.)  So we use a real function
54457 ** for MSVC and a macro for everything else.  Ticket #2457.
54458 */
54459 #ifndef NDEBUG
54460   static void assertCellInfo(BtCursor *pCur){
54461     CellInfo info;
54462     int iPage = pCur->iPage;
54463     memset(&info, 0, sizeof(info));
54464     btreeParseCell(pCur->apPage[iPage], pCur->aiIdx[iPage], &info);
54465     assert( CORRUPT_DB || memcmp(&info, &pCur->info, sizeof(info))==0 );
54466   }
54467 #else
54468   #define assertCellInfo(x)
54469 #endif
54470 #ifdef _MSC_VER
54471   /* Use a real function in MSVC to work around bugs in that compiler. */
54472   static void getCellInfo(BtCursor *pCur){
54473     if( pCur->info.nSize==0 ){
54474       int iPage = pCur->iPage;
54475       btreeParseCell(pCur->apPage[iPage],pCur->aiIdx[iPage],&pCur->info);
54476       pCur->validNKey = 1;
54477     }else{
54478       assertCellInfo(pCur);
54479     }
54480   }
54481 #else /* if not _MSC_VER */
54482   /* Use a macro in all other compilers so that the function is inlined */
54483 #define getCellInfo(pCur)                                                      \
54484   if( pCur->info.nSize==0 ){                                                   \
54485     int iPage = pCur->iPage;                                                   \
54486     btreeParseCell(pCur->apPage[iPage],pCur->aiIdx[iPage],&pCur->info); \
54487     pCur->validNKey = 1;                                                       \
54488   }else{                                                                       \
54489     assertCellInfo(pCur);                                                      \
54490   }
54491 #endif /* _MSC_VER */
54492 
54493 #ifndef NDEBUG  /* The next routine used only within assert() statements */
54494 /*
54495 ** Return true if the given BtCursor is valid.  A valid cursor is one
54496 ** that is currently pointing to a row in a (non-empty) table.
54497 ** This is a verification routine is used only within assert() statements.
54498 */
54499 SQLITE_PRIVATE int sqlite3BtreeCursorIsValid(BtCursor *pCur){
54500   return pCur && pCur->eState==CURSOR_VALID;
54501 }
54502 #endif /* NDEBUG */
54503 
54504 /*
54505 ** Set *pSize to the size of the buffer needed to hold the value of
54506 ** the key for the current entry.  If the cursor is not pointing
54507 ** to a valid entry, *pSize is set to 0.
54508 **
54509 ** For a table with the INTKEY flag set, this routine returns the key
54510 ** itself, not the number of bytes in the key.
54511 **
54512 ** The caller must position the cursor prior to invoking this routine.
54513 **
54514 ** This routine cannot fail.  It always returns SQLITE_OK.
54515 */
54516 SQLITE_PRIVATE int sqlite3BtreeKeySize(BtCursor *pCur, i64 *pSize){
54517   assert( cursorHoldsMutex(pCur) );
54518   assert( pCur->eState==CURSOR_INVALID || pCur->eState==CURSOR_VALID );
54519   if( pCur->eState!=CURSOR_VALID ){
54520     *pSize = 0;
54521   }else{
54522     getCellInfo(pCur);
54523     *pSize = pCur->info.nKey;
54524   }
54525   return SQLITE_OK;
54526 }
54527 
54528 /*
54529 ** Set *pSize to the number of bytes of data in the entry the
54530 ** cursor currently points to.
54531 **
54532 ** The caller must guarantee that the cursor is pointing to a non-NULL
54533 ** valid entry.  In other words, the calling procedure must guarantee
54534 ** that the cursor has Cursor.eState==CURSOR_VALID.
54535 **
54536 ** Failure is not possible.  This function always returns SQLITE_OK.
54537 ** It might just as well be a procedure (returning void) but we continue
54538 ** to return an integer result code for historical reasons.
54539 */
54540 SQLITE_PRIVATE int sqlite3BtreeDataSize(BtCursor *pCur, u32 *pSize){
54541   assert( cursorHoldsMutex(pCur) );
54542   assert( pCur->eState==CURSOR_VALID );
54543   getCellInfo(pCur);
54544   *pSize = pCur->info.nData;
54545   return SQLITE_OK;
54546 }
54547 
54548 /*
54549 ** Given the page number of an overflow page in the database (parameter
54550 ** ovfl), this function finds the page number of the next page in the
54551 ** linked list of overflow pages. If possible, it uses the auto-vacuum
54552 ** pointer-map data instead of reading the content of page ovfl to do so.
54553 **
54554 ** If an error occurs an SQLite error code is returned. Otherwise:
54555 **
54556 ** The page number of the next overflow page in the linked list is
54557 ** written to *pPgnoNext. If page ovfl is the last page in its linked
54558 ** list, *pPgnoNext is set to zero.
54559 **
54560 ** If ppPage is not NULL, and a reference to the MemPage object corresponding
54561 ** to page number pOvfl was obtained, then *ppPage is set to point to that
54562 ** reference. It is the responsibility of the caller to call releasePage()
54563 ** on *ppPage to free the reference. In no reference was obtained (because
54564 ** the pointer-map was used to obtain the value for *pPgnoNext), then
54565 ** *ppPage is set to zero.
54566 */
54567 static int getOverflowPage(
54568   BtShared *pBt,               /* The database file */
54569   Pgno ovfl,                   /* Current overflow page number */
54570   MemPage **ppPage,            /* OUT: MemPage handle (may be NULL) */
54571   Pgno *pPgnoNext              /* OUT: Next overflow page number */
54572 ){
54573   Pgno next = 0;
54574   MemPage *pPage = 0;
54575   int rc = SQLITE_OK;
54576 
54577   assert( sqlite3_mutex_held(pBt->mutex) );
54578   assert(pPgnoNext);
54579 
54580 #ifndef SQLITE_OMIT_AUTOVACUUM
54581   /* Try to find the next page in the overflow list using the
54582   ** autovacuum pointer-map pages. Guess that the next page in
54583   ** the overflow list is page number (ovfl+1). If that guess turns
54584   ** out to be wrong, fall back to loading the data of page
54585   ** number ovfl to determine the next page number.
54586   */
54587   if( pBt->autoVacuum ){
54588     Pgno pgno;
54589     Pgno iGuess = ovfl+1;
54590     u8 eType;
54591 
54592     while( PTRMAP_ISPAGE(pBt, iGuess) || iGuess==PENDING_BYTE_PAGE(pBt) ){
54593       iGuess++;
54594     }
54595 
54596     if( iGuess<=btreePagecount(pBt) ){
54597       rc = ptrmapGet(pBt, iGuess, &eType, &pgno);
54598       if( rc==SQLITE_OK && eType==PTRMAP_OVERFLOW2 && pgno==ovfl ){
54599         next = iGuess;
54600         rc = SQLITE_DONE;
54601       }
54602     }
54603   }
54604 #endif
54605 
54606   assert( next==0 || rc==SQLITE_DONE );
54607   if( rc==SQLITE_OK ){
54608     rc = btreeGetPage(pBt, ovfl, &pPage, (ppPage==0) ? PAGER_GET_READONLY : 0);
54609     assert( rc==SQLITE_OK || pPage==0 );
54610     if( rc==SQLITE_OK ){
54611       next = get4byte(pPage->aData);
54612     }
54613   }
54614 
54615   *pPgnoNext = next;
54616   if( ppPage ){
54617     *ppPage = pPage;
54618   }else{
54619     releasePage(pPage);
54620   }
54621   return (rc==SQLITE_DONE ? SQLITE_OK : rc);
54622 }
54623 
54624 /*
54625 ** Copy data from a buffer to a page, or from a page to a buffer.
54626 **
54627 ** pPayload is a pointer to data stored on database page pDbPage.
54628 ** If argument eOp is false, then nByte bytes of data are copied
54629 ** from pPayload to the buffer pointed at by pBuf. If eOp is true,
54630 ** then sqlite3PagerWrite() is called on pDbPage and nByte bytes
54631 ** of data are copied from the buffer pBuf to pPayload.
54632 **
54633 ** SQLITE_OK is returned on success, otherwise an error code.
54634 */
54635 static int copyPayload(
54636   void *pPayload,           /* Pointer to page data */
54637   void *pBuf,               /* Pointer to buffer */
54638   int nByte,                /* Number of bytes to copy */
54639   int eOp,                  /* 0 -> copy from page, 1 -> copy to page */
54640   DbPage *pDbPage           /* Page containing pPayload */
54641 ){
54642   if( eOp ){
54643     /* Copy data from buffer to page (a write operation) */
54644     int rc = sqlite3PagerWrite(pDbPage);
54645     if( rc!=SQLITE_OK ){
54646       return rc;
54647     }
54648     memcpy(pPayload, pBuf, nByte);
54649   }else{
54650     /* Copy data from page to buffer (a read operation) */
54651     memcpy(pBuf, pPayload, nByte);
54652   }
54653   return SQLITE_OK;
54654 }
54655 
54656 /*
54657 ** This function is used to read or overwrite payload information
54658 ** for the entry that the pCur cursor is pointing to. If the eOp
54659 ** parameter is 0, this is a read operation (data copied into
54660 ** buffer pBuf). If it is non-zero, a write (data copied from
54661 ** buffer pBuf).
54662 **
54663 ** A total of "amt" bytes are read or written beginning at "offset".
54664 ** Data is read to or from the buffer pBuf.
54665 **
54666 ** The content being read or written might appear on the main page
54667 ** or be scattered out on multiple overflow pages.
54668 **
54669 ** If the BtCursor.isIncrblobHandle flag is set, and the current
54670 ** cursor entry uses one or more overflow pages, this function
54671 ** allocates space for and lazily popluates the overflow page-list
54672 ** cache array (BtCursor.aOverflow). Subsequent calls use this
54673 ** cache to make seeking to the supplied offset more efficient.
54674 **
54675 ** Once an overflow page-list cache has been allocated, it may be
54676 ** invalidated if some other cursor writes to the same table, or if
54677 ** the cursor is moved to a different row. Additionally, in auto-vacuum
54678 ** mode, the following events may invalidate an overflow page-list cache.
54679 **
54680 **   * An incremental vacuum,
54681 **   * A commit in auto_vacuum="full" mode,
54682 **   * Creating a table (may require moving an overflow page).
54683 */
54684 static int accessPayload(
54685   BtCursor *pCur,      /* Cursor pointing to entry to read from */
54686   u32 offset,          /* Begin reading this far into payload */
54687   u32 amt,             /* Read this many bytes */
54688   unsigned char *pBuf, /* Write the bytes into this buffer */
54689   int eOp              /* zero to read. non-zero to write. */
54690 ){
54691   unsigned char *aPayload;
54692   int rc = SQLITE_OK;
54693   u32 nKey;
54694   int iIdx = 0;
54695   MemPage *pPage = pCur->apPage[pCur->iPage]; /* Btree page of current entry */
54696   BtShared *pBt = pCur->pBt;                  /* Btree this cursor belongs to */
54697 
54698   assert( pPage );
54699   assert( pCur->eState==CURSOR_VALID );
54700   assert( pCur->aiIdx[pCur->iPage]<pPage->nCell );
54701   assert( cursorHoldsMutex(pCur) );
54702 
54703   getCellInfo(pCur);
54704   aPayload = pCur->info.pCell + pCur->info.nHeader;
54705   nKey = (pPage->intKey ? 0 : (int)pCur->info.nKey);
54706 
54707   if( NEVER(offset+amt > nKey+pCur->info.nData)
54708    || &aPayload[pCur->info.nLocal] > &pPage->aData[pBt->usableSize]
54709   ){
54710     /* Trying to read or write past the end of the data is an error */
54711     return SQLITE_CORRUPT_BKPT;
54712   }
54713 
54714   /* Check if data must be read/written to/from the btree page itself. */
54715   if( offset<pCur->info.nLocal ){
54716     int a = amt;
54717     if( a+offset>pCur->info.nLocal ){
54718       a = pCur->info.nLocal - offset;
54719     }
54720     rc = copyPayload(&aPayload[offset], pBuf, a, eOp, pPage->pDbPage);
54721     offset = 0;
54722     pBuf += a;
54723     amt -= a;
54724   }else{
54725     offset -= pCur->info.nLocal;
54726   }
54727 
54728   if( rc==SQLITE_OK && amt>0 ){
54729     const u32 ovflSize = pBt->usableSize - 4;  /* Bytes content per ovfl page */
54730     Pgno nextPage;
54731 
54732     nextPage = get4byte(&aPayload[pCur->info.nLocal]);
54733 
54734 #ifndef SQLITE_OMIT_INCRBLOB
54735     /* If the isIncrblobHandle flag is set and the BtCursor.aOverflow[]
54736     ** has not been allocated, allocate it now. The array is sized at
54737     ** one entry for each overflow page in the overflow chain. The
54738     ** page number of the first overflow page is stored in aOverflow[0],
54739     ** etc. A value of 0 in the aOverflow[] array means "not yet known"
54740     ** (the cache is lazily populated).
54741     */
54742     if( pCur->isIncrblobHandle && !pCur->aOverflow ){
54743       int nOvfl = (pCur->info.nPayload-pCur->info.nLocal+ovflSize-1)/ovflSize;
54744       pCur->aOverflow = (Pgno *)sqlite3MallocZero(sizeof(Pgno)*nOvfl);
54745       /* nOvfl is always positive.  If it were zero, fetchPayload would have
54746       ** been used instead of this routine. */
54747       if( ALWAYS(nOvfl) && !pCur->aOverflow ){
54748         rc = SQLITE_NOMEM;
54749       }
54750     }
54751 
54752     /* If the overflow page-list cache has been allocated and the
54753     ** entry for the first required overflow page is valid, skip
54754     ** directly to it.
54755     */
54756     if( pCur->aOverflow && pCur->aOverflow[offset/ovflSize] ){
54757       iIdx = (offset/ovflSize);
54758       nextPage = pCur->aOverflow[iIdx];
54759       offset = (offset%ovflSize);
54760     }
54761 #endif
54762 
54763     for( ; rc==SQLITE_OK && amt>0 && nextPage; iIdx++){
54764 
54765 #ifndef SQLITE_OMIT_INCRBLOB
54766       /* If required, populate the overflow page-list cache. */
54767       if( pCur->aOverflow ){
54768         assert(!pCur->aOverflow[iIdx] || pCur->aOverflow[iIdx]==nextPage);
54769         pCur->aOverflow[iIdx] = nextPage;
54770       }
54771 #endif
54772 
54773       if( offset>=ovflSize ){
54774         /* The only reason to read this page is to obtain the page
54775         ** number for the next page in the overflow chain. The page
54776         ** data is not required. So first try to lookup the overflow
54777         ** page-list cache, if any, then fall back to the getOverflowPage()
54778         ** function.
54779         */
54780 #ifndef SQLITE_OMIT_INCRBLOB
54781         if( pCur->aOverflow && pCur->aOverflow[iIdx+1] ){
54782           nextPage = pCur->aOverflow[iIdx+1];
54783         } else
54784 #endif
54785           rc = getOverflowPage(pBt, nextPage, 0, &nextPage);
54786         offset -= ovflSize;
54787       }else{
54788         /* Need to read this page properly. It contains some of the
54789         ** range of data that is being read (eOp==0) or written (eOp!=0).
54790         */
54791 #ifdef SQLITE_DIRECT_OVERFLOW_READ
54792         sqlite3_file *fd;
54793 #endif
54794         int a = amt;
54795         if( a + offset > ovflSize ){
54796           a = ovflSize - offset;
54797         }
54798 
54799 #ifdef SQLITE_DIRECT_OVERFLOW_READ
54800         /* If all the following are true:
54801         **
54802         **   1) this is a read operation, and
54803         **   2) data is required from the start of this overflow page, and
54804         **   3) the database is file-backed, and
54805         **   4) there is no open write-transaction, and
54806         **   5) the database is not a WAL database,
54807         **
54808         ** then data can be read directly from the database file into the
54809         ** output buffer, bypassing the page-cache altogether. This speeds
54810         ** up loading large records that span many overflow pages.
54811         */
54812         if( eOp==0                                             /* (1) */
54813          && offset==0                                          /* (2) */
54814          && pBt->inTransaction==TRANS_READ                     /* (4) */
54815          && (fd = sqlite3PagerFile(pBt->pPager))->pMethods     /* (3) */
54816          && pBt->pPage1->aData[19]==0x01                       /* (5) */
54817         ){
54818           u8 aSave[4];
54819           u8 *aWrite = &pBuf[-4];
54820           memcpy(aSave, aWrite, 4);
54821           rc = sqlite3OsRead(fd, aWrite, a+4, (i64)pBt->pageSize*(nextPage-1));
54822           nextPage = get4byte(aWrite);
54823           memcpy(aWrite, aSave, 4);
54824         }else
54825 #endif
54826 
54827         {
54828           DbPage *pDbPage;
54829           rc = sqlite3PagerAcquire(pBt->pPager, nextPage, &pDbPage,
54830               (eOp==0 ? PAGER_GET_READONLY : 0)
54831           );
54832           if( rc==SQLITE_OK ){
54833             aPayload = sqlite3PagerGetData(pDbPage);
54834             nextPage = get4byte(aPayload);
54835             rc = copyPayload(&aPayload[offset+4], pBuf, a, eOp, pDbPage);
54836             sqlite3PagerUnref(pDbPage);
54837             offset = 0;
54838           }
54839         }
54840         amt -= a;
54841         pBuf += a;
54842       }
54843     }
54844   }
54845 
54846   if( rc==SQLITE_OK && amt>0 ){
54847     return SQLITE_CORRUPT_BKPT;
54848   }
54849   return rc;
54850 }
54851 
54852 /*
54853 ** Read part of the key associated with cursor pCur.  Exactly
54854 ** "amt" bytes will be transfered into pBuf[].  The transfer
54855 ** begins at "offset".
54856 **
54857 ** The caller must ensure that pCur is pointing to a valid row
54858 ** in the table.
54859 **
54860 ** Return SQLITE_OK on success or an error code if anything goes
54861 ** wrong.  An error is returned if "offset+amt" is larger than
54862 ** the available payload.
54863 */
54864 SQLITE_PRIVATE int sqlite3BtreeKey(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
54865   assert( cursorHoldsMutex(pCur) );
54866   assert( pCur->eState==CURSOR_VALID );
54867   assert( pCur->iPage>=0 && pCur->apPage[pCur->iPage] );
54868   assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell );
54869   return accessPayload(pCur, offset, amt, (unsigned char*)pBuf, 0);
54870 }
54871 
54872 /*
54873 ** Read part of the data associated with cursor pCur.  Exactly
54874 ** "amt" bytes will be transfered into pBuf[].  The transfer
54875 ** begins at "offset".
54876 **
54877 ** Return SQLITE_OK on success or an error code if anything goes
54878 ** wrong.  An error is returned if "offset+amt" is larger than
54879 ** the available payload.
54880 */
54881 SQLITE_PRIVATE int sqlite3BtreeData(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){
54882   int rc;
54883 
54884 #ifndef SQLITE_OMIT_INCRBLOB
54885   if ( pCur->eState==CURSOR_INVALID ){
54886     return SQLITE_ABORT;
54887   }
54888 #endif
54889 
54890   assert( cursorHoldsMutex(pCur) );
54891   rc = restoreCursorPosition(pCur);
54892   if( rc==SQLITE_OK ){
54893     assert( pCur->eState==CURSOR_VALID );
54894     assert( pCur->iPage>=0 && pCur->apPage[pCur->iPage] );
54895     assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell );
54896     rc = accessPayload(pCur, offset, amt, pBuf, 0);
54897   }
54898   return rc;
54899 }
54900 
54901 /*
54902 ** Return a pointer to payload information from the entry that the
54903 ** pCur cursor is pointing to.  The pointer is to the beginning of
54904 ** the key if index btrees (pPage->intKey==0) and is the data for
54905 ** table btrees (pPage->intKey==1). The number of bytes of available
54906 ** key/data is written into *pAmt.  If *pAmt==0, then the value
54907 ** returned will not be a valid pointer.
54908 **
54909 ** This routine is an optimization.  It is common for the entire key
54910 ** and data to fit on the local page and for there to be no overflow
54911 ** pages.  When that is so, this routine can be used to access the
54912 ** key and data without making a copy.  If the key and/or data spills
54913 ** onto overflow pages, then accessPayload() must be used to reassemble
54914 ** the key/data and copy it into a preallocated buffer.
54915 **
54916 ** The pointer returned by this routine looks directly into the cached
54917 ** page of the database.  The data might change or move the next time
54918 ** any btree routine is called.
54919 */
54920 static const void *fetchPayload(
54921   BtCursor *pCur,      /* Cursor pointing to entry to read from */
54922   u32 *pAmt            /* Write the number of available bytes here */
54923 ){
54924   assert( pCur!=0 && pCur->iPage>=0 && pCur->apPage[pCur->iPage]);
54925   assert( pCur->eState==CURSOR_VALID );
54926   assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
54927   assert( cursorHoldsMutex(pCur) );
54928   assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell );
54929   if( pCur->info.nSize==0 ){
54930     btreeParseCell(pCur->apPage[pCur->iPage], pCur->aiIdx[pCur->iPage],
54931                    &pCur->info);
54932   }
54933   *pAmt = pCur->info.nLocal;
54934   return (void*)(pCur->info.pCell + pCur->info.nHeader);
54935 }
54936 
54937 
54938 /*
54939 ** For the entry that cursor pCur is point to, return as
54940 ** many bytes of the key or data as are available on the local
54941 ** b-tree page.  Write the number of available bytes into *pAmt.
54942 **
54943 ** The pointer returned is ephemeral.  The key/data may move
54944 ** or be destroyed on the next call to any Btree routine,
54945 ** including calls from other threads against the same cache.
54946 ** Hence, a mutex on the BtShared should be held prior to calling
54947 ** this routine.
54948 **
54949 ** These routines is used to get quick access to key and data
54950 ** in the common case where no overflow pages are used.
54951 */
54952 SQLITE_PRIVATE const void *sqlite3BtreeKeyFetch(BtCursor *pCur, u32 *pAmt){
54953   return fetchPayload(pCur, pAmt);
54954 }
54955 SQLITE_PRIVATE const void *sqlite3BtreeDataFetch(BtCursor *pCur, u32 *pAmt){
54956   return fetchPayload(pCur, pAmt);
54957 }
54958 
54959 
54960 /*
54961 ** Move the cursor down to a new child page.  The newPgno argument is the
54962 ** page number of the child page to move to.
54963 **
54964 ** This function returns SQLITE_CORRUPT if the page-header flags field of
54965 ** the new child page does not match the flags field of the parent (i.e.
54966 ** if an intkey page appears to be the parent of a non-intkey page, or
54967 ** vice-versa).
54968 */
54969 static int moveToChild(BtCursor *pCur, u32 newPgno){
54970   int rc;
54971   int i = pCur->iPage;
54972   MemPage *pNewPage;
54973   BtShared *pBt = pCur->pBt;
54974 
54975   assert( cursorHoldsMutex(pCur) );
54976   assert( pCur->eState==CURSOR_VALID );
54977   assert( pCur->iPage<BTCURSOR_MAX_DEPTH );
54978   assert( pCur->iPage>=0 );
54979   if( pCur->iPage>=(BTCURSOR_MAX_DEPTH-1) ){
54980     return SQLITE_CORRUPT_BKPT;
54981   }
54982   rc = getAndInitPage(pBt, newPgno, &pNewPage,
54983                pCur->wrFlag==0 ? PAGER_GET_READONLY : 0);
54984   if( rc ) return rc;
54985   pCur->apPage[i+1] = pNewPage;
54986   pCur->aiIdx[i+1] = 0;
54987   pCur->iPage++;
54988 
54989   pCur->info.nSize = 0;
54990   pCur->validNKey = 0;
54991   if( pNewPage->nCell<1 || pNewPage->intKey!=pCur->apPage[i]->intKey ){
54992     return SQLITE_CORRUPT_BKPT;
54993   }
54994   return SQLITE_OK;
54995 }
54996 
54997 #if 0
54998 /*
54999 ** Page pParent is an internal (non-leaf) tree page. This function
55000 ** asserts that page number iChild is the left-child if the iIdx'th
55001 ** cell in page pParent. Or, if iIdx is equal to the total number of
55002 ** cells in pParent, that page number iChild is the right-child of
55003 ** the page.
55004 */
55005 static void assertParentIndex(MemPage *pParent, int iIdx, Pgno iChild){
55006   assert( iIdx<=pParent->nCell );
55007   if( iIdx==pParent->nCell ){
55008     assert( get4byte(&pParent->aData[pParent->hdrOffset+8])==iChild );
55009   }else{
55010     assert( get4byte(findCell(pParent, iIdx))==iChild );
55011   }
55012 }
55013 #else
55014 #  define assertParentIndex(x,y,z)
55015 #endif
55016 
55017 /*
55018 ** Move the cursor up to the parent page.
55019 **
55020 ** pCur->idx is set to the cell index that contains the pointer
55021 ** to the page we are coming from.  If we are coming from the
55022 ** right-most child page then pCur->idx is set to one more than
55023 ** the largest cell index.
55024 */
55025 static void moveToParent(BtCursor *pCur){
55026   assert( cursorHoldsMutex(pCur) );
55027   assert( pCur->eState==CURSOR_VALID );
55028   assert( pCur->iPage>0 );
55029   assert( pCur->apPage[pCur->iPage] );
55030 
55031   /* UPDATE: It is actually possible for the condition tested by the assert
55032   ** below to be untrue if the database file is corrupt. This can occur if
55033   ** one cursor has modified page pParent while a reference to it is held
55034   ** by a second cursor. Which can only happen if a single page is linked
55035   ** into more than one b-tree structure in a corrupt database.  */
55036 #if 0
55037   assertParentIndex(
55038     pCur->apPage[pCur->iPage-1],
55039     pCur->aiIdx[pCur->iPage-1],
55040     pCur->apPage[pCur->iPage]->pgno
55041   );
55042 #endif
55043   testcase( pCur->aiIdx[pCur->iPage-1] > pCur->apPage[pCur->iPage-1]->nCell );
55044 
55045   releasePage(pCur->apPage[pCur->iPage]);
55046   pCur->iPage--;
55047   pCur->info.nSize = 0;
55048   pCur->validNKey = 0;
55049 }
55050 
55051 /*
55052 ** Move the cursor to point to the root page of its b-tree structure.
55053 **
55054 ** If the table has a virtual root page, then the cursor is moved to point
55055 ** to the virtual root page instead of the actual root page. A table has a
55056 ** virtual root page when the actual root page contains no cells and a
55057 ** single child page. This can only happen with the table rooted at page 1.
55058 **
55059 ** If the b-tree structure is empty, the cursor state is set to
55060 ** CURSOR_INVALID. Otherwise, the cursor is set to point to the first
55061 ** cell located on the root (or virtual root) page and the cursor state
55062 ** is set to CURSOR_VALID.
55063 **
55064 ** If this function returns successfully, it may be assumed that the
55065 ** page-header flags indicate that the [virtual] root-page is the expected
55066 ** kind of b-tree page (i.e. if when opening the cursor the caller did not
55067 ** specify a KeyInfo structure the flags byte is set to 0x05 or 0x0D,
55068 ** indicating a table b-tree, or if the caller did specify a KeyInfo
55069 ** structure the flags byte is set to 0x02 or 0x0A, indicating an index
55070 ** b-tree).
55071 */
55072 static int moveToRoot(BtCursor *pCur){
55073   MemPage *pRoot;
55074   int rc = SQLITE_OK;
55075 
55076   assert( cursorHoldsMutex(pCur) );
55077   assert( CURSOR_INVALID < CURSOR_REQUIRESEEK );
55078   assert( CURSOR_VALID   < CURSOR_REQUIRESEEK );
55079   assert( CURSOR_FAULT   > CURSOR_REQUIRESEEK );
55080   if( pCur->eState>=CURSOR_REQUIRESEEK ){
55081     if( pCur->eState==CURSOR_FAULT ){
55082       assert( pCur->skipNext!=SQLITE_OK );
55083       return pCur->skipNext;
55084     }
55085     sqlite3BtreeClearCursor(pCur);
55086   }
55087 
55088   if( pCur->iPage>=0 ){
55089     while( pCur->iPage ) releasePage(pCur->apPage[pCur->iPage--]);
55090   }else if( pCur->pgnoRoot==0 ){
55091     pCur->eState = CURSOR_INVALID;
55092     return SQLITE_OK;
55093   }else{
55094     rc = getAndInitPage(pCur->pBtree->pBt, pCur->pgnoRoot, &pCur->apPage[0],
55095                         pCur->wrFlag==0 ? PAGER_GET_READONLY : 0);
55096     if( rc!=SQLITE_OK ){
55097       pCur->eState = CURSOR_INVALID;
55098       return rc;
55099     }
55100     pCur->iPage = 0;
55101   }
55102   pRoot = pCur->apPage[0];
55103   assert( pRoot->pgno==pCur->pgnoRoot );
55104 
55105   /* If pCur->pKeyInfo is not NULL, then the caller that opened this cursor
55106   ** expected to open it on an index b-tree. Otherwise, if pKeyInfo is
55107   ** NULL, the caller expects a table b-tree. If this is not the case,
55108   ** return an SQLITE_CORRUPT error.
55109   **
55110   ** Earlier versions of SQLite assumed that this test could not fail
55111   ** if the root page was already loaded when this function was called (i.e.
55112   ** if pCur->iPage>=0). But this is not so if the database is corrupted
55113   ** in such a way that page pRoot is linked into a second b-tree table
55114   ** (or the freelist).  */
55115   assert( pRoot->intKey==1 || pRoot->intKey==0 );
55116   if( pRoot->isInit==0 || (pCur->pKeyInfo==0)!=pRoot->intKey ){
55117     return SQLITE_CORRUPT_BKPT;
55118   }
55119 
55120   pCur->aiIdx[0] = 0;
55121   pCur->info.nSize = 0;
55122   pCur->atLast = 0;
55123   pCur->validNKey = 0;
55124 
55125   if( pRoot->nCell>0 ){
55126     pCur->eState = CURSOR_VALID;
55127   }else if( !pRoot->leaf ){
55128     Pgno subpage;
55129     if( pRoot->pgno!=1 ) return SQLITE_CORRUPT_BKPT;
55130     subpage = get4byte(&pRoot->aData[pRoot->hdrOffset+8]);
55131     pCur->eState = CURSOR_VALID;
55132     rc = moveToChild(pCur, subpage);
55133   }else{
55134     pCur->eState = CURSOR_INVALID;
55135   }
55136   return rc;
55137 }
55138 
55139 /*
55140 ** Move the cursor down to the left-most leaf entry beneath the
55141 ** entry to which it is currently pointing.
55142 **
55143 ** The left-most leaf is the one with the smallest key - the first
55144 ** in ascending order.
55145 */
55146 static int moveToLeftmost(BtCursor *pCur){
55147   Pgno pgno;
55148   int rc = SQLITE_OK;
55149   MemPage *pPage;
55150 
55151   assert( cursorHoldsMutex(pCur) );
55152   assert( pCur->eState==CURSOR_VALID );
55153   while( rc==SQLITE_OK && !(pPage = pCur->apPage[pCur->iPage])->leaf ){
55154     assert( pCur->aiIdx[pCur->iPage]<pPage->nCell );
55155     pgno = get4byte(findCell(pPage, pCur->aiIdx[pCur->iPage]));
55156     rc = moveToChild(pCur, pgno);
55157   }
55158   return rc;
55159 }
55160 
55161 /*
55162 ** Move the cursor down to the right-most leaf entry beneath the
55163 ** page to which it is currently pointing.  Notice the difference
55164 ** between moveToLeftmost() and moveToRightmost().  moveToLeftmost()
55165 ** finds the left-most entry beneath the *entry* whereas moveToRightmost()
55166 ** finds the right-most entry beneath the *page*.
55167 **
55168 ** The right-most entry is the one with the largest key - the last
55169 ** key in ascending order.
55170 */
55171 static int moveToRightmost(BtCursor *pCur){
55172   Pgno pgno;
55173   int rc = SQLITE_OK;
55174   MemPage *pPage = 0;
55175 
55176   assert( cursorHoldsMutex(pCur) );
55177   assert( pCur->eState==CURSOR_VALID );
55178   while( rc==SQLITE_OK && !(pPage = pCur->apPage[pCur->iPage])->leaf ){
55179     pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
55180     pCur->aiIdx[pCur->iPage] = pPage->nCell;
55181     rc = moveToChild(pCur, pgno);
55182   }
55183   if( rc==SQLITE_OK ){
55184     pCur->aiIdx[pCur->iPage] = pPage->nCell-1;
55185     pCur->info.nSize = 0;
55186     pCur->validNKey = 0;
55187   }
55188   return rc;
55189 }
55190 
55191 /* Move the cursor to the first entry in the table.  Return SQLITE_OK
55192 ** on success.  Set *pRes to 0 if the cursor actually points to something
55193 ** or set *pRes to 1 if the table is empty.
55194 */
55195 SQLITE_PRIVATE int sqlite3BtreeFirst(BtCursor *pCur, int *pRes){
55196   int rc;
55197 
55198   assert( cursorHoldsMutex(pCur) );
55199   assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
55200   rc = moveToRoot(pCur);
55201   if( rc==SQLITE_OK ){
55202     if( pCur->eState==CURSOR_INVALID ){
55203       assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->nCell==0 );
55204       *pRes = 1;
55205     }else{
55206       assert( pCur->apPage[pCur->iPage]->nCell>0 );
55207       *pRes = 0;
55208       rc = moveToLeftmost(pCur);
55209     }
55210   }
55211   return rc;
55212 }
55213 
55214 /* Move the cursor to the last entry in the table.  Return SQLITE_OK
55215 ** on success.  Set *pRes to 0 if the cursor actually points to something
55216 ** or set *pRes to 1 if the table is empty.
55217 */
55218 SQLITE_PRIVATE int sqlite3BtreeLast(BtCursor *pCur, int *pRes){
55219   int rc;
55220 
55221   assert( cursorHoldsMutex(pCur) );
55222   assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
55223 
55224   /* If the cursor already points to the last entry, this is a no-op. */
55225   if( CURSOR_VALID==pCur->eState && pCur->atLast ){
55226 #ifdef SQLITE_DEBUG
55227     /* This block serves to assert() that the cursor really does point
55228     ** to the last entry in the b-tree. */
55229     int ii;
55230     for(ii=0; ii<pCur->iPage; ii++){
55231       assert( pCur->aiIdx[ii]==pCur->apPage[ii]->nCell );
55232     }
55233     assert( pCur->aiIdx[pCur->iPage]==pCur->apPage[pCur->iPage]->nCell-1 );
55234     assert( pCur->apPage[pCur->iPage]->leaf );
55235 #endif
55236     return SQLITE_OK;
55237   }
55238 
55239   rc = moveToRoot(pCur);
55240   if( rc==SQLITE_OK ){
55241     if( CURSOR_INVALID==pCur->eState ){
55242       assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->nCell==0 );
55243       *pRes = 1;
55244     }else{
55245       assert( pCur->eState==CURSOR_VALID );
55246       *pRes = 0;
55247       rc = moveToRightmost(pCur);
55248       pCur->atLast = rc==SQLITE_OK ?1:0;
55249     }
55250   }
55251   return rc;
55252 }
55253 
55254 /* Move the cursor so that it points to an entry near the key
55255 ** specified by pIdxKey or intKey.   Return a success code.
55256 **
55257 ** For INTKEY tables, the intKey parameter is used.  pIdxKey
55258 ** must be NULL.  For index tables, pIdxKey is used and intKey
55259 ** is ignored.
55260 **
55261 ** If an exact match is not found, then the cursor is always
55262 ** left pointing at a leaf page which would hold the entry if it
55263 ** were present.  The cursor might point to an entry that comes
55264 ** before or after the key.
55265 **
55266 ** An integer is written into *pRes which is the result of
55267 ** comparing the key with the entry to which the cursor is
55268 ** pointing.  The meaning of the integer written into
55269 ** *pRes is as follows:
55270 **
55271 **     *pRes<0      The cursor is left pointing at an entry that
55272 **                  is smaller than intKey/pIdxKey or if the table is empty
55273 **                  and the cursor is therefore left point to nothing.
55274 **
55275 **     *pRes==0     The cursor is left pointing at an entry that
55276 **                  exactly matches intKey/pIdxKey.
55277 **
55278 **     *pRes>0      The cursor is left pointing at an entry that
55279 **                  is larger than intKey/pIdxKey.
55280 **
55281 */
55282 SQLITE_PRIVATE int sqlite3BtreeMovetoUnpacked(
55283   BtCursor *pCur,          /* The cursor to be moved */
55284   UnpackedRecord *pIdxKey, /* Unpacked index key */
55285   i64 intKey,              /* The table key */
55286   int biasRight,           /* If true, bias the search to the high end */
55287   int *pRes                /* Write search results here */
55288 ){
55289   int rc;
55290 
55291   assert( cursorHoldsMutex(pCur) );
55292   assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
55293   assert( pRes );
55294   assert( (pIdxKey==0)==(pCur->pKeyInfo==0) );
55295 
55296   /* If the cursor is already positioned at the point we are trying
55297   ** to move to, then just return without doing any work */
55298   if( pCur->eState==CURSOR_VALID && pCur->validNKey
55299    && pCur->apPage[0]->intKey
55300   ){
55301     if( pCur->info.nKey==intKey ){
55302       *pRes = 0;
55303       return SQLITE_OK;
55304     }
55305     if( pCur->atLast && pCur->info.nKey<intKey ){
55306       *pRes = -1;
55307       return SQLITE_OK;
55308     }
55309   }
55310 
55311   rc = moveToRoot(pCur);
55312   if( rc ){
55313     return rc;
55314   }
55315   assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage] );
55316   assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->isInit );
55317   assert( pCur->eState==CURSOR_INVALID || pCur->apPage[pCur->iPage]->nCell>0 );
55318   if( pCur->eState==CURSOR_INVALID ){
55319     *pRes = -1;
55320     assert( pCur->pgnoRoot==0 || pCur->apPage[pCur->iPage]->nCell==0 );
55321     return SQLITE_OK;
55322   }
55323   assert( pCur->apPage[0]->intKey || pIdxKey );
55324   for(;;){
55325     int lwr, upr, idx, c;
55326     Pgno chldPg;
55327     MemPage *pPage = pCur->apPage[pCur->iPage];
55328     u8 *pCell;                          /* Pointer to current cell in pPage */
55329 
55330     /* pPage->nCell must be greater than zero. If this is the root-page
55331     ** the cursor would have been INVALID above and this for(;;) loop
55332     ** not run. If this is not the root-page, then the moveToChild() routine
55333     ** would have already detected db corruption. Similarly, pPage must
55334     ** be the right kind (index or table) of b-tree page. Otherwise
55335     ** a moveToChild() or moveToRoot() call would have detected corruption.  */
55336     assert( pPage->nCell>0 );
55337     assert( pPage->intKey==(pIdxKey==0) );
55338     lwr = 0;
55339     upr = pPage->nCell-1;
55340     assert( biasRight==0 || biasRight==1 );
55341     idx = upr>>(1-biasRight); /* idx = biasRight ? upr : (lwr+upr)/2; */
55342     pCur->aiIdx[pCur->iPage] = (u16)idx;
55343     if( pPage->intKey ){
55344       for(;;){
55345         i64 nCellKey;
55346         pCell = findCell(pPage, idx) + pPage->childPtrSize;
55347         if( pPage->hasData ){
55348           while( 0x80 <= *(pCell++) ){
55349             if( pCell>=pPage->aDataEnd ) return SQLITE_CORRUPT_BKPT;
55350           }
55351         }
55352         getVarint(pCell, (u64*)&nCellKey);
55353         if( nCellKey<intKey ){
55354           lwr = idx+1;
55355           if( lwr>upr ){ c = -1; break; }
55356         }else if( nCellKey>intKey ){
55357           upr = idx-1;
55358           if( lwr>upr ){ c = +1; break; }
55359         }else{
55360           assert( nCellKey==intKey );
55361           pCur->validNKey = 1;
55362           pCur->info.nKey = nCellKey;
55363           pCur->aiIdx[pCur->iPage] = (u16)idx;
55364           if( !pPage->leaf ){
55365             lwr = idx;
55366             goto moveto_next_layer;
55367           }else{
55368             *pRes = 0;
55369             rc = SQLITE_OK;
55370             goto moveto_finish;
55371           }
55372         }
55373         assert( lwr+upr>=0 );
55374         idx = (lwr+upr)>>1;  /* idx = (lwr+upr)/2; */
55375       }
55376     }else{
55377       for(;;){
55378         int nCell;
55379         pCell = findCell(pPage, idx) + pPage->childPtrSize;
55380 
55381         /* The maximum supported page-size is 65536 bytes. This means that
55382         ** the maximum number of record bytes stored on an index B-Tree
55383         ** page is less than 16384 bytes and may be stored as a 2-byte
55384         ** varint. This information is used to attempt to avoid parsing
55385         ** the entire cell by checking for the cases where the record is
55386         ** stored entirely within the b-tree page by inspecting the first
55387         ** 2 bytes of the cell.
55388         */
55389         nCell = pCell[0];
55390         if( nCell<=pPage->max1bytePayload ){
55391           /* This branch runs if the record-size field of the cell is a
55392           ** single byte varint and the record fits entirely on the main
55393           ** b-tree page.  */
55394           testcase( pCell+nCell+1==pPage->aDataEnd );
55395           c = sqlite3VdbeRecordCompare(nCell, (void*)&pCell[1], pIdxKey);
55396         }else if( !(pCell[1] & 0x80)
55397           && (nCell = ((nCell&0x7f)<<7) + pCell[1])<=pPage->maxLocal
55398         ){
55399           /* The record-size field is a 2 byte varint and the record
55400           ** fits entirely on the main b-tree page.  */
55401           testcase( pCell+nCell+2==pPage->aDataEnd );
55402           c = sqlite3VdbeRecordCompare(nCell, (void*)&pCell[2], pIdxKey);
55403         }else{
55404           /* The record flows over onto one or more overflow pages. In
55405           ** this case the whole cell needs to be parsed, a buffer allocated
55406           ** and accessPayload() used to retrieve the record into the
55407           ** buffer before VdbeRecordCompare() can be called. */
55408           void *pCellKey;
55409           u8 * const pCellBody = pCell - pPage->childPtrSize;
55410           btreeParseCellPtr(pPage, pCellBody, &pCur->info);
55411           nCell = (int)pCur->info.nKey;
55412           pCellKey = sqlite3Malloc( nCell );
55413           if( pCellKey==0 ){
55414             rc = SQLITE_NOMEM;
55415             goto moveto_finish;
55416           }
55417           pCur->aiIdx[pCur->iPage] = (u16)idx;
55418           rc = accessPayload(pCur, 0, nCell, (unsigned char*)pCellKey, 0);
55419           if( rc ){
55420             sqlite3_free(pCellKey);
55421             goto moveto_finish;
55422           }
55423           c = sqlite3VdbeRecordCompare(nCell, pCellKey, pIdxKey);
55424           sqlite3_free(pCellKey);
55425         }
55426         if( c<0 ){
55427           lwr = idx+1;
55428         }else if( c>0 ){
55429           upr = idx-1;
55430         }else{
55431           assert( c==0 );
55432           *pRes = 0;
55433           rc = SQLITE_OK;
55434           pCur->aiIdx[pCur->iPage] = (u16)idx;
55435           goto moveto_finish;
55436         }
55437         if( lwr>upr ) break;
55438         assert( lwr+upr>=0 );
55439         idx = (lwr+upr)>>1;  /* idx = (lwr+upr)/2 */
55440       }
55441     }
55442     assert( lwr==upr+1 || (pPage->intKey && !pPage->leaf) );
55443     assert( pPage->isInit );
55444     if( pPage->leaf ){
55445       assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell );
55446       pCur->aiIdx[pCur->iPage] = (u16)idx;
55447       *pRes = c;
55448       rc = SQLITE_OK;
55449       goto moveto_finish;
55450     }
55451 moveto_next_layer:
55452     if( lwr>=pPage->nCell ){
55453       chldPg = get4byte(&pPage->aData[pPage->hdrOffset+8]);
55454     }else{
55455       chldPg = get4byte(findCell(pPage, lwr));
55456     }
55457     pCur->aiIdx[pCur->iPage] = (u16)lwr;
55458     rc = moveToChild(pCur, chldPg);
55459     if( rc ) break;
55460   }
55461 moveto_finish:
55462   pCur->info.nSize = 0;
55463   pCur->validNKey = 0;
55464   return rc;
55465 }
55466 
55467 
55468 /*
55469 ** Return TRUE if the cursor is not pointing at an entry of the table.
55470 **
55471 ** TRUE will be returned after a call to sqlite3BtreeNext() moves
55472 ** past the last entry in the table or sqlite3BtreePrev() moves past
55473 ** the first entry.  TRUE is also returned if the table is empty.
55474 */
55475 SQLITE_PRIVATE int sqlite3BtreeEof(BtCursor *pCur){
55476   /* TODO: What if the cursor is in CURSOR_REQUIRESEEK but all table entries
55477   ** have been deleted? This API will need to change to return an error code
55478   ** as well as the boolean result value.
55479   */
55480   return (CURSOR_VALID!=pCur->eState);
55481 }
55482 
55483 /*
55484 ** Advance the cursor to the next entry in the database.  If
55485 ** successful then set *pRes=0.  If the cursor
55486 ** was already pointing to the last entry in the database before
55487 ** this routine was called, then set *pRes=1.
55488 */
55489 SQLITE_PRIVATE int sqlite3BtreeNext(BtCursor *pCur, int *pRes){
55490   int rc;
55491   int idx;
55492   MemPage *pPage;
55493 
55494   assert( cursorHoldsMutex(pCur) );
55495   assert( pRes!=0 );
55496   assert( pCur->skipNext==0 || pCur->eState!=CURSOR_VALID );
55497   if( pCur->eState!=CURSOR_VALID ){
55498     rc = restoreCursorPosition(pCur);
55499     if( rc!=SQLITE_OK ){
55500       *pRes = 0;
55501       return rc;
55502     }
55503     if( CURSOR_INVALID==pCur->eState ){
55504       *pRes = 1;
55505       return SQLITE_OK;
55506     }
55507     if( pCur->skipNext ){
55508       assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_SKIPNEXT );
55509       pCur->eState = CURSOR_VALID;
55510       if( pCur->skipNext>0 ){
55511         pCur->skipNext = 0;
55512         *pRes = 0;
55513         return SQLITE_OK;
55514       }
55515       pCur->skipNext = 0;
55516     }
55517   }
55518 
55519   pPage = pCur->apPage[pCur->iPage];
55520   idx = ++pCur->aiIdx[pCur->iPage];
55521   assert( pPage->isInit );
55522 
55523   /* If the database file is corrupt, it is possible for the value of idx
55524   ** to be invalid here. This can only occur if a second cursor modifies
55525   ** the page while cursor pCur is holding a reference to it. Which can
55526   ** only happen if the database is corrupt in such a way as to link the
55527   ** page into more than one b-tree structure. */
55528   testcase( idx>pPage->nCell );
55529 
55530   pCur->info.nSize = 0;
55531   pCur->validNKey = 0;
55532   if( idx>=pPage->nCell ){
55533     if( !pPage->leaf ){
55534       rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8]));
55535       if( rc ){
55536         *pRes = 0;
55537         return rc;
55538       }
55539       rc = moveToLeftmost(pCur);
55540       *pRes = 0;
55541       return rc;
55542     }
55543     do{
55544       if( pCur->iPage==0 ){
55545         *pRes = 1;
55546         pCur->eState = CURSOR_INVALID;
55547         return SQLITE_OK;
55548       }
55549       moveToParent(pCur);
55550       pPage = pCur->apPage[pCur->iPage];
55551     }while( pCur->aiIdx[pCur->iPage]>=pPage->nCell );
55552     *pRes = 0;
55553     if( pPage->intKey ){
55554       rc = sqlite3BtreeNext(pCur, pRes);
55555     }else{
55556       rc = SQLITE_OK;
55557     }
55558     return rc;
55559   }
55560   *pRes = 0;
55561   if( pPage->leaf ){
55562     return SQLITE_OK;
55563   }
55564   rc = moveToLeftmost(pCur);
55565   return rc;
55566 }
55567 
55568 
55569 /*
55570 ** Step the cursor to the back to the previous entry in the database.  If
55571 ** successful then set *pRes=0.  If the cursor
55572 ** was already pointing to the first entry in the database before
55573 ** this routine was called, then set *pRes=1.
55574 */
55575 SQLITE_PRIVATE int sqlite3BtreePrevious(BtCursor *pCur, int *pRes){
55576   int rc;
55577   MemPage *pPage;
55578 
55579   assert( cursorHoldsMutex(pCur) );
55580   assert( pRes!=0 );
55581   assert( pCur->skipNext==0 || pCur->eState!=CURSOR_VALID );
55582   pCur->atLast = 0;
55583   if( pCur->eState!=CURSOR_VALID ){
55584     if( ALWAYS(pCur->eState>=CURSOR_REQUIRESEEK) ){
55585       rc = btreeRestoreCursorPosition(pCur);
55586       if( rc!=SQLITE_OK ){
55587         *pRes = 0;
55588         return rc;
55589       }
55590     }
55591     if( CURSOR_INVALID==pCur->eState ){
55592       *pRes = 1;
55593       return SQLITE_OK;
55594     }
55595     if( pCur->skipNext ){
55596       assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_SKIPNEXT );
55597       pCur->eState = CURSOR_VALID;
55598       if( pCur->skipNext<0 ){
55599         pCur->skipNext = 0;
55600         *pRes = 0;
55601         return SQLITE_OK;
55602       }
55603       pCur->skipNext = 0;
55604     }
55605   }
55606 
55607   pPage = pCur->apPage[pCur->iPage];
55608   assert( pPage->isInit );
55609   if( !pPage->leaf ){
55610     int idx = pCur->aiIdx[pCur->iPage];
55611     rc = moveToChild(pCur, get4byte(findCell(pPage, idx)));
55612     if( rc ){
55613       *pRes = 0;
55614       return rc;
55615     }
55616     rc = moveToRightmost(pCur);
55617   }else{
55618     while( pCur->aiIdx[pCur->iPage]==0 ){
55619       if( pCur->iPage==0 ){
55620         pCur->eState = CURSOR_INVALID;
55621         *pRes = 1;
55622         return SQLITE_OK;
55623       }
55624       moveToParent(pCur);
55625     }
55626     pCur->info.nSize = 0;
55627     pCur->validNKey = 0;
55628 
55629     pCur->aiIdx[pCur->iPage]--;
55630     pPage = pCur->apPage[pCur->iPage];
55631     if( pPage->intKey && !pPage->leaf ){
55632       rc = sqlite3BtreePrevious(pCur, pRes);
55633     }else{
55634       rc = SQLITE_OK;
55635     }
55636   }
55637   *pRes = 0;
55638   return rc;
55639 }
55640 
55641 /*
55642 ** Allocate a new page from the database file.
55643 **
55644 ** The new page is marked as dirty.  (In other words, sqlite3PagerWrite()
55645 ** has already been called on the new page.)  The new page has also
55646 ** been referenced and the calling routine is responsible for calling
55647 ** sqlite3PagerUnref() on the new page when it is done.
55648 **
55649 ** SQLITE_OK is returned on success.  Any other return value indicates
55650 ** an error.  *ppPage and *pPgno are undefined in the event of an error.
55651 ** Do not invoke sqlite3PagerUnref() on *ppPage if an error is returned.
55652 **
55653 ** If the "nearby" parameter is not 0, then an effort is made to
55654 ** locate a page close to the page number "nearby".  This can be used in an
55655 ** attempt to keep related pages close to each other in the database file,
55656 ** which in turn can make database access faster.
55657 **
55658 ** If the eMode parameter is BTALLOC_EXACT and the nearby page exists
55659 ** anywhere on the free-list, then it is guaranteed to be returned.  If
55660 ** eMode is BTALLOC_LT then the page returned will be less than or equal
55661 ** to nearby if any such page exists.  If eMode is BTALLOC_ANY then there
55662 ** are no restrictions on which page is returned.
55663 */
55664 static int allocateBtreePage(
55665   BtShared *pBt,         /* The btree */
55666   MemPage **ppPage,      /* Store pointer to the allocated page here */
55667   Pgno *pPgno,           /* Store the page number here */
55668   Pgno nearby,           /* Search for a page near this one */
55669   u8 eMode               /* BTALLOC_EXACT, BTALLOC_LT, or BTALLOC_ANY */
55670 ){
55671   MemPage *pPage1;
55672   int rc;
55673   u32 n;     /* Number of pages on the freelist */
55674   u32 k;     /* Number of leaves on the trunk of the freelist */
55675   MemPage *pTrunk = 0;
55676   MemPage *pPrevTrunk = 0;
55677   Pgno mxPage;     /* Total size of the database file */
55678 
55679   assert( sqlite3_mutex_held(pBt->mutex) );
55680   assert( eMode==BTALLOC_ANY || (nearby>0 && IfNotOmitAV(pBt->autoVacuum)) );
55681   pPage1 = pBt->pPage1;
55682   mxPage = btreePagecount(pBt);
55683   n = get4byte(&pPage1->aData[36]);
55684   testcase( n==mxPage-1 );
55685   if( n>=mxPage ){
55686     return SQLITE_CORRUPT_BKPT;
55687   }
55688   if( n>0 ){
55689     /* There are pages on the freelist.  Reuse one of those pages. */
55690     Pgno iTrunk;
55691     u8 searchList = 0; /* If the free-list must be searched for 'nearby' */
55692 
55693     /* If eMode==BTALLOC_EXACT and a query of the pointer-map
55694     ** shows that the page 'nearby' is somewhere on the free-list, then
55695     ** the entire-list will be searched for that page.
55696     */
55697 #ifndef SQLITE_OMIT_AUTOVACUUM
55698     if( eMode==BTALLOC_EXACT ){
55699       if( nearby<=mxPage ){
55700         u8 eType;
55701         assert( nearby>0 );
55702         assert( pBt->autoVacuum );
55703         rc = ptrmapGet(pBt, nearby, &eType, 0);
55704         if( rc ) return rc;
55705         if( eType==PTRMAP_FREEPAGE ){
55706           searchList = 1;
55707         }
55708       }
55709     }else if( eMode==BTALLOC_LE ){
55710       searchList = 1;
55711     }
55712 #endif
55713 
55714     /* Decrement the free-list count by 1. Set iTrunk to the index of the
55715     ** first free-list trunk page. iPrevTrunk is initially 1.
55716     */
55717     rc = sqlite3PagerWrite(pPage1->pDbPage);
55718     if( rc ) return rc;
55719     put4byte(&pPage1->aData[36], n-1);
55720 
55721     /* The code within this loop is run only once if the 'searchList' variable
55722     ** is not true. Otherwise, it runs once for each trunk-page on the
55723     ** free-list until the page 'nearby' is located (eMode==BTALLOC_EXACT)
55724     ** or until a page less than 'nearby' is located (eMode==BTALLOC_LT)
55725     */
55726     do {
55727       pPrevTrunk = pTrunk;
55728       if( pPrevTrunk ){
55729         iTrunk = get4byte(&pPrevTrunk->aData[0]);
55730       }else{
55731         iTrunk = get4byte(&pPage1->aData[32]);
55732       }
55733       testcase( iTrunk==mxPage );
55734       if( iTrunk>mxPage ){
55735         rc = SQLITE_CORRUPT_BKPT;
55736       }else{
55737         rc = btreeGetPage(pBt, iTrunk, &pTrunk, 0);
55738       }
55739       if( rc ){
55740         pTrunk = 0;
55741         goto end_allocate_page;
55742       }
55743       assert( pTrunk!=0 );
55744       assert( pTrunk->aData!=0 );
55745 
55746       k = get4byte(&pTrunk->aData[4]); /* # of leaves on this trunk page */
55747       if( k==0 && !searchList ){
55748         /* The trunk has no leaves and the list is not being searched.
55749         ** So extract the trunk page itself and use it as the newly
55750         ** allocated page */
55751         assert( pPrevTrunk==0 );
55752         rc = sqlite3PagerWrite(pTrunk->pDbPage);
55753         if( rc ){
55754           goto end_allocate_page;
55755         }
55756         *pPgno = iTrunk;
55757         memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4);
55758         *ppPage = pTrunk;
55759         pTrunk = 0;
55760         TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1));
55761       }else if( k>(u32)(pBt->usableSize/4 - 2) ){
55762         /* Value of k is out of range.  Database corruption */
55763         rc = SQLITE_CORRUPT_BKPT;
55764         goto end_allocate_page;
55765 #ifndef SQLITE_OMIT_AUTOVACUUM
55766       }else if( searchList
55767             && (nearby==iTrunk || (iTrunk<nearby && eMode==BTALLOC_LE))
55768       ){
55769         /* The list is being searched and this trunk page is the page
55770         ** to allocate, regardless of whether it has leaves.
55771         */
55772         *pPgno = iTrunk;
55773         *ppPage = pTrunk;
55774         searchList = 0;
55775         rc = sqlite3PagerWrite(pTrunk->pDbPage);
55776         if( rc ){
55777           goto end_allocate_page;
55778         }
55779         if( k==0 ){
55780           if( !pPrevTrunk ){
55781             memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4);
55782           }else{
55783             rc = sqlite3PagerWrite(pPrevTrunk->pDbPage);
55784             if( rc!=SQLITE_OK ){
55785               goto end_allocate_page;
55786             }
55787             memcpy(&pPrevTrunk->aData[0], &pTrunk->aData[0], 4);
55788           }
55789         }else{
55790           /* The trunk page is required by the caller but it contains
55791           ** pointers to free-list leaves. The first leaf becomes a trunk
55792           ** page in this case.
55793           */
55794           MemPage *pNewTrunk;
55795           Pgno iNewTrunk = get4byte(&pTrunk->aData[8]);
55796           if( iNewTrunk>mxPage ){
55797             rc = SQLITE_CORRUPT_BKPT;
55798             goto end_allocate_page;
55799           }
55800           testcase( iNewTrunk==mxPage );
55801           rc = btreeGetPage(pBt, iNewTrunk, &pNewTrunk, 0);
55802           if( rc!=SQLITE_OK ){
55803             goto end_allocate_page;
55804           }
55805           rc = sqlite3PagerWrite(pNewTrunk->pDbPage);
55806           if( rc!=SQLITE_OK ){
55807             releasePage(pNewTrunk);
55808             goto end_allocate_page;
55809           }
55810           memcpy(&pNewTrunk->aData[0], &pTrunk->aData[0], 4);
55811           put4byte(&pNewTrunk->aData[4], k-1);
55812           memcpy(&pNewTrunk->aData[8], &pTrunk->aData[12], (k-1)*4);
55813           releasePage(pNewTrunk);
55814           if( !pPrevTrunk ){
55815             assert( sqlite3PagerIswriteable(pPage1->pDbPage) );
55816             put4byte(&pPage1->aData[32], iNewTrunk);
55817           }else{
55818             rc = sqlite3PagerWrite(pPrevTrunk->pDbPage);
55819             if( rc ){
55820               goto end_allocate_page;
55821             }
55822             put4byte(&pPrevTrunk->aData[0], iNewTrunk);
55823           }
55824         }
55825         pTrunk = 0;
55826         TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1));
55827 #endif
55828       }else if( k>0 ){
55829         /* Extract a leaf from the trunk */
55830         u32 closest;
55831         Pgno iPage;
55832         unsigned char *aData = pTrunk->aData;
55833         if( nearby>0 ){
55834           u32 i;
55835           closest = 0;
55836           if( eMode==BTALLOC_LE ){
55837             for(i=0; i<k; i++){
55838               iPage = get4byte(&aData[8+i*4]);
55839               if( iPage<=nearby ){
55840                 closest = i;
55841                 break;
55842               }
55843             }
55844           }else{
55845             int dist;
55846             dist = sqlite3AbsInt32(get4byte(&aData[8]) - nearby);
55847             for(i=1; i<k; i++){
55848               int d2 = sqlite3AbsInt32(get4byte(&aData[8+i*4]) - nearby);
55849               if( d2<dist ){
55850                 closest = i;
55851                 dist = d2;
55852               }
55853             }
55854           }
55855         }else{
55856           closest = 0;
55857         }
55858 
55859         iPage = get4byte(&aData[8+closest*4]);
55860         testcase( iPage==mxPage );
55861         if( iPage>mxPage ){
55862           rc = SQLITE_CORRUPT_BKPT;
55863           goto end_allocate_page;
55864         }
55865         testcase( iPage==mxPage );
55866         if( !searchList
55867          || (iPage==nearby || (iPage<nearby && eMode==BTALLOC_LE))
55868         ){
55869           int noContent;
55870           *pPgno = iPage;
55871           TRACE(("ALLOCATE: %d was leaf %d of %d on trunk %d"
55872                  ": %d more free pages\n",
55873                  *pPgno, closest+1, k, pTrunk->pgno, n-1));
55874           rc = sqlite3PagerWrite(pTrunk->pDbPage);
55875           if( rc ) goto end_allocate_page;
55876           if( closest<k-1 ){
55877             memcpy(&aData[8+closest*4], &aData[4+k*4], 4);
55878           }
55879           put4byte(&aData[4], k-1);
55880           noContent = !btreeGetHasContent(pBt, *pPgno) ? PAGER_GET_NOCONTENT : 0;
55881           rc = btreeGetPage(pBt, *pPgno, ppPage, noContent);
55882           if( rc==SQLITE_OK ){
55883             rc = sqlite3PagerWrite((*ppPage)->pDbPage);
55884             if( rc!=SQLITE_OK ){
55885               releasePage(*ppPage);
55886             }
55887           }
55888           searchList = 0;
55889         }
55890       }
55891       releasePage(pPrevTrunk);
55892       pPrevTrunk = 0;
55893     }while( searchList );
55894   }else{
55895     /* There are no pages on the freelist, so append a new page to the
55896     ** database image.
55897     **
55898     ** Normally, new pages allocated by this block can be requested from the
55899     ** pager layer with the 'no-content' flag set. This prevents the pager
55900     ** from trying to read the pages content from disk. However, if the
55901     ** current transaction has already run one or more incremental-vacuum
55902     ** steps, then the page we are about to allocate may contain content
55903     ** that is required in the event of a rollback. In this case, do
55904     ** not set the no-content flag. This causes the pager to load and journal
55905     ** the current page content before overwriting it.
55906     **
55907     ** Note that the pager will not actually attempt to load or journal
55908     ** content for any page that really does lie past the end of the database
55909     ** file on disk. So the effects of disabling the no-content optimization
55910     ** here are confined to those pages that lie between the end of the
55911     ** database image and the end of the database file.
55912     */
55913     int bNoContent = (0==IfNotOmitAV(pBt->bDoTruncate)) ? PAGER_GET_NOCONTENT : 0;
55914 
55915     rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
55916     if( rc ) return rc;
55917     pBt->nPage++;
55918     if( pBt->nPage==PENDING_BYTE_PAGE(pBt) ) pBt->nPage++;
55919 
55920 #ifndef SQLITE_OMIT_AUTOVACUUM
55921     if( pBt->autoVacuum && PTRMAP_ISPAGE(pBt, pBt->nPage) ){
55922       /* If *pPgno refers to a pointer-map page, allocate two new pages
55923       ** at the end of the file instead of one. The first allocated page
55924       ** becomes a new pointer-map page, the second is used by the caller.
55925       */
55926       MemPage *pPg = 0;
55927       TRACE(("ALLOCATE: %d from end of file (pointer-map page)\n", pBt->nPage));
55928       assert( pBt->nPage!=PENDING_BYTE_PAGE(pBt) );
55929       rc = btreeGetPage(pBt, pBt->nPage, &pPg, bNoContent);
55930       if( rc==SQLITE_OK ){
55931         rc = sqlite3PagerWrite(pPg->pDbPage);
55932         releasePage(pPg);
55933       }
55934       if( rc ) return rc;
55935       pBt->nPage++;
55936       if( pBt->nPage==PENDING_BYTE_PAGE(pBt) ){ pBt->nPage++; }
55937     }
55938 #endif
55939     put4byte(28 + (u8*)pBt->pPage1->aData, pBt->nPage);
55940     *pPgno = pBt->nPage;
55941 
55942     assert( *pPgno!=PENDING_BYTE_PAGE(pBt) );
55943     rc = btreeGetPage(pBt, *pPgno, ppPage, bNoContent);
55944     if( rc ) return rc;
55945     rc = sqlite3PagerWrite((*ppPage)->pDbPage);
55946     if( rc!=SQLITE_OK ){
55947       releasePage(*ppPage);
55948     }
55949     TRACE(("ALLOCATE: %d from end of file\n", *pPgno));
55950   }
55951 
55952   assert( *pPgno!=PENDING_BYTE_PAGE(pBt) );
55953 
55954 end_allocate_page:
55955   releasePage(pTrunk);
55956   releasePage(pPrevTrunk);
55957   if( rc==SQLITE_OK ){
55958     if( sqlite3PagerPageRefcount((*ppPage)->pDbPage)>1 ){
55959       releasePage(*ppPage);
55960       *ppPage = 0;
55961       return SQLITE_CORRUPT_BKPT;
55962     }
55963     (*ppPage)->isInit = 0;
55964   }else{
55965     *ppPage = 0;
55966   }
55967   assert( rc!=SQLITE_OK || sqlite3PagerIswriteable((*ppPage)->pDbPage) );
55968   return rc;
55969 }
55970 
55971 /*
55972 ** This function is used to add page iPage to the database file free-list.
55973 ** It is assumed that the page is not already a part of the free-list.
55974 **
55975 ** The value passed as the second argument to this function is optional.
55976 ** If the caller happens to have a pointer to the MemPage object
55977 ** corresponding to page iPage handy, it may pass it as the second value.
55978 ** Otherwise, it may pass NULL.
55979 **
55980 ** If a pointer to a MemPage object is passed as the second argument,
55981 ** its reference count is not altered by this function.
55982 */
55983 static int freePage2(BtShared *pBt, MemPage *pMemPage, Pgno iPage){
55984   MemPage *pTrunk = 0;                /* Free-list trunk page */
55985   Pgno iTrunk = 0;                    /* Page number of free-list trunk page */
55986   MemPage *pPage1 = pBt->pPage1;      /* Local reference to page 1 */
55987   MemPage *pPage;                     /* Page being freed. May be NULL. */
55988   int rc;                             /* Return Code */
55989   int nFree;                          /* Initial number of pages on free-list */
55990 
55991   assert( sqlite3_mutex_held(pBt->mutex) );
55992   assert( iPage>1 );
55993   assert( !pMemPage || pMemPage->pgno==iPage );
55994 
55995   if( pMemPage ){
55996     pPage = pMemPage;
55997     sqlite3PagerRef(pPage->pDbPage);
55998   }else{
55999     pPage = btreePageLookup(pBt, iPage);
56000   }
56001 
56002   /* Increment the free page count on pPage1 */
56003   rc = sqlite3PagerWrite(pPage1->pDbPage);
56004   if( rc ) goto freepage_out;
56005   nFree = get4byte(&pPage1->aData[36]);
56006   put4byte(&pPage1->aData[36], nFree+1);
56007 
56008   if( pBt->btsFlags & BTS_SECURE_DELETE ){
56009     /* If the secure_delete option is enabled, then
56010     ** always fully overwrite deleted information with zeros.
56011     */
56012     if( (!pPage && ((rc = btreeGetPage(pBt, iPage, &pPage, 0))!=0) )
56013      ||            ((rc = sqlite3PagerWrite(pPage->pDbPage))!=0)
56014     ){
56015       goto freepage_out;
56016     }
56017     memset(pPage->aData, 0, pPage->pBt->pageSize);
56018   }
56019 
56020   /* If the database supports auto-vacuum, write an entry in the pointer-map
56021   ** to indicate that the page is free.
56022   */
56023   if( ISAUTOVACUUM ){
56024     ptrmapPut(pBt, iPage, PTRMAP_FREEPAGE, 0, &rc);
56025     if( rc ) goto freepage_out;
56026   }
56027 
56028   /* Now manipulate the actual database free-list structure. There are two
56029   ** possibilities. If the free-list is currently empty, or if the first
56030   ** trunk page in the free-list is full, then this page will become a
56031   ** new free-list trunk page. Otherwise, it will become a leaf of the
56032   ** first trunk page in the current free-list. This block tests if it
56033   ** is possible to add the page as a new free-list leaf.
56034   */
56035   if( nFree!=0 ){
56036     u32 nLeaf;                /* Initial number of leaf cells on trunk page */
56037 
56038     iTrunk = get4byte(&pPage1->aData[32]);
56039     rc = btreeGetPage(pBt, iTrunk, &pTrunk, 0);
56040     if( rc!=SQLITE_OK ){
56041       goto freepage_out;
56042     }
56043 
56044     nLeaf = get4byte(&pTrunk->aData[4]);
56045     assert( pBt->usableSize>32 );
56046     if( nLeaf > (u32)pBt->usableSize/4 - 2 ){
56047       rc = SQLITE_CORRUPT_BKPT;
56048       goto freepage_out;
56049     }
56050     if( nLeaf < (u32)pBt->usableSize/4 - 8 ){
56051       /* In this case there is room on the trunk page to insert the page
56052       ** being freed as a new leaf.
56053       **
56054       ** Note that the trunk page is not really full until it contains
56055       ** usableSize/4 - 2 entries, not usableSize/4 - 8 entries as we have
56056       ** coded.  But due to a coding error in versions of SQLite prior to
56057       ** 3.6.0, databases with freelist trunk pages holding more than
56058       ** usableSize/4 - 8 entries will be reported as corrupt.  In order
56059       ** to maintain backwards compatibility with older versions of SQLite,
56060       ** we will continue to restrict the number of entries to usableSize/4 - 8
56061       ** for now.  At some point in the future (once everyone has upgraded
56062       ** to 3.6.0 or later) we should consider fixing the conditional above
56063       ** to read "usableSize/4-2" instead of "usableSize/4-8".
56064       */
56065       rc = sqlite3PagerWrite(pTrunk->pDbPage);
56066       if( rc==SQLITE_OK ){
56067         put4byte(&pTrunk->aData[4], nLeaf+1);
56068         put4byte(&pTrunk->aData[8+nLeaf*4], iPage);
56069         if( pPage && (pBt->btsFlags & BTS_SECURE_DELETE)==0 ){
56070           sqlite3PagerDontWrite(pPage->pDbPage);
56071         }
56072         rc = btreeSetHasContent(pBt, iPage);
56073       }
56074       TRACE(("FREE-PAGE: %d leaf on trunk page %d\n",pPage->pgno,pTrunk->pgno));
56075       goto freepage_out;
56076     }
56077   }
56078 
56079   /* If control flows to this point, then it was not possible to add the
56080   ** the page being freed as a leaf page of the first trunk in the free-list.
56081   ** Possibly because the free-list is empty, or possibly because the
56082   ** first trunk in the free-list is full. Either way, the page being freed
56083   ** will become the new first trunk page in the free-list.
56084   */
56085   if( pPage==0 && SQLITE_OK!=(rc = btreeGetPage(pBt, iPage, &pPage, 0)) ){
56086     goto freepage_out;
56087   }
56088   rc = sqlite3PagerWrite(pPage->pDbPage);
56089   if( rc!=SQLITE_OK ){
56090     goto freepage_out;
56091   }
56092   put4byte(pPage->aData, iTrunk);
56093   put4byte(&pPage->aData[4], 0);
56094   put4byte(&pPage1->aData[32], iPage);
56095   TRACE(("FREE-PAGE: %d new trunk page replacing %d\n", pPage->pgno, iTrunk));
56096 
56097 freepage_out:
56098   if( pPage ){
56099     pPage->isInit = 0;
56100   }
56101   releasePage(pPage);
56102   releasePage(pTrunk);
56103   return rc;
56104 }
56105 static void freePage(MemPage *pPage, int *pRC){
56106   if( (*pRC)==SQLITE_OK ){
56107     *pRC = freePage2(pPage->pBt, pPage, pPage->pgno);
56108   }
56109 }
56110 
56111 /*
56112 ** Free any overflow pages associated with the given Cell.
56113 */
56114 static int clearCell(MemPage *pPage, unsigned char *pCell){
56115   BtShared *pBt = pPage->pBt;
56116   CellInfo info;
56117   Pgno ovflPgno;
56118   int rc;
56119   int nOvfl;
56120   u32 ovflPageSize;
56121 
56122   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
56123   btreeParseCellPtr(pPage, pCell, &info);
56124   if( info.iOverflow==0 ){
56125     return SQLITE_OK;  /* No overflow pages. Return without doing anything */
56126   }
56127   if( pCell+info.iOverflow+3 > pPage->aData+pPage->maskPage ){
56128     return SQLITE_CORRUPT_BKPT;  /* Cell extends past end of page */
56129   }
56130   ovflPgno = get4byte(&pCell[info.iOverflow]);
56131   assert( pBt->usableSize > 4 );
56132   ovflPageSize = pBt->usableSize - 4;
56133   nOvfl = (info.nPayload - info.nLocal + ovflPageSize - 1)/ovflPageSize;
56134   assert( ovflPgno==0 || nOvfl>0 );
56135   while( nOvfl-- ){
56136     Pgno iNext = 0;
56137     MemPage *pOvfl = 0;
56138     if( ovflPgno<2 || ovflPgno>btreePagecount(pBt) ){
56139       /* 0 is not a legal page number and page 1 cannot be an
56140       ** overflow page. Therefore if ovflPgno<2 or past the end of the
56141       ** file the database must be corrupt. */
56142       return SQLITE_CORRUPT_BKPT;
56143     }
56144     if( nOvfl ){
56145       rc = getOverflowPage(pBt, ovflPgno, &pOvfl, &iNext);
56146       if( rc ) return rc;
56147     }
56148 
56149     if( ( pOvfl || ((pOvfl = btreePageLookup(pBt, ovflPgno))!=0) )
56150      && sqlite3PagerPageRefcount(pOvfl->pDbPage)!=1
56151     ){
56152       /* There is no reason any cursor should have an outstanding reference
56153       ** to an overflow page belonging to a cell that is being deleted/updated.
56154       ** So if there exists more than one reference to this page, then it
56155       ** must not really be an overflow page and the database must be corrupt.
56156       ** It is helpful to detect this before calling freePage2(), as
56157       ** freePage2() may zero the page contents if secure-delete mode is
56158       ** enabled. If this 'overflow' page happens to be a page that the
56159       ** caller is iterating through or using in some other way, this
56160       ** can be problematic.
56161       */
56162       rc = SQLITE_CORRUPT_BKPT;
56163     }else{
56164       rc = freePage2(pBt, pOvfl, ovflPgno);
56165     }
56166 
56167     if( pOvfl ){
56168       sqlite3PagerUnref(pOvfl->pDbPage);
56169     }
56170     if( rc ) return rc;
56171     ovflPgno = iNext;
56172   }
56173   return SQLITE_OK;
56174 }
56175 
56176 /*
56177 ** Create the byte sequence used to represent a cell on page pPage
56178 ** and write that byte sequence into pCell[].  Overflow pages are
56179 ** allocated and filled in as necessary.  The calling procedure
56180 ** is responsible for making sure sufficient space has been allocated
56181 ** for pCell[].
56182 **
56183 ** Note that pCell does not necessary need to point to the pPage->aData
56184 ** area.  pCell might point to some temporary storage.  The cell will
56185 ** be constructed in this temporary area then copied into pPage->aData
56186 ** later.
56187 */
56188 static int fillInCell(
56189   MemPage *pPage,                /* The page that contains the cell */
56190   unsigned char *pCell,          /* Complete text of the cell */
56191   const void *pKey, i64 nKey,    /* The key */
56192   const void *pData,int nData,   /* The data */
56193   int nZero,                     /* Extra zero bytes to append to pData */
56194   int *pnSize                    /* Write cell size here */
56195 ){
56196   int nPayload;
56197   const u8 *pSrc;
56198   int nSrc, n, rc;
56199   int spaceLeft;
56200   MemPage *pOvfl = 0;
56201   MemPage *pToRelease = 0;
56202   unsigned char *pPrior;
56203   unsigned char *pPayload;
56204   BtShared *pBt = pPage->pBt;
56205   Pgno pgnoOvfl = 0;
56206   int nHeader;
56207   CellInfo info;
56208 
56209   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
56210 
56211   /* pPage is not necessarily writeable since pCell might be auxiliary
56212   ** buffer space that is separate from the pPage buffer area */
56213   assert( pCell<pPage->aData || pCell>=&pPage->aData[pBt->pageSize]
56214             || sqlite3PagerIswriteable(pPage->pDbPage) );
56215 
56216   /* Fill in the header. */
56217   nHeader = 0;
56218   if( !pPage->leaf ){
56219     nHeader += 4;
56220   }
56221   if( pPage->hasData ){
56222     nHeader += putVarint32(&pCell[nHeader], nData+nZero);
56223   }else{
56224     nData = nZero = 0;
56225   }
56226   nHeader += putVarint(&pCell[nHeader], *(u64*)&nKey);
56227   btreeParseCellPtr(pPage, pCell, &info);
56228   assert( info.nHeader==nHeader );
56229   assert( info.nKey==nKey );
56230   assert( info.nData==(u32)(nData+nZero) );
56231 
56232   /* Fill in the payload */
56233   nPayload = nData + nZero;
56234   if( pPage->intKey ){
56235     pSrc = pData;
56236     nSrc = nData;
56237     nData = 0;
56238   }else{
56239     if( NEVER(nKey>0x7fffffff || pKey==0) ){
56240       return SQLITE_CORRUPT_BKPT;
56241     }
56242     nPayload += (int)nKey;
56243     pSrc = pKey;
56244     nSrc = (int)nKey;
56245   }
56246   *pnSize = info.nSize;
56247   spaceLeft = info.nLocal;
56248   pPayload = &pCell[nHeader];
56249   pPrior = &pCell[info.iOverflow];
56250 
56251   while( nPayload>0 ){
56252     if( spaceLeft==0 ){
56253 #ifndef SQLITE_OMIT_AUTOVACUUM
56254       Pgno pgnoPtrmap = pgnoOvfl; /* Overflow page pointer-map entry page */
56255       if( pBt->autoVacuum ){
56256         do{
56257           pgnoOvfl++;
56258         } while(
56259           PTRMAP_ISPAGE(pBt, pgnoOvfl) || pgnoOvfl==PENDING_BYTE_PAGE(pBt)
56260         );
56261       }
56262 #endif
56263       rc = allocateBtreePage(pBt, &pOvfl, &pgnoOvfl, pgnoOvfl, 0);
56264 #ifndef SQLITE_OMIT_AUTOVACUUM
56265       /* If the database supports auto-vacuum, and the second or subsequent
56266       ** overflow page is being allocated, add an entry to the pointer-map
56267       ** for that page now.
56268       **
56269       ** If this is the first overflow page, then write a partial entry
56270       ** to the pointer-map. If we write nothing to this pointer-map slot,
56271       ** then the optimistic overflow chain processing in clearCell()
56272       ** may misinterpret the uninitialized values and delete the
56273       ** wrong pages from the database.
56274       */
56275       if( pBt->autoVacuum && rc==SQLITE_OK ){
56276         u8 eType = (pgnoPtrmap?PTRMAP_OVERFLOW2:PTRMAP_OVERFLOW1);
56277         ptrmapPut(pBt, pgnoOvfl, eType, pgnoPtrmap, &rc);
56278         if( rc ){
56279           releasePage(pOvfl);
56280         }
56281       }
56282 #endif
56283       if( rc ){
56284         releasePage(pToRelease);
56285         return rc;
56286       }
56287 
56288       /* If pToRelease is not zero than pPrior points into the data area
56289       ** of pToRelease.  Make sure pToRelease is still writeable. */
56290       assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) );
56291 
56292       /* If pPrior is part of the data area of pPage, then make sure pPage
56293       ** is still writeable */
56294       assert( pPrior<pPage->aData || pPrior>=&pPage->aData[pBt->pageSize]
56295             || sqlite3PagerIswriteable(pPage->pDbPage) );
56296 
56297       put4byte(pPrior, pgnoOvfl);
56298       releasePage(pToRelease);
56299       pToRelease = pOvfl;
56300       pPrior = pOvfl->aData;
56301       put4byte(pPrior, 0);
56302       pPayload = &pOvfl->aData[4];
56303       spaceLeft = pBt->usableSize - 4;
56304     }
56305     n = nPayload;
56306     if( n>spaceLeft ) n = spaceLeft;
56307 
56308     /* If pToRelease is not zero than pPayload points into the data area
56309     ** of pToRelease.  Make sure pToRelease is still writeable. */
56310     assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) );
56311 
56312     /* If pPayload is part of the data area of pPage, then make sure pPage
56313     ** is still writeable */
56314     assert( pPayload<pPage->aData || pPayload>=&pPage->aData[pBt->pageSize]
56315             || sqlite3PagerIswriteable(pPage->pDbPage) );
56316 
56317     if( nSrc>0 ){
56318       if( n>nSrc ) n = nSrc;
56319       assert( pSrc );
56320       memcpy(pPayload, pSrc, n);
56321     }else{
56322       memset(pPayload, 0, n);
56323     }
56324     nPayload -= n;
56325     pPayload += n;
56326     pSrc += n;
56327     nSrc -= n;
56328     spaceLeft -= n;
56329     if( nSrc==0 ){
56330       nSrc = nData;
56331       pSrc = pData;
56332     }
56333   }
56334   releasePage(pToRelease);
56335   return SQLITE_OK;
56336 }
56337 
56338 /*
56339 ** Remove the i-th cell from pPage.  This routine effects pPage only.
56340 ** The cell content is not freed or deallocated.  It is assumed that
56341 ** the cell content has been copied someplace else.  This routine just
56342 ** removes the reference to the cell from pPage.
56343 **
56344 ** "sz" must be the number of bytes in the cell.
56345 */
56346 static void dropCell(MemPage *pPage, int idx, int sz, int *pRC){
56347   u32 pc;         /* Offset to cell content of cell being deleted */
56348   u8 *data;       /* pPage->aData */
56349   u8 *ptr;        /* Used to move bytes around within data[] */
56350   int rc;         /* The return code */
56351   int hdr;        /* Beginning of the header.  0 most pages.  100 page 1 */
56352 
56353   if( *pRC ) return;
56354 
56355   assert( idx>=0 && idx<pPage->nCell );
56356   assert( sz==cellSize(pPage, idx) );
56357   assert( sqlite3PagerIswriteable(pPage->pDbPage) );
56358   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
56359   data = pPage->aData;
56360   ptr = &pPage->aCellIdx[2*idx];
56361   pc = get2byte(ptr);
56362   hdr = pPage->hdrOffset;
56363   testcase( pc==get2byte(&data[hdr+5]) );
56364   testcase( pc+sz==pPage->pBt->usableSize );
56365   if( pc < (u32)get2byte(&data[hdr+5]) || pc+sz > pPage->pBt->usableSize ){
56366     *pRC = SQLITE_CORRUPT_BKPT;
56367     return;
56368   }
56369   rc = freeSpace(pPage, pc, sz);
56370   if( rc ){
56371     *pRC = rc;
56372     return;
56373   }
56374   pPage->nCell--;
56375   memmove(ptr, ptr+2, 2*(pPage->nCell - idx));
56376   put2byte(&data[hdr+3], pPage->nCell);
56377   pPage->nFree += 2;
56378 }
56379 
56380 /*
56381 ** Insert a new cell on pPage at cell index "i".  pCell points to the
56382 ** content of the cell.
56383 **
56384 ** If the cell content will fit on the page, then put it there.  If it
56385 ** will not fit, then make a copy of the cell content into pTemp if
56386 ** pTemp is not null.  Regardless of pTemp, allocate a new entry
56387 ** in pPage->apOvfl[] and make it point to the cell content (either
56388 ** in pTemp or the original pCell) and also record its index.
56389 ** Allocating a new entry in pPage->aCell[] implies that
56390 ** pPage->nOverflow is incremented.
56391 **
56392 ** If nSkip is non-zero, then do not copy the first nSkip bytes of the
56393 ** cell. The caller will overwrite them after this function returns. If
56394 ** nSkip is non-zero, then pCell may not point to an invalid memory location
56395 ** (but pCell+nSkip is always valid).
56396 */
56397 static void insertCell(
56398   MemPage *pPage,   /* Page into which we are copying */
56399   int i,            /* New cell becomes the i-th cell of the page */
56400   u8 *pCell,        /* Content of the new cell */
56401   int sz,           /* Bytes of content in pCell */
56402   u8 *pTemp,        /* Temp storage space for pCell, if needed */
56403   Pgno iChild,      /* If non-zero, replace first 4 bytes with this value */
56404   int *pRC          /* Read and write return code from here */
56405 ){
56406   int idx = 0;      /* Where to write new cell content in data[] */
56407   int j;            /* Loop counter */
56408   int end;          /* First byte past the last cell pointer in data[] */
56409   int ins;          /* Index in data[] where new cell pointer is inserted */
56410   int cellOffset;   /* Address of first cell pointer in data[] */
56411   u8 *data;         /* The content of the whole page */
56412   int nSkip = (iChild ? 4 : 0);
56413 
56414   if( *pRC ) return;
56415 
56416   assert( i>=0 && i<=pPage->nCell+pPage->nOverflow );
56417   assert( pPage->nCell<=MX_CELL(pPage->pBt) && MX_CELL(pPage->pBt)<=10921 );
56418   assert( pPage->nOverflow<=ArraySize(pPage->apOvfl) );
56419   assert( ArraySize(pPage->apOvfl)==ArraySize(pPage->aiOvfl) );
56420   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
56421   /* The cell should normally be sized correctly.  However, when moving a
56422   ** malformed cell from a leaf page to an interior page, if the cell size
56423   ** wanted to be less than 4 but got rounded up to 4 on the leaf, then size
56424   ** might be less than 8 (leaf-size + pointer) on the interior node.  Hence
56425   ** the term after the || in the following assert(). */
56426   assert( sz==cellSizePtr(pPage, pCell) || (sz==8 && iChild>0) );
56427   if( pPage->nOverflow || sz+2>pPage->nFree ){
56428     if( pTemp ){
56429       memcpy(pTemp+nSkip, pCell+nSkip, sz-nSkip);
56430       pCell = pTemp;
56431     }
56432     if( iChild ){
56433       put4byte(pCell, iChild);
56434     }
56435     j = pPage->nOverflow++;
56436     assert( j<(int)(sizeof(pPage->apOvfl)/sizeof(pPage->apOvfl[0])) );
56437     pPage->apOvfl[j] = pCell;
56438     pPage->aiOvfl[j] = (u16)i;
56439   }else{
56440     int rc = sqlite3PagerWrite(pPage->pDbPage);
56441     if( rc!=SQLITE_OK ){
56442       *pRC = rc;
56443       return;
56444     }
56445     assert( sqlite3PagerIswriteable(pPage->pDbPage) );
56446     data = pPage->aData;
56447     cellOffset = pPage->cellOffset;
56448     end = cellOffset + 2*pPage->nCell;
56449     ins = cellOffset + 2*i;
56450     rc = allocateSpace(pPage, sz, &idx);
56451     if( rc ){ *pRC = rc; return; }
56452     /* The allocateSpace() routine guarantees the following two properties
56453     ** if it returns success */
56454     assert( idx >= end+2 );
56455     assert( idx+sz <= (int)pPage->pBt->usableSize );
56456     pPage->nCell++;
56457     pPage->nFree -= (u16)(2 + sz);
56458     memcpy(&data[idx+nSkip], pCell+nSkip, sz-nSkip);
56459     if( iChild ){
56460       put4byte(&data[idx], iChild);
56461     }
56462     memmove(&data[ins+2], &data[ins], end-ins);
56463     put2byte(&data[ins], idx);
56464     put2byte(&data[pPage->hdrOffset+3], pPage->nCell);
56465 #ifndef SQLITE_OMIT_AUTOVACUUM
56466     if( pPage->pBt->autoVacuum ){
56467       /* The cell may contain a pointer to an overflow page. If so, write
56468       ** the entry for the overflow page into the pointer map.
56469       */
56470       ptrmapPutOvflPtr(pPage, pCell, pRC);
56471     }
56472 #endif
56473   }
56474 }
56475 
56476 /*
56477 ** Add a list of cells to a page.  The page should be initially empty.
56478 ** The cells are guaranteed to fit on the page.
56479 */
56480 static void assemblePage(
56481   MemPage *pPage,   /* The page to be assemblied */
56482   int nCell,        /* The number of cells to add to this page */
56483   u8 **apCell,      /* Pointers to cell bodies */
56484   u16 *aSize        /* Sizes of the cells */
56485 ){
56486   int i;            /* Loop counter */
56487   u8 *pCellptr;     /* Address of next cell pointer */
56488   int cellbody;     /* Address of next cell body */
56489   u8 * const data = pPage->aData;             /* Pointer to data for pPage */
56490   const int hdr = pPage->hdrOffset;           /* Offset of header on pPage */
56491   const int nUsable = pPage->pBt->usableSize; /* Usable size of page */
56492 
56493   assert( pPage->nOverflow==0 );
56494   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
56495   assert( nCell>=0 && nCell<=(int)MX_CELL(pPage->pBt)
56496             && (int)MX_CELL(pPage->pBt)<=10921);
56497   assert( sqlite3PagerIswriteable(pPage->pDbPage) );
56498 
56499   /* Check that the page has just been zeroed by zeroPage() */
56500   assert( pPage->nCell==0 );
56501   assert( get2byteNotZero(&data[hdr+5])==nUsable );
56502 
56503   pCellptr = &pPage->aCellIdx[nCell*2];
56504   cellbody = nUsable;
56505   for(i=nCell-1; i>=0; i--){
56506     u16 sz = aSize[i];
56507     pCellptr -= 2;
56508     cellbody -= sz;
56509     put2byte(pCellptr, cellbody);
56510     memcpy(&data[cellbody], apCell[i], sz);
56511   }
56512   put2byte(&data[hdr+3], nCell);
56513   put2byte(&data[hdr+5], cellbody);
56514   pPage->nFree -= (nCell*2 + nUsable - cellbody);
56515   pPage->nCell = (u16)nCell;
56516 }
56517 
56518 /*
56519 ** The following parameters determine how many adjacent pages get involved
56520 ** in a balancing operation.  NN is the number of neighbors on either side
56521 ** of the page that participate in the balancing operation.  NB is the
56522 ** total number of pages that participate, including the target page and
56523 ** NN neighbors on either side.
56524 **
56525 ** The minimum value of NN is 1 (of course).  Increasing NN above 1
56526 ** (to 2 or 3) gives a modest improvement in SELECT and DELETE performance
56527 ** in exchange for a larger degradation in INSERT and UPDATE performance.
56528 ** The value of NN appears to give the best results overall.
56529 */
56530 #define NN 1             /* Number of neighbors on either side of pPage */
56531 #define NB (NN*2+1)      /* Total pages involved in the balance */
56532 
56533 
56534 #ifndef SQLITE_OMIT_QUICKBALANCE
56535 /*
56536 ** This version of balance() handles the common special case where
56537 ** a new entry is being inserted on the extreme right-end of the
56538 ** tree, in other words, when the new entry will become the largest
56539 ** entry in the tree.
56540 **
56541 ** Instead of trying to balance the 3 right-most leaf pages, just add
56542 ** a new page to the right-hand side and put the one new entry in
56543 ** that page.  This leaves the right side of the tree somewhat
56544 ** unbalanced.  But odds are that we will be inserting new entries
56545 ** at the end soon afterwards so the nearly empty page will quickly
56546 ** fill up.  On average.
56547 **
56548 ** pPage is the leaf page which is the right-most page in the tree.
56549 ** pParent is its parent.  pPage must have a single overflow entry
56550 ** which is also the right-most entry on the page.
56551 **
56552 ** The pSpace buffer is used to store a temporary copy of the divider
56553 ** cell that will be inserted into pParent. Such a cell consists of a 4
56554 ** byte page number followed by a variable length integer. In other
56555 ** words, at most 13 bytes. Hence the pSpace buffer must be at
56556 ** least 13 bytes in size.
56557 */
56558 static int balance_quick(MemPage *pParent, MemPage *pPage, u8 *pSpace){
56559   BtShared *const pBt = pPage->pBt;    /* B-Tree Database */
56560   MemPage *pNew;                       /* Newly allocated page */
56561   int rc;                              /* Return Code */
56562   Pgno pgnoNew;                        /* Page number of pNew */
56563 
56564   assert( sqlite3_mutex_held(pPage->pBt->mutex) );
56565   assert( sqlite3PagerIswriteable(pParent->pDbPage) );
56566   assert( pPage->nOverflow==1 );
56567 
56568   /* This error condition is now caught prior to reaching this function */
56569   if( pPage->nCell==0 ) return SQLITE_CORRUPT_BKPT;
56570 
56571   /* Allocate a new page. This page will become the right-sibling of
56572   ** pPage. Make the parent page writable, so that the new divider cell
56573   ** may be inserted. If both these operations are successful, proceed.
56574   */
56575   rc = allocateBtreePage(pBt, &pNew, &pgnoNew, 0, 0);
56576 
56577   if( rc==SQLITE_OK ){
56578 
56579     u8 *pOut = &pSpace[4];
56580     u8 *pCell = pPage->apOvfl[0];
56581     u16 szCell = cellSizePtr(pPage, pCell);
56582     u8 *pStop;
56583 
56584     assert( sqlite3PagerIswriteable(pNew->pDbPage) );
56585     assert( pPage->aData[0]==(PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF) );
56586     zeroPage(pNew, PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF);
56587     assemblePage(pNew, 1, &pCell, &szCell);
56588 
56589     /* If this is an auto-vacuum database, update the pointer map
56590     ** with entries for the new page, and any pointer from the
56591     ** cell on the page to an overflow page. If either of these
56592     ** operations fails, the return code is set, but the contents
56593     ** of the parent page are still manipulated by thh code below.
56594     ** That is Ok, at this point the parent page is guaranteed to
56595     ** be marked as dirty. Returning an error code will cause a
56596     ** rollback, undoing any changes made to the parent page.
56597     */
56598     if( ISAUTOVACUUM ){
56599       ptrmapPut(pBt, pgnoNew, PTRMAP_BTREE, pParent->pgno, &rc);
56600       if( szCell>pNew->minLocal ){
56601         ptrmapPutOvflPtr(pNew, pCell, &rc);
56602       }
56603     }
56604 
56605     /* Create a divider cell to insert into pParent. The divider cell
56606     ** consists of a 4-byte page number (the page number of pPage) and
56607     ** a variable length key value (which must be the same value as the
56608     ** largest key on pPage).
56609     **
56610     ** To find the largest key value on pPage, first find the right-most
56611     ** cell on pPage. The first two fields of this cell are the
56612     ** record-length (a variable length integer at most 32-bits in size)
56613     ** and the key value (a variable length integer, may have any value).
56614     ** The first of the while(...) loops below skips over the record-length
56615     ** field. The second while(...) loop copies the key value from the
56616     ** cell on pPage into the pSpace buffer.
56617     */
56618     pCell = findCell(pPage, pPage->nCell-1);
56619     pStop = &pCell[9];
56620     while( (*(pCell++)&0x80) && pCell<pStop );
56621     pStop = &pCell[9];
56622     while( ((*(pOut++) = *(pCell++))&0x80) && pCell<pStop );
56623 
56624     /* Insert the new divider cell into pParent. */
56625     insertCell(pParent, pParent->nCell, pSpace, (int)(pOut-pSpace),
56626                0, pPage->pgno, &rc);
56627 
56628     /* Set the right-child pointer of pParent to point to the new page. */
56629     put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew);
56630 
56631     /* Release the reference to the new page. */
56632     releasePage(pNew);
56633   }
56634 
56635   return rc;
56636 }
56637 #endif /* SQLITE_OMIT_QUICKBALANCE */
56638 
56639 #if 0
56640 /*
56641 ** This function does not contribute anything to the operation of SQLite.
56642 ** it is sometimes activated temporarily while debugging code responsible
56643 ** for setting pointer-map entries.
56644 */
56645 static int ptrmapCheckPages(MemPage **apPage, int nPage){
56646   int i, j;
56647   for(i=0; i<nPage; i++){
56648     Pgno n;
56649     u8 e;
56650     MemPage *pPage = apPage[i];
56651     BtShared *pBt = pPage->pBt;
56652     assert( pPage->isInit );
56653 
56654     for(j=0; j<pPage->nCell; j++){
56655       CellInfo info;
56656       u8 *z;
56657 
56658       z = findCell(pPage, j);
56659       btreeParseCellPtr(pPage, z, &info);
56660       if( info.iOverflow ){
56661         Pgno ovfl = get4byte(&z[info.iOverflow]);
56662         ptrmapGet(pBt, ovfl, &e, &n);
56663         assert( n==pPage->pgno && e==PTRMAP_OVERFLOW1 );
56664       }
56665       if( !pPage->leaf ){
56666         Pgno child = get4byte(z);
56667         ptrmapGet(pBt, child, &e, &n);
56668         assert( n==pPage->pgno && e==PTRMAP_BTREE );
56669       }
56670     }
56671     if( !pPage->leaf ){
56672       Pgno child = get4byte(&pPage->aData[pPage->hdrOffset+8]);
56673       ptrmapGet(pBt, child, &e, &n);
56674       assert( n==pPage->pgno && e==PTRMAP_BTREE );
56675     }
56676   }
56677   return 1;
56678 }
56679 #endif
56680 
56681 /*
56682 ** This function is used to copy the contents of the b-tree node stored
56683 ** on page pFrom to page pTo. If page pFrom was not a leaf page, then
56684 ** the pointer-map entries for each child page are updated so that the
56685 ** parent page stored in the pointer map is page pTo. If pFrom contained
56686 ** any cells with overflow page pointers, then the corresponding pointer
56687 ** map entries are also updated so that the parent page is page pTo.
56688 **
56689 ** If pFrom is currently carrying any overflow cells (entries in the
56690 ** MemPage.apOvfl[] array), they are not copied to pTo.
56691 **
56692 ** Before returning, page pTo is reinitialized using btreeInitPage().
56693 **
56694 ** The performance of this function is not critical. It is only used by
56695 ** the balance_shallower() and balance_deeper() procedures, neither of
56696 ** which are called often under normal circumstances.
56697 */
56698 static void copyNodeContent(MemPage *pFrom, MemPage *pTo, int *pRC){
56699   if( (*pRC)==SQLITE_OK ){
56700     BtShared * const pBt = pFrom->pBt;
56701     u8 * const aFrom = pFrom->aData;
56702     u8 * const aTo = pTo->aData;
56703     int const iFromHdr = pFrom->hdrOffset;
56704     int const iToHdr = ((pTo->pgno==1) ? 100 : 0);
56705     int rc;
56706     int iData;
56707 
56708 
56709     assert( pFrom->isInit );
56710     assert( pFrom->nFree>=iToHdr );
56711     assert( get2byte(&aFrom[iFromHdr+5]) <= (int)pBt->usableSize );
56712 
56713     /* Copy the b-tree node content from page pFrom to page pTo. */
56714     iData = get2byte(&aFrom[iFromHdr+5]);
56715     memcpy(&aTo[iData], &aFrom[iData], pBt->usableSize-iData);
56716     memcpy(&aTo[iToHdr], &aFrom[iFromHdr], pFrom->cellOffset + 2*pFrom->nCell);
56717 
56718     /* Reinitialize page pTo so that the contents of the MemPage structure
56719     ** match the new data. The initialization of pTo can actually fail under
56720     ** fairly obscure circumstances, even though it is a copy of initialized
56721     ** page pFrom.
56722     */
56723     pTo->isInit = 0;
56724     rc = btreeInitPage(pTo);
56725     if( rc!=SQLITE_OK ){
56726       *pRC = rc;
56727       return;
56728     }
56729 
56730     /* If this is an auto-vacuum database, update the pointer-map entries
56731     ** for any b-tree or overflow pages that pTo now contains the pointers to.
56732     */
56733     if( ISAUTOVACUUM ){
56734       *pRC = setChildPtrmaps(pTo);
56735     }
56736   }
56737 }
56738 
56739 /*
56740 ** This routine redistributes cells on the iParentIdx'th child of pParent
56741 ** (hereafter "the page") and up to 2 siblings so that all pages have about the
56742 ** same amount of free space. Usually a single sibling on either side of the
56743 ** page are used in the balancing, though both siblings might come from one
56744 ** side if the page is the first or last child of its parent. If the page
56745 ** has fewer than 2 siblings (something which can only happen if the page
56746 ** is a root page or a child of a root page) then all available siblings
56747 ** participate in the balancing.
56748 **
56749 ** The number of siblings of the page might be increased or decreased by
56750 ** one or two in an effort to keep pages nearly full but not over full.
56751 **
56752 ** Note that when this routine is called, some of the cells on the page
56753 ** might not actually be stored in MemPage.aData[]. This can happen
56754 ** if the page is overfull. This routine ensures that all cells allocated
56755 ** to the page and its siblings fit into MemPage.aData[] before returning.
56756 **
56757 ** In the course of balancing the page and its siblings, cells may be
56758 ** inserted into or removed from the parent page (pParent). Doing so
56759 ** may cause the parent page to become overfull or underfull. If this
56760 ** happens, it is the responsibility of the caller to invoke the correct
56761 ** balancing routine to fix this problem (see the balance() routine).
56762 **
56763 ** If this routine fails for any reason, it might leave the database
56764 ** in a corrupted state. So if this routine fails, the database should
56765 ** be rolled back.
56766 **
56767 ** The third argument to this function, aOvflSpace, is a pointer to a
56768 ** buffer big enough to hold one page. If while inserting cells into the parent
56769 ** page (pParent) the parent page becomes overfull, this buffer is
56770 ** used to store the parent's overflow cells. Because this function inserts
56771 ** a maximum of four divider cells into the parent page, and the maximum
56772 ** size of a cell stored within an internal node is always less than 1/4
56773 ** of the page-size, the aOvflSpace[] buffer is guaranteed to be large
56774 ** enough for all overflow cells.
56775 **
56776 ** If aOvflSpace is set to a null pointer, this function returns
56777 ** SQLITE_NOMEM.
56778 */
56779 #if defined(_MSC_VER) && _MSC_VER >= 1700 && defined(_M_ARM)
56780 #pragma optimize("", off)
56781 #endif
56782 static int balance_nonroot(
56783   MemPage *pParent,               /* Parent page of siblings being balanced */
56784   int iParentIdx,                 /* Index of "the page" in pParent */
56785   u8 *aOvflSpace,                 /* page-size bytes of space for parent ovfl */
56786   int isRoot,                     /* True if pParent is a root-page */
56787   int bBulk                       /* True if this call is part of a bulk load */
56788 ){
56789   BtShared *pBt;               /* The whole database */
56790   int nCell = 0;               /* Number of cells in apCell[] */
56791   int nMaxCells = 0;           /* Allocated size of apCell, szCell, aFrom. */
56792   int nNew = 0;                /* Number of pages in apNew[] */
56793   int nOld;                    /* Number of pages in apOld[] */
56794   int i, j, k;                 /* Loop counters */
56795   int nxDiv;                   /* Next divider slot in pParent->aCell[] */
56796   int rc = SQLITE_OK;          /* The return code */
56797   u16 leafCorrection;          /* 4 if pPage is a leaf.  0 if not */
56798   int leafData;                /* True if pPage is a leaf of a LEAFDATA tree */
56799   int usableSpace;             /* Bytes in pPage beyond the header */
56800   int pageFlags;               /* Value of pPage->aData[0] */
56801   int subtotal;                /* Subtotal of bytes in cells on one page */
56802   int iSpace1 = 0;             /* First unused byte of aSpace1[] */
56803   int iOvflSpace = 0;          /* First unused byte of aOvflSpace[] */
56804   int szScratch;               /* Size of scratch memory requested */
56805   MemPage *apOld[NB];          /* pPage and up to two siblings */
56806   MemPage *apCopy[NB];         /* Private copies of apOld[] pages */
56807   MemPage *apNew[NB+2];        /* pPage and up to NB siblings after balancing */
56808   u8 *pRight;                  /* Location in parent of right-sibling pointer */
56809   u8 *apDiv[NB-1];             /* Divider cells in pParent */
56810   int cntNew[NB+2];            /* Index in aCell[] of cell after i-th page */
56811   int szNew[NB+2];             /* Combined size of cells place on i-th page */
56812   u8 **apCell = 0;             /* All cells begin balanced */
56813   u16 *szCell;                 /* Local size of all cells in apCell[] */
56814   u8 *aSpace1;                 /* Space for copies of dividers cells */
56815   Pgno pgno;                   /* Temp var to store a page number in */
56816 
56817   pBt = pParent->pBt;
56818   assert( sqlite3_mutex_held(pBt->mutex) );
56819   assert( sqlite3PagerIswriteable(pParent->pDbPage) );
56820 
56821 #if 0
56822   TRACE(("BALANCE: begin page %d child of %d\n", pPage->pgno, pParent->pgno));
56823 #endif
56824 
56825   /* At this point pParent may have at most one overflow cell. And if
56826   ** this overflow cell is present, it must be the cell with
56827   ** index iParentIdx. This scenario comes about when this function
56828   ** is called (indirectly) from sqlite3BtreeDelete().
56829   */
56830   assert( pParent->nOverflow==0 || pParent->nOverflow==1 );
56831   assert( pParent->nOverflow==0 || pParent->aiOvfl[0]==iParentIdx );
56832 
56833   if( !aOvflSpace ){
56834     return SQLITE_NOMEM;
56835   }
56836 
56837   /* Find the sibling pages to balance. Also locate the cells in pParent
56838   ** that divide the siblings. An attempt is made to find NN siblings on
56839   ** either side of pPage. More siblings are taken from one side, however,
56840   ** if there are fewer than NN siblings on the other side. If pParent
56841   ** has NB or fewer children then all children of pParent are taken.
56842   **
56843   ** This loop also drops the divider cells from the parent page. This
56844   ** way, the remainder of the function does not have to deal with any
56845   ** overflow cells in the parent page, since if any existed they will
56846   ** have already been removed.
56847   */
56848   i = pParent->nOverflow + pParent->nCell;
56849   if( i<2 ){
56850     nxDiv = 0;
56851   }else{
56852     assert( bBulk==0 || bBulk==1 );
56853     if( iParentIdx==0 ){
56854       nxDiv = 0;
56855     }else if( iParentIdx==i ){
56856       nxDiv = i-2+bBulk;
56857     }else{
56858       assert( bBulk==0 );
56859       nxDiv = iParentIdx-1;
56860     }
56861     i = 2-bBulk;
56862   }
56863   nOld = i+1;
56864   if( (i+nxDiv-pParent->nOverflow)==pParent->nCell ){
56865     pRight = &pParent->aData[pParent->hdrOffset+8];
56866   }else{
56867     pRight = findCell(pParent, i+nxDiv-pParent->nOverflow);
56868   }
56869   pgno = get4byte(pRight);
56870   while( 1 ){
56871     rc = getAndInitPage(pBt, pgno, &apOld[i], 0);
56872     if( rc ){
56873       memset(apOld, 0, (i+1)*sizeof(MemPage*));
56874       goto balance_cleanup;
56875     }
56876     nMaxCells += 1+apOld[i]->nCell+apOld[i]->nOverflow;
56877     if( (i--)==0 ) break;
56878 
56879     if( i+nxDiv==pParent->aiOvfl[0] && pParent->nOverflow ){
56880       apDiv[i] = pParent->apOvfl[0];
56881       pgno = get4byte(apDiv[i]);
56882       szNew[i] = cellSizePtr(pParent, apDiv[i]);
56883       pParent->nOverflow = 0;
56884     }else{
56885       apDiv[i] = findCell(pParent, i+nxDiv-pParent->nOverflow);
56886       pgno = get4byte(apDiv[i]);
56887       szNew[i] = cellSizePtr(pParent, apDiv[i]);
56888 
56889       /* Drop the cell from the parent page. apDiv[i] still points to
56890       ** the cell within the parent, even though it has been dropped.
56891       ** This is safe because dropping a cell only overwrites the first
56892       ** four bytes of it, and this function does not need the first
56893       ** four bytes of the divider cell. So the pointer is safe to use
56894       ** later on.
56895       **
56896       ** But not if we are in secure-delete mode. In secure-delete mode,
56897       ** the dropCell() routine will overwrite the entire cell with zeroes.
56898       ** In this case, temporarily copy the cell into the aOvflSpace[]
56899       ** buffer. It will be copied out again as soon as the aSpace[] buffer
56900       ** is allocated.  */
56901       if( pBt->btsFlags & BTS_SECURE_DELETE ){
56902         int iOff;
56903 
56904         iOff = SQLITE_PTR_TO_INT(apDiv[i]) - SQLITE_PTR_TO_INT(pParent->aData);
56905         if( (iOff+szNew[i])>(int)pBt->usableSize ){
56906           rc = SQLITE_CORRUPT_BKPT;
56907           memset(apOld, 0, (i+1)*sizeof(MemPage*));
56908           goto balance_cleanup;
56909         }else{
56910           memcpy(&aOvflSpace[iOff], apDiv[i], szNew[i]);
56911           apDiv[i] = &aOvflSpace[apDiv[i]-pParent->aData];
56912         }
56913       }
56914       dropCell(pParent, i+nxDiv-pParent->nOverflow, szNew[i], &rc);
56915     }
56916   }
56917 
56918   /* Make nMaxCells a multiple of 4 in order to preserve 8-byte
56919   ** alignment */
56920   nMaxCells = (nMaxCells + 3)&~3;
56921 
56922   /*
56923   ** Allocate space for memory structures
56924   */
56925   k = pBt->pageSize + ROUND8(sizeof(MemPage));
56926   szScratch =
56927        nMaxCells*sizeof(u8*)                       /* apCell */
56928      + nMaxCells*sizeof(u16)                       /* szCell */
56929      + pBt->pageSize                               /* aSpace1 */
56930      + k*nOld;                                     /* Page copies (apCopy) */
56931   apCell = sqlite3ScratchMalloc( szScratch );
56932   if( apCell==0 ){
56933     rc = SQLITE_NOMEM;
56934     goto balance_cleanup;
56935   }
56936   szCell = (u16*)&apCell[nMaxCells];
56937   aSpace1 = (u8*)&szCell[nMaxCells];
56938   assert( EIGHT_BYTE_ALIGNMENT(aSpace1) );
56939 
56940   /*
56941   ** Load pointers to all cells on sibling pages and the divider cells
56942   ** into the local apCell[] array.  Make copies of the divider cells
56943   ** into space obtained from aSpace1[] and remove the divider cells
56944   ** from pParent.
56945   **
56946   ** If the siblings are on leaf pages, then the child pointers of the
56947   ** divider cells are stripped from the cells before they are copied
56948   ** into aSpace1[].  In this way, all cells in apCell[] are without
56949   ** child pointers.  If siblings are not leaves, then all cell in
56950   ** apCell[] include child pointers.  Either way, all cells in apCell[]
56951   ** are alike.
56952   **
56953   ** leafCorrection:  4 if pPage is a leaf.  0 if pPage is not a leaf.
56954   **       leafData:  1 if pPage holds key+data and pParent holds only keys.
56955   */
56956   leafCorrection = apOld[0]->leaf*4;
56957   leafData = apOld[0]->hasData;
56958   for(i=0; i<nOld; i++){
56959     int limit;
56960 
56961     /* Before doing anything else, take a copy of the i'th original sibling
56962     ** The rest of this function will use data from the copies rather
56963     ** that the original pages since the original pages will be in the
56964     ** process of being overwritten.  */
56965     MemPage *pOld = apCopy[i] = (MemPage*)&aSpace1[pBt->pageSize + k*i];
56966     memcpy(pOld, apOld[i], sizeof(MemPage));
56967     pOld->aData = (void*)&pOld[1];
56968     memcpy(pOld->aData, apOld[i]->aData, pBt->pageSize);
56969 
56970     limit = pOld->nCell+pOld->nOverflow;
56971     if( pOld->nOverflow>0 ){
56972       for(j=0; j<limit; j++){
56973         assert( nCell<nMaxCells );
56974         apCell[nCell] = findOverflowCell(pOld, j);
56975         szCell[nCell] = cellSizePtr(pOld, apCell[nCell]);
56976         nCell++;
56977       }
56978     }else{
56979       u8 *aData = pOld->aData;
56980       u16 maskPage = pOld->maskPage;
56981       u16 cellOffset = pOld->cellOffset;
56982       for(j=0; j<limit; j++){
56983         assert( nCell<nMaxCells );
56984         apCell[nCell] = findCellv2(aData, maskPage, cellOffset, j);
56985         szCell[nCell] = cellSizePtr(pOld, apCell[nCell]);
56986         nCell++;
56987       }
56988     }
56989     if( i<nOld-1 && !leafData){
56990       u16 sz = (u16)szNew[i];
56991       u8 *pTemp;
56992       assert( nCell<nMaxCells );
56993       szCell[nCell] = sz;
56994       pTemp = &aSpace1[iSpace1];
56995       iSpace1 += sz;
56996       assert( sz<=pBt->maxLocal+23 );
56997       assert( iSpace1 <= (int)pBt->pageSize );
56998       memcpy(pTemp, apDiv[i], sz);
56999       apCell[nCell] = pTemp+leafCorrection;
57000       assert( leafCorrection==0 || leafCorrection==4 );
57001       szCell[nCell] = szCell[nCell] - leafCorrection;
57002       if( !pOld->leaf ){
57003         assert( leafCorrection==0 );
57004         assert( pOld->hdrOffset==0 );
57005         /* The right pointer of the child page pOld becomes the left
57006         ** pointer of the divider cell */
57007         memcpy(apCell[nCell], &pOld->aData[8], 4);
57008       }else{
57009         assert( leafCorrection==4 );
57010         if( szCell[nCell]<4 ){
57011           /* Do not allow any cells smaller than 4 bytes. */
57012           szCell[nCell] = 4;
57013         }
57014       }
57015       nCell++;
57016     }
57017   }
57018 
57019   /*
57020   ** Figure out the number of pages needed to hold all nCell cells.
57021   ** Store this number in "k".  Also compute szNew[] which is the total
57022   ** size of all cells on the i-th page and cntNew[] which is the index
57023   ** in apCell[] of the cell that divides page i from page i+1.
57024   ** cntNew[k] should equal nCell.
57025   **
57026   ** Values computed by this block:
57027   **
57028   **           k: The total number of sibling pages
57029   **    szNew[i]: Spaced used on the i-th sibling page.
57030   **   cntNew[i]: Index in apCell[] and szCell[] for the first cell to
57031   **              the right of the i-th sibling page.
57032   ** usableSpace: Number of bytes of space available on each sibling.
57033   **
57034   */
57035   usableSpace = pBt->usableSize - 12 + leafCorrection;
57036   for(subtotal=k=i=0; i<nCell; i++){
57037     assert( i<nMaxCells );
57038     subtotal += szCell[i] + 2;
57039     if( subtotal > usableSpace ){
57040       szNew[k] = subtotal - szCell[i];
57041       cntNew[k] = i;
57042       if( leafData ){ i--; }
57043       subtotal = 0;
57044       k++;
57045       if( k>NB+1 ){ rc = SQLITE_CORRUPT_BKPT; goto balance_cleanup; }
57046     }
57047   }
57048   szNew[k] = subtotal;
57049   cntNew[k] = nCell;
57050   k++;
57051 
57052   /*
57053   ** The packing computed by the previous block is biased toward the siblings
57054   ** on the left side.  The left siblings are always nearly full, while the
57055   ** right-most sibling might be nearly empty.  This block of code attempts
57056   ** to adjust the packing of siblings to get a better balance.
57057   **
57058   ** This adjustment is more than an optimization.  The packing above might
57059   ** be so out of balance as to be illegal.  For example, the right-most
57060   ** sibling might be completely empty.  This adjustment is not optional.
57061   */
57062   for(i=k-1; i>0; i--){
57063     int szRight = szNew[i];  /* Size of sibling on the right */
57064     int szLeft = szNew[i-1]; /* Size of sibling on the left */
57065     int r;              /* Index of right-most cell in left sibling */
57066     int d;              /* Index of first cell to the left of right sibling */
57067 
57068     r = cntNew[i-1] - 1;
57069     d = r + 1 - leafData;
57070     assert( d<nMaxCells );
57071     assert( r<nMaxCells );
57072     while( szRight==0
57073        || (!bBulk && szRight+szCell[d]+2<=szLeft-(szCell[r]+2))
57074     ){
57075       szRight += szCell[d] + 2;
57076       szLeft -= szCell[r] + 2;
57077       cntNew[i-1]--;
57078       r = cntNew[i-1] - 1;
57079       d = r + 1 - leafData;
57080     }
57081     szNew[i] = szRight;
57082     szNew[i-1] = szLeft;
57083   }
57084 
57085   /* Either we found one or more cells (cntnew[0])>0) or pPage is
57086   ** a virtual root page.  A virtual root page is when the real root
57087   ** page is page 1 and we are the only child of that page.
57088   **
57089   ** UPDATE:  The assert() below is not necessarily true if the database
57090   ** file is corrupt.  The corruption will be detected and reported later
57091   ** in this procedure so there is no need to act upon it now.
57092   */
57093 #if 0
57094   assert( cntNew[0]>0 || (pParent->pgno==1 && pParent->nCell==0) );
57095 #endif
57096 
57097   TRACE(("BALANCE: old: %d %d %d  ",
57098     apOld[0]->pgno,
57099     nOld>=2 ? apOld[1]->pgno : 0,
57100     nOld>=3 ? apOld[2]->pgno : 0
57101   ));
57102 
57103   /*
57104   ** Allocate k new pages.  Reuse old pages where possible.
57105   */
57106   if( apOld[0]->pgno<=1 ){
57107     rc = SQLITE_CORRUPT_BKPT;
57108     goto balance_cleanup;
57109   }
57110   pageFlags = apOld[0]->aData[0];
57111   for(i=0; i<k; i++){
57112     MemPage *pNew;
57113     if( i<nOld ){
57114       pNew = apNew[i] = apOld[i];
57115       apOld[i] = 0;
57116       rc = sqlite3PagerWrite(pNew->pDbPage);
57117       nNew++;
57118       if( rc ) goto balance_cleanup;
57119     }else{
57120       assert( i>0 );
57121       rc = allocateBtreePage(pBt, &pNew, &pgno, (bBulk ? 1 : pgno), 0);
57122       if( rc ) goto balance_cleanup;
57123       apNew[i] = pNew;
57124       nNew++;
57125 
57126       /* Set the pointer-map entry for the new sibling page. */
57127       if( ISAUTOVACUUM ){
57128         ptrmapPut(pBt, pNew->pgno, PTRMAP_BTREE, pParent->pgno, &rc);
57129         if( rc!=SQLITE_OK ){
57130           goto balance_cleanup;
57131         }
57132       }
57133     }
57134   }
57135 
57136   /* Free any old pages that were not reused as new pages.
57137   */
57138   while( i<nOld ){
57139     freePage(apOld[i], &rc);
57140     if( rc ) goto balance_cleanup;
57141     releasePage(apOld[i]);
57142     apOld[i] = 0;
57143     i++;
57144   }
57145 
57146   /*
57147   ** Put the new pages in accending order.  This helps to
57148   ** keep entries in the disk file in order so that a scan
57149   ** of the table is a linear scan through the file.  That
57150   ** in turn helps the operating system to deliver pages
57151   ** from the disk more rapidly.
57152   **
57153   ** An O(n^2) insertion sort algorithm is used, but since
57154   ** n is never more than NB (a small constant), that should
57155   ** not be a problem.
57156   **
57157   ** When NB==3, this one optimization makes the database
57158   ** about 25% faster for large insertions and deletions.
57159   */
57160   for(i=0; i<k-1; i++){
57161     int minV = apNew[i]->pgno;
57162     int minI = i;
57163     for(j=i+1; j<k; j++){
57164       if( apNew[j]->pgno<(unsigned)minV ){
57165         minI = j;
57166         minV = apNew[j]->pgno;
57167       }
57168     }
57169     if( minI>i ){
57170       MemPage *pT;
57171       pT = apNew[i];
57172       apNew[i] = apNew[minI];
57173       apNew[minI] = pT;
57174     }
57175   }
57176   TRACE(("new: %d(%d) %d(%d) %d(%d) %d(%d) %d(%d)\n",
57177     apNew[0]->pgno, szNew[0],
57178     nNew>=2 ? apNew[1]->pgno : 0, nNew>=2 ? szNew[1] : 0,
57179     nNew>=3 ? apNew[2]->pgno : 0, nNew>=3 ? szNew[2] : 0,
57180     nNew>=4 ? apNew[3]->pgno : 0, nNew>=4 ? szNew[3] : 0,
57181     nNew>=5 ? apNew[4]->pgno : 0, nNew>=5 ? szNew[4] : 0));
57182 
57183   assert( sqlite3PagerIswriteable(pParent->pDbPage) );
57184   put4byte(pRight, apNew[nNew-1]->pgno);
57185 
57186   /*
57187   ** Evenly distribute the data in apCell[] across the new pages.
57188   ** Insert divider cells into pParent as necessary.
57189   */
57190   j = 0;
57191   for(i=0; i<nNew; i++){
57192     /* Assemble the new sibling page. */
57193     MemPage *pNew = apNew[i];
57194     assert( j<nMaxCells );
57195     zeroPage(pNew, pageFlags);
57196     assemblePage(pNew, cntNew[i]-j, &apCell[j], &szCell[j]);
57197     assert( pNew->nCell>0 || (nNew==1 && cntNew[0]==0) );
57198     assert( pNew->nOverflow==0 );
57199 
57200     j = cntNew[i];
57201 
57202     /* If the sibling page assembled above was not the right-most sibling,
57203     ** insert a divider cell into the parent page.
57204     */
57205     assert( i<nNew-1 || j==nCell );
57206     if( j<nCell ){
57207       u8 *pCell;
57208       u8 *pTemp;
57209       int sz;
57210 
57211       assert( j<nMaxCells );
57212       pCell = apCell[j];
57213       sz = szCell[j] + leafCorrection;
57214       pTemp = &aOvflSpace[iOvflSpace];
57215       if( !pNew->leaf ){
57216         memcpy(&pNew->aData[8], pCell, 4);
57217       }else if( leafData ){
57218         /* If the tree is a leaf-data tree, and the siblings are leaves,
57219         ** then there is no divider cell in apCell[]. Instead, the divider
57220         ** cell consists of the integer key for the right-most cell of
57221         ** the sibling-page assembled above only.
57222         */
57223         CellInfo info;
57224         j--;
57225         btreeParseCellPtr(pNew, apCell[j], &info);
57226         pCell = pTemp;
57227         sz = 4 + putVarint(&pCell[4], info.nKey);
57228         pTemp = 0;
57229       }else{
57230         pCell -= 4;
57231         /* Obscure case for non-leaf-data trees: If the cell at pCell was
57232         ** previously stored on a leaf node, and its reported size was 4
57233         ** bytes, then it may actually be smaller than this
57234         ** (see btreeParseCellPtr(), 4 bytes is the minimum size of
57235         ** any cell). But it is important to pass the correct size to
57236         ** insertCell(), so reparse the cell now.
57237         **
57238         ** Note that this can never happen in an SQLite data file, as all
57239         ** cells are at least 4 bytes. It only happens in b-trees used
57240         ** to evaluate "IN (SELECT ...)" and similar clauses.
57241         */
57242         if( szCell[j]==4 ){
57243           assert(leafCorrection==4);
57244           sz = cellSizePtr(pParent, pCell);
57245         }
57246       }
57247       iOvflSpace += sz;
57248       assert( sz<=pBt->maxLocal+23 );
57249       assert( iOvflSpace <= (int)pBt->pageSize );
57250       insertCell(pParent, nxDiv, pCell, sz, pTemp, pNew->pgno, &rc);
57251       if( rc!=SQLITE_OK ) goto balance_cleanup;
57252       assert( sqlite3PagerIswriteable(pParent->pDbPage) );
57253 
57254       j++;
57255       nxDiv++;
57256     }
57257   }
57258   assert( j==nCell );
57259   assert( nOld>0 );
57260   assert( nNew>0 );
57261   if( (pageFlags & PTF_LEAF)==0 ){
57262     u8 *zChild = &apCopy[nOld-1]->aData[8];
57263     memcpy(&apNew[nNew-1]->aData[8], zChild, 4);
57264   }
57265 
57266   if( isRoot && pParent->nCell==0 && pParent->hdrOffset<=apNew[0]->nFree ){
57267     /* The root page of the b-tree now contains no cells. The only sibling
57268     ** page is the right-child of the parent. Copy the contents of the
57269     ** child page into the parent, decreasing the overall height of the
57270     ** b-tree structure by one. This is described as the "balance-shallower"
57271     ** sub-algorithm in some documentation.
57272     **
57273     ** If this is an auto-vacuum database, the call to copyNodeContent()
57274     ** sets all pointer-map entries corresponding to database image pages
57275     ** for which the pointer is stored within the content being copied.
57276     **
57277     ** The second assert below verifies that the child page is defragmented
57278     ** (it must be, as it was just reconstructed using assemblePage()). This
57279     ** is important if the parent page happens to be page 1 of the database
57280     ** image.  */
57281     assert( nNew==1 );
57282     assert( apNew[0]->nFree ==
57283         (get2byte(&apNew[0]->aData[5])-apNew[0]->cellOffset-apNew[0]->nCell*2)
57284     );
57285     copyNodeContent(apNew[0], pParent, &rc);
57286     freePage(apNew[0], &rc);
57287   }else if( ISAUTOVACUUM ){
57288     /* Fix the pointer-map entries for all the cells that were shifted around.
57289     ** There are several different types of pointer-map entries that need to
57290     ** be dealt with by this routine. Some of these have been set already, but
57291     ** many have not. The following is a summary:
57292     **
57293     **   1) The entries associated with new sibling pages that were not
57294     **      siblings when this function was called. These have already
57295     **      been set. We don't need to worry about old siblings that were
57296     **      moved to the free-list - the freePage() code has taken care
57297     **      of those.
57298     **
57299     **   2) The pointer-map entries associated with the first overflow
57300     **      page in any overflow chains used by new divider cells. These
57301     **      have also already been taken care of by the insertCell() code.
57302     **
57303     **   3) If the sibling pages are not leaves, then the child pages of
57304     **      cells stored on the sibling pages may need to be updated.
57305     **
57306     **   4) If the sibling pages are not internal intkey nodes, then any
57307     **      overflow pages used by these cells may need to be updated
57308     **      (internal intkey nodes never contain pointers to overflow pages).
57309     **
57310     **   5) If the sibling pages are not leaves, then the pointer-map
57311     **      entries for the right-child pages of each sibling may need
57312     **      to be updated.
57313     **
57314     ** Cases 1 and 2 are dealt with above by other code. The next
57315     ** block deals with cases 3 and 4 and the one after that, case 5. Since
57316     ** setting a pointer map entry is a relatively expensive operation, this
57317     ** code only sets pointer map entries for child or overflow pages that have
57318     ** actually moved between pages.  */
57319     MemPage *pNew = apNew[0];
57320     MemPage *pOld = apCopy[0];
57321     int nOverflow = pOld->nOverflow;
57322     int iNextOld = pOld->nCell + nOverflow;
57323     int iOverflow = (nOverflow ? pOld->aiOvfl[0] : -1);
57324     j = 0;                             /* Current 'old' sibling page */
57325     k = 0;                             /* Current 'new' sibling page */
57326     for(i=0; i<nCell; i++){
57327       int isDivider = 0;
57328       while( i==iNextOld ){
57329         /* Cell i is the cell immediately following the last cell on old
57330         ** sibling page j. If the siblings are not leaf pages of an
57331         ** intkey b-tree, then cell i was a divider cell. */
57332         assert( j+1 < ArraySize(apCopy) );
57333         assert( j+1 < nOld );
57334         pOld = apCopy[++j];
57335         iNextOld = i + !leafData + pOld->nCell + pOld->nOverflow;
57336         if( pOld->nOverflow ){
57337           nOverflow = pOld->nOverflow;
57338           iOverflow = i + !leafData + pOld->aiOvfl[0];
57339         }
57340         isDivider = !leafData;
57341       }
57342 
57343       assert(nOverflow>0 || iOverflow<i );
57344       assert(nOverflow<2 || pOld->aiOvfl[0]==pOld->aiOvfl[1]-1);
57345       assert(nOverflow<3 || pOld->aiOvfl[1]==pOld->aiOvfl[2]-1);
57346       if( i==iOverflow ){
57347         isDivider = 1;
57348         if( (--nOverflow)>0 ){
57349           iOverflow++;
57350         }
57351       }
57352 
57353       if( i==cntNew[k] ){
57354         /* Cell i is the cell immediately following the last cell on new
57355         ** sibling page k. If the siblings are not leaf pages of an
57356         ** intkey b-tree, then cell i is a divider cell.  */
57357         pNew = apNew[++k];
57358         if( !leafData ) continue;
57359       }
57360       assert( j<nOld );
57361       assert( k<nNew );
57362 
57363       /* If the cell was originally divider cell (and is not now) or
57364       ** an overflow cell, or if the cell was located on a different sibling
57365       ** page before the balancing, then the pointer map entries associated
57366       ** with any child or overflow pages need to be updated.  */
57367       if( isDivider || pOld->pgno!=pNew->pgno ){
57368         if( !leafCorrection ){
57369           ptrmapPut(pBt, get4byte(apCell[i]), PTRMAP_BTREE, pNew->pgno, &rc);
57370         }
57371         if( szCell[i]>pNew->minLocal ){
57372           ptrmapPutOvflPtr(pNew, apCell[i], &rc);
57373         }
57374       }
57375     }
57376 
57377     if( !leafCorrection ){
57378       for(i=0; i<nNew; i++){
57379         u32 key = get4byte(&apNew[i]->aData[8]);
57380         ptrmapPut(pBt, key, PTRMAP_BTREE, apNew[i]->pgno, &rc);
57381       }
57382     }
57383 
57384 #if 0
57385     /* The ptrmapCheckPages() contains assert() statements that verify that
57386     ** all pointer map pages are set correctly. This is helpful while
57387     ** debugging. This is usually disabled because a corrupt database may
57388     ** cause an assert() statement to fail.  */
57389     ptrmapCheckPages(apNew, nNew);
57390     ptrmapCheckPages(&pParent, 1);
57391 #endif
57392   }
57393 
57394   assert( pParent->isInit );
57395   TRACE(("BALANCE: finished: old=%d new=%d cells=%d\n",
57396           nOld, nNew, nCell));
57397 
57398   /*
57399   ** Cleanup before returning.
57400   */
57401 balance_cleanup:
57402   sqlite3ScratchFree(apCell);
57403   for(i=0; i<nOld; i++){
57404     releasePage(apOld[i]);
57405   }
57406   for(i=0; i<nNew; i++){
57407     releasePage(apNew[i]);
57408   }
57409 
57410   return rc;
57411 }
57412 #if defined(_MSC_VER) && _MSC_VER >= 1700 && defined(_M_ARM)
57413 #pragma optimize("", on)
57414 #endif
57415 
57416 
57417 /*
57418 ** This function is called when the root page of a b-tree structure is
57419 ** overfull (has one or more overflow pages).
57420 **
57421 ** A new child page is allocated and the contents of the current root
57422 ** page, including overflow cells, are copied into the child. The root
57423 ** page is then overwritten to make it an empty page with the right-child
57424 ** pointer pointing to the new page.
57425 **
57426 ** Before returning, all pointer-map entries corresponding to pages
57427 ** that the new child-page now contains pointers to are updated. The
57428 ** entry corresponding to the new right-child pointer of the root
57429 ** page is also updated.
57430 **
57431 ** If successful, *ppChild is set to contain a reference to the child
57432 ** page and SQLITE_OK is returned. In this case the caller is required
57433 ** to call releasePage() on *ppChild exactly once. If an error occurs,
57434 ** an error code is returned and *ppChild is set to 0.
57435 */
57436 static int balance_deeper(MemPage *pRoot, MemPage **ppChild){
57437   int rc;                        /* Return value from subprocedures */
57438   MemPage *pChild = 0;           /* Pointer to a new child page */
57439   Pgno pgnoChild = 0;            /* Page number of the new child page */
57440   BtShared *pBt = pRoot->pBt;    /* The BTree */
57441 
57442   assert( pRoot->nOverflow>0 );
57443   assert( sqlite3_mutex_held(pBt->mutex) );
57444 
57445   /* Make pRoot, the root page of the b-tree, writable. Allocate a new
57446   ** page that will become the new right-child of pPage. Copy the contents
57447   ** of the node stored on pRoot into the new child page.
57448   */
57449   rc = sqlite3PagerWrite(pRoot->pDbPage);
57450   if( rc==SQLITE_OK ){
57451     rc = allocateBtreePage(pBt,&pChild,&pgnoChild,pRoot->pgno,0);
57452     copyNodeContent(pRoot, pChild, &rc);
57453     if( ISAUTOVACUUM ){
57454       ptrmapPut(pBt, pgnoChild, PTRMAP_BTREE, pRoot->pgno, &rc);
57455     }
57456   }
57457   if( rc ){
57458     *ppChild = 0;
57459     releasePage(pChild);
57460     return rc;
57461   }
57462   assert( sqlite3PagerIswriteable(pChild->pDbPage) );
57463   assert( sqlite3PagerIswriteable(pRoot->pDbPage) );
57464   assert( pChild->nCell==pRoot->nCell );
57465 
57466   TRACE(("BALANCE: copy root %d into %d\n", pRoot->pgno, pChild->pgno));
57467 
57468   /* Copy the overflow cells from pRoot to pChild */
57469   memcpy(pChild->aiOvfl, pRoot->aiOvfl,
57470          pRoot->nOverflow*sizeof(pRoot->aiOvfl[0]));
57471   memcpy(pChild->apOvfl, pRoot->apOvfl,
57472          pRoot->nOverflow*sizeof(pRoot->apOvfl[0]));
57473   pChild->nOverflow = pRoot->nOverflow;
57474 
57475   /* Zero the contents of pRoot. Then install pChild as the right-child. */
57476   zeroPage(pRoot, pChild->aData[0] & ~PTF_LEAF);
57477   put4byte(&pRoot->aData[pRoot->hdrOffset+8], pgnoChild);
57478 
57479   *ppChild = pChild;
57480   return SQLITE_OK;
57481 }
57482 
57483 /*
57484 ** The page that pCur currently points to has just been modified in
57485 ** some way. This function figures out if this modification means the
57486 ** tree needs to be balanced, and if so calls the appropriate balancing
57487 ** routine. Balancing routines are:
57488 **
57489 **   balance_quick()
57490 **   balance_deeper()
57491 **   balance_nonroot()
57492 */
57493 static int balance(BtCursor *pCur){
57494   int rc = SQLITE_OK;
57495   const int nMin = pCur->pBt->usableSize * 2 / 3;
57496   u8 aBalanceQuickSpace[13];
57497   u8 *pFree = 0;
57498 
57499   TESTONLY( int balance_quick_called = 0 );
57500   TESTONLY( int balance_deeper_called = 0 );
57501 
57502   do {
57503     int iPage = pCur->iPage;
57504     MemPage *pPage = pCur->apPage[iPage];
57505 
57506     if( iPage==0 ){
57507       if( pPage->nOverflow ){
57508         /* The root page of the b-tree is overfull. In this case call the
57509         ** balance_deeper() function to create a new child for the root-page
57510         ** and copy the current contents of the root-page to it. The
57511         ** next iteration of the do-loop will balance the child page.
57512         */
57513         assert( (balance_deeper_called++)==0 );
57514         rc = balance_deeper(pPage, &pCur->apPage[1]);
57515         if( rc==SQLITE_OK ){
57516           pCur->iPage = 1;
57517           pCur->aiIdx[0] = 0;
57518           pCur->aiIdx[1] = 0;
57519           assert( pCur->apPage[1]->nOverflow );
57520         }
57521       }else{
57522         break;
57523       }
57524     }else if( pPage->nOverflow==0 && pPage->nFree<=nMin ){
57525       break;
57526     }else{
57527       MemPage * const pParent = pCur->apPage[iPage-1];
57528       int const iIdx = pCur->aiIdx[iPage-1];
57529 
57530       rc = sqlite3PagerWrite(pParent->pDbPage);
57531       if( rc==SQLITE_OK ){
57532 #ifndef SQLITE_OMIT_QUICKBALANCE
57533         if( pPage->hasData
57534          && pPage->nOverflow==1
57535          && pPage->aiOvfl[0]==pPage->nCell
57536          && pParent->pgno!=1
57537          && pParent->nCell==iIdx
57538         ){
57539           /* Call balance_quick() to create a new sibling of pPage on which
57540           ** to store the overflow cell. balance_quick() inserts a new cell
57541           ** into pParent, which may cause pParent overflow. If this
57542           ** happens, the next interation of the do-loop will balance pParent
57543           ** use either balance_nonroot() or balance_deeper(). Until this
57544           ** happens, the overflow cell is stored in the aBalanceQuickSpace[]
57545           ** buffer.
57546           **
57547           ** The purpose of the following assert() is to check that only a
57548           ** single call to balance_quick() is made for each call to this
57549           ** function. If this were not verified, a subtle bug involving reuse
57550           ** of the aBalanceQuickSpace[] might sneak in.
57551           */
57552           assert( (balance_quick_called++)==0 );
57553           rc = balance_quick(pParent, pPage, aBalanceQuickSpace);
57554         }else
57555 #endif
57556         {
57557           /* In this case, call balance_nonroot() to redistribute cells
57558           ** between pPage and up to 2 of its sibling pages. This involves
57559           ** modifying the contents of pParent, which may cause pParent to
57560           ** become overfull or underfull. The next iteration of the do-loop
57561           ** will balance the parent page to correct this.
57562           **
57563           ** If the parent page becomes overfull, the overflow cell or cells
57564           ** are stored in the pSpace buffer allocated immediately below.
57565           ** A subsequent iteration of the do-loop will deal with this by
57566           ** calling balance_nonroot() (balance_deeper() may be called first,
57567           ** but it doesn't deal with overflow cells - just moves them to a
57568           ** different page). Once this subsequent call to balance_nonroot()
57569           ** has completed, it is safe to release the pSpace buffer used by
57570           ** the previous call, as the overflow cell data will have been
57571           ** copied either into the body of a database page or into the new
57572           ** pSpace buffer passed to the latter call to balance_nonroot().
57573           */
57574           u8 *pSpace = sqlite3PageMalloc(pCur->pBt->pageSize);
57575           rc = balance_nonroot(pParent, iIdx, pSpace, iPage==1, pCur->hints);
57576           if( pFree ){
57577             /* If pFree is not NULL, it points to the pSpace buffer used
57578             ** by a previous call to balance_nonroot(). Its contents are
57579             ** now stored either on real database pages or within the
57580             ** new pSpace buffer, so it may be safely freed here. */
57581             sqlite3PageFree(pFree);
57582           }
57583 
57584           /* The pSpace buffer will be freed after the next call to
57585           ** balance_nonroot(), or just before this function returns, whichever
57586           ** comes first. */
57587           pFree = pSpace;
57588         }
57589       }
57590 
57591       pPage->nOverflow = 0;
57592 
57593       /* The next iteration of the do-loop balances the parent page. */
57594       releasePage(pPage);
57595       pCur->iPage--;
57596     }
57597   }while( rc==SQLITE_OK );
57598 
57599   if( pFree ){
57600     sqlite3PageFree(pFree);
57601   }
57602   return rc;
57603 }
57604 
57605 
57606 /*
57607 ** Insert a new record into the BTree.  The key is given by (pKey,nKey)
57608 ** and the data is given by (pData,nData).  The cursor is used only to
57609 ** define what table the record should be inserted into.  The cursor
57610 ** is left pointing at a random location.
57611 **
57612 ** For an INTKEY table, only the nKey value of the key is used.  pKey is
57613 ** ignored.  For a ZERODATA table, the pData and nData are both ignored.
57614 **
57615 ** If the seekResult parameter is non-zero, then a successful call to
57616 ** MovetoUnpacked() to seek cursor pCur to (pKey, nKey) has already
57617 ** been performed. seekResult is the search result returned (a negative
57618 ** number if pCur points at an entry that is smaller than (pKey, nKey), or
57619 ** a positive value if pCur points at an etry that is larger than
57620 ** (pKey, nKey)).
57621 **
57622 ** If the seekResult parameter is non-zero, then the caller guarantees that
57623 ** cursor pCur is pointing at the existing copy of a row that is to be
57624 ** overwritten.  If the seekResult parameter is 0, then cursor pCur may
57625 ** point to any entry or to no entry at all and so this function has to seek
57626 ** the cursor before the new key can be inserted.
57627 */
57628 SQLITE_PRIVATE int sqlite3BtreeInsert(
57629   BtCursor *pCur,                /* Insert data into the table of this cursor */
57630   const void *pKey, i64 nKey,    /* The key of the new record */
57631   const void *pData, int nData,  /* The data of the new record */
57632   int nZero,                     /* Number of extra 0 bytes to append to data */
57633   int appendBias,                /* True if this is likely an append */
57634   int seekResult                 /* Result of prior MovetoUnpacked() call */
57635 ){
57636   int rc;
57637   int loc = seekResult;          /* -1: before desired location  +1: after */
57638   int szNew = 0;
57639   int idx;
57640   MemPage *pPage;
57641   Btree *p = pCur->pBtree;
57642   BtShared *pBt = p->pBt;
57643   unsigned char *oldCell;
57644   unsigned char *newCell = 0;
57645 
57646   if( pCur->eState==CURSOR_FAULT ){
57647     assert( pCur->skipNext!=SQLITE_OK );
57648     return pCur->skipNext;
57649   }
57650 
57651   assert( cursorHoldsMutex(pCur) );
57652   assert( pCur->wrFlag && pBt->inTransaction==TRANS_WRITE
57653               && (pBt->btsFlags & BTS_READ_ONLY)==0 );
57654   assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
57655 
57656   /* Assert that the caller has been consistent. If this cursor was opened
57657   ** expecting an index b-tree, then the caller should be inserting blob
57658   ** keys with no associated data. If the cursor was opened expecting an
57659   ** intkey table, the caller should be inserting integer keys with a
57660   ** blob of associated data.  */
57661   assert( (pKey==0)==(pCur->pKeyInfo==0) );
57662 
57663   /* Save the positions of any other cursors open on this table.
57664   **
57665   ** In some cases, the call to btreeMoveto() below is a no-op. For
57666   ** example, when inserting data into a table with auto-generated integer
57667   ** keys, the VDBE layer invokes sqlite3BtreeLast() to figure out the
57668   ** integer key to use. It then calls this function to actually insert the
57669   ** data into the intkey B-Tree. In this case btreeMoveto() recognizes
57670   ** that the cursor is already where it needs to be and returns without
57671   ** doing any work. To avoid thwarting these optimizations, it is important
57672   ** not to clear the cursor here.
57673   */
57674   rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur);
57675   if( rc ) return rc;
57676 
57677   /* If this is an insert into a table b-tree, invalidate any incrblob
57678   ** cursors open on the row being replaced (assuming this is a replace
57679   ** operation - if it is not, the following is a no-op).  */
57680   if( pCur->pKeyInfo==0 ){
57681     invalidateIncrblobCursors(p, nKey, 0);
57682   }
57683 
57684   if( !loc ){
57685     rc = btreeMoveto(pCur, pKey, nKey, appendBias, &loc);
57686     if( rc ) return rc;
57687   }
57688   assert( pCur->eState==CURSOR_VALID || (pCur->eState==CURSOR_INVALID && loc) );
57689 
57690   pPage = pCur->apPage[pCur->iPage];
57691   assert( pPage->intKey || nKey>=0 );
57692   assert( pPage->leaf || !pPage->intKey );
57693 
57694   TRACE(("INSERT: table=%d nkey=%lld ndata=%d page=%d %s\n",
57695           pCur->pgnoRoot, nKey, nData, pPage->pgno,
57696           loc==0 ? "overwrite" : "new entry"));
57697   assert( pPage->isInit );
57698   allocateTempSpace(pBt);
57699   newCell = pBt->pTmpSpace;
57700   if( newCell==0 ) return SQLITE_NOMEM;
57701   rc = fillInCell(pPage, newCell, pKey, nKey, pData, nData, nZero, &szNew);
57702   if( rc ) goto end_insert;
57703   assert( szNew==cellSizePtr(pPage, newCell) );
57704   assert( szNew <= MX_CELL_SIZE(pBt) );
57705   idx = pCur->aiIdx[pCur->iPage];
57706   if( loc==0 ){
57707     u16 szOld;
57708     assert( idx<pPage->nCell );
57709     rc = sqlite3PagerWrite(pPage->pDbPage);
57710     if( rc ){
57711       goto end_insert;
57712     }
57713     oldCell = findCell(pPage, idx);
57714     if( !pPage->leaf ){
57715       memcpy(newCell, oldCell, 4);
57716     }
57717     szOld = cellSizePtr(pPage, oldCell);
57718     rc = clearCell(pPage, oldCell);
57719     dropCell(pPage, idx, szOld, &rc);
57720     if( rc ) goto end_insert;
57721   }else if( loc<0 && pPage->nCell>0 ){
57722     assert( pPage->leaf );
57723     idx = ++pCur->aiIdx[pCur->iPage];
57724   }else{
57725     assert( pPage->leaf );
57726   }
57727   insertCell(pPage, idx, newCell, szNew, 0, 0, &rc);
57728   assert( rc!=SQLITE_OK || pPage->nCell>0 || pPage->nOverflow>0 );
57729 
57730   /* If no error has occurred and pPage has an overflow cell, call balance()
57731   ** to redistribute the cells within the tree. Since balance() may move
57732   ** the cursor, zero the BtCursor.info.nSize and BtCursor.validNKey
57733   ** variables.
57734   **
57735   ** Previous versions of SQLite called moveToRoot() to move the cursor
57736   ** back to the root page as balance() used to invalidate the contents
57737   ** of BtCursor.apPage[] and BtCursor.aiIdx[]. Instead of doing that,
57738   ** set the cursor state to "invalid". This makes common insert operations
57739   ** slightly faster.
57740   **
57741   ** There is a subtle but important optimization here too. When inserting
57742   ** multiple records into an intkey b-tree using a single cursor (as can
57743   ** happen while processing an "INSERT INTO ... SELECT" statement), it
57744   ** is advantageous to leave the cursor pointing to the last entry in
57745   ** the b-tree if possible. If the cursor is left pointing to the last
57746   ** entry in the table, and the next row inserted has an integer key
57747   ** larger than the largest existing key, it is possible to insert the
57748   ** row without seeking the cursor. This can be a big performance boost.
57749   */
57750   pCur->info.nSize = 0;
57751   pCur->validNKey = 0;
57752   if( rc==SQLITE_OK && pPage->nOverflow ){
57753     rc = balance(pCur);
57754 
57755     /* Must make sure nOverflow is reset to zero even if the balance()
57756     ** fails. Internal data structure corruption will result otherwise.
57757     ** Also, set the cursor state to invalid. This stops saveCursorPosition()
57758     ** from trying to save the current position of the cursor.  */
57759     pCur->apPage[pCur->iPage]->nOverflow = 0;
57760     pCur->eState = CURSOR_INVALID;
57761   }
57762   assert( pCur->apPage[pCur->iPage]->nOverflow==0 );
57763 
57764 end_insert:
57765   return rc;
57766 }
57767 
57768 /*
57769 ** Delete the entry that the cursor is pointing to.  The cursor
57770 ** is left pointing at a arbitrary location.
57771 */
57772 SQLITE_PRIVATE int sqlite3BtreeDelete(BtCursor *pCur){
57773   Btree *p = pCur->pBtree;
57774   BtShared *pBt = p->pBt;
57775   int rc;                              /* Return code */
57776   MemPage *pPage;                      /* Page to delete cell from */
57777   unsigned char *pCell;                /* Pointer to cell to delete */
57778   int iCellIdx;                        /* Index of cell to delete */
57779   int iCellDepth;                      /* Depth of node containing pCell */
57780 
57781   assert( cursorHoldsMutex(pCur) );
57782   assert( pBt->inTransaction==TRANS_WRITE );
57783   assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
57784   assert( pCur->wrFlag );
57785   assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) );
57786   assert( !hasReadConflicts(p, pCur->pgnoRoot) );
57787 
57788   if( NEVER(pCur->aiIdx[pCur->iPage]>=pCur->apPage[pCur->iPage]->nCell)
57789    || NEVER(pCur->eState!=CURSOR_VALID)
57790   ){
57791     return SQLITE_ERROR;  /* Something has gone awry. */
57792   }
57793 
57794   iCellDepth = pCur->iPage;
57795   iCellIdx = pCur->aiIdx[iCellDepth];
57796   pPage = pCur->apPage[iCellDepth];
57797   pCell = findCell(pPage, iCellIdx);
57798 
57799   /* If the page containing the entry to delete is not a leaf page, move
57800   ** the cursor to the largest entry in the tree that is smaller than
57801   ** the entry being deleted. This cell will replace the cell being deleted
57802   ** from the internal node. The 'previous' entry is used for this instead
57803   ** of the 'next' entry, as the previous entry is always a part of the
57804   ** sub-tree headed by the child page of the cell being deleted. This makes
57805   ** balancing the tree following the delete operation easier.  */
57806   if( !pPage->leaf ){
57807     int notUsed;
57808     rc = sqlite3BtreePrevious(pCur, &notUsed);
57809     if( rc ) return rc;
57810   }
57811 
57812   /* Save the positions of any other cursors open on this table before
57813   ** making any modifications. Make the page containing the entry to be
57814   ** deleted writable. Then free any overflow pages associated with the
57815   ** entry and finally remove the cell itself from within the page.
57816   */
57817   rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur);
57818   if( rc ) return rc;
57819 
57820   /* If this is a delete operation to remove a row from a table b-tree,
57821   ** invalidate any incrblob cursors open on the row being deleted.  */
57822   if( pCur->pKeyInfo==0 ){
57823     invalidateIncrblobCursors(p, pCur->info.nKey, 0);
57824   }
57825 
57826   rc = sqlite3PagerWrite(pPage->pDbPage);
57827   if( rc ) return rc;
57828   rc = clearCell(pPage, pCell);
57829   dropCell(pPage, iCellIdx, cellSizePtr(pPage, pCell), &rc);
57830   if( rc ) return rc;
57831 
57832   /* If the cell deleted was not located on a leaf page, then the cursor
57833   ** is currently pointing to the largest entry in the sub-tree headed
57834   ** by the child-page of the cell that was just deleted from an internal
57835   ** node. The cell from the leaf node needs to be moved to the internal
57836   ** node to replace the deleted cell.  */
57837   if( !pPage->leaf ){
57838     MemPage *pLeaf = pCur->apPage[pCur->iPage];
57839     int nCell;
57840     Pgno n = pCur->apPage[iCellDepth+1]->pgno;
57841     unsigned char *pTmp;
57842 
57843     pCell = findCell(pLeaf, pLeaf->nCell-1);
57844     nCell = cellSizePtr(pLeaf, pCell);
57845     assert( MX_CELL_SIZE(pBt) >= nCell );
57846 
57847     allocateTempSpace(pBt);
57848     pTmp = pBt->pTmpSpace;
57849 
57850     rc = sqlite3PagerWrite(pLeaf->pDbPage);
57851     insertCell(pPage, iCellIdx, pCell-4, nCell+4, pTmp, n, &rc);
57852     dropCell(pLeaf, pLeaf->nCell-1, nCell, &rc);
57853     if( rc ) return rc;
57854   }
57855 
57856   /* Balance the tree. If the entry deleted was located on a leaf page,
57857   ** then the cursor still points to that page. In this case the first
57858   ** call to balance() repairs the tree, and the if(...) condition is
57859   ** never true.
57860   **
57861   ** Otherwise, if the entry deleted was on an internal node page, then
57862   ** pCur is pointing to the leaf page from which a cell was removed to
57863   ** replace the cell deleted from the internal node. This is slightly
57864   ** tricky as the leaf node may be underfull, and the internal node may
57865   ** be either under or overfull. In this case run the balancing algorithm
57866   ** on the leaf node first. If the balance proceeds far enough up the
57867   ** tree that we can be sure that any problem in the internal node has
57868   ** been corrected, so be it. Otherwise, after balancing the leaf node,
57869   ** walk the cursor up the tree to the internal node and balance it as
57870   ** well.  */
57871   rc = balance(pCur);
57872   if( rc==SQLITE_OK && pCur->iPage>iCellDepth ){
57873     while( pCur->iPage>iCellDepth ){
57874       releasePage(pCur->apPage[pCur->iPage--]);
57875     }
57876     rc = balance(pCur);
57877   }
57878 
57879   if( rc==SQLITE_OK ){
57880     moveToRoot(pCur);
57881   }
57882   return rc;
57883 }
57884 
57885 /*
57886 ** Create a new BTree table.  Write into *piTable the page
57887 ** number for the root page of the new table.
57888 **
57889 ** The type of type is determined by the flags parameter.  Only the
57890 ** following values of flags are currently in use.  Other values for
57891 ** flags might not work:
57892 **
57893 **     BTREE_INTKEY|BTREE_LEAFDATA     Used for SQL tables with rowid keys
57894 **     BTREE_ZERODATA                  Used for SQL indices
57895 */
57896 static int btreeCreateTable(Btree *p, int *piTable, int createTabFlags){
57897   BtShared *pBt = p->pBt;
57898   MemPage *pRoot;
57899   Pgno pgnoRoot;
57900   int rc;
57901   int ptfFlags;          /* Page-type flage for the root page of new table */
57902 
57903   assert( sqlite3BtreeHoldsMutex(p) );
57904   assert( pBt->inTransaction==TRANS_WRITE );
57905   assert( (pBt->btsFlags & BTS_READ_ONLY)==0 );
57906 
57907 #ifdef SQLITE_OMIT_AUTOVACUUM
57908   rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0);
57909   if( rc ){
57910     return rc;
57911   }
57912 #else
57913   if( pBt->autoVacuum ){
57914     Pgno pgnoMove;      /* Move a page here to make room for the root-page */
57915     MemPage *pPageMove; /* The page to move to. */
57916 
57917     /* Creating a new table may probably require moving an existing database
57918     ** to make room for the new tables root page. In case this page turns
57919     ** out to be an overflow page, delete all overflow page-map caches
57920     ** held by open cursors.
57921     */
57922     invalidateAllOverflowCache(pBt);
57923 
57924     /* Read the value of meta[3] from the database to determine where the
57925     ** root page of the new table should go. meta[3] is the largest root-page
57926     ** created so far, so the new root-page is (meta[3]+1).
57927     */
57928     sqlite3BtreeGetMeta(p, BTREE_LARGEST_ROOT_PAGE, &pgnoRoot);
57929     pgnoRoot++;
57930 
57931     /* The new root-page may not be allocated on a pointer-map page, or the
57932     ** PENDING_BYTE page.
57933     */
57934     while( pgnoRoot==PTRMAP_PAGENO(pBt, pgnoRoot) ||
57935         pgnoRoot==PENDING_BYTE_PAGE(pBt) ){
57936       pgnoRoot++;
57937     }
57938     assert( pgnoRoot>=3 );
57939 
57940     /* Allocate a page. The page that currently resides at pgnoRoot will
57941     ** be moved to the allocated page (unless the allocated page happens
57942     ** to reside at pgnoRoot).
57943     */
57944     rc = allocateBtreePage(pBt, &pPageMove, &pgnoMove, pgnoRoot, BTALLOC_EXACT);
57945     if( rc!=SQLITE_OK ){
57946       return rc;
57947     }
57948 
57949     if( pgnoMove!=pgnoRoot ){
57950       /* pgnoRoot is the page that will be used for the root-page of
57951       ** the new table (assuming an error did not occur). But we were
57952       ** allocated pgnoMove. If required (i.e. if it was not allocated
57953       ** by extending the file), the current page at position pgnoMove
57954       ** is already journaled.
57955       */
57956       u8 eType = 0;
57957       Pgno iPtrPage = 0;
57958 
57959       /* Save the positions of any open cursors. This is required in
57960       ** case they are holding a reference to an xFetch reference
57961       ** corresponding to page pgnoRoot.  */
57962       rc = saveAllCursors(pBt, 0, 0);
57963       releasePage(pPageMove);
57964       if( rc!=SQLITE_OK ){
57965         return rc;
57966       }
57967 
57968       /* Move the page currently at pgnoRoot to pgnoMove. */
57969       rc = btreeGetPage(pBt, pgnoRoot, &pRoot, 0);
57970       if( rc!=SQLITE_OK ){
57971         return rc;
57972       }
57973       rc = ptrmapGet(pBt, pgnoRoot, &eType, &iPtrPage);
57974       if( eType==PTRMAP_ROOTPAGE || eType==PTRMAP_FREEPAGE ){
57975         rc = SQLITE_CORRUPT_BKPT;
57976       }
57977       if( rc!=SQLITE_OK ){
57978         releasePage(pRoot);
57979         return rc;
57980       }
57981       assert( eType!=PTRMAP_ROOTPAGE );
57982       assert( eType!=PTRMAP_FREEPAGE );
57983       rc = relocatePage(pBt, pRoot, eType, iPtrPage, pgnoMove, 0);
57984       releasePage(pRoot);
57985 
57986       /* Obtain the page at pgnoRoot */
57987       if( rc!=SQLITE_OK ){
57988         return rc;
57989       }
57990       rc = btreeGetPage(pBt, pgnoRoot, &pRoot, 0);
57991       if( rc!=SQLITE_OK ){
57992         return rc;
57993       }
57994       rc = sqlite3PagerWrite(pRoot->pDbPage);
57995       if( rc!=SQLITE_OK ){
57996         releasePage(pRoot);
57997         return rc;
57998       }
57999     }else{
58000       pRoot = pPageMove;
58001     }
58002 
58003     /* Update the pointer-map and meta-data with the new root-page number. */
58004     ptrmapPut(pBt, pgnoRoot, PTRMAP_ROOTPAGE, 0, &rc);
58005     if( rc ){
58006       releasePage(pRoot);
58007       return rc;
58008     }
58009 
58010     /* When the new root page was allocated, page 1 was made writable in
58011     ** order either to increase the database filesize, or to decrement the
58012     ** freelist count.  Hence, the sqlite3BtreeUpdateMeta() call cannot fail.
58013     */
58014     assert( sqlite3PagerIswriteable(pBt->pPage1->pDbPage) );
58015     rc = sqlite3BtreeUpdateMeta(p, 4, pgnoRoot);
58016     if( NEVER(rc) ){
58017       releasePage(pRoot);
58018       return rc;
58019     }
58020 
58021   }else{
58022     rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0);
58023     if( rc ) return rc;
58024   }
58025 #endif
58026   assert( sqlite3PagerIswriteable(pRoot->pDbPage) );
58027   if( createTabFlags & BTREE_INTKEY ){
58028     ptfFlags = PTF_INTKEY | PTF_LEAFDATA | PTF_LEAF;
58029   }else{
58030     ptfFlags = PTF_ZERODATA | PTF_LEAF;
58031   }
58032   zeroPage(pRoot, ptfFlags);
58033   sqlite3PagerUnref(pRoot->pDbPage);
58034   assert( (pBt->openFlags & BTREE_SINGLE)==0 || pgnoRoot==2 );
58035   *piTable = (int)pgnoRoot;
58036   return SQLITE_OK;
58037 }
58038 SQLITE_PRIVATE int sqlite3BtreeCreateTable(Btree *p, int *piTable, int flags){
58039   int rc;
58040   sqlite3BtreeEnter(p);
58041   rc = btreeCreateTable(p, piTable, flags);
58042   sqlite3BtreeLeave(p);
58043   return rc;
58044 }
58045 
58046 /*
58047 ** Erase the given database page and all its children.  Return
58048 ** the page to the freelist.
58049 */
58050 static int clearDatabasePage(
58051   BtShared *pBt,           /* The BTree that contains the table */
58052   Pgno pgno,               /* Page number to clear */
58053   int freePageFlag,        /* Deallocate page if true */
58054   int *pnChange            /* Add number of Cells freed to this counter */
58055 ){
58056   MemPage *pPage;
58057   int rc;
58058   unsigned char *pCell;
58059   int i;
58060   int hdr;
58061 
58062   assert( sqlite3_mutex_held(pBt->mutex) );
58063   if( pgno>btreePagecount(pBt) ){
58064     return SQLITE_CORRUPT_BKPT;
58065   }
58066 
58067   rc = getAndInitPage(pBt, pgno, &pPage, 0);
58068   if( rc ) return rc;
58069   hdr = pPage->hdrOffset;
58070   for(i=0; i<pPage->nCell; i++){
58071     pCell = findCell(pPage, i);
58072     if( !pPage->leaf ){
58073       rc = clearDatabasePage(pBt, get4byte(pCell), 1, pnChange);
58074       if( rc ) goto cleardatabasepage_out;
58075     }
58076     rc = clearCell(pPage, pCell);
58077     if( rc ) goto cleardatabasepage_out;
58078   }
58079   if( !pPage->leaf ){
58080     rc = clearDatabasePage(pBt, get4byte(&pPage->aData[hdr+8]), 1, pnChange);
58081     if( rc ) goto cleardatabasepage_out;
58082   }else if( pnChange ){
58083     assert( pPage->intKey );
58084     *pnChange += pPage->nCell;
58085   }
58086   if( freePageFlag ){
58087     freePage(pPage, &rc);
58088   }else if( (rc = sqlite3PagerWrite(pPage->pDbPage))==0 ){
58089     zeroPage(pPage, pPage->aData[hdr] | PTF_LEAF);
58090   }
58091 
58092 cleardatabasepage_out:
58093   releasePage(pPage);
58094   return rc;
58095 }
58096 
58097 /*
58098 ** Delete all information from a single table in the database.  iTable is
58099 ** the page number of the root of the table.  After this routine returns,
58100 ** the root page is empty, but still exists.
58101 **
58102 ** This routine will fail with SQLITE_LOCKED if there are any open
58103 ** read cursors on the table.  Open write cursors are moved to the
58104 ** root of the table.
58105 **
58106 ** If pnChange is not NULL, then table iTable must be an intkey table. The
58107 ** integer value pointed to by pnChange is incremented by the number of
58108 ** entries in the table.
58109 */
58110 SQLITE_PRIVATE int sqlite3BtreeClearTable(Btree *p, int iTable, int *pnChange){
58111   int rc;
58112   BtShared *pBt = p->pBt;
58113   sqlite3BtreeEnter(p);
58114   assert( p->inTrans==TRANS_WRITE );
58115 
58116   rc = saveAllCursors(pBt, (Pgno)iTable, 0);
58117 
58118   if( SQLITE_OK==rc ){
58119     /* Invalidate all incrblob cursors open on table iTable (assuming iTable
58120     ** is the root of a table b-tree - if it is not, the following call is
58121     ** a no-op).  */
58122     invalidateIncrblobCursors(p, 0, 1);
58123     rc = clearDatabasePage(pBt, (Pgno)iTable, 0, pnChange);
58124   }
58125   sqlite3BtreeLeave(p);
58126   return rc;
58127 }
58128 
58129 /*
58130 ** Erase all information in a table and add the root of the table to
58131 ** the freelist.  Except, the root of the principle table (the one on
58132 ** page 1) is never added to the freelist.
58133 **
58134 ** This routine will fail with SQLITE_LOCKED if there are any open
58135 ** cursors on the table.
58136 **
58137 ** If AUTOVACUUM is enabled and the page at iTable is not the last
58138 ** root page in the database file, then the last root page
58139 ** in the database file is moved into the slot formerly occupied by
58140 ** iTable and that last slot formerly occupied by the last root page
58141 ** is added to the freelist instead of iTable.  In this say, all
58142 ** root pages are kept at the beginning of the database file, which
58143 ** is necessary for AUTOVACUUM to work right.  *piMoved is set to the
58144 ** page number that used to be the last root page in the file before
58145 ** the move.  If no page gets moved, *piMoved is set to 0.
58146 ** The last root page is recorded in meta[3] and the value of
58147 ** meta[3] is updated by this procedure.
58148 */
58149 static int btreeDropTable(Btree *p, Pgno iTable, int *piMoved){
58150   int rc;
58151   MemPage *pPage = 0;
58152   BtShared *pBt = p->pBt;
58153 
58154   assert( sqlite3BtreeHoldsMutex(p) );
58155   assert( p->inTrans==TRANS_WRITE );
58156 
58157   /* It is illegal to drop a table if any cursors are open on the
58158   ** database. This is because in auto-vacuum mode the backend may
58159   ** need to move another root-page to fill a gap left by the deleted
58160   ** root page. If an open cursor was using this page a problem would
58161   ** occur.
58162   **
58163   ** This error is caught long before control reaches this point.
58164   */
58165   if( NEVER(pBt->pCursor) ){
58166     sqlite3ConnectionBlocked(p->db, pBt->pCursor->pBtree->db);
58167     return SQLITE_LOCKED_SHAREDCACHE;
58168   }
58169 
58170   rc = btreeGetPage(pBt, (Pgno)iTable, &pPage, 0);
58171   if( rc ) return rc;
58172   rc = sqlite3BtreeClearTable(p, iTable, 0);
58173   if( rc ){
58174     releasePage(pPage);
58175     return rc;
58176   }
58177 
58178   *piMoved = 0;
58179 
58180   if( iTable>1 ){
58181 #ifdef SQLITE_OMIT_AUTOVACUUM
58182     freePage(pPage, &rc);
58183     releasePage(pPage);
58184 #else
58185     if( pBt->autoVacuum ){
58186       Pgno maxRootPgno;
58187       sqlite3BtreeGetMeta(p, BTREE_LARGEST_ROOT_PAGE, &maxRootPgno);
58188 
58189       if( iTable==maxRootPgno ){
58190         /* If the table being dropped is the table with the largest root-page
58191         ** number in the database, put the root page on the free list.
58192         */
58193         freePage(pPage, &rc);
58194         releasePage(pPage);
58195         if( rc!=SQLITE_OK ){
58196           return rc;
58197         }
58198       }else{
58199         /* The table being dropped does not have the largest root-page
58200         ** number in the database. So move the page that does into the
58201         ** gap left by the deleted root-page.
58202         */
58203         MemPage *pMove;
58204         releasePage(pPage);
58205         rc = btreeGetPage(pBt, maxRootPgno, &pMove, 0);
58206         if( rc!=SQLITE_OK ){
58207           return rc;
58208         }
58209         rc = relocatePage(pBt, pMove, PTRMAP_ROOTPAGE, 0, iTable, 0);
58210         releasePage(pMove);
58211         if( rc!=SQLITE_OK ){
58212           return rc;
58213         }
58214         pMove = 0;
58215         rc = btreeGetPage(pBt, maxRootPgno, &pMove, 0);
58216         freePage(pMove, &rc);
58217         releasePage(pMove);
58218         if( rc!=SQLITE_OK ){
58219           return rc;
58220         }
58221         *piMoved = maxRootPgno;
58222       }
58223 
58224       /* Set the new 'max-root-page' value in the database header. This
58225       ** is the old value less one, less one more if that happens to
58226       ** be a root-page number, less one again if that is the
58227       ** PENDING_BYTE_PAGE.
58228       */
58229       maxRootPgno--;
58230       while( maxRootPgno==PENDING_BYTE_PAGE(pBt)
58231              || PTRMAP_ISPAGE(pBt, maxRootPgno) ){
58232         maxRootPgno--;
58233       }
58234       assert( maxRootPgno!=PENDING_BYTE_PAGE(pBt) );
58235 
58236       rc = sqlite3BtreeUpdateMeta(p, 4, maxRootPgno);
58237     }else{
58238       freePage(pPage, &rc);
58239       releasePage(pPage);
58240     }
58241 #endif
58242   }else{
58243     /* If sqlite3BtreeDropTable was called on page 1.
58244     ** This really never should happen except in a corrupt
58245     ** database.
58246     */
58247     zeroPage(pPage, PTF_INTKEY|PTF_LEAF );
58248     releasePage(pPage);
58249   }
58250   return rc;
58251 }
58252 SQLITE_PRIVATE int sqlite3BtreeDropTable(Btree *p, int iTable, int *piMoved){
58253   int rc;
58254   sqlite3BtreeEnter(p);
58255   rc = btreeDropTable(p, iTable, piMoved);
58256   sqlite3BtreeLeave(p);
58257   return rc;
58258 }
58259 
58260 
58261 /*
58262 ** This function may only be called if the b-tree connection already
58263 ** has a read or write transaction open on the database.
58264 **
58265 ** Read the meta-information out of a database file.  Meta[0]
58266 ** is the number of free pages currently in the database.  Meta[1]
58267 ** through meta[15] are available for use by higher layers.  Meta[0]
58268 ** is read-only, the others are read/write.
58269 **
58270 ** The schema layer numbers meta values differently.  At the schema
58271 ** layer (and the SetCookie and ReadCookie opcodes) the number of
58272 ** free pages is not visible.  So Cookie[0] is the same as Meta[1].
58273 */
58274 SQLITE_PRIVATE void sqlite3BtreeGetMeta(Btree *p, int idx, u32 *pMeta){
58275   BtShared *pBt = p->pBt;
58276 
58277   sqlite3BtreeEnter(p);
58278   assert( p->inTrans>TRANS_NONE );
58279   assert( SQLITE_OK==querySharedCacheTableLock(p, MASTER_ROOT, READ_LOCK) );
58280   assert( pBt->pPage1 );
58281   assert( idx>=0 && idx<=15 );
58282 
58283   *pMeta = get4byte(&pBt->pPage1->aData[36 + idx*4]);
58284 
58285   /* If auto-vacuum is disabled in this build and this is an auto-vacuum
58286   ** database, mark the database as read-only.  */
58287 #ifdef SQLITE_OMIT_AUTOVACUUM
58288   if( idx==BTREE_LARGEST_ROOT_PAGE && *pMeta>0 ){
58289     pBt->btsFlags |= BTS_READ_ONLY;
58290   }
58291 #endif
58292 
58293   sqlite3BtreeLeave(p);
58294 }
58295 
58296 /*
58297 ** Write meta-information back into the database.  Meta[0] is
58298 ** read-only and may not be written.
58299 */
58300 SQLITE_PRIVATE int sqlite3BtreeUpdateMeta(Btree *p, int idx, u32 iMeta){
58301   BtShared *pBt = p->pBt;
58302   unsigned char *pP1;
58303   int rc;
58304   assert( idx>=1 && idx<=15 );
58305   sqlite3BtreeEnter(p);
58306   assert( p->inTrans==TRANS_WRITE );
58307   assert( pBt->pPage1!=0 );
58308   pP1 = pBt->pPage1->aData;
58309   rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
58310   if( rc==SQLITE_OK ){
58311     put4byte(&pP1[36 + idx*4], iMeta);
58312 #ifndef SQLITE_OMIT_AUTOVACUUM
58313     if( idx==BTREE_INCR_VACUUM ){
58314       assert( pBt->autoVacuum || iMeta==0 );
58315       assert( iMeta==0 || iMeta==1 );
58316       pBt->incrVacuum = (u8)iMeta;
58317     }
58318 #endif
58319   }
58320   sqlite3BtreeLeave(p);
58321   return rc;
58322 }
58323 
58324 #ifndef SQLITE_OMIT_BTREECOUNT
58325 /*
58326 ** The first argument, pCur, is a cursor opened on some b-tree. Count the
58327 ** number of entries in the b-tree and write the result to *pnEntry.
58328 **
58329 ** SQLITE_OK is returned if the operation is successfully executed.
58330 ** Otherwise, if an error is encountered (i.e. an IO error or database
58331 ** corruption) an SQLite error code is returned.
58332 */
58333 SQLITE_PRIVATE int sqlite3BtreeCount(BtCursor *pCur, i64 *pnEntry){
58334   i64 nEntry = 0;                      /* Value to return in *pnEntry */
58335   int rc;                              /* Return code */
58336 
58337   if( pCur->pgnoRoot==0 ){
58338     *pnEntry = 0;
58339     return SQLITE_OK;
58340   }
58341   rc = moveToRoot(pCur);
58342 
58343   /* Unless an error occurs, the following loop runs one iteration for each
58344   ** page in the B-Tree structure (not including overflow pages).
58345   */
58346   while( rc==SQLITE_OK ){
58347     int iIdx;                          /* Index of child node in parent */
58348     MemPage *pPage;                    /* Current page of the b-tree */
58349 
58350     /* If this is a leaf page or the tree is not an int-key tree, then
58351     ** this page contains countable entries. Increment the entry counter
58352     ** accordingly.
58353     */
58354     pPage = pCur->apPage[pCur->iPage];
58355     if( pPage->leaf || !pPage->intKey ){
58356       nEntry += pPage->nCell;
58357     }
58358 
58359     /* pPage is a leaf node. This loop navigates the cursor so that it
58360     ** points to the first interior cell that it points to the parent of
58361     ** the next page in the tree that has not yet been visited. The
58362     ** pCur->aiIdx[pCur->iPage] value is set to the index of the parent cell
58363     ** of the page, or to the number of cells in the page if the next page
58364     ** to visit is the right-child of its parent.
58365     **
58366     ** If all pages in the tree have been visited, return SQLITE_OK to the
58367     ** caller.
58368     */
58369     if( pPage->leaf ){
58370       do {
58371         if( pCur->iPage==0 ){
58372           /* All pages of the b-tree have been visited. Return successfully. */
58373           *pnEntry = nEntry;
58374           return SQLITE_OK;
58375         }
58376         moveToParent(pCur);
58377       }while ( pCur->aiIdx[pCur->iPage]>=pCur->apPage[pCur->iPage]->nCell );
58378 
58379       pCur->aiIdx[pCur->iPage]++;
58380       pPage = pCur->apPage[pCur->iPage];
58381     }
58382 
58383     /* Descend to the child node of the cell that the cursor currently
58384     ** points at. This is the right-child if (iIdx==pPage->nCell).
58385     */
58386     iIdx = pCur->aiIdx[pCur->iPage];
58387     if( iIdx==pPage->nCell ){
58388       rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8]));
58389     }else{
58390       rc = moveToChild(pCur, get4byte(findCell(pPage, iIdx)));
58391     }
58392   }
58393 
58394   /* An error has occurred. Return an error code. */
58395   return rc;
58396 }
58397 #endif
58398 
58399 /*
58400 ** Return the pager associated with a BTree.  This routine is used for
58401 ** testing and debugging only.
58402 */
58403 SQLITE_PRIVATE Pager *sqlite3BtreePager(Btree *p){
58404   return p->pBt->pPager;
58405 }
58406 
58407 #ifndef SQLITE_OMIT_INTEGRITY_CHECK
58408 /*
58409 ** Append a message to the error message string.
58410 */
58411 static void checkAppendMsg(
58412   IntegrityCk *pCheck,
58413   char *zMsg1,
58414   const char *zFormat,
58415   ...
58416 ){
58417   va_list ap;
58418   if( !pCheck->mxErr ) return;
58419   pCheck->mxErr--;
58420   pCheck->nErr++;
58421   va_start(ap, zFormat);
58422   if( pCheck->errMsg.nChar ){
58423     sqlite3StrAccumAppend(&pCheck->errMsg, "\n", 1);
58424   }
58425   if( zMsg1 ){
58426     sqlite3StrAccumAppendAll(&pCheck->errMsg, zMsg1);
58427   }
58428   sqlite3VXPrintf(&pCheck->errMsg, 1, zFormat, ap);
58429   va_end(ap);
58430   if( pCheck->errMsg.accError==STRACCUM_NOMEM ){
58431     pCheck->mallocFailed = 1;
58432   }
58433 }
58434 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
58435 
58436 #ifndef SQLITE_OMIT_INTEGRITY_CHECK
58437 
58438 /*
58439 ** Return non-zero if the bit in the IntegrityCk.aPgRef[] array that
58440 ** corresponds to page iPg is already set.
58441 */
58442 static int getPageReferenced(IntegrityCk *pCheck, Pgno iPg){
58443   assert( iPg<=pCheck->nPage && sizeof(pCheck->aPgRef[0])==1 );
58444   return (pCheck->aPgRef[iPg/8] & (1 << (iPg & 0x07)));
58445 }
58446 
58447 /*
58448 ** Set the bit in the IntegrityCk.aPgRef[] array that corresponds to page iPg.
58449 */
58450 static void setPageReferenced(IntegrityCk *pCheck, Pgno iPg){
58451   assert( iPg<=pCheck->nPage && sizeof(pCheck->aPgRef[0])==1 );
58452   pCheck->aPgRef[iPg/8] |= (1 << (iPg & 0x07));
58453 }
58454 
58455 
58456 /*
58457 ** Add 1 to the reference count for page iPage.  If this is the second
58458 ** reference to the page, add an error message to pCheck->zErrMsg.
58459 ** Return 1 if there are 2 ore more references to the page and 0 if
58460 ** if this is the first reference to the page.
58461 **
58462 ** Also check that the page number is in bounds.
58463 */
58464 static int checkRef(IntegrityCk *pCheck, Pgno iPage, char *zContext){
58465   if( iPage==0 ) return 1;
58466   if( iPage>pCheck->nPage ){
58467     checkAppendMsg(pCheck, zContext, "invalid page number %d", iPage);
58468     return 1;
58469   }
58470   if( getPageReferenced(pCheck, iPage) ){
58471     checkAppendMsg(pCheck, zContext, "2nd reference to page %d", iPage);
58472     return 1;
58473   }
58474   setPageReferenced(pCheck, iPage);
58475   return 0;
58476 }
58477 
58478 #ifndef SQLITE_OMIT_AUTOVACUUM
58479 /*
58480 ** Check that the entry in the pointer-map for page iChild maps to
58481 ** page iParent, pointer type ptrType. If not, append an error message
58482 ** to pCheck.
58483 */
58484 static void checkPtrmap(
58485   IntegrityCk *pCheck,   /* Integrity check context */
58486   Pgno iChild,           /* Child page number */
58487   u8 eType,              /* Expected pointer map type */
58488   Pgno iParent,          /* Expected pointer map parent page number */
58489   char *zContext         /* Context description (used for error msg) */
58490 ){
58491   int rc;
58492   u8 ePtrmapType;
58493   Pgno iPtrmapParent;
58494 
58495   rc = ptrmapGet(pCheck->pBt, iChild, &ePtrmapType, &iPtrmapParent);
58496   if( rc!=SQLITE_OK ){
58497     if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ) pCheck->mallocFailed = 1;
58498     checkAppendMsg(pCheck, zContext, "Failed to read ptrmap key=%d", iChild);
58499     return;
58500   }
58501 
58502   if( ePtrmapType!=eType || iPtrmapParent!=iParent ){
58503     checkAppendMsg(pCheck, zContext,
58504       "Bad ptr map entry key=%d expected=(%d,%d) got=(%d,%d)",
58505       iChild, eType, iParent, ePtrmapType, iPtrmapParent);
58506   }
58507 }
58508 #endif
58509 
58510 /*
58511 ** Check the integrity of the freelist or of an overflow page list.
58512 ** Verify that the number of pages on the list is N.
58513 */
58514 static void checkList(
58515   IntegrityCk *pCheck,  /* Integrity checking context */
58516   int isFreeList,       /* True for a freelist.  False for overflow page list */
58517   int iPage,            /* Page number for first page in the list */
58518   int N,                /* Expected number of pages in the list */
58519   char *zContext        /* Context for error messages */
58520 ){
58521   int i;
58522   int expected = N;
58523   int iFirst = iPage;
58524   while( N-- > 0 && pCheck->mxErr ){
58525     DbPage *pOvflPage;
58526     unsigned char *pOvflData;
58527     if( iPage<1 ){
58528       checkAppendMsg(pCheck, zContext,
58529          "%d of %d pages missing from overflow list starting at %d",
58530           N+1, expected, iFirst);
58531       break;
58532     }
58533     if( checkRef(pCheck, iPage, zContext) ) break;
58534     if( sqlite3PagerGet(pCheck->pPager, (Pgno)iPage, &pOvflPage) ){
58535       checkAppendMsg(pCheck, zContext, "failed to get page %d", iPage);
58536       break;
58537     }
58538     pOvflData = (unsigned char *)sqlite3PagerGetData(pOvflPage);
58539     if( isFreeList ){
58540       int n = get4byte(&pOvflData[4]);
58541 #ifndef SQLITE_OMIT_AUTOVACUUM
58542       if( pCheck->pBt->autoVacuum ){
58543         checkPtrmap(pCheck, iPage, PTRMAP_FREEPAGE, 0, zContext);
58544       }
58545 #endif
58546       if( n>(int)pCheck->pBt->usableSize/4-2 ){
58547         checkAppendMsg(pCheck, zContext,
58548            "freelist leaf count too big on page %d", iPage);
58549         N--;
58550       }else{
58551         for(i=0; i<n; i++){
58552           Pgno iFreePage = get4byte(&pOvflData[8+i*4]);
58553 #ifndef SQLITE_OMIT_AUTOVACUUM
58554           if( pCheck->pBt->autoVacuum ){
58555             checkPtrmap(pCheck, iFreePage, PTRMAP_FREEPAGE, 0, zContext);
58556           }
58557 #endif
58558           checkRef(pCheck, iFreePage, zContext);
58559         }
58560         N -= n;
58561       }
58562     }
58563 #ifndef SQLITE_OMIT_AUTOVACUUM
58564     else{
58565       /* If this database supports auto-vacuum and iPage is not the last
58566       ** page in this overflow list, check that the pointer-map entry for
58567       ** the following page matches iPage.
58568       */
58569       if( pCheck->pBt->autoVacuum && N>0 ){
58570         i = get4byte(pOvflData);
58571         checkPtrmap(pCheck, i, PTRMAP_OVERFLOW2, iPage, zContext);
58572       }
58573     }
58574 #endif
58575     iPage = get4byte(pOvflData);
58576     sqlite3PagerUnref(pOvflPage);
58577   }
58578 }
58579 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
58580 
58581 #ifndef SQLITE_OMIT_INTEGRITY_CHECK
58582 /*
58583 ** Do various sanity checks on a single page of a tree.  Return
58584 ** the tree depth.  Root pages return 0.  Parents of root pages
58585 ** return 1, and so forth.
58586 **
58587 ** These checks are done:
58588 **
58589 **      1.  Make sure that cells and freeblocks do not overlap
58590 **          but combine to completely cover the page.
58591 **  NO  2.  Make sure cell keys are in order.
58592 **  NO  3.  Make sure no key is less than or equal to zLowerBound.
58593 **  NO  4.  Make sure no key is greater than or equal to zUpperBound.
58594 **      5.  Check the integrity of overflow pages.
58595 **      6.  Recursively call checkTreePage on all children.
58596 **      7.  Verify that the depth of all children is the same.
58597 **      8.  Make sure this page is at least 33% full or else it is
58598 **          the root of the tree.
58599 */
58600 static int checkTreePage(
58601   IntegrityCk *pCheck,  /* Context for the sanity check */
58602   int iPage,            /* Page number of the page to check */
58603   char *zParentContext, /* Parent context */
58604   i64 *pnParentMinKey,
58605   i64 *pnParentMaxKey
58606 ){
58607   MemPage *pPage;
58608   int i, rc, depth, d2, pgno, cnt;
58609   int hdr, cellStart;
58610   int nCell;
58611   u8 *data;
58612   BtShared *pBt;
58613   int usableSize;
58614   char zContext[100];
58615   char *hit = 0;
58616   i64 nMinKey = 0;
58617   i64 nMaxKey = 0;
58618 
58619   sqlite3_snprintf(sizeof(zContext), zContext, "Page %d: ", iPage);
58620 
58621   /* Check that the page exists
58622   */
58623   pBt = pCheck->pBt;
58624   usableSize = pBt->usableSize;
58625   if( iPage==0 ) return 0;
58626   if( checkRef(pCheck, iPage, zParentContext) ) return 0;
58627   if( (rc = btreeGetPage(pBt, (Pgno)iPage, &pPage, 0))!=0 ){
58628     checkAppendMsg(pCheck, zContext,
58629        "unable to get the page. error code=%d", rc);
58630     return 0;
58631   }
58632 
58633   /* Clear MemPage.isInit to make sure the corruption detection code in
58634   ** btreeInitPage() is executed.  */
58635   pPage->isInit = 0;
58636   if( (rc = btreeInitPage(pPage))!=0 ){
58637     assert( rc==SQLITE_CORRUPT );  /* The only possible error from InitPage */
58638     checkAppendMsg(pCheck, zContext,
58639                    "btreeInitPage() returns error code %d", rc);
58640     releasePage(pPage);
58641     return 0;
58642   }
58643 
58644   /* Check out all the cells.
58645   */
58646   depth = 0;
58647   for(i=0; i<pPage->nCell && pCheck->mxErr; i++){
58648     u8 *pCell;
58649     u32 sz;
58650     CellInfo info;
58651 
58652     /* Check payload overflow pages
58653     */
58654     sqlite3_snprintf(sizeof(zContext), zContext,
58655              "On tree page %d cell %d: ", iPage, i);
58656     pCell = findCell(pPage,i);
58657     btreeParseCellPtr(pPage, pCell, &info);
58658     sz = info.nData;
58659     if( !pPage->intKey ) sz += (int)info.nKey;
58660     /* For intKey pages, check that the keys are in order.
58661     */
58662     else if( i==0 ) nMinKey = nMaxKey = info.nKey;
58663     else{
58664       if( info.nKey <= nMaxKey ){
58665         checkAppendMsg(pCheck, zContext,
58666             "Rowid %lld out of order (previous was %lld)", info.nKey, nMaxKey);
58667       }
58668       nMaxKey = info.nKey;
58669     }
58670     assert( sz==info.nPayload );
58671     if( (sz>info.nLocal)
58672      && (&pCell[info.iOverflow]<=&pPage->aData[pBt->usableSize])
58673     ){
58674       int nPage = (sz - info.nLocal + usableSize - 5)/(usableSize - 4);
58675       Pgno pgnoOvfl = get4byte(&pCell[info.iOverflow]);
58676 #ifndef SQLITE_OMIT_AUTOVACUUM
58677       if( pBt->autoVacuum ){
58678         checkPtrmap(pCheck, pgnoOvfl, PTRMAP_OVERFLOW1, iPage, zContext);
58679       }
58680 #endif
58681       checkList(pCheck, 0, pgnoOvfl, nPage, zContext);
58682     }
58683 
58684     /* Check sanity of left child page.
58685     */
58686     if( !pPage->leaf ){
58687       pgno = get4byte(pCell);
58688 #ifndef SQLITE_OMIT_AUTOVACUUM
58689       if( pBt->autoVacuum ){
58690         checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage, zContext);
58691       }
58692 #endif
58693       d2 = checkTreePage(pCheck, pgno, zContext, &nMinKey, i==0 ? NULL : &nMaxKey);
58694       if( i>0 && d2!=depth ){
58695         checkAppendMsg(pCheck, zContext, "Child page depth differs");
58696       }
58697       depth = d2;
58698     }
58699   }
58700 
58701   if( !pPage->leaf ){
58702     pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]);
58703     sqlite3_snprintf(sizeof(zContext), zContext,
58704                      "On page %d at right child: ", iPage);
58705 #ifndef SQLITE_OMIT_AUTOVACUUM
58706     if( pBt->autoVacuum ){
58707       checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage, zContext);
58708     }
58709 #endif
58710     checkTreePage(pCheck, pgno, zContext, NULL, !pPage->nCell ? NULL : &nMaxKey);
58711   }
58712 
58713   /* For intKey leaf pages, check that the min/max keys are in order
58714   ** with any left/parent/right pages.
58715   */
58716   if( pPage->leaf && pPage->intKey ){
58717     /* if we are a left child page */
58718     if( pnParentMinKey ){
58719       /* if we are the left most child page */
58720       if( !pnParentMaxKey ){
58721         if( nMaxKey > *pnParentMinKey ){
58722           checkAppendMsg(pCheck, zContext,
58723               "Rowid %lld out of order (max larger than parent min of %lld)",
58724               nMaxKey, *pnParentMinKey);
58725         }
58726       }else{
58727         if( nMinKey <= *pnParentMinKey ){
58728           checkAppendMsg(pCheck, zContext,
58729               "Rowid %lld out of order (min less than parent min of %lld)",
58730               nMinKey, *pnParentMinKey);
58731         }
58732         if( nMaxKey > *pnParentMaxKey ){
58733           checkAppendMsg(pCheck, zContext,
58734               "Rowid %lld out of order (max larger than parent max of %lld)",
58735               nMaxKey, *pnParentMaxKey);
58736         }
58737         *pnParentMinKey = nMaxKey;
58738       }
58739     /* else if we're a right child page */
58740     } else if( pnParentMaxKey ){
58741       if( nMinKey <= *pnParentMaxKey ){
58742         checkAppendMsg(pCheck, zContext,
58743             "Rowid %lld out of order (min less than parent max of %lld)",
58744             nMinKey, *pnParentMaxKey);
58745       }
58746     }
58747   }
58748 
58749   /* Check for complete coverage of the page
58750   */
58751   data = pPage->aData;
58752   hdr = pPage->hdrOffset;
58753   hit = sqlite3PageMalloc( pBt->pageSize );
58754   if( hit==0 ){
58755     pCheck->mallocFailed = 1;
58756   }else{
58757     int contentOffset = get2byteNotZero(&data[hdr+5]);
58758     assert( contentOffset<=usableSize );  /* Enforced by btreeInitPage() */
58759     memset(hit+contentOffset, 0, usableSize-contentOffset);
58760     memset(hit, 1, contentOffset);
58761     nCell = get2byte(&data[hdr+3]);
58762     cellStart = hdr + 12 - 4*pPage->leaf;
58763     for(i=0; i<nCell; i++){
58764       int pc = get2byte(&data[cellStart+i*2]);
58765       u32 size = 65536;
58766       int j;
58767       if( pc<=usableSize-4 ){
58768         size = cellSizePtr(pPage, &data[pc]);
58769       }
58770       if( (int)(pc+size-1)>=usableSize ){
58771         checkAppendMsg(pCheck, 0,
58772             "Corruption detected in cell %d on page %d",i,iPage);
58773       }else{
58774         for(j=pc+size-1; j>=pc; j--) hit[j]++;
58775       }
58776     }
58777     i = get2byte(&data[hdr+1]);
58778     while( i>0 ){
58779       int size, j;
58780       assert( i<=usableSize-4 );     /* Enforced by btreeInitPage() */
58781       size = get2byte(&data[i+2]);
58782       assert( i+size<=usableSize );  /* Enforced by btreeInitPage() */
58783       for(j=i+size-1; j>=i; j--) hit[j]++;
58784       j = get2byte(&data[i]);
58785       assert( j==0 || j>i+size );  /* Enforced by btreeInitPage() */
58786       assert( j<=usableSize-4 );   /* Enforced by btreeInitPage() */
58787       i = j;
58788     }
58789     for(i=cnt=0; i<usableSize; i++){
58790       if( hit[i]==0 ){
58791         cnt++;
58792       }else if( hit[i]>1 ){
58793         checkAppendMsg(pCheck, 0,
58794           "Multiple uses for byte %d of page %d", i, iPage);
58795         break;
58796       }
58797     }
58798     if( cnt!=data[hdr+7] ){
58799       checkAppendMsg(pCheck, 0,
58800           "Fragmentation of %d bytes reported as %d on page %d",
58801           cnt, data[hdr+7], iPage);
58802     }
58803   }
58804   sqlite3PageFree(hit);
58805   releasePage(pPage);
58806   return depth+1;
58807 }
58808 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
58809 
58810 #ifndef SQLITE_OMIT_INTEGRITY_CHECK
58811 /*
58812 ** This routine does a complete check of the given BTree file.  aRoot[] is
58813 ** an array of pages numbers were each page number is the root page of
58814 ** a table.  nRoot is the number of entries in aRoot.
58815 **
58816 ** A read-only or read-write transaction must be opened before calling
58817 ** this function.
58818 **
58819 ** Write the number of error seen in *pnErr.  Except for some memory
58820 ** allocation errors,  an error message held in memory obtained from
58821 ** malloc is returned if *pnErr is non-zero.  If *pnErr==0 then NULL is
58822 ** returned.  If a memory allocation error occurs, NULL is returned.
58823 */
58824 SQLITE_PRIVATE char *sqlite3BtreeIntegrityCheck(
58825   Btree *p,     /* The btree to be checked */
58826   int *aRoot,   /* An array of root pages numbers for individual trees */
58827   int nRoot,    /* Number of entries in aRoot[] */
58828   int mxErr,    /* Stop reporting errors after this many */
58829   int *pnErr    /* Write number of errors seen to this variable */
58830 ){
58831   Pgno i;
58832   int nRef;
58833   IntegrityCk sCheck;
58834   BtShared *pBt = p->pBt;
58835   char zErr[100];
58836 
58837   sqlite3BtreeEnter(p);
58838   assert( p->inTrans>TRANS_NONE && pBt->inTransaction>TRANS_NONE );
58839   nRef = sqlite3PagerRefcount(pBt->pPager);
58840   sCheck.pBt = pBt;
58841   sCheck.pPager = pBt->pPager;
58842   sCheck.nPage = btreePagecount(sCheck.pBt);
58843   sCheck.mxErr = mxErr;
58844   sCheck.nErr = 0;
58845   sCheck.mallocFailed = 0;
58846   *pnErr = 0;
58847   if( sCheck.nPage==0 ){
58848     sqlite3BtreeLeave(p);
58849     return 0;
58850   }
58851 
58852   sCheck.aPgRef = sqlite3MallocZero((sCheck.nPage / 8)+ 1);
58853   if( !sCheck.aPgRef ){
58854     *pnErr = 1;
58855     sqlite3BtreeLeave(p);
58856     return 0;
58857   }
58858   i = PENDING_BYTE_PAGE(pBt);
58859   if( i<=sCheck.nPage ) setPageReferenced(&sCheck, i);
58860   sqlite3StrAccumInit(&sCheck.errMsg, zErr, sizeof(zErr), SQLITE_MAX_LENGTH);
58861   sCheck.errMsg.useMalloc = 2;
58862 
58863   /* Check the integrity of the freelist
58864   */
58865   checkList(&sCheck, 1, get4byte(&pBt->pPage1->aData[32]),
58866             get4byte(&pBt->pPage1->aData[36]), "Main freelist: ");
58867 
58868   /* Check all the tables.
58869   */
58870   for(i=0; (int)i<nRoot && sCheck.mxErr; i++){
58871     if( aRoot[i]==0 ) continue;
58872 #ifndef SQLITE_OMIT_AUTOVACUUM
58873     if( pBt->autoVacuum && aRoot[i]>1 ){
58874       checkPtrmap(&sCheck, aRoot[i], PTRMAP_ROOTPAGE, 0, 0);
58875     }
58876 #endif
58877     checkTreePage(&sCheck, aRoot[i], "List of tree roots: ", NULL, NULL);
58878   }
58879 
58880   /* Make sure every page in the file is referenced
58881   */
58882   for(i=1; i<=sCheck.nPage && sCheck.mxErr; i++){
58883 #ifdef SQLITE_OMIT_AUTOVACUUM
58884     if( getPageReferenced(&sCheck, i)==0 ){
58885       checkAppendMsg(&sCheck, 0, "Page %d is never used", i);
58886     }
58887 #else
58888     /* If the database supports auto-vacuum, make sure no tables contain
58889     ** references to pointer-map pages.
58890     */
58891     if( getPageReferenced(&sCheck, i)==0 &&
58892        (PTRMAP_PAGENO(pBt, i)!=i || !pBt->autoVacuum) ){
58893       checkAppendMsg(&sCheck, 0, "Page %d is never used", i);
58894     }
58895     if( getPageReferenced(&sCheck, i)!=0 &&
58896        (PTRMAP_PAGENO(pBt, i)==i && pBt->autoVacuum) ){
58897       checkAppendMsg(&sCheck, 0, "Pointer map page %d is referenced", i);
58898     }
58899 #endif
58900   }
58901 
58902   /* Make sure this analysis did not leave any unref() pages.
58903   ** This is an internal consistency check; an integrity check
58904   ** of the integrity check.
58905   */
58906   if( NEVER(nRef != sqlite3PagerRefcount(pBt->pPager)) ){
58907     checkAppendMsg(&sCheck, 0,
58908       "Outstanding page count goes from %d to %d during this analysis",
58909       nRef, sqlite3PagerRefcount(pBt->pPager)
58910     );
58911   }
58912 
58913   /* Clean  up and report errors.
58914   */
58915   sqlite3BtreeLeave(p);
58916   sqlite3_free(sCheck.aPgRef);
58917   if( sCheck.mallocFailed ){
58918     sqlite3StrAccumReset(&sCheck.errMsg);
58919     *pnErr = sCheck.nErr+1;
58920     return 0;
58921   }
58922   *pnErr = sCheck.nErr;
58923   if( sCheck.nErr==0 ) sqlite3StrAccumReset(&sCheck.errMsg);
58924   return sqlite3StrAccumFinish(&sCheck.errMsg);
58925 }
58926 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
58927 
58928 /*
58929 ** Return the full pathname of the underlying database file.  Return
58930 ** an empty string if the database is in-memory or a TEMP database.
58931 **
58932 ** The pager filename is invariant as long as the pager is
58933 ** open so it is safe to access without the BtShared mutex.
58934 */
58935 SQLITE_PRIVATE const char *sqlite3BtreeGetFilename(Btree *p){
58936   assert( p->pBt->pPager!=0 );
58937   return sqlite3PagerFilename(p->pBt->pPager, 1);
58938 }
58939 
58940 /*
58941 ** Return the pathname of the journal file for this database. The return
58942 ** value of this routine is the same regardless of whether the journal file
58943 ** has been created or not.
58944 **
58945 ** The pager journal filename is invariant as long as the pager is
58946 ** open so it is safe to access without the BtShared mutex.
58947 */
58948 SQLITE_PRIVATE const char *sqlite3BtreeGetJournalname(Btree *p){
58949   assert( p->pBt->pPager!=0 );
58950   return sqlite3PagerJournalname(p->pBt->pPager);
58951 }
58952 
58953 /*
58954 ** Return non-zero if a transaction is active.
58955 */
58956 SQLITE_PRIVATE int sqlite3BtreeIsInTrans(Btree *p){
58957   assert( p==0 || sqlite3_mutex_held(p->db->mutex) );
58958   return (p && (p->inTrans==TRANS_WRITE));
58959 }
58960 
58961 #ifndef SQLITE_OMIT_WAL
58962 /*
58963 ** Run a checkpoint on the Btree passed as the first argument.
58964 **
58965 ** Return SQLITE_LOCKED if this or any other connection has an open
58966 ** transaction on the shared-cache the argument Btree is connected to.
58967 **
58968 ** Parameter eMode is one of SQLITE_CHECKPOINT_PASSIVE, FULL or RESTART.
58969 */
58970 SQLITE_PRIVATE int sqlite3BtreeCheckpoint(Btree *p, int eMode, int *pnLog, int *pnCkpt){
58971   int rc = SQLITE_OK;
58972   if( p ){
58973     BtShared *pBt = p->pBt;
58974     sqlite3BtreeEnter(p);
58975     if( pBt->inTransaction!=TRANS_NONE ){
58976       rc = SQLITE_LOCKED;
58977     }else{
58978       rc = sqlite3PagerCheckpoint(pBt->pPager, eMode, pnLog, pnCkpt);
58979     }
58980     sqlite3BtreeLeave(p);
58981   }
58982   return rc;
58983 }
58984 #endif
58985 
58986 /*
58987 ** Return non-zero if a read (or write) transaction is active.
58988 */
58989 SQLITE_PRIVATE int sqlite3BtreeIsInReadTrans(Btree *p){
58990   assert( p );
58991   assert( sqlite3_mutex_held(p->db->mutex) );
58992   return p->inTrans!=TRANS_NONE;
58993 }
58994 
58995 SQLITE_PRIVATE int sqlite3BtreeIsInBackup(Btree *p){
58996   assert( p );
58997   assert( sqlite3_mutex_held(p->db->mutex) );
58998   return p->nBackup!=0;
58999 }
59000 
59001 /*
59002 ** This function returns a pointer to a blob of memory associated with
59003 ** a single shared-btree. The memory is used by client code for its own
59004 ** purposes (for example, to store a high-level schema associated with
59005 ** the shared-btree). The btree layer manages reference counting issues.
59006 **
59007 ** The first time this is called on a shared-btree, nBytes bytes of memory
59008 ** are allocated, zeroed, and returned to the caller. For each subsequent
59009 ** call the nBytes parameter is ignored and a pointer to the same blob
59010 ** of memory returned.
59011 **
59012 ** If the nBytes parameter is 0 and the blob of memory has not yet been
59013 ** allocated, a null pointer is returned. If the blob has already been
59014 ** allocated, it is returned as normal.
59015 **
59016 ** Just before the shared-btree is closed, the function passed as the
59017 ** xFree argument when the memory allocation was made is invoked on the
59018 ** blob of allocated memory. The xFree function should not call sqlite3_free()
59019 ** on the memory, the btree layer does that.
59020 */
59021 SQLITE_PRIVATE void *sqlite3BtreeSchema(Btree *p, int nBytes, void(*xFree)(void *)){
59022   BtShared *pBt = p->pBt;
59023   sqlite3BtreeEnter(p);
59024   if( !pBt->pSchema && nBytes ){
59025     pBt->pSchema = sqlite3DbMallocZero(0, nBytes);
59026     pBt->xFreeSchema = xFree;
59027   }
59028   sqlite3BtreeLeave(p);
59029   return pBt->pSchema;
59030 }
59031 
59032 /*
59033 ** Return SQLITE_LOCKED_SHAREDCACHE if another user of the same shared
59034 ** btree as the argument handle holds an exclusive lock on the
59035 ** sqlite_master table. Otherwise SQLITE_OK.
59036 */
59037 SQLITE_PRIVATE int sqlite3BtreeSchemaLocked(Btree *p){
59038   int rc;
59039   assert( sqlite3_mutex_held(p->db->mutex) );
59040   sqlite3BtreeEnter(p);
59041   rc = querySharedCacheTableLock(p, MASTER_ROOT, READ_LOCK);
59042   assert( rc==SQLITE_OK || rc==SQLITE_LOCKED_SHAREDCACHE );
59043   sqlite3BtreeLeave(p);
59044   return rc;
59045 }
59046 
59047 
59048 #ifndef SQLITE_OMIT_SHARED_CACHE
59049 /*
59050 ** Obtain a lock on the table whose root page is iTab.  The
59051 ** lock is a write lock if isWritelock is true or a read lock
59052 ** if it is false.
59053 */
59054 SQLITE_PRIVATE int sqlite3BtreeLockTable(Btree *p, int iTab, u8 isWriteLock){
59055   int rc = SQLITE_OK;
59056   assert( p->inTrans!=TRANS_NONE );
59057   if( p->sharable ){
59058     u8 lockType = READ_LOCK + isWriteLock;
59059     assert( READ_LOCK+1==WRITE_LOCK );
59060     assert( isWriteLock==0 || isWriteLock==1 );
59061 
59062     sqlite3BtreeEnter(p);
59063     rc = querySharedCacheTableLock(p, iTab, lockType);
59064     if( rc==SQLITE_OK ){
59065       rc = setSharedCacheTableLock(p, iTab, lockType);
59066     }
59067     sqlite3BtreeLeave(p);
59068   }
59069   return rc;
59070 }
59071 #endif
59072 
59073 #ifndef SQLITE_OMIT_INCRBLOB
59074 /*
59075 ** Argument pCsr must be a cursor opened for writing on an
59076 ** INTKEY table currently pointing at a valid table entry.
59077 ** This function modifies the data stored as part of that entry.
59078 **
59079 ** Only the data content may only be modified, it is not possible to
59080 ** change the length of the data stored. If this function is called with
59081 ** parameters that attempt to write past the end of the existing data,
59082 ** no modifications are made and SQLITE_CORRUPT is returned.
59083 */
59084 SQLITE_PRIVATE int sqlite3BtreePutData(BtCursor *pCsr, u32 offset, u32 amt, void *z){
59085   int rc;
59086   assert( cursorHoldsMutex(pCsr) );
59087   assert( sqlite3_mutex_held(pCsr->pBtree->db->mutex) );
59088   assert( pCsr->isIncrblobHandle );
59089 
59090   rc = restoreCursorPosition(pCsr);
59091   if( rc!=SQLITE_OK ){
59092     return rc;
59093   }
59094   assert( pCsr->eState!=CURSOR_REQUIRESEEK );
59095   if( pCsr->eState!=CURSOR_VALID ){
59096     return SQLITE_ABORT;
59097   }
59098 
59099   /* Save the positions of all other cursors open on this table. This is
59100   ** required in case any of them are holding references to an xFetch
59101   ** version of the b-tree page modified by the accessPayload call below.
59102   **
59103   ** Note that pCsr must be open on a BTREE_INTKEY table and saveCursorPosition()
59104   ** and hence saveAllCursors() cannot fail on a BTREE_INTKEY table, hence
59105   ** saveAllCursors can only return SQLITE_OK.
59106   */
59107   VVA_ONLY(rc =) saveAllCursors(pCsr->pBt, pCsr->pgnoRoot, pCsr);
59108   assert( rc==SQLITE_OK );
59109 
59110   /* Check some assumptions:
59111   **   (a) the cursor is open for writing,
59112   **   (b) there is a read/write transaction open,
59113   **   (c) the connection holds a write-lock on the table (if required),
59114   **   (d) there are no conflicting read-locks, and
59115   **   (e) the cursor points at a valid row of an intKey table.
59116   */
59117   if( !pCsr->wrFlag ){
59118     return SQLITE_READONLY;
59119   }
59120   assert( (pCsr->pBt->btsFlags & BTS_READ_ONLY)==0
59121               && pCsr->pBt->inTransaction==TRANS_WRITE );
59122   assert( hasSharedCacheTableLock(pCsr->pBtree, pCsr->pgnoRoot, 0, 2) );
59123   assert( !hasReadConflicts(pCsr->pBtree, pCsr->pgnoRoot) );
59124   assert( pCsr->apPage[pCsr->iPage]->intKey );
59125 
59126   return accessPayload(pCsr, offset, amt, (unsigned char *)z, 1);
59127 }
59128 
59129 /*
59130 ** Set a flag on this cursor to cache the locations of pages from the
59131 ** overflow list for the current row. This is used by cursors opened
59132 ** for incremental blob IO only.
59133 **
59134 ** This function sets a flag only. The actual page location cache
59135 ** (stored in BtCursor.aOverflow[]) is allocated and used by function
59136 ** accessPayload() (the worker function for sqlite3BtreeData() and
59137 ** sqlite3BtreePutData()).
59138 */
59139 SQLITE_PRIVATE void sqlite3BtreeCacheOverflow(BtCursor *pCur){
59140   assert( cursorHoldsMutex(pCur) );
59141   assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
59142   invalidateOverflowCache(pCur);
59143   pCur->isIncrblobHandle = 1;
59144 }
59145 #endif
59146 
59147 /*
59148 ** Set both the "read version" (single byte at byte offset 18) and
59149 ** "write version" (single byte at byte offset 19) fields in the database
59150 ** header to iVersion.
59151 */
59152 SQLITE_PRIVATE int sqlite3BtreeSetVersion(Btree *pBtree, int iVersion){
59153   BtShared *pBt = pBtree->pBt;
59154   int rc;                         /* Return code */
59155 
59156   assert( iVersion==1 || iVersion==2 );
59157 
59158   /* If setting the version fields to 1, do not automatically open the
59159   ** WAL connection, even if the version fields are currently set to 2.
59160   */
59161   pBt->btsFlags &= ~BTS_NO_WAL;
59162   if( iVersion==1 ) pBt->btsFlags |= BTS_NO_WAL;
59163 
59164   rc = sqlite3BtreeBeginTrans(pBtree, 0);
59165   if( rc==SQLITE_OK ){
59166     u8 *aData = pBt->pPage1->aData;
59167     if( aData[18]!=(u8)iVersion || aData[19]!=(u8)iVersion ){
59168       rc = sqlite3BtreeBeginTrans(pBtree, 2);
59169       if( rc==SQLITE_OK ){
59170         rc = sqlite3PagerWrite(pBt->pPage1->pDbPage);
59171         if( rc==SQLITE_OK ){
59172           aData[18] = (u8)iVersion;
59173           aData[19] = (u8)iVersion;
59174         }
59175       }
59176     }
59177   }
59178 
59179   pBt->btsFlags &= ~BTS_NO_WAL;
59180   return rc;
59181 }
59182 
59183 /*
59184 ** set the mask of hint flags for cursor pCsr. Currently the only valid
59185 ** values are 0 and BTREE_BULKLOAD.
59186 */
59187 SQLITE_PRIVATE void sqlite3BtreeCursorHints(BtCursor *pCsr, unsigned int mask){
59188   assert( mask==BTREE_BULKLOAD || mask==0 );
59189   pCsr->hints = mask;
59190 }
59191 
59192 /************** End of btree.c ***********************************************/
59193 /************** Begin file backup.c ******************************************/
59194 /*
59195 ** 2009 January 28
59196 **
59197 ** The author disclaims copyright to this source code.  In place of
59198 ** a legal notice, here is a blessing:
59199 **
59200 **    May you do good and not evil.
59201 **    May you find forgiveness for yourself and forgive others.
59202 **    May you share freely, never taking more than you give.
59203 **
59204 *************************************************************************
59205 ** This file contains the implementation of the sqlite3_backup_XXX()
59206 ** API functions and the related features.
59207 */
59208 
59209 /*
59210 ** Structure allocated for each backup operation.
59211 */
59212 struct sqlite3_backup {
59213   sqlite3* pDestDb;        /* Destination database handle */
59214   Btree *pDest;            /* Destination b-tree file */
59215   u32 iDestSchema;         /* Original schema cookie in destination */
59216   int bDestLocked;         /* True once a write-transaction is open on pDest */
59217 
59218   Pgno iNext;              /* Page number of the next source page to copy */
59219   sqlite3* pSrcDb;         /* Source database handle */
59220   Btree *pSrc;             /* Source b-tree file */
59221 
59222   int rc;                  /* Backup process error code */
59223 
59224   /* These two variables are set by every call to backup_step(). They are
59225   ** read by calls to backup_remaining() and backup_pagecount().
59226   */
59227   Pgno nRemaining;         /* Number of pages left to copy */
59228   Pgno nPagecount;         /* Total number of pages to copy */
59229 
59230   int isAttached;          /* True once backup has been registered with pager */
59231   sqlite3_backup *pNext;   /* Next backup associated with source pager */
59232 };
59233 
59234 /*
59235 ** THREAD SAFETY NOTES:
59236 **
59237 **   Once it has been created using backup_init(), a single sqlite3_backup
59238 **   structure may be accessed via two groups of thread-safe entry points:
59239 **
59240 **     * Via the sqlite3_backup_XXX() API function backup_step() and
59241 **       backup_finish(). Both these functions obtain the source database
59242 **       handle mutex and the mutex associated with the source BtShared
59243 **       structure, in that order.
59244 **
59245 **     * Via the BackupUpdate() and BackupRestart() functions, which are
59246 **       invoked by the pager layer to report various state changes in
59247 **       the page cache associated with the source database. The mutex
59248 **       associated with the source database BtShared structure will always
59249 **       be held when either of these functions are invoked.
59250 **
59251 **   The other sqlite3_backup_XXX() API functions, backup_remaining() and
59252 **   backup_pagecount() are not thread-safe functions. If they are called
59253 **   while some other thread is calling backup_step() or backup_finish(),
59254 **   the values returned may be invalid. There is no way for a call to
59255 **   BackupUpdate() or BackupRestart() to interfere with backup_remaining()
59256 **   or backup_pagecount().
59257 **
59258 **   Depending on the SQLite configuration, the database handles and/or
59259 **   the Btree objects may have their own mutexes that require locking.
59260 **   Non-sharable Btrees (in-memory databases for example), do not have
59261 **   associated mutexes.
59262 */
59263 
59264 /*
59265 ** Return a pointer corresponding to database zDb (i.e. "main", "temp")
59266 ** in connection handle pDb. If such a database cannot be found, return
59267 ** a NULL pointer and write an error message to pErrorDb.
59268 **
59269 ** If the "temp" database is requested, it may need to be opened by this
59270 ** function. If an error occurs while doing so, return 0 and write an
59271 ** error message to pErrorDb.
59272 */
59273 static Btree *findBtree(sqlite3 *pErrorDb, sqlite3 *pDb, const char *zDb){
59274   int i = sqlite3FindDbName(pDb, zDb);
59275 
59276   if( i==1 ){
59277     Parse *pParse;
59278     int rc = 0;
59279     pParse = sqlite3StackAllocZero(pErrorDb, sizeof(*pParse));
59280     if( pParse==0 ){
59281       sqlite3Error(pErrorDb, SQLITE_NOMEM, "out of memory");
59282       rc = SQLITE_NOMEM;
59283     }else{
59284       pParse->db = pDb;
59285       if( sqlite3OpenTempDatabase(pParse) ){
59286         sqlite3Error(pErrorDb, pParse->rc, "%s", pParse->zErrMsg);
59287         rc = SQLITE_ERROR;
59288       }
59289       sqlite3DbFree(pErrorDb, pParse->zErrMsg);
59290       sqlite3ParserReset(pParse);
59291       sqlite3StackFree(pErrorDb, pParse);
59292     }
59293     if( rc ){
59294       return 0;
59295     }
59296   }
59297 
59298   if( i<0 ){
59299     sqlite3Error(pErrorDb, SQLITE_ERROR, "unknown database %s", zDb);
59300     return 0;
59301   }
59302 
59303   return pDb->aDb[i].pBt;
59304 }
59305 
59306 /*
59307 ** Attempt to set the page size of the destination to match the page size
59308 ** of the source.
59309 */
59310 static int setDestPgsz(sqlite3_backup *p){
59311   int rc;
59312   rc = sqlite3BtreeSetPageSize(p->pDest,sqlite3BtreeGetPageSize(p->pSrc),-1,0);
59313   return rc;
59314 }
59315 
59316 /*
59317 ** Create an sqlite3_backup process to copy the contents of zSrcDb from
59318 ** connection handle pSrcDb to zDestDb in pDestDb. If successful, return
59319 ** a pointer to the new sqlite3_backup object.
59320 **
59321 ** If an error occurs, NULL is returned and an error code and error message
59322 ** stored in database handle pDestDb.
59323 */
59324 SQLITE_API sqlite3_backup *sqlite3_backup_init(
59325   sqlite3* pDestDb,                     /* Database to write to */
59326   const char *zDestDb,                  /* Name of database within pDestDb */
59327   sqlite3* pSrcDb,                      /* Database connection to read from */
59328   const char *zSrcDb                    /* Name of database within pSrcDb */
59329 ){
59330   sqlite3_backup *p;                    /* Value to return */
59331 
59332   /* Lock the source database handle. The destination database
59333   ** handle is not locked in this routine, but it is locked in
59334   ** sqlite3_backup_step(). The user is required to ensure that no
59335   ** other thread accesses the destination handle for the duration
59336   ** of the backup operation.  Any attempt to use the destination
59337   ** database connection while a backup is in progress may cause
59338   ** a malfunction or a deadlock.
59339   */
59340   sqlite3_mutex_enter(pSrcDb->mutex);
59341   sqlite3_mutex_enter(pDestDb->mutex);
59342 
59343   if( pSrcDb==pDestDb ){
59344     sqlite3Error(
59345         pDestDb, SQLITE_ERROR, "source and destination must be distinct"
59346     );
59347     p = 0;
59348   }else {
59349     /* Allocate space for a new sqlite3_backup object...
59350     ** EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a
59351     ** call to sqlite3_backup_init() and is destroyed by a call to
59352     ** sqlite3_backup_finish(). */
59353     p = (sqlite3_backup *)sqlite3MallocZero(sizeof(sqlite3_backup));
59354     if( !p ){
59355       sqlite3Error(pDestDb, SQLITE_NOMEM, 0);
59356     }
59357   }
59358 
59359   /* If the allocation succeeded, populate the new object. */
59360   if( p ){
59361     p->pSrc = findBtree(pDestDb, pSrcDb, zSrcDb);
59362     p->pDest = findBtree(pDestDb, pDestDb, zDestDb);
59363     p->pDestDb = pDestDb;
59364     p->pSrcDb = pSrcDb;
59365     p->iNext = 1;
59366     p->isAttached = 0;
59367 
59368     if( 0==p->pSrc || 0==p->pDest || setDestPgsz(p)==SQLITE_NOMEM ){
59369       /* One (or both) of the named databases did not exist or an OOM
59370       ** error was hit.  The error has already been written into the
59371       ** pDestDb handle.  All that is left to do here is free the
59372       ** sqlite3_backup structure.
59373       */
59374       sqlite3_free(p);
59375       p = 0;
59376     }
59377   }
59378   if( p ){
59379     p->pSrc->nBackup++;
59380   }
59381 
59382   sqlite3_mutex_leave(pDestDb->mutex);
59383   sqlite3_mutex_leave(pSrcDb->mutex);
59384   return p;
59385 }
59386 
59387 /*
59388 ** Argument rc is an SQLite error code. Return true if this error is
59389 ** considered fatal if encountered during a backup operation. All errors
59390 ** are considered fatal except for SQLITE_BUSY and SQLITE_LOCKED.
59391 */
59392 static int isFatalError(int rc){
59393   return (rc!=SQLITE_OK && rc!=SQLITE_BUSY && ALWAYS(rc!=SQLITE_LOCKED));
59394 }
59395 
59396 /*
59397 ** Parameter zSrcData points to a buffer containing the data for
59398 ** page iSrcPg from the source database. Copy this data into the
59399 ** destination database.
59400 */
59401 static int backupOnePage(
59402   sqlite3_backup *p,              /* Backup handle */
59403   Pgno iSrcPg,                    /* Source database page to backup */
59404   const u8 *zSrcData,             /* Source database page data */
59405   int bUpdate                     /* True for an update, false otherwise */
59406 ){
59407   Pager * const pDestPager = sqlite3BtreePager(p->pDest);
59408   const int nSrcPgsz = sqlite3BtreeGetPageSize(p->pSrc);
59409   int nDestPgsz = sqlite3BtreeGetPageSize(p->pDest);
59410   const int nCopy = MIN(nSrcPgsz, nDestPgsz);
59411   const i64 iEnd = (i64)iSrcPg*(i64)nSrcPgsz;
59412 #ifdef SQLITE_HAS_CODEC
59413   /* Use BtreeGetReserveNoMutex() for the source b-tree, as although it is
59414   ** guaranteed that the shared-mutex is held by this thread, handle
59415   ** p->pSrc may not actually be the owner.  */
59416   int nSrcReserve = sqlite3BtreeGetReserveNoMutex(p->pSrc);
59417   int nDestReserve = sqlite3BtreeGetReserve(p->pDest);
59418 #endif
59419   int rc = SQLITE_OK;
59420   i64 iOff;
59421 
59422   assert( sqlite3BtreeGetReserveNoMutex(p->pSrc)>=0 );
59423   assert( p->bDestLocked );
59424   assert( !isFatalError(p->rc) );
59425   assert( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) );
59426   assert( zSrcData );
59427 
59428   /* Catch the case where the destination is an in-memory database and the
59429   ** page sizes of the source and destination differ.
59430   */
59431   if( nSrcPgsz!=nDestPgsz && sqlite3PagerIsMemdb(pDestPager) ){
59432     rc = SQLITE_READONLY;
59433   }
59434 
59435 #ifdef SQLITE_HAS_CODEC
59436   /* Backup is not possible if the page size of the destination is changing
59437   ** and a codec is in use.
59438   */
59439   if( nSrcPgsz!=nDestPgsz && sqlite3PagerGetCodec(pDestPager)!=0 ){
59440     rc = SQLITE_READONLY;
59441   }
59442 
59443   /* Backup is not possible if the number of bytes of reserve space differ
59444   ** between source and destination.  If there is a difference, try to
59445   ** fix the destination to agree with the source.  If that is not possible,
59446   ** then the backup cannot proceed.
59447   */
59448   if( nSrcReserve!=nDestReserve ){
59449     u32 newPgsz = nSrcPgsz;
59450     rc = sqlite3PagerSetPagesize(pDestPager, &newPgsz, nSrcReserve);
59451     if( rc==SQLITE_OK && newPgsz!=nSrcPgsz ) rc = SQLITE_READONLY;
59452   }
59453 #endif
59454 
59455   /* This loop runs once for each destination page spanned by the source
59456   ** page. For each iteration, variable iOff is set to the byte offset
59457   ** of the destination page.
59458   */
59459   for(iOff=iEnd-(i64)nSrcPgsz; rc==SQLITE_OK && iOff<iEnd; iOff+=nDestPgsz){
59460     DbPage *pDestPg = 0;
59461     Pgno iDest = (Pgno)(iOff/nDestPgsz)+1;
59462     if( iDest==PENDING_BYTE_PAGE(p->pDest->pBt) ) continue;
59463     if( SQLITE_OK==(rc = sqlite3PagerGet(pDestPager, iDest, &pDestPg))
59464      && SQLITE_OK==(rc = sqlite3PagerWrite(pDestPg))
59465     ){
59466       const u8 *zIn = &zSrcData[iOff%nSrcPgsz];
59467       u8 *zDestData = sqlite3PagerGetData(pDestPg);
59468       u8 *zOut = &zDestData[iOff%nDestPgsz];
59469 
59470       /* Copy the data from the source page into the destination page.
59471       ** Then clear the Btree layer MemPage.isInit flag. Both this module
59472       ** and the pager code use this trick (clearing the first byte
59473       ** of the page 'extra' space to invalidate the Btree layers
59474       ** cached parse of the page). MemPage.isInit is marked
59475       ** "MUST BE FIRST" for this purpose.
59476       */
59477       memcpy(zOut, zIn, nCopy);
59478       ((u8 *)sqlite3PagerGetExtra(pDestPg))[0] = 0;
59479       if( iOff==0 && bUpdate==0 ){
59480         sqlite3Put4byte(&zOut[28], sqlite3BtreeLastPage(p->pSrc));
59481       }
59482     }
59483     sqlite3PagerUnref(pDestPg);
59484   }
59485 
59486   return rc;
59487 }
59488 
59489 /*
59490 ** If pFile is currently larger than iSize bytes, then truncate it to
59491 ** exactly iSize bytes. If pFile is not larger than iSize bytes, then
59492 ** this function is a no-op.
59493 **
59494 ** Return SQLITE_OK if everything is successful, or an SQLite error
59495 ** code if an error occurs.
59496 */
59497 static int backupTruncateFile(sqlite3_file *pFile, i64 iSize){
59498   i64 iCurrent;
59499   int rc = sqlite3OsFileSize(pFile, &iCurrent);
59500   if( rc==SQLITE_OK && iCurrent>iSize ){
59501     rc = sqlite3OsTruncate(pFile, iSize);
59502   }
59503   return rc;
59504 }
59505 
59506 /*
59507 ** Register this backup object with the associated source pager for
59508 ** callbacks when pages are changed or the cache invalidated.
59509 */
59510 static void attachBackupObject(sqlite3_backup *p){
59511   sqlite3_backup **pp;
59512   assert( sqlite3BtreeHoldsMutex(p->pSrc) );
59513   pp = sqlite3PagerBackupPtr(sqlite3BtreePager(p->pSrc));
59514   p->pNext = *pp;
59515   *pp = p;
59516   p->isAttached = 1;
59517 }
59518 
59519 /*
59520 ** Copy nPage pages from the source b-tree to the destination.
59521 */
59522 SQLITE_API int sqlite3_backup_step(sqlite3_backup *p, int nPage){
59523   int rc;
59524   int destMode;       /* Destination journal mode */
59525   int pgszSrc = 0;    /* Source page size */
59526   int pgszDest = 0;   /* Destination page size */
59527 
59528   sqlite3_mutex_enter(p->pSrcDb->mutex);
59529   sqlite3BtreeEnter(p->pSrc);
59530   if( p->pDestDb ){
59531     sqlite3_mutex_enter(p->pDestDb->mutex);
59532   }
59533 
59534   rc = p->rc;
59535   if( !isFatalError(rc) ){
59536     Pager * const pSrcPager = sqlite3BtreePager(p->pSrc);     /* Source pager */
59537     Pager * const pDestPager = sqlite3BtreePager(p->pDest);   /* Dest pager */
59538     int ii;                            /* Iterator variable */
59539     int nSrcPage = -1;                 /* Size of source db in pages */
59540     int bCloseTrans = 0;               /* True if src db requires unlocking */
59541 
59542     /* If the source pager is currently in a write-transaction, return
59543     ** SQLITE_BUSY immediately.
59544     */
59545     if( p->pDestDb && p->pSrc->pBt->inTransaction==TRANS_WRITE ){
59546       rc = SQLITE_BUSY;
59547     }else{
59548       rc = SQLITE_OK;
59549     }
59550 
59551     /* Lock the destination database, if it is not locked already. */
59552     if( SQLITE_OK==rc && p->bDestLocked==0
59553      && SQLITE_OK==(rc = sqlite3BtreeBeginTrans(p->pDest, 2))
59554     ){
59555       p->bDestLocked = 1;
59556       sqlite3BtreeGetMeta(p->pDest, BTREE_SCHEMA_VERSION, &p->iDestSchema);
59557     }
59558 
59559     /* If there is no open read-transaction on the source database, open
59560     ** one now. If a transaction is opened here, then it will be closed
59561     ** before this function exits.
59562     */
59563     if( rc==SQLITE_OK && 0==sqlite3BtreeIsInReadTrans(p->pSrc) ){
59564       rc = sqlite3BtreeBeginTrans(p->pSrc, 0);
59565       bCloseTrans = 1;
59566     }
59567 
59568     /* Do not allow backup if the destination database is in WAL mode
59569     ** and the page sizes are different between source and destination */
59570     pgszSrc = sqlite3BtreeGetPageSize(p->pSrc);
59571     pgszDest = sqlite3BtreeGetPageSize(p->pDest);
59572     destMode = sqlite3PagerGetJournalMode(sqlite3BtreePager(p->pDest));
59573     if( SQLITE_OK==rc && destMode==PAGER_JOURNALMODE_WAL && pgszSrc!=pgszDest ){
59574       rc = SQLITE_READONLY;
59575     }
59576 
59577     /* Now that there is a read-lock on the source database, query the
59578     ** source pager for the number of pages in the database.
59579     */
59580     nSrcPage = (int)sqlite3BtreeLastPage(p->pSrc);
59581     assert( nSrcPage>=0 );
59582     for(ii=0; (nPage<0 || ii<nPage) && p->iNext<=(Pgno)nSrcPage && !rc; ii++){
59583       const Pgno iSrcPg = p->iNext;                 /* Source page number */
59584       if( iSrcPg!=PENDING_BYTE_PAGE(p->pSrc->pBt) ){
59585         DbPage *pSrcPg;                             /* Source page object */
59586         rc = sqlite3PagerAcquire(pSrcPager, iSrcPg, &pSrcPg,
59587                                  PAGER_GET_READONLY);
59588         if( rc==SQLITE_OK ){
59589           rc = backupOnePage(p, iSrcPg, sqlite3PagerGetData(pSrcPg), 0);
59590           sqlite3PagerUnref(pSrcPg);
59591         }
59592       }
59593       p->iNext++;
59594     }
59595     if( rc==SQLITE_OK ){
59596       p->nPagecount = nSrcPage;
59597       p->nRemaining = nSrcPage+1-p->iNext;
59598       if( p->iNext>(Pgno)nSrcPage ){
59599         rc = SQLITE_DONE;
59600       }else if( !p->isAttached ){
59601         attachBackupObject(p);
59602       }
59603     }
59604 
59605     /* Update the schema version field in the destination database. This
59606     ** is to make sure that the schema-version really does change in
59607     ** the case where the source and destination databases have the
59608     ** same schema version.
59609     */
59610     if( rc==SQLITE_DONE ){
59611       if( nSrcPage==0 ){
59612         rc = sqlite3BtreeNewDb(p->pDest);
59613         nSrcPage = 1;
59614       }
59615       if( rc==SQLITE_OK || rc==SQLITE_DONE ){
59616         rc = sqlite3BtreeUpdateMeta(p->pDest,1,p->iDestSchema+1);
59617       }
59618       if( rc==SQLITE_OK ){
59619         if( p->pDestDb ){
59620           sqlite3ResetAllSchemasOfConnection(p->pDestDb);
59621         }
59622         if( destMode==PAGER_JOURNALMODE_WAL ){
59623           rc = sqlite3BtreeSetVersion(p->pDest, 2);
59624         }
59625       }
59626       if( rc==SQLITE_OK ){
59627         int nDestTruncate;
59628         /* Set nDestTruncate to the final number of pages in the destination
59629         ** database. The complication here is that the destination page
59630         ** size may be different to the source page size.
59631         **
59632         ** If the source page size is smaller than the destination page size,
59633         ** round up. In this case the call to sqlite3OsTruncate() below will
59634         ** fix the size of the file. However it is important to call
59635         ** sqlite3PagerTruncateImage() here so that any pages in the
59636         ** destination file that lie beyond the nDestTruncate page mark are
59637         ** journalled by PagerCommitPhaseOne() before they are destroyed
59638         ** by the file truncation.
59639         */
59640         assert( pgszSrc==sqlite3BtreeGetPageSize(p->pSrc) );
59641         assert( pgszDest==sqlite3BtreeGetPageSize(p->pDest) );
59642         if( pgszSrc<pgszDest ){
59643           int ratio = pgszDest/pgszSrc;
59644           nDestTruncate = (nSrcPage+ratio-1)/ratio;
59645           if( nDestTruncate==(int)PENDING_BYTE_PAGE(p->pDest->pBt) ){
59646             nDestTruncate--;
59647           }
59648         }else{
59649           nDestTruncate = nSrcPage * (pgszSrc/pgszDest);
59650         }
59651         assert( nDestTruncate>0 );
59652 
59653         if( pgszSrc<pgszDest ){
59654           /* If the source page-size is smaller than the destination page-size,
59655           ** two extra things may need to happen:
59656           **
59657           **   * The destination may need to be truncated, and
59658           **
59659           **   * Data stored on the pages immediately following the
59660           **     pending-byte page in the source database may need to be
59661           **     copied into the destination database.
59662           */
59663           const i64 iSize = (i64)pgszSrc * (i64)nSrcPage;
59664           sqlite3_file * const pFile = sqlite3PagerFile(pDestPager);
59665           Pgno iPg;
59666           int nDstPage;
59667           i64 iOff;
59668           i64 iEnd;
59669 
59670           assert( pFile );
59671           assert( nDestTruncate==0
59672               || (i64)nDestTruncate*(i64)pgszDest >= iSize || (
59673                 nDestTruncate==(int)(PENDING_BYTE_PAGE(p->pDest->pBt)-1)
59674              && iSize>=PENDING_BYTE && iSize<=PENDING_BYTE+pgszDest
59675           ));
59676 
59677           /* This block ensures that all data required to recreate the original
59678           ** database has been stored in the journal for pDestPager and the
59679           ** journal synced to disk. So at this point we may safely modify
59680           ** the database file in any way, knowing that if a power failure
59681           ** occurs, the original database will be reconstructed from the
59682           ** journal file.  */
59683           sqlite3PagerPagecount(pDestPager, &nDstPage);
59684           for(iPg=nDestTruncate; rc==SQLITE_OK && iPg<=(Pgno)nDstPage; iPg++){
59685             if( iPg!=PENDING_BYTE_PAGE(p->pDest->pBt) ){
59686               DbPage *pPg;
59687               rc = sqlite3PagerGet(pDestPager, iPg, &pPg);
59688               if( rc==SQLITE_OK ){
59689                 rc = sqlite3PagerWrite(pPg);
59690                 sqlite3PagerUnref(pPg);
59691               }
59692             }
59693           }
59694           if( rc==SQLITE_OK ){
59695             rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 1);
59696           }
59697 
59698           /* Write the extra pages and truncate the database file as required */
59699           iEnd = MIN(PENDING_BYTE + pgszDest, iSize);
59700           for(
59701             iOff=PENDING_BYTE+pgszSrc;
59702             rc==SQLITE_OK && iOff<iEnd;
59703             iOff+=pgszSrc
59704           ){
59705             PgHdr *pSrcPg = 0;
59706             const Pgno iSrcPg = (Pgno)((iOff/pgszSrc)+1);
59707             rc = sqlite3PagerGet(pSrcPager, iSrcPg, &pSrcPg);
59708             if( rc==SQLITE_OK ){
59709               u8 *zData = sqlite3PagerGetData(pSrcPg);
59710               rc = sqlite3OsWrite(pFile, zData, pgszSrc, iOff);
59711             }
59712             sqlite3PagerUnref(pSrcPg);
59713           }
59714           if( rc==SQLITE_OK ){
59715             rc = backupTruncateFile(pFile, iSize);
59716           }
59717 
59718           /* Sync the database file to disk. */
59719           if( rc==SQLITE_OK ){
59720             rc = sqlite3PagerSync(pDestPager, 0);
59721           }
59722         }else{
59723           sqlite3PagerTruncateImage(pDestPager, nDestTruncate);
59724           rc = sqlite3PagerCommitPhaseOne(pDestPager, 0, 0);
59725         }
59726 
59727         /* Finish committing the transaction to the destination database. */
59728         if( SQLITE_OK==rc
59729          && SQLITE_OK==(rc = sqlite3BtreeCommitPhaseTwo(p->pDest, 0))
59730         ){
59731           rc = SQLITE_DONE;
59732         }
59733       }
59734     }
59735 
59736     /* If bCloseTrans is true, then this function opened a read transaction
59737     ** on the source database. Close the read transaction here. There is
59738     ** no need to check the return values of the btree methods here, as
59739     ** "committing" a read-only transaction cannot fail.
59740     */
59741     if( bCloseTrans ){
59742       TESTONLY( int rc2 );
59743       TESTONLY( rc2  = ) sqlite3BtreeCommitPhaseOne(p->pSrc, 0);
59744       TESTONLY( rc2 |= ) sqlite3BtreeCommitPhaseTwo(p->pSrc, 0);
59745       assert( rc2==SQLITE_OK );
59746     }
59747 
59748     if( rc==SQLITE_IOERR_NOMEM ){
59749       rc = SQLITE_NOMEM;
59750     }
59751     p->rc = rc;
59752   }
59753   if( p->pDestDb ){
59754     sqlite3_mutex_leave(p->pDestDb->mutex);
59755   }
59756   sqlite3BtreeLeave(p->pSrc);
59757   sqlite3_mutex_leave(p->pSrcDb->mutex);
59758   return rc;
59759 }
59760 
59761 /*
59762 ** Release all resources associated with an sqlite3_backup* handle.
59763 */
59764 SQLITE_API int sqlite3_backup_finish(sqlite3_backup *p){
59765   sqlite3_backup **pp;                 /* Ptr to head of pagers backup list */
59766   sqlite3 *pSrcDb;                     /* Source database connection */
59767   int rc;                              /* Value to return */
59768 
59769   /* Enter the mutexes */
59770   if( p==0 ) return SQLITE_OK;
59771   pSrcDb = p->pSrcDb;
59772   sqlite3_mutex_enter(pSrcDb->mutex);
59773   sqlite3BtreeEnter(p->pSrc);
59774   if( p->pDestDb ){
59775     sqlite3_mutex_enter(p->pDestDb->mutex);
59776   }
59777 
59778   /* Detach this backup from the source pager. */
59779   if( p->pDestDb ){
59780     p->pSrc->nBackup--;
59781   }
59782   if( p->isAttached ){
59783     pp = sqlite3PagerBackupPtr(sqlite3BtreePager(p->pSrc));
59784     while( *pp!=p ){
59785       pp = &(*pp)->pNext;
59786     }
59787     *pp = p->pNext;
59788   }
59789 
59790   /* If a transaction is still open on the Btree, roll it back. */
59791   sqlite3BtreeRollback(p->pDest, SQLITE_OK);
59792 
59793   /* Set the error code of the destination database handle. */
59794   rc = (p->rc==SQLITE_DONE) ? SQLITE_OK : p->rc;
59795   if( p->pDestDb ){
59796     sqlite3Error(p->pDestDb, rc, 0);
59797 
59798     /* Exit the mutexes and free the backup context structure. */
59799     sqlite3LeaveMutexAndCloseZombie(p->pDestDb);
59800   }
59801   sqlite3BtreeLeave(p->pSrc);
59802   if( p->pDestDb ){
59803     /* EVIDENCE-OF: R-64852-21591 The sqlite3_backup object is created by a
59804     ** call to sqlite3_backup_init() and is destroyed by a call to
59805     ** sqlite3_backup_finish(). */
59806     sqlite3_free(p);
59807   }
59808   sqlite3LeaveMutexAndCloseZombie(pSrcDb);
59809   return rc;
59810 }
59811 
59812 /*
59813 ** Return the number of pages still to be backed up as of the most recent
59814 ** call to sqlite3_backup_step().
59815 */
59816 SQLITE_API int sqlite3_backup_remaining(sqlite3_backup *p){
59817   return p->nRemaining;
59818 }
59819 
59820 /*
59821 ** Return the total number of pages in the source database as of the most
59822 ** recent call to sqlite3_backup_step().
59823 */
59824 SQLITE_API int sqlite3_backup_pagecount(sqlite3_backup *p){
59825   return p->nPagecount;
59826 }
59827 
59828 /*
59829 ** This function is called after the contents of page iPage of the
59830 ** source database have been modified. If page iPage has already been
59831 ** copied into the destination database, then the data written to the
59832 ** destination is now invalidated. The destination copy of iPage needs
59833 ** to be updated with the new data before the backup operation is
59834 ** complete.
59835 **
59836 ** It is assumed that the mutex associated with the BtShared object
59837 ** corresponding to the source database is held when this function is
59838 ** called.
59839 */
59840 SQLITE_PRIVATE void sqlite3BackupUpdate(sqlite3_backup *pBackup, Pgno iPage, const u8 *aData){
59841   sqlite3_backup *p;                   /* Iterator variable */
59842   for(p=pBackup; p; p=p->pNext){
59843     assert( sqlite3_mutex_held(p->pSrc->pBt->mutex) );
59844     if( !isFatalError(p->rc) && iPage<p->iNext ){
59845       /* The backup process p has already copied page iPage. But now it
59846       ** has been modified by a transaction on the source pager. Copy
59847       ** the new data into the backup.
59848       */
59849       int rc;
59850       assert( p->pDestDb );
59851       sqlite3_mutex_enter(p->pDestDb->mutex);
59852       rc = backupOnePage(p, iPage, aData, 1);
59853       sqlite3_mutex_leave(p->pDestDb->mutex);
59854       assert( rc!=SQLITE_BUSY && rc!=SQLITE_LOCKED );
59855       if( rc!=SQLITE_OK ){
59856         p->rc = rc;
59857       }
59858     }
59859   }
59860 }
59861 
59862 /*
59863 ** Restart the backup process. This is called when the pager layer
59864 ** detects that the database has been modified by an external database
59865 ** connection. In this case there is no way of knowing which of the
59866 ** pages that have been copied into the destination database are still
59867 ** valid and which are not, so the entire process needs to be restarted.
59868 **
59869 ** It is assumed that the mutex associated with the BtShared object
59870 ** corresponding to the source database is held when this function is
59871 ** called.
59872 */
59873 SQLITE_PRIVATE void sqlite3BackupRestart(sqlite3_backup *pBackup){
59874   sqlite3_backup *p;                   /* Iterator variable */
59875   for(p=pBackup; p; p=p->pNext){
59876     assert( sqlite3_mutex_held(p->pSrc->pBt->mutex) );
59877     p->iNext = 1;
59878   }
59879 }
59880 
59881 #ifndef SQLITE_OMIT_VACUUM
59882 /*
59883 ** Copy the complete content of pBtFrom into pBtTo.  A transaction
59884 ** must be active for both files.
59885 **
59886 ** The size of file pTo may be reduced by this operation. If anything
59887 ** goes wrong, the transaction on pTo is rolled back. If successful, the
59888 ** transaction is committed before returning.
59889 */
59890 SQLITE_PRIVATE int sqlite3BtreeCopyFile(Btree *pTo, Btree *pFrom){
59891   int rc;
59892   sqlite3_file *pFd;              /* File descriptor for database pTo */
59893   sqlite3_backup b;
59894   sqlite3BtreeEnter(pTo);
59895   sqlite3BtreeEnter(pFrom);
59896 
59897   assert( sqlite3BtreeIsInTrans(pTo) );
59898   pFd = sqlite3PagerFile(sqlite3BtreePager(pTo));
59899   if( pFd->pMethods ){
59900     i64 nByte = sqlite3BtreeGetPageSize(pFrom)*(i64)sqlite3BtreeLastPage(pFrom);
59901     rc = sqlite3OsFileControl(pFd, SQLITE_FCNTL_OVERWRITE, &nByte);
59902     if( rc==SQLITE_NOTFOUND ) rc = SQLITE_OK;
59903     if( rc ) goto copy_finished;
59904   }
59905 
59906   /* Set up an sqlite3_backup object. sqlite3_backup.pDestDb must be set
59907   ** to 0. This is used by the implementations of sqlite3_backup_step()
59908   ** and sqlite3_backup_finish() to detect that they are being called
59909   ** from this function, not directly by the user.
59910   */
59911   memset(&b, 0, sizeof(b));
59912   b.pSrcDb = pFrom->db;
59913   b.pSrc = pFrom;
59914   b.pDest = pTo;
59915   b.iNext = 1;
59916 
59917   /* 0x7FFFFFFF is the hard limit for the number of pages in a database
59918   ** file. By passing this as the number of pages to copy to
59919   ** sqlite3_backup_step(), we can guarantee that the copy finishes
59920   ** within a single call (unless an error occurs). The assert() statement
59921   ** checks this assumption - (p->rc) should be set to either SQLITE_DONE
59922   ** or an error code.
59923   */
59924   sqlite3_backup_step(&b, 0x7FFFFFFF);
59925   assert( b.rc!=SQLITE_OK );
59926   rc = sqlite3_backup_finish(&b);
59927   if( rc==SQLITE_OK ){
59928     pTo->pBt->btsFlags &= ~BTS_PAGESIZE_FIXED;
59929   }else{
59930     sqlite3PagerClearCache(sqlite3BtreePager(b.pDest));
59931   }
59932 
59933   assert( sqlite3BtreeIsInTrans(pTo)==0 );
59934 copy_finished:
59935   sqlite3BtreeLeave(pFrom);
59936   sqlite3BtreeLeave(pTo);
59937   return rc;
59938 }
59939 #endif /* SQLITE_OMIT_VACUUM */
59940 
59941 /************** End of backup.c **********************************************/
59942 /************** Begin file vdbemem.c *****************************************/
59943 /*
59944 ** 2004 May 26
59945 **
59946 ** The author disclaims copyright to this source code.  In place of
59947 ** a legal notice, here is a blessing:
59948 **
59949 **    May you do good and not evil.
59950 **    May you find forgiveness for yourself and forgive others.
59951 **    May you share freely, never taking more than you give.
59952 **
59953 *************************************************************************
59954 **
59955 ** This file contains code use to manipulate "Mem" structure.  A "Mem"
59956 ** stores a single value in the VDBE.  Mem is an opaque structure visible
59957 ** only within the VDBE.  Interface routines refer to a Mem using the
59958 ** name sqlite_value
59959 */
59960 
59961 /*
59962 ** If pMem is an object with a valid string representation, this routine
59963 ** ensures the internal encoding for the string representation is
59964 ** 'desiredEnc', one of SQLITE_UTF8, SQLITE_UTF16LE or SQLITE_UTF16BE.
59965 **
59966 ** If pMem is not a string object, or the encoding of the string
59967 ** representation is already stored using the requested encoding, then this
59968 ** routine is a no-op.
59969 **
59970 ** SQLITE_OK is returned if the conversion is successful (or not required).
59971 ** SQLITE_NOMEM may be returned if a malloc() fails during conversion
59972 ** between formats.
59973 */
59974 SQLITE_PRIVATE int sqlite3VdbeChangeEncoding(Mem *pMem, int desiredEnc){
59975 #ifndef SQLITE_OMIT_UTF16
59976   int rc;
59977 #endif
59978   assert( (pMem->flags&MEM_RowSet)==0 );
59979   assert( desiredEnc==SQLITE_UTF8 || desiredEnc==SQLITE_UTF16LE
59980            || desiredEnc==SQLITE_UTF16BE );
59981   if( !(pMem->flags&MEM_Str) || pMem->enc==desiredEnc ){
59982     return SQLITE_OK;
59983   }
59984   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
59985 #ifdef SQLITE_OMIT_UTF16
59986   return SQLITE_ERROR;
59987 #else
59988 
59989   /* MemTranslate() may return SQLITE_OK or SQLITE_NOMEM. If NOMEM is returned,
59990   ** then the encoding of the value may not have changed.
59991   */
59992   rc = sqlite3VdbeMemTranslate(pMem, (u8)desiredEnc);
59993   assert(rc==SQLITE_OK    || rc==SQLITE_NOMEM);
59994   assert(rc==SQLITE_OK    || pMem->enc!=desiredEnc);
59995   assert(rc==SQLITE_NOMEM || pMem->enc==desiredEnc);
59996   return rc;
59997 #endif
59998 }
59999 
60000 /*
60001 ** Make sure pMem->z points to a writable allocation of at least
60002 ** min(n,32) bytes.
60003 **
60004 ** If the bPreserve argument is true, then copy of the content of
60005 ** pMem->z into the new allocation.  pMem must be either a string or
60006 ** blob if bPreserve is true.  If bPreserve is false, any prior content
60007 ** in pMem->z is discarded.
60008 */
60009 SQLITE_PRIVATE int sqlite3VdbeMemGrow(Mem *pMem, int n, int bPreserve){
60010   assert( 1 >=
60011     ((pMem->zMalloc && pMem->zMalloc==pMem->z) ? 1 : 0) +
60012     (((pMem->flags&MEM_Dyn)&&pMem->xDel) ? 1 : 0) +
60013     ((pMem->flags&MEM_Ephem) ? 1 : 0) +
60014     ((pMem->flags&MEM_Static) ? 1 : 0)
60015   );
60016   assert( (pMem->flags&MEM_RowSet)==0 );
60017 
60018   /* If the bPreserve flag is set to true, then the memory cell must already
60019   ** contain a valid string or blob value.  */
60020   assert( bPreserve==0 || pMem->flags&(MEM_Blob|MEM_Str) );
60021   testcase( bPreserve && pMem->z==0 );
60022 
60023   if( pMem->zMalloc==0 || sqlite3DbMallocSize(pMem->db, pMem->zMalloc)<n ){
60024     if( n<32 ) n = 32;
60025     if( bPreserve && pMem->z==pMem->zMalloc ){
60026       pMem->z = pMem->zMalloc = sqlite3DbReallocOrFree(pMem->db, pMem->z, n);
60027       bPreserve = 0;
60028     }else{
60029       sqlite3DbFree(pMem->db, pMem->zMalloc);
60030       pMem->zMalloc = sqlite3DbMallocRaw(pMem->db, n);
60031     }
60032     if( pMem->zMalloc==0 ){
60033       sqlite3VdbeMemRelease(pMem);
60034       pMem->flags = MEM_Null;
60035       return SQLITE_NOMEM;
60036     }
60037   }
60038 
60039   if( pMem->z && bPreserve && pMem->z!=pMem->zMalloc ){
60040     memcpy(pMem->zMalloc, pMem->z, pMem->n);
60041   }
60042   if( (pMem->flags&MEM_Dyn)!=0 && pMem->xDel ){
60043     assert( pMem->xDel!=SQLITE_DYNAMIC );
60044     pMem->xDel((void *)(pMem->z));
60045   }
60046 
60047   pMem->z = pMem->zMalloc;
60048   pMem->flags &= ~(MEM_Ephem|MEM_Static);
60049   pMem->xDel = 0;
60050   return SQLITE_OK;
60051 }
60052 
60053 /*
60054 ** Make the given Mem object MEM_Dyn.  In other words, make it so
60055 ** that any TEXT or BLOB content is stored in memory obtained from
60056 ** malloc().  In this way, we know that the memory is safe to be
60057 ** overwritten or altered.
60058 **
60059 ** Return SQLITE_OK on success or SQLITE_NOMEM if malloc fails.
60060 */
60061 SQLITE_PRIVATE int sqlite3VdbeMemMakeWriteable(Mem *pMem){
60062   int f;
60063   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
60064   assert( (pMem->flags&MEM_RowSet)==0 );
60065   ExpandBlob(pMem);
60066   f = pMem->flags;
60067   if( (f&(MEM_Str|MEM_Blob)) && pMem->z!=pMem->zMalloc ){
60068     if( sqlite3VdbeMemGrow(pMem, pMem->n + 2, 1) ){
60069       return SQLITE_NOMEM;
60070     }
60071     pMem->z[pMem->n] = 0;
60072     pMem->z[pMem->n+1] = 0;
60073     pMem->flags |= MEM_Term;
60074 #ifdef SQLITE_DEBUG
60075     pMem->pScopyFrom = 0;
60076 #endif
60077   }
60078 
60079   return SQLITE_OK;
60080 }
60081 
60082 /*
60083 ** If the given Mem* has a zero-filled tail, turn it into an ordinary
60084 ** blob stored in dynamically allocated space.
60085 */
60086 #ifndef SQLITE_OMIT_INCRBLOB
60087 SQLITE_PRIVATE int sqlite3VdbeMemExpandBlob(Mem *pMem){
60088   if( pMem->flags & MEM_Zero ){
60089     int nByte;
60090     assert( pMem->flags&MEM_Blob );
60091     assert( (pMem->flags&MEM_RowSet)==0 );
60092     assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
60093 
60094     /* Set nByte to the number of bytes required to store the expanded blob. */
60095     nByte = pMem->n + pMem->u.nZero;
60096     if( nByte<=0 ){
60097       nByte = 1;
60098     }
60099     if( sqlite3VdbeMemGrow(pMem, nByte, 1) ){
60100       return SQLITE_NOMEM;
60101     }
60102 
60103     memset(&pMem->z[pMem->n], 0, pMem->u.nZero);
60104     pMem->n += pMem->u.nZero;
60105     pMem->flags &= ~(MEM_Zero|MEM_Term);
60106   }
60107   return SQLITE_OK;
60108 }
60109 #endif
60110 
60111 
60112 /*
60113 ** Make sure the given Mem is \u0000 terminated.
60114 */
60115 SQLITE_PRIVATE int sqlite3VdbeMemNulTerminate(Mem *pMem){
60116   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
60117   if( (pMem->flags & MEM_Term)!=0 || (pMem->flags & MEM_Str)==0 ){
60118     return SQLITE_OK;   /* Nothing to do */
60119   }
60120   if( sqlite3VdbeMemGrow(pMem, pMem->n+2, 1) ){
60121     return SQLITE_NOMEM;
60122   }
60123   pMem->z[pMem->n] = 0;
60124   pMem->z[pMem->n+1] = 0;
60125   pMem->flags |= MEM_Term;
60126   return SQLITE_OK;
60127 }
60128 
60129 /*
60130 ** Add MEM_Str to the set of representations for the given Mem.  Numbers
60131 ** are converted using sqlite3_snprintf().  Converting a BLOB to a string
60132 ** is a no-op.
60133 **
60134 ** Existing representations MEM_Int and MEM_Real are *not* invalidated.
60135 **
60136 ** A MEM_Null value will never be passed to this function. This function is
60137 ** used for converting values to text for returning to the user (i.e. via
60138 ** sqlite3_value_text()), or for ensuring that values to be used as btree
60139 ** keys are strings. In the former case a NULL pointer is returned the
60140 ** user and the later is an internal programming error.
60141 */
60142 SQLITE_PRIVATE int sqlite3VdbeMemStringify(Mem *pMem, int enc){
60143   int rc = SQLITE_OK;
60144   int fg = pMem->flags;
60145   const int nByte = 32;
60146 
60147   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
60148   assert( !(fg&MEM_Zero) );
60149   assert( !(fg&(MEM_Str|MEM_Blob)) );
60150   assert( fg&(MEM_Int|MEM_Real) );
60151   assert( (pMem->flags&MEM_RowSet)==0 );
60152   assert( EIGHT_BYTE_ALIGNMENT(pMem) );
60153 
60154 
60155   if( sqlite3VdbeMemGrow(pMem, nByte, 0) ){
60156     return SQLITE_NOMEM;
60157   }
60158 
60159   /* For a Real or Integer, use sqlite3_mprintf() to produce the UTF-8
60160   ** string representation of the value. Then, if the required encoding
60161   ** is UTF-16le or UTF-16be do a translation.
60162   **
60163   ** FIX ME: It would be better if sqlite3_snprintf() could do UTF-16.
60164   */
60165   if( fg & MEM_Int ){
60166     sqlite3_snprintf(nByte, pMem->z, "%lld", pMem->u.i);
60167   }else{
60168     assert( fg & MEM_Real );
60169     sqlite3_snprintf(nByte, pMem->z, "%!.15g", pMem->r);
60170   }
60171   pMem->n = sqlite3Strlen30(pMem->z);
60172   pMem->enc = SQLITE_UTF8;
60173   pMem->flags |= MEM_Str|MEM_Term;
60174   sqlite3VdbeChangeEncoding(pMem, enc);
60175   return rc;
60176 }
60177 
60178 /*
60179 ** Memory cell pMem contains the context of an aggregate function.
60180 ** This routine calls the finalize method for that function.  The
60181 ** result of the aggregate is stored back into pMem.
60182 **
60183 ** Return SQLITE_ERROR if the finalizer reports an error.  SQLITE_OK
60184 ** otherwise.
60185 */
60186 SQLITE_PRIVATE int sqlite3VdbeMemFinalize(Mem *pMem, FuncDef *pFunc){
60187   int rc = SQLITE_OK;
60188   if( ALWAYS(pFunc && pFunc->xFinalize) ){
60189     sqlite3_context ctx;
60190     assert( (pMem->flags & MEM_Null)!=0 || pFunc==pMem->u.pDef );
60191     assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
60192     memset(&ctx, 0, sizeof(ctx));
60193     ctx.s.flags = MEM_Null;
60194     ctx.s.db = pMem->db;
60195     ctx.pMem = pMem;
60196     ctx.pFunc = pFunc;
60197     pFunc->xFinalize(&ctx); /* IMP: R-24505-23230 */
60198     assert( 0==(pMem->flags&MEM_Dyn) && !pMem->xDel );
60199     sqlite3DbFree(pMem->db, pMem->zMalloc);
60200     memcpy(pMem, &ctx.s, sizeof(ctx.s));
60201     rc = ctx.isError;
60202   }
60203   return rc;
60204 }
60205 
60206 /*
60207 ** If the memory cell contains a string value that must be freed by
60208 ** invoking an external callback, free it now. Calling this function
60209 ** does not free any Mem.zMalloc buffer.
60210 */
60211 SQLITE_PRIVATE void sqlite3VdbeMemReleaseExternal(Mem *p){
60212   assert( p->db==0 || sqlite3_mutex_held(p->db->mutex) );
60213   if( p->flags&MEM_Agg ){
60214     sqlite3VdbeMemFinalize(p, p->u.pDef);
60215     assert( (p->flags & MEM_Agg)==0 );
60216     sqlite3VdbeMemRelease(p);
60217   }else if( p->flags&MEM_Dyn && p->xDel ){
60218     assert( (p->flags&MEM_RowSet)==0 );
60219     assert( p->xDel!=SQLITE_DYNAMIC );
60220     p->xDel((void *)p->z);
60221     p->xDel = 0;
60222   }else if( p->flags&MEM_RowSet ){
60223     sqlite3RowSetClear(p->u.pRowSet);
60224   }else if( p->flags&MEM_Frame ){
60225     sqlite3VdbeMemSetNull(p);
60226   }
60227 }
60228 
60229 /*
60230 ** Release any memory held by the Mem. This may leave the Mem in an
60231 ** inconsistent state, for example with (Mem.z==0) and
60232 ** (Mem.type==SQLITE_TEXT).
60233 */
60234 SQLITE_PRIVATE void sqlite3VdbeMemRelease(Mem *p){
60235   VdbeMemRelease(p);
60236   if( p->zMalloc ){
60237     sqlite3DbFree(p->db, p->zMalloc);
60238     p->zMalloc = 0;
60239   }
60240   p->z = 0;
60241   assert( p->xDel==0 );  /* Zeroed by VdbeMemRelease() above */
60242 }
60243 
60244 /*
60245 ** Convert a 64-bit IEEE double into a 64-bit signed integer.
60246 ** If the double is out of range of a 64-bit signed integer then
60247 ** return the closest available 64-bit signed integer.
60248 */
60249 static i64 doubleToInt64(double r){
60250 #ifdef SQLITE_OMIT_FLOATING_POINT
60251   /* When floating-point is omitted, double and int64 are the same thing */
60252   return r;
60253 #else
60254   /*
60255   ** Many compilers we encounter do not define constants for the
60256   ** minimum and maximum 64-bit integers, or they define them
60257   ** inconsistently.  And many do not understand the "LL" notation.
60258   ** So we define our own static constants here using nothing
60259   ** larger than a 32-bit integer constant.
60260   */
60261   static const i64 maxInt = LARGEST_INT64;
60262   static const i64 minInt = SMALLEST_INT64;
60263 
60264   if( r<=(double)minInt ){
60265     return minInt;
60266   }else if( r>=(double)maxInt ){
60267     return maxInt;
60268   }else{
60269     return (i64)r;
60270   }
60271 #endif
60272 }
60273 
60274 /*
60275 ** Return some kind of integer value which is the best we can do
60276 ** at representing the value that *pMem describes as an integer.
60277 ** If pMem is an integer, then the value is exact.  If pMem is
60278 ** a floating-point then the value returned is the integer part.
60279 ** If pMem is a string or blob, then we make an attempt to convert
60280 ** it into a integer and return that.  If pMem represents an
60281 ** an SQL-NULL value, return 0.
60282 **
60283 ** If pMem represents a string value, its encoding might be changed.
60284 */
60285 SQLITE_PRIVATE i64 sqlite3VdbeIntValue(Mem *pMem){
60286   int flags;
60287   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
60288   assert( EIGHT_BYTE_ALIGNMENT(pMem) );
60289   flags = pMem->flags;
60290   if( flags & MEM_Int ){
60291     return pMem->u.i;
60292   }else if( flags & MEM_Real ){
60293     return doubleToInt64(pMem->r);
60294   }else if( flags & (MEM_Str|MEM_Blob) ){
60295     i64 value = 0;
60296     assert( pMem->z || pMem->n==0 );
60297     testcase( pMem->z==0 );
60298     sqlite3Atoi64(pMem->z, &value, pMem->n, pMem->enc);
60299     return value;
60300   }else{
60301     return 0;
60302   }
60303 }
60304 
60305 /*
60306 ** Return the best representation of pMem that we can get into a
60307 ** double.  If pMem is already a double or an integer, return its
60308 ** value.  If it is a string or blob, try to convert it to a double.
60309 ** If it is a NULL, return 0.0.
60310 */
60311 SQLITE_PRIVATE double sqlite3VdbeRealValue(Mem *pMem){
60312   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
60313   assert( EIGHT_BYTE_ALIGNMENT(pMem) );
60314   if( pMem->flags & MEM_Real ){
60315     return pMem->r;
60316   }else if( pMem->flags & MEM_Int ){
60317     return (double)pMem->u.i;
60318   }else if( pMem->flags & (MEM_Str|MEM_Blob) ){
60319     /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
60320     double val = (double)0;
60321     sqlite3AtoF(pMem->z, &val, pMem->n, pMem->enc);
60322     return val;
60323   }else{
60324     /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
60325     return (double)0;
60326   }
60327 }
60328 
60329 /*
60330 ** The MEM structure is already a MEM_Real.  Try to also make it a
60331 ** MEM_Int if we can.
60332 */
60333 SQLITE_PRIVATE void sqlite3VdbeIntegerAffinity(Mem *pMem){
60334   assert( pMem->flags & MEM_Real );
60335   assert( (pMem->flags & MEM_RowSet)==0 );
60336   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
60337   assert( EIGHT_BYTE_ALIGNMENT(pMem) );
60338 
60339   pMem->u.i = doubleToInt64(pMem->r);
60340 
60341   /* Only mark the value as an integer if
60342   **
60343   **    (1) the round-trip conversion real->int->real is a no-op, and
60344   **    (2) The integer is neither the largest nor the smallest
60345   **        possible integer (ticket #3922)
60346   **
60347   ** The second and third terms in the following conditional enforces
60348   ** the second condition under the assumption that addition overflow causes
60349   ** values to wrap around.
60350   */
60351   if( pMem->r==(double)pMem->u.i
60352    && pMem->u.i>SMALLEST_INT64
60353    && pMem->u.i<LARGEST_INT64
60354   ){
60355     pMem->flags |= MEM_Int;
60356   }
60357 }
60358 
60359 /*
60360 ** Convert pMem to type integer.  Invalidate any prior representations.
60361 */
60362 SQLITE_PRIVATE int sqlite3VdbeMemIntegerify(Mem *pMem){
60363   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
60364   assert( (pMem->flags & MEM_RowSet)==0 );
60365   assert( EIGHT_BYTE_ALIGNMENT(pMem) );
60366 
60367   pMem->u.i = sqlite3VdbeIntValue(pMem);
60368   MemSetTypeFlag(pMem, MEM_Int);
60369   return SQLITE_OK;
60370 }
60371 
60372 /*
60373 ** Convert pMem so that it is of type MEM_Real.
60374 ** Invalidate any prior representations.
60375 */
60376 SQLITE_PRIVATE int sqlite3VdbeMemRealify(Mem *pMem){
60377   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
60378   assert( EIGHT_BYTE_ALIGNMENT(pMem) );
60379 
60380   pMem->r = sqlite3VdbeRealValue(pMem);
60381   MemSetTypeFlag(pMem, MEM_Real);
60382   return SQLITE_OK;
60383 }
60384 
60385 /*
60386 ** Convert pMem so that it has types MEM_Real or MEM_Int or both.
60387 ** Invalidate any prior representations.
60388 **
60389 ** Every effort is made to force the conversion, even if the input
60390 ** is a string that does not look completely like a number.  Convert
60391 ** as much of the string as we can and ignore the rest.
60392 */
60393 SQLITE_PRIVATE int sqlite3VdbeMemNumerify(Mem *pMem){
60394   if( (pMem->flags & (MEM_Int|MEM_Real|MEM_Null))==0 ){
60395     assert( (pMem->flags & (MEM_Blob|MEM_Str))!=0 );
60396     assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
60397     if( 0==sqlite3Atoi64(pMem->z, &pMem->u.i, pMem->n, pMem->enc) ){
60398       MemSetTypeFlag(pMem, MEM_Int);
60399     }else{
60400       pMem->r = sqlite3VdbeRealValue(pMem);
60401       MemSetTypeFlag(pMem, MEM_Real);
60402       sqlite3VdbeIntegerAffinity(pMem);
60403     }
60404   }
60405   assert( (pMem->flags & (MEM_Int|MEM_Real|MEM_Null))!=0 );
60406   pMem->flags &= ~(MEM_Str|MEM_Blob);
60407   return SQLITE_OK;
60408 }
60409 
60410 /*
60411 ** Delete any previous value and set the value stored in *pMem to NULL.
60412 */
60413 SQLITE_PRIVATE void sqlite3VdbeMemSetNull(Mem *pMem){
60414   if( pMem->flags & MEM_Frame ){
60415     VdbeFrame *pFrame = pMem->u.pFrame;
60416     pFrame->pParent = pFrame->v->pDelFrame;
60417     pFrame->v->pDelFrame = pFrame;
60418   }
60419   if( pMem->flags & MEM_RowSet ){
60420     sqlite3RowSetClear(pMem->u.pRowSet);
60421   }
60422   MemSetTypeFlag(pMem, MEM_Null);
60423   pMem->type = SQLITE_NULL;
60424 }
60425 SQLITE_PRIVATE void sqlite3ValueSetNull(sqlite3_value *p){
60426   sqlite3VdbeMemSetNull((Mem*)p);
60427 }
60428 
60429 /*
60430 ** Delete any previous value and set the value to be a BLOB of length
60431 ** n containing all zeros.
60432 */
60433 SQLITE_PRIVATE void sqlite3VdbeMemSetZeroBlob(Mem *pMem, int n){
60434   sqlite3VdbeMemRelease(pMem);
60435   pMem->flags = MEM_Blob|MEM_Zero;
60436   pMem->type = SQLITE_BLOB;
60437   pMem->n = 0;
60438   if( n<0 ) n = 0;
60439   pMem->u.nZero = n;
60440   pMem->enc = SQLITE_UTF8;
60441 
60442 #ifdef SQLITE_OMIT_INCRBLOB
60443   sqlite3VdbeMemGrow(pMem, n, 0);
60444   if( pMem->z ){
60445     pMem->n = n;
60446     memset(pMem->z, 0, n);
60447   }
60448 #endif
60449 }
60450 
60451 /*
60452 ** Delete any previous value and set the value stored in *pMem to val,
60453 ** manifest type INTEGER.
60454 */
60455 SQLITE_PRIVATE void sqlite3VdbeMemSetInt64(Mem *pMem, i64 val){
60456   sqlite3VdbeMemRelease(pMem);
60457   pMem->u.i = val;
60458   pMem->flags = MEM_Int;
60459   pMem->type = SQLITE_INTEGER;
60460 }
60461 
60462 #ifndef SQLITE_OMIT_FLOATING_POINT
60463 /*
60464 ** Delete any previous value and set the value stored in *pMem to val,
60465 ** manifest type REAL.
60466 */
60467 SQLITE_PRIVATE void sqlite3VdbeMemSetDouble(Mem *pMem, double val){
60468   if( sqlite3IsNaN(val) ){
60469     sqlite3VdbeMemSetNull(pMem);
60470   }else{
60471     sqlite3VdbeMemRelease(pMem);
60472     pMem->r = val;
60473     pMem->flags = MEM_Real;
60474     pMem->type = SQLITE_FLOAT;
60475   }
60476 }
60477 #endif
60478 
60479 /*
60480 ** Delete any previous value and set the value of pMem to be an
60481 ** empty boolean index.
60482 */
60483 SQLITE_PRIVATE void sqlite3VdbeMemSetRowSet(Mem *pMem){
60484   sqlite3 *db = pMem->db;
60485   assert( db!=0 );
60486   assert( (pMem->flags & MEM_RowSet)==0 );
60487   sqlite3VdbeMemRelease(pMem);
60488   pMem->zMalloc = sqlite3DbMallocRaw(db, 64);
60489   if( db->mallocFailed ){
60490     pMem->flags = MEM_Null;
60491   }else{
60492     assert( pMem->zMalloc );
60493     pMem->u.pRowSet = sqlite3RowSetInit(db, pMem->zMalloc,
60494                                        sqlite3DbMallocSize(db, pMem->zMalloc));
60495     assert( pMem->u.pRowSet!=0 );
60496     pMem->flags = MEM_RowSet;
60497   }
60498 }
60499 
60500 /*
60501 ** Return true if the Mem object contains a TEXT or BLOB that is
60502 ** too large - whose size exceeds SQLITE_MAX_LENGTH.
60503 */
60504 SQLITE_PRIVATE int sqlite3VdbeMemTooBig(Mem *p){
60505   assert( p->db!=0 );
60506   if( p->flags & (MEM_Str|MEM_Blob) ){
60507     int n = p->n;
60508     if( p->flags & MEM_Zero ){
60509       n += p->u.nZero;
60510     }
60511     return n>p->db->aLimit[SQLITE_LIMIT_LENGTH];
60512   }
60513   return 0;
60514 }
60515 
60516 #ifdef SQLITE_DEBUG
60517 /*
60518 ** This routine prepares a memory cell for modication by breaking
60519 ** its link to a shallow copy and by marking any current shallow
60520 ** copies of this cell as invalid.
60521 **
60522 ** This is used for testing and debugging only - to make sure shallow
60523 ** copies are not misused.
60524 */
60525 SQLITE_PRIVATE void sqlite3VdbeMemAboutToChange(Vdbe *pVdbe, Mem *pMem){
60526   int i;
60527   Mem *pX;
60528   for(i=1, pX=&pVdbe->aMem[1]; i<=pVdbe->nMem; i++, pX++){
60529     if( pX->pScopyFrom==pMem ){
60530       pX->flags |= MEM_Invalid;
60531       pX->pScopyFrom = 0;
60532     }
60533   }
60534   pMem->pScopyFrom = 0;
60535 }
60536 #endif /* SQLITE_DEBUG */
60537 
60538 /*
60539 ** Size of struct Mem not including the Mem.zMalloc member.
60540 */
60541 #define MEMCELLSIZE offsetof(Mem,zMalloc)
60542 
60543 /*
60544 ** Make an shallow copy of pFrom into pTo.  Prior contents of
60545 ** pTo are freed.  The pFrom->z field is not duplicated.  If
60546 ** pFrom->z is used, then pTo->z points to the same thing as pFrom->z
60547 ** and flags gets srcType (either MEM_Ephem or MEM_Static).
60548 */
60549 SQLITE_PRIVATE void sqlite3VdbeMemShallowCopy(Mem *pTo, const Mem *pFrom, int srcType){
60550   assert( (pFrom->flags & MEM_RowSet)==0 );
60551   VdbeMemRelease(pTo);
60552   memcpy(pTo, pFrom, MEMCELLSIZE);
60553   pTo->xDel = 0;
60554   if( (pFrom->flags&MEM_Static)==0 ){
60555     pTo->flags &= ~(MEM_Dyn|MEM_Static|MEM_Ephem);
60556     assert( srcType==MEM_Ephem || srcType==MEM_Static );
60557     pTo->flags |= srcType;
60558   }
60559 }
60560 
60561 /*
60562 ** Make a full copy of pFrom into pTo.  Prior contents of pTo are
60563 ** freed before the copy is made.
60564 */
60565 SQLITE_PRIVATE int sqlite3VdbeMemCopy(Mem *pTo, const Mem *pFrom){
60566   int rc = SQLITE_OK;
60567 
60568   assert( (pFrom->flags & MEM_RowSet)==0 );
60569   VdbeMemRelease(pTo);
60570   memcpy(pTo, pFrom, MEMCELLSIZE);
60571   pTo->flags &= ~MEM_Dyn;
60572 
60573   if( pTo->flags&(MEM_Str|MEM_Blob) ){
60574     if( 0==(pFrom->flags&MEM_Static) ){
60575       pTo->flags |= MEM_Ephem;
60576       rc = sqlite3VdbeMemMakeWriteable(pTo);
60577     }
60578   }
60579 
60580   return rc;
60581 }
60582 
60583 /*
60584 ** Transfer the contents of pFrom to pTo. Any existing value in pTo is
60585 ** freed. If pFrom contains ephemeral data, a copy is made.
60586 **
60587 ** pFrom contains an SQL NULL when this routine returns.
60588 */
60589 SQLITE_PRIVATE void sqlite3VdbeMemMove(Mem *pTo, Mem *pFrom){
60590   assert( pFrom->db==0 || sqlite3_mutex_held(pFrom->db->mutex) );
60591   assert( pTo->db==0 || sqlite3_mutex_held(pTo->db->mutex) );
60592   assert( pFrom->db==0 || pTo->db==0 || pFrom->db==pTo->db );
60593 
60594   sqlite3VdbeMemRelease(pTo);
60595   memcpy(pTo, pFrom, sizeof(Mem));
60596   pFrom->flags = MEM_Null;
60597   pFrom->xDel = 0;
60598   pFrom->zMalloc = 0;
60599 }
60600 
60601 /*
60602 ** Change the value of a Mem to be a string or a BLOB.
60603 **
60604 ** The memory management strategy depends on the value of the xDel
60605 ** parameter. If the value passed is SQLITE_TRANSIENT, then the
60606 ** string is copied into a (possibly existing) buffer managed by the
60607 ** Mem structure. Otherwise, any existing buffer is freed and the
60608 ** pointer copied.
60609 **
60610 ** If the string is too large (if it exceeds the SQLITE_LIMIT_LENGTH
60611 ** size limit) then no memory allocation occurs.  If the string can be
60612 ** stored without allocating memory, then it is.  If a memory allocation
60613 ** is required to store the string, then value of pMem is unchanged.  In
60614 ** either case, SQLITE_TOOBIG is returned.
60615 */
60616 SQLITE_PRIVATE int sqlite3VdbeMemSetStr(
60617   Mem *pMem,          /* Memory cell to set to string value */
60618   const char *z,      /* String pointer */
60619   int n,              /* Bytes in string, or negative */
60620   u8 enc,             /* Encoding of z.  0 for BLOBs */
60621   void (*xDel)(void*) /* Destructor function */
60622 ){
60623   int nByte = n;      /* New value for pMem->n */
60624   int iLimit;         /* Maximum allowed string or blob size */
60625   u16 flags = 0;      /* New value for pMem->flags */
60626 
60627   assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );
60628   assert( (pMem->flags & MEM_RowSet)==0 );
60629 
60630   /* If z is a NULL pointer, set pMem to contain an SQL NULL. */
60631   if( !z ){
60632     sqlite3VdbeMemSetNull(pMem);
60633     return SQLITE_OK;
60634   }
60635 
60636   if( pMem->db ){
60637     iLimit = pMem->db->aLimit[SQLITE_LIMIT_LENGTH];
60638   }else{
60639     iLimit = SQLITE_MAX_LENGTH;
60640   }
60641   flags = (enc==0?MEM_Blob:MEM_Str);
60642   if( nByte<0 ){
60643     assert( enc!=0 );
60644     if( enc==SQLITE_UTF8 ){
60645       for(nByte=0; nByte<=iLimit && z[nByte]; nByte++){}
60646     }else{
60647       for(nByte=0; nByte<=iLimit && (z[nByte] | z[nByte+1]); nByte+=2){}
60648     }
60649     flags |= MEM_Term;
60650   }
60651 
60652   /* The following block sets the new values of Mem.z and Mem.xDel. It
60653   ** also sets a flag in local variable "flags" to indicate the memory
60654   ** management (one of MEM_Dyn or MEM_Static).
60655   */
60656   if( xDel==SQLITE_TRANSIENT ){
60657     int nAlloc = nByte;
60658     if( flags&MEM_Term ){
60659       nAlloc += (enc==SQLITE_UTF8?1:2);
60660     }
60661     if( nByte>iLimit ){
60662       return SQLITE_TOOBIG;
60663     }
60664     if( sqlite3VdbeMemGrow(pMem, nAlloc, 0) ){
60665       return SQLITE_NOMEM;
60666     }
60667     memcpy(pMem->z, z, nAlloc);
60668   }else if( xDel==SQLITE_DYNAMIC ){
60669     sqlite3VdbeMemRelease(pMem);
60670     pMem->zMalloc = pMem->z = (char *)z;
60671     pMem->xDel = 0;
60672   }else{
60673     sqlite3VdbeMemRelease(pMem);
60674     pMem->z = (char *)z;
60675     pMem->xDel = xDel;
60676     flags |= ((xDel==SQLITE_STATIC)?MEM_Static:MEM_Dyn);
60677   }
60678 
60679   pMem->n = nByte;
60680   pMem->flags = flags;
60681   pMem->enc = (enc==0 ? SQLITE_UTF8 : enc);
60682   pMem->type = (enc==0 ? SQLITE_BLOB : SQLITE_TEXT);
60683 
60684 #ifndef SQLITE_OMIT_UTF16
60685   if( pMem->enc!=SQLITE_UTF8 && sqlite3VdbeMemHandleBom(pMem) ){
60686     return SQLITE_NOMEM;
60687   }
60688 #endif
60689 
60690   if( nByte>iLimit ){
60691     return SQLITE_TOOBIG;
60692   }
60693 
60694   return SQLITE_OK;
60695 }
60696 
60697 /*
60698 ** Compare the values contained by the two memory cells, returning
60699 ** negative, zero or positive if pMem1 is less than, equal to, or greater
60700 ** than pMem2. Sorting order is NULL's first, followed by numbers (integers
60701 ** and reals) sorted numerically, followed by text ordered by the collating
60702 ** sequence pColl and finally blob's ordered by memcmp().
60703 **
60704 ** Two NULL values are considered equal by this function.
60705 */
60706 SQLITE_PRIVATE int sqlite3MemCompare(const Mem *pMem1, const Mem *pMem2, const CollSeq *pColl){
60707   int rc;
60708   int f1, f2;
60709   int combined_flags;
60710 
60711   f1 = pMem1->flags;
60712   f2 = pMem2->flags;
60713   combined_flags = f1|f2;
60714   assert( (combined_flags & MEM_RowSet)==0 );
60715 
60716   /* If one value is NULL, it is less than the other. If both values
60717   ** are NULL, return 0.
60718   */
60719   if( combined_flags&MEM_Null ){
60720     return (f2&MEM_Null) - (f1&MEM_Null);
60721   }
60722 
60723   /* If one value is a number and the other is not, the number is less.
60724   ** If both are numbers, compare as reals if one is a real, or as integers
60725   ** if both values are integers.
60726   */
60727   if( combined_flags&(MEM_Int|MEM_Real) ){
60728     double r1, r2;
60729     if( (f1 & f2 & MEM_Int)!=0 ){
60730       if( pMem1->u.i < pMem2->u.i ) return -1;
60731       if( pMem1->u.i > pMem2->u.i ) return 1;
60732       return 0;
60733     }
60734     if( (f1&MEM_Real)!=0 ){
60735       r1 = pMem1->r;
60736     }else if( (f1&MEM_Int)!=0 ){
60737       r1 = (double)pMem1->u.i;
60738     }else{
60739       return 1;
60740     }
60741     if( (f2&MEM_Real)!=0 ){
60742       r2 = pMem2->r;
60743     }else if( (f2&MEM_Int)!=0 ){
60744       r2 = (double)pMem2->u.i;
60745     }else{
60746       return -1;
60747     }
60748     if( r1<r2 ) return -1;
60749     if( r1>r2 ) return 1;
60750     return 0;
60751   }
60752 
60753   /* If one value is a string and the other is a blob, the string is less.
60754   ** If both are strings, compare using the collating functions.
60755   */
60756   if( combined_flags&MEM_Str ){
60757     if( (f1 & MEM_Str)==0 ){
60758       return 1;
60759     }
60760     if( (f2 & MEM_Str)==0 ){
60761       return -1;
60762     }
60763 
60764     assert( pMem1->enc==pMem2->enc );
60765     assert( pMem1->enc==SQLITE_UTF8 ||
60766             pMem1->enc==SQLITE_UTF16LE || pMem1->enc==SQLITE_UTF16BE );
60767 
60768     /* The collation sequence must be defined at this point, even if
60769     ** the user deletes the collation sequence after the vdbe program is
60770     ** compiled (this was not always the case).
60771     */
60772     assert( !pColl || pColl->xCmp );
60773 
60774     if( pColl ){
60775       if( pMem1->enc==pColl->enc ){
60776         /* The strings are already in the correct encoding.  Call the
60777         ** comparison function directly */
60778         return pColl->xCmp(pColl->pUser,pMem1->n,pMem1->z,pMem2->n,pMem2->z);
60779       }else{
60780         const void *v1, *v2;
60781         int n1, n2;
60782         Mem c1;
60783         Mem c2;
60784         memset(&c1, 0, sizeof(c1));
60785         memset(&c2, 0, sizeof(c2));
60786         sqlite3VdbeMemShallowCopy(&c1, pMem1, MEM_Ephem);
60787         sqlite3VdbeMemShallowCopy(&c2, pMem2, MEM_Ephem);
60788         v1 = sqlite3ValueText((sqlite3_value*)&c1, pColl->enc);
60789         n1 = v1==0 ? 0 : c1.n;
60790         v2 = sqlite3ValueText((sqlite3_value*)&c2, pColl->enc);
60791         n2 = v2==0 ? 0 : c2.n;
60792         rc = pColl->xCmp(pColl->pUser, n1, v1, n2, v2);
60793         sqlite3VdbeMemRelease(&c1);
60794         sqlite3VdbeMemRelease(&c2);
60795         return rc;
60796       }
60797     }
60798     /* If a NULL pointer was passed as the collate function, fall through
60799     ** to the blob case and use memcmp().  */
60800   }
60801 
60802   /* Both values must be blobs.  Compare using memcmp().  */
60803   rc = memcmp(pMem1->z, pMem2->z, (pMem1->n>pMem2->n)?pMem2->n:pMem1->n);
60804   if( rc==0 ){
60805     rc = pMem1->n - pMem2->n;
60806   }
60807   return rc;
60808 }
60809 
60810 /*
60811 ** Move data out of a btree key or data field and into a Mem structure.
60812 ** The data or key is taken from the entry that pCur is currently pointing
60813 ** to.  offset and amt determine what portion of the data or key to retrieve.
60814 ** key is true to get the key or false to get data.  The result is written
60815 ** into the pMem element.
60816 **
60817 ** The pMem structure is assumed to be uninitialized.  Any prior content
60818 ** is overwritten without being freed.
60819 **
60820 ** If this routine fails for any reason (malloc returns NULL or unable
60821 ** to read from the disk) then the pMem is left in an inconsistent state.
60822 */
60823 SQLITE_PRIVATE int sqlite3VdbeMemFromBtree(
60824   BtCursor *pCur,   /* Cursor pointing at record to retrieve. */
60825   u32 offset,       /* Offset from the start of data to return bytes from. */
60826   u32 amt,          /* Number of bytes to return. */
60827   int key,          /* If true, retrieve from the btree key, not data. */
60828   Mem *pMem         /* OUT: Return data in this Mem structure. */
60829 ){
60830   char *zData;        /* Data from the btree layer */
60831   u32 available = 0;  /* Number of bytes available on the local btree page */
60832   int rc = SQLITE_OK; /* Return code */
60833 
60834   assert( sqlite3BtreeCursorIsValid(pCur) );
60835 
60836   /* Note: the calls to BtreeKeyFetch() and DataFetch() below assert()
60837   ** that both the BtShared and database handle mutexes are held. */
60838   assert( (pMem->flags & MEM_RowSet)==0 );
60839   if( key ){
60840     zData = (char *)sqlite3BtreeKeyFetch(pCur, &available);
60841   }else{
60842     zData = (char *)sqlite3BtreeDataFetch(pCur, &available);
60843   }
60844   assert( zData!=0 );
60845 
60846   if( offset+amt<=available ){
60847     sqlite3VdbeMemRelease(pMem);
60848     pMem->z = &zData[offset];
60849     pMem->flags = MEM_Blob|MEM_Ephem;
60850   }else if( SQLITE_OK==(rc = sqlite3VdbeMemGrow(pMem, amt+2, 0)) ){
60851     pMem->flags = MEM_Blob|MEM_Dyn|MEM_Term;
60852     pMem->enc = 0;
60853     pMem->type = SQLITE_BLOB;
60854     if( key ){
60855       rc = sqlite3BtreeKey(pCur, offset, amt, pMem->z);
60856     }else{
60857       rc = sqlite3BtreeData(pCur, offset, amt, pMem->z);
60858     }
60859     pMem->z[amt] = 0;
60860     pMem->z[amt+1] = 0;
60861     if( rc!=SQLITE_OK ){
60862       sqlite3VdbeMemRelease(pMem);
60863     }
60864   }
60865   pMem->n = (int)amt;
60866 
60867   return rc;
60868 }
60869 
60870 /* This function is only available internally, it is not part of the
60871 ** external API. It works in a similar way to sqlite3_value_text(),
60872 ** except the data returned is in the encoding specified by the second
60873 ** parameter, which must be one of SQLITE_UTF16BE, SQLITE_UTF16LE or
60874 ** SQLITE_UTF8.
60875 **
60876 ** (2006-02-16:)  The enc value can be or-ed with SQLITE_UTF16_ALIGNED.
60877 ** If that is the case, then the result must be aligned on an even byte
60878 ** boundary.
60879 */
60880 SQLITE_PRIVATE const void *sqlite3ValueText(sqlite3_value* pVal, u8 enc){
60881   if( !pVal ) return 0;
60882 
60883   assert( pVal->db==0 || sqlite3_mutex_held(pVal->db->mutex) );
60884   assert( (enc&3)==(enc&~SQLITE_UTF16_ALIGNED) );
60885   assert( (pVal->flags & MEM_RowSet)==0 );
60886 
60887   if( pVal->flags&MEM_Null ){
60888     return 0;
60889   }
60890   assert( (MEM_Blob>>3) == MEM_Str );
60891   pVal->flags |= (pVal->flags & MEM_Blob)>>3;
60892   ExpandBlob(pVal);
60893   if( pVal->flags&MEM_Str ){
60894     sqlite3VdbeChangeEncoding(pVal, enc & ~SQLITE_UTF16_ALIGNED);
60895     if( (enc & SQLITE_UTF16_ALIGNED)!=0 && 1==(1&SQLITE_PTR_TO_INT(pVal->z)) ){
60896       assert( (pVal->flags & (MEM_Ephem|MEM_Static))!=0 );
60897       if( sqlite3VdbeMemMakeWriteable(pVal)!=SQLITE_OK ){
60898         return 0;
60899       }
60900     }
60901     sqlite3VdbeMemNulTerminate(pVal); /* IMP: R-31275-44060 */
60902   }else{
60903     assert( (pVal->flags&MEM_Blob)==0 );
60904     sqlite3VdbeMemStringify(pVal, enc);
60905     assert( 0==(1&SQLITE_PTR_TO_INT(pVal->z)) );
60906   }
60907   assert(pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) || pVal->db==0
60908               || pVal->db->mallocFailed );
60909   if( pVal->enc==(enc & ~SQLITE_UTF16_ALIGNED) ){
60910     return pVal->z;
60911   }else{
60912     return 0;
60913   }
60914 }
60915 
60916 /*
60917 ** Create a new sqlite3_value object.
60918 */
60919 SQLITE_PRIVATE sqlite3_value *sqlite3ValueNew(sqlite3 *db){
60920   Mem *p = sqlite3DbMallocZero(db, sizeof(*p));
60921   if( p ){
60922     p->flags = MEM_Null;
60923     p->type = SQLITE_NULL;
60924     p->db = db;
60925   }
60926   return p;
60927 }
60928 
60929 /*
60930 ** Context object passed by sqlite3Stat4ProbeSetValue() through to
60931 ** valueNew(). See comments above valueNew() for details.
60932 */
60933 struct ValueNewStat4Ctx {
60934   Parse *pParse;
60935   Index *pIdx;
60936   UnpackedRecord **ppRec;
60937   int iVal;
60938 };
60939 
60940 /*
60941 ** Allocate and return a pointer to a new sqlite3_value object. If
60942 ** the second argument to this function is NULL, the object is allocated
60943 ** by calling sqlite3ValueNew().
60944 **
60945 ** Otherwise, if the second argument is non-zero, then this function is
60946 ** being called indirectly by sqlite3Stat4ProbeSetValue(). If it has not
60947 ** already been allocated, allocate the UnpackedRecord structure that
60948 ** that function will return to its caller here. Then return a pointer
60949 ** an sqlite3_value within the UnpackedRecord.a[] array.
60950 */
60951 static sqlite3_value *valueNew(sqlite3 *db, struct ValueNewStat4Ctx *p){
60952 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
60953   if( p ){
60954     UnpackedRecord *pRec = p->ppRec[0];
60955 
60956     if( pRec==0 ){
60957       Index *pIdx = p->pIdx;      /* Index being probed */
60958       int nByte;                  /* Bytes of space to allocate */
60959       int i;                      /* Counter variable */
60960       int nCol = pIdx->nColumn;   /* Number of index columns including rowid */
60961 
60962       nByte = sizeof(Mem) * nCol + ROUND8(sizeof(UnpackedRecord));
60963       pRec = (UnpackedRecord*)sqlite3DbMallocZero(db, nByte);
60964       if( pRec ){
60965         pRec->pKeyInfo = sqlite3KeyInfoOfIndex(p->pParse, pIdx);
60966         if( pRec->pKeyInfo ){
60967           assert( pRec->pKeyInfo->nField+pRec->pKeyInfo->nXField==nCol );
60968           assert( pRec->pKeyInfo->enc==ENC(db) );
60969           pRec->flags = UNPACKED_PREFIX_MATCH;
60970           pRec->aMem = (Mem *)((u8*)pRec + ROUND8(sizeof(UnpackedRecord)));
60971           for(i=0; i<nCol; i++){
60972             pRec->aMem[i].flags = MEM_Null;
60973             pRec->aMem[i].type = SQLITE_NULL;
60974             pRec->aMem[i].db = db;
60975           }
60976         }else{
60977           sqlite3DbFree(db, pRec);
60978           pRec = 0;
60979         }
60980       }
60981       if( pRec==0 ) return 0;
60982       p->ppRec[0] = pRec;
60983     }
60984 
60985     pRec->nField = p->iVal+1;
60986     return &pRec->aMem[p->iVal];
60987   }
60988 #else
60989   UNUSED_PARAMETER(p);
60990 #endif /* defined(SQLITE_ENABLE_STAT3_OR_STAT4) */
60991   return sqlite3ValueNew(db);
60992 }
60993 
60994 /*
60995 ** Extract a value from the supplied expression in the manner described
60996 ** above sqlite3ValueFromExpr(). Allocate the sqlite3_value object
60997 ** using valueNew().
60998 **
60999 ** If pCtx is NULL and an error occurs after the sqlite3_value object
61000 ** has been allocated, it is freed before returning. Or, if pCtx is not
61001 ** NULL, it is assumed that the caller will free any allocated object
61002 ** in all cases.
61003 */
61004 static int valueFromExpr(
61005   sqlite3 *db,                    /* The database connection */
61006   Expr *pExpr,                    /* The expression to evaluate */
61007   u8 enc,                         /* Encoding to use */
61008   u8 affinity,                    /* Affinity to use */
61009   sqlite3_value **ppVal,          /* Write the new value here */
61010   struct ValueNewStat4Ctx *pCtx   /* Second argument for valueNew() */
61011 ){
61012   int op;
61013   char *zVal = 0;
61014   sqlite3_value *pVal = 0;
61015   int negInt = 1;
61016   const char *zNeg = "";
61017   int rc = SQLITE_OK;
61018 
61019   if( !pExpr ){
61020     *ppVal = 0;
61021     return SQLITE_OK;
61022   }
61023   op = pExpr->op;
61024   if( NEVER(op==TK_REGISTER) ) op = pExpr->op2;
61025 
61026   /* Handle negative integers in a single step.  This is needed in the
61027   ** case when the value is -9223372036854775808.
61028   */
61029   if( op==TK_UMINUS
61030    && (pExpr->pLeft->op==TK_INTEGER || pExpr->pLeft->op==TK_FLOAT) ){
61031     pExpr = pExpr->pLeft;
61032     op = pExpr->op;
61033     negInt = -1;
61034     zNeg = "-";
61035   }
61036 
61037   if( op==TK_STRING || op==TK_FLOAT || op==TK_INTEGER ){
61038     pVal = valueNew(db, pCtx);
61039     if( pVal==0 ) goto no_mem;
61040     if( ExprHasProperty(pExpr, EP_IntValue) ){
61041       sqlite3VdbeMemSetInt64(pVal, (i64)pExpr->u.iValue*negInt);
61042     }else{
61043       zVal = sqlite3MPrintf(db, "%s%s", zNeg, pExpr->u.zToken);
61044       if( zVal==0 ) goto no_mem;
61045       sqlite3ValueSetStr(pVal, -1, zVal, SQLITE_UTF8, SQLITE_DYNAMIC);
61046       if( op==TK_FLOAT ) pVal->type = SQLITE_FLOAT;
61047     }
61048     if( (op==TK_INTEGER || op==TK_FLOAT ) && affinity==SQLITE_AFF_NONE ){
61049       sqlite3ValueApplyAffinity(pVal, SQLITE_AFF_NUMERIC, SQLITE_UTF8);
61050     }else{
61051       sqlite3ValueApplyAffinity(pVal, affinity, SQLITE_UTF8);
61052     }
61053     if( pVal->flags & (MEM_Int|MEM_Real) ) pVal->flags &= ~MEM_Str;
61054     if( enc!=SQLITE_UTF8 ){
61055       rc = sqlite3VdbeChangeEncoding(pVal, enc);
61056     }
61057   }else if( op==TK_UMINUS ) {
61058     /* This branch happens for multiple negative signs.  Ex: -(-5) */
61059     if( SQLITE_OK==sqlite3ValueFromExpr(db,pExpr->pLeft,enc,affinity,&pVal)
61060      && pVal!=0
61061     ){
61062       sqlite3VdbeMemNumerify(pVal);
61063       if( pVal->u.i==SMALLEST_INT64 ){
61064         pVal->flags &= MEM_Int;
61065         pVal->flags |= MEM_Real;
61066         pVal->r = (double)LARGEST_INT64;
61067       }else{
61068         pVal->u.i = -pVal->u.i;
61069       }
61070       pVal->r = -pVal->r;
61071       sqlite3ValueApplyAffinity(pVal, affinity, enc);
61072     }
61073   }else if( op==TK_NULL ){
61074     pVal = valueNew(db, pCtx);
61075     if( pVal==0 ) goto no_mem;
61076   }
61077 #ifndef SQLITE_OMIT_BLOB_LITERAL
61078   else if( op==TK_BLOB ){
61079     int nVal;
61080     assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' );
61081     assert( pExpr->u.zToken[1]=='\'' );
61082     pVal = valueNew(db, pCtx);
61083     if( !pVal ) goto no_mem;
61084     zVal = &pExpr->u.zToken[2];
61085     nVal = sqlite3Strlen30(zVal)-1;
61086     assert( zVal[nVal]=='\'' );
61087     sqlite3VdbeMemSetStr(pVal, sqlite3HexToBlob(db, zVal, nVal), nVal/2,
61088                          0, SQLITE_DYNAMIC);
61089   }
61090 #endif
61091 
61092   if( pVal ){
61093     sqlite3VdbeMemStoreType(pVal);
61094   }
61095   *ppVal = pVal;
61096   return rc;
61097 
61098 no_mem:
61099   db->mallocFailed = 1;
61100   sqlite3DbFree(db, zVal);
61101   assert( *ppVal==0 );
61102 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
61103   if( pCtx==0 ) sqlite3ValueFree(pVal);
61104 #else
61105   assert( pCtx==0 ); sqlite3ValueFree(pVal);
61106 #endif
61107   return SQLITE_NOMEM;
61108 }
61109 
61110 /*
61111 ** Create a new sqlite3_value object, containing the value of pExpr.
61112 **
61113 ** This only works for very simple expressions that consist of one constant
61114 ** token (i.e. "5", "5.1", "'a string'"). If the expression can
61115 ** be converted directly into a value, then the value is allocated and
61116 ** a pointer written to *ppVal. The caller is responsible for deallocating
61117 ** the value by passing it to sqlite3ValueFree() later on. If the expression
61118 ** cannot be converted to a value, then *ppVal is set to NULL.
61119 */
61120 SQLITE_PRIVATE int sqlite3ValueFromExpr(
61121   sqlite3 *db,              /* The database connection */
61122   Expr *pExpr,              /* The expression to evaluate */
61123   u8 enc,                   /* Encoding to use */
61124   u8 affinity,              /* Affinity to use */
61125   sqlite3_value **ppVal     /* Write the new value here */
61126 ){
61127   return valueFromExpr(db, pExpr, enc, affinity, ppVal, 0);
61128 }
61129 
61130 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
61131 /*
61132 ** The implementation of the sqlite_record() function. This function accepts
61133 ** a single argument of any type. The return value is a formatted database
61134 ** record (a blob) containing the argument value.
61135 **
61136 ** This is used to convert the value stored in the 'sample' column of the
61137 ** sqlite_stat3 table to the record format SQLite uses internally.
61138 */
61139 static void recordFunc(
61140   sqlite3_context *context,
61141   int argc,
61142   sqlite3_value **argv
61143 ){
61144   const int file_format = 1;
61145   int iSerial;                    /* Serial type */
61146   int nSerial;                    /* Bytes of space for iSerial as varint */
61147   int nVal;                       /* Bytes of space required for argv[0] */
61148   int nRet;
61149   sqlite3 *db;
61150   u8 *aRet;
61151 
61152   UNUSED_PARAMETER( argc );
61153   iSerial = sqlite3VdbeSerialType(argv[0], file_format);
61154   nSerial = sqlite3VarintLen(iSerial);
61155   nVal = sqlite3VdbeSerialTypeLen(iSerial);
61156   db = sqlite3_context_db_handle(context);
61157 
61158   nRet = 1 + nSerial + nVal;
61159   aRet = sqlite3DbMallocRaw(db, nRet);
61160   if( aRet==0 ){
61161     sqlite3_result_error_nomem(context);
61162   }else{
61163     aRet[0] = nSerial+1;
61164     sqlite3PutVarint(&aRet[1], iSerial);
61165     sqlite3VdbeSerialPut(&aRet[1+nSerial], argv[0], iSerial);
61166     sqlite3_result_blob(context, aRet, nRet, SQLITE_TRANSIENT);
61167     sqlite3DbFree(db, aRet);
61168   }
61169 }
61170 
61171 /*
61172 ** Register built-in functions used to help read ANALYZE data.
61173 */
61174 SQLITE_PRIVATE void sqlite3AnalyzeFunctions(void){
61175   static SQLITE_WSD FuncDef aAnalyzeTableFuncs[] = {
61176     FUNCTION(sqlite_record,   1, 0, 0, recordFunc),
61177   };
61178   int i;
61179   FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions);
61180   FuncDef *aFunc = (FuncDef*)&GLOBAL(FuncDef, aAnalyzeTableFuncs);
61181   for(i=0; i<ArraySize(aAnalyzeTableFuncs); i++){
61182     sqlite3FuncDefInsert(pHash, &aFunc[i]);
61183   }
61184 }
61185 
61186 /*
61187 ** This function is used to allocate and populate UnpackedRecord
61188 ** structures intended to be compared against sample index keys stored
61189 ** in the sqlite_stat4 table.
61190 **
61191 ** A single call to this function attempts to populates field iVal (leftmost
61192 ** is 0 etc.) of the unpacked record with a value extracted from expression
61193 ** pExpr. Extraction of values is possible if:
61194 **
61195 **  * (pExpr==0). In this case the value is assumed to be an SQL NULL,
61196 **
61197 **  * The expression is a bound variable, and this is a reprepare, or
61198 **
61199 **  * The sqlite3ValueFromExpr() function is able to extract a value
61200 **    from the expression (i.e. the expression is a literal value).
61201 **
61202 ** If a value can be extracted, the affinity passed as the 5th argument
61203 ** is applied to it before it is copied into the UnpackedRecord. Output
61204 ** parameter *pbOk is set to true if a value is extracted, or false
61205 ** otherwise.
61206 **
61207 ** When this function is called, *ppRec must either point to an object
61208 ** allocated by an earlier call to this function, or must be NULL. If it
61209 ** is NULL and a value can be successfully extracted, a new UnpackedRecord
61210 ** is allocated (and *ppRec set to point to it) before returning.
61211 **
61212 ** Unless an error is encountered, SQLITE_OK is returned. It is not an
61213 ** error if a value cannot be extracted from pExpr. If an error does
61214 ** occur, an SQLite error code is returned.
61215 */
61216 SQLITE_PRIVATE int sqlite3Stat4ProbeSetValue(
61217   Parse *pParse,                  /* Parse context */
61218   Index *pIdx,                    /* Index being probed */
61219   UnpackedRecord **ppRec,         /* IN/OUT: Probe record */
61220   Expr *pExpr,                    /* The expression to extract a value from */
61221   u8 affinity,                    /* Affinity to use */
61222   int iVal,                       /* Array element to populate */
61223   int *pbOk                       /* OUT: True if value was extracted */
61224 ){
61225   int rc = SQLITE_OK;
61226   sqlite3_value *pVal = 0;
61227   sqlite3 *db = pParse->db;
61228 
61229 
61230   struct ValueNewStat4Ctx alloc;
61231   alloc.pParse = pParse;
61232   alloc.pIdx = pIdx;
61233   alloc.ppRec = ppRec;
61234   alloc.iVal = iVal;
61235 
61236   /* Skip over any TK_COLLATE nodes */
61237   pExpr = sqlite3ExprSkipCollate(pExpr);
61238 
61239   if( !pExpr ){
61240     pVal = valueNew(db, &alloc);
61241     if( pVal ){
61242       sqlite3VdbeMemSetNull((Mem*)pVal);
61243     }
61244   }else if( pExpr->op==TK_VARIABLE
61245         || NEVER(pExpr->op==TK_REGISTER && pExpr->op2==TK_VARIABLE)
61246   ){
61247     Vdbe *v;
61248     int iBindVar = pExpr->iColumn;
61249     sqlite3VdbeSetVarmask(pParse->pVdbe, iBindVar);
61250     if( (v = pParse->pReprepare)!=0 ){
61251       pVal = valueNew(db, &alloc);
61252       if( pVal ){
61253         rc = sqlite3VdbeMemCopy((Mem*)pVal, &v->aVar[iBindVar-1]);
61254         if( rc==SQLITE_OK ){
61255           sqlite3ValueApplyAffinity(pVal, affinity, ENC(db));
61256         }
61257         pVal->db = pParse->db;
61258         sqlite3VdbeMemStoreType((Mem*)pVal);
61259       }
61260     }
61261   }else{
61262     rc = valueFromExpr(db, pExpr, ENC(db), affinity, &pVal, &alloc);
61263   }
61264   *pbOk = (pVal!=0);
61265 
61266   assert( pVal==0 || pVal->db==db );
61267   return rc;
61268 }
61269 
61270 /*
61271 ** Unless it is NULL, the argument must be an UnpackedRecord object returned
61272 ** by an earlier call to sqlite3Stat4ProbeSetValue(). This call deletes
61273 ** the object.
61274 */
61275 SQLITE_PRIVATE void sqlite3Stat4ProbeFree(UnpackedRecord *pRec){
61276   if( pRec ){
61277     int i;
61278     int nCol = pRec->pKeyInfo->nField+pRec->pKeyInfo->nXField;
61279     Mem *aMem = pRec->aMem;
61280     sqlite3 *db = aMem[0].db;
61281     for(i=0; i<nCol; i++){
61282       sqlite3DbFree(db, aMem[i].zMalloc);
61283     }
61284     sqlite3KeyInfoUnref(pRec->pKeyInfo);
61285     sqlite3DbFree(db, pRec);
61286   }
61287 }
61288 #endif /* ifdef SQLITE_ENABLE_STAT4 */
61289 
61290 /*
61291 ** Change the string value of an sqlite3_value object
61292 */
61293 SQLITE_PRIVATE void sqlite3ValueSetStr(
61294   sqlite3_value *v,     /* Value to be set */
61295   int n,                /* Length of string z */
61296   const void *z,        /* Text of the new string */
61297   u8 enc,               /* Encoding to use */
61298   void (*xDel)(void*)   /* Destructor for the string */
61299 ){
61300   if( v ) sqlite3VdbeMemSetStr((Mem *)v, z, n, enc, xDel);
61301 }
61302 
61303 /*
61304 ** Free an sqlite3_value object
61305 */
61306 SQLITE_PRIVATE void sqlite3ValueFree(sqlite3_value *v){
61307   if( !v ) return;
61308   sqlite3VdbeMemRelease((Mem *)v);
61309   sqlite3DbFree(((Mem*)v)->db, v);
61310 }
61311 
61312 /*
61313 ** Return the number of bytes in the sqlite3_value object assuming
61314 ** that it uses the encoding "enc"
61315 */
61316 SQLITE_PRIVATE int sqlite3ValueBytes(sqlite3_value *pVal, u8 enc){
61317   Mem *p = (Mem*)pVal;
61318   if( (p->flags & MEM_Blob)!=0 || sqlite3ValueText(pVal, enc) ){
61319     if( p->flags & MEM_Zero ){
61320       return p->n + p->u.nZero;
61321     }else{
61322       return p->n;
61323     }
61324   }
61325   return 0;
61326 }
61327 
61328 /************** End of vdbemem.c *********************************************/
61329 /************** Begin file vdbeaux.c *****************************************/
61330 /*
61331 ** 2003 September 6
61332 **
61333 ** The author disclaims copyright to this source code.  In place of
61334 ** a legal notice, here is a blessing:
61335 **
61336 **    May you do good and not evil.
61337 **    May you find forgiveness for yourself and forgive others.
61338 **    May you share freely, never taking more than you give.
61339 **
61340 *************************************************************************
61341 ** This file contains code used for creating, destroying, and populating
61342 ** a VDBE (or an "sqlite3_stmt" as it is known to the outside world.)  Prior
61343 ** to version 2.8.7, all this code was combined into the vdbe.c source file.
61344 ** But that file was getting too big so this subroutines were split out.
61345 */
61346 
61347 /*
61348 ** Create a new virtual database engine.
61349 */
61350 SQLITE_PRIVATE Vdbe *sqlite3VdbeCreate(Parse *pParse){
61351   sqlite3 *db = pParse->db;
61352   Vdbe *p;
61353   p = sqlite3DbMallocZero(db, sizeof(Vdbe) );
61354   if( p==0 ) return 0;
61355   p->db = db;
61356   if( db->pVdbe ){
61357     db->pVdbe->pPrev = p;
61358   }
61359   p->pNext = db->pVdbe;
61360   p->pPrev = 0;
61361   db->pVdbe = p;
61362   p->magic = VDBE_MAGIC_INIT;
61363   p->pParse = pParse;
61364   assert( pParse->aLabel==0 );
61365   assert( pParse->nLabel==0 );
61366   assert( pParse->nOpAlloc==0 );
61367   return p;
61368 }
61369 
61370 /*
61371 ** Remember the SQL string for a prepared statement.
61372 */
61373 SQLITE_PRIVATE void sqlite3VdbeSetSql(Vdbe *p, const char *z, int n, int isPrepareV2){
61374   assert( isPrepareV2==1 || isPrepareV2==0 );
61375   if( p==0 ) return;
61376 #if defined(SQLITE_OMIT_TRACE) && !defined(SQLITE_ENABLE_SQLLOG)
61377   if( !isPrepareV2 ) return;
61378 #endif
61379   assert( p->zSql==0 );
61380   p->zSql = sqlite3DbStrNDup(p->db, z, n);
61381   p->isPrepareV2 = (u8)isPrepareV2;
61382 }
61383 
61384 /*
61385 ** Return the SQL associated with a prepared statement
61386 */
61387 SQLITE_API const char *sqlite3_sql(sqlite3_stmt *pStmt){
61388   Vdbe *p = (Vdbe *)pStmt;
61389   return (p && p->isPrepareV2) ? p->zSql : 0;
61390 }
61391 
61392 /*
61393 ** Swap all content between two VDBE structures.
61394 */
61395 SQLITE_PRIVATE void sqlite3VdbeSwap(Vdbe *pA, Vdbe *pB){
61396   Vdbe tmp, *pTmp;
61397   char *zTmp;
61398   tmp = *pA;
61399   *pA = *pB;
61400   *pB = tmp;
61401   pTmp = pA->pNext;
61402   pA->pNext = pB->pNext;
61403   pB->pNext = pTmp;
61404   pTmp = pA->pPrev;
61405   pA->pPrev = pB->pPrev;
61406   pB->pPrev = pTmp;
61407   zTmp = pA->zSql;
61408   pA->zSql = pB->zSql;
61409   pB->zSql = zTmp;
61410   pB->isPrepareV2 = pA->isPrepareV2;
61411 }
61412 
61413 /*
61414 ** Resize the Vdbe.aOp array so that it is at least one op larger than
61415 ** it was.
61416 **
61417 ** If an out-of-memory error occurs while resizing the array, return
61418 ** SQLITE_NOMEM. In this case Vdbe.aOp and Vdbe.nOpAlloc remain
61419 ** unchanged (this is so that any opcodes already allocated can be
61420 ** correctly deallocated along with the rest of the Vdbe).
61421 */
61422 static int growOpArray(Vdbe *v){
61423   VdbeOp *pNew;
61424   Parse *p = v->pParse;
61425   int nNew = (p->nOpAlloc ? p->nOpAlloc*2 : (int)(1024/sizeof(Op)));
61426   pNew = sqlite3DbRealloc(p->db, v->aOp, nNew*sizeof(Op));
61427   if( pNew ){
61428     p->nOpAlloc = sqlite3DbMallocSize(p->db, pNew)/sizeof(Op);
61429     v->aOp = pNew;
61430   }
61431   return (pNew ? SQLITE_OK : SQLITE_NOMEM);
61432 }
61433 
61434 #ifdef SQLITE_DEBUG
61435 /* This routine is just a convenient place to set a breakpoint that will
61436 ** fire after each opcode is inserted and displayed using
61437 ** "PRAGMA vdbe_addoptrace=on".
61438 */
61439 static void test_addop_breakpoint(void){
61440   static int n = 0;
61441   n++;
61442 }
61443 #endif
61444 
61445 /*
61446 ** Add a new instruction to the list of instructions current in the
61447 ** VDBE.  Return the address of the new instruction.
61448 **
61449 ** Parameters:
61450 **
61451 **    p               Pointer to the VDBE
61452 **
61453 **    op              The opcode for this instruction
61454 **
61455 **    p1, p2, p3      Operands
61456 **
61457 ** Use the sqlite3VdbeResolveLabel() function to fix an address and
61458 ** the sqlite3VdbeChangeP4() function to change the value of the P4
61459 ** operand.
61460 */
61461 SQLITE_PRIVATE int sqlite3VdbeAddOp3(Vdbe *p, int op, int p1, int p2, int p3){
61462   int i;
61463   VdbeOp *pOp;
61464 
61465   i = p->nOp;
61466   assert( p->magic==VDBE_MAGIC_INIT );
61467   assert( op>0 && op<0xff );
61468   if( p->pParse->nOpAlloc<=i ){
61469     if( growOpArray(p) ){
61470       return 1;
61471     }
61472   }
61473   p->nOp++;
61474   pOp = &p->aOp[i];
61475   pOp->opcode = (u8)op;
61476   pOp->p5 = 0;
61477   pOp->p1 = p1;
61478   pOp->p2 = p2;
61479   pOp->p3 = p3;
61480   pOp->p4.p = 0;
61481   pOp->p4type = P4_NOTUSED;
61482 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
61483   pOp->zComment = 0;
61484 #endif
61485 #ifdef SQLITE_DEBUG
61486   if( p->db->flags & SQLITE_VdbeAddopTrace ){
61487     int jj, kk;
61488     Parse *pParse = p->pParse;
61489     for(jj=kk=0; jj<SQLITE_N_COLCACHE; jj++){
61490       struct yColCache *x = pParse->aColCache + jj;
61491       if( x->iLevel>pParse->iCacheLevel || x->iReg==0 ) continue;
61492       printf(" r[%d]={%d:%d}", x->iReg, x->iTable, x->iColumn);
61493       kk++;
61494     }
61495     if( kk ) printf("\n");
61496     sqlite3VdbePrintOp(0, i, &p->aOp[i]);
61497     test_addop_breakpoint();
61498   }
61499 #endif
61500 #ifdef VDBE_PROFILE
61501   pOp->cycles = 0;
61502   pOp->cnt = 0;
61503 #endif
61504   return i;
61505 }
61506 SQLITE_PRIVATE int sqlite3VdbeAddOp0(Vdbe *p, int op){
61507   return sqlite3VdbeAddOp3(p, op, 0, 0, 0);
61508 }
61509 SQLITE_PRIVATE int sqlite3VdbeAddOp1(Vdbe *p, int op, int p1){
61510   return sqlite3VdbeAddOp3(p, op, p1, 0, 0);
61511 }
61512 SQLITE_PRIVATE int sqlite3VdbeAddOp2(Vdbe *p, int op, int p1, int p2){
61513   return sqlite3VdbeAddOp3(p, op, p1, p2, 0);
61514 }
61515 
61516 
61517 /*
61518 ** Add an opcode that includes the p4 value as a pointer.
61519 */
61520 SQLITE_PRIVATE int sqlite3VdbeAddOp4(
61521   Vdbe *p,            /* Add the opcode to this VM */
61522   int op,             /* The new opcode */
61523   int p1,             /* The P1 operand */
61524   int p2,             /* The P2 operand */
61525   int p3,             /* The P3 operand */
61526   const char *zP4,    /* The P4 operand */
61527   int p4type          /* P4 operand type */
61528 ){
61529   int addr = sqlite3VdbeAddOp3(p, op, p1, p2, p3);
61530   sqlite3VdbeChangeP4(p, addr, zP4, p4type);
61531   return addr;
61532 }
61533 
61534 /*
61535 ** Add an OP_ParseSchema opcode.  This routine is broken out from
61536 ** sqlite3VdbeAddOp4() since it needs to also needs to mark all btrees
61537 ** as having been used.
61538 **
61539 ** The zWhere string must have been obtained from sqlite3_malloc().
61540 ** This routine will take ownership of the allocated memory.
61541 */
61542 SQLITE_PRIVATE void sqlite3VdbeAddParseSchemaOp(Vdbe *p, int iDb, char *zWhere){
61543   int j;
61544   int addr = sqlite3VdbeAddOp3(p, OP_ParseSchema, iDb, 0, 0);
61545   sqlite3VdbeChangeP4(p, addr, zWhere, P4_DYNAMIC);
61546   for(j=0; j<p->db->nDb; j++) sqlite3VdbeUsesBtree(p, j);
61547 }
61548 
61549 /*
61550 ** Add an opcode that includes the p4 value as an integer.
61551 */
61552 SQLITE_PRIVATE int sqlite3VdbeAddOp4Int(
61553   Vdbe *p,            /* Add the opcode to this VM */
61554   int op,             /* The new opcode */
61555   int p1,             /* The P1 operand */
61556   int p2,             /* The P2 operand */
61557   int p3,             /* The P3 operand */
61558   int p4              /* The P4 operand as an integer */
61559 ){
61560   int addr = sqlite3VdbeAddOp3(p, op, p1, p2, p3);
61561   sqlite3VdbeChangeP4(p, addr, SQLITE_INT_TO_PTR(p4), P4_INT32);
61562   return addr;
61563 }
61564 
61565 /*
61566 ** Create a new symbolic label for an instruction that has yet to be
61567 ** coded.  The symbolic label is really just a negative number.  The
61568 ** label can be used as the P2 value of an operation.  Later, when
61569 ** the label is resolved to a specific address, the VDBE will scan
61570 ** through its operation list and change all values of P2 which match
61571 ** the label into the resolved address.
61572 **
61573 ** The VDBE knows that a P2 value is a label because labels are
61574 ** always negative and P2 values are suppose to be non-negative.
61575 ** Hence, a negative P2 value is a label that has yet to be resolved.
61576 **
61577 ** Zero is returned if a malloc() fails.
61578 */
61579 SQLITE_PRIVATE int sqlite3VdbeMakeLabel(Vdbe *v){
61580   Parse *p = v->pParse;
61581   int i = p->nLabel++;
61582   assert( v->magic==VDBE_MAGIC_INIT );
61583   if( (i & (i-1))==0 ){
61584     p->aLabel = sqlite3DbReallocOrFree(p->db, p->aLabel,
61585                                        (i*2+1)*sizeof(p->aLabel[0]));
61586   }
61587   if( p->aLabel ){
61588     p->aLabel[i] = -1;
61589   }
61590   return -1-i;
61591 }
61592 
61593 /*
61594 ** Resolve label "x" to be the address of the next instruction to
61595 ** be inserted.  The parameter "x" must have been obtained from
61596 ** a prior call to sqlite3VdbeMakeLabel().
61597 */
61598 SQLITE_PRIVATE void sqlite3VdbeResolveLabel(Vdbe *v, int x){
61599   Parse *p = v->pParse;
61600   int j = -1-x;
61601   assert( v->magic==VDBE_MAGIC_INIT );
61602   assert( j<p->nLabel );
61603   if( j>=0 && p->aLabel ){
61604     p->aLabel[j] = v->nOp;
61605   }
61606   p->iFixedOp = v->nOp - 1;
61607 }
61608 
61609 /*
61610 ** Mark the VDBE as one that can only be run one time.
61611 */
61612 SQLITE_PRIVATE void sqlite3VdbeRunOnlyOnce(Vdbe *p){
61613   p->runOnlyOnce = 1;
61614 }
61615 
61616 #ifdef SQLITE_DEBUG /* sqlite3AssertMayAbort() logic */
61617 
61618 /*
61619 ** The following type and function are used to iterate through all opcodes
61620 ** in a Vdbe main program and each of the sub-programs (triggers) it may
61621 ** invoke directly or indirectly. It should be used as follows:
61622 **
61623 **   Op *pOp;
61624 **   VdbeOpIter sIter;
61625 **
61626 **   memset(&sIter, 0, sizeof(sIter));
61627 **   sIter.v = v;                            // v is of type Vdbe*
61628 **   while( (pOp = opIterNext(&sIter)) ){
61629 **     // Do something with pOp
61630 **   }
61631 **   sqlite3DbFree(v->db, sIter.apSub);
61632 **
61633 */
61634 typedef struct VdbeOpIter VdbeOpIter;
61635 struct VdbeOpIter {
61636   Vdbe *v;                   /* Vdbe to iterate through the opcodes of */
61637   SubProgram **apSub;        /* Array of subprograms */
61638   int nSub;                  /* Number of entries in apSub */
61639   int iAddr;                 /* Address of next instruction to return */
61640   int iSub;                  /* 0 = main program, 1 = first sub-program etc. */
61641 };
61642 static Op *opIterNext(VdbeOpIter *p){
61643   Vdbe *v = p->v;
61644   Op *pRet = 0;
61645   Op *aOp;
61646   int nOp;
61647 
61648   if( p->iSub<=p->nSub ){
61649 
61650     if( p->iSub==0 ){
61651       aOp = v->aOp;
61652       nOp = v->nOp;
61653     }else{
61654       aOp = p->apSub[p->iSub-1]->aOp;
61655       nOp = p->apSub[p->iSub-1]->nOp;
61656     }
61657     assert( p->iAddr<nOp );
61658 
61659     pRet = &aOp[p->iAddr];
61660     p->iAddr++;
61661     if( p->iAddr==nOp ){
61662       p->iSub++;
61663       p->iAddr = 0;
61664     }
61665 
61666     if( pRet->p4type==P4_SUBPROGRAM ){
61667       int nByte = (p->nSub+1)*sizeof(SubProgram*);
61668       int j;
61669       for(j=0; j<p->nSub; j++){
61670         if( p->apSub[j]==pRet->p4.pProgram ) break;
61671       }
61672       if( j==p->nSub ){
61673         p->apSub = sqlite3DbReallocOrFree(v->db, p->apSub, nByte);
61674         if( !p->apSub ){
61675           pRet = 0;
61676         }else{
61677           p->apSub[p->nSub++] = pRet->p4.pProgram;
61678         }
61679       }
61680     }
61681   }
61682 
61683   return pRet;
61684 }
61685 
61686 /*
61687 ** Check if the program stored in the VM associated with pParse may
61688 ** throw an ABORT exception (causing the statement, but not entire transaction
61689 ** to be rolled back). This condition is true if the main program or any
61690 ** sub-programs contains any of the following:
61691 **
61692 **   *  OP_Halt with P1=SQLITE_CONSTRAINT and P2=OE_Abort.
61693 **   *  OP_HaltIfNull with P1=SQLITE_CONSTRAINT and P2=OE_Abort.
61694 **   *  OP_Destroy
61695 **   *  OP_VUpdate
61696 **   *  OP_VRename
61697 **   *  OP_FkCounter with P2==0 (immediate foreign key constraint)
61698 **
61699 ** Then check that the value of Parse.mayAbort is true if an
61700 ** ABORT may be thrown, or false otherwise. Return true if it does
61701 ** match, or false otherwise. This function is intended to be used as
61702 ** part of an assert statement in the compiler. Similar to:
61703 **
61704 **   assert( sqlite3VdbeAssertMayAbort(pParse->pVdbe, pParse->mayAbort) );
61705 */
61706 SQLITE_PRIVATE int sqlite3VdbeAssertMayAbort(Vdbe *v, int mayAbort){
61707   int hasAbort = 0;
61708   Op *pOp;
61709   VdbeOpIter sIter;
61710   memset(&sIter, 0, sizeof(sIter));
61711   sIter.v = v;
61712 
61713   while( (pOp = opIterNext(&sIter))!=0 ){
61714     int opcode = pOp->opcode;
61715     if( opcode==OP_Destroy || opcode==OP_VUpdate || opcode==OP_VRename
61716 #ifndef SQLITE_OMIT_FOREIGN_KEY
61717      || (opcode==OP_FkCounter && pOp->p1==0 && pOp->p2==1)
61718 #endif
61719      || ((opcode==OP_Halt || opcode==OP_HaltIfNull)
61720       && ((pOp->p1&0xff)==SQLITE_CONSTRAINT && pOp->p2==OE_Abort))
61721     ){
61722       hasAbort = 1;
61723       break;
61724     }
61725   }
61726   sqlite3DbFree(v->db, sIter.apSub);
61727 
61728   /* Return true if hasAbort==mayAbort. Or if a malloc failure occurred.
61729   ** If malloc failed, then the while() loop above may not have iterated
61730   ** through all opcodes and hasAbort may be set incorrectly. Return
61731   ** true for this case to prevent the assert() in the callers frame
61732   ** from failing.  */
61733   return ( v->db->mallocFailed || hasAbort==mayAbort );
61734 }
61735 #endif /* SQLITE_DEBUG - the sqlite3AssertMayAbort() function */
61736 
61737 /*
61738 ** Loop through the program looking for P2 values that are negative
61739 ** on jump instructions.  Each such value is a label.  Resolve the
61740 ** label by setting the P2 value to its correct non-zero value.
61741 **
61742 ** This routine is called once after all opcodes have been inserted.
61743 **
61744 ** Variable *pMaxFuncArgs is set to the maximum value of any P2 argument
61745 ** to an OP_Function, OP_AggStep or OP_VFilter opcode. This is used by
61746 ** sqlite3VdbeMakeReady() to size the Vdbe.apArg[] array.
61747 **
61748 ** The Op.opflags field is set on all opcodes.
61749 */
61750 static void resolveP2Values(Vdbe *p, int *pMaxFuncArgs){
61751   int i;
61752   int nMaxArgs = *pMaxFuncArgs;
61753   Op *pOp;
61754   Parse *pParse = p->pParse;
61755   int *aLabel = pParse->aLabel;
61756   p->readOnly = 1;
61757   p->bIsReader = 0;
61758   for(pOp=p->aOp, i=p->nOp-1; i>=0; i--, pOp++){
61759     u8 opcode = pOp->opcode;
61760 
61761     /* NOTE: Be sure to update mkopcodeh.awk when adding or removing
61762     ** cases from this switch! */
61763     switch( opcode ){
61764       case OP_Function:
61765       case OP_AggStep: {
61766         if( pOp->p5>nMaxArgs ) nMaxArgs = pOp->p5;
61767         break;
61768       }
61769       case OP_Transaction: {
61770         if( pOp->p2!=0 ) p->readOnly = 0;
61771         /* fall thru */
61772       }
61773       case OP_AutoCommit:
61774       case OP_Savepoint: {
61775         p->bIsReader = 1;
61776         break;
61777       }
61778 #ifndef SQLITE_OMIT_WAL
61779       case OP_Checkpoint:
61780 #endif
61781       case OP_Vacuum:
61782       case OP_JournalMode: {
61783         p->readOnly = 0;
61784         p->bIsReader = 1;
61785         break;
61786       }
61787 #ifndef SQLITE_OMIT_VIRTUALTABLE
61788       case OP_VUpdate: {
61789         if( pOp->p2>nMaxArgs ) nMaxArgs = pOp->p2;
61790         break;
61791       }
61792       case OP_VFilter: {
61793         int n;
61794         assert( p->nOp - i >= 3 );
61795         assert( pOp[-1].opcode==OP_Integer );
61796         n = pOp[-1].p1;
61797         if( n>nMaxArgs ) nMaxArgs = n;
61798         break;
61799       }
61800 #endif
61801       case OP_Next:
61802       case OP_NextIfOpen:
61803       case OP_SorterNext: {
61804         pOp->p4.xAdvance = sqlite3BtreeNext;
61805         pOp->p4type = P4_ADVANCE;
61806         break;
61807       }
61808       case OP_Prev:
61809       case OP_PrevIfOpen: {
61810         pOp->p4.xAdvance = sqlite3BtreePrevious;
61811         pOp->p4type = P4_ADVANCE;
61812         break;
61813       }
61814     }
61815 
61816     pOp->opflags = sqlite3OpcodeProperty[opcode];
61817     if( (pOp->opflags & OPFLG_JUMP)!=0 && pOp->p2<0 ){
61818       assert( -1-pOp->p2<pParse->nLabel );
61819       pOp->p2 = aLabel[-1-pOp->p2];
61820     }
61821   }
61822   sqlite3DbFree(p->db, pParse->aLabel);
61823   pParse->aLabel = 0;
61824   pParse->nLabel = 0;
61825   *pMaxFuncArgs = nMaxArgs;
61826   assert( p->bIsReader!=0 || p->btreeMask==0 );
61827 }
61828 
61829 /*
61830 ** Return the address of the next instruction to be inserted.
61831 */
61832 SQLITE_PRIVATE int sqlite3VdbeCurrentAddr(Vdbe *p){
61833   assert( p->magic==VDBE_MAGIC_INIT );
61834   return p->nOp;
61835 }
61836 
61837 /*
61838 ** This function returns a pointer to the array of opcodes associated with
61839 ** the Vdbe passed as the first argument. It is the callers responsibility
61840 ** to arrange for the returned array to be eventually freed using the
61841 ** vdbeFreeOpArray() function.
61842 **
61843 ** Before returning, *pnOp is set to the number of entries in the returned
61844 ** array. Also, *pnMaxArg is set to the larger of its current value and
61845 ** the number of entries in the Vdbe.apArg[] array required to execute the
61846 ** returned program.
61847 */
61848 SQLITE_PRIVATE VdbeOp *sqlite3VdbeTakeOpArray(Vdbe *p, int *pnOp, int *pnMaxArg){
61849   VdbeOp *aOp = p->aOp;
61850   assert( aOp && !p->db->mallocFailed );
61851 
61852   /* Check that sqlite3VdbeUsesBtree() was not called on this VM */
61853   assert( p->btreeMask==0 );
61854 
61855   resolveP2Values(p, pnMaxArg);
61856   *pnOp = p->nOp;
61857   p->aOp = 0;
61858   return aOp;
61859 }
61860 
61861 /*
61862 ** Add a whole list of operations to the operation stack.  Return the
61863 ** address of the first operation added.
61864 */
61865 SQLITE_PRIVATE int sqlite3VdbeAddOpList(Vdbe *p, int nOp, VdbeOpList const *aOp){
61866   int addr;
61867   assert( p->magic==VDBE_MAGIC_INIT );
61868   if( p->nOp + nOp > p->pParse->nOpAlloc && growOpArray(p) ){
61869     return 0;
61870   }
61871   addr = p->nOp;
61872   if( ALWAYS(nOp>0) ){
61873     int i;
61874     VdbeOpList const *pIn = aOp;
61875     for(i=0; i<nOp; i++, pIn++){
61876       int p2 = pIn->p2;
61877       VdbeOp *pOut = &p->aOp[i+addr];
61878       pOut->opcode = pIn->opcode;
61879       pOut->p1 = pIn->p1;
61880       if( p2<0 ){
61881         assert( sqlite3OpcodeProperty[pOut->opcode] & OPFLG_JUMP );
61882         pOut->p2 = addr + ADDR(p2);
61883       }else{
61884         pOut->p2 = p2;
61885       }
61886       pOut->p3 = pIn->p3;
61887       pOut->p4type = P4_NOTUSED;
61888       pOut->p4.p = 0;
61889       pOut->p5 = 0;
61890 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
61891       pOut->zComment = 0;
61892 #endif
61893 #ifdef SQLITE_DEBUG
61894       if( p->db->flags & SQLITE_VdbeAddopTrace ){
61895         sqlite3VdbePrintOp(0, i+addr, &p->aOp[i+addr]);
61896       }
61897 #endif
61898     }
61899     p->nOp += nOp;
61900   }
61901   return addr;
61902 }
61903 
61904 /*
61905 ** Change the value of the P1 operand for a specific instruction.
61906 ** This routine is useful when a large program is loaded from a
61907 ** static array using sqlite3VdbeAddOpList but we want to make a
61908 ** few minor changes to the program.
61909 */
61910 SQLITE_PRIVATE void sqlite3VdbeChangeP1(Vdbe *p, u32 addr, int val){
61911   assert( p!=0 );
61912   if( ((u32)p->nOp)>addr ){
61913     p->aOp[addr].p1 = val;
61914   }
61915 }
61916 
61917 /*
61918 ** Change the value of the P2 operand for a specific instruction.
61919 ** This routine is useful for setting a jump destination.
61920 */
61921 SQLITE_PRIVATE void sqlite3VdbeChangeP2(Vdbe *p, u32 addr, int val){
61922   assert( p!=0 );
61923   if( ((u32)p->nOp)>addr ){
61924     p->aOp[addr].p2 = val;
61925   }
61926 }
61927 
61928 /*
61929 ** Change the value of the P3 operand for a specific instruction.
61930 */
61931 SQLITE_PRIVATE void sqlite3VdbeChangeP3(Vdbe *p, u32 addr, int val){
61932   assert( p!=0 );
61933   if( ((u32)p->nOp)>addr ){
61934     p->aOp[addr].p3 = val;
61935   }
61936 }
61937 
61938 /*
61939 ** Change the value of the P5 operand for the most recently
61940 ** added operation.
61941 */
61942 SQLITE_PRIVATE void sqlite3VdbeChangeP5(Vdbe *p, u8 val){
61943   assert( p!=0 );
61944   if( p->aOp ){
61945     assert( p->nOp>0 );
61946     p->aOp[p->nOp-1].p5 = val;
61947   }
61948 }
61949 
61950 /*
61951 ** Change the P2 operand of instruction addr so that it points to
61952 ** the address of the next instruction to be coded.
61953 */
61954 SQLITE_PRIVATE void sqlite3VdbeJumpHere(Vdbe *p, int addr){
61955   sqlite3VdbeChangeP2(p, addr, p->nOp);
61956   p->pParse->iFixedOp = p->nOp - 1;
61957 }
61958 
61959 
61960 /*
61961 ** If the input FuncDef structure is ephemeral, then free it.  If
61962 ** the FuncDef is not ephermal, then do nothing.
61963 */
61964 static void freeEphemeralFunction(sqlite3 *db, FuncDef *pDef){
61965   if( ALWAYS(pDef) && (pDef->funcFlags & SQLITE_FUNC_EPHEM)!=0 ){
61966     sqlite3DbFree(db, pDef);
61967   }
61968 }
61969 
61970 static void vdbeFreeOpArray(sqlite3 *, Op *, int);
61971 
61972 /*
61973 ** Delete a P4 value if necessary.
61974 */
61975 static void freeP4(sqlite3 *db, int p4type, void *p4){
61976   if( p4 ){
61977     assert( db );
61978     switch( p4type ){
61979       case P4_REAL:
61980       case P4_INT64:
61981       case P4_DYNAMIC:
61982       case P4_INTARRAY: {
61983         sqlite3DbFree(db, p4);
61984         break;
61985       }
61986       case P4_KEYINFO: {
61987         if( db->pnBytesFreed==0 ) sqlite3KeyInfoUnref((KeyInfo*)p4);
61988         break;
61989       }
61990       case P4_MPRINTF: {
61991         if( db->pnBytesFreed==0 ) sqlite3_free(p4);
61992         break;
61993       }
61994       case P4_FUNCDEF: {
61995         freeEphemeralFunction(db, (FuncDef*)p4);
61996         break;
61997       }
61998       case P4_MEM: {
61999         if( db->pnBytesFreed==0 ){
62000           sqlite3ValueFree((sqlite3_value*)p4);
62001         }else{
62002           Mem *p = (Mem*)p4;
62003           sqlite3DbFree(db, p->zMalloc);
62004           sqlite3DbFree(db, p);
62005         }
62006         break;
62007       }
62008       case P4_VTAB : {
62009         if( db->pnBytesFreed==0 ) sqlite3VtabUnlock((VTable *)p4);
62010         break;
62011       }
62012     }
62013   }
62014 }
62015 
62016 /*
62017 ** Free the space allocated for aOp and any p4 values allocated for the
62018 ** opcodes contained within. If aOp is not NULL it is assumed to contain
62019 ** nOp entries.
62020 */
62021 static void vdbeFreeOpArray(sqlite3 *db, Op *aOp, int nOp){
62022   if( aOp ){
62023     Op *pOp;
62024     for(pOp=aOp; pOp<&aOp[nOp]; pOp++){
62025       freeP4(db, pOp->p4type, pOp->p4.p);
62026 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
62027       sqlite3DbFree(db, pOp->zComment);
62028 #endif
62029     }
62030   }
62031   sqlite3DbFree(db, aOp);
62032 }
62033 
62034 /*
62035 ** Link the SubProgram object passed as the second argument into the linked
62036 ** list at Vdbe.pSubProgram. This list is used to delete all sub-program
62037 ** objects when the VM is no longer required.
62038 */
62039 SQLITE_PRIVATE void sqlite3VdbeLinkSubProgram(Vdbe *pVdbe, SubProgram *p){
62040   p->pNext = pVdbe->pProgram;
62041   pVdbe->pProgram = p;
62042 }
62043 
62044 /*
62045 ** Change the opcode at addr into OP_Noop
62046 */
62047 SQLITE_PRIVATE void sqlite3VdbeChangeToNoop(Vdbe *p, int addr){
62048   if( p->aOp ){
62049     VdbeOp *pOp = &p->aOp[addr];
62050     sqlite3 *db = p->db;
62051     freeP4(db, pOp->p4type, pOp->p4.p);
62052     memset(pOp, 0, sizeof(pOp[0]));
62053     pOp->opcode = OP_Noop;
62054     if( addr==p->nOp-1 ) p->nOp--;
62055   }
62056 }
62057 
62058 /*
62059 ** Remove the last opcode inserted
62060 */
62061 SQLITE_PRIVATE int sqlite3VdbeDeletePriorOpcode(Vdbe *p, u8 op){
62062   if( (p->nOp-1)>(p->pParse->iFixedOp) && p->aOp[p->nOp-1].opcode==op ){
62063     sqlite3VdbeChangeToNoop(p, p->nOp-1);
62064     return 1;
62065   }else{
62066     return 0;
62067   }
62068 }
62069 
62070 /*
62071 ** Change the value of the P4 operand for a specific instruction.
62072 ** This routine is useful when a large program is loaded from a
62073 ** static array using sqlite3VdbeAddOpList but we want to make a
62074 ** few minor changes to the program.
62075 **
62076 ** If n>=0 then the P4 operand is dynamic, meaning that a copy of
62077 ** the string is made into memory obtained from sqlite3_malloc().
62078 ** A value of n==0 means copy bytes of zP4 up to and including the
62079 ** first null byte.  If n>0 then copy n+1 bytes of zP4.
62080 **
62081 ** Other values of n (P4_STATIC, P4_COLLSEQ etc.) indicate that zP4 points
62082 ** to a string or structure that is guaranteed to exist for the lifetime of
62083 ** the Vdbe. In these cases we can just copy the pointer.
62084 **
62085 ** If addr<0 then change P4 on the most recently inserted instruction.
62086 */
62087 SQLITE_PRIVATE void sqlite3VdbeChangeP4(Vdbe *p, int addr, const char *zP4, int n){
62088   Op *pOp;
62089   sqlite3 *db;
62090   assert( p!=0 );
62091   db = p->db;
62092   assert( p->magic==VDBE_MAGIC_INIT );
62093   if( p->aOp==0 || db->mallocFailed ){
62094     if( n!=P4_VTAB ){
62095       freeP4(db, n, (void*)*(char**)&zP4);
62096     }
62097     return;
62098   }
62099   assert( p->nOp>0 );
62100   assert( addr<p->nOp );
62101   if( addr<0 ){
62102     addr = p->nOp - 1;
62103   }
62104   pOp = &p->aOp[addr];
62105   assert( pOp->p4type==P4_NOTUSED || pOp->p4type==P4_INT32 );
62106   freeP4(db, pOp->p4type, pOp->p4.p);
62107   pOp->p4.p = 0;
62108   if( n==P4_INT32 ){
62109     /* Note: this cast is safe, because the origin data point was an int
62110     ** that was cast to a (const char *). */
62111     pOp->p4.i = SQLITE_PTR_TO_INT(zP4);
62112     pOp->p4type = P4_INT32;
62113   }else if( zP4==0 ){
62114     pOp->p4.p = 0;
62115     pOp->p4type = P4_NOTUSED;
62116   }else if( n==P4_KEYINFO ){
62117     pOp->p4.p = (void*)zP4;
62118     pOp->p4type = P4_KEYINFO;
62119   }else if( n==P4_VTAB ){
62120     pOp->p4.p = (void*)zP4;
62121     pOp->p4type = P4_VTAB;
62122     sqlite3VtabLock((VTable *)zP4);
62123     assert( ((VTable *)zP4)->db==p->db );
62124   }else if( n<0 ){
62125     pOp->p4.p = (void*)zP4;
62126     pOp->p4type = (signed char)n;
62127   }else{
62128     if( n==0 ) n = sqlite3Strlen30(zP4);
62129     pOp->p4.z = sqlite3DbStrNDup(p->db, zP4, n);
62130     pOp->p4type = P4_DYNAMIC;
62131   }
62132 }
62133 
62134 /*
62135 ** Set the P4 on the most recently added opcode to the KeyInfo for the
62136 ** index given.
62137 */
62138 SQLITE_PRIVATE void sqlite3VdbeSetP4KeyInfo(Parse *pParse, Index *pIdx){
62139   Vdbe *v = pParse->pVdbe;
62140   assert( v!=0 );
62141   assert( pIdx!=0 );
62142   sqlite3VdbeChangeP4(v, -1, (char*)sqlite3KeyInfoOfIndex(pParse, pIdx),
62143                       P4_KEYINFO);
62144 }
62145 
62146 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
62147 /*
62148 ** Change the comment on the most recently coded instruction.  Or
62149 ** insert a No-op and add the comment to that new instruction.  This
62150 ** makes the code easier to read during debugging.  None of this happens
62151 ** in a production build.
62152 */
62153 static void vdbeVComment(Vdbe *p, const char *zFormat, va_list ap){
62154   assert( p->nOp>0 || p->aOp==0 );
62155   assert( p->aOp==0 || p->aOp[p->nOp-1].zComment==0 || p->db->mallocFailed );
62156   if( p->nOp ){
62157     assert( p->aOp );
62158     sqlite3DbFree(p->db, p->aOp[p->nOp-1].zComment);
62159     p->aOp[p->nOp-1].zComment = sqlite3VMPrintf(p->db, zFormat, ap);
62160   }
62161 }
62162 SQLITE_PRIVATE void sqlite3VdbeComment(Vdbe *p, const char *zFormat, ...){
62163   va_list ap;
62164   if( p ){
62165     va_start(ap, zFormat);
62166     vdbeVComment(p, zFormat, ap);
62167     va_end(ap);
62168   }
62169 }
62170 SQLITE_PRIVATE void sqlite3VdbeNoopComment(Vdbe *p, const char *zFormat, ...){
62171   va_list ap;
62172   if( p ){
62173     sqlite3VdbeAddOp0(p, OP_Noop);
62174     va_start(ap, zFormat);
62175     vdbeVComment(p, zFormat, ap);
62176     va_end(ap);
62177   }
62178 }
62179 #endif  /* NDEBUG */
62180 
62181 /*
62182 ** Return the opcode for a given address.  If the address is -1, then
62183 ** return the most recently inserted opcode.
62184 **
62185 ** If a memory allocation error has occurred prior to the calling of this
62186 ** routine, then a pointer to a dummy VdbeOp will be returned.  That opcode
62187 ** is readable but not writable, though it is cast to a writable value.
62188 ** The return of a dummy opcode allows the call to continue functioning
62189 ** after a OOM fault without having to check to see if the return from
62190 ** this routine is a valid pointer.  But because the dummy.opcode is 0,
62191 ** dummy will never be written to.  This is verified by code inspection and
62192 ** by running with Valgrind.
62193 **
62194 ** About the #ifdef SQLITE_OMIT_TRACE:  Normally, this routine is never called
62195 ** unless p->nOp>0.  This is because in the absense of SQLITE_OMIT_TRACE,
62196 ** an OP_Trace instruction is always inserted by sqlite3VdbeGet() as soon as
62197 ** a new VDBE is created.  So we are free to set addr to p->nOp-1 without
62198 ** having to double-check to make sure that the result is non-negative. But
62199 ** if SQLITE_OMIT_TRACE is defined, the OP_Trace is omitted and we do need to
62200 ** check the value of p->nOp-1 before continuing.
62201 */
62202 SQLITE_PRIVATE VdbeOp *sqlite3VdbeGetOp(Vdbe *p, int addr){
62203   /* C89 specifies that the constant "dummy" will be initialized to all
62204   ** zeros, which is correct.  MSVC generates a warning, nevertheless. */
62205   static VdbeOp dummy;  /* Ignore the MSVC warning about no initializer */
62206   assert( p->magic==VDBE_MAGIC_INIT );
62207   if( addr<0 ){
62208 #ifdef SQLITE_OMIT_TRACE
62209     if( p->nOp==0 ) return (VdbeOp*)&dummy;
62210 #endif
62211     addr = p->nOp - 1;
62212   }
62213   assert( (addr>=0 && addr<p->nOp) || p->db->mallocFailed );
62214   if( p->db->mallocFailed ){
62215     return (VdbeOp*)&dummy;
62216   }else{
62217     return &p->aOp[addr];
62218   }
62219 }
62220 
62221 #if defined(SQLITE_ENABLE_EXPLAIN_COMMENTS)
62222 /*
62223 ** Return an integer value for one of the parameters to the opcode pOp
62224 ** determined by character c.
62225 */
62226 static int translateP(char c, const Op *pOp){
62227   if( c=='1' ) return pOp->p1;
62228   if( c=='2' ) return pOp->p2;
62229   if( c=='3' ) return pOp->p3;
62230   if( c=='4' ) return pOp->p4.i;
62231   return pOp->p5;
62232 }
62233 
62234 /*
62235 ** Compute a string for the "comment" field of a VDBE opcode listing.
62236 **
62237 ** The Synopsis: field in comments in the vdbe.c source file gets converted
62238 ** to an extra string that is appended to the sqlite3OpcodeName().  In the
62239 ** absence of other comments, this synopsis becomes the comment on the opcode.
62240 ** Some translation occurs:
62241 **
62242 **       "PX"      ->  "r[X]"
62243 **       "PX@PY"   ->  "r[X..X+Y-1]"  or "r[x]" if y is 0 or 1
62244 **       "PX@PY+1" ->  "r[X..X+Y]"    or "r[x]" if y is 0
62245 **       "PY..PY"  ->  "r[X..Y]"      or "r[x]" if y<=x
62246 */
62247 static int displayComment(
62248   const Op *pOp,     /* The opcode to be commented */
62249   const char *zP4,   /* Previously obtained value for P4 */
62250   char *zTemp,       /* Write result here */
62251   int nTemp          /* Space available in zTemp[] */
62252 ){
62253   const char *zOpName;
62254   const char *zSynopsis;
62255   int nOpName;
62256   int ii, jj;
62257   zOpName = sqlite3OpcodeName(pOp->opcode);
62258   nOpName = sqlite3Strlen30(zOpName);
62259   if( zOpName[nOpName+1] ){
62260     int seenCom = 0;
62261     char c;
62262     zSynopsis = zOpName += nOpName + 1;
62263     for(ii=jj=0; jj<nTemp-1 && (c = zSynopsis[ii])!=0; ii++){
62264       if( c=='P' ){
62265         c = zSynopsis[++ii];
62266         if( c=='4' ){
62267           sqlite3_snprintf(nTemp-jj, zTemp+jj, "%s", zP4);
62268         }else if( c=='X' ){
62269           sqlite3_snprintf(nTemp-jj, zTemp+jj, "%s", pOp->zComment);
62270           seenCom = 1;
62271         }else{
62272           int v1 = translateP(c, pOp);
62273           int v2;
62274           sqlite3_snprintf(nTemp-jj, zTemp+jj, "%d", v1);
62275           if( strncmp(zSynopsis+ii+1, "@P", 2)==0 ){
62276             ii += 3;
62277             jj += sqlite3Strlen30(zTemp+jj);
62278             v2 = translateP(zSynopsis[ii], pOp);
62279             if( strncmp(zSynopsis+ii+1,"+1",2)==0 ){
62280               ii += 2;
62281               v2++;
62282             }
62283             if( v2>1 ){
62284               sqlite3_snprintf(nTemp-jj, zTemp+jj, "..%d", v1+v2-1);
62285             }
62286           }else if( strncmp(zSynopsis+ii+1, "..P3", 4)==0 && pOp->p3==0 ){
62287             ii += 4;
62288           }
62289         }
62290         jj += sqlite3Strlen30(zTemp+jj);
62291       }else{
62292         zTemp[jj++] = c;
62293       }
62294     }
62295     if( !seenCom && jj<nTemp-5 && pOp->zComment ){
62296       sqlite3_snprintf(nTemp-jj, zTemp+jj, "; %s", pOp->zComment);
62297       jj += sqlite3Strlen30(zTemp+jj);
62298     }
62299     if( jj<nTemp ) zTemp[jj] = 0;
62300   }else if( pOp->zComment ){
62301     sqlite3_snprintf(nTemp, zTemp, "%s", pOp->zComment);
62302     jj = sqlite3Strlen30(zTemp);
62303   }else{
62304     zTemp[0] = 0;
62305     jj = 0;
62306   }
62307   return jj;
62308 }
62309 #endif /* SQLITE_DEBUG */
62310 
62311 
62312 #if !defined(SQLITE_OMIT_EXPLAIN) || !defined(NDEBUG) \
62313      || defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
62314 /*
62315 ** Compute a string that describes the P4 parameter for an opcode.
62316 ** Use zTemp for any required temporary buffer space.
62317 */
62318 static char *displayP4(Op *pOp, char *zTemp, int nTemp){
62319   char *zP4 = zTemp;
62320   assert( nTemp>=20 );
62321   switch( pOp->p4type ){
62322     case P4_KEYINFO: {
62323       int i, j;
62324       KeyInfo *pKeyInfo = pOp->p4.pKeyInfo;
62325       assert( pKeyInfo->aSortOrder!=0 );
62326       sqlite3_snprintf(nTemp, zTemp, "k(%d", pKeyInfo->nField);
62327       i = sqlite3Strlen30(zTemp);
62328       for(j=0; j<pKeyInfo->nField; j++){
62329         CollSeq *pColl = pKeyInfo->aColl[j];
62330         const char *zColl = pColl ? pColl->zName : "nil";
62331         int n = sqlite3Strlen30(zColl);
62332         if( n==6 && memcmp(zColl,"BINARY",6)==0 ){
62333           zColl = "B";
62334           n = 1;
62335         }
62336         if( i+n>nTemp-6 ){
62337           memcpy(&zTemp[i],",...",4);
62338           break;
62339         }
62340         zTemp[i++] = ',';
62341         if( pKeyInfo->aSortOrder[j] ){
62342           zTemp[i++] = '-';
62343         }
62344         memcpy(&zTemp[i], zColl, n+1);
62345         i += n;
62346       }
62347       zTemp[i++] = ')';
62348       zTemp[i] = 0;
62349       assert( i<nTemp );
62350       break;
62351     }
62352     case P4_COLLSEQ: {
62353       CollSeq *pColl = pOp->p4.pColl;
62354       sqlite3_snprintf(nTemp, zTemp, "(%.20s)", pColl->zName);
62355       break;
62356     }
62357     case P4_FUNCDEF: {
62358       FuncDef *pDef = pOp->p4.pFunc;
62359       sqlite3_snprintf(nTemp, zTemp, "%s(%d)", pDef->zName, pDef->nArg);
62360       break;
62361     }
62362     case P4_INT64: {
62363       sqlite3_snprintf(nTemp, zTemp, "%lld", *pOp->p4.pI64);
62364       break;
62365     }
62366     case P4_INT32: {
62367       sqlite3_snprintf(nTemp, zTemp, "%d", pOp->p4.i);
62368       break;
62369     }
62370     case P4_REAL: {
62371       sqlite3_snprintf(nTemp, zTemp, "%.16g", *pOp->p4.pReal);
62372       break;
62373     }
62374     case P4_MEM: {
62375       Mem *pMem = pOp->p4.pMem;
62376       if( pMem->flags & MEM_Str ){
62377         zP4 = pMem->z;
62378       }else if( pMem->flags & MEM_Int ){
62379         sqlite3_snprintf(nTemp, zTemp, "%lld", pMem->u.i);
62380       }else if( pMem->flags & MEM_Real ){
62381         sqlite3_snprintf(nTemp, zTemp, "%.16g", pMem->r);
62382       }else if( pMem->flags & MEM_Null ){
62383         sqlite3_snprintf(nTemp, zTemp, "NULL");
62384       }else{
62385         assert( pMem->flags & MEM_Blob );
62386         zP4 = "(blob)";
62387       }
62388       break;
62389     }
62390 #ifndef SQLITE_OMIT_VIRTUALTABLE
62391     case P4_VTAB: {
62392       sqlite3_vtab *pVtab = pOp->p4.pVtab->pVtab;
62393       sqlite3_snprintf(nTemp, zTemp, "vtab:%p:%p", pVtab, pVtab->pModule);
62394       break;
62395     }
62396 #endif
62397     case P4_INTARRAY: {
62398       sqlite3_snprintf(nTemp, zTemp, "intarray");
62399       break;
62400     }
62401     case P4_SUBPROGRAM: {
62402       sqlite3_snprintf(nTemp, zTemp, "program");
62403       break;
62404     }
62405     case P4_ADVANCE: {
62406       zTemp[0] = 0;
62407       break;
62408     }
62409     default: {
62410       zP4 = pOp->p4.z;
62411       if( zP4==0 ){
62412         zP4 = zTemp;
62413         zTemp[0] = 0;
62414       }
62415     }
62416   }
62417   assert( zP4!=0 );
62418   return zP4;
62419 }
62420 #endif
62421 
62422 /*
62423 ** Declare to the Vdbe that the BTree object at db->aDb[i] is used.
62424 **
62425 ** The prepared statements need to know in advance the complete set of
62426 ** attached databases that will be use.  A mask of these databases
62427 ** is maintained in p->btreeMask.  The p->lockMask value is the subset of
62428 ** p->btreeMask of databases that will require a lock.
62429 */
62430 SQLITE_PRIVATE void sqlite3VdbeUsesBtree(Vdbe *p, int i){
62431   assert( i>=0 && i<p->db->nDb && i<(int)sizeof(yDbMask)*8 );
62432   assert( i<(int)sizeof(p->btreeMask)*8 );
62433   p->btreeMask |= ((yDbMask)1)<<i;
62434   if( i!=1 && sqlite3BtreeSharable(p->db->aDb[i].pBt) ){
62435     p->lockMask |= ((yDbMask)1)<<i;
62436   }
62437 }
62438 
62439 #if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0
62440 /*
62441 ** If SQLite is compiled to support shared-cache mode and to be threadsafe,
62442 ** this routine obtains the mutex associated with each BtShared structure
62443 ** that may be accessed by the VM passed as an argument. In doing so it also
62444 ** sets the BtShared.db member of each of the BtShared structures, ensuring
62445 ** that the correct busy-handler callback is invoked if required.
62446 **
62447 ** If SQLite is not threadsafe but does support shared-cache mode, then
62448 ** sqlite3BtreeEnter() is invoked to set the BtShared.db variables
62449 ** of all of BtShared structures accessible via the database handle
62450 ** associated with the VM.
62451 **
62452 ** If SQLite is not threadsafe and does not support shared-cache mode, this
62453 ** function is a no-op.
62454 **
62455 ** The p->btreeMask field is a bitmask of all btrees that the prepared
62456 ** statement p will ever use.  Let N be the number of bits in p->btreeMask
62457 ** corresponding to btrees that use shared cache.  Then the runtime of
62458 ** this routine is N*N.  But as N is rarely more than 1, this should not
62459 ** be a problem.
62460 */
62461 SQLITE_PRIVATE void sqlite3VdbeEnter(Vdbe *p){
62462   int i;
62463   yDbMask mask;
62464   sqlite3 *db;
62465   Db *aDb;
62466   int nDb;
62467   if( p->lockMask==0 ) return;  /* The common case */
62468   db = p->db;
62469   aDb = db->aDb;
62470   nDb = db->nDb;
62471   for(i=0, mask=1; i<nDb; i++, mask += mask){
62472     if( i!=1 && (mask & p->lockMask)!=0 && ALWAYS(aDb[i].pBt!=0) ){
62473       sqlite3BtreeEnter(aDb[i].pBt);
62474     }
62475   }
62476 }
62477 #endif
62478 
62479 #if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE>0
62480 /*
62481 ** Unlock all of the btrees previously locked by a call to sqlite3VdbeEnter().
62482 */
62483 SQLITE_PRIVATE void sqlite3VdbeLeave(Vdbe *p){
62484   int i;
62485   yDbMask mask;
62486   sqlite3 *db;
62487   Db *aDb;
62488   int nDb;
62489   if( p->lockMask==0 ) return;  /* The common case */
62490   db = p->db;
62491   aDb = db->aDb;
62492   nDb = db->nDb;
62493   for(i=0, mask=1; i<nDb; i++, mask += mask){
62494     if( i!=1 && (mask & p->lockMask)!=0 && ALWAYS(aDb[i].pBt!=0) ){
62495       sqlite3BtreeLeave(aDb[i].pBt);
62496     }
62497   }
62498 }
62499 #endif
62500 
62501 #if defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
62502 /*
62503 ** Print a single opcode.  This routine is used for debugging only.
62504 */
62505 SQLITE_PRIVATE void sqlite3VdbePrintOp(FILE *pOut, int pc, Op *pOp){
62506   char *zP4;
62507   char zPtr[50];
62508   char zCom[100];
62509   static const char *zFormat1 = "%4d %-13s %4d %4d %4d %-13s %.2X %s\n";
62510   if( pOut==0 ) pOut = stdout;
62511   zP4 = displayP4(pOp, zPtr, sizeof(zPtr));
62512 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
62513   displayComment(pOp, zP4, zCom, sizeof(zCom));
62514 #else
62515   zCom[0] = 0
62516 #endif
62517   /* NB:  The sqlite3OpcodeName() function is implemented by code created
62518   ** by the mkopcodeh.awk and mkopcodec.awk scripts which extract the
62519   ** information from the vdbe.c source text */
62520   fprintf(pOut, zFormat1, pc,
62521       sqlite3OpcodeName(pOp->opcode), pOp->p1, pOp->p2, pOp->p3, zP4, pOp->p5,
62522       zCom
62523   );
62524   fflush(pOut);
62525 }
62526 #endif
62527 
62528 /*
62529 ** Release an array of N Mem elements
62530 */
62531 static void releaseMemArray(Mem *p, int N){
62532   if( p && N ){
62533     Mem *pEnd;
62534     sqlite3 *db = p->db;
62535     u8 malloc_failed = db->mallocFailed;
62536     if( db->pnBytesFreed ){
62537       for(pEnd=&p[N]; p<pEnd; p++){
62538         sqlite3DbFree(db, p->zMalloc);
62539       }
62540       return;
62541     }
62542     for(pEnd=&p[N]; p<pEnd; p++){
62543       assert( (&p[1])==pEnd || p[0].db==p[1].db );
62544 
62545       /* This block is really an inlined version of sqlite3VdbeMemRelease()
62546       ** that takes advantage of the fact that the memory cell value is
62547       ** being set to NULL after releasing any dynamic resources.
62548       **
62549       ** The justification for duplicating code is that according to
62550       ** callgrind, this causes a certain test case to hit the CPU 4.7
62551       ** percent less (x86 linux, gcc version 4.1.2, -O6) than if
62552       ** sqlite3MemRelease() were called from here. With -O2, this jumps
62553       ** to 6.6 percent. The test case is inserting 1000 rows into a table
62554       ** with no indexes using a single prepared INSERT statement, bind()
62555       ** and reset(). Inserts are grouped into a transaction.
62556       */
62557       if( p->flags&(MEM_Agg|MEM_Dyn|MEM_Frame|MEM_RowSet) ){
62558         sqlite3VdbeMemRelease(p);
62559       }else if( p->zMalloc ){
62560         sqlite3DbFree(db, p->zMalloc);
62561         p->zMalloc = 0;
62562       }
62563 
62564       p->flags = MEM_Invalid;
62565     }
62566     db->mallocFailed = malloc_failed;
62567   }
62568 }
62569 
62570 /*
62571 ** Delete a VdbeFrame object and its contents. VdbeFrame objects are
62572 ** allocated by the OP_Program opcode in sqlite3VdbeExec().
62573 */
62574 SQLITE_PRIVATE void sqlite3VdbeFrameDelete(VdbeFrame *p){
62575   int i;
62576   Mem *aMem = VdbeFrameMem(p);
62577   VdbeCursor **apCsr = (VdbeCursor **)&aMem[p->nChildMem];
62578   for(i=0; i<p->nChildCsr; i++){
62579     sqlite3VdbeFreeCursor(p->v, apCsr[i]);
62580   }
62581   releaseMemArray(aMem, p->nChildMem);
62582   sqlite3DbFree(p->v->db, p);
62583 }
62584 
62585 #ifndef SQLITE_OMIT_EXPLAIN
62586 /*
62587 ** Give a listing of the program in the virtual machine.
62588 **
62589 ** The interface is the same as sqlite3VdbeExec().  But instead of
62590 ** running the code, it invokes the callback once for each instruction.
62591 ** This feature is used to implement "EXPLAIN".
62592 **
62593 ** When p->explain==1, each instruction is listed.  When
62594 ** p->explain==2, only OP_Explain instructions are listed and these
62595 ** are shown in a different format.  p->explain==2 is used to implement
62596 ** EXPLAIN QUERY PLAN.
62597 **
62598 ** When p->explain==1, first the main program is listed, then each of
62599 ** the trigger subprograms are listed one by one.
62600 */
62601 SQLITE_PRIVATE int sqlite3VdbeList(
62602   Vdbe *p                   /* The VDBE */
62603 ){
62604   int nRow;                            /* Stop when row count reaches this */
62605   int nSub = 0;                        /* Number of sub-vdbes seen so far */
62606   SubProgram **apSub = 0;              /* Array of sub-vdbes */
62607   Mem *pSub = 0;                       /* Memory cell hold array of subprogs */
62608   sqlite3 *db = p->db;                 /* The database connection */
62609   int i;                               /* Loop counter */
62610   int rc = SQLITE_OK;                  /* Return code */
62611   Mem *pMem = &p->aMem[1];             /* First Mem of result set */
62612 
62613   assert( p->explain );
62614   assert( p->magic==VDBE_MAGIC_RUN );
62615   assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY || p->rc==SQLITE_NOMEM );
62616 
62617   /* Even though this opcode does not use dynamic strings for
62618   ** the result, result columns may become dynamic if the user calls
62619   ** sqlite3_column_text16(), causing a translation to UTF-16 encoding.
62620   */
62621   releaseMemArray(pMem, 8);
62622   p->pResultSet = 0;
62623 
62624   if( p->rc==SQLITE_NOMEM ){
62625     /* This happens if a malloc() inside a call to sqlite3_column_text() or
62626     ** sqlite3_column_text16() failed.  */
62627     db->mallocFailed = 1;
62628     return SQLITE_ERROR;
62629   }
62630 
62631   /* When the number of output rows reaches nRow, that means the
62632   ** listing has finished and sqlite3_step() should return SQLITE_DONE.
62633   ** nRow is the sum of the number of rows in the main program, plus
62634   ** the sum of the number of rows in all trigger subprograms encountered
62635   ** so far.  The nRow value will increase as new trigger subprograms are
62636   ** encountered, but p->pc will eventually catch up to nRow.
62637   */
62638   nRow = p->nOp;
62639   if( p->explain==1 ){
62640     /* The first 8 memory cells are used for the result set.  So we will
62641     ** commandeer the 9th cell to use as storage for an array of pointers
62642     ** to trigger subprograms.  The VDBE is guaranteed to have at least 9
62643     ** cells.  */
62644     assert( p->nMem>9 );
62645     pSub = &p->aMem[9];
62646     if( pSub->flags&MEM_Blob ){
62647       /* On the first call to sqlite3_step(), pSub will hold a NULL.  It is
62648       ** initialized to a BLOB by the P4_SUBPROGRAM processing logic below */
62649       nSub = pSub->n/sizeof(Vdbe*);
62650       apSub = (SubProgram **)pSub->z;
62651     }
62652     for(i=0; i<nSub; i++){
62653       nRow += apSub[i]->nOp;
62654     }
62655   }
62656 
62657   do{
62658     i = p->pc++;
62659   }while( i<nRow && p->explain==2 && p->aOp[i].opcode!=OP_Explain );
62660   if( i>=nRow ){
62661     p->rc = SQLITE_OK;
62662     rc = SQLITE_DONE;
62663   }else if( db->u1.isInterrupted ){
62664     p->rc = SQLITE_INTERRUPT;
62665     rc = SQLITE_ERROR;
62666     sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(p->rc));
62667   }else{
62668     char *zP4;
62669     Op *pOp;
62670     if( i<p->nOp ){
62671       /* The output line number is small enough that we are still in the
62672       ** main program. */
62673       pOp = &p->aOp[i];
62674     }else{
62675       /* We are currently listing subprograms.  Figure out which one and
62676       ** pick up the appropriate opcode. */
62677       int j;
62678       i -= p->nOp;
62679       for(j=0; i>=apSub[j]->nOp; j++){
62680         i -= apSub[j]->nOp;
62681       }
62682       pOp = &apSub[j]->aOp[i];
62683     }
62684     if( p->explain==1 ){
62685       pMem->flags = MEM_Int;
62686       pMem->type = SQLITE_INTEGER;
62687       pMem->u.i = i;                                /* Program counter */
62688       pMem++;
62689 
62690       pMem->flags = MEM_Static|MEM_Str|MEM_Term;
62691       pMem->z = (char*)sqlite3OpcodeName(pOp->opcode); /* Opcode */
62692       assert( pMem->z!=0 );
62693       pMem->n = sqlite3Strlen30(pMem->z);
62694       pMem->type = SQLITE_TEXT;
62695       pMem->enc = SQLITE_UTF8;
62696       pMem++;
62697 
62698       /* When an OP_Program opcode is encounter (the only opcode that has
62699       ** a P4_SUBPROGRAM argument), expand the size of the array of subprograms
62700       ** kept in p->aMem[9].z to hold the new program - assuming this subprogram
62701       ** has not already been seen.
62702       */
62703       if( pOp->p4type==P4_SUBPROGRAM ){
62704         int nByte = (nSub+1)*sizeof(SubProgram*);
62705         int j;
62706         for(j=0; j<nSub; j++){
62707           if( apSub[j]==pOp->p4.pProgram ) break;
62708         }
62709         if( j==nSub && SQLITE_OK==sqlite3VdbeMemGrow(pSub, nByte, nSub!=0) ){
62710           apSub = (SubProgram **)pSub->z;
62711           apSub[nSub++] = pOp->p4.pProgram;
62712           pSub->flags |= MEM_Blob;
62713           pSub->n = nSub*sizeof(SubProgram*);
62714         }
62715       }
62716     }
62717 
62718     pMem->flags = MEM_Int;
62719     pMem->u.i = pOp->p1;                          /* P1 */
62720     pMem->type = SQLITE_INTEGER;
62721     pMem++;
62722 
62723     pMem->flags = MEM_Int;
62724     pMem->u.i = pOp->p2;                          /* P2 */
62725     pMem->type = SQLITE_INTEGER;
62726     pMem++;
62727 
62728     pMem->flags = MEM_Int;
62729     pMem->u.i = pOp->p3;                          /* P3 */
62730     pMem->type = SQLITE_INTEGER;
62731     pMem++;
62732 
62733     if( sqlite3VdbeMemGrow(pMem, 32, 0) ){            /* P4 */
62734       assert( p->db->mallocFailed );
62735       return SQLITE_ERROR;
62736     }
62737     pMem->flags = MEM_Dyn|MEM_Str|MEM_Term;
62738     zP4 = displayP4(pOp, pMem->z, 32);
62739     if( zP4!=pMem->z ){
62740       sqlite3VdbeMemSetStr(pMem, zP4, -1, SQLITE_UTF8, 0);
62741     }else{
62742       assert( pMem->z!=0 );
62743       pMem->n = sqlite3Strlen30(pMem->z);
62744       pMem->enc = SQLITE_UTF8;
62745     }
62746     pMem->type = SQLITE_TEXT;
62747     pMem++;
62748 
62749     if( p->explain==1 ){
62750       if( sqlite3VdbeMemGrow(pMem, 4, 0) ){
62751         assert( p->db->mallocFailed );
62752         return SQLITE_ERROR;
62753       }
62754       pMem->flags = MEM_Dyn|MEM_Str|MEM_Term;
62755       pMem->n = 2;
62756       sqlite3_snprintf(3, pMem->z, "%.2x", pOp->p5);   /* P5 */
62757       pMem->type = SQLITE_TEXT;
62758       pMem->enc = SQLITE_UTF8;
62759       pMem++;
62760 
62761 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
62762       if( sqlite3VdbeMemGrow(pMem, 500, 0) ){
62763         assert( p->db->mallocFailed );
62764         return SQLITE_ERROR;
62765       }
62766       pMem->flags = MEM_Dyn|MEM_Str|MEM_Term;
62767       pMem->n = displayComment(pOp, zP4, pMem->z, 500);
62768       pMem->type = SQLITE_TEXT;
62769       pMem->enc = SQLITE_UTF8;
62770 #else
62771       pMem->flags = MEM_Null;                       /* Comment */
62772       pMem->type = SQLITE_NULL;
62773 #endif
62774     }
62775 
62776     p->nResColumn = 8 - 4*(p->explain-1);
62777     p->pResultSet = &p->aMem[1];
62778     p->rc = SQLITE_OK;
62779     rc = SQLITE_ROW;
62780   }
62781   return rc;
62782 }
62783 #endif /* SQLITE_OMIT_EXPLAIN */
62784 
62785 #ifdef SQLITE_DEBUG
62786 /*
62787 ** Print the SQL that was used to generate a VDBE program.
62788 */
62789 SQLITE_PRIVATE void sqlite3VdbePrintSql(Vdbe *p){
62790   const char *z = 0;
62791   if( p->zSql ){
62792     z = p->zSql;
62793   }else if( p->nOp>=1 ){
62794     const VdbeOp *pOp = &p->aOp[0];
62795     if( pOp->opcode==OP_Trace && pOp->p4.z!=0 ){
62796       z = pOp->p4.z;
62797       while( sqlite3Isspace(*z) ) z++;
62798     }
62799   }
62800   if( z ) printf("SQL: [%s]\n", z);
62801 }
62802 #endif
62803 
62804 #if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE)
62805 /*
62806 ** Print an IOTRACE message showing SQL content.
62807 */
62808 SQLITE_PRIVATE void sqlite3VdbeIOTraceSql(Vdbe *p){
62809   int nOp = p->nOp;
62810   VdbeOp *pOp;
62811   if( sqlite3IoTrace==0 ) return;
62812   if( nOp<1 ) return;
62813   pOp = &p->aOp[0];
62814   if( pOp->opcode==OP_Trace && pOp->p4.z!=0 ){
62815     int i, j;
62816     char z[1000];
62817     sqlite3_snprintf(sizeof(z), z, "%s", pOp->p4.z);
62818     for(i=0; sqlite3Isspace(z[i]); i++){}
62819     for(j=0; z[i]; i++){
62820       if( sqlite3Isspace(z[i]) ){
62821         if( z[i-1]!=' ' ){
62822           z[j++] = ' ';
62823         }
62824       }else{
62825         z[j++] = z[i];
62826       }
62827     }
62828     z[j] = 0;
62829     sqlite3IoTrace("SQL %s\n", z);
62830   }
62831 }
62832 #endif /* !SQLITE_OMIT_TRACE && SQLITE_ENABLE_IOTRACE */
62833 
62834 /*
62835 ** Allocate space from a fixed size buffer and return a pointer to
62836 ** that space.  If insufficient space is available, return NULL.
62837 **
62838 ** The pBuf parameter is the initial value of a pointer which will
62839 ** receive the new memory.  pBuf is normally NULL.  If pBuf is not
62840 ** NULL, it means that memory space has already been allocated and that
62841 ** this routine should not allocate any new memory.  When pBuf is not
62842 ** NULL simply return pBuf.  Only allocate new memory space when pBuf
62843 ** is NULL.
62844 **
62845 ** nByte is the number of bytes of space needed.
62846 **
62847 ** *ppFrom points to available space and pEnd points to the end of the
62848 ** available space.  When space is allocated, *ppFrom is advanced past
62849 ** the end of the allocated space.
62850 **
62851 ** *pnByte is a counter of the number of bytes of space that have failed
62852 ** to allocate.  If there is insufficient space in *ppFrom to satisfy the
62853 ** request, then increment *pnByte by the amount of the request.
62854 */
62855 static void *allocSpace(
62856   void *pBuf,          /* Where return pointer will be stored */
62857   int nByte,           /* Number of bytes to allocate */
62858   u8 **ppFrom,         /* IN/OUT: Allocate from *ppFrom */
62859   u8 *pEnd,            /* Pointer to 1 byte past the end of *ppFrom buffer */
62860   int *pnByte          /* If allocation cannot be made, increment *pnByte */
62861 ){
62862   assert( EIGHT_BYTE_ALIGNMENT(*ppFrom) );
62863   if( pBuf ) return pBuf;
62864   nByte = ROUND8(nByte);
62865   if( &(*ppFrom)[nByte] <= pEnd ){
62866     pBuf = (void*)*ppFrom;
62867     *ppFrom += nByte;
62868   }else{
62869     *pnByte += nByte;
62870   }
62871   return pBuf;
62872 }
62873 
62874 /*
62875 ** Rewind the VDBE back to the beginning in preparation for
62876 ** running it.
62877 */
62878 SQLITE_PRIVATE void sqlite3VdbeRewind(Vdbe *p){
62879 #if defined(SQLITE_DEBUG) || defined(VDBE_PROFILE)
62880   int i;
62881 #endif
62882   assert( p!=0 );
62883   assert( p->magic==VDBE_MAGIC_INIT );
62884 
62885   /* There should be at least one opcode.
62886   */
62887   assert( p->nOp>0 );
62888 
62889   /* Set the magic to VDBE_MAGIC_RUN sooner rather than later. */
62890   p->magic = VDBE_MAGIC_RUN;
62891 
62892 #ifdef SQLITE_DEBUG
62893   for(i=1; i<p->nMem; i++){
62894     assert( p->aMem[i].db==p->db );
62895   }
62896 #endif
62897   p->pc = -1;
62898   p->rc = SQLITE_OK;
62899   p->errorAction = OE_Abort;
62900   p->magic = VDBE_MAGIC_RUN;
62901   p->nChange = 0;
62902   p->cacheCtr = 1;
62903   p->minWriteFileFormat = 255;
62904   p->iStatement = 0;
62905   p->nFkConstraint = 0;
62906 #ifdef VDBE_PROFILE
62907   for(i=0; i<p->nOp; i++){
62908     p->aOp[i].cnt = 0;
62909     p->aOp[i].cycles = 0;
62910   }
62911 #endif
62912 }
62913 
62914 /*
62915 ** Prepare a virtual machine for execution for the first time after
62916 ** creating the virtual machine.  This involves things such
62917 ** as allocating stack space and initializing the program counter.
62918 ** After the VDBE has be prepped, it can be executed by one or more
62919 ** calls to sqlite3VdbeExec().
62920 **
62921 ** This function may be called exact once on a each virtual machine.
62922 ** After this routine is called the VM has been "packaged" and is ready
62923 ** to run.  After this routine is called, futher calls to
62924 ** sqlite3VdbeAddOp() functions are prohibited.  This routine disconnects
62925 ** the Vdbe from the Parse object that helped generate it so that the
62926 ** the Vdbe becomes an independent entity and the Parse object can be
62927 ** destroyed.
62928 **
62929 ** Use the sqlite3VdbeRewind() procedure to restore a virtual machine back
62930 ** to its initial state after it has been run.
62931 */
62932 SQLITE_PRIVATE void sqlite3VdbeMakeReady(
62933   Vdbe *p,                       /* The VDBE */
62934   Parse *pParse                  /* Parsing context */
62935 ){
62936   sqlite3 *db;                   /* The database connection */
62937   int nVar;                      /* Number of parameters */
62938   int nMem;                      /* Number of VM memory registers */
62939   int nCursor;                   /* Number of cursors required */
62940   int nArg;                      /* Number of arguments in subprograms */
62941   int nOnce;                     /* Number of OP_Once instructions */
62942   int n;                         /* Loop counter */
62943   u8 *zCsr;                      /* Memory available for allocation */
62944   u8 *zEnd;                      /* First byte past allocated memory */
62945   int nByte;                     /* How much extra memory is needed */
62946 
62947   assert( p!=0 );
62948   assert( p->nOp>0 );
62949   assert( pParse!=0 );
62950   assert( p->magic==VDBE_MAGIC_INIT );
62951   assert( pParse==p->pParse );
62952   db = p->db;
62953   assert( db->mallocFailed==0 );
62954   nVar = pParse->nVar;
62955   nMem = pParse->nMem;
62956   nCursor = pParse->nTab;
62957   nArg = pParse->nMaxArg;
62958   nOnce = pParse->nOnce;
62959   if( nOnce==0 ) nOnce = 1; /* Ensure at least one byte in p->aOnceFlag[] */
62960 
62961   /* For each cursor required, also allocate a memory cell. Memory
62962   ** cells (nMem+1-nCursor)..nMem, inclusive, will never be used by
62963   ** the vdbe program. Instead they are used to allocate space for
62964   ** VdbeCursor/BtCursor structures. The blob of memory associated with
62965   ** cursor 0 is stored in memory cell nMem. Memory cell (nMem-1)
62966   ** stores the blob of memory associated with cursor 1, etc.
62967   **
62968   ** See also: allocateCursor().
62969   */
62970   nMem += nCursor;
62971 
62972   /* Allocate space for memory registers, SQL variables, VDBE cursors and
62973   ** an array to marshal SQL function arguments in.
62974   */
62975   zCsr = (u8*)&p->aOp[p->nOp];            /* Memory avaliable for allocation */
62976   zEnd = (u8*)&p->aOp[pParse->nOpAlloc];  /* First byte past end of zCsr[] */
62977 
62978   resolveP2Values(p, &nArg);
62979   p->usesStmtJournal = (u8)(pParse->isMultiWrite && pParse->mayAbort);
62980   if( pParse->explain && nMem<10 ){
62981     nMem = 10;
62982   }
62983   memset(zCsr, 0, zEnd-zCsr);
62984   zCsr += (zCsr - (u8*)0)&7;
62985   assert( EIGHT_BYTE_ALIGNMENT(zCsr) );
62986   p->expired = 0;
62987 
62988   /* Memory for registers, parameters, cursor, etc, is allocated in two
62989   ** passes.  On the first pass, we try to reuse unused space at the
62990   ** end of the opcode array.  If we are unable to satisfy all memory
62991   ** requirements by reusing the opcode array tail, then the second
62992   ** pass will fill in the rest using a fresh allocation.
62993   **
62994   ** This two-pass approach that reuses as much memory as possible from
62995   ** the leftover space at the end of the opcode array can significantly
62996   ** reduce the amount of memory held by a prepared statement.
62997   */
62998   do {
62999     nByte = 0;
63000     p->aMem = allocSpace(p->aMem, nMem*sizeof(Mem), &zCsr, zEnd, &nByte);
63001     p->aVar = allocSpace(p->aVar, nVar*sizeof(Mem), &zCsr, zEnd, &nByte);
63002     p->apArg = allocSpace(p->apArg, nArg*sizeof(Mem*), &zCsr, zEnd, &nByte);
63003     p->azVar = allocSpace(p->azVar, nVar*sizeof(char*), &zCsr, zEnd, &nByte);
63004     p->apCsr = allocSpace(p->apCsr, nCursor*sizeof(VdbeCursor*),
63005                           &zCsr, zEnd, &nByte);
63006     p->aOnceFlag = allocSpace(p->aOnceFlag, nOnce, &zCsr, zEnd, &nByte);
63007     if( nByte ){
63008       p->pFree = sqlite3DbMallocZero(db, nByte);
63009     }
63010     zCsr = p->pFree;
63011     zEnd = &zCsr[nByte];
63012   }while( nByte && !db->mallocFailed );
63013 
63014   p->nCursor = nCursor;
63015   p->nOnceFlag = nOnce;
63016   if( p->aVar ){
63017     p->nVar = (ynVar)nVar;
63018     for(n=0; n<nVar; n++){
63019       p->aVar[n].flags = MEM_Null;
63020       p->aVar[n].db = db;
63021     }
63022   }
63023   if( p->azVar ){
63024     p->nzVar = pParse->nzVar;
63025     memcpy(p->azVar, pParse->azVar, p->nzVar*sizeof(p->azVar[0]));
63026     memset(pParse->azVar, 0, pParse->nzVar*sizeof(pParse->azVar[0]));
63027   }
63028   if( p->aMem ){
63029     p->aMem--;                      /* aMem[] goes from 1..nMem */
63030     p->nMem = nMem;                 /*       not from 0..nMem-1 */
63031     for(n=1; n<=nMem; n++){
63032       p->aMem[n].flags = MEM_Invalid;
63033       p->aMem[n].db = db;
63034     }
63035   }
63036   p->explain = pParse->explain;
63037   sqlite3VdbeRewind(p);
63038 }
63039 
63040 /*
63041 ** Close a VDBE cursor and release all the resources that cursor
63042 ** happens to hold.
63043 */
63044 SQLITE_PRIVATE void sqlite3VdbeFreeCursor(Vdbe *p, VdbeCursor *pCx){
63045   if( pCx==0 ){
63046     return;
63047   }
63048   sqlite3VdbeSorterClose(p->db, pCx);
63049   if( pCx->pBt ){
63050     sqlite3BtreeClose(pCx->pBt);
63051     /* The pCx->pCursor will be close automatically, if it exists, by
63052     ** the call above. */
63053   }else if( pCx->pCursor ){
63054     sqlite3BtreeCloseCursor(pCx->pCursor);
63055   }
63056 #ifndef SQLITE_OMIT_VIRTUALTABLE
63057   if( pCx->pVtabCursor ){
63058     sqlite3_vtab_cursor *pVtabCursor = pCx->pVtabCursor;
63059     const sqlite3_module *pModule = pVtabCursor->pVtab->pModule;
63060     p->inVtabMethod = 1;
63061     pModule->xClose(pVtabCursor);
63062     p->inVtabMethod = 0;
63063   }
63064 #endif
63065 }
63066 
63067 /*
63068 ** Copy the values stored in the VdbeFrame structure to its Vdbe. This
63069 ** is used, for example, when a trigger sub-program is halted to restore
63070 ** control to the main program.
63071 */
63072 SQLITE_PRIVATE int sqlite3VdbeFrameRestore(VdbeFrame *pFrame){
63073   Vdbe *v = pFrame->v;
63074   v->aOnceFlag = pFrame->aOnceFlag;
63075   v->nOnceFlag = pFrame->nOnceFlag;
63076   v->aOp = pFrame->aOp;
63077   v->nOp = pFrame->nOp;
63078   v->aMem = pFrame->aMem;
63079   v->nMem = pFrame->nMem;
63080   v->apCsr = pFrame->apCsr;
63081   v->nCursor = pFrame->nCursor;
63082   v->db->lastRowid = pFrame->lastRowid;
63083   v->nChange = pFrame->nChange;
63084   return pFrame->pc;
63085 }
63086 
63087 /*
63088 ** Close all cursors.
63089 **
63090 ** Also release any dynamic memory held by the VM in the Vdbe.aMem memory
63091 ** cell array. This is necessary as the memory cell array may contain
63092 ** pointers to VdbeFrame objects, which may in turn contain pointers to
63093 ** open cursors.
63094 */
63095 static void closeAllCursors(Vdbe *p){
63096   if( p->pFrame ){
63097     VdbeFrame *pFrame;
63098     for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
63099     sqlite3VdbeFrameRestore(pFrame);
63100   }
63101   p->pFrame = 0;
63102   p->nFrame = 0;
63103 
63104   if( p->apCsr ){
63105     int i;
63106     for(i=0; i<p->nCursor; i++){
63107       VdbeCursor *pC = p->apCsr[i];
63108       if( pC ){
63109         sqlite3VdbeFreeCursor(p, pC);
63110         p->apCsr[i] = 0;
63111       }
63112     }
63113   }
63114   if( p->aMem ){
63115     releaseMemArray(&p->aMem[1], p->nMem);
63116   }
63117   while( p->pDelFrame ){
63118     VdbeFrame *pDel = p->pDelFrame;
63119     p->pDelFrame = pDel->pParent;
63120     sqlite3VdbeFrameDelete(pDel);
63121   }
63122 
63123   /* Delete any auxdata allocations made by the VM */
63124   sqlite3VdbeDeleteAuxData(p, -1, 0);
63125   assert( p->pAuxData==0 );
63126 }
63127 
63128 /*
63129 ** Clean up the VM after execution.
63130 **
63131 ** This routine will automatically close any cursors, lists, and/or
63132 ** sorters that were left open.  It also deletes the values of
63133 ** variables in the aVar[] array.
63134 */
63135 static void Cleanup(Vdbe *p){
63136   sqlite3 *db = p->db;
63137 
63138 #ifdef SQLITE_DEBUG
63139   /* Execute assert() statements to ensure that the Vdbe.apCsr[] and
63140   ** Vdbe.aMem[] arrays have already been cleaned up.  */
63141   int i;
63142   if( p->apCsr ) for(i=0; i<p->nCursor; i++) assert( p->apCsr[i]==0 );
63143   if( p->aMem ){
63144     for(i=1; i<=p->nMem; i++) assert( p->aMem[i].flags==MEM_Invalid );
63145   }
63146 #endif
63147 
63148   sqlite3DbFree(db, p->zErrMsg);
63149   p->zErrMsg = 0;
63150   p->pResultSet = 0;
63151 }
63152 
63153 /*
63154 ** Set the number of result columns that will be returned by this SQL
63155 ** statement. This is now set at compile time, rather than during
63156 ** execution of the vdbe program so that sqlite3_column_count() can
63157 ** be called on an SQL statement before sqlite3_step().
63158 */
63159 SQLITE_PRIVATE void sqlite3VdbeSetNumCols(Vdbe *p, int nResColumn){
63160   Mem *pColName;
63161   int n;
63162   sqlite3 *db = p->db;
63163 
63164   releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
63165   sqlite3DbFree(db, p->aColName);
63166   n = nResColumn*COLNAME_N;
63167   p->nResColumn = (u16)nResColumn;
63168   p->aColName = pColName = (Mem*)sqlite3DbMallocZero(db, sizeof(Mem)*n );
63169   if( p->aColName==0 ) return;
63170   while( n-- > 0 ){
63171     pColName->flags = MEM_Null;
63172     pColName->db = p->db;
63173     pColName++;
63174   }
63175 }
63176 
63177 /*
63178 ** Set the name of the idx'th column to be returned by the SQL statement.
63179 ** zName must be a pointer to a nul terminated string.
63180 **
63181 ** This call must be made after a call to sqlite3VdbeSetNumCols().
63182 **
63183 ** The final parameter, xDel, must be one of SQLITE_DYNAMIC, SQLITE_STATIC
63184 ** or SQLITE_TRANSIENT. If it is SQLITE_DYNAMIC, then the buffer pointed
63185 ** to by zName will be freed by sqlite3DbFree() when the vdbe is destroyed.
63186 */
63187 SQLITE_PRIVATE int sqlite3VdbeSetColName(
63188   Vdbe *p,                         /* Vdbe being configured */
63189   int idx,                         /* Index of column zName applies to */
63190   int var,                         /* One of the COLNAME_* constants */
63191   const char *zName,               /* Pointer to buffer containing name */
63192   void (*xDel)(void*)              /* Memory management strategy for zName */
63193 ){
63194   int rc;
63195   Mem *pColName;
63196   assert( idx<p->nResColumn );
63197   assert( var<COLNAME_N );
63198   if( p->db->mallocFailed ){
63199     assert( !zName || xDel!=SQLITE_DYNAMIC );
63200     return SQLITE_NOMEM;
63201   }
63202   assert( p->aColName!=0 );
63203   pColName = &(p->aColName[idx+var*p->nResColumn]);
63204   rc = sqlite3VdbeMemSetStr(pColName, zName, -1, SQLITE_UTF8, xDel);
63205   assert( rc!=0 || !zName || (pColName->flags&MEM_Term)!=0 );
63206   return rc;
63207 }
63208 
63209 /*
63210 ** A read or write transaction may or may not be active on database handle
63211 ** db. If a transaction is active, commit it. If there is a
63212 ** write-transaction spanning more than one database file, this routine
63213 ** takes care of the master journal trickery.
63214 */
63215 static int vdbeCommit(sqlite3 *db, Vdbe *p){
63216   int i;
63217   int nTrans = 0;  /* Number of databases with an active write-transaction */
63218   int rc = SQLITE_OK;
63219   int needXcommit = 0;
63220 
63221 #ifdef SQLITE_OMIT_VIRTUALTABLE
63222   /* With this option, sqlite3VtabSync() is defined to be simply
63223   ** SQLITE_OK so p is not used.
63224   */
63225   UNUSED_PARAMETER(p);
63226 #endif
63227 
63228   /* Before doing anything else, call the xSync() callback for any
63229   ** virtual module tables written in this transaction. This has to
63230   ** be done before determining whether a master journal file is
63231   ** required, as an xSync() callback may add an attached database
63232   ** to the transaction.
63233   */
63234   rc = sqlite3VtabSync(db, p);
63235 
63236   /* This loop determines (a) if the commit hook should be invoked and
63237   ** (b) how many database files have open write transactions, not
63238   ** including the temp database. (b) is important because if more than
63239   ** one database file has an open write transaction, a master journal
63240   ** file is required for an atomic commit.
63241   */
63242   for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
63243     Btree *pBt = db->aDb[i].pBt;
63244     if( sqlite3BtreeIsInTrans(pBt) ){
63245       needXcommit = 1;
63246       if( i!=1 ) nTrans++;
63247       sqlite3BtreeEnter(pBt);
63248       rc = sqlite3PagerExclusiveLock(sqlite3BtreePager(pBt));
63249       sqlite3BtreeLeave(pBt);
63250     }
63251   }
63252   if( rc!=SQLITE_OK ){
63253     return rc;
63254   }
63255 
63256   /* If there are any write-transactions at all, invoke the commit hook */
63257   if( needXcommit && db->xCommitCallback ){
63258     rc = db->xCommitCallback(db->pCommitArg);
63259     if( rc ){
63260       return SQLITE_CONSTRAINT_COMMITHOOK;
63261     }
63262   }
63263 
63264   /* The simple case - no more than one database file (not counting the
63265   ** TEMP database) has a transaction active.   There is no need for the
63266   ** master-journal.
63267   **
63268   ** If the return value of sqlite3BtreeGetFilename() is a zero length
63269   ** string, it means the main database is :memory: or a temp file.  In
63270   ** that case we do not support atomic multi-file commits, so use the
63271   ** simple case then too.
63272   */
63273   if( 0==sqlite3Strlen30(sqlite3BtreeGetFilename(db->aDb[0].pBt))
63274    || nTrans<=1
63275   ){
63276     for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
63277       Btree *pBt = db->aDb[i].pBt;
63278       if( pBt ){
63279         rc = sqlite3BtreeCommitPhaseOne(pBt, 0);
63280       }
63281     }
63282 
63283     /* Do the commit only if all databases successfully complete phase 1.
63284     ** If one of the BtreeCommitPhaseOne() calls fails, this indicates an
63285     ** IO error while deleting or truncating a journal file. It is unlikely,
63286     ** but could happen. In this case abandon processing and return the error.
63287     */
63288     for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
63289       Btree *pBt = db->aDb[i].pBt;
63290       if( pBt ){
63291         rc = sqlite3BtreeCommitPhaseTwo(pBt, 0);
63292       }
63293     }
63294     if( rc==SQLITE_OK ){
63295       sqlite3VtabCommit(db);
63296     }
63297   }
63298 
63299   /* The complex case - There is a multi-file write-transaction active.
63300   ** This requires a master journal file to ensure the transaction is
63301   ** committed atomicly.
63302   */
63303 #ifndef SQLITE_OMIT_DISKIO
63304   else{
63305     sqlite3_vfs *pVfs = db->pVfs;
63306     int needSync = 0;
63307     char *zMaster = 0;   /* File-name for the master journal */
63308     char const *zMainFile = sqlite3BtreeGetFilename(db->aDb[0].pBt);
63309     sqlite3_file *pMaster = 0;
63310     i64 offset = 0;
63311     int res;
63312     int retryCount = 0;
63313     int nMainFile;
63314 
63315     /* Select a master journal file name */
63316     nMainFile = sqlite3Strlen30(zMainFile);
63317     zMaster = sqlite3MPrintf(db, "%s-mjXXXXXX9XXz", zMainFile);
63318     if( zMaster==0 ) return SQLITE_NOMEM;
63319     do {
63320       u32 iRandom;
63321       if( retryCount ){
63322         if( retryCount>100 ){
63323           sqlite3_log(SQLITE_FULL, "MJ delete: %s", zMaster);
63324           sqlite3OsDelete(pVfs, zMaster, 0);
63325           break;
63326         }else if( retryCount==1 ){
63327           sqlite3_log(SQLITE_FULL, "MJ collide: %s", zMaster);
63328         }
63329       }
63330       retryCount++;
63331       sqlite3_randomness(sizeof(iRandom), &iRandom);
63332       sqlite3_snprintf(13, &zMaster[nMainFile], "-mj%06X9%02X",
63333                                (iRandom>>8)&0xffffff, iRandom&0xff);
63334       /* The antipenultimate character of the master journal name must
63335       ** be "9" to avoid name collisions when using 8+3 filenames. */
63336       assert( zMaster[sqlite3Strlen30(zMaster)-3]=='9' );
63337       sqlite3FileSuffix3(zMainFile, zMaster);
63338       rc = sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS, &res);
63339     }while( rc==SQLITE_OK && res );
63340     if( rc==SQLITE_OK ){
63341       /* Open the master journal. */
63342       rc = sqlite3OsOpenMalloc(pVfs, zMaster, &pMaster,
63343           SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|
63344           SQLITE_OPEN_EXCLUSIVE|SQLITE_OPEN_MASTER_JOURNAL, 0
63345       );
63346     }
63347     if( rc!=SQLITE_OK ){
63348       sqlite3DbFree(db, zMaster);
63349       return rc;
63350     }
63351 
63352     /* Write the name of each database file in the transaction into the new
63353     ** master journal file. If an error occurs at this point close
63354     ** and delete the master journal file. All the individual journal files
63355     ** still have 'null' as the master journal pointer, so they will roll
63356     ** back independently if a failure occurs.
63357     */
63358     for(i=0; i<db->nDb; i++){
63359       Btree *pBt = db->aDb[i].pBt;
63360       if( sqlite3BtreeIsInTrans(pBt) ){
63361         char const *zFile = sqlite3BtreeGetJournalname(pBt);
63362         if( zFile==0 ){
63363           continue;  /* Ignore TEMP and :memory: databases */
63364         }
63365         assert( zFile[0]!=0 );
63366         if( !needSync && !sqlite3BtreeSyncDisabled(pBt) ){
63367           needSync = 1;
63368         }
63369         rc = sqlite3OsWrite(pMaster, zFile, sqlite3Strlen30(zFile)+1, offset);
63370         offset += sqlite3Strlen30(zFile)+1;
63371         if( rc!=SQLITE_OK ){
63372           sqlite3OsCloseFree(pMaster);
63373           sqlite3OsDelete(pVfs, zMaster, 0);
63374           sqlite3DbFree(db, zMaster);
63375           return rc;
63376         }
63377       }
63378     }
63379 
63380     /* Sync the master journal file. If the IOCAP_SEQUENTIAL device
63381     ** flag is set this is not required.
63382     */
63383     if( needSync
63384      && 0==(sqlite3OsDeviceCharacteristics(pMaster)&SQLITE_IOCAP_SEQUENTIAL)
63385      && SQLITE_OK!=(rc = sqlite3OsSync(pMaster, SQLITE_SYNC_NORMAL))
63386     ){
63387       sqlite3OsCloseFree(pMaster);
63388       sqlite3OsDelete(pVfs, zMaster, 0);
63389       sqlite3DbFree(db, zMaster);
63390       return rc;
63391     }
63392 
63393     /* Sync all the db files involved in the transaction. The same call
63394     ** sets the master journal pointer in each individual journal. If
63395     ** an error occurs here, do not delete the master journal file.
63396     **
63397     ** If the error occurs during the first call to
63398     ** sqlite3BtreeCommitPhaseOne(), then there is a chance that the
63399     ** master journal file will be orphaned. But we cannot delete it,
63400     ** in case the master journal file name was written into the journal
63401     ** file before the failure occurred.
63402     */
63403     for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
63404       Btree *pBt = db->aDb[i].pBt;
63405       if( pBt ){
63406         rc = sqlite3BtreeCommitPhaseOne(pBt, zMaster);
63407       }
63408     }
63409     sqlite3OsCloseFree(pMaster);
63410     assert( rc!=SQLITE_BUSY );
63411     if( rc!=SQLITE_OK ){
63412       sqlite3DbFree(db, zMaster);
63413       return rc;
63414     }
63415 
63416     /* Delete the master journal file. This commits the transaction. After
63417     ** doing this the directory is synced again before any individual
63418     ** transaction files are deleted.
63419     */
63420     rc = sqlite3OsDelete(pVfs, zMaster, 1);
63421     sqlite3DbFree(db, zMaster);
63422     zMaster = 0;
63423     if( rc ){
63424       return rc;
63425     }
63426 
63427     /* All files and directories have already been synced, so the following
63428     ** calls to sqlite3BtreeCommitPhaseTwo() are only closing files and
63429     ** deleting or truncating journals. If something goes wrong while
63430     ** this is happening we don't really care. The integrity of the
63431     ** transaction is already guaranteed, but some stray 'cold' journals
63432     ** may be lying around. Returning an error code won't help matters.
63433     */
63434     disable_simulated_io_errors();
63435     sqlite3BeginBenignMalloc();
63436     for(i=0; i<db->nDb; i++){
63437       Btree *pBt = db->aDb[i].pBt;
63438       if( pBt ){
63439         sqlite3BtreeCommitPhaseTwo(pBt, 1);
63440       }
63441     }
63442     sqlite3EndBenignMalloc();
63443     enable_simulated_io_errors();
63444 
63445     sqlite3VtabCommit(db);
63446   }
63447 #endif
63448 
63449   return rc;
63450 }
63451 
63452 /*
63453 ** This routine checks that the sqlite3.nVdbeActive count variable
63454 ** matches the number of vdbe's in the list sqlite3.pVdbe that are
63455 ** currently active. An assertion fails if the two counts do not match.
63456 ** This is an internal self-check only - it is not an essential processing
63457 ** step.
63458 **
63459 ** This is a no-op if NDEBUG is defined.
63460 */
63461 #ifndef NDEBUG
63462 static void checkActiveVdbeCnt(sqlite3 *db){
63463   Vdbe *p;
63464   int cnt = 0;
63465   int nWrite = 0;
63466   int nRead = 0;
63467   p = db->pVdbe;
63468   while( p ){
63469     if( p->magic==VDBE_MAGIC_RUN && p->pc>=0 ){
63470       cnt++;
63471       if( p->readOnly==0 ) nWrite++;
63472       if( p->bIsReader ) nRead++;
63473     }
63474     p = p->pNext;
63475   }
63476   assert( cnt==db->nVdbeActive );
63477   assert( nWrite==db->nVdbeWrite );
63478   assert( nRead==db->nVdbeRead );
63479 }
63480 #else
63481 #define checkActiveVdbeCnt(x)
63482 #endif
63483 
63484 /*
63485 ** If the Vdbe passed as the first argument opened a statement-transaction,
63486 ** close it now. Argument eOp must be either SAVEPOINT_ROLLBACK or
63487 ** SAVEPOINT_RELEASE. If it is SAVEPOINT_ROLLBACK, then the statement
63488 ** transaction is rolled back. If eOp is SAVEPOINT_RELEASE, then the
63489 ** statement transaction is committed.
63490 **
63491 ** If an IO error occurs, an SQLITE_IOERR_XXX error code is returned.
63492 ** Otherwise SQLITE_OK.
63493 */
63494 SQLITE_PRIVATE int sqlite3VdbeCloseStatement(Vdbe *p, int eOp){
63495   sqlite3 *const db = p->db;
63496   int rc = SQLITE_OK;
63497 
63498   /* If p->iStatement is greater than zero, then this Vdbe opened a
63499   ** statement transaction that should be closed here. The only exception
63500   ** is that an IO error may have occurred, causing an emergency rollback.
63501   ** In this case (db->nStatement==0), and there is nothing to do.
63502   */
63503   if( db->nStatement && p->iStatement ){
63504     int i;
63505     const int iSavepoint = p->iStatement-1;
63506 
63507     assert( eOp==SAVEPOINT_ROLLBACK || eOp==SAVEPOINT_RELEASE);
63508     assert( db->nStatement>0 );
63509     assert( p->iStatement==(db->nStatement+db->nSavepoint) );
63510 
63511     for(i=0; i<db->nDb; i++){
63512       int rc2 = SQLITE_OK;
63513       Btree *pBt = db->aDb[i].pBt;
63514       if( pBt ){
63515         if( eOp==SAVEPOINT_ROLLBACK ){
63516           rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_ROLLBACK, iSavepoint);
63517         }
63518         if( rc2==SQLITE_OK ){
63519           rc2 = sqlite3BtreeSavepoint(pBt, SAVEPOINT_RELEASE, iSavepoint);
63520         }
63521         if( rc==SQLITE_OK ){
63522           rc = rc2;
63523         }
63524       }
63525     }
63526     db->nStatement--;
63527     p->iStatement = 0;
63528 
63529     if( rc==SQLITE_OK ){
63530       if( eOp==SAVEPOINT_ROLLBACK ){
63531         rc = sqlite3VtabSavepoint(db, SAVEPOINT_ROLLBACK, iSavepoint);
63532       }
63533       if( rc==SQLITE_OK ){
63534         rc = sqlite3VtabSavepoint(db, SAVEPOINT_RELEASE, iSavepoint);
63535       }
63536     }
63537 
63538     /* If the statement transaction is being rolled back, also restore the
63539     ** database handles deferred constraint counter to the value it had when
63540     ** the statement transaction was opened.  */
63541     if( eOp==SAVEPOINT_ROLLBACK ){
63542       db->nDeferredCons = p->nStmtDefCons;
63543       db->nDeferredImmCons = p->nStmtDefImmCons;
63544     }
63545   }
63546   return rc;
63547 }
63548 
63549 /*
63550 ** This function is called when a transaction opened by the database
63551 ** handle associated with the VM passed as an argument is about to be
63552 ** committed. If there are outstanding deferred foreign key constraint
63553 ** violations, return SQLITE_ERROR. Otherwise, SQLITE_OK.
63554 **
63555 ** If there are outstanding FK violations and this function returns
63556 ** SQLITE_ERROR, set the result of the VM to SQLITE_CONSTRAINT_FOREIGNKEY
63557 ** and write an error message to it. Then return SQLITE_ERROR.
63558 */
63559 #ifndef SQLITE_OMIT_FOREIGN_KEY
63560 SQLITE_PRIVATE int sqlite3VdbeCheckFk(Vdbe *p, int deferred){
63561   sqlite3 *db = p->db;
63562   if( (deferred && (db->nDeferredCons+db->nDeferredImmCons)>0)
63563    || (!deferred && p->nFkConstraint>0)
63564   ){
63565     p->rc = SQLITE_CONSTRAINT_FOREIGNKEY;
63566     p->errorAction = OE_Abort;
63567     sqlite3SetString(&p->zErrMsg, db, "FOREIGN KEY constraint failed");
63568     return SQLITE_ERROR;
63569   }
63570   return SQLITE_OK;
63571 }
63572 #endif
63573 
63574 /*
63575 ** This routine is called the when a VDBE tries to halt.  If the VDBE
63576 ** has made changes and is in autocommit mode, then commit those
63577 ** changes.  If a rollback is needed, then do the rollback.
63578 **
63579 ** This routine is the only way to move the state of a VM from
63580 ** SQLITE_MAGIC_RUN to SQLITE_MAGIC_HALT.  It is harmless to
63581 ** call this on a VM that is in the SQLITE_MAGIC_HALT state.
63582 **
63583 ** Return an error code.  If the commit could not complete because of
63584 ** lock contention, return SQLITE_BUSY.  If SQLITE_BUSY is returned, it
63585 ** means the close did not happen and needs to be repeated.
63586 */
63587 SQLITE_PRIVATE int sqlite3VdbeHalt(Vdbe *p){
63588   int rc;                         /* Used to store transient return codes */
63589   sqlite3 *db = p->db;
63590 
63591   /* This function contains the logic that determines if a statement or
63592   ** transaction will be committed or rolled back as a result of the
63593   ** execution of this virtual machine.
63594   **
63595   ** If any of the following errors occur:
63596   **
63597   **     SQLITE_NOMEM
63598   **     SQLITE_IOERR
63599   **     SQLITE_FULL
63600   **     SQLITE_INTERRUPT
63601   **
63602   ** Then the internal cache might have been left in an inconsistent
63603   ** state.  We need to rollback the statement transaction, if there is
63604   ** one, or the complete transaction if there is no statement transaction.
63605   */
63606 
63607   if( p->db->mallocFailed ){
63608     p->rc = SQLITE_NOMEM;
63609   }
63610   if( p->aOnceFlag ) memset(p->aOnceFlag, 0, p->nOnceFlag);
63611   closeAllCursors(p);
63612   if( p->magic!=VDBE_MAGIC_RUN ){
63613     return SQLITE_OK;
63614   }
63615   checkActiveVdbeCnt(db);
63616 
63617   /* No commit or rollback needed if the program never started or if the
63618   ** SQL statement does not read or write a database file.  */
63619   if( p->pc>=0 && p->bIsReader ){
63620     int mrc;   /* Primary error code from p->rc */
63621     int eStatementOp = 0;
63622     int isSpecialError;            /* Set to true if a 'special' error */
63623 
63624     /* Lock all btrees used by the statement */
63625     sqlite3VdbeEnter(p);
63626 
63627     /* Check for one of the special errors */
63628     mrc = p->rc & 0xff;
63629     assert( p->rc!=SQLITE_IOERR_BLOCKED );  /* This error no longer exists */
63630     isSpecialError = mrc==SQLITE_NOMEM || mrc==SQLITE_IOERR
63631                      || mrc==SQLITE_INTERRUPT || mrc==SQLITE_FULL;
63632     if( isSpecialError ){
63633       /* If the query was read-only and the error code is SQLITE_INTERRUPT,
63634       ** no rollback is necessary. Otherwise, at least a savepoint
63635       ** transaction must be rolled back to restore the database to a
63636       ** consistent state.
63637       **
63638       ** Even if the statement is read-only, it is important to perform
63639       ** a statement or transaction rollback operation. If the error
63640       ** occurred while writing to the journal, sub-journal or database
63641       ** file as part of an effort to free up cache space (see function
63642       ** pagerStress() in pager.c), the rollback is required to restore
63643       ** the pager to a consistent state.
63644       */
63645       if( !p->readOnly || mrc!=SQLITE_INTERRUPT ){
63646         if( (mrc==SQLITE_NOMEM || mrc==SQLITE_FULL) && p->usesStmtJournal ){
63647           eStatementOp = SAVEPOINT_ROLLBACK;
63648         }else{
63649           /* We are forced to roll back the active transaction. Before doing
63650           ** so, abort any other statements this handle currently has active.
63651           */
63652           sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
63653           sqlite3CloseSavepoints(db);
63654           db->autoCommit = 1;
63655         }
63656       }
63657     }
63658 
63659     /* Check for immediate foreign key violations. */
63660     if( p->rc==SQLITE_OK ){
63661       sqlite3VdbeCheckFk(p, 0);
63662     }
63663 
63664     /* If the auto-commit flag is set and this is the only active writer
63665     ** VM, then we do either a commit or rollback of the current transaction.
63666     **
63667     ** Note: This block also runs if one of the special errors handled
63668     ** above has occurred.
63669     */
63670     if( !sqlite3VtabInSync(db)
63671      && db->autoCommit
63672      && db->nVdbeWrite==(p->readOnly==0)
63673     ){
63674       if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){
63675         rc = sqlite3VdbeCheckFk(p, 1);
63676         if( rc!=SQLITE_OK ){
63677           if( NEVER(p->readOnly) ){
63678             sqlite3VdbeLeave(p);
63679             return SQLITE_ERROR;
63680           }
63681           rc = SQLITE_CONSTRAINT_FOREIGNKEY;
63682         }else{
63683           /* The auto-commit flag is true, the vdbe program was successful
63684           ** or hit an 'OR FAIL' constraint and there are no deferred foreign
63685           ** key constraints to hold up the transaction. This means a commit
63686           ** is required. */
63687           rc = vdbeCommit(db, p);
63688         }
63689         if( rc==SQLITE_BUSY && p->readOnly ){
63690           sqlite3VdbeLeave(p);
63691           return SQLITE_BUSY;
63692         }else if( rc!=SQLITE_OK ){
63693           p->rc = rc;
63694           sqlite3RollbackAll(db, SQLITE_OK);
63695         }else{
63696           db->nDeferredCons = 0;
63697           db->nDeferredImmCons = 0;
63698           db->flags &= ~SQLITE_DeferFKs;
63699           sqlite3CommitInternalChanges(db);
63700         }
63701       }else{
63702         sqlite3RollbackAll(db, SQLITE_OK);
63703       }
63704       db->nStatement = 0;
63705     }else if( eStatementOp==0 ){
63706       if( p->rc==SQLITE_OK || p->errorAction==OE_Fail ){
63707         eStatementOp = SAVEPOINT_RELEASE;
63708       }else if( p->errorAction==OE_Abort ){
63709         eStatementOp = SAVEPOINT_ROLLBACK;
63710       }else{
63711         sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
63712         sqlite3CloseSavepoints(db);
63713         db->autoCommit = 1;
63714       }
63715     }
63716 
63717     /* If eStatementOp is non-zero, then a statement transaction needs to
63718     ** be committed or rolled back. Call sqlite3VdbeCloseStatement() to
63719     ** do so. If this operation returns an error, and the current statement
63720     ** error code is SQLITE_OK or SQLITE_CONSTRAINT, then promote the
63721     ** current statement error code.
63722     */
63723     if( eStatementOp ){
63724       rc = sqlite3VdbeCloseStatement(p, eStatementOp);
63725       if( rc ){
63726         if( p->rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT ){
63727           p->rc = rc;
63728           sqlite3DbFree(db, p->zErrMsg);
63729           p->zErrMsg = 0;
63730         }
63731         sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
63732         sqlite3CloseSavepoints(db);
63733         db->autoCommit = 1;
63734       }
63735     }
63736 
63737     /* If this was an INSERT, UPDATE or DELETE and no statement transaction
63738     ** has been rolled back, update the database connection change-counter.
63739     */
63740     if( p->changeCntOn ){
63741       if( eStatementOp!=SAVEPOINT_ROLLBACK ){
63742         sqlite3VdbeSetChanges(db, p->nChange);
63743       }else{
63744         sqlite3VdbeSetChanges(db, 0);
63745       }
63746       p->nChange = 0;
63747     }
63748 
63749     /* Release the locks */
63750     sqlite3VdbeLeave(p);
63751   }
63752 
63753   /* We have successfully halted and closed the VM.  Record this fact. */
63754   if( p->pc>=0 ){
63755     db->nVdbeActive--;
63756     if( !p->readOnly ) db->nVdbeWrite--;
63757     if( p->bIsReader ) db->nVdbeRead--;
63758     assert( db->nVdbeActive>=db->nVdbeRead );
63759     assert( db->nVdbeRead>=db->nVdbeWrite );
63760     assert( db->nVdbeWrite>=0 );
63761   }
63762   p->magic = VDBE_MAGIC_HALT;
63763   checkActiveVdbeCnt(db);
63764   if( p->db->mallocFailed ){
63765     p->rc = SQLITE_NOMEM;
63766   }
63767 
63768   /* If the auto-commit flag is set to true, then any locks that were held
63769   ** by connection db have now been released. Call sqlite3ConnectionUnlocked()
63770   ** to invoke any required unlock-notify callbacks.
63771   */
63772   if( db->autoCommit ){
63773     sqlite3ConnectionUnlocked(db);
63774   }
63775 
63776   assert( db->nVdbeActive>0 || db->autoCommit==0 || db->nStatement==0 );
63777   return (p->rc==SQLITE_BUSY ? SQLITE_BUSY : SQLITE_OK);
63778 }
63779 
63780 
63781 /*
63782 ** Each VDBE holds the result of the most recent sqlite3_step() call
63783 ** in p->rc.  This routine sets that result back to SQLITE_OK.
63784 */
63785 SQLITE_PRIVATE void sqlite3VdbeResetStepResult(Vdbe *p){
63786   p->rc = SQLITE_OK;
63787 }
63788 
63789 /*
63790 ** Copy the error code and error message belonging to the VDBE passed
63791 ** as the first argument to its database handle (so that they will be
63792 ** returned by calls to sqlite3_errcode() and sqlite3_errmsg()).
63793 **
63794 ** This function does not clear the VDBE error code or message, just
63795 ** copies them to the database handle.
63796 */
63797 SQLITE_PRIVATE int sqlite3VdbeTransferError(Vdbe *p){
63798   sqlite3 *db = p->db;
63799   int rc = p->rc;
63800   if( p->zErrMsg ){
63801     u8 mallocFailed = db->mallocFailed;
63802     sqlite3BeginBenignMalloc();
63803     if( db->pErr==0 ) db->pErr = sqlite3ValueNew(db);
63804     sqlite3ValueSetStr(db->pErr, -1, p->zErrMsg, SQLITE_UTF8, SQLITE_TRANSIENT);
63805     sqlite3EndBenignMalloc();
63806     db->mallocFailed = mallocFailed;
63807     db->errCode = rc;
63808   }else{
63809     sqlite3Error(db, rc, 0);
63810   }
63811   return rc;
63812 }
63813 
63814 #ifdef SQLITE_ENABLE_SQLLOG
63815 /*
63816 ** If an SQLITE_CONFIG_SQLLOG hook is registered and the VM has been run,
63817 ** invoke it.
63818 */
63819 static void vdbeInvokeSqllog(Vdbe *v){
63820   if( sqlite3GlobalConfig.xSqllog && v->rc==SQLITE_OK && v->zSql && v->pc>=0 ){
63821     char *zExpanded = sqlite3VdbeExpandSql(v, v->zSql);
63822     assert( v->db->init.busy==0 );
63823     if( zExpanded ){
63824       sqlite3GlobalConfig.xSqllog(
63825           sqlite3GlobalConfig.pSqllogArg, v->db, zExpanded, 1
63826       );
63827       sqlite3DbFree(v->db, zExpanded);
63828     }
63829   }
63830 }
63831 #else
63832 # define vdbeInvokeSqllog(x)
63833 #endif
63834 
63835 /*
63836 ** Clean up a VDBE after execution but do not delete the VDBE just yet.
63837 ** Write any error messages into *pzErrMsg.  Return the result code.
63838 **
63839 ** After this routine is run, the VDBE should be ready to be executed
63840 ** again.
63841 **
63842 ** To look at it another way, this routine resets the state of the
63843 ** virtual machine from VDBE_MAGIC_RUN or VDBE_MAGIC_HALT back to
63844 ** VDBE_MAGIC_INIT.
63845 */
63846 SQLITE_PRIVATE int sqlite3VdbeReset(Vdbe *p){
63847   sqlite3 *db;
63848   db = p->db;
63849 
63850   /* If the VM did not run to completion or if it encountered an
63851   ** error, then it might not have been halted properly.  So halt
63852   ** it now.
63853   */
63854   sqlite3VdbeHalt(p);
63855 
63856   /* If the VDBE has be run even partially, then transfer the error code
63857   ** and error message from the VDBE into the main database structure.  But
63858   ** if the VDBE has just been set to run but has not actually executed any
63859   ** instructions yet, leave the main database error information unchanged.
63860   */
63861   if( p->pc>=0 ){
63862     vdbeInvokeSqllog(p);
63863     sqlite3VdbeTransferError(p);
63864     sqlite3DbFree(db, p->zErrMsg);
63865     p->zErrMsg = 0;
63866     if( p->runOnlyOnce ) p->expired = 1;
63867   }else if( p->rc && p->expired ){
63868     /* The expired flag was set on the VDBE before the first call
63869     ** to sqlite3_step(). For consistency (since sqlite3_step() was
63870     ** called), set the database error in this case as well.
63871     */
63872     sqlite3Error(db, p->rc, p->zErrMsg ? "%s" : 0, p->zErrMsg);
63873     sqlite3DbFree(db, p->zErrMsg);
63874     p->zErrMsg = 0;
63875   }
63876 
63877   /* Reclaim all memory used by the VDBE
63878   */
63879   Cleanup(p);
63880 
63881   /* Save profiling information from this VDBE run.
63882   */
63883 #ifdef VDBE_PROFILE
63884   {
63885     FILE *out = fopen("vdbe_profile.out", "a");
63886     if( out ){
63887       int i;
63888       fprintf(out, "---- ");
63889       for(i=0; i<p->nOp; i++){
63890         fprintf(out, "%02x", p->aOp[i].opcode);
63891       }
63892       fprintf(out, "\n");
63893       for(i=0; i<p->nOp; i++){
63894         fprintf(out, "%6d %10lld %8lld ",
63895            p->aOp[i].cnt,
63896            p->aOp[i].cycles,
63897            p->aOp[i].cnt>0 ? p->aOp[i].cycles/p->aOp[i].cnt : 0
63898         );
63899         sqlite3VdbePrintOp(out, i, &p->aOp[i]);
63900       }
63901       fclose(out);
63902     }
63903   }
63904 #endif
63905   p->iCurrentTime = 0;
63906   p->magic = VDBE_MAGIC_INIT;
63907   return p->rc & db->errMask;
63908 }
63909 
63910 /*
63911 ** Clean up and delete a VDBE after execution.  Return an integer which is
63912 ** the result code.  Write any error message text into *pzErrMsg.
63913 */
63914 SQLITE_PRIVATE int sqlite3VdbeFinalize(Vdbe *p){
63915   int rc = SQLITE_OK;
63916   if( p->magic==VDBE_MAGIC_RUN || p->magic==VDBE_MAGIC_HALT ){
63917     rc = sqlite3VdbeReset(p);
63918     assert( (rc & p->db->errMask)==rc );
63919   }
63920   sqlite3VdbeDelete(p);
63921   return rc;
63922 }
63923 
63924 /*
63925 ** If parameter iOp is less than zero, then invoke the destructor for
63926 ** all auxiliary data pointers currently cached by the VM passed as
63927 ** the first argument.
63928 **
63929 ** Or, if iOp is greater than or equal to zero, then the destructor is
63930 ** only invoked for those auxiliary data pointers created by the user
63931 ** function invoked by the OP_Function opcode at instruction iOp of
63932 ** VM pVdbe, and only then if:
63933 **
63934 **    * the associated function parameter is the 32nd or later (counting
63935 **      from left to right), or
63936 **
63937 **    * the corresponding bit in argument mask is clear (where the first
63938 **      function parameter corrsponds to bit 0 etc.).
63939 */
63940 SQLITE_PRIVATE void sqlite3VdbeDeleteAuxData(Vdbe *pVdbe, int iOp, int mask){
63941   AuxData **pp = &pVdbe->pAuxData;
63942   while( *pp ){
63943     AuxData *pAux = *pp;
63944     if( (iOp<0)
63945      || (pAux->iOp==iOp && (pAux->iArg>31 || !(mask & MASKBIT32(pAux->iArg))))
63946     ){
63947       testcase( pAux->iArg==31 );
63948       if( pAux->xDelete ){
63949         pAux->xDelete(pAux->pAux);
63950       }
63951       *pp = pAux->pNext;
63952       sqlite3DbFree(pVdbe->db, pAux);
63953     }else{
63954       pp= &pAux->pNext;
63955     }
63956   }
63957 }
63958 
63959 /*
63960 ** Free all memory associated with the Vdbe passed as the second argument,
63961 ** except for object itself, which is preserved.
63962 **
63963 ** The difference between this function and sqlite3VdbeDelete() is that
63964 ** VdbeDelete() also unlinks the Vdbe from the list of VMs associated with
63965 ** the database connection and frees the object itself.
63966 */
63967 SQLITE_PRIVATE void sqlite3VdbeClearObject(sqlite3 *db, Vdbe *p){
63968   SubProgram *pSub, *pNext;
63969   int i;
63970   assert( p->db==0 || p->db==db );
63971   releaseMemArray(p->aVar, p->nVar);
63972   releaseMemArray(p->aColName, p->nResColumn*COLNAME_N);
63973   for(pSub=p->pProgram; pSub; pSub=pNext){
63974     pNext = pSub->pNext;
63975     vdbeFreeOpArray(db, pSub->aOp, pSub->nOp);
63976     sqlite3DbFree(db, pSub);
63977   }
63978   for(i=p->nzVar-1; i>=0; i--) sqlite3DbFree(db, p->azVar[i]);
63979   vdbeFreeOpArray(db, p->aOp, p->nOp);
63980   sqlite3DbFree(db, p->aColName);
63981   sqlite3DbFree(db, p->zSql);
63982   sqlite3DbFree(db, p->pFree);
63983 #if defined(SQLITE_ENABLE_TREE_EXPLAIN)
63984   sqlite3DbFree(db, p->zExplain);
63985   sqlite3DbFree(db, p->pExplain);
63986 #endif
63987 }
63988 
63989 /*
63990 ** Delete an entire VDBE.
63991 */
63992 SQLITE_PRIVATE void sqlite3VdbeDelete(Vdbe *p){
63993   sqlite3 *db;
63994 
63995   if( NEVER(p==0) ) return;
63996   db = p->db;
63997   assert( sqlite3_mutex_held(db->mutex) );
63998   sqlite3VdbeClearObject(db, p);
63999   if( p->pPrev ){
64000     p->pPrev->pNext = p->pNext;
64001   }else{
64002     assert( db->pVdbe==p );
64003     db->pVdbe = p->pNext;
64004   }
64005   if( p->pNext ){
64006     p->pNext->pPrev = p->pPrev;
64007   }
64008   p->magic = VDBE_MAGIC_DEAD;
64009   p->db = 0;
64010   sqlite3DbFree(db, p);
64011 }
64012 
64013 /*
64014 ** Make sure the cursor p is ready to read or write the row to which it
64015 ** was last positioned.  Return an error code if an OOM fault or I/O error
64016 ** prevents us from positioning the cursor to its correct position.
64017 **
64018 ** If a MoveTo operation is pending on the given cursor, then do that
64019 ** MoveTo now.  If no move is pending, check to see if the row has been
64020 ** deleted out from under the cursor and if it has, mark the row as
64021 ** a NULL row.
64022 **
64023 ** If the cursor is already pointing to the correct row and that row has
64024 ** not been deleted out from under the cursor, then this routine is a no-op.
64025 */
64026 SQLITE_PRIVATE int sqlite3VdbeCursorMoveto(VdbeCursor *p){
64027   if( p->deferredMoveto ){
64028     int res, rc;
64029 #ifdef SQLITE_TEST
64030     extern int sqlite3_search_count;
64031 #endif
64032     assert( p->isTable );
64033     rc = sqlite3BtreeMovetoUnpacked(p->pCursor, 0, p->movetoTarget, 0, &res);
64034     if( rc ) return rc;
64035     p->lastRowid = p->movetoTarget;
64036     if( res!=0 ) return SQLITE_CORRUPT_BKPT;
64037     p->rowidIsValid = 1;
64038 #ifdef SQLITE_TEST
64039     sqlite3_search_count++;
64040 #endif
64041     p->deferredMoveto = 0;
64042     p->cacheStatus = CACHE_STALE;
64043   }else if( p->pCursor ){
64044     int hasMoved;
64045     int rc = sqlite3BtreeCursorHasMoved(p->pCursor, &hasMoved);
64046     if( rc ) return rc;
64047     if( hasMoved ){
64048       p->cacheStatus = CACHE_STALE;
64049       p->nullRow = 1;
64050     }
64051   }
64052   return SQLITE_OK;
64053 }
64054 
64055 /*
64056 ** The following functions:
64057 **
64058 ** sqlite3VdbeSerialType()
64059 ** sqlite3VdbeSerialTypeLen()
64060 ** sqlite3VdbeSerialLen()
64061 ** sqlite3VdbeSerialPut()
64062 ** sqlite3VdbeSerialGet()
64063 **
64064 ** encapsulate the code that serializes values for storage in SQLite
64065 ** data and index records. Each serialized value consists of a
64066 ** 'serial-type' and a blob of data. The serial type is an 8-byte unsigned
64067 ** integer, stored as a varint.
64068 **
64069 ** In an SQLite index record, the serial type is stored directly before
64070 ** the blob of data that it corresponds to. In a table record, all serial
64071 ** types are stored at the start of the record, and the blobs of data at
64072 ** the end. Hence these functions allow the caller to handle the
64073 ** serial-type and data blob separately.
64074 **
64075 ** The following table describes the various storage classes for data:
64076 **
64077 **   serial type        bytes of data      type
64078 **   --------------     ---------------    ---------------
64079 **      0                     0            NULL
64080 **      1                     1            signed integer
64081 **      2                     2            signed integer
64082 **      3                     3            signed integer
64083 **      4                     4            signed integer
64084 **      5                     6            signed integer
64085 **      6                     8            signed integer
64086 **      7                     8            IEEE float
64087 **      8                     0            Integer constant 0
64088 **      9                     0            Integer constant 1
64089 **     10,11                               reserved for expansion
64090 **    N>=12 and even       (N-12)/2        BLOB
64091 **    N>=13 and odd        (N-13)/2        text
64092 **
64093 ** The 8 and 9 types were added in 3.3.0, file format 4.  Prior versions
64094 ** of SQLite will not understand those serial types.
64095 */
64096 
64097 /*
64098 ** Return the serial-type for the value stored in pMem.
64099 */
64100 SQLITE_PRIVATE u32 sqlite3VdbeSerialType(Mem *pMem, int file_format){
64101   int flags = pMem->flags;
64102   int n;
64103 
64104   if( flags&MEM_Null ){
64105     return 0;
64106   }
64107   if( flags&MEM_Int ){
64108     /* Figure out whether to use 1, 2, 4, 6 or 8 bytes. */
64109 #   define MAX_6BYTE ((((i64)0x00008000)<<32)-1)
64110     i64 i = pMem->u.i;
64111     u64 u;
64112     if( i<0 ){
64113       if( i<(-MAX_6BYTE) ) return 6;
64114       /* Previous test prevents:  u = -(-9223372036854775808) */
64115       u = -i;
64116     }else{
64117       u = i;
64118     }
64119     if( u<=127 ){
64120       return ((i&1)==i && file_format>=4) ? 8+(u32)u : 1;
64121     }
64122     if( u<=32767 ) return 2;
64123     if( u<=8388607 ) return 3;
64124     if( u<=2147483647 ) return 4;
64125     if( u<=MAX_6BYTE ) return 5;
64126     return 6;
64127   }
64128   if( flags&MEM_Real ){
64129     return 7;
64130   }
64131   assert( pMem->db->mallocFailed || flags&(MEM_Str|MEM_Blob) );
64132   n = pMem->n;
64133   if( flags & MEM_Zero ){
64134     n += pMem->u.nZero;
64135   }
64136   assert( n>=0 );
64137   return ((n*2) + 12 + ((flags&MEM_Str)!=0));
64138 }
64139 
64140 /*
64141 ** Return the length of the data corresponding to the supplied serial-type.
64142 */
64143 SQLITE_PRIVATE u32 sqlite3VdbeSerialTypeLen(u32 serial_type){
64144   if( serial_type>=12 ){
64145     return (serial_type-12)/2;
64146   }else{
64147     static const u8 aSize[] = { 0, 1, 2, 3, 4, 6, 8, 8, 0, 0, 0, 0 };
64148     return aSize[serial_type];
64149   }
64150 }
64151 
64152 /*
64153 ** If we are on an architecture with mixed-endian floating
64154 ** points (ex: ARM7) then swap the lower 4 bytes with the
64155 ** upper 4 bytes.  Return the result.
64156 **
64157 ** For most architectures, this is a no-op.
64158 **
64159 ** (later):  It is reported to me that the mixed-endian problem
64160 ** on ARM7 is an issue with GCC, not with the ARM7 chip.  It seems
64161 ** that early versions of GCC stored the two words of a 64-bit
64162 ** float in the wrong order.  And that error has been propagated
64163 ** ever since.  The blame is not necessarily with GCC, though.
64164 ** GCC might have just copying the problem from a prior compiler.
64165 ** I am also told that newer versions of GCC that follow a different
64166 ** ABI get the byte order right.
64167 **
64168 ** Developers using SQLite on an ARM7 should compile and run their
64169 ** application using -DSQLITE_DEBUG=1 at least once.  With DEBUG
64170 ** enabled, some asserts below will ensure that the byte order of
64171 ** floating point values is correct.
64172 **
64173 ** (2007-08-30)  Frank van Vugt has studied this problem closely
64174 ** and has send his findings to the SQLite developers.  Frank
64175 ** writes that some Linux kernels offer floating point hardware
64176 ** emulation that uses only 32-bit mantissas instead of a full
64177 ** 48-bits as required by the IEEE standard.  (This is the
64178 ** CONFIG_FPE_FASTFPE option.)  On such systems, floating point
64179 ** byte swapping becomes very complicated.  To avoid problems,
64180 ** the necessary byte swapping is carried out using a 64-bit integer
64181 ** rather than a 64-bit float.  Frank assures us that the code here
64182 ** works for him.  We, the developers, have no way to independently
64183 ** verify this, but Frank seems to know what he is talking about
64184 ** so we trust him.
64185 */
64186 #ifdef SQLITE_MIXED_ENDIAN_64BIT_FLOAT
64187 static u64 floatSwap(u64 in){
64188   union {
64189     u64 r;
64190     u32 i[2];
64191   } u;
64192   u32 t;
64193 
64194   u.r = in;
64195   t = u.i[0];
64196   u.i[0] = u.i[1];
64197   u.i[1] = t;
64198   return u.r;
64199 }
64200 # define swapMixedEndianFloat(X)  X = floatSwap(X)
64201 #else
64202 # define swapMixedEndianFloat(X)
64203 #endif
64204 
64205 /*
64206 ** Write the serialized data blob for the value stored in pMem into
64207 ** buf. It is assumed that the caller has allocated sufficient space.
64208 ** Return the number of bytes written.
64209 **
64210 ** nBuf is the amount of space left in buf[].  The caller is responsible
64211 ** for allocating enough space to buf[] to hold the entire field, exclusive
64212 ** of the pMem->u.nZero bytes for a MEM_Zero value.
64213 **
64214 ** Return the number of bytes actually written into buf[].  The number
64215 ** of bytes in the zero-filled tail is included in the return value only
64216 ** if those bytes were zeroed in buf[].
64217 */
64218 SQLITE_PRIVATE u32 sqlite3VdbeSerialPut(u8 *buf, Mem *pMem, u32 serial_type){
64219   u32 len;
64220 
64221   /* Integer and Real */
64222   if( serial_type<=7 && serial_type>0 ){
64223     u64 v;
64224     u32 i;
64225     if( serial_type==7 ){
64226       assert( sizeof(v)==sizeof(pMem->r) );
64227       memcpy(&v, &pMem->r, sizeof(v));
64228       swapMixedEndianFloat(v);
64229     }else{
64230       v = pMem->u.i;
64231     }
64232     len = i = sqlite3VdbeSerialTypeLen(serial_type);
64233     while( i-- ){
64234       buf[i] = (u8)(v&0xFF);
64235       v >>= 8;
64236     }
64237     return len;
64238   }
64239 
64240   /* String or blob */
64241   if( serial_type>=12 ){
64242     assert( pMem->n + ((pMem->flags & MEM_Zero)?pMem->u.nZero:0)
64243              == (int)sqlite3VdbeSerialTypeLen(serial_type) );
64244     len = pMem->n;
64245     memcpy(buf, pMem->z, len);
64246     return len;
64247   }
64248 
64249   /* NULL or constants 0 or 1 */
64250   return 0;
64251 }
64252 
64253 /*
64254 ** Deserialize the data blob pointed to by buf as serial type serial_type
64255 ** and store the result in pMem.  Return the number of bytes read.
64256 */
64257 SQLITE_PRIVATE u32 sqlite3VdbeSerialGet(
64258   const unsigned char *buf,     /* Buffer to deserialize from */
64259   u32 serial_type,              /* Serial type to deserialize */
64260   Mem *pMem                     /* Memory cell to write value into */
64261 ){
64262   u64 x;
64263   u32 y;
64264   int i;
64265   switch( serial_type ){
64266     case 10:   /* Reserved for future use */
64267     case 11:   /* Reserved for future use */
64268     case 0: {  /* NULL */
64269       pMem->flags = MEM_Null;
64270       break;
64271     }
64272     case 1: { /* 1-byte signed integer */
64273       pMem->u.i = (signed char)buf[0];
64274       pMem->flags = MEM_Int;
64275       return 1;
64276     }
64277     case 2: { /* 2-byte signed integer */
64278       i = 256*(signed char)buf[0] | buf[1];
64279       pMem->u.i = (i64)i;
64280       pMem->flags = MEM_Int;
64281       return 2;
64282     }
64283     case 3: { /* 3-byte signed integer */
64284       i = 65536*(signed char)buf[0] | (buf[1]<<8) | buf[2];
64285       pMem->u.i = (i64)i;
64286       pMem->flags = MEM_Int;
64287       return 3;
64288     }
64289     case 4: { /* 4-byte signed integer */
64290       y = ((unsigned)buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | buf[3];
64291       pMem->u.i = (i64)*(int*)&y;
64292       pMem->flags = MEM_Int;
64293       return 4;
64294     }
64295     case 5: { /* 6-byte signed integer */
64296       x = 256*(signed char)buf[0] + buf[1];
64297       y = ((unsigned)buf[2]<<24) | (buf[3]<<16) | (buf[4]<<8) | buf[5];
64298       x = (x<<32) | y;
64299       pMem->u.i = *(i64*)&x;
64300       pMem->flags = MEM_Int;
64301       return 6;
64302     }
64303     case 6:   /* 8-byte signed integer */
64304     case 7: { /* IEEE floating point */
64305 #if !defined(NDEBUG) && !defined(SQLITE_OMIT_FLOATING_POINT)
64306       /* Verify that integers and floating point values use the same
64307       ** byte order.  Or, that if SQLITE_MIXED_ENDIAN_64BIT_FLOAT is
64308       ** defined that 64-bit floating point values really are mixed
64309       ** endian.
64310       */
64311       static const u64 t1 = ((u64)0x3ff00000)<<32;
64312       static const double r1 = 1.0;
64313       u64 t2 = t1;
64314       swapMixedEndianFloat(t2);
64315       assert( sizeof(r1)==sizeof(t2) && memcmp(&r1, &t2, sizeof(r1))==0 );
64316 #endif
64317       x = ((unsigned)buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | buf[3];
64318       y = ((unsigned)buf[4]<<24) | (buf[5]<<16) | (buf[6]<<8) | buf[7];
64319       x = (x<<32) | y;
64320       if( serial_type==6 ){
64321         pMem->u.i = *(i64*)&x;
64322         pMem->flags = MEM_Int;
64323       }else{
64324         assert( sizeof(x)==8 && sizeof(pMem->r)==8 );
64325         swapMixedEndianFloat(x);
64326         memcpy(&pMem->r, &x, sizeof(x));
64327         pMem->flags = sqlite3IsNaN(pMem->r) ? MEM_Null : MEM_Real;
64328       }
64329       return 8;
64330     }
64331     case 8:    /* Integer 0 */
64332     case 9: {  /* Integer 1 */
64333       pMem->u.i = serial_type-8;
64334       pMem->flags = MEM_Int;
64335       return 0;
64336     }
64337     default: {
64338       static const u16 aFlag[] = { MEM_Blob|MEM_Ephem, MEM_Str|MEM_Ephem };
64339       u32 len = (serial_type-12)/2;
64340       pMem->z = (char *)buf;
64341       pMem->n = len;
64342       pMem->xDel = 0;
64343       pMem->flags = aFlag[serial_type&1];
64344       return len;
64345     }
64346   }
64347   return 0;
64348 }
64349 
64350 /*
64351 ** This routine is used to allocate sufficient space for an UnpackedRecord
64352 ** structure large enough to be used with sqlite3VdbeRecordUnpack() if
64353 ** the first argument is a pointer to KeyInfo structure pKeyInfo.
64354 **
64355 ** The space is either allocated using sqlite3DbMallocRaw() or from within
64356 ** the unaligned buffer passed via the second and third arguments (presumably
64357 ** stack space). If the former, then *ppFree is set to a pointer that should
64358 ** be eventually freed by the caller using sqlite3DbFree(). Or, if the
64359 ** allocation comes from the pSpace/szSpace buffer, *ppFree is set to NULL
64360 ** before returning.
64361 **
64362 ** If an OOM error occurs, NULL is returned.
64363 */
64364 SQLITE_PRIVATE UnpackedRecord *sqlite3VdbeAllocUnpackedRecord(
64365   KeyInfo *pKeyInfo,              /* Description of the record */
64366   char *pSpace,                   /* Unaligned space available */
64367   int szSpace,                    /* Size of pSpace[] in bytes */
64368   char **ppFree                   /* OUT: Caller should free this pointer */
64369 ){
64370   UnpackedRecord *p;              /* Unpacked record to return */
64371   int nOff;                       /* Increment pSpace by nOff to align it */
64372   int nByte;                      /* Number of bytes required for *p */
64373 
64374   /* We want to shift the pointer pSpace up such that it is 8-byte aligned.
64375   ** Thus, we need to calculate a value, nOff, between 0 and 7, to shift
64376   ** it by.  If pSpace is already 8-byte aligned, nOff should be zero.
64377   */
64378   nOff = (8 - (SQLITE_PTR_TO_INT(pSpace) & 7)) & 7;
64379   nByte = ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*(pKeyInfo->nField+1);
64380   if( nByte>szSpace+nOff ){
64381     p = (UnpackedRecord *)sqlite3DbMallocRaw(pKeyInfo->db, nByte);
64382     *ppFree = (char *)p;
64383     if( !p ) return 0;
64384   }else{
64385     p = (UnpackedRecord*)&pSpace[nOff];
64386     *ppFree = 0;
64387   }
64388 
64389   p->aMem = (Mem*)&((char*)p)[ROUND8(sizeof(UnpackedRecord))];
64390   assert( pKeyInfo->aSortOrder!=0 );
64391   p->pKeyInfo = pKeyInfo;
64392   p->nField = pKeyInfo->nField + 1;
64393   return p;
64394 }
64395 
64396 /*
64397 ** Given the nKey-byte encoding of a record in pKey[], populate the
64398 ** UnpackedRecord structure indicated by the fourth argument with the
64399 ** contents of the decoded record.
64400 */
64401 SQLITE_PRIVATE void sqlite3VdbeRecordUnpack(
64402   KeyInfo *pKeyInfo,     /* Information about the record format */
64403   int nKey,              /* Size of the binary record */
64404   const void *pKey,      /* The binary record */
64405   UnpackedRecord *p      /* Populate this structure before returning. */
64406 ){
64407   const unsigned char *aKey = (const unsigned char *)pKey;
64408   int d;
64409   u32 idx;                        /* Offset in aKey[] to read from */
64410   u16 u;                          /* Unsigned loop counter */
64411   u32 szHdr;
64412   Mem *pMem = p->aMem;
64413 
64414   p->flags = 0;
64415   assert( EIGHT_BYTE_ALIGNMENT(pMem) );
64416   idx = getVarint32(aKey, szHdr);
64417   d = szHdr;
64418   u = 0;
64419   while( idx<szHdr && u<p->nField && d<=nKey ){
64420     u32 serial_type;
64421 
64422     idx += getVarint32(&aKey[idx], serial_type);
64423     pMem->enc = pKeyInfo->enc;
64424     pMem->db = pKeyInfo->db;
64425     /* pMem->flags = 0; // sqlite3VdbeSerialGet() will set this for us */
64426     pMem->zMalloc = 0;
64427     d += sqlite3VdbeSerialGet(&aKey[d], serial_type, pMem);
64428     pMem++;
64429     u++;
64430   }
64431   assert( u<=pKeyInfo->nField + 1 );
64432   p->nField = u;
64433 }
64434 
64435 /*
64436 ** This function compares the two table rows or index records
64437 ** specified by {nKey1, pKey1} and pPKey2.  It returns a negative, zero
64438 ** or positive integer if key1 is less than, equal to or
64439 ** greater than key2.  The {nKey1, pKey1} key must be a blob
64440 ** created by th OP_MakeRecord opcode of the VDBE.  The pPKey2
64441 ** key must be a parsed key such as obtained from
64442 ** sqlite3VdbeParseRecord.
64443 **
64444 ** Key1 and Key2 do not have to contain the same number of fields.
64445 ** The key with fewer fields is usually compares less than the
64446 ** longer key.  However if the UNPACKED_INCRKEY flags in pPKey2 is set
64447 ** and the common prefixes are equal, then key1 is less than key2.
64448 ** Or if the UNPACKED_MATCH_PREFIX flag is set and the prefixes are
64449 ** equal, then the keys are considered to be equal and
64450 ** the parts beyond the common prefix are ignored.
64451 */
64452 SQLITE_PRIVATE int sqlite3VdbeRecordCompare(
64453   int nKey1, const void *pKey1, /* Left key */
64454   UnpackedRecord *pPKey2        /* Right key */
64455 ){
64456   u32 d1;            /* Offset into aKey[] of next data element */
64457   u32 idx1;          /* Offset into aKey[] of next header element */
64458   u32 szHdr1;        /* Number of bytes in header */
64459   int i = 0;
64460   int rc = 0;
64461   const unsigned char *aKey1 = (const unsigned char *)pKey1;
64462   KeyInfo *pKeyInfo;
64463   Mem mem1;
64464 
64465   pKeyInfo = pPKey2->pKeyInfo;
64466   mem1.enc = pKeyInfo->enc;
64467   mem1.db = pKeyInfo->db;
64468   /* mem1.flags = 0;  // Will be initialized by sqlite3VdbeSerialGet() */
64469   VVA_ONLY( mem1.zMalloc = 0; ) /* Only needed by assert() statements */
64470 
64471   /* Compilers may complain that mem1.u.i is potentially uninitialized.
64472   ** We could initialize it, as shown here, to silence those complaints.
64473   ** But in fact, mem1.u.i will never actually be used uninitialized, and doing
64474   ** the unnecessary initialization has a measurable negative performance
64475   ** impact, since this routine is a very high runner.  And so, we choose
64476   ** to ignore the compiler warnings and leave this variable uninitialized.
64477   */
64478   /*  mem1.u.i = 0;  // not needed, here to silence compiler warning */
64479 
64480   idx1 = getVarint32(aKey1, szHdr1);
64481   d1 = szHdr1;
64482   assert( pKeyInfo->nField+pKeyInfo->nXField>=pPKey2->nField || CORRUPT_DB );
64483   assert( pKeyInfo->aSortOrder!=0 );
64484   assert( pKeyInfo->nField>0 );
64485   assert( idx1<=szHdr1 || CORRUPT_DB );
64486   do{
64487     u32 serial_type1;
64488 
64489     /* Read the serial types for the next element in each key. */
64490     idx1 += getVarint32( aKey1+idx1, serial_type1 );
64491 
64492     /* Verify that there is enough key space remaining to avoid
64493     ** a buffer overread.  The "d1+serial_type1+2" subexpression will
64494     ** always be greater than or equal to the amount of required key space.
64495     ** Use that approximation to avoid the more expensive call to
64496     ** sqlite3VdbeSerialTypeLen() in the common case.
64497     */
64498     if( d1+serial_type1+2>(u32)nKey1
64499      && d1+sqlite3VdbeSerialTypeLen(serial_type1)>(u32)nKey1
64500     ){
64501       break;
64502     }
64503 
64504     /* Extract the values to be compared.
64505     */
64506     d1 += sqlite3VdbeSerialGet(&aKey1[d1], serial_type1, &mem1);
64507 
64508     /* Do the comparison
64509     */
64510     rc = sqlite3MemCompare(&mem1, &pPKey2->aMem[i], pKeyInfo->aColl[i]);
64511     if( rc!=0 ){
64512       assert( mem1.zMalloc==0 );  /* See comment below */
64513       if( pKeyInfo->aSortOrder[i] ){
64514         rc = -rc;  /* Invert the result for DESC sort order. */
64515       }
64516       return rc;
64517     }
64518     i++;
64519   }while( idx1<szHdr1 && i<pPKey2->nField );
64520 
64521   /* No memory allocation is ever used on mem1.  Prove this using
64522   ** the following assert().  If the assert() fails, it indicates a
64523   ** memory leak and a need to call sqlite3VdbeMemRelease(&mem1).
64524   */
64525   assert( mem1.zMalloc==0 );
64526 
64527   /* rc==0 here means that one of the keys ran out of fields and
64528   ** all the fields up to that point were equal. If the UNPACKED_INCRKEY
64529   ** flag is set, then break the tie by treating key2 as larger.
64530   ** If the UPACKED_PREFIX_MATCH flag is set, then keys with common prefixes
64531   ** are considered to be equal.  Otherwise, the longer key is the
64532   ** larger.  As it happens, the pPKey2 will always be the longer
64533   ** if there is a difference.
64534   */
64535   assert( rc==0 );
64536   if( pPKey2->flags & UNPACKED_INCRKEY ){
64537     rc = -1;
64538   }else if( pPKey2->flags & UNPACKED_PREFIX_MATCH ){
64539     /* Leave rc==0 */
64540   }else if( idx1<szHdr1 ){
64541     rc = 1;
64542   }
64543   return rc;
64544 }
64545 
64546 
64547 /*
64548 ** pCur points at an index entry created using the OP_MakeRecord opcode.
64549 ** Read the rowid (the last field in the record) and store it in *rowid.
64550 ** Return SQLITE_OK if everything works, or an error code otherwise.
64551 **
64552 ** pCur might be pointing to text obtained from a corrupt database file.
64553 ** So the content cannot be trusted.  Do appropriate checks on the content.
64554 */
64555 SQLITE_PRIVATE int sqlite3VdbeIdxRowid(sqlite3 *db, BtCursor *pCur, i64 *rowid){
64556   i64 nCellKey = 0;
64557   int rc;
64558   u32 szHdr;        /* Size of the header */
64559   u32 typeRowid;    /* Serial type of the rowid */
64560   u32 lenRowid;     /* Size of the rowid */
64561   Mem m, v;
64562 
64563   UNUSED_PARAMETER(db);
64564 
64565   /* Get the size of the index entry.  Only indices entries of less
64566   ** than 2GiB are support - anything large must be database corruption.
64567   ** Any corruption is detected in sqlite3BtreeParseCellPtr(), though, so
64568   ** this code can safely assume that nCellKey is 32-bits
64569   */
64570   assert( sqlite3BtreeCursorIsValid(pCur) );
64571   VVA_ONLY(rc =) sqlite3BtreeKeySize(pCur, &nCellKey);
64572   assert( rc==SQLITE_OK );     /* pCur is always valid so KeySize cannot fail */
64573   assert( (nCellKey & SQLITE_MAX_U32)==(u64)nCellKey );
64574 
64575   /* Read in the complete content of the index entry */
64576   memset(&m, 0, sizeof(m));
64577   rc = sqlite3VdbeMemFromBtree(pCur, 0, (u32)nCellKey, 1, &m);
64578   if( rc ){
64579     return rc;
64580   }
64581 
64582   /* The index entry must begin with a header size */
64583   (void)getVarint32((u8*)m.z, szHdr);
64584   testcase( szHdr==3 );
64585   testcase( szHdr==m.n );
64586   if( unlikely(szHdr<3 || (int)szHdr>m.n) ){
64587     goto idx_rowid_corruption;
64588   }
64589 
64590   /* The last field of the index should be an integer - the ROWID.
64591   ** Verify that the last entry really is an integer. */
64592   (void)getVarint32((u8*)&m.z[szHdr-1], typeRowid);
64593   testcase( typeRowid==1 );
64594   testcase( typeRowid==2 );
64595   testcase( typeRowid==3 );
64596   testcase( typeRowid==4 );
64597   testcase( typeRowid==5 );
64598   testcase( typeRowid==6 );
64599   testcase( typeRowid==8 );
64600   testcase( typeRowid==9 );
64601   if( unlikely(typeRowid<1 || typeRowid>9 || typeRowid==7) ){
64602     goto idx_rowid_corruption;
64603   }
64604   lenRowid = sqlite3VdbeSerialTypeLen(typeRowid);
64605   testcase( (u32)m.n==szHdr+lenRowid );
64606   if( unlikely((u32)m.n<szHdr+lenRowid) ){
64607     goto idx_rowid_corruption;
64608   }
64609 
64610   /* Fetch the integer off the end of the index record */
64611   sqlite3VdbeSerialGet((u8*)&m.z[m.n-lenRowid], typeRowid, &v);
64612   *rowid = v.u.i;
64613   sqlite3VdbeMemRelease(&m);
64614   return SQLITE_OK;
64615 
64616   /* Jump here if database corruption is detected after m has been
64617   ** allocated.  Free the m object and return SQLITE_CORRUPT. */
64618 idx_rowid_corruption:
64619   testcase( m.zMalloc!=0 );
64620   sqlite3VdbeMemRelease(&m);
64621   return SQLITE_CORRUPT_BKPT;
64622 }
64623 
64624 /*
64625 ** Compare the key of the index entry that cursor pC is pointing to against
64626 ** the key string in pUnpacked.  Write into *pRes a number
64627 ** that is negative, zero, or positive if pC is less than, equal to,
64628 ** or greater than pUnpacked.  Return SQLITE_OK on success.
64629 **
64630 ** pUnpacked is either created without a rowid or is truncated so that it
64631 ** omits the rowid at the end.  The rowid at the end of the index entry
64632 ** is ignored as well.  Hence, this routine only compares the prefixes
64633 ** of the keys prior to the final rowid, not the entire key.
64634 */
64635 SQLITE_PRIVATE int sqlite3VdbeIdxKeyCompare(
64636   VdbeCursor *pC,             /* The cursor to compare against */
64637   UnpackedRecord *pUnpacked,  /* Unpacked version of key to compare against */
64638   int *res                    /* Write the comparison result here */
64639 ){
64640   i64 nCellKey = 0;
64641   int rc;
64642   BtCursor *pCur = pC->pCursor;
64643   Mem m;
64644 
64645   assert( sqlite3BtreeCursorIsValid(pCur) );
64646   VVA_ONLY(rc =) sqlite3BtreeKeySize(pCur, &nCellKey);
64647   assert( rc==SQLITE_OK );    /* pCur is always valid so KeySize cannot fail */
64648   /* nCellKey will always be between 0 and 0xffffffff because of the say
64649   ** that btreeParseCellPtr() and sqlite3GetVarint32() are implemented */
64650   if( nCellKey<=0 || nCellKey>0x7fffffff ){
64651     *res = 0;
64652     return SQLITE_CORRUPT_BKPT;
64653   }
64654   memset(&m, 0, sizeof(m));
64655   rc = sqlite3VdbeMemFromBtree(pC->pCursor, 0, (u32)nCellKey, 1, &m);
64656   if( rc ){
64657     return rc;
64658   }
64659   assert( pUnpacked->flags & UNPACKED_PREFIX_MATCH );
64660   *res = sqlite3VdbeRecordCompare(m.n, m.z, pUnpacked);
64661   sqlite3VdbeMemRelease(&m);
64662   return SQLITE_OK;
64663 }
64664 
64665 /*
64666 ** This routine sets the value to be returned by subsequent calls to
64667 ** sqlite3_changes() on the database handle 'db'.
64668 */
64669 SQLITE_PRIVATE void sqlite3VdbeSetChanges(sqlite3 *db, int nChange){
64670   assert( sqlite3_mutex_held(db->mutex) );
64671   db->nChange = nChange;
64672   db->nTotalChange += nChange;
64673 }
64674 
64675 /*
64676 ** Set a flag in the vdbe to update the change counter when it is finalised
64677 ** or reset.
64678 */
64679 SQLITE_PRIVATE void sqlite3VdbeCountChanges(Vdbe *v){
64680   v->changeCntOn = 1;
64681 }
64682 
64683 /*
64684 ** Mark every prepared statement associated with a database connection
64685 ** as expired.
64686 **
64687 ** An expired statement means that recompilation of the statement is
64688 ** recommend.  Statements expire when things happen that make their
64689 ** programs obsolete.  Removing user-defined functions or collating
64690 ** sequences, or changing an authorization function are the types of
64691 ** things that make prepared statements obsolete.
64692 */
64693 SQLITE_PRIVATE void sqlite3ExpirePreparedStatements(sqlite3 *db){
64694   Vdbe *p;
64695   for(p = db->pVdbe; p; p=p->pNext){
64696     p->expired = 1;
64697   }
64698 }
64699 
64700 /*
64701 ** Return the database associated with the Vdbe.
64702 */
64703 SQLITE_PRIVATE sqlite3 *sqlite3VdbeDb(Vdbe *v){
64704   return v->db;
64705 }
64706 
64707 /*
64708 ** Return a pointer to an sqlite3_value structure containing the value bound
64709 ** parameter iVar of VM v. Except, if the value is an SQL NULL, return
64710 ** 0 instead. Unless it is NULL, apply affinity aff (one of the SQLITE_AFF_*
64711 ** constants) to the value before returning it.
64712 **
64713 ** The returned value must be freed by the caller using sqlite3ValueFree().
64714 */
64715 SQLITE_PRIVATE sqlite3_value *sqlite3VdbeGetBoundValue(Vdbe *v, int iVar, u8 aff){
64716   assert( iVar>0 );
64717   if( v ){
64718     Mem *pMem = &v->aVar[iVar-1];
64719     if( 0==(pMem->flags & MEM_Null) ){
64720       sqlite3_value *pRet = sqlite3ValueNew(v->db);
64721       if( pRet ){
64722         sqlite3VdbeMemCopy((Mem *)pRet, pMem);
64723         sqlite3ValueApplyAffinity(pRet, aff, SQLITE_UTF8);
64724         sqlite3VdbeMemStoreType((Mem *)pRet);
64725       }
64726       return pRet;
64727     }
64728   }
64729   return 0;
64730 }
64731 
64732 /*
64733 ** Configure SQL variable iVar so that binding a new value to it signals
64734 ** to sqlite3_reoptimize() that re-preparing the statement may result
64735 ** in a better query plan.
64736 */
64737 SQLITE_PRIVATE void sqlite3VdbeSetVarmask(Vdbe *v, int iVar){
64738   assert( iVar>0 );
64739   if( iVar>32 ){
64740     v->expmask = 0xffffffff;
64741   }else{
64742     v->expmask |= ((u32)1 << (iVar-1));
64743   }
64744 }
64745 
64746 #ifndef SQLITE_OMIT_VIRTUALTABLE
64747 /*
64748 ** Transfer error message text from an sqlite3_vtab.zErrMsg (text stored
64749 ** in memory obtained from sqlite3_malloc) into a Vdbe.zErrMsg (text stored
64750 ** in memory obtained from sqlite3DbMalloc).
64751 */
64752 SQLITE_PRIVATE void sqlite3VtabImportErrmsg(Vdbe *p, sqlite3_vtab *pVtab){
64753   sqlite3 *db = p->db;
64754   sqlite3DbFree(db, p->zErrMsg);
64755   p->zErrMsg = sqlite3DbStrDup(db, pVtab->zErrMsg);
64756   sqlite3_free(pVtab->zErrMsg);
64757   pVtab->zErrMsg = 0;
64758 }
64759 #endif /* SQLITE_OMIT_VIRTUALTABLE */
64760 
64761 /************** End of vdbeaux.c *********************************************/
64762 /************** Begin file vdbeapi.c *****************************************/
64763 /*
64764 ** 2004 May 26
64765 **
64766 ** The author disclaims copyright to this source code.  In place of
64767 ** a legal notice, here is a blessing:
64768 **
64769 **    May you do good and not evil.
64770 **    May you find forgiveness for yourself and forgive others.
64771 **    May you share freely, never taking more than you give.
64772 **
64773 *************************************************************************
64774 **
64775 ** This file contains code use to implement APIs that are part of the
64776 ** VDBE.
64777 */
64778 
64779 #ifndef SQLITE_OMIT_DEPRECATED
64780 /*
64781 ** Return TRUE (non-zero) of the statement supplied as an argument needs
64782 ** to be recompiled.  A statement needs to be recompiled whenever the
64783 ** execution environment changes in a way that would alter the program
64784 ** that sqlite3_prepare() generates.  For example, if new functions or
64785 ** collating sequences are registered or if an authorizer function is
64786 ** added or changed.
64787 */
64788 SQLITE_API int sqlite3_expired(sqlite3_stmt *pStmt){
64789   Vdbe *p = (Vdbe*)pStmt;
64790   return p==0 || p->expired;
64791 }
64792 #endif
64793 
64794 /*
64795 ** Check on a Vdbe to make sure it has not been finalized.  Log
64796 ** an error and return true if it has been finalized (or is otherwise
64797 ** invalid).  Return false if it is ok.
64798 */
64799 static int vdbeSafety(Vdbe *p){
64800   if( p->db==0 ){
64801     sqlite3_log(SQLITE_MISUSE, "API called with finalized prepared statement");
64802     return 1;
64803   }else{
64804     return 0;
64805   }
64806 }
64807 static int vdbeSafetyNotNull(Vdbe *p){
64808   if( p==0 ){
64809     sqlite3_log(SQLITE_MISUSE, "API called with NULL prepared statement");
64810     return 1;
64811   }else{
64812     return vdbeSafety(p);
64813   }
64814 }
64815 
64816 /*
64817 ** The following routine destroys a virtual machine that is created by
64818 ** the sqlite3_compile() routine. The integer returned is an SQLITE_
64819 ** success/failure code that describes the result of executing the virtual
64820 ** machine.
64821 **
64822 ** This routine sets the error code and string returned by
64823 ** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
64824 */
64825 SQLITE_API int sqlite3_finalize(sqlite3_stmt *pStmt){
64826   int rc;
64827   if( pStmt==0 ){
64828     /* IMPLEMENTATION-OF: R-57228-12904 Invoking sqlite3_finalize() on a NULL
64829     ** pointer is a harmless no-op. */
64830     rc = SQLITE_OK;
64831   }else{
64832     Vdbe *v = (Vdbe*)pStmt;
64833     sqlite3 *db = v->db;
64834     if( vdbeSafety(v) ) return SQLITE_MISUSE_BKPT;
64835     sqlite3_mutex_enter(db->mutex);
64836     rc = sqlite3VdbeFinalize(v);
64837     rc = sqlite3ApiExit(db, rc);
64838     sqlite3LeaveMutexAndCloseZombie(db);
64839   }
64840   return rc;
64841 }
64842 
64843 /*
64844 ** Terminate the current execution of an SQL statement and reset it
64845 ** back to its starting state so that it can be reused. A success code from
64846 ** the prior execution is returned.
64847 **
64848 ** This routine sets the error code and string returned by
64849 ** sqlite3_errcode(), sqlite3_errmsg() and sqlite3_errmsg16().
64850 */
64851 SQLITE_API int sqlite3_reset(sqlite3_stmt *pStmt){
64852   int rc;
64853   if( pStmt==0 ){
64854     rc = SQLITE_OK;
64855   }else{
64856     Vdbe *v = (Vdbe*)pStmt;
64857     sqlite3_mutex_enter(v->db->mutex);
64858     rc = sqlite3VdbeReset(v);
64859     sqlite3VdbeRewind(v);
64860     assert( (rc & (v->db->errMask))==rc );
64861     rc = sqlite3ApiExit(v->db, rc);
64862     sqlite3_mutex_leave(v->db->mutex);
64863   }
64864   return rc;
64865 }
64866 
64867 /*
64868 ** Set all the parameters in the compiled SQL statement to NULL.
64869 */
64870 SQLITE_API int sqlite3_clear_bindings(sqlite3_stmt *pStmt){
64871   int i;
64872   int rc = SQLITE_OK;
64873   Vdbe *p = (Vdbe*)pStmt;
64874 #if SQLITE_THREADSAFE
64875   sqlite3_mutex *mutex = ((Vdbe*)pStmt)->db->mutex;
64876 #endif
64877   sqlite3_mutex_enter(mutex);
64878   for(i=0; i<p->nVar; i++){
64879     sqlite3VdbeMemRelease(&p->aVar[i]);
64880     p->aVar[i].flags = MEM_Null;
64881   }
64882   if( p->isPrepareV2 && p->expmask ){
64883     p->expired = 1;
64884   }
64885   sqlite3_mutex_leave(mutex);
64886   return rc;
64887 }
64888 
64889 
64890 /**************************** sqlite3_value_  *******************************
64891 ** The following routines extract information from a Mem or sqlite3_value
64892 ** structure.
64893 */
64894 SQLITE_API const void *sqlite3_value_blob(sqlite3_value *pVal){
64895   Mem *p = (Mem*)pVal;
64896   if( p->flags & (MEM_Blob|MEM_Str) ){
64897     sqlite3VdbeMemExpandBlob(p);
64898     p->flags &= ~MEM_Str;
64899     p->flags |= MEM_Blob;
64900     return p->n ? p->z : 0;
64901   }else{
64902     return sqlite3_value_text(pVal);
64903   }
64904 }
64905 SQLITE_API int sqlite3_value_bytes(sqlite3_value *pVal){
64906   return sqlite3ValueBytes(pVal, SQLITE_UTF8);
64907 }
64908 SQLITE_API int sqlite3_value_bytes16(sqlite3_value *pVal){
64909   return sqlite3ValueBytes(pVal, SQLITE_UTF16NATIVE);
64910 }
64911 SQLITE_API double sqlite3_value_double(sqlite3_value *pVal){
64912   return sqlite3VdbeRealValue((Mem*)pVal);
64913 }
64914 SQLITE_API int sqlite3_value_int(sqlite3_value *pVal){
64915   return (int)sqlite3VdbeIntValue((Mem*)pVal);
64916 }
64917 SQLITE_API sqlite_int64 sqlite3_value_int64(sqlite3_value *pVal){
64918   return sqlite3VdbeIntValue((Mem*)pVal);
64919 }
64920 SQLITE_API const unsigned char *sqlite3_value_text(sqlite3_value *pVal){
64921   return (const unsigned char *)sqlite3ValueText(pVal, SQLITE_UTF8);
64922 }
64923 #ifndef SQLITE_OMIT_UTF16
64924 SQLITE_API const void *sqlite3_value_text16(sqlite3_value* pVal){
64925   return sqlite3ValueText(pVal, SQLITE_UTF16NATIVE);
64926 }
64927 SQLITE_API const void *sqlite3_value_text16be(sqlite3_value *pVal){
64928   return sqlite3ValueText(pVal, SQLITE_UTF16BE);
64929 }
64930 SQLITE_API const void *sqlite3_value_text16le(sqlite3_value *pVal){
64931   return sqlite3ValueText(pVal, SQLITE_UTF16LE);
64932 }
64933 #endif /* SQLITE_OMIT_UTF16 */
64934 SQLITE_API int sqlite3_value_type(sqlite3_value* pVal){
64935   return pVal->type;
64936 }
64937 
64938 /**************************** sqlite3_result_  *******************************
64939 ** The following routines are used by user-defined functions to specify
64940 ** the function result.
64941 **
64942 ** The setStrOrError() funtion calls sqlite3VdbeMemSetStr() to store the
64943 ** result as a string or blob but if the string or blob is too large, it
64944 ** then sets the error code to SQLITE_TOOBIG
64945 */
64946 static void setResultStrOrError(
64947   sqlite3_context *pCtx,  /* Function context */
64948   const char *z,          /* String pointer */
64949   int n,                  /* Bytes in string, or negative */
64950   u8 enc,                 /* Encoding of z.  0 for BLOBs */
64951   void (*xDel)(void*)     /* Destructor function */
64952 ){
64953   if( sqlite3VdbeMemSetStr(&pCtx->s, z, n, enc, xDel)==SQLITE_TOOBIG ){
64954     sqlite3_result_error_toobig(pCtx);
64955   }
64956 }
64957 SQLITE_API void sqlite3_result_blob(
64958   sqlite3_context *pCtx,
64959   const void *z,
64960   int n,
64961   void (*xDel)(void *)
64962 ){
64963   assert( n>=0 );
64964   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
64965   setResultStrOrError(pCtx, z, n, 0, xDel);
64966 }
64967 SQLITE_API void sqlite3_result_double(sqlite3_context *pCtx, double rVal){
64968   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
64969   sqlite3VdbeMemSetDouble(&pCtx->s, rVal);
64970 }
64971 SQLITE_API void sqlite3_result_error(sqlite3_context *pCtx, const char *z, int n){
64972   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
64973   pCtx->isError = SQLITE_ERROR;
64974   pCtx->fErrorOrAux = 1;
64975   sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF8, SQLITE_TRANSIENT);
64976 }
64977 #ifndef SQLITE_OMIT_UTF16
64978 SQLITE_API void sqlite3_result_error16(sqlite3_context *pCtx, const void *z, int n){
64979   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
64980   pCtx->isError = SQLITE_ERROR;
64981   pCtx->fErrorOrAux = 1;
64982   sqlite3VdbeMemSetStr(&pCtx->s, z, n, SQLITE_UTF16NATIVE, SQLITE_TRANSIENT);
64983 }
64984 #endif
64985 SQLITE_API void sqlite3_result_int(sqlite3_context *pCtx, int iVal){
64986   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
64987   sqlite3VdbeMemSetInt64(&pCtx->s, (i64)iVal);
64988 }
64989 SQLITE_API void sqlite3_result_int64(sqlite3_context *pCtx, i64 iVal){
64990   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
64991   sqlite3VdbeMemSetInt64(&pCtx->s, iVal);
64992 }
64993 SQLITE_API void sqlite3_result_null(sqlite3_context *pCtx){
64994   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
64995   sqlite3VdbeMemSetNull(&pCtx->s);
64996 }
64997 SQLITE_API void sqlite3_result_text(
64998   sqlite3_context *pCtx,
64999   const char *z,
65000   int n,
65001   void (*xDel)(void *)
65002 ){
65003   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
65004   setResultStrOrError(pCtx, z, n, SQLITE_UTF8, xDel);
65005 }
65006 #ifndef SQLITE_OMIT_UTF16
65007 SQLITE_API void sqlite3_result_text16(
65008   sqlite3_context *pCtx,
65009   const void *z,
65010   int n,
65011   void (*xDel)(void *)
65012 ){
65013   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
65014   setResultStrOrError(pCtx, z, n, SQLITE_UTF16NATIVE, xDel);
65015 }
65016 SQLITE_API void sqlite3_result_text16be(
65017   sqlite3_context *pCtx,
65018   const void *z,
65019   int n,
65020   void (*xDel)(void *)
65021 ){
65022   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
65023   setResultStrOrError(pCtx, z, n, SQLITE_UTF16BE, xDel);
65024 }
65025 SQLITE_API void sqlite3_result_text16le(
65026   sqlite3_context *pCtx,
65027   const void *z,
65028   int n,
65029   void (*xDel)(void *)
65030 ){
65031   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
65032   setResultStrOrError(pCtx, z, n, SQLITE_UTF16LE, xDel);
65033 }
65034 #endif /* SQLITE_OMIT_UTF16 */
65035 SQLITE_API void sqlite3_result_value(sqlite3_context *pCtx, sqlite3_value *pValue){
65036   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
65037   sqlite3VdbeMemCopy(&pCtx->s, pValue);
65038 }
65039 SQLITE_API void sqlite3_result_zeroblob(sqlite3_context *pCtx, int n){
65040   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
65041   sqlite3VdbeMemSetZeroBlob(&pCtx->s, n);
65042 }
65043 SQLITE_API void sqlite3_result_error_code(sqlite3_context *pCtx, int errCode){
65044   pCtx->isError = errCode;
65045   pCtx->fErrorOrAux = 1;
65046   if( pCtx->s.flags & MEM_Null ){
65047     sqlite3VdbeMemSetStr(&pCtx->s, sqlite3ErrStr(errCode), -1,
65048                          SQLITE_UTF8, SQLITE_STATIC);
65049   }
65050 }
65051 
65052 /* Force an SQLITE_TOOBIG error. */
65053 SQLITE_API void sqlite3_result_error_toobig(sqlite3_context *pCtx){
65054   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
65055   pCtx->isError = SQLITE_TOOBIG;
65056   pCtx->fErrorOrAux = 1;
65057   sqlite3VdbeMemSetStr(&pCtx->s, "string or blob too big", -1,
65058                        SQLITE_UTF8, SQLITE_STATIC);
65059 }
65060 
65061 /* An SQLITE_NOMEM error. */
65062 SQLITE_API void sqlite3_result_error_nomem(sqlite3_context *pCtx){
65063   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
65064   sqlite3VdbeMemSetNull(&pCtx->s);
65065   pCtx->isError = SQLITE_NOMEM;
65066   pCtx->fErrorOrAux = 1;
65067   pCtx->s.db->mallocFailed = 1;
65068 }
65069 
65070 /*
65071 ** This function is called after a transaction has been committed. It
65072 ** invokes callbacks registered with sqlite3_wal_hook() as required.
65073 */
65074 static int doWalCallbacks(sqlite3 *db){
65075   int rc = SQLITE_OK;
65076 #ifndef SQLITE_OMIT_WAL
65077   int i;
65078   for(i=0; i<db->nDb; i++){
65079     Btree *pBt = db->aDb[i].pBt;
65080     if( pBt ){
65081       int nEntry = sqlite3PagerWalCallback(sqlite3BtreePager(pBt));
65082       if( db->xWalCallback && nEntry>0 && rc==SQLITE_OK ){
65083         rc = db->xWalCallback(db->pWalArg, db, db->aDb[i].zName, nEntry);
65084       }
65085     }
65086   }
65087 #endif
65088   return rc;
65089 }
65090 
65091 /*
65092 ** Execute the statement pStmt, either until a row of data is ready, the
65093 ** statement is completely executed or an error occurs.
65094 **
65095 ** This routine implements the bulk of the logic behind the sqlite_step()
65096 ** API.  The only thing omitted is the automatic recompile if a
65097 ** schema change has occurred.  That detail is handled by the
65098 ** outer sqlite3_step() wrapper procedure.
65099 */
65100 static int sqlite3Step(Vdbe *p){
65101   sqlite3 *db;
65102   int rc;
65103 
65104   assert(p);
65105   if( p->magic!=VDBE_MAGIC_RUN ){
65106     /* We used to require that sqlite3_reset() be called before retrying
65107     ** sqlite3_step() after any error or after SQLITE_DONE.  But beginning
65108     ** with version 3.7.0, we changed this so that sqlite3_reset() would
65109     ** be called automatically instead of throwing the SQLITE_MISUSE error.
65110     ** This "automatic-reset" change is not technically an incompatibility,
65111     ** since any application that receives an SQLITE_MISUSE is broken by
65112     ** definition.
65113     **
65114     ** Nevertheless, some published applications that were originally written
65115     ** for version 3.6.23 or earlier do in fact depend on SQLITE_MISUSE
65116     ** returns, and those were broken by the automatic-reset change.  As a
65117     ** a work-around, the SQLITE_OMIT_AUTORESET compile-time restores the
65118     ** legacy behavior of returning SQLITE_MISUSE for cases where the
65119     ** previous sqlite3_step() returned something other than a SQLITE_LOCKED
65120     ** or SQLITE_BUSY error.
65121     */
65122 #ifdef SQLITE_OMIT_AUTORESET
65123     if( p->rc==SQLITE_BUSY || p->rc==SQLITE_LOCKED ){
65124       sqlite3_reset((sqlite3_stmt*)p);
65125     }else{
65126       return SQLITE_MISUSE_BKPT;
65127     }
65128 #else
65129     sqlite3_reset((sqlite3_stmt*)p);
65130 #endif
65131   }
65132 
65133   /* Check that malloc() has not failed. If it has, return early. */
65134   db = p->db;
65135   if( db->mallocFailed ){
65136     p->rc = SQLITE_NOMEM;
65137     return SQLITE_NOMEM;
65138   }
65139 
65140   if( p->pc<=0 && p->expired ){
65141     p->rc = SQLITE_SCHEMA;
65142     rc = SQLITE_ERROR;
65143     goto end_of_step;
65144   }
65145   if( p->pc<0 ){
65146     /* If there are no other statements currently running, then
65147     ** reset the interrupt flag.  This prevents a call to sqlite3_interrupt
65148     ** from interrupting a statement that has not yet started.
65149     */
65150     if( db->nVdbeActive==0 ){
65151       db->u1.isInterrupted = 0;
65152     }
65153 
65154     assert( db->nVdbeWrite>0 || db->autoCommit==0
65155         || (db->nDeferredCons==0 && db->nDeferredImmCons==0)
65156     );
65157 
65158 #ifndef SQLITE_OMIT_TRACE
65159     if( db->xProfile && !db->init.busy ){
65160       sqlite3OsCurrentTimeInt64(db->pVfs, &p->startTime);
65161     }
65162 #endif
65163 
65164     db->nVdbeActive++;
65165     if( p->readOnly==0 ) db->nVdbeWrite++;
65166     if( p->bIsReader ) db->nVdbeRead++;
65167     p->pc = 0;
65168   }
65169 #ifndef SQLITE_OMIT_EXPLAIN
65170   if( p->explain ){
65171     rc = sqlite3VdbeList(p);
65172   }else
65173 #endif /* SQLITE_OMIT_EXPLAIN */
65174   {
65175     db->nVdbeExec++;
65176     rc = sqlite3VdbeExec(p);
65177     db->nVdbeExec--;
65178   }
65179 
65180 #ifndef SQLITE_OMIT_TRACE
65181   /* Invoke the profile callback if there is one
65182   */
65183   if( rc!=SQLITE_ROW && db->xProfile && !db->init.busy && p->zSql ){
65184     sqlite3_int64 iNow;
65185     sqlite3OsCurrentTimeInt64(db->pVfs, &iNow);
65186     db->xProfile(db->pProfileArg, p->zSql, (iNow - p->startTime)*1000000);
65187   }
65188 #endif
65189 
65190   if( rc==SQLITE_DONE ){
65191     assert( p->rc==SQLITE_OK );
65192     p->rc = doWalCallbacks(db);
65193     if( p->rc!=SQLITE_OK ){
65194       rc = SQLITE_ERROR;
65195     }
65196   }
65197 
65198   db->errCode = rc;
65199   if( SQLITE_NOMEM==sqlite3ApiExit(p->db, p->rc) ){
65200     p->rc = SQLITE_NOMEM;
65201   }
65202 end_of_step:
65203   /* At this point local variable rc holds the value that should be
65204   ** returned if this statement was compiled using the legacy
65205   ** sqlite3_prepare() interface. According to the docs, this can only
65206   ** be one of the values in the first assert() below. Variable p->rc
65207   ** contains the value that would be returned if sqlite3_finalize()
65208   ** were called on statement p.
65209   */
65210   assert( rc==SQLITE_ROW  || rc==SQLITE_DONE   || rc==SQLITE_ERROR
65211        || rc==SQLITE_BUSY || rc==SQLITE_MISUSE
65212   );
65213   assert( p->rc!=SQLITE_ROW && p->rc!=SQLITE_DONE );
65214   if( p->isPrepareV2 && rc!=SQLITE_ROW && rc!=SQLITE_DONE ){
65215     /* If this statement was prepared using sqlite3_prepare_v2(), and an
65216     ** error has occurred, then return the error code in p->rc to the
65217     ** caller. Set the error code in the database handle to the same value.
65218     */
65219     rc = sqlite3VdbeTransferError(p);
65220   }
65221   return (rc&db->errMask);
65222 }
65223 
65224 /*
65225 ** This is the top-level implementation of sqlite3_step().  Call
65226 ** sqlite3Step() to do most of the work.  If a schema error occurs,
65227 ** call sqlite3Reprepare() and try again.
65228 */
65229 SQLITE_API int sqlite3_step(sqlite3_stmt *pStmt){
65230   int rc = SQLITE_OK;      /* Result from sqlite3Step() */
65231   int rc2 = SQLITE_OK;     /* Result from sqlite3Reprepare() */
65232   Vdbe *v = (Vdbe*)pStmt;  /* the prepared statement */
65233   int cnt = 0;             /* Counter to prevent infinite loop of reprepares */
65234   sqlite3 *db;             /* The database connection */
65235 
65236   if( vdbeSafetyNotNull(v) ){
65237     return SQLITE_MISUSE_BKPT;
65238   }
65239   db = v->db;
65240   sqlite3_mutex_enter(db->mutex);
65241   v->doingRerun = 0;
65242   while( (rc = sqlite3Step(v))==SQLITE_SCHEMA
65243          && cnt++ < SQLITE_MAX_SCHEMA_RETRY
65244          && (rc2 = rc = sqlite3Reprepare(v))==SQLITE_OK ){
65245     sqlite3_reset(pStmt);
65246     v->doingRerun = 1;
65247     assert( v->expired==0 );
65248   }
65249   if( rc2!=SQLITE_OK ){
65250     /* This case occurs after failing to recompile an sql statement.
65251     ** The error message from the SQL compiler has already been loaded
65252     ** into the database handle. This block copies the error message
65253     ** from the database handle into the statement and sets the statement
65254     ** program counter to 0 to ensure that when the statement is
65255     ** finalized or reset the parser error message is available via
65256     ** sqlite3_errmsg() and sqlite3_errcode().
65257     */
65258     const char *zErr = (const char *)sqlite3_value_text(db->pErr);
65259     assert( zErr!=0 || db->mallocFailed );
65260     sqlite3DbFree(db, v->zErrMsg);
65261     if( !db->mallocFailed ){
65262       v->zErrMsg = sqlite3DbStrDup(db, zErr);
65263       v->rc = rc2;
65264     } else {
65265       v->zErrMsg = 0;
65266       v->rc = rc = SQLITE_NOMEM;
65267     }
65268   }
65269   rc = sqlite3ApiExit(db, rc);
65270   sqlite3_mutex_leave(db->mutex);
65271   return rc;
65272 }
65273 
65274 
65275 /*
65276 ** Extract the user data from a sqlite3_context structure and return a
65277 ** pointer to it.
65278 */
65279 SQLITE_API void *sqlite3_user_data(sqlite3_context *p){
65280   assert( p && p->pFunc );
65281   return p->pFunc->pUserData;
65282 }
65283 
65284 /*
65285 ** Extract the user data from a sqlite3_context structure and return a
65286 ** pointer to it.
65287 **
65288 ** IMPLEMENTATION-OF: R-46798-50301 The sqlite3_context_db_handle() interface
65289 ** returns a copy of the pointer to the database connection (the 1st
65290 ** parameter) of the sqlite3_create_function() and
65291 ** sqlite3_create_function16() routines that originally registered the
65292 ** application defined function.
65293 */
65294 SQLITE_API sqlite3 *sqlite3_context_db_handle(sqlite3_context *p){
65295   assert( p && p->pFunc );
65296   return p->s.db;
65297 }
65298 
65299 /*
65300 ** Return the current time for a statement
65301 */
65302 SQLITE_PRIVATE sqlite3_int64 sqlite3StmtCurrentTime(sqlite3_context *p){
65303   Vdbe *v = p->pVdbe;
65304   int rc;
65305   if( v->iCurrentTime==0 ){
65306     rc = sqlite3OsCurrentTimeInt64(p->s.db->pVfs, &v->iCurrentTime);
65307     if( rc ) v->iCurrentTime = 0;
65308   }
65309   return v->iCurrentTime;
65310 }
65311 
65312 /*
65313 ** The following is the implementation of an SQL function that always
65314 ** fails with an error message stating that the function is used in the
65315 ** wrong context.  The sqlite3_overload_function() API might construct
65316 ** SQL function that use this routine so that the functions will exist
65317 ** for name resolution but are actually overloaded by the xFindFunction
65318 ** method of virtual tables.
65319 */
65320 SQLITE_PRIVATE void sqlite3InvalidFunction(
65321   sqlite3_context *context,  /* The function calling context */
65322   int NotUsed,               /* Number of arguments to the function */
65323   sqlite3_value **NotUsed2   /* Value of each argument */
65324 ){
65325   const char *zName = context->pFunc->zName;
65326   char *zErr;
65327   UNUSED_PARAMETER2(NotUsed, NotUsed2);
65328   zErr = sqlite3_mprintf(
65329       "unable to use function %s in the requested context", zName);
65330   sqlite3_result_error(context, zErr, -1);
65331   sqlite3_free(zErr);
65332 }
65333 
65334 /*
65335 ** Allocate or return the aggregate context for a user function.  A new
65336 ** context is allocated on the first call.  Subsequent calls return the
65337 ** same context that was returned on prior calls.
65338 */
65339 SQLITE_API void *sqlite3_aggregate_context(sqlite3_context *p, int nByte){
65340   Mem *pMem;
65341   assert( p && p->pFunc && p->pFunc->xStep );
65342   assert( sqlite3_mutex_held(p->s.db->mutex) );
65343   pMem = p->pMem;
65344   testcase( nByte<0 );
65345   if( (pMem->flags & MEM_Agg)==0 ){
65346     if( nByte<=0 ){
65347       sqlite3VdbeMemReleaseExternal(pMem);
65348       pMem->flags = MEM_Null;
65349       pMem->z = 0;
65350     }else{
65351       sqlite3VdbeMemGrow(pMem, nByte, 0);
65352       pMem->flags = MEM_Agg;
65353       pMem->u.pDef = p->pFunc;
65354       if( pMem->z ){
65355         memset(pMem->z, 0, nByte);
65356       }
65357     }
65358   }
65359   return (void*)pMem->z;
65360 }
65361 
65362 /*
65363 ** Return the auxilary data pointer, if any, for the iArg'th argument to
65364 ** the user-function defined by pCtx.
65365 */
65366 SQLITE_API void *sqlite3_get_auxdata(sqlite3_context *pCtx, int iArg){
65367   AuxData *pAuxData;
65368 
65369   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
65370   for(pAuxData=pCtx->pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNext){
65371     if( pAuxData->iOp==pCtx->iOp && pAuxData->iArg==iArg ) break;
65372   }
65373 
65374   return (pAuxData ? pAuxData->pAux : 0);
65375 }
65376 
65377 /*
65378 ** Set the auxilary data pointer and delete function, for the iArg'th
65379 ** argument to the user-function defined by pCtx. Any previous value is
65380 ** deleted by calling the delete function specified when it was set.
65381 */
65382 SQLITE_API void sqlite3_set_auxdata(
65383   sqlite3_context *pCtx,
65384   int iArg,
65385   void *pAux,
65386   void (*xDelete)(void*)
65387 ){
65388   AuxData *pAuxData;
65389   Vdbe *pVdbe = pCtx->pVdbe;
65390 
65391   assert( sqlite3_mutex_held(pCtx->s.db->mutex) );
65392   if( iArg<0 ) goto failed;
65393 
65394   for(pAuxData=pVdbe->pAuxData; pAuxData; pAuxData=pAuxData->pNext){
65395     if( pAuxData->iOp==pCtx->iOp && pAuxData->iArg==iArg ) break;
65396   }
65397   if( pAuxData==0 ){
65398     pAuxData = sqlite3DbMallocZero(pVdbe->db, sizeof(AuxData));
65399     if( !pAuxData ) goto failed;
65400     pAuxData->iOp = pCtx->iOp;
65401     pAuxData->iArg = iArg;
65402     pAuxData->pNext = pVdbe->pAuxData;
65403     pVdbe->pAuxData = pAuxData;
65404     if( pCtx->fErrorOrAux==0 ){
65405       pCtx->isError = 0;
65406       pCtx->fErrorOrAux = 1;
65407     }
65408   }else if( pAuxData->xDelete ){
65409     pAuxData->xDelete(pAuxData->pAux);
65410   }
65411 
65412   pAuxData->pAux = pAux;
65413   pAuxData->xDelete = xDelete;
65414   return;
65415 
65416 failed:
65417   if( xDelete ){
65418     xDelete(pAux);
65419   }
65420 }
65421 
65422 #ifndef SQLITE_OMIT_DEPRECATED
65423 /*
65424 ** Return the number of times the Step function of a aggregate has been
65425 ** called.
65426 **
65427 ** This function is deprecated.  Do not use it for new code.  It is
65428 ** provide only to avoid breaking legacy code.  New aggregate function
65429 ** implementations should keep their own counts within their aggregate
65430 ** context.
65431 */
65432 SQLITE_API int sqlite3_aggregate_count(sqlite3_context *p){
65433   assert( p && p->pMem && p->pFunc && p->pFunc->xStep );
65434   return p->pMem->n;
65435 }
65436 #endif
65437 
65438 /*
65439 ** Return the number of columns in the result set for the statement pStmt.
65440 */
65441 SQLITE_API int sqlite3_column_count(sqlite3_stmt *pStmt){
65442   Vdbe *pVm = (Vdbe *)pStmt;
65443   return pVm ? pVm->nResColumn : 0;
65444 }
65445 
65446 /*
65447 ** Return the number of values available from the current row of the
65448 ** currently executing statement pStmt.
65449 */
65450 SQLITE_API int sqlite3_data_count(sqlite3_stmt *pStmt){
65451   Vdbe *pVm = (Vdbe *)pStmt;
65452   if( pVm==0 || pVm->pResultSet==0 ) return 0;
65453   return pVm->nResColumn;
65454 }
65455 
65456 
65457 /*
65458 ** Check to see if column iCol of the given statement is valid.  If
65459 ** it is, return a pointer to the Mem for the value of that column.
65460 ** If iCol is not valid, return a pointer to a Mem which has a value
65461 ** of NULL.
65462 */
65463 static Mem *columnMem(sqlite3_stmt *pStmt, int i){
65464   Vdbe *pVm;
65465   Mem *pOut;
65466 
65467   pVm = (Vdbe *)pStmt;
65468   if( pVm && pVm->pResultSet!=0 && i<pVm->nResColumn && i>=0 ){
65469     sqlite3_mutex_enter(pVm->db->mutex);
65470     pOut = &pVm->pResultSet[i];
65471   }else{
65472     /* If the value passed as the second argument is out of range, return
65473     ** a pointer to the following static Mem object which contains the
65474     ** value SQL NULL. Even though the Mem structure contains an element
65475     ** of type i64, on certain architectures (x86) with certain compiler
65476     ** switches (-Os), gcc may align this Mem object on a 4-byte boundary
65477     ** instead of an 8-byte one. This all works fine, except that when
65478     ** running with SQLITE_DEBUG defined the SQLite code sometimes assert()s
65479     ** that a Mem structure is located on an 8-byte boundary. To prevent
65480     ** these assert()s from failing, when building with SQLITE_DEBUG defined
65481     ** using gcc, we force nullMem to be 8-byte aligned using the magical
65482     ** __attribute__((aligned(8))) macro.  */
65483     static const Mem nullMem
65484 #if defined(SQLITE_DEBUG) && defined(__GNUC__)
65485       __attribute__((aligned(8)))
65486 #endif
65487       = {0, "", (double)0, {0}, 0, MEM_Null, SQLITE_NULL, 0,
65488 #ifdef SQLITE_DEBUG
65489          0, 0,  /* pScopyFrom, pFiller */
65490 #endif
65491          0, 0 };
65492 
65493     if( pVm && ALWAYS(pVm->db) ){
65494       sqlite3_mutex_enter(pVm->db->mutex);
65495       sqlite3Error(pVm->db, SQLITE_RANGE, 0);
65496     }
65497     pOut = (Mem*)&nullMem;
65498   }
65499   return pOut;
65500 }
65501 
65502 /*
65503 ** This function is called after invoking an sqlite3_value_XXX function on a
65504 ** column value (i.e. a value returned by evaluating an SQL expression in the
65505 ** select list of a SELECT statement) that may cause a malloc() failure. If
65506 ** malloc() has failed, the threads mallocFailed flag is cleared and the result
65507 ** code of statement pStmt set to SQLITE_NOMEM.
65508 **
65509 ** Specifically, this is called from within:
65510 **
65511 **     sqlite3_column_int()
65512 **     sqlite3_column_int64()
65513 **     sqlite3_column_text()
65514 **     sqlite3_column_text16()
65515 **     sqlite3_column_real()
65516 **     sqlite3_column_bytes()
65517 **     sqlite3_column_bytes16()
65518 **     sqiite3_column_blob()
65519 */
65520 static void columnMallocFailure(sqlite3_stmt *pStmt)
65521 {
65522   /* If malloc() failed during an encoding conversion within an
65523   ** sqlite3_column_XXX API, then set the return code of the statement to
65524   ** SQLITE_NOMEM. The next call to _step() (if any) will return SQLITE_ERROR
65525   ** and _finalize() will return NOMEM.
65526   */
65527   Vdbe *p = (Vdbe *)pStmt;
65528   if( p ){
65529     p->rc = sqlite3ApiExit(p->db, p->rc);
65530     sqlite3_mutex_leave(p->db->mutex);
65531   }
65532 }
65533 
65534 /**************************** sqlite3_column_  *******************************
65535 ** The following routines are used to access elements of the current row
65536 ** in the result set.
65537 */
65538 SQLITE_API const void *sqlite3_column_blob(sqlite3_stmt *pStmt, int i){
65539   const void *val;
65540   val = sqlite3_value_blob( columnMem(pStmt,i) );
65541   /* Even though there is no encoding conversion, value_blob() might
65542   ** need to call malloc() to expand the result of a zeroblob()
65543   ** expression.
65544   */
65545   columnMallocFailure(pStmt);
65546   return val;
65547 }
65548 SQLITE_API int sqlite3_column_bytes(sqlite3_stmt *pStmt, int i){
65549   int val = sqlite3_value_bytes( columnMem(pStmt,i) );
65550   columnMallocFailure(pStmt);
65551   return val;
65552 }
65553 SQLITE_API int sqlite3_column_bytes16(sqlite3_stmt *pStmt, int i){
65554   int val = sqlite3_value_bytes16( columnMem(pStmt,i) );
65555   columnMallocFailure(pStmt);
65556   return val;
65557 }
65558 SQLITE_API double sqlite3_column_double(sqlite3_stmt *pStmt, int i){
65559   double val = sqlite3_value_double( columnMem(pStmt,i) );
65560   columnMallocFailure(pStmt);
65561   return val;
65562 }
65563 SQLITE_API int sqlite3_column_int(sqlite3_stmt *pStmt, int i){
65564   int val = sqlite3_value_int( columnMem(pStmt,i) );
65565   columnMallocFailure(pStmt);
65566   return val;
65567 }
65568 SQLITE_API sqlite_int64 sqlite3_column_int64(sqlite3_stmt *pStmt, int i){
65569   sqlite_int64 val = sqlite3_value_int64( columnMem(pStmt,i) );
65570   columnMallocFailure(pStmt);
65571   return val;
65572 }
65573 SQLITE_API const unsigned char *sqlite3_column_text(sqlite3_stmt *pStmt, int i){
65574   const unsigned char *val = sqlite3_value_text( columnMem(pStmt,i) );
65575   columnMallocFailure(pStmt);
65576   return val;
65577 }
65578 SQLITE_API sqlite3_value *sqlite3_column_value(sqlite3_stmt *pStmt, int i){
65579   Mem *pOut = columnMem(pStmt, i);
65580   if( pOut->flags&MEM_Static ){
65581     pOut->flags &= ~MEM_Static;
65582     pOut->flags |= MEM_Ephem;
65583   }
65584   columnMallocFailure(pStmt);
65585   return (sqlite3_value *)pOut;
65586 }
65587 #ifndef SQLITE_OMIT_UTF16
65588 SQLITE_API const void *sqlite3_column_text16(sqlite3_stmt *pStmt, int i){
65589   const void *val = sqlite3_value_text16( columnMem(pStmt,i) );
65590   columnMallocFailure(pStmt);
65591   return val;
65592 }
65593 #endif /* SQLITE_OMIT_UTF16 */
65594 SQLITE_API int sqlite3_column_type(sqlite3_stmt *pStmt, int i){
65595   int iType = sqlite3_value_type( columnMem(pStmt,i) );
65596   columnMallocFailure(pStmt);
65597   return iType;
65598 }
65599 
65600 /*
65601 ** Convert the N-th element of pStmt->pColName[] into a string using
65602 ** xFunc() then return that string.  If N is out of range, return 0.
65603 **
65604 ** There are up to 5 names for each column.  useType determines which
65605 ** name is returned.  Here are the names:
65606 **
65607 **    0      The column name as it should be displayed for output
65608 **    1      The datatype name for the column
65609 **    2      The name of the database that the column derives from
65610 **    3      The name of the table that the column derives from
65611 **    4      The name of the table column that the result column derives from
65612 **
65613 ** If the result is not a simple column reference (if it is an expression
65614 ** or a constant) then useTypes 2, 3, and 4 return NULL.
65615 */
65616 static const void *columnName(
65617   sqlite3_stmt *pStmt,
65618   int N,
65619   const void *(*xFunc)(Mem*),
65620   int useType
65621 ){
65622   const void *ret = 0;
65623   Vdbe *p = (Vdbe *)pStmt;
65624   int n;
65625   sqlite3 *db = p->db;
65626 
65627   assert( db!=0 );
65628   n = sqlite3_column_count(pStmt);
65629   if( N<n && N>=0 ){
65630     N += useType*n;
65631     sqlite3_mutex_enter(db->mutex);
65632     assert( db->mallocFailed==0 );
65633     ret = xFunc(&p->aColName[N]);
65634      /* A malloc may have failed inside of the xFunc() call. If this
65635     ** is the case, clear the mallocFailed flag and return NULL.
65636     */
65637     if( db->mallocFailed ){
65638       db->mallocFailed = 0;
65639       ret = 0;
65640     }
65641     sqlite3_mutex_leave(db->mutex);
65642   }
65643   return ret;
65644 }
65645 
65646 /*
65647 ** Return the name of the Nth column of the result set returned by SQL
65648 ** statement pStmt.
65649 */
65650 SQLITE_API const char *sqlite3_column_name(sqlite3_stmt *pStmt, int N){
65651   return columnName(
65652       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_NAME);
65653 }
65654 #ifndef SQLITE_OMIT_UTF16
65655 SQLITE_API const void *sqlite3_column_name16(sqlite3_stmt *pStmt, int N){
65656   return columnName(
65657       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_NAME);
65658 }
65659 #endif
65660 
65661 /*
65662 ** Constraint:  If you have ENABLE_COLUMN_METADATA then you must
65663 ** not define OMIT_DECLTYPE.
65664 */
65665 #if defined(SQLITE_OMIT_DECLTYPE) && defined(SQLITE_ENABLE_COLUMN_METADATA)
65666 # error "Must not define both SQLITE_OMIT_DECLTYPE \
65667          and SQLITE_ENABLE_COLUMN_METADATA"
65668 #endif
65669 
65670 #ifndef SQLITE_OMIT_DECLTYPE
65671 /*
65672 ** Return the column declaration type (if applicable) of the 'i'th column
65673 ** of the result set of SQL statement pStmt.
65674 */
65675 SQLITE_API const char *sqlite3_column_decltype(sqlite3_stmt *pStmt, int N){
65676   return columnName(
65677       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DECLTYPE);
65678 }
65679 #ifndef SQLITE_OMIT_UTF16
65680 SQLITE_API const void *sqlite3_column_decltype16(sqlite3_stmt *pStmt, int N){
65681   return columnName(
65682       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DECLTYPE);
65683 }
65684 #endif /* SQLITE_OMIT_UTF16 */
65685 #endif /* SQLITE_OMIT_DECLTYPE */
65686 
65687 #ifdef SQLITE_ENABLE_COLUMN_METADATA
65688 /*
65689 ** Return the name of the database from which a result column derives.
65690 ** NULL is returned if the result column is an expression or constant or
65691 ** anything else which is not an unabiguous reference to a database column.
65692 */
65693 SQLITE_API const char *sqlite3_column_database_name(sqlite3_stmt *pStmt, int N){
65694   return columnName(
65695       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_DATABASE);
65696 }
65697 #ifndef SQLITE_OMIT_UTF16
65698 SQLITE_API const void *sqlite3_column_database_name16(sqlite3_stmt *pStmt, int N){
65699   return columnName(
65700       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_DATABASE);
65701 }
65702 #endif /* SQLITE_OMIT_UTF16 */
65703 
65704 /*
65705 ** Return the name of the table from which a result column derives.
65706 ** NULL is returned if the result column is an expression or constant or
65707 ** anything else which is not an unabiguous reference to a database column.
65708 */
65709 SQLITE_API const char *sqlite3_column_table_name(sqlite3_stmt *pStmt, int N){
65710   return columnName(
65711       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_TABLE);
65712 }
65713 #ifndef SQLITE_OMIT_UTF16
65714 SQLITE_API const void *sqlite3_column_table_name16(sqlite3_stmt *pStmt, int N){
65715   return columnName(
65716       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_TABLE);
65717 }
65718 #endif /* SQLITE_OMIT_UTF16 */
65719 
65720 /*
65721 ** Return the name of the table column from which a result column derives.
65722 ** NULL is returned if the result column is an expression or constant or
65723 ** anything else which is not an unabiguous reference to a database column.
65724 */
65725 SQLITE_API const char *sqlite3_column_origin_name(sqlite3_stmt *pStmt, int N){
65726   return columnName(
65727       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text, COLNAME_COLUMN);
65728 }
65729 #ifndef SQLITE_OMIT_UTF16
65730 SQLITE_API const void *sqlite3_column_origin_name16(sqlite3_stmt *pStmt, int N){
65731   return columnName(
65732       pStmt, N, (const void*(*)(Mem*))sqlite3_value_text16, COLNAME_COLUMN);
65733 }
65734 #endif /* SQLITE_OMIT_UTF16 */
65735 #endif /* SQLITE_ENABLE_COLUMN_METADATA */
65736 
65737 
65738 /******************************* sqlite3_bind_  ***************************
65739 **
65740 ** Routines used to attach values to wildcards in a compiled SQL statement.
65741 */
65742 /*
65743 ** Unbind the value bound to variable i in virtual machine p. This is the
65744 ** the same as binding a NULL value to the column. If the "i" parameter is
65745 ** out of range, then SQLITE_RANGE is returned. Othewise SQLITE_OK.
65746 **
65747 ** A successful evaluation of this routine acquires the mutex on p.
65748 ** the mutex is released if any kind of error occurs.
65749 **
65750 ** The error code stored in database p->db is overwritten with the return
65751 ** value in any case.
65752 */
65753 static int vdbeUnbind(Vdbe *p, int i){
65754   Mem *pVar;
65755   if( vdbeSafetyNotNull(p) ){
65756     return SQLITE_MISUSE_BKPT;
65757   }
65758   sqlite3_mutex_enter(p->db->mutex);
65759   if( p->magic!=VDBE_MAGIC_RUN || p->pc>=0 ){
65760     sqlite3Error(p->db, SQLITE_MISUSE, 0);
65761     sqlite3_mutex_leave(p->db->mutex);
65762     sqlite3_log(SQLITE_MISUSE,
65763         "bind on a busy prepared statement: [%s]", p->zSql);
65764     return SQLITE_MISUSE_BKPT;
65765   }
65766   if( i<1 || i>p->nVar ){
65767     sqlite3Error(p->db, SQLITE_RANGE, 0);
65768     sqlite3_mutex_leave(p->db->mutex);
65769     return SQLITE_RANGE;
65770   }
65771   i--;
65772   pVar = &p->aVar[i];
65773   sqlite3VdbeMemRelease(pVar);
65774   pVar->flags = MEM_Null;
65775   sqlite3Error(p->db, SQLITE_OK, 0);
65776 
65777   /* If the bit corresponding to this variable in Vdbe.expmask is set, then
65778   ** binding a new value to this variable invalidates the current query plan.
65779   **
65780   ** IMPLEMENTATION-OF: R-48440-37595 If the specific value bound to host
65781   ** parameter in the WHERE clause might influence the choice of query plan
65782   ** for a statement, then the statement will be automatically recompiled,
65783   ** as if there had been a schema change, on the first sqlite3_step() call
65784   ** following any change to the bindings of that parameter.
65785   */
65786   if( p->isPrepareV2 &&
65787      ((i<32 && p->expmask & ((u32)1 << i)) || p->expmask==0xffffffff)
65788   ){
65789     p->expired = 1;
65790   }
65791   return SQLITE_OK;
65792 }
65793 
65794 /*
65795 ** Bind a text or BLOB value.
65796 */
65797 static int bindText(
65798   sqlite3_stmt *pStmt,   /* The statement to bind against */
65799   int i,                 /* Index of the parameter to bind */
65800   const void *zData,     /* Pointer to the data to be bound */
65801   int nData,             /* Number of bytes of data to be bound */
65802   void (*xDel)(void*),   /* Destructor for the data */
65803   u8 encoding            /* Encoding for the data */
65804 ){
65805   Vdbe *p = (Vdbe *)pStmt;
65806   Mem *pVar;
65807   int rc;
65808 
65809   rc = vdbeUnbind(p, i);
65810   if( rc==SQLITE_OK ){
65811     if( zData!=0 ){
65812       pVar = &p->aVar[i-1];
65813       rc = sqlite3VdbeMemSetStr(pVar, zData, nData, encoding, xDel);
65814       if( rc==SQLITE_OK && encoding!=0 ){
65815         rc = sqlite3VdbeChangeEncoding(pVar, ENC(p->db));
65816       }
65817       sqlite3Error(p->db, rc, 0);
65818       rc = sqlite3ApiExit(p->db, rc);
65819     }
65820     sqlite3_mutex_leave(p->db->mutex);
65821   }else if( xDel!=SQLITE_STATIC && xDel!=SQLITE_TRANSIENT ){
65822     xDel((void*)zData);
65823   }
65824   return rc;
65825 }
65826 
65827 
65828 /*
65829 ** Bind a blob value to an SQL statement variable.
65830 */
65831 SQLITE_API int sqlite3_bind_blob(
65832   sqlite3_stmt *pStmt,
65833   int i,
65834   const void *zData,
65835   int nData,
65836   void (*xDel)(void*)
65837 ){
65838   return bindText(pStmt, i, zData, nData, xDel, 0);
65839 }
65840 SQLITE_API int sqlite3_bind_double(sqlite3_stmt *pStmt, int i, double rValue){
65841   int rc;
65842   Vdbe *p = (Vdbe *)pStmt;
65843   rc = vdbeUnbind(p, i);
65844   if( rc==SQLITE_OK ){
65845     sqlite3VdbeMemSetDouble(&p->aVar[i-1], rValue);
65846     sqlite3_mutex_leave(p->db->mutex);
65847   }
65848   return rc;
65849 }
65850 SQLITE_API int sqlite3_bind_int(sqlite3_stmt *p, int i, int iValue){
65851   return sqlite3_bind_int64(p, i, (i64)iValue);
65852 }
65853 SQLITE_API int sqlite3_bind_int64(sqlite3_stmt *pStmt, int i, sqlite_int64 iValue){
65854   int rc;
65855   Vdbe *p = (Vdbe *)pStmt;
65856   rc = vdbeUnbind(p, i);
65857   if( rc==SQLITE_OK ){
65858     sqlite3VdbeMemSetInt64(&p->aVar[i-1], iValue);
65859     sqlite3_mutex_leave(p->db->mutex);
65860   }
65861   return rc;
65862 }
65863 SQLITE_API int sqlite3_bind_null(sqlite3_stmt *pStmt, int i){
65864   int rc;
65865   Vdbe *p = (Vdbe*)pStmt;
65866   rc = vdbeUnbind(p, i);
65867   if( rc==SQLITE_OK ){
65868     sqlite3_mutex_leave(p->db->mutex);
65869   }
65870   return rc;
65871 }
65872 SQLITE_API int sqlite3_bind_text(
65873   sqlite3_stmt *pStmt,
65874   int i,
65875   const char *zData,
65876   int nData,
65877   void (*xDel)(void*)
65878 ){
65879   return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF8);
65880 }
65881 #ifndef SQLITE_OMIT_UTF16
65882 SQLITE_API int sqlite3_bind_text16(
65883   sqlite3_stmt *pStmt,
65884   int i,
65885   const void *zData,
65886   int nData,
65887   void (*xDel)(void*)
65888 ){
65889   return bindText(pStmt, i, zData, nData, xDel, SQLITE_UTF16NATIVE);
65890 }
65891 #endif /* SQLITE_OMIT_UTF16 */
65892 SQLITE_API int sqlite3_bind_value(sqlite3_stmt *pStmt, int i, const sqlite3_value *pValue){
65893   int rc;
65894   switch( pValue->type ){
65895     case SQLITE_INTEGER: {
65896       rc = sqlite3_bind_int64(pStmt, i, pValue->u.i);
65897       break;
65898     }
65899     case SQLITE_FLOAT: {
65900       rc = sqlite3_bind_double(pStmt, i, pValue->r);
65901       break;
65902     }
65903     case SQLITE_BLOB: {
65904       if( pValue->flags & MEM_Zero ){
65905         rc = sqlite3_bind_zeroblob(pStmt, i, pValue->u.nZero);
65906       }else{
65907         rc = sqlite3_bind_blob(pStmt, i, pValue->z, pValue->n,SQLITE_TRANSIENT);
65908       }
65909       break;
65910     }
65911     case SQLITE_TEXT: {
65912       rc = bindText(pStmt,i,  pValue->z, pValue->n, SQLITE_TRANSIENT,
65913                               pValue->enc);
65914       break;
65915     }
65916     default: {
65917       rc = sqlite3_bind_null(pStmt, i);
65918       break;
65919     }
65920   }
65921   return rc;
65922 }
65923 SQLITE_API int sqlite3_bind_zeroblob(sqlite3_stmt *pStmt, int i, int n){
65924   int rc;
65925   Vdbe *p = (Vdbe *)pStmt;
65926   rc = vdbeUnbind(p, i);
65927   if( rc==SQLITE_OK ){
65928     sqlite3VdbeMemSetZeroBlob(&p->aVar[i-1], n);
65929     sqlite3_mutex_leave(p->db->mutex);
65930   }
65931   return rc;
65932 }
65933 
65934 /*
65935 ** Return the number of wildcards that can be potentially bound to.
65936 ** This routine is added to support DBD::SQLite.
65937 */
65938 SQLITE_API int sqlite3_bind_parameter_count(sqlite3_stmt *pStmt){
65939   Vdbe *p = (Vdbe*)pStmt;
65940   return p ? p->nVar : 0;
65941 }
65942 
65943 /*
65944 ** Return the name of a wildcard parameter.  Return NULL if the index
65945 ** is out of range or if the wildcard is unnamed.
65946 **
65947 ** The result is always UTF-8.
65948 */
65949 SQLITE_API const char *sqlite3_bind_parameter_name(sqlite3_stmt *pStmt, int i){
65950   Vdbe *p = (Vdbe*)pStmt;
65951   if( p==0 || i<1 || i>p->nzVar ){
65952     return 0;
65953   }
65954   return p->azVar[i-1];
65955 }
65956 
65957 /*
65958 ** Given a wildcard parameter name, return the index of the variable
65959 ** with that name.  If there is no variable with the given name,
65960 ** return 0.
65961 */
65962 SQLITE_PRIVATE int sqlite3VdbeParameterIndex(Vdbe *p, const char *zName, int nName){
65963   int i;
65964   if( p==0 ){
65965     return 0;
65966   }
65967   if( zName ){
65968     for(i=0; i<p->nzVar; i++){
65969       const char *z = p->azVar[i];
65970       if( z && strncmp(z,zName,nName)==0 && z[nName]==0 ){
65971         return i+1;
65972       }
65973     }
65974   }
65975   return 0;
65976 }
65977 SQLITE_API int sqlite3_bind_parameter_index(sqlite3_stmt *pStmt, const char *zName){
65978   return sqlite3VdbeParameterIndex((Vdbe*)pStmt, zName, sqlite3Strlen30(zName));
65979 }
65980 
65981 /*
65982 ** Transfer all bindings from the first statement over to the second.
65983 */
65984 SQLITE_PRIVATE int sqlite3TransferBindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){
65985   Vdbe *pFrom = (Vdbe*)pFromStmt;
65986   Vdbe *pTo = (Vdbe*)pToStmt;
65987   int i;
65988   assert( pTo->db==pFrom->db );
65989   assert( pTo->nVar==pFrom->nVar );
65990   sqlite3_mutex_enter(pTo->db->mutex);
65991   for(i=0; i<pFrom->nVar; i++){
65992     sqlite3VdbeMemMove(&pTo->aVar[i], &pFrom->aVar[i]);
65993   }
65994   sqlite3_mutex_leave(pTo->db->mutex);
65995   return SQLITE_OK;
65996 }
65997 
65998 #ifndef SQLITE_OMIT_DEPRECATED
65999 /*
66000 ** Deprecated external interface.  Internal/core SQLite code
66001 ** should call sqlite3TransferBindings.
66002 **
66003 ** Is is misuse to call this routine with statements from different
66004 ** database connections.  But as this is a deprecated interface, we
66005 ** will not bother to check for that condition.
66006 **
66007 ** If the two statements contain a different number of bindings, then
66008 ** an SQLITE_ERROR is returned.  Nothing else can go wrong, so otherwise
66009 ** SQLITE_OK is returned.
66010 */
66011 SQLITE_API int sqlite3_transfer_bindings(sqlite3_stmt *pFromStmt, sqlite3_stmt *pToStmt){
66012   Vdbe *pFrom = (Vdbe*)pFromStmt;
66013   Vdbe *pTo = (Vdbe*)pToStmt;
66014   if( pFrom->nVar!=pTo->nVar ){
66015     return SQLITE_ERROR;
66016   }
66017   if( pTo->isPrepareV2 && pTo->expmask ){
66018     pTo->expired = 1;
66019   }
66020   if( pFrom->isPrepareV2 && pFrom->expmask ){
66021     pFrom->expired = 1;
66022   }
66023   return sqlite3TransferBindings(pFromStmt, pToStmt);
66024 }
66025 #endif
66026 
66027 /*
66028 ** Return the sqlite3* database handle to which the prepared statement given
66029 ** in the argument belongs.  This is the same database handle that was
66030 ** the first argument to the sqlite3_prepare() that was used to create
66031 ** the statement in the first place.
66032 */
66033 SQLITE_API sqlite3 *sqlite3_db_handle(sqlite3_stmt *pStmt){
66034   return pStmt ? ((Vdbe*)pStmt)->db : 0;
66035 }
66036 
66037 /*
66038 ** Return true if the prepared statement is guaranteed to not modify the
66039 ** database.
66040 */
66041 SQLITE_API int sqlite3_stmt_readonly(sqlite3_stmt *pStmt){
66042   return pStmt ? ((Vdbe*)pStmt)->readOnly : 1;
66043 }
66044 
66045 /*
66046 ** Return true if the prepared statement is in need of being reset.
66047 */
66048 SQLITE_API int sqlite3_stmt_busy(sqlite3_stmt *pStmt){
66049   Vdbe *v = (Vdbe*)pStmt;
66050   return v!=0 && v->pc>0 && v->magic==VDBE_MAGIC_RUN;
66051 }
66052 
66053 /*
66054 ** Return a pointer to the next prepared statement after pStmt associated
66055 ** with database connection pDb.  If pStmt is NULL, return the first
66056 ** prepared statement for the database connection.  Return NULL if there
66057 ** are no more.
66058 */
66059 SQLITE_API sqlite3_stmt *sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt){
66060   sqlite3_stmt *pNext;
66061   sqlite3_mutex_enter(pDb->mutex);
66062   if( pStmt==0 ){
66063     pNext = (sqlite3_stmt*)pDb->pVdbe;
66064   }else{
66065     pNext = (sqlite3_stmt*)((Vdbe*)pStmt)->pNext;
66066   }
66067   sqlite3_mutex_leave(pDb->mutex);
66068   return pNext;
66069 }
66070 
66071 /*
66072 ** Return the value of a status counter for a prepared statement
66073 */
66074 SQLITE_API int sqlite3_stmt_status(sqlite3_stmt *pStmt, int op, int resetFlag){
66075   Vdbe *pVdbe = (Vdbe*)pStmt;
66076   u32 v = pVdbe->aCounter[op];
66077   if( resetFlag ) pVdbe->aCounter[op] = 0;
66078   return (int)v;
66079 }
66080 
66081 /************** End of vdbeapi.c *********************************************/
66082 /************** Begin file vdbetrace.c ***************************************/
66083 /*
66084 ** 2009 November 25
66085 **
66086 ** The author disclaims copyright to this source code.  In place of
66087 ** a legal notice, here is a blessing:
66088 **
66089 **    May you do good and not evil.
66090 **    May you find forgiveness for yourself and forgive others.
66091 **    May you share freely, never taking more than you give.
66092 **
66093 *************************************************************************
66094 **
66095 ** This file contains code used to insert the values of host parameters
66096 ** (aka "wildcards") into the SQL text output by sqlite3_trace().
66097 **
66098 ** The Vdbe parse-tree explainer is also found here.
66099 */
66100 
66101 #ifndef SQLITE_OMIT_TRACE
66102 
66103 /*
66104 ** zSql is a zero-terminated string of UTF-8 SQL text.  Return the number of
66105 ** bytes in this text up to but excluding the first character in
66106 ** a host parameter.  If the text contains no host parameters, return
66107 ** the total number of bytes in the text.
66108 */
66109 static int findNextHostParameter(const char *zSql, int *pnToken){
66110   int tokenType;
66111   int nTotal = 0;
66112   int n;
66113 
66114   *pnToken = 0;
66115   while( zSql[0] ){
66116     n = sqlite3GetToken((u8*)zSql, &tokenType);
66117     assert( n>0 && tokenType!=TK_ILLEGAL );
66118     if( tokenType==TK_VARIABLE ){
66119       *pnToken = n;
66120       break;
66121     }
66122     nTotal += n;
66123     zSql += n;
66124   }
66125   return nTotal;
66126 }
66127 
66128 /*
66129 ** This function returns a pointer to a nul-terminated string in memory
66130 ** obtained from sqlite3DbMalloc(). If sqlite3.nVdbeExec is 1, then the
66131 ** string contains a copy of zRawSql but with host parameters expanded to
66132 ** their current bindings. Or, if sqlite3.nVdbeExec is greater than 1,
66133 ** then the returned string holds a copy of zRawSql with "-- " prepended
66134 ** to each line of text.
66135 **
66136 ** If the SQLITE_TRACE_SIZE_LIMIT macro is defined to an integer, then
66137 ** then long strings and blobs are truncated to that many bytes.  This
66138 ** can be used to prevent unreasonably large trace strings when dealing
66139 ** with large (multi-megabyte) strings and blobs.
66140 **
66141 ** The calling function is responsible for making sure the memory returned
66142 ** is eventually freed.
66143 **
66144 ** ALGORITHM:  Scan the input string looking for host parameters in any of
66145 ** these forms:  ?, ?N, $A, @A, :A.  Take care to avoid text within
66146 ** string literals, quoted identifier names, and comments.  For text forms,
66147 ** the host parameter index is found by scanning the perpared
66148 ** statement for the corresponding OP_Variable opcode.  Once the host
66149 ** parameter index is known, locate the value in p->aVar[].  Then render
66150 ** the value as a literal in place of the host parameter name.
66151 */
66152 SQLITE_PRIVATE char *sqlite3VdbeExpandSql(
66153   Vdbe *p,                 /* The prepared statement being evaluated */
66154   const char *zRawSql      /* Raw text of the SQL statement */
66155 ){
66156   sqlite3 *db;             /* The database connection */
66157   int idx = 0;             /* Index of a host parameter */
66158   int nextIndex = 1;       /* Index of next ? host parameter */
66159   int n;                   /* Length of a token prefix */
66160   int nToken;              /* Length of the parameter token */
66161   int i;                   /* Loop counter */
66162   Mem *pVar;               /* Value of a host parameter */
66163   StrAccum out;            /* Accumulate the output here */
66164   char zBase[100];         /* Initial working space */
66165 
66166   db = p->db;
66167   sqlite3StrAccumInit(&out, zBase, sizeof(zBase),
66168                       db->aLimit[SQLITE_LIMIT_LENGTH]);
66169   out.db = db;
66170   if( db->nVdbeExec>1 ){
66171     while( *zRawSql ){
66172       const char *zStart = zRawSql;
66173       while( *(zRawSql++)!='\n' && *zRawSql );
66174       sqlite3StrAccumAppend(&out, "-- ", 3);
66175       assert( (zRawSql - zStart) > 0 );
66176       sqlite3StrAccumAppend(&out, zStart, (int)(zRawSql-zStart));
66177     }
66178   }else{
66179     while( zRawSql[0] ){
66180       n = findNextHostParameter(zRawSql, &nToken);
66181       assert( n>0 );
66182       sqlite3StrAccumAppend(&out, zRawSql, n);
66183       zRawSql += n;
66184       assert( zRawSql[0] || nToken==0 );
66185       if( nToken==0 ) break;
66186       if( zRawSql[0]=='?' ){
66187         if( nToken>1 ){
66188           assert( sqlite3Isdigit(zRawSql[1]) );
66189           sqlite3GetInt32(&zRawSql[1], &idx);
66190         }else{
66191           idx = nextIndex;
66192         }
66193       }else{
66194         assert( zRawSql[0]==':' || zRawSql[0]=='$' || zRawSql[0]=='@' );
66195         testcase( zRawSql[0]==':' );
66196         testcase( zRawSql[0]=='$' );
66197         testcase( zRawSql[0]=='@' );
66198         idx = sqlite3VdbeParameterIndex(p, zRawSql, nToken);
66199         assert( idx>0 );
66200       }
66201       zRawSql += nToken;
66202       nextIndex = idx + 1;
66203       assert( idx>0 && idx<=p->nVar );
66204       pVar = &p->aVar[idx-1];
66205       if( pVar->flags & MEM_Null ){
66206         sqlite3StrAccumAppend(&out, "NULL", 4);
66207       }else if( pVar->flags & MEM_Int ){
66208         sqlite3XPrintf(&out, 0, "%lld", pVar->u.i);
66209       }else if( pVar->flags & MEM_Real ){
66210         sqlite3XPrintf(&out, 0, "%!.15g", pVar->r);
66211       }else if( pVar->flags & MEM_Str ){
66212         int nOut;  /* Number of bytes of the string text to include in output */
66213 #ifndef SQLITE_OMIT_UTF16
66214         u8 enc = ENC(db);
66215         Mem utf8;
66216         if( enc!=SQLITE_UTF8 ){
66217           memset(&utf8, 0, sizeof(utf8));
66218           utf8.db = db;
66219           sqlite3VdbeMemSetStr(&utf8, pVar->z, pVar->n, enc, SQLITE_STATIC);
66220           sqlite3VdbeChangeEncoding(&utf8, SQLITE_UTF8);
66221           pVar = &utf8;
66222         }
66223 #endif
66224         nOut = pVar->n;
66225 #ifdef SQLITE_TRACE_SIZE_LIMIT
66226         if( nOut>SQLITE_TRACE_SIZE_LIMIT ){
66227           nOut = SQLITE_TRACE_SIZE_LIMIT;
66228           while( nOut<pVar->n && (pVar->z[nOut]&0xc0)==0x80 ){ nOut++; }
66229         }
66230 #endif
66231         sqlite3XPrintf(&out, 0, "'%.*q'", nOut, pVar->z);
66232 #ifdef SQLITE_TRACE_SIZE_LIMIT
66233         if( nOut<pVar->n ){
66234           sqlite3XPrintf(&out, 0, "/*+%d bytes*/", pVar->n-nOut);
66235         }
66236 #endif
66237 #ifndef SQLITE_OMIT_UTF16
66238         if( enc!=SQLITE_UTF8 ) sqlite3VdbeMemRelease(&utf8);
66239 #endif
66240       }else if( pVar->flags & MEM_Zero ){
66241         sqlite3XPrintf(&out, 0, "zeroblob(%d)", pVar->u.nZero);
66242       }else{
66243         int nOut;  /* Number of bytes of the blob to include in output */
66244         assert( pVar->flags & MEM_Blob );
66245         sqlite3StrAccumAppend(&out, "x'", 2);
66246         nOut = pVar->n;
66247 #ifdef SQLITE_TRACE_SIZE_LIMIT
66248         if( nOut>SQLITE_TRACE_SIZE_LIMIT ) nOut = SQLITE_TRACE_SIZE_LIMIT;
66249 #endif
66250         for(i=0; i<nOut; i++){
66251           sqlite3XPrintf(&out, 0, "%02x", pVar->z[i]&0xff);
66252         }
66253         sqlite3StrAccumAppend(&out, "'", 1);
66254 #ifdef SQLITE_TRACE_SIZE_LIMIT
66255         if( nOut<pVar->n ){
66256           sqlite3XPrintf(&out, 0, "/*+%d bytes*/", pVar->n-nOut);
66257         }
66258 #endif
66259       }
66260     }
66261   }
66262   return sqlite3StrAccumFinish(&out);
66263 }
66264 
66265 #endif /* #ifndef SQLITE_OMIT_TRACE */
66266 
66267 /*****************************************************************************
66268 ** The following code implements the data-structure explaining logic
66269 ** for the Vdbe.
66270 */
66271 
66272 #if defined(SQLITE_ENABLE_TREE_EXPLAIN)
66273 
66274 /*
66275 ** Allocate a new Explain object
66276 */
66277 SQLITE_PRIVATE void sqlite3ExplainBegin(Vdbe *pVdbe){
66278   if( pVdbe ){
66279     Explain *p;
66280     sqlite3BeginBenignMalloc();
66281     p = (Explain *)sqlite3MallocZero( sizeof(Explain) );
66282     if( p ){
66283       p->pVdbe = pVdbe;
66284       sqlite3_free(pVdbe->pExplain);
66285       pVdbe->pExplain = p;
66286       sqlite3StrAccumInit(&p->str, p->zBase, sizeof(p->zBase),
66287                           SQLITE_MAX_LENGTH);
66288       p->str.useMalloc = 2;
66289     }else{
66290       sqlite3EndBenignMalloc();
66291     }
66292   }
66293 }
66294 
66295 /*
66296 ** Return true if the Explain ends with a new-line.
66297 */
66298 static int endsWithNL(Explain *p){
66299   return p && p->str.zText && p->str.nChar
66300            && p->str.zText[p->str.nChar-1]=='\n';
66301 }
66302 
66303 /*
66304 ** Append text to the indentation
66305 */
66306 SQLITE_PRIVATE void sqlite3ExplainPrintf(Vdbe *pVdbe, const char *zFormat, ...){
66307   Explain *p;
66308   if( pVdbe && (p = pVdbe->pExplain)!=0 ){
66309     va_list ap;
66310     if( p->nIndent && endsWithNL(p) ){
66311       int n = p->nIndent;
66312       if( n>ArraySize(p->aIndent) ) n = ArraySize(p->aIndent);
66313       sqlite3AppendSpace(&p->str, p->aIndent[n-1]);
66314     }
66315     va_start(ap, zFormat);
66316     sqlite3VXPrintf(&p->str, SQLITE_PRINTF_INTERNAL, zFormat, ap);
66317     va_end(ap);
66318   }
66319 }
66320 
66321 /*
66322 ** Append a '\n' if there is not already one.
66323 */
66324 SQLITE_PRIVATE void sqlite3ExplainNL(Vdbe *pVdbe){
66325   Explain *p;
66326   if( pVdbe && (p = pVdbe->pExplain)!=0 && !endsWithNL(p) ){
66327     sqlite3StrAccumAppend(&p->str, "\n", 1);
66328   }
66329 }
66330 
66331 /*
66332 ** Push a new indentation level.  Subsequent lines will be indented
66333 ** so that they begin at the current cursor position.
66334 */
66335 SQLITE_PRIVATE void sqlite3ExplainPush(Vdbe *pVdbe){
66336   Explain *p;
66337   if( pVdbe && (p = pVdbe->pExplain)!=0 ){
66338     if( p->str.zText && p->nIndent<ArraySize(p->aIndent) ){
66339       const char *z = p->str.zText;
66340       int i = p->str.nChar-1;
66341       int x;
66342       while( i>=0 && z[i]!='\n' ){ i--; }
66343       x = (p->str.nChar - 1) - i;
66344       if( p->nIndent && x<p->aIndent[p->nIndent-1] ){
66345         x = p->aIndent[p->nIndent-1];
66346       }
66347       p->aIndent[p->nIndent] = x;
66348     }
66349     p->nIndent++;
66350   }
66351 }
66352 
66353 /*
66354 ** Pop the indentation stack by one level.
66355 */
66356 SQLITE_PRIVATE void sqlite3ExplainPop(Vdbe *p){
66357   if( p && p->pExplain ) p->pExplain->nIndent--;
66358 }
66359 
66360 /*
66361 ** Free the indentation structure
66362 */
66363 SQLITE_PRIVATE void sqlite3ExplainFinish(Vdbe *pVdbe){
66364   if( pVdbe && pVdbe->pExplain ){
66365     sqlite3_free(pVdbe->zExplain);
66366     sqlite3ExplainNL(pVdbe);
66367     pVdbe->zExplain = sqlite3StrAccumFinish(&pVdbe->pExplain->str);
66368     sqlite3_free(pVdbe->pExplain);
66369     pVdbe->pExplain = 0;
66370     sqlite3EndBenignMalloc();
66371   }
66372 }
66373 
66374 /*
66375 ** Return the explanation of a virtual machine.
66376 */
66377 SQLITE_PRIVATE const char *sqlite3VdbeExplanation(Vdbe *pVdbe){
66378   return (pVdbe && pVdbe->zExplain) ? pVdbe->zExplain : 0;
66379 }
66380 #endif /* defined(SQLITE_DEBUG) */
66381 
66382 /************** End of vdbetrace.c *******************************************/
66383 /************** Begin file vdbe.c ********************************************/
66384 /*
66385 ** 2001 September 15
66386 **
66387 ** The author disclaims copyright to this source code.  In place of
66388 ** a legal notice, here is a blessing:
66389 **
66390 **    May you do good and not evil.
66391 **    May you find forgiveness for yourself and forgive others.
66392 **    May you share freely, never taking more than you give.
66393 **
66394 *************************************************************************
66395 ** The code in this file implements execution method of the
66396 ** Virtual Database Engine (VDBE).  A separate file ("vdbeaux.c")
66397 ** handles housekeeping details such as creating and deleting
66398 ** VDBE instances.  This file is solely interested in executing
66399 ** the VDBE program.
66400 **
66401 ** In the external interface, an "sqlite3_stmt*" is an opaque pointer
66402 ** to a VDBE.
66403 **
66404 ** The SQL parser generates a program which is then executed by
66405 ** the VDBE to do the work of the SQL statement.  VDBE programs are
66406 ** similar in form to assembly language.  The program consists of
66407 ** a linear sequence of operations.  Each operation has an opcode
66408 ** and 5 operands.  Operands P1, P2, and P3 are integers.  Operand P4
66409 ** is a null-terminated string.  Operand P5 is an unsigned character.
66410 ** Few opcodes use all 5 operands.
66411 **
66412 ** Computation results are stored on a set of registers numbered beginning
66413 ** with 1 and going up to Vdbe.nMem.  Each register can store
66414 ** either an integer, a null-terminated string, a floating point
66415 ** number, or the SQL "NULL" value.  An implicit conversion from one
66416 ** type to the other occurs as necessary.
66417 **
66418 ** Most of the code in this file is taken up by the sqlite3VdbeExec()
66419 ** function which does the work of interpreting a VDBE program.
66420 ** But other routines are also provided to help in building up
66421 ** a program instruction by instruction.
66422 **
66423 ** Various scripts scan this source file in order to generate HTML
66424 ** documentation, headers files, or other derived files.  The formatting
66425 ** of the code in this file is, therefore, important.  See other comments
66426 ** in this file for details.  If in doubt, do not deviate from existing
66427 ** commenting and indentation practices when changing or adding code.
66428 */
66429 
66430 /*
66431 ** Invoke this macro on memory cells just prior to changing the
66432 ** value of the cell.  This macro verifies that shallow copies are
66433 ** not misused.
66434 */
66435 #ifdef SQLITE_DEBUG
66436 # define memAboutToChange(P,M) sqlite3VdbeMemAboutToChange(P,M)
66437 #else
66438 # define memAboutToChange(P,M)
66439 #endif
66440 
66441 /*
66442 ** The following global variable is incremented every time a cursor
66443 ** moves, either by the OP_SeekXX, OP_Next, or OP_Prev opcodes.  The test
66444 ** procedures use this information to make sure that indices are
66445 ** working correctly.  This variable has no function other than to
66446 ** help verify the correct operation of the library.
66447 */
66448 #ifdef SQLITE_TEST
66449 SQLITE_API int sqlite3_search_count = 0;
66450 #endif
66451 
66452 /*
66453 ** When this global variable is positive, it gets decremented once before
66454 ** each instruction in the VDBE.  When it reaches zero, the u1.isInterrupted
66455 ** field of the sqlite3 structure is set in order to simulate an interrupt.
66456 **
66457 ** This facility is used for testing purposes only.  It does not function
66458 ** in an ordinary build.
66459 */
66460 #ifdef SQLITE_TEST
66461 SQLITE_API int sqlite3_interrupt_count = 0;
66462 #endif
66463 
66464 /*
66465 ** The next global variable is incremented each type the OP_Sort opcode
66466 ** is executed.  The test procedures use this information to make sure that
66467 ** sorting is occurring or not occurring at appropriate times.   This variable
66468 ** has no function other than to help verify the correct operation of the
66469 ** library.
66470 */
66471 #ifdef SQLITE_TEST
66472 SQLITE_API int sqlite3_sort_count = 0;
66473 #endif
66474 
66475 /*
66476 ** The next global variable records the size of the largest MEM_Blob
66477 ** or MEM_Str that has been used by a VDBE opcode.  The test procedures
66478 ** use this information to make sure that the zero-blob functionality
66479 ** is working correctly.   This variable has no function other than to
66480 ** help verify the correct operation of the library.
66481 */
66482 #ifdef SQLITE_TEST
66483 SQLITE_API int sqlite3_max_blobsize = 0;
66484 static void updateMaxBlobsize(Mem *p){
66485   if( (p->flags & (MEM_Str|MEM_Blob))!=0 && p->n>sqlite3_max_blobsize ){
66486     sqlite3_max_blobsize = p->n;
66487   }
66488 }
66489 #endif
66490 
66491 /*
66492 ** The next global variable is incremented each type the OP_Found opcode
66493 ** is executed. This is used to test whether or not the foreign key
66494 ** operation implemented using OP_FkIsZero is working. This variable
66495 ** has no function other than to help verify the correct operation of the
66496 ** library.
66497 */
66498 #ifdef SQLITE_TEST
66499 SQLITE_API int sqlite3_found_count = 0;
66500 #endif
66501 
66502 /*
66503 ** Test a register to see if it exceeds the current maximum blob size.
66504 ** If it does, record the new maximum blob size.
66505 */
66506 #if defined(SQLITE_TEST) && !defined(SQLITE_OMIT_BUILTIN_TEST)
66507 # define UPDATE_MAX_BLOBSIZE(P)  updateMaxBlobsize(P)
66508 #else
66509 # define UPDATE_MAX_BLOBSIZE(P)
66510 #endif
66511 
66512 /*
66513 ** Convert the given register into a string if it isn't one
66514 ** already. Return non-zero if a malloc() fails.
66515 */
66516 #define Stringify(P, enc) \
66517    if(((P)->flags&(MEM_Str|MEM_Blob))==0 && sqlite3VdbeMemStringify(P,enc)) \
66518      { goto no_mem; }
66519 
66520 /*
66521 ** An ephemeral string value (signified by the MEM_Ephem flag) contains
66522 ** a pointer to a dynamically allocated string where some other entity
66523 ** is responsible for deallocating that string.  Because the register
66524 ** does not control the string, it might be deleted without the register
66525 ** knowing it.
66526 **
66527 ** This routine converts an ephemeral string into a dynamically allocated
66528 ** string that the register itself controls.  In other words, it
66529 ** converts an MEM_Ephem string into an MEM_Dyn string.
66530 */
66531 #define Deephemeralize(P) \
66532    if( ((P)->flags&MEM_Ephem)!=0 \
66533        && sqlite3VdbeMemMakeWriteable(P) ){ goto no_mem;}
66534 
66535 /* Return true if the cursor was opened using the OP_OpenSorter opcode. */
66536 # define isSorter(x) ((x)->pSorter!=0)
66537 
66538 /*
66539 ** Argument pMem points at a register that will be passed to a
66540 ** user-defined function or returned to the user as the result of a query.
66541 ** This routine sets the pMem->type variable used by the sqlite3_value_*()
66542 ** routines.
66543 */
66544 SQLITE_PRIVATE void sqlite3VdbeMemStoreType(Mem *pMem){
66545   int flags = pMem->flags;
66546   if( flags & MEM_Null ){
66547     pMem->type = SQLITE_NULL;
66548   }
66549   else if( flags & MEM_Int ){
66550     pMem->type = SQLITE_INTEGER;
66551   }
66552   else if( flags & MEM_Real ){
66553     pMem->type = SQLITE_FLOAT;
66554   }
66555   else if( flags & MEM_Str ){
66556     pMem->type = SQLITE_TEXT;
66557   }else{
66558     pMem->type = SQLITE_BLOB;
66559   }
66560 }
66561 
66562 /*
66563 ** Allocate VdbeCursor number iCur.  Return a pointer to it.  Return NULL
66564 ** if we run out of memory.
66565 */
66566 static VdbeCursor *allocateCursor(
66567   Vdbe *p,              /* The virtual machine */
66568   int iCur,             /* Index of the new VdbeCursor */
66569   int nField,           /* Number of fields in the table or index */
66570   int iDb,              /* Database the cursor belongs to, or -1 */
66571   int isBtreeCursor     /* True for B-Tree.  False for pseudo-table or vtab */
66572 ){
66573   /* Find the memory cell that will be used to store the blob of memory
66574   ** required for this VdbeCursor structure. It is convenient to use a
66575   ** vdbe memory cell to manage the memory allocation required for a
66576   ** VdbeCursor structure for the following reasons:
66577   **
66578   **   * Sometimes cursor numbers are used for a couple of different
66579   **     purposes in a vdbe program. The different uses might require
66580   **     different sized allocations. Memory cells provide growable
66581   **     allocations.
66582   **
66583   **   * When using ENABLE_MEMORY_MANAGEMENT, memory cell buffers can
66584   **     be freed lazily via the sqlite3_release_memory() API. This
66585   **     minimizes the number of malloc calls made by the system.
66586   **
66587   ** Memory cells for cursors are allocated at the top of the address
66588   ** space. Memory cell (p->nMem) corresponds to cursor 0. Space for
66589   ** cursor 1 is managed by memory cell (p->nMem-1), etc.
66590   */
66591   Mem *pMem = &p->aMem[p->nMem-iCur];
66592 
66593   int nByte;
66594   VdbeCursor *pCx = 0;
66595   nByte =
66596       ROUND8(sizeof(VdbeCursor)) + 2*sizeof(u32)*nField +
66597       (isBtreeCursor?sqlite3BtreeCursorSize():0);
66598 
66599   assert( iCur<p->nCursor );
66600   if( p->apCsr[iCur] ){
66601     sqlite3VdbeFreeCursor(p, p->apCsr[iCur]);
66602     p->apCsr[iCur] = 0;
66603   }
66604   if( SQLITE_OK==sqlite3VdbeMemGrow(pMem, nByte, 0) ){
66605     p->apCsr[iCur] = pCx = (VdbeCursor*)pMem->z;
66606     memset(pCx, 0, sizeof(VdbeCursor));
66607     pCx->iDb = iDb;
66608     pCx->nField = nField;
66609     if( isBtreeCursor ){
66610       pCx->pCursor = (BtCursor*)
66611           &pMem->z[ROUND8(sizeof(VdbeCursor))+2*sizeof(u32)*nField];
66612       sqlite3BtreeCursorZero(pCx->pCursor);
66613     }
66614   }
66615   return pCx;
66616 }
66617 
66618 /*
66619 ** Try to convert a value into a numeric representation if we can
66620 ** do so without loss of information.  In other words, if the string
66621 ** looks like a number, convert it into a number.  If it does not
66622 ** look like a number, leave it alone.
66623 */
66624 static void applyNumericAffinity(Mem *pRec){
66625   if( (pRec->flags & (MEM_Real|MEM_Int))==0 ){
66626     double rValue;
66627     i64 iValue;
66628     u8 enc = pRec->enc;
66629     if( (pRec->flags&MEM_Str)==0 ) return;
66630     if( sqlite3AtoF(pRec->z, &rValue, pRec->n, enc)==0 ) return;
66631     if( 0==sqlite3Atoi64(pRec->z, &iValue, pRec->n, enc) ){
66632       pRec->u.i = iValue;
66633       pRec->flags |= MEM_Int;
66634     }else{
66635       pRec->r = rValue;
66636       pRec->flags |= MEM_Real;
66637     }
66638   }
66639 }
66640 
66641 /*
66642 ** Processing is determine by the affinity parameter:
66643 **
66644 ** SQLITE_AFF_INTEGER:
66645 ** SQLITE_AFF_REAL:
66646 ** SQLITE_AFF_NUMERIC:
66647 **    Try to convert pRec to an integer representation or a
66648 **    floating-point representation if an integer representation
66649 **    is not possible.  Note that the integer representation is
66650 **    always preferred, even if the affinity is REAL, because
66651 **    an integer representation is more space efficient on disk.
66652 **
66653 ** SQLITE_AFF_TEXT:
66654 **    Convert pRec to a text representation.
66655 **
66656 ** SQLITE_AFF_NONE:
66657 **    No-op.  pRec is unchanged.
66658 */
66659 static void applyAffinity(
66660   Mem *pRec,          /* The value to apply affinity to */
66661   char affinity,      /* The affinity to be applied */
66662   u8 enc              /* Use this text encoding */
66663 ){
66664   if( affinity==SQLITE_AFF_TEXT ){
66665     /* Only attempt the conversion to TEXT if there is an integer or real
66666     ** representation (blob and NULL do not get converted) but no string
66667     ** representation.
66668     */
66669     if( 0==(pRec->flags&MEM_Str) && (pRec->flags&(MEM_Real|MEM_Int)) ){
66670       sqlite3VdbeMemStringify(pRec, enc);
66671     }
66672     pRec->flags &= ~(MEM_Real|MEM_Int);
66673   }else if( affinity!=SQLITE_AFF_NONE ){
66674     assert( affinity==SQLITE_AFF_INTEGER || affinity==SQLITE_AFF_REAL
66675              || affinity==SQLITE_AFF_NUMERIC );
66676     applyNumericAffinity(pRec);
66677     if( pRec->flags & MEM_Real ){
66678       sqlite3VdbeIntegerAffinity(pRec);
66679     }
66680   }
66681 }
66682 
66683 /*
66684 ** Try to convert the type of a function argument or a result column
66685 ** into a numeric representation.  Use either INTEGER or REAL whichever
66686 ** is appropriate.  But only do the conversion if it is possible without
66687 ** loss of information and return the revised type of the argument.
66688 */
66689 SQLITE_API int sqlite3_value_numeric_type(sqlite3_value *pVal){
66690   Mem *pMem = (Mem*)pVal;
66691   if( pMem->type==SQLITE_TEXT ){
66692     applyNumericAffinity(pMem);
66693     sqlite3VdbeMemStoreType(pMem);
66694   }
66695   return pMem->type;
66696 }
66697 
66698 /*
66699 ** Exported version of applyAffinity(). This one works on sqlite3_value*,
66700 ** not the internal Mem* type.
66701 */
66702 SQLITE_PRIVATE void sqlite3ValueApplyAffinity(
66703   sqlite3_value *pVal,
66704   u8 affinity,
66705   u8 enc
66706 ){
66707   applyAffinity((Mem *)pVal, affinity, enc);
66708 }
66709 
66710 #ifdef SQLITE_DEBUG
66711 /*
66712 ** Write a nice string representation of the contents of cell pMem
66713 ** into buffer zBuf, length nBuf.
66714 */
66715 SQLITE_PRIVATE void sqlite3VdbeMemPrettyPrint(Mem *pMem, char *zBuf){
66716   char *zCsr = zBuf;
66717   int f = pMem->flags;
66718 
66719   static const char *const encnames[] = {"(X)", "(8)", "(16LE)", "(16BE)"};
66720 
66721   if( f&MEM_Blob ){
66722     int i;
66723     char c;
66724     if( f & MEM_Dyn ){
66725       c = 'z';
66726       assert( (f & (MEM_Static|MEM_Ephem))==0 );
66727     }else if( f & MEM_Static ){
66728       c = 't';
66729       assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
66730     }else if( f & MEM_Ephem ){
66731       c = 'e';
66732       assert( (f & (MEM_Static|MEM_Dyn))==0 );
66733     }else{
66734       c = 's';
66735     }
66736 
66737     sqlite3_snprintf(100, zCsr, "%c", c);
66738     zCsr += sqlite3Strlen30(zCsr);
66739     sqlite3_snprintf(100, zCsr, "%d[", pMem->n);
66740     zCsr += sqlite3Strlen30(zCsr);
66741     for(i=0; i<16 && i<pMem->n; i++){
66742       sqlite3_snprintf(100, zCsr, "%02X", ((int)pMem->z[i] & 0xFF));
66743       zCsr += sqlite3Strlen30(zCsr);
66744     }
66745     for(i=0; i<16 && i<pMem->n; i++){
66746       char z = pMem->z[i];
66747       if( z<32 || z>126 ) *zCsr++ = '.';
66748       else *zCsr++ = z;
66749     }
66750 
66751     sqlite3_snprintf(100, zCsr, "]%s", encnames[pMem->enc]);
66752     zCsr += sqlite3Strlen30(zCsr);
66753     if( f & MEM_Zero ){
66754       sqlite3_snprintf(100, zCsr,"+%dz",pMem->u.nZero);
66755       zCsr += sqlite3Strlen30(zCsr);
66756     }
66757     *zCsr = '\0';
66758   }else if( f & MEM_Str ){
66759     int j, k;
66760     zBuf[0] = ' ';
66761     if( f & MEM_Dyn ){
66762       zBuf[1] = 'z';
66763       assert( (f & (MEM_Static|MEM_Ephem))==0 );
66764     }else if( f & MEM_Static ){
66765       zBuf[1] = 't';
66766       assert( (f & (MEM_Dyn|MEM_Ephem))==0 );
66767     }else if( f & MEM_Ephem ){
66768       zBuf[1] = 'e';
66769       assert( (f & (MEM_Static|MEM_Dyn))==0 );
66770     }else{
66771       zBuf[1] = 's';
66772     }
66773     k = 2;
66774     sqlite3_snprintf(100, &zBuf[k], "%d", pMem->n);
66775     k += sqlite3Strlen30(&zBuf[k]);
66776     zBuf[k++] = '[';
66777     for(j=0; j<15 && j<pMem->n; j++){
66778       u8 c = pMem->z[j];
66779       if( c>=0x20 && c<0x7f ){
66780         zBuf[k++] = c;
66781       }else{
66782         zBuf[k++] = '.';
66783       }
66784     }
66785     zBuf[k++] = ']';
66786     sqlite3_snprintf(100,&zBuf[k], encnames[pMem->enc]);
66787     k += sqlite3Strlen30(&zBuf[k]);
66788     zBuf[k++] = 0;
66789   }
66790 }
66791 #endif
66792 
66793 #ifdef SQLITE_DEBUG
66794 /*
66795 ** Print the value of a register for tracing purposes:
66796 */
66797 static void memTracePrint(Mem *p){
66798   if( p->flags & MEM_Invalid ){
66799     printf(" undefined");
66800   }else if( p->flags & MEM_Null ){
66801     printf(" NULL");
66802   }else if( (p->flags & (MEM_Int|MEM_Str))==(MEM_Int|MEM_Str) ){
66803     printf(" si:%lld", p->u.i);
66804   }else if( p->flags & MEM_Int ){
66805     printf(" i:%lld", p->u.i);
66806 #ifndef SQLITE_OMIT_FLOATING_POINT
66807   }else if( p->flags & MEM_Real ){
66808     printf(" r:%g", p->r);
66809 #endif
66810   }else if( p->flags & MEM_RowSet ){
66811     printf(" (rowset)");
66812   }else{
66813     char zBuf[200];
66814     sqlite3VdbeMemPrettyPrint(p, zBuf);
66815     printf(" %s", zBuf);
66816   }
66817 }
66818 static void registerTrace(int iReg, Mem *p){
66819   printf("REG[%d] = ", iReg);
66820   memTracePrint(p);
66821   printf("\n");
66822 }
66823 #endif
66824 
66825 #ifdef SQLITE_DEBUG
66826 #  define REGISTER_TRACE(R,M) if(db->flags&SQLITE_VdbeTrace)registerTrace(R,M)
66827 #else
66828 #  define REGISTER_TRACE(R,M)
66829 #endif
66830 
66831 
66832 #ifdef VDBE_PROFILE
66833 
66834 /*
66835 ** hwtime.h contains inline assembler code for implementing
66836 ** high-performance timing routines.
66837 */
66838 /************** Include hwtime.h in the middle of vdbe.c *********************/
66839 /************** Begin file hwtime.h ******************************************/
66840 /*
66841 ** 2008 May 27
66842 **
66843 ** The author disclaims copyright to this source code.  In place of
66844 ** a legal notice, here is a blessing:
66845 **
66846 **    May you do good and not evil.
66847 **    May you find forgiveness for yourself and forgive others.
66848 **    May you share freely, never taking more than you give.
66849 **
66850 ******************************************************************************
66851 **
66852 ** This file contains inline asm code for retrieving "high-performance"
66853 ** counters for x86 class CPUs.
66854 */
66855 #ifndef _HWTIME_H_
66856 #define _HWTIME_H_
66857 
66858 /*
66859 ** The following routine only works on pentium-class (or newer) processors.
66860 ** It uses the RDTSC opcode to read the cycle count value out of the
66861 ** processor and returns that value.  This can be used for high-res
66862 ** profiling.
66863 */
66864 #if (defined(__GNUC__) || defined(_MSC_VER)) && \
66865       (defined(i386) || defined(__i386__) || defined(_M_IX86))
66866 
66867   #if defined(__GNUC__)
66868 
66869   __inline__ sqlite_uint64 sqlite3Hwtime(void){
66870      unsigned int lo, hi;
66871      __asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
66872      return (sqlite_uint64)hi << 32 | lo;
66873   }
66874 
66875   #elif defined(_MSC_VER)
66876 
66877   __declspec(naked) __inline sqlite_uint64 __cdecl sqlite3Hwtime(void){
66878      __asm {
66879         rdtsc
66880         ret       ; return value at EDX:EAX
66881      }
66882   }
66883 
66884   #endif
66885 
66886 #elif (defined(__GNUC__) && defined(__x86_64__))
66887 
66888   __inline__ sqlite_uint64 sqlite3Hwtime(void){
66889       unsigned long val;
66890       __asm__ __volatile__ ("rdtsc" : "=A" (val));
66891       return val;
66892   }
66893 
66894 #elif (defined(__GNUC__) && defined(__ppc__))
66895 
66896   __inline__ sqlite_uint64 sqlite3Hwtime(void){
66897       unsigned long long retval;
66898       unsigned long junk;
66899       __asm__ __volatile__ ("\n\
66900           1:      mftbu   %1\n\
66901                   mftb    %L0\n\
66902                   mftbu   %0\n\
66903                   cmpw    %0,%1\n\
66904                   bne     1b"
66905                   : "=r" (retval), "=r" (junk));
66906       return retval;
66907   }
66908 
66909 #else
66910 
66911   #error Need implementation of sqlite3Hwtime() for your platform.
66912 
66913   /*
66914   ** To compile without implementing sqlite3Hwtime() for your platform,
66915   ** you can remove the above #error and use the following
66916   ** stub function.  You will lose timing support for many
66917   ** of the debugging and testing utilities, but it should at
66918   ** least compile and run.
66919   */
66920 SQLITE_PRIVATE   sqlite_uint64 sqlite3Hwtime(void){ return ((sqlite_uint64)0); }
66921 
66922 #endif
66923 
66924 #endif /* !defined(_HWTIME_H_) */
66925 
66926 /************** End of hwtime.h **********************************************/
66927 /************** Continuing where we left off in vdbe.c ***********************/
66928 
66929 #endif
66930 
66931 /*
66932 ** The CHECK_FOR_INTERRUPT macro defined here looks to see if the
66933 ** sqlite3_interrupt() routine has been called.  If it has been, then
66934 ** processing of the VDBE program is interrupted.
66935 **
66936 ** This macro added to every instruction that does a jump in order to
66937 ** implement a loop.  This test used to be on every single instruction,
66938 ** but that meant we more testing than we needed.  By only testing the
66939 ** flag on jump instructions, we get a (small) speed improvement.
66940 */
66941 #define CHECK_FOR_INTERRUPT \
66942    if( db->u1.isInterrupted ) goto abort_due_to_interrupt;
66943 
66944 
66945 #ifndef NDEBUG
66946 /*
66947 ** This function is only called from within an assert() expression. It
66948 ** checks that the sqlite3.nTransaction variable is correctly set to
66949 ** the number of non-transaction savepoints currently in the
66950 ** linked list starting at sqlite3.pSavepoint.
66951 **
66952 ** Usage:
66953 **
66954 **     assert( checkSavepointCount(db) );
66955 */
66956 static int checkSavepointCount(sqlite3 *db){
66957   int n = 0;
66958   Savepoint *p;
66959   for(p=db->pSavepoint; p; p=p->pNext) n++;
66960   assert( n==(db->nSavepoint + db->isTransactionSavepoint) );
66961   return 1;
66962 }
66963 #endif
66964 
66965 
66966 /*
66967 ** Execute as much of a VDBE program as we can then return.
66968 **
66969 ** sqlite3VdbeMakeReady() must be called before this routine in order to
66970 ** close the program with a final OP_Halt and to set up the callbacks
66971 ** and the error message pointer.
66972 **
66973 ** Whenever a row or result data is available, this routine will either
66974 ** invoke the result callback (if there is one) or return with
66975 ** SQLITE_ROW.
66976 **
66977 ** If an attempt is made to open a locked database, then this routine
66978 ** will either invoke the busy callback (if there is one) or it will
66979 ** return SQLITE_BUSY.
66980 **
66981 ** If an error occurs, an error message is written to memory obtained
66982 ** from sqlite3_malloc() and p->zErrMsg is made to point to that memory.
66983 ** The error code is stored in p->rc and this routine returns SQLITE_ERROR.
66984 **
66985 ** If the callback ever returns non-zero, then the program exits
66986 ** immediately.  There will be no error message but the p->rc field is
66987 ** set to SQLITE_ABORT and this routine will return SQLITE_ERROR.
66988 **
66989 ** A memory allocation error causes p->rc to be set to SQLITE_NOMEM and this
66990 ** routine to return SQLITE_ERROR.
66991 **
66992 ** Other fatal errors return SQLITE_ERROR.
66993 **
66994 ** After this routine has finished, sqlite3VdbeFinalize() should be
66995 ** used to clean up the mess that was left behind.
66996 */
66997 SQLITE_PRIVATE int sqlite3VdbeExec(
66998   Vdbe *p                    /* The VDBE */
66999 ){
67000   int pc=0;                  /* The program counter */
67001   Op *aOp = p->aOp;          /* Copy of p->aOp */
67002   Op *pOp;                   /* Current operation */
67003   int rc = SQLITE_OK;        /* Value to return */
67004   sqlite3 *db = p->db;       /* The database */
67005   u8 resetSchemaOnFault = 0; /* Reset schema after an error if positive */
67006   u8 encoding = ENC(db);     /* The database encoding */
67007   int iCompare = 0;          /* Result of last OP_Compare operation */
67008   unsigned nVmStep = 0;      /* Number of virtual machine steps */
67009 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
67010   unsigned nProgressLimit = 0;/* Invoke xProgress() when nVmStep reaches this */
67011 #endif
67012   Mem *aMem = p->aMem;       /* Copy of p->aMem */
67013   Mem *pIn1 = 0;             /* 1st input operand */
67014   Mem *pIn2 = 0;             /* 2nd input operand */
67015   Mem *pIn3 = 0;             /* 3rd input operand */
67016   Mem *pOut = 0;             /* Output operand */
67017   int *aPermute = 0;         /* Permutation of columns for OP_Compare */
67018   i64 lastRowid = db->lastRowid;  /* Saved value of the last insert ROWID */
67019 #ifdef VDBE_PROFILE
67020   u64 start;                 /* CPU clock count at start of opcode */
67021   int origPc;                /* Program counter at start of opcode */
67022 #endif
67023   /*** INSERT STACK UNION HERE ***/
67024 
67025   assert( p->magic==VDBE_MAGIC_RUN );  /* sqlite3_step() verifies this */
67026   sqlite3VdbeEnter(p);
67027   if( p->rc==SQLITE_NOMEM ){
67028     /* This happens if a malloc() inside a call to sqlite3_column_text() or
67029     ** sqlite3_column_text16() failed.  */
67030     goto no_mem;
67031   }
67032   assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY );
67033   assert( p->bIsReader || p->readOnly!=0 );
67034   p->rc = SQLITE_OK;
67035   p->iCurrentTime = 0;
67036   assert( p->explain==0 );
67037   p->pResultSet = 0;
67038   db->busyHandler.nBusy = 0;
67039   CHECK_FOR_INTERRUPT;
67040   sqlite3VdbeIOTraceSql(p);
67041 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
67042   if( db->xProgress ){
67043     assert( 0 < db->nProgressOps );
67044     nProgressLimit = (unsigned)p->aCounter[SQLITE_STMTSTATUS_VM_STEP];
67045     if( nProgressLimit==0 ){
67046       nProgressLimit = db->nProgressOps;
67047     }else{
67048       nProgressLimit %= (unsigned)db->nProgressOps;
67049     }
67050   }
67051 #endif
67052 #ifdef SQLITE_DEBUG
67053   sqlite3BeginBenignMalloc();
67054   if( p->pc==0
67055    && (p->db->flags & (SQLITE_VdbeListing|SQLITE_VdbeEQP|SQLITE_VdbeTrace))!=0
67056   ){
67057     int i;
67058     int once = 1;
67059     sqlite3VdbePrintSql(p);
67060     if( p->db->flags & SQLITE_VdbeListing ){
67061       printf("VDBE Program Listing:\n");
67062       for(i=0; i<p->nOp; i++){
67063         sqlite3VdbePrintOp(stdout, i, &aOp[i]);
67064       }
67065     }
67066     if( p->db->flags & SQLITE_VdbeEQP ){
67067       for(i=0; i<p->nOp; i++){
67068         if( aOp[i].opcode==OP_Explain ){
67069           if( once ) printf("VDBE Query Plan:\n");
67070           printf("%s\n", aOp[i].p4.z);
67071           once = 0;
67072         }
67073       }
67074     }
67075     if( p->db->flags & SQLITE_VdbeTrace )  printf("VDBE Trace:\n");
67076   }
67077   sqlite3EndBenignMalloc();
67078 #endif
67079   for(pc=p->pc; rc==SQLITE_OK; pc++){
67080     assert( pc>=0 && pc<p->nOp );
67081     if( db->mallocFailed ) goto no_mem;
67082 #ifdef VDBE_PROFILE
67083     origPc = pc;
67084     start = sqlite3Hwtime();
67085 #endif
67086     nVmStep++;
67087     pOp = &aOp[pc];
67088 
67089     /* Only allow tracing if SQLITE_DEBUG is defined.
67090     */
67091 #ifdef SQLITE_DEBUG
67092     if( db->flags & SQLITE_VdbeTrace ){
67093       sqlite3VdbePrintOp(stdout, pc, pOp);
67094     }
67095 #endif
67096 
67097 
67098     /* Check to see if we need to simulate an interrupt.  This only happens
67099     ** if we have a special test build.
67100     */
67101 #ifdef SQLITE_TEST
67102     if( sqlite3_interrupt_count>0 ){
67103       sqlite3_interrupt_count--;
67104       if( sqlite3_interrupt_count==0 ){
67105         sqlite3_interrupt(db);
67106       }
67107     }
67108 #endif
67109 
67110     /* On any opcode with the "out2-prerelease" tag, free any
67111     ** external allocations out of mem[p2] and set mem[p2] to be
67112     ** an undefined integer.  Opcodes will either fill in the integer
67113     ** value or convert mem[p2] to a different type.
67114     */
67115     assert( pOp->opflags==sqlite3OpcodeProperty[pOp->opcode] );
67116     if( pOp->opflags & OPFLG_OUT2_PRERELEASE ){
67117       assert( pOp->p2>0 );
67118       assert( pOp->p2<=(p->nMem-p->nCursor) );
67119       pOut = &aMem[pOp->p2];
67120       memAboutToChange(p, pOut);
67121       VdbeMemRelease(pOut);
67122       pOut->flags = MEM_Int;
67123     }
67124 
67125     /* Sanity checking on other operands */
67126 #ifdef SQLITE_DEBUG
67127     if( (pOp->opflags & OPFLG_IN1)!=0 ){
67128       assert( pOp->p1>0 );
67129       assert( pOp->p1<=(p->nMem-p->nCursor) );
67130       assert( memIsValid(&aMem[pOp->p1]) );
67131       REGISTER_TRACE(pOp->p1, &aMem[pOp->p1]);
67132     }
67133     if( (pOp->opflags & OPFLG_IN2)!=0 ){
67134       assert( pOp->p2>0 );
67135       assert( pOp->p2<=(p->nMem-p->nCursor) );
67136       assert( memIsValid(&aMem[pOp->p2]) );
67137       REGISTER_TRACE(pOp->p2, &aMem[pOp->p2]);
67138     }
67139     if( (pOp->opflags & OPFLG_IN3)!=0 ){
67140       assert( pOp->p3>0 );
67141       assert( pOp->p3<=(p->nMem-p->nCursor) );
67142       assert( memIsValid(&aMem[pOp->p3]) );
67143       REGISTER_TRACE(pOp->p3, &aMem[pOp->p3]);
67144     }
67145     if( (pOp->opflags & OPFLG_OUT2)!=0 ){
67146       assert( pOp->p2>0 );
67147       assert( pOp->p2<=(p->nMem-p->nCursor) );
67148       memAboutToChange(p, &aMem[pOp->p2]);
67149     }
67150     if( (pOp->opflags & OPFLG_OUT3)!=0 ){
67151       assert( pOp->p3>0 );
67152       assert( pOp->p3<=(p->nMem-p->nCursor) );
67153       memAboutToChange(p, &aMem[pOp->p3]);
67154     }
67155 #endif
67156 
67157     switch( pOp->opcode ){
67158 
67159 /*****************************************************************************
67160 ** What follows is a massive switch statement where each case implements a
67161 ** separate instruction in the virtual machine.  If we follow the usual
67162 ** indentation conventions, each case should be indented by 6 spaces.  But
67163 ** that is a lot of wasted space on the left margin.  So the code within
67164 ** the switch statement will break with convention and be flush-left. Another
67165 ** big comment (similar to this one) will mark the point in the code where
67166 ** we transition back to normal indentation.
67167 **
67168 ** The formatting of each case is important.  The makefile for SQLite
67169 ** generates two C files "opcodes.h" and "opcodes.c" by scanning this
67170 ** file looking for lines that begin with "case OP_".  The opcodes.h files
67171 ** will be filled with #defines that give unique integer values to each
67172 ** opcode and the opcodes.c file is filled with an array of strings where
67173 ** each string is the symbolic name for the corresponding opcode.  If the
67174 ** case statement is followed by a comment of the form "/# same as ... #/"
67175 ** that comment is used to determine the particular value of the opcode.
67176 **
67177 ** Other keywords in the comment that follows each case are used to
67178 ** construct the OPFLG_INITIALIZER value that initializes opcodeProperty[].
67179 ** Keywords include: in1, in2, in3, out2_prerelease, out2, out3.  See
67180 ** the mkopcodeh.awk script for additional information.
67181 **
67182 ** Documentation about VDBE opcodes is generated by scanning this file
67183 ** for lines of that contain "Opcode:".  That line and all subsequent
67184 ** comment lines are used in the generation of the opcode.html documentation
67185 ** file.
67186 **
67187 ** SUMMARY:
67188 **
67189 **     Formatting is important to scripts that scan this file.
67190 **     Do not deviate from the formatting style currently in use.
67191 **
67192 *****************************************************************************/
67193 
67194 /* Opcode:  Goto * P2 * * *
67195 **
67196 ** An unconditional jump to address P2.
67197 ** The next instruction executed will be
67198 ** the one at index P2 from the beginning of
67199 ** the program.
67200 */
67201 case OP_Goto: {             /* jump */
67202   pc = pOp->p2 - 1;
67203 
67204   /* Opcodes that are used as the bottom of a loop (OP_Next, OP_Prev,
67205   ** OP_VNext, OP_RowSetNext, or OP_SorterNext) all jump here upon
67206   ** completion.  Check to see if sqlite3_interrupt() has been called
67207   ** or if the progress callback needs to be invoked.
67208   **
67209   ** This code uses unstructured "goto" statements and does not look clean.
67210   ** But that is not due to sloppy coding habits. The code is written this
67211   ** way for performance, to avoid having to run the interrupt and progress
67212   ** checks on every opcode.  This helps sqlite3_step() to run about 1.5%
67213   ** faster according to "valgrind --tool=cachegrind" */
67214 check_for_interrupt:
67215   CHECK_FOR_INTERRUPT;
67216 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
67217   /* Call the progress callback if it is configured and the required number
67218   ** of VDBE ops have been executed (either since this invocation of
67219   ** sqlite3VdbeExec() or since last time the progress callback was called).
67220   ** If the progress callback returns non-zero, exit the virtual machine with
67221   ** a return code SQLITE_ABORT.
67222   */
67223   if( db->xProgress!=0 && nVmStep>=nProgressLimit ){
67224     assert( db->nProgressOps!=0 );
67225     nProgressLimit = nVmStep + db->nProgressOps - (nVmStep%db->nProgressOps);
67226     if( db->xProgress(db->pProgressArg) ){
67227       rc = SQLITE_INTERRUPT;
67228       goto vdbe_error_halt;
67229     }
67230   }
67231 #endif
67232 
67233   break;
67234 }
67235 
67236 /* Opcode:  Gosub P1 P2 * * *
67237 **
67238 ** Write the current address onto register P1
67239 ** and then jump to address P2.
67240 */
67241 case OP_Gosub: {            /* jump */
67242   assert( pOp->p1>0 && pOp->p1<=(p->nMem-p->nCursor) );
67243   pIn1 = &aMem[pOp->p1];
67244   assert( (pIn1->flags & MEM_Dyn)==0 );
67245   memAboutToChange(p, pIn1);
67246   pIn1->flags = MEM_Int;
67247   pIn1->u.i = pc;
67248   REGISTER_TRACE(pOp->p1, pIn1);
67249   pc = pOp->p2 - 1;
67250   break;
67251 }
67252 
67253 /* Opcode:  Return P1 * * * *
67254 **
67255 ** Jump to the next instruction after the address in register P1.
67256 */
67257 case OP_Return: {           /* in1 */
67258   pIn1 = &aMem[pOp->p1];
67259   assert( pIn1->flags & MEM_Int );
67260   pc = (int)pIn1->u.i;
67261   break;
67262 }
67263 
67264 /* Opcode:  Yield P1 * * * *
67265 **
67266 ** Swap the program counter with the value in register P1.
67267 */
67268 case OP_Yield: {            /* in1 */
67269   int pcDest;
67270   pIn1 = &aMem[pOp->p1];
67271   assert( (pIn1->flags & MEM_Dyn)==0 );
67272   pIn1->flags = MEM_Int;
67273   pcDest = (int)pIn1->u.i;
67274   pIn1->u.i = pc;
67275   REGISTER_TRACE(pOp->p1, pIn1);
67276   pc = pcDest;
67277   break;
67278 }
67279 
67280 /* Opcode:  HaltIfNull  P1 P2 P3 P4 P5
67281 ** Synopsis:  if r[P3] null then halt
67282 **
67283 ** Check the value in register P3.  If it is NULL then Halt using
67284 ** parameter P1, P2, and P4 as if this were a Halt instruction.  If the
67285 ** value in register P3 is not NULL, then this routine is a no-op.
67286 ** The P5 parameter should be 1.
67287 */
67288 case OP_HaltIfNull: {      /* in3 */
67289   pIn3 = &aMem[pOp->p3];
67290   if( (pIn3->flags & MEM_Null)==0 ) break;
67291   /* Fall through into OP_Halt */
67292 }
67293 
67294 /* Opcode:  Halt P1 P2 * P4 P5
67295 **
67296 ** Exit immediately.  All open cursors, etc are closed
67297 ** automatically.
67298 **
67299 ** P1 is the result code returned by sqlite3_exec(), sqlite3_reset(),
67300 ** or sqlite3_finalize().  For a normal halt, this should be SQLITE_OK (0).
67301 ** For errors, it can be some other value.  If P1!=0 then P2 will determine
67302 ** whether or not to rollback the current transaction.  Do not rollback
67303 ** if P2==OE_Fail. Do the rollback if P2==OE_Rollback.  If P2==OE_Abort,
67304 ** then back out all changes that have occurred during this execution of the
67305 ** VDBE, but do not rollback the transaction.
67306 **
67307 ** If P4 is not null then it is an error message string.
67308 **
67309 ** P5 is a value between 0 and 4, inclusive, that modifies the P4 string.
67310 **
67311 **    0:  (no change)
67312 **    1:  NOT NULL contraint failed: P4
67313 **    2:  UNIQUE constraint failed: P4
67314 **    3:  CHECK constraint failed: P4
67315 **    4:  FOREIGN KEY constraint failed: P4
67316 **
67317 ** If P5 is not zero and P4 is NULL, then everything after the ":" is
67318 ** omitted.
67319 **
67320 ** There is an implied "Halt 0 0 0" instruction inserted at the very end of
67321 ** every program.  So a jump past the last instruction of the program
67322 ** is the same as executing Halt.
67323 */
67324 case OP_Halt: {
67325   const char *zType;
67326   const char *zLogFmt;
67327 
67328   if( pOp->p1==SQLITE_OK && p->pFrame ){
67329     /* Halt the sub-program. Return control to the parent frame. */
67330     VdbeFrame *pFrame = p->pFrame;
67331     p->pFrame = pFrame->pParent;
67332     p->nFrame--;
67333     sqlite3VdbeSetChanges(db, p->nChange);
67334     pc = sqlite3VdbeFrameRestore(pFrame);
67335     lastRowid = db->lastRowid;
67336     if( pOp->p2==OE_Ignore ){
67337       /* Instruction pc is the OP_Program that invoked the sub-program
67338       ** currently being halted. If the p2 instruction of this OP_Halt
67339       ** instruction is set to OE_Ignore, then the sub-program is throwing
67340       ** an IGNORE exception. In this case jump to the address specified
67341       ** as the p2 of the calling OP_Program.  */
67342       pc = p->aOp[pc].p2-1;
67343     }
67344     aOp = p->aOp;
67345     aMem = p->aMem;
67346     break;
67347   }
67348   p->rc = pOp->p1;
67349   p->errorAction = (u8)pOp->p2;
67350   p->pc = pc;
67351   if( p->rc ){
67352     if( pOp->p5 ){
67353       static const char * const azType[] = { "NOT NULL", "UNIQUE", "CHECK",
67354                                              "FOREIGN KEY" };
67355       assert( pOp->p5>=1 && pOp->p5<=4 );
67356       testcase( pOp->p5==1 );
67357       testcase( pOp->p5==2 );
67358       testcase( pOp->p5==3 );
67359       testcase( pOp->p5==4 );
67360       zType = azType[pOp->p5-1];
67361     }else{
67362       zType = 0;
67363     }
67364     assert( zType!=0 || pOp->p4.z!=0 );
67365     zLogFmt = "abort at %d in [%s]: %s";
67366     if( zType && pOp->p4.z ){
67367       sqlite3SetString(&p->zErrMsg, db, "%s constraint failed: %s",
67368                        zType, pOp->p4.z);
67369     }else if( pOp->p4.z ){
67370       sqlite3SetString(&p->zErrMsg, db, "%s", pOp->p4.z);
67371     }else{
67372       sqlite3SetString(&p->zErrMsg, db, "%s constraint failed", zType);
67373     }
67374     sqlite3_log(pOp->p1, zLogFmt, pc, p->zSql, p->zErrMsg);
67375   }
67376   rc = sqlite3VdbeHalt(p);
67377   assert( rc==SQLITE_BUSY || rc==SQLITE_OK || rc==SQLITE_ERROR );
67378   if( rc==SQLITE_BUSY ){
67379     p->rc = rc = SQLITE_BUSY;
67380   }else{
67381     assert( rc==SQLITE_OK || (p->rc&0xff)==SQLITE_CONSTRAINT );
67382     assert( rc==SQLITE_OK || db->nDeferredCons>0 || db->nDeferredImmCons>0 );
67383     rc = p->rc ? SQLITE_ERROR : SQLITE_DONE;
67384   }
67385   goto vdbe_return;
67386 }
67387 
67388 /* Opcode: Integer P1 P2 * * *
67389 ** Synopsis: r[P2]=P1
67390 **
67391 ** The 32-bit integer value P1 is written into register P2.
67392 */
67393 case OP_Integer: {         /* out2-prerelease */
67394   pOut->u.i = pOp->p1;
67395   break;
67396 }
67397 
67398 /* Opcode: Int64 * P2 * P4 *
67399 ** Synopsis: r[P2]=P4
67400 **
67401 ** P4 is a pointer to a 64-bit integer value.
67402 ** Write that value into register P2.
67403 */
67404 case OP_Int64: {           /* out2-prerelease */
67405   assert( pOp->p4.pI64!=0 );
67406   pOut->u.i = *pOp->p4.pI64;
67407   break;
67408 }
67409 
67410 #ifndef SQLITE_OMIT_FLOATING_POINT
67411 /* Opcode: Real * P2 * P4 *
67412 ** Synopsis: r[P2]=P4
67413 **
67414 ** P4 is a pointer to a 64-bit floating point value.
67415 ** Write that value into register P2.
67416 */
67417 case OP_Real: {            /* same as TK_FLOAT, out2-prerelease */
67418   pOut->flags = MEM_Real;
67419   assert( !sqlite3IsNaN(*pOp->p4.pReal) );
67420   pOut->r = *pOp->p4.pReal;
67421   break;
67422 }
67423 #endif
67424 
67425 /* Opcode: String8 * P2 * P4 *
67426 ** Synopsis: r[P2]='P4'
67427 **
67428 ** P4 points to a nul terminated UTF-8 string. This opcode is transformed
67429 ** into an OP_String before it is executed for the first time.
67430 */
67431 case OP_String8: {         /* same as TK_STRING, out2-prerelease */
67432   assert( pOp->p4.z!=0 );
67433   pOp->opcode = OP_String;
67434   pOp->p1 = sqlite3Strlen30(pOp->p4.z);
67435 
67436 #ifndef SQLITE_OMIT_UTF16
67437   if( encoding!=SQLITE_UTF8 ){
67438     rc = sqlite3VdbeMemSetStr(pOut, pOp->p4.z, -1, SQLITE_UTF8, SQLITE_STATIC);
67439     if( rc==SQLITE_TOOBIG ) goto too_big;
67440     if( SQLITE_OK!=sqlite3VdbeChangeEncoding(pOut, encoding) ) goto no_mem;
67441     assert( pOut->zMalloc==pOut->z );
67442     assert( pOut->flags & MEM_Dyn );
67443     pOut->zMalloc = 0;
67444     pOut->flags |= MEM_Static;
67445     pOut->flags &= ~MEM_Dyn;
67446     if( pOp->p4type==P4_DYNAMIC ){
67447       sqlite3DbFree(db, pOp->p4.z);
67448     }
67449     pOp->p4type = P4_DYNAMIC;
67450     pOp->p4.z = pOut->z;
67451     pOp->p1 = pOut->n;
67452   }
67453 #endif
67454   if( pOp->p1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
67455     goto too_big;
67456   }
67457   /* Fall through to the next case, OP_String */
67458 }
67459 
67460 /* Opcode: String P1 P2 * P4 *
67461 ** Synopsis: r[P2]='P4' (len=P1)
67462 **
67463 ** The string value P4 of length P1 (bytes) is stored in register P2.
67464 */
67465 case OP_String: {          /* out2-prerelease */
67466   assert( pOp->p4.z!=0 );
67467   pOut->flags = MEM_Str|MEM_Static|MEM_Term;
67468   pOut->z = pOp->p4.z;
67469   pOut->n = pOp->p1;
67470   pOut->enc = encoding;
67471   UPDATE_MAX_BLOBSIZE(pOut);
67472   break;
67473 }
67474 
67475 /* Opcode: Null P1 P2 P3 * *
67476 ** Synopsis:  r[P2..P3]=NULL
67477 **
67478 ** Write a NULL into registers P2.  If P3 greater than P2, then also write
67479 ** NULL into register P3 and every register in between P2 and P3.  If P3
67480 ** is less than P2 (typically P3 is zero) then only register P2 is
67481 ** set to NULL.
67482 **
67483 ** If the P1 value is non-zero, then also set the MEM_Cleared flag so that
67484 ** NULL values will not compare equal even if SQLITE_NULLEQ is set on
67485 ** OP_Ne or OP_Eq.
67486 */
67487 case OP_Null: {           /* out2-prerelease */
67488   int cnt;
67489   u16 nullFlag;
67490   cnt = pOp->p3-pOp->p2;
67491   assert( pOp->p3<=(p->nMem-p->nCursor) );
67492   pOut->flags = nullFlag = pOp->p1 ? (MEM_Null|MEM_Cleared) : MEM_Null;
67493   while( cnt>0 ){
67494     pOut++;
67495     memAboutToChange(p, pOut);
67496     VdbeMemRelease(pOut);
67497     pOut->flags = nullFlag;
67498     cnt--;
67499   }
67500   break;
67501 }
67502 
67503 
67504 /* Opcode: Blob P1 P2 * P4
67505 ** Synopsis: r[P2]=P4 (len=P1)
67506 **
67507 ** P4 points to a blob of data P1 bytes long.  Store this
67508 ** blob in register P2.
67509 */
67510 case OP_Blob: {                /* out2-prerelease */
67511   assert( pOp->p1 <= SQLITE_MAX_LENGTH );
67512   sqlite3VdbeMemSetStr(pOut, pOp->p4.z, pOp->p1, 0, 0);
67513   pOut->enc = encoding;
67514   UPDATE_MAX_BLOBSIZE(pOut);
67515   break;
67516 }
67517 
67518 /* Opcode: Variable P1 P2 * P4 *
67519 ** Synopsis: r[P2]=parameter(P1,P4)
67520 **
67521 ** Transfer the values of bound parameter P1 into register P2
67522 **
67523 ** If the parameter is named, then its name appears in P4 and P3==1.
67524 ** The P4 value is used by sqlite3_bind_parameter_name().
67525 */
67526 case OP_Variable: {            /* out2-prerelease */
67527   Mem *pVar;       /* Value being transferred */
67528 
67529   assert( pOp->p1>0 && pOp->p1<=p->nVar );
67530   assert( pOp->p4.z==0 || pOp->p4.z==p->azVar[pOp->p1-1] );
67531   pVar = &p->aVar[pOp->p1 - 1];
67532   if( sqlite3VdbeMemTooBig(pVar) ){
67533     goto too_big;
67534   }
67535   sqlite3VdbeMemShallowCopy(pOut, pVar, MEM_Static);
67536   UPDATE_MAX_BLOBSIZE(pOut);
67537   break;
67538 }
67539 
67540 /* Opcode: Move P1 P2 P3 * *
67541 ** Synopsis:  r[P2@P3]=r[P1@P3]
67542 **
67543 ** Move the values in register P1..P1+P3 over into
67544 ** registers P2..P2+P3.  Registers P1..P1+P3 are
67545 ** left holding a NULL.  It is an error for register ranges
67546 ** P1..P1+P3 and P2..P2+P3 to overlap.
67547 */
67548 case OP_Move: {
67549   char *zMalloc;   /* Holding variable for allocated memory */
67550   int n;           /* Number of registers left to copy */
67551   int p1;          /* Register to copy from */
67552   int p2;          /* Register to copy to */
67553 
67554   n = pOp->p3;
67555   p1 = pOp->p1;
67556   p2 = pOp->p2;
67557   assert( n>=0 && p1>0 && p2>0 );
67558   assert( p1+n<=p2 || p2+n<=p1 );
67559 
67560   pIn1 = &aMem[p1];
67561   pOut = &aMem[p2];
67562   do{
67563     assert( pOut<=&aMem[(p->nMem-p->nCursor)] );
67564     assert( pIn1<=&aMem[(p->nMem-p->nCursor)] );
67565     assert( memIsValid(pIn1) );
67566     memAboutToChange(p, pOut);
67567     zMalloc = pOut->zMalloc;
67568     pOut->zMalloc = 0;
67569     sqlite3VdbeMemMove(pOut, pIn1);
67570 #ifdef SQLITE_DEBUG
67571     if( pOut->pScopyFrom>=&aMem[p1] && pOut->pScopyFrom<&aMem[p1+pOp->p3] ){
67572       pOut->pScopyFrom += p1 - pOp->p2;
67573     }
67574 #endif
67575     pIn1->zMalloc = zMalloc;
67576     REGISTER_TRACE(p2++, pOut);
67577     pIn1++;
67578     pOut++;
67579   }while( n-- );
67580   break;
67581 }
67582 
67583 /* Opcode: Copy P1 P2 P3 * *
67584 ** Synopsis: r[P2@P3+1]=r[P1@P3+1]
67585 **
67586 ** Make a copy of registers P1..P1+P3 into registers P2..P2+P3.
67587 **
67588 ** This instruction makes a deep copy of the value.  A duplicate
67589 ** is made of any string or blob constant.  See also OP_SCopy.
67590 */
67591 case OP_Copy: {
67592   int n;
67593 
67594   n = pOp->p3;
67595   pIn1 = &aMem[pOp->p1];
67596   pOut = &aMem[pOp->p2];
67597   assert( pOut!=pIn1 );
67598   while( 1 ){
67599     sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
67600     Deephemeralize(pOut);
67601 #ifdef SQLITE_DEBUG
67602     pOut->pScopyFrom = 0;
67603 #endif
67604     REGISTER_TRACE(pOp->p2+pOp->p3-n, pOut);
67605     if( (n--)==0 ) break;
67606     pOut++;
67607     pIn1++;
67608   }
67609   break;
67610 }
67611 
67612 /* Opcode: SCopy P1 P2 * * *
67613 ** Synopsis: r[P2]=r[P1]
67614 **
67615 ** Make a shallow copy of register P1 into register P2.
67616 **
67617 ** This instruction makes a shallow copy of the value.  If the value
67618 ** is a string or blob, then the copy is only a pointer to the
67619 ** original and hence if the original changes so will the copy.
67620 ** Worse, if the original is deallocated, the copy becomes invalid.
67621 ** Thus the program must guarantee that the original will not change
67622 ** during the lifetime of the copy.  Use OP_Copy to make a complete
67623 ** copy.
67624 */
67625 case OP_SCopy: {            /* out2 */
67626   pIn1 = &aMem[pOp->p1];
67627   pOut = &aMem[pOp->p2];
67628   assert( pOut!=pIn1 );
67629   sqlite3VdbeMemShallowCopy(pOut, pIn1, MEM_Ephem);
67630 #ifdef SQLITE_DEBUG
67631   if( pOut->pScopyFrom==0 ) pOut->pScopyFrom = pIn1;
67632 #endif
67633   break;
67634 }
67635 
67636 /* Opcode: ResultRow P1 P2 * * *
67637 ** Synopsis:  output=r[P1@P2]
67638 **
67639 ** The registers P1 through P1+P2-1 contain a single row of
67640 ** results. This opcode causes the sqlite3_step() call to terminate
67641 ** with an SQLITE_ROW return code and it sets up the sqlite3_stmt
67642 ** structure to provide access to the top P1 values as the result
67643 ** row.
67644 */
67645 case OP_ResultRow: {
67646   Mem *pMem;
67647   int i;
67648   assert( p->nResColumn==pOp->p2 );
67649   assert( pOp->p1>0 );
67650   assert( pOp->p1+pOp->p2<=(p->nMem-p->nCursor)+1 );
67651 
67652 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
67653   /* Run the progress counter just before returning.
67654   */
67655   if( db->xProgress!=0
67656    && nVmStep>=nProgressLimit
67657    && db->xProgress(db->pProgressArg)!=0
67658   ){
67659     rc = SQLITE_INTERRUPT;
67660     goto vdbe_error_halt;
67661   }
67662 #endif
67663 
67664   /* If this statement has violated immediate foreign key constraints, do
67665   ** not return the number of rows modified. And do not RELEASE the statement
67666   ** transaction. It needs to be rolled back.  */
67667   if( SQLITE_OK!=(rc = sqlite3VdbeCheckFk(p, 0)) ){
67668     assert( db->flags&SQLITE_CountRows );
67669     assert( p->usesStmtJournal );
67670     break;
67671   }
67672 
67673   /* If the SQLITE_CountRows flag is set in sqlite3.flags mask, then
67674   ** DML statements invoke this opcode to return the number of rows
67675   ** modified to the user. This is the only way that a VM that
67676   ** opens a statement transaction may invoke this opcode.
67677   **
67678   ** In case this is such a statement, close any statement transaction
67679   ** opened by this VM before returning control to the user. This is to
67680   ** ensure that statement-transactions are always nested, not overlapping.
67681   ** If the open statement-transaction is not closed here, then the user
67682   ** may step another VM that opens its own statement transaction. This
67683   ** may lead to overlapping statement transactions.
67684   **
67685   ** The statement transaction is never a top-level transaction.  Hence
67686   ** the RELEASE call below can never fail.
67687   */
67688   assert( p->iStatement==0 || db->flags&SQLITE_CountRows );
67689   rc = sqlite3VdbeCloseStatement(p, SAVEPOINT_RELEASE);
67690   if( NEVER(rc!=SQLITE_OK) ){
67691     break;
67692   }
67693 
67694   /* Invalidate all ephemeral cursor row caches */
67695   p->cacheCtr = (p->cacheCtr + 2)|1;
67696 
67697   /* Make sure the results of the current row are \000 terminated
67698   ** and have an assigned type.  The results are de-ephemeralized as
67699   ** a side effect.
67700   */
67701   pMem = p->pResultSet = &aMem[pOp->p1];
67702   for(i=0; i<pOp->p2; i++){
67703     assert( memIsValid(&pMem[i]) );
67704     Deephemeralize(&pMem[i]);
67705     assert( (pMem[i].flags & MEM_Ephem)==0
67706             || (pMem[i].flags & (MEM_Str|MEM_Blob))==0 );
67707     sqlite3VdbeMemNulTerminate(&pMem[i]);
67708     sqlite3VdbeMemStoreType(&pMem[i]);
67709     REGISTER_TRACE(pOp->p1+i, &pMem[i]);
67710   }
67711   if( db->mallocFailed ) goto no_mem;
67712 
67713   /* Return SQLITE_ROW
67714   */
67715   p->pc = pc + 1;
67716   rc = SQLITE_ROW;
67717   goto vdbe_return;
67718 }
67719 
67720 /* Opcode: Concat P1 P2 P3 * *
67721 ** Synopsis: r[P3]=r[P2]+r[P1]
67722 **
67723 ** Add the text in register P1 onto the end of the text in
67724 ** register P2 and store the result in register P3.
67725 ** If either the P1 or P2 text are NULL then store NULL in P3.
67726 **
67727 **   P3 = P2 || P1
67728 **
67729 ** It is illegal for P1 and P3 to be the same register. Sometimes,
67730 ** if P3 is the same register as P2, the implementation is able
67731 ** to avoid a memcpy().
67732 */
67733 case OP_Concat: {           /* same as TK_CONCAT, in1, in2, out3 */
67734   i64 nByte;
67735 
67736   pIn1 = &aMem[pOp->p1];
67737   pIn2 = &aMem[pOp->p2];
67738   pOut = &aMem[pOp->p3];
67739   assert( pIn1!=pOut );
67740   if( (pIn1->flags | pIn2->flags) & MEM_Null ){
67741     sqlite3VdbeMemSetNull(pOut);
67742     break;
67743   }
67744   if( ExpandBlob(pIn1) || ExpandBlob(pIn2) ) goto no_mem;
67745   Stringify(pIn1, encoding);
67746   Stringify(pIn2, encoding);
67747   nByte = pIn1->n + pIn2->n;
67748   if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
67749     goto too_big;
67750   }
67751   MemSetTypeFlag(pOut, MEM_Str);
67752   if( sqlite3VdbeMemGrow(pOut, (int)nByte+2, pOut==pIn2) ){
67753     goto no_mem;
67754   }
67755   if( pOut!=pIn2 ){
67756     memcpy(pOut->z, pIn2->z, pIn2->n);
67757   }
67758   memcpy(&pOut->z[pIn2->n], pIn1->z, pIn1->n);
67759   pOut->z[nByte]=0;
67760   pOut->z[nByte+1] = 0;
67761   pOut->flags |= MEM_Term;
67762   pOut->n = (int)nByte;
67763   pOut->enc = encoding;
67764   UPDATE_MAX_BLOBSIZE(pOut);
67765   break;
67766 }
67767 
67768 /* Opcode: Add P1 P2 P3 * *
67769 ** Synopsis:  r[P3]=r[P1]+r[P2]
67770 **
67771 ** Add the value in register P1 to the value in register P2
67772 ** and store the result in register P3.
67773 ** If either input is NULL, the result is NULL.
67774 */
67775 /* Opcode: Multiply P1 P2 P3 * *
67776 ** Synopsis:  r[P3]=r[P1]*r[P2]
67777 **
67778 **
67779 ** Multiply the value in register P1 by the value in register P2
67780 ** and store the result in register P3.
67781 ** If either input is NULL, the result is NULL.
67782 */
67783 /* Opcode: Subtract P1 P2 P3 * *
67784 ** Synopsis:  r[P3]=r[P2]-r[P1]
67785 **
67786 ** Subtract the value in register P1 from the value in register P2
67787 ** and store the result in register P3.
67788 ** If either input is NULL, the result is NULL.
67789 */
67790 /* Opcode: Divide P1 P2 P3 * *
67791 ** Synopsis:  r[P3]=r[P2]/r[P1]
67792 **
67793 ** Divide the value in register P1 by the value in register P2
67794 ** and store the result in register P3 (P3=P2/P1). If the value in
67795 ** register P1 is zero, then the result is NULL. If either input is
67796 ** NULL, the result is NULL.
67797 */
67798 /* Opcode: Remainder P1 P2 P3 * *
67799 ** Synopsis:  r[P3]=r[P2]%r[P1]
67800 **
67801 ** Compute the remainder after integer register P2 is divided by
67802 ** register P1 and store the result in register P3.
67803 ** If the value in register P1 is zero the result is NULL.
67804 ** If either operand is NULL, the result is NULL.
67805 */
67806 case OP_Add:                   /* same as TK_PLUS, in1, in2, out3 */
67807 case OP_Subtract:              /* same as TK_MINUS, in1, in2, out3 */
67808 case OP_Multiply:              /* same as TK_STAR, in1, in2, out3 */
67809 case OP_Divide:                /* same as TK_SLASH, in1, in2, out3 */
67810 case OP_Remainder: {           /* same as TK_REM, in1, in2, out3 */
67811   char bIntint;   /* Started out as two integer operands */
67812   int flags;      /* Combined MEM_* flags from both inputs */
67813   i64 iA;         /* Integer value of left operand */
67814   i64 iB;         /* Integer value of right operand */
67815   double rA;      /* Real value of left operand */
67816   double rB;      /* Real value of right operand */
67817 
67818   pIn1 = &aMem[pOp->p1];
67819   applyNumericAffinity(pIn1);
67820   pIn2 = &aMem[pOp->p2];
67821   applyNumericAffinity(pIn2);
67822   pOut = &aMem[pOp->p3];
67823   flags = pIn1->flags | pIn2->flags;
67824   if( (flags & MEM_Null)!=0 ) goto arithmetic_result_is_null;
67825   if( (pIn1->flags & pIn2->flags & MEM_Int)==MEM_Int ){
67826     iA = pIn1->u.i;
67827     iB = pIn2->u.i;
67828     bIntint = 1;
67829     switch( pOp->opcode ){
67830       case OP_Add:       if( sqlite3AddInt64(&iB,iA) ) goto fp_math;  break;
67831       case OP_Subtract:  if( sqlite3SubInt64(&iB,iA) ) goto fp_math;  break;
67832       case OP_Multiply:  if( sqlite3MulInt64(&iB,iA) ) goto fp_math;  break;
67833       case OP_Divide: {
67834         if( iA==0 ) goto arithmetic_result_is_null;
67835         if( iA==-1 && iB==SMALLEST_INT64 ) goto fp_math;
67836         iB /= iA;
67837         break;
67838       }
67839       default: {
67840         if( iA==0 ) goto arithmetic_result_is_null;
67841         if( iA==-1 ) iA = 1;
67842         iB %= iA;
67843         break;
67844       }
67845     }
67846     pOut->u.i = iB;
67847     MemSetTypeFlag(pOut, MEM_Int);
67848   }else{
67849     bIntint = 0;
67850 fp_math:
67851     rA = sqlite3VdbeRealValue(pIn1);
67852     rB = sqlite3VdbeRealValue(pIn2);
67853     switch( pOp->opcode ){
67854       case OP_Add:         rB += rA;       break;
67855       case OP_Subtract:    rB -= rA;       break;
67856       case OP_Multiply:    rB *= rA;       break;
67857       case OP_Divide: {
67858         /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
67859         if( rA==(double)0 ) goto arithmetic_result_is_null;
67860         rB /= rA;
67861         break;
67862       }
67863       default: {
67864         iA = (i64)rA;
67865         iB = (i64)rB;
67866         if( iA==0 ) goto arithmetic_result_is_null;
67867         if( iA==-1 ) iA = 1;
67868         rB = (double)(iB % iA);
67869         break;
67870       }
67871     }
67872 #ifdef SQLITE_OMIT_FLOATING_POINT
67873     pOut->u.i = rB;
67874     MemSetTypeFlag(pOut, MEM_Int);
67875 #else
67876     if( sqlite3IsNaN(rB) ){
67877       goto arithmetic_result_is_null;
67878     }
67879     pOut->r = rB;
67880     MemSetTypeFlag(pOut, MEM_Real);
67881     if( (flags & MEM_Real)==0 && !bIntint ){
67882       sqlite3VdbeIntegerAffinity(pOut);
67883     }
67884 #endif
67885   }
67886   break;
67887 
67888 arithmetic_result_is_null:
67889   sqlite3VdbeMemSetNull(pOut);
67890   break;
67891 }
67892 
67893 /* Opcode: CollSeq P1 * * P4
67894 **
67895 ** P4 is a pointer to a CollSeq struct. If the next call to a user function
67896 ** or aggregate calls sqlite3GetFuncCollSeq(), this collation sequence will
67897 ** be returned. This is used by the built-in min(), max() and nullif()
67898 ** functions.
67899 **
67900 ** If P1 is not zero, then it is a register that a subsequent min() or
67901 ** max() aggregate will set to 1 if the current row is not the minimum or
67902 ** maximum.  The P1 register is initialized to 0 by this instruction.
67903 **
67904 ** The interface used by the implementation of the aforementioned functions
67905 ** to retrieve the collation sequence set by this opcode is not available
67906 ** publicly, only to user functions defined in func.c.
67907 */
67908 case OP_CollSeq: {
67909   assert( pOp->p4type==P4_COLLSEQ );
67910   if( pOp->p1 ){
67911     sqlite3VdbeMemSetInt64(&aMem[pOp->p1], 0);
67912   }
67913   break;
67914 }
67915 
67916 /* Opcode: Function P1 P2 P3 P4 P5
67917 ** Synopsis: r[P3]=func(r[P2@P5])
67918 **
67919 ** Invoke a user function (P4 is a pointer to a Function structure that
67920 ** defines the function) with P5 arguments taken from register P2 and
67921 ** successors.  The result of the function is stored in register P3.
67922 ** Register P3 must not be one of the function inputs.
67923 **
67924 ** P1 is a 32-bit bitmask indicating whether or not each argument to the
67925 ** function was determined to be constant at compile time. If the first
67926 ** argument was constant then bit 0 of P1 is set. This is used to determine
67927 ** whether meta data associated with a user function argument using the
67928 ** sqlite3_set_auxdata() API may be safely retained until the next
67929 ** invocation of this opcode.
67930 **
67931 ** See also: AggStep and AggFinal
67932 */
67933 case OP_Function: {
67934   int i;
67935   Mem *pArg;
67936   sqlite3_context ctx;
67937   sqlite3_value **apVal;
67938   int n;
67939 
67940   n = pOp->p5;
67941   apVal = p->apArg;
67942   assert( apVal || n==0 );
67943   assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
67944   pOut = &aMem[pOp->p3];
67945   memAboutToChange(p, pOut);
67946 
67947   assert( n==0 || (pOp->p2>0 && pOp->p2+n<=(p->nMem-p->nCursor)+1) );
67948   assert( pOp->p3<pOp->p2 || pOp->p3>=pOp->p2+n );
67949   pArg = &aMem[pOp->p2];
67950   for(i=0; i<n; i++, pArg++){
67951     assert( memIsValid(pArg) );
67952     apVal[i] = pArg;
67953     Deephemeralize(pArg);
67954     sqlite3VdbeMemStoreType(pArg);
67955     REGISTER_TRACE(pOp->p2+i, pArg);
67956   }
67957 
67958   assert( pOp->p4type==P4_FUNCDEF );
67959   ctx.pFunc = pOp->p4.pFunc;
67960   ctx.iOp = pc;
67961   ctx.pVdbe = p;
67962 
67963   /* The output cell may already have a buffer allocated. Move
67964   ** the pointer to ctx.s so in case the user-function can use
67965   ** the already allocated buffer instead of allocating a new one.
67966   */
67967   memcpy(&ctx.s, pOut, sizeof(Mem));
67968   pOut->flags = MEM_Null;
67969   pOut->xDel = 0;
67970   pOut->zMalloc = 0;
67971   MemSetTypeFlag(&ctx.s, MEM_Null);
67972 
67973   ctx.fErrorOrAux = 0;
67974   if( ctx.pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL ){
67975     assert( pOp>aOp );
67976     assert( pOp[-1].p4type==P4_COLLSEQ );
67977     assert( pOp[-1].opcode==OP_CollSeq );
67978     ctx.pColl = pOp[-1].p4.pColl;
67979   }
67980   db->lastRowid = lastRowid;
67981   (*ctx.pFunc->xFunc)(&ctx, n, apVal); /* IMP: R-24505-23230 */
67982   lastRowid = db->lastRowid;
67983 
67984   if( db->mallocFailed ){
67985     /* Even though a malloc() has failed, the implementation of the
67986     ** user function may have called an sqlite3_result_XXX() function
67987     ** to return a value. The following call releases any resources
67988     ** associated with such a value.
67989     */
67990     sqlite3VdbeMemRelease(&ctx.s);
67991     goto no_mem;
67992   }
67993 
67994   /* If the function returned an error, throw an exception */
67995   if( ctx.fErrorOrAux ){
67996     if( ctx.isError ){
67997       sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&ctx.s));
67998       rc = ctx.isError;
67999     }
68000     sqlite3VdbeDeleteAuxData(p, pc, pOp->p1);
68001   }
68002 
68003   /* Copy the result of the function into register P3 */
68004   sqlite3VdbeChangeEncoding(&ctx.s, encoding);
68005   assert( pOut->flags==MEM_Null );
68006   memcpy(pOut, &ctx.s, sizeof(Mem));
68007   if( sqlite3VdbeMemTooBig(pOut) ){
68008     goto too_big;
68009   }
68010 
68011 #if 0
68012   /* The app-defined function has done something that as caused this
68013   ** statement to expire.  (Perhaps the function called sqlite3_exec()
68014   ** with a CREATE TABLE statement.)
68015   */
68016   if( p->expired ) rc = SQLITE_ABORT;
68017 #endif
68018 
68019   REGISTER_TRACE(pOp->p3, pOut);
68020   UPDATE_MAX_BLOBSIZE(pOut);
68021   break;
68022 }
68023 
68024 /* Opcode: BitAnd P1 P2 P3 * *
68025 ** Synopsis:  r[P3]=r[P1]&r[P2]
68026 **
68027 ** Take the bit-wise AND of the values in register P1 and P2 and
68028 ** store the result in register P3.
68029 ** If either input is NULL, the result is NULL.
68030 */
68031 /* Opcode: BitOr P1 P2 P3 * *
68032 ** Synopsis:  r[P3]=r[P1]|r[P2]
68033 **
68034 ** Take the bit-wise OR of the values in register P1 and P2 and
68035 ** store the result in register P3.
68036 ** If either input is NULL, the result is NULL.
68037 */
68038 /* Opcode: ShiftLeft P1 P2 P3 * *
68039 ** Synopsis:  r[P3]=r[P2]<<r[P1]
68040 **
68041 ** Shift the integer value in register P2 to the left by the
68042 ** number of bits specified by the integer in register P1.
68043 ** Store the result in register P3.
68044 ** If either input is NULL, the result is NULL.
68045 */
68046 /* Opcode: ShiftRight P1 P2 P3 * *
68047 ** Synopsis:  r[P3]=r[P2]>>r[P1]
68048 **
68049 ** Shift the integer value in register P2 to the right by the
68050 ** number of bits specified by the integer in register P1.
68051 ** Store the result in register P3.
68052 ** If either input is NULL, the result is NULL.
68053 */
68054 case OP_BitAnd:                 /* same as TK_BITAND, in1, in2, out3 */
68055 case OP_BitOr:                  /* same as TK_BITOR, in1, in2, out3 */
68056 case OP_ShiftLeft:              /* same as TK_LSHIFT, in1, in2, out3 */
68057 case OP_ShiftRight: {           /* same as TK_RSHIFT, in1, in2, out3 */
68058   i64 iA;
68059   u64 uA;
68060   i64 iB;
68061   u8 op;
68062 
68063   pIn1 = &aMem[pOp->p1];
68064   pIn2 = &aMem[pOp->p2];
68065   pOut = &aMem[pOp->p3];
68066   if( (pIn1->flags | pIn2->flags) & MEM_Null ){
68067     sqlite3VdbeMemSetNull(pOut);
68068     break;
68069   }
68070   iA = sqlite3VdbeIntValue(pIn2);
68071   iB = sqlite3VdbeIntValue(pIn1);
68072   op = pOp->opcode;
68073   if( op==OP_BitAnd ){
68074     iA &= iB;
68075   }else if( op==OP_BitOr ){
68076     iA |= iB;
68077   }else if( iB!=0 ){
68078     assert( op==OP_ShiftRight || op==OP_ShiftLeft );
68079 
68080     /* If shifting by a negative amount, shift in the other direction */
68081     if( iB<0 ){
68082       assert( OP_ShiftRight==OP_ShiftLeft+1 );
68083       op = 2*OP_ShiftLeft + 1 - op;
68084       iB = iB>(-64) ? -iB : 64;
68085     }
68086 
68087     if( iB>=64 ){
68088       iA = (iA>=0 || op==OP_ShiftLeft) ? 0 : -1;
68089     }else{
68090       memcpy(&uA, &iA, sizeof(uA));
68091       if( op==OP_ShiftLeft ){
68092         uA <<= iB;
68093       }else{
68094         uA >>= iB;
68095         /* Sign-extend on a right shift of a negative number */
68096         if( iA<0 ) uA |= ((((u64)0xffffffff)<<32)|0xffffffff) << (64-iB);
68097       }
68098       memcpy(&iA, &uA, sizeof(iA));
68099     }
68100   }
68101   pOut->u.i = iA;
68102   MemSetTypeFlag(pOut, MEM_Int);
68103   break;
68104 }
68105 
68106 /* Opcode: AddImm  P1 P2 * * *
68107 ** Synopsis:  r[P1]=r[P1]+P2
68108 **
68109 ** Add the constant P2 to the value in register P1.
68110 ** The result is always an integer.
68111 **
68112 ** To force any register to be an integer, just add 0.
68113 */
68114 case OP_AddImm: {            /* in1 */
68115   pIn1 = &aMem[pOp->p1];
68116   memAboutToChange(p, pIn1);
68117   sqlite3VdbeMemIntegerify(pIn1);
68118   pIn1->u.i += pOp->p2;
68119   break;
68120 }
68121 
68122 /* Opcode: MustBeInt P1 P2 * * *
68123 **
68124 ** Force the value in register P1 to be an integer.  If the value
68125 ** in P1 is not an integer and cannot be converted into an integer
68126 ** without data loss, then jump immediately to P2, or if P2==0
68127 ** raise an SQLITE_MISMATCH exception.
68128 */
68129 case OP_MustBeInt: {            /* jump, in1 */
68130   pIn1 = &aMem[pOp->p1];
68131   if( (pIn1->flags & MEM_Int)==0 ){
68132     applyAffinity(pIn1, SQLITE_AFF_NUMERIC, encoding);
68133     if( (pIn1->flags & MEM_Int)==0 ){
68134       if( pOp->p2==0 ){
68135         rc = SQLITE_MISMATCH;
68136         goto abort_due_to_error;
68137       }else{
68138         pc = pOp->p2 - 1;
68139         break;
68140       }
68141     }
68142   }
68143   MemSetTypeFlag(pIn1, MEM_Int);
68144   break;
68145 }
68146 
68147 #ifndef SQLITE_OMIT_FLOATING_POINT
68148 /* Opcode: RealAffinity P1 * * * *
68149 **
68150 ** If register P1 holds an integer convert it to a real value.
68151 **
68152 ** This opcode is used when extracting information from a column that
68153 ** has REAL affinity.  Such column values may still be stored as
68154 ** integers, for space efficiency, but after extraction we want them
68155 ** to have only a real value.
68156 */
68157 case OP_RealAffinity: {                  /* in1 */
68158   pIn1 = &aMem[pOp->p1];
68159   if( pIn1->flags & MEM_Int ){
68160     sqlite3VdbeMemRealify(pIn1);
68161   }
68162   break;
68163 }
68164 #endif
68165 
68166 #ifndef SQLITE_OMIT_CAST
68167 /* Opcode: ToText P1 * * * *
68168 **
68169 ** Force the value in register P1 to be text.
68170 ** If the value is numeric, convert it to a string using the
68171 ** equivalent of printf().  Blob values are unchanged and
68172 ** are afterwards simply interpreted as text.
68173 **
68174 ** A NULL value is not changed by this routine.  It remains NULL.
68175 */
68176 case OP_ToText: {                  /* same as TK_TO_TEXT, in1 */
68177   pIn1 = &aMem[pOp->p1];
68178   memAboutToChange(p, pIn1);
68179   if( pIn1->flags & MEM_Null ) break;
68180   assert( MEM_Str==(MEM_Blob>>3) );
68181   pIn1->flags |= (pIn1->flags&MEM_Blob)>>3;
68182   applyAffinity(pIn1, SQLITE_AFF_TEXT, encoding);
68183   rc = ExpandBlob(pIn1);
68184   assert( pIn1->flags & MEM_Str || db->mallocFailed );
68185   pIn1->flags &= ~(MEM_Int|MEM_Real|MEM_Blob|MEM_Zero);
68186   UPDATE_MAX_BLOBSIZE(pIn1);
68187   break;
68188 }
68189 
68190 /* Opcode: ToBlob P1 * * * *
68191 **
68192 ** Force the value in register P1 to be a BLOB.
68193 ** If the value is numeric, convert it to a string first.
68194 ** Strings are simply reinterpreted as blobs with no change
68195 ** to the underlying data.
68196 **
68197 ** A NULL value is not changed by this routine.  It remains NULL.
68198 */
68199 case OP_ToBlob: {                  /* same as TK_TO_BLOB, in1 */
68200   pIn1 = &aMem[pOp->p1];
68201   if( pIn1->flags & MEM_Null ) break;
68202   if( (pIn1->flags & MEM_Blob)==0 ){
68203     applyAffinity(pIn1, SQLITE_AFF_TEXT, encoding);
68204     assert( pIn1->flags & MEM_Str || db->mallocFailed );
68205     MemSetTypeFlag(pIn1, MEM_Blob);
68206   }else{
68207     pIn1->flags &= ~(MEM_TypeMask&~MEM_Blob);
68208   }
68209   UPDATE_MAX_BLOBSIZE(pIn1);
68210   break;
68211 }
68212 
68213 /* Opcode: ToNumeric P1 * * * *
68214 **
68215 ** Force the value in register P1 to be numeric (either an
68216 ** integer or a floating-point number.)
68217 ** If the value is text or blob, try to convert it to an using the
68218 ** equivalent of atoi() or atof() and store 0 if no such conversion
68219 ** is possible.
68220 **
68221 ** A NULL value is not changed by this routine.  It remains NULL.
68222 */
68223 case OP_ToNumeric: {                  /* same as TK_TO_NUMERIC, in1 */
68224   pIn1 = &aMem[pOp->p1];
68225   sqlite3VdbeMemNumerify(pIn1);
68226   break;
68227 }
68228 #endif /* SQLITE_OMIT_CAST */
68229 
68230 /* Opcode: ToInt P1 * * * *
68231 **
68232 ** Force the value in register P1 to be an integer.  If
68233 ** The value is currently a real number, drop its fractional part.
68234 ** If the value is text or blob, try to convert it to an integer using the
68235 ** equivalent of atoi() and store 0 if no such conversion is possible.
68236 **
68237 ** A NULL value is not changed by this routine.  It remains NULL.
68238 */
68239 case OP_ToInt: {                  /* same as TK_TO_INT, in1 */
68240   pIn1 = &aMem[pOp->p1];
68241   if( (pIn1->flags & MEM_Null)==0 ){
68242     sqlite3VdbeMemIntegerify(pIn1);
68243   }
68244   break;
68245 }
68246 
68247 #if !defined(SQLITE_OMIT_CAST) && !defined(SQLITE_OMIT_FLOATING_POINT)
68248 /* Opcode: ToReal P1 * * * *
68249 **
68250 ** Force the value in register P1 to be a floating point number.
68251 ** If The value is currently an integer, convert it.
68252 ** If the value is text or blob, try to convert it to an integer using the
68253 ** equivalent of atoi() and store 0.0 if no such conversion is possible.
68254 **
68255 ** A NULL value is not changed by this routine.  It remains NULL.
68256 */
68257 case OP_ToReal: {                  /* same as TK_TO_REAL, in1 */
68258   pIn1 = &aMem[pOp->p1];
68259   memAboutToChange(p, pIn1);
68260   if( (pIn1->flags & MEM_Null)==0 ){
68261     sqlite3VdbeMemRealify(pIn1);
68262   }
68263   break;
68264 }
68265 #endif /* !defined(SQLITE_OMIT_CAST) && !defined(SQLITE_OMIT_FLOATING_POINT) */
68266 
68267 /* Opcode: Lt P1 P2 P3 P4 P5
68268 ** Synopsis: if r[P1]<r[P3] goto P2
68269 **
68270 ** Compare the values in register P1 and P3.  If reg(P3)<reg(P1) then
68271 ** jump to address P2.
68272 **
68273 ** If the SQLITE_JUMPIFNULL bit of P5 is set and either reg(P1) or
68274 ** reg(P3) is NULL then take the jump.  If the SQLITE_JUMPIFNULL
68275 ** bit is clear then fall through if either operand is NULL.
68276 **
68277 ** The SQLITE_AFF_MASK portion of P5 must be an affinity character -
68278 ** SQLITE_AFF_TEXT, SQLITE_AFF_INTEGER, and so forth. An attempt is made
68279 ** to coerce both inputs according to this affinity before the
68280 ** comparison is made. If the SQLITE_AFF_MASK is 0x00, then numeric
68281 ** affinity is used. Note that the affinity conversions are stored
68282 ** back into the input registers P1 and P3.  So this opcode can cause
68283 ** persistent changes to registers P1 and P3.
68284 **
68285 ** Once any conversions have taken place, and neither value is NULL,
68286 ** the values are compared. If both values are blobs then memcmp() is
68287 ** used to determine the results of the comparison.  If both values
68288 ** are text, then the appropriate collating function specified in
68289 ** P4 is  used to do the comparison.  If P4 is not specified then
68290 ** memcmp() is used to compare text string.  If both values are
68291 ** numeric, then a numeric comparison is used. If the two values
68292 ** are of different types, then numbers are considered less than
68293 ** strings and strings are considered less than blobs.
68294 **
68295 ** If the SQLITE_STOREP2 bit of P5 is set, then do not jump.  Instead,
68296 ** store a boolean result (either 0, or 1, or NULL) in register P2.
68297 **
68298 ** If the SQLITE_NULLEQ bit is set in P5, then NULL values are considered
68299 ** equal to one another, provided that they do not have their MEM_Cleared
68300 ** bit set.
68301 */
68302 /* Opcode: Ne P1 P2 P3 P4 P5
68303 ** Synopsis: if r[P1]!=r[P3] goto P2
68304 **
68305 ** This works just like the Lt opcode except that the jump is taken if
68306 ** the operands in registers P1 and P3 are not equal.  See the Lt opcode for
68307 ** additional information.
68308 **
68309 ** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either
68310 ** true or false and is never NULL.  If both operands are NULL then the result
68311 ** of comparison is false.  If either operand is NULL then the result is true.
68312 ** If neither operand is NULL the result is the same as it would be if
68313 ** the SQLITE_NULLEQ flag were omitted from P5.
68314 */
68315 /* Opcode: Eq P1 P2 P3 P4 P5
68316 ** Synopsis: if r[P1]==r[P3] goto P2
68317 **
68318 ** This works just like the Lt opcode except that the jump is taken if
68319 ** the operands in registers P1 and P3 are equal.
68320 ** See the Lt opcode for additional information.
68321 **
68322 ** If SQLITE_NULLEQ is set in P5 then the result of comparison is always either
68323 ** true or false and is never NULL.  If both operands are NULL then the result
68324 ** of comparison is true.  If either operand is NULL then the result is false.
68325 ** If neither operand is NULL the result is the same as it would be if
68326 ** the SQLITE_NULLEQ flag were omitted from P5.
68327 */
68328 /* Opcode: Le P1 P2 P3 P4 P5
68329 ** Synopsis: if r[P1]<=r[P3] goto P2
68330 **
68331 ** This works just like the Lt opcode except that the jump is taken if
68332 ** the content of register P3 is less than or equal to the content of
68333 ** register P1.  See the Lt opcode for additional information.
68334 */
68335 /* Opcode: Gt P1 P2 P3 P4 P5
68336 ** Synopsis: if r[P1]>r[P3] goto P2
68337 **
68338 ** This works just like the Lt opcode except that the jump is taken if
68339 ** the content of register P3 is greater than the content of
68340 ** register P1.  See the Lt opcode for additional information.
68341 */
68342 /* Opcode: Ge P1 P2 P3 P4 P5
68343 ** Synopsis: if r[P1]>=r[P3] goto P2
68344 **
68345 ** This works just like the Lt opcode except that the jump is taken if
68346 ** the content of register P3 is greater than or equal to the content of
68347 ** register P1.  See the Lt opcode for additional information.
68348 */
68349 case OP_Eq:               /* same as TK_EQ, jump, in1, in3 */
68350 case OP_Ne:               /* same as TK_NE, jump, in1, in3 */
68351 case OP_Lt:               /* same as TK_LT, jump, in1, in3 */
68352 case OP_Le:               /* same as TK_LE, jump, in1, in3 */
68353 case OP_Gt:               /* same as TK_GT, jump, in1, in3 */
68354 case OP_Ge: {             /* same as TK_GE, jump, in1, in3 */
68355   int res;            /* Result of the comparison of pIn1 against pIn3 */
68356   char affinity;      /* Affinity to use for comparison */
68357   u16 flags1;         /* Copy of initial value of pIn1->flags */
68358   u16 flags3;         /* Copy of initial value of pIn3->flags */
68359 
68360   pIn1 = &aMem[pOp->p1];
68361   pIn3 = &aMem[pOp->p3];
68362   flags1 = pIn1->flags;
68363   flags3 = pIn3->flags;
68364   if( (flags1 | flags3)&MEM_Null ){
68365     /* One or both operands are NULL */
68366     if( pOp->p5 & SQLITE_NULLEQ ){
68367       /* If SQLITE_NULLEQ is set (which will only happen if the operator is
68368       ** OP_Eq or OP_Ne) then take the jump or not depending on whether
68369       ** or not both operands are null.
68370       */
68371       assert( pOp->opcode==OP_Eq || pOp->opcode==OP_Ne );
68372       assert( (flags1 & MEM_Cleared)==0 );
68373       if( (flags1&MEM_Null)!=0
68374        && (flags3&MEM_Null)!=0
68375        && (flags3&MEM_Cleared)==0
68376       ){
68377         res = 0;  /* Results are equal */
68378       }else{
68379         res = 1;  /* Results are not equal */
68380       }
68381     }else{
68382       /* SQLITE_NULLEQ is clear and at least one operand is NULL,
68383       ** then the result is always NULL.
68384       ** The jump is taken if the SQLITE_JUMPIFNULL bit is set.
68385       */
68386       if( pOp->p5 & SQLITE_JUMPIFNULL ){
68387         pc = pOp->p2-1;
68388       }else if( pOp->p5 & SQLITE_STOREP2 ){
68389         pOut = &aMem[pOp->p2];
68390         MemSetTypeFlag(pOut, MEM_Null);
68391         REGISTER_TRACE(pOp->p2, pOut);
68392       }
68393       break;
68394     }
68395   }else{
68396     /* Neither operand is NULL.  Do a comparison. */
68397     affinity = pOp->p5 & SQLITE_AFF_MASK;
68398     if( affinity ){
68399       applyAffinity(pIn1, affinity, encoding);
68400       applyAffinity(pIn3, affinity, encoding);
68401       if( db->mallocFailed ) goto no_mem;
68402     }
68403 
68404     assert( pOp->p4type==P4_COLLSEQ || pOp->p4.pColl==0 );
68405     ExpandBlob(pIn1);
68406     ExpandBlob(pIn3);
68407     res = sqlite3MemCompare(pIn3, pIn1, pOp->p4.pColl);
68408   }
68409   switch( pOp->opcode ){
68410     case OP_Eq:    res = res==0;     break;
68411     case OP_Ne:    res = res!=0;     break;
68412     case OP_Lt:    res = res<0;      break;
68413     case OP_Le:    res = res<=0;     break;
68414     case OP_Gt:    res = res>0;      break;
68415     default:       res = res>=0;     break;
68416   }
68417 
68418   if( pOp->p5 & SQLITE_STOREP2 ){
68419     pOut = &aMem[pOp->p2];
68420     memAboutToChange(p, pOut);
68421     MemSetTypeFlag(pOut, MEM_Int);
68422     pOut->u.i = res;
68423     REGISTER_TRACE(pOp->p2, pOut);
68424   }else if( res ){
68425     pc = pOp->p2-1;
68426   }
68427 
68428   /* Undo any changes made by applyAffinity() to the input registers. */
68429   pIn1->flags = (pIn1->flags&~MEM_TypeMask) | (flags1&MEM_TypeMask);
68430   pIn3->flags = (pIn3->flags&~MEM_TypeMask) | (flags3&MEM_TypeMask);
68431   break;
68432 }
68433 
68434 /* Opcode: Permutation * * * P4 *
68435 **
68436 ** Set the permutation used by the OP_Compare operator to be the array
68437 ** of integers in P4.
68438 **
68439 ** The permutation is only valid until the next OP_Compare that has
68440 ** the OPFLAG_PERMUTE bit set in P5. Typically the OP_Permutation should
68441 ** occur immediately prior to the OP_Compare.
68442 */
68443 case OP_Permutation: {
68444   assert( pOp->p4type==P4_INTARRAY );
68445   assert( pOp->p4.ai );
68446   aPermute = pOp->p4.ai;
68447   break;
68448 }
68449 
68450 /* Opcode: Compare P1 P2 P3 P4 P5
68451 **
68452 ** Compare two vectors of registers in reg(P1)..reg(P1+P3-1) (call this
68453 ** vector "A") and in reg(P2)..reg(P2+P3-1) ("B").  Save the result of
68454 ** the comparison for use by the next OP_Jump instruct.
68455 **
68456 ** If P5 has the OPFLAG_PERMUTE bit set, then the order of comparison is
68457 ** determined by the most recent OP_Permutation operator.  If the
68458 ** OPFLAG_PERMUTE bit is clear, then register are compared in sequential
68459 ** order.
68460 **
68461 ** P4 is a KeyInfo structure that defines collating sequences and sort
68462 ** orders for the comparison.  The permutation applies to registers
68463 ** only.  The KeyInfo elements are used sequentially.
68464 **
68465 ** The comparison is a sort comparison, so NULLs compare equal,
68466 ** NULLs are less than numbers, numbers are less than strings,
68467 ** and strings are less than blobs.
68468 */
68469 case OP_Compare: {
68470   int n;
68471   int i;
68472   int p1;
68473   int p2;
68474   const KeyInfo *pKeyInfo;
68475   int idx;
68476   CollSeq *pColl;    /* Collating sequence to use on this term */
68477   int bRev;          /* True for DESCENDING sort order */
68478 
68479   if( (pOp->p5 & OPFLAG_PERMUTE)==0 ) aPermute = 0;
68480   n = pOp->p3;
68481   pKeyInfo = pOp->p4.pKeyInfo;
68482   assert( n>0 );
68483   assert( pKeyInfo!=0 );
68484   p1 = pOp->p1;
68485   p2 = pOp->p2;
68486 #if SQLITE_DEBUG
68487   if( aPermute ){
68488     int k, mx = 0;
68489     for(k=0; k<n; k++) if( aPermute[k]>mx ) mx = aPermute[k];
68490     assert( p1>0 && p1+mx<=(p->nMem-p->nCursor)+1 );
68491     assert( p2>0 && p2+mx<=(p->nMem-p->nCursor)+1 );
68492   }else{
68493     assert( p1>0 && p1+n<=(p->nMem-p->nCursor)+1 );
68494     assert( p2>0 && p2+n<=(p->nMem-p->nCursor)+1 );
68495   }
68496 #endif /* SQLITE_DEBUG */
68497   for(i=0; i<n; i++){
68498     idx = aPermute ? aPermute[i] : i;
68499     assert( memIsValid(&aMem[p1+idx]) );
68500     assert( memIsValid(&aMem[p2+idx]) );
68501     REGISTER_TRACE(p1+idx, &aMem[p1+idx]);
68502     REGISTER_TRACE(p2+idx, &aMem[p2+idx]);
68503     assert( i<pKeyInfo->nField );
68504     pColl = pKeyInfo->aColl[i];
68505     bRev = pKeyInfo->aSortOrder[i];
68506     iCompare = sqlite3MemCompare(&aMem[p1+idx], &aMem[p2+idx], pColl);
68507     if( iCompare ){
68508       if( bRev ) iCompare = -iCompare;
68509       break;
68510     }
68511   }
68512   aPermute = 0;
68513   break;
68514 }
68515 
68516 /* Opcode: Jump P1 P2 P3 * *
68517 **
68518 ** Jump to the instruction at address P1, P2, or P3 depending on whether
68519 ** in the most recent OP_Compare instruction the P1 vector was less than
68520 ** equal to, or greater than the P2 vector, respectively.
68521 */
68522 case OP_Jump: {             /* jump */
68523   if( iCompare<0 ){
68524     pc = pOp->p1 - 1;
68525   }else if( iCompare==0 ){
68526     pc = pOp->p2 - 1;
68527   }else{
68528     pc = pOp->p3 - 1;
68529   }
68530   break;
68531 }
68532 
68533 /* Opcode: And P1 P2 P3 * *
68534 ** Synopsis: r[P3]=(r[P1] && r[P2])
68535 **
68536 ** Take the logical AND of the values in registers P1 and P2 and
68537 ** write the result into register P3.
68538 **
68539 ** If either P1 or P2 is 0 (false) then the result is 0 even if
68540 ** the other input is NULL.  A NULL and true or two NULLs give
68541 ** a NULL output.
68542 */
68543 /* Opcode: Or P1 P2 P3 * *
68544 ** Synopsis: r[P3]=(r[P1] || r[P2])
68545 **
68546 ** Take the logical OR of the values in register P1 and P2 and
68547 ** store the answer in register P3.
68548 **
68549 ** If either P1 or P2 is nonzero (true) then the result is 1 (true)
68550 ** even if the other input is NULL.  A NULL and false or two NULLs
68551 ** give a NULL output.
68552 */
68553 case OP_And:              /* same as TK_AND, in1, in2, out3 */
68554 case OP_Or: {             /* same as TK_OR, in1, in2, out3 */
68555   int v1;    /* Left operand:  0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
68556   int v2;    /* Right operand: 0==FALSE, 1==TRUE, 2==UNKNOWN or NULL */
68557 
68558   pIn1 = &aMem[pOp->p1];
68559   if( pIn1->flags & MEM_Null ){
68560     v1 = 2;
68561   }else{
68562     v1 = sqlite3VdbeIntValue(pIn1)!=0;
68563   }
68564   pIn2 = &aMem[pOp->p2];
68565   if( pIn2->flags & MEM_Null ){
68566     v2 = 2;
68567   }else{
68568     v2 = sqlite3VdbeIntValue(pIn2)!=0;
68569   }
68570   if( pOp->opcode==OP_And ){
68571     static const unsigned char and_logic[] = { 0, 0, 0, 0, 1, 2, 0, 2, 2 };
68572     v1 = and_logic[v1*3+v2];
68573   }else{
68574     static const unsigned char or_logic[] = { 0, 1, 2, 1, 1, 1, 2, 1, 2 };
68575     v1 = or_logic[v1*3+v2];
68576   }
68577   pOut = &aMem[pOp->p3];
68578   if( v1==2 ){
68579     MemSetTypeFlag(pOut, MEM_Null);
68580   }else{
68581     pOut->u.i = v1;
68582     MemSetTypeFlag(pOut, MEM_Int);
68583   }
68584   break;
68585 }
68586 
68587 /* Opcode: Not P1 P2 * * *
68588 ** Synopsis: r[P2]= !r[P1]
68589 **
68590 ** Interpret the value in register P1 as a boolean value.  Store the
68591 ** boolean complement in register P2.  If the value in register P1 is
68592 ** NULL, then a NULL is stored in P2.
68593 */
68594 case OP_Not: {                /* same as TK_NOT, in1, out2 */
68595   pIn1 = &aMem[pOp->p1];
68596   pOut = &aMem[pOp->p2];
68597   if( pIn1->flags & MEM_Null ){
68598     sqlite3VdbeMemSetNull(pOut);
68599   }else{
68600     sqlite3VdbeMemSetInt64(pOut, !sqlite3VdbeIntValue(pIn1));
68601   }
68602   break;
68603 }
68604 
68605 /* Opcode: BitNot P1 P2 * * *
68606 ** Synopsis: r[P1]= ~r[P1]
68607 **
68608 ** Interpret the content of register P1 as an integer.  Store the
68609 ** ones-complement of the P1 value into register P2.  If P1 holds
68610 ** a NULL then store a NULL in P2.
68611 */
68612 case OP_BitNot: {             /* same as TK_BITNOT, in1, out2 */
68613   pIn1 = &aMem[pOp->p1];
68614   pOut = &aMem[pOp->p2];
68615   if( pIn1->flags & MEM_Null ){
68616     sqlite3VdbeMemSetNull(pOut);
68617   }else{
68618     sqlite3VdbeMemSetInt64(pOut, ~sqlite3VdbeIntValue(pIn1));
68619   }
68620   break;
68621 }
68622 
68623 /* Opcode: Once P1 P2 * * *
68624 **
68625 ** Check if OP_Once flag P1 is set. If so, jump to instruction P2. Otherwise,
68626 ** set the flag and fall through to the next instruction.
68627 */
68628 case OP_Once: {             /* jump */
68629   assert( pOp->p1<p->nOnceFlag );
68630   if( p->aOnceFlag[pOp->p1] ){
68631     pc = pOp->p2-1;
68632   }else{
68633     p->aOnceFlag[pOp->p1] = 1;
68634   }
68635   break;
68636 }
68637 
68638 /* Opcode: If P1 P2 P3 * *
68639 **
68640 ** Jump to P2 if the value in register P1 is true.  The value
68641 ** is considered true if it is numeric and non-zero.  If the value
68642 ** in P1 is NULL then take the jump if P3 is non-zero.
68643 */
68644 /* Opcode: IfNot P1 P2 P3 * *
68645 **
68646 ** Jump to P2 if the value in register P1 is False.  The value
68647 ** is considered false if it has a numeric value of zero.  If the value
68648 ** in P1 is NULL then take the jump if P3 is zero.
68649 */
68650 case OP_If:                 /* jump, in1 */
68651 case OP_IfNot: {            /* jump, in1 */
68652   int c;
68653   pIn1 = &aMem[pOp->p1];
68654   if( pIn1->flags & MEM_Null ){
68655     c = pOp->p3;
68656   }else{
68657 #ifdef SQLITE_OMIT_FLOATING_POINT
68658     c = sqlite3VdbeIntValue(pIn1)!=0;
68659 #else
68660     c = sqlite3VdbeRealValue(pIn1)!=0.0;
68661 #endif
68662     if( pOp->opcode==OP_IfNot ) c = !c;
68663   }
68664   if( c ){
68665     pc = pOp->p2-1;
68666   }
68667   break;
68668 }
68669 
68670 /* Opcode: IsNull P1 P2 * * *
68671 ** Synopsis:  if r[P1]==NULL goto P2
68672 **
68673 ** Jump to P2 if the value in register P1 is NULL.
68674 */
68675 case OP_IsNull: {            /* same as TK_ISNULL, jump, in1 */
68676   pIn1 = &aMem[pOp->p1];
68677   if( (pIn1->flags & MEM_Null)!=0 ){
68678     pc = pOp->p2 - 1;
68679   }
68680   break;
68681 }
68682 
68683 /* Opcode: NotNull P1 P2 * * *
68684 ** Synopsis: if r[P1]!=NULL goto P2
68685 **
68686 ** Jump to P2 if the value in register P1 is not NULL.
68687 */
68688 case OP_NotNull: {            /* same as TK_NOTNULL, jump, in1 */
68689   pIn1 = &aMem[pOp->p1];
68690   if( (pIn1->flags & MEM_Null)==0 ){
68691     pc = pOp->p2 - 1;
68692   }
68693   break;
68694 }
68695 
68696 /* Opcode: Column P1 P2 P3 P4 P5
68697 ** Synopsis:  r[P3]=PX
68698 **
68699 ** Interpret the data that cursor P1 points to as a structure built using
68700 ** the MakeRecord instruction.  (See the MakeRecord opcode for additional
68701 ** information about the format of the data.)  Extract the P2-th column
68702 ** from this record.  If there are less that (P2+1)
68703 ** values in the record, extract a NULL.
68704 **
68705 ** The value extracted is stored in register P3.
68706 **
68707 ** If the column contains fewer than P2 fields, then extract a NULL.  Or,
68708 ** if the P4 argument is a P4_MEM use the value of the P4 argument as
68709 ** the result.
68710 **
68711 ** If the OPFLAG_CLEARCACHE bit is set on P5 and P1 is a pseudo-table cursor,
68712 ** then the cache of the cursor is reset prior to extracting the column.
68713 ** The first OP_Column against a pseudo-table after the value of the content
68714 ** register has changed should have this bit set.
68715 **
68716 ** If the OPFLAG_LENGTHARG and OPFLAG_TYPEOFARG bits are set on P5 when
68717 ** the result is guaranteed to only be used as the argument of a length()
68718 ** or typeof() function, respectively.  The loading of large blobs can be
68719 ** skipped for length() and all content loading can be skipped for typeof().
68720 */
68721 case OP_Column: {
68722   i64 payloadSize64; /* Number of bytes in the record */
68723   int p2;            /* column number to retrieve */
68724   VdbeCursor *pC;    /* The VDBE cursor */
68725   BtCursor *pCrsr;   /* The BTree cursor */
68726   u32 *aType;        /* aType[i] holds the numeric type of the i-th column */
68727   u32 *aOffset;      /* aOffset[i] is offset to start of data for i-th column */
68728   int len;           /* The length of the serialized data for the column */
68729   int i;             /* Loop counter */
68730   Mem *pDest;        /* Where to write the extracted value */
68731   Mem sMem;          /* For storing the record being decoded */
68732   const u8 *zData;   /* Part of the record being decoded */
68733   const u8 *zHdr;    /* Next unparsed byte of the header */
68734   const u8 *zEndHdr; /* Pointer to first byte after the header */
68735   u32 offset;        /* Offset into the data */
68736   u32 szField;       /* Number of bytes in the content of a field */
68737   u32 avail;         /* Number of bytes of available data */
68738   u32 t;             /* A type code from the record header */
68739   Mem *pReg;         /* PseudoTable input register */
68740 
68741   p2 = pOp->p2;
68742   assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
68743   pDest = &aMem[pOp->p3];
68744   memAboutToChange(p, pDest);
68745   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
68746   pC = p->apCsr[pOp->p1];
68747   assert( pC!=0 );
68748   assert( p2<pC->nField );
68749   aType = pC->aType;
68750   aOffset = aType + pC->nField;
68751 #ifndef SQLITE_OMIT_VIRTUALTABLE
68752   assert( pC->pVtabCursor==0 ); /* OP_Column never called on virtual table */
68753 #endif
68754   pCrsr = pC->pCursor;
68755   assert( pCrsr!=0 || pC->pseudoTableReg>0 ); /* pCrsr NULL on PseudoTables */
68756   assert( pCrsr!=0 || pC->nullRow );          /* pC->nullRow on PseudoTables */
68757 
68758   /* If the cursor cache is stale, bring it up-to-date */
68759   rc = sqlite3VdbeCursorMoveto(pC);
68760   if( rc ) goto abort_due_to_error;
68761   if( pC->cacheStatus!=p->cacheCtr || (pOp->p5&OPFLAG_CLEARCACHE)!=0 ){
68762     if( pC->nullRow ){
68763       if( pCrsr==0 ){
68764         assert( pC->pseudoTableReg>0 );
68765         pReg = &aMem[pC->pseudoTableReg];
68766         if( pC->multiPseudo ){
68767           sqlite3VdbeMemShallowCopy(pDest, pReg+p2, MEM_Ephem);
68768           Deephemeralize(pDest);
68769           goto op_column_out;
68770         }
68771         assert( pReg->flags & MEM_Blob );
68772         assert( memIsValid(pReg) );
68773         pC->payloadSize = pC->szRow = avail = pReg->n;
68774         pC->aRow = (u8*)pReg->z;
68775       }else{
68776         MemSetTypeFlag(pDest, MEM_Null);
68777         goto op_column_out;
68778       }
68779     }else{
68780       assert( pCrsr );
68781       if( pC->isTable==0 ){
68782         assert( sqlite3BtreeCursorIsValid(pCrsr) );
68783         VVA_ONLY(rc =) sqlite3BtreeKeySize(pCrsr, &payloadSize64);
68784         assert( rc==SQLITE_OK ); /* True because of CursorMoveto() call above */
68785         /* sqlite3BtreeParseCellPtr() uses getVarint32() to extract the
68786         ** payload size, so it is impossible for payloadSize64 to be
68787         ** larger than 32 bits. */
68788         assert( (payloadSize64 & SQLITE_MAX_U32)==(u64)payloadSize64 );
68789         pC->aRow = sqlite3BtreeKeyFetch(pCrsr, &avail);
68790         pC->payloadSize = (u32)payloadSize64;
68791       }else{
68792         assert( sqlite3BtreeCursorIsValid(pCrsr) );
68793         VVA_ONLY(rc =) sqlite3BtreeDataSize(pCrsr, &pC->payloadSize);
68794         assert( rc==SQLITE_OK );   /* DataSize() cannot fail */
68795         pC->aRow = sqlite3BtreeDataFetch(pCrsr, &avail);
68796       }
68797       assert( avail<=65536 );  /* Maximum page size is 64KiB */
68798       if( pC->payloadSize <= (u32)avail ){
68799         pC->szRow = pC->payloadSize;
68800       }else{
68801         pC->szRow = avail;
68802       }
68803       if( pC->payloadSize > (u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
68804         goto too_big;
68805       }
68806     }
68807     pC->cacheStatus = p->cacheCtr;
68808     pC->iHdrOffset = getVarint32(pC->aRow, offset);
68809     pC->nHdrParsed = 0;
68810     aOffset[0] = offset;
68811     if( avail<offset ){
68812       /* pC->aRow does not have to hold the entire row, but it does at least
68813       ** need to cover the header of the record.  If pC->aRow does not contain
68814       ** the complete header, then set it to zero, forcing the header to be
68815       ** dynamically allocated. */
68816       pC->aRow = 0;
68817       pC->szRow = 0;
68818     }
68819 
68820     /* Make sure a corrupt database has not given us an oversize header.
68821     ** Do this now to avoid an oversize memory allocation.
68822     **
68823     ** Type entries can be between 1 and 5 bytes each.  But 4 and 5 byte
68824     ** types use so much data space that there can only be 4096 and 32 of
68825     ** them, respectively.  So the maximum header length results from a
68826     ** 3-byte type for each of the maximum of 32768 columns plus three
68827     ** extra bytes for the header length itself.  32768*3 + 3 = 98307.
68828     */
68829     if( offset > 98307 || offset > pC->payloadSize ){
68830       rc = SQLITE_CORRUPT_BKPT;
68831       goto op_column_error;
68832     }
68833   }
68834 
68835   /* Make sure at least the first p2+1 entries of the header have been
68836   ** parsed and valid information is in aOffset[] and aType[].
68837   */
68838   if( pC->nHdrParsed<=p2 ){
68839     /* If there is more header available for parsing in the record, try
68840     ** to extract additional fields up through the p2+1-th field
68841     */
68842     if( pC->iHdrOffset<aOffset[0] ){
68843       /* Make sure zData points to enough of the record to cover the header. */
68844       if( pC->aRow==0 ){
68845         memset(&sMem, 0, sizeof(sMem));
68846         rc = sqlite3VdbeMemFromBtree(pCrsr, 0, aOffset[0],
68847                                      !pC->isTable, &sMem);
68848         if( rc!=SQLITE_OK ){
68849           goto op_column_error;
68850         }
68851         zData = (u8*)sMem.z;
68852       }else{
68853         zData = pC->aRow;
68854       }
68855 
68856       /* Fill in aType[i] and aOffset[i] values through the p2-th field. */
68857       i = pC->nHdrParsed;
68858       offset = aOffset[i];
68859       zHdr = zData + pC->iHdrOffset;
68860       zEndHdr = zData + aOffset[0];
68861       assert( i<=p2 && zHdr<zEndHdr );
68862       do{
68863         if( zHdr[0]<0x80 ){
68864           t = zHdr[0];
68865           zHdr++;
68866         }else{
68867           zHdr += sqlite3GetVarint32(zHdr, &t);
68868         }
68869         aType[i] = t;
68870         szField = sqlite3VdbeSerialTypeLen(t);
68871         offset += szField;
68872         if( offset<szField ){  /* True if offset overflows */
68873           zHdr = &zEndHdr[1];  /* Forces SQLITE_CORRUPT return below */
68874           break;
68875         }
68876         i++;
68877         aOffset[i] = offset;
68878       }while( i<=p2 && zHdr<zEndHdr );
68879       pC->nHdrParsed = i;
68880       pC->iHdrOffset = (u32)(zHdr - zData);
68881       if( pC->aRow==0 ){
68882         sqlite3VdbeMemRelease(&sMem);
68883         sMem.flags = MEM_Null;
68884       }
68885 
68886       /* If we have read more header data than was contained in the header,
68887       ** or if the end of the last field appears to be past the end of the
68888       ** record, or if the end of the last field appears to be before the end
68889       ** of the record (when all fields present), then we must be dealing
68890       ** with a corrupt database.
68891       */
68892       if( (zHdr > zEndHdr)
68893        || (offset > pC->payloadSize)
68894        || (zHdr==zEndHdr && offset!=pC->payloadSize)
68895       ){
68896         rc = SQLITE_CORRUPT_BKPT;
68897         goto op_column_error;
68898       }
68899     }
68900 
68901     /* If after trying to extra new entries from the header, nHdrParsed is
68902     ** still not up to p2, that means that the record has fewer than p2
68903     ** columns.  So the result will be either the default value or a NULL.
68904     */
68905     if( pC->nHdrParsed<=p2 ){
68906       if( pOp->p4type==P4_MEM ){
68907         sqlite3VdbeMemShallowCopy(pDest, pOp->p4.pMem, MEM_Static);
68908       }else{
68909         MemSetTypeFlag(pDest, MEM_Null);
68910       }
68911       goto op_column_out;
68912     }
68913   }
68914 
68915   /* Extract the content for the p2+1-th column.  Control can only
68916   ** reach this point if aOffset[p2], aOffset[p2+1], and aType[p2] are
68917   ** all valid.
68918   */
68919   assert( p2<pC->nHdrParsed );
68920   assert( rc==SQLITE_OK );
68921   if( pC->szRow>=aOffset[p2+1] ){
68922     /* This is the common case where the desired content fits on the original
68923     ** page - where the content is not on an overflow page */
68924     VdbeMemRelease(pDest);
68925     sqlite3VdbeSerialGet(pC->aRow+aOffset[p2], aType[p2], pDest);
68926   }else{
68927     /* This branch happens only when content is on overflow pages */
68928     t = aType[p2];
68929     if( ((pOp->p5 & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG))!=0
68930           && ((t>=12 && (t&1)==0) || (pOp->p5 & OPFLAG_TYPEOFARG)!=0))
68931      || (len = sqlite3VdbeSerialTypeLen(t))==0
68932     ){
68933       /* Content is irrelevant for the typeof() function and for
68934       ** the length(X) function if X is a blob.  So we might as well use
68935       ** bogus content rather than reading content from disk.  NULL works
68936       ** for text and blob and whatever is in the payloadSize64 variable
68937       ** will work for everything else.  Content is also irrelevant if
68938       ** the content length is 0. */
68939       zData = t<=13 ? (u8*)&payloadSize64 : 0;
68940       sMem.zMalloc = 0;
68941     }else{
68942       memset(&sMem, 0, sizeof(sMem));
68943       sqlite3VdbeMemMove(&sMem, pDest);
68944       rc = sqlite3VdbeMemFromBtree(pCrsr, aOffset[p2], len, !pC->isTable,
68945                                    &sMem);
68946       if( rc!=SQLITE_OK ){
68947         goto op_column_error;
68948       }
68949       zData = (u8*)sMem.z;
68950     }
68951     sqlite3VdbeSerialGet(zData, t, pDest);
68952     /* If we dynamically allocated space to hold the data (in the
68953     ** sqlite3VdbeMemFromBtree() call above) then transfer control of that
68954     ** dynamically allocated space over to the pDest structure.
68955     ** This prevents a memory copy. */
68956     if( sMem.zMalloc ){
68957       assert( sMem.z==sMem.zMalloc );
68958       assert( !(pDest->flags & MEM_Dyn) );
68959       assert( !(pDest->flags & (MEM_Blob|MEM_Str)) || pDest->z==sMem.z );
68960       pDest->flags &= ~(MEM_Ephem|MEM_Static);
68961       pDest->flags |= MEM_Term;
68962       pDest->z = sMem.z;
68963       pDest->zMalloc = sMem.zMalloc;
68964     }
68965   }
68966   pDest->enc = encoding;
68967 
68968 op_column_out:
68969   Deephemeralize(pDest);
68970 op_column_error:
68971   UPDATE_MAX_BLOBSIZE(pDest);
68972   REGISTER_TRACE(pOp->p3, pDest);
68973   break;
68974 }
68975 
68976 /* Opcode: Affinity P1 P2 * P4 *
68977 ** Synopsis: affinity(r[P1@P2])
68978 **
68979 ** Apply affinities to a range of P2 registers starting with P1.
68980 **
68981 ** P4 is a string that is P2 characters long. The nth character of the
68982 ** string indicates the column affinity that should be used for the nth
68983 ** memory cell in the range.
68984 */
68985 case OP_Affinity: {
68986   const char *zAffinity;   /* The affinity to be applied */
68987   char cAff;               /* A single character of affinity */
68988 
68989   zAffinity = pOp->p4.z;
68990   assert( zAffinity!=0 );
68991   assert( zAffinity[pOp->p2]==0 );
68992   pIn1 = &aMem[pOp->p1];
68993   while( (cAff = *(zAffinity++))!=0 ){
68994     assert( pIn1 <= &p->aMem[(p->nMem-p->nCursor)] );
68995     assert( memIsValid(pIn1) );
68996     ExpandBlob(pIn1);
68997     applyAffinity(pIn1, cAff, encoding);
68998     pIn1++;
68999   }
69000   break;
69001 }
69002 
69003 /* Opcode: MakeRecord P1 P2 P3 P4 *
69004 ** Synopsis: r[P3]=mkrec(r[P1@P2])
69005 **
69006 ** Convert P2 registers beginning with P1 into the [record format]
69007 ** use as a data record in a database table or as a key
69008 ** in an index.  The OP_Column opcode can decode the record later.
69009 **
69010 ** P4 may be a string that is P2 characters long.  The nth character of the
69011 ** string indicates the column affinity that should be used for the nth
69012 ** field of the index key.
69013 **
69014 ** The mapping from character to affinity is given by the SQLITE_AFF_
69015 ** macros defined in sqliteInt.h.
69016 **
69017 ** If P4 is NULL then all index fields have the affinity NONE.
69018 */
69019 case OP_MakeRecord: {
69020   u8 *zNewRecord;        /* A buffer to hold the data for the new record */
69021   Mem *pRec;             /* The new record */
69022   u64 nData;             /* Number of bytes of data space */
69023   int nHdr;              /* Number of bytes of header space */
69024   i64 nByte;             /* Data space required for this record */
69025   int nZero;             /* Number of zero bytes at the end of the record */
69026   int nVarint;           /* Number of bytes in a varint */
69027   u32 serial_type;       /* Type field */
69028   Mem *pData0;           /* First field to be combined into the record */
69029   Mem *pLast;            /* Last field of the record */
69030   int nField;            /* Number of fields in the record */
69031   char *zAffinity;       /* The affinity string for the record */
69032   int file_format;       /* File format to use for encoding */
69033   int i;                 /* Space used in zNewRecord[] header */
69034   int j;                 /* Space used in zNewRecord[] content */
69035   int len;               /* Length of a field */
69036 
69037   /* Assuming the record contains N fields, the record format looks
69038   ** like this:
69039   **
69040   ** ------------------------------------------------------------------------
69041   ** | hdr-size | type 0 | type 1 | ... | type N-1 | data0 | ... | data N-1 |
69042   ** ------------------------------------------------------------------------
69043   **
69044   ** Data(0) is taken from register P1.  Data(1) comes from register P1+1
69045   ** and so froth.
69046   **
69047   ** Each type field is a varint representing the serial type of the
69048   ** corresponding data element (see sqlite3VdbeSerialType()). The
69049   ** hdr-size field is also a varint which is the offset from the beginning
69050   ** of the record to data0.
69051   */
69052   nData = 0;         /* Number of bytes of data space */
69053   nHdr = 0;          /* Number of bytes of header space */
69054   nZero = 0;         /* Number of zero bytes at the end of the record */
69055   nField = pOp->p1;
69056   zAffinity = pOp->p4.z;
69057   assert( nField>0 && pOp->p2>0 && pOp->p2+nField<=(p->nMem-p->nCursor)+1 );
69058   pData0 = &aMem[nField];
69059   nField = pOp->p2;
69060   pLast = &pData0[nField-1];
69061   file_format = p->minWriteFileFormat;
69062 
69063   /* Identify the output register */
69064   assert( pOp->p3<pOp->p1 || pOp->p3>=pOp->p1+pOp->p2 );
69065   pOut = &aMem[pOp->p3];
69066   memAboutToChange(p, pOut);
69067 
69068   /* Apply the requested affinity to all inputs
69069   */
69070   assert( pData0<=pLast );
69071   if( zAffinity ){
69072     pRec = pData0;
69073     do{
69074       applyAffinity(pRec, *(zAffinity++), encoding);
69075     }while( (++pRec)<=pLast );
69076   }
69077 
69078   /* Loop through the elements that will make up the record to figure
69079   ** out how much space is required for the new record.
69080   */
69081   pRec = pLast;
69082   do{
69083     assert( memIsValid(pRec) );
69084     serial_type = sqlite3VdbeSerialType(pRec, file_format);
69085     len = sqlite3VdbeSerialTypeLen(serial_type);
69086     if( pRec->flags & MEM_Zero ){
69087       if( nData ){
69088         sqlite3VdbeMemExpandBlob(pRec);
69089       }else{
69090         nZero += pRec->u.nZero;
69091         len -= pRec->u.nZero;
69092       }
69093     }
69094     nData += len;
69095     testcase( serial_type==127 );
69096     testcase( serial_type==128 );
69097     nHdr += serial_type<=127 ? 1 : sqlite3VarintLen(serial_type);
69098   }while( (--pRec)>=pData0 );
69099 
69100   /* Add the initial header varint and total the size */
69101   testcase( nHdr==126 );
69102   testcase( nHdr==127 );
69103   if( nHdr<=126 ){
69104     /* The common case */
69105     nHdr += 1;
69106   }else{
69107     /* Rare case of a really large header */
69108     nVarint = sqlite3VarintLen(nHdr);
69109     nHdr += nVarint;
69110     if( nVarint<sqlite3VarintLen(nHdr) ) nHdr++;
69111   }
69112   nByte = nHdr+nData;
69113   if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
69114     goto too_big;
69115   }
69116 
69117   /* Make sure the output register has a buffer large enough to store
69118   ** the new record. The output register (pOp->p3) is not allowed to
69119   ** be one of the input registers (because the following call to
69120   ** sqlite3VdbeMemGrow() could clobber the value before it is used).
69121   */
69122   if( sqlite3VdbeMemGrow(pOut, (int)nByte, 0) ){
69123     goto no_mem;
69124   }
69125   zNewRecord = (u8 *)pOut->z;
69126 
69127   /* Write the record */
69128   i = putVarint32(zNewRecord, nHdr);
69129   j = nHdr;
69130   assert( pData0<=pLast );
69131   pRec = pData0;
69132   do{
69133     serial_type = sqlite3VdbeSerialType(pRec, file_format);
69134     i += putVarint32(&zNewRecord[i], serial_type);            /* serial type */
69135     j += sqlite3VdbeSerialPut(&zNewRecord[j], pRec, serial_type); /* content */
69136   }while( (++pRec)<=pLast );
69137   assert( i==nHdr );
69138   assert( j==nByte );
69139 
69140   assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
69141   pOut->n = (int)nByte;
69142   pOut->flags = MEM_Blob | MEM_Dyn;
69143   pOut->xDel = 0;
69144   if( nZero ){
69145     pOut->u.nZero = nZero;
69146     pOut->flags |= MEM_Zero;
69147   }
69148   pOut->enc = SQLITE_UTF8;  /* In case the blob is ever converted to text */
69149   REGISTER_TRACE(pOp->p3, pOut);
69150   UPDATE_MAX_BLOBSIZE(pOut);
69151   break;
69152 }
69153 
69154 /* Opcode: Count P1 P2 * * *
69155 ** Synopsis: r[P2]=count()
69156 **
69157 ** Store the number of entries (an integer value) in the table or index
69158 ** opened by cursor P1 in register P2
69159 */
69160 #ifndef SQLITE_OMIT_BTREECOUNT
69161 case OP_Count: {         /* out2-prerelease */
69162   i64 nEntry;
69163   BtCursor *pCrsr;
69164 
69165   pCrsr = p->apCsr[pOp->p1]->pCursor;
69166   assert( pCrsr );
69167   nEntry = 0;  /* Not needed.  Only used to silence a warning. */
69168   rc = sqlite3BtreeCount(pCrsr, &nEntry);
69169   pOut->u.i = nEntry;
69170   break;
69171 }
69172 #endif
69173 
69174 /* Opcode: Savepoint P1 * * P4 *
69175 **
69176 ** Open, release or rollback the savepoint named by parameter P4, depending
69177 ** on the value of P1. To open a new savepoint, P1==0. To release (commit) an
69178 ** existing savepoint, P1==1, or to rollback an existing savepoint P1==2.
69179 */
69180 case OP_Savepoint: {
69181   int p1;                         /* Value of P1 operand */
69182   char *zName;                    /* Name of savepoint */
69183   int nName;
69184   Savepoint *pNew;
69185   Savepoint *pSavepoint;
69186   Savepoint *pTmp;
69187   int iSavepoint;
69188   int ii;
69189 
69190   p1 = pOp->p1;
69191   zName = pOp->p4.z;
69192 
69193   /* Assert that the p1 parameter is valid. Also that if there is no open
69194   ** transaction, then there cannot be any savepoints.
69195   */
69196   assert( db->pSavepoint==0 || db->autoCommit==0 );
69197   assert( p1==SAVEPOINT_BEGIN||p1==SAVEPOINT_RELEASE||p1==SAVEPOINT_ROLLBACK );
69198   assert( db->pSavepoint || db->isTransactionSavepoint==0 );
69199   assert( checkSavepointCount(db) );
69200   assert( p->bIsReader );
69201 
69202   if( p1==SAVEPOINT_BEGIN ){
69203     if( db->nVdbeWrite>0 ){
69204       /* A new savepoint cannot be created if there are active write
69205       ** statements (i.e. open read/write incremental blob handles).
69206       */
69207       sqlite3SetString(&p->zErrMsg, db, "cannot open savepoint - "
69208         "SQL statements in progress");
69209       rc = SQLITE_BUSY;
69210     }else{
69211       nName = sqlite3Strlen30(zName);
69212 
69213 #ifndef SQLITE_OMIT_VIRTUALTABLE
69214       /* This call is Ok even if this savepoint is actually a transaction
69215       ** savepoint (and therefore should not prompt xSavepoint()) callbacks.
69216       ** If this is a transaction savepoint being opened, it is guaranteed
69217       ** that the db->aVTrans[] array is empty.  */
69218       assert( db->autoCommit==0 || db->nVTrans==0 );
69219       rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN,
69220                                 db->nStatement+db->nSavepoint);
69221       if( rc!=SQLITE_OK ) goto abort_due_to_error;
69222 #endif
69223 
69224       /* Create a new savepoint structure. */
69225       pNew = sqlite3DbMallocRaw(db, sizeof(Savepoint)+nName+1);
69226       if( pNew ){
69227         pNew->zName = (char *)&pNew[1];
69228         memcpy(pNew->zName, zName, nName+1);
69229 
69230         /* If there is no open transaction, then mark this as a special
69231         ** "transaction savepoint". */
69232         if( db->autoCommit ){
69233           db->autoCommit = 0;
69234           db->isTransactionSavepoint = 1;
69235         }else{
69236           db->nSavepoint++;
69237         }
69238 
69239         /* Link the new savepoint into the database handle's list. */
69240         pNew->pNext = db->pSavepoint;
69241         db->pSavepoint = pNew;
69242         pNew->nDeferredCons = db->nDeferredCons;
69243         pNew->nDeferredImmCons = db->nDeferredImmCons;
69244       }
69245     }
69246   }else{
69247     iSavepoint = 0;
69248 
69249     /* Find the named savepoint. If there is no such savepoint, then an
69250     ** an error is returned to the user.  */
69251     for(
69252       pSavepoint = db->pSavepoint;
69253       pSavepoint && sqlite3StrICmp(pSavepoint->zName, zName);
69254       pSavepoint = pSavepoint->pNext
69255     ){
69256       iSavepoint++;
69257     }
69258     if( !pSavepoint ){
69259       sqlite3SetString(&p->zErrMsg, db, "no such savepoint: %s", zName);
69260       rc = SQLITE_ERROR;
69261     }else if( db->nVdbeWrite>0 && p1==SAVEPOINT_RELEASE ){
69262       /* It is not possible to release (commit) a savepoint if there are
69263       ** active write statements.
69264       */
69265       sqlite3SetString(&p->zErrMsg, db,
69266         "cannot release savepoint - SQL statements in progress"
69267       );
69268       rc = SQLITE_BUSY;
69269     }else{
69270 
69271       /* Determine whether or not this is a transaction savepoint. If so,
69272       ** and this is a RELEASE command, then the current transaction
69273       ** is committed.
69274       */
69275       int isTransaction = pSavepoint->pNext==0 && db->isTransactionSavepoint;
69276       if( isTransaction && p1==SAVEPOINT_RELEASE ){
69277         if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
69278           goto vdbe_return;
69279         }
69280         db->autoCommit = 1;
69281         if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
69282           p->pc = pc;
69283           db->autoCommit = 0;
69284           p->rc = rc = SQLITE_BUSY;
69285           goto vdbe_return;
69286         }
69287         db->isTransactionSavepoint = 0;
69288         rc = p->rc;
69289       }else{
69290         iSavepoint = db->nSavepoint - iSavepoint - 1;
69291         if( p1==SAVEPOINT_ROLLBACK ){
69292           for(ii=0; ii<db->nDb; ii++){
69293             sqlite3BtreeTripAllCursors(db->aDb[ii].pBt, SQLITE_ABORT);
69294           }
69295         }
69296         for(ii=0; ii<db->nDb; ii++){
69297           rc = sqlite3BtreeSavepoint(db->aDb[ii].pBt, p1, iSavepoint);
69298           if( rc!=SQLITE_OK ){
69299             goto abort_due_to_error;
69300           }
69301         }
69302         if( p1==SAVEPOINT_ROLLBACK && (db->flags&SQLITE_InternChanges)!=0 ){
69303           sqlite3ExpirePreparedStatements(db);
69304           sqlite3ResetAllSchemasOfConnection(db);
69305           db->flags = (db->flags | SQLITE_InternChanges);
69306         }
69307       }
69308 
69309       /* Regardless of whether this is a RELEASE or ROLLBACK, destroy all
69310       ** savepoints nested inside of the savepoint being operated on. */
69311       while( db->pSavepoint!=pSavepoint ){
69312         pTmp = db->pSavepoint;
69313         db->pSavepoint = pTmp->pNext;
69314         sqlite3DbFree(db, pTmp);
69315         db->nSavepoint--;
69316       }
69317 
69318       /* If it is a RELEASE, then destroy the savepoint being operated on
69319       ** too. If it is a ROLLBACK TO, then set the number of deferred
69320       ** constraint violations present in the database to the value stored
69321       ** when the savepoint was created.  */
69322       if( p1==SAVEPOINT_RELEASE ){
69323         assert( pSavepoint==db->pSavepoint );
69324         db->pSavepoint = pSavepoint->pNext;
69325         sqlite3DbFree(db, pSavepoint);
69326         if( !isTransaction ){
69327           db->nSavepoint--;
69328         }
69329       }else{
69330         db->nDeferredCons = pSavepoint->nDeferredCons;
69331         db->nDeferredImmCons = pSavepoint->nDeferredImmCons;
69332       }
69333 
69334       if( !isTransaction ){
69335         rc = sqlite3VtabSavepoint(db, p1, iSavepoint);
69336         if( rc!=SQLITE_OK ) goto abort_due_to_error;
69337       }
69338     }
69339   }
69340 
69341   break;
69342 }
69343 
69344 /* Opcode: AutoCommit P1 P2 * * *
69345 **
69346 ** Set the database auto-commit flag to P1 (1 or 0). If P2 is true, roll
69347 ** back any currently active btree transactions. If there are any active
69348 ** VMs (apart from this one), then a ROLLBACK fails.  A COMMIT fails if
69349 ** there are active writing VMs or active VMs that use shared cache.
69350 **
69351 ** This instruction causes the VM to halt.
69352 */
69353 case OP_AutoCommit: {
69354   int desiredAutoCommit;
69355   int iRollback;
69356   int turnOnAC;
69357 
69358   desiredAutoCommit = pOp->p1;
69359   iRollback = pOp->p2;
69360   turnOnAC = desiredAutoCommit && !db->autoCommit;
69361   assert( desiredAutoCommit==1 || desiredAutoCommit==0 );
69362   assert( desiredAutoCommit==1 || iRollback==0 );
69363   assert( db->nVdbeActive>0 );  /* At least this one VM is active */
69364   assert( p->bIsReader );
69365 
69366 #if 0
69367   if( turnOnAC && iRollback && db->nVdbeActive>1 ){
69368     /* If this instruction implements a ROLLBACK and other VMs are
69369     ** still running, and a transaction is active, return an error indicating
69370     ** that the other VMs must complete first.
69371     */
69372     sqlite3SetString(&p->zErrMsg, db, "cannot rollback transaction - "
69373         "SQL statements in progress");
69374     rc = SQLITE_BUSY;
69375   }else
69376 #endif
69377   if( turnOnAC && !iRollback && db->nVdbeWrite>0 ){
69378     /* If this instruction implements a COMMIT and other VMs are writing
69379     ** return an error indicating that the other VMs must complete first.
69380     */
69381     sqlite3SetString(&p->zErrMsg, db, "cannot commit transaction - "
69382         "SQL statements in progress");
69383     rc = SQLITE_BUSY;
69384   }else if( desiredAutoCommit!=db->autoCommit ){
69385     if( iRollback ){
69386       assert( desiredAutoCommit==1 );
69387       sqlite3RollbackAll(db, SQLITE_ABORT_ROLLBACK);
69388       db->autoCommit = 1;
69389     }else if( (rc = sqlite3VdbeCheckFk(p, 1))!=SQLITE_OK ){
69390       goto vdbe_return;
69391     }else{
69392       db->autoCommit = (u8)desiredAutoCommit;
69393       if( sqlite3VdbeHalt(p)==SQLITE_BUSY ){
69394         p->pc = pc;
69395         db->autoCommit = (u8)(1-desiredAutoCommit);
69396         p->rc = rc = SQLITE_BUSY;
69397         goto vdbe_return;
69398       }
69399     }
69400     assert( db->nStatement==0 );
69401     sqlite3CloseSavepoints(db);
69402     if( p->rc==SQLITE_OK ){
69403       rc = SQLITE_DONE;
69404     }else{
69405       rc = SQLITE_ERROR;
69406     }
69407     goto vdbe_return;
69408   }else{
69409     sqlite3SetString(&p->zErrMsg, db,
69410         (!desiredAutoCommit)?"cannot start a transaction within a transaction":(
69411         (iRollback)?"cannot rollback - no transaction is active":
69412                    "cannot commit - no transaction is active"));
69413 
69414     rc = SQLITE_ERROR;
69415   }
69416   break;
69417 }
69418 
69419 /* Opcode: Transaction P1 P2 * * *
69420 **
69421 ** Begin a transaction.  The transaction ends when a Commit or Rollback
69422 ** opcode is encountered.  Depending on the ON CONFLICT setting, the
69423 ** transaction might also be rolled back if an error is encountered.
69424 **
69425 ** P1 is the index of the database file on which the transaction is
69426 ** started.  Index 0 is the main database file and index 1 is the
69427 ** file used for temporary tables.  Indices of 2 or more are used for
69428 ** attached databases.
69429 **
69430 ** If P2 is non-zero, then a write-transaction is started.  A RESERVED lock is
69431 ** obtained on the database file when a write-transaction is started.  No
69432 ** other process can start another write transaction while this transaction is
69433 ** underway.  Starting a write transaction also creates a rollback journal. A
69434 ** write transaction must be started before any changes can be made to the
69435 ** database.  If P2 is greater than or equal to 2 then an EXCLUSIVE lock is
69436 ** also obtained on the file.
69437 **
69438 ** If a write-transaction is started and the Vdbe.usesStmtJournal flag is
69439 ** true (this flag is set if the Vdbe may modify more than one row and may
69440 ** throw an ABORT exception), a statement transaction may also be opened.
69441 ** More specifically, a statement transaction is opened iff the database
69442 ** connection is currently not in autocommit mode, or if there are other
69443 ** active statements. A statement transaction allows the changes made by this
69444 ** VDBE to be rolled back after an error without having to roll back the
69445 ** entire transaction. If no error is encountered, the statement transaction
69446 ** will automatically commit when the VDBE halts.
69447 **
69448 ** If P2 is zero, then a read-lock is obtained on the database file.
69449 */
69450 case OP_Transaction: {
69451   Btree *pBt;
69452 
69453   assert( p->bIsReader );
69454   assert( p->readOnly==0 || pOp->p2==0 );
69455   assert( pOp->p1>=0 && pOp->p1<db->nDb );
69456   assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
69457   if( pOp->p2 && (db->flags & SQLITE_QueryOnly)!=0 ){
69458     rc = SQLITE_READONLY;
69459     goto abort_due_to_error;
69460   }
69461   pBt = db->aDb[pOp->p1].pBt;
69462 
69463   if( pBt ){
69464     rc = sqlite3BtreeBeginTrans(pBt, pOp->p2);
69465     if( rc==SQLITE_BUSY ){
69466       p->pc = pc;
69467       p->rc = rc = SQLITE_BUSY;
69468       goto vdbe_return;
69469     }
69470     if( rc!=SQLITE_OK ){
69471       goto abort_due_to_error;
69472     }
69473 
69474     if( pOp->p2 && p->usesStmtJournal
69475      && (db->autoCommit==0 || db->nVdbeRead>1)
69476     ){
69477       assert( sqlite3BtreeIsInTrans(pBt) );
69478       if( p->iStatement==0 ){
69479         assert( db->nStatement>=0 && db->nSavepoint>=0 );
69480         db->nStatement++;
69481         p->iStatement = db->nSavepoint + db->nStatement;
69482       }
69483 
69484       rc = sqlite3VtabSavepoint(db, SAVEPOINT_BEGIN, p->iStatement-1);
69485       if( rc==SQLITE_OK ){
69486         rc = sqlite3BtreeBeginStmt(pBt, p->iStatement);
69487       }
69488 
69489       /* Store the current value of the database handles deferred constraint
69490       ** counter. If the statement transaction needs to be rolled back,
69491       ** the value of this counter needs to be restored too.  */
69492       p->nStmtDefCons = db->nDeferredCons;
69493       p->nStmtDefImmCons = db->nDeferredImmCons;
69494     }
69495   }
69496   break;
69497 }
69498 
69499 /* Opcode: ReadCookie P1 P2 P3 * *
69500 **
69501 ** Read cookie number P3 from database P1 and write it into register P2.
69502 ** P3==1 is the schema version.  P3==2 is the database format.
69503 ** P3==3 is the recommended pager cache size, and so forth.  P1==0 is
69504 ** the main database file and P1==1 is the database file used to store
69505 ** temporary tables.
69506 **
69507 ** There must be a read-lock on the database (either a transaction
69508 ** must be started or there must be an open cursor) before
69509 ** executing this instruction.
69510 */
69511 case OP_ReadCookie: {               /* out2-prerelease */
69512   int iMeta;
69513   int iDb;
69514   int iCookie;
69515 
69516   assert( p->bIsReader );
69517   iDb = pOp->p1;
69518   iCookie = pOp->p3;
69519   assert( pOp->p3<SQLITE_N_BTREE_META );
69520   assert( iDb>=0 && iDb<db->nDb );
69521   assert( db->aDb[iDb].pBt!=0 );
69522   assert( (p->btreeMask & (((yDbMask)1)<<iDb))!=0 );
69523 
69524   sqlite3BtreeGetMeta(db->aDb[iDb].pBt, iCookie, (u32 *)&iMeta);
69525   pOut->u.i = iMeta;
69526   break;
69527 }
69528 
69529 /* Opcode: SetCookie P1 P2 P3 * *
69530 **
69531 ** Write the content of register P3 (interpreted as an integer)
69532 ** into cookie number P2 of database P1.  P2==1 is the schema version.
69533 ** P2==2 is the database format. P2==3 is the recommended pager cache
69534 ** size, and so forth.  P1==0 is the main database file and P1==1 is the
69535 ** database file used to store temporary tables.
69536 **
69537 ** A transaction must be started before executing this opcode.
69538 */
69539 case OP_SetCookie: {       /* in3 */
69540   Db *pDb;
69541   assert( pOp->p2<SQLITE_N_BTREE_META );
69542   assert( pOp->p1>=0 && pOp->p1<db->nDb );
69543   assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
69544   assert( p->readOnly==0 );
69545   pDb = &db->aDb[pOp->p1];
69546   assert( pDb->pBt!=0 );
69547   assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) );
69548   pIn3 = &aMem[pOp->p3];
69549   sqlite3VdbeMemIntegerify(pIn3);
69550   /* See note about index shifting on OP_ReadCookie */
69551   rc = sqlite3BtreeUpdateMeta(pDb->pBt, pOp->p2, (int)pIn3->u.i);
69552   if( pOp->p2==BTREE_SCHEMA_VERSION ){
69553     /* When the schema cookie changes, record the new cookie internally */
69554     pDb->pSchema->schema_cookie = (int)pIn3->u.i;
69555     db->flags |= SQLITE_InternChanges;
69556   }else if( pOp->p2==BTREE_FILE_FORMAT ){
69557     /* Record changes in the file format */
69558     pDb->pSchema->file_format = (u8)pIn3->u.i;
69559   }
69560   if( pOp->p1==1 ){
69561     /* Invalidate all prepared statements whenever the TEMP database
69562     ** schema is changed.  Ticket #1644 */
69563     sqlite3ExpirePreparedStatements(db);
69564     p->expired = 0;
69565   }
69566   break;
69567 }
69568 
69569 /* Opcode: VerifyCookie P1 P2 P3 * *
69570 **
69571 ** Check the value of global database parameter number 0 (the
69572 ** schema version) and make sure it is equal to P2 and that the
69573 ** generation counter on the local schema parse equals P3.
69574 **
69575 ** P1 is the database number which is 0 for the main database file
69576 ** and 1 for the file holding temporary tables and some higher number
69577 ** for auxiliary databases.
69578 **
69579 ** The cookie changes its value whenever the database schema changes.
69580 ** This operation is used to detect when that the cookie has changed
69581 ** and that the current process needs to reread the schema.
69582 **
69583 ** Either a transaction needs to have been started or an OP_Open needs
69584 ** to be executed (to establish a read lock) before this opcode is
69585 ** invoked.
69586 */
69587 case OP_VerifyCookie: {
69588   int iMeta;
69589   int iGen;
69590   Btree *pBt;
69591 
69592   assert( pOp->p1>=0 && pOp->p1<db->nDb );
69593   assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
69594   assert( sqlite3SchemaMutexHeld(db, pOp->p1, 0) );
69595   assert( p->bIsReader );
69596   pBt = db->aDb[pOp->p1].pBt;
69597   if( pBt ){
69598     sqlite3BtreeGetMeta(pBt, BTREE_SCHEMA_VERSION, (u32 *)&iMeta);
69599     iGen = db->aDb[pOp->p1].pSchema->iGeneration;
69600   }else{
69601     iGen = iMeta = 0;
69602   }
69603   if( iMeta!=pOp->p2 || iGen!=pOp->p3 ){
69604     sqlite3DbFree(db, p->zErrMsg);
69605     p->zErrMsg = sqlite3DbStrDup(db, "database schema has changed");
69606     /* If the schema-cookie from the database file matches the cookie
69607     ** stored with the in-memory representation of the schema, do
69608     ** not reload the schema from the database file.
69609     **
69610     ** If virtual-tables are in use, this is not just an optimization.
69611     ** Often, v-tables store their data in other SQLite tables, which
69612     ** are queried from within xNext() and other v-table methods using
69613     ** prepared queries. If such a query is out-of-date, we do not want to
69614     ** discard the database schema, as the user code implementing the
69615     ** v-table would have to be ready for the sqlite3_vtab structure itself
69616     ** to be invalidated whenever sqlite3_step() is called from within
69617     ** a v-table method.
69618     */
69619     if( db->aDb[pOp->p1].pSchema->schema_cookie!=iMeta ){
69620       sqlite3ResetOneSchema(db, pOp->p1);
69621     }
69622 
69623     p->expired = 1;
69624     rc = SQLITE_SCHEMA;
69625   }
69626   break;
69627 }
69628 
69629 /* Opcode: OpenRead P1 P2 P3 P4 P5
69630 ** Synopsis: root=P2 iDb=P3
69631 **
69632 ** Open a read-only cursor for the database table whose root page is
69633 ** P2 in a database file.  The database file is determined by P3.
69634 ** P3==0 means the main database, P3==1 means the database used for
69635 ** temporary tables, and P3>1 means used the corresponding attached
69636 ** database.  Give the new cursor an identifier of P1.  The P1
69637 ** values need not be contiguous but all P1 values should be small integers.
69638 ** It is an error for P1 to be negative.
69639 **
69640 ** If P5!=0 then use the content of register P2 as the root page, not
69641 ** the value of P2 itself.
69642 **
69643 ** There will be a read lock on the database whenever there is an
69644 ** open cursor.  If the database was unlocked prior to this instruction
69645 ** then a read lock is acquired as part of this instruction.  A read
69646 ** lock allows other processes to read the database but prohibits
69647 ** any other process from modifying the database.  The read lock is
69648 ** released when all cursors are closed.  If this instruction attempts
69649 ** to get a read lock but fails, the script terminates with an
69650 ** SQLITE_BUSY error code.
69651 **
69652 ** The P4 value may be either an integer (P4_INT32) or a pointer to
69653 ** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo
69654 ** structure, then said structure defines the content and collating
69655 ** sequence of the index being opened. Otherwise, if P4 is an integer
69656 ** value, it is set to the number of columns in the table.
69657 **
69658 ** See also OpenWrite.
69659 */
69660 /* Opcode: OpenWrite P1 P2 P3 P4 P5
69661 ** Synopsis: root=P2 iDb=P3
69662 **
69663 ** Open a read/write cursor named P1 on the table or index whose root
69664 ** page is P2.  Or if P5!=0 use the content of register P2 to find the
69665 ** root page.
69666 **
69667 ** The P4 value may be either an integer (P4_INT32) or a pointer to
69668 ** a KeyInfo structure (P4_KEYINFO). If it is a pointer to a KeyInfo
69669 ** structure, then said structure defines the content and collating
69670 ** sequence of the index being opened. Otherwise, if P4 is an integer
69671 ** value, it is set to the number of columns in the table, or to the
69672 ** largest index of any column of the table that is actually used.
69673 **
69674 ** This instruction works just like OpenRead except that it opens the cursor
69675 ** in read/write mode.  For a given table, there can be one or more read-only
69676 ** cursors or a single read/write cursor but not both.
69677 **
69678 ** See also OpenRead.
69679 */
69680 case OP_OpenRead:
69681 case OP_OpenWrite: {
69682   int nField;
69683   KeyInfo *pKeyInfo;
69684   int p2;
69685   int iDb;
69686   int wrFlag;
69687   Btree *pX;
69688   VdbeCursor *pCur;
69689   Db *pDb;
69690 
69691   assert( (pOp->p5&(OPFLAG_P2ISREG|OPFLAG_BULKCSR))==pOp->p5 );
69692   assert( pOp->opcode==OP_OpenWrite || pOp->p5==0 );
69693   assert( p->bIsReader );
69694   assert( pOp->opcode==OP_OpenRead || p->readOnly==0 );
69695 
69696   if( p->expired ){
69697     rc = SQLITE_ABORT;
69698     break;
69699   }
69700 
69701   nField = 0;
69702   pKeyInfo = 0;
69703   p2 = pOp->p2;
69704   iDb = pOp->p3;
69705   assert( iDb>=0 && iDb<db->nDb );
69706   assert( (p->btreeMask & (((yDbMask)1)<<iDb))!=0 );
69707   pDb = &db->aDb[iDb];
69708   pX = pDb->pBt;
69709   assert( pX!=0 );
69710   if( pOp->opcode==OP_OpenWrite ){
69711     wrFlag = 1;
69712     assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
69713     if( pDb->pSchema->file_format < p->minWriteFileFormat ){
69714       p->minWriteFileFormat = pDb->pSchema->file_format;
69715     }
69716   }else{
69717     wrFlag = 0;
69718   }
69719   if( pOp->p5 & OPFLAG_P2ISREG ){
69720     assert( p2>0 );
69721     assert( p2<=(p->nMem-p->nCursor) );
69722     pIn2 = &aMem[p2];
69723     assert( memIsValid(pIn2) );
69724     assert( (pIn2->flags & MEM_Int)!=0 );
69725     sqlite3VdbeMemIntegerify(pIn2);
69726     p2 = (int)pIn2->u.i;
69727     /* The p2 value always comes from a prior OP_CreateTable opcode and
69728     ** that opcode will always set the p2 value to 2 or more or else fail.
69729     ** If there were a failure, the prepared statement would have halted
69730     ** before reaching this instruction. */
69731     if( NEVER(p2<2) ) {
69732       rc = SQLITE_CORRUPT_BKPT;
69733       goto abort_due_to_error;
69734     }
69735   }
69736   if( pOp->p4type==P4_KEYINFO ){
69737     pKeyInfo = pOp->p4.pKeyInfo;
69738     assert( pKeyInfo->enc==ENC(db) );
69739     assert( pKeyInfo->db==db );
69740     nField = pKeyInfo->nField+pKeyInfo->nXField;
69741   }else if( pOp->p4type==P4_INT32 ){
69742     nField = pOp->p4.i;
69743   }
69744   assert( pOp->p1>=0 );
69745   assert( nField>=0 );
69746   testcase( nField==0 );  /* Table with INTEGER PRIMARY KEY and nothing else */
69747   pCur = allocateCursor(p, pOp->p1, nField, iDb, 1);
69748   if( pCur==0 ) goto no_mem;
69749   pCur->nullRow = 1;
69750   pCur->isOrdered = 1;
69751   rc = sqlite3BtreeCursor(pX, p2, wrFlag, pKeyInfo, pCur->pCursor);
69752   pCur->pKeyInfo = pKeyInfo;
69753   assert( OPFLAG_BULKCSR==BTREE_BULKLOAD );
69754   sqlite3BtreeCursorHints(pCur->pCursor, (pOp->p5 & OPFLAG_BULKCSR));
69755 
69756   /* Since it performs no memory allocation or IO, the only value that
69757   ** sqlite3BtreeCursor() may return is SQLITE_OK. */
69758   assert( rc==SQLITE_OK );
69759 
69760   /* Set the VdbeCursor.isTable variable. Previous versions of
69761   ** SQLite used to check if the root-page flags were sane at this point
69762   ** and report database corruption if they were not, but this check has
69763   ** since moved into the btree layer.  */
69764   pCur->isTable = pOp->p4type!=P4_KEYINFO;
69765   break;
69766 }
69767 
69768 /* Opcode: OpenEphemeral P1 P2 * P4 P5
69769 ** Synopsis: nColumn=P2
69770 **
69771 ** Open a new cursor P1 to a transient table.
69772 ** The cursor is always opened read/write even if
69773 ** the main database is read-only.  The ephemeral
69774 ** table is deleted automatically when the cursor is closed.
69775 **
69776 ** P2 is the number of columns in the ephemeral table.
69777 ** The cursor points to a BTree table if P4==0 and to a BTree index
69778 ** if P4 is not 0.  If P4 is not NULL, it points to a KeyInfo structure
69779 ** that defines the format of keys in the index.
69780 **
69781 ** The P5 parameter can be a mask of the BTREE_* flags defined
69782 ** in btree.h.  These flags control aspects of the operation of
69783 ** the btree.  The BTREE_OMIT_JOURNAL and BTREE_SINGLE flags are
69784 ** added automatically.
69785 */
69786 /* Opcode: OpenAutoindex P1 P2 * P4 *
69787 ** Synopsis: nColumn=P2
69788 **
69789 ** This opcode works the same as OP_OpenEphemeral.  It has a
69790 ** different name to distinguish its use.  Tables created using
69791 ** by this opcode will be used for automatically created transient
69792 ** indices in joins.
69793 */
69794 case OP_OpenAutoindex:
69795 case OP_OpenEphemeral: {
69796   VdbeCursor *pCx;
69797   KeyInfo *pKeyInfo;
69798 
69799   static const int vfsFlags =
69800       SQLITE_OPEN_READWRITE |
69801       SQLITE_OPEN_CREATE |
69802       SQLITE_OPEN_EXCLUSIVE |
69803       SQLITE_OPEN_DELETEONCLOSE |
69804       SQLITE_OPEN_TRANSIENT_DB;
69805   assert( pOp->p1>=0 );
69806   assert( pOp->p2>=0 );
69807   pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, 1);
69808   if( pCx==0 ) goto no_mem;
69809   pCx->nullRow = 1;
69810   rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pCx->pBt,
69811                         BTREE_OMIT_JOURNAL | BTREE_SINGLE | pOp->p5, vfsFlags);
69812   if( rc==SQLITE_OK ){
69813     rc = sqlite3BtreeBeginTrans(pCx->pBt, 1);
69814   }
69815   if( rc==SQLITE_OK ){
69816     /* If a transient index is required, create it by calling
69817     ** sqlite3BtreeCreateTable() with the BTREE_BLOBKEY flag before
69818     ** opening it. If a transient table is required, just use the
69819     ** automatically created table with root-page 1 (an BLOB_INTKEY table).
69820     */
69821     if( (pKeyInfo = pOp->p4.pKeyInfo)!=0 ){
69822       int pgno;
69823       assert( pOp->p4type==P4_KEYINFO );
69824       rc = sqlite3BtreeCreateTable(pCx->pBt, &pgno, BTREE_BLOBKEY | pOp->p5);
69825       if( rc==SQLITE_OK ){
69826         assert( pgno==MASTER_ROOT+1 );
69827         assert( pKeyInfo->db==db );
69828         assert( pKeyInfo->enc==ENC(db) );
69829         pCx->pKeyInfo = pKeyInfo;
69830         rc = sqlite3BtreeCursor(pCx->pBt, pgno, 1, pKeyInfo, pCx->pCursor);
69831       }
69832       pCx->isTable = 0;
69833     }else{
69834       rc = sqlite3BtreeCursor(pCx->pBt, MASTER_ROOT, 1, 0, pCx->pCursor);
69835       pCx->isTable = 1;
69836     }
69837   }
69838   pCx->isOrdered = (pOp->p5!=BTREE_UNORDERED);
69839   break;
69840 }
69841 
69842 /* Opcode: SorterOpen P1 * * P4 *
69843 **
69844 ** This opcode works like OP_OpenEphemeral except that it opens
69845 ** a transient index that is specifically designed to sort large
69846 ** tables using an external merge-sort algorithm.
69847 */
69848 case OP_SorterOpen: {
69849   VdbeCursor *pCx;
69850 
69851   assert( pOp->p1>=0 );
69852   assert( pOp->p2>=0 );
69853   pCx = allocateCursor(p, pOp->p1, pOp->p2, -1, 1);
69854   if( pCx==0 ) goto no_mem;
69855   pCx->pKeyInfo = pOp->p4.pKeyInfo;
69856   assert( pCx->pKeyInfo->db==db );
69857   assert( pCx->pKeyInfo->enc==ENC(db) );
69858   rc = sqlite3VdbeSorterInit(db, pCx);
69859   break;
69860 }
69861 
69862 /* Opcode: OpenPseudo P1 P2 P3 * P5
69863 ** Synopsis: content in r[P2@P3]
69864 **
69865 ** Open a new cursor that points to a fake table that contains a single
69866 ** row of data.  The content of that one row in the content of memory
69867 ** register P2 when P5==0.  In other words, cursor P1 becomes an alias for the
69868 ** MEM_Blob content contained in register P2.  When P5==1, then the
69869 ** row is represented by P3 consecutive registers beginning with P2.
69870 **
69871 ** A pseudo-table created by this opcode is used to hold a single
69872 ** row output from the sorter so that the row can be decomposed into
69873 ** individual columns using the OP_Column opcode.  The OP_Column opcode
69874 ** is the only cursor opcode that works with a pseudo-table.
69875 **
69876 ** P3 is the number of fields in the records that will be stored by
69877 ** the pseudo-table.
69878 */
69879 case OP_OpenPseudo: {
69880   VdbeCursor *pCx;
69881 
69882   assert( pOp->p1>=0 );
69883   assert( pOp->p3>=0 );
69884   pCx = allocateCursor(p, pOp->p1, pOp->p3, -1, 0);
69885   if( pCx==0 ) goto no_mem;
69886   pCx->nullRow = 1;
69887   pCx->pseudoTableReg = pOp->p2;
69888   pCx->isTable = 1;
69889   pCx->multiPseudo = pOp->p5;
69890   break;
69891 }
69892 
69893 /* Opcode: Close P1 * * * *
69894 **
69895 ** Close a cursor previously opened as P1.  If P1 is not
69896 ** currently open, this instruction is a no-op.
69897 */
69898 case OP_Close: {
69899   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
69900   sqlite3VdbeFreeCursor(p, p->apCsr[pOp->p1]);
69901   p->apCsr[pOp->p1] = 0;
69902   break;
69903 }
69904 
69905 /* Opcode: SeekGe P1 P2 P3 P4 *
69906 ** Synopsis: key=r[P3@P4]
69907 **
69908 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
69909 ** use the value in register P3 as the key.  If cursor P1 refers
69910 ** to an SQL index, then P3 is the first in an array of P4 registers
69911 ** that are used as an unpacked index key.
69912 **
69913 ** Reposition cursor P1 so that  it points to the smallest entry that
69914 ** is greater than or equal to the key value. If there are no records
69915 ** greater than or equal to the key and P2 is not zero, then jump to P2.
69916 **
69917 ** See also: Found, NotFound, Distinct, SeekLt, SeekGt, SeekLe
69918 */
69919 /* Opcode: SeekGt P1 P2 P3 P4 *
69920 ** Synopsis: key=r[P3@P4]
69921 **
69922 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
69923 ** use the value in register P3 as a key. If cursor P1 refers
69924 ** to an SQL index, then P3 is the first in an array of P4 registers
69925 ** that are used as an unpacked index key.
69926 **
69927 ** Reposition cursor P1 so that  it points to the smallest entry that
69928 ** is greater than the key value. If there are no records greater than
69929 ** the key and P2 is not zero, then jump to P2.
69930 **
69931 ** See also: Found, NotFound, Distinct, SeekLt, SeekGe, SeekLe
69932 */
69933 /* Opcode: SeekLt P1 P2 P3 P4 *
69934 ** Synopsis: key=r[P3@P4]
69935 **
69936 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
69937 ** use the value in register P3 as a key. If cursor P1 refers
69938 ** to an SQL index, then P3 is the first in an array of P4 registers
69939 ** that are used as an unpacked index key.
69940 **
69941 ** Reposition cursor P1 so that  it points to the largest entry that
69942 ** is less than the key value. If there are no records less than
69943 ** the key and P2 is not zero, then jump to P2.
69944 **
69945 ** See also: Found, NotFound, Distinct, SeekGt, SeekGe, SeekLe
69946 */
69947 /* Opcode: SeekLe P1 P2 P3 P4 *
69948 ** Synopsis: key=r[P3@P4]
69949 **
69950 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
69951 ** use the value in register P3 as a key. If cursor P1 refers
69952 ** to an SQL index, then P3 is the first in an array of P4 registers
69953 ** that are used as an unpacked index key.
69954 **
69955 ** Reposition cursor P1 so that it points to the largest entry that
69956 ** is less than or equal to the key value. If there are no records
69957 ** less than or equal to the key and P2 is not zero, then jump to P2.
69958 **
69959 ** See also: Found, NotFound, Distinct, SeekGt, SeekGe, SeekLt
69960 */
69961 case OP_SeekLt:         /* jump, in3 */
69962 case OP_SeekLe:         /* jump, in3 */
69963 case OP_SeekGe:         /* jump, in3 */
69964 case OP_SeekGt: {       /* jump, in3 */
69965   int res;
69966   int oc;
69967   VdbeCursor *pC;
69968   UnpackedRecord r;
69969   int nField;
69970   i64 iKey;      /* The rowid we are to seek to */
69971 
69972   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
69973   assert( pOp->p2!=0 );
69974   pC = p->apCsr[pOp->p1];
69975   assert( pC!=0 );
69976   assert( pC->pseudoTableReg==0 );
69977   assert( OP_SeekLe == OP_SeekLt+1 );
69978   assert( OP_SeekGe == OP_SeekLt+2 );
69979   assert( OP_SeekGt == OP_SeekLt+3 );
69980   assert( pC->isOrdered );
69981   assert( pC->pCursor!=0 );
69982   oc = pOp->opcode;
69983   pC->nullRow = 0;
69984   if( pC->isTable ){
69985     /* The input value in P3 might be of any type: integer, real, string,
69986     ** blob, or NULL.  But it needs to be an integer before we can do
69987     ** the seek, so covert it. */
69988     pIn3 = &aMem[pOp->p3];
69989     applyNumericAffinity(pIn3);
69990     iKey = sqlite3VdbeIntValue(pIn3);
69991     pC->rowidIsValid = 0;
69992 
69993     /* If the P3 value could not be converted into an integer without
69994     ** loss of information, then special processing is required... */
69995     if( (pIn3->flags & MEM_Int)==0 ){
69996       if( (pIn3->flags & MEM_Real)==0 ){
69997         /* If the P3 value cannot be converted into any kind of a number,
69998         ** then the seek is not possible, so jump to P2 */
69999         pc = pOp->p2 - 1;
70000         break;
70001       }
70002 
70003       /* If the approximation iKey is larger than the actual real search
70004       ** term, substitute >= for > and < for <=. e.g. if the search term
70005       ** is 4.9 and the integer approximation 5:
70006       **
70007       **        (x >  4.9)    ->     (x >= 5)
70008       **        (x <= 4.9)    ->     (x <  5)
70009       */
70010       if( pIn3->r<(double)iKey ){
70011         assert( OP_SeekGe==(OP_SeekGt-1) );
70012         assert( OP_SeekLt==(OP_SeekLe-1) );
70013         assert( (OP_SeekLe & 0x0001)==(OP_SeekGt & 0x0001) );
70014         if( (oc & 0x0001)==(OP_SeekGt & 0x0001) ) oc--;
70015       }
70016 
70017       /* If the approximation iKey is smaller than the actual real search
70018       ** term, substitute <= for < and > for >=.  */
70019       else if( pIn3->r>(double)iKey ){
70020         assert( OP_SeekLe==(OP_SeekLt+1) );
70021         assert( OP_SeekGt==(OP_SeekGe+1) );
70022         assert( (OP_SeekLt & 0x0001)==(OP_SeekGe & 0x0001) );
70023         if( (oc & 0x0001)==(OP_SeekLt & 0x0001) ) oc++;
70024       }
70025     }
70026     rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, 0, (u64)iKey, 0, &res);
70027     if( rc!=SQLITE_OK ){
70028       goto abort_due_to_error;
70029     }
70030     if( res==0 ){
70031       pC->rowidIsValid = 1;
70032       pC->lastRowid = iKey;
70033     }
70034   }else{
70035     nField = pOp->p4.i;
70036     assert( pOp->p4type==P4_INT32 );
70037     assert( nField>0 );
70038     r.pKeyInfo = pC->pKeyInfo;
70039     r.nField = (u16)nField;
70040 
70041     /* The next line of code computes as follows, only faster:
70042     **   if( oc==OP_SeekGt || oc==OP_SeekLe ){
70043     **     r.flags = UNPACKED_INCRKEY;
70044     **   }else{
70045     **     r.flags = 0;
70046     **   }
70047     */
70048     r.flags = (u8)(UNPACKED_INCRKEY * (1 & (oc - OP_SeekLt)));
70049     assert( oc!=OP_SeekGt || r.flags==UNPACKED_INCRKEY );
70050     assert( oc!=OP_SeekLe || r.flags==UNPACKED_INCRKEY );
70051     assert( oc!=OP_SeekGe || r.flags==0 );
70052     assert( oc!=OP_SeekLt || r.flags==0 );
70053 
70054     r.aMem = &aMem[pOp->p3];
70055 #ifdef SQLITE_DEBUG
70056     { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
70057 #endif
70058     ExpandBlob(r.aMem);
70059     rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, &r, 0, 0, &res);
70060     if( rc!=SQLITE_OK ){
70061       goto abort_due_to_error;
70062     }
70063     pC->rowidIsValid = 0;
70064   }
70065   pC->deferredMoveto = 0;
70066   pC->cacheStatus = CACHE_STALE;
70067 #ifdef SQLITE_TEST
70068   sqlite3_search_count++;
70069 #endif
70070   if( oc>=OP_SeekGe ){  assert( oc==OP_SeekGe || oc==OP_SeekGt );
70071     if( res<0 || (res==0 && oc==OP_SeekGt) ){
70072       rc = sqlite3BtreeNext(pC->pCursor, &res);
70073       if( rc!=SQLITE_OK ) goto abort_due_to_error;
70074       pC->rowidIsValid = 0;
70075     }else{
70076       res = 0;
70077     }
70078   }else{
70079     assert( oc==OP_SeekLt || oc==OP_SeekLe );
70080     if( res>0 || (res==0 && oc==OP_SeekLt) ){
70081       rc = sqlite3BtreePrevious(pC->pCursor, &res);
70082       if( rc!=SQLITE_OK ) goto abort_due_to_error;
70083       pC->rowidIsValid = 0;
70084     }else{
70085       /* res might be negative because the table is empty.  Check to
70086       ** see if this is the case.
70087       */
70088       res = sqlite3BtreeEof(pC->pCursor);
70089     }
70090   }
70091   assert( pOp->p2>0 );
70092   if( res ){
70093     pc = pOp->p2 - 1;
70094   }
70095   break;
70096 }
70097 
70098 /* Opcode: Seek P1 P2 * * *
70099 ** Synopsis:  intkey=r[P2]
70100 **
70101 ** P1 is an open table cursor and P2 is a rowid integer.  Arrange
70102 ** for P1 to move so that it points to the rowid given by P2.
70103 **
70104 ** This is actually a deferred seek.  Nothing actually happens until
70105 ** the cursor is used to read a record.  That way, if no reads
70106 ** occur, no unnecessary I/O happens.
70107 */
70108 case OP_Seek: {    /* in2 */
70109   VdbeCursor *pC;
70110 
70111   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
70112   pC = p->apCsr[pOp->p1];
70113   assert( pC!=0 );
70114   assert( pC->pCursor!=0 );
70115   assert( pC->isTable );
70116   pC->nullRow = 0;
70117   pIn2 = &aMem[pOp->p2];
70118   pC->movetoTarget = sqlite3VdbeIntValue(pIn2);
70119   pC->rowidIsValid = 0;
70120   pC->deferredMoveto = 1;
70121   break;
70122 }
70123 
70124 
70125 /* Opcode: Found P1 P2 P3 P4 *
70126 ** Synopsis: key=r[P3@P4]
70127 **
70128 ** If P4==0 then register P3 holds a blob constructed by MakeRecord.  If
70129 ** P4>0 then register P3 is the first of P4 registers that form an unpacked
70130 ** record.
70131 **
70132 ** Cursor P1 is on an index btree.  If the record identified by P3 and P4
70133 ** is a prefix of any entry in P1 then a jump is made to P2 and
70134 ** P1 is left pointing at the matching entry.
70135 **
70136 ** See also: NotFound, NoConflict, NotExists. SeekGe
70137 */
70138 /* Opcode: NotFound P1 P2 P3 P4 *
70139 ** Synopsis: key=r[P3@P4]
70140 **
70141 ** If P4==0 then register P3 holds a blob constructed by MakeRecord.  If
70142 ** P4>0 then register P3 is the first of P4 registers that form an unpacked
70143 ** record.
70144 **
70145 ** Cursor P1 is on an index btree.  If the record identified by P3 and P4
70146 ** is not the prefix of any entry in P1 then a jump is made to P2.  If P1
70147 ** does contain an entry whose prefix matches the P3/P4 record then control
70148 ** falls through to the next instruction and P1 is left pointing at the
70149 ** matching entry.
70150 **
70151 ** See also: Found, NotExists, NoConflict
70152 */
70153 /* Opcode: NoConflict P1 P2 P3 P4 *
70154 ** Synopsis: key=r[P3@P4]
70155 **
70156 ** If P4==0 then register P3 holds a blob constructed by MakeRecord.  If
70157 ** P4>0 then register P3 is the first of P4 registers that form an unpacked
70158 ** record.
70159 **
70160 ** Cursor P1 is on an index btree.  If the record identified by P3 and P4
70161 ** contains any NULL value, jump immediately to P2.  If all terms of the
70162 ** record are not-NULL then a check is done to determine if any row in the
70163 ** P1 index btree has a matching key prefix.  If there are no matches, jump
70164 ** immediately to P2.  If there is a match, fall through and leave the P1
70165 ** cursor pointing to the matching row.
70166 **
70167 ** This opcode is similar to OP_NotFound with the exceptions that the
70168 ** branch is always taken if any part of the search key input is NULL.
70169 **
70170 ** See also: NotFound, Found, NotExists
70171 */
70172 case OP_NoConflict:     /* jump, in3 */
70173 case OP_NotFound:       /* jump, in3 */
70174 case OP_Found: {        /* jump, in3 */
70175   int alreadyExists;
70176   int ii;
70177   VdbeCursor *pC;
70178   int res;
70179   char *pFree;
70180   UnpackedRecord *pIdxKey;
70181   UnpackedRecord r;
70182   char aTempRec[ROUND8(sizeof(UnpackedRecord)) + sizeof(Mem)*4 + 7];
70183 
70184 #ifdef SQLITE_TEST
70185   if( pOp->opcode!=OP_NoConflict ) sqlite3_found_count++;
70186 #endif
70187 
70188   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
70189   assert( pOp->p4type==P4_INT32 );
70190   pC = p->apCsr[pOp->p1];
70191   assert( pC!=0 );
70192   pIn3 = &aMem[pOp->p3];
70193   assert( pC->pCursor!=0 );
70194   assert( pC->isTable==0 );
70195   pFree = 0;  /* Not needed.  Only used to suppress a compiler warning. */
70196   if( pOp->p4.i>0 ){
70197     r.pKeyInfo = pC->pKeyInfo;
70198     r.nField = (u16)pOp->p4.i;
70199     r.aMem = pIn3;
70200 #ifdef SQLITE_DEBUG
70201     {
70202       int i;
70203       for(i=0; i<r.nField; i++){
70204         assert( memIsValid(&r.aMem[i]) );
70205         if( i ) REGISTER_TRACE(pOp->p3+i, &r.aMem[i]);
70206       }
70207     }
70208 #endif
70209     r.flags = UNPACKED_PREFIX_MATCH;
70210     pIdxKey = &r;
70211   }else{
70212     pIdxKey = sqlite3VdbeAllocUnpackedRecord(
70213         pC->pKeyInfo, aTempRec, sizeof(aTempRec), &pFree
70214     );
70215     if( pIdxKey==0 ) goto no_mem;
70216     assert( pIn3->flags & MEM_Blob );
70217     assert( (pIn3->flags & MEM_Zero)==0 );  /* zeroblobs already expanded */
70218     sqlite3VdbeRecordUnpack(pC->pKeyInfo, pIn3->n, pIn3->z, pIdxKey);
70219     pIdxKey->flags |= UNPACKED_PREFIX_MATCH;
70220   }
70221   if( pOp->opcode==OP_NoConflict ){
70222     /* For the OP_NoConflict opcode, take the jump if any of the
70223     ** input fields are NULL, since any key with a NULL will not
70224     ** conflict */
70225     for(ii=0; ii<r.nField; ii++){
70226       if( r.aMem[ii].flags & MEM_Null ){
70227         pc = pOp->p2 - 1;
70228         break;
70229       }
70230     }
70231   }
70232   rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, pIdxKey, 0, 0, &res);
70233   if( pOp->p4.i==0 ){
70234     sqlite3DbFree(db, pFree);
70235   }
70236   if( rc!=SQLITE_OK ){
70237     break;
70238   }
70239   pC->seekResult = res;
70240   alreadyExists = (res==0);
70241   pC->nullRow = 1-alreadyExists;
70242   pC->deferredMoveto = 0;
70243   pC->cacheStatus = CACHE_STALE;
70244   if( pOp->opcode==OP_Found ){
70245     if( alreadyExists ) pc = pOp->p2 - 1;
70246   }else{
70247     if( !alreadyExists ) pc = pOp->p2 - 1;
70248   }
70249   break;
70250 }
70251 
70252 /* Opcode: NotExists P1 P2 P3 * *
70253 ** Synopsis: intkey=r[P3]
70254 **
70255 ** P1 is the index of a cursor open on an SQL table btree (with integer
70256 ** keys).  P3 is an integer rowid.  If P1 does not contain a record with
70257 ** rowid P3 then jump immediately to P2.  If P1 does contain a record
70258 ** with rowid P3 then leave the cursor pointing at that record and fall
70259 ** through to the next instruction.
70260 **
70261 ** The OP_NotFound opcode performs the same operation on index btrees
70262 ** (with arbitrary multi-value keys).
70263 **
70264 ** See also: Found, NotFound, NoConflict
70265 */
70266 case OP_NotExists: {        /* jump, in3 */
70267   VdbeCursor *pC;
70268   BtCursor *pCrsr;
70269   int res;
70270   u64 iKey;
70271 
70272   pIn3 = &aMem[pOp->p3];
70273   assert( pIn3->flags & MEM_Int );
70274   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
70275   pC = p->apCsr[pOp->p1];
70276   assert( pC!=0 );
70277   assert( pC->isTable );
70278   assert( pC->pseudoTableReg==0 );
70279   pCrsr = pC->pCursor;
70280   assert( pCrsr!=0 );
70281   res = 0;
70282   iKey = pIn3->u.i;
70283   rc = sqlite3BtreeMovetoUnpacked(pCrsr, 0, iKey, 0, &res);
70284   pC->lastRowid = pIn3->u.i;
70285   pC->rowidIsValid = res==0 ?1:0;
70286   pC->nullRow = 0;
70287   pC->cacheStatus = CACHE_STALE;
70288   pC->deferredMoveto = 0;
70289   if( res!=0 ){
70290     pc = pOp->p2 - 1;
70291     assert( pC->rowidIsValid==0 );
70292   }
70293   pC->seekResult = res;
70294   break;
70295 }
70296 
70297 /* Opcode: Sequence P1 P2 * * *
70298 ** Synopsis: r[P2]=rowid
70299 **
70300 ** Find the next available sequence number for cursor P1.
70301 ** Write the sequence number into register P2.
70302 ** The sequence number on the cursor is incremented after this
70303 ** instruction.
70304 */
70305 case OP_Sequence: {           /* out2-prerelease */
70306   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
70307   assert( p->apCsr[pOp->p1]!=0 );
70308   pOut->u.i = p->apCsr[pOp->p1]->seqCount++;
70309   break;
70310 }
70311 
70312 
70313 /* Opcode: NewRowid P1 P2 P3 * *
70314 ** Synopsis: r[P2]=rowid
70315 **
70316 ** Get a new integer record number (a.k.a "rowid") used as the key to a table.
70317 ** The record number is not previously used as a key in the database
70318 ** table that cursor P1 points to.  The new record number is written
70319 ** written to register P2.
70320 **
70321 ** If P3>0 then P3 is a register in the root frame of this VDBE that holds
70322 ** the largest previously generated record number. No new record numbers are
70323 ** allowed to be less than this value. When this value reaches its maximum,
70324 ** an SQLITE_FULL error is generated. The P3 register is updated with the '
70325 ** generated record number. This P3 mechanism is used to help implement the
70326 ** AUTOINCREMENT feature.
70327 */
70328 case OP_NewRowid: {           /* out2-prerelease */
70329   i64 v;                 /* The new rowid */
70330   VdbeCursor *pC;        /* Cursor of table to get the new rowid */
70331   int res;               /* Result of an sqlite3BtreeLast() */
70332   int cnt;               /* Counter to limit the number of searches */
70333   Mem *pMem;             /* Register holding largest rowid for AUTOINCREMENT */
70334   VdbeFrame *pFrame;     /* Root frame of VDBE */
70335 
70336   v = 0;
70337   res = 0;
70338   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
70339   pC = p->apCsr[pOp->p1];
70340   assert( pC!=0 );
70341   if( NEVER(pC->pCursor==0) ){
70342     /* The zero initialization above is all that is needed */
70343   }else{
70344     /* The next rowid or record number (different terms for the same
70345     ** thing) is obtained in a two-step algorithm.
70346     **
70347     ** First we attempt to find the largest existing rowid and add one
70348     ** to that.  But if the largest existing rowid is already the maximum
70349     ** positive integer, we have to fall through to the second
70350     ** probabilistic algorithm
70351     **
70352     ** The second algorithm is to select a rowid at random and see if
70353     ** it already exists in the table.  If it does not exist, we have
70354     ** succeeded.  If the random rowid does exist, we select a new one
70355     ** and try again, up to 100 times.
70356     */
70357     assert( pC->isTable );
70358 
70359 #ifdef SQLITE_32BIT_ROWID
70360 #   define MAX_ROWID 0x7fffffff
70361 #else
70362     /* Some compilers complain about constants of the form 0x7fffffffffffffff.
70363     ** Others complain about 0x7ffffffffffffffffLL.  The following macro seems
70364     ** to provide the constant while making all compilers happy.
70365     */
70366 #   define MAX_ROWID  (i64)( (((u64)0x7fffffff)<<32) | (u64)0xffffffff )
70367 #endif
70368 
70369     if( !pC->useRandomRowid ){
70370       v = sqlite3BtreeGetCachedRowid(pC->pCursor);
70371       if( v==0 ){
70372         rc = sqlite3BtreeLast(pC->pCursor, &res);
70373         if( rc!=SQLITE_OK ){
70374           goto abort_due_to_error;
70375         }
70376         if( res ){
70377           v = 1;   /* IMP: R-61914-48074 */
70378         }else{
70379           assert( sqlite3BtreeCursorIsValid(pC->pCursor) );
70380           rc = sqlite3BtreeKeySize(pC->pCursor, &v);
70381           assert( rc==SQLITE_OK );   /* Cannot fail following BtreeLast() */
70382           if( v>=MAX_ROWID ){
70383             pC->useRandomRowid = 1;
70384           }else{
70385             v++;   /* IMP: R-29538-34987 */
70386           }
70387         }
70388       }
70389 
70390 #ifndef SQLITE_OMIT_AUTOINCREMENT
70391       if( pOp->p3 ){
70392         /* Assert that P3 is a valid memory cell. */
70393         assert( pOp->p3>0 );
70394         if( p->pFrame ){
70395           for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
70396           /* Assert that P3 is a valid memory cell. */
70397           assert( pOp->p3<=pFrame->nMem );
70398           pMem = &pFrame->aMem[pOp->p3];
70399         }else{
70400           /* Assert that P3 is a valid memory cell. */
70401           assert( pOp->p3<=(p->nMem-p->nCursor) );
70402           pMem = &aMem[pOp->p3];
70403           memAboutToChange(p, pMem);
70404         }
70405         assert( memIsValid(pMem) );
70406 
70407         REGISTER_TRACE(pOp->p3, pMem);
70408         sqlite3VdbeMemIntegerify(pMem);
70409         assert( (pMem->flags & MEM_Int)!=0 );  /* mem(P3) holds an integer */
70410         if( pMem->u.i==MAX_ROWID || pC->useRandomRowid ){
70411           rc = SQLITE_FULL;   /* IMP: R-12275-61338 */
70412           goto abort_due_to_error;
70413         }
70414         if( v<pMem->u.i+1 ){
70415           v = pMem->u.i + 1;
70416         }
70417         pMem->u.i = v;
70418       }
70419 #endif
70420 
70421       sqlite3BtreeSetCachedRowid(pC->pCursor, v<MAX_ROWID ? v+1 : 0);
70422     }
70423     if( pC->useRandomRowid ){
70424       /* IMPLEMENTATION-OF: R-07677-41881 If the largest ROWID is equal to the
70425       ** largest possible integer (9223372036854775807) then the database
70426       ** engine starts picking positive candidate ROWIDs at random until
70427       ** it finds one that is not previously used. */
70428       assert( pOp->p3==0 );  /* We cannot be in random rowid mode if this is
70429                              ** an AUTOINCREMENT table. */
70430       /* on the first attempt, simply do one more than previous */
70431       v = lastRowid;
70432       v &= (MAX_ROWID>>1); /* ensure doesn't go negative */
70433       v++; /* ensure non-zero */
70434       cnt = 0;
70435       while(   ((rc = sqlite3BtreeMovetoUnpacked(pC->pCursor, 0, (u64)v,
70436                                                  0, &res))==SQLITE_OK)
70437             && (res==0)
70438             && (++cnt<100)){
70439         /* collision - try another random rowid */
70440         sqlite3_randomness(sizeof(v), &v);
70441         if( cnt<5 ){
70442           /* try "small" random rowids for the initial attempts */
70443           v &= 0xffffff;
70444         }else{
70445           v &= (MAX_ROWID>>1); /* ensure doesn't go negative */
70446         }
70447         v++; /* ensure non-zero */
70448       }
70449       if( rc==SQLITE_OK && res==0 ){
70450         rc = SQLITE_FULL;   /* IMP: R-38219-53002 */
70451         goto abort_due_to_error;
70452       }
70453       assert( v>0 );  /* EV: R-40812-03570 */
70454     }
70455     pC->rowidIsValid = 0;
70456     pC->deferredMoveto = 0;
70457     pC->cacheStatus = CACHE_STALE;
70458   }
70459   pOut->u.i = v;
70460   break;
70461 }
70462 
70463 /* Opcode: Insert P1 P2 P3 P4 P5
70464 ** Synopsis: intkey=r[P3] data=r[P2]
70465 **
70466 ** Write an entry into the table of cursor P1.  A new entry is
70467 ** created if it doesn't already exist or the data for an existing
70468 ** entry is overwritten.  The data is the value MEM_Blob stored in register
70469 ** number P2. The key is stored in register P3. The key must
70470 ** be a MEM_Int.
70471 **
70472 ** If the OPFLAG_NCHANGE flag of P5 is set, then the row change count is
70473 ** incremented (otherwise not).  If the OPFLAG_LASTROWID flag of P5 is set,
70474 ** then rowid is stored for subsequent return by the
70475 ** sqlite3_last_insert_rowid() function (otherwise it is unmodified).
70476 **
70477 ** If the OPFLAG_USESEEKRESULT flag of P5 is set and if the result of
70478 ** the last seek operation (OP_NotExists) was a success, then this
70479 ** operation will not attempt to find the appropriate row before doing
70480 ** the insert but will instead overwrite the row that the cursor is
70481 ** currently pointing to.  Presumably, the prior OP_NotExists opcode
70482 ** has already positioned the cursor correctly.  This is an optimization
70483 ** that boosts performance by avoiding redundant seeks.
70484 **
70485 ** If the OPFLAG_ISUPDATE flag is set, then this opcode is part of an
70486 ** UPDATE operation.  Otherwise (if the flag is clear) then this opcode
70487 ** is part of an INSERT operation.  The difference is only important to
70488 ** the update hook.
70489 **
70490 ** Parameter P4 may point to a string containing the table-name, or
70491 ** may be NULL. If it is not NULL, then the update-hook
70492 ** (sqlite3.xUpdateCallback) is invoked following a successful insert.
70493 **
70494 ** (WARNING/TODO: If P1 is a pseudo-cursor and P2 is dynamically
70495 ** allocated, then ownership of P2 is transferred to the pseudo-cursor
70496 ** and register P2 becomes ephemeral.  If the cursor is changed, the
70497 ** value of register P2 will then change.  Make sure this does not
70498 ** cause any problems.)
70499 **
70500 ** This instruction only works on tables.  The equivalent instruction
70501 ** for indices is OP_IdxInsert.
70502 */
70503 /* Opcode: InsertInt P1 P2 P3 P4 P5
70504 ** Synopsis:  intkey=P3 data=r[P2]
70505 **
70506 ** This works exactly like OP_Insert except that the key is the
70507 ** integer value P3, not the value of the integer stored in register P3.
70508 */
70509 case OP_Insert:
70510 case OP_InsertInt: {
70511   Mem *pData;       /* MEM cell holding data for the record to be inserted */
70512   Mem *pKey;        /* MEM cell holding key  for the record */
70513   i64 iKey;         /* The integer ROWID or key for the record to be inserted */
70514   VdbeCursor *pC;   /* Cursor to table into which insert is written */
70515   int nZero;        /* Number of zero-bytes to append */
70516   int seekResult;   /* Result of prior seek or 0 if no USESEEKRESULT flag */
70517   const char *zDb;  /* database name - used by the update hook */
70518   const char *zTbl; /* Table name - used by the opdate hook */
70519   int op;           /* Opcode for update hook: SQLITE_UPDATE or SQLITE_INSERT */
70520 
70521   pData = &aMem[pOp->p2];
70522   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
70523   assert( memIsValid(pData) );
70524   pC = p->apCsr[pOp->p1];
70525   assert( pC!=0 );
70526   assert( pC->pCursor!=0 );
70527   assert( pC->pseudoTableReg==0 );
70528   assert( pC->isTable );
70529   REGISTER_TRACE(pOp->p2, pData);
70530 
70531   if( pOp->opcode==OP_Insert ){
70532     pKey = &aMem[pOp->p3];
70533     assert( pKey->flags & MEM_Int );
70534     assert( memIsValid(pKey) );
70535     REGISTER_TRACE(pOp->p3, pKey);
70536     iKey = pKey->u.i;
70537   }else{
70538     assert( pOp->opcode==OP_InsertInt );
70539     iKey = pOp->p3;
70540   }
70541 
70542   if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
70543   if( pOp->p5 & OPFLAG_LASTROWID ) db->lastRowid = lastRowid = iKey;
70544   if( pData->flags & MEM_Null ){
70545     pData->z = 0;
70546     pData->n = 0;
70547   }else{
70548     assert( pData->flags & (MEM_Blob|MEM_Str) );
70549   }
70550   seekResult = ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0);
70551   if( pData->flags & MEM_Zero ){
70552     nZero = pData->u.nZero;
70553   }else{
70554     nZero = 0;
70555   }
70556   sqlite3BtreeSetCachedRowid(pC->pCursor, 0);
70557   rc = sqlite3BtreeInsert(pC->pCursor, 0, iKey,
70558                           pData->z, pData->n, nZero,
70559                           (pOp->p5 & OPFLAG_APPEND)!=0, seekResult
70560   );
70561   pC->rowidIsValid = 0;
70562   pC->deferredMoveto = 0;
70563   pC->cacheStatus = CACHE_STALE;
70564 
70565   /* Invoke the update-hook if required. */
70566   if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z ){
70567     zDb = db->aDb[pC->iDb].zName;
70568     zTbl = pOp->p4.z;
70569     op = ((pOp->p5 & OPFLAG_ISUPDATE) ? SQLITE_UPDATE : SQLITE_INSERT);
70570     assert( pC->isTable );
70571     db->xUpdateCallback(db->pUpdateArg, op, zDb, zTbl, iKey);
70572     assert( pC->iDb>=0 );
70573   }
70574   break;
70575 }
70576 
70577 /* Opcode: Delete P1 P2 * P4 *
70578 **
70579 ** Delete the record at which the P1 cursor is currently pointing.
70580 **
70581 ** The cursor will be left pointing at either the next or the previous
70582 ** record in the table. If it is left pointing at the next record, then
70583 ** the next Next instruction will be a no-op.  Hence it is OK to delete
70584 ** a record from within an Next loop.
70585 **
70586 ** If the OPFLAG_NCHANGE flag of P2 is set, then the row change count is
70587 ** incremented (otherwise not).
70588 **
70589 ** P1 must not be pseudo-table.  It has to be a real table with
70590 ** multiple rows.
70591 **
70592 ** If P4 is not NULL, then it is the name of the table that P1 is
70593 ** pointing to.  The update hook will be invoked, if it exists.
70594 ** If P4 is not NULL then the P1 cursor must have been positioned
70595 ** using OP_NotFound prior to invoking this opcode.
70596 */
70597 case OP_Delete: {
70598   i64 iKey;
70599   VdbeCursor *pC;
70600 
70601   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
70602   pC = p->apCsr[pOp->p1];
70603   assert( pC!=0 );
70604   assert( pC->pCursor!=0 );  /* Only valid for real tables, no pseudotables */
70605   iKey = pC->lastRowid;      /* Only used for the update hook */
70606 
70607   /* The OP_Delete opcode always follows an OP_NotExists or OP_Last or
70608   ** OP_Column on the same table without any intervening operations that
70609   ** might move or invalidate the cursor.  Hence cursor pC is always pointing
70610   ** to the row to be deleted and the sqlite3VdbeCursorMoveto() operation
70611   ** below is always a no-op and cannot fail.  We will run it anyhow, though,
70612   ** to guard against future changes to the code generator.
70613   **/
70614   assert( pC->deferredMoveto==0 );
70615   rc = sqlite3VdbeCursorMoveto(pC);
70616   if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error;
70617 
70618   sqlite3BtreeSetCachedRowid(pC->pCursor, 0);
70619   rc = sqlite3BtreeDelete(pC->pCursor);
70620   pC->cacheStatus = CACHE_STALE;
70621 
70622   /* Invoke the update-hook if required. */
70623   if( rc==SQLITE_OK && db->xUpdateCallback && pOp->p4.z && pC->isTable ){
70624     db->xUpdateCallback(db->pUpdateArg, SQLITE_DELETE,
70625                         db->aDb[pC->iDb].zName, pOp->p4.z, iKey);
70626     assert( pC->iDb>=0 );
70627   }
70628   if( pOp->p2 & OPFLAG_NCHANGE ) p->nChange++;
70629   break;
70630 }
70631 /* Opcode: ResetCount * * * * *
70632 **
70633 ** The value of the change counter is copied to the database handle
70634 ** change counter (returned by subsequent calls to sqlite3_changes()).
70635 ** Then the VMs internal change counter resets to 0.
70636 ** This is used by trigger programs.
70637 */
70638 case OP_ResetCount: {
70639   sqlite3VdbeSetChanges(db, p->nChange);
70640   p->nChange = 0;
70641   break;
70642 }
70643 
70644 /* Opcode: SorterCompare P1 P2 P3 P4
70645 ** Synopsis:  if key(P1)!=rtrim(r[P3],P4) goto P2
70646 **
70647 ** P1 is a sorter cursor. This instruction compares a prefix of the
70648 ** the record blob in register P3 against a prefix of the entry that
70649 ** the sorter cursor currently points to.  The final P4 fields of both
70650 ** the P3 and sorter record are ignored.
70651 **
70652 ** If either P3 or the sorter contains a NULL in one of their significant
70653 ** fields (not counting the P4 fields at the end which are ignored) then
70654 ** the comparison is assumed to be equal.
70655 **
70656 ** Fall through to next instruction if the two records compare equal to
70657 ** each other.  Jump to P2 if they are different.
70658 */
70659 case OP_SorterCompare: {
70660   VdbeCursor *pC;
70661   int res;
70662   int nIgnore;
70663 
70664   pC = p->apCsr[pOp->p1];
70665   assert( isSorter(pC) );
70666   assert( pOp->p4type==P4_INT32 );
70667   pIn3 = &aMem[pOp->p3];
70668   nIgnore = pOp->p4.i;
70669   rc = sqlite3VdbeSorterCompare(pC, pIn3, nIgnore, &res);
70670   if( res ){
70671     pc = pOp->p2-1;
70672   }
70673   break;
70674 };
70675 
70676 /* Opcode: SorterData P1 P2 * * *
70677 ** Synopsis: r[P2]=data
70678 **
70679 ** Write into register P2 the current sorter data for sorter cursor P1.
70680 */
70681 case OP_SorterData: {
70682   VdbeCursor *pC;
70683 
70684   pOut = &aMem[pOp->p2];
70685   pC = p->apCsr[pOp->p1];
70686   assert( isSorter(pC) );
70687   rc = sqlite3VdbeSorterRowkey(pC, pOut);
70688   break;
70689 }
70690 
70691 /* Opcode: RowData P1 P2 * * *
70692 ** Synopsis: r[P2]=data
70693 **
70694 ** Write into register P2 the complete row data for cursor P1.
70695 ** There is no interpretation of the data.
70696 ** It is just copied onto the P2 register exactly as
70697 ** it is found in the database file.
70698 **
70699 ** If the P1 cursor must be pointing to a valid row (not a NULL row)
70700 ** of a real table, not a pseudo-table.
70701 */
70702 /* Opcode: RowKey P1 P2 * * *
70703 ** Synopsis: r[P2]=key
70704 **
70705 ** Write into register P2 the complete row key for cursor P1.
70706 ** There is no interpretation of the data.
70707 ** The key is copied onto the P3 register exactly as
70708 ** it is found in the database file.
70709 **
70710 ** If the P1 cursor must be pointing to a valid row (not a NULL row)
70711 ** of a real table, not a pseudo-table.
70712 */
70713 case OP_RowKey:
70714 case OP_RowData: {
70715   VdbeCursor *pC;
70716   BtCursor *pCrsr;
70717   u32 n;
70718   i64 n64;
70719 
70720   pOut = &aMem[pOp->p2];
70721   memAboutToChange(p, pOut);
70722 
70723   /* Note that RowKey and RowData are really exactly the same instruction */
70724   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
70725   pC = p->apCsr[pOp->p1];
70726   assert( isSorter(pC)==0 );
70727   assert( pC->isTable || pOp->opcode!=OP_RowData );
70728   assert( pC->isTable==0 || pOp->opcode==OP_RowData );
70729   assert( pC!=0 );
70730   assert( pC->nullRow==0 );
70731   assert( pC->pseudoTableReg==0 );
70732   assert( pC->pCursor!=0 );
70733   pCrsr = pC->pCursor;
70734   assert( sqlite3BtreeCursorIsValid(pCrsr) );
70735 
70736   /* The OP_RowKey and OP_RowData opcodes always follow OP_NotExists or
70737   ** OP_Rewind/Op_Next with no intervening instructions that might invalidate
70738   ** the cursor.  Hence the following sqlite3VdbeCursorMoveto() call is always
70739   ** a no-op and can never fail.  But we leave it in place as a safety.
70740   */
70741   assert( pC->deferredMoveto==0 );
70742   rc = sqlite3VdbeCursorMoveto(pC);
70743   if( NEVER(rc!=SQLITE_OK) ) goto abort_due_to_error;
70744 
70745   if( pC->isTable==0 ){
70746     assert( !pC->isTable );
70747     VVA_ONLY(rc =) sqlite3BtreeKeySize(pCrsr, &n64);
70748     assert( rc==SQLITE_OK );    /* True because of CursorMoveto() call above */
70749     if( n64>db->aLimit[SQLITE_LIMIT_LENGTH] ){
70750       goto too_big;
70751     }
70752     n = (u32)n64;
70753   }else{
70754     VVA_ONLY(rc =) sqlite3BtreeDataSize(pCrsr, &n);
70755     assert( rc==SQLITE_OK );    /* DataSize() cannot fail */
70756     if( n>(u32)db->aLimit[SQLITE_LIMIT_LENGTH] ){
70757       goto too_big;
70758     }
70759   }
70760   if( sqlite3VdbeMemGrow(pOut, n, 0) ){
70761     goto no_mem;
70762   }
70763   pOut->n = n;
70764   MemSetTypeFlag(pOut, MEM_Blob);
70765   if( pC->isTable==0 ){
70766     rc = sqlite3BtreeKey(pCrsr, 0, n, pOut->z);
70767   }else{
70768     rc = sqlite3BtreeData(pCrsr, 0, n, pOut->z);
70769   }
70770   pOut->enc = SQLITE_UTF8;  /* In case the blob is ever cast to text */
70771   UPDATE_MAX_BLOBSIZE(pOut);
70772   REGISTER_TRACE(pOp->p2, pOut);
70773   break;
70774 }
70775 
70776 /* Opcode: Rowid P1 P2 * * *
70777 ** Synopsis: r[P2]=rowid
70778 **
70779 ** Store in register P2 an integer which is the key of the table entry that
70780 ** P1 is currently point to.
70781 **
70782 ** P1 can be either an ordinary table or a virtual table.  There used to
70783 ** be a separate OP_VRowid opcode for use with virtual tables, but this
70784 ** one opcode now works for both table types.
70785 */
70786 case OP_Rowid: {                 /* out2-prerelease */
70787   VdbeCursor *pC;
70788   i64 v;
70789   sqlite3_vtab *pVtab;
70790   const sqlite3_module *pModule;
70791 
70792   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
70793   pC = p->apCsr[pOp->p1];
70794   assert( pC!=0 );
70795   assert( pC->pseudoTableReg==0 || pC->nullRow );
70796   if( pC->nullRow ){
70797     pOut->flags = MEM_Null;
70798     break;
70799   }else if( pC->deferredMoveto ){
70800     v = pC->movetoTarget;
70801 #ifndef SQLITE_OMIT_VIRTUALTABLE
70802   }else if( pC->pVtabCursor ){
70803     pVtab = pC->pVtabCursor->pVtab;
70804     pModule = pVtab->pModule;
70805     assert( pModule->xRowid );
70806     rc = pModule->xRowid(pC->pVtabCursor, &v);
70807     sqlite3VtabImportErrmsg(p, pVtab);
70808 #endif /* SQLITE_OMIT_VIRTUALTABLE */
70809   }else{
70810     assert( pC->pCursor!=0 );
70811     rc = sqlite3VdbeCursorMoveto(pC);
70812     if( rc ) goto abort_due_to_error;
70813     if( pC->rowidIsValid ){
70814       v = pC->lastRowid;
70815     }else{
70816       rc = sqlite3BtreeKeySize(pC->pCursor, &v);
70817       assert( rc==SQLITE_OK );  /* Always so because of CursorMoveto() above */
70818     }
70819   }
70820   pOut->u.i = v;
70821   break;
70822 }
70823 
70824 /* Opcode: NullRow P1 * * * *
70825 **
70826 ** Move the cursor P1 to a null row.  Any OP_Column operations
70827 ** that occur while the cursor is on the null row will always
70828 ** write a NULL.
70829 */
70830 case OP_NullRow: {
70831   VdbeCursor *pC;
70832 
70833   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
70834   pC = p->apCsr[pOp->p1];
70835   assert( pC!=0 );
70836   pC->nullRow = 1;
70837   pC->rowidIsValid = 0;
70838   pC->cacheStatus = CACHE_STALE;
70839   if( pC->pCursor ){
70840     sqlite3BtreeClearCursor(pC->pCursor);
70841   }
70842   break;
70843 }
70844 
70845 /* Opcode: Last P1 P2 * * *
70846 **
70847 ** The next use of the Rowid or Column or Next instruction for P1
70848 ** will refer to the last entry in the database table or index.
70849 ** If the table or index is empty and P2>0, then jump immediately to P2.
70850 ** If P2 is 0 or if the table or index is not empty, fall through
70851 ** to the following instruction.
70852 */
70853 case OP_Last: {        /* jump */
70854   VdbeCursor *pC;
70855   BtCursor *pCrsr;
70856   int res;
70857 
70858   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
70859   pC = p->apCsr[pOp->p1];
70860   assert( pC!=0 );
70861   pCrsr = pC->pCursor;
70862   res = 0;
70863   assert( pCrsr!=0 );
70864   rc = sqlite3BtreeLast(pCrsr, &res);
70865   pC->nullRow = (u8)res;
70866   pC->deferredMoveto = 0;
70867   pC->rowidIsValid = 0;
70868   pC->cacheStatus = CACHE_STALE;
70869   if( pOp->p2>0 && res ){
70870     pc = pOp->p2 - 1;
70871   }
70872   break;
70873 }
70874 
70875 
70876 /* Opcode: Sort P1 P2 * * *
70877 **
70878 ** This opcode does exactly the same thing as OP_Rewind except that
70879 ** it increments an undocumented global variable used for testing.
70880 **
70881 ** Sorting is accomplished by writing records into a sorting index,
70882 ** then rewinding that index and playing it back from beginning to
70883 ** end.  We use the OP_Sort opcode instead of OP_Rewind to do the
70884 ** rewinding so that the global variable will be incremented and
70885 ** regression tests can determine whether or not the optimizer is
70886 ** correctly optimizing out sorts.
70887 */
70888 case OP_SorterSort:    /* jump */
70889 case OP_Sort: {        /* jump */
70890 #ifdef SQLITE_TEST
70891   sqlite3_sort_count++;
70892   sqlite3_search_count--;
70893 #endif
70894   p->aCounter[SQLITE_STMTSTATUS_SORT]++;
70895   /* Fall through into OP_Rewind */
70896 }
70897 /* Opcode: Rewind P1 P2 * * *
70898 **
70899 ** The next use of the Rowid or Column or Next instruction for P1
70900 ** will refer to the first entry in the database table or index.
70901 ** If the table or index is empty and P2>0, then jump immediately to P2.
70902 ** If P2 is 0 or if the table or index is not empty, fall through
70903 ** to the following instruction.
70904 */
70905 case OP_Rewind: {        /* jump */
70906   VdbeCursor *pC;
70907   BtCursor *pCrsr;
70908   int res;
70909 
70910   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
70911   pC = p->apCsr[pOp->p1];
70912   assert( pC!=0 );
70913   assert( isSorter(pC)==(pOp->opcode==OP_SorterSort) );
70914   res = 1;
70915   if( isSorter(pC) ){
70916     rc = sqlite3VdbeSorterRewind(db, pC, &res);
70917   }else{
70918     pCrsr = pC->pCursor;
70919     assert( pCrsr );
70920     rc = sqlite3BtreeFirst(pCrsr, &res);
70921     pC->deferredMoveto = 0;
70922     pC->cacheStatus = CACHE_STALE;
70923     pC->rowidIsValid = 0;
70924   }
70925   pC->nullRow = (u8)res;
70926   assert( pOp->p2>0 && pOp->p2<p->nOp );
70927   if( res ){
70928     pc = pOp->p2 - 1;
70929   }
70930   break;
70931 }
70932 
70933 /* Opcode: Next P1 P2 * * P5
70934 **
70935 ** Advance cursor P1 so that it points to the next key/data pair in its
70936 ** table or index.  If there are no more key/value pairs then fall through
70937 ** to the following instruction.  But if the cursor advance was successful,
70938 ** jump immediately to P2.
70939 **
70940 ** The P1 cursor must be for a real table, not a pseudo-table.  P1 must have
70941 ** been opened prior to this opcode or the program will segfault.
70942 **
70943 ** P4 is always of type P4_ADVANCE. The function pointer points to
70944 ** sqlite3BtreeNext().
70945 **
70946 ** If P5 is positive and the jump is taken, then event counter
70947 ** number P5-1 in the prepared statement is incremented.
70948 **
70949 ** See also: Prev, NextIfOpen
70950 */
70951 /* Opcode: NextIfOpen P1 P2 * * P5
70952 **
70953 ** This opcode works just like OP_Next except that if cursor P1 is not
70954 ** open it behaves a no-op.
70955 */
70956 /* Opcode: Prev P1 P2 * * P5
70957 **
70958 ** Back up cursor P1 so that it points to the previous key/data pair in its
70959 ** table or index.  If there is no previous key/value pairs then fall through
70960 ** to the following instruction.  But if the cursor backup was successful,
70961 ** jump immediately to P2.
70962 **
70963 ** The P1 cursor must be for a real table, not a pseudo-table.  If P1 is
70964 ** not open then the behavior is undefined.
70965 **
70966 ** P4 is always of type P4_ADVANCE. The function pointer points to
70967 ** sqlite3BtreePrevious().
70968 **
70969 ** If P5 is positive and the jump is taken, then event counter
70970 ** number P5-1 in the prepared statement is incremented.
70971 */
70972 /* Opcode: PrevIfOpen P1 P2 * * P5
70973 **
70974 ** This opcode works just like OP_Prev except that if cursor P1 is not
70975 ** open it behaves a no-op.
70976 */
70977 case OP_SorterNext: {  /* jump */
70978   VdbeCursor *pC;
70979   int res;
70980 
70981   pC = p->apCsr[pOp->p1];
70982   assert( isSorter(pC) );
70983   rc = sqlite3VdbeSorterNext(db, pC, &res);
70984   goto next_tail;
70985 case OP_PrevIfOpen:    /* jump */
70986 case OP_NextIfOpen:    /* jump */
70987   if( p->apCsr[pOp->p1]==0 ) break;
70988   /* Fall through */
70989 case OP_Prev:          /* jump */
70990 case OP_Next:          /* jump */
70991   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
70992   assert( pOp->p5<ArraySize(p->aCounter) );
70993   pC = p->apCsr[pOp->p1];
70994   assert( pC!=0 );
70995   assert( pC->deferredMoveto==0 );
70996   assert( pC->pCursor );
70997   assert( pOp->opcode!=OP_Next || pOp->p4.xAdvance==sqlite3BtreeNext );
70998   assert( pOp->opcode!=OP_Prev || pOp->p4.xAdvance==sqlite3BtreePrevious );
70999   assert( pOp->opcode!=OP_NextIfOpen || pOp->p4.xAdvance==sqlite3BtreeNext );
71000   assert( pOp->opcode!=OP_PrevIfOpen || pOp->p4.xAdvance==sqlite3BtreePrevious);
71001   rc = pOp->p4.xAdvance(pC->pCursor, &res);
71002 next_tail:
71003   pC->cacheStatus = CACHE_STALE;
71004   if( res==0 ){
71005     pC->nullRow = 0;
71006     pc = pOp->p2 - 1;
71007     p->aCounter[pOp->p5]++;
71008 #ifdef SQLITE_TEST
71009     sqlite3_search_count++;
71010 #endif
71011   }else{
71012     pC->nullRow = 1;
71013   }
71014   pC->rowidIsValid = 0;
71015   goto check_for_interrupt;
71016 }
71017 
71018 /* Opcode: IdxInsert P1 P2 P3 * P5
71019 ** Synopsis: key=r[P2]
71020 **
71021 ** Register P2 holds an SQL index key made using the
71022 ** MakeRecord instructions.  This opcode writes that key
71023 ** into the index P1.  Data for the entry is nil.
71024 **
71025 ** P3 is a flag that provides a hint to the b-tree layer that this
71026 ** insert is likely to be an append.
71027 **
71028 ** This instruction only works for indices.  The equivalent instruction
71029 ** for tables is OP_Insert.
71030 */
71031 case OP_SorterInsert:       /* in2 */
71032 case OP_IdxInsert: {        /* in2 */
71033   VdbeCursor *pC;
71034   BtCursor *pCrsr;
71035   int nKey;
71036   const char *zKey;
71037 
71038   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
71039   pC = p->apCsr[pOp->p1];
71040   assert( pC!=0 );
71041   assert( isSorter(pC)==(pOp->opcode==OP_SorterInsert) );
71042   pIn2 = &aMem[pOp->p2];
71043   assert( pIn2->flags & MEM_Blob );
71044   pCrsr = pC->pCursor;
71045   if( pOp->p5 & OPFLAG_NCHANGE ) p->nChange++;
71046   assert( pCrsr!=0 );
71047   assert( pC->isTable==0 );
71048   rc = ExpandBlob(pIn2);
71049   if( rc==SQLITE_OK ){
71050     if( isSorter(pC) ){
71051       rc = sqlite3VdbeSorterWrite(db, pC, pIn2);
71052     }else{
71053       nKey = pIn2->n;
71054       zKey = pIn2->z;
71055       rc = sqlite3BtreeInsert(pCrsr, zKey, nKey, "", 0, 0, pOp->p3,
71056           ((pOp->p5 & OPFLAG_USESEEKRESULT) ? pC->seekResult : 0)
71057           );
71058       assert( pC->deferredMoveto==0 );
71059       pC->cacheStatus = CACHE_STALE;
71060     }
71061   }
71062   break;
71063 }
71064 
71065 /* Opcode: IdxDelete P1 P2 P3 * *
71066 ** Synopsis: key=r[P2@P3]
71067 **
71068 ** The content of P3 registers starting at register P2 form
71069 ** an unpacked index key. This opcode removes that entry from the
71070 ** index opened by cursor P1.
71071 */
71072 case OP_IdxDelete: {
71073   VdbeCursor *pC;
71074   BtCursor *pCrsr;
71075   int res;
71076   UnpackedRecord r;
71077 
71078   assert( pOp->p3>0 );
71079   assert( pOp->p2>0 && pOp->p2+pOp->p3<=(p->nMem-p->nCursor)+1 );
71080   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
71081   pC = p->apCsr[pOp->p1];
71082   assert( pC!=0 );
71083   pCrsr = pC->pCursor;
71084   assert( pCrsr!=0 );
71085   assert( pOp->p5==0 );
71086   r.pKeyInfo = pC->pKeyInfo;
71087   r.nField = (u16)pOp->p3;
71088   r.flags = UNPACKED_PREFIX_MATCH;
71089   r.aMem = &aMem[pOp->p2];
71090 #ifdef SQLITE_DEBUG
71091   { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
71092 #endif
71093   rc = sqlite3BtreeMovetoUnpacked(pCrsr, &r, 0, 0, &res);
71094   if( rc==SQLITE_OK && res==0 ){
71095     rc = sqlite3BtreeDelete(pCrsr);
71096   }
71097   assert( pC->deferredMoveto==0 );
71098   pC->cacheStatus = CACHE_STALE;
71099   break;
71100 }
71101 
71102 /* Opcode: IdxRowid P1 P2 * * *
71103 ** Synopsis: r[P2]=rowid
71104 **
71105 ** Write into register P2 an integer which is the last entry in the record at
71106 ** the end of the index key pointed to by cursor P1.  This integer should be
71107 ** the rowid of the table entry to which this index entry points.
71108 **
71109 ** See also: Rowid, MakeRecord.
71110 */
71111 case OP_IdxRowid: {              /* out2-prerelease */
71112   BtCursor *pCrsr;
71113   VdbeCursor *pC;
71114   i64 rowid;
71115 
71116   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
71117   pC = p->apCsr[pOp->p1];
71118   assert( pC!=0 );
71119   pCrsr = pC->pCursor;
71120   assert( pCrsr!=0 );
71121   pOut->flags = MEM_Null;
71122   rc = sqlite3VdbeCursorMoveto(pC);
71123   if( NEVER(rc) ) goto abort_due_to_error;
71124   assert( pC->deferredMoveto==0 );
71125   assert( pC->isTable==0 );
71126   if( !pC->nullRow ){
71127     rowid = 0;  /* Not needed.  Only used to silence a warning. */
71128     rc = sqlite3VdbeIdxRowid(db, pCrsr, &rowid);
71129     if( rc!=SQLITE_OK ){
71130       goto abort_due_to_error;
71131     }
71132     pOut->u.i = rowid;
71133     pOut->flags = MEM_Int;
71134   }
71135   break;
71136 }
71137 
71138 /* Opcode: IdxGE P1 P2 P3 P4 P5
71139 ** Synopsis: key=r[P3@P4]
71140 **
71141 ** The P4 register values beginning with P3 form an unpacked index
71142 ** key that omits the ROWID.  Compare this key value against the index
71143 ** that P1 is currently pointing to, ignoring the ROWID on the P1 index.
71144 **
71145 ** If the P1 index entry is greater than or equal to the key value
71146 ** then jump to P2.  Otherwise fall through to the next instruction.
71147 **
71148 ** If P5 is non-zero then the key value is increased by an epsilon
71149 ** prior to the comparison.  This make the opcode work like IdxGT except
71150 ** that if the key from register P3 is a prefix of the key in the cursor,
71151 ** the result is false whereas it would be true with IdxGT.
71152 */
71153 /* Opcode: IdxLT P1 P2 P3 P4 P5
71154 ** Synopsis: key=r[P3@P4]
71155 **
71156 ** The P4 register values beginning with P3 form an unpacked index
71157 ** key that omits the ROWID.  Compare this key value against the index
71158 ** that P1 is currently pointing to, ignoring the ROWID on the P1 index.
71159 **
71160 ** If the P1 index entry is less than the key value then jump to P2.
71161 ** Otherwise fall through to the next instruction.
71162 **
71163 ** If P5 is non-zero then the key value is increased by an epsilon prior
71164 ** to the comparison.  This makes the opcode work like IdxLE.
71165 */
71166 case OP_IdxLT:          /* jump */
71167 case OP_IdxGE: {        /* jump */
71168   VdbeCursor *pC;
71169   int res;
71170   UnpackedRecord r;
71171 
71172   assert( pOp->p1>=0 && pOp->p1<p->nCursor );
71173   pC = p->apCsr[pOp->p1];
71174   assert( pC!=0 );
71175   assert( pC->isOrdered );
71176   assert( pC->pCursor!=0);
71177   assert( pC->deferredMoveto==0 );
71178   assert( pOp->p5==0 || pOp->p5==1 );
71179   assert( pOp->p4type==P4_INT32 );
71180   r.pKeyInfo = pC->pKeyInfo;
71181   r.nField = (u16)pOp->p4.i;
71182   if( pOp->p5 ){
71183     r.flags = UNPACKED_INCRKEY | UNPACKED_PREFIX_MATCH;
71184   }else{
71185     r.flags = UNPACKED_PREFIX_MATCH;
71186   }
71187   r.aMem = &aMem[pOp->p3];
71188 #ifdef SQLITE_DEBUG
71189   { int i; for(i=0; i<r.nField; i++) assert( memIsValid(&r.aMem[i]) ); }
71190 #endif
71191   res = 0;  /* Not needed.  Only used to silence a warning. */
71192   rc = sqlite3VdbeIdxKeyCompare(pC, &r, &res);
71193   if( pOp->opcode==OP_IdxLT ){
71194     res = -res;
71195   }else{
71196     assert( pOp->opcode==OP_IdxGE );
71197     res++;
71198   }
71199   if( res>0 ){
71200     pc = pOp->p2 - 1 ;
71201   }
71202   break;
71203 }
71204 
71205 /* Opcode: Destroy P1 P2 P3 * *
71206 **
71207 ** Delete an entire database table or index whose root page in the database
71208 ** file is given by P1.
71209 **
71210 ** The table being destroyed is in the main database file if P3==0.  If
71211 ** P3==1 then the table to be clear is in the auxiliary database file
71212 ** that is used to store tables create using CREATE TEMPORARY TABLE.
71213 **
71214 ** If AUTOVACUUM is enabled then it is possible that another root page
71215 ** might be moved into the newly deleted root page in order to keep all
71216 ** root pages contiguous at the beginning of the database.  The former
71217 ** value of the root page that moved - its value before the move occurred -
71218 ** is stored in register P2.  If no page
71219 ** movement was required (because the table being dropped was already
71220 ** the last one in the database) then a zero is stored in register P2.
71221 ** If AUTOVACUUM is disabled then a zero is stored in register P2.
71222 **
71223 ** See also: Clear
71224 */
71225 case OP_Destroy: {     /* out2-prerelease */
71226   int iMoved;
71227   int iCnt;
71228   Vdbe *pVdbe;
71229   int iDb;
71230 
71231   assert( p->readOnly==0 );
71232 #ifndef SQLITE_OMIT_VIRTUALTABLE
71233   iCnt = 0;
71234   for(pVdbe=db->pVdbe; pVdbe; pVdbe = pVdbe->pNext){
71235     if( pVdbe->magic==VDBE_MAGIC_RUN && pVdbe->bIsReader
71236      && pVdbe->inVtabMethod<2 && pVdbe->pc>=0
71237     ){
71238       iCnt++;
71239     }
71240   }
71241 #else
71242   iCnt = db->nVdbeRead;
71243 #endif
71244   pOut->flags = MEM_Null;
71245   if( iCnt>1 ){
71246     rc = SQLITE_LOCKED;
71247     p->errorAction = OE_Abort;
71248   }else{
71249     iDb = pOp->p3;
71250     assert( iCnt==1 );
71251     assert( (p->btreeMask & (((yDbMask)1)<<iDb))!=0 );
71252     iMoved = 0;  /* Not needed.  Only to silence a warning. */
71253     rc = sqlite3BtreeDropTable(db->aDb[iDb].pBt, pOp->p1, &iMoved);
71254     pOut->flags = MEM_Int;
71255     pOut->u.i = iMoved;
71256 #ifndef SQLITE_OMIT_AUTOVACUUM
71257     if( rc==SQLITE_OK && iMoved!=0 ){
71258       sqlite3RootPageMoved(db, iDb, iMoved, pOp->p1);
71259       /* All OP_Destroy operations occur on the same btree */
71260       assert( resetSchemaOnFault==0 || resetSchemaOnFault==iDb+1 );
71261       resetSchemaOnFault = iDb+1;
71262     }
71263 #endif
71264   }
71265   break;
71266 }
71267 
71268 /* Opcode: Clear P1 P2 P3
71269 **
71270 ** Delete all contents of the database table or index whose root page
71271 ** in the database file is given by P1.  But, unlike Destroy, do not
71272 ** remove the table or index from the database file.
71273 **
71274 ** The table being clear is in the main database file if P2==0.  If
71275 ** P2==1 then the table to be clear is in the auxiliary database file
71276 ** that is used to store tables create using CREATE TEMPORARY TABLE.
71277 **
71278 ** If the P3 value is non-zero, then the table referred to must be an
71279 ** intkey table (an SQL table, not an index). In this case the row change
71280 ** count is incremented by the number of rows in the table being cleared.
71281 ** If P3 is greater than zero, then the value stored in register P3 is
71282 ** also incremented by the number of rows in the table being cleared.
71283 **
71284 ** See also: Destroy
71285 */
71286 case OP_Clear: {
71287   int nChange;
71288 
71289   nChange = 0;
71290   assert( p->readOnly==0 );
71291   assert( pOp->p1!=1 );
71292   assert( (p->btreeMask & (((yDbMask)1)<<pOp->p2))!=0 );
71293   rc = sqlite3BtreeClearTable(
71294       db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &nChange : 0)
71295   );
71296   if( pOp->p3 ){
71297     p->nChange += nChange;
71298     if( pOp->p3>0 ){
71299       assert( memIsValid(&aMem[pOp->p3]) );
71300       memAboutToChange(p, &aMem[pOp->p3]);
71301       aMem[pOp->p3].u.i += nChange;
71302     }
71303   }
71304   break;
71305 }
71306 
71307 /* Opcode: CreateTable P1 P2 * * *
71308 ** Synopsis: r[P2]=root iDb=P1
71309 **
71310 ** Allocate a new table in the main database file if P1==0 or in the
71311 ** auxiliary database file if P1==1 or in an attached database if
71312 ** P1>1.  Write the root page number of the new table into
71313 ** register P2
71314 **
71315 ** The difference between a table and an index is this:  A table must
71316 ** have a 4-byte integer key and can have arbitrary data.  An index
71317 ** has an arbitrary key but no data.
71318 **
71319 ** See also: CreateIndex
71320 */
71321 /* Opcode: CreateIndex P1 P2 * * *
71322 ** Synopsis: r[P2]=root iDb=P1
71323 **
71324 ** Allocate a new index in the main database file if P1==0 or in the
71325 ** auxiliary database file if P1==1 or in an attached database if
71326 ** P1>1.  Write the root page number of the new table into
71327 ** register P2.
71328 **
71329 ** See documentation on OP_CreateTable for additional information.
71330 */
71331 case OP_CreateIndex:            /* out2-prerelease */
71332 case OP_CreateTable: {          /* out2-prerelease */
71333   int pgno;
71334   int flags;
71335   Db *pDb;
71336 
71337   pgno = 0;
71338   assert( pOp->p1>=0 && pOp->p1<db->nDb );
71339   assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
71340   assert( p->readOnly==0 );
71341   pDb = &db->aDb[pOp->p1];
71342   assert( pDb->pBt!=0 );
71343   if( pOp->opcode==OP_CreateTable ){
71344     /* flags = BTREE_INTKEY; */
71345     flags = BTREE_INTKEY;
71346   }else{
71347     flags = BTREE_BLOBKEY;
71348   }
71349   rc = sqlite3BtreeCreateTable(pDb->pBt, &pgno, flags);
71350   pOut->u.i = pgno;
71351   break;
71352 }
71353 
71354 /* Opcode: ParseSchema P1 * * P4 *
71355 **
71356 ** Read and parse all entries from the SQLITE_MASTER table of database P1
71357 ** that match the WHERE clause P4.
71358 **
71359 ** This opcode invokes the parser to create a new virtual machine,
71360 ** then runs the new virtual machine.  It is thus a re-entrant opcode.
71361 */
71362 case OP_ParseSchema: {
71363   int iDb;
71364   const char *zMaster;
71365   char *zSql;
71366   InitData initData;
71367 
71368   /* Any prepared statement that invokes this opcode will hold mutexes
71369   ** on every btree.  This is a prerequisite for invoking
71370   ** sqlite3InitCallback().
71371   */
71372 #ifdef SQLITE_DEBUG
71373   for(iDb=0; iDb<db->nDb; iDb++){
71374     assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );
71375   }
71376 #endif
71377 
71378   iDb = pOp->p1;
71379   assert( iDb>=0 && iDb<db->nDb );
71380   assert( DbHasProperty(db, iDb, DB_SchemaLoaded) );
71381   /* Used to be a conditional */ {
71382     zMaster = SCHEMA_TABLE(iDb);
71383     initData.db = db;
71384     initData.iDb = pOp->p1;
71385     initData.pzErrMsg = &p->zErrMsg;
71386     zSql = sqlite3MPrintf(db,
71387        "SELECT name, rootpage, sql FROM '%q'.%s WHERE %s ORDER BY rowid",
71388        db->aDb[iDb].zName, zMaster, pOp->p4.z);
71389     if( zSql==0 ){
71390       rc = SQLITE_NOMEM;
71391     }else{
71392       assert( db->init.busy==0 );
71393       db->init.busy = 1;
71394       initData.rc = SQLITE_OK;
71395       assert( !db->mallocFailed );
71396       rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
71397       if( rc==SQLITE_OK ) rc = initData.rc;
71398       sqlite3DbFree(db, zSql);
71399       db->init.busy = 0;
71400     }
71401   }
71402   if( rc ) sqlite3ResetAllSchemasOfConnection(db);
71403   if( rc==SQLITE_NOMEM ){
71404     goto no_mem;
71405   }
71406   break;
71407 }
71408 
71409 #if !defined(SQLITE_OMIT_ANALYZE)
71410 /* Opcode: LoadAnalysis P1 * * * *
71411 **
71412 ** Read the sqlite_stat1 table for database P1 and load the content
71413 ** of that table into the internal index hash table.  This will cause
71414 ** the analysis to be used when preparing all subsequent queries.
71415 */
71416 case OP_LoadAnalysis: {
71417   assert( pOp->p1>=0 && pOp->p1<db->nDb );
71418   rc = sqlite3AnalysisLoad(db, pOp->p1);
71419   break;
71420 }
71421 #endif /* !defined(SQLITE_OMIT_ANALYZE) */
71422 
71423 /* Opcode: DropTable P1 * * P4 *
71424 **
71425 ** Remove the internal (in-memory) data structures that describe
71426 ** the table named P4 in database P1.  This is called after a table
71427 ** is dropped in order to keep the internal representation of the
71428 ** schema consistent with what is on disk.
71429 */
71430 case OP_DropTable: {
71431   sqlite3UnlinkAndDeleteTable(db, pOp->p1, pOp->p4.z);
71432   break;
71433 }
71434 
71435 /* Opcode: DropIndex P1 * * P4 *
71436 **
71437 ** Remove the internal (in-memory) data structures that describe
71438 ** the index named P4 in database P1.  This is called after an index
71439 ** is dropped in order to keep the internal representation of the
71440 ** schema consistent with what is on disk.
71441 */
71442 case OP_DropIndex: {
71443   sqlite3UnlinkAndDeleteIndex(db, pOp->p1, pOp->p4.z);
71444   break;
71445 }
71446 
71447 /* Opcode: DropTrigger P1 * * P4 *
71448 **
71449 ** Remove the internal (in-memory) data structures that describe
71450 ** the trigger named P4 in database P1.  This is called after a trigger
71451 ** is dropped in order to keep the internal representation of the
71452 ** schema consistent with what is on disk.
71453 */
71454 case OP_DropTrigger: {
71455   sqlite3UnlinkAndDeleteTrigger(db, pOp->p1, pOp->p4.z);
71456   break;
71457 }
71458 
71459 
71460 #ifndef SQLITE_OMIT_INTEGRITY_CHECK
71461 /* Opcode: IntegrityCk P1 P2 P3 * P5
71462 **
71463 ** Do an analysis of the currently open database.  Store in
71464 ** register P1 the text of an error message describing any problems.
71465 ** If no problems are found, store a NULL in register P1.
71466 **
71467 ** The register P3 contains the maximum number of allowed errors.
71468 ** At most reg(P3) errors will be reported.
71469 ** In other words, the analysis stops as soon as reg(P1) errors are
71470 ** seen.  Reg(P1) is updated with the number of errors remaining.
71471 **
71472 ** The root page numbers of all tables in the database are integer
71473 ** stored in reg(P1), reg(P1+1), reg(P1+2), ....  There are P2 tables
71474 ** total.
71475 **
71476 ** If P5 is not zero, the check is done on the auxiliary database
71477 ** file, not the main database file.
71478 **
71479 ** This opcode is used to implement the integrity_check pragma.
71480 */
71481 case OP_IntegrityCk: {
71482   int nRoot;      /* Number of tables to check.  (Number of root pages.) */
71483   int *aRoot;     /* Array of rootpage numbers for tables to be checked */
71484   int j;          /* Loop counter */
71485   int nErr;       /* Number of errors reported */
71486   char *z;        /* Text of the error report */
71487   Mem *pnErr;     /* Register keeping track of errors remaining */
71488 
71489   assert( p->bIsReader );
71490   nRoot = pOp->p2;
71491   assert( nRoot>0 );
71492   aRoot = sqlite3DbMallocRaw(db, sizeof(int)*(nRoot+1) );
71493   if( aRoot==0 ) goto no_mem;
71494   assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
71495   pnErr = &aMem[pOp->p3];
71496   assert( (pnErr->flags & MEM_Int)!=0 );
71497   assert( (pnErr->flags & (MEM_Str|MEM_Blob))==0 );
71498   pIn1 = &aMem[pOp->p1];
71499   for(j=0; j<nRoot; j++){
71500     aRoot[j] = (int)sqlite3VdbeIntValue(&pIn1[j]);
71501   }
71502   aRoot[j] = 0;
71503   assert( pOp->p5<db->nDb );
71504   assert( (p->btreeMask & (((yDbMask)1)<<pOp->p5))!=0 );
71505   z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p5].pBt, aRoot, nRoot,
71506                                  (int)pnErr->u.i, &nErr);
71507   sqlite3DbFree(db, aRoot);
71508   pnErr->u.i -= nErr;
71509   sqlite3VdbeMemSetNull(pIn1);
71510   if( nErr==0 ){
71511     assert( z==0 );
71512   }else if( z==0 ){
71513     goto no_mem;
71514   }else{
71515     sqlite3VdbeMemSetStr(pIn1, z, -1, SQLITE_UTF8, sqlite3_free);
71516   }
71517   UPDATE_MAX_BLOBSIZE(pIn1);
71518   sqlite3VdbeChangeEncoding(pIn1, encoding);
71519   break;
71520 }
71521 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
71522 
71523 /* Opcode: RowSetAdd P1 P2 * * *
71524 ** Synopsis:  rowset(P1)=r[P2]
71525 **
71526 ** Insert the integer value held by register P2 into a boolean index
71527 ** held in register P1.
71528 **
71529 ** An assertion fails if P2 is not an integer.
71530 */
71531 case OP_RowSetAdd: {       /* in1, in2 */
71532   pIn1 = &aMem[pOp->p1];
71533   pIn2 = &aMem[pOp->p2];
71534   assert( (pIn2->flags & MEM_Int)!=0 );
71535   if( (pIn1->flags & MEM_RowSet)==0 ){
71536     sqlite3VdbeMemSetRowSet(pIn1);
71537     if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem;
71538   }
71539   sqlite3RowSetInsert(pIn1->u.pRowSet, pIn2->u.i);
71540   break;
71541 }
71542 
71543 /* Opcode: RowSetRead P1 P2 P3 * *
71544 ** Synopsis:  r[P3]=rowset(P1)
71545 **
71546 ** Extract the smallest value from boolean index P1 and put that value into
71547 ** register P3.  Or, if boolean index P1 is initially empty, leave P3
71548 ** unchanged and jump to instruction P2.
71549 */
71550 case OP_RowSetRead: {       /* jump, in1, out3 */
71551   i64 val;
71552 
71553   pIn1 = &aMem[pOp->p1];
71554   if( (pIn1->flags & MEM_RowSet)==0
71555    || sqlite3RowSetNext(pIn1->u.pRowSet, &val)==0
71556   ){
71557     /* The boolean index is empty */
71558     sqlite3VdbeMemSetNull(pIn1);
71559     pc = pOp->p2 - 1;
71560   }else{
71561     /* A value was pulled from the index */
71562     sqlite3VdbeMemSetInt64(&aMem[pOp->p3], val);
71563   }
71564   goto check_for_interrupt;
71565 }
71566 
71567 /* Opcode: RowSetTest P1 P2 P3 P4
71568 ** Synopsis: if r[P3] in rowset(P1) goto P2
71569 **
71570 ** Register P3 is assumed to hold a 64-bit integer value. If register P1
71571 ** contains a RowSet object and that RowSet object contains
71572 ** the value held in P3, jump to register P2. Otherwise, insert the
71573 ** integer in P3 into the RowSet and continue on to the
71574 ** next opcode.
71575 **
71576 ** The RowSet object is optimized for the case where successive sets
71577 ** of integers, where each set contains no duplicates. Each set
71578 ** of values is identified by a unique P4 value. The first set
71579 ** must have P4==0, the final set P4=-1.  P4 must be either -1 or
71580 ** non-negative.  For non-negative values of P4 only the lower 4
71581 ** bits are significant.
71582 **
71583 ** This allows optimizations: (a) when P4==0 there is no need to test
71584 ** the rowset object for P3, as it is guaranteed not to contain it,
71585 ** (b) when P4==-1 there is no need to insert the value, as it will
71586 ** never be tested for, and (c) when a value that is part of set X is
71587 ** inserted, there is no need to search to see if the same value was
71588 ** previously inserted as part of set X (only if it was previously
71589 ** inserted as part of some other set).
71590 */
71591 case OP_RowSetTest: {                     /* jump, in1, in3 */
71592   int iSet;
71593   int exists;
71594 
71595   pIn1 = &aMem[pOp->p1];
71596   pIn3 = &aMem[pOp->p3];
71597   iSet = pOp->p4.i;
71598   assert( pIn3->flags&MEM_Int );
71599 
71600   /* If there is anything other than a rowset object in memory cell P1,
71601   ** delete it now and initialize P1 with an empty rowset
71602   */
71603   if( (pIn1->flags & MEM_RowSet)==0 ){
71604     sqlite3VdbeMemSetRowSet(pIn1);
71605     if( (pIn1->flags & MEM_RowSet)==0 ) goto no_mem;
71606   }
71607 
71608   assert( pOp->p4type==P4_INT32 );
71609   assert( iSet==-1 || iSet>=0 );
71610   if( iSet ){
71611     exists = sqlite3RowSetTest(pIn1->u.pRowSet,
71612                                (u8)(iSet>=0 ? iSet & 0xf : 0xff),
71613                                pIn3->u.i);
71614     if( exists ){
71615       pc = pOp->p2 - 1;
71616       break;
71617     }
71618   }
71619   if( iSet>=0 ){
71620     sqlite3RowSetInsert(pIn1->u.pRowSet, pIn3->u.i);
71621   }
71622   break;
71623 }
71624 
71625 
71626 #ifndef SQLITE_OMIT_TRIGGER
71627 
71628 /* Opcode: Program P1 P2 P3 P4 *
71629 **
71630 ** Execute the trigger program passed as P4 (type P4_SUBPROGRAM).
71631 **
71632 ** P1 contains the address of the memory cell that contains the first memory
71633 ** cell in an array of values used as arguments to the sub-program. P2
71634 ** contains the address to jump to if the sub-program throws an IGNORE
71635 ** exception using the RAISE() function. Register P3 contains the address
71636 ** of a memory cell in this (the parent) VM that is used to allocate the
71637 ** memory required by the sub-vdbe at runtime.
71638 **
71639 ** P4 is a pointer to the VM containing the trigger program.
71640 */
71641 case OP_Program: {        /* jump */
71642   int nMem;               /* Number of memory registers for sub-program */
71643   int nByte;              /* Bytes of runtime space required for sub-program */
71644   Mem *pRt;               /* Register to allocate runtime space */
71645   Mem *pMem;              /* Used to iterate through memory cells */
71646   Mem *pEnd;              /* Last memory cell in new array */
71647   VdbeFrame *pFrame;      /* New vdbe frame to execute in */
71648   SubProgram *pProgram;   /* Sub-program to execute */
71649   void *t;                /* Token identifying trigger */
71650 
71651   pProgram = pOp->p4.pProgram;
71652   pRt = &aMem[pOp->p3];
71653   assert( pProgram->nOp>0 );
71654 
71655   /* If the p5 flag is clear, then recursive invocation of triggers is
71656   ** disabled for backwards compatibility (p5 is set if this sub-program
71657   ** is really a trigger, not a foreign key action, and the flag set
71658   ** and cleared by the "PRAGMA recursive_triggers" command is clear).
71659   **
71660   ** It is recursive invocation of triggers, at the SQL level, that is
71661   ** disabled. In some cases a single trigger may generate more than one
71662   ** SubProgram (if the trigger may be executed with more than one different
71663   ** ON CONFLICT algorithm). SubProgram structures associated with a
71664   ** single trigger all have the same value for the SubProgram.token
71665   ** variable.  */
71666   if( pOp->p5 ){
71667     t = pProgram->token;
71668     for(pFrame=p->pFrame; pFrame && pFrame->token!=t; pFrame=pFrame->pParent);
71669     if( pFrame ) break;
71670   }
71671 
71672   if( p->nFrame>=db->aLimit[SQLITE_LIMIT_TRIGGER_DEPTH] ){
71673     rc = SQLITE_ERROR;
71674     sqlite3SetString(&p->zErrMsg, db, "too many levels of trigger recursion");
71675     break;
71676   }
71677 
71678   /* Register pRt is used to store the memory required to save the state
71679   ** of the current program, and the memory required at runtime to execute
71680   ** the trigger program. If this trigger has been fired before, then pRt
71681   ** is already allocated. Otherwise, it must be initialized.  */
71682   if( (pRt->flags&MEM_Frame)==0 ){
71683     /* SubProgram.nMem is set to the number of memory cells used by the
71684     ** program stored in SubProgram.aOp. As well as these, one memory
71685     ** cell is required for each cursor used by the program. Set local
71686     ** variable nMem (and later, VdbeFrame.nChildMem) to this value.
71687     */
71688     nMem = pProgram->nMem + pProgram->nCsr;
71689     nByte = ROUND8(sizeof(VdbeFrame))
71690               + nMem * sizeof(Mem)
71691               + pProgram->nCsr * sizeof(VdbeCursor *)
71692               + pProgram->nOnce * sizeof(u8);
71693     pFrame = sqlite3DbMallocZero(db, nByte);
71694     if( !pFrame ){
71695       goto no_mem;
71696     }
71697     sqlite3VdbeMemRelease(pRt);
71698     pRt->flags = MEM_Frame;
71699     pRt->u.pFrame = pFrame;
71700 
71701     pFrame->v = p;
71702     pFrame->nChildMem = nMem;
71703     pFrame->nChildCsr = pProgram->nCsr;
71704     pFrame->pc = pc;
71705     pFrame->aMem = p->aMem;
71706     pFrame->nMem = p->nMem;
71707     pFrame->apCsr = p->apCsr;
71708     pFrame->nCursor = p->nCursor;
71709     pFrame->aOp = p->aOp;
71710     pFrame->nOp = p->nOp;
71711     pFrame->token = pProgram->token;
71712     pFrame->aOnceFlag = p->aOnceFlag;
71713     pFrame->nOnceFlag = p->nOnceFlag;
71714 
71715     pEnd = &VdbeFrameMem(pFrame)[pFrame->nChildMem];
71716     for(pMem=VdbeFrameMem(pFrame); pMem!=pEnd; pMem++){
71717       pMem->flags = MEM_Invalid;
71718       pMem->db = db;
71719     }
71720   }else{
71721     pFrame = pRt->u.pFrame;
71722     assert( pProgram->nMem+pProgram->nCsr==pFrame->nChildMem );
71723     assert( pProgram->nCsr==pFrame->nChildCsr );
71724     assert( pc==pFrame->pc );
71725   }
71726 
71727   p->nFrame++;
71728   pFrame->pParent = p->pFrame;
71729   pFrame->lastRowid = lastRowid;
71730   pFrame->nChange = p->nChange;
71731   p->nChange = 0;
71732   p->pFrame = pFrame;
71733   p->aMem = aMem = &VdbeFrameMem(pFrame)[-1];
71734   p->nMem = pFrame->nChildMem;
71735   p->nCursor = (u16)pFrame->nChildCsr;
71736   p->apCsr = (VdbeCursor **)&aMem[p->nMem+1];
71737   p->aOp = aOp = pProgram->aOp;
71738   p->nOp = pProgram->nOp;
71739   p->aOnceFlag = (u8 *)&p->apCsr[p->nCursor];
71740   p->nOnceFlag = pProgram->nOnce;
71741   pc = -1;
71742   memset(p->aOnceFlag, 0, p->nOnceFlag);
71743 
71744   break;
71745 }
71746 
71747 /* Opcode: Param P1 P2 * * *
71748 **
71749 ** This opcode is only ever present in sub-programs called via the
71750 ** OP_Program instruction. Copy a value currently stored in a memory
71751 ** cell of the calling (parent) frame to cell P2 in the current frames
71752 ** address space. This is used by trigger programs to access the new.*
71753 ** and old.* values.
71754 **
71755 ** The address of the cell in the parent frame is determined by adding
71756 ** the value of the P1 argument to the value of the P1 argument to the
71757 ** calling OP_Program instruction.
71758 */
71759 case OP_Param: {           /* out2-prerelease */
71760   VdbeFrame *pFrame;
71761   Mem *pIn;
71762   pFrame = p->pFrame;
71763   pIn = &pFrame->aMem[pOp->p1 + pFrame->aOp[pFrame->pc].p1];
71764   sqlite3VdbeMemShallowCopy(pOut, pIn, MEM_Ephem);
71765   break;
71766 }
71767 
71768 #endif /* #ifndef SQLITE_OMIT_TRIGGER */
71769 
71770 #ifndef SQLITE_OMIT_FOREIGN_KEY
71771 /* Opcode: FkCounter P1 P2 * * *
71772 ** Synopsis: fkctr[P1]+=P2
71773 **
71774 ** Increment a "constraint counter" by P2 (P2 may be negative or positive).
71775 ** If P1 is non-zero, the database constraint counter is incremented
71776 ** (deferred foreign key constraints). Otherwise, if P1 is zero, the
71777 ** statement counter is incremented (immediate foreign key constraints).
71778 */
71779 case OP_FkCounter: {
71780   if( db->flags & SQLITE_DeferFKs ){
71781     db->nDeferredImmCons += pOp->p2;
71782   }else if( pOp->p1 ){
71783     db->nDeferredCons += pOp->p2;
71784   }else{
71785     p->nFkConstraint += pOp->p2;
71786   }
71787   break;
71788 }
71789 
71790 /* Opcode: FkIfZero P1 P2 * * *
71791 ** Synopsis: if fkctr[P1]==0 goto P2
71792 **
71793 ** This opcode tests if a foreign key constraint-counter is currently zero.
71794 ** If so, jump to instruction P2. Otherwise, fall through to the next
71795 ** instruction.
71796 **
71797 ** If P1 is non-zero, then the jump is taken if the database constraint-counter
71798 ** is zero (the one that counts deferred constraint violations). If P1 is
71799 ** zero, the jump is taken if the statement constraint-counter is zero
71800 ** (immediate foreign key constraint violations).
71801 */
71802 case OP_FkIfZero: {         /* jump */
71803   if( pOp->p1 ){
71804     if( db->nDeferredCons==0 && db->nDeferredImmCons==0 ) pc = pOp->p2-1;
71805   }else{
71806     if( p->nFkConstraint==0 && db->nDeferredImmCons==0 ) pc = pOp->p2-1;
71807   }
71808   break;
71809 }
71810 #endif /* #ifndef SQLITE_OMIT_FOREIGN_KEY */
71811 
71812 #ifndef SQLITE_OMIT_AUTOINCREMENT
71813 /* Opcode: MemMax P1 P2 * * *
71814 ** Synopsis: r[P1]=max(r[P1],r[P2])
71815 **
71816 ** P1 is a register in the root frame of this VM (the root frame is
71817 ** different from the current frame if this instruction is being executed
71818 ** within a sub-program). Set the value of register P1 to the maximum of
71819 ** its current value and the value in register P2.
71820 **
71821 ** This instruction throws an error if the memory cell is not initially
71822 ** an integer.
71823 */
71824 case OP_MemMax: {        /* in2 */
71825   VdbeFrame *pFrame;
71826   if( p->pFrame ){
71827     for(pFrame=p->pFrame; pFrame->pParent; pFrame=pFrame->pParent);
71828     pIn1 = &pFrame->aMem[pOp->p1];
71829   }else{
71830     pIn1 = &aMem[pOp->p1];
71831   }
71832   assert( memIsValid(pIn1) );
71833   sqlite3VdbeMemIntegerify(pIn1);
71834   pIn2 = &aMem[pOp->p2];
71835   sqlite3VdbeMemIntegerify(pIn2);
71836   if( pIn1->u.i<pIn2->u.i){
71837     pIn1->u.i = pIn2->u.i;
71838   }
71839   break;
71840 }
71841 #endif /* SQLITE_OMIT_AUTOINCREMENT */
71842 
71843 /* Opcode: IfPos P1 P2 * * *
71844 ** Synopsis: if r[P1]>0 goto P2
71845 **
71846 ** If the value of register P1 is 1 or greater, jump to P2.
71847 **
71848 ** It is illegal to use this instruction on a register that does
71849 ** not contain an integer.  An assertion fault will result if you try.
71850 */
71851 case OP_IfPos: {        /* jump, in1 */
71852   pIn1 = &aMem[pOp->p1];
71853   assert( pIn1->flags&MEM_Int );
71854   if( pIn1->u.i>0 ){
71855      pc = pOp->p2 - 1;
71856   }
71857   break;
71858 }
71859 
71860 /* Opcode: IfNeg P1 P2 * * *
71861 ** Synopsis: if r[P1]<0 goto P2
71862 **
71863 ** If the value of register P1 is less than zero, jump to P2.
71864 **
71865 ** It is illegal to use this instruction on a register that does
71866 ** not contain an integer.  An assertion fault will result if you try.
71867 */
71868 case OP_IfNeg: {        /* jump, in1 */
71869   pIn1 = &aMem[pOp->p1];
71870   assert( pIn1->flags&MEM_Int );
71871   if( pIn1->u.i<0 ){
71872      pc = pOp->p2 - 1;
71873   }
71874   break;
71875 }
71876 
71877 /* Opcode: IfZero P1 P2 P3 * *
71878 ** Synopsis: r[P1]+=P3, if r[P1]==0 goto P2
71879 **
71880 ** The register P1 must contain an integer.  Add literal P3 to the
71881 ** value in register P1.  If the result is exactly 0, jump to P2.
71882 **
71883 ** It is illegal to use this instruction on a register that does
71884 ** not contain an integer.  An assertion fault will result if you try.
71885 */
71886 case OP_IfZero: {        /* jump, in1 */
71887   pIn1 = &aMem[pOp->p1];
71888   assert( pIn1->flags&MEM_Int );
71889   pIn1->u.i += pOp->p3;
71890   if( pIn1->u.i==0 ){
71891      pc = pOp->p2 - 1;
71892   }
71893   break;
71894 }
71895 
71896 /* Opcode: AggStep * P2 P3 P4 P5
71897 ** Synopsis: accum=r[P3] step(r[P2@P5])
71898 **
71899 ** Execute the step function for an aggregate.  The
71900 ** function has P5 arguments.   P4 is a pointer to the FuncDef
71901 ** structure that specifies the function.  Use register
71902 ** P3 as the accumulator.
71903 **
71904 ** The P5 arguments are taken from register P2 and its
71905 ** successors.
71906 */
71907 case OP_AggStep: {
71908   int n;
71909   int i;
71910   Mem *pMem;
71911   Mem *pRec;
71912   sqlite3_context ctx;
71913   sqlite3_value **apVal;
71914 
71915   n = pOp->p5;
71916   assert( n>=0 );
71917   pRec = &aMem[pOp->p2];
71918   apVal = p->apArg;
71919   assert( apVal || n==0 );
71920   for(i=0; i<n; i++, pRec++){
71921     assert( memIsValid(pRec) );
71922     apVal[i] = pRec;
71923     memAboutToChange(p, pRec);
71924     sqlite3VdbeMemStoreType(pRec);
71925   }
71926   ctx.pFunc = pOp->p4.pFunc;
71927   assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
71928   ctx.pMem = pMem = &aMem[pOp->p3];
71929   pMem->n++;
71930   ctx.s.flags = MEM_Null;
71931   ctx.s.z = 0;
71932   ctx.s.zMalloc = 0;
71933   ctx.s.xDel = 0;
71934   ctx.s.db = db;
71935   ctx.isError = 0;
71936   ctx.pColl = 0;
71937   ctx.skipFlag = 0;
71938   if( ctx.pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL ){
71939     assert( pOp>p->aOp );
71940     assert( pOp[-1].p4type==P4_COLLSEQ );
71941     assert( pOp[-1].opcode==OP_CollSeq );
71942     ctx.pColl = pOp[-1].p4.pColl;
71943   }
71944   (ctx.pFunc->xStep)(&ctx, n, apVal); /* IMP: R-24505-23230 */
71945   if( ctx.isError ){
71946     sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&ctx.s));
71947     rc = ctx.isError;
71948   }
71949   if( ctx.skipFlag ){
71950     assert( pOp[-1].opcode==OP_CollSeq );
71951     i = pOp[-1].p1;
71952     if( i ) sqlite3VdbeMemSetInt64(&aMem[i], 1);
71953   }
71954 
71955   sqlite3VdbeMemRelease(&ctx.s);
71956 
71957   break;
71958 }
71959 
71960 /* Opcode: AggFinal P1 P2 * P4 *
71961 ** Synopsis: accum=r[P1] N=P2
71962 **
71963 ** Execute the finalizer function for an aggregate.  P1 is
71964 ** the memory location that is the accumulator for the aggregate.
71965 **
71966 ** P2 is the number of arguments that the step function takes and
71967 ** P4 is a pointer to the FuncDef for this function.  The P2
71968 ** argument is not used by this opcode.  It is only there to disambiguate
71969 ** functions that can take varying numbers of arguments.  The
71970 ** P4 argument is only needed for the degenerate case where
71971 ** the step function was not previously called.
71972 */
71973 case OP_AggFinal: {
71974   Mem *pMem;
71975   assert( pOp->p1>0 && pOp->p1<=(p->nMem-p->nCursor) );
71976   pMem = &aMem[pOp->p1];
71977   assert( (pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
71978   rc = sqlite3VdbeMemFinalize(pMem, pOp->p4.pFunc);
71979   if( rc ){
71980     sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(pMem));
71981   }
71982   sqlite3VdbeChangeEncoding(pMem, encoding);
71983   UPDATE_MAX_BLOBSIZE(pMem);
71984   if( sqlite3VdbeMemTooBig(pMem) ){
71985     goto too_big;
71986   }
71987   break;
71988 }
71989 
71990 #ifndef SQLITE_OMIT_WAL
71991 /* Opcode: Checkpoint P1 P2 P3 * *
71992 **
71993 ** Checkpoint database P1. This is a no-op if P1 is not currently in
71994 ** WAL mode. Parameter P2 is one of SQLITE_CHECKPOINT_PASSIVE, FULL
71995 ** or RESTART.  Write 1 or 0 into mem[P3] if the checkpoint returns
71996 ** SQLITE_BUSY or not, respectively.  Write the number of pages in the
71997 ** WAL after the checkpoint into mem[P3+1] and the number of pages
71998 ** in the WAL that have been checkpointed after the checkpoint
71999 ** completes into mem[P3+2].  However on an error, mem[P3+1] and
72000 ** mem[P3+2] are initialized to -1.
72001 */
72002 case OP_Checkpoint: {
72003   int i;                          /* Loop counter */
72004   int aRes[3];                    /* Results */
72005   Mem *pMem;                      /* Write results here */
72006 
72007   assert( p->readOnly==0 );
72008   aRes[0] = 0;
72009   aRes[1] = aRes[2] = -1;
72010   assert( pOp->p2==SQLITE_CHECKPOINT_PASSIVE
72011        || pOp->p2==SQLITE_CHECKPOINT_FULL
72012        || pOp->p2==SQLITE_CHECKPOINT_RESTART
72013   );
72014   rc = sqlite3Checkpoint(db, pOp->p1, pOp->p2, &aRes[1], &aRes[2]);
72015   if( rc==SQLITE_BUSY ){
72016     rc = SQLITE_OK;
72017     aRes[0] = 1;
72018   }
72019   for(i=0, pMem = &aMem[pOp->p3]; i<3; i++, pMem++){
72020     sqlite3VdbeMemSetInt64(pMem, (i64)aRes[i]);
72021   }
72022   break;
72023 };
72024 #endif
72025 
72026 #ifndef SQLITE_OMIT_PRAGMA
72027 /* Opcode: JournalMode P1 P2 P3 * P5
72028 **
72029 ** Change the journal mode of database P1 to P3. P3 must be one of the
72030 ** PAGER_JOURNALMODE_XXX values. If changing between the various rollback
72031 ** modes (delete, truncate, persist, off and memory), this is a simple
72032 ** operation. No IO is required.
72033 **
72034 ** If changing into or out of WAL mode the procedure is more complicated.
72035 **
72036 ** Write a string containing the final journal-mode to register P2.
72037 */
72038 case OP_JournalMode: {    /* out2-prerelease */
72039   Btree *pBt;                     /* Btree to change journal mode of */
72040   Pager *pPager;                  /* Pager associated with pBt */
72041   int eNew;                       /* New journal mode */
72042   int eOld;                       /* The old journal mode */
72043 #ifndef SQLITE_OMIT_WAL
72044   const char *zFilename;          /* Name of database file for pPager */
72045 #endif
72046 
72047   eNew = pOp->p3;
72048   assert( eNew==PAGER_JOURNALMODE_DELETE
72049        || eNew==PAGER_JOURNALMODE_TRUNCATE
72050        || eNew==PAGER_JOURNALMODE_PERSIST
72051        || eNew==PAGER_JOURNALMODE_OFF
72052        || eNew==PAGER_JOURNALMODE_MEMORY
72053        || eNew==PAGER_JOURNALMODE_WAL
72054        || eNew==PAGER_JOURNALMODE_QUERY
72055   );
72056   assert( pOp->p1>=0 && pOp->p1<db->nDb );
72057   assert( p->readOnly==0 );
72058 
72059   pBt = db->aDb[pOp->p1].pBt;
72060   pPager = sqlite3BtreePager(pBt);
72061   eOld = sqlite3PagerGetJournalMode(pPager);
72062   if( eNew==PAGER_JOURNALMODE_QUERY ) eNew = eOld;
72063   if( !sqlite3PagerOkToChangeJournalMode(pPager) ) eNew = eOld;
72064 
72065 #ifndef SQLITE_OMIT_WAL
72066   zFilename = sqlite3PagerFilename(pPager, 1);
72067 
72068   /* Do not allow a transition to journal_mode=WAL for a database
72069   ** in temporary storage or if the VFS does not support shared memory
72070   */
72071   if( eNew==PAGER_JOURNALMODE_WAL
72072    && (sqlite3Strlen30(zFilename)==0           /* Temp file */
72073        || !sqlite3PagerWalSupported(pPager))   /* No shared-memory support */
72074   ){
72075     eNew = eOld;
72076   }
72077 
72078   if( (eNew!=eOld)
72079    && (eOld==PAGER_JOURNALMODE_WAL || eNew==PAGER_JOURNALMODE_WAL)
72080   ){
72081     if( !db->autoCommit || db->nVdbeRead>1 ){
72082       rc = SQLITE_ERROR;
72083       sqlite3SetString(&p->zErrMsg, db,
72084           "cannot change %s wal mode from within a transaction",
72085           (eNew==PAGER_JOURNALMODE_WAL ? "into" : "out of")
72086       );
72087       break;
72088     }else{
72089 
72090       if( eOld==PAGER_JOURNALMODE_WAL ){
72091         /* If leaving WAL mode, close the log file. If successful, the call
72092         ** to PagerCloseWal() checkpoints and deletes the write-ahead-log
72093         ** file. An EXCLUSIVE lock may still be held on the database file
72094         ** after a successful return.
72095         */
72096         rc = sqlite3PagerCloseWal(pPager);
72097         if( rc==SQLITE_OK ){
72098           sqlite3PagerSetJournalMode(pPager, eNew);
72099         }
72100       }else if( eOld==PAGER_JOURNALMODE_MEMORY ){
72101         /* Cannot transition directly from MEMORY to WAL.  Use mode OFF
72102         ** as an intermediate */
72103         sqlite3PagerSetJournalMode(pPager, PAGER_JOURNALMODE_OFF);
72104       }
72105 
72106       /* Open a transaction on the database file. Regardless of the journal
72107       ** mode, this transaction always uses a rollback journal.
72108       */
72109       assert( sqlite3BtreeIsInTrans(pBt)==0 );
72110       if( rc==SQLITE_OK ){
72111         rc = sqlite3BtreeSetVersion(pBt, (eNew==PAGER_JOURNALMODE_WAL ? 2 : 1));
72112       }
72113     }
72114   }
72115 #endif /* ifndef SQLITE_OMIT_WAL */
72116 
72117   if( rc ){
72118     eNew = eOld;
72119   }
72120   eNew = sqlite3PagerSetJournalMode(pPager, eNew);
72121 
72122   pOut = &aMem[pOp->p2];
72123   pOut->flags = MEM_Str|MEM_Static|MEM_Term;
72124   pOut->z = (char *)sqlite3JournalModename(eNew);
72125   pOut->n = sqlite3Strlen30(pOut->z);
72126   pOut->enc = SQLITE_UTF8;
72127   sqlite3VdbeChangeEncoding(pOut, encoding);
72128   break;
72129 };
72130 #endif /* SQLITE_OMIT_PRAGMA */
72131 
72132 #if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH)
72133 /* Opcode: Vacuum * * * * *
72134 **
72135 ** Vacuum the entire database.  This opcode will cause other virtual
72136 ** machines to be created and run.  It may not be called from within
72137 ** a transaction.
72138 */
72139 case OP_Vacuum: {
72140   assert( p->readOnly==0 );
72141   rc = sqlite3RunVacuum(&p->zErrMsg, db);
72142   break;
72143 }
72144 #endif
72145 
72146 #if !defined(SQLITE_OMIT_AUTOVACUUM)
72147 /* Opcode: IncrVacuum P1 P2 * * *
72148 **
72149 ** Perform a single step of the incremental vacuum procedure on
72150 ** the P1 database. If the vacuum has finished, jump to instruction
72151 ** P2. Otherwise, fall through to the next instruction.
72152 */
72153 case OP_IncrVacuum: {        /* jump */
72154   Btree *pBt;
72155 
72156   assert( pOp->p1>=0 && pOp->p1<db->nDb );
72157   assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
72158   assert( p->readOnly==0 );
72159   pBt = db->aDb[pOp->p1].pBt;
72160   rc = sqlite3BtreeIncrVacuum(pBt);
72161   if( rc==SQLITE_DONE ){
72162     pc = pOp->p2 - 1;
72163     rc = SQLITE_OK;
72164   }
72165   break;
72166 }
72167 #endif
72168 
72169 /* Opcode: Expire P1 * * * *
72170 **
72171 ** Cause precompiled statements to become expired. An expired statement
72172 ** fails with an error code of SQLITE_SCHEMA if it is ever executed
72173 ** (via sqlite3_step()).
72174 **
72175 ** If P1 is 0, then all SQL statements become expired. If P1 is non-zero,
72176 ** then only the currently executing statement is affected.
72177 */
72178 case OP_Expire: {
72179   if( !pOp->p1 ){
72180     sqlite3ExpirePreparedStatements(db);
72181   }else{
72182     p->expired = 1;
72183   }
72184   break;
72185 }
72186 
72187 #ifndef SQLITE_OMIT_SHARED_CACHE
72188 /* Opcode: TableLock P1 P2 P3 P4 *
72189 ** Synopsis: iDb=P1 root=P2 write=P3
72190 **
72191 ** Obtain a lock on a particular table. This instruction is only used when
72192 ** the shared-cache feature is enabled.
72193 **
72194 ** P1 is the index of the database in sqlite3.aDb[] of the database
72195 ** on which the lock is acquired.  A readlock is obtained if P3==0 or
72196 ** a write lock if P3==1.
72197 **
72198 ** P2 contains the root-page of the table to lock.
72199 **
72200 ** P4 contains a pointer to the name of the table being locked. This is only
72201 ** used to generate an error message if the lock cannot be obtained.
72202 */
72203 case OP_TableLock: {
72204   u8 isWriteLock = (u8)pOp->p3;
72205   if( isWriteLock || 0==(db->flags&SQLITE_ReadUncommitted) ){
72206     int p1 = pOp->p1;
72207     assert( p1>=0 && p1<db->nDb );
72208     assert( (p->btreeMask & (((yDbMask)1)<<p1))!=0 );
72209     assert( isWriteLock==0 || isWriteLock==1 );
72210     rc = sqlite3BtreeLockTable(db->aDb[p1].pBt, pOp->p2, isWriteLock);
72211     if( (rc&0xFF)==SQLITE_LOCKED ){
72212       const char *z = pOp->p4.z;
72213       sqlite3SetString(&p->zErrMsg, db, "database table is locked: %s", z);
72214     }
72215   }
72216   break;
72217 }
72218 #endif /* SQLITE_OMIT_SHARED_CACHE */
72219 
72220 #ifndef SQLITE_OMIT_VIRTUALTABLE
72221 /* Opcode: VBegin * * * P4 *
72222 **
72223 ** P4 may be a pointer to an sqlite3_vtab structure. If so, call the
72224 ** xBegin method for that table.
72225 **
72226 ** Also, whether or not P4 is set, check that this is not being called from
72227 ** within a callback to a virtual table xSync() method. If it is, the error
72228 ** code will be set to SQLITE_LOCKED.
72229 */
72230 case OP_VBegin: {
72231   VTable *pVTab;
72232   pVTab = pOp->p4.pVtab;
72233   rc = sqlite3VtabBegin(db, pVTab);
72234   if( pVTab ) sqlite3VtabImportErrmsg(p, pVTab->pVtab);
72235   break;
72236 }
72237 #endif /* SQLITE_OMIT_VIRTUALTABLE */
72238 
72239 #ifndef SQLITE_OMIT_VIRTUALTABLE
72240 /* Opcode: VCreate P1 * * P4 *
72241 **
72242 ** P4 is the name of a virtual table in database P1. Call the xCreate method
72243 ** for that table.
72244 */
72245 case OP_VCreate: {
72246   rc = sqlite3VtabCallCreate(db, pOp->p1, pOp->p4.z, &p->zErrMsg);
72247   break;
72248 }
72249 #endif /* SQLITE_OMIT_VIRTUALTABLE */
72250 
72251 #ifndef SQLITE_OMIT_VIRTUALTABLE
72252 /* Opcode: VDestroy P1 * * P4 *
72253 **
72254 ** P4 is the name of a virtual table in database P1.  Call the xDestroy method
72255 ** of that table.
72256 */
72257 case OP_VDestroy: {
72258   p->inVtabMethod = 2;
72259   rc = sqlite3VtabCallDestroy(db, pOp->p1, pOp->p4.z);
72260   p->inVtabMethod = 0;
72261   break;
72262 }
72263 #endif /* SQLITE_OMIT_VIRTUALTABLE */
72264 
72265 #ifndef SQLITE_OMIT_VIRTUALTABLE
72266 /* Opcode: VOpen P1 * * P4 *
72267 **
72268 ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
72269 ** P1 is a cursor number.  This opcode opens a cursor to the virtual
72270 ** table and stores that cursor in P1.
72271 */
72272 case OP_VOpen: {
72273   VdbeCursor *pCur;
72274   sqlite3_vtab_cursor *pVtabCursor;
72275   sqlite3_vtab *pVtab;
72276   sqlite3_module *pModule;
72277 
72278   assert( p->bIsReader );
72279   pCur = 0;
72280   pVtabCursor = 0;
72281   pVtab = pOp->p4.pVtab->pVtab;
72282   pModule = (sqlite3_module *)pVtab->pModule;
72283   assert(pVtab && pModule);
72284   rc = pModule->xOpen(pVtab, &pVtabCursor);
72285   sqlite3VtabImportErrmsg(p, pVtab);
72286   if( SQLITE_OK==rc ){
72287     /* Initialize sqlite3_vtab_cursor base class */
72288     pVtabCursor->pVtab = pVtab;
72289 
72290     /* Initialize vdbe cursor object */
72291     pCur = allocateCursor(p, pOp->p1, 0, -1, 0);
72292     if( pCur ){
72293       pCur->pVtabCursor = pVtabCursor;
72294     }else{
72295       db->mallocFailed = 1;
72296       pModule->xClose(pVtabCursor);
72297     }
72298   }
72299   break;
72300 }
72301 #endif /* SQLITE_OMIT_VIRTUALTABLE */
72302 
72303 #ifndef SQLITE_OMIT_VIRTUALTABLE
72304 /* Opcode: VFilter P1 P2 P3 P4 *
72305 ** Synopsis: iPlan=r[P3] zPlan='P4'
72306 **
72307 ** P1 is a cursor opened using VOpen.  P2 is an address to jump to if
72308 ** the filtered result set is empty.
72309 **
72310 ** P4 is either NULL or a string that was generated by the xBestIndex
72311 ** method of the module.  The interpretation of the P4 string is left
72312 ** to the module implementation.
72313 **
72314 ** This opcode invokes the xFilter method on the virtual table specified
72315 ** by P1.  The integer query plan parameter to xFilter is stored in register
72316 ** P3. Register P3+1 stores the argc parameter to be passed to the
72317 ** xFilter method. Registers P3+2..P3+1+argc are the argc
72318 ** additional parameters which are passed to
72319 ** xFilter as argv. Register P3+2 becomes argv[0] when passed to xFilter.
72320 **
72321 ** A jump is made to P2 if the result set after filtering would be empty.
72322 */
72323 case OP_VFilter: {   /* jump */
72324   int nArg;
72325   int iQuery;
72326   const sqlite3_module *pModule;
72327   Mem *pQuery;
72328   Mem *pArgc;
72329   sqlite3_vtab_cursor *pVtabCursor;
72330   sqlite3_vtab *pVtab;
72331   VdbeCursor *pCur;
72332   int res;
72333   int i;
72334   Mem **apArg;
72335 
72336   pQuery = &aMem[pOp->p3];
72337   pArgc = &pQuery[1];
72338   pCur = p->apCsr[pOp->p1];
72339   assert( memIsValid(pQuery) );
72340   REGISTER_TRACE(pOp->p3, pQuery);
72341   assert( pCur->pVtabCursor );
72342   pVtabCursor = pCur->pVtabCursor;
72343   pVtab = pVtabCursor->pVtab;
72344   pModule = pVtab->pModule;
72345 
72346   /* Grab the index number and argc parameters */
72347   assert( (pQuery->flags&MEM_Int)!=0 && pArgc->flags==MEM_Int );
72348   nArg = (int)pArgc->u.i;
72349   iQuery = (int)pQuery->u.i;
72350 
72351   /* Invoke the xFilter method */
72352   {
72353     res = 0;
72354     apArg = p->apArg;
72355     for(i = 0; i<nArg; i++){
72356       apArg[i] = &pArgc[i+1];
72357       sqlite3VdbeMemStoreType(apArg[i]);
72358     }
72359 
72360     p->inVtabMethod = 1;
72361     rc = pModule->xFilter(pVtabCursor, iQuery, pOp->p4.z, nArg, apArg);
72362     p->inVtabMethod = 0;
72363     sqlite3VtabImportErrmsg(p, pVtab);
72364     if( rc==SQLITE_OK ){
72365       res = pModule->xEof(pVtabCursor);
72366     }
72367 
72368     if( res ){
72369       pc = pOp->p2 - 1;
72370     }
72371   }
72372   pCur->nullRow = 0;
72373 
72374   break;
72375 }
72376 #endif /* SQLITE_OMIT_VIRTUALTABLE */
72377 
72378 #ifndef SQLITE_OMIT_VIRTUALTABLE
72379 /* Opcode: VColumn P1 P2 P3 * *
72380 ** Synopsis: r[P3]=vcolumn(P2)
72381 **
72382 ** Store the value of the P2-th column of
72383 ** the row of the virtual-table that the
72384 ** P1 cursor is pointing to into register P3.
72385 */
72386 case OP_VColumn: {
72387   sqlite3_vtab *pVtab;
72388   const sqlite3_module *pModule;
72389   Mem *pDest;
72390   sqlite3_context sContext;
72391 
72392   VdbeCursor *pCur = p->apCsr[pOp->p1];
72393   assert( pCur->pVtabCursor );
72394   assert( pOp->p3>0 && pOp->p3<=(p->nMem-p->nCursor) );
72395   pDest = &aMem[pOp->p3];
72396   memAboutToChange(p, pDest);
72397   if( pCur->nullRow ){
72398     sqlite3VdbeMemSetNull(pDest);
72399     break;
72400   }
72401   pVtab = pCur->pVtabCursor->pVtab;
72402   pModule = pVtab->pModule;
72403   assert( pModule->xColumn );
72404   memset(&sContext, 0, sizeof(sContext));
72405 
72406   /* The output cell may already have a buffer allocated. Move
72407   ** the current contents to sContext.s so in case the user-function
72408   ** can use the already allocated buffer instead of allocating a
72409   ** new one.
72410   */
72411   sqlite3VdbeMemMove(&sContext.s, pDest);
72412   MemSetTypeFlag(&sContext.s, MEM_Null);
72413 
72414   rc = pModule->xColumn(pCur->pVtabCursor, &sContext, pOp->p2);
72415   sqlite3VtabImportErrmsg(p, pVtab);
72416   if( sContext.isError ){
72417     rc = sContext.isError;
72418   }
72419 
72420   /* Copy the result of the function to the P3 register. We
72421   ** do this regardless of whether or not an error occurred to ensure any
72422   ** dynamic allocation in sContext.s (a Mem struct) is  released.
72423   */
72424   sqlite3VdbeChangeEncoding(&sContext.s, encoding);
72425   sqlite3VdbeMemMove(pDest, &sContext.s);
72426   REGISTER_TRACE(pOp->p3, pDest);
72427   UPDATE_MAX_BLOBSIZE(pDest);
72428 
72429   if( sqlite3VdbeMemTooBig(pDest) ){
72430     goto too_big;
72431   }
72432   break;
72433 }
72434 #endif /* SQLITE_OMIT_VIRTUALTABLE */
72435 
72436 #ifndef SQLITE_OMIT_VIRTUALTABLE
72437 /* Opcode: VNext P1 P2 * * *
72438 **
72439 ** Advance virtual table P1 to the next row in its result set and
72440 ** jump to instruction P2.  Or, if the virtual table has reached
72441 ** the end of its result set, then fall through to the next instruction.
72442 */
72443 case OP_VNext: {   /* jump */
72444   sqlite3_vtab *pVtab;
72445   const sqlite3_module *pModule;
72446   int res;
72447   VdbeCursor *pCur;
72448 
72449   res = 0;
72450   pCur = p->apCsr[pOp->p1];
72451   assert( pCur->pVtabCursor );
72452   if( pCur->nullRow ){
72453     break;
72454   }
72455   pVtab = pCur->pVtabCursor->pVtab;
72456   pModule = pVtab->pModule;
72457   assert( pModule->xNext );
72458 
72459   /* Invoke the xNext() method of the module. There is no way for the
72460   ** underlying implementation to return an error if one occurs during
72461   ** xNext(). Instead, if an error occurs, true is returned (indicating that
72462   ** data is available) and the error code returned when xColumn or
72463   ** some other method is next invoked on the save virtual table cursor.
72464   */
72465   p->inVtabMethod = 1;
72466   rc = pModule->xNext(pCur->pVtabCursor);
72467   p->inVtabMethod = 0;
72468   sqlite3VtabImportErrmsg(p, pVtab);
72469   if( rc==SQLITE_OK ){
72470     res = pModule->xEof(pCur->pVtabCursor);
72471   }
72472 
72473   if( !res ){
72474     /* If there is data, jump to P2 */
72475     pc = pOp->p2 - 1;
72476   }
72477   goto check_for_interrupt;
72478 }
72479 #endif /* SQLITE_OMIT_VIRTUALTABLE */
72480 
72481 #ifndef SQLITE_OMIT_VIRTUALTABLE
72482 /* Opcode: VRename P1 * * P4 *
72483 **
72484 ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
72485 ** This opcode invokes the corresponding xRename method. The value
72486 ** in register P1 is passed as the zName argument to the xRename method.
72487 */
72488 case OP_VRename: {
72489   sqlite3_vtab *pVtab;
72490   Mem *pName;
72491 
72492   pVtab = pOp->p4.pVtab->pVtab;
72493   pName = &aMem[pOp->p1];
72494   assert( pVtab->pModule->xRename );
72495   assert( memIsValid(pName) );
72496   assert( p->readOnly==0 );
72497   REGISTER_TRACE(pOp->p1, pName);
72498   assert( pName->flags & MEM_Str );
72499   testcase( pName->enc==SQLITE_UTF8 );
72500   testcase( pName->enc==SQLITE_UTF16BE );
72501   testcase( pName->enc==SQLITE_UTF16LE );
72502   rc = sqlite3VdbeChangeEncoding(pName, SQLITE_UTF8);
72503   if( rc==SQLITE_OK ){
72504     rc = pVtab->pModule->xRename(pVtab, pName->z);
72505     sqlite3VtabImportErrmsg(p, pVtab);
72506     p->expired = 0;
72507   }
72508   break;
72509 }
72510 #endif
72511 
72512 #ifndef SQLITE_OMIT_VIRTUALTABLE
72513 /* Opcode: VUpdate P1 P2 P3 P4 *
72514 ** Synopsis: data=r[P3@P2]
72515 **
72516 ** P4 is a pointer to a virtual table object, an sqlite3_vtab structure.
72517 ** This opcode invokes the corresponding xUpdate method. P2 values
72518 ** are contiguous memory cells starting at P3 to pass to the xUpdate
72519 ** invocation. The value in register (P3+P2-1) corresponds to the
72520 ** p2th element of the argv array passed to xUpdate.
72521 **
72522 ** The xUpdate method will do a DELETE or an INSERT or both.
72523 ** The argv[0] element (which corresponds to memory cell P3)
72524 ** is the rowid of a row to delete.  If argv[0] is NULL then no
72525 ** deletion occurs.  The argv[1] element is the rowid of the new
72526 ** row.  This can be NULL to have the virtual table select the new
72527 ** rowid for itself.  The subsequent elements in the array are
72528 ** the values of columns in the new row.
72529 **
72530 ** If P2==1 then no insert is performed.  argv[0] is the rowid of
72531 ** a row to delete.
72532 **
72533 ** P1 is a boolean flag. If it is set to true and the xUpdate call
72534 ** is successful, then the value returned by sqlite3_last_insert_rowid()
72535 ** is set to the value of the rowid for the row just inserted.
72536 */
72537 case OP_VUpdate: {
72538   sqlite3_vtab *pVtab;
72539   sqlite3_module *pModule;
72540   int nArg;
72541   int i;
72542   sqlite_int64 rowid;
72543   Mem **apArg;
72544   Mem *pX;
72545 
72546   assert( pOp->p2==1        || pOp->p5==OE_Fail   || pOp->p5==OE_Rollback
72547        || pOp->p5==OE_Abort || pOp->p5==OE_Ignore || pOp->p5==OE_Replace
72548   );
72549   assert( p->readOnly==0 );
72550   pVtab = pOp->p4.pVtab->pVtab;
72551   pModule = (sqlite3_module *)pVtab->pModule;
72552   nArg = pOp->p2;
72553   assert( pOp->p4type==P4_VTAB );
72554   if( ALWAYS(pModule->xUpdate) ){
72555     u8 vtabOnConflict = db->vtabOnConflict;
72556     apArg = p->apArg;
72557     pX = &aMem[pOp->p3];
72558     for(i=0; i<nArg; i++){
72559       assert( memIsValid(pX) );
72560       memAboutToChange(p, pX);
72561       sqlite3VdbeMemStoreType(pX);
72562       apArg[i] = pX;
72563       pX++;
72564     }
72565     db->vtabOnConflict = pOp->p5;
72566     rc = pModule->xUpdate(pVtab, nArg, apArg, &rowid);
72567     db->vtabOnConflict = vtabOnConflict;
72568     sqlite3VtabImportErrmsg(p, pVtab);
72569     if( rc==SQLITE_OK && pOp->p1 ){
72570       assert( nArg>1 && apArg[0] && (apArg[0]->flags&MEM_Null) );
72571       db->lastRowid = lastRowid = rowid;
72572     }
72573     if( (rc&0xff)==SQLITE_CONSTRAINT && pOp->p4.pVtab->bConstraint ){
72574       if( pOp->p5==OE_Ignore ){
72575         rc = SQLITE_OK;
72576       }else{
72577         p->errorAction = ((pOp->p5==OE_Replace) ? OE_Abort : pOp->p5);
72578       }
72579     }else{
72580       p->nChange++;
72581     }
72582   }
72583   break;
72584 }
72585 #endif /* SQLITE_OMIT_VIRTUALTABLE */
72586 
72587 #ifndef  SQLITE_OMIT_PAGER_PRAGMAS
72588 /* Opcode: Pagecount P1 P2 * * *
72589 **
72590 ** Write the current number of pages in database P1 to memory cell P2.
72591 */
72592 case OP_Pagecount: {            /* out2-prerelease */
72593   pOut->u.i = sqlite3BtreeLastPage(db->aDb[pOp->p1].pBt);
72594   break;
72595 }
72596 #endif
72597 
72598 
72599 #ifndef  SQLITE_OMIT_PAGER_PRAGMAS
72600 /* Opcode: MaxPgcnt P1 P2 P3 * *
72601 **
72602 ** Try to set the maximum page count for database P1 to the value in P3.
72603 ** Do not let the maximum page count fall below the current page count and
72604 ** do not change the maximum page count value if P3==0.
72605 **
72606 ** Store the maximum page count after the change in register P2.
72607 */
72608 case OP_MaxPgcnt: {            /* out2-prerelease */
72609   unsigned int newMax;
72610   Btree *pBt;
72611 
72612   pBt = db->aDb[pOp->p1].pBt;
72613   newMax = 0;
72614   if( pOp->p3 ){
72615     newMax = sqlite3BtreeLastPage(pBt);
72616     if( newMax < (unsigned)pOp->p3 ) newMax = (unsigned)pOp->p3;
72617   }
72618   pOut->u.i = sqlite3BtreeMaxPageCount(pBt, newMax);
72619   break;
72620 }
72621 #endif
72622 
72623 
72624 #ifndef SQLITE_OMIT_TRACE
72625 /* Opcode: Trace * * * P4 *
72626 **
72627 ** If tracing is enabled (by the sqlite3_trace()) interface, then
72628 ** the UTF-8 string contained in P4 is emitted on the trace callback.
72629 */
72630 case OP_Trace: {
72631   char *zTrace;
72632   char *z;
72633 
72634   if( db->xTrace
72635    && !p->doingRerun
72636    && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
72637   ){
72638     z = sqlite3VdbeExpandSql(p, zTrace);
72639     db->xTrace(db->pTraceArg, z);
72640     sqlite3DbFree(db, z);
72641   }
72642 #ifdef SQLITE_USE_FCNTL_TRACE
72643   zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql);
72644   if( zTrace ){
72645     int i;
72646     for(i=0; i<db->nDb; i++){
72647       if( MASKBIT(i) & p->btreeMask)==0 ) continue;
72648       sqlite3_file_control(db, db->aDb[i].zName, SQLITE_FCNTL_TRACE, zTrace);
72649     }
72650   }
72651 #endif /* SQLITE_USE_FCNTL_TRACE */
72652 #ifdef SQLITE_DEBUG
72653   if( (db->flags & SQLITE_SqlTrace)!=0
72654    && (zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql))!=0
72655   ){
72656     sqlite3DebugPrintf("SQL-trace: %s\n", zTrace);
72657   }
72658 #endif /* SQLITE_DEBUG */
72659   break;
72660 }
72661 #endif
72662 
72663 
72664 /* Opcode: Noop * * * * *
72665 **
72666 ** Do nothing.  This instruction is often useful as a jump
72667 ** destination.
72668 */
72669 /*
72670 ** The magic Explain opcode are only inserted when explain==2 (which
72671 ** is to say when the EXPLAIN QUERY PLAN syntax is used.)
72672 ** This opcode records information from the optimizer.  It is the
72673 ** the same as a no-op.  This opcodesnever appears in a real VM program.
72674 */
72675 default: {          /* This is really OP_Noop and OP_Explain */
72676   assert( pOp->opcode==OP_Noop || pOp->opcode==OP_Explain );
72677   break;
72678 }
72679 
72680 /*****************************************************************************
72681 ** The cases of the switch statement above this line should all be indented
72682 ** by 6 spaces.  But the left-most 6 spaces have been removed to improve the
72683 ** readability.  From this point on down, the normal indentation rules are
72684 ** restored.
72685 *****************************************************************************/
72686     }
72687 
72688 #ifdef VDBE_PROFILE
72689     {
72690       u64 elapsed = sqlite3Hwtime() - start;
72691       pOp->cycles += elapsed;
72692       pOp->cnt++;
72693 #if 0
72694         fprintf(stdout, "%10llu ", elapsed);
72695         sqlite3VdbePrintOp(stdout, origPc, &aOp[origPc]);
72696 #endif
72697     }
72698 #endif
72699 
72700     /* The following code adds nothing to the actual functionality
72701     ** of the program.  It is only here for testing and debugging.
72702     ** On the other hand, it does burn CPU cycles every time through
72703     ** the evaluator loop.  So we can leave it out when NDEBUG is defined.
72704     */
72705 #ifndef NDEBUG
72706     assert( pc>=-1 && pc<p->nOp );
72707 
72708 #ifdef SQLITE_DEBUG
72709     if( db->flags & SQLITE_VdbeTrace ){
72710       if( rc!=0 ) printf("rc=%d\n",rc);
72711       if( pOp->opflags & (OPFLG_OUT2_PRERELEASE|OPFLG_OUT2) ){
72712         registerTrace(pOp->p2, &aMem[pOp->p2]);
72713       }
72714       if( pOp->opflags & OPFLG_OUT3 ){
72715         registerTrace(pOp->p3, &aMem[pOp->p3]);
72716       }
72717     }
72718 #endif  /* SQLITE_DEBUG */
72719 #endif  /* NDEBUG */
72720   }  /* The end of the for(;;) loop the loops through opcodes */
72721 
72722   /* If we reach this point, it means that execution is finished with
72723   ** an error of some kind.
72724   */
72725 vdbe_error_halt:
72726   assert( rc );
72727   p->rc = rc;
72728   testcase( sqlite3GlobalConfig.xLog!=0 );
72729   sqlite3_log(rc, "statement aborts at %d: [%s] %s",
72730                    pc, p->zSql, p->zErrMsg);
72731   sqlite3VdbeHalt(p);
72732   if( rc==SQLITE_IOERR_NOMEM ) db->mallocFailed = 1;
72733   rc = SQLITE_ERROR;
72734   if( resetSchemaOnFault>0 ){
72735     sqlite3ResetOneSchema(db, resetSchemaOnFault-1);
72736   }
72737 
72738   /* This is the only way out of this procedure.  We have to
72739   ** release the mutexes on btrees that were acquired at the
72740   ** top. */
72741 vdbe_return:
72742   db->lastRowid = lastRowid;
72743   testcase( nVmStep>0 );
72744   p->aCounter[SQLITE_STMTSTATUS_VM_STEP] += (int)nVmStep;
72745   sqlite3VdbeLeave(p);
72746   return rc;
72747 
72748   /* Jump to here if a string or blob larger than SQLITE_MAX_LENGTH
72749   ** is encountered.
72750   */
72751 too_big:
72752   sqlite3SetString(&p->zErrMsg, db, "string or blob too big");
72753   rc = SQLITE_TOOBIG;
72754   goto vdbe_error_halt;
72755 
72756   /* Jump to here if a malloc() fails.
72757   */
72758 no_mem:
72759   db->mallocFailed = 1;
72760   sqlite3SetString(&p->zErrMsg, db, "out of memory");
72761   rc = SQLITE_NOMEM;
72762   goto vdbe_error_halt;
72763 
72764   /* Jump to here for any other kind of fatal error.  The "rc" variable
72765   ** should hold the error number.
72766   */
72767 abort_due_to_error:
72768   assert( p->zErrMsg==0 );
72769   if( db->mallocFailed ) rc = SQLITE_NOMEM;
72770   if( rc!=SQLITE_IOERR_NOMEM ){
72771     sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(rc));
72772   }
72773   goto vdbe_error_halt;
72774 
72775   /* Jump to here if the sqlite3_interrupt() API sets the interrupt
72776   ** flag.
72777   */
72778 abort_due_to_interrupt:
72779   assert( db->u1.isInterrupted );
72780   rc = SQLITE_INTERRUPT;
72781   p->rc = rc;
72782   sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3ErrStr(rc));
72783   goto vdbe_error_halt;
72784 }
72785 
72786 
72787 /************** End of vdbe.c ************************************************/
72788 /************** Begin file vdbeblob.c ****************************************/
72789 /*
72790 ** 2007 May 1
72791 **
72792 ** The author disclaims copyright to this source code.  In place of
72793 ** a legal notice, here is a blessing:
72794 **
72795 **    May you do good and not evil.
72796 **    May you find forgiveness for yourself and forgive others.
72797 **    May you share freely, never taking more than you give.
72798 **
72799 *************************************************************************
72800 **
72801 ** This file contains code used to implement incremental BLOB I/O.
72802 */
72803 
72804 
72805 #ifndef SQLITE_OMIT_INCRBLOB
72806 
72807 /*
72808 ** Valid sqlite3_blob* handles point to Incrblob structures.
72809 */
72810 typedef struct Incrblob Incrblob;
72811 struct Incrblob {
72812   int flags;              /* Copy of "flags" passed to sqlite3_blob_open() */
72813   int nByte;              /* Size of open blob, in bytes */
72814   int iOffset;            /* Byte offset of blob in cursor data */
72815   int iCol;               /* Table column this handle is open on */
72816   BtCursor *pCsr;         /* Cursor pointing at blob row */
72817   sqlite3_stmt *pStmt;    /* Statement holding cursor open */
72818   sqlite3 *db;            /* The associated database */
72819 };
72820 
72821 
72822 /*
72823 ** This function is used by both blob_open() and blob_reopen(). It seeks
72824 ** the b-tree cursor associated with blob handle p to point to row iRow.
72825 ** If successful, SQLITE_OK is returned and subsequent calls to
72826 ** sqlite3_blob_read() or sqlite3_blob_write() access the specified row.
72827 **
72828 ** If an error occurs, or if the specified row does not exist or does not
72829 ** contain a value of type TEXT or BLOB in the column nominated when the
72830 ** blob handle was opened, then an error code is returned and *pzErr may
72831 ** be set to point to a buffer containing an error message. It is the
72832 ** responsibility of the caller to free the error message buffer using
72833 ** sqlite3DbFree().
72834 **
72835 ** If an error does occur, then the b-tree cursor is closed. All subsequent
72836 ** calls to sqlite3_blob_read(), blob_write() or blob_reopen() will
72837 ** immediately return SQLITE_ABORT.
72838 */
72839 static int blobSeekToRow(Incrblob *p, sqlite3_int64 iRow, char **pzErr){
72840   int rc;                         /* Error code */
72841   char *zErr = 0;                 /* Error message */
72842   Vdbe *v = (Vdbe *)p->pStmt;
72843 
72844   /* Set the value of the SQL statements only variable to integer iRow.
72845   ** This is done directly instead of using sqlite3_bind_int64() to avoid
72846   ** triggering asserts related to mutexes.
72847   */
72848   assert( v->aVar[0].flags&MEM_Int );
72849   v->aVar[0].u.i = iRow;
72850 
72851   rc = sqlite3_step(p->pStmt);
72852   if( rc==SQLITE_ROW ){
72853     VdbeCursor *pC = v->apCsr[0];
72854     u32 type = pC->aType[p->iCol];
72855     if( type<12 ){
72856       zErr = sqlite3MPrintf(p->db, "cannot open value of type %s",
72857           type==0?"null": type==7?"real": "integer"
72858       );
72859       rc = SQLITE_ERROR;
72860       sqlite3_finalize(p->pStmt);
72861       p->pStmt = 0;
72862     }else{
72863       p->iOffset = pC->aType[p->iCol + pC->nField];
72864       p->nByte = sqlite3VdbeSerialTypeLen(type);
72865       p->pCsr =  pC->pCursor;
72866       sqlite3BtreeEnterCursor(p->pCsr);
72867       sqlite3BtreeCacheOverflow(p->pCsr);
72868       sqlite3BtreeLeaveCursor(p->pCsr);
72869     }
72870   }
72871 
72872   if( rc==SQLITE_ROW ){
72873     rc = SQLITE_OK;
72874   }else if( p->pStmt ){
72875     rc = sqlite3_finalize(p->pStmt);
72876     p->pStmt = 0;
72877     if( rc==SQLITE_OK ){
72878       zErr = sqlite3MPrintf(p->db, "no such rowid: %lld", iRow);
72879       rc = SQLITE_ERROR;
72880     }else{
72881       zErr = sqlite3MPrintf(p->db, "%s", sqlite3_errmsg(p->db));
72882     }
72883   }
72884 
72885   assert( rc!=SQLITE_OK || zErr==0 );
72886   assert( rc!=SQLITE_ROW && rc!=SQLITE_DONE );
72887 
72888   *pzErr = zErr;
72889   return rc;
72890 }
72891 
72892 /*
72893 ** Open a blob handle.
72894 */
72895 SQLITE_API int sqlite3_blob_open(
72896   sqlite3* db,            /* The database connection */
72897   const char *zDb,        /* The attached database containing the blob */
72898   const char *zTable,     /* The table containing the blob */
72899   const char *zColumn,    /* The column containing the blob */
72900   sqlite_int64 iRow,      /* The row containing the glob */
72901   int flags,              /* True -> read/write access, false -> read-only */
72902   sqlite3_blob **ppBlob   /* Handle for accessing the blob returned here */
72903 ){
72904   int nAttempt = 0;
72905   int iCol;               /* Index of zColumn in row-record */
72906 
72907   /* This VDBE program seeks a btree cursor to the identified
72908   ** db/table/row entry. The reason for using a vdbe program instead
72909   ** of writing code to use the b-tree layer directly is that the
72910   ** vdbe program will take advantage of the various transaction,
72911   ** locking and error handling infrastructure built into the vdbe.
72912   **
72913   ** After seeking the cursor, the vdbe executes an OP_ResultRow.
72914   ** Code external to the Vdbe then "borrows" the b-tree cursor and
72915   ** uses it to implement the blob_read(), blob_write() and
72916   ** blob_bytes() functions.
72917   **
72918   ** The sqlite3_blob_close() function finalizes the vdbe program,
72919   ** which closes the b-tree cursor and (possibly) commits the
72920   ** transaction.
72921   */
72922   static const VdbeOpList openBlob[] = {
72923     {OP_Transaction, 0, 0, 0},     /* 0: Start a transaction */
72924     {OP_VerifyCookie, 0, 0, 0},    /* 1: Check the schema cookie */
72925     {OP_TableLock, 0, 0, 0},       /* 2: Acquire a read or write lock */
72926 
72927     /* One of the following two instructions is replaced by an OP_Noop. */
72928     {OP_OpenRead, 0, 0, 0},        /* 3: Open cursor 0 for reading */
72929     {OP_OpenWrite, 0, 0, 0},       /* 4: Open cursor 0 for read/write */
72930 
72931     {OP_Variable, 1, 1, 1},        /* 5: Push the rowid to the stack */
72932     {OP_NotExists, 0, 10, 1},      /* 6: Seek the cursor */
72933     {OP_Column, 0, 0, 1},          /* 7  */
72934     {OP_ResultRow, 1, 0, 0},       /* 8  */
72935     {OP_Goto, 0, 5, 0},            /* 9  */
72936     {OP_Close, 0, 0, 0},           /* 10 */
72937     {OP_Halt, 0, 0, 0},            /* 11 */
72938   };
72939 
72940   int rc = SQLITE_OK;
72941   char *zErr = 0;
72942   Table *pTab;
72943   Parse *pParse = 0;
72944   Incrblob *pBlob = 0;
72945 
72946   flags = !!flags;                /* flags = (flags ? 1 : 0); */
72947   *ppBlob = 0;
72948 
72949   sqlite3_mutex_enter(db->mutex);
72950 
72951   pBlob = (Incrblob *)sqlite3DbMallocZero(db, sizeof(Incrblob));
72952   if( !pBlob ) goto blob_open_out;
72953   pParse = sqlite3StackAllocRaw(db, sizeof(*pParse));
72954   if( !pParse ) goto blob_open_out;
72955 
72956   do {
72957     memset(pParse, 0, sizeof(Parse));
72958     pParse->db = db;
72959     sqlite3DbFree(db, zErr);
72960     zErr = 0;
72961 
72962     sqlite3BtreeEnterAll(db);
72963     pTab = sqlite3LocateTable(pParse, 0, zTable, zDb);
72964     if( pTab && IsVirtual(pTab) ){
72965       pTab = 0;
72966       sqlite3ErrorMsg(pParse, "cannot open virtual table: %s", zTable);
72967     }
72968     if( pTab && !HasRowid(pTab) ){
72969       pTab = 0;
72970       sqlite3ErrorMsg(pParse, "cannot open table without rowid: %s", zTable);
72971     }
72972 #ifndef SQLITE_OMIT_VIEW
72973     if( pTab && pTab->pSelect ){
72974       pTab = 0;
72975       sqlite3ErrorMsg(pParse, "cannot open view: %s", zTable);
72976     }
72977 #endif
72978     if( !pTab ){
72979       if( pParse->zErrMsg ){
72980         sqlite3DbFree(db, zErr);
72981         zErr = pParse->zErrMsg;
72982         pParse->zErrMsg = 0;
72983       }
72984       rc = SQLITE_ERROR;
72985       sqlite3BtreeLeaveAll(db);
72986       goto blob_open_out;
72987     }
72988 
72989     /* Now search pTab for the exact column. */
72990     for(iCol=0; iCol<pTab->nCol; iCol++) {
72991       if( sqlite3StrICmp(pTab->aCol[iCol].zName, zColumn)==0 ){
72992         break;
72993       }
72994     }
72995     if( iCol==pTab->nCol ){
72996       sqlite3DbFree(db, zErr);
72997       zErr = sqlite3MPrintf(db, "no such column: \"%s\"", zColumn);
72998       rc = SQLITE_ERROR;
72999       sqlite3BtreeLeaveAll(db);
73000       goto blob_open_out;
73001     }
73002 
73003     /* If the value is being opened for writing, check that the
73004     ** column is not indexed, and that it is not part of a foreign key.
73005     ** It is against the rules to open a column to which either of these
73006     ** descriptions applies for writing.  */
73007     if( flags ){
73008       const char *zFault = 0;
73009       Index *pIdx;
73010 #ifndef SQLITE_OMIT_FOREIGN_KEY
73011       if( db->flags&SQLITE_ForeignKeys ){
73012         /* Check that the column is not part of an FK child key definition. It
73013         ** is not necessary to check if it is part of a parent key, as parent
73014         ** key columns must be indexed. The check below will pick up this
73015         ** case.  */
73016         FKey *pFKey;
73017         for(pFKey=pTab->pFKey; pFKey; pFKey=pFKey->pNextFrom){
73018           int j;
73019           for(j=0; j<pFKey->nCol; j++){
73020             if( pFKey->aCol[j].iFrom==iCol ){
73021               zFault = "foreign key";
73022             }
73023           }
73024         }
73025       }
73026 #endif
73027       for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
73028         int j;
73029         for(j=0; j<pIdx->nKeyCol; j++){
73030           if( pIdx->aiColumn[j]==iCol ){
73031             zFault = "indexed";
73032           }
73033         }
73034       }
73035       if( zFault ){
73036         sqlite3DbFree(db, zErr);
73037         zErr = sqlite3MPrintf(db, "cannot open %s column for writing", zFault);
73038         rc = SQLITE_ERROR;
73039         sqlite3BtreeLeaveAll(db);
73040         goto blob_open_out;
73041       }
73042     }
73043 
73044     pBlob->pStmt = (sqlite3_stmt *)sqlite3VdbeCreate(pParse);
73045     assert( pBlob->pStmt || db->mallocFailed );
73046     if( pBlob->pStmt ){
73047       Vdbe *v = (Vdbe *)pBlob->pStmt;
73048       int iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
73049 
73050       sqlite3VdbeAddOpList(v, sizeof(openBlob)/sizeof(VdbeOpList), openBlob);
73051 
73052 
73053       /* Configure the OP_Transaction */
73054       sqlite3VdbeChangeP1(v, 0, iDb);
73055       sqlite3VdbeChangeP2(v, 0, flags);
73056 
73057       /* Configure the OP_VerifyCookie */
73058       sqlite3VdbeChangeP1(v, 1, iDb);
73059       sqlite3VdbeChangeP2(v, 1, pTab->pSchema->schema_cookie);
73060       sqlite3VdbeChangeP3(v, 1, pTab->pSchema->iGeneration);
73061 
73062       /* Make sure a mutex is held on the table to be accessed */
73063       sqlite3VdbeUsesBtree(v, iDb);
73064 
73065       /* Configure the OP_TableLock instruction */
73066 #ifdef SQLITE_OMIT_SHARED_CACHE
73067       sqlite3VdbeChangeToNoop(v, 2);
73068 #else
73069       sqlite3VdbeChangeP1(v, 2, iDb);
73070       sqlite3VdbeChangeP2(v, 2, pTab->tnum);
73071       sqlite3VdbeChangeP3(v, 2, flags);
73072       sqlite3VdbeChangeP4(v, 2, pTab->zName, P4_TRANSIENT);
73073 #endif
73074 
73075       /* Remove either the OP_OpenWrite or OpenRead. Set the P2
73076       ** parameter of the other to pTab->tnum.  */
73077       sqlite3VdbeChangeToNoop(v, 4 - flags);
73078       sqlite3VdbeChangeP2(v, 3 + flags, pTab->tnum);
73079       sqlite3VdbeChangeP3(v, 3 + flags, iDb);
73080 
73081       /* Configure the number of columns. Configure the cursor to
73082       ** think that the table has one more column than it really
73083       ** does. An OP_Column to retrieve this imaginary column will
73084       ** always return an SQL NULL. This is useful because it means
73085       ** we can invoke OP_Column to fill in the vdbe cursors type
73086       ** and offset cache without causing any IO.
73087       */
73088       sqlite3VdbeChangeP4(v, 3+flags, SQLITE_INT_TO_PTR(pTab->nCol+1),P4_INT32);
73089       sqlite3VdbeChangeP2(v, 7, pTab->nCol);
73090       if( !db->mallocFailed ){
73091         pParse->nVar = 1;
73092         pParse->nMem = 1;
73093         pParse->nTab = 1;
73094         sqlite3VdbeMakeReady(v, pParse);
73095       }
73096     }
73097 
73098     pBlob->flags = flags;
73099     pBlob->iCol = iCol;
73100     pBlob->db = db;
73101     sqlite3BtreeLeaveAll(db);
73102     if( db->mallocFailed ){
73103       goto blob_open_out;
73104     }
73105     sqlite3_bind_int64(pBlob->pStmt, 1, iRow);
73106     rc = blobSeekToRow(pBlob, iRow, &zErr);
73107   } while( (++nAttempt)<SQLITE_MAX_SCHEMA_RETRY && rc==SQLITE_SCHEMA );
73108 
73109 blob_open_out:
73110   if( rc==SQLITE_OK && db->mallocFailed==0 ){
73111     *ppBlob = (sqlite3_blob *)pBlob;
73112   }else{
73113     if( pBlob && pBlob->pStmt ) sqlite3VdbeFinalize((Vdbe *)pBlob->pStmt);
73114     sqlite3DbFree(db, pBlob);
73115   }
73116   sqlite3Error(db, rc, (zErr ? "%s" : 0), zErr);
73117   sqlite3DbFree(db, zErr);
73118   sqlite3ParserReset(pParse);
73119   sqlite3StackFree(db, pParse);
73120   rc = sqlite3ApiExit(db, rc);
73121   sqlite3_mutex_leave(db->mutex);
73122   return rc;
73123 }
73124 
73125 /*
73126 ** Close a blob handle that was previously created using
73127 ** sqlite3_blob_open().
73128 */
73129 SQLITE_API int sqlite3_blob_close(sqlite3_blob *pBlob){
73130   Incrblob *p = (Incrblob *)pBlob;
73131   int rc;
73132   sqlite3 *db;
73133 
73134   if( p ){
73135     db = p->db;
73136     sqlite3_mutex_enter(db->mutex);
73137     rc = sqlite3_finalize(p->pStmt);
73138     sqlite3DbFree(db, p);
73139     sqlite3_mutex_leave(db->mutex);
73140   }else{
73141     rc = SQLITE_OK;
73142   }
73143   return rc;
73144 }
73145 
73146 /*
73147 ** Perform a read or write operation on a blob
73148 */
73149 static int blobReadWrite(
73150   sqlite3_blob *pBlob,
73151   void *z,
73152   int n,
73153   int iOffset,
73154   int (*xCall)(BtCursor*, u32, u32, void*)
73155 ){
73156   int rc;
73157   Incrblob *p = (Incrblob *)pBlob;
73158   Vdbe *v;
73159   sqlite3 *db;
73160 
73161   if( p==0 ) return SQLITE_MISUSE_BKPT;
73162   db = p->db;
73163   sqlite3_mutex_enter(db->mutex);
73164   v = (Vdbe*)p->pStmt;
73165 
73166   if( n<0 || iOffset<0 || (iOffset+n)>p->nByte ){
73167     /* Request is out of range. Return a transient error. */
73168     rc = SQLITE_ERROR;
73169     sqlite3Error(db, SQLITE_ERROR, 0);
73170   }else if( v==0 ){
73171     /* If there is no statement handle, then the blob-handle has
73172     ** already been invalidated. Return SQLITE_ABORT in this case.
73173     */
73174     rc = SQLITE_ABORT;
73175   }else{
73176     /* Call either BtreeData() or BtreePutData(). If SQLITE_ABORT is
73177     ** returned, clean-up the statement handle.
73178     */
73179     assert( db == v->db );
73180     sqlite3BtreeEnterCursor(p->pCsr);
73181     rc = xCall(p->pCsr, iOffset+p->iOffset, n, z);
73182     sqlite3BtreeLeaveCursor(p->pCsr);
73183     if( rc==SQLITE_ABORT ){
73184       sqlite3VdbeFinalize(v);
73185       p->pStmt = 0;
73186     }else{
73187       db->errCode = rc;
73188       v->rc = rc;
73189     }
73190   }
73191   rc = sqlite3ApiExit(db, rc);
73192   sqlite3_mutex_leave(db->mutex);
73193   return rc;
73194 }
73195 
73196 /*
73197 ** Read data from a blob handle.
73198 */
73199 SQLITE_API int sqlite3_blob_read(sqlite3_blob *pBlob, void *z, int n, int iOffset){
73200   return blobReadWrite(pBlob, z, n, iOffset, sqlite3BtreeData);
73201 }
73202 
73203 /*
73204 ** Write data to a blob handle.
73205 */
73206 SQLITE_API int sqlite3_blob_write(sqlite3_blob *pBlob, const void *z, int n, int iOffset){
73207   return blobReadWrite(pBlob, (void *)z, n, iOffset, sqlite3BtreePutData);
73208 }
73209 
73210 /*
73211 ** Query a blob handle for the size of the data.
73212 **
73213 ** The Incrblob.nByte field is fixed for the lifetime of the Incrblob
73214 ** so no mutex is required for access.
73215 */
73216 SQLITE_API int sqlite3_blob_bytes(sqlite3_blob *pBlob){
73217   Incrblob *p = (Incrblob *)pBlob;
73218   return (p && p->pStmt) ? p->nByte : 0;
73219 }
73220 
73221 /*
73222 ** Move an existing blob handle to point to a different row of the same
73223 ** database table.
73224 **
73225 ** If an error occurs, or if the specified row does not exist or does not
73226 ** contain a blob or text value, then an error code is returned and the
73227 ** database handle error code and message set. If this happens, then all
73228 ** subsequent calls to sqlite3_blob_xxx() functions (except blob_close())
73229 ** immediately return SQLITE_ABORT.
73230 */
73231 SQLITE_API int sqlite3_blob_reopen(sqlite3_blob *pBlob, sqlite3_int64 iRow){
73232   int rc;
73233   Incrblob *p = (Incrblob *)pBlob;
73234   sqlite3 *db;
73235 
73236   if( p==0 ) return SQLITE_MISUSE_BKPT;
73237   db = p->db;
73238   sqlite3_mutex_enter(db->mutex);
73239 
73240   if( p->pStmt==0 ){
73241     /* If there is no statement handle, then the blob-handle has
73242     ** already been invalidated. Return SQLITE_ABORT in this case.
73243     */
73244     rc = SQLITE_ABORT;
73245   }else{
73246     char *zErr;
73247     rc = blobSeekToRow(p, iRow, &zErr);
73248     if( rc!=SQLITE_OK ){
73249       sqlite3Error(db, rc, (zErr ? "%s" : 0), zErr);
73250       sqlite3DbFree(db, zErr);
73251     }
73252     assert( rc!=SQLITE_SCHEMA );
73253   }
73254 
73255   rc = sqlite3ApiExit(db, rc);
73256   assert( rc==SQLITE_OK || p->pStmt==0 );
73257   sqlite3_mutex_leave(db->mutex);
73258   return rc;
73259 }
73260 
73261 #endif /* #ifndef SQLITE_OMIT_INCRBLOB */
73262 
73263 /************** End of vdbeblob.c ********************************************/
73264 /************** Begin file vdbesort.c ****************************************/
73265 /*
73266 ** 2011 July 9
73267 **
73268 ** The author disclaims copyright to this source code.  In place of
73269 ** a legal notice, here is a blessing:
73270 **
73271 **    May you do good and not evil.
73272 **    May you find forgiveness for yourself and forgive others.
73273 **    May you share freely, never taking more than you give.
73274 **
73275 *************************************************************************
73276 ** This file contains code for the VdbeSorter object, used in concert with
73277 ** a VdbeCursor to sort large numbers of keys (as may be required, for
73278 ** example, by CREATE INDEX statements on tables too large to fit in main
73279 ** memory).
73280 */
73281 
73282 
73283 
73284 typedef struct VdbeSorterIter VdbeSorterIter;
73285 typedef struct SorterRecord SorterRecord;
73286 typedef struct FileWriter FileWriter;
73287 
73288 /*
73289 ** NOTES ON DATA STRUCTURE USED FOR N-WAY MERGES:
73290 **
73291 ** As keys are added to the sorter, they are written to disk in a series
73292 ** of sorted packed-memory-arrays (PMAs). The size of each PMA is roughly
73293 ** the same as the cache-size allowed for temporary databases. In order
73294 ** to allow the caller to extract keys from the sorter in sorted order,
73295 ** all PMAs currently stored on disk must be merged together. This comment
73296 ** describes the data structure used to do so. The structure supports
73297 ** merging any number of arrays in a single pass with no redundant comparison
73298 ** operations.
73299 **
73300 ** The aIter[] array contains an iterator for each of the PMAs being merged.
73301 ** An aIter[] iterator either points to a valid key or else is at EOF. For
73302 ** the purposes of the paragraphs below, we assume that the array is actually
73303 ** N elements in size, where N is the smallest power of 2 greater to or equal
73304 ** to the number of iterators being merged. The extra aIter[] elements are
73305 ** treated as if they are empty (always at EOF).
73306 **
73307 ** The aTree[] array is also N elements in size. The value of N is stored in
73308 ** the VdbeSorter.nTree variable.
73309 **
73310 ** The final (N/2) elements of aTree[] contain the results of comparing
73311 ** pairs of iterator keys together. Element i contains the result of
73312 ** comparing aIter[2*i-N] and aIter[2*i-N+1]. Whichever key is smaller, the
73313 ** aTree element is set to the index of it.
73314 **
73315 ** For the purposes of this comparison, EOF is considered greater than any
73316 ** other key value. If the keys are equal (only possible with two EOF
73317 ** values), it doesn't matter which index is stored.
73318 **
73319 ** The (N/4) elements of aTree[] that precede the final (N/2) described
73320 ** above contains the index of the smallest of each block of 4 iterators.
73321 ** And so on. So that aTree[1] contains the index of the iterator that
73322 ** currently points to the smallest key value. aTree[0] is unused.
73323 **
73324 ** Example:
73325 **
73326 **     aIter[0] -> Banana
73327 **     aIter[1] -> Feijoa
73328 **     aIter[2] -> Elderberry
73329 **     aIter[3] -> Currant
73330 **     aIter[4] -> Grapefruit
73331 **     aIter[5] -> Apple
73332 **     aIter[6] -> Durian
73333 **     aIter[7] -> EOF
73334 **
73335 **     aTree[] = { X, 5   0, 5    0, 3, 5, 6 }
73336 **
73337 ** The current element is "Apple" (the value of the key indicated by
73338 ** iterator 5). When the Next() operation is invoked, iterator 5 will
73339 ** be advanced to the next key in its segment. Say the next key is
73340 ** "Eggplant":
73341 **
73342 **     aIter[5] -> Eggplant
73343 **
73344 ** The contents of aTree[] are updated first by comparing the new iterator
73345 ** 5 key to the current key of iterator 4 (still "Grapefruit"). The iterator
73346 ** 5 value is still smaller, so aTree[6] is set to 5. And so on up the tree.
73347 ** The value of iterator 6 - "Durian" - is now smaller than that of iterator
73348 ** 5, so aTree[3] is set to 6. Key 0 is smaller than key 6 (Banana<Durian),
73349 ** so the value written into element 1 of the array is 0. As follows:
73350 **
73351 **     aTree[] = { X, 0   0, 6    0, 3, 5, 6 }
73352 **
73353 ** In other words, each time we advance to the next sorter element, log2(N)
73354 ** key comparison operations are required, where N is the number of segments
73355 ** being merged (rounded up to the next power of 2).
73356 */
73357 struct VdbeSorter {
73358   i64 iWriteOff;                  /* Current write offset within file pTemp1 */
73359   i64 iReadOff;                   /* Current read offset within file pTemp1 */
73360   int nInMemory;                  /* Current size of pRecord list as PMA */
73361   int nTree;                      /* Used size of aTree/aIter (power of 2) */
73362   int nPMA;                       /* Number of PMAs stored in pTemp1 */
73363   int mnPmaSize;                  /* Minimum PMA size, in bytes */
73364   int mxPmaSize;                  /* Maximum PMA size, in bytes.  0==no limit */
73365   VdbeSorterIter *aIter;          /* Array of iterators to merge */
73366   int *aTree;                     /* Current state of incremental merge */
73367   sqlite3_file *pTemp1;           /* PMA file 1 */
73368   SorterRecord *pRecord;          /* Head of in-memory record list */
73369   UnpackedRecord *pUnpacked;      /* Used to unpack keys */
73370 };
73371 
73372 /*
73373 ** The following type is an iterator for a PMA. It caches the current key in
73374 ** variables nKey/aKey. If the iterator is at EOF, pFile==0.
73375 */
73376 struct VdbeSorterIter {
73377   i64 iReadOff;                   /* Current read offset */
73378   i64 iEof;                       /* 1 byte past EOF for this iterator */
73379   int nAlloc;                     /* Bytes of space at aAlloc */
73380   int nKey;                       /* Number of bytes in key */
73381   sqlite3_file *pFile;            /* File iterator is reading from */
73382   u8 *aAlloc;                     /* Allocated space */
73383   u8 *aKey;                       /* Pointer to current key */
73384   u8 *aBuffer;                    /* Current read buffer */
73385   int nBuffer;                    /* Size of read buffer in bytes */
73386 };
73387 
73388 /*
73389 ** An instance of this structure is used to organize the stream of records
73390 ** being written to files by the merge-sort code into aligned, page-sized
73391 ** blocks.  Doing all I/O in aligned page-sized blocks helps I/O to go
73392 ** faster on many operating systems.
73393 */
73394 struct FileWriter {
73395   int eFWErr;                     /* Non-zero if in an error state */
73396   u8 *aBuffer;                    /* Pointer to write buffer */
73397   int nBuffer;                    /* Size of write buffer in bytes */
73398   int iBufStart;                  /* First byte of buffer to write */
73399   int iBufEnd;                    /* Last byte of buffer to write */
73400   i64 iWriteOff;                  /* Offset of start of buffer in file */
73401   sqlite3_file *pFile;            /* File to write to */
73402 };
73403 
73404 /*
73405 ** A structure to store a single record. All in-memory records are connected
73406 ** together into a linked list headed at VdbeSorter.pRecord using the
73407 ** SorterRecord.pNext pointer.
73408 */
73409 struct SorterRecord {
73410   void *pVal;
73411   int nVal;
73412   SorterRecord *pNext;
73413 };
73414 
73415 /* Minimum allowable value for the VdbeSorter.nWorking variable */
73416 #define SORTER_MIN_WORKING 10
73417 
73418 /* Maximum number of segments to merge in a single pass. */
73419 #define SORTER_MAX_MERGE_COUNT 16
73420 
73421 /*
73422 ** Free all memory belonging to the VdbeSorterIter object passed as the second
73423 ** argument. All structure fields are set to zero before returning.
73424 */
73425 static void vdbeSorterIterZero(sqlite3 *db, VdbeSorterIter *pIter){
73426   sqlite3DbFree(db, pIter->aAlloc);
73427   sqlite3DbFree(db, pIter->aBuffer);
73428   memset(pIter, 0, sizeof(VdbeSorterIter));
73429 }
73430 
73431 /*
73432 ** Read nByte bytes of data from the stream of data iterated by object p.
73433 ** If successful, set *ppOut to point to a buffer containing the data
73434 ** and return SQLITE_OK. Otherwise, if an error occurs, return an SQLite
73435 ** error code.
73436 **
73437 ** The buffer indicated by *ppOut may only be considered valid until the
73438 ** next call to this function.
73439 */
73440 static int vdbeSorterIterRead(
73441   sqlite3 *db,                    /* Database handle (for malloc) */
73442   VdbeSorterIter *p,              /* Iterator */
73443   int nByte,                      /* Bytes of data to read */
73444   u8 **ppOut                      /* OUT: Pointer to buffer containing data */
73445 ){
73446   int iBuf;                       /* Offset within buffer to read from */
73447   int nAvail;                     /* Bytes of data available in buffer */
73448   assert( p->aBuffer );
73449 
73450   /* If there is no more data to be read from the buffer, read the next
73451   ** p->nBuffer bytes of data from the file into it. Or, if there are less
73452   ** than p->nBuffer bytes remaining in the PMA, read all remaining data.  */
73453   iBuf = p->iReadOff % p->nBuffer;
73454   if( iBuf==0 ){
73455     int nRead;                    /* Bytes to read from disk */
73456     int rc;                       /* sqlite3OsRead() return code */
73457 
73458     /* Determine how many bytes of data to read. */
73459     if( (p->iEof - p->iReadOff) > (i64)p->nBuffer ){
73460       nRead = p->nBuffer;
73461     }else{
73462       nRead = (int)(p->iEof - p->iReadOff);
73463     }
73464     assert( nRead>0 );
73465 
73466     /* Read data from the file. Return early if an error occurs. */
73467     rc = sqlite3OsRead(p->pFile, p->aBuffer, nRead, p->iReadOff);
73468     assert( rc!=SQLITE_IOERR_SHORT_READ );
73469     if( rc!=SQLITE_OK ) return rc;
73470   }
73471   nAvail = p->nBuffer - iBuf;
73472 
73473   if( nByte<=nAvail ){
73474     /* The requested data is available in the in-memory buffer. In this
73475     ** case there is no need to make a copy of the data, just return a
73476     ** pointer into the buffer to the caller.  */
73477     *ppOut = &p->aBuffer[iBuf];
73478     p->iReadOff += nByte;
73479   }else{
73480     /* The requested data is not all available in the in-memory buffer.
73481     ** In this case, allocate space at p->aAlloc[] to copy the requested
73482     ** range into. Then return a copy of pointer p->aAlloc to the caller.  */
73483     int nRem;                     /* Bytes remaining to copy */
73484 
73485     /* Extend the p->aAlloc[] allocation if required. */
73486     if( p->nAlloc<nByte ){
73487       int nNew = p->nAlloc*2;
73488       while( nByte>nNew ) nNew = nNew*2;
73489       p->aAlloc = sqlite3DbReallocOrFree(db, p->aAlloc, nNew);
73490       if( !p->aAlloc ) return SQLITE_NOMEM;
73491       p->nAlloc = nNew;
73492     }
73493 
73494     /* Copy as much data as is available in the buffer into the start of
73495     ** p->aAlloc[].  */
73496     memcpy(p->aAlloc, &p->aBuffer[iBuf], nAvail);
73497     p->iReadOff += nAvail;
73498     nRem = nByte - nAvail;
73499 
73500     /* The following loop copies up to p->nBuffer bytes per iteration into
73501     ** the p->aAlloc[] buffer.  */
73502     while( nRem>0 ){
73503       int rc;                     /* vdbeSorterIterRead() return code */
73504       int nCopy;                  /* Number of bytes to copy */
73505       u8 *aNext;                  /* Pointer to buffer to copy data from */
73506 
73507       nCopy = nRem;
73508       if( nRem>p->nBuffer ) nCopy = p->nBuffer;
73509       rc = vdbeSorterIterRead(db, p, nCopy, &aNext);
73510       if( rc!=SQLITE_OK ) return rc;
73511       assert( aNext!=p->aAlloc );
73512       memcpy(&p->aAlloc[nByte - nRem], aNext, nCopy);
73513       nRem -= nCopy;
73514     }
73515 
73516     *ppOut = p->aAlloc;
73517   }
73518 
73519   return SQLITE_OK;
73520 }
73521 
73522 /*
73523 ** Read a varint from the stream of data accessed by p. Set *pnOut to
73524 ** the value read.
73525 */
73526 static int vdbeSorterIterVarint(sqlite3 *db, VdbeSorterIter *p, u64 *pnOut){
73527   int iBuf;
73528 
73529   iBuf = p->iReadOff % p->nBuffer;
73530   if( iBuf && (p->nBuffer-iBuf)>=9 ){
73531     p->iReadOff += sqlite3GetVarint(&p->aBuffer[iBuf], pnOut);
73532   }else{
73533     u8 aVarint[16], *a;
73534     int i = 0, rc;
73535     do{
73536       rc = vdbeSorterIterRead(db, p, 1, &a);
73537       if( rc ) return rc;
73538       aVarint[(i++)&0xf] = a[0];
73539     }while( (a[0]&0x80)!=0 );
73540     sqlite3GetVarint(aVarint, pnOut);
73541   }
73542 
73543   return SQLITE_OK;
73544 }
73545 
73546 
73547 /*
73548 ** Advance iterator pIter to the next key in its PMA. Return SQLITE_OK if
73549 ** no error occurs, or an SQLite error code if one does.
73550 */
73551 static int vdbeSorterIterNext(
73552   sqlite3 *db,                    /* Database handle (for sqlite3DbMalloc() ) */
73553   VdbeSorterIter *pIter           /* Iterator to advance */
73554 ){
73555   int rc;                         /* Return Code */
73556   u64 nRec = 0;                   /* Size of record in bytes */
73557 
73558   if( pIter->iReadOff>=pIter->iEof ){
73559     /* This is an EOF condition */
73560     vdbeSorterIterZero(db, pIter);
73561     return SQLITE_OK;
73562   }
73563 
73564   rc = vdbeSorterIterVarint(db, pIter, &nRec);
73565   if( rc==SQLITE_OK ){
73566     pIter->nKey = (int)nRec;
73567     rc = vdbeSorterIterRead(db, pIter, (int)nRec, &pIter->aKey);
73568   }
73569 
73570   return rc;
73571 }
73572 
73573 /*
73574 ** Initialize iterator pIter to scan through the PMA stored in file pFile
73575 ** starting at offset iStart and ending at offset iEof-1. This function
73576 ** leaves the iterator pointing to the first key in the PMA (or EOF if the
73577 ** PMA is empty).
73578 */
73579 static int vdbeSorterIterInit(
73580   sqlite3 *db,                    /* Database handle */
73581   const VdbeSorter *pSorter,      /* Sorter object */
73582   i64 iStart,                     /* Start offset in pFile */
73583   VdbeSorterIter *pIter,          /* Iterator to populate */
73584   i64 *pnByte                     /* IN/OUT: Increment this value by PMA size */
73585 ){
73586   int rc = SQLITE_OK;
73587   int nBuf;
73588 
73589   nBuf = sqlite3BtreeGetPageSize(db->aDb[0].pBt);
73590 
73591   assert( pSorter->iWriteOff>iStart );
73592   assert( pIter->aAlloc==0 );
73593   assert( pIter->aBuffer==0 );
73594   pIter->pFile = pSorter->pTemp1;
73595   pIter->iReadOff = iStart;
73596   pIter->nAlloc = 128;
73597   pIter->aAlloc = (u8 *)sqlite3DbMallocRaw(db, pIter->nAlloc);
73598   pIter->nBuffer = nBuf;
73599   pIter->aBuffer = (u8 *)sqlite3DbMallocRaw(db, nBuf);
73600 
73601   if( !pIter->aBuffer ){
73602     rc = SQLITE_NOMEM;
73603   }else{
73604     int iBuf;
73605 
73606     iBuf = iStart % nBuf;
73607     if( iBuf ){
73608       int nRead = nBuf - iBuf;
73609       if( (iStart + nRead) > pSorter->iWriteOff ){
73610         nRead = (int)(pSorter->iWriteOff - iStart);
73611       }
73612       rc = sqlite3OsRead(
73613           pSorter->pTemp1, &pIter->aBuffer[iBuf], nRead, iStart
73614       );
73615       assert( rc!=SQLITE_IOERR_SHORT_READ );
73616     }
73617 
73618     if( rc==SQLITE_OK ){
73619       u64 nByte;                       /* Size of PMA in bytes */
73620       pIter->iEof = pSorter->iWriteOff;
73621       rc = vdbeSorterIterVarint(db, pIter, &nByte);
73622       pIter->iEof = pIter->iReadOff + nByte;
73623       *pnByte += nByte;
73624     }
73625   }
73626 
73627   if( rc==SQLITE_OK ){
73628     rc = vdbeSorterIterNext(db, pIter);
73629   }
73630   return rc;
73631 }
73632 
73633 
73634 /*
73635 ** Compare key1 (buffer pKey1, size nKey1 bytes) with key2 (buffer pKey2,
73636 ** size nKey2 bytes).  Argument pKeyInfo supplies the collation functions
73637 ** used by the comparison. If an error occurs, return an SQLite error code.
73638 ** Otherwise, return SQLITE_OK and set *pRes to a negative, zero or positive
73639 ** value, depending on whether key1 is smaller, equal to or larger than key2.
73640 **
73641 ** If the bOmitRowid argument is non-zero, assume both keys end in a rowid
73642 ** field. For the purposes of the comparison, ignore it. Also, if bOmitRowid
73643 ** is true and key1 contains even a single NULL value, it is considered to
73644 ** be less than key2. Even if key2 also contains NULL values.
73645 **
73646 ** If pKey2 is passed a NULL pointer, then it is assumed that the pCsr->aSpace
73647 ** has been allocated and contains an unpacked record that is used as key2.
73648 */
73649 static void vdbeSorterCompare(
73650   const VdbeCursor *pCsr,         /* Cursor object (for pKeyInfo) */
73651   int nIgnore,                    /* Ignore the last nIgnore fields */
73652   const void *pKey1, int nKey1,   /* Left side of comparison */
73653   const void *pKey2, int nKey2,   /* Right side of comparison */
73654   int *pRes                       /* OUT: Result of comparison */
73655 ){
73656   KeyInfo *pKeyInfo = pCsr->pKeyInfo;
73657   VdbeSorter *pSorter = pCsr->pSorter;
73658   UnpackedRecord *r2 = pSorter->pUnpacked;
73659   int i;
73660 
73661   if( pKey2 ){
73662     sqlite3VdbeRecordUnpack(pKeyInfo, nKey2, pKey2, r2);
73663   }
73664 
73665   if( nIgnore ){
73666     r2->nField = pKeyInfo->nField - nIgnore;
73667     assert( r2->nField>0 );
73668     for(i=0; i<r2->nField; i++){
73669       if( r2->aMem[i].flags & MEM_Null ){
73670         *pRes = -1;
73671         return;
73672       }
73673     }
73674     r2->flags |= UNPACKED_PREFIX_MATCH;
73675   }
73676 
73677   *pRes = sqlite3VdbeRecordCompare(nKey1, pKey1, r2);
73678 }
73679 
73680 /*
73681 ** This function is called to compare two iterator keys when merging
73682 ** multiple b-tree segments. Parameter iOut is the index of the aTree[]
73683 ** value to recalculate.
73684 */
73685 static int vdbeSorterDoCompare(const VdbeCursor *pCsr, int iOut){
73686   VdbeSorter *pSorter = pCsr->pSorter;
73687   int i1;
73688   int i2;
73689   int iRes;
73690   VdbeSorterIter *p1;
73691   VdbeSorterIter *p2;
73692 
73693   assert( iOut<pSorter->nTree && iOut>0 );
73694 
73695   if( iOut>=(pSorter->nTree/2) ){
73696     i1 = (iOut - pSorter->nTree/2) * 2;
73697     i2 = i1 + 1;
73698   }else{
73699     i1 = pSorter->aTree[iOut*2];
73700     i2 = pSorter->aTree[iOut*2+1];
73701   }
73702 
73703   p1 = &pSorter->aIter[i1];
73704   p2 = &pSorter->aIter[i2];
73705 
73706   if( p1->pFile==0 ){
73707     iRes = i2;
73708   }else if( p2->pFile==0 ){
73709     iRes = i1;
73710   }else{
73711     int res;
73712     assert( pCsr->pSorter->pUnpacked!=0 );  /* allocated in vdbeSorterMerge() */
73713     vdbeSorterCompare(
73714         pCsr, 0, p1->aKey, p1->nKey, p2->aKey, p2->nKey, &res
73715     );
73716     if( res<=0 ){
73717       iRes = i1;
73718     }else{
73719       iRes = i2;
73720     }
73721   }
73722 
73723   pSorter->aTree[iOut] = iRes;
73724   return SQLITE_OK;
73725 }
73726 
73727 /*
73728 ** Initialize the temporary index cursor just opened as a sorter cursor.
73729 */
73730 SQLITE_PRIVATE int sqlite3VdbeSorterInit(sqlite3 *db, VdbeCursor *pCsr){
73731   int pgsz;                       /* Page size of main database */
73732   int mxCache;                    /* Cache size */
73733   VdbeSorter *pSorter;            /* The new sorter */
73734   char *d;                        /* Dummy */
73735 
73736   assert( pCsr->pKeyInfo && pCsr->pBt==0 );
73737   pCsr->pSorter = pSorter = sqlite3DbMallocZero(db, sizeof(VdbeSorter));
73738   if( pSorter==0 ){
73739     return SQLITE_NOMEM;
73740   }
73741 
73742   pSorter->pUnpacked = sqlite3VdbeAllocUnpackedRecord(pCsr->pKeyInfo, 0, 0, &d);
73743   if( pSorter->pUnpacked==0 ) return SQLITE_NOMEM;
73744   assert( pSorter->pUnpacked==(UnpackedRecord *)d );
73745 
73746   if( !sqlite3TempInMemory(db) ){
73747     pgsz = sqlite3BtreeGetPageSize(db->aDb[0].pBt);
73748     pSorter->mnPmaSize = SORTER_MIN_WORKING * pgsz;
73749     mxCache = db->aDb[0].pSchema->cache_size;
73750     if( mxCache<SORTER_MIN_WORKING ) mxCache = SORTER_MIN_WORKING;
73751     pSorter->mxPmaSize = mxCache * pgsz;
73752   }
73753 
73754   return SQLITE_OK;
73755 }
73756 
73757 /*
73758 ** Free the list of sorted records starting at pRecord.
73759 */
73760 static void vdbeSorterRecordFree(sqlite3 *db, SorterRecord *pRecord){
73761   SorterRecord *p;
73762   SorterRecord *pNext;
73763   for(p=pRecord; p; p=pNext){
73764     pNext = p->pNext;
73765     sqlite3DbFree(db, p);
73766   }
73767 }
73768 
73769 /*
73770 ** Free any cursor components allocated by sqlite3VdbeSorterXXX routines.
73771 */
73772 SQLITE_PRIVATE void sqlite3VdbeSorterClose(sqlite3 *db, VdbeCursor *pCsr){
73773   VdbeSorter *pSorter = pCsr->pSorter;
73774   if( pSorter ){
73775     if( pSorter->aIter ){
73776       int i;
73777       for(i=0; i<pSorter->nTree; i++){
73778         vdbeSorterIterZero(db, &pSorter->aIter[i]);
73779       }
73780       sqlite3DbFree(db, pSorter->aIter);
73781     }
73782     if( pSorter->pTemp1 ){
73783       sqlite3OsCloseFree(pSorter->pTemp1);
73784     }
73785     vdbeSorterRecordFree(db, pSorter->pRecord);
73786     sqlite3DbFree(db, pSorter->pUnpacked);
73787     sqlite3DbFree(db, pSorter);
73788     pCsr->pSorter = 0;
73789   }
73790 }
73791 
73792 /*
73793 ** Allocate space for a file-handle and open a temporary file. If successful,
73794 ** set *ppFile to point to the malloc'd file-handle and return SQLITE_OK.
73795 ** Otherwise, set *ppFile to 0 and return an SQLite error code.
73796 */
73797 static int vdbeSorterOpenTempFile(sqlite3 *db, sqlite3_file **ppFile){
73798   int dummy;
73799   return sqlite3OsOpenMalloc(db->pVfs, 0, ppFile,
73800       SQLITE_OPEN_TEMP_JOURNAL |
73801       SQLITE_OPEN_READWRITE    | SQLITE_OPEN_CREATE |
73802       SQLITE_OPEN_EXCLUSIVE    | SQLITE_OPEN_DELETEONCLOSE, &dummy
73803   );
73804 }
73805 
73806 /*
73807 ** Merge the two sorted lists p1 and p2 into a single list.
73808 ** Set *ppOut to the head of the new list.
73809 */
73810 static void vdbeSorterMerge(
73811   const VdbeCursor *pCsr,         /* For pKeyInfo */
73812   SorterRecord *p1,               /* First list to merge */
73813   SorterRecord *p2,               /* Second list to merge */
73814   SorterRecord **ppOut            /* OUT: Head of merged list */
73815 ){
73816   SorterRecord *pFinal = 0;
73817   SorterRecord **pp = &pFinal;
73818   void *pVal2 = p2 ? p2->pVal : 0;
73819 
73820   while( p1 && p2 ){
73821     int res;
73822     vdbeSorterCompare(pCsr, 0, p1->pVal, p1->nVal, pVal2, p2->nVal, &res);
73823     if( res<=0 ){
73824       *pp = p1;
73825       pp = &p1->pNext;
73826       p1 = p1->pNext;
73827       pVal2 = 0;
73828     }else{
73829       *pp = p2;
73830        pp = &p2->pNext;
73831       p2 = p2->pNext;
73832       if( p2==0 ) break;
73833       pVal2 = p2->pVal;
73834     }
73835   }
73836   *pp = p1 ? p1 : p2;
73837   *ppOut = pFinal;
73838 }
73839 
73840 /*
73841 ** Sort the linked list of records headed at pCsr->pRecord. Return SQLITE_OK
73842 ** if successful, or an SQLite error code (i.e. SQLITE_NOMEM) if an error
73843 ** occurs.
73844 */
73845 static int vdbeSorterSort(const VdbeCursor *pCsr){
73846   int i;
73847   SorterRecord **aSlot;
73848   SorterRecord *p;
73849   VdbeSorter *pSorter = pCsr->pSorter;
73850 
73851   aSlot = (SorterRecord **)sqlite3MallocZero(64 * sizeof(SorterRecord *));
73852   if( !aSlot ){
73853     return SQLITE_NOMEM;
73854   }
73855 
73856   p = pSorter->pRecord;
73857   while( p ){
73858     SorterRecord *pNext = p->pNext;
73859     p->pNext = 0;
73860     for(i=0; aSlot[i]; i++){
73861       vdbeSorterMerge(pCsr, p, aSlot[i], &p);
73862       aSlot[i] = 0;
73863     }
73864     aSlot[i] = p;
73865     p = pNext;
73866   }
73867 
73868   p = 0;
73869   for(i=0; i<64; i++){
73870     vdbeSorterMerge(pCsr, p, aSlot[i], &p);
73871   }
73872   pSorter->pRecord = p;
73873 
73874   sqlite3_free(aSlot);
73875   return SQLITE_OK;
73876 }
73877 
73878 /*
73879 ** Initialize a file-writer object.
73880 */
73881 static void fileWriterInit(
73882   sqlite3 *db,                    /* Database (for malloc) */
73883   sqlite3_file *pFile,            /* File to write to */
73884   FileWriter *p,                  /* Object to populate */
73885   i64 iStart                      /* Offset of pFile to begin writing at */
73886 ){
73887   int nBuf = sqlite3BtreeGetPageSize(db->aDb[0].pBt);
73888 
73889   memset(p, 0, sizeof(FileWriter));
73890   p->aBuffer = (u8 *)sqlite3DbMallocRaw(db, nBuf);
73891   if( !p->aBuffer ){
73892     p->eFWErr = SQLITE_NOMEM;
73893   }else{
73894     p->iBufEnd = p->iBufStart = (iStart % nBuf);
73895     p->iWriteOff = iStart - p->iBufStart;
73896     p->nBuffer = nBuf;
73897     p->pFile = pFile;
73898   }
73899 }
73900 
73901 /*
73902 ** Write nData bytes of data to the file-write object. Return SQLITE_OK
73903 ** if successful, or an SQLite error code if an error occurs.
73904 */
73905 static void fileWriterWrite(FileWriter *p, u8 *pData, int nData){
73906   int nRem = nData;
73907   while( nRem>0 && p->eFWErr==0 ){
73908     int nCopy = nRem;
73909     if( nCopy>(p->nBuffer - p->iBufEnd) ){
73910       nCopy = p->nBuffer - p->iBufEnd;
73911     }
73912 
73913     memcpy(&p->aBuffer[p->iBufEnd], &pData[nData-nRem], nCopy);
73914     p->iBufEnd += nCopy;
73915     if( p->iBufEnd==p->nBuffer ){
73916       p->eFWErr = sqlite3OsWrite(p->pFile,
73917           &p->aBuffer[p->iBufStart], p->iBufEnd - p->iBufStart,
73918           p->iWriteOff + p->iBufStart
73919       );
73920       p->iBufStart = p->iBufEnd = 0;
73921       p->iWriteOff += p->nBuffer;
73922     }
73923     assert( p->iBufEnd<p->nBuffer );
73924 
73925     nRem -= nCopy;
73926   }
73927 }
73928 
73929 /*
73930 ** Flush any buffered data to disk and clean up the file-writer object.
73931 ** The results of using the file-writer after this call are undefined.
73932 ** Return SQLITE_OK if flushing the buffered data succeeds or is not
73933 ** required. Otherwise, return an SQLite error code.
73934 **
73935 ** Before returning, set *piEof to the offset immediately following the
73936 ** last byte written to the file.
73937 */
73938 static int fileWriterFinish(sqlite3 *db, FileWriter *p, i64 *piEof){
73939   int rc;
73940   if( p->eFWErr==0 && ALWAYS(p->aBuffer) && p->iBufEnd>p->iBufStart ){
73941     p->eFWErr = sqlite3OsWrite(p->pFile,
73942         &p->aBuffer[p->iBufStart], p->iBufEnd - p->iBufStart,
73943         p->iWriteOff + p->iBufStart
73944     );
73945   }
73946   *piEof = (p->iWriteOff + p->iBufEnd);
73947   sqlite3DbFree(db, p->aBuffer);
73948   rc = p->eFWErr;
73949   memset(p, 0, sizeof(FileWriter));
73950   return rc;
73951 }
73952 
73953 /*
73954 ** Write value iVal encoded as a varint to the file-write object. Return
73955 ** SQLITE_OK if successful, or an SQLite error code if an error occurs.
73956 */
73957 static void fileWriterWriteVarint(FileWriter *p, u64 iVal){
73958   int nByte;
73959   u8 aByte[10];
73960   nByte = sqlite3PutVarint(aByte, iVal);
73961   fileWriterWrite(p, aByte, nByte);
73962 }
73963 
73964 /*
73965 ** Write the current contents of the in-memory linked-list to a PMA. Return
73966 ** SQLITE_OK if successful, or an SQLite error code otherwise.
73967 **
73968 ** The format of a PMA is:
73969 **
73970 **     * A varint. This varint contains the total number of bytes of content
73971 **       in the PMA (not including the varint itself).
73972 **
73973 **     * One or more records packed end-to-end in order of ascending keys.
73974 **       Each record consists of a varint followed by a blob of data (the
73975 **       key). The varint is the number of bytes in the blob of data.
73976 */
73977 static int vdbeSorterListToPMA(sqlite3 *db, const VdbeCursor *pCsr){
73978   int rc = SQLITE_OK;             /* Return code */
73979   VdbeSorter *pSorter = pCsr->pSorter;
73980   FileWriter writer;
73981 
73982   memset(&writer, 0, sizeof(FileWriter));
73983 
73984   if( pSorter->nInMemory==0 ){
73985     assert( pSorter->pRecord==0 );
73986     return rc;
73987   }
73988 
73989   rc = vdbeSorterSort(pCsr);
73990 
73991   /* If the first temporary PMA file has not been opened, open it now. */
73992   if( rc==SQLITE_OK && pSorter->pTemp1==0 ){
73993     rc = vdbeSorterOpenTempFile(db, &pSorter->pTemp1);
73994     assert( rc!=SQLITE_OK || pSorter->pTemp1 );
73995     assert( pSorter->iWriteOff==0 );
73996     assert( pSorter->nPMA==0 );
73997   }
73998 
73999   if( rc==SQLITE_OK ){
74000     SorterRecord *p;
74001     SorterRecord *pNext = 0;
74002 
74003     fileWriterInit(db, pSorter->pTemp1, &writer, pSorter->iWriteOff);
74004     pSorter->nPMA++;
74005     fileWriterWriteVarint(&writer, pSorter->nInMemory);
74006     for(p=pSorter->pRecord; p; p=pNext){
74007       pNext = p->pNext;
74008       fileWriterWriteVarint(&writer, p->nVal);
74009       fileWriterWrite(&writer, p->pVal, p->nVal);
74010       sqlite3DbFree(db, p);
74011     }
74012     pSorter->pRecord = p;
74013     rc = fileWriterFinish(db, &writer, &pSorter->iWriteOff);
74014   }
74015 
74016   return rc;
74017 }
74018 
74019 /*
74020 ** Add a record to the sorter.
74021 */
74022 SQLITE_PRIVATE int sqlite3VdbeSorterWrite(
74023   sqlite3 *db,                    /* Database handle */
74024   const VdbeCursor *pCsr,               /* Sorter cursor */
74025   Mem *pVal                       /* Memory cell containing record */
74026 ){
74027   VdbeSorter *pSorter = pCsr->pSorter;
74028   int rc = SQLITE_OK;             /* Return Code */
74029   SorterRecord *pNew;             /* New list element */
74030 
74031   assert( pSorter );
74032   pSorter->nInMemory += sqlite3VarintLen(pVal->n) + pVal->n;
74033 
74034   pNew = (SorterRecord *)sqlite3DbMallocRaw(db, pVal->n + sizeof(SorterRecord));
74035   if( pNew==0 ){
74036     rc = SQLITE_NOMEM;
74037   }else{
74038     pNew->pVal = (void *)&pNew[1];
74039     memcpy(pNew->pVal, pVal->z, pVal->n);
74040     pNew->nVal = pVal->n;
74041     pNew->pNext = pSorter->pRecord;
74042     pSorter->pRecord = pNew;
74043   }
74044 
74045   /* See if the contents of the sorter should now be written out. They
74046   ** are written out when either of the following are true:
74047   **
74048   **   * The total memory allocated for the in-memory list is greater
74049   **     than (page-size * cache-size), or
74050   **
74051   **   * The total memory allocated for the in-memory list is greater
74052   **     than (page-size * 10) and sqlite3HeapNearlyFull() returns true.
74053   */
74054   if( rc==SQLITE_OK && pSorter->mxPmaSize>0 && (
74055         (pSorter->nInMemory>pSorter->mxPmaSize)
74056      || (pSorter->nInMemory>pSorter->mnPmaSize && sqlite3HeapNearlyFull())
74057   )){
74058 #ifdef SQLITE_DEBUG
74059     i64 nExpect = pSorter->iWriteOff
74060                 + sqlite3VarintLen(pSorter->nInMemory)
74061                 + pSorter->nInMemory;
74062 #endif
74063     rc = vdbeSorterListToPMA(db, pCsr);
74064     pSorter->nInMemory = 0;
74065     assert( rc!=SQLITE_OK || (nExpect==pSorter->iWriteOff) );
74066   }
74067 
74068   return rc;
74069 }
74070 
74071 /*
74072 ** Helper function for sqlite3VdbeSorterRewind().
74073 */
74074 static int vdbeSorterInitMerge(
74075   sqlite3 *db,                    /* Database handle */
74076   const VdbeCursor *pCsr,         /* Cursor handle for this sorter */
74077   i64 *pnByte                     /* Sum of bytes in all opened PMAs */
74078 ){
74079   VdbeSorter *pSorter = pCsr->pSorter;
74080   int rc = SQLITE_OK;             /* Return code */
74081   int i;                          /* Used to iterator through aIter[] */
74082   i64 nByte = 0;                  /* Total bytes in all opened PMAs */
74083 
74084   /* Initialize the iterators. */
74085   for(i=0; i<SORTER_MAX_MERGE_COUNT; i++){
74086     VdbeSorterIter *pIter = &pSorter->aIter[i];
74087     rc = vdbeSorterIterInit(db, pSorter, pSorter->iReadOff, pIter, &nByte);
74088     pSorter->iReadOff = pIter->iEof;
74089     assert( rc!=SQLITE_OK || pSorter->iReadOff<=pSorter->iWriteOff );
74090     if( rc!=SQLITE_OK || pSorter->iReadOff>=pSorter->iWriteOff ) break;
74091   }
74092 
74093   /* Initialize the aTree[] array. */
74094   for(i=pSorter->nTree-1; rc==SQLITE_OK && i>0; i--){
74095     rc = vdbeSorterDoCompare(pCsr, i);
74096   }
74097 
74098   *pnByte = nByte;
74099   return rc;
74100 }
74101 
74102 /*
74103 ** Once the sorter has been populated, this function is called to prepare
74104 ** for iterating through its contents in sorted order.
74105 */
74106 SQLITE_PRIVATE int sqlite3VdbeSorterRewind(sqlite3 *db, const VdbeCursor *pCsr, int *pbEof){
74107   VdbeSorter *pSorter = pCsr->pSorter;
74108   int rc;                         /* Return code */
74109   sqlite3_file *pTemp2 = 0;       /* Second temp file to use */
74110   i64 iWrite2 = 0;                /* Write offset for pTemp2 */
74111   int nIter;                      /* Number of iterators used */
74112   int nByte;                      /* Bytes of space required for aIter/aTree */
74113   int N = 2;                      /* Power of 2 >= nIter */
74114 
74115   assert( pSorter );
74116 
74117   /* If no data has been written to disk, then do not do so now. Instead,
74118   ** sort the VdbeSorter.pRecord list. The vdbe layer will read data directly
74119   ** from the in-memory list.  */
74120   if( pSorter->nPMA==0 ){
74121     *pbEof = !pSorter->pRecord;
74122     assert( pSorter->aTree==0 );
74123     return vdbeSorterSort(pCsr);
74124   }
74125 
74126   /* Write the current in-memory list to a PMA. */
74127   rc = vdbeSorterListToPMA(db, pCsr);
74128   if( rc!=SQLITE_OK ) return rc;
74129 
74130   /* Allocate space for aIter[] and aTree[]. */
74131   nIter = pSorter->nPMA;
74132   if( nIter>SORTER_MAX_MERGE_COUNT ) nIter = SORTER_MAX_MERGE_COUNT;
74133   assert( nIter>0 );
74134   while( N<nIter ) N += N;
74135   nByte = N * (sizeof(int) + sizeof(VdbeSorterIter));
74136   pSorter->aIter = (VdbeSorterIter *)sqlite3DbMallocZero(db, nByte);
74137   if( !pSorter->aIter ) return SQLITE_NOMEM;
74138   pSorter->aTree = (int *)&pSorter->aIter[N];
74139   pSorter->nTree = N;
74140 
74141   do {
74142     int iNew;                     /* Index of new, merged, PMA */
74143 
74144     for(iNew=0;
74145         rc==SQLITE_OK && iNew*SORTER_MAX_MERGE_COUNT<pSorter->nPMA;
74146         iNew++
74147     ){
74148       int rc2;                    /* Return code from fileWriterFinish() */
74149       FileWriter writer;          /* Object used to write to disk */
74150       i64 nWrite;                 /* Number of bytes in new PMA */
74151 
74152       memset(&writer, 0, sizeof(FileWriter));
74153 
74154       /* If there are SORTER_MAX_MERGE_COUNT or less PMAs in file pTemp1,
74155       ** initialize an iterator for each of them and break out of the loop.
74156       ** These iterators will be incrementally merged as the VDBE layer calls
74157       ** sqlite3VdbeSorterNext().
74158       **
74159       ** Otherwise, if pTemp1 contains more than SORTER_MAX_MERGE_COUNT PMAs,
74160       ** initialize interators for SORTER_MAX_MERGE_COUNT of them. These PMAs
74161       ** are merged into a single PMA that is written to file pTemp2.
74162       */
74163       rc = vdbeSorterInitMerge(db, pCsr, &nWrite);
74164       assert( rc!=SQLITE_OK || pSorter->aIter[ pSorter->aTree[1] ].pFile );
74165       if( rc!=SQLITE_OK || pSorter->nPMA<=SORTER_MAX_MERGE_COUNT ){
74166         break;
74167       }
74168 
74169       /* Open the second temp file, if it is not already open. */
74170       if( pTemp2==0 ){
74171         assert( iWrite2==0 );
74172         rc = vdbeSorterOpenTempFile(db, &pTemp2);
74173       }
74174 
74175       if( rc==SQLITE_OK ){
74176         int bEof = 0;
74177         fileWriterInit(db, pTemp2, &writer, iWrite2);
74178         fileWriterWriteVarint(&writer, nWrite);
74179         while( rc==SQLITE_OK && bEof==0 ){
74180           VdbeSorterIter *pIter = &pSorter->aIter[ pSorter->aTree[1] ];
74181           assert( pIter->pFile );
74182 
74183           fileWriterWriteVarint(&writer, pIter->nKey);
74184           fileWriterWrite(&writer, pIter->aKey, pIter->nKey);
74185           rc = sqlite3VdbeSorterNext(db, pCsr, &bEof);
74186         }
74187         rc2 = fileWriterFinish(db, &writer, &iWrite2);
74188         if( rc==SQLITE_OK ) rc = rc2;
74189       }
74190     }
74191 
74192     if( pSorter->nPMA<=SORTER_MAX_MERGE_COUNT ){
74193       break;
74194     }else{
74195       sqlite3_file *pTmp = pSorter->pTemp1;
74196       pSorter->nPMA = iNew;
74197       pSorter->pTemp1 = pTemp2;
74198       pTemp2 = pTmp;
74199       pSorter->iWriteOff = iWrite2;
74200       pSorter->iReadOff = 0;
74201       iWrite2 = 0;
74202     }
74203   }while( rc==SQLITE_OK );
74204 
74205   if( pTemp2 ){
74206     sqlite3OsCloseFree(pTemp2);
74207   }
74208   *pbEof = (pSorter->aIter[pSorter->aTree[1]].pFile==0);
74209   return rc;
74210 }
74211 
74212 /*
74213 ** Advance to the next element in the sorter.
74214 */
74215 SQLITE_PRIVATE int sqlite3VdbeSorterNext(sqlite3 *db, const VdbeCursor *pCsr, int *pbEof){
74216   VdbeSorter *pSorter = pCsr->pSorter;
74217   int rc;                         /* Return code */
74218 
74219   if( pSorter->aTree ){
74220     int iPrev = pSorter->aTree[1];/* Index of iterator to advance */
74221     int i;                        /* Index of aTree[] to recalculate */
74222 
74223     rc = vdbeSorterIterNext(db, &pSorter->aIter[iPrev]);
74224     for(i=(pSorter->nTree+iPrev)/2; rc==SQLITE_OK && i>0; i=i/2){
74225       rc = vdbeSorterDoCompare(pCsr, i);
74226     }
74227 
74228     *pbEof = (pSorter->aIter[pSorter->aTree[1]].pFile==0);
74229   }else{
74230     SorterRecord *pFree = pSorter->pRecord;
74231     pSorter->pRecord = pFree->pNext;
74232     pFree->pNext = 0;
74233     vdbeSorterRecordFree(db, pFree);
74234     *pbEof = !pSorter->pRecord;
74235     rc = SQLITE_OK;
74236   }
74237   return rc;
74238 }
74239 
74240 /*
74241 ** Return a pointer to a buffer owned by the sorter that contains the
74242 ** current key.
74243 */
74244 static void *vdbeSorterRowkey(
74245   const VdbeSorter *pSorter,      /* Sorter object */
74246   int *pnKey                      /* OUT: Size of current key in bytes */
74247 ){
74248   void *pKey;
74249   if( pSorter->aTree ){
74250     VdbeSorterIter *pIter;
74251     pIter = &pSorter->aIter[ pSorter->aTree[1] ];
74252     *pnKey = pIter->nKey;
74253     pKey = pIter->aKey;
74254   }else{
74255     *pnKey = pSorter->pRecord->nVal;
74256     pKey = pSorter->pRecord->pVal;
74257   }
74258   return pKey;
74259 }
74260 
74261 /*
74262 ** Copy the current sorter key into the memory cell pOut.
74263 */
74264 SQLITE_PRIVATE int sqlite3VdbeSorterRowkey(const VdbeCursor *pCsr, Mem *pOut){
74265   VdbeSorter *pSorter = pCsr->pSorter;
74266   void *pKey; int nKey;           /* Sorter key to copy into pOut */
74267 
74268   pKey = vdbeSorterRowkey(pSorter, &nKey);
74269   if( sqlite3VdbeMemGrow(pOut, nKey, 0) ){
74270     return SQLITE_NOMEM;
74271   }
74272   pOut->n = nKey;
74273   MemSetTypeFlag(pOut, MEM_Blob);
74274   memcpy(pOut->z, pKey, nKey);
74275 
74276   return SQLITE_OK;
74277 }
74278 
74279 /*
74280 ** Compare the key in memory cell pVal with the key that the sorter cursor
74281 ** passed as the first argument currently points to. For the purposes of
74282 ** the comparison, ignore the rowid field at the end of each record.
74283 **
74284 ** If an error occurs, return an SQLite error code (i.e. SQLITE_NOMEM).
74285 ** Otherwise, set *pRes to a negative, zero or positive value if the
74286 ** key in pVal is smaller than, equal to or larger than the current sorter
74287 ** key.
74288 */
74289 SQLITE_PRIVATE int sqlite3VdbeSorterCompare(
74290   const VdbeCursor *pCsr,         /* Sorter cursor */
74291   Mem *pVal,                      /* Value to compare to current sorter key */
74292   int nIgnore,                    /* Ignore this many fields at the end */
74293   int *pRes                       /* OUT: Result of comparison */
74294 ){
74295   VdbeSorter *pSorter = pCsr->pSorter;
74296   void *pKey; int nKey;           /* Sorter key to compare pVal with */
74297 
74298   pKey = vdbeSorterRowkey(pSorter, &nKey);
74299   vdbeSorterCompare(pCsr, nIgnore, pVal->z, pVal->n, pKey, nKey, pRes);
74300   return SQLITE_OK;
74301 }
74302 
74303 /************** End of vdbesort.c ********************************************/
74304 /************** Begin file journal.c *****************************************/
74305 /*
74306 ** 2007 August 22
74307 **
74308 ** The author disclaims copyright to this source code.  In place of
74309 ** a legal notice, here is a blessing:
74310 **
74311 **    May you do good and not evil.
74312 **    May you find forgiveness for yourself and forgive others.
74313 **    May you share freely, never taking more than you give.
74314 **
74315 *************************************************************************
74316 **
74317 ** This file implements a special kind of sqlite3_file object used
74318 ** by SQLite to create journal files if the atomic-write optimization
74319 ** is enabled.
74320 **
74321 ** The distinctive characteristic of this sqlite3_file is that the
74322 ** actual on disk file is created lazily. When the file is created,
74323 ** the caller specifies a buffer size for an in-memory buffer to
74324 ** be used to service read() and write() requests. The actual file
74325 ** on disk is not created or populated until either:
74326 **
74327 **   1) The in-memory representation grows too large for the allocated
74328 **      buffer, or
74329 **   2) The sqlite3JournalCreate() function is called.
74330 */
74331 #ifdef SQLITE_ENABLE_ATOMIC_WRITE
74332 
74333 
74334 /*
74335 ** A JournalFile object is a subclass of sqlite3_file used by
74336 ** as an open file handle for journal files.
74337 */
74338 struct JournalFile {
74339   sqlite3_io_methods *pMethod;    /* I/O methods on journal files */
74340   int nBuf;                       /* Size of zBuf[] in bytes */
74341   char *zBuf;                     /* Space to buffer journal writes */
74342   int iSize;                      /* Amount of zBuf[] currently used */
74343   int flags;                      /* xOpen flags */
74344   sqlite3_vfs *pVfs;              /* The "real" underlying VFS */
74345   sqlite3_file *pReal;            /* The "real" underlying file descriptor */
74346   const char *zJournal;           /* Name of the journal file */
74347 };
74348 typedef struct JournalFile JournalFile;
74349 
74350 /*
74351 ** If it does not already exists, create and populate the on-disk file
74352 ** for JournalFile p.
74353 */
74354 static int createFile(JournalFile *p){
74355   int rc = SQLITE_OK;
74356   if( !p->pReal ){
74357     sqlite3_file *pReal = (sqlite3_file *)&p[1];
74358     rc = sqlite3OsOpen(p->pVfs, p->zJournal, pReal, p->flags, 0);
74359     if( rc==SQLITE_OK ){
74360       p->pReal = pReal;
74361       if( p->iSize>0 ){
74362         assert(p->iSize<=p->nBuf);
74363         rc = sqlite3OsWrite(p->pReal, p->zBuf, p->iSize, 0);
74364       }
74365       if( rc!=SQLITE_OK ){
74366         /* If an error occurred while writing to the file, close it before
74367         ** returning. This way, SQLite uses the in-memory journal data to
74368         ** roll back changes made to the internal page-cache before this
74369         ** function was called.  */
74370         sqlite3OsClose(pReal);
74371         p->pReal = 0;
74372       }
74373     }
74374   }
74375   return rc;
74376 }
74377 
74378 /*
74379 ** Close the file.
74380 */
74381 static int jrnlClose(sqlite3_file *pJfd){
74382   JournalFile *p = (JournalFile *)pJfd;
74383   if( p->pReal ){
74384     sqlite3OsClose(p->pReal);
74385   }
74386   sqlite3_free(p->zBuf);
74387   return SQLITE_OK;
74388 }
74389 
74390 /*
74391 ** Read data from the file.
74392 */
74393 static int jrnlRead(
74394   sqlite3_file *pJfd,    /* The journal file from which to read */
74395   void *zBuf,            /* Put the results here */
74396   int iAmt,              /* Number of bytes to read */
74397   sqlite_int64 iOfst     /* Begin reading at this offset */
74398 ){
74399   int rc = SQLITE_OK;
74400   JournalFile *p = (JournalFile *)pJfd;
74401   if( p->pReal ){
74402     rc = sqlite3OsRead(p->pReal, zBuf, iAmt, iOfst);
74403   }else if( (iAmt+iOfst)>p->iSize ){
74404     rc = SQLITE_IOERR_SHORT_READ;
74405   }else{
74406     memcpy(zBuf, &p->zBuf[iOfst], iAmt);
74407   }
74408   return rc;
74409 }
74410 
74411 /*
74412 ** Write data to the file.
74413 */
74414 static int jrnlWrite(
74415   sqlite3_file *pJfd,    /* The journal file into which to write */
74416   const void *zBuf,      /* Take data to be written from here */
74417   int iAmt,              /* Number of bytes to write */
74418   sqlite_int64 iOfst     /* Begin writing at this offset into the file */
74419 ){
74420   int rc = SQLITE_OK;
74421   JournalFile *p = (JournalFile *)pJfd;
74422   if( !p->pReal && (iOfst+iAmt)>p->nBuf ){
74423     rc = createFile(p);
74424   }
74425   if( rc==SQLITE_OK ){
74426     if( p->pReal ){
74427       rc = sqlite3OsWrite(p->pReal, zBuf, iAmt, iOfst);
74428     }else{
74429       memcpy(&p->zBuf[iOfst], zBuf, iAmt);
74430       if( p->iSize<(iOfst+iAmt) ){
74431         p->iSize = (iOfst+iAmt);
74432       }
74433     }
74434   }
74435   return rc;
74436 }
74437 
74438 /*
74439 ** Truncate the file.
74440 */
74441 static int jrnlTruncate(sqlite3_file *pJfd, sqlite_int64 size){
74442   int rc = SQLITE_OK;
74443   JournalFile *p = (JournalFile *)pJfd;
74444   if( p->pReal ){
74445     rc = sqlite3OsTruncate(p->pReal, size);
74446   }else if( size<p->iSize ){
74447     p->iSize = size;
74448   }
74449   return rc;
74450 }
74451 
74452 /*
74453 ** Sync the file.
74454 */
74455 static int jrnlSync(sqlite3_file *pJfd, int flags){
74456   int rc;
74457   JournalFile *p = (JournalFile *)pJfd;
74458   if( p->pReal ){
74459     rc = sqlite3OsSync(p->pReal, flags);
74460   }else{
74461     rc = SQLITE_OK;
74462   }
74463   return rc;
74464 }
74465 
74466 /*
74467 ** Query the size of the file in bytes.
74468 */
74469 static int jrnlFileSize(sqlite3_file *pJfd, sqlite_int64 *pSize){
74470   int rc = SQLITE_OK;
74471   JournalFile *p = (JournalFile *)pJfd;
74472   if( p->pReal ){
74473     rc = sqlite3OsFileSize(p->pReal, pSize);
74474   }else{
74475     *pSize = (sqlite_int64) p->iSize;
74476   }
74477   return rc;
74478 }
74479 
74480 /*
74481 ** Table of methods for JournalFile sqlite3_file object.
74482 */
74483 static struct sqlite3_io_methods JournalFileMethods = {
74484   1,             /* iVersion */
74485   jrnlClose,     /* xClose */
74486   jrnlRead,      /* xRead */
74487   jrnlWrite,     /* xWrite */
74488   jrnlTruncate,  /* xTruncate */
74489   jrnlSync,      /* xSync */
74490   jrnlFileSize,  /* xFileSize */
74491   0,             /* xLock */
74492   0,             /* xUnlock */
74493   0,             /* xCheckReservedLock */
74494   0,             /* xFileControl */
74495   0,             /* xSectorSize */
74496   0,             /* xDeviceCharacteristics */
74497   0,             /* xShmMap */
74498   0,             /* xShmLock */
74499   0,             /* xShmBarrier */
74500   0              /* xShmUnmap */
74501 };
74502 
74503 /*
74504 ** Open a journal file.
74505 */
74506 SQLITE_PRIVATE int sqlite3JournalOpen(
74507   sqlite3_vfs *pVfs,         /* The VFS to use for actual file I/O */
74508   const char *zName,         /* Name of the journal file */
74509   sqlite3_file *pJfd,        /* Preallocated, blank file handle */
74510   int flags,                 /* Opening flags */
74511   int nBuf                   /* Bytes buffered before opening the file */
74512 ){
74513   JournalFile *p = (JournalFile *)pJfd;
74514   memset(p, 0, sqlite3JournalSize(pVfs));
74515   if( nBuf>0 ){
74516     p->zBuf = sqlite3MallocZero(nBuf);
74517     if( !p->zBuf ){
74518       return SQLITE_NOMEM;
74519     }
74520   }else{
74521     return sqlite3OsOpen(pVfs, zName, pJfd, flags, 0);
74522   }
74523   p->pMethod = &JournalFileMethods;
74524   p->nBuf = nBuf;
74525   p->flags = flags;
74526   p->zJournal = zName;
74527   p->pVfs = pVfs;
74528   return SQLITE_OK;
74529 }
74530 
74531 /*
74532 ** If the argument p points to a JournalFile structure, and the underlying
74533 ** file has not yet been created, create it now.
74534 */
74535 SQLITE_PRIVATE int sqlite3JournalCreate(sqlite3_file *p){
74536   if( p->pMethods!=&JournalFileMethods ){
74537     return SQLITE_OK;
74538   }
74539   return createFile((JournalFile *)p);
74540 }
74541 
74542 /*
74543 ** The file-handle passed as the only argument is guaranteed to be an open
74544 ** file. It may or may not be of class JournalFile. If the file is a
74545 ** JournalFile, and the underlying file on disk has not yet been opened,
74546 ** return 0. Otherwise, return 1.
74547 */
74548 SQLITE_PRIVATE int sqlite3JournalExists(sqlite3_file *p){
74549   return (p->pMethods!=&JournalFileMethods || ((JournalFile *)p)->pReal!=0);
74550 }
74551 
74552 /*
74553 ** Return the number of bytes required to store a JournalFile that uses vfs
74554 ** pVfs to create the underlying on-disk files.
74555 */
74556 SQLITE_PRIVATE int sqlite3JournalSize(sqlite3_vfs *pVfs){
74557   return (pVfs->szOsFile+sizeof(JournalFile));
74558 }
74559 #endif
74560 
74561 /************** End of journal.c *********************************************/
74562 /************** Begin file memjournal.c **************************************/
74563 /*
74564 ** 2008 October 7
74565 **
74566 ** The author disclaims copyright to this source code.  In place of
74567 ** a legal notice, here is a blessing:
74568 **
74569 **    May you do good and not evil.
74570 **    May you find forgiveness for yourself and forgive others.
74571 **    May you share freely, never taking more than you give.
74572 **
74573 *************************************************************************
74574 **
74575 ** This file contains code use to implement an in-memory rollback journal.
74576 ** The in-memory rollback journal is used to journal transactions for
74577 ** ":memory:" databases and when the journal_mode=MEMORY pragma is used.
74578 */
74579 
74580 /* Forward references to internal structures */
74581 typedef struct MemJournal MemJournal;
74582 typedef struct FilePoint FilePoint;
74583 typedef struct FileChunk FileChunk;
74584 
74585 /* Space to hold the rollback journal is allocated in increments of
74586 ** this many bytes.
74587 **
74588 ** The size chosen is a little less than a power of two.  That way,
74589 ** the FileChunk object will have a size that almost exactly fills
74590 ** a power-of-two allocation.  This mimimizes wasted space in power-of-two
74591 ** memory allocators.
74592 */
74593 #define JOURNAL_CHUNKSIZE ((int)(1024-sizeof(FileChunk*)))
74594 
74595 /*
74596 ** The rollback journal is composed of a linked list of these structures.
74597 */
74598 struct FileChunk {
74599   FileChunk *pNext;               /* Next chunk in the journal */
74600   u8 zChunk[JOURNAL_CHUNKSIZE];   /* Content of this chunk */
74601 };
74602 
74603 /*
74604 ** An instance of this object serves as a cursor into the rollback journal.
74605 ** The cursor can be either for reading or writing.
74606 */
74607 struct FilePoint {
74608   sqlite3_int64 iOffset;          /* Offset from the beginning of the file */
74609   FileChunk *pChunk;              /* Specific chunk into which cursor points */
74610 };
74611 
74612 /*
74613 ** This subclass is a subclass of sqlite3_file.  Each open memory-journal
74614 ** is an instance of this class.
74615 */
74616 struct MemJournal {
74617   sqlite3_io_methods *pMethod;    /* Parent class. MUST BE FIRST */
74618   FileChunk *pFirst;              /* Head of in-memory chunk-list */
74619   FilePoint endpoint;             /* Pointer to the end of the file */
74620   FilePoint readpoint;            /* Pointer to the end of the last xRead() */
74621 };
74622 
74623 /*
74624 ** Read data from the in-memory journal file.  This is the implementation
74625 ** of the sqlite3_vfs.xRead method.
74626 */
74627 static int memjrnlRead(
74628   sqlite3_file *pJfd,    /* The journal file from which to read */
74629   void *zBuf,            /* Put the results here */
74630   int iAmt,              /* Number of bytes to read */
74631   sqlite_int64 iOfst     /* Begin reading at this offset */
74632 ){
74633   MemJournal *p = (MemJournal *)pJfd;
74634   u8 *zOut = zBuf;
74635   int nRead = iAmt;
74636   int iChunkOffset;
74637   FileChunk *pChunk;
74638 
74639   /* SQLite never tries to read past the end of a rollback journal file */
74640   assert( iOfst+iAmt<=p->endpoint.iOffset );
74641 
74642   if( p->readpoint.iOffset!=iOfst || iOfst==0 ){
74643     sqlite3_int64 iOff = 0;
74644     for(pChunk=p->pFirst;
74645         ALWAYS(pChunk) && (iOff+JOURNAL_CHUNKSIZE)<=iOfst;
74646         pChunk=pChunk->pNext
74647     ){
74648       iOff += JOURNAL_CHUNKSIZE;
74649     }
74650   }else{
74651     pChunk = p->readpoint.pChunk;
74652   }
74653 
74654   iChunkOffset = (int)(iOfst%JOURNAL_CHUNKSIZE);
74655   do {
74656     int iSpace = JOURNAL_CHUNKSIZE - iChunkOffset;
74657     int nCopy = MIN(nRead, (JOURNAL_CHUNKSIZE - iChunkOffset));
74658     memcpy(zOut, &pChunk->zChunk[iChunkOffset], nCopy);
74659     zOut += nCopy;
74660     nRead -= iSpace;
74661     iChunkOffset = 0;
74662   } while( nRead>=0 && (pChunk=pChunk->pNext)!=0 && nRead>0 );
74663   p->readpoint.iOffset = iOfst+iAmt;
74664   p->readpoint.pChunk = pChunk;
74665 
74666   return SQLITE_OK;
74667 }
74668 
74669 /*
74670 ** Write data to the file.
74671 */
74672 static int memjrnlWrite(
74673   sqlite3_file *pJfd,    /* The journal file into which to write */
74674   const void *zBuf,      /* Take data to be written from here */
74675   int iAmt,              /* Number of bytes to write */
74676   sqlite_int64 iOfst     /* Begin writing at this offset into the file */
74677 ){
74678   MemJournal *p = (MemJournal *)pJfd;
74679   int nWrite = iAmt;
74680   u8 *zWrite = (u8 *)zBuf;
74681 
74682   /* An in-memory journal file should only ever be appended to. Random
74683   ** access writes are not required by sqlite.
74684   */
74685   assert( iOfst==p->endpoint.iOffset );
74686   UNUSED_PARAMETER(iOfst);
74687 
74688   while( nWrite>0 ){
74689     FileChunk *pChunk = p->endpoint.pChunk;
74690     int iChunkOffset = (int)(p->endpoint.iOffset%JOURNAL_CHUNKSIZE);
74691     int iSpace = MIN(nWrite, JOURNAL_CHUNKSIZE - iChunkOffset);
74692 
74693     if( iChunkOffset==0 ){
74694       /* New chunk is required to extend the file. */
74695       FileChunk *pNew = sqlite3_malloc(sizeof(FileChunk));
74696       if( !pNew ){
74697         return SQLITE_IOERR_NOMEM;
74698       }
74699       pNew->pNext = 0;
74700       if( pChunk ){
74701         assert( p->pFirst );
74702         pChunk->pNext = pNew;
74703       }else{
74704         assert( !p->pFirst );
74705         p->pFirst = pNew;
74706       }
74707       p->endpoint.pChunk = pNew;
74708     }
74709 
74710     memcpy(&p->endpoint.pChunk->zChunk[iChunkOffset], zWrite, iSpace);
74711     zWrite += iSpace;
74712     nWrite -= iSpace;
74713     p->endpoint.iOffset += iSpace;
74714   }
74715 
74716   return SQLITE_OK;
74717 }
74718 
74719 /*
74720 ** Truncate the file.
74721 */
74722 static int memjrnlTruncate(sqlite3_file *pJfd, sqlite_int64 size){
74723   MemJournal *p = (MemJournal *)pJfd;
74724   FileChunk *pChunk;
74725   assert(size==0);
74726   UNUSED_PARAMETER(size);
74727   pChunk = p->pFirst;
74728   while( pChunk ){
74729     FileChunk *pTmp = pChunk;
74730     pChunk = pChunk->pNext;
74731     sqlite3_free(pTmp);
74732   }
74733   sqlite3MemJournalOpen(pJfd);
74734   return SQLITE_OK;
74735 }
74736 
74737 /*
74738 ** Close the file.
74739 */
74740 static int memjrnlClose(sqlite3_file *pJfd){
74741   memjrnlTruncate(pJfd, 0);
74742   return SQLITE_OK;
74743 }
74744 
74745 
74746 /*
74747 ** Sync the file.
74748 **
74749 ** Syncing an in-memory journal is a no-op.  And, in fact, this routine
74750 ** is never called in a working implementation.  This implementation
74751 ** exists purely as a contingency, in case some malfunction in some other
74752 ** part of SQLite causes Sync to be called by mistake.
74753 */
74754 static int memjrnlSync(sqlite3_file *NotUsed, int NotUsed2){
74755   UNUSED_PARAMETER2(NotUsed, NotUsed2);
74756   return SQLITE_OK;
74757 }
74758 
74759 /*
74760 ** Query the size of the file in bytes.
74761 */
74762 static int memjrnlFileSize(sqlite3_file *pJfd, sqlite_int64 *pSize){
74763   MemJournal *p = (MemJournal *)pJfd;
74764   *pSize = (sqlite_int64) p->endpoint.iOffset;
74765   return SQLITE_OK;
74766 }
74767 
74768 /*
74769 ** Table of methods for MemJournal sqlite3_file object.
74770 */
74771 static const struct sqlite3_io_methods MemJournalMethods = {
74772   1,                /* iVersion */
74773   memjrnlClose,     /* xClose */
74774   memjrnlRead,      /* xRead */
74775   memjrnlWrite,     /* xWrite */
74776   memjrnlTruncate,  /* xTruncate */
74777   memjrnlSync,      /* xSync */
74778   memjrnlFileSize,  /* xFileSize */
74779   0,                /* xLock */
74780   0,                /* xUnlock */
74781   0,                /* xCheckReservedLock */
74782   0,                /* xFileControl */
74783   0,                /* xSectorSize */
74784   0,                /* xDeviceCharacteristics */
74785   0,                /* xShmMap */
74786   0,                /* xShmLock */
74787   0,                /* xShmBarrier */
74788   0,                /* xShmUnmap */
74789   0,                /* xFetch */
74790   0                 /* xUnfetch */
74791 };
74792 
74793 /*
74794 ** Open a journal file.
74795 */
74796 SQLITE_PRIVATE void sqlite3MemJournalOpen(sqlite3_file *pJfd){
74797   MemJournal *p = (MemJournal *)pJfd;
74798   assert( EIGHT_BYTE_ALIGNMENT(p) );
74799   memset(p, 0, sqlite3MemJournalSize());
74800   p->pMethod = (sqlite3_io_methods*)&MemJournalMethods;
74801 }
74802 
74803 /*
74804 ** Return true if the file-handle passed as an argument is
74805 ** an in-memory journal
74806 */
74807 SQLITE_PRIVATE int sqlite3IsMemJournal(sqlite3_file *pJfd){
74808   return pJfd->pMethods==&MemJournalMethods;
74809 }
74810 
74811 /*
74812 ** Return the number of bytes required to store a MemJournal file descriptor.
74813 */
74814 SQLITE_PRIVATE int sqlite3MemJournalSize(void){
74815   return sizeof(MemJournal);
74816 }
74817 
74818 /************** End of memjournal.c ******************************************/
74819 /************** Begin file walker.c ******************************************/
74820 /*
74821 ** 2008 August 16
74822 **
74823 ** The author disclaims copyright to this source code.  In place of
74824 ** a legal notice, here is a blessing:
74825 **
74826 **    May you do good and not evil.
74827 **    May you find forgiveness for yourself and forgive others.
74828 **    May you share freely, never taking more than you give.
74829 **
74830 *************************************************************************
74831 ** This file contains routines used for walking the parser tree for
74832 ** an SQL statement.
74833 */
74834 /* #include <stdlib.h> */
74835 /* #include <string.h> */
74836 
74837 
74838 /*
74839 ** Walk an expression tree.  Invoke the callback once for each node
74840 ** of the expression, while decending.  (In other words, the callback
74841 ** is invoked before visiting children.)
74842 **
74843 ** The return value from the callback should be one of the WRC_*
74844 ** constants to specify how to proceed with the walk.
74845 **
74846 **    WRC_Continue      Continue descending down the tree.
74847 **
74848 **    WRC_Prune         Do not descend into child nodes.  But allow
74849 **                      the walk to continue with sibling nodes.
74850 **
74851 **    WRC_Abort         Do no more callbacks.  Unwind the stack and
74852 **                      return the top-level walk call.
74853 **
74854 ** The return value from this routine is WRC_Abort to abandon the tree walk
74855 ** and WRC_Continue to continue.
74856 */
74857 SQLITE_PRIVATE int sqlite3WalkExpr(Walker *pWalker, Expr *pExpr){
74858   int rc;
74859   if( pExpr==0 ) return WRC_Continue;
74860   testcase( ExprHasProperty(pExpr, EP_TokenOnly) );
74861   testcase( ExprHasProperty(pExpr, EP_Reduced) );
74862   rc = pWalker->xExprCallback(pWalker, pExpr);
74863   if( rc==WRC_Continue
74864               && !ExprHasProperty(pExpr,EP_TokenOnly) ){
74865     if( sqlite3WalkExpr(pWalker, pExpr->pLeft) ) return WRC_Abort;
74866     if( sqlite3WalkExpr(pWalker, pExpr->pRight) ) return WRC_Abort;
74867     if( ExprHasProperty(pExpr, EP_xIsSelect) ){
74868       if( sqlite3WalkSelect(pWalker, pExpr->x.pSelect) ) return WRC_Abort;
74869     }else{
74870       if( sqlite3WalkExprList(pWalker, pExpr->x.pList) ) return WRC_Abort;
74871     }
74872   }
74873   return rc & WRC_Abort;
74874 }
74875 
74876 /*
74877 ** Call sqlite3WalkExpr() for every expression in list p or until
74878 ** an abort request is seen.
74879 */
74880 SQLITE_PRIVATE int sqlite3WalkExprList(Walker *pWalker, ExprList *p){
74881   int i;
74882   struct ExprList_item *pItem;
74883   if( p ){
74884     for(i=p->nExpr, pItem=p->a; i>0; i--, pItem++){
74885       if( sqlite3WalkExpr(pWalker, pItem->pExpr) ) return WRC_Abort;
74886     }
74887   }
74888   return WRC_Continue;
74889 }
74890 
74891 /*
74892 ** Walk all expressions associated with SELECT statement p.  Do
74893 ** not invoke the SELECT callback on p, but do (of course) invoke
74894 ** any expr callbacks and SELECT callbacks that come from subqueries.
74895 ** Return WRC_Abort or WRC_Continue.
74896 */
74897 SQLITE_PRIVATE int sqlite3WalkSelectExpr(Walker *pWalker, Select *p){
74898   if( sqlite3WalkExprList(pWalker, p->pEList) ) return WRC_Abort;
74899   if( sqlite3WalkExpr(pWalker, p->pWhere) ) return WRC_Abort;
74900   if( sqlite3WalkExprList(pWalker, p->pGroupBy) ) return WRC_Abort;
74901   if( sqlite3WalkExpr(pWalker, p->pHaving) ) return WRC_Abort;
74902   if( sqlite3WalkExprList(pWalker, p->pOrderBy) ) return WRC_Abort;
74903   if( sqlite3WalkExpr(pWalker, p->pLimit) ) return WRC_Abort;
74904   if( sqlite3WalkExpr(pWalker, p->pOffset) ) return WRC_Abort;
74905   return WRC_Continue;
74906 }
74907 
74908 /*
74909 ** Walk the parse trees associated with all subqueries in the
74910 ** FROM clause of SELECT statement p.  Do not invoke the select
74911 ** callback on p, but do invoke it on each FROM clause subquery
74912 ** and on any subqueries further down in the tree.  Return
74913 ** WRC_Abort or WRC_Continue;
74914 */
74915 SQLITE_PRIVATE int sqlite3WalkSelectFrom(Walker *pWalker, Select *p){
74916   SrcList *pSrc;
74917   int i;
74918   struct SrcList_item *pItem;
74919 
74920   pSrc = p->pSrc;
74921   if( ALWAYS(pSrc) ){
74922     for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){
74923       if( sqlite3WalkSelect(pWalker, pItem->pSelect) ){
74924         return WRC_Abort;
74925       }
74926     }
74927   }
74928   return WRC_Continue;
74929 }
74930 
74931 /*
74932 ** Call sqlite3WalkExpr() for every expression in Select statement p.
74933 ** Invoke sqlite3WalkSelect() for subqueries in the FROM clause and
74934 ** on the compound select chain, p->pPrior.
74935 **
74936 ** If it is not NULL, the xSelectCallback() callback is invoked before
74937 ** the walk of the expressions and FROM clause. The xSelectCallback2()
74938 ** method, if it is not NULL, is invoked following the walk of the
74939 ** expressions and FROM clause.
74940 **
74941 ** Return WRC_Continue under normal conditions.  Return WRC_Abort if
74942 ** there is an abort request.
74943 **
74944 ** If the Walker does not have an xSelectCallback() then this routine
74945 ** is a no-op returning WRC_Continue.
74946 */
74947 SQLITE_PRIVATE int sqlite3WalkSelect(Walker *pWalker, Select *p){
74948   int rc;
74949   if( p==0 || (pWalker->xSelectCallback==0 && pWalker->xSelectCallback2==0) ){
74950     return WRC_Continue;
74951   }
74952   rc = WRC_Continue;
74953   pWalker->walkerDepth++;
74954   while( p ){
74955     if( pWalker->xSelectCallback ){
74956        rc = pWalker->xSelectCallback(pWalker, p);
74957        if( rc ) break;
74958     }
74959     if( sqlite3WalkSelectExpr(pWalker, p)
74960      || sqlite3WalkSelectFrom(pWalker, p)
74961     ){
74962       pWalker->walkerDepth--;
74963       return WRC_Abort;
74964     }
74965     if( pWalker->xSelectCallback2 ){
74966       pWalker->xSelectCallback2(pWalker, p);
74967     }
74968     p = p->pPrior;
74969   }
74970   pWalker->walkerDepth--;
74971   return rc & WRC_Abort;
74972 }
74973 
74974 /************** End of walker.c **********************************************/
74975 /************** Begin file resolve.c *****************************************/
74976 /*
74977 ** 2008 August 18
74978 **
74979 ** The author disclaims copyright to this source code.  In place of
74980 ** a legal notice, here is a blessing:
74981 **
74982 **    May you do good and not evil.
74983 **    May you find forgiveness for yourself and forgive others.
74984 **    May you share freely, never taking more than you give.
74985 **
74986 *************************************************************************
74987 **
74988 ** This file contains routines used for walking the parser tree and
74989 ** resolve all identifiers by associating them with a particular
74990 ** table and column.
74991 */
74992 /* #include <stdlib.h> */
74993 /* #include <string.h> */
74994 
74995 /*
74996 ** Walk the expression tree pExpr and increase the aggregate function
74997 ** depth (the Expr.op2 field) by N on every TK_AGG_FUNCTION node.
74998 ** This needs to occur when copying a TK_AGG_FUNCTION node from an
74999 ** outer query into an inner subquery.
75000 **
75001 ** incrAggFunctionDepth(pExpr,n) is the main routine.  incrAggDepth(..)
75002 ** is a helper function - a callback for the tree walker.
75003 */
75004 static int incrAggDepth(Walker *pWalker, Expr *pExpr){
75005   if( pExpr->op==TK_AGG_FUNCTION ) pExpr->op2 += pWalker->u.i;
75006   return WRC_Continue;
75007 }
75008 static void incrAggFunctionDepth(Expr *pExpr, int N){
75009   if( N>0 ){
75010     Walker w;
75011     memset(&w, 0, sizeof(w));
75012     w.xExprCallback = incrAggDepth;
75013     w.u.i = N;
75014     sqlite3WalkExpr(&w, pExpr);
75015   }
75016 }
75017 
75018 /*
75019 ** Turn the pExpr expression into an alias for the iCol-th column of the
75020 ** result set in pEList.
75021 **
75022 ** If the result set column is a simple column reference, then this routine
75023 ** makes an exact copy.  But for any other kind of expression, this
75024 ** routine make a copy of the result set column as the argument to the
75025 ** TK_AS operator.  The TK_AS operator causes the expression to be
75026 ** evaluated just once and then reused for each alias.
75027 **
75028 ** The reason for suppressing the TK_AS term when the expression is a simple
75029 ** column reference is so that the column reference will be recognized as
75030 ** usable by indices within the WHERE clause processing logic.
75031 **
75032 ** The TK_AS operator is inhibited if zType[0]=='G'.  This means
75033 ** that in a GROUP BY clause, the expression is evaluated twice.  Hence:
75034 **
75035 **     SELECT random()%5 AS x, count(*) FROM tab GROUP BY x
75036 **
75037 ** Is equivalent to:
75038 **
75039 **     SELECT random()%5 AS x, count(*) FROM tab GROUP BY random()%5
75040 **
75041 ** The result of random()%5 in the GROUP BY clause is probably different
75042 ** from the result in the result-set.  On the other hand Standard SQL does
75043 ** not allow the GROUP BY clause to contain references to result-set columns.
75044 ** So this should never come up in well-formed queries.
75045 **
75046 ** If the reference is followed by a COLLATE operator, then make sure
75047 ** the COLLATE operator is preserved.  For example:
75048 **
75049 **     SELECT a+b, c+d FROM t1 ORDER BY 1 COLLATE nocase;
75050 **
75051 ** Should be transformed into:
75052 **
75053 **     SELECT a+b, c+d FROM t1 ORDER BY (a+b) COLLATE nocase;
75054 **
75055 ** The nSubquery parameter specifies how many levels of subquery the
75056 ** alias is removed from the original expression.  The usually value is
75057 ** zero but it might be more if the alias is contained within a subquery
75058 ** of the original expression.  The Expr.op2 field of TK_AGG_FUNCTION
75059 ** structures must be increased by the nSubquery amount.
75060 */
75061 static void resolveAlias(
75062   Parse *pParse,         /* Parsing context */
75063   ExprList *pEList,      /* A result set */
75064   int iCol,              /* A column in the result set.  0..pEList->nExpr-1 */
75065   Expr *pExpr,           /* Transform this into an alias to the result set */
75066   const char *zType,     /* "GROUP" or "ORDER" or "" */
75067   int nSubquery          /* Number of subqueries that the label is moving */
75068 ){
75069   Expr *pOrig;           /* The iCol-th column of the result set */
75070   Expr *pDup;            /* Copy of pOrig */
75071   sqlite3 *db;           /* The database connection */
75072 
75073   assert( iCol>=0 && iCol<pEList->nExpr );
75074   pOrig = pEList->a[iCol].pExpr;
75075   assert( pOrig!=0 );
75076   assert( pOrig->flags & EP_Resolved );
75077   db = pParse->db;
75078   pDup = sqlite3ExprDup(db, pOrig, 0);
75079   if( pDup==0 ) return;
75080   if( pOrig->op!=TK_COLUMN && zType[0]!='G' ){
75081     incrAggFunctionDepth(pDup, nSubquery);
75082     pDup = sqlite3PExpr(pParse, TK_AS, pDup, 0, 0);
75083     if( pDup==0 ) return;
75084     ExprSetProperty(pDup, EP_Skip);
75085     if( pEList->a[iCol].u.x.iAlias==0 ){
75086       pEList->a[iCol].u.x.iAlias = (u16)(++pParse->nAlias);
75087     }
75088     pDup->iTable = pEList->a[iCol].u.x.iAlias;
75089   }
75090   if( pExpr->op==TK_COLLATE ){
75091     pDup = sqlite3ExprAddCollateString(pParse, pDup, pExpr->u.zToken);
75092   }
75093 
75094   /* Before calling sqlite3ExprDelete(), set the EP_Static flag. This
75095   ** prevents ExprDelete() from deleting the Expr structure itself,
75096   ** allowing it to be repopulated by the memcpy() on the following line.
75097   ** The pExpr->u.zToken might point into memory that will be freed by the
75098   ** sqlite3DbFree(db, pDup) on the last line of this block, so be sure to
75099   ** make a copy of the token before doing the sqlite3DbFree().
75100   */
75101   ExprSetProperty(pExpr, EP_Static);
75102   sqlite3ExprDelete(db, pExpr);
75103   memcpy(pExpr, pDup, sizeof(*pExpr));
75104   if( !ExprHasProperty(pExpr, EP_IntValue) && pExpr->u.zToken!=0 ){
75105     assert( (pExpr->flags & (EP_Reduced|EP_TokenOnly))==0 );
75106     pExpr->u.zToken = sqlite3DbStrDup(db, pExpr->u.zToken);
75107     pExpr->flags |= EP_MemToken;
75108   }
75109   sqlite3DbFree(db, pDup);
75110 }
75111 
75112 
75113 /*
75114 ** Return TRUE if the name zCol occurs anywhere in the USING clause.
75115 **
75116 ** Return FALSE if the USING clause is NULL or if it does not contain
75117 ** zCol.
75118 */
75119 static int nameInUsingClause(IdList *pUsing, const char *zCol){
75120   if( pUsing ){
75121     int k;
75122     for(k=0; k<pUsing->nId; k++){
75123       if( sqlite3StrICmp(pUsing->a[k].zName, zCol)==0 ) return 1;
75124     }
75125   }
75126   return 0;
75127 }
75128 
75129 /*
75130 ** Subqueries stores the original database, table and column names for their
75131 ** result sets in ExprList.a[].zSpan, in the form "DATABASE.TABLE.COLUMN".
75132 ** Check to see if the zSpan given to this routine matches the zDb, zTab,
75133 ** and zCol.  If any of zDb, zTab, and zCol are NULL then those fields will
75134 ** match anything.
75135 */
75136 SQLITE_PRIVATE int sqlite3MatchSpanName(
75137   const char *zSpan,
75138   const char *zCol,
75139   const char *zTab,
75140   const char *zDb
75141 ){
75142   int n;
75143   for(n=0; ALWAYS(zSpan[n]) && zSpan[n]!='.'; n++){}
75144   if( zDb && (sqlite3StrNICmp(zSpan, zDb, n)!=0 || zDb[n]!=0) ){
75145     return 0;
75146   }
75147   zSpan += n+1;
75148   for(n=0; ALWAYS(zSpan[n]) && zSpan[n]!='.'; n++){}
75149   if( zTab && (sqlite3StrNICmp(zSpan, zTab, n)!=0 || zTab[n]!=0) ){
75150     return 0;
75151   }
75152   zSpan += n+1;
75153   if( zCol && sqlite3StrICmp(zSpan, zCol)!=0 ){
75154     return 0;
75155   }
75156   return 1;
75157 }
75158 
75159 /*
75160 ** Given the name of a column of the form X.Y.Z or Y.Z or just Z, look up
75161 ** that name in the set of source tables in pSrcList and make the pExpr
75162 ** expression node refer back to that source column.  The following changes
75163 ** are made to pExpr:
75164 **
75165 **    pExpr->iDb           Set the index in db->aDb[] of the database X
75166 **                         (even if X is implied).
75167 **    pExpr->iTable        Set to the cursor number for the table obtained
75168 **                         from pSrcList.
75169 **    pExpr->pTab          Points to the Table structure of X.Y (even if
75170 **                         X and/or Y are implied.)
75171 **    pExpr->iColumn       Set to the column number within the table.
75172 **    pExpr->op            Set to TK_COLUMN.
75173 **    pExpr->pLeft         Any expression this points to is deleted
75174 **    pExpr->pRight        Any expression this points to is deleted.
75175 **
75176 ** The zDb variable is the name of the database (the "X").  This value may be
75177 ** NULL meaning that name is of the form Y.Z or Z.  Any available database
75178 ** can be used.  The zTable variable is the name of the table (the "Y").  This
75179 ** value can be NULL if zDb is also NULL.  If zTable is NULL it
75180 ** means that the form of the name is Z and that columns from any table
75181 ** can be used.
75182 **
75183 ** If the name cannot be resolved unambiguously, leave an error message
75184 ** in pParse and return WRC_Abort.  Return WRC_Prune on success.
75185 */
75186 static int lookupName(
75187   Parse *pParse,       /* The parsing context */
75188   const char *zDb,     /* Name of the database containing table, or NULL */
75189   const char *zTab,    /* Name of table containing column, or NULL */
75190   const char *zCol,    /* Name of the column. */
75191   NameContext *pNC,    /* The name context used to resolve the name */
75192   Expr *pExpr          /* Make this EXPR node point to the selected column */
75193 ){
75194   int i, j;                         /* Loop counters */
75195   int cnt = 0;                      /* Number of matching column names */
75196   int cntTab = 0;                   /* Number of matching table names */
75197   int nSubquery = 0;                /* How many levels of subquery */
75198   sqlite3 *db = pParse->db;         /* The database connection */
75199   struct SrcList_item *pItem;       /* Use for looping over pSrcList items */
75200   struct SrcList_item *pMatch = 0;  /* The matching pSrcList item */
75201   NameContext *pTopNC = pNC;        /* First namecontext in the list */
75202   Schema *pSchema = 0;              /* Schema of the expression */
75203   int isTrigger = 0;                /* True if resolved to a trigger column */
75204   Table *pTab = 0;                  /* Table hold the row */
75205   Column *pCol;                     /* A column of pTab */
75206 
75207   assert( pNC );     /* the name context cannot be NULL. */
75208   assert( zCol );    /* The Z in X.Y.Z cannot be NULL */
75209   assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) );
75210 
75211   /* Initialize the node to no-match */
75212   pExpr->iTable = -1;
75213   pExpr->pTab = 0;
75214   ExprSetVVAProperty(pExpr, EP_NoReduce);
75215 
75216   /* Translate the schema name in zDb into a pointer to the corresponding
75217   ** schema.  If not found, pSchema will remain NULL and nothing will match
75218   ** resulting in an appropriate error message toward the end of this routine
75219   */
75220   if( zDb ){
75221     testcase( pNC->ncFlags & NC_PartIdx );
75222     testcase( pNC->ncFlags & NC_IsCheck );
75223     if( (pNC->ncFlags & (NC_PartIdx|NC_IsCheck))!=0 ){
75224       /* Silently ignore database qualifiers inside CHECK constraints and partial
75225       ** indices.  Do not raise errors because that might break legacy and
75226       ** because it does not hurt anything to just ignore the database name. */
75227       zDb = 0;
75228     }else{
75229       for(i=0; i<db->nDb; i++){
75230         assert( db->aDb[i].zName );
75231         if( sqlite3StrICmp(db->aDb[i].zName,zDb)==0 ){
75232           pSchema = db->aDb[i].pSchema;
75233           break;
75234         }
75235       }
75236     }
75237   }
75238 
75239   /* Start at the inner-most context and move outward until a match is found */
75240   while( pNC && cnt==0 ){
75241     ExprList *pEList;
75242     SrcList *pSrcList = pNC->pSrcList;
75243 
75244     if( pSrcList ){
75245       for(i=0, pItem=pSrcList->a; i<pSrcList->nSrc; i++, pItem++){
75246         pTab = pItem->pTab;
75247         assert( pTab!=0 && pTab->zName!=0 );
75248         assert( pTab->nCol>0 );
75249         if( pItem->pSelect && (pItem->pSelect->selFlags & SF_NestedFrom)!=0 ){
75250           int hit = 0;
75251           pEList = pItem->pSelect->pEList;
75252           for(j=0; j<pEList->nExpr; j++){
75253             if( sqlite3MatchSpanName(pEList->a[j].zSpan, zCol, zTab, zDb) ){
75254               cnt++;
75255               cntTab = 2;
75256               pMatch = pItem;
75257               pExpr->iColumn = j;
75258               hit = 1;
75259             }
75260           }
75261           if( hit || zTab==0 ) continue;
75262         }
75263         if( zDb && pTab->pSchema!=pSchema ){
75264           continue;
75265         }
75266         if( zTab ){
75267           const char *zTabName = pItem->zAlias ? pItem->zAlias : pTab->zName;
75268           assert( zTabName!=0 );
75269           if( sqlite3StrICmp(zTabName, zTab)!=0 ){
75270             continue;
75271           }
75272         }
75273         if( 0==(cntTab++) ){
75274           pMatch = pItem;
75275         }
75276         for(j=0, pCol=pTab->aCol; j<pTab->nCol; j++, pCol++){
75277           if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
75278             /* If there has been exactly one prior match and this match
75279             ** is for the right-hand table of a NATURAL JOIN or is in a
75280             ** USING clause, then skip this match.
75281             */
75282             if( cnt==1 ){
75283               if( pItem->jointype & JT_NATURAL ) continue;
75284               if( nameInUsingClause(pItem->pUsing, zCol) ) continue;
75285             }
75286             cnt++;
75287             pMatch = pItem;
75288             /* Substitute the rowid (column -1) for the INTEGER PRIMARY KEY */
75289             pExpr->iColumn = j==pTab->iPKey ? -1 : (i16)j;
75290             break;
75291           }
75292         }
75293       }
75294       if( pMatch ){
75295         pExpr->iTable = pMatch->iCursor;
75296         pExpr->pTab = pMatch->pTab;
75297         pSchema = pExpr->pTab->pSchema;
75298       }
75299     } /* if( pSrcList ) */
75300 
75301 #ifndef SQLITE_OMIT_TRIGGER
75302     /* If we have not already resolved the name, then maybe
75303     ** it is a new.* or old.* trigger argument reference
75304     */
75305     if( zDb==0 && zTab!=0 && cntTab==0 && pParse->pTriggerTab!=0 ){
75306       int op = pParse->eTriggerOp;
75307       assert( op==TK_DELETE || op==TK_UPDATE || op==TK_INSERT );
75308       if( op!=TK_DELETE && sqlite3StrICmp("new",zTab) == 0 ){
75309         pExpr->iTable = 1;
75310         pTab = pParse->pTriggerTab;
75311       }else if( op!=TK_INSERT && sqlite3StrICmp("old",zTab)==0 ){
75312         pExpr->iTable = 0;
75313         pTab = pParse->pTriggerTab;
75314       }
75315 
75316       if( pTab ){
75317         int iCol;
75318         pSchema = pTab->pSchema;
75319         cntTab++;
75320         for(iCol=0, pCol=pTab->aCol; iCol<pTab->nCol; iCol++, pCol++){
75321           if( sqlite3StrICmp(pCol->zName, zCol)==0 ){
75322             if( iCol==pTab->iPKey ){
75323               iCol = -1;
75324             }
75325             break;
75326           }
75327         }
75328         if( iCol>=pTab->nCol && sqlite3IsRowid(zCol) && HasRowid(pTab) ){
75329           /* IMP: R-24309-18625 */
75330           /* IMP: R-44911-55124 */
75331           iCol = -1;
75332         }
75333         if( iCol<pTab->nCol ){
75334           cnt++;
75335           if( iCol<0 ){
75336             pExpr->affinity = SQLITE_AFF_INTEGER;
75337           }else if( pExpr->iTable==0 ){
75338             testcase( iCol==31 );
75339             testcase( iCol==32 );
75340             pParse->oldmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol));
75341           }else{
75342             testcase( iCol==31 );
75343             testcase( iCol==32 );
75344             pParse->newmask |= (iCol>=32 ? 0xffffffff : (((u32)1)<<iCol));
75345           }
75346           pExpr->iColumn = (i16)iCol;
75347           pExpr->pTab = pTab;
75348           isTrigger = 1;
75349         }
75350       }
75351     }
75352 #endif /* !defined(SQLITE_OMIT_TRIGGER) */
75353 
75354     /*
75355     ** Perhaps the name is a reference to the ROWID
75356     */
75357     assert( pTab!=0 || cntTab==0 );
75358     if( cnt==0 && cntTab==1 && sqlite3IsRowid(zCol) && HasRowid(pTab) ){
75359       cnt = 1;
75360       pExpr->iColumn = -1;     /* IMP: R-44911-55124 */
75361       pExpr->affinity = SQLITE_AFF_INTEGER;
75362     }
75363 
75364     /*
75365     ** If the input is of the form Z (not Y.Z or X.Y.Z) then the name Z
75366     ** might refer to an result-set alias.  This happens, for example, when
75367     ** we are resolving names in the WHERE clause of the following command:
75368     **
75369     **     SELECT a+b AS x FROM table WHERE x<10;
75370     **
75371     ** In cases like this, replace pExpr with a copy of the expression that
75372     ** forms the result set entry ("a+b" in the example) and return immediately.
75373     ** Note that the expression in the result set should have already been
75374     ** resolved by the time the WHERE clause is resolved.
75375     **
75376     ** The ability to use an output result-set column in the WHERE, GROUP BY,
75377     ** or HAVING clauses, or as part of a larger expression in the ORDRE BY
75378     ** clause is not standard SQL.  This is a (goofy) SQLite extension, that
75379     ** is supported for backwards compatibility only.  TO DO: Issue a warning
75380     ** on sqlite3_log() whenever the capability is used.
75381     */
75382     if( (pEList = pNC->pEList)!=0
75383      && zTab==0
75384      && cnt==0
75385     ){
75386       for(j=0; j<pEList->nExpr; j++){
75387         char *zAs = pEList->a[j].zName;
75388         if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
75389           Expr *pOrig;
75390           assert( pExpr->pLeft==0 && pExpr->pRight==0 );
75391           assert( pExpr->x.pList==0 );
75392           assert( pExpr->x.pSelect==0 );
75393           pOrig = pEList->a[j].pExpr;
75394           if( (pNC->ncFlags&NC_AllowAgg)==0 && ExprHasProperty(pOrig, EP_Agg) ){
75395             sqlite3ErrorMsg(pParse, "misuse of aliased aggregate %s", zAs);
75396             return WRC_Abort;
75397           }
75398           resolveAlias(pParse, pEList, j, pExpr, "", nSubquery);
75399           cnt = 1;
75400           pMatch = 0;
75401           assert( zTab==0 && zDb==0 );
75402           goto lookupname_end;
75403         }
75404       }
75405     }
75406 
75407     /* Advance to the next name context.  The loop will exit when either
75408     ** we have a match (cnt>0) or when we run out of name contexts.
75409     */
75410     if( cnt==0 ){
75411       pNC = pNC->pNext;
75412       nSubquery++;
75413     }
75414   }
75415 
75416   /*
75417   ** If X and Y are NULL (in other words if only the column name Z is
75418   ** supplied) and the value of Z is enclosed in double-quotes, then
75419   ** Z is a string literal if it doesn't match any column names.  In that
75420   ** case, we need to return right away and not make any changes to
75421   ** pExpr.
75422   **
75423   ** Because no reference was made to outer contexts, the pNC->nRef
75424   ** fields are not changed in any context.
75425   */
75426   if( cnt==0 && zTab==0 && ExprHasProperty(pExpr,EP_DblQuoted) ){
75427     pExpr->op = TK_STRING;
75428     pExpr->pTab = 0;
75429     return WRC_Prune;
75430   }
75431 
75432   /*
75433   ** cnt==0 means there was not match.  cnt>1 means there were two or
75434   ** more matches.  Either way, we have an error.
75435   */
75436   if( cnt!=1 ){
75437     const char *zErr;
75438     zErr = cnt==0 ? "no such column" : "ambiguous column name";
75439     if( zDb ){
75440       sqlite3ErrorMsg(pParse, "%s: %s.%s.%s", zErr, zDb, zTab, zCol);
75441     }else if( zTab ){
75442       sqlite3ErrorMsg(pParse, "%s: %s.%s", zErr, zTab, zCol);
75443     }else{
75444       sqlite3ErrorMsg(pParse, "%s: %s", zErr, zCol);
75445     }
75446     pParse->checkSchema = 1;
75447     pTopNC->nErr++;
75448   }
75449 
75450   /* If a column from a table in pSrcList is referenced, then record
75451   ** this fact in the pSrcList.a[].colUsed bitmask.  Column 0 causes
75452   ** bit 0 to be set.  Column 1 sets bit 1.  And so forth.  If the
75453   ** column number is greater than the number of bits in the bitmask
75454   ** then set the high-order bit of the bitmask.
75455   */
75456   if( pExpr->iColumn>=0 && pMatch!=0 ){
75457     int n = pExpr->iColumn;
75458     testcase( n==BMS-1 );
75459     if( n>=BMS ){
75460       n = BMS-1;
75461     }
75462     assert( pMatch->iCursor==pExpr->iTable );
75463     pMatch->colUsed |= ((Bitmask)1)<<n;
75464   }
75465 
75466   /* Clean up and return
75467   */
75468   sqlite3ExprDelete(db, pExpr->pLeft);
75469   pExpr->pLeft = 0;
75470   sqlite3ExprDelete(db, pExpr->pRight);
75471   pExpr->pRight = 0;
75472   pExpr->op = (isTrigger ? TK_TRIGGER : TK_COLUMN);
75473 lookupname_end:
75474   if( cnt==1 ){
75475     assert( pNC!=0 );
75476     if( pExpr->op!=TK_AS ){
75477       sqlite3AuthRead(pParse, pExpr, pSchema, pNC->pSrcList);
75478     }
75479     /* Increment the nRef value on all name contexts from TopNC up to
75480     ** the point where the name matched. */
75481     for(;;){
75482       assert( pTopNC!=0 );
75483       pTopNC->nRef++;
75484       if( pTopNC==pNC ) break;
75485       pTopNC = pTopNC->pNext;
75486     }
75487     return WRC_Prune;
75488   } else {
75489     return WRC_Abort;
75490   }
75491 }
75492 
75493 /*
75494 ** Allocate and return a pointer to an expression to load the column iCol
75495 ** from datasource iSrc in SrcList pSrc.
75496 */
75497 SQLITE_PRIVATE Expr *sqlite3CreateColumnExpr(sqlite3 *db, SrcList *pSrc, int iSrc, int iCol){
75498   Expr *p = sqlite3ExprAlloc(db, TK_COLUMN, 0, 0);
75499   if( p ){
75500     struct SrcList_item *pItem = &pSrc->a[iSrc];
75501     p->pTab = pItem->pTab;
75502     p->iTable = pItem->iCursor;
75503     if( p->pTab->iPKey==iCol ){
75504       p->iColumn = -1;
75505     }else{
75506       p->iColumn = (ynVar)iCol;
75507       testcase( iCol==BMS );
75508       testcase( iCol==BMS-1 );
75509       pItem->colUsed |= ((Bitmask)1)<<(iCol>=BMS ? BMS-1 : iCol);
75510     }
75511     ExprSetProperty(p, EP_Resolved);
75512   }
75513   return p;
75514 }
75515 
75516 /*
75517 ** Report an error that an expression is not valid for a partial index WHERE
75518 ** clause.
75519 */
75520 static void notValidPartIdxWhere(
75521   Parse *pParse,       /* Leave error message here */
75522   NameContext *pNC,    /* The name context */
75523   const char *zMsg     /* Type of error */
75524 ){
75525   if( (pNC->ncFlags & NC_PartIdx)!=0 ){
75526     sqlite3ErrorMsg(pParse, "%s prohibited in partial index WHERE clauses",
75527                     zMsg);
75528   }
75529 }
75530 
75531 #ifndef SQLITE_OMIT_CHECK
75532 /*
75533 ** Report an error that an expression is not valid for a CHECK constraint.
75534 */
75535 static void notValidCheckConstraint(
75536   Parse *pParse,       /* Leave error message here */
75537   NameContext *pNC,    /* The name context */
75538   const char *zMsg     /* Type of error */
75539 ){
75540   if( (pNC->ncFlags & NC_IsCheck)!=0 ){
75541     sqlite3ErrorMsg(pParse,"%s prohibited in CHECK constraints", zMsg);
75542   }
75543 }
75544 #else
75545 # define notValidCheckConstraint(P,N,M)
75546 #endif
75547 
75548 /*
75549 ** Expression p should encode a floating point value between 1.0 and 0.0.
75550 ** Return 1024 times this value.  Or return -1 if p is not a floating point
75551 ** value between 1.0 and 0.0.
75552 */
75553 static int exprProbability(Expr *p){
75554   double r = -1.0;
75555   if( p->op!=TK_FLOAT ) return -1;
75556   sqlite3AtoF(p->u.zToken, &r, sqlite3Strlen30(p->u.zToken), SQLITE_UTF8);
75557   assert( r>=0.0 );
75558   if( r>1.0 ) return -1;
75559   return (int)(r*1000.0);
75560 }
75561 
75562 /*
75563 ** This routine is callback for sqlite3WalkExpr().
75564 **
75565 ** Resolve symbolic names into TK_COLUMN operators for the current
75566 ** node in the expression tree.  Return 0 to continue the search down
75567 ** the tree or 2 to abort the tree walk.
75568 **
75569 ** This routine also does error checking and name resolution for
75570 ** function names.  The operator for aggregate functions is changed
75571 ** to TK_AGG_FUNCTION.
75572 */
75573 static int resolveExprStep(Walker *pWalker, Expr *pExpr){
75574   NameContext *pNC;
75575   Parse *pParse;
75576 
75577   pNC = pWalker->u.pNC;
75578   assert( pNC!=0 );
75579   pParse = pNC->pParse;
75580   assert( pParse==pWalker->pParse );
75581 
75582   if( ExprHasProperty(pExpr, EP_Resolved) ) return WRC_Prune;
75583   ExprSetProperty(pExpr, EP_Resolved);
75584 #ifndef NDEBUG
75585   if( pNC->pSrcList && pNC->pSrcList->nAlloc>0 ){
75586     SrcList *pSrcList = pNC->pSrcList;
75587     int i;
75588     for(i=0; i<pNC->pSrcList->nSrc; i++){
75589       assert( pSrcList->a[i].iCursor>=0 && pSrcList->a[i].iCursor<pParse->nTab);
75590     }
75591   }
75592 #endif
75593   switch( pExpr->op ){
75594 
75595 #if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY)
75596     /* The special operator TK_ROW means use the rowid for the first
75597     ** column in the FROM clause.  This is used by the LIMIT and ORDER BY
75598     ** clause processing on UPDATE and DELETE statements.
75599     */
75600     case TK_ROW: {
75601       SrcList *pSrcList = pNC->pSrcList;
75602       struct SrcList_item *pItem;
75603       assert( pSrcList && pSrcList->nSrc==1 );
75604       pItem = pSrcList->a;
75605       pExpr->op = TK_COLUMN;
75606       pExpr->pTab = pItem->pTab;
75607       pExpr->iTable = pItem->iCursor;
75608       pExpr->iColumn = -1;
75609       pExpr->affinity = SQLITE_AFF_INTEGER;
75610       break;
75611     }
75612 #endif /* defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY) */
75613 
75614     /* A lone identifier is the name of a column.
75615     */
75616     case TK_ID: {
75617       return lookupName(pParse, 0, 0, pExpr->u.zToken, pNC, pExpr);
75618     }
75619 
75620     /* A table name and column name:     ID.ID
75621     ** Or a database, table and column:  ID.ID.ID
75622     */
75623     case TK_DOT: {
75624       const char *zColumn;
75625       const char *zTable;
75626       const char *zDb;
75627       Expr *pRight;
75628 
75629       /* if( pSrcList==0 ) break; */
75630       pRight = pExpr->pRight;
75631       if( pRight->op==TK_ID ){
75632         zDb = 0;
75633         zTable = pExpr->pLeft->u.zToken;
75634         zColumn = pRight->u.zToken;
75635       }else{
75636         assert( pRight->op==TK_DOT );
75637         zDb = pExpr->pLeft->u.zToken;
75638         zTable = pRight->pLeft->u.zToken;
75639         zColumn = pRight->pRight->u.zToken;
75640       }
75641       return lookupName(pParse, zDb, zTable, zColumn, pNC, pExpr);
75642     }
75643 
75644     /* Resolve function names
75645     */
75646     case TK_FUNCTION: {
75647       ExprList *pList = pExpr->x.pList;    /* The argument list */
75648       int n = pList ? pList->nExpr : 0;    /* Number of arguments */
75649       int no_such_func = 0;       /* True if no such function exists */
75650       int wrong_num_args = 0;     /* True if wrong number of arguments */
75651       int is_agg = 0;             /* True if is an aggregate function */
75652       int auth;                   /* Authorization to use the function */
75653       int nId;                    /* Number of characters in function name */
75654       const char *zId;            /* The function name. */
75655       FuncDef *pDef;              /* Information about the function */
75656       u8 enc = ENC(pParse->db);   /* The database encoding */
75657 
75658       assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
75659       notValidPartIdxWhere(pParse, pNC, "functions");
75660       zId = pExpr->u.zToken;
75661       nId = sqlite3Strlen30(zId);
75662       pDef = sqlite3FindFunction(pParse->db, zId, nId, n, enc, 0);
75663       if( pDef==0 ){
75664         pDef = sqlite3FindFunction(pParse->db, zId, nId, -2, enc, 0);
75665         if( pDef==0 ){
75666           no_such_func = 1;
75667         }else{
75668           wrong_num_args = 1;
75669         }
75670       }else{
75671         is_agg = pDef->xFunc==0;
75672         if( pDef->funcFlags & SQLITE_FUNC_UNLIKELY ){
75673           ExprSetProperty(pExpr, EP_Unlikely|EP_Skip);
75674           if( n==2 ){
75675             pExpr->iTable = exprProbability(pList->a[1].pExpr);
75676             if( pExpr->iTable<0 ){
75677               sqlite3ErrorMsg(pParse, "second argument to likelihood() must be a "
75678                                       "constant between 0.0 and 1.0");
75679               pNC->nErr++;
75680             }
75681           }else{
75682             /* EVIDENCE-OF: R-61304-29449 The unlikely(X) function is equivalent to
75683             ** likelihood(X, 0.0625).
75684             ** EVIDENCE-OF: R-01283-11636 The unlikely(X) function is short-hand for
75685             ** likelihood(X,0.0625). */
75686             pExpr->iTable = 62;  /* TUNING:  Default 2nd arg to unlikely() is 0.0625 */
75687           }
75688         }
75689       }
75690 #ifndef SQLITE_OMIT_AUTHORIZATION
75691       if( pDef ){
75692         auth = sqlite3AuthCheck(pParse, SQLITE_FUNCTION, 0, pDef->zName, 0);
75693         if( auth!=SQLITE_OK ){
75694           if( auth==SQLITE_DENY ){
75695             sqlite3ErrorMsg(pParse, "not authorized to use function: %s",
75696                                     pDef->zName);
75697             pNC->nErr++;
75698           }
75699           pExpr->op = TK_NULL;
75700           return WRC_Prune;
75701         }
75702         if( pDef->funcFlags & SQLITE_FUNC_CONSTANT ) ExprSetProperty(pExpr,EP_Constant);
75703       }
75704 #endif
75705       if( is_agg && (pNC->ncFlags & NC_AllowAgg)==0 ){
75706         sqlite3ErrorMsg(pParse, "misuse of aggregate function %.*s()", nId,zId);
75707         pNC->nErr++;
75708         is_agg = 0;
75709       }else if( no_such_func && pParse->db->init.busy==0 ){
75710         sqlite3ErrorMsg(pParse, "no such function: %.*s", nId, zId);
75711         pNC->nErr++;
75712       }else if( wrong_num_args ){
75713         sqlite3ErrorMsg(pParse,"wrong number of arguments to function %.*s()",
75714              nId, zId);
75715         pNC->nErr++;
75716       }
75717       if( is_agg ) pNC->ncFlags &= ~NC_AllowAgg;
75718       sqlite3WalkExprList(pWalker, pList);
75719       if( is_agg ){
75720         NameContext *pNC2 = pNC;
75721         pExpr->op = TK_AGG_FUNCTION;
75722         pExpr->op2 = 0;
75723         while( pNC2 && !sqlite3FunctionUsesThisSrc(pExpr, pNC2->pSrcList) ){
75724           pExpr->op2++;
75725           pNC2 = pNC2->pNext;
75726         }
75727         if( pNC2 ) pNC2->ncFlags |= NC_HasAgg;
75728         pNC->ncFlags |= NC_AllowAgg;
75729       }
75730       /* FIX ME:  Compute pExpr->affinity based on the expected return
75731       ** type of the function
75732       */
75733       return WRC_Prune;
75734     }
75735 #ifndef SQLITE_OMIT_SUBQUERY
75736     case TK_SELECT:
75737     case TK_EXISTS:  testcase( pExpr->op==TK_EXISTS );
75738 #endif
75739     case TK_IN: {
75740       testcase( pExpr->op==TK_IN );
75741       if( ExprHasProperty(pExpr, EP_xIsSelect) ){
75742         int nRef = pNC->nRef;
75743         notValidCheckConstraint(pParse, pNC, "subqueries");
75744         notValidPartIdxWhere(pParse, pNC, "subqueries");
75745         sqlite3WalkSelect(pWalker, pExpr->x.pSelect);
75746         assert( pNC->nRef>=nRef );
75747         if( nRef!=pNC->nRef ){
75748           ExprSetProperty(pExpr, EP_VarSelect);
75749         }
75750       }
75751       break;
75752     }
75753     case TK_VARIABLE: {
75754       notValidCheckConstraint(pParse, pNC, "parameters");
75755       notValidPartIdxWhere(pParse, pNC, "parameters");
75756       break;
75757     }
75758   }
75759   return (pParse->nErr || pParse->db->mallocFailed) ? WRC_Abort : WRC_Continue;
75760 }
75761 
75762 /*
75763 ** pEList is a list of expressions which are really the result set of the
75764 ** a SELECT statement.  pE is a term in an ORDER BY or GROUP BY clause.
75765 ** This routine checks to see if pE is a simple identifier which corresponds
75766 ** to the AS-name of one of the terms of the expression list.  If it is,
75767 ** this routine return an integer between 1 and N where N is the number of
75768 ** elements in pEList, corresponding to the matching entry.  If there is
75769 ** no match, or if pE is not a simple identifier, then this routine
75770 ** return 0.
75771 **
75772 ** pEList has been resolved.  pE has not.
75773 */
75774 static int resolveAsName(
75775   Parse *pParse,     /* Parsing context for error messages */
75776   ExprList *pEList,  /* List of expressions to scan */
75777   Expr *pE           /* Expression we are trying to match */
75778 ){
75779   int i;             /* Loop counter */
75780 
75781   UNUSED_PARAMETER(pParse);
75782 
75783   if( pE->op==TK_ID ){
75784     char *zCol = pE->u.zToken;
75785     for(i=0; i<pEList->nExpr; i++){
75786       char *zAs = pEList->a[i].zName;
75787       if( zAs!=0 && sqlite3StrICmp(zAs, zCol)==0 ){
75788         return i+1;
75789       }
75790     }
75791   }
75792   return 0;
75793 }
75794 
75795 /*
75796 ** pE is a pointer to an expression which is a single term in the
75797 ** ORDER BY of a compound SELECT.  The expression has not been
75798 ** name resolved.
75799 **
75800 ** At the point this routine is called, we already know that the
75801 ** ORDER BY term is not an integer index into the result set.  That
75802 ** case is handled by the calling routine.
75803 **
75804 ** Attempt to match pE against result set columns in the left-most
75805 ** SELECT statement.  Return the index i of the matching column,
75806 ** as an indication to the caller that it should sort by the i-th column.
75807 ** The left-most column is 1.  In other words, the value returned is the
75808 ** same integer value that would be used in the SQL statement to indicate
75809 ** the column.
75810 **
75811 ** If there is no match, return 0.  Return -1 if an error occurs.
75812 */
75813 static int resolveOrderByTermToExprList(
75814   Parse *pParse,     /* Parsing context for error messages */
75815   Select *pSelect,   /* The SELECT statement with the ORDER BY clause */
75816   Expr *pE           /* The specific ORDER BY term */
75817 ){
75818   int i;             /* Loop counter */
75819   ExprList *pEList;  /* The columns of the result set */
75820   NameContext nc;    /* Name context for resolving pE */
75821   sqlite3 *db;       /* Database connection */
75822   int rc;            /* Return code from subprocedures */
75823   u8 savedSuppErr;   /* Saved value of db->suppressErr */
75824 
75825   assert( sqlite3ExprIsInteger(pE, &i)==0 );
75826   pEList = pSelect->pEList;
75827 
75828   /* Resolve all names in the ORDER BY term expression
75829   */
75830   memset(&nc, 0, sizeof(nc));
75831   nc.pParse = pParse;
75832   nc.pSrcList = pSelect->pSrc;
75833   nc.pEList = pEList;
75834   nc.ncFlags = NC_AllowAgg;
75835   nc.nErr = 0;
75836   db = pParse->db;
75837   savedSuppErr = db->suppressErr;
75838   db->suppressErr = 1;
75839   rc = sqlite3ResolveExprNames(&nc, pE);
75840   db->suppressErr = savedSuppErr;
75841   if( rc ) return 0;
75842 
75843   /* Try to match the ORDER BY expression against an expression
75844   ** in the result set.  Return an 1-based index of the matching
75845   ** result-set entry.
75846   */
75847   for(i=0; i<pEList->nExpr; i++){
75848     if( sqlite3ExprCompare(pEList->a[i].pExpr, pE, -1)<2 ){
75849       return i+1;
75850     }
75851   }
75852 
75853   /* If no match, return 0. */
75854   return 0;
75855 }
75856 
75857 /*
75858 ** Generate an ORDER BY or GROUP BY term out-of-range error.
75859 */
75860 static void resolveOutOfRangeError(
75861   Parse *pParse,         /* The error context into which to write the error */
75862   const char *zType,     /* "ORDER" or "GROUP" */
75863   int i,                 /* The index (1-based) of the term out of range */
75864   int mx                 /* Largest permissible value of i */
75865 ){
75866   sqlite3ErrorMsg(pParse,
75867     "%r %s BY term out of range - should be "
75868     "between 1 and %d", i, zType, mx);
75869 }
75870 
75871 /*
75872 ** Analyze the ORDER BY clause in a compound SELECT statement.   Modify
75873 ** each term of the ORDER BY clause is a constant integer between 1
75874 ** and N where N is the number of columns in the compound SELECT.
75875 **
75876 ** ORDER BY terms that are already an integer between 1 and N are
75877 ** unmodified.  ORDER BY terms that are integers outside the range of
75878 ** 1 through N generate an error.  ORDER BY terms that are expressions
75879 ** are matched against result set expressions of compound SELECT
75880 ** beginning with the left-most SELECT and working toward the right.
75881 ** At the first match, the ORDER BY expression is transformed into
75882 ** the integer column number.
75883 **
75884 ** Return the number of errors seen.
75885 */
75886 static int resolveCompoundOrderBy(
75887   Parse *pParse,        /* Parsing context.  Leave error messages here */
75888   Select *pSelect       /* The SELECT statement containing the ORDER BY */
75889 ){
75890   int i;
75891   ExprList *pOrderBy;
75892   ExprList *pEList;
75893   sqlite3 *db;
75894   int moreToDo = 1;
75895 
75896   pOrderBy = pSelect->pOrderBy;
75897   if( pOrderBy==0 ) return 0;
75898   db = pParse->db;
75899 #if SQLITE_MAX_COLUMN
75900   if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
75901     sqlite3ErrorMsg(pParse, "too many terms in ORDER BY clause");
75902     return 1;
75903   }
75904 #endif
75905   for(i=0; i<pOrderBy->nExpr; i++){
75906     pOrderBy->a[i].done = 0;
75907   }
75908   pSelect->pNext = 0;
75909   while( pSelect->pPrior ){
75910     pSelect->pPrior->pNext = pSelect;
75911     pSelect = pSelect->pPrior;
75912   }
75913   while( pSelect && moreToDo ){
75914     struct ExprList_item *pItem;
75915     moreToDo = 0;
75916     pEList = pSelect->pEList;
75917     assert( pEList!=0 );
75918     for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
75919       int iCol = -1;
75920       Expr *pE, *pDup;
75921       if( pItem->done ) continue;
75922       pE = sqlite3ExprSkipCollate(pItem->pExpr);
75923       if( sqlite3ExprIsInteger(pE, &iCol) ){
75924         if( iCol<=0 || iCol>pEList->nExpr ){
75925           resolveOutOfRangeError(pParse, "ORDER", i+1, pEList->nExpr);
75926           return 1;
75927         }
75928       }else{
75929         iCol = resolveAsName(pParse, pEList, pE);
75930         if( iCol==0 ){
75931           pDup = sqlite3ExprDup(db, pE, 0);
75932           if( !db->mallocFailed ){
75933             assert(pDup);
75934             iCol = resolveOrderByTermToExprList(pParse, pSelect, pDup);
75935           }
75936           sqlite3ExprDelete(db, pDup);
75937         }
75938       }
75939       if( iCol>0 ){
75940         /* Convert the ORDER BY term into an integer column number iCol,
75941         ** taking care to preserve the COLLATE clause if it exists */
75942         Expr *pNew = sqlite3Expr(db, TK_INTEGER, 0);
75943         if( pNew==0 ) return 1;
75944         pNew->flags |= EP_IntValue;
75945         pNew->u.iValue = iCol;
75946         if( pItem->pExpr==pE ){
75947           pItem->pExpr = pNew;
75948         }else{
75949           assert( pItem->pExpr->op==TK_COLLATE );
75950           assert( pItem->pExpr->pLeft==pE );
75951           pItem->pExpr->pLeft = pNew;
75952         }
75953         sqlite3ExprDelete(db, pE);
75954         pItem->u.x.iOrderByCol = (u16)iCol;
75955         pItem->done = 1;
75956       }else{
75957         moreToDo = 1;
75958       }
75959     }
75960     pSelect = pSelect->pNext;
75961   }
75962   for(i=0; i<pOrderBy->nExpr; i++){
75963     if( pOrderBy->a[i].done==0 ){
75964       sqlite3ErrorMsg(pParse, "%r ORDER BY term does not match any "
75965             "column in the result set", i+1);
75966       return 1;
75967     }
75968   }
75969   return 0;
75970 }
75971 
75972 /*
75973 ** Check every term in the ORDER BY or GROUP BY clause pOrderBy of
75974 ** the SELECT statement pSelect.  If any term is reference to a
75975 ** result set expression (as determined by the ExprList.a.u.x.iOrderByCol
75976 ** field) then convert that term into a copy of the corresponding result set
75977 ** column.
75978 **
75979 ** If any errors are detected, add an error message to pParse and
75980 ** return non-zero.  Return zero if no errors are seen.
75981 */
75982 SQLITE_PRIVATE int sqlite3ResolveOrderGroupBy(
75983   Parse *pParse,        /* Parsing context.  Leave error messages here */
75984   Select *pSelect,      /* The SELECT statement containing the clause */
75985   ExprList *pOrderBy,   /* The ORDER BY or GROUP BY clause to be processed */
75986   const char *zType     /* "ORDER" or "GROUP" */
75987 ){
75988   int i;
75989   sqlite3 *db = pParse->db;
75990   ExprList *pEList;
75991   struct ExprList_item *pItem;
75992 
75993   if( pOrderBy==0 || pParse->db->mallocFailed ) return 0;
75994 #if SQLITE_MAX_COLUMN
75995   if( pOrderBy->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
75996     sqlite3ErrorMsg(pParse, "too many terms in %s BY clause", zType);
75997     return 1;
75998   }
75999 #endif
76000   pEList = pSelect->pEList;
76001   assert( pEList!=0 );  /* sqlite3SelectNew() guarantees this */
76002   for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
76003     if( pItem->u.x.iOrderByCol ){
76004       if( pItem->u.x.iOrderByCol>pEList->nExpr ){
76005         resolveOutOfRangeError(pParse, zType, i+1, pEList->nExpr);
76006         return 1;
76007       }
76008       resolveAlias(pParse, pEList, pItem->u.x.iOrderByCol-1, pItem->pExpr, zType,0);
76009     }
76010   }
76011   return 0;
76012 }
76013 
76014 /*
76015 ** pOrderBy is an ORDER BY or GROUP BY clause in SELECT statement pSelect.
76016 ** The Name context of the SELECT statement is pNC.  zType is either
76017 ** "ORDER" or "GROUP" depending on which type of clause pOrderBy is.
76018 **
76019 ** This routine resolves each term of the clause into an expression.
76020 ** If the order-by term is an integer I between 1 and N (where N is the
76021 ** number of columns in the result set of the SELECT) then the expression
76022 ** in the resolution is a copy of the I-th result-set expression.  If
76023 ** the order-by term is an identifier that corresponds to the AS-name of
76024 ** a result-set expression, then the term resolves to a copy of the
76025 ** result-set expression.  Otherwise, the expression is resolved in
76026 ** the usual way - using sqlite3ResolveExprNames().
76027 **
76028 ** This routine returns the number of errors.  If errors occur, then
76029 ** an appropriate error message might be left in pParse.  (OOM errors
76030 ** excepted.)
76031 */
76032 static int resolveOrderGroupBy(
76033   NameContext *pNC,     /* The name context of the SELECT statement */
76034   Select *pSelect,      /* The SELECT statement holding pOrderBy */
76035   ExprList *pOrderBy,   /* An ORDER BY or GROUP BY clause to resolve */
76036   const char *zType     /* Either "ORDER" or "GROUP", as appropriate */
76037 ){
76038   int i, j;                      /* Loop counters */
76039   int iCol;                      /* Column number */
76040   struct ExprList_item *pItem;   /* A term of the ORDER BY clause */
76041   Parse *pParse;                 /* Parsing context */
76042   int nResult;                   /* Number of terms in the result set */
76043 
76044   if( pOrderBy==0 ) return 0;
76045   nResult = pSelect->pEList->nExpr;
76046   pParse = pNC->pParse;
76047   for(i=0, pItem=pOrderBy->a; i<pOrderBy->nExpr; i++, pItem++){
76048     Expr *pE = pItem->pExpr;
76049     Expr *pE2 = sqlite3ExprSkipCollate(pE);
76050     if( zType[0]!='G' ){
76051       iCol = resolveAsName(pParse, pSelect->pEList, pE2);
76052       if( iCol>0 ){
76053         /* If an AS-name match is found, mark this ORDER BY column as being
76054         ** a copy of the iCol-th result-set column.  The subsequent call to
76055         ** sqlite3ResolveOrderGroupBy() will convert the expression to a
76056         ** copy of the iCol-th result-set expression. */
76057         pItem->u.x.iOrderByCol = (u16)iCol;
76058         continue;
76059       }
76060     }
76061     if( sqlite3ExprIsInteger(pE2, &iCol) ){
76062       /* The ORDER BY term is an integer constant.  Again, set the column
76063       ** number so that sqlite3ResolveOrderGroupBy() will convert the
76064       ** order-by term to a copy of the result-set expression */
76065       if( iCol<1 || iCol>0xffff ){
76066         resolveOutOfRangeError(pParse, zType, i+1, nResult);
76067         return 1;
76068       }
76069       pItem->u.x.iOrderByCol = (u16)iCol;
76070       continue;
76071     }
76072 
76073     /* Otherwise, treat the ORDER BY term as an ordinary expression */
76074     pItem->u.x.iOrderByCol = 0;
76075     if( sqlite3ResolveExprNames(pNC, pE) ){
76076       return 1;
76077     }
76078     for(j=0; j<pSelect->pEList->nExpr; j++){
76079       if( sqlite3ExprCompare(pE, pSelect->pEList->a[j].pExpr, -1)==0 ){
76080         pItem->u.x.iOrderByCol = j+1;
76081       }
76082     }
76083   }
76084   return sqlite3ResolveOrderGroupBy(pParse, pSelect, pOrderBy, zType);
76085 }
76086 
76087 /*
76088 ** Resolve names in the SELECT statement p and all of its descendents.
76089 */
76090 static int resolveSelectStep(Walker *pWalker, Select *p){
76091   NameContext *pOuterNC;  /* Context that contains this SELECT */
76092   NameContext sNC;        /* Name context of this SELECT */
76093   int isCompound;         /* True if p is a compound select */
76094   int nCompound;          /* Number of compound terms processed so far */
76095   Parse *pParse;          /* Parsing context */
76096   ExprList *pEList;       /* Result set expression list */
76097   int i;                  /* Loop counter */
76098   ExprList *pGroupBy;     /* The GROUP BY clause */
76099   Select *pLeftmost;      /* Left-most of SELECT of a compound */
76100   sqlite3 *db;            /* Database connection */
76101 
76102 
76103   assert( p!=0 );
76104   if( p->selFlags & SF_Resolved ){
76105     return WRC_Prune;
76106   }
76107   pOuterNC = pWalker->u.pNC;
76108   pParse = pWalker->pParse;
76109   db = pParse->db;
76110 
76111   /* Normally sqlite3SelectExpand() will be called first and will have
76112   ** already expanded this SELECT.  However, if this is a subquery within
76113   ** an expression, sqlite3ResolveExprNames() will be called without a
76114   ** prior call to sqlite3SelectExpand().  When that happens, let
76115   ** sqlite3SelectPrep() do all of the processing for this SELECT.
76116   ** sqlite3SelectPrep() will invoke both sqlite3SelectExpand() and
76117   ** this routine in the correct order.
76118   */
76119   if( (p->selFlags & SF_Expanded)==0 ){
76120     sqlite3SelectPrep(pParse, p, pOuterNC);
76121     return (pParse->nErr || db->mallocFailed) ? WRC_Abort : WRC_Prune;
76122   }
76123 
76124   isCompound = p->pPrior!=0;
76125   nCompound = 0;
76126   pLeftmost = p;
76127   while( p ){
76128     assert( (p->selFlags & SF_Expanded)!=0 );
76129     assert( (p->selFlags & SF_Resolved)==0 );
76130     p->selFlags |= SF_Resolved;
76131 
76132     /* Resolve the expressions in the LIMIT and OFFSET clauses. These
76133     ** are not allowed to refer to any names, so pass an empty NameContext.
76134     */
76135     memset(&sNC, 0, sizeof(sNC));
76136     sNC.pParse = pParse;
76137     if( sqlite3ResolveExprNames(&sNC, p->pLimit) ||
76138         sqlite3ResolveExprNames(&sNC, p->pOffset) ){
76139       return WRC_Abort;
76140     }
76141 
76142     /* Recursively resolve names in all subqueries
76143     */
76144     for(i=0; i<p->pSrc->nSrc; i++){
76145       struct SrcList_item *pItem = &p->pSrc->a[i];
76146       if( pItem->pSelect ){
76147         NameContext *pNC;         /* Used to iterate name contexts */
76148         int nRef = 0;             /* Refcount for pOuterNC and outer contexts */
76149         const char *zSavedContext = pParse->zAuthContext;
76150 
76151         /* Count the total number of references to pOuterNC and all of its
76152         ** parent contexts. After resolving references to expressions in
76153         ** pItem->pSelect, check if this value has changed. If so, then
76154         ** SELECT statement pItem->pSelect must be correlated. Set the
76155         ** pItem->isCorrelated flag if this is the case. */
76156         for(pNC=pOuterNC; pNC; pNC=pNC->pNext) nRef += pNC->nRef;
76157 
76158         if( pItem->zName ) pParse->zAuthContext = pItem->zName;
76159         sqlite3ResolveSelectNames(pParse, pItem->pSelect, pOuterNC);
76160         pParse->zAuthContext = zSavedContext;
76161         if( pParse->nErr || db->mallocFailed ) return WRC_Abort;
76162 
76163         for(pNC=pOuterNC; pNC; pNC=pNC->pNext) nRef -= pNC->nRef;
76164         assert( pItem->isCorrelated==0 && nRef<=0 );
76165         pItem->isCorrelated = (nRef!=0);
76166       }
76167     }
76168 
76169     /* Set up the local name-context to pass to sqlite3ResolveExprNames() to
76170     ** resolve the result-set expression list.
76171     */
76172     sNC.ncFlags = NC_AllowAgg;
76173     sNC.pSrcList = p->pSrc;
76174     sNC.pNext = pOuterNC;
76175 
76176     /* Resolve names in the result set. */
76177     pEList = p->pEList;
76178     assert( pEList!=0 );
76179     for(i=0; i<pEList->nExpr; i++){
76180       Expr *pX = pEList->a[i].pExpr;
76181       if( sqlite3ResolveExprNames(&sNC, pX) ){
76182         return WRC_Abort;
76183       }
76184     }
76185 
76186     /* If there are no aggregate functions in the result-set, and no GROUP BY
76187     ** expression, do not allow aggregates in any of the other expressions.
76188     */
76189     assert( (p->selFlags & SF_Aggregate)==0 );
76190     pGroupBy = p->pGroupBy;
76191     if( pGroupBy || (sNC.ncFlags & NC_HasAgg)!=0 ){
76192       p->selFlags |= SF_Aggregate;
76193     }else{
76194       sNC.ncFlags &= ~NC_AllowAgg;
76195     }
76196 
76197     /* If a HAVING clause is present, then there must be a GROUP BY clause.
76198     */
76199     if( p->pHaving && !pGroupBy ){
76200       sqlite3ErrorMsg(pParse, "a GROUP BY clause is required before HAVING");
76201       return WRC_Abort;
76202     }
76203 
76204     /* Add the output column list to the name-context before parsing the
76205     ** other expressions in the SELECT statement. This is so that
76206     ** expressions in the WHERE clause (etc.) can refer to expressions by
76207     ** aliases in the result set.
76208     **
76209     ** Minor point: If this is the case, then the expression will be
76210     ** re-evaluated for each reference to it.
76211     */
76212     sNC.pEList = p->pEList;
76213     if( sqlite3ResolveExprNames(&sNC, p->pHaving) ) return WRC_Abort;
76214     if( sqlite3ResolveExprNames(&sNC, p->pWhere) ) return WRC_Abort;
76215 
76216     /* The ORDER BY and GROUP BY clauses may not refer to terms in
76217     ** outer queries
76218     */
76219     sNC.pNext = 0;
76220     sNC.ncFlags |= NC_AllowAgg;
76221 
76222     /* Process the ORDER BY clause for singleton SELECT statements.
76223     ** The ORDER BY clause for compounds SELECT statements is handled
76224     ** below, after all of the result-sets for all of the elements of
76225     ** the compound have been resolved.
76226     */
76227     if( !isCompound && resolveOrderGroupBy(&sNC, p, p->pOrderBy, "ORDER") ){
76228       return WRC_Abort;
76229     }
76230     if( db->mallocFailed ){
76231       return WRC_Abort;
76232     }
76233 
76234     /* Resolve the GROUP BY clause.  At the same time, make sure
76235     ** the GROUP BY clause does not contain aggregate functions.
76236     */
76237     if( pGroupBy ){
76238       struct ExprList_item *pItem;
76239 
76240       if( resolveOrderGroupBy(&sNC, p, pGroupBy, "GROUP") || db->mallocFailed ){
76241         return WRC_Abort;
76242       }
76243       for(i=0, pItem=pGroupBy->a; i<pGroupBy->nExpr; i++, pItem++){
76244         if( ExprHasProperty(pItem->pExpr, EP_Agg) ){
76245           sqlite3ErrorMsg(pParse, "aggregate functions are not allowed in "
76246               "the GROUP BY clause");
76247           return WRC_Abort;
76248         }
76249       }
76250     }
76251 
76252     /* Advance to the next term of the compound
76253     */
76254     p = p->pPrior;
76255     nCompound++;
76256   }
76257 
76258   /* Resolve the ORDER BY on a compound SELECT after all terms of
76259   ** the compound have been resolved.
76260   */
76261   if( isCompound && resolveCompoundOrderBy(pParse, pLeftmost) ){
76262     return WRC_Abort;
76263   }
76264 
76265   return WRC_Prune;
76266 }
76267 
76268 /*
76269 ** This routine walks an expression tree and resolves references to
76270 ** table columns and result-set columns.  At the same time, do error
76271 ** checking on function usage and set a flag if any aggregate functions
76272 ** are seen.
76273 **
76274 ** To resolve table columns references we look for nodes (or subtrees) of the
76275 ** form X.Y.Z or Y.Z or just Z where
76276 **
76277 **      X:   The name of a database.  Ex:  "main" or "temp" or
76278 **           the symbolic name assigned to an ATTACH-ed database.
76279 **
76280 **      Y:   The name of a table in a FROM clause.  Or in a trigger
76281 **           one of the special names "old" or "new".
76282 **
76283 **      Z:   The name of a column in table Y.
76284 **
76285 ** The node at the root of the subtree is modified as follows:
76286 **
76287 **    Expr.op        Changed to TK_COLUMN
76288 **    Expr.pTab      Points to the Table object for X.Y
76289 **    Expr.iColumn   The column index in X.Y.  -1 for the rowid.
76290 **    Expr.iTable    The VDBE cursor number for X.Y
76291 **
76292 **
76293 ** To resolve result-set references, look for expression nodes of the
76294 ** form Z (with no X and Y prefix) where the Z matches the right-hand
76295 ** size of an AS clause in the result-set of a SELECT.  The Z expression
76296 ** is replaced by a copy of the left-hand side of the result-set expression.
76297 ** Table-name and function resolution occurs on the substituted expression
76298 ** tree.  For example, in:
76299 **
76300 **      SELECT a+b AS x, c+d AS y FROM t1 ORDER BY x;
76301 **
76302 ** The "x" term of the order by is replaced by "a+b" to render:
76303 **
76304 **      SELECT a+b AS x, c+d AS y FROM t1 ORDER BY a+b;
76305 **
76306 ** Function calls are checked to make sure that the function is
76307 ** defined and that the correct number of arguments are specified.
76308 ** If the function is an aggregate function, then the NC_HasAgg flag is
76309 ** set and the opcode is changed from TK_FUNCTION to TK_AGG_FUNCTION.
76310 ** If an expression contains aggregate functions then the EP_Agg
76311 ** property on the expression is set.
76312 **
76313 ** An error message is left in pParse if anything is amiss.  The number
76314 ** if errors is returned.
76315 */
76316 SQLITE_PRIVATE int sqlite3ResolveExprNames(
76317   NameContext *pNC,       /* Namespace to resolve expressions in. */
76318   Expr *pExpr             /* The expression to be analyzed. */
76319 ){
76320   u8 savedHasAgg;
76321   Walker w;
76322 
76323   if( pExpr==0 ) return 0;
76324 #if SQLITE_MAX_EXPR_DEPTH>0
76325   {
76326     Parse *pParse = pNC->pParse;
76327     if( sqlite3ExprCheckHeight(pParse, pExpr->nHeight+pNC->pParse->nHeight) ){
76328       return 1;
76329     }
76330     pParse->nHeight += pExpr->nHeight;
76331   }
76332 #endif
76333   savedHasAgg = pNC->ncFlags & NC_HasAgg;
76334   pNC->ncFlags &= ~NC_HasAgg;
76335   memset(&w, 0, sizeof(w));
76336   w.xExprCallback = resolveExprStep;
76337   w.xSelectCallback = resolveSelectStep;
76338   w.pParse = pNC->pParse;
76339   w.u.pNC = pNC;
76340   sqlite3WalkExpr(&w, pExpr);
76341 #if SQLITE_MAX_EXPR_DEPTH>0
76342   pNC->pParse->nHeight -= pExpr->nHeight;
76343 #endif
76344   if( pNC->nErr>0 || w.pParse->nErr>0 ){
76345     ExprSetProperty(pExpr, EP_Error);
76346   }
76347   if( pNC->ncFlags & NC_HasAgg ){
76348     ExprSetProperty(pExpr, EP_Agg);
76349   }else if( savedHasAgg ){
76350     pNC->ncFlags |= NC_HasAgg;
76351   }
76352   return ExprHasProperty(pExpr, EP_Error);
76353 }
76354 
76355 
76356 /*
76357 ** Resolve all names in all expressions of a SELECT and in all
76358 ** decendents of the SELECT, including compounds off of p->pPrior,
76359 ** subqueries in expressions, and subqueries used as FROM clause
76360 ** terms.
76361 **
76362 ** See sqlite3ResolveExprNames() for a description of the kinds of
76363 ** transformations that occur.
76364 **
76365 ** All SELECT statements should have been expanded using
76366 ** sqlite3SelectExpand() prior to invoking this routine.
76367 */
76368 SQLITE_PRIVATE void sqlite3ResolveSelectNames(
76369   Parse *pParse,         /* The parser context */
76370   Select *p,             /* The SELECT statement being coded. */
76371   NameContext *pOuterNC  /* Name context for parent SELECT statement */
76372 ){
76373   Walker w;
76374 
76375   assert( p!=0 );
76376   memset(&w, 0, sizeof(w));
76377   w.xExprCallback = resolveExprStep;
76378   w.xSelectCallback = resolveSelectStep;
76379   w.pParse = pParse;
76380   w.u.pNC = pOuterNC;
76381   sqlite3WalkSelect(&w, p);
76382 }
76383 
76384 /*
76385 ** Resolve names in expressions that can only reference a single table:
76386 **
76387 **    *   CHECK constraints
76388 **    *   WHERE clauses on partial indices
76389 **
76390 ** The Expr.iTable value for Expr.op==TK_COLUMN nodes of the expression
76391 ** is set to -1 and the Expr.iColumn value is set to the column number.
76392 **
76393 ** Any errors cause an error message to be set in pParse.
76394 */
76395 SQLITE_PRIVATE void sqlite3ResolveSelfReference(
76396   Parse *pParse,      /* Parsing context */
76397   Table *pTab,        /* The table being referenced */
76398   int type,           /* NC_IsCheck or NC_PartIdx */
76399   Expr *pExpr,        /* Expression to resolve.  May be NULL. */
76400   ExprList *pList     /* Expression list to resolve.  May be NUL. */
76401 ){
76402   SrcList sSrc;                   /* Fake SrcList for pParse->pNewTable */
76403   NameContext sNC;                /* Name context for pParse->pNewTable */
76404   int i;                          /* Loop counter */
76405 
76406   assert( type==NC_IsCheck || type==NC_PartIdx );
76407   memset(&sNC, 0, sizeof(sNC));
76408   memset(&sSrc, 0, sizeof(sSrc));
76409   sSrc.nSrc = 1;
76410   sSrc.a[0].zName = pTab->zName;
76411   sSrc.a[0].pTab = pTab;
76412   sSrc.a[0].iCursor = -1;
76413   sNC.pParse = pParse;
76414   sNC.pSrcList = &sSrc;
76415   sNC.ncFlags = type;
76416   if( sqlite3ResolveExprNames(&sNC, pExpr) ) return;
76417   if( pList ){
76418     for(i=0; i<pList->nExpr; i++){
76419       if( sqlite3ResolveExprNames(&sNC, pList->a[i].pExpr) ){
76420         return;
76421       }
76422     }
76423   }
76424 }
76425 
76426 /************** End of resolve.c *********************************************/
76427 /************** Begin file expr.c ********************************************/
76428 /*
76429 ** 2001 September 15
76430 **
76431 ** The author disclaims copyright to this source code.  In place of
76432 ** a legal notice, here is a blessing:
76433 **
76434 **    May you do good and not evil.
76435 **    May you find forgiveness for yourself and forgive others.
76436 **    May you share freely, never taking more than you give.
76437 **
76438 *************************************************************************
76439 ** This file contains routines used for analyzing expressions and
76440 ** for generating VDBE code that evaluates expressions in SQLite.
76441 */
76442 
76443 /*
76444 ** Return the 'affinity' of the expression pExpr if any.
76445 **
76446 ** If pExpr is a column, a reference to a column via an 'AS' alias,
76447 ** or a sub-select with a column as the return value, then the
76448 ** affinity of that column is returned. Otherwise, 0x00 is returned,
76449 ** indicating no affinity for the expression.
76450 **
76451 ** i.e. the WHERE clause expresssions in the following statements all
76452 ** have an affinity:
76453 **
76454 ** CREATE TABLE t1(a);
76455 ** SELECT * FROM t1 WHERE a;
76456 ** SELECT a AS b FROM t1 WHERE b;
76457 ** SELECT * FROM t1 WHERE (select a from t1);
76458 */
76459 SQLITE_PRIVATE char sqlite3ExprAffinity(Expr *pExpr){
76460   int op;
76461   pExpr = sqlite3ExprSkipCollate(pExpr);
76462   op = pExpr->op;
76463   if( op==TK_SELECT ){
76464     assert( pExpr->flags&EP_xIsSelect );
76465     return sqlite3ExprAffinity(pExpr->x.pSelect->pEList->a[0].pExpr);
76466   }
76467 #ifndef SQLITE_OMIT_CAST
76468   if( op==TK_CAST ){
76469     assert( !ExprHasProperty(pExpr, EP_IntValue) );
76470     return sqlite3AffinityType(pExpr->u.zToken, 0);
76471   }
76472 #endif
76473   if( (op==TK_AGG_COLUMN || op==TK_COLUMN || op==TK_REGISTER)
76474    && pExpr->pTab!=0
76475   ){
76476     /* op==TK_REGISTER && pExpr->pTab!=0 happens when pExpr was originally
76477     ** a TK_COLUMN but was previously evaluated and cached in a register */
76478     int j = pExpr->iColumn;
76479     if( j<0 ) return SQLITE_AFF_INTEGER;
76480     assert( pExpr->pTab && j<pExpr->pTab->nCol );
76481     return pExpr->pTab->aCol[j].affinity;
76482   }
76483   return pExpr->affinity;
76484 }
76485 
76486 /*
76487 ** Set the collating sequence for expression pExpr to be the collating
76488 ** sequence named by pToken.   Return a pointer to a new Expr node that
76489 ** implements the COLLATE operator.
76490 **
76491 ** If a memory allocation error occurs, that fact is recorded in pParse->db
76492 ** and the pExpr parameter is returned unchanged.
76493 */
76494 SQLITE_PRIVATE Expr *sqlite3ExprAddCollateToken(Parse *pParse, Expr *pExpr, Token *pCollName){
76495   if( pCollName->n>0 ){
76496     Expr *pNew = sqlite3ExprAlloc(pParse->db, TK_COLLATE, pCollName, 1);
76497     if( pNew ){
76498       pNew->pLeft = pExpr;
76499       pNew->flags |= EP_Collate|EP_Skip;
76500       pExpr = pNew;
76501     }
76502   }
76503   return pExpr;
76504 }
76505 SQLITE_PRIVATE Expr *sqlite3ExprAddCollateString(Parse *pParse, Expr *pExpr, const char *zC){
76506   Token s;
76507   assert( zC!=0 );
76508   s.z = zC;
76509   s.n = sqlite3Strlen30(s.z);
76510   return sqlite3ExprAddCollateToken(pParse, pExpr, &s);
76511 }
76512 
76513 /*
76514 ** Skip over any TK_COLLATE or TK_AS operators and any unlikely()
76515 ** or likelihood() function at the root of an expression.
76516 */
76517 SQLITE_PRIVATE Expr *sqlite3ExprSkipCollate(Expr *pExpr){
76518   while( pExpr && ExprHasProperty(pExpr, EP_Skip) ){
76519     if( ExprHasProperty(pExpr, EP_Unlikely) ){
76520       assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
76521       assert( pExpr->x.pList->nExpr>0 );
76522       assert( pExpr->op==TK_FUNCTION );
76523       pExpr = pExpr->x.pList->a[0].pExpr;
76524     }else{
76525       assert( pExpr->op==TK_COLLATE || pExpr->op==TK_AS );
76526       pExpr = pExpr->pLeft;
76527     }
76528   }
76529   return pExpr;
76530 }
76531 
76532 /*
76533 ** Return the collation sequence for the expression pExpr. If
76534 ** there is no defined collating sequence, return NULL.
76535 **
76536 ** The collating sequence might be determined by a COLLATE operator
76537 ** or by the presence of a column with a defined collating sequence.
76538 ** COLLATE operators take first precedence.  Left operands take
76539 ** precedence over right operands.
76540 */
76541 SQLITE_PRIVATE CollSeq *sqlite3ExprCollSeq(Parse *pParse, Expr *pExpr){
76542   sqlite3 *db = pParse->db;
76543   CollSeq *pColl = 0;
76544   Expr *p = pExpr;
76545   while( p ){
76546     int op = p->op;
76547     if( op==TK_CAST || op==TK_UPLUS ){
76548       p = p->pLeft;
76549       continue;
76550     }
76551     if( op==TK_COLLATE || (op==TK_REGISTER && p->op2==TK_COLLATE) ){
76552       pColl = sqlite3GetCollSeq(pParse, ENC(db), 0, p->u.zToken);
76553       break;
76554     }
76555     if( p->pTab!=0
76556      && (op==TK_AGG_COLUMN || op==TK_COLUMN
76557           || op==TK_REGISTER || op==TK_TRIGGER)
76558     ){
76559       /* op==TK_REGISTER && p->pTab!=0 happens when pExpr was originally
76560       ** a TK_COLUMN but was previously evaluated and cached in a register */
76561       int j = p->iColumn;
76562       if( j>=0 ){
76563         const char *zColl = p->pTab->aCol[j].zColl;
76564         pColl = sqlite3FindCollSeq(db, ENC(db), zColl, 0);
76565       }
76566       break;
76567     }
76568     if( p->flags & EP_Collate ){
76569       if( ALWAYS(p->pLeft) && (p->pLeft->flags & EP_Collate)!=0 ){
76570         p = p->pLeft;
76571       }else{
76572         p = p->pRight;
76573       }
76574     }else{
76575       break;
76576     }
76577   }
76578   if( sqlite3CheckCollSeq(pParse, pColl) ){
76579     pColl = 0;
76580   }
76581   return pColl;
76582 }
76583 
76584 /*
76585 ** pExpr is an operand of a comparison operator.  aff2 is the
76586 ** type affinity of the other operand.  This routine returns the
76587 ** type affinity that should be used for the comparison operator.
76588 */
76589 SQLITE_PRIVATE char sqlite3CompareAffinity(Expr *pExpr, char aff2){
76590   char aff1 = sqlite3ExprAffinity(pExpr);
76591   if( aff1 && aff2 ){
76592     /* Both sides of the comparison are columns. If one has numeric
76593     ** affinity, use that. Otherwise use no affinity.
76594     */
76595     if( sqlite3IsNumericAffinity(aff1) || sqlite3IsNumericAffinity(aff2) ){
76596       return SQLITE_AFF_NUMERIC;
76597     }else{
76598       return SQLITE_AFF_NONE;
76599     }
76600   }else if( !aff1 && !aff2 ){
76601     /* Neither side of the comparison is a column.  Compare the
76602     ** results directly.
76603     */
76604     return SQLITE_AFF_NONE;
76605   }else{
76606     /* One side is a column, the other is not. Use the columns affinity. */
76607     assert( aff1==0 || aff2==0 );
76608     return (aff1 + aff2);
76609   }
76610 }
76611 
76612 /*
76613 ** pExpr is a comparison operator.  Return the type affinity that should
76614 ** be applied to both operands prior to doing the comparison.
76615 */
76616 static char comparisonAffinity(Expr *pExpr){
76617   char aff;
76618   assert( pExpr->op==TK_EQ || pExpr->op==TK_IN || pExpr->op==TK_LT ||
76619           pExpr->op==TK_GT || pExpr->op==TK_GE || pExpr->op==TK_LE ||
76620           pExpr->op==TK_NE || pExpr->op==TK_IS || pExpr->op==TK_ISNOT );
76621   assert( pExpr->pLeft );
76622   aff = sqlite3ExprAffinity(pExpr->pLeft);
76623   if( pExpr->pRight ){
76624     aff = sqlite3CompareAffinity(pExpr->pRight, aff);
76625   }else if( ExprHasProperty(pExpr, EP_xIsSelect) ){
76626     aff = sqlite3CompareAffinity(pExpr->x.pSelect->pEList->a[0].pExpr, aff);
76627   }else if( !aff ){
76628     aff = SQLITE_AFF_NONE;
76629   }
76630   return aff;
76631 }
76632 
76633 /*
76634 ** pExpr is a comparison expression, eg. '=', '<', IN(...) etc.
76635 ** idx_affinity is the affinity of an indexed column. Return true
76636 ** if the index with affinity idx_affinity may be used to implement
76637 ** the comparison in pExpr.
76638 */
76639 SQLITE_PRIVATE int sqlite3IndexAffinityOk(Expr *pExpr, char idx_affinity){
76640   char aff = comparisonAffinity(pExpr);
76641   switch( aff ){
76642     case SQLITE_AFF_NONE:
76643       return 1;
76644     case SQLITE_AFF_TEXT:
76645       return idx_affinity==SQLITE_AFF_TEXT;
76646     default:
76647       return sqlite3IsNumericAffinity(idx_affinity);
76648   }
76649 }
76650 
76651 /*
76652 ** Return the P5 value that should be used for a binary comparison
76653 ** opcode (OP_Eq, OP_Ge etc.) used to compare pExpr1 and pExpr2.
76654 */
76655 static u8 binaryCompareP5(Expr *pExpr1, Expr *pExpr2, int jumpIfNull){
76656   u8 aff = (char)sqlite3ExprAffinity(pExpr2);
76657   aff = (u8)sqlite3CompareAffinity(pExpr1, aff) | (u8)jumpIfNull;
76658   return aff;
76659 }
76660 
76661 /*
76662 ** Return a pointer to the collation sequence that should be used by
76663 ** a binary comparison operator comparing pLeft and pRight.
76664 **
76665 ** If the left hand expression has a collating sequence type, then it is
76666 ** used. Otherwise the collation sequence for the right hand expression
76667 ** is used, or the default (BINARY) if neither expression has a collating
76668 ** type.
76669 **
76670 ** Argument pRight (but not pLeft) may be a null pointer. In this case,
76671 ** it is not considered.
76672 */
76673 SQLITE_PRIVATE CollSeq *sqlite3BinaryCompareCollSeq(
76674   Parse *pParse,
76675   Expr *pLeft,
76676   Expr *pRight
76677 ){
76678   CollSeq *pColl;
76679   assert( pLeft );
76680   if( pLeft->flags & EP_Collate ){
76681     pColl = sqlite3ExprCollSeq(pParse, pLeft);
76682   }else if( pRight && (pRight->flags & EP_Collate)!=0 ){
76683     pColl = sqlite3ExprCollSeq(pParse, pRight);
76684   }else{
76685     pColl = sqlite3ExprCollSeq(pParse, pLeft);
76686     if( !pColl ){
76687       pColl = sqlite3ExprCollSeq(pParse, pRight);
76688     }
76689   }
76690   return pColl;
76691 }
76692 
76693 /*
76694 ** Generate code for a comparison operator.
76695 */
76696 static int codeCompare(
76697   Parse *pParse,    /* The parsing (and code generating) context */
76698   Expr *pLeft,      /* The left operand */
76699   Expr *pRight,     /* The right operand */
76700   int opcode,       /* The comparison opcode */
76701   int in1, int in2, /* Register holding operands */
76702   int dest,         /* Jump here if true.  */
76703   int jumpIfNull    /* If true, jump if either operand is NULL */
76704 ){
76705   int p5;
76706   int addr;
76707   CollSeq *p4;
76708 
76709   p4 = sqlite3BinaryCompareCollSeq(pParse, pLeft, pRight);
76710   p5 = binaryCompareP5(pLeft, pRight, jumpIfNull);
76711   addr = sqlite3VdbeAddOp4(pParse->pVdbe, opcode, in2, dest, in1,
76712                            (void*)p4, P4_COLLSEQ);
76713   sqlite3VdbeChangeP5(pParse->pVdbe, (u8)p5);
76714   return addr;
76715 }
76716 
76717 #if SQLITE_MAX_EXPR_DEPTH>0
76718 /*
76719 ** Check that argument nHeight is less than or equal to the maximum
76720 ** expression depth allowed. If it is not, leave an error message in
76721 ** pParse.
76722 */
76723 SQLITE_PRIVATE int sqlite3ExprCheckHeight(Parse *pParse, int nHeight){
76724   int rc = SQLITE_OK;
76725   int mxHeight = pParse->db->aLimit[SQLITE_LIMIT_EXPR_DEPTH];
76726   if( nHeight>mxHeight ){
76727     sqlite3ErrorMsg(pParse,
76728        "Expression tree is too large (maximum depth %d)", mxHeight
76729     );
76730     rc = SQLITE_ERROR;
76731   }
76732   return rc;
76733 }
76734 
76735 /* The following three functions, heightOfExpr(), heightOfExprList()
76736 ** and heightOfSelect(), are used to determine the maximum height
76737 ** of any expression tree referenced by the structure passed as the
76738 ** first argument.
76739 **
76740 ** If this maximum height is greater than the current value pointed
76741 ** to by pnHeight, the second parameter, then set *pnHeight to that
76742 ** value.
76743 */
76744 static void heightOfExpr(Expr *p, int *pnHeight){
76745   if( p ){
76746     if( p->nHeight>*pnHeight ){
76747       *pnHeight = p->nHeight;
76748     }
76749   }
76750 }
76751 static void heightOfExprList(ExprList *p, int *pnHeight){
76752   if( p ){
76753     int i;
76754     for(i=0; i<p->nExpr; i++){
76755       heightOfExpr(p->a[i].pExpr, pnHeight);
76756     }
76757   }
76758 }
76759 static void heightOfSelect(Select *p, int *pnHeight){
76760   if( p ){
76761     heightOfExpr(p->pWhere, pnHeight);
76762     heightOfExpr(p->pHaving, pnHeight);
76763     heightOfExpr(p->pLimit, pnHeight);
76764     heightOfExpr(p->pOffset, pnHeight);
76765     heightOfExprList(p->pEList, pnHeight);
76766     heightOfExprList(p->pGroupBy, pnHeight);
76767     heightOfExprList(p->pOrderBy, pnHeight);
76768     heightOfSelect(p->pPrior, pnHeight);
76769   }
76770 }
76771 
76772 /*
76773 ** Set the Expr.nHeight variable in the structure passed as an
76774 ** argument. An expression with no children, Expr.pList or
76775 ** Expr.pSelect member has a height of 1. Any other expression
76776 ** has a height equal to the maximum height of any other
76777 ** referenced Expr plus one.
76778 */
76779 static void exprSetHeight(Expr *p){
76780   int nHeight = 0;
76781   heightOfExpr(p->pLeft, &nHeight);
76782   heightOfExpr(p->pRight, &nHeight);
76783   if( ExprHasProperty(p, EP_xIsSelect) ){
76784     heightOfSelect(p->x.pSelect, &nHeight);
76785   }else{
76786     heightOfExprList(p->x.pList, &nHeight);
76787   }
76788   p->nHeight = nHeight + 1;
76789 }
76790 
76791 /*
76792 ** Set the Expr.nHeight variable using the exprSetHeight() function. If
76793 ** the height is greater than the maximum allowed expression depth,
76794 ** leave an error in pParse.
76795 */
76796 SQLITE_PRIVATE void sqlite3ExprSetHeight(Parse *pParse, Expr *p){
76797   exprSetHeight(p);
76798   sqlite3ExprCheckHeight(pParse, p->nHeight);
76799 }
76800 
76801 /*
76802 ** Return the maximum height of any expression tree referenced
76803 ** by the select statement passed as an argument.
76804 */
76805 SQLITE_PRIVATE int sqlite3SelectExprHeight(Select *p){
76806   int nHeight = 0;
76807   heightOfSelect(p, &nHeight);
76808   return nHeight;
76809 }
76810 #else
76811   #define exprSetHeight(y)
76812 #endif /* SQLITE_MAX_EXPR_DEPTH>0 */
76813 
76814 /*
76815 ** This routine is the core allocator for Expr nodes.
76816 **
76817 ** Construct a new expression node and return a pointer to it.  Memory
76818 ** for this node and for the pToken argument is a single allocation
76819 ** obtained from sqlite3DbMalloc().  The calling function
76820 ** is responsible for making sure the node eventually gets freed.
76821 **
76822 ** If dequote is true, then the token (if it exists) is dequoted.
76823 ** If dequote is false, no dequoting is performance.  The deQuote
76824 ** parameter is ignored if pToken is NULL or if the token does not
76825 ** appear to be quoted.  If the quotes were of the form "..." (double-quotes)
76826 ** then the EP_DblQuoted flag is set on the expression node.
76827 **
76828 ** Special case:  If op==TK_INTEGER and pToken points to a string that
76829 ** can be translated into a 32-bit integer, then the token is not
76830 ** stored in u.zToken.  Instead, the integer values is written
76831 ** into u.iValue and the EP_IntValue flag is set.  No extra storage
76832 ** is allocated to hold the integer text and the dequote flag is ignored.
76833 */
76834 SQLITE_PRIVATE Expr *sqlite3ExprAlloc(
76835   sqlite3 *db,            /* Handle for sqlite3DbMallocZero() (may be null) */
76836   int op,                 /* Expression opcode */
76837   const Token *pToken,    /* Token argument.  Might be NULL */
76838   int dequote             /* True to dequote */
76839 ){
76840   Expr *pNew;
76841   int nExtra = 0;
76842   int iValue = 0;
76843 
76844   if( pToken ){
76845     if( op!=TK_INTEGER || pToken->z==0
76846           || sqlite3GetInt32(pToken->z, &iValue)==0 ){
76847       nExtra = pToken->n+1;
76848       assert( iValue>=0 );
76849     }
76850   }
76851   pNew = sqlite3DbMallocZero(db, sizeof(Expr)+nExtra);
76852   if( pNew ){
76853     pNew->op = (u8)op;
76854     pNew->iAgg = -1;
76855     if( pToken ){
76856       if( nExtra==0 ){
76857         pNew->flags |= EP_IntValue;
76858         pNew->u.iValue = iValue;
76859       }else{
76860         int c;
76861         pNew->u.zToken = (char*)&pNew[1];
76862         assert( pToken->z!=0 || pToken->n==0 );
76863         if( pToken->n ) memcpy(pNew->u.zToken, pToken->z, pToken->n);
76864         pNew->u.zToken[pToken->n] = 0;
76865         if( dequote && nExtra>=3
76866              && ((c = pToken->z[0])=='\'' || c=='"' || c=='[' || c=='`') ){
76867           sqlite3Dequote(pNew->u.zToken);
76868           if( c=='"' ) pNew->flags |= EP_DblQuoted;
76869         }
76870       }
76871     }
76872 #if SQLITE_MAX_EXPR_DEPTH>0
76873     pNew->nHeight = 1;
76874 #endif
76875   }
76876   return pNew;
76877 }
76878 
76879 /*
76880 ** Allocate a new expression node from a zero-terminated token that has
76881 ** already been dequoted.
76882 */
76883 SQLITE_PRIVATE Expr *sqlite3Expr(
76884   sqlite3 *db,            /* Handle for sqlite3DbMallocZero() (may be null) */
76885   int op,                 /* Expression opcode */
76886   const char *zToken      /* Token argument.  Might be NULL */
76887 ){
76888   Token x;
76889   x.z = zToken;
76890   x.n = zToken ? sqlite3Strlen30(zToken) : 0;
76891   return sqlite3ExprAlloc(db, op, &x, 0);
76892 }
76893 
76894 /*
76895 ** Attach subtrees pLeft and pRight to the Expr node pRoot.
76896 **
76897 ** If pRoot==NULL that means that a memory allocation error has occurred.
76898 ** In that case, delete the subtrees pLeft and pRight.
76899 */
76900 SQLITE_PRIVATE void sqlite3ExprAttachSubtrees(
76901   sqlite3 *db,
76902   Expr *pRoot,
76903   Expr *pLeft,
76904   Expr *pRight
76905 ){
76906   if( pRoot==0 ){
76907     assert( db->mallocFailed );
76908     sqlite3ExprDelete(db, pLeft);
76909     sqlite3ExprDelete(db, pRight);
76910   }else{
76911     if( pRight ){
76912       pRoot->pRight = pRight;
76913       pRoot->flags |= EP_Collate & pRight->flags;
76914     }
76915     if( pLeft ){
76916       pRoot->pLeft = pLeft;
76917       pRoot->flags |= EP_Collate & pLeft->flags;
76918     }
76919     exprSetHeight(pRoot);
76920   }
76921 }
76922 
76923 /*
76924 ** Allocate a Expr node which joins as many as two subtrees.
76925 **
76926 ** One or both of the subtrees can be NULL.  Return a pointer to the new
76927 ** Expr node.  Or, if an OOM error occurs, set pParse->db->mallocFailed,
76928 ** free the subtrees and return NULL.
76929 */
76930 SQLITE_PRIVATE Expr *sqlite3PExpr(
76931   Parse *pParse,          /* Parsing context */
76932   int op,                 /* Expression opcode */
76933   Expr *pLeft,            /* Left operand */
76934   Expr *pRight,           /* Right operand */
76935   const Token *pToken     /* Argument token */
76936 ){
76937   Expr *p;
76938   if( op==TK_AND && pLeft && pRight ){
76939     /* Take advantage of short-circuit false optimization for AND */
76940     p = sqlite3ExprAnd(pParse->db, pLeft, pRight);
76941   }else{
76942     p = sqlite3ExprAlloc(pParse->db, op, pToken, 1);
76943     sqlite3ExprAttachSubtrees(pParse->db, p, pLeft, pRight);
76944   }
76945   if( p ) {
76946     sqlite3ExprCheckHeight(pParse, p->nHeight);
76947   }
76948   return p;
76949 }
76950 
76951 /*
76952 ** If the expression is always either TRUE or FALSE (respectively),
76953 ** then return 1.  If one cannot determine the truth value of the
76954 ** expression at compile-time return 0.
76955 **
76956 ** This is an optimization.  If is OK to return 0 here even if
76957 ** the expression really is always false or false (a false negative).
76958 ** But it is a bug to return 1 if the expression might have different
76959 ** boolean values in different circumstances (a false positive.)
76960 **
76961 ** Note that if the expression is part of conditional for a
76962 ** LEFT JOIN, then we cannot determine at compile-time whether or not
76963 ** is it true or false, so always return 0.
76964 */
76965 static int exprAlwaysTrue(Expr *p){
76966   int v = 0;
76967   if( ExprHasProperty(p, EP_FromJoin) ) return 0;
76968   if( !sqlite3ExprIsInteger(p, &v) ) return 0;
76969   return v!=0;
76970 }
76971 static int exprAlwaysFalse(Expr *p){
76972   int v = 0;
76973   if( ExprHasProperty(p, EP_FromJoin) ) return 0;
76974   if( !sqlite3ExprIsInteger(p, &v) ) return 0;
76975   return v==0;
76976 }
76977 
76978 /*
76979 ** Join two expressions using an AND operator.  If either expression is
76980 ** NULL, then just return the other expression.
76981 **
76982 ** If one side or the other of the AND is known to be false, then instead
76983 ** of returning an AND expression, just return a constant expression with
76984 ** a value of false.
76985 */
76986 SQLITE_PRIVATE Expr *sqlite3ExprAnd(sqlite3 *db, Expr *pLeft, Expr *pRight){
76987   if( pLeft==0 ){
76988     return pRight;
76989   }else if( pRight==0 ){
76990     return pLeft;
76991   }else if( exprAlwaysFalse(pLeft) || exprAlwaysFalse(pRight) ){
76992     sqlite3ExprDelete(db, pLeft);
76993     sqlite3ExprDelete(db, pRight);
76994     return sqlite3ExprAlloc(db, TK_INTEGER, &sqlite3IntTokens[0], 0);
76995   }else{
76996     Expr *pNew = sqlite3ExprAlloc(db, TK_AND, 0, 0);
76997     sqlite3ExprAttachSubtrees(db, pNew, pLeft, pRight);
76998     return pNew;
76999   }
77000 }
77001 
77002 /*
77003 ** Construct a new expression node for a function with multiple
77004 ** arguments.
77005 */
77006 SQLITE_PRIVATE Expr *sqlite3ExprFunction(Parse *pParse, ExprList *pList, Token *pToken){
77007   Expr *pNew;
77008   sqlite3 *db = pParse->db;
77009   assert( pToken );
77010   pNew = sqlite3ExprAlloc(db, TK_FUNCTION, pToken, 1);
77011   if( pNew==0 ){
77012     sqlite3ExprListDelete(db, pList); /* Avoid memory leak when malloc fails */
77013     return 0;
77014   }
77015   pNew->x.pList = pList;
77016   assert( !ExprHasProperty(pNew, EP_xIsSelect) );
77017   sqlite3ExprSetHeight(pParse, pNew);
77018   return pNew;
77019 }
77020 
77021 /*
77022 ** Assign a variable number to an expression that encodes a wildcard
77023 ** in the original SQL statement.
77024 **
77025 ** Wildcards consisting of a single "?" are assigned the next sequential
77026 ** variable number.
77027 **
77028 ** Wildcards of the form "?nnn" are assigned the number "nnn".  We make
77029 ** sure "nnn" is not too be to avoid a denial of service attack when
77030 ** the SQL statement comes from an external source.
77031 **
77032 ** Wildcards of the form ":aaa", "@aaa", or "$aaa" are assigned the same number
77033 ** as the previous instance of the same wildcard.  Or if this is the first
77034 ** instance of the wildcard, the next sequenial variable number is
77035 ** assigned.
77036 */
77037 SQLITE_PRIVATE void sqlite3ExprAssignVarNumber(Parse *pParse, Expr *pExpr){
77038   sqlite3 *db = pParse->db;
77039   const char *z;
77040 
77041   if( pExpr==0 ) return;
77042   assert( !ExprHasProperty(pExpr, EP_IntValue|EP_Reduced|EP_TokenOnly) );
77043   z = pExpr->u.zToken;
77044   assert( z!=0 );
77045   assert( z[0]!=0 );
77046   if( z[1]==0 ){
77047     /* Wildcard of the form "?".  Assign the next variable number */
77048     assert( z[0]=='?' );
77049     pExpr->iColumn = (ynVar)(++pParse->nVar);
77050   }else{
77051     ynVar x = 0;
77052     u32 n = sqlite3Strlen30(z);
77053     if( z[0]=='?' ){
77054       /* Wildcard of the form "?nnn".  Convert "nnn" to an integer and
77055       ** use it as the variable number */
77056       i64 i;
77057       int bOk = 0==sqlite3Atoi64(&z[1], &i, n-1, SQLITE_UTF8);
77058       pExpr->iColumn = x = (ynVar)i;
77059       testcase( i==0 );
77060       testcase( i==1 );
77061       testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]-1 );
77062       testcase( i==db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] );
77063       if( bOk==0 || i<1 || i>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
77064         sqlite3ErrorMsg(pParse, "variable number must be between ?1 and ?%d",
77065             db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER]);
77066         x = 0;
77067       }
77068       if( i>pParse->nVar ){
77069         pParse->nVar = (int)i;
77070       }
77071     }else{
77072       /* Wildcards like ":aaa", "$aaa" or "@aaa".  Reuse the same variable
77073       ** number as the prior appearance of the same name, or if the name
77074       ** has never appeared before, reuse the same variable number
77075       */
77076       ynVar i;
77077       for(i=0; i<pParse->nzVar; i++){
77078         if( pParse->azVar[i] && strcmp(pParse->azVar[i],z)==0 ){
77079           pExpr->iColumn = x = (ynVar)i+1;
77080           break;
77081         }
77082       }
77083       if( x==0 ) x = pExpr->iColumn = (ynVar)(++pParse->nVar);
77084     }
77085     if( x>0 ){
77086       if( x>pParse->nzVar ){
77087         char **a;
77088         a = sqlite3DbRealloc(db, pParse->azVar, x*sizeof(a[0]));
77089         if( a==0 ) return;  /* Error reported through db->mallocFailed */
77090         pParse->azVar = a;
77091         memset(&a[pParse->nzVar], 0, (x-pParse->nzVar)*sizeof(a[0]));
77092         pParse->nzVar = x;
77093       }
77094       if( z[0]!='?' || pParse->azVar[x-1]==0 ){
77095         sqlite3DbFree(db, pParse->azVar[x-1]);
77096         pParse->azVar[x-1] = sqlite3DbStrNDup(db, z, n);
77097       }
77098     }
77099   }
77100   if( !pParse->nErr && pParse->nVar>db->aLimit[SQLITE_LIMIT_VARIABLE_NUMBER] ){
77101     sqlite3ErrorMsg(pParse, "too many SQL variables");
77102   }
77103 }
77104 
77105 /*
77106 ** Recursively delete an expression tree.
77107 */
77108 SQLITE_PRIVATE void sqlite3ExprDelete(sqlite3 *db, Expr *p){
77109   if( p==0 ) return;
77110   /* Sanity check: Assert that the IntValue is non-negative if it exists */
77111   assert( !ExprHasProperty(p, EP_IntValue) || p->u.iValue>=0 );
77112   if( !ExprHasProperty(p, EP_TokenOnly) ){
77113     /* The Expr.x union is never used at the same time as Expr.pRight */
77114     assert( p->x.pList==0 || p->pRight==0 );
77115     sqlite3ExprDelete(db, p->pLeft);
77116     sqlite3ExprDelete(db, p->pRight);
77117     if( ExprHasProperty(p, EP_MemToken) ) sqlite3DbFree(db, p->u.zToken);
77118     if( ExprHasProperty(p, EP_xIsSelect) ){
77119       sqlite3SelectDelete(db, p->x.pSelect);
77120     }else{
77121       sqlite3ExprListDelete(db, p->x.pList);
77122     }
77123   }
77124   if( !ExprHasProperty(p, EP_Static) ){
77125     sqlite3DbFree(db, p);
77126   }
77127 }
77128 
77129 /*
77130 ** Return the number of bytes allocated for the expression structure
77131 ** passed as the first argument. This is always one of EXPR_FULLSIZE,
77132 ** EXPR_REDUCEDSIZE or EXPR_TOKENONLYSIZE.
77133 */
77134 static int exprStructSize(Expr *p){
77135   if( ExprHasProperty(p, EP_TokenOnly) ) return EXPR_TOKENONLYSIZE;
77136   if( ExprHasProperty(p, EP_Reduced) ) return EXPR_REDUCEDSIZE;
77137   return EXPR_FULLSIZE;
77138 }
77139 
77140 /*
77141 ** The dupedExpr*Size() routines each return the number of bytes required
77142 ** to store a copy of an expression or expression tree.  They differ in
77143 ** how much of the tree is measured.
77144 **
77145 **     dupedExprStructSize()     Size of only the Expr structure
77146 **     dupedExprNodeSize()       Size of Expr + space for token
77147 **     dupedExprSize()           Expr + token + subtree components
77148 **
77149 ***************************************************************************
77150 **
77151 ** The dupedExprStructSize() function returns two values OR-ed together:
77152 ** (1) the space required for a copy of the Expr structure only and
77153 ** (2) the EP_xxx flags that indicate what the structure size should be.
77154 ** The return values is always one of:
77155 **
77156 **      EXPR_FULLSIZE
77157 **      EXPR_REDUCEDSIZE   | EP_Reduced
77158 **      EXPR_TOKENONLYSIZE | EP_TokenOnly
77159 **
77160 ** The size of the structure can be found by masking the return value
77161 ** of this routine with 0xfff.  The flags can be found by masking the
77162 ** return value with EP_Reduced|EP_TokenOnly.
77163 **
77164 ** Note that with flags==EXPRDUP_REDUCE, this routines works on full-size
77165 ** (unreduced) Expr objects as they or originally constructed by the parser.
77166 ** During expression analysis, extra information is computed and moved into
77167 ** later parts of teh Expr object and that extra information might get chopped
77168 ** off if the expression is reduced.  Note also that it does not work to
77169 ** make a EXPRDUP_REDUCE copy of a reduced expression.  It is only legal
77170 ** to reduce a pristine expression tree from the parser.  The implementation
77171 ** of dupedExprStructSize() contain multiple assert() statements that attempt
77172 ** to enforce this constraint.
77173 */
77174 static int dupedExprStructSize(Expr *p, int flags){
77175   int nSize;
77176   assert( flags==EXPRDUP_REDUCE || flags==0 ); /* Only one flag value allowed */
77177   assert( EXPR_FULLSIZE<=0xfff );
77178   assert( (0xfff & (EP_Reduced|EP_TokenOnly))==0 );
77179   if( 0==(flags&EXPRDUP_REDUCE) ){
77180     nSize = EXPR_FULLSIZE;
77181   }else{
77182     assert( !ExprHasProperty(p, EP_TokenOnly|EP_Reduced) );
77183     assert( !ExprHasProperty(p, EP_FromJoin) );
77184     assert( !ExprHasProperty(p, EP_MemToken) );
77185     assert( !ExprHasProperty(p, EP_NoReduce) );
77186     if( p->pLeft || p->x.pList ){
77187       nSize = EXPR_REDUCEDSIZE | EP_Reduced;
77188     }else{
77189       assert( p->pRight==0 );
77190       nSize = EXPR_TOKENONLYSIZE | EP_TokenOnly;
77191     }
77192   }
77193   return nSize;
77194 }
77195 
77196 /*
77197 ** This function returns the space in bytes required to store the copy
77198 ** of the Expr structure and a copy of the Expr.u.zToken string (if that
77199 ** string is defined.)
77200 */
77201 static int dupedExprNodeSize(Expr *p, int flags){
77202   int nByte = dupedExprStructSize(p, flags) & 0xfff;
77203   if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){
77204     nByte += sqlite3Strlen30(p->u.zToken)+1;
77205   }
77206   return ROUND8(nByte);
77207 }
77208 
77209 /*
77210 ** Return the number of bytes required to create a duplicate of the
77211 ** expression passed as the first argument. The second argument is a
77212 ** mask containing EXPRDUP_XXX flags.
77213 **
77214 ** The value returned includes space to create a copy of the Expr struct
77215 ** itself and the buffer referred to by Expr.u.zToken, if any.
77216 **
77217 ** If the EXPRDUP_REDUCE flag is set, then the return value includes
77218 ** space to duplicate all Expr nodes in the tree formed by Expr.pLeft
77219 ** and Expr.pRight variables (but not for any structures pointed to or
77220 ** descended from the Expr.x.pList or Expr.x.pSelect variables).
77221 */
77222 static int dupedExprSize(Expr *p, int flags){
77223   int nByte = 0;
77224   if( p ){
77225     nByte = dupedExprNodeSize(p, flags);
77226     if( flags&EXPRDUP_REDUCE ){
77227       nByte += dupedExprSize(p->pLeft, flags) + dupedExprSize(p->pRight, flags);
77228     }
77229   }
77230   return nByte;
77231 }
77232 
77233 /*
77234 ** This function is similar to sqlite3ExprDup(), except that if pzBuffer
77235 ** is not NULL then *pzBuffer is assumed to point to a buffer large enough
77236 ** to store the copy of expression p, the copies of p->u.zToken
77237 ** (if applicable), and the copies of the p->pLeft and p->pRight expressions,
77238 ** if any. Before returning, *pzBuffer is set to the first byte passed the
77239 ** portion of the buffer copied into by this function.
77240 */
77241 static Expr *exprDup(sqlite3 *db, Expr *p, int flags, u8 **pzBuffer){
77242   Expr *pNew = 0;                      /* Value to return */
77243   if( p ){
77244     const int isReduced = (flags&EXPRDUP_REDUCE);
77245     u8 *zAlloc;
77246     u32 staticFlag = 0;
77247 
77248     assert( pzBuffer==0 || isReduced );
77249 
77250     /* Figure out where to write the new Expr structure. */
77251     if( pzBuffer ){
77252       zAlloc = *pzBuffer;
77253       staticFlag = EP_Static;
77254     }else{
77255       zAlloc = sqlite3DbMallocRaw(db, dupedExprSize(p, flags));
77256     }
77257     pNew = (Expr *)zAlloc;
77258 
77259     if( pNew ){
77260       /* Set nNewSize to the size allocated for the structure pointed to
77261       ** by pNew. This is either EXPR_FULLSIZE, EXPR_REDUCEDSIZE or
77262       ** EXPR_TOKENONLYSIZE. nToken is set to the number of bytes consumed
77263       ** by the copy of the p->u.zToken string (if any).
77264       */
77265       const unsigned nStructSize = dupedExprStructSize(p, flags);
77266       const int nNewSize = nStructSize & 0xfff;
77267       int nToken;
77268       if( !ExprHasProperty(p, EP_IntValue) && p->u.zToken ){
77269         nToken = sqlite3Strlen30(p->u.zToken) + 1;
77270       }else{
77271         nToken = 0;
77272       }
77273       if( isReduced ){
77274         assert( ExprHasProperty(p, EP_Reduced)==0 );
77275         memcpy(zAlloc, p, nNewSize);
77276       }else{
77277         int nSize = exprStructSize(p);
77278         memcpy(zAlloc, p, nSize);
77279         memset(&zAlloc[nSize], 0, EXPR_FULLSIZE-nSize);
77280       }
77281 
77282       /* Set the EP_Reduced, EP_TokenOnly, and EP_Static flags appropriately. */
77283       pNew->flags &= ~(EP_Reduced|EP_TokenOnly|EP_Static|EP_MemToken);
77284       pNew->flags |= nStructSize & (EP_Reduced|EP_TokenOnly);
77285       pNew->flags |= staticFlag;
77286 
77287       /* Copy the p->u.zToken string, if any. */
77288       if( nToken ){
77289         char *zToken = pNew->u.zToken = (char*)&zAlloc[nNewSize];
77290         memcpy(zToken, p->u.zToken, nToken);
77291       }
77292 
77293       if( 0==((p->flags|pNew->flags) & EP_TokenOnly) ){
77294         /* Fill in the pNew->x.pSelect or pNew->x.pList member. */
77295         if( ExprHasProperty(p, EP_xIsSelect) ){
77296           pNew->x.pSelect = sqlite3SelectDup(db, p->x.pSelect, isReduced);
77297         }else{
77298           pNew->x.pList = sqlite3ExprListDup(db, p->x.pList, isReduced);
77299         }
77300       }
77301 
77302       /* Fill in pNew->pLeft and pNew->pRight. */
77303       if( ExprHasProperty(pNew, EP_Reduced|EP_TokenOnly) ){
77304         zAlloc += dupedExprNodeSize(p, flags);
77305         if( ExprHasProperty(pNew, EP_Reduced) ){
77306           pNew->pLeft = exprDup(db, p->pLeft, EXPRDUP_REDUCE, &zAlloc);
77307           pNew->pRight = exprDup(db, p->pRight, EXPRDUP_REDUCE, &zAlloc);
77308         }
77309         if( pzBuffer ){
77310           *pzBuffer = zAlloc;
77311         }
77312       }else{
77313         if( !ExprHasProperty(p, EP_TokenOnly) ){
77314           pNew->pLeft = sqlite3ExprDup(db, p->pLeft, 0);
77315           pNew->pRight = sqlite3ExprDup(db, p->pRight, 0);
77316         }
77317       }
77318 
77319     }
77320   }
77321   return pNew;
77322 }
77323 
77324 /*
77325 ** Create and return a deep copy of the object passed as the second
77326 ** argument. If an OOM condition is encountered, NULL is returned
77327 ** and the db->mallocFailed flag set.
77328 */
77329 #ifndef SQLITE_OMIT_CTE
77330 static With *withDup(sqlite3 *db, With *p){
77331   With *pRet = 0;
77332   if( p ){
77333     int nByte = sizeof(*p) + sizeof(p->a[0]) * (p->nCte-1);
77334     pRet = sqlite3DbMallocZero(db, nByte);
77335     if( pRet ){
77336       int i;
77337       pRet->nCte = p->nCte;
77338       for(i=0; i<p->nCte; i++){
77339         pRet->a[i].pSelect = sqlite3SelectDup(db, p->a[i].pSelect, 0);
77340         pRet->a[i].pCols = sqlite3ExprListDup(db, p->a[i].pCols, 0);
77341         pRet->a[i].zName = sqlite3DbStrDup(db, p->a[i].zName);
77342       }
77343     }
77344   }
77345   return pRet;
77346 }
77347 #else
77348 # define withDup(x,y) 0
77349 #endif
77350 
77351 /*
77352 ** The following group of routines make deep copies of expressions,
77353 ** expression lists, ID lists, and select statements.  The copies can
77354 ** be deleted (by being passed to their respective ...Delete() routines)
77355 ** without effecting the originals.
77356 **
77357 ** The expression list, ID, and source lists return by sqlite3ExprListDup(),
77358 ** sqlite3IdListDup(), and sqlite3SrcListDup() can not be further expanded
77359 ** by subsequent calls to sqlite*ListAppend() routines.
77360 **
77361 ** Any tables that the SrcList might point to are not duplicated.
77362 **
77363 ** The flags parameter contains a combination of the EXPRDUP_XXX flags.
77364 ** If the EXPRDUP_REDUCE flag is set, then the structure returned is a
77365 ** truncated version of the usual Expr structure that will be stored as
77366 ** part of the in-memory representation of the database schema.
77367 */
77368 SQLITE_PRIVATE Expr *sqlite3ExprDup(sqlite3 *db, Expr *p, int flags){
77369   return exprDup(db, p, flags, 0);
77370 }
77371 SQLITE_PRIVATE ExprList *sqlite3ExprListDup(sqlite3 *db, ExprList *p, int flags){
77372   ExprList *pNew;
77373   struct ExprList_item *pItem, *pOldItem;
77374   int i;
77375   if( p==0 ) return 0;
77376   pNew = sqlite3DbMallocRaw(db, sizeof(*pNew) );
77377   if( pNew==0 ) return 0;
77378   pNew->iECursor = 0;
77379   pNew->nExpr = i = p->nExpr;
77380   if( (flags & EXPRDUP_REDUCE)==0 ) for(i=1; i<p->nExpr; i+=i){}
77381   pNew->a = pItem = sqlite3DbMallocRaw(db,  i*sizeof(p->a[0]) );
77382   if( pItem==0 ){
77383     sqlite3DbFree(db, pNew);
77384     return 0;
77385   }
77386   pOldItem = p->a;
77387   for(i=0; i<p->nExpr; i++, pItem++, pOldItem++){
77388     Expr *pOldExpr = pOldItem->pExpr;
77389     pItem->pExpr = sqlite3ExprDup(db, pOldExpr, flags);
77390     pItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
77391     pItem->zSpan = sqlite3DbStrDup(db, pOldItem->zSpan);
77392     pItem->sortOrder = pOldItem->sortOrder;
77393     pItem->done = 0;
77394     pItem->bSpanIsTab = pOldItem->bSpanIsTab;
77395     pItem->u = pOldItem->u;
77396   }
77397   return pNew;
77398 }
77399 
77400 /*
77401 ** If cursors, triggers, views and subqueries are all omitted from
77402 ** the build, then none of the following routines, except for
77403 ** sqlite3SelectDup(), can be called. sqlite3SelectDup() is sometimes
77404 ** called with a NULL argument.
77405 */
77406 #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER) \
77407  || !defined(SQLITE_OMIT_SUBQUERY)
77408 SQLITE_PRIVATE SrcList *sqlite3SrcListDup(sqlite3 *db, SrcList *p, int flags){
77409   SrcList *pNew;
77410   int i;
77411   int nByte;
77412   if( p==0 ) return 0;
77413   nByte = sizeof(*p) + (p->nSrc>0 ? sizeof(p->a[0]) * (p->nSrc-1) : 0);
77414   pNew = sqlite3DbMallocRaw(db, nByte );
77415   if( pNew==0 ) return 0;
77416   pNew->nSrc = pNew->nAlloc = p->nSrc;
77417   for(i=0; i<p->nSrc; i++){
77418     struct SrcList_item *pNewItem = &pNew->a[i];
77419     struct SrcList_item *pOldItem = &p->a[i];
77420     Table *pTab;
77421     pNewItem->pSchema = pOldItem->pSchema;
77422     pNewItem->zDatabase = sqlite3DbStrDup(db, pOldItem->zDatabase);
77423     pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
77424     pNewItem->zAlias = sqlite3DbStrDup(db, pOldItem->zAlias);
77425     pNewItem->jointype = pOldItem->jointype;
77426     pNewItem->iCursor = pOldItem->iCursor;
77427     pNewItem->addrFillSub = pOldItem->addrFillSub;
77428     pNewItem->regReturn = pOldItem->regReturn;
77429     pNewItem->isCorrelated = pOldItem->isCorrelated;
77430     pNewItem->viaCoroutine = pOldItem->viaCoroutine;
77431     pNewItem->isRecursive = pOldItem->isRecursive;
77432     pNewItem->zIndex = sqlite3DbStrDup(db, pOldItem->zIndex);
77433     pNewItem->notIndexed = pOldItem->notIndexed;
77434     pNewItem->pIndex = pOldItem->pIndex;
77435     pTab = pNewItem->pTab = pOldItem->pTab;
77436     if( pTab ){
77437       pTab->nRef++;
77438     }
77439     pNewItem->pSelect = sqlite3SelectDup(db, pOldItem->pSelect, flags);
77440     pNewItem->pOn = sqlite3ExprDup(db, pOldItem->pOn, flags);
77441     pNewItem->pUsing = sqlite3IdListDup(db, pOldItem->pUsing);
77442     pNewItem->colUsed = pOldItem->colUsed;
77443   }
77444   return pNew;
77445 }
77446 SQLITE_PRIVATE IdList *sqlite3IdListDup(sqlite3 *db, IdList *p){
77447   IdList *pNew;
77448   int i;
77449   if( p==0 ) return 0;
77450   pNew = sqlite3DbMallocRaw(db, sizeof(*pNew) );
77451   if( pNew==0 ) return 0;
77452   pNew->nId = p->nId;
77453   pNew->a = sqlite3DbMallocRaw(db, p->nId*sizeof(p->a[0]) );
77454   if( pNew->a==0 ){
77455     sqlite3DbFree(db, pNew);
77456     return 0;
77457   }
77458   /* Note that because the size of the allocation for p->a[] is not
77459   ** necessarily a power of two, sqlite3IdListAppend() may not be called
77460   ** on the duplicate created by this function. */
77461   for(i=0; i<p->nId; i++){
77462     struct IdList_item *pNewItem = &pNew->a[i];
77463     struct IdList_item *pOldItem = &p->a[i];
77464     pNewItem->zName = sqlite3DbStrDup(db, pOldItem->zName);
77465     pNewItem->idx = pOldItem->idx;
77466   }
77467   return pNew;
77468 }
77469 SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3 *db, Select *p, int flags){
77470   Select *pNew, *pPrior;
77471   if( p==0 ) return 0;
77472   pNew = sqlite3DbMallocRaw(db, sizeof(*p) );
77473   if( pNew==0 ) return 0;
77474   pNew->pEList = sqlite3ExprListDup(db, p->pEList, flags);
77475   pNew->pSrc = sqlite3SrcListDup(db, p->pSrc, flags);
77476   pNew->pWhere = sqlite3ExprDup(db, p->pWhere, flags);
77477   pNew->pGroupBy = sqlite3ExprListDup(db, p->pGroupBy, flags);
77478   pNew->pHaving = sqlite3ExprDup(db, p->pHaving, flags);
77479   pNew->pOrderBy = sqlite3ExprListDup(db, p->pOrderBy, flags);
77480   pNew->op = p->op;
77481   pNew->pPrior = pPrior = sqlite3SelectDup(db, p->pPrior, flags);
77482   if( pPrior ) pPrior->pNext = pNew;
77483   pNew->pNext = 0;
77484   pNew->pLimit = sqlite3ExprDup(db, p->pLimit, flags);
77485   pNew->pOffset = sqlite3ExprDup(db, p->pOffset, flags);
77486   pNew->iLimit = 0;
77487   pNew->iOffset = 0;
77488   pNew->selFlags = p->selFlags & ~SF_UsesEphemeral;
77489   pNew->pRightmost = 0;
77490   pNew->addrOpenEphm[0] = -1;
77491   pNew->addrOpenEphm[1] = -1;
77492   pNew->addrOpenEphm[2] = -1;
77493   pNew->nSelectRow = p->nSelectRow;
77494   pNew->pWith = withDup(db, p->pWith);
77495   return pNew;
77496 }
77497 #else
77498 SQLITE_PRIVATE Select *sqlite3SelectDup(sqlite3 *db, Select *p, int flags){
77499   assert( p==0 );
77500   return 0;
77501 }
77502 #endif
77503 
77504 
77505 /*
77506 ** Add a new element to the end of an expression list.  If pList is
77507 ** initially NULL, then create a new expression list.
77508 **
77509 ** If a memory allocation error occurs, the entire list is freed and
77510 ** NULL is returned.  If non-NULL is returned, then it is guaranteed
77511 ** that the new entry was successfully appended.
77512 */
77513 SQLITE_PRIVATE ExprList *sqlite3ExprListAppend(
77514   Parse *pParse,          /* Parsing context */
77515   ExprList *pList,        /* List to which to append. Might be NULL */
77516   Expr *pExpr             /* Expression to be appended. Might be NULL */
77517 ){
77518   sqlite3 *db = pParse->db;
77519   if( pList==0 ){
77520     pList = sqlite3DbMallocZero(db, sizeof(ExprList) );
77521     if( pList==0 ){
77522       goto no_mem;
77523     }
77524     pList->a = sqlite3DbMallocRaw(db, sizeof(pList->a[0]));
77525     if( pList->a==0 ) goto no_mem;
77526   }else if( (pList->nExpr & (pList->nExpr-1))==0 ){
77527     struct ExprList_item *a;
77528     assert( pList->nExpr>0 );
77529     a = sqlite3DbRealloc(db, pList->a, pList->nExpr*2*sizeof(pList->a[0]));
77530     if( a==0 ){
77531       goto no_mem;
77532     }
77533     pList->a = a;
77534   }
77535   assert( pList->a!=0 );
77536   if( 1 ){
77537     struct ExprList_item *pItem = &pList->a[pList->nExpr++];
77538     memset(pItem, 0, sizeof(*pItem));
77539     pItem->pExpr = pExpr;
77540   }
77541   return pList;
77542 
77543 no_mem:
77544   /* Avoid leaking memory if malloc has failed. */
77545   sqlite3ExprDelete(db, pExpr);
77546   sqlite3ExprListDelete(db, pList);
77547   return 0;
77548 }
77549 
77550 /*
77551 ** Set the ExprList.a[].zName element of the most recently added item
77552 ** on the expression list.
77553 **
77554 ** pList might be NULL following an OOM error.  But pName should never be
77555 ** NULL.  If a memory allocation fails, the pParse->db->mallocFailed flag
77556 ** is set.
77557 */
77558 SQLITE_PRIVATE void sqlite3ExprListSetName(
77559   Parse *pParse,          /* Parsing context */
77560   ExprList *pList,        /* List to which to add the span. */
77561   Token *pName,           /* Name to be added */
77562   int dequote             /* True to cause the name to be dequoted */
77563 ){
77564   assert( pList!=0 || pParse->db->mallocFailed!=0 );
77565   if( pList ){
77566     struct ExprList_item *pItem;
77567     assert( pList->nExpr>0 );
77568     pItem = &pList->a[pList->nExpr-1];
77569     assert( pItem->zName==0 );
77570     pItem->zName = sqlite3DbStrNDup(pParse->db, pName->z, pName->n);
77571     if( dequote && pItem->zName ) sqlite3Dequote(pItem->zName);
77572   }
77573 }
77574 
77575 /*
77576 ** Set the ExprList.a[].zSpan element of the most recently added item
77577 ** on the expression list.
77578 **
77579 ** pList might be NULL following an OOM error.  But pSpan should never be
77580 ** NULL.  If a memory allocation fails, the pParse->db->mallocFailed flag
77581 ** is set.
77582 */
77583 SQLITE_PRIVATE void sqlite3ExprListSetSpan(
77584   Parse *pParse,          /* Parsing context */
77585   ExprList *pList,        /* List to which to add the span. */
77586   ExprSpan *pSpan         /* The span to be added */
77587 ){
77588   sqlite3 *db = pParse->db;
77589   assert( pList!=0 || db->mallocFailed!=0 );
77590   if( pList ){
77591     struct ExprList_item *pItem = &pList->a[pList->nExpr-1];
77592     assert( pList->nExpr>0 );
77593     assert( db->mallocFailed || pItem->pExpr==pSpan->pExpr );
77594     sqlite3DbFree(db, pItem->zSpan);
77595     pItem->zSpan = sqlite3DbStrNDup(db, (char*)pSpan->zStart,
77596                                     (int)(pSpan->zEnd - pSpan->zStart));
77597   }
77598 }
77599 
77600 /*
77601 ** If the expression list pEList contains more than iLimit elements,
77602 ** leave an error message in pParse.
77603 */
77604 SQLITE_PRIVATE void sqlite3ExprListCheckLength(
77605   Parse *pParse,
77606   ExprList *pEList,
77607   const char *zObject
77608 ){
77609   int mx = pParse->db->aLimit[SQLITE_LIMIT_COLUMN];
77610   testcase( pEList && pEList->nExpr==mx );
77611   testcase( pEList && pEList->nExpr==mx+1 );
77612   if( pEList && pEList->nExpr>mx ){
77613     sqlite3ErrorMsg(pParse, "too many columns in %s", zObject);
77614   }
77615 }
77616 
77617 /*
77618 ** Delete an entire expression list.
77619 */
77620 SQLITE_PRIVATE void sqlite3ExprListDelete(sqlite3 *db, ExprList *pList){
77621   int i;
77622   struct ExprList_item *pItem;
77623   if( pList==0 ) return;
77624   assert( pList->a!=0 || pList->nExpr==0 );
77625   for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
77626     sqlite3ExprDelete(db, pItem->pExpr);
77627     sqlite3DbFree(db, pItem->zName);
77628     sqlite3DbFree(db, pItem->zSpan);
77629   }
77630   sqlite3DbFree(db, pList->a);
77631   sqlite3DbFree(db, pList);
77632 }
77633 
77634 /*
77635 ** These routines are Walker callbacks.  Walker.u.pi is a pointer
77636 ** to an integer.  These routines are checking an expression to see
77637 ** if it is a constant.  Set *Walker.u.pi to 0 if the expression is
77638 ** not constant.
77639 **
77640 ** These callback routines are used to implement the following:
77641 **
77642 **     sqlite3ExprIsConstant()
77643 **     sqlite3ExprIsConstantNotJoin()
77644 **     sqlite3ExprIsConstantOrFunction()
77645 **
77646 */
77647 static int exprNodeIsConstant(Walker *pWalker, Expr *pExpr){
77648 
77649   /* If pWalker->u.i is 3 then any term of the expression that comes from
77650   ** the ON or USING clauses of a join disqualifies the expression
77651   ** from being considered constant. */
77652   if( pWalker->u.i==3 && ExprHasProperty(pExpr, EP_FromJoin) ){
77653     pWalker->u.i = 0;
77654     return WRC_Abort;
77655   }
77656 
77657   switch( pExpr->op ){
77658     /* Consider functions to be constant if all their arguments are constant
77659     ** and either pWalker->u.i==2 or the function as the SQLITE_FUNC_CONST
77660     ** flag. */
77661     case TK_FUNCTION:
77662       if( pWalker->u.i==2 || ExprHasProperty(pExpr,EP_Constant) ){
77663         return WRC_Continue;
77664       }
77665       /* Fall through */
77666     case TK_ID:
77667     case TK_COLUMN:
77668     case TK_AGG_FUNCTION:
77669     case TK_AGG_COLUMN:
77670       testcase( pExpr->op==TK_ID );
77671       testcase( pExpr->op==TK_COLUMN );
77672       testcase( pExpr->op==TK_AGG_FUNCTION );
77673       testcase( pExpr->op==TK_AGG_COLUMN );
77674       pWalker->u.i = 0;
77675       return WRC_Abort;
77676     default:
77677       testcase( pExpr->op==TK_SELECT ); /* selectNodeIsConstant will disallow */
77678       testcase( pExpr->op==TK_EXISTS ); /* selectNodeIsConstant will disallow */
77679       return WRC_Continue;
77680   }
77681 }
77682 static int selectNodeIsConstant(Walker *pWalker, Select *NotUsed){
77683   UNUSED_PARAMETER(NotUsed);
77684   pWalker->u.i = 0;
77685   return WRC_Abort;
77686 }
77687 static int exprIsConst(Expr *p, int initFlag){
77688   Walker w;
77689   memset(&w, 0, sizeof(w));
77690   w.u.i = initFlag;
77691   w.xExprCallback = exprNodeIsConstant;
77692   w.xSelectCallback = selectNodeIsConstant;
77693   sqlite3WalkExpr(&w, p);
77694   return w.u.i;
77695 }
77696 
77697 /*
77698 ** Walk an expression tree.  Return 1 if the expression is constant
77699 ** and 0 if it involves variables or function calls.
77700 **
77701 ** For the purposes of this function, a double-quoted string (ex: "abc")
77702 ** is considered a variable but a single-quoted string (ex: 'abc') is
77703 ** a constant.
77704 */
77705 SQLITE_PRIVATE int sqlite3ExprIsConstant(Expr *p){
77706   return exprIsConst(p, 1);
77707 }
77708 
77709 /*
77710 ** Walk an expression tree.  Return 1 if the expression is constant
77711 ** that does no originate from the ON or USING clauses of a join.
77712 ** Return 0 if it involves variables or function calls or terms from
77713 ** an ON or USING clause.
77714 */
77715 SQLITE_PRIVATE int sqlite3ExprIsConstantNotJoin(Expr *p){
77716   return exprIsConst(p, 3);
77717 }
77718 
77719 /*
77720 ** Walk an expression tree.  Return 1 if the expression is constant
77721 ** or a function call with constant arguments.  Return and 0 if there
77722 ** are any variables.
77723 **
77724 ** For the purposes of this function, a double-quoted string (ex: "abc")
77725 ** is considered a variable but a single-quoted string (ex: 'abc') is
77726 ** a constant.
77727 */
77728 SQLITE_PRIVATE int sqlite3ExprIsConstantOrFunction(Expr *p){
77729   return exprIsConst(p, 2);
77730 }
77731 
77732 /*
77733 ** If the expression p codes a constant integer that is small enough
77734 ** to fit in a 32-bit integer, return 1 and put the value of the integer
77735 ** in *pValue.  If the expression is not an integer or if it is too big
77736 ** to fit in a signed 32-bit integer, return 0 and leave *pValue unchanged.
77737 */
77738 SQLITE_PRIVATE int sqlite3ExprIsInteger(Expr *p, int *pValue){
77739   int rc = 0;
77740 
77741   /* If an expression is an integer literal that fits in a signed 32-bit
77742   ** integer, then the EP_IntValue flag will have already been set */
77743   assert( p->op!=TK_INTEGER || (p->flags & EP_IntValue)!=0
77744            || sqlite3GetInt32(p->u.zToken, &rc)==0 );
77745 
77746   if( p->flags & EP_IntValue ){
77747     *pValue = p->u.iValue;
77748     return 1;
77749   }
77750   switch( p->op ){
77751     case TK_UPLUS: {
77752       rc = sqlite3ExprIsInteger(p->pLeft, pValue);
77753       break;
77754     }
77755     case TK_UMINUS: {
77756       int v;
77757       if( sqlite3ExprIsInteger(p->pLeft, &v) ){
77758         assert( v!=(-2147483647-1) );
77759         *pValue = -v;
77760         rc = 1;
77761       }
77762       break;
77763     }
77764     default: break;
77765   }
77766   return rc;
77767 }
77768 
77769 /*
77770 ** Return FALSE if there is no chance that the expression can be NULL.
77771 **
77772 ** If the expression might be NULL or if the expression is too complex
77773 ** to tell return TRUE.
77774 **
77775 ** This routine is used as an optimization, to skip OP_IsNull opcodes
77776 ** when we know that a value cannot be NULL.  Hence, a false positive
77777 ** (returning TRUE when in fact the expression can never be NULL) might
77778 ** be a small performance hit but is otherwise harmless.  On the other
77779 ** hand, a false negative (returning FALSE when the result could be NULL)
77780 ** will likely result in an incorrect answer.  So when in doubt, return
77781 ** TRUE.
77782 */
77783 SQLITE_PRIVATE int sqlite3ExprCanBeNull(const Expr *p){
77784   u8 op;
77785   while( p->op==TK_UPLUS || p->op==TK_UMINUS ){ p = p->pLeft; }
77786   op = p->op;
77787   if( op==TK_REGISTER ) op = p->op2;
77788   switch( op ){
77789     case TK_INTEGER:
77790     case TK_STRING:
77791     case TK_FLOAT:
77792     case TK_BLOB:
77793       return 0;
77794     default:
77795       return 1;
77796   }
77797 }
77798 
77799 /*
77800 ** Generate an OP_IsNull instruction that tests register iReg and jumps
77801 ** to location iDest if the value in iReg is NULL.  The value in iReg
77802 ** was computed by pExpr.  If we can look at pExpr at compile-time and
77803 ** determine that it can never generate a NULL, then the OP_IsNull operation
77804 ** can be omitted.
77805 */
77806 SQLITE_PRIVATE void sqlite3ExprCodeIsNullJump(
77807   Vdbe *v,            /* The VDBE under construction */
77808   const Expr *pExpr,  /* Only generate OP_IsNull if this expr can be NULL */
77809   int iReg,           /* Test the value in this register for NULL */
77810   int iDest           /* Jump here if the value is null */
77811 ){
77812   if( sqlite3ExprCanBeNull(pExpr) ){
77813     sqlite3VdbeAddOp2(v, OP_IsNull, iReg, iDest);
77814   }
77815 }
77816 
77817 /*
77818 ** Return TRUE if the given expression is a constant which would be
77819 ** unchanged by OP_Affinity with the affinity given in the second
77820 ** argument.
77821 **
77822 ** This routine is used to determine if the OP_Affinity operation
77823 ** can be omitted.  When in doubt return FALSE.  A false negative
77824 ** is harmless.  A false positive, however, can result in the wrong
77825 ** answer.
77826 */
77827 SQLITE_PRIVATE int sqlite3ExprNeedsNoAffinityChange(const Expr *p, char aff){
77828   u8 op;
77829   if( aff==SQLITE_AFF_NONE ) return 1;
77830   while( p->op==TK_UPLUS || p->op==TK_UMINUS ){ p = p->pLeft; }
77831   op = p->op;
77832   if( op==TK_REGISTER ) op = p->op2;
77833   switch( op ){
77834     case TK_INTEGER: {
77835       return aff==SQLITE_AFF_INTEGER || aff==SQLITE_AFF_NUMERIC;
77836     }
77837     case TK_FLOAT: {
77838       return aff==SQLITE_AFF_REAL || aff==SQLITE_AFF_NUMERIC;
77839     }
77840     case TK_STRING: {
77841       return aff==SQLITE_AFF_TEXT;
77842     }
77843     case TK_BLOB: {
77844       return 1;
77845     }
77846     case TK_COLUMN: {
77847       assert( p->iTable>=0 );  /* p cannot be part of a CHECK constraint */
77848       return p->iColumn<0
77849           && (aff==SQLITE_AFF_INTEGER || aff==SQLITE_AFF_NUMERIC);
77850     }
77851     default: {
77852       return 0;
77853     }
77854   }
77855 }
77856 
77857 /*
77858 ** Return TRUE if the given string is a row-id column name.
77859 */
77860 SQLITE_PRIVATE int sqlite3IsRowid(const char *z){
77861   if( sqlite3StrICmp(z, "_ROWID_")==0 ) return 1;
77862   if( sqlite3StrICmp(z, "ROWID")==0 ) return 1;
77863   if( sqlite3StrICmp(z, "OID")==0 ) return 1;
77864   return 0;
77865 }
77866 
77867 /*
77868 ** Return true if we are able to the IN operator optimization on a
77869 ** query of the form
77870 **
77871 **       x IN (SELECT ...)
77872 **
77873 ** Where the SELECT... clause is as specified by the parameter to this
77874 ** routine.
77875 **
77876 ** The Select object passed in has already been preprocessed and no
77877 ** errors have been found.
77878 */
77879 #ifndef SQLITE_OMIT_SUBQUERY
77880 static int isCandidateForInOpt(Select *p){
77881   SrcList *pSrc;
77882   ExprList *pEList;
77883   Table *pTab;
77884   if( p==0 ) return 0;                   /* right-hand side of IN is SELECT */
77885   if( p->pPrior ) return 0;              /* Not a compound SELECT */
77886   if( p->selFlags & (SF_Distinct|SF_Aggregate) ){
77887     testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct );
77888     testcase( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate );
77889     return 0; /* No DISTINCT keyword and no aggregate functions */
77890   }
77891   assert( p->pGroupBy==0 );              /* Has no GROUP BY clause */
77892   if( p->pLimit ) return 0;              /* Has no LIMIT clause */
77893   assert( p->pOffset==0 );               /* No LIMIT means no OFFSET */
77894   if( p->pWhere ) return 0;              /* Has no WHERE clause */
77895   pSrc = p->pSrc;
77896   assert( pSrc!=0 );
77897   if( pSrc->nSrc!=1 ) return 0;          /* Single term in FROM clause */
77898   if( pSrc->a[0].pSelect ) return 0;     /* FROM is not a subquery or view */
77899   pTab = pSrc->a[0].pTab;
77900   if( NEVER(pTab==0) ) return 0;
77901   assert( pTab->pSelect==0 );            /* FROM clause is not a view */
77902   if( IsVirtual(pTab) ) return 0;        /* FROM clause not a virtual table */
77903   pEList = p->pEList;
77904   if( pEList->nExpr!=1 ) return 0;       /* One column in the result set */
77905   if( pEList->a[0].pExpr->op!=TK_COLUMN ) return 0; /* Result is a column */
77906   return 1;
77907 }
77908 #endif /* SQLITE_OMIT_SUBQUERY */
77909 
77910 /*
77911 ** Code an OP_Once instruction and allocate space for its flag. Return the
77912 ** address of the new instruction.
77913 */
77914 SQLITE_PRIVATE int sqlite3CodeOnce(Parse *pParse){
77915   Vdbe *v = sqlite3GetVdbe(pParse);      /* Virtual machine being coded */
77916   return sqlite3VdbeAddOp1(v, OP_Once, pParse->nOnce++);
77917 }
77918 
77919 /*
77920 ** This function is used by the implementation of the IN (...) operator.
77921 ** The pX parameter is the expression on the RHS of the IN operator, which
77922 ** might be either a list of expressions or a subquery.
77923 **
77924 ** The job of this routine is to find or create a b-tree object that can
77925 ** be used either to test for membership in the RHS set or to iterate through
77926 ** all members of the RHS set, skipping duplicates.
77927 **
77928 ** A cursor is opened on the b-tree object that the RHS of the IN operator
77929 ** and pX->iTable is set to the index of that cursor.
77930 **
77931 ** The returned value of this function indicates the b-tree type, as follows:
77932 **
77933 **   IN_INDEX_ROWID      - The cursor was opened on a database table.
77934 **   IN_INDEX_INDEX_ASC  - The cursor was opened on an ascending index.
77935 **   IN_INDEX_INDEX_DESC - The cursor was opened on a descending index.
77936 **   IN_INDEX_EPH        - The cursor was opened on a specially created and
77937 **                         populated epheremal table.
77938 **
77939 ** An existing b-tree might be used if the RHS expression pX is a simple
77940 ** subquery such as:
77941 **
77942 **     SELECT <column> FROM <table>
77943 **
77944 ** If the RHS of the IN operator is a list or a more complex subquery, then
77945 ** an ephemeral table might need to be generated from the RHS and then
77946 ** pX->iTable made to point to the ephermeral table instead of an
77947 ** existing table.
77948 **
77949 ** If the prNotFound parameter is 0, then the b-tree will be used to iterate
77950 ** through the set members, skipping any duplicates. In this case an
77951 ** epheremal table must be used unless the selected <column> is guaranteed
77952 ** to be unique - either because it is an INTEGER PRIMARY KEY or it
77953 ** has a UNIQUE constraint or UNIQUE index.
77954 **
77955 ** If the prNotFound parameter is not 0, then the b-tree will be used
77956 ** for fast set membership tests. In this case an epheremal table must
77957 ** be used unless <column> is an INTEGER PRIMARY KEY or an index can
77958 ** be found with <column> as its left-most column.
77959 **
77960 ** When the b-tree is being used for membership tests, the calling function
77961 ** needs to know whether or not the structure contains an SQL NULL
77962 ** value in order to correctly evaluate expressions like "X IN (Y, Z)".
77963 ** If there is any chance that the (...) might contain a NULL value at
77964 ** runtime, then a register is allocated and the register number written
77965 ** to *prNotFound. If there is no chance that the (...) contains a
77966 ** NULL value, then *prNotFound is left unchanged.
77967 **
77968 ** If a register is allocated and its location stored in *prNotFound, then
77969 ** its initial value is NULL.  If the (...) does not remain constant
77970 ** for the duration of the query (i.e. the SELECT within the (...)
77971 ** is a correlated subquery) then the value of the allocated register is
77972 ** reset to NULL each time the subquery is rerun. This allows the
77973 ** caller to use vdbe code equivalent to the following:
77974 **
77975 **   if( register==NULL ){
77976 **     has_null = <test if data structure contains null>
77977 **     register = 1
77978 **   }
77979 **
77980 ** in order to avoid running the <test if data structure contains null>
77981 ** test more often than is necessary.
77982 */
77983 #ifndef SQLITE_OMIT_SUBQUERY
77984 SQLITE_PRIVATE int sqlite3FindInIndex(Parse *pParse, Expr *pX, int *prNotFound){
77985   Select *p;                            /* SELECT to the right of IN operator */
77986   int eType = 0;                        /* Type of RHS table. IN_INDEX_* */
77987   int iTab = pParse->nTab++;            /* Cursor of the RHS table */
77988   int mustBeUnique = (prNotFound==0);   /* True if RHS must be unique */
77989   Vdbe *v = sqlite3GetVdbe(pParse);     /* Virtual machine being coded */
77990 
77991   assert( pX->op==TK_IN );
77992 
77993   /* Check to see if an existing table or index can be used to
77994   ** satisfy the query.  This is preferable to generating a new
77995   ** ephemeral table.
77996   */
77997   p = (ExprHasProperty(pX, EP_xIsSelect) ? pX->x.pSelect : 0);
77998   if( ALWAYS(pParse->nErr==0) && isCandidateForInOpt(p) ){
77999     sqlite3 *db = pParse->db;              /* Database connection */
78000     Table *pTab;                           /* Table <table>. */
78001     Expr *pExpr;                           /* Expression <column> */
78002     i16 iCol;                              /* Index of column <column> */
78003     i16 iDb;                               /* Database idx for pTab */
78004 
78005     assert( p );                        /* Because of isCandidateForInOpt(p) */
78006     assert( p->pEList!=0 );             /* Because of isCandidateForInOpt(p) */
78007     assert( p->pEList->a[0].pExpr!=0 ); /* Because of isCandidateForInOpt(p) */
78008     assert( p->pSrc!=0 );               /* Because of isCandidateForInOpt(p) */
78009     pTab = p->pSrc->a[0].pTab;
78010     pExpr = p->pEList->a[0].pExpr;
78011     iCol = (i16)pExpr->iColumn;
78012 
78013     /* Code an OP_VerifyCookie and OP_TableLock for <table>. */
78014     iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
78015     sqlite3CodeVerifySchema(pParse, iDb);
78016     sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
78017 
78018     /* This function is only called from two places. In both cases the vdbe
78019     ** has already been allocated. So assume sqlite3GetVdbe() is always
78020     ** successful here.
78021     */
78022     assert(v);
78023     if( iCol<0 ){
78024       int iAddr;
78025 
78026       iAddr = sqlite3CodeOnce(pParse);
78027 
78028       sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead);
78029       eType = IN_INDEX_ROWID;
78030 
78031       sqlite3VdbeJumpHere(v, iAddr);
78032     }else{
78033       Index *pIdx;                         /* Iterator variable */
78034 
78035       /* The collation sequence used by the comparison. If an index is to
78036       ** be used in place of a temp-table, it must be ordered according
78037       ** to this collation sequence.  */
78038       CollSeq *pReq = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pExpr);
78039 
78040       /* Check that the affinity that will be used to perform the
78041       ** comparison is the same as the affinity of the column. If
78042       ** it is not, it is not possible to use any index.
78043       */
78044       int affinity_ok = sqlite3IndexAffinityOk(pX, pTab->aCol[iCol].affinity);
78045 
78046       for(pIdx=pTab->pIndex; pIdx && eType==0 && affinity_ok; pIdx=pIdx->pNext){
78047         if( (pIdx->aiColumn[0]==iCol)
78048          && sqlite3FindCollSeq(db, ENC(db), pIdx->azColl[0], 0)==pReq
78049          && (!mustBeUnique || (pIdx->nKeyCol==1 && pIdx->onError!=OE_None))
78050         ){
78051           int iAddr = sqlite3CodeOnce(pParse);
78052           sqlite3VdbeAddOp3(v, OP_OpenRead, iTab, pIdx->tnum, iDb);
78053           sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
78054           VdbeComment((v, "%s", pIdx->zName));
78055           assert( IN_INDEX_INDEX_DESC == IN_INDEX_INDEX_ASC+1 );
78056           eType = IN_INDEX_INDEX_ASC + pIdx->aSortOrder[0];
78057 
78058           sqlite3VdbeJumpHere(v, iAddr);
78059           if( prNotFound && !pTab->aCol[iCol].notNull ){
78060             *prNotFound = ++pParse->nMem;
78061             sqlite3VdbeAddOp2(v, OP_Null, 0, *prNotFound);
78062           }
78063         }
78064       }
78065     }
78066   }
78067 
78068   if( eType==0 ){
78069     /* Could not found an existing table or index to use as the RHS b-tree.
78070     ** We will have to generate an ephemeral table to do the job.
78071     */
78072     u32 savedNQueryLoop = pParse->nQueryLoop;
78073     int rMayHaveNull = 0;
78074     eType = IN_INDEX_EPH;
78075     if( prNotFound ){
78076       *prNotFound = rMayHaveNull = ++pParse->nMem;
78077       sqlite3VdbeAddOp2(v, OP_Null, 0, *prNotFound);
78078     }else{
78079       testcase( pParse->nQueryLoop>0 );
78080       pParse->nQueryLoop = 0;
78081       if( pX->pLeft->iColumn<0 && !ExprHasProperty(pX, EP_xIsSelect) ){
78082         eType = IN_INDEX_ROWID;
78083       }
78084     }
78085     sqlite3CodeSubselect(pParse, pX, rMayHaveNull, eType==IN_INDEX_ROWID);
78086     pParse->nQueryLoop = savedNQueryLoop;
78087   }else{
78088     pX->iTable = iTab;
78089   }
78090   return eType;
78091 }
78092 #endif
78093 
78094 /*
78095 ** Generate code for scalar subqueries used as a subquery expression, EXISTS,
78096 ** or IN operators.  Examples:
78097 **
78098 **     (SELECT a FROM b)          -- subquery
78099 **     EXISTS (SELECT a FROM b)   -- EXISTS subquery
78100 **     x IN (4,5,11)              -- IN operator with list on right-hand side
78101 **     x IN (SELECT a FROM b)     -- IN operator with subquery on the right
78102 **
78103 ** The pExpr parameter describes the expression that contains the IN
78104 ** operator or subquery.
78105 **
78106 ** If parameter isRowid is non-zero, then expression pExpr is guaranteed
78107 ** to be of the form "<rowid> IN (?, ?, ?)", where <rowid> is a reference
78108 ** to some integer key column of a table B-Tree. In this case, use an
78109 ** intkey B-Tree to store the set of IN(...) values instead of the usual
78110 ** (slower) variable length keys B-Tree.
78111 **
78112 ** If rMayHaveNull is non-zero, that means that the operation is an IN
78113 ** (not a SELECT or EXISTS) and that the RHS might contains NULLs.
78114 ** Furthermore, the IN is in a WHERE clause and that we really want
78115 ** to iterate over the RHS of the IN operator in order to quickly locate
78116 ** all corresponding LHS elements.  All this routine does is initialize
78117 ** the register given by rMayHaveNull to NULL.  Calling routines will take
78118 ** care of changing this register value to non-NULL if the RHS is NULL-free.
78119 **
78120 ** If rMayHaveNull is zero, that means that the subquery is being used
78121 ** for membership testing only.  There is no need to initialize any
78122 ** registers to indicate the presence or absence of NULLs on the RHS.
78123 **
78124 ** For a SELECT or EXISTS operator, return the register that holds the
78125 ** result.  For IN operators or if an error occurs, the return value is 0.
78126 */
78127 #ifndef SQLITE_OMIT_SUBQUERY
78128 SQLITE_PRIVATE int sqlite3CodeSubselect(
78129   Parse *pParse,          /* Parsing context */
78130   Expr *pExpr,            /* The IN, SELECT, or EXISTS operator */
78131   int rMayHaveNull,       /* Register that records whether NULLs exist in RHS */
78132   int isRowid             /* If true, LHS of IN operator is a rowid */
78133 ){
78134   int testAddr = -1;                      /* One-time test address */
78135   int rReg = 0;                           /* Register storing resulting */
78136   Vdbe *v = sqlite3GetVdbe(pParse);
78137   if( NEVER(v==0) ) return 0;
78138   sqlite3ExprCachePush(pParse);
78139 
78140   /* This code must be run in its entirety every time it is encountered
78141   ** if any of the following is true:
78142   **
78143   **    *  The right-hand side is a correlated subquery
78144   **    *  The right-hand side is an expression list containing variables
78145   **    *  We are inside a trigger
78146   **
78147   ** If all of the above are false, then we can run this code just once
78148   ** save the results, and reuse the same result on subsequent invocations.
78149   */
78150   if( !ExprHasProperty(pExpr, EP_VarSelect) ){
78151     testAddr = sqlite3CodeOnce(pParse);
78152   }
78153 
78154 #ifndef SQLITE_OMIT_EXPLAIN
78155   if( pParse->explain==2 ){
78156     char *zMsg = sqlite3MPrintf(
78157         pParse->db, "EXECUTE %s%s SUBQUERY %d", testAddr>=0?"":"CORRELATED ",
78158         pExpr->op==TK_IN?"LIST":"SCALAR", pParse->iNextSelectId
78159     );
78160     sqlite3VdbeAddOp4(v, OP_Explain, pParse->iSelectId, 0, 0, zMsg, P4_DYNAMIC);
78161   }
78162 #endif
78163 
78164   switch( pExpr->op ){
78165     case TK_IN: {
78166       char affinity;              /* Affinity of the LHS of the IN */
78167       int addr;                   /* Address of OP_OpenEphemeral instruction */
78168       Expr *pLeft = pExpr->pLeft; /* the LHS of the IN operator */
78169       KeyInfo *pKeyInfo = 0;      /* Key information */
78170 
78171       if( rMayHaveNull ){
78172         sqlite3VdbeAddOp2(v, OP_Null, 0, rMayHaveNull);
78173       }
78174 
78175       affinity = sqlite3ExprAffinity(pLeft);
78176 
78177       /* Whether this is an 'x IN(SELECT...)' or an 'x IN(<exprlist>)'
78178       ** expression it is handled the same way.  An ephemeral table is
78179       ** filled with single-field index keys representing the results
78180       ** from the SELECT or the <exprlist>.
78181       **
78182       ** If the 'x' expression is a column value, or the SELECT...
78183       ** statement returns a column value, then the affinity of that
78184       ** column is used to build the index keys. If both 'x' and the
78185       ** SELECT... statement are columns, then numeric affinity is used
78186       ** if either column has NUMERIC or INTEGER affinity. If neither
78187       ** 'x' nor the SELECT... statement are columns, then numeric affinity
78188       ** is used.
78189       */
78190       pExpr->iTable = pParse->nTab++;
78191       addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pExpr->iTable, !isRowid);
78192       if( rMayHaveNull==0 ) sqlite3VdbeChangeP5(v, BTREE_UNORDERED);
78193       pKeyInfo = isRowid ? 0 : sqlite3KeyInfoAlloc(pParse->db, 1, 1);
78194 
78195       if( ExprHasProperty(pExpr, EP_xIsSelect) ){
78196         /* Case 1:     expr IN (SELECT ...)
78197         **
78198         ** Generate code to write the results of the select into the temporary
78199         ** table allocated and opened above.
78200         */
78201         SelectDest dest;
78202         ExprList *pEList;
78203 
78204         assert( !isRowid );
78205         sqlite3SelectDestInit(&dest, SRT_Set, pExpr->iTable);
78206         dest.affSdst = (u8)affinity;
78207         assert( (pExpr->iTable&0x0000FFFF)==pExpr->iTable );
78208         pExpr->x.pSelect->iLimit = 0;
78209         testcase( pKeyInfo==0 ); /* Caused by OOM in sqlite3KeyInfoAlloc() */
78210         if( sqlite3Select(pParse, pExpr->x.pSelect, &dest) ){
78211           sqlite3KeyInfoUnref(pKeyInfo);
78212           return 0;
78213         }
78214         pEList = pExpr->x.pSelect->pEList;
78215         assert( pKeyInfo!=0 ); /* OOM will cause exit after sqlite3Select() */
78216         assert( pEList!=0 );
78217         assert( pEList->nExpr>0 );
78218         assert( sqlite3KeyInfoIsWriteable(pKeyInfo) );
78219         pKeyInfo->aColl[0] = sqlite3BinaryCompareCollSeq(pParse, pExpr->pLeft,
78220                                                          pEList->a[0].pExpr);
78221       }else if( ALWAYS(pExpr->x.pList!=0) ){
78222         /* Case 2:     expr IN (exprlist)
78223         **
78224         ** For each expression, build an index key from the evaluation and
78225         ** store it in the temporary table. If <expr> is a column, then use
78226         ** that columns affinity when building index keys. If <expr> is not
78227         ** a column, use numeric affinity.
78228         */
78229         int i;
78230         ExprList *pList = pExpr->x.pList;
78231         struct ExprList_item *pItem;
78232         int r1, r2, r3;
78233 
78234         if( !affinity ){
78235           affinity = SQLITE_AFF_NONE;
78236         }
78237         if( pKeyInfo ){
78238           assert( sqlite3KeyInfoIsWriteable(pKeyInfo) );
78239           pKeyInfo->aColl[0] = sqlite3ExprCollSeq(pParse, pExpr->pLeft);
78240         }
78241 
78242         /* Loop through each expression in <exprlist>. */
78243         r1 = sqlite3GetTempReg(pParse);
78244         r2 = sqlite3GetTempReg(pParse);
78245         sqlite3VdbeAddOp2(v, OP_Null, 0, r2);
78246         for(i=pList->nExpr, pItem=pList->a; i>0; i--, pItem++){
78247           Expr *pE2 = pItem->pExpr;
78248           int iValToIns;
78249 
78250           /* If the expression is not constant then we will need to
78251           ** disable the test that was generated above that makes sure
78252           ** this code only executes once.  Because for a non-constant
78253           ** expression we need to rerun this code each time.
78254           */
78255           if( testAddr>=0 && !sqlite3ExprIsConstant(pE2) ){
78256             sqlite3VdbeChangeToNoop(v, testAddr);
78257             testAddr = -1;
78258           }
78259 
78260           /* Evaluate the expression and insert it into the temp table */
78261           if( isRowid && sqlite3ExprIsInteger(pE2, &iValToIns) ){
78262             sqlite3VdbeAddOp3(v, OP_InsertInt, pExpr->iTable, r2, iValToIns);
78263           }else{
78264             r3 = sqlite3ExprCodeTarget(pParse, pE2, r1);
78265             if( isRowid ){
78266               sqlite3VdbeAddOp2(v, OP_MustBeInt, r3,
78267                                 sqlite3VdbeCurrentAddr(v)+2);
78268               sqlite3VdbeAddOp3(v, OP_Insert, pExpr->iTable, r2, r3);
78269             }else{
78270               sqlite3VdbeAddOp4(v, OP_MakeRecord, r3, 1, r2, &affinity, 1);
78271               sqlite3ExprCacheAffinityChange(pParse, r3, 1);
78272               sqlite3VdbeAddOp2(v, OP_IdxInsert, pExpr->iTable, r2);
78273             }
78274           }
78275         }
78276         sqlite3ReleaseTempReg(pParse, r1);
78277         sqlite3ReleaseTempReg(pParse, r2);
78278       }
78279       if( pKeyInfo ){
78280         sqlite3VdbeChangeP4(v, addr, (void *)pKeyInfo, P4_KEYINFO);
78281       }
78282       break;
78283     }
78284 
78285     case TK_EXISTS:
78286     case TK_SELECT:
78287     default: {
78288       /* If this has to be a scalar SELECT.  Generate code to put the
78289       ** value of this select in a memory cell and record the number
78290       ** of the memory cell in iColumn.  If this is an EXISTS, write
78291       ** an integer 0 (not exists) or 1 (exists) into a memory cell
78292       ** and record that memory cell in iColumn.
78293       */
78294       Select *pSel;                         /* SELECT statement to encode */
78295       SelectDest dest;                      /* How to deal with SELECt result */
78296 
78297       testcase( pExpr->op==TK_EXISTS );
78298       testcase( pExpr->op==TK_SELECT );
78299       assert( pExpr->op==TK_EXISTS || pExpr->op==TK_SELECT );
78300 
78301       assert( ExprHasProperty(pExpr, EP_xIsSelect) );
78302       pSel = pExpr->x.pSelect;
78303       sqlite3SelectDestInit(&dest, 0, ++pParse->nMem);
78304       if( pExpr->op==TK_SELECT ){
78305         dest.eDest = SRT_Mem;
78306         sqlite3VdbeAddOp2(v, OP_Null, 0, dest.iSDParm);
78307         VdbeComment((v, "Init subquery result"));
78308       }else{
78309         dest.eDest = SRT_Exists;
78310         sqlite3VdbeAddOp2(v, OP_Integer, 0, dest.iSDParm);
78311         VdbeComment((v, "Init EXISTS result"));
78312       }
78313       sqlite3ExprDelete(pParse->db, pSel->pLimit);
78314       pSel->pLimit = sqlite3PExpr(pParse, TK_INTEGER, 0, 0,
78315                                   &sqlite3IntTokens[1]);
78316       pSel->iLimit = 0;
78317       if( sqlite3Select(pParse, pSel, &dest) ){
78318         return 0;
78319       }
78320       rReg = dest.iSDParm;
78321       ExprSetVVAProperty(pExpr, EP_NoReduce);
78322       break;
78323     }
78324   }
78325 
78326   if( testAddr>=0 ){
78327     sqlite3VdbeJumpHere(v, testAddr);
78328   }
78329   sqlite3ExprCachePop(pParse, 1);
78330 
78331   return rReg;
78332 }
78333 #endif /* SQLITE_OMIT_SUBQUERY */
78334 
78335 #ifndef SQLITE_OMIT_SUBQUERY
78336 /*
78337 ** Generate code for an IN expression.
78338 **
78339 **      x IN (SELECT ...)
78340 **      x IN (value, value, ...)
78341 **
78342 ** The left-hand side (LHS) is a scalar expression.  The right-hand side (RHS)
78343 ** is an array of zero or more values.  The expression is true if the LHS is
78344 ** contained within the RHS.  The value of the expression is unknown (NULL)
78345 ** if the LHS is NULL or if the LHS is not contained within the RHS and the
78346 ** RHS contains one or more NULL values.
78347 **
78348 ** This routine generates code will jump to destIfFalse if the LHS is not
78349 ** contained within the RHS.  If due to NULLs we cannot determine if the LHS
78350 ** is contained in the RHS then jump to destIfNull.  If the LHS is contained
78351 ** within the RHS then fall through.
78352 */
78353 static void sqlite3ExprCodeIN(
78354   Parse *pParse,        /* Parsing and code generating context */
78355   Expr *pExpr,          /* The IN expression */
78356   int destIfFalse,      /* Jump here if LHS is not contained in the RHS */
78357   int destIfNull        /* Jump here if the results are unknown due to NULLs */
78358 ){
78359   int rRhsHasNull = 0;  /* Register that is true if RHS contains NULL values */
78360   char affinity;        /* Comparison affinity to use */
78361   int eType;            /* Type of the RHS */
78362   int r1;               /* Temporary use register */
78363   Vdbe *v;              /* Statement under construction */
78364 
78365   /* Compute the RHS.   After this step, the table with cursor
78366   ** pExpr->iTable will contains the values that make up the RHS.
78367   */
78368   v = pParse->pVdbe;
78369   assert( v!=0 );       /* OOM detected prior to this routine */
78370   VdbeNoopComment((v, "begin IN expr"));
78371   eType = sqlite3FindInIndex(pParse, pExpr, &rRhsHasNull);
78372 
78373   /* Figure out the affinity to use to create a key from the results
78374   ** of the expression. affinityStr stores a static string suitable for
78375   ** P4 of OP_MakeRecord.
78376   */
78377   affinity = comparisonAffinity(pExpr);
78378 
78379   /* Code the LHS, the <expr> from "<expr> IN (...)".
78380   */
78381   sqlite3ExprCachePush(pParse);
78382   r1 = sqlite3GetTempReg(pParse);
78383   sqlite3ExprCode(pParse, pExpr->pLeft, r1);
78384 
78385   /* If the LHS is NULL, then the result is either false or NULL depending
78386   ** on whether the RHS is empty or not, respectively.
78387   */
78388   if( destIfNull==destIfFalse ){
78389     /* Shortcut for the common case where the false and NULL outcomes are
78390     ** the same. */
78391     sqlite3VdbeAddOp2(v, OP_IsNull, r1, destIfNull);
78392   }else{
78393     int addr1 = sqlite3VdbeAddOp1(v, OP_NotNull, r1);
78394     sqlite3VdbeAddOp2(v, OP_Rewind, pExpr->iTable, destIfFalse);
78395     sqlite3VdbeAddOp2(v, OP_Goto, 0, destIfNull);
78396     sqlite3VdbeJumpHere(v, addr1);
78397   }
78398 
78399   if( eType==IN_INDEX_ROWID ){
78400     /* In this case, the RHS is the ROWID of table b-tree
78401     */
78402     sqlite3VdbeAddOp2(v, OP_MustBeInt, r1, destIfFalse);
78403     sqlite3VdbeAddOp3(v, OP_NotExists, pExpr->iTable, destIfFalse, r1);
78404   }else{
78405     /* In this case, the RHS is an index b-tree.
78406     */
78407     sqlite3VdbeAddOp4(v, OP_Affinity, r1, 1, 0, &affinity, 1);
78408 
78409     /* If the set membership test fails, then the result of the
78410     ** "x IN (...)" expression must be either 0 or NULL. If the set
78411     ** contains no NULL values, then the result is 0. If the set
78412     ** contains one or more NULL values, then the result of the
78413     ** expression is also NULL.
78414     */
78415     if( rRhsHasNull==0 || destIfFalse==destIfNull ){
78416       /* This branch runs if it is known at compile time that the RHS
78417       ** cannot contain NULL values. This happens as the result
78418       ** of a "NOT NULL" constraint in the database schema.
78419       **
78420       ** Also run this branch if NULL is equivalent to FALSE
78421       ** for this particular IN operator.
78422       */
78423       sqlite3VdbeAddOp4Int(v, OP_NotFound, pExpr->iTable, destIfFalse, r1, 1);
78424 
78425     }else{
78426       /* In this branch, the RHS of the IN might contain a NULL and
78427       ** the presence of a NULL on the RHS makes a difference in the
78428       ** outcome.
78429       */
78430       int j1, j2, j3;
78431 
78432       /* First check to see if the LHS is contained in the RHS.  If so,
78433       ** then the presence of NULLs in the RHS does not matter, so jump
78434       ** over all of the code that follows.
78435       */
78436       j1 = sqlite3VdbeAddOp4Int(v, OP_Found, pExpr->iTable, 0, r1, 1);
78437 
78438       /* Here we begin generating code that runs if the LHS is not
78439       ** contained within the RHS.  Generate additional code that
78440       ** tests the RHS for NULLs.  If the RHS contains a NULL then
78441       ** jump to destIfNull.  If there are no NULLs in the RHS then
78442       ** jump to destIfFalse.
78443       */
78444       j2 = sqlite3VdbeAddOp1(v, OP_NotNull, rRhsHasNull);
78445       j3 = sqlite3VdbeAddOp4Int(v, OP_Found, pExpr->iTable, 0, rRhsHasNull, 1);
78446       sqlite3VdbeAddOp2(v, OP_Integer, -1, rRhsHasNull);
78447       sqlite3VdbeJumpHere(v, j3);
78448       sqlite3VdbeAddOp2(v, OP_AddImm, rRhsHasNull, 1);
78449       sqlite3VdbeJumpHere(v, j2);
78450 
78451       /* Jump to the appropriate target depending on whether or not
78452       ** the RHS contains a NULL
78453       */
78454       sqlite3VdbeAddOp2(v, OP_If, rRhsHasNull, destIfNull);
78455       sqlite3VdbeAddOp2(v, OP_Goto, 0, destIfFalse);
78456 
78457       /* The OP_Found at the top of this branch jumps here when true,
78458       ** causing the overall IN expression evaluation to fall through.
78459       */
78460       sqlite3VdbeJumpHere(v, j1);
78461     }
78462   }
78463   sqlite3ReleaseTempReg(pParse, r1);
78464   sqlite3ExprCachePop(pParse, 1);
78465   VdbeComment((v, "end IN expr"));
78466 }
78467 #endif /* SQLITE_OMIT_SUBQUERY */
78468 
78469 /*
78470 ** Duplicate an 8-byte value
78471 */
78472 static char *dup8bytes(Vdbe *v, const char *in){
78473   char *out = sqlite3DbMallocRaw(sqlite3VdbeDb(v), 8);
78474   if( out ){
78475     memcpy(out, in, 8);
78476   }
78477   return out;
78478 }
78479 
78480 #ifndef SQLITE_OMIT_FLOATING_POINT
78481 /*
78482 ** Generate an instruction that will put the floating point
78483 ** value described by z[0..n-1] into register iMem.
78484 **
78485 ** The z[] string will probably not be zero-terminated.  But the
78486 ** z[n] character is guaranteed to be something that does not look
78487 ** like the continuation of the number.
78488 */
78489 static void codeReal(Vdbe *v, const char *z, int negateFlag, int iMem){
78490   if( ALWAYS(z!=0) ){
78491     double value;
78492     char *zV;
78493     sqlite3AtoF(z, &value, sqlite3Strlen30(z), SQLITE_UTF8);
78494     assert( !sqlite3IsNaN(value) ); /* The new AtoF never returns NaN */
78495     if( negateFlag ) value = -value;
78496     zV = dup8bytes(v, (char*)&value);
78497     sqlite3VdbeAddOp4(v, OP_Real, 0, iMem, 0, zV, P4_REAL);
78498   }
78499 }
78500 #endif
78501 
78502 
78503 /*
78504 ** Generate an instruction that will put the integer describe by
78505 ** text z[0..n-1] into register iMem.
78506 **
78507 ** Expr.u.zToken is always UTF8 and zero-terminated.
78508 */
78509 static void codeInteger(Parse *pParse, Expr *pExpr, int negFlag, int iMem){
78510   Vdbe *v = pParse->pVdbe;
78511   if( pExpr->flags & EP_IntValue ){
78512     int i = pExpr->u.iValue;
78513     assert( i>=0 );
78514     if( negFlag ) i = -i;
78515     sqlite3VdbeAddOp2(v, OP_Integer, i, iMem);
78516   }else{
78517     int c;
78518     i64 value;
78519     const char *z = pExpr->u.zToken;
78520     assert( z!=0 );
78521     c = sqlite3Atoi64(z, &value, sqlite3Strlen30(z), SQLITE_UTF8);
78522     if( c==0 || (c==2 && negFlag) ){
78523       char *zV;
78524       if( negFlag ){ value = c==2 ? SMALLEST_INT64 : -value; }
78525       zV = dup8bytes(v, (char*)&value);
78526       sqlite3VdbeAddOp4(v, OP_Int64, 0, iMem, 0, zV, P4_INT64);
78527     }else{
78528 #ifdef SQLITE_OMIT_FLOATING_POINT
78529       sqlite3ErrorMsg(pParse, "oversized integer: %s%s", negFlag ? "-" : "", z);
78530 #else
78531       codeReal(v, z, negFlag, iMem);
78532 #endif
78533     }
78534   }
78535 }
78536 
78537 /*
78538 ** Clear a cache entry.
78539 */
78540 static void cacheEntryClear(Parse *pParse, struct yColCache *p){
78541   if( p->tempReg ){
78542     if( pParse->nTempReg<ArraySize(pParse->aTempReg) ){
78543       pParse->aTempReg[pParse->nTempReg++] = p->iReg;
78544     }
78545     p->tempReg = 0;
78546   }
78547 }
78548 
78549 
78550 /*
78551 ** Record in the column cache that a particular column from a
78552 ** particular table is stored in a particular register.
78553 */
78554 SQLITE_PRIVATE void sqlite3ExprCacheStore(Parse *pParse, int iTab, int iCol, int iReg){
78555   int i;
78556   int minLru;
78557   int idxLru;
78558   struct yColCache *p;
78559 
78560   assert( iReg>0 );  /* Register numbers are always positive */
78561   assert( iCol>=-1 && iCol<32768 );  /* Finite column numbers */
78562 
78563   /* The SQLITE_ColumnCache flag disables the column cache.  This is used
78564   ** for testing only - to verify that SQLite always gets the same answer
78565   ** with and without the column cache.
78566   */
78567   if( OptimizationDisabled(pParse->db, SQLITE_ColumnCache) ) return;
78568 
78569   /* First replace any existing entry.
78570   **
78571   ** Actually, the way the column cache is currently used, we are guaranteed
78572   ** that the object will never already be in cache.  Verify this guarantee.
78573   */
78574 #ifndef NDEBUG
78575   for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
78576     assert( p->iReg==0 || p->iTable!=iTab || p->iColumn!=iCol );
78577   }
78578 #endif
78579 
78580   /* Find an empty slot and replace it */
78581   for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
78582     if( p->iReg==0 ){
78583       p->iLevel = pParse->iCacheLevel;
78584       p->iTable = iTab;
78585       p->iColumn = iCol;
78586       p->iReg = iReg;
78587       p->tempReg = 0;
78588       p->lru = pParse->iCacheCnt++;
78589       return;
78590     }
78591   }
78592 
78593   /* Replace the last recently used */
78594   minLru = 0x7fffffff;
78595   idxLru = -1;
78596   for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
78597     if( p->lru<minLru ){
78598       idxLru = i;
78599       minLru = p->lru;
78600     }
78601   }
78602   if( ALWAYS(idxLru>=0) ){
78603     p = &pParse->aColCache[idxLru];
78604     p->iLevel = pParse->iCacheLevel;
78605     p->iTable = iTab;
78606     p->iColumn = iCol;
78607     p->iReg = iReg;
78608     p->tempReg = 0;
78609     p->lru = pParse->iCacheCnt++;
78610     return;
78611   }
78612 }
78613 
78614 /*
78615 ** Indicate that registers between iReg..iReg+nReg-1 are being overwritten.
78616 ** Purge the range of registers from the column cache.
78617 */
78618 SQLITE_PRIVATE void sqlite3ExprCacheRemove(Parse *pParse, int iReg, int nReg){
78619   int i;
78620   int iLast = iReg + nReg - 1;
78621   struct yColCache *p;
78622   for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
78623     int r = p->iReg;
78624     if( r>=iReg && r<=iLast ){
78625       cacheEntryClear(pParse, p);
78626       p->iReg = 0;
78627     }
78628   }
78629 }
78630 
78631 /*
78632 ** Remember the current column cache context.  Any new entries added
78633 ** added to the column cache after this call are removed when the
78634 ** corresponding pop occurs.
78635 */
78636 SQLITE_PRIVATE void sqlite3ExprCachePush(Parse *pParse){
78637   pParse->iCacheLevel++;
78638 #ifdef SQLITE_DEBUG
78639   if( pParse->db->flags & SQLITE_VdbeAddopTrace ){
78640     printf("PUSH to %d\n", pParse->iCacheLevel);
78641   }
78642 #endif
78643 }
78644 
78645 /*
78646 ** Remove from the column cache any entries that were added since the
78647 ** the previous N Push operations.  In other words, restore the cache
78648 ** to the state it was in N Pushes ago.
78649 */
78650 SQLITE_PRIVATE void sqlite3ExprCachePop(Parse *pParse, int N){
78651   int i;
78652   struct yColCache *p;
78653   assert( N>0 );
78654   assert( pParse->iCacheLevel>=N );
78655   pParse->iCacheLevel -= N;
78656 #ifdef SQLITE_DEBUG
78657   if( pParse->db->flags & SQLITE_VdbeAddopTrace ){
78658     printf("POP  to %d\n", pParse->iCacheLevel);
78659   }
78660 #endif
78661   for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
78662     if( p->iReg && p->iLevel>pParse->iCacheLevel ){
78663       cacheEntryClear(pParse, p);
78664       p->iReg = 0;
78665     }
78666   }
78667 }
78668 
78669 /*
78670 ** When a cached column is reused, make sure that its register is
78671 ** no longer available as a temp register.  ticket #3879:  that same
78672 ** register might be in the cache in multiple places, so be sure to
78673 ** get them all.
78674 */
78675 static void sqlite3ExprCachePinRegister(Parse *pParse, int iReg){
78676   int i;
78677   struct yColCache *p;
78678   for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
78679     if( p->iReg==iReg ){
78680       p->tempReg = 0;
78681     }
78682   }
78683 }
78684 
78685 /*
78686 ** Generate code to extract the value of the iCol-th column of a table.
78687 */
78688 SQLITE_PRIVATE void sqlite3ExprCodeGetColumnOfTable(
78689   Vdbe *v,        /* The VDBE under construction */
78690   Table *pTab,    /* The table containing the value */
78691   int iTabCur,    /* The table cursor.  Or the PK cursor for WITHOUT ROWID */
78692   int iCol,       /* Index of the column to extract */
78693   int regOut      /* Extract the value into this register */
78694 ){
78695   if( iCol<0 || iCol==pTab->iPKey ){
78696     sqlite3VdbeAddOp2(v, OP_Rowid, iTabCur, regOut);
78697   }else{
78698     int op = IsVirtual(pTab) ? OP_VColumn : OP_Column;
78699     int x = iCol;
78700     if( !HasRowid(pTab) ){
78701       x = sqlite3ColumnOfIndex(sqlite3PrimaryKeyIndex(pTab), iCol);
78702     }
78703     sqlite3VdbeAddOp3(v, op, iTabCur, x, regOut);
78704   }
78705   if( iCol>=0 ){
78706     sqlite3ColumnDefault(v, pTab, iCol, regOut);
78707   }
78708 }
78709 
78710 /*
78711 ** Generate code that will extract the iColumn-th column from
78712 ** table pTab and store the column value in a register.  An effort
78713 ** is made to store the column value in register iReg, but this is
78714 ** not guaranteed.  The location of the column value is returned.
78715 **
78716 ** There must be an open cursor to pTab in iTable when this routine
78717 ** is called.  If iColumn<0 then code is generated that extracts the rowid.
78718 */
78719 SQLITE_PRIVATE int sqlite3ExprCodeGetColumn(
78720   Parse *pParse,   /* Parsing and code generating context */
78721   Table *pTab,     /* Description of the table we are reading from */
78722   int iColumn,     /* Index of the table column */
78723   int iTable,      /* The cursor pointing to the table */
78724   int iReg,        /* Store results here */
78725   u8 p5            /* P5 value for OP_Column */
78726 ){
78727   Vdbe *v = pParse->pVdbe;
78728   int i;
78729   struct yColCache *p;
78730 
78731   for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
78732     if( p->iReg>0 && p->iTable==iTable && p->iColumn==iColumn ){
78733       p->lru = pParse->iCacheCnt++;
78734       sqlite3ExprCachePinRegister(pParse, p->iReg);
78735       return p->iReg;
78736     }
78737   }
78738   assert( v!=0 );
78739   sqlite3ExprCodeGetColumnOfTable(v, pTab, iTable, iColumn, iReg);
78740   if( p5 ){
78741     sqlite3VdbeChangeP5(v, p5);
78742   }else{
78743     sqlite3ExprCacheStore(pParse, iTable, iColumn, iReg);
78744   }
78745   return iReg;
78746 }
78747 
78748 /*
78749 ** Clear all column cache entries.
78750 */
78751 SQLITE_PRIVATE void sqlite3ExprCacheClear(Parse *pParse){
78752   int i;
78753   struct yColCache *p;
78754 
78755 #if SQLITE_DEBUG
78756   if( pParse->db->flags & SQLITE_VdbeAddopTrace ){
78757     printf("CLEAR\n");
78758   }
78759 #endif
78760   for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
78761     if( p->iReg ){
78762       cacheEntryClear(pParse, p);
78763       p->iReg = 0;
78764     }
78765   }
78766 }
78767 
78768 /*
78769 ** Record the fact that an affinity change has occurred on iCount
78770 ** registers starting with iStart.
78771 */
78772 SQLITE_PRIVATE void sqlite3ExprCacheAffinityChange(Parse *pParse, int iStart, int iCount){
78773   sqlite3ExprCacheRemove(pParse, iStart, iCount);
78774 }
78775 
78776 /*
78777 ** Generate code to move content from registers iFrom...iFrom+nReg-1
78778 ** over to iTo..iTo+nReg-1. Keep the column cache up-to-date.
78779 */
78780 SQLITE_PRIVATE void sqlite3ExprCodeMove(Parse *pParse, int iFrom, int iTo, int nReg){
78781   int i;
78782   struct yColCache *p;
78783   assert( iFrom>=iTo+nReg || iFrom+nReg<=iTo );
78784   sqlite3VdbeAddOp3(pParse->pVdbe, OP_Move, iFrom, iTo, nReg-1);
78785   for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
78786     int x = p->iReg;
78787     if( x>=iFrom && x<iFrom+nReg ){
78788       p->iReg += iTo-iFrom;
78789     }
78790   }
78791 }
78792 
78793 #if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST)
78794 /*
78795 ** Return true if any register in the range iFrom..iTo (inclusive)
78796 ** is used as part of the column cache.
78797 **
78798 ** This routine is used within assert() and testcase() macros only
78799 ** and does not appear in a normal build.
78800 */
78801 static int usedAsColumnCache(Parse *pParse, int iFrom, int iTo){
78802   int i;
78803   struct yColCache *p;
78804   for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
78805     int r = p->iReg;
78806     if( r>=iFrom && r<=iTo ) return 1;    /*NO_TEST*/
78807   }
78808   return 0;
78809 }
78810 #endif /* SQLITE_DEBUG || SQLITE_COVERAGE_TEST */
78811 
78812 /*
78813 ** Convert an expression node to a TK_REGISTER
78814 */
78815 static void exprToRegister(Expr *p, int iReg){
78816   p->op2 = p->op;
78817   p->op = TK_REGISTER;
78818   p->iTable = iReg;
78819   ExprClearProperty(p, EP_Skip);
78820 }
78821 
78822 /*
78823 ** Generate code into the current Vdbe to evaluate the given
78824 ** expression.  Attempt to store the results in register "target".
78825 ** Return the register where results are stored.
78826 **
78827 ** With this routine, there is no guarantee that results will
78828 ** be stored in target.  The result might be stored in some other
78829 ** register if it is convenient to do so.  The calling function
78830 ** must check the return code and move the results to the desired
78831 ** register.
78832 */
78833 SQLITE_PRIVATE int sqlite3ExprCodeTarget(Parse *pParse, Expr *pExpr, int target){
78834   Vdbe *v = pParse->pVdbe;  /* The VM under construction */
78835   int op;                   /* The opcode being coded */
78836   int inReg = target;       /* Results stored in register inReg */
78837   int regFree1 = 0;         /* If non-zero free this temporary register */
78838   int regFree2 = 0;         /* If non-zero free this temporary register */
78839   int r1, r2, r3, r4;       /* Various register numbers */
78840   sqlite3 *db = pParse->db; /* The database connection */
78841   Expr tempX;               /* Temporary expression node */
78842 
78843   assert( target>0 && target<=pParse->nMem );
78844   if( v==0 ){
78845     assert( pParse->db->mallocFailed );
78846     return 0;
78847   }
78848 
78849   if( pExpr==0 ){
78850     op = TK_NULL;
78851   }else{
78852     op = pExpr->op;
78853   }
78854   switch( op ){
78855     case TK_AGG_COLUMN: {
78856       AggInfo *pAggInfo = pExpr->pAggInfo;
78857       struct AggInfo_col *pCol = &pAggInfo->aCol[pExpr->iAgg];
78858       if( !pAggInfo->directMode ){
78859         assert( pCol->iMem>0 );
78860         inReg = pCol->iMem;
78861         break;
78862       }else if( pAggInfo->useSortingIdx ){
78863         sqlite3VdbeAddOp3(v, OP_Column, pAggInfo->sortingIdxPTab,
78864                               pCol->iSorterColumn, target);
78865         break;
78866       }
78867       /* Otherwise, fall thru into the TK_COLUMN case */
78868     }
78869     case TK_COLUMN: {
78870       int iTab = pExpr->iTable;
78871       if( iTab<0 ){
78872         if( pParse->ckBase>0 ){
78873           /* Generating CHECK constraints or inserting into partial index */
78874           inReg = pExpr->iColumn + pParse->ckBase;
78875           break;
78876         }else{
78877           /* Deleting from a partial index */
78878           iTab = pParse->iPartIdxTab;
78879         }
78880       }
78881       inReg = sqlite3ExprCodeGetColumn(pParse, pExpr->pTab,
78882                                pExpr->iColumn, iTab, target,
78883                                pExpr->op2);
78884       break;
78885     }
78886     case TK_INTEGER: {
78887       codeInteger(pParse, pExpr, 0, target);
78888       break;
78889     }
78890 #ifndef SQLITE_OMIT_FLOATING_POINT
78891     case TK_FLOAT: {
78892       assert( !ExprHasProperty(pExpr, EP_IntValue) );
78893       codeReal(v, pExpr->u.zToken, 0, target);
78894       break;
78895     }
78896 #endif
78897     case TK_STRING: {
78898       assert( !ExprHasProperty(pExpr, EP_IntValue) );
78899       sqlite3VdbeAddOp4(v, OP_String8, 0, target, 0, pExpr->u.zToken, 0);
78900       break;
78901     }
78902     case TK_NULL: {
78903       sqlite3VdbeAddOp2(v, OP_Null, 0, target);
78904       break;
78905     }
78906 #ifndef SQLITE_OMIT_BLOB_LITERAL
78907     case TK_BLOB: {
78908       int n;
78909       const char *z;
78910       char *zBlob;
78911       assert( !ExprHasProperty(pExpr, EP_IntValue) );
78912       assert( pExpr->u.zToken[0]=='x' || pExpr->u.zToken[0]=='X' );
78913       assert( pExpr->u.zToken[1]=='\'' );
78914       z = &pExpr->u.zToken[2];
78915       n = sqlite3Strlen30(z) - 1;
78916       assert( z[n]=='\'' );
78917       zBlob = sqlite3HexToBlob(sqlite3VdbeDb(v), z, n);
78918       sqlite3VdbeAddOp4(v, OP_Blob, n/2, target, 0, zBlob, P4_DYNAMIC);
78919       break;
78920     }
78921 #endif
78922     case TK_VARIABLE: {
78923       assert( !ExprHasProperty(pExpr, EP_IntValue) );
78924       assert( pExpr->u.zToken!=0 );
78925       assert( pExpr->u.zToken[0]!=0 );
78926       sqlite3VdbeAddOp2(v, OP_Variable, pExpr->iColumn, target);
78927       if( pExpr->u.zToken[1]!=0 ){
78928         assert( pExpr->u.zToken[0]=='?'
78929              || strcmp(pExpr->u.zToken, pParse->azVar[pExpr->iColumn-1])==0 );
78930         sqlite3VdbeChangeP4(v, -1, pParse->azVar[pExpr->iColumn-1], P4_STATIC);
78931       }
78932       break;
78933     }
78934     case TK_REGISTER: {
78935       inReg = pExpr->iTable;
78936       break;
78937     }
78938     case TK_AS: {
78939       inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
78940       break;
78941     }
78942 #ifndef SQLITE_OMIT_CAST
78943     case TK_CAST: {
78944       /* Expressions of the form:   CAST(pLeft AS token) */
78945       int aff, to_op;
78946       inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
78947       assert( !ExprHasProperty(pExpr, EP_IntValue) );
78948       aff = sqlite3AffinityType(pExpr->u.zToken, 0);
78949       to_op = aff - SQLITE_AFF_TEXT + OP_ToText;
78950       assert( to_op==OP_ToText    || aff!=SQLITE_AFF_TEXT    );
78951       assert( to_op==OP_ToBlob    || aff!=SQLITE_AFF_NONE    );
78952       assert( to_op==OP_ToNumeric || aff!=SQLITE_AFF_NUMERIC );
78953       assert( to_op==OP_ToInt     || aff!=SQLITE_AFF_INTEGER );
78954       assert( to_op==OP_ToReal    || aff!=SQLITE_AFF_REAL    );
78955       testcase( to_op==OP_ToText );
78956       testcase( to_op==OP_ToBlob );
78957       testcase( to_op==OP_ToNumeric );
78958       testcase( to_op==OP_ToInt );
78959       testcase( to_op==OP_ToReal );
78960       if( inReg!=target ){
78961         sqlite3VdbeAddOp2(v, OP_SCopy, inReg, target);
78962         inReg = target;
78963       }
78964       sqlite3VdbeAddOp1(v, to_op, inReg);
78965       testcase( usedAsColumnCache(pParse, inReg, inReg) );
78966       sqlite3ExprCacheAffinityChange(pParse, inReg, 1);
78967       break;
78968     }
78969 #endif /* SQLITE_OMIT_CAST */
78970     case TK_LT:
78971     case TK_LE:
78972     case TK_GT:
78973     case TK_GE:
78974     case TK_NE:
78975     case TK_EQ: {
78976       assert( TK_LT==OP_Lt );
78977       assert( TK_LE==OP_Le );
78978       assert( TK_GT==OP_Gt );
78979       assert( TK_GE==OP_Ge );
78980       assert( TK_EQ==OP_Eq );
78981       assert( TK_NE==OP_Ne );
78982       testcase( op==TK_LT );
78983       testcase( op==TK_LE );
78984       testcase( op==TK_GT );
78985       testcase( op==TK_GE );
78986       testcase( op==TK_EQ );
78987       testcase( op==TK_NE );
78988       r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
78989       r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
78990       codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
78991                   r1, r2, inReg, SQLITE_STOREP2);
78992       testcase( regFree1==0 );
78993       testcase( regFree2==0 );
78994       break;
78995     }
78996     case TK_IS:
78997     case TK_ISNOT: {
78998       testcase( op==TK_IS );
78999       testcase( op==TK_ISNOT );
79000       r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
79001       r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
79002       op = (op==TK_IS) ? TK_EQ : TK_NE;
79003       codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
79004                   r1, r2, inReg, SQLITE_STOREP2 | SQLITE_NULLEQ);
79005       testcase( regFree1==0 );
79006       testcase( regFree2==0 );
79007       break;
79008     }
79009     case TK_AND:
79010     case TK_OR:
79011     case TK_PLUS:
79012     case TK_STAR:
79013     case TK_MINUS:
79014     case TK_REM:
79015     case TK_BITAND:
79016     case TK_BITOR:
79017     case TK_SLASH:
79018     case TK_LSHIFT:
79019     case TK_RSHIFT:
79020     case TK_CONCAT: {
79021       assert( TK_AND==OP_And );
79022       assert( TK_OR==OP_Or );
79023       assert( TK_PLUS==OP_Add );
79024       assert( TK_MINUS==OP_Subtract );
79025       assert( TK_REM==OP_Remainder );
79026       assert( TK_BITAND==OP_BitAnd );
79027       assert( TK_BITOR==OP_BitOr );
79028       assert( TK_SLASH==OP_Divide );
79029       assert( TK_LSHIFT==OP_ShiftLeft );
79030       assert( TK_RSHIFT==OP_ShiftRight );
79031       assert( TK_CONCAT==OP_Concat );
79032       testcase( op==TK_AND );
79033       testcase( op==TK_OR );
79034       testcase( op==TK_PLUS );
79035       testcase( op==TK_MINUS );
79036       testcase( op==TK_REM );
79037       testcase( op==TK_BITAND );
79038       testcase( op==TK_BITOR );
79039       testcase( op==TK_SLASH );
79040       testcase( op==TK_LSHIFT );
79041       testcase( op==TK_RSHIFT );
79042       testcase( op==TK_CONCAT );
79043       r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
79044       r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
79045       sqlite3VdbeAddOp3(v, op, r2, r1, target);
79046       testcase( regFree1==0 );
79047       testcase( regFree2==0 );
79048       break;
79049     }
79050     case TK_UMINUS: {
79051       Expr *pLeft = pExpr->pLeft;
79052       assert( pLeft );
79053       if( pLeft->op==TK_INTEGER ){
79054         codeInteger(pParse, pLeft, 1, target);
79055 #ifndef SQLITE_OMIT_FLOATING_POINT
79056       }else if( pLeft->op==TK_FLOAT ){
79057         assert( !ExprHasProperty(pExpr, EP_IntValue) );
79058         codeReal(v, pLeft->u.zToken, 1, target);
79059 #endif
79060       }else{
79061         tempX.op = TK_INTEGER;
79062         tempX.flags = EP_IntValue|EP_TokenOnly;
79063         tempX.u.iValue = 0;
79064         r1 = sqlite3ExprCodeTemp(pParse, &tempX, &regFree1);
79065         r2 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree2);
79066         sqlite3VdbeAddOp3(v, OP_Subtract, r2, r1, target);
79067         testcase( regFree2==0 );
79068       }
79069       inReg = target;
79070       break;
79071     }
79072     case TK_BITNOT:
79073     case TK_NOT: {
79074       assert( TK_BITNOT==OP_BitNot );
79075       assert( TK_NOT==OP_Not );
79076       testcase( op==TK_BITNOT );
79077       testcase( op==TK_NOT );
79078       r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
79079       testcase( regFree1==0 );
79080       inReg = target;
79081       sqlite3VdbeAddOp2(v, op, r1, inReg);
79082       break;
79083     }
79084     case TK_ISNULL:
79085     case TK_NOTNULL: {
79086       int addr;
79087       assert( TK_ISNULL==OP_IsNull );
79088       assert( TK_NOTNULL==OP_NotNull );
79089       testcase( op==TK_ISNULL );
79090       testcase( op==TK_NOTNULL );
79091       sqlite3VdbeAddOp2(v, OP_Integer, 1, target);
79092       r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
79093       testcase( regFree1==0 );
79094       addr = sqlite3VdbeAddOp1(v, op, r1);
79095       sqlite3VdbeAddOp2(v, OP_AddImm, target, -1);
79096       sqlite3VdbeJumpHere(v, addr);
79097       break;
79098     }
79099     case TK_AGG_FUNCTION: {
79100       AggInfo *pInfo = pExpr->pAggInfo;
79101       if( pInfo==0 ){
79102         assert( !ExprHasProperty(pExpr, EP_IntValue) );
79103         sqlite3ErrorMsg(pParse, "misuse of aggregate: %s()", pExpr->u.zToken);
79104       }else{
79105         inReg = pInfo->aFunc[pExpr->iAgg].iMem;
79106       }
79107       break;
79108     }
79109     case TK_FUNCTION: {
79110       ExprList *pFarg;       /* List of function arguments */
79111       int nFarg;             /* Number of function arguments */
79112       FuncDef *pDef;         /* The function definition object */
79113       int nId;               /* Length of the function name in bytes */
79114       const char *zId;       /* The function name */
79115       u32 constMask = 0;     /* Mask of function arguments that are constant */
79116       int i;                 /* Loop counter */
79117       u8 enc = ENC(db);      /* The text encoding used by this database */
79118       CollSeq *pColl = 0;    /* A collating sequence */
79119 
79120       assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
79121       if( ExprHasProperty(pExpr, EP_TokenOnly) ){
79122         pFarg = 0;
79123       }else{
79124         pFarg = pExpr->x.pList;
79125       }
79126       nFarg = pFarg ? pFarg->nExpr : 0;
79127       assert( !ExprHasProperty(pExpr, EP_IntValue) );
79128       zId = pExpr->u.zToken;
79129       nId = sqlite3Strlen30(zId);
79130       pDef = sqlite3FindFunction(db, zId, nId, nFarg, enc, 0);
79131       if( pDef==0 ){
79132         sqlite3ErrorMsg(pParse, "unknown function: %.*s()", nId, zId);
79133         break;
79134       }
79135 
79136       /* Attempt a direct implementation of the built-in COALESCE() and
79137       ** IFNULL() functions.  This avoids unnecessary evalation of
79138       ** arguments past the first non-NULL argument.
79139       */
79140       if( pDef->funcFlags & SQLITE_FUNC_COALESCE ){
79141         int endCoalesce = sqlite3VdbeMakeLabel(v);
79142         assert( nFarg>=2 );
79143         sqlite3ExprCode(pParse, pFarg->a[0].pExpr, target);
79144         for(i=1; i<nFarg; i++){
79145           sqlite3VdbeAddOp2(v, OP_NotNull, target, endCoalesce);
79146           sqlite3ExprCacheRemove(pParse, target, 1);
79147           sqlite3ExprCachePush(pParse);
79148           sqlite3ExprCode(pParse, pFarg->a[i].pExpr, target);
79149           sqlite3ExprCachePop(pParse, 1);
79150         }
79151         sqlite3VdbeResolveLabel(v, endCoalesce);
79152         break;
79153       }
79154 
79155       /* The UNLIKELY() function is a no-op.  The result is the value
79156       ** of the first argument.
79157       */
79158       if( pDef->funcFlags & SQLITE_FUNC_UNLIKELY ){
79159         assert( nFarg>=1 );
79160         sqlite3ExprCode(pParse, pFarg->a[0].pExpr, target);
79161         break;
79162       }
79163 
79164       for(i=0; i<nFarg; i++){
79165         if( i<32 && sqlite3ExprIsConstant(pFarg->a[i].pExpr) ){
79166           testcase( i==31 );
79167           constMask |= MASKBIT32(i);
79168         }
79169         if( (pDef->funcFlags & SQLITE_FUNC_NEEDCOLL)!=0 && !pColl ){
79170           pColl = sqlite3ExprCollSeq(pParse, pFarg->a[i].pExpr);
79171         }
79172       }
79173       if( pFarg ){
79174         if( constMask ){
79175           r1 = pParse->nMem+1;
79176           pParse->nMem += nFarg;
79177         }else{
79178           r1 = sqlite3GetTempRange(pParse, nFarg);
79179         }
79180 
79181         /* For length() and typeof() functions with a column argument,
79182         ** set the P5 parameter to the OP_Column opcode to OPFLAG_LENGTHARG
79183         ** or OPFLAG_TYPEOFARG respectively, to avoid unnecessary data
79184         ** loading.
79185         */
79186         if( (pDef->funcFlags & (SQLITE_FUNC_LENGTH|SQLITE_FUNC_TYPEOF))!=0 ){
79187           u8 exprOp;
79188           assert( nFarg==1 );
79189           assert( pFarg->a[0].pExpr!=0 );
79190           exprOp = pFarg->a[0].pExpr->op;
79191           if( exprOp==TK_COLUMN || exprOp==TK_AGG_COLUMN ){
79192             assert( SQLITE_FUNC_LENGTH==OPFLAG_LENGTHARG );
79193             assert( SQLITE_FUNC_TYPEOF==OPFLAG_TYPEOFARG );
79194             testcase( pDef->funcFlags & OPFLAG_LENGTHARG );
79195             pFarg->a[0].pExpr->op2 =
79196                   pDef->funcFlags & (OPFLAG_LENGTHARG|OPFLAG_TYPEOFARG);
79197           }
79198         }
79199 
79200         sqlite3ExprCachePush(pParse);     /* Ticket 2ea2425d34be */
79201         sqlite3ExprCodeExprList(pParse, pFarg, r1,
79202                                 SQLITE_ECEL_DUP|SQLITE_ECEL_FACTOR);
79203         sqlite3ExprCachePop(pParse, 1);   /* Ticket 2ea2425d34be */
79204       }else{
79205         r1 = 0;
79206       }
79207 #ifndef SQLITE_OMIT_VIRTUALTABLE
79208       /* Possibly overload the function if the first argument is
79209       ** a virtual table column.
79210       **
79211       ** For infix functions (LIKE, GLOB, REGEXP, and MATCH) use the
79212       ** second argument, not the first, as the argument to test to
79213       ** see if it is a column in a virtual table.  This is done because
79214       ** the left operand of infix functions (the operand we want to
79215       ** control overloading) ends up as the second argument to the
79216       ** function.  The expression "A glob B" is equivalent to
79217       ** "glob(B,A).  We want to use the A in "A glob B" to test
79218       ** for function overloading.  But we use the B term in "glob(B,A)".
79219       */
79220       if( nFarg>=2 && (pExpr->flags & EP_InfixFunc) ){
79221         pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[1].pExpr);
79222       }else if( nFarg>0 ){
79223         pDef = sqlite3VtabOverloadFunction(db, pDef, nFarg, pFarg->a[0].pExpr);
79224       }
79225 #endif
79226       if( pDef->funcFlags & SQLITE_FUNC_NEEDCOLL ){
79227         if( !pColl ) pColl = db->pDfltColl;
79228         sqlite3VdbeAddOp4(v, OP_CollSeq, 0, 0, 0, (char *)pColl, P4_COLLSEQ);
79229       }
79230       sqlite3VdbeAddOp4(v, OP_Function, constMask, r1, target,
79231                         (char*)pDef, P4_FUNCDEF);
79232       sqlite3VdbeChangeP5(v, (u8)nFarg);
79233       if( nFarg && constMask==0 ){
79234         sqlite3ReleaseTempRange(pParse, r1, nFarg);
79235       }
79236       break;
79237     }
79238 #ifndef SQLITE_OMIT_SUBQUERY
79239     case TK_EXISTS:
79240     case TK_SELECT: {
79241       testcase( op==TK_EXISTS );
79242       testcase( op==TK_SELECT );
79243       inReg = sqlite3CodeSubselect(pParse, pExpr, 0, 0);
79244       break;
79245     }
79246     case TK_IN: {
79247       int destIfFalse = sqlite3VdbeMakeLabel(v);
79248       int destIfNull = sqlite3VdbeMakeLabel(v);
79249       sqlite3VdbeAddOp2(v, OP_Null, 0, target);
79250       sqlite3ExprCodeIN(pParse, pExpr, destIfFalse, destIfNull);
79251       sqlite3VdbeAddOp2(v, OP_Integer, 1, target);
79252       sqlite3VdbeResolveLabel(v, destIfFalse);
79253       sqlite3VdbeAddOp2(v, OP_AddImm, target, 0);
79254       sqlite3VdbeResolveLabel(v, destIfNull);
79255       break;
79256     }
79257 #endif /* SQLITE_OMIT_SUBQUERY */
79258 
79259 
79260     /*
79261     **    x BETWEEN y AND z
79262     **
79263     ** This is equivalent to
79264     **
79265     **    x>=y AND x<=z
79266     **
79267     ** X is stored in pExpr->pLeft.
79268     ** Y is stored in pExpr->pList->a[0].pExpr.
79269     ** Z is stored in pExpr->pList->a[1].pExpr.
79270     */
79271     case TK_BETWEEN: {
79272       Expr *pLeft = pExpr->pLeft;
79273       struct ExprList_item *pLItem = pExpr->x.pList->a;
79274       Expr *pRight = pLItem->pExpr;
79275 
79276       r1 = sqlite3ExprCodeTemp(pParse, pLeft, &regFree1);
79277       r2 = sqlite3ExprCodeTemp(pParse, pRight, &regFree2);
79278       testcase( regFree1==0 );
79279       testcase( regFree2==0 );
79280       r3 = sqlite3GetTempReg(pParse);
79281       r4 = sqlite3GetTempReg(pParse);
79282       codeCompare(pParse, pLeft, pRight, OP_Ge,
79283                   r1, r2, r3, SQLITE_STOREP2);
79284       pLItem++;
79285       pRight = pLItem->pExpr;
79286       sqlite3ReleaseTempReg(pParse, regFree2);
79287       r2 = sqlite3ExprCodeTemp(pParse, pRight, &regFree2);
79288       testcase( regFree2==0 );
79289       codeCompare(pParse, pLeft, pRight, OP_Le, r1, r2, r4, SQLITE_STOREP2);
79290       sqlite3VdbeAddOp3(v, OP_And, r3, r4, target);
79291       sqlite3ReleaseTempReg(pParse, r3);
79292       sqlite3ReleaseTempReg(pParse, r4);
79293       break;
79294     }
79295     case TK_COLLATE:
79296     case TK_UPLUS: {
79297       inReg = sqlite3ExprCodeTarget(pParse, pExpr->pLeft, target);
79298       break;
79299     }
79300 
79301     case TK_TRIGGER: {
79302       /* If the opcode is TK_TRIGGER, then the expression is a reference
79303       ** to a column in the new.* or old.* pseudo-tables available to
79304       ** trigger programs. In this case Expr.iTable is set to 1 for the
79305       ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn
79306       ** is set to the column of the pseudo-table to read, or to -1 to
79307       ** read the rowid field.
79308       **
79309       ** The expression is implemented using an OP_Param opcode. The p1
79310       ** parameter is set to 0 for an old.rowid reference, or to (i+1)
79311       ** to reference another column of the old.* pseudo-table, where
79312       ** i is the index of the column. For a new.rowid reference, p1 is
79313       ** set to (n+1), where n is the number of columns in each pseudo-table.
79314       ** For a reference to any other column in the new.* pseudo-table, p1
79315       ** is set to (n+2+i), where n and i are as defined previously. For
79316       ** example, if the table on which triggers are being fired is
79317       ** declared as:
79318       **
79319       **   CREATE TABLE t1(a, b);
79320       **
79321       ** Then p1 is interpreted as follows:
79322       **
79323       **   p1==0   ->    old.rowid     p1==3   ->    new.rowid
79324       **   p1==1   ->    old.a         p1==4   ->    new.a
79325       **   p1==2   ->    old.b         p1==5   ->    new.b
79326       */
79327       Table *pTab = pExpr->pTab;
79328       int p1 = pExpr->iTable * (pTab->nCol+1) + 1 + pExpr->iColumn;
79329 
79330       assert( pExpr->iTable==0 || pExpr->iTable==1 );
79331       assert( pExpr->iColumn>=-1 && pExpr->iColumn<pTab->nCol );
79332       assert( pTab->iPKey<0 || pExpr->iColumn!=pTab->iPKey );
79333       assert( p1>=0 && p1<(pTab->nCol*2+2) );
79334 
79335       sqlite3VdbeAddOp2(v, OP_Param, p1, target);
79336       VdbeComment((v, "%s.%s -> $%d",
79337         (pExpr->iTable ? "new" : "old"),
79338         (pExpr->iColumn<0 ? "rowid" : pExpr->pTab->aCol[pExpr->iColumn].zName),
79339         target
79340       ));
79341 
79342 #ifndef SQLITE_OMIT_FLOATING_POINT
79343       /* If the column has REAL affinity, it may currently be stored as an
79344       ** integer. Use OP_RealAffinity to make sure it is really real.  */
79345       if( pExpr->iColumn>=0
79346        && pTab->aCol[pExpr->iColumn].affinity==SQLITE_AFF_REAL
79347       ){
79348         sqlite3VdbeAddOp1(v, OP_RealAffinity, target);
79349       }
79350 #endif
79351       break;
79352     }
79353 
79354 
79355     /*
79356     ** Form A:
79357     **   CASE x WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END
79358     **
79359     ** Form B:
79360     **   CASE WHEN e1 THEN r1 WHEN e2 THEN r2 ... WHEN eN THEN rN ELSE y END
79361     **
79362     ** Form A is can be transformed into the equivalent form B as follows:
79363     **   CASE WHEN x=e1 THEN r1 WHEN x=e2 THEN r2 ...
79364     **        WHEN x=eN THEN rN ELSE y END
79365     **
79366     ** X (if it exists) is in pExpr->pLeft.
79367     ** Y is in the last element of pExpr->x.pList if pExpr->x.pList->nExpr is
79368     ** odd.  The Y is also optional.  If the number of elements in x.pList
79369     ** is even, then Y is omitted and the "otherwise" result is NULL.
79370     ** Ei is in pExpr->pList->a[i*2] and Ri is pExpr->pList->a[i*2+1].
79371     **
79372     ** The result of the expression is the Ri for the first matching Ei,
79373     ** or if there is no matching Ei, the ELSE term Y, or if there is
79374     ** no ELSE term, NULL.
79375     */
79376     default: assert( op==TK_CASE ); {
79377       int endLabel;                     /* GOTO label for end of CASE stmt */
79378       int nextCase;                     /* GOTO label for next WHEN clause */
79379       int nExpr;                        /* 2x number of WHEN terms */
79380       int i;                            /* Loop counter */
79381       ExprList *pEList;                 /* List of WHEN terms */
79382       struct ExprList_item *aListelem;  /* Array of WHEN terms */
79383       Expr opCompare;                   /* The X==Ei expression */
79384       Expr *pX;                         /* The X expression */
79385       Expr *pTest = 0;                  /* X==Ei (form A) or just Ei (form B) */
79386       VVA_ONLY( int iCacheLevel = pParse->iCacheLevel; )
79387 
79388       assert( !ExprHasProperty(pExpr, EP_xIsSelect) && pExpr->x.pList );
79389       assert(pExpr->x.pList->nExpr > 0);
79390       pEList = pExpr->x.pList;
79391       aListelem = pEList->a;
79392       nExpr = pEList->nExpr;
79393       endLabel = sqlite3VdbeMakeLabel(v);
79394       if( (pX = pExpr->pLeft)!=0 ){
79395         tempX = *pX;
79396         testcase( pX->op==TK_COLUMN );
79397         exprToRegister(&tempX, sqlite3ExprCodeTemp(pParse, pX, &regFree1));
79398         testcase( regFree1==0 );
79399         opCompare.op = TK_EQ;
79400         opCompare.pLeft = &tempX;
79401         pTest = &opCompare;
79402         /* Ticket b351d95f9cd5ef17e9d9dbae18f5ca8611190001:
79403         ** The value in regFree1 might get SCopy-ed into the file result.
79404         ** So make sure that the regFree1 register is not reused for other
79405         ** purposes and possibly overwritten.  */
79406         regFree1 = 0;
79407       }
79408       for(i=0; i<nExpr-1; i=i+2){
79409         sqlite3ExprCachePush(pParse);
79410         if( pX ){
79411           assert( pTest!=0 );
79412           opCompare.pRight = aListelem[i].pExpr;
79413         }else{
79414           pTest = aListelem[i].pExpr;
79415         }
79416         nextCase = sqlite3VdbeMakeLabel(v);
79417         testcase( pTest->op==TK_COLUMN );
79418         sqlite3ExprIfFalse(pParse, pTest, nextCase, SQLITE_JUMPIFNULL);
79419         testcase( aListelem[i+1].pExpr->op==TK_COLUMN );
79420         sqlite3ExprCode(pParse, aListelem[i+1].pExpr, target);
79421         sqlite3VdbeAddOp2(v, OP_Goto, 0, endLabel);
79422         sqlite3ExprCachePop(pParse, 1);
79423         sqlite3VdbeResolveLabel(v, nextCase);
79424       }
79425       if( (nExpr&1)!=0 ){
79426         sqlite3ExprCachePush(pParse);
79427         sqlite3ExprCode(pParse, pEList->a[nExpr-1].pExpr, target);
79428         sqlite3ExprCachePop(pParse, 1);
79429       }else{
79430         sqlite3VdbeAddOp2(v, OP_Null, 0, target);
79431       }
79432       assert( db->mallocFailed || pParse->nErr>0
79433            || pParse->iCacheLevel==iCacheLevel );
79434       sqlite3VdbeResolveLabel(v, endLabel);
79435       break;
79436     }
79437 #ifndef SQLITE_OMIT_TRIGGER
79438     case TK_RAISE: {
79439       assert( pExpr->affinity==OE_Rollback
79440            || pExpr->affinity==OE_Abort
79441            || pExpr->affinity==OE_Fail
79442            || pExpr->affinity==OE_Ignore
79443       );
79444       if( !pParse->pTriggerTab ){
79445         sqlite3ErrorMsg(pParse,
79446                        "RAISE() may only be used within a trigger-program");
79447         return 0;
79448       }
79449       if( pExpr->affinity==OE_Abort ){
79450         sqlite3MayAbort(pParse);
79451       }
79452       assert( !ExprHasProperty(pExpr, EP_IntValue) );
79453       if( pExpr->affinity==OE_Ignore ){
79454         sqlite3VdbeAddOp4(
79455             v, OP_Halt, SQLITE_OK, OE_Ignore, 0, pExpr->u.zToken,0);
79456       }else{
79457         sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_TRIGGER,
79458                               pExpr->affinity, pExpr->u.zToken, 0, 0);
79459       }
79460 
79461       break;
79462     }
79463 #endif
79464   }
79465   sqlite3ReleaseTempReg(pParse, regFree1);
79466   sqlite3ReleaseTempReg(pParse, regFree2);
79467   return inReg;
79468 }
79469 
79470 /*
79471 ** Factor out the code of the given expression to initialization time.
79472 */
79473 SQLITE_PRIVATE void sqlite3ExprCodeAtInit(
79474   Parse *pParse,    /* Parsing context */
79475   Expr *pExpr,      /* The expression to code when the VDBE initializes */
79476   int regDest,      /* Store the value in this register */
79477   u8 reusable       /* True if this expression is reusable */
79478 ){
79479   ExprList *p;
79480   assert( ConstFactorOk(pParse) );
79481   p = pParse->pConstExpr;
79482   pExpr = sqlite3ExprDup(pParse->db, pExpr, 0);
79483   p = sqlite3ExprListAppend(pParse, p, pExpr);
79484   if( p ){
79485      struct ExprList_item *pItem = &p->a[p->nExpr-1];
79486      pItem->u.iConstExprReg = regDest;
79487      pItem->reusable = reusable;
79488   }
79489   pParse->pConstExpr = p;
79490 }
79491 
79492 /*
79493 ** Generate code to evaluate an expression and store the results
79494 ** into a register.  Return the register number where the results
79495 ** are stored.
79496 **
79497 ** If the register is a temporary register that can be deallocated,
79498 ** then write its number into *pReg.  If the result register is not
79499 ** a temporary, then set *pReg to zero.
79500 **
79501 ** If pExpr is a constant, then this routine might generate this
79502 ** code to fill the register in the initialization section of the
79503 ** VDBE program, in order to factor it out of the evaluation loop.
79504 */
79505 SQLITE_PRIVATE int sqlite3ExprCodeTemp(Parse *pParse, Expr *pExpr, int *pReg){
79506   int r2;
79507   pExpr = sqlite3ExprSkipCollate(pExpr);
79508   if( ConstFactorOk(pParse)
79509    && pExpr->op!=TK_REGISTER
79510    && sqlite3ExprIsConstantNotJoin(pExpr)
79511   ){
79512     ExprList *p = pParse->pConstExpr;
79513     int i;
79514     *pReg  = 0;
79515     if( p ){
79516       struct ExprList_item *pItem;
79517       for(pItem=p->a, i=p->nExpr; i>0; pItem++, i--){
79518         if( pItem->reusable && sqlite3ExprCompare(pItem->pExpr,pExpr,-1)==0 ){
79519           return pItem->u.iConstExprReg;
79520         }
79521       }
79522     }
79523     r2 = ++pParse->nMem;
79524     sqlite3ExprCodeAtInit(pParse, pExpr, r2, 1);
79525   }else{
79526     int r1 = sqlite3GetTempReg(pParse);
79527     r2 = sqlite3ExprCodeTarget(pParse, pExpr, r1);
79528     if( r2==r1 ){
79529       *pReg = r1;
79530     }else{
79531       sqlite3ReleaseTempReg(pParse, r1);
79532       *pReg = 0;
79533     }
79534   }
79535   return r2;
79536 }
79537 
79538 /*
79539 ** Generate code that will evaluate expression pExpr and store the
79540 ** results in register target.  The results are guaranteed to appear
79541 ** in register target.
79542 */
79543 SQLITE_PRIVATE int sqlite3ExprCode(Parse *pParse, Expr *pExpr, int target){
79544   int inReg;
79545 
79546   assert( target>0 && target<=pParse->nMem );
79547   if( pExpr && pExpr->op==TK_REGISTER ){
79548     sqlite3VdbeAddOp2(pParse->pVdbe, OP_Copy, pExpr->iTable, target);
79549   }else{
79550     inReg = sqlite3ExprCodeTarget(pParse, pExpr, target);
79551     assert( pParse->pVdbe || pParse->db->mallocFailed );
79552     if( inReg!=target && pParse->pVdbe ){
79553       sqlite3VdbeAddOp2(pParse->pVdbe, OP_SCopy, inReg, target);
79554     }
79555   }
79556   return target;
79557 }
79558 
79559 /*
79560 ** Generate code that evalutes the given expression and puts the result
79561 ** in register target.
79562 **
79563 ** Also make a copy of the expression results into another "cache" register
79564 ** and modify the expression so that the next time it is evaluated,
79565 ** the result is a copy of the cache register.
79566 **
79567 ** This routine is used for expressions that are used multiple
79568 ** times.  They are evaluated once and the results of the expression
79569 ** are reused.
79570 */
79571 SQLITE_PRIVATE int sqlite3ExprCodeAndCache(Parse *pParse, Expr *pExpr, int target){
79572   Vdbe *v = pParse->pVdbe;
79573   int inReg;
79574   inReg = sqlite3ExprCode(pParse, pExpr, target);
79575   assert( target>0 );
79576   /* The only place, other than this routine, where expressions can be
79577   ** converted to TK_REGISTER is internal subexpressions in BETWEEN and
79578   ** CASE operators.  Neither ever calls this routine.  And this routine
79579   ** is never called twice on the same expression.  Hence it is impossible
79580   ** for the input to this routine to already be a register.  Nevertheless,
79581   ** it seems prudent to keep the ALWAYS() in case the conditions above
79582   ** change with future modifications or enhancements. */
79583   if( ALWAYS(pExpr->op!=TK_REGISTER) ){
79584     int iMem;
79585     iMem = ++pParse->nMem;
79586     sqlite3VdbeAddOp2(v, OP_Copy, inReg, iMem);
79587     exprToRegister(pExpr, iMem);
79588   }
79589   return inReg;
79590 }
79591 
79592 #if defined(SQLITE_ENABLE_TREE_EXPLAIN)
79593 /*
79594 ** Generate a human-readable explanation of an expression tree.
79595 */
79596 SQLITE_PRIVATE void sqlite3ExplainExpr(Vdbe *pOut, Expr *pExpr){
79597   int op;                   /* The opcode being coded */
79598   const char *zBinOp = 0;   /* Binary operator */
79599   const char *zUniOp = 0;   /* Unary operator */
79600   if( pExpr==0 ){
79601     op = TK_NULL;
79602   }else{
79603     op = pExpr->op;
79604   }
79605   switch( op ){
79606     case TK_AGG_COLUMN: {
79607       sqlite3ExplainPrintf(pOut, "AGG{%d:%d}",
79608             pExpr->iTable, pExpr->iColumn);
79609       break;
79610     }
79611     case TK_COLUMN: {
79612       if( pExpr->iTable<0 ){
79613         /* This only happens when coding check constraints */
79614         sqlite3ExplainPrintf(pOut, "COLUMN(%d)", pExpr->iColumn);
79615       }else{
79616         sqlite3ExplainPrintf(pOut, "{%d:%d}",
79617                              pExpr->iTable, pExpr->iColumn);
79618       }
79619       break;
79620     }
79621     case TK_INTEGER: {
79622       if( pExpr->flags & EP_IntValue ){
79623         sqlite3ExplainPrintf(pOut, "%d", pExpr->u.iValue);
79624       }else{
79625         sqlite3ExplainPrintf(pOut, "%s", pExpr->u.zToken);
79626       }
79627       break;
79628     }
79629 #ifndef SQLITE_OMIT_FLOATING_POINT
79630     case TK_FLOAT: {
79631       sqlite3ExplainPrintf(pOut,"%s", pExpr->u.zToken);
79632       break;
79633     }
79634 #endif
79635     case TK_STRING: {
79636       sqlite3ExplainPrintf(pOut,"%Q", pExpr->u.zToken);
79637       break;
79638     }
79639     case TK_NULL: {
79640       sqlite3ExplainPrintf(pOut,"NULL");
79641       break;
79642     }
79643 #ifndef SQLITE_OMIT_BLOB_LITERAL
79644     case TK_BLOB: {
79645       sqlite3ExplainPrintf(pOut,"%s", pExpr->u.zToken);
79646       break;
79647     }
79648 #endif
79649     case TK_VARIABLE: {
79650       sqlite3ExplainPrintf(pOut,"VARIABLE(%s,%d)",
79651                            pExpr->u.zToken, pExpr->iColumn);
79652       break;
79653     }
79654     case TK_REGISTER: {
79655       sqlite3ExplainPrintf(pOut,"REGISTER(%d)", pExpr->iTable);
79656       break;
79657     }
79658     case TK_AS: {
79659       sqlite3ExplainExpr(pOut, pExpr->pLeft);
79660       break;
79661     }
79662 #ifndef SQLITE_OMIT_CAST
79663     case TK_CAST: {
79664       /* Expressions of the form:   CAST(pLeft AS token) */
79665       const char *zAff = "unk";
79666       switch( sqlite3AffinityType(pExpr->u.zToken, 0) ){
79667         case SQLITE_AFF_TEXT:    zAff = "TEXT";     break;
79668         case SQLITE_AFF_NONE:    zAff = "NONE";     break;
79669         case SQLITE_AFF_NUMERIC: zAff = "NUMERIC";  break;
79670         case SQLITE_AFF_INTEGER: zAff = "INTEGER";  break;
79671         case SQLITE_AFF_REAL:    zAff = "REAL";     break;
79672       }
79673       sqlite3ExplainPrintf(pOut, "CAST-%s(", zAff);
79674       sqlite3ExplainExpr(pOut, pExpr->pLeft);
79675       sqlite3ExplainPrintf(pOut, ")");
79676       break;
79677     }
79678 #endif /* SQLITE_OMIT_CAST */
79679     case TK_LT:      zBinOp = "LT";     break;
79680     case TK_LE:      zBinOp = "LE";     break;
79681     case TK_GT:      zBinOp = "GT";     break;
79682     case TK_GE:      zBinOp = "GE";     break;
79683     case TK_NE:      zBinOp = "NE";     break;
79684     case TK_EQ:      zBinOp = "EQ";     break;
79685     case TK_IS:      zBinOp = "IS";     break;
79686     case TK_ISNOT:   zBinOp = "ISNOT";  break;
79687     case TK_AND:     zBinOp = "AND";    break;
79688     case TK_OR:      zBinOp = "OR";     break;
79689     case TK_PLUS:    zBinOp = "ADD";    break;
79690     case TK_STAR:    zBinOp = "MUL";    break;
79691     case TK_MINUS:   zBinOp = "SUB";    break;
79692     case TK_REM:     zBinOp = "REM";    break;
79693     case TK_BITAND:  zBinOp = "BITAND"; break;
79694     case TK_BITOR:   zBinOp = "BITOR";  break;
79695     case TK_SLASH:   zBinOp = "DIV";    break;
79696     case TK_LSHIFT:  zBinOp = "LSHIFT"; break;
79697     case TK_RSHIFT:  zBinOp = "RSHIFT"; break;
79698     case TK_CONCAT:  zBinOp = "CONCAT"; break;
79699 
79700     case TK_UMINUS:  zUniOp = "UMINUS"; break;
79701     case TK_UPLUS:   zUniOp = "UPLUS";  break;
79702     case TK_BITNOT:  zUniOp = "BITNOT"; break;
79703     case TK_NOT:     zUniOp = "NOT";    break;
79704     case TK_ISNULL:  zUniOp = "ISNULL"; break;
79705     case TK_NOTNULL: zUniOp = "NOTNULL"; break;
79706 
79707     case TK_COLLATE: {
79708       sqlite3ExplainExpr(pOut, pExpr->pLeft);
79709       sqlite3ExplainPrintf(pOut,".COLLATE(%s)",pExpr->u.zToken);
79710       break;
79711     }
79712 
79713     case TK_AGG_FUNCTION:
79714     case TK_FUNCTION: {
79715       ExprList *pFarg;       /* List of function arguments */
79716       if( ExprHasProperty(pExpr, EP_TokenOnly) ){
79717         pFarg = 0;
79718       }else{
79719         pFarg = pExpr->x.pList;
79720       }
79721       if( op==TK_AGG_FUNCTION ){
79722         sqlite3ExplainPrintf(pOut, "AGG_FUNCTION%d:%s(",
79723                              pExpr->op2, pExpr->u.zToken);
79724       }else{
79725         sqlite3ExplainPrintf(pOut, "FUNCTION:%s(", pExpr->u.zToken);
79726       }
79727       if( pFarg ){
79728         sqlite3ExplainExprList(pOut, pFarg);
79729       }
79730       sqlite3ExplainPrintf(pOut, ")");
79731       break;
79732     }
79733 #ifndef SQLITE_OMIT_SUBQUERY
79734     case TK_EXISTS: {
79735       sqlite3ExplainPrintf(pOut, "EXISTS(");
79736       sqlite3ExplainSelect(pOut, pExpr->x.pSelect);
79737       sqlite3ExplainPrintf(pOut,")");
79738       break;
79739     }
79740     case TK_SELECT: {
79741       sqlite3ExplainPrintf(pOut, "(");
79742       sqlite3ExplainSelect(pOut, pExpr->x.pSelect);
79743       sqlite3ExplainPrintf(pOut, ")");
79744       break;
79745     }
79746     case TK_IN: {
79747       sqlite3ExplainPrintf(pOut, "IN(");
79748       sqlite3ExplainExpr(pOut, pExpr->pLeft);
79749       sqlite3ExplainPrintf(pOut, ",");
79750       if( ExprHasProperty(pExpr, EP_xIsSelect) ){
79751         sqlite3ExplainSelect(pOut, pExpr->x.pSelect);
79752       }else{
79753         sqlite3ExplainExprList(pOut, pExpr->x.pList);
79754       }
79755       sqlite3ExplainPrintf(pOut, ")");
79756       break;
79757     }
79758 #endif /* SQLITE_OMIT_SUBQUERY */
79759 
79760     /*
79761     **    x BETWEEN y AND z
79762     **
79763     ** This is equivalent to
79764     **
79765     **    x>=y AND x<=z
79766     **
79767     ** X is stored in pExpr->pLeft.
79768     ** Y is stored in pExpr->pList->a[0].pExpr.
79769     ** Z is stored in pExpr->pList->a[1].pExpr.
79770     */
79771     case TK_BETWEEN: {
79772       Expr *pX = pExpr->pLeft;
79773       Expr *pY = pExpr->x.pList->a[0].pExpr;
79774       Expr *pZ = pExpr->x.pList->a[1].pExpr;
79775       sqlite3ExplainPrintf(pOut, "BETWEEN(");
79776       sqlite3ExplainExpr(pOut, pX);
79777       sqlite3ExplainPrintf(pOut, ",");
79778       sqlite3ExplainExpr(pOut, pY);
79779       sqlite3ExplainPrintf(pOut, ",");
79780       sqlite3ExplainExpr(pOut, pZ);
79781       sqlite3ExplainPrintf(pOut, ")");
79782       break;
79783     }
79784     case TK_TRIGGER: {
79785       /* If the opcode is TK_TRIGGER, then the expression is a reference
79786       ** to a column in the new.* or old.* pseudo-tables available to
79787       ** trigger programs. In this case Expr.iTable is set to 1 for the
79788       ** new.* pseudo-table, or 0 for the old.* pseudo-table. Expr.iColumn
79789       ** is set to the column of the pseudo-table to read, or to -1 to
79790       ** read the rowid field.
79791       */
79792       sqlite3ExplainPrintf(pOut, "%s(%d)",
79793           pExpr->iTable ? "NEW" : "OLD", pExpr->iColumn);
79794       break;
79795     }
79796     case TK_CASE: {
79797       sqlite3ExplainPrintf(pOut, "CASE(");
79798       sqlite3ExplainExpr(pOut, pExpr->pLeft);
79799       sqlite3ExplainPrintf(pOut, ",");
79800       sqlite3ExplainExprList(pOut, pExpr->x.pList);
79801       break;
79802     }
79803 #ifndef SQLITE_OMIT_TRIGGER
79804     case TK_RAISE: {
79805       const char *zType = "unk";
79806       switch( pExpr->affinity ){
79807         case OE_Rollback:   zType = "rollback";  break;
79808         case OE_Abort:      zType = "abort";     break;
79809         case OE_Fail:       zType = "fail";      break;
79810         case OE_Ignore:     zType = "ignore";    break;
79811       }
79812       sqlite3ExplainPrintf(pOut, "RAISE-%s(%s)", zType, pExpr->u.zToken);
79813       break;
79814     }
79815 #endif
79816   }
79817   if( zBinOp ){
79818     sqlite3ExplainPrintf(pOut,"%s(", zBinOp);
79819     sqlite3ExplainExpr(pOut, pExpr->pLeft);
79820     sqlite3ExplainPrintf(pOut,",");
79821     sqlite3ExplainExpr(pOut, pExpr->pRight);
79822     sqlite3ExplainPrintf(pOut,")");
79823   }else if( zUniOp ){
79824     sqlite3ExplainPrintf(pOut,"%s(", zUniOp);
79825     sqlite3ExplainExpr(pOut, pExpr->pLeft);
79826     sqlite3ExplainPrintf(pOut,")");
79827   }
79828 }
79829 #endif /* defined(SQLITE_ENABLE_TREE_EXPLAIN) */
79830 
79831 #if defined(SQLITE_ENABLE_TREE_EXPLAIN)
79832 /*
79833 ** Generate a human-readable explanation of an expression list.
79834 */
79835 SQLITE_PRIVATE void sqlite3ExplainExprList(Vdbe *pOut, ExprList *pList){
79836   int i;
79837   if( pList==0 || pList->nExpr==0 ){
79838     sqlite3ExplainPrintf(pOut, "(empty-list)");
79839     return;
79840   }else if( pList->nExpr==1 ){
79841     sqlite3ExplainExpr(pOut, pList->a[0].pExpr);
79842   }else{
79843     sqlite3ExplainPush(pOut);
79844     for(i=0; i<pList->nExpr; i++){
79845       sqlite3ExplainPrintf(pOut, "item[%d] = ", i);
79846       sqlite3ExplainPush(pOut);
79847       sqlite3ExplainExpr(pOut, pList->a[i].pExpr);
79848       sqlite3ExplainPop(pOut);
79849       if( pList->a[i].zName ){
79850         sqlite3ExplainPrintf(pOut, " AS %s", pList->a[i].zName);
79851       }
79852       if( pList->a[i].bSpanIsTab ){
79853         sqlite3ExplainPrintf(pOut, " (%s)", pList->a[i].zSpan);
79854       }
79855       if( i<pList->nExpr-1 ){
79856         sqlite3ExplainNL(pOut);
79857       }
79858     }
79859     sqlite3ExplainPop(pOut);
79860   }
79861 }
79862 #endif /* SQLITE_DEBUG */
79863 
79864 /*
79865 ** Generate code that pushes the value of every element of the given
79866 ** expression list into a sequence of registers beginning at target.
79867 **
79868 ** Return the number of elements evaluated.
79869 **
79870 ** The SQLITE_ECEL_DUP flag prevents the arguments from being
79871 ** filled using OP_SCopy.  OP_Copy must be used instead.
79872 **
79873 ** The SQLITE_ECEL_FACTOR argument allows constant arguments to be
79874 ** factored out into initialization code.
79875 */
79876 SQLITE_PRIVATE int sqlite3ExprCodeExprList(
79877   Parse *pParse,     /* Parsing context */
79878   ExprList *pList,   /* The expression list to be coded */
79879   int target,        /* Where to write results */
79880   u8 flags           /* SQLITE_ECEL_* flags */
79881 ){
79882   struct ExprList_item *pItem;
79883   int i, n;
79884   u8 copyOp = (flags & SQLITE_ECEL_DUP) ? OP_Copy : OP_SCopy;
79885   assert( pList!=0 );
79886   assert( target>0 );
79887   assert( pParse->pVdbe!=0 );  /* Never gets this far otherwise */
79888   n = pList->nExpr;
79889   if( !ConstFactorOk(pParse) ) flags &= ~SQLITE_ECEL_FACTOR;
79890   for(pItem=pList->a, i=0; i<n; i++, pItem++){
79891     Expr *pExpr = pItem->pExpr;
79892     if( (flags & SQLITE_ECEL_FACTOR)!=0 && sqlite3ExprIsConstant(pExpr) ){
79893       sqlite3ExprCodeAtInit(pParse, pExpr, target+i, 0);
79894     }else{
79895       int inReg = sqlite3ExprCodeTarget(pParse, pExpr, target+i);
79896       if( inReg!=target+i ){
79897         VdbeOp *pOp;
79898         Vdbe *v = pParse->pVdbe;
79899         if( copyOp==OP_Copy
79900          && (pOp=sqlite3VdbeGetOp(v, -1))->opcode==OP_Copy
79901          && pOp->p1+pOp->p3+1==inReg
79902          && pOp->p2+pOp->p3+1==target+i
79903         ){
79904           pOp->p3++;
79905         }else{
79906           sqlite3VdbeAddOp2(v, copyOp, inReg, target+i);
79907         }
79908       }
79909     }
79910   }
79911   return n;
79912 }
79913 
79914 /*
79915 ** Generate code for a BETWEEN operator.
79916 **
79917 **    x BETWEEN y AND z
79918 **
79919 ** The above is equivalent to
79920 **
79921 **    x>=y AND x<=z
79922 **
79923 ** Code it as such, taking care to do the common subexpression
79924 ** elementation of x.
79925 */
79926 static void exprCodeBetween(
79927   Parse *pParse,    /* Parsing and code generating context */
79928   Expr *pExpr,      /* The BETWEEN expression */
79929   int dest,         /* Jump here if the jump is taken */
79930   int jumpIfTrue,   /* Take the jump if the BETWEEN is true */
79931   int jumpIfNull    /* Take the jump if the BETWEEN is NULL */
79932 ){
79933   Expr exprAnd;     /* The AND operator in  x>=y AND x<=z  */
79934   Expr compLeft;    /* The  x>=y  term */
79935   Expr compRight;   /* The  x<=z  term */
79936   Expr exprX;       /* The  x  subexpression */
79937   int regFree1 = 0; /* Temporary use register */
79938 
79939   assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
79940   exprX = *pExpr->pLeft;
79941   exprAnd.op = TK_AND;
79942   exprAnd.pLeft = &compLeft;
79943   exprAnd.pRight = &compRight;
79944   compLeft.op = TK_GE;
79945   compLeft.pLeft = &exprX;
79946   compLeft.pRight = pExpr->x.pList->a[0].pExpr;
79947   compRight.op = TK_LE;
79948   compRight.pLeft = &exprX;
79949   compRight.pRight = pExpr->x.pList->a[1].pExpr;
79950   exprToRegister(&exprX, sqlite3ExprCodeTemp(pParse, &exprX, &regFree1));
79951   if( jumpIfTrue ){
79952     sqlite3ExprIfTrue(pParse, &exprAnd, dest, jumpIfNull);
79953   }else{
79954     sqlite3ExprIfFalse(pParse, &exprAnd, dest, jumpIfNull);
79955   }
79956   sqlite3ReleaseTempReg(pParse, regFree1);
79957 
79958   /* Ensure adequate test coverage */
79959   testcase( jumpIfTrue==0 && jumpIfNull==0 && regFree1==0 );
79960   testcase( jumpIfTrue==0 && jumpIfNull==0 && regFree1!=0 );
79961   testcase( jumpIfTrue==0 && jumpIfNull!=0 && regFree1==0 );
79962   testcase( jumpIfTrue==0 && jumpIfNull!=0 && regFree1!=0 );
79963   testcase( jumpIfTrue!=0 && jumpIfNull==0 && regFree1==0 );
79964   testcase( jumpIfTrue!=0 && jumpIfNull==0 && regFree1!=0 );
79965   testcase( jumpIfTrue!=0 && jumpIfNull!=0 && regFree1==0 );
79966   testcase( jumpIfTrue!=0 && jumpIfNull!=0 && regFree1!=0 );
79967 }
79968 
79969 /*
79970 ** Generate code for a boolean expression such that a jump is made
79971 ** to the label "dest" if the expression is true but execution
79972 ** continues straight thru if the expression is false.
79973 **
79974 ** If the expression evaluates to NULL (neither true nor false), then
79975 ** take the jump if the jumpIfNull flag is SQLITE_JUMPIFNULL.
79976 **
79977 ** This code depends on the fact that certain token values (ex: TK_EQ)
79978 ** are the same as opcode values (ex: OP_Eq) that implement the corresponding
79979 ** operation.  Special comments in vdbe.c and the mkopcodeh.awk script in
79980 ** the make process cause these values to align.  Assert()s in the code
79981 ** below verify that the numbers are aligned correctly.
79982 */
79983 SQLITE_PRIVATE void sqlite3ExprIfTrue(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
79984   Vdbe *v = pParse->pVdbe;
79985   int op = 0;
79986   int regFree1 = 0;
79987   int regFree2 = 0;
79988   int r1, r2;
79989 
79990   assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 );
79991   if( NEVER(v==0) )     return;  /* Existence of VDBE checked by caller */
79992   if( NEVER(pExpr==0) ) return;  /* No way this can happen */
79993   op = pExpr->op;
79994   switch( op ){
79995     case TK_AND: {
79996       int d2 = sqlite3VdbeMakeLabel(v);
79997       testcase( jumpIfNull==0 );
79998       sqlite3ExprIfFalse(pParse, pExpr->pLeft, d2,jumpIfNull^SQLITE_JUMPIFNULL);
79999       sqlite3ExprCachePush(pParse);
80000       sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
80001       sqlite3VdbeResolveLabel(v, d2);
80002       sqlite3ExprCachePop(pParse, 1);
80003       break;
80004     }
80005     case TK_OR: {
80006       testcase( jumpIfNull==0 );
80007       sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
80008       sqlite3ExprCachePush(pParse);
80009       sqlite3ExprIfTrue(pParse, pExpr->pRight, dest, jumpIfNull);
80010       sqlite3ExprCachePop(pParse, 1);
80011       break;
80012     }
80013     case TK_NOT: {
80014       testcase( jumpIfNull==0 );
80015       sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
80016       break;
80017     }
80018     case TK_LT:
80019     case TK_LE:
80020     case TK_GT:
80021     case TK_GE:
80022     case TK_NE:
80023     case TK_EQ: {
80024       assert( TK_LT==OP_Lt );
80025       assert( TK_LE==OP_Le );
80026       assert( TK_GT==OP_Gt );
80027       assert( TK_GE==OP_Ge );
80028       assert( TK_EQ==OP_Eq );
80029       assert( TK_NE==OP_Ne );
80030       testcase( op==TK_LT );
80031       testcase( op==TK_LE );
80032       testcase( op==TK_GT );
80033       testcase( op==TK_GE );
80034       testcase( op==TK_EQ );
80035       testcase( op==TK_NE );
80036       testcase( jumpIfNull==0 );
80037       r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
80038       r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
80039       codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
80040                   r1, r2, dest, jumpIfNull);
80041       testcase( regFree1==0 );
80042       testcase( regFree2==0 );
80043       break;
80044     }
80045     case TK_IS:
80046     case TK_ISNOT: {
80047       testcase( op==TK_IS );
80048       testcase( op==TK_ISNOT );
80049       r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
80050       r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
80051       op = (op==TK_IS) ? TK_EQ : TK_NE;
80052       codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
80053                   r1, r2, dest, SQLITE_NULLEQ);
80054       testcase( regFree1==0 );
80055       testcase( regFree2==0 );
80056       break;
80057     }
80058     case TK_ISNULL:
80059     case TK_NOTNULL: {
80060       assert( TK_ISNULL==OP_IsNull );
80061       assert( TK_NOTNULL==OP_NotNull );
80062       testcase( op==TK_ISNULL );
80063       testcase( op==TK_NOTNULL );
80064       r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
80065       sqlite3VdbeAddOp2(v, op, r1, dest);
80066       testcase( regFree1==0 );
80067       break;
80068     }
80069     case TK_BETWEEN: {
80070       testcase( jumpIfNull==0 );
80071       exprCodeBetween(pParse, pExpr, dest, 1, jumpIfNull);
80072       break;
80073     }
80074 #ifndef SQLITE_OMIT_SUBQUERY
80075     case TK_IN: {
80076       int destIfFalse = sqlite3VdbeMakeLabel(v);
80077       int destIfNull = jumpIfNull ? dest : destIfFalse;
80078       sqlite3ExprCodeIN(pParse, pExpr, destIfFalse, destIfNull);
80079       sqlite3VdbeAddOp2(v, OP_Goto, 0, dest);
80080       sqlite3VdbeResolveLabel(v, destIfFalse);
80081       break;
80082     }
80083 #endif
80084     default: {
80085       if( exprAlwaysTrue(pExpr) ){
80086         sqlite3VdbeAddOp2(v, OP_Goto, 0, dest);
80087       }else if( exprAlwaysFalse(pExpr) ){
80088         /* No-op */
80089       }else{
80090         r1 = sqlite3ExprCodeTemp(pParse, pExpr, &regFree1);
80091         sqlite3VdbeAddOp3(v, OP_If, r1, dest, jumpIfNull!=0);
80092         testcase( regFree1==0 );
80093         testcase( jumpIfNull==0 );
80094       }
80095       break;
80096     }
80097   }
80098   sqlite3ReleaseTempReg(pParse, regFree1);
80099   sqlite3ReleaseTempReg(pParse, regFree2);
80100 }
80101 
80102 /*
80103 ** Generate code for a boolean expression such that a jump is made
80104 ** to the label "dest" if the expression is false but execution
80105 ** continues straight thru if the expression is true.
80106 **
80107 ** If the expression evaluates to NULL (neither true nor false) then
80108 ** jump if jumpIfNull is SQLITE_JUMPIFNULL or fall through if jumpIfNull
80109 ** is 0.
80110 */
80111 SQLITE_PRIVATE void sqlite3ExprIfFalse(Parse *pParse, Expr *pExpr, int dest, int jumpIfNull){
80112   Vdbe *v = pParse->pVdbe;
80113   int op = 0;
80114   int regFree1 = 0;
80115   int regFree2 = 0;
80116   int r1, r2;
80117 
80118   assert( jumpIfNull==SQLITE_JUMPIFNULL || jumpIfNull==0 );
80119   if( NEVER(v==0) ) return; /* Existence of VDBE checked by caller */
80120   if( pExpr==0 )    return;
80121 
80122   /* The value of pExpr->op and op are related as follows:
80123   **
80124   **       pExpr->op            op
80125   **       ---------          ----------
80126   **       TK_ISNULL          OP_NotNull
80127   **       TK_NOTNULL         OP_IsNull
80128   **       TK_NE              OP_Eq
80129   **       TK_EQ              OP_Ne
80130   **       TK_GT              OP_Le
80131   **       TK_LE              OP_Gt
80132   **       TK_GE              OP_Lt
80133   **       TK_LT              OP_Ge
80134   **
80135   ** For other values of pExpr->op, op is undefined and unused.
80136   ** The value of TK_ and OP_ constants are arranged such that we
80137   ** can compute the mapping above using the following expression.
80138   ** Assert()s verify that the computation is correct.
80139   */
80140   op = ((pExpr->op+(TK_ISNULL&1))^1)-(TK_ISNULL&1);
80141 
80142   /* Verify correct alignment of TK_ and OP_ constants
80143   */
80144   assert( pExpr->op!=TK_ISNULL || op==OP_NotNull );
80145   assert( pExpr->op!=TK_NOTNULL || op==OP_IsNull );
80146   assert( pExpr->op!=TK_NE || op==OP_Eq );
80147   assert( pExpr->op!=TK_EQ || op==OP_Ne );
80148   assert( pExpr->op!=TK_LT || op==OP_Ge );
80149   assert( pExpr->op!=TK_LE || op==OP_Gt );
80150   assert( pExpr->op!=TK_GT || op==OP_Le );
80151   assert( pExpr->op!=TK_GE || op==OP_Lt );
80152 
80153   switch( pExpr->op ){
80154     case TK_AND: {
80155       testcase( jumpIfNull==0 );
80156       sqlite3ExprIfFalse(pParse, pExpr->pLeft, dest, jumpIfNull);
80157       sqlite3ExprCachePush(pParse);
80158       sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
80159       sqlite3ExprCachePop(pParse, 1);
80160       break;
80161     }
80162     case TK_OR: {
80163       int d2 = sqlite3VdbeMakeLabel(v);
80164       testcase( jumpIfNull==0 );
80165       sqlite3ExprIfTrue(pParse, pExpr->pLeft, d2, jumpIfNull^SQLITE_JUMPIFNULL);
80166       sqlite3ExprCachePush(pParse);
80167       sqlite3ExprIfFalse(pParse, pExpr->pRight, dest, jumpIfNull);
80168       sqlite3VdbeResolveLabel(v, d2);
80169       sqlite3ExprCachePop(pParse, 1);
80170       break;
80171     }
80172     case TK_NOT: {
80173       testcase( jumpIfNull==0 );
80174       sqlite3ExprIfTrue(pParse, pExpr->pLeft, dest, jumpIfNull);
80175       break;
80176     }
80177     case TK_LT:
80178     case TK_LE:
80179     case TK_GT:
80180     case TK_GE:
80181     case TK_NE:
80182     case TK_EQ: {
80183       testcase( op==TK_LT );
80184       testcase( op==TK_LE );
80185       testcase( op==TK_GT );
80186       testcase( op==TK_GE );
80187       testcase( op==TK_EQ );
80188       testcase( op==TK_NE );
80189       testcase( jumpIfNull==0 );
80190       r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
80191       r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
80192       codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
80193                   r1, r2, dest, jumpIfNull);
80194       testcase( regFree1==0 );
80195       testcase( regFree2==0 );
80196       break;
80197     }
80198     case TK_IS:
80199     case TK_ISNOT: {
80200       testcase( pExpr->op==TK_IS );
80201       testcase( pExpr->op==TK_ISNOT );
80202       r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
80203       r2 = sqlite3ExprCodeTemp(pParse, pExpr->pRight, &regFree2);
80204       op = (pExpr->op==TK_IS) ? TK_NE : TK_EQ;
80205       codeCompare(pParse, pExpr->pLeft, pExpr->pRight, op,
80206                   r1, r2, dest, SQLITE_NULLEQ);
80207       testcase( regFree1==0 );
80208       testcase( regFree2==0 );
80209       break;
80210     }
80211     case TK_ISNULL:
80212     case TK_NOTNULL: {
80213       testcase( op==TK_ISNULL );
80214       testcase( op==TK_NOTNULL );
80215       r1 = sqlite3ExprCodeTemp(pParse, pExpr->pLeft, &regFree1);
80216       sqlite3VdbeAddOp2(v, op, r1, dest);
80217       testcase( regFree1==0 );
80218       break;
80219     }
80220     case TK_BETWEEN: {
80221       testcase( jumpIfNull==0 );
80222       exprCodeBetween(pParse, pExpr, dest, 0, jumpIfNull);
80223       break;
80224     }
80225 #ifndef SQLITE_OMIT_SUBQUERY
80226     case TK_IN: {
80227       if( jumpIfNull ){
80228         sqlite3ExprCodeIN(pParse, pExpr, dest, dest);
80229       }else{
80230         int destIfNull = sqlite3VdbeMakeLabel(v);
80231         sqlite3ExprCodeIN(pParse, pExpr, dest, destIfNull);
80232         sqlite3VdbeResolveLabel(v, destIfNull);
80233       }
80234       break;
80235     }
80236 #endif
80237     default: {
80238       if( exprAlwaysFalse(pExpr) ){
80239         sqlite3VdbeAddOp2(v, OP_Goto, 0, dest);
80240       }else if( exprAlwaysTrue(pExpr) ){
80241         /* no-op */
80242       }else{
80243         r1 = sqlite3ExprCodeTemp(pParse, pExpr, &regFree1);
80244         sqlite3VdbeAddOp3(v, OP_IfNot, r1, dest, jumpIfNull!=0);
80245         testcase( regFree1==0 );
80246         testcase( jumpIfNull==0 );
80247       }
80248       break;
80249     }
80250   }
80251   sqlite3ReleaseTempReg(pParse, regFree1);
80252   sqlite3ReleaseTempReg(pParse, regFree2);
80253 }
80254 
80255 /*
80256 ** Do a deep comparison of two expression trees.  Return 0 if the two
80257 ** expressions are completely identical.  Return 1 if they differ only
80258 ** by a COLLATE operator at the top level.  Return 2 if there are differences
80259 ** other than the top-level COLLATE operator.
80260 **
80261 ** If any subelement of pB has Expr.iTable==(-1) then it is allowed
80262 ** to compare equal to an equivalent element in pA with Expr.iTable==iTab.
80263 **
80264 ** The pA side might be using TK_REGISTER.  If that is the case and pB is
80265 ** not using TK_REGISTER but is otherwise equivalent, then still return 0.
80266 **
80267 ** Sometimes this routine will return 2 even if the two expressions
80268 ** really are equivalent.  If we cannot prove that the expressions are
80269 ** identical, we return 2 just to be safe.  So if this routine
80270 ** returns 2, then you do not really know for certain if the two
80271 ** expressions are the same.  But if you get a 0 or 1 return, then you
80272 ** can be sure the expressions are the same.  In the places where
80273 ** this routine is used, it does not hurt to get an extra 2 - that
80274 ** just might result in some slightly slower code.  But returning
80275 ** an incorrect 0 or 1 could lead to a malfunction.
80276 */
80277 SQLITE_PRIVATE int sqlite3ExprCompare(Expr *pA, Expr *pB, int iTab){
80278   u32 combinedFlags;
80279   if( pA==0 || pB==0 ){
80280     return pB==pA ? 0 : 2;
80281   }
80282   combinedFlags = pA->flags | pB->flags;
80283   if( combinedFlags & EP_IntValue ){
80284     if( (pA->flags&pB->flags&EP_IntValue)!=0 && pA->u.iValue==pB->u.iValue ){
80285       return 0;
80286     }
80287     return 2;
80288   }
80289   if( pA->op!=pB->op ){
80290     if( pA->op==TK_COLLATE && sqlite3ExprCompare(pA->pLeft, pB, iTab)<2 ){
80291       return 1;
80292     }
80293     if( pB->op==TK_COLLATE && sqlite3ExprCompare(pA, pB->pLeft, iTab)<2 ){
80294       return 1;
80295     }
80296     return 2;
80297   }
80298   if( pA->op!=TK_COLUMN && ALWAYS(pA->op!=TK_AGG_COLUMN) && pA->u.zToken ){
80299     if( strcmp(pA->u.zToken,pB->u.zToken)!=0 ){
80300       return pA->op==TK_COLLATE ? 1 : 2;
80301     }
80302   }
80303   if( (pA->flags & EP_Distinct)!=(pB->flags & EP_Distinct) ) return 2;
80304   if( ALWAYS((combinedFlags & EP_TokenOnly)==0) ){
80305     if( combinedFlags & EP_xIsSelect ) return 2;
80306     if( sqlite3ExprCompare(pA->pLeft, pB->pLeft, iTab) ) return 2;
80307     if( sqlite3ExprCompare(pA->pRight, pB->pRight, iTab) ) return 2;
80308     if( sqlite3ExprListCompare(pA->x.pList, pB->x.pList, iTab) ) return 2;
80309     if( ALWAYS((combinedFlags & EP_Reduced)==0) ){
80310       if( pA->iColumn!=pB->iColumn ) return 2;
80311       if( pA->iTable!=pB->iTable
80312        && (pA->iTable!=iTab || NEVER(pB->iTable>=0)) ) return 2;
80313     }
80314   }
80315   return 0;
80316 }
80317 
80318 /*
80319 ** Compare two ExprList objects.  Return 0 if they are identical and
80320 ** non-zero if they differ in any way.
80321 **
80322 ** If any subelement of pB has Expr.iTable==(-1) then it is allowed
80323 ** to compare equal to an equivalent element in pA with Expr.iTable==iTab.
80324 **
80325 ** This routine might return non-zero for equivalent ExprLists.  The
80326 ** only consequence will be disabled optimizations.  But this routine
80327 ** must never return 0 if the two ExprList objects are different, or
80328 ** a malfunction will result.
80329 **
80330 ** Two NULL pointers are considered to be the same.  But a NULL pointer
80331 ** always differs from a non-NULL pointer.
80332 */
80333 SQLITE_PRIVATE int sqlite3ExprListCompare(ExprList *pA, ExprList *pB, int iTab){
80334   int i;
80335   if( pA==0 && pB==0 ) return 0;
80336   if( pA==0 || pB==0 ) return 1;
80337   if( pA->nExpr!=pB->nExpr ) return 1;
80338   for(i=0; i<pA->nExpr; i++){
80339     Expr *pExprA = pA->a[i].pExpr;
80340     Expr *pExprB = pB->a[i].pExpr;
80341     if( pA->a[i].sortOrder!=pB->a[i].sortOrder ) return 1;
80342     if( sqlite3ExprCompare(pExprA, pExprB, iTab) ) return 1;
80343   }
80344   return 0;
80345 }
80346 
80347 /*
80348 ** Return true if we can prove the pE2 will always be true if pE1 is
80349 ** true.  Return false if we cannot complete the proof or if pE2 might
80350 ** be false.  Examples:
80351 **
80352 **     pE1: x==5       pE2: x==5             Result: true
80353 **     pE1: x>0        pE2: x==5             Result: false
80354 **     pE1: x=21       pE2: x=21 OR y=43     Result: true
80355 **     pE1: x!=123     pE2: x IS NOT NULL    Result: true
80356 **     pE1: x!=?1      pE2: x IS NOT NULL    Result: true
80357 **     pE1: x IS NULL  pE2: x IS NOT NULL    Result: false
80358 **     pE1: x IS ?2    pE2: x IS NOT NULL    Reuslt: false
80359 **
80360 ** When comparing TK_COLUMN nodes between pE1 and pE2, if pE2 has
80361 ** Expr.iTable<0 then assume a table number given by iTab.
80362 **
80363 ** When in doubt, return false.  Returning true might give a performance
80364 ** improvement.  Returning false might cause a performance reduction, but
80365 ** it will always give the correct answer and is hence always safe.
80366 */
80367 SQLITE_PRIVATE int sqlite3ExprImpliesExpr(Expr *pE1, Expr *pE2, int iTab){
80368   if( sqlite3ExprCompare(pE1, pE2, iTab)==0 ){
80369     return 1;
80370   }
80371   if( pE2->op==TK_OR
80372    && (sqlite3ExprImpliesExpr(pE1, pE2->pLeft, iTab)
80373              || sqlite3ExprImpliesExpr(pE1, pE2->pRight, iTab) )
80374   ){
80375     return 1;
80376   }
80377   if( pE2->op==TK_NOTNULL
80378    && sqlite3ExprCompare(pE1->pLeft, pE2->pLeft, iTab)==0
80379    && (pE1->op!=TK_ISNULL && pE1->op!=TK_IS)
80380   ){
80381     return 1;
80382   }
80383   return 0;
80384 }
80385 
80386 /*
80387 ** An instance of the following structure is used by the tree walker
80388 ** to count references to table columns in the arguments of an
80389 ** aggregate function, in order to implement the
80390 ** sqlite3FunctionThisSrc() routine.
80391 */
80392 struct SrcCount {
80393   SrcList *pSrc;   /* One particular FROM clause in a nested query */
80394   int nThis;       /* Number of references to columns in pSrcList */
80395   int nOther;      /* Number of references to columns in other FROM clauses */
80396 };
80397 
80398 /*
80399 ** Count the number of references to columns.
80400 */
80401 static int exprSrcCount(Walker *pWalker, Expr *pExpr){
80402   /* The NEVER() on the second term is because sqlite3FunctionUsesThisSrc()
80403   ** is always called before sqlite3ExprAnalyzeAggregates() and so the
80404   ** TK_COLUMNs have not yet been converted into TK_AGG_COLUMN.  If
80405   ** sqlite3FunctionUsesThisSrc() is used differently in the future, the
80406   ** NEVER() will need to be removed. */
80407   if( pExpr->op==TK_COLUMN || NEVER(pExpr->op==TK_AGG_COLUMN) ){
80408     int i;
80409     struct SrcCount *p = pWalker->u.pSrcCount;
80410     SrcList *pSrc = p->pSrc;
80411     for(i=0; i<pSrc->nSrc; i++){
80412       if( pExpr->iTable==pSrc->a[i].iCursor ) break;
80413     }
80414     if( i<pSrc->nSrc ){
80415       p->nThis++;
80416     }else{
80417       p->nOther++;
80418     }
80419   }
80420   return WRC_Continue;
80421 }
80422 
80423 /*
80424 ** Determine if any of the arguments to the pExpr Function reference
80425 ** pSrcList.  Return true if they do.  Also return true if the function
80426 ** has no arguments or has only constant arguments.  Return false if pExpr
80427 ** references columns but not columns of tables found in pSrcList.
80428 */
80429 SQLITE_PRIVATE int sqlite3FunctionUsesThisSrc(Expr *pExpr, SrcList *pSrcList){
80430   Walker w;
80431   struct SrcCount cnt;
80432   assert( pExpr->op==TK_AGG_FUNCTION );
80433   memset(&w, 0, sizeof(w));
80434   w.xExprCallback = exprSrcCount;
80435   w.u.pSrcCount = &cnt;
80436   cnt.pSrc = pSrcList;
80437   cnt.nThis = 0;
80438   cnt.nOther = 0;
80439   sqlite3WalkExprList(&w, pExpr->x.pList);
80440   return cnt.nThis>0 || cnt.nOther==0;
80441 }
80442 
80443 /*
80444 ** Add a new element to the pAggInfo->aCol[] array.  Return the index of
80445 ** the new element.  Return a negative number if malloc fails.
80446 */
80447 static int addAggInfoColumn(sqlite3 *db, AggInfo *pInfo){
80448   int i;
80449   pInfo->aCol = sqlite3ArrayAllocate(
80450        db,
80451        pInfo->aCol,
80452        sizeof(pInfo->aCol[0]),
80453        &pInfo->nColumn,
80454        &i
80455   );
80456   return i;
80457 }
80458 
80459 /*
80460 ** Add a new element to the pAggInfo->aFunc[] array.  Return the index of
80461 ** the new element.  Return a negative number if malloc fails.
80462 */
80463 static int addAggInfoFunc(sqlite3 *db, AggInfo *pInfo){
80464   int i;
80465   pInfo->aFunc = sqlite3ArrayAllocate(
80466        db,
80467        pInfo->aFunc,
80468        sizeof(pInfo->aFunc[0]),
80469        &pInfo->nFunc,
80470        &i
80471   );
80472   return i;
80473 }
80474 
80475 /*
80476 ** This is the xExprCallback for a tree walker.  It is used to
80477 ** implement sqlite3ExprAnalyzeAggregates().  See sqlite3ExprAnalyzeAggregates
80478 ** for additional information.
80479 */
80480 static int analyzeAggregate(Walker *pWalker, Expr *pExpr){
80481   int i;
80482   NameContext *pNC = pWalker->u.pNC;
80483   Parse *pParse = pNC->pParse;
80484   SrcList *pSrcList = pNC->pSrcList;
80485   AggInfo *pAggInfo = pNC->pAggInfo;
80486 
80487   switch( pExpr->op ){
80488     case TK_AGG_COLUMN:
80489     case TK_COLUMN: {
80490       testcase( pExpr->op==TK_AGG_COLUMN );
80491       testcase( pExpr->op==TK_COLUMN );
80492       /* Check to see if the column is in one of the tables in the FROM
80493       ** clause of the aggregate query */
80494       if( ALWAYS(pSrcList!=0) ){
80495         struct SrcList_item *pItem = pSrcList->a;
80496         for(i=0; i<pSrcList->nSrc; i++, pItem++){
80497           struct AggInfo_col *pCol;
80498           assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) );
80499           if( pExpr->iTable==pItem->iCursor ){
80500             /* If we reach this point, it means that pExpr refers to a table
80501             ** that is in the FROM clause of the aggregate query.
80502             **
80503             ** Make an entry for the column in pAggInfo->aCol[] if there
80504             ** is not an entry there already.
80505             */
80506             int k;
80507             pCol = pAggInfo->aCol;
80508             for(k=0; k<pAggInfo->nColumn; k++, pCol++){
80509               if( pCol->iTable==pExpr->iTable &&
80510                   pCol->iColumn==pExpr->iColumn ){
80511                 break;
80512               }
80513             }
80514             if( (k>=pAggInfo->nColumn)
80515              && (k = addAggInfoColumn(pParse->db, pAggInfo))>=0
80516             ){
80517               pCol = &pAggInfo->aCol[k];
80518               pCol->pTab = pExpr->pTab;
80519               pCol->iTable = pExpr->iTable;
80520               pCol->iColumn = pExpr->iColumn;
80521               pCol->iMem = ++pParse->nMem;
80522               pCol->iSorterColumn = -1;
80523               pCol->pExpr = pExpr;
80524               if( pAggInfo->pGroupBy ){
80525                 int j, n;
80526                 ExprList *pGB = pAggInfo->pGroupBy;
80527                 struct ExprList_item *pTerm = pGB->a;
80528                 n = pGB->nExpr;
80529                 for(j=0; j<n; j++, pTerm++){
80530                   Expr *pE = pTerm->pExpr;
80531                   if( pE->op==TK_COLUMN && pE->iTable==pExpr->iTable &&
80532                       pE->iColumn==pExpr->iColumn ){
80533                     pCol->iSorterColumn = j;
80534                     break;
80535                   }
80536                 }
80537               }
80538               if( pCol->iSorterColumn<0 ){
80539                 pCol->iSorterColumn = pAggInfo->nSortingColumn++;
80540               }
80541             }
80542             /* There is now an entry for pExpr in pAggInfo->aCol[] (either
80543             ** because it was there before or because we just created it).
80544             ** Convert the pExpr to be a TK_AGG_COLUMN referring to that
80545             ** pAggInfo->aCol[] entry.
80546             */
80547             ExprSetVVAProperty(pExpr, EP_NoReduce);
80548             pExpr->pAggInfo = pAggInfo;
80549             pExpr->op = TK_AGG_COLUMN;
80550             pExpr->iAgg = (i16)k;
80551             break;
80552           } /* endif pExpr->iTable==pItem->iCursor */
80553         } /* end loop over pSrcList */
80554       }
80555       return WRC_Prune;
80556     }
80557     case TK_AGG_FUNCTION: {
80558       if( (pNC->ncFlags & NC_InAggFunc)==0
80559        && pWalker->walkerDepth==pExpr->op2
80560       ){
80561         /* Check to see if pExpr is a duplicate of another aggregate
80562         ** function that is already in the pAggInfo structure
80563         */
80564         struct AggInfo_func *pItem = pAggInfo->aFunc;
80565         for(i=0; i<pAggInfo->nFunc; i++, pItem++){
80566           if( sqlite3ExprCompare(pItem->pExpr, pExpr, -1)==0 ){
80567             break;
80568           }
80569         }
80570         if( i>=pAggInfo->nFunc ){
80571           /* pExpr is original.  Make a new entry in pAggInfo->aFunc[]
80572           */
80573           u8 enc = ENC(pParse->db);
80574           i = addAggInfoFunc(pParse->db, pAggInfo);
80575           if( i>=0 ){
80576             assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
80577             pItem = &pAggInfo->aFunc[i];
80578             pItem->pExpr = pExpr;
80579             pItem->iMem = ++pParse->nMem;
80580             assert( !ExprHasProperty(pExpr, EP_IntValue) );
80581             pItem->pFunc = sqlite3FindFunction(pParse->db,
80582                    pExpr->u.zToken, sqlite3Strlen30(pExpr->u.zToken),
80583                    pExpr->x.pList ? pExpr->x.pList->nExpr : 0, enc, 0);
80584             if( pExpr->flags & EP_Distinct ){
80585               pItem->iDistinct = pParse->nTab++;
80586             }else{
80587               pItem->iDistinct = -1;
80588             }
80589           }
80590         }
80591         /* Make pExpr point to the appropriate pAggInfo->aFunc[] entry
80592         */
80593         assert( !ExprHasProperty(pExpr, EP_TokenOnly|EP_Reduced) );
80594         ExprSetVVAProperty(pExpr, EP_NoReduce);
80595         pExpr->iAgg = (i16)i;
80596         pExpr->pAggInfo = pAggInfo;
80597         return WRC_Prune;
80598       }else{
80599         return WRC_Continue;
80600       }
80601     }
80602   }
80603   return WRC_Continue;
80604 }
80605 static int analyzeAggregatesInSelect(Walker *pWalker, Select *pSelect){
80606   UNUSED_PARAMETER(pWalker);
80607   UNUSED_PARAMETER(pSelect);
80608   return WRC_Continue;
80609 }
80610 
80611 /*
80612 ** Analyze the pExpr expression looking for aggregate functions and
80613 ** for variables that need to be added to AggInfo object that pNC->pAggInfo
80614 ** points to.  Additional entries are made on the AggInfo object as
80615 ** necessary.
80616 **
80617 ** This routine should only be called after the expression has been
80618 ** analyzed by sqlite3ResolveExprNames().
80619 */
80620 SQLITE_PRIVATE void sqlite3ExprAnalyzeAggregates(NameContext *pNC, Expr *pExpr){
80621   Walker w;
80622   memset(&w, 0, sizeof(w));
80623   w.xExprCallback = analyzeAggregate;
80624   w.xSelectCallback = analyzeAggregatesInSelect;
80625   w.u.pNC = pNC;
80626   assert( pNC->pSrcList!=0 );
80627   sqlite3WalkExpr(&w, pExpr);
80628 }
80629 
80630 /*
80631 ** Call sqlite3ExprAnalyzeAggregates() for every expression in an
80632 ** expression list.  Return the number of errors.
80633 **
80634 ** If an error is found, the analysis is cut short.
80635 */
80636 SQLITE_PRIVATE void sqlite3ExprAnalyzeAggList(NameContext *pNC, ExprList *pList){
80637   struct ExprList_item *pItem;
80638   int i;
80639   if( pList ){
80640     for(pItem=pList->a, i=0; i<pList->nExpr; i++, pItem++){
80641       sqlite3ExprAnalyzeAggregates(pNC, pItem->pExpr);
80642     }
80643   }
80644 }
80645 
80646 /*
80647 ** Allocate a single new register for use to hold some intermediate result.
80648 */
80649 SQLITE_PRIVATE int sqlite3GetTempReg(Parse *pParse){
80650   if( pParse->nTempReg==0 ){
80651     return ++pParse->nMem;
80652   }
80653   return pParse->aTempReg[--pParse->nTempReg];
80654 }
80655 
80656 /*
80657 ** Deallocate a register, making available for reuse for some other
80658 ** purpose.
80659 **
80660 ** If a register is currently being used by the column cache, then
80661 ** the dallocation is deferred until the column cache line that uses
80662 ** the register becomes stale.
80663 */
80664 SQLITE_PRIVATE void sqlite3ReleaseTempReg(Parse *pParse, int iReg){
80665   if( iReg && pParse->nTempReg<ArraySize(pParse->aTempReg) ){
80666     int i;
80667     struct yColCache *p;
80668     for(i=0, p=pParse->aColCache; i<SQLITE_N_COLCACHE; i++, p++){
80669       if( p->iReg==iReg ){
80670         p->tempReg = 1;
80671         return;
80672       }
80673     }
80674     pParse->aTempReg[pParse->nTempReg++] = iReg;
80675   }
80676 }
80677 
80678 /*
80679 ** Allocate or deallocate a block of nReg consecutive registers
80680 */
80681 SQLITE_PRIVATE int sqlite3GetTempRange(Parse *pParse, int nReg){
80682   int i, n;
80683   i = pParse->iRangeReg;
80684   n = pParse->nRangeReg;
80685   if( nReg<=n ){
80686     assert( !usedAsColumnCache(pParse, i, i+n-1) );
80687     pParse->iRangeReg += nReg;
80688     pParse->nRangeReg -= nReg;
80689   }else{
80690     i = pParse->nMem+1;
80691     pParse->nMem += nReg;
80692   }
80693   return i;
80694 }
80695 SQLITE_PRIVATE void sqlite3ReleaseTempRange(Parse *pParse, int iReg, int nReg){
80696   sqlite3ExprCacheRemove(pParse, iReg, nReg);
80697   if( nReg>pParse->nRangeReg ){
80698     pParse->nRangeReg = nReg;
80699     pParse->iRangeReg = iReg;
80700   }
80701 }
80702 
80703 /*
80704 ** Mark all temporary registers as being unavailable for reuse.
80705 */
80706 SQLITE_PRIVATE void sqlite3ClearTempRegCache(Parse *pParse){
80707   pParse->nTempReg = 0;
80708   pParse->nRangeReg = 0;
80709 }
80710 
80711 /************** End of expr.c ************************************************/
80712 /************** Begin file alter.c *******************************************/
80713 /*
80714 ** 2005 February 15
80715 **
80716 ** The author disclaims copyright to this source code.  In place of
80717 ** a legal notice, here is a blessing:
80718 **
80719 **    May you do good and not evil.
80720 **    May you find forgiveness for yourself and forgive others.
80721 **    May you share freely, never taking more than you give.
80722 **
80723 *************************************************************************
80724 ** This file contains C code routines that used to generate VDBE code
80725 ** that implements the ALTER TABLE command.
80726 */
80727 
80728 /*
80729 ** The code in this file only exists if we are not omitting the
80730 ** ALTER TABLE logic from the build.
80731 */
80732 #ifndef SQLITE_OMIT_ALTERTABLE
80733 
80734 
80735 /*
80736 ** This function is used by SQL generated to implement the
80737 ** ALTER TABLE command. The first argument is the text of a CREATE TABLE or
80738 ** CREATE INDEX command. The second is a table name. The table name in
80739 ** the CREATE TABLE or CREATE INDEX statement is replaced with the third
80740 ** argument and the result returned. Examples:
80741 **
80742 ** sqlite_rename_table('CREATE TABLE abc(a, b, c)', 'def')
80743 **     -> 'CREATE TABLE def(a, b, c)'
80744 **
80745 ** sqlite_rename_table('CREATE INDEX i ON abc(a)', 'def')
80746 **     -> 'CREATE INDEX i ON def(a, b, c)'
80747 */
80748 static void renameTableFunc(
80749   sqlite3_context *context,
80750   int NotUsed,
80751   sqlite3_value **argv
80752 ){
80753   unsigned char const *zSql = sqlite3_value_text(argv[0]);
80754   unsigned char const *zTableName = sqlite3_value_text(argv[1]);
80755 
80756   int token;
80757   Token tname;
80758   unsigned char const *zCsr = zSql;
80759   int len = 0;
80760   char *zRet;
80761 
80762   sqlite3 *db = sqlite3_context_db_handle(context);
80763 
80764   UNUSED_PARAMETER(NotUsed);
80765 
80766   /* The principle used to locate the table name in the CREATE TABLE
80767   ** statement is that the table name is the first non-space token that
80768   ** is immediately followed by a TK_LP or TK_USING token.
80769   */
80770   if( zSql ){
80771     do {
80772       if( !*zCsr ){
80773         /* Ran out of input before finding an opening bracket. Return NULL. */
80774         return;
80775       }
80776 
80777       /* Store the token that zCsr points to in tname. */
80778       tname.z = (char*)zCsr;
80779       tname.n = len;
80780 
80781       /* Advance zCsr to the next token. Store that token type in 'token',
80782       ** and its length in 'len' (to be used next iteration of this loop).
80783       */
80784       do {
80785         zCsr += len;
80786         len = sqlite3GetToken(zCsr, &token);
80787       } while( token==TK_SPACE );
80788       assert( len>0 );
80789     } while( token!=TK_LP && token!=TK_USING );
80790 
80791     zRet = sqlite3MPrintf(db, "%.*s\"%w\"%s", ((u8*)tname.z) - zSql, zSql,
80792        zTableName, tname.z+tname.n);
80793     sqlite3_result_text(context, zRet, -1, SQLITE_DYNAMIC);
80794   }
80795 }
80796 
80797 /*
80798 ** This C function implements an SQL user function that is used by SQL code
80799 ** generated by the ALTER TABLE ... RENAME command to modify the definition
80800 ** of any foreign key constraints that use the table being renamed as the
80801 ** parent table. It is passed three arguments:
80802 **
80803 **   1) The complete text of the CREATE TABLE statement being modified,
80804 **   2) The old name of the table being renamed, and
80805 **   3) The new name of the table being renamed.
80806 **
80807 ** It returns the new CREATE TABLE statement. For example:
80808 **
80809 **   sqlite_rename_parent('CREATE TABLE t1(a REFERENCES t2)', 't2', 't3')
80810 **       -> 'CREATE TABLE t1(a REFERENCES t3)'
80811 */
80812 #ifndef SQLITE_OMIT_FOREIGN_KEY
80813 static void renameParentFunc(
80814   sqlite3_context *context,
80815   int NotUsed,
80816   sqlite3_value **argv
80817 ){
80818   sqlite3 *db = sqlite3_context_db_handle(context);
80819   char *zOutput = 0;
80820   char *zResult;
80821   unsigned char const *zInput = sqlite3_value_text(argv[0]);
80822   unsigned char const *zOld = sqlite3_value_text(argv[1]);
80823   unsigned char const *zNew = sqlite3_value_text(argv[2]);
80824 
80825   unsigned const char *z;         /* Pointer to token */
80826   int n;                          /* Length of token z */
80827   int token;                      /* Type of token */
80828 
80829   UNUSED_PARAMETER(NotUsed);
80830   for(z=zInput; *z; z=z+n){
80831     n = sqlite3GetToken(z, &token);
80832     if( token==TK_REFERENCES ){
80833       char *zParent;
80834       do {
80835         z += n;
80836         n = sqlite3GetToken(z, &token);
80837       }while( token==TK_SPACE );
80838 
80839       zParent = sqlite3DbStrNDup(db, (const char *)z, n);
80840       if( zParent==0 ) break;
80841       sqlite3Dequote(zParent);
80842       if( 0==sqlite3StrICmp((const char *)zOld, zParent) ){
80843         char *zOut = sqlite3MPrintf(db, "%s%.*s\"%w\"",
80844             (zOutput?zOutput:""), z-zInput, zInput, (const char *)zNew
80845         );
80846         sqlite3DbFree(db, zOutput);
80847         zOutput = zOut;
80848         zInput = &z[n];
80849       }
80850       sqlite3DbFree(db, zParent);
80851     }
80852   }
80853 
80854   zResult = sqlite3MPrintf(db, "%s%s", (zOutput?zOutput:""), zInput),
80855   sqlite3_result_text(context, zResult, -1, SQLITE_DYNAMIC);
80856   sqlite3DbFree(db, zOutput);
80857 }
80858 #endif
80859 
80860 #ifndef SQLITE_OMIT_TRIGGER
80861 /* This function is used by SQL generated to implement the
80862 ** ALTER TABLE command. The first argument is the text of a CREATE TRIGGER
80863 ** statement. The second is a table name. The table name in the CREATE
80864 ** TRIGGER statement is replaced with the third argument and the result
80865 ** returned. This is analagous to renameTableFunc() above, except for CREATE
80866 ** TRIGGER, not CREATE INDEX and CREATE TABLE.
80867 */
80868 static void renameTriggerFunc(
80869   sqlite3_context *context,
80870   int NotUsed,
80871   sqlite3_value **argv
80872 ){
80873   unsigned char const *zSql = sqlite3_value_text(argv[0]);
80874   unsigned char const *zTableName = sqlite3_value_text(argv[1]);
80875 
80876   int token;
80877   Token tname;
80878   int dist = 3;
80879   unsigned char const *zCsr = zSql;
80880   int len = 0;
80881   char *zRet;
80882   sqlite3 *db = sqlite3_context_db_handle(context);
80883 
80884   UNUSED_PARAMETER(NotUsed);
80885 
80886   /* The principle used to locate the table name in the CREATE TRIGGER
80887   ** statement is that the table name is the first token that is immediatedly
80888   ** preceded by either TK_ON or TK_DOT and immediatedly followed by one
80889   ** of TK_WHEN, TK_BEGIN or TK_FOR.
80890   */
80891   if( zSql ){
80892     do {
80893 
80894       if( !*zCsr ){
80895         /* Ran out of input before finding the table name. Return NULL. */
80896         return;
80897       }
80898 
80899       /* Store the token that zCsr points to in tname. */
80900       tname.z = (char*)zCsr;
80901       tname.n = len;
80902 
80903       /* Advance zCsr to the next token. Store that token type in 'token',
80904       ** and its length in 'len' (to be used next iteration of this loop).
80905       */
80906       do {
80907         zCsr += len;
80908         len = sqlite3GetToken(zCsr, &token);
80909       }while( token==TK_SPACE );
80910       assert( len>0 );
80911 
80912       /* Variable 'dist' stores the number of tokens read since the most
80913       ** recent TK_DOT or TK_ON. This means that when a WHEN, FOR or BEGIN
80914       ** token is read and 'dist' equals 2, the condition stated above
80915       ** to be met.
80916       **
80917       ** Note that ON cannot be a database, table or column name, so
80918       ** there is no need to worry about syntax like
80919       ** "CREATE TRIGGER ... ON ON.ON BEGIN ..." etc.
80920       */
80921       dist++;
80922       if( token==TK_DOT || token==TK_ON ){
80923         dist = 0;
80924       }
80925     } while( dist!=2 || (token!=TK_WHEN && token!=TK_FOR && token!=TK_BEGIN) );
80926 
80927     /* Variable tname now contains the token that is the old table-name
80928     ** in the CREATE TRIGGER statement.
80929     */
80930     zRet = sqlite3MPrintf(db, "%.*s\"%w\"%s", ((u8*)tname.z) - zSql, zSql,
80931        zTableName, tname.z+tname.n);
80932     sqlite3_result_text(context, zRet, -1, SQLITE_DYNAMIC);
80933   }
80934 }
80935 #endif   /* !SQLITE_OMIT_TRIGGER */
80936 
80937 /*
80938 ** Register built-in functions used to help implement ALTER TABLE
80939 */
80940 SQLITE_PRIVATE void sqlite3AlterFunctions(void){
80941   static SQLITE_WSD FuncDef aAlterTableFuncs[] = {
80942     FUNCTION(sqlite_rename_table,   2, 0, 0, renameTableFunc),
80943 #ifndef SQLITE_OMIT_TRIGGER
80944     FUNCTION(sqlite_rename_trigger, 2, 0, 0, renameTriggerFunc),
80945 #endif
80946 #ifndef SQLITE_OMIT_FOREIGN_KEY
80947     FUNCTION(sqlite_rename_parent,  3, 0, 0, renameParentFunc),
80948 #endif
80949   };
80950   int i;
80951   FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions);
80952   FuncDef *aFunc = (FuncDef*)&GLOBAL(FuncDef, aAlterTableFuncs);
80953 
80954   for(i=0; i<ArraySize(aAlterTableFuncs); i++){
80955     sqlite3FuncDefInsert(pHash, &aFunc[i]);
80956   }
80957 }
80958 
80959 /*
80960 ** This function is used to create the text of expressions of the form:
80961 **
80962 **   name=<constant1> OR name=<constant2> OR ...
80963 **
80964 ** If argument zWhere is NULL, then a pointer string containing the text
80965 ** "name=<constant>" is returned, where <constant> is the quoted version
80966 ** of the string passed as argument zConstant. The returned buffer is
80967 ** allocated using sqlite3DbMalloc(). It is the responsibility of the
80968 ** caller to ensure that it is eventually freed.
80969 **
80970 ** If argument zWhere is not NULL, then the string returned is
80971 ** "<where> OR name=<constant>", where <where> is the contents of zWhere.
80972 ** In this case zWhere is passed to sqlite3DbFree() before returning.
80973 **
80974 */
80975 static char *whereOrName(sqlite3 *db, char *zWhere, char *zConstant){
80976   char *zNew;
80977   if( !zWhere ){
80978     zNew = sqlite3MPrintf(db, "name=%Q", zConstant);
80979   }else{
80980     zNew = sqlite3MPrintf(db, "%s OR name=%Q", zWhere, zConstant);
80981     sqlite3DbFree(db, zWhere);
80982   }
80983   return zNew;
80984 }
80985 
80986 #if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
80987 /*
80988 ** Generate the text of a WHERE expression which can be used to select all
80989 ** tables that have foreign key constraints that refer to table pTab (i.e.
80990 ** constraints for which pTab is the parent table) from the sqlite_master
80991 ** table.
80992 */
80993 static char *whereForeignKeys(Parse *pParse, Table *pTab){
80994   FKey *p;
80995   char *zWhere = 0;
80996   for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
80997     zWhere = whereOrName(pParse->db, zWhere, p->pFrom->zName);
80998   }
80999   return zWhere;
81000 }
81001 #endif
81002 
81003 /*
81004 ** Generate the text of a WHERE expression which can be used to select all
81005 ** temporary triggers on table pTab from the sqlite_temp_master table. If
81006 ** table pTab has no temporary triggers, or is itself stored in the
81007 ** temporary database, NULL is returned.
81008 */
81009 static char *whereTempTriggers(Parse *pParse, Table *pTab){
81010   Trigger *pTrig;
81011   char *zWhere = 0;
81012   const Schema *pTempSchema = pParse->db->aDb[1].pSchema; /* Temp db schema */
81013 
81014   /* If the table is not located in the temp-db (in which case NULL is
81015   ** returned, loop through the tables list of triggers. For each trigger
81016   ** that is not part of the temp-db schema, add a clause to the WHERE
81017   ** expression being built up in zWhere.
81018   */
81019   if( pTab->pSchema!=pTempSchema ){
81020     sqlite3 *db = pParse->db;
81021     for(pTrig=sqlite3TriggerList(pParse, pTab); pTrig; pTrig=pTrig->pNext){
81022       if( pTrig->pSchema==pTempSchema ){
81023         zWhere = whereOrName(db, zWhere, pTrig->zName);
81024       }
81025     }
81026   }
81027   if( zWhere ){
81028     char *zNew = sqlite3MPrintf(pParse->db, "type='trigger' AND (%s)", zWhere);
81029     sqlite3DbFree(pParse->db, zWhere);
81030     zWhere = zNew;
81031   }
81032   return zWhere;
81033 }
81034 
81035 /*
81036 ** Generate code to drop and reload the internal representation of table
81037 ** pTab from the database, including triggers and temporary triggers.
81038 ** Argument zName is the name of the table in the database schema at
81039 ** the time the generated code is executed. This can be different from
81040 ** pTab->zName if this function is being called to code part of an
81041 ** "ALTER TABLE RENAME TO" statement.
81042 */
81043 static void reloadTableSchema(Parse *pParse, Table *pTab, const char *zName){
81044   Vdbe *v;
81045   char *zWhere;
81046   int iDb;                   /* Index of database containing pTab */
81047 #ifndef SQLITE_OMIT_TRIGGER
81048   Trigger *pTrig;
81049 #endif
81050 
81051   v = sqlite3GetVdbe(pParse);
81052   if( NEVER(v==0) ) return;
81053   assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
81054   iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
81055   assert( iDb>=0 );
81056 
81057 #ifndef SQLITE_OMIT_TRIGGER
81058   /* Drop any table triggers from the internal schema. */
81059   for(pTrig=sqlite3TriggerList(pParse, pTab); pTrig; pTrig=pTrig->pNext){
81060     int iTrigDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema);
81061     assert( iTrigDb==iDb || iTrigDb==1 );
81062     sqlite3VdbeAddOp4(v, OP_DropTrigger, iTrigDb, 0, 0, pTrig->zName, 0);
81063   }
81064 #endif
81065 
81066   /* Drop the table and index from the internal schema.  */
81067   sqlite3VdbeAddOp4(v, OP_DropTable, iDb, 0, 0, pTab->zName, 0);
81068 
81069   /* Reload the table, index and permanent trigger schemas. */
81070   zWhere = sqlite3MPrintf(pParse->db, "tbl_name=%Q", zName);
81071   if( !zWhere ) return;
81072   sqlite3VdbeAddParseSchemaOp(v, iDb, zWhere);
81073 
81074 #ifndef SQLITE_OMIT_TRIGGER
81075   /* Now, if the table is not stored in the temp database, reload any temp
81076   ** triggers. Don't use IN(...) in case SQLITE_OMIT_SUBQUERY is defined.
81077   */
81078   if( (zWhere=whereTempTriggers(pParse, pTab))!=0 ){
81079     sqlite3VdbeAddParseSchemaOp(v, 1, zWhere);
81080   }
81081 #endif
81082 }
81083 
81084 /*
81085 ** Parameter zName is the name of a table that is about to be altered
81086 ** (either with ALTER TABLE ... RENAME TO or ALTER TABLE ... ADD COLUMN).
81087 ** If the table is a system table, this function leaves an error message
81088 ** in pParse->zErr (system tables may not be altered) and returns non-zero.
81089 **
81090 ** Or, if zName is not a system table, zero is returned.
81091 */
81092 static int isSystemTable(Parse *pParse, const char *zName){
81093   if( sqlite3Strlen30(zName)>6 && 0==sqlite3StrNICmp(zName, "sqlite_", 7) ){
81094     sqlite3ErrorMsg(pParse, "table %s may not be altered", zName);
81095     return 1;
81096   }
81097   return 0;
81098 }
81099 
81100 /*
81101 ** Generate code to implement the "ALTER TABLE xxx RENAME TO yyy"
81102 ** command.
81103 */
81104 SQLITE_PRIVATE void sqlite3AlterRenameTable(
81105   Parse *pParse,            /* Parser context. */
81106   SrcList *pSrc,            /* The table to rename. */
81107   Token *pName              /* The new table name. */
81108 ){
81109   int iDb;                  /* Database that contains the table */
81110   char *zDb;                /* Name of database iDb */
81111   Table *pTab;              /* Table being renamed */
81112   char *zName = 0;          /* NULL-terminated version of pName */
81113   sqlite3 *db = pParse->db; /* Database connection */
81114   int nTabName;             /* Number of UTF-8 characters in zTabName */
81115   const char *zTabName;     /* Original name of the table */
81116   Vdbe *v;
81117 #ifndef SQLITE_OMIT_TRIGGER
81118   char *zWhere = 0;         /* Where clause to locate temp triggers */
81119 #endif
81120   VTable *pVTab = 0;        /* Non-zero if this is a v-tab with an xRename() */
81121   int savedDbFlags;         /* Saved value of db->flags */
81122 
81123   savedDbFlags = db->flags;
81124   if( NEVER(db->mallocFailed) ) goto exit_rename_table;
81125   assert( pSrc->nSrc==1 );
81126   assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
81127 
81128   pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]);
81129   if( !pTab ) goto exit_rename_table;
81130   iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
81131   zDb = db->aDb[iDb].zName;
81132   db->flags |= SQLITE_PreferBuiltin;
81133 
81134   /* Get a NULL terminated version of the new table name. */
81135   zName = sqlite3NameFromToken(db, pName);
81136   if( !zName ) goto exit_rename_table;
81137 
81138   /* Check that a table or index named 'zName' does not already exist
81139   ** in database iDb. If so, this is an error.
81140   */
81141   if( sqlite3FindTable(db, zName, zDb) || sqlite3FindIndex(db, zName, zDb) ){
81142     sqlite3ErrorMsg(pParse,
81143         "there is already another table or index with this name: %s", zName);
81144     goto exit_rename_table;
81145   }
81146 
81147   /* Make sure it is not a system table being altered, or a reserved name
81148   ** that the table is being renamed to.
81149   */
81150   if( SQLITE_OK!=isSystemTable(pParse, pTab->zName) ){
81151     goto exit_rename_table;
81152   }
81153   if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){ goto
81154     exit_rename_table;
81155   }
81156 
81157 #ifndef SQLITE_OMIT_VIEW
81158   if( pTab->pSelect ){
81159     sqlite3ErrorMsg(pParse, "view %s may not be altered", pTab->zName);
81160     goto exit_rename_table;
81161   }
81162 #endif
81163 
81164 #ifndef SQLITE_OMIT_AUTHORIZATION
81165   /* Invoke the authorization callback. */
81166   if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
81167     goto exit_rename_table;
81168   }
81169 #endif
81170 
81171 #ifndef SQLITE_OMIT_VIRTUALTABLE
81172   if( sqlite3ViewGetColumnNames(pParse, pTab) ){
81173     goto exit_rename_table;
81174   }
81175   if( IsVirtual(pTab) ){
81176     pVTab = sqlite3GetVTable(db, pTab);
81177     if( pVTab->pVtab->pModule->xRename==0 ){
81178       pVTab = 0;
81179     }
81180   }
81181 #endif
81182 
81183   /* Begin a transaction and code the VerifyCookie for database iDb.
81184   ** Then modify the schema cookie (since the ALTER TABLE modifies the
81185   ** schema). Open a statement transaction if the table is a virtual
81186   ** table.
81187   */
81188   v = sqlite3GetVdbe(pParse);
81189   if( v==0 ){
81190     goto exit_rename_table;
81191   }
81192   sqlite3BeginWriteOperation(pParse, pVTab!=0, iDb);
81193   sqlite3ChangeCookie(pParse, iDb);
81194 
81195   /* If this is a virtual table, invoke the xRename() function if
81196   ** one is defined. The xRename() callback will modify the names
81197   ** of any resources used by the v-table implementation (including other
81198   ** SQLite tables) that are identified by the name of the virtual table.
81199   */
81200 #ifndef SQLITE_OMIT_VIRTUALTABLE
81201   if( pVTab ){
81202     int i = ++pParse->nMem;
81203     sqlite3VdbeAddOp4(v, OP_String8, 0, i, 0, zName, 0);
81204     sqlite3VdbeAddOp4(v, OP_VRename, i, 0, 0,(const char*)pVTab, P4_VTAB);
81205     sqlite3MayAbort(pParse);
81206   }
81207 #endif
81208 
81209   /* figure out how many UTF-8 characters are in zName */
81210   zTabName = pTab->zName;
81211   nTabName = sqlite3Utf8CharLen(zTabName, -1);
81212 
81213 #if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
81214   if( db->flags&SQLITE_ForeignKeys ){
81215     /* If foreign-key support is enabled, rewrite the CREATE TABLE
81216     ** statements corresponding to all child tables of foreign key constraints
81217     ** for which the renamed table is the parent table.  */
81218     if( (zWhere=whereForeignKeys(pParse, pTab))!=0 ){
81219       sqlite3NestedParse(pParse,
81220           "UPDATE \"%w\".%s SET "
81221               "sql = sqlite_rename_parent(sql, %Q, %Q) "
81222               "WHERE %s;", zDb, SCHEMA_TABLE(iDb), zTabName, zName, zWhere);
81223       sqlite3DbFree(db, zWhere);
81224     }
81225   }
81226 #endif
81227 
81228   /* Modify the sqlite_master table to use the new table name. */
81229   sqlite3NestedParse(pParse,
81230       "UPDATE %Q.%s SET "
81231 #ifdef SQLITE_OMIT_TRIGGER
81232           "sql = sqlite_rename_table(sql, %Q), "
81233 #else
81234           "sql = CASE "
81235             "WHEN type = 'trigger' THEN sqlite_rename_trigger(sql, %Q)"
81236             "ELSE sqlite_rename_table(sql, %Q) END, "
81237 #endif
81238           "tbl_name = %Q, "
81239           "name = CASE "
81240             "WHEN type='table' THEN %Q "
81241             "WHEN name LIKE 'sqlite_autoindex%%' AND type='index' THEN "
81242              "'sqlite_autoindex_' || %Q || substr(name,%d+18) "
81243             "ELSE name END "
81244       "WHERE tbl_name=%Q COLLATE nocase AND "
81245           "(type='table' OR type='index' OR type='trigger');",
81246       zDb, SCHEMA_TABLE(iDb), zName, zName, zName,
81247 #ifndef SQLITE_OMIT_TRIGGER
81248       zName,
81249 #endif
81250       zName, nTabName, zTabName
81251   );
81252 
81253 #ifndef SQLITE_OMIT_AUTOINCREMENT
81254   /* If the sqlite_sequence table exists in this database, then update
81255   ** it with the new table name.
81256   */
81257   if( sqlite3FindTable(db, "sqlite_sequence", zDb) ){
81258     sqlite3NestedParse(pParse,
81259         "UPDATE \"%w\".sqlite_sequence set name = %Q WHERE name = %Q",
81260         zDb, zName, pTab->zName);
81261   }
81262 #endif
81263 
81264 #ifndef SQLITE_OMIT_TRIGGER
81265   /* If there are TEMP triggers on this table, modify the sqlite_temp_master
81266   ** table. Don't do this if the table being ALTERed is itself located in
81267   ** the temp database.
81268   */
81269   if( (zWhere=whereTempTriggers(pParse, pTab))!=0 ){
81270     sqlite3NestedParse(pParse,
81271         "UPDATE sqlite_temp_master SET "
81272             "sql = sqlite_rename_trigger(sql, %Q), "
81273             "tbl_name = %Q "
81274             "WHERE %s;", zName, zName, zWhere);
81275     sqlite3DbFree(db, zWhere);
81276   }
81277 #endif
81278 
81279 #if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
81280   if( db->flags&SQLITE_ForeignKeys ){
81281     FKey *p;
81282     for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
81283       Table *pFrom = p->pFrom;
81284       if( pFrom!=pTab ){
81285         reloadTableSchema(pParse, p->pFrom, pFrom->zName);
81286       }
81287     }
81288   }
81289 #endif
81290 
81291   /* Drop and reload the internal table schema. */
81292   reloadTableSchema(pParse, pTab, zName);
81293 
81294 exit_rename_table:
81295   sqlite3SrcListDelete(db, pSrc);
81296   sqlite3DbFree(db, zName);
81297   db->flags = savedDbFlags;
81298 }
81299 
81300 
81301 /*
81302 ** Generate code to make sure the file format number is at least minFormat.
81303 ** The generated code will increase the file format number if necessary.
81304 */
81305 SQLITE_PRIVATE void sqlite3MinimumFileFormat(Parse *pParse, int iDb, int minFormat){
81306   Vdbe *v;
81307   v = sqlite3GetVdbe(pParse);
81308   /* The VDBE should have been allocated before this routine is called.
81309   ** If that allocation failed, we would have quit before reaching this
81310   ** point */
81311   if( ALWAYS(v) ){
81312     int r1 = sqlite3GetTempReg(pParse);
81313     int r2 = sqlite3GetTempReg(pParse);
81314     int j1;
81315     sqlite3VdbeAddOp3(v, OP_ReadCookie, iDb, r1, BTREE_FILE_FORMAT);
81316     sqlite3VdbeUsesBtree(v, iDb);
81317     sqlite3VdbeAddOp2(v, OP_Integer, minFormat, r2);
81318     j1 = sqlite3VdbeAddOp3(v, OP_Ge, r2, 0, r1);
81319     sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_FILE_FORMAT, r2);
81320     sqlite3VdbeJumpHere(v, j1);
81321     sqlite3ReleaseTempReg(pParse, r1);
81322     sqlite3ReleaseTempReg(pParse, r2);
81323   }
81324 }
81325 
81326 /*
81327 ** This function is called after an "ALTER TABLE ... ADD" statement
81328 ** has been parsed. Argument pColDef contains the text of the new
81329 ** column definition.
81330 **
81331 ** The Table structure pParse->pNewTable was extended to include
81332 ** the new column during parsing.
81333 */
81334 SQLITE_PRIVATE void sqlite3AlterFinishAddColumn(Parse *pParse, Token *pColDef){
81335   Table *pNew;              /* Copy of pParse->pNewTable */
81336   Table *pTab;              /* Table being altered */
81337   int iDb;                  /* Database number */
81338   const char *zDb;          /* Database name */
81339   const char *zTab;         /* Table name */
81340   char *zCol;               /* Null-terminated column definition */
81341   Column *pCol;             /* The new column */
81342   Expr *pDflt;              /* Default value for the new column */
81343   sqlite3 *db;              /* The database connection; */
81344 
81345   db = pParse->db;
81346   if( pParse->nErr || db->mallocFailed ) return;
81347   pNew = pParse->pNewTable;
81348   assert( pNew );
81349 
81350   assert( sqlite3BtreeHoldsAllMutexes(db) );
81351   iDb = sqlite3SchemaToIndex(db, pNew->pSchema);
81352   zDb = db->aDb[iDb].zName;
81353   zTab = &pNew->zName[16];  /* Skip the "sqlite_altertab_" prefix on the name */
81354   pCol = &pNew->aCol[pNew->nCol-1];
81355   pDflt = pCol->pDflt;
81356   pTab = sqlite3FindTable(db, zTab, zDb);
81357   assert( pTab );
81358 
81359 #ifndef SQLITE_OMIT_AUTHORIZATION
81360   /* Invoke the authorization callback. */
81361   if( sqlite3AuthCheck(pParse, SQLITE_ALTER_TABLE, zDb, pTab->zName, 0) ){
81362     return;
81363   }
81364 #endif
81365 
81366   /* If the default value for the new column was specified with a
81367   ** literal NULL, then set pDflt to 0. This simplifies checking
81368   ** for an SQL NULL default below.
81369   */
81370   if( pDflt && pDflt->op==TK_NULL ){
81371     pDflt = 0;
81372   }
81373 
81374   /* Check that the new column is not specified as PRIMARY KEY or UNIQUE.
81375   ** If there is a NOT NULL constraint, then the default value for the
81376   ** column must not be NULL.
81377   */
81378   if( pCol->colFlags & COLFLAG_PRIMKEY ){
81379     sqlite3ErrorMsg(pParse, "Cannot add a PRIMARY KEY column");
81380     return;
81381   }
81382   if( pNew->pIndex ){
81383     sqlite3ErrorMsg(pParse, "Cannot add a UNIQUE column");
81384     return;
81385   }
81386   if( (db->flags&SQLITE_ForeignKeys) && pNew->pFKey && pDflt ){
81387     sqlite3ErrorMsg(pParse,
81388         "Cannot add a REFERENCES column with non-NULL default value");
81389     return;
81390   }
81391   if( pCol->notNull && !pDflt ){
81392     sqlite3ErrorMsg(pParse,
81393         "Cannot add a NOT NULL column with default value NULL");
81394     return;
81395   }
81396 
81397   /* Ensure the default expression is something that sqlite3ValueFromExpr()
81398   ** can handle (i.e. not CURRENT_TIME etc.)
81399   */
81400   if( pDflt ){
81401     sqlite3_value *pVal = 0;
81402     if( sqlite3ValueFromExpr(db, pDflt, SQLITE_UTF8, SQLITE_AFF_NONE, &pVal) ){
81403       db->mallocFailed = 1;
81404       return;
81405     }
81406     if( !pVal ){
81407       sqlite3ErrorMsg(pParse, "Cannot add a column with non-constant default");
81408       return;
81409     }
81410     sqlite3ValueFree(pVal);
81411   }
81412 
81413   /* Modify the CREATE TABLE statement. */
81414   zCol = sqlite3DbStrNDup(db, (char*)pColDef->z, pColDef->n);
81415   if( zCol ){
81416     char *zEnd = &zCol[pColDef->n-1];
81417     int savedDbFlags = db->flags;
81418     while( zEnd>zCol && (*zEnd==';' || sqlite3Isspace(*zEnd)) ){
81419       *zEnd-- = '\0';
81420     }
81421     db->flags |= SQLITE_PreferBuiltin;
81422     sqlite3NestedParse(pParse,
81423         "UPDATE \"%w\".%s SET "
81424           "sql = substr(sql,1,%d) || ', ' || %Q || substr(sql,%d) "
81425         "WHERE type = 'table' AND name = %Q",
81426       zDb, SCHEMA_TABLE(iDb), pNew->addColOffset, zCol, pNew->addColOffset+1,
81427       zTab
81428     );
81429     sqlite3DbFree(db, zCol);
81430     db->flags = savedDbFlags;
81431   }
81432 
81433   /* If the default value of the new column is NULL, then set the file
81434   ** format to 2. If the default value of the new column is not NULL,
81435   ** the file format becomes 3.
81436   */
81437   sqlite3MinimumFileFormat(pParse, iDb, pDflt ? 3 : 2);
81438 
81439   /* Reload the schema of the modified table. */
81440   reloadTableSchema(pParse, pTab, pTab->zName);
81441 }
81442 
81443 /*
81444 ** This function is called by the parser after the table-name in
81445 ** an "ALTER TABLE <table-name> ADD" statement is parsed. Argument
81446 ** pSrc is the full-name of the table being altered.
81447 **
81448 ** This routine makes a (partial) copy of the Table structure
81449 ** for the table being altered and sets Parse.pNewTable to point
81450 ** to it. Routines called by the parser as the column definition
81451 ** is parsed (i.e. sqlite3AddColumn()) add the new Column data to
81452 ** the copy. The copy of the Table structure is deleted by tokenize.c
81453 ** after parsing is finished.
81454 **
81455 ** Routine sqlite3AlterFinishAddColumn() will be called to complete
81456 ** coding the "ALTER TABLE ... ADD" statement.
81457 */
81458 SQLITE_PRIVATE void sqlite3AlterBeginAddColumn(Parse *pParse, SrcList *pSrc){
81459   Table *pNew;
81460   Table *pTab;
81461   Vdbe *v;
81462   int iDb;
81463   int i;
81464   int nAlloc;
81465   sqlite3 *db = pParse->db;
81466 
81467   /* Look up the table being altered. */
81468   assert( pParse->pNewTable==0 );
81469   assert( sqlite3BtreeHoldsAllMutexes(db) );
81470   if( db->mallocFailed ) goto exit_begin_add_column;
81471   pTab = sqlite3LocateTableItem(pParse, 0, &pSrc->a[0]);
81472   if( !pTab ) goto exit_begin_add_column;
81473 
81474 #ifndef SQLITE_OMIT_VIRTUALTABLE
81475   if( IsVirtual(pTab) ){
81476     sqlite3ErrorMsg(pParse, "virtual tables may not be altered");
81477     goto exit_begin_add_column;
81478   }
81479 #endif
81480 
81481   /* Make sure this is not an attempt to ALTER a view. */
81482   if( pTab->pSelect ){
81483     sqlite3ErrorMsg(pParse, "Cannot add a column to a view");
81484     goto exit_begin_add_column;
81485   }
81486   if( SQLITE_OK!=isSystemTable(pParse, pTab->zName) ){
81487     goto exit_begin_add_column;
81488   }
81489 
81490   assert( pTab->addColOffset>0 );
81491   iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
81492 
81493   /* Put a copy of the Table struct in Parse.pNewTable for the
81494   ** sqlite3AddColumn() function and friends to modify.  But modify
81495   ** the name by adding an "sqlite_altertab_" prefix.  By adding this
81496   ** prefix, we insure that the name will not collide with an existing
81497   ** table because user table are not allowed to have the "sqlite_"
81498   ** prefix on their name.
81499   */
81500   pNew = (Table*)sqlite3DbMallocZero(db, sizeof(Table));
81501   if( !pNew ) goto exit_begin_add_column;
81502   pParse->pNewTable = pNew;
81503   pNew->nRef = 1;
81504   pNew->nCol = pTab->nCol;
81505   assert( pNew->nCol>0 );
81506   nAlloc = (((pNew->nCol-1)/8)*8)+8;
81507   assert( nAlloc>=pNew->nCol && nAlloc%8==0 && nAlloc-pNew->nCol<8 );
81508   pNew->aCol = (Column*)sqlite3DbMallocZero(db, sizeof(Column)*nAlloc);
81509   pNew->zName = sqlite3MPrintf(db, "sqlite_altertab_%s", pTab->zName);
81510   if( !pNew->aCol || !pNew->zName ){
81511     db->mallocFailed = 1;
81512     goto exit_begin_add_column;
81513   }
81514   memcpy(pNew->aCol, pTab->aCol, sizeof(Column)*pNew->nCol);
81515   for(i=0; i<pNew->nCol; i++){
81516     Column *pCol = &pNew->aCol[i];
81517     pCol->zName = sqlite3DbStrDup(db, pCol->zName);
81518     pCol->zColl = 0;
81519     pCol->zType = 0;
81520     pCol->pDflt = 0;
81521     pCol->zDflt = 0;
81522   }
81523   pNew->pSchema = db->aDb[iDb].pSchema;
81524   pNew->addColOffset = pTab->addColOffset;
81525   pNew->nRef = 1;
81526 
81527   /* Begin a transaction and increment the schema cookie.  */
81528   sqlite3BeginWriteOperation(pParse, 0, iDb);
81529   v = sqlite3GetVdbe(pParse);
81530   if( !v ) goto exit_begin_add_column;
81531   sqlite3ChangeCookie(pParse, iDb);
81532 
81533 exit_begin_add_column:
81534   sqlite3SrcListDelete(db, pSrc);
81535   return;
81536 }
81537 #endif  /* SQLITE_ALTER_TABLE */
81538 
81539 /************** End of alter.c ***********************************************/
81540 /************** Begin file analyze.c *****************************************/
81541 /*
81542 ** 2005-07-08
81543 **
81544 ** The author disclaims copyright to this source code.  In place of
81545 ** a legal notice, here is a blessing:
81546 **
81547 **    May you do good and not evil.
81548 **    May you find forgiveness for yourself and forgive others.
81549 **    May you share freely, never taking more than you give.
81550 **
81551 *************************************************************************
81552 ** This file contains code associated with the ANALYZE command.
81553 **
81554 ** The ANALYZE command gather statistics about the content of tables
81555 ** and indices.  These statistics are made available to the query planner
81556 ** to help it make better decisions about how to perform queries.
81557 **
81558 ** The following system tables are or have been supported:
81559 **
81560 **    CREATE TABLE sqlite_stat1(tbl, idx, stat);
81561 **    CREATE TABLE sqlite_stat2(tbl, idx, sampleno, sample);
81562 **    CREATE TABLE sqlite_stat3(tbl, idx, nEq, nLt, nDLt, sample);
81563 **    CREATE TABLE sqlite_stat4(tbl, idx, nEq, nLt, nDLt, sample);
81564 **
81565 ** Additional tables might be added in future releases of SQLite.
81566 ** The sqlite_stat2 table is not created or used unless the SQLite version
81567 ** is between 3.6.18 and 3.7.8, inclusive, and unless SQLite is compiled
81568 ** with SQLITE_ENABLE_STAT2.  The sqlite_stat2 table is deprecated.
81569 ** The sqlite_stat2 table is superseded by sqlite_stat3, which is only
81570 ** created and used by SQLite versions 3.7.9 and later and with
81571 ** SQLITE_ENABLE_STAT3 defined.  The functionality of sqlite_stat3
81572 ** is a superset of sqlite_stat2.  The sqlite_stat4 is an enhanced
81573 ** version of sqlite_stat3 and is only available when compiled with
81574 ** SQLITE_ENABLE_STAT4 and in SQLite versions 3.8.1 and later.  It is
81575 ** not possible to enable both STAT3 and STAT4 at the same time.  If they
81576 ** are both enabled, then STAT4 takes precedence.
81577 **
81578 ** For most applications, sqlite_stat1 provides all the statisics required
81579 ** for the query planner to make good choices.
81580 **
81581 ** Format of sqlite_stat1:
81582 **
81583 ** There is normally one row per index, with the index identified by the
81584 ** name in the idx column.  The tbl column is the name of the table to
81585 ** which the index belongs.  In each such row, the stat column will be
81586 ** a string consisting of a list of integers.  The first integer in this
81587 ** list is the number of rows in the index.  (This is the same as the
81588 ** number of rows in the table, except for partial indices.)  The second
81589 ** integer is the average number of rows in the index that have the same
81590 ** value in the first column of the index.  The third integer is the average
81591 ** number of rows in the index that have the same value for the first two
81592 ** columns.  The N-th integer (for N>1) is the average number of rows in
81593 ** the index which have the same value for the first N-1 columns.  For
81594 ** a K-column index, there will be K+1 integers in the stat column.  If
81595 ** the index is unique, then the last integer will be 1.
81596 **
81597 ** The list of integers in the stat column can optionally be followed
81598 ** by the keyword "unordered".  The "unordered" keyword, if it is present,
81599 ** must be separated from the last integer by a single space.  If the
81600 ** "unordered" keyword is present, then the query planner assumes that
81601 ** the index is unordered and will not use the index for a range query.
81602 **
81603 ** If the sqlite_stat1.idx column is NULL, then the sqlite_stat1.stat
81604 ** column contains a single integer which is the (estimated) number of
81605 ** rows in the table identified by sqlite_stat1.tbl.
81606 **
81607 ** Format of sqlite_stat2:
81608 **
81609 ** The sqlite_stat2 is only created and is only used if SQLite is compiled
81610 ** with SQLITE_ENABLE_STAT2 and if the SQLite version number is between
81611 ** 3.6.18 and 3.7.8.  The "stat2" table contains additional information
81612 ** about the distribution of keys within an index.  The index is identified by
81613 ** the "idx" column and the "tbl" column is the name of the table to which
81614 ** the index belongs.  There are usually 10 rows in the sqlite_stat2
81615 ** table for each index.
81616 **
81617 ** The sqlite_stat2 entries for an index that have sampleno between 0 and 9
81618 ** inclusive are samples of the left-most key value in the index taken at
81619 ** evenly spaced points along the index.  Let the number of samples be S
81620 ** (10 in the standard build) and let C be the number of rows in the index.
81621 ** Then the sampled rows are given by:
81622 **
81623 **     rownumber = (i*C*2 + C)/(S*2)
81624 **
81625 ** For i between 0 and S-1.  Conceptually, the index space is divided into
81626 ** S uniform buckets and the samples are the middle row from each bucket.
81627 **
81628 ** The format for sqlite_stat2 is recorded here for legacy reference.  This
81629 ** version of SQLite does not support sqlite_stat2.  It neither reads nor
81630 ** writes the sqlite_stat2 table.  This version of SQLite only supports
81631 ** sqlite_stat3.
81632 **
81633 ** Format for sqlite_stat3:
81634 **
81635 ** The sqlite_stat3 format is a subset of sqlite_stat4.  Hence, the
81636 ** sqlite_stat4 format will be described first.  Further information
81637 ** about sqlite_stat3 follows the sqlite_stat4 description.
81638 **
81639 ** Format for sqlite_stat4:
81640 **
81641 ** As with sqlite_stat2, the sqlite_stat4 table contains histogram data
81642 ** to aid the query planner in choosing good indices based on the values
81643 ** that indexed columns are compared against in the WHERE clauses of
81644 ** queries.
81645 **
81646 ** The sqlite_stat4 table contains multiple entries for each index.
81647 ** The idx column names the index and the tbl column is the table of the
81648 ** index.  If the idx and tbl columns are the same, then the sample is
81649 ** of the INTEGER PRIMARY KEY.  The sample column is a blob which is the
81650 ** binary encoding of a key from the index.  The nEq column is a
81651 ** list of integers.  The first integer is the approximate number
81652 ** of entries in the index whose left-most column exactly matches
81653 ** the left-most column of the sample.  The second integer in nEq
81654 ** is the approximate number of entries in the index where the
81655 ** first two columns match the first two columns of the sample.
81656 ** And so forth.  nLt is another list of integers that show the approximate
81657 ** number of entries that are strictly less than the sample.  The first
81658 ** integer in nLt contains the number of entries in the index where the
81659 ** left-most column is less than the left-most column of the sample.
81660 ** The K-th integer in the nLt entry is the number of index entries
81661 ** where the first K columns are less than the first K columns of the
81662 ** sample.  The nDLt column is like nLt except that it contains the
81663 ** number of distinct entries in the index that are less than the
81664 ** sample.
81665 **
81666 ** There can be an arbitrary number of sqlite_stat4 entries per index.
81667 ** The ANALYZE command will typically generate sqlite_stat4 tables
81668 ** that contain between 10 and 40 samples which are distributed across
81669 ** the key space, though not uniformly, and which include samples with
81670 ** large nEq values.
81671 **
81672 ** Format for sqlite_stat3 redux:
81673 **
81674 ** The sqlite_stat3 table is like sqlite_stat4 except that it only
81675 ** looks at the left-most column of the index.  The sqlite_stat3.sample
81676 ** column contains the actual value of the left-most column instead
81677 ** of a blob encoding of the complete index key as is found in
81678 ** sqlite_stat4.sample.  The nEq, nLt, and nDLt entries of sqlite_stat3
81679 ** all contain just a single integer which is the same as the first
81680 ** integer in the equivalent columns in sqlite_stat4.
81681 */
81682 #ifndef SQLITE_OMIT_ANALYZE
81683 
81684 #if defined(SQLITE_ENABLE_STAT4)
81685 # define IsStat4     1
81686 # define IsStat3     0
81687 #elif defined(SQLITE_ENABLE_STAT3)
81688 # define IsStat4     0
81689 # define IsStat3     1
81690 #else
81691 # define IsStat4     0
81692 # define IsStat3     0
81693 # undef SQLITE_STAT4_SAMPLES
81694 # define SQLITE_STAT4_SAMPLES 1
81695 #endif
81696 #define IsStat34    (IsStat3+IsStat4)  /* 1 for STAT3 or STAT4. 0 otherwise */
81697 
81698 /*
81699 ** This routine generates code that opens the sqlite_statN tables.
81700 ** The sqlite_stat1 table is always relevant.  sqlite_stat2 is now
81701 ** obsolete.  sqlite_stat3 and sqlite_stat4 are only opened when
81702 ** appropriate compile-time options are provided.
81703 **
81704 ** If the sqlite_statN tables do not previously exist, it is created.
81705 **
81706 ** Argument zWhere may be a pointer to a buffer containing a table name,
81707 ** or it may be a NULL pointer. If it is not NULL, then all entries in
81708 ** the sqlite_statN tables associated with the named table are deleted.
81709 ** If zWhere==0, then code is generated to delete all stat table entries.
81710 */
81711 static void openStatTable(
81712   Parse *pParse,          /* Parsing context */
81713   int iDb,                /* The database we are looking in */
81714   int iStatCur,           /* Open the sqlite_stat1 table on this cursor */
81715   const char *zWhere,     /* Delete entries for this table or index */
81716   const char *zWhereType  /* Either "tbl" or "idx" */
81717 ){
81718   static const struct {
81719     const char *zName;
81720     const char *zCols;
81721   } aTable[] = {
81722     { "sqlite_stat1", "tbl,idx,stat" },
81723 #if defined(SQLITE_ENABLE_STAT4)
81724     { "sqlite_stat4", "tbl,idx,neq,nlt,ndlt,sample" },
81725     { "sqlite_stat3", 0 },
81726 #elif defined(SQLITE_ENABLE_STAT3)
81727     { "sqlite_stat3", "tbl,idx,neq,nlt,ndlt,sample" },
81728     { "sqlite_stat4", 0 },
81729 #else
81730     { "sqlite_stat3", 0 },
81731     { "sqlite_stat4", 0 },
81732 #endif
81733   };
81734   int i;
81735   sqlite3 *db = pParse->db;
81736   Db *pDb;
81737   Vdbe *v = sqlite3GetVdbe(pParse);
81738   int aRoot[ArraySize(aTable)];
81739   u8 aCreateTbl[ArraySize(aTable)];
81740 
81741   if( v==0 ) return;
81742   assert( sqlite3BtreeHoldsAllMutexes(db) );
81743   assert( sqlite3VdbeDb(v)==db );
81744   pDb = &db->aDb[iDb];
81745 
81746   /* Create new statistic tables if they do not exist, or clear them
81747   ** if they do already exist.
81748   */
81749   for(i=0; i<ArraySize(aTable); i++){
81750     const char *zTab = aTable[i].zName;
81751     Table *pStat;
81752     if( (pStat = sqlite3FindTable(db, zTab, pDb->zName))==0 ){
81753       if( aTable[i].zCols ){
81754         /* The sqlite_statN table does not exist. Create it. Note that a
81755         ** side-effect of the CREATE TABLE statement is to leave the rootpage
81756         ** of the new table in register pParse->regRoot. This is important
81757         ** because the OpenWrite opcode below will be needing it. */
81758         sqlite3NestedParse(pParse,
81759             "CREATE TABLE %Q.%s(%s)", pDb->zName, zTab, aTable[i].zCols
81760         );
81761         aRoot[i] = pParse->regRoot;
81762         aCreateTbl[i] = OPFLAG_P2ISREG;
81763       }
81764     }else{
81765       /* The table already exists. If zWhere is not NULL, delete all entries
81766       ** associated with the table zWhere. If zWhere is NULL, delete the
81767       ** entire contents of the table. */
81768       aRoot[i] = pStat->tnum;
81769       aCreateTbl[i] = 0;
81770       sqlite3TableLock(pParse, iDb, aRoot[i], 1, zTab);
81771       if( zWhere ){
81772         sqlite3NestedParse(pParse,
81773            "DELETE FROM %Q.%s WHERE %s=%Q",
81774            pDb->zName, zTab, zWhereType, zWhere
81775         );
81776       }else{
81777         /* The sqlite_stat[134] table already exists.  Delete all rows. */
81778         sqlite3VdbeAddOp2(v, OP_Clear, aRoot[i], iDb);
81779       }
81780     }
81781   }
81782 
81783   /* Open the sqlite_stat[134] tables for writing. */
81784   for(i=0; aTable[i].zCols; i++){
81785     assert( i<ArraySize(aTable) );
81786     sqlite3VdbeAddOp4Int(v, OP_OpenWrite, iStatCur+i, aRoot[i], iDb, 3);
81787     sqlite3VdbeChangeP5(v, aCreateTbl[i]);
81788   }
81789 }
81790 
81791 /*
81792 ** Recommended number of samples for sqlite_stat4
81793 */
81794 #ifndef SQLITE_STAT4_SAMPLES
81795 # define SQLITE_STAT4_SAMPLES 24
81796 #endif
81797 
81798 /*
81799 ** Three SQL functions - stat_init(), stat_push(), and stat_get() -
81800 ** share an instance of the following structure to hold their state
81801 ** information.
81802 */
81803 typedef struct Stat4Accum Stat4Accum;
81804 typedef struct Stat4Sample Stat4Sample;
81805 struct Stat4Sample {
81806   tRowcnt *anEq;                  /* sqlite_stat4.nEq */
81807   tRowcnt *anDLt;                 /* sqlite_stat4.nDLt */
81808 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
81809   tRowcnt *anLt;                  /* sqlite_stat4.nLt */
81810   union {
81811     i64 iRowid;                     /* Rowid in main table of the key */
81812     u8 *aRowid;                     /* Key for WITHOUT ROWID tables */
81813   } u;
81814   u32 nRowid;                     /* Sizeof aRowid[] */
81815   u8 isPSample;                   /* True if a periodic sample */
81816   int iCol;                       /* If !isPSample, the reason for inclusion */
81817   u32 iHash;                      /* Tiebreaker hash */
81818 #endif
81819 };
81820 struct Stat4Accum {
81821   tRowcnt nRow;             /* Number of rows in the entire table */
81822   tRowcnt nPSample;         /* How often to do a periodic sample */
81823   int nCol;                 /* Number of columns in index + rowid */
81824   int mxSample;             /* Maximum number of samples to accumulate */
81825   Stat4Sample current;      /* Current row as a Stat4Sample */
81826   u32 iPrn;                 /* Pseudo-random number used for sampling */
81827   Stat4Sample *aBest;       /* Array of nCol best samples */
81828   int iMin;                 /* Index in a[] of entry with minimum score */
81829   int nSample;              /* Current number of samples */
81830   int iGet;                 /* Index of current sample accessed by stat_get() */
81831   Stat4Sample *a;           /* Array of mxSample Stat4Sample objects */
81832   sqlite3 *db;              /* Database connection, for malloc() */
81833 };
81834 
81835 /* Reclaim memory used by a Stat4Sample
81836 */
81837 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
81838 static void sampleClear(sqlite3 *db, Stat4Sample *p){
81839   assert( db!=0 );
81840   if( p->nRowid ){
81841     sqlite3DbFree(db, p->u.aRowid);
81842     p->nRowid = 0;
81843   }
81844 }
81845 #endif
81846 
81847 /* Initialize the BLOB value of a ROWID
81848 */
81849 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
81850 static void sampleSetRowid(sqlite3 *db, Stat4Sample *p, int n, const u8 *pData){
81851   assert( db!=0 );
81852   if( p->nRowid ) sqlite3DbFree(db, p->u.aRowid);
81853   p->u.aRowid = sqlite3DbMallocRaw(db, n);
81854   if( p->u.aRowid ){
81855     p->nRowid = n;
81856     memcpy(p->u.aRowid, pData, n);
81857   }else{
81858     p->nRowid = 0;
81859   }
81860 }
81861 #endif
81862 
81863 /* Initialize the INTEGER value of a ROWID.
81864 */
81865 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
81866 static void sampleSetRowidInt64(sqlite3 *db, Stat4Sample *p, i64 iRowid){
81867   assert( db!=0 );
81868   if( p->nRowid ) sqlite3DbFree(db, p->u.aRowid);
81869   p->nRowid = 0;
81870   p->u.iRowid = iRowid;
81871 }
81872 #endif
81873 
81874 
81875 /*
81876 ** Copy the contents of object (*pFrom) into (*pTo).
81877 */
81878 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
81879 static void sampleCopy(Stat4Accum *p, Stat4Sample *pTo, Stat4Sample *pFrom){
81880   pTo->isPSample = pFrom->isPSample;
81881   pTo->iCol = pFrom->iCol;
81882   pTo->iHash = pFrom->iHash;
81883   memcpy(pTo->anEq, pFrom->anEq, sizeof(tRowcnt)*p->nCol);
81884   memcpy(pTo->anLt, pFrom->anLt, sizeof(tRowcnt)*p->nCol);
81885   memcpy(pTo->anDLt, pFrom->anDLt, sizeof(tRowcnt)*p->nCol);
81886   if( pFrom->nRowid ){
81887     sampleSetRowid(p->db, pTo, pFrom->nRowid, pFrom->u.aRowid);
81888   }else{
81889     sampleSetRowidInt64(p->db, pTo, pFrom->u.iRowid);
81890   }
81891 }
81892 #endif
81893 
81894 /*
81895 ** Reclaim all memory of a Stat4Accum structure.
81896 */
81897 static void stat4Destructor(void *pOld){
81898   Stat4Accum *p = (Stat4Accum*)pOld;
81899 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
81900   int i;
81901   for(i=0; i<p->nCol; i++) sampleClear(p->db, p->aBest+i);
81902   for(i=0; i<p->mxSample; i++) sampleClear(p->db, p->a+i);
81903   sampleClear(p->db, &p->current);
81904 #endif
81905   sqlite3DbFree(p->db, p);
81906 }
81907 
81908 /*
81909 ** Implementation of the stat_init(N,C) SQL function. The two parameters
81910 ** are the number of rows in the table or index (C) and the number of columns
81911 ** in the index (N).  The second argument (C) is only used for STAT3 and STAT4.
81912 **
81913 ** This routine allocates the Stat4Accum object in heap memory. The return
81914 ** value is a pointer to the the Stat4Accum object encoded as a blob (i.e.
81915 ** the size of the blob is sizeof(void*) bytes).
81916 */
81917 static void statInit(
81918   sqlite3_context *context,
81919   int argc,
81920   sqlite3_value **argv
81921 ){
81922   Stat4Accum *p;
81923   int nCol;                       /* Number of columns in index being sampled */
81924   int nColUp;                     /* nCol rounded up for alignment */
81925   int n;                          /* Bytes of space to allocate */
81926   sqlite3 *db;                    /* Database connection */
81927 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
81928   int mxSample = SQLITE_STAT4_SAMPLES;
81929 #endif
81930 
81931   /* Decode the three function arguments */
81932   UNUSED_PARAMETER(argc);
81933   nCol = sqlite3_value_int(argv[0]);
81934   assert( nCol>1 );               /* >1 because it includes the rowid column */
81935   nColUp = sizeof(tRowcnt)<8 ? (nCol+1)&~1 : nCol;
81936 
81937   /* Allocate the space required for the Stat4Accum object */
81938   n = sizeof(*p)
81939     + sizeof(tRowcnt)*nColUp                  /* Stat4Accum.anEq */
81940     + sizeof(tRowcnt)*nColUp                  /* Stat4Accum.anDLt */
81941 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
81942     + sizeof(tRowcnt)*nColUp                  /* Stat4Accum.anLt */
81943     + sizeof(Stat4Sample)*(nCol+mxSample)     /* Stat4Accum.aBest[], a[] */
81944     + sizeof(tRowcnt)*3*nColUp*(nCol+mxSample)
81945 #endif
81946   ;
81947   db = sqlite3_context_db_handle(context);
81948   p = sqlite3DbMallocZero(db, n);
81949   if( p==0 ){
81950     sqlite3_result_error_nomem(context);
81951     return;
81952   }
81953 
81954   p->db = db;
81955   p->nRow = 0;
81956   p->nCol = nCol;
81957   p->current.anDLt = (tRowcnt*)&p[1];
81958   p->current.anEq = &p->current.anDLt[nColUp];
81959 
81960 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
81961   {
81962     u8 *pSpace;                     /* Allocated space not yet assigned */
81963     int i;                          /* Used to iterate through p->aSample[] */
81964 
81965     p->iGet = -1;
81966     p->mxSample = mxSample;
81967     p->nPSample = (tRowcnt)(sqlite3_value_int64(argv[1])/(mxSample/3+1) + 1);
81968     p->current.anLt = &p->current.anEq[nColUp];
81969     p->iPrn = nCol*0x689e962d ^ sqlite3_value_int(argv[1])*0xd0944565;
81970 
81971     /* Set up the Stat4Accum.a[] and aBest[] arrays */
81972     p->a = (struct Stat4Sample*)&p->current.anLt[nColUp];
81973     p->aBest = &p->a[mxSample];
81974     pSpace = (u8*)(&p->a[mxSample+nCol]);
81975     for(i=0; i<(mxSample+nCol); i++){
81976       p->a[i].anEq = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp);
81977       p->a[i].anLt = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp);
81978       p->a[i].anDLt = (tRowcnt *)pSpace; pSpace += (sizeof(tRowcnt) * nColUp);
81979     }
81980     assert( (pSpace - (u8*)p)==n );
81981 
81982     for(i=0; i<nCol; i++){
81983       p->aBest[i].iCol = i;
81984     }
81985   }
81986 #endif
81987 
81988   /* Return a pointer to the allocated object to the caller */
81989   sqlite3_result_blob(context, p, sizeof(p), stat4Destructor);
81990 }
81991 static const FuncDef statInitFuncdef = {
81992   1+IsStat34,      /* nArg */
81993   SQLITE_UTF8,     /* funcFlags */
81994   0,               /* pUserData */
81995   0,               /* pNext */
81996   statInit,        /* xFunc */
81997   0,               /* xStep */
81998   0,               /* xFinalize */
81999   "stat_init",     /* zName */
82000   0,               /* pHash */
82001   0                /* pDestructor */
82002 };
82003 
82004 #ifdef SQLITE_ENABLE_STAT4
82005 /*
82006 ** pNew and pOld are both candidate non-periodic samples selected for
82007 ** the same column (pNew->iCol==pOld->iCol). Ignoring this column and
82008 ** considering only any trailing columns and the sample hash value, this
82009 ** function returns true if sample pNew is to be preferred over pOld.
82010 ** In other words, if we assume that the cardinalities of the selected
82011 ** column for pNew and pOld are equal, is pNew to be preferred over pOld.
82012 **
82013 ** This function assumes that for each argument sample, the contents of
82014 ** the anEq[] array from pSample->anEq[pSample->iCol+1] onwards are valid.
82015 */
82016 static int sampleIsBetterPost(
82017   Stat4Accum *pAccum,
82018   Stat4Sample *pNew,
82019   Stat4Sample *pOld
82020 ){
82021   int nCol = pAccum->nCol;
82022   int i;
82023   assert( pNew->iCol==pOld->iCol );
82024   for(i=pNew->iCol+1; i<nCol; i++){
82025     if( pNew->anEq[i]>pOld->anEq[i] ) return 1;
82026     if( pNew->anEq[i]<pOld->anEq[i] ) return 0;
82027   }
82028   if( pNew->iHash>pOld->iHash ) return 1;
82029   return 0;
82030 }
82031 #endif
82032 
82033 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
82034 /*
82035 ** Return true if pNew is to be preferred over pOld.
82036 **
82037 ** This function assumes that for each argument sample, the contents of
82038 ** the anEq[] array from pSample->anEq[pSample->iCol] onwards are valid.
82039 */
82040 static int sampleIsBetter(
82041   Stat4Accum *pAccum,
82042   Stat4Sample *pNew,
82043   Stat4Sample *pOld
82044 ){
82045   tRowcnt nEqNew = pNew->anEq[pNew->iCol];
82046   tRowcnt nEqOld = pOld->anEq[pOld->iCol];
82047 
82048   assert( pOld->isPSample==0 && pNew->isPSample==0 );
82049   assert( IsStat4 || (pNew->iCol==0 && pOld->iCol==0) );
82050 
82051   if( (nEqNew>nEqOld) ) return 1;
82052 #ifdef SQLITE_ENABLE_STAT4
82053   if( nEqNew==nEqOld ){
82054     if( pNew->iCol<pOld->iCol ) return 1;
82055     return (pNew->iCol==pOld->iCol && sampleIsBetterPost(pAccum, pNew, pOld));
82056   }
82057   return 0;
82058 #else
82059   return (nEqNew==nEqOld && pNew->iHash>pOld->iHash);
82060 #endif
82061 }
82062 
82063 /*
82064 ** Copy the contents of sample *pNew into the p->a[] array. If necessary,
82065 ** remove the least desirable sample from p->a[] to make room.
82066 */
82067 static void sampleInsert(Stat4Accum *p, Stat4Sample *pNew, int nEqZero){
82068   Stat4Sample *pSample = 0;
82069   int i;
82070 
82071   assert( IsStat4 || nEqZero==0 );
82072 
82073 #ifdef SQLITE_ENABLE_STAT4
82074   if( pNew->isPSample==0 ){
82075     Stat4Sample *pUpgrade = 0;
82076     assert( pNew->anEq[pNew->iCol]>0 );
82077 
82078     /* This sample is being added because the prefix that ends in column
82079     ** iCol occurs many times in the table. However, if we have already
82080     ** added a sample that shares this prefix, there is no need to add
82081     ** this one. Instead, upgrade the priority of the highest priority
82082     ** existing sample that shares this prefix.  */
82083     for(i=p->nSample-1; i>=0; i--){
82084       Stat4Sample *pOld = &p->a[i];
82085       if( pOld->anEq[pNew->iCol]==0 ){
82086         if( pOld->isPSample ) return;
82087         assert( pOld->iCol>pNew->iCol );
82088         assert( sampleIsBetter(p, pNew, pOld) );
82089         if( pUpgrade==0 || sampleIsBetter(p, pOld, pUpgrade) ){
82090           pUpgrade = pOld;
82091         }
82092       }
82093     }
82094     if( pUpgrade ){
82095       pUpgrade->iCol = pNew->iCol;
82096       pUpgrade->anEq[pUpgrade->iCol] = pNew->anEq[pUpgrade->iCol];
82097       goto find_new_min;
82098     }
82099   }
82100 #endif
82101 
82102   /* If necessary, remove sample iMin to make room for the new sample. */
82103   if( p->nSample>=p->mxSample ){
82104     Stat4Sample *pMin = &p->a[p->iMin];
82105     tRowcnt *anEq = pMin->anEq;
82106     tRowcnt *anLt = pMin->anLt;
82107     tRowcnt *anDLt = pMin->anDLt;
82108     sampleClear(p->db, pMin);
82109     memmove(pMin, &pMin[1], sizeof(p->a[0])*(p->nSample-p->iMin-1));
82110     pSample = &p->a[p->nSample-1];
82111     pSample->nRowid = 0;
82112     pSample->anEq = anEq;
82113     pSample->anDLt = anDLt;
82114     pSample->anLt = anLt;
82115     p->nSample = p->mxSample-1;
82116   }
82117 
82118   /* The "rows less-than" for the rowid column must be greater than that
82119   ** for the last sample in the p->a[] array. Otherwise, the samples would
82120   ** be out of order. */
82121 #ifdef SQLITE_ENABLE_STAT4
82122   assert( p->nSample==0
82123        || pNew->anLt[p->nCol-1] > p->a[p->nSample-1].anLt[p->nCol-1] );
82124 #endif
82125 
82126   /* Insert the new sample */
82127   pSample = &p->a[p->nSample];
82128   sampleCopy(p, pSample, pNew);
82129   p->nSample++;
82130 
82131   /* Zero the first nEqZero entries in the anEq[] array. */
82132   memset(pSample->anEq, 0, sizeof(tRowcnt)*nEqZero);
82133 
82134 #ifdef SQLITE_ENABLE_STAT4
82135  find_new_min:
82136 #endif
82137   if( p->nSample>=p->mxSample ){
82138     int iMin = -1;
82139     for(i=0; i<p->mxSample; i++){
82140       if( p->a[i].isPSample ) continue;
82141       if( iMin<0 || sampleIsBetter(p, &p->a[iMin], &p->a[i]) ){
82142         iMin = i;
82143       }
82144     }
82145     assert( iMin>=0 );
82146     p->iMin = iMin;
82147   }
82148 }
82149 #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
82150 
82151 /*
82152 ** Field iChng of the index being scanned has changed. So at this point
82153 ** p->current contains a sample that reflects the previous row of the
82154 ** index. The value of anEq[iChng] and subsequent anEq[] elements are
82155 ** correct at this point.
82156 */
82157 static void samplePushPrevious(Stat4Accum *p, int iChng){
82158 #ifdef SQLITE_ENABLE_STAT4
82159   int i;
82160 
82161   /* Check if any samples from the aBest[] array should be pushed
82162   ** into IndexSample.a[] at this point.  */
82163   for(i=(p->nCol-2); i>=iChng; i--){
82164     Stat4Sample *pBest = &p->aBest[i];
82165     pBest->anEq[i] = p->current.anEq[i];
82166     if( p->nSample<p->mxSample || sampleIsBetter(p, pBest, &p->a[p->iMin]) ){
82167       sampleInsert(p, pBest, i);
82168     }
82169   }
82170 
82171   /* Update the anEq[] fields of any samples already collected. */
82172   for(i=p->nSample-1; i>=0; i--){
82173     int j;
82174     for(j=iChng; j<p->nCol; j++){
82175       if( p->a[i].anEq[j]==0 ) p->a[i].anEq[j] = p->current.anEq[j];
82176     }
82177   }
82178 #endif
82179 
82180 #if defined(SQLITE_ENABLE_STAT3) && !defined(SQLITE_ENABLE_STAT4)
82181   if( iChng==0 ){
82182     tRowcnt nLt = p->current.anLt[0];
82183     tRowcnt nEq = p->current.anEq[0];
82184 
82185     /* Check if this is to be a periodic sample. If so, add it. */
82186     if( (nLt/p->nPSample)!=(nLt+nEq)/p->nPSample ){
82187       p->current.isPSample = 1;
82188       sampleInsert(p, &p->current, 0);
82189       p->current.isPSample = 0;
82190     }else
82191 
82192     /* Or if it is a non-periodic sample. Add it in this case too. */
82193     if( p->nSample<p->mxSample
82194      || sampleIsBetter(p, &p->current, &p->a[p->iMin])
82195     ){
82196       sampleInsert(p, &p->current, 0);
82197     }
82198   }
82199 #endif
82200 
82201 #ifndef SQLITE_ENABLE_STAT3_OR_STAT4
82202   UNUSED_PARAMETER( p );
82203   UNUSED_PARAMETER( iChng );
82204 #endif
82205 }
82206 
82207 /*
82208 ** Implementation of the stat_push SQL function:  stat_push(P,C,R)
82209 ** Arguments:
82210 **
82211 **    P     Pointer to the Stat4Accum object created by stat_init()
82212 **    C     Index of left-most column to differ from previous row
82213 **    R     Rowid for the current row.  Might be a key record for
82214 **          WITHOUT ROWID tables.
82215 **
82216 ** The SQL function always returns NULL.
82217 **
82218 ** The R parameter is only used for STAT3 and STAT4
82219 */
82220 static void statPush(
82221   sqlite3_context *context,
82222   int argc,
82223   sqlite3_value **argv
82224 ){
82225   int i;
82226 
82227   /* The three function arguments */
82228   Stat4Accum *p = (Stat4Accum*)sqlite3_value_blob(argv[0]);
82229   int iChng = sqlite3_value_int(argv[1]);
82230 
82231   UNUSED_PARAMETER( argc );
82232   UNUSED_PARAMETER( context );
82233   assert( p->nCol>1 );        /* Includes rowid field */
82234   assert( iChng<p->nCol );
82235 
82236   if( p->nRow==0 ){
82237     /* This is the first call to this function. Do initialization. */
82238     for(i=0; i<p->nCol; i++) p->current.anEq[i] = 1;
82239   }else{
82240     /* Second and subsequent calls get processed here */
82241     samplePushPrevious(p, iChng);
82242 
82243     /* Update anDLt[], anLt[] and anEq[] to reflect the values that apply
82244     ** to the current row of the index. */
82245     for(i=0; i<iChng; i++){
82246       p->current.anEq[i]++;
82247     }
82248     for(i=iChng; i<p->nCol; i++){
82249       p->current.anDLt[i]++;
82250 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
82251       p->current.anLt[i] += p->current.anEq[i];
82252 #endif
82253       p->current.anEq[i] = 1;
82254     }
82255   }
82256   p->nRow++;
82257 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
82258   if( sqlite3_value_type(argv[2])==SQLITE_INTEGER ){
82259     sampleSetRowidInt64(p->db, &p->current, sqlite3_value_int64(argv[2]));
82260   }else{
82261     sampleSetRowid(p->db, &p->current, sqlite3_value_bytes(argv[2]),
82262                                        sqlite3_value_blob(argv[2]));
82263   }
82264   p->current.iHash = p->iPrn = p->iPrn*1103515245 + 12345;
82265 #endif
82266 
82267 #ifdef SQLITE_ENABLE_STAT4
82268   {
82269     tRowcnt nLt = p->current.anLt[p->nCol-1];
82270 
82271     /* Check if this is to be a periodic sample. If so, add it. */
82272     if( (nLt/p->nPSample)!=(nLt+1)/p->nPSample ){
82273       p->current.isPSample = 1;
82274       p->current.iCol = 0;
82275       sampleInsert(p, &p->current, p->nCol-1);
82276       p->current.isPSample = 0;
82277     }
82278 
82279     /* Update the aBest[] array. */
82280     for(i=0; i<(p->nCol-1); i++){
82281       p->current.iCol = i;
82282       if( i>=iChng || sampleIsBetterPost(p, &p->current, &p->aBest[i]) ){
82283         sampleCopy(p, &p->aBest[i], &p->current);
82284       }
82285     }
82286   }
82287 #endif
82288 }
82289 static const FuncDef statPushFuncdef = {
82290   2+IsStat34,      /* nArg */
82291   SQLITE_UTF8,     /* funcFlags */
82292   0,               /* pUserData */
82293   0,               /* pNext */
82294   statPush,        /* xFunc */
82295   0,               /* xStep */
82296   0,               /* xFinalize */
82297   "stat_push",     /* zName */
82298   0,               /* pHash */
82299   0                /* pDestructor */
82300 };
82301 
82302 #define STAT_GET_STAT1 0          /* "stat" column of stat1 table */
82303 #define STAT_GET_ROWID 1          /* "rowid" column of stat[34] entry */
82304 #define STAT_GET_NEQ   2          /* "neq" column of stat[34] entry */
82305 #define STAT_GET_NLT   3          /* "nlt" column of stat[34] entry */
82306 #define STAT_GET_NDLT  4          /* "ndlt" column of stat[34] entry */
82307 
82308 /*
82309 ** Implementation of the stat_get(P,J) SQL function.  This routine is
82310 ** used to query the results.  Content is returned for parameter J
82311 ** which is one of the STAT_GET_xxxx values defined above.
82312 **
82313 ** If neither STAT3 nor STAT4 are enabled, then J is always
82314 ** STAT_GET_STAT1 and is hence omitted and this routine becomes
82315 ** a one-parameter function, stat_get(P), that always returns the
82316 ** stat1 table entry information.
82317 */
82318 static void statGet(
82319   sqlite3_context *context,
82320   int argc,
82321   sqlite3_value **argv
82322 ){
82323   Stat4Accum *p = (Stat4Accum*)sqlite3_value_blob(argv[0]);
82324 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
82325   /* STAT3 and STAT4 have a parameter on this routine. */
82326   int eCall = sqlite3_value_int(argv[1]);
82327   assert( argc==2 );
82328   assert( eCall==STAT_GET_STAT1 || eCall==STAT_GET_NEQ
82329        || eCall==STAT_GET_ROWID || eCall==STAT_GET_NLT
82330        || eCall==STAT_GET_NDLT
82331   );
82332   if( eCall==STAT_GET_STAT1 )
82333 #else
82334   assert( argc==1 );
82335 #endif
82336   {
82337     /* Return the value to store in the "stat" column of the sqlite_stat1
82338     ** table for this index.
82339     **
82340     ** The value is a string composed of a list of integers describing
82341     ** the index. The first integer in the list is the total number of
82342     ** entries in the index. There is one additional integer in the list
82343     ** for each indexed column. This additional integer is an estimate of
82344     ** the number of rows matched by a stabbing query on the index using
82345     ** a key with the corresponding number of fields. In other words,
82346     ** if the index is on columns (a,b) and the sqlite_stat1 value is
82347     ** "100 10 2", then SQLite estimates that:
82348     **
82349     **   * the index contains 100 rows,
82350     **   * "WHERE a=?" matches 10 rows, and
82351     **   * "WHERE a=? AND b=?" matches 2 rows.
82352     **
82353     ** If D is the count of distinct values and K is the total number of
82354     ** rows, then each estimate is computed as:
82355     **
82356     **        I = (K+D-1)/D
82357     */
82358     char *z;
82359     int i;
82360 
82361     char *zRet = sqlite3MallocZero(p->nCol * 25);
82362     if( zRet==0 ){
82363       sqlite3_result_error_nomem(context);
82364       return;
82365     }
82366 
82367     sqlite3_snprintf(24, zRet, "%llu", (u64)p->nRow);
82368     z = zRet + sqlite3Strlen30(zRet);
82369     for(i=0; i<(p->nCol-1); i++){
82370       u64 nDistinct = p->current.anDLt[i] + 1;
82371       u64 iVal = (p->nRow + nDistinct - 1) / nDistinct;
82372       sqlite3_snprintf(24, z, " %llu", iVal);
82373       z += sqlite3Strlen30(z);
82374       assert( p->current.anEq[i] );
82375     }
82376     assert( z[0]=='\0' && z>zRet );
82377 
82378     sqlite3_result_text(context, zRet, -1, sqlite3_free);
82379   }
82380 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
82381   else if( eCall==STAT_GET_ROWID ){
82382     if( p->iGet<0 ){
82383       samplePushPrevious(p, 0);
82384       p->iGet = 0;
82385     }
82386     if( p->iGet<p->nSample ){
82387       Stat4Sample *pS = p->a + p->iGet;
82388       if( pS->nRowid==0 ){
82389         sqlite3_result_int64(context, pS->u.iRowid);
82390       }else{
82391         sqlite3_result_blob(context, pS->u.aRowid, pS->nRowid,
82392                             SQLITE_TRANSIENT);
82393       }
82394     }
82395   }else{
82396     tRowcnt *aCnt = 0;
82397 
82398     assert( p->iGet<p->nSample );
82399     switch( eCall ){
82400       case STAT_GET_NEQ:  aCnt = p->a[p->iGet].anEq; break;
82401       case STAT_GET_NLT:  aCnt = p->a[p->iGet].anLt; break;
82402       default: {
82403         aCnt = p->a[p->iGet].anDLt;
82404         p->iGet++;
82405         break;
82406       }
82407     }
82408 
82409     if( IsStat3 ){
82410       sqlite3_result_int64(context, (i64)aCnt[0]);
82411     }else{
82412       char *zRet = sqlite3MallocZero(p->nCol * 25);
82413       if( zRet==0 ){
82414         sqlite3_result_error_nomem(context);
82415       }else{
82416         int i;
82417         char *z = zRet;
82418         for(i=0; i<p->nCol; i++){
82419           sqlite3_snprintf(24, z, "%llu ", (u64)aCnt[i]);
82420           z += sqlite3Strlen30(z);
82421         }
82422         assert( z[0]=='\0' && z>zRet );
82423         z[-1] = '\0';
82424         sqlite3_result_text(context, zRet, -1, sqlite3_free);
82425       }
82426     }
82427   }
82428 #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
82429 #ifndef SQLITE_DEBUG
82430   UNUSED_PARAMETER( argc );
82431 #endif
82432 }
82433 static const FuncDef statGetFuncdef = {
82434   1+IsStat34,      /* nArg */
82435   SQLITE_UTF8,     /* funcFlags */
82436   0,               /* pUserData */
82437   0,               /* pNext */
82438   statGet,         /* xFunc */
82439   0,               /* xStep */
82440   0,               /* xFinalize */
82441   "stat_get",      /* zName */
82442   0,               /* pHash */
82443   0                /* pDestructor */
82444 };
82445 
82446 static void callStatGet(Vdbe *v, int regStat4, int iParam, int regOut){
82447   assert( regOut!=regStat4 && regOut!=regStat4+1 );
82448 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
82449   sqlite3VdbeAddOp2(v, OP_Integer, iParam, regStat4+1);
82450 #elif SQLITE_DEBUG
82451   assert( iParam==STAT_GET_STAT1 );
82452 #else
82453   UNUSED_PARAMETER( iParam );
82454 #endif
82455   sqlite3VdbeAddOp3(v, OP_Function, 0, regStat4, regOut);
82456   sqlite3VdbeChangeP4(v, -1, (char*)&statGetFuncdef, P4_FUNCDEF);
82457   sqlite3VdbeChangeP5(v, 1 + IsStat34);
82458 }
82459 
82460 /*
82461 ** Generate code to do an analysis of all indices associated with
82462 ** a single table.
82463 */
82464 static void analyzeOneTable(
82465   Parse *pParse,   /* Parser context */
82466   Table *pTab,     /* Table whose indices are to be analyzed */
82467   Index *pOnlyIdx, /* If not NULL, only analyze this one index */
82468   int iStatCur,    /* Index of VdbeCursor that writes the sqlite_stat1 table */
82469   int iMem,        /* Available memory locations begin here */
82470   int iTab         /* Next available cursor */
82471 ){
82472   sqlite3 *db = pParse->db;    /* Database handle */
82473   Index *pIdx;                 /* An index to being analyzed */
82474   int iIdxCur;                 /* Cursor open on index being analyzed */
82475   int iTabCur;                 /* Table cursor */
82476   Vdbe *v;                     /* The virtual machine being built up */
82477   int i;                       /* Loop counter */
82478   int jZeroRows = -1;          /* Jump from here if number of rows is zero */
82479   int iDb;                     /* Index of database containing pTab */
82480   u8 needTableCnt = 1;         /* True to count the table */
82481   int regNewRowid = iMem++;    /* Rowid for the inserted record */
82482   int regStat4 = iMem++;       /* Register to hold Stat4Accum object */
82483   int regChng = iMem++;        /* Index of changed index field */
82484 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
82485   int regRowid = iMem++;       /* Rowid argument passed to stat_push() */
82486 #endif
82487   int regTemp = iMem++;        /* Temporary use register */
82488   int regTabname = iMem++;     /* Register containing table name */
82489   int regIdxname = iMem++;     /* Register containing index name */
82490   int regStat1 = iMem++;       /* Value for the stat column of sqlite_stat1 */
82491   int regPrev = iMem;          /* MUST BE LAST (see below) */
82492 
82493   pParse->nMem = MAX(pParse->nMem, iMem);
82494   v = sqlite3GetVdbe(pParse);
82495   if( v==0 || NEVER(pTab==0) ){
82496     return;
82497   }
82498   if( pTab->tnum==0 ){
82499     /* Do not gather statistics on views or virtual tables */
82500     return;
82501   }
82502   if( sqlite3_strnicmp(pTab->zName, "sqlite_", 7)==0 ){
82503     /* Do not gather statistics on system tables */
82504     return;
82505   }
82506   assert( sqlite3BtreeHoldsAllMutexes(db) );
82507   iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
82508   assert( iDb>=0 );
82509   assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
82510 #ifndef SQLITE_OMIT_AUTHORIZATION
82511   if( sqlite3AuthCheck(pParse, SQLITE_ANALYZE, pTab->zName, 0,
82512       db->aDb[iDb].zName ) ){
82513     return;
82514   }
82515 #endif
82516 
82517   /* Establish a read-lock on the table at the shared-cache level.
82518   ** Open a read-only cursor on the table. Also allocate a cursor number
82519   ** to use for scanning indexes (iIdxCur). No index cursor is opened at
82520   ** this time though.  */
82521   sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
82522   iTabCur = iTab++;
82523   iIdxCur = iTab++;
82524   pParse->nTab = MAX(pParse->nTab, iTab);
82525   sqlite3OpenTable(pParse, iTabCur, iDb, pTab, OP_OpenRead);
82526   sqlite3VdbeAddOp4(v, OP_String8, 0, regTabname, 0, pTab->zName, 0);
82527 
82528   for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
82529     int nCol;                     /* Number of columns indexed by pIdx */
82530     int *aGotoChng;               /* Array of jump instruction addresses */
82531     int addrRewind;               /* Address of "OP_Rewind iIdxCur" */
82532     int addrGotoChng0;            /* Address of "Goto addr_chng_0" */
82533     int addrNextRow;              /* Address of "next_row:" */
82534     const char *zIdxName;         /* Name of the index */
82535 
82536     if( pOnlyIdx && pOnlyIdx!=pIdx ) continue;
82537     if( pIdx->pPartIdxWhere==0 ) needTableCnt = 0;
82538     VdbeNoopComment((v, "Begin analysis of %s", pIdx->zName));
82539     nCol = pIdx->nKeyCol;
82540     aGotoChng = sqlite3DbMallocRaw(db, sizeof(int)*(nCol+1));
82541     if( aGotoChng==0 ) continue;
82542 
82543     /* Populate the register containing the index name. */
82544     if( pIdx->autoIndex==2 && !HasRowid(pTab) ){
82545       zIdxName = pTab->zName;
82546     }else{
82547       zIdxName = pIdx->zName;
82548     }
82549     sqlite3VdbeAddOp4(v, OP_String8, 0, regIdxname, 0, zIdxName, 0);
82550 
82551     /*
82552     ** Pseudo-code for loop that calls stat_push():
82553     **
82554     **   Rewind csr
82555     **   if eof(csr) goto end_of_scan;
82556     **   regChng = 0
82557     **   goto chng_addr_0;
82558     **
82559     **  next_row:
82560     **   regChng = 0
82561     **   if( idx(0) != regPrev(0) ) goto chng_addr_0
82562     **   regChng = 1
82563     **   if( idx(1) != regPrev(1) ) goto chng_addr_1
82564     **   ...
82565     **   regChng = N
82566     **   goto chng_addr_N
82567     **
82568     **  chng_addr_0:
82569     **   regPrev(0) = idx(0)
82570     **  chng_addr_1:
82571     **   regPrev(1) = idx(1)
82572     **  ...
82573     **
82574     **  chng_addr_N:
82575     **   regRowid = idx(rowid)
82576     **   stat_push(P, regChng, regRowid)
82577     **   Next csr
82578     **   if !eof(csr) goto next_row;
82579     **
82580     **  end_of_scan:
82581     */
82582 
82583     /* Make sure there are enough memory cells allocated to accommodate
82584     ** the regPrev array and a trailing rowid (the rowid slot is required
82585     ** when building a record to insert into the sample column of
82586     ** the sqlite_stat4 table.  */
82587     pParse->nMem = MAX(pParse->nMem, regPrev+nCol);
82588 
82589     /* Open a read-only cursor on the index being analyzed. */
82590     assert( iDb==sqlite3SchemaToIndex(db, pIdx->pSchema) );
82591     sqlite3VdbeAddOp3(v, OP_OpenRead, iIdxCur, pIdx->tnum, iDb);
82592     sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
82593     VdbeComment((v, "%s", pIdx->zName));
82594 
82595     /* Invoke the stat_init() function. The arguments are:
82596     **
82597     **    (1) the number of columns in the index including the rowid,
82598     **    (2) the number of rows in the index,
82599     **
82600     ** The second argument is only used for STAT3 and STAT4
82601     */
82602 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
82603     sqlite3VdbeAddOp2(v, OP_Count, iIdxCur, regStat4+2);
82604 #endif
82605     sqlite3VdbeAddOp2(v, OP_Integer, nCol+1, regStat4+1);
82606     sqlite3VdbeAddOp3(v, OP_Function, 0, regStat4+1, regStat4);
82607     sqlite3VdbeChangeP4(v, -1, (char*)&statInitFuncdef, P4_FUNCDEF);
82608     sqlite3VdbeChangeP5(v, 1+IsStat34);
82609 
82610     /* Implementation of the following:
82611     **
82612     **   Rewind csr
82613     **   if eof(csr) goto end_of_scan;
82614     **   regChng = 0
82615     **   goto next_push_0;
82616     **
82617     */
82618     addrRewind = sqlite3VdbeAddOp1(v, OP_Rewind, iIdxCur);
82619     sqlite3VdbeAddOp2(v, OP_Integer, 0, regChng);
82620     addrGotoChng0 = sqlite3VdbeAddOp0(v, OP_Goto);
82621 
82622     /*
82623     **  next_row:
82624     **   regChng = 0
82625     **   if( idx(0) != regPrev(0) ) goto chng_addr_0
82626     **   regChng = 1
82627     **   if( idx(1) != regPrev(1) ) goto chng_addr_1
82628     **   ...
82629     **   regChng = N
82630     **   goto chng_addr_N
82631     */
82632     addrNextRow = sqlite3VdbeCurrentAddr(v);
82633     for(i=0; i<nCol; i++){
82634       char *pColl = (char*)sqlite3LocateCollSeq(pParse, pIdx->azColl[i]);
82635       sqlite3VdbeAddOp2(v, OP_Integer, i, regChng);
82636       sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, regTemp);
82637       aGotoChng[i] =
82638       sqlite3VdbeAddOp4(v, OP_Ne, regTemp, 0, regPrev+i, pColl, P4_COLLSEQ);
82639       sqlite3VdbeChangeP5(v, SQLITE_NULLEQ);
82640     }
82641     sqlite3VdbeAddOp2(v, OP_Integer, nCol, regChng);
82642     aGotoChng[nCol] = sqlite3VdbeAddOp0(v, OP_Goto);
82643 
82644     /*
82645     **  chng_addr_0:
82646     **   regPrev(0) = idx(0)
82647     **  chng_addr_1:
82648     **   regPrev(1) = idx(1)
82649     **  ...
82650     */
82651     sqlite3VdbeJumpHere(v, addrGotoChng0);
82652     for(i=0; i<nCol; i++){
82653       sqlite3VdbeJumpHere(v, aGotoChng[i]);
82654       sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, i, regPrev+i);
82655     }
82656 
82657     /*
82658     **  chng_addr_N:
82659     **   regRowid = idx(rowid)            // STAT34 only
82660     **   stat_push(P, regChng, regRowid)  // 3rd parameter STAT34 only
82661     **   Next csr
82662     **   if !eof(csr) goto next_row;
82663     */
82664     sqlite3VdbeJumpHere(v, aGotoChng[nCol]);
82665 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
82666     assert( regRowid==(regStat4+2) );
82667     if( HasRowid(pTab) ){
82668       sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur, regRowid);
82669     }else{
82670       Index *pPk = sqlite3PrimaryKeyIndex(pIdx->pTable);
82671       int j, k, regKey;
82672       regKey = sqlite3GetTempRange(pParse, pPk->nKeyCol);
82673       for(j=0; j<pPk->nKeyCol; j++){
82674         k = sqlite3ColumnOfIndex(pIdx, pPk->aiColumn[j]);
82675         sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, k, regKey+j);
82676         VdbeComment((v, "%s", pTab->aCol[pPk->aiColumn[j]].zName));
82677       }
82678       sqlite3VdbeAddOp3(v, OP_MakeRecord, regKey, pPk->nKeyCol, regRowid);
82679       sqlite3ReleaseTempRange(pParse, regKey, pPk->nKeyCol);
82680     }
82681 #endif
82682     assert( regChng==(regStat4+1) );
82683     sqlite3VdbeAddOp3(v, OP_Function, 1, regStat4, regTemp);
82684     sqlite3VdbeChangeP4(v, -1, (char*)&statPushFuncdef, P4_FUNCDEF);
82685     sqlite3VdbeChangeP5(v, 2+IsStat34);
82686     sqlite3VdbeAddOp2(v, OP_Next, iIdxCur, addrNextRow);
82687 
82688     /* Add the entry to the stat1 table. */
82689     callStatGet(v, regStat4, STAT_GET_STAT1, regStat1);
82690     sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regTemp, "aaa", 0);
82691     sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regNewRowid);
82692     sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regTemp, regNewRowid);
82693     sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
82694 
82695     /* Add the entries to the stat3 or stat4 table. */
82696 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
82697     {
82698       int regEq = regStat1;
82699       int regLt = regStat1+1;
82700       int regDLt = regStat1+2;
82701       int regSample = regStat1+3;
82702       int regCol = regStat1+4;
82703       int regSampleRowid = regCol + nCol;
82704       int addrNext;
82705       int addrIsNull;
82706       u8 seekOp = HasRowid(pTab) ? OP_NotExists : OP_NotFound;
82707 
82708       pParse->nMem = MAX(pParse->nMem, regCol+nCol+1);
82709 
82710       addrNext = sqlite3VdbeCurrentAddr(v);
82711       callStatGet(v, regStat4, STAT_GET_ROWID, regSampleRowid);
82712       addrIsNull = sqlite3VdbeAddOp1(v, OP_IsNull, regSampleRowid);
82713       callStatGet(v, regStat4, STAT_GET_NEQ, regEq);
82714       callStatGet(v, regStat4, STAT_GET_NLT, regLt);
82715       callStatGet(v, regStat4, STAT_GET_NDLT, regDLt);
82716       sqlite3VdbeAddOp4Int(v, seekOp, iTabCur, addrNext, regSampleRowid, 0);
82717 #ifdef SQLITE_ENABLE_STAT3
82718       sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur,
82719                                       pIdx->aiColumn[0], regSample);
82720 #else
82721       for(i=0; i<nCol; i++){
82722         i16 iCol = pIdx->aiColumn[i];
82723         sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur, iCol, regCol+i);
82724       }
82725       sqlite3VdbeAddOp3(v, OP_MakeRecord, regCol, nCol+1, regSample);
82726 #endif
82727       sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 6, regTemp, "bbbbbb", 0);
82728       sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur+1, regNewRowid);
82729       sqlite3VdbeAddOp3(v, OP_Insert, iStatCur+1, regTemp, regNewRowid);
82730       sqlite3VdbeAddOp2(v, OP_Goto, 0, addrNext);
82731       sqlite3VdbeJumpHere(v, addrIsNull);
82732     }
82733 #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
82734 
82735     /* End of analysis */
82736     sqlite3VdbeJumpHere(v, addrRewind);
82737     sqlite3DbFree(db, aGotoChng);
82738   }
82739 
82740 
82741   /* Create a single sqlite_stat1 entry containing NULL as the index
82742   ** name and the row count as the content.
82743   */
82744   if( pOnlyIdx==0 && needTableCnt ){
82745     VdbeComment((v, "%s", pTab->zName));
82746     sqlite3VdbeAddOp2(v, OP_Count, iTabCur, regStat1);
82747     jZeroRows = sqlite3VdbeAddOp1(v, OP_IfNot, regStat1);
82748     sqlite3VdbeAddOp2(v, OP_Null, 0, regIdxname);
82749     sqlite3VdbeAddOp4(v, OP_MakeRecord, regTabname, 3, regTemp, "aaa", 0);
82750     sqlite3VdbeAddOp2(v, OP_NewRowid, iStatCur, regNewRowid);
82751     sqlite3VdbeAddOp3(v, OP_Insert, iStatCur, regTemp, regNewRowid);
82752     sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
82753     sqlite3VdbeJumpHere(v, jZeroRows);
82754   }
82755 }
82756 
82757 
82758 /*
82759 ** Generate code that will cause the most recent index analysis to
82760 ** be loaded into internal hash tables where is can be used.
82761 */
82762 static void loadAnalysis(Parse *pParse, int iDb){
82763   Vdbe *v = sqlite3GetVdbe(pParse);
82764   if( v ){
82765     sqlite3VdbeAddOp1(v, OP_LoadAnalysis, iDb);
82766   }
82767 }
82768 
82769 /*
82770 ** Generate code that will do an analysis of an entire database
82771 */
82772 static void analyzeDatabase(Parse *pParse, int iDb){
82773   sqlite3 *db = pParse->db;
82774   Schema *pSchema = db->aDb[iDb].pSchema;    /* Schema of database iDb */
82775   HashElem *k;
82776   int iStatCur;
82777   int iMem;
82778   int iTab;
82779 
82780   sqlite3BeginWriteOperation(pParse, 0, iDb);
82781   iStatCur = pParse->nTab;
82782   pParse->nTab += 3;
82783   openStatTable(pParse, iDb, iStatCur, 0, 0);
82784   iMem = pParse->nMem+1;
82785   iTab = pParse->nTab;
82786   assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
82787   for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){
82788     Table *pTab = (Table*)sqliteHashData(k);
82789     analyzeOneTable(pParse, pTab, 0, iStatCur, iMem, iTab);
82790   }
82791   loadAnalysis(pParse, iDb);
82792 }
82793 
82794 /*
82795 ** Generate code that will do an analysis of a single table in
82796 ** a database.  If pOnlyIdx is not NULL then it is a single index
82797 ** in pTab that should be analyzed.
82798 */
82799 static void analyzeTable(Parse *pParse, Table *pTab, Index *pOnlyIdx){
82800   int iDb;
82801   int iStatCur;
82802 
82803   assert( pTab!=0 );
82804   assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
82805   iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
82806   sqlite3BeginWriteOperation(pParse, 0, iDb);
82807   iStatCur = pParse->nTab;
82808   pParse->nTab += 3;
82809   if( pOnlyIdx ){
82810     openStatTable(pParse, iDb, iStatCur, pOnlyIdx->zName, "idx");
82811   }else{
82812     openStatTable(pParse, iDb, iStatCur, pTab->zName, "tbl");
82813   }
82814   analyzeOneTable(pParse, pTab, pOnlyIdx, iStatCur,pParse->nMem+1,pParse->nTab);
82815   loadAnalysis(pParse, iDb);
82816 }
82817 
82818 /*
82819 ** Generate code for the ANALYZE command.  The parser calls this routine
82820 ** when it recognizes an ANALYZE command.
82821 **
82822 **        ANALYZE                            -- 1
82823 **        ANALYZE  <database>                -- 2
82824 **        ANALYZE  ?<database>.?<tablename>  -- 3
82825 **
82826 ** Form 1 causes all indices in all attached databases to be analyzed.
82827 ** Form 2 analyzes all indices the single database named.
82828 ** Form 3 analyzes all indices associated with the named table.
82829 */
82830 SQLITE_PRIVATE void sqlite3Analyze(Parse *pParse, Token *pName1, Token *pName2){
82831   sqlite3 *db = pParse->db;
82832   int iDb;
82833   int i;
82834   char *z, *zDb;
82835   Table *pTab;
82836   Index *pIdx;
82837   Token *pTableName;
82838 
82839   /* Read the database schema. If an error occurs, leave an error message
82840   ** and code in pParse and return NULL. */
82841   assert( sqlite3BtreeHoldsAllMutexes(pParse->db) );
82842   if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
82843     return;
82844   }
82845 
82846   assert( pName2!=0 || pName1==0 );
82847   if( pName1==0 ){
82848     /* Form 1:  Analyze everything */
82849     for(i=0; i<db->nDb; i++){
82850       if( i==1 ) continue;  /* Do not analyze the TEMP database */
82851       analyzeDatabase(pParse, i);
82852     }
82853   }else if( pName2->n==0 ){
82854     /* Form 2:  Analyze the database or table named */
82855     iDb = sqlite3FindDb(db, pName1);
82856     if( iDb>=0 ){
82857       analyzeDatabase(pParse, iDb);
82858     }else{
82859       z = sqlite3NameFromToken(db, pName1);
82860       if( z ){
82861         if( (pIdx = sqlite3FindIndex(db, z, 0))!=0 ){
82862           analyzeTable(pParse, pIdx->pTable, pIdx);
82863         }else if( (pTab = sqlite3LocateTable(pParse, 0, z, 0))!=0 ){
82864           analyzeTable(pParse, pTab, 0);
82865         }
82866         sqlite3DbFree(db, z);
82867       }
82868     }
82869   }else{
82870     /* Form 3: Analyze the fully qualified table name */
82871     iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pTableName);
82872     if( iDb>=0 ){
82873       zDb = db->aDb[iDb].zName;
82874       z = sqlite3NameFromToken(db, pTableName);
82875       if( z ){
82876         if( (pIdx = sqlite3FindIndex(db, z, zDb))!=0 ){
82877           analyzeTable(pParse, pIdx->pTable, pIdx);
82878         }else if( (pTab = sqlite3LocateTable(pParse, 0, z, zDb))!=0 ){
82879           analyzeTable(pParse, pTab, 0);
82880         }
82881         sqlite3DbFree(db, z);
82882       }
82883     }
82884   }
82885 }
82886 
82887 /*
82888 ** Used to pass information from the analyzer reader through to the
82889 ** callback routine.
82890 */
82891 typedef struct analysisInfo analysisInfo;
82892 struct analysisInfo {
82893   sqlite3 *db;
82894   const char *zDatabase;
82895 };
82896 
82897 /*
82898 ** The first argument points to a nul-terminated string containing a
82899 ** list of space separated integers. Read the first nOut of these into
82900 ** the array aOut[].
82901 */
82902 static void decodeIntArray(
82903   char *zIntArray,       /* String containing int array to decode */
82904   int nOut,              /* Number of slots in aOut[] */
82905   tRowcnt *aOut,         /* Store integers here */
82906   Index *pIndex          /* Handle extra flags for this index, if not NULL */
82907 ){
82908   char *z = zIntArray;
82909   int c;
82910   int i;
82911   tRowcnt v;
82912 
82913 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
82914   if( z==0 ) z = "";
82915 #else
82916   if( NEVER(z==0) ) z = "";
82917 #endif
82918   for(i=0; *z && i<nOut; i++){
82919     v = 0;
82920     while( (c=z[0])>='0' && c<='9' ){
82921       v = v*10 + c - '0';
82922       z++;
82923     }
82924     aOut[i] = v;
82925     if( *z==' ' ) z++;
82926   }
82927 #ifndef SQLITE_ENABLE_STAT3_OR_STAT4
82928   assert( pIndex!=0 );
82929 #else
82930   if( pIndex )
82931 #endif
82932   {
82933     if( strcmp(z, "unordered")==0 ){
82934       pIndex->bUnordered = 1;
82935     }else if( sqlite3_strglob("sz=[0-9]*", z)==0 ){
82936       int v32 = 0;
82937       sqlite3GetInt32(z+3, &v32);
82938       pIndex->szIdxRow = sqlite3LogEst(v32);
82939     }
82940   }
82941 }
82942 
82943 /*
82944 ** This callback is invoked once for each index when reading the
82945 ** sqlite_stat1 table.
82946 **
82947 **     argv[0] = name of the table
82948 **     argv[1] = name of the index (might be NULL)
82949 **     argv[2] = results of analysis - on integer for each column
82950 **
82951 ** Entries for which argv[1]==NULL simply record the number of rows in
82952 ** the table.
82953 */
82954 static int analysisLoader(void *pData, int argc, char **argv, char **NotUsed){
82955   analysisInfo *pInfo = (analysisInfo*)pData;
82956   Index *pIndex;
82957   Table *pTable;
82958   const char *z;
82959 
82960   assert( argc==3 );
82961   UNUSED_PARAMETER2(NotUsed, argc);
82962 
82963   if( argv==0 || argv[0]==0 || argv[2]==0 ){
82964     return 0;
82965   }
82966   pTable = sqlite3FindTable(pInfo->db, argv[0], pInfo->zDatabase);
82967   if( pTable==0 ){
82968     return 0;
82969   }
82970   if( argv[1]==0 ){
82971     pIndex = 0;
82972   }else if( sqlite3_stricmp(argv[0],argv[1])==0 ){
82973     pIndex = sqlite3PrimaryKeyIndex(pTable);
82974   }else{
82975     pIndex = sqlite3FindIndex(pInfo->db, argv[1], pInfo->zDatabase);
82976   }
82977   z = argv[2];
82978 
82979   if( pIndex ){
82980     decodeIntArray((char*)z, pIndex->nKeyCol+1, pIndex->aiRowEst, pIndex);
82981     if( pIndex->pPartIdxWhere==0 ) pTable->nRowEst = pIndex->aiRowEst[0];
82982   }else{
82983     Index fakeIdx;
82984     fakeIdx.szIdxRow = pTable->szTabRow;
82985     decodeIntArray((char*)z, 1, &pTable->nRowEst, &fakeIdx);
82986     pTable->szTabRow = fakeIdx.szIdxRow;
82987   }
82988 
82989   return 0;
82990 }
82991 
82992 /*
82993 ** If the Index.aSample variable is not NULL, delete the aSample[] array
82994 ** and its contents.
82995 */
82996 SQLITE_PRIVATE void sqlite3DeleteIndexSamples(sqlite3 *db, Index *pIdx){
82997 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
82998   if( pIdx->aSample ){
82999     int j;
83000     for(j=0; j<pIdx->nSample; j++){
83001       IndexSample *p = &pIdx->aSample[j];
83002       sqlite3DbFree(db, p->p);
83003     }
83004     sqlite3DbFree(db, pIdx->aSample);
83005   }
83006   if( db && db->pnBytesFreed==0 ){
83007     pIdx->nSample = 0;
83008     pIdx->aSample = 0;
83009   }
83010 #else
83011   UNUSED_PARAMETER(db);
83012   UNUSED_PARAMETER(pIdx);
83013 #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
83014 }
83015 
83016 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
83017 /*
83018 ** Populate the pIdx->aAvgEq[] array based on the samples currently
83019 ** stored in pIdx->aSample[].
83020 */
83021 static void initAvgEq(Index *pIdx){
83022   if( pIdx ){
83023     IndexSample *aSample = pIdx->aSample;
83024     IndexSample *pFinal = &aSample[pIdx->nSample-1];
83025     int iCol;
83026     for(iCol=0; iCol<pIdx->nKeyCol; iCol++){
83027       int i;                    /* Used to iterate through samples */
83028       tRowcnt sumEq = 0;        /* Sum of the nEq values */
83029       tRowcnt nSum = 0;         /* Number of terms contributing to sumEq */
83030       tRowcnt avgEq = 0;
83031       tRowcnt nDLt = pFinal->anDLt[iCol];
83032 
83033       /* Set nSum to the number of distinct (iCol+1) field prefixes that
83034       ** occur in the stat4 table for this index before pFinal. Set
83035       ** sumEq to the sum of the nEq values for column iCol for the same
83036       ** set (adding the value only once where there exist dupicate
83037       ** prefixes).  */
83038       for(i=0; i<(pIdx->nSample-1); i++){
83039         if( aSample[i].anDLt[iCol]!=aSample[i+1].anDLt[iCol] ){
83040           sumEq += aSample[i].anEq[iCol];
83041           nSum++;
83042         }
83043       }
83044       if( nDLt>nSum ){
83045         avgEq = (pFinal->anLt[iCol] - sumEq)/(nDLt - nSum);
83046       }
83047       if( avgEq==0 ) avgEq = 1;
83048       pIdx->aAvgEq[iCol] = avgEq;
83049       if( pIdx->nSampleCol==1 ) break;
83050     }
83051   }
83052 }
83053 
83054 /*
83055 ** Look up an index by name.  Or, if the name of a WITHOUT ROWID table
83056 ** is supplied instead, find the PRIMARY KEY index for that table.
83057 */
83058 static Index *findIndexOrPrimaryKey(
83059   sqlite3 *db,
83060   const char *zName,
83061   const char *zDb
83062 ){
83063   Index *pIdx = sqlite3FindIndex(db, zName, zDb);
83064   if( pIdx==0 ){
83065     Table *pTab = sqlite3FindTable(db, zName, zDb);
83066     if( pTab && !HasRowid(pTab) ) pIdx = sqlite3PrimaryKeyIndex(pTab);
83067   }
83068   return pIdx;
83069 }
83070 
83071 /*
83072 ** Load the content from either the sqlite_stat4 or sqlite_stat3 table
83073 ** into the relevant Index.aSample[] arrays.
83074 **
83075 ** Arguments zSql1 and zSql2 must point to SQL statements that return
83076 ** data equivalent to the following (statements are different for stat3,
83077 ** see the caller of this function for details):
83078 **
83079 **    zSql1: SELECT idx,count(*) FROM %Q.sqlite_stat4 GROUP BY idx
83080 **    zSql2: SELECT idx,neq,nlt,ndlt,sample FROM %Q.sqlite_stat4
83081 **
83082 ** where %Q is replaced with the database name before the SQL is executed.
83083 */
83084 static int loadStatTbl(
83085   sqlite3 *db,                  /* Database handle */
83086   int bStat3,                   /* Assume single column records only */
83087   const char *zSql1,            /* SQL statement 1 (see above) */
83088   const char *zSql2,            /* SQL statement 2 (see above) */
83089   const char *zDb               /* Database name (e.g. "main") */
83090 ){
83091   int rc;                       /* Result codes from subroutines */
83092   sqlite3_stmt *pStmt = 0;      /* An SQL statement being run */
83093   char *zSql;                   /* Text of the SQL statement */
83094   Index *pPrevIdx = 0;          /* Previous index in the loop */
83095   IndexSample *pSample;         /* A slot in pIdx->aSample[] */
83096 
83097   assert( db->lookaside.bEnabled==0 );
83098   zSql = sqlite3MPrintf(db, zSql1, zDb);
83099   if( !zSql ){
83100     return SQLITE_NOMEM;
83101   }
83102   rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
83103   sqlite3DbFree(db, zSql);
83104   if( rc ) return rc;
83105 
83106   while( sqlite3_step(pStmt)==SQLITE_ROW ){
83107     int nIdxCol = 1;              /* Number of columns in stat4 records */
83108     int nAvgCol = 1;              /* Number of entries in Index.aAvgEq */
83109 
83110     char *zIndex;   /* Index name */
83111     Index *pIdx;    /* Pointer to the index object */
83112     int nSample;    /* Number of samples */
83113     int nByte;      /* Bytes of space required */
83114     int i;          /* Bytes of space required */
83115     tRowcnt *pSpace;
83116 
83117     zIndex = (char *)sqlite3_column_text(pStmt, 0);
83118     if( zIndex==0 ) continue;
83119     nSample = sqlite3_column_int(pStmt, 1);
83120     pIdx = findIndexOrPrimaryKey(db, zIndex, zDb);
83121     assert( pIdx==0 || bStat3 || pIdx->nSample==0 );
83122     /* Index.nSample is non-zero at this point if data has already been
83123     ** loaded from the stat4 table. In this case ignore stat3 data.  */
83124     if( pIdx==0 || pIdx->nSample ) continue;
83125     if( bStat3==0 ){
83126       nIdxCol = pIdx->nKeyCol+1;
83127       nAvgCol = pIdx->nKeyCol;
83128     }
83129     pIdx->nSampleCol = nIdxCol;
83130     nByte = sizeof(IndexSample) * nSample;
83131     nByte += sizeof(tRowcnt) * nIdxCol * 3 * nSample;
83132     nByte += nAvgCol * sizeof(tRowcnt);     /* Space for Index.aAvgEq[] */
83133 
83134     pIdx->aSample = sqlite3DbMallocZero(db, nByte);
83135     if( pIdx->aSample==0 ){
83136       sqlite3_finalize(pStmt);
83137       return SQLITE_NOMEM;
83138     }
83139     pSpace = (tRowcnt*)&pIdx->aSample[nSample];
83140     pIdx->aAvgEq = pSpace; pSpace += nAvgCol;
83141     for(i=0; i<nSample; i++){
83142       pIdx->aSample[i].anEq = pSpace; pSpace += nIdxCol;
83143       pIdx->aSample[i].anLt = pSpace; pSpace += nIdxCol;
83144       pIdx->aSample[i].anDLt = pSpace; pSpace += nIdxCol;
83145     }
83146     assert( ((u8*)pSpace)-nByte==(u8*)(pIdx->aSample) );
83147   }
83148   rc = sqlite3_finalize(pStmt);
83149   if( rc ) return rc;
83150 
83151   zSql = sqlite3MPrintf(db, zSql2, zDb);
83152   if( !zSql ){
83153     return SQLITE_NOMEM;
83154   }
83155   rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
83156   sqlite3DbFree(db, zSql);
83157   if( rc ) return rc;
83158 
83159   while( sqlite3_step(pStmt)==SQLITE_ROW ){
83160     char *zIndex;                 /* Index name */
83161     Index *pIdx;                  /* Pointer to the index object */
83162     int nCol = 1;                 /* Number of columns in index */
83163 
83164     zIndex = (char *)sqlite3_column_text(pStmt, 0);
83165     if( zIndex==0 ) continue;
83166     pIdx = findIndexOrPrimaryKey(db, zIndex, zDb);
83167     if( pIdx==0 ) continue;
83168     /* This next condition is true if data has already been loaded from
83169     ** the sqlite_stat4 table. In this case ignore stat3 data.  */
83170     nCol = pIdx->nSampleCol;
83171     if( bStat3 && nCol>1 ) continue;
83172     if( pIdx!=pPrevIdx ){
83173       initAvgEq(pPrevIdx);
83174       pPrevIdx = pIdx;
83175     }
83176     pSample = &pIdx->aSample[pIdx->nSample];
83177     decodeIntArray((char*)sqlite3_column_text(pStmt,1), nCol, pSample->anEq, 0);
83178     decodeIntArray((char*)sqlite3_column_text(pStmt,2), nCol, pSample->anLt, 0);
83179     decodeIntArray((char*)sqlite3_column_text(pStmt,3), nCol, pSample->anDLt,0);
83180 
83181     /* Take a copy of the sample. Add two 0x00 bytes the end of the buffer.
83182     ** This is in case the sample record is corrupted. In that case, the
83183     ** sqlite3VdbeRecordCompare() may read up to two varints past the
83184     ** end of the allocated buffer before it realizes it is dealing with
83185     ** a corrupt record. Adding the two 0x00 bytes prevents this from causing
83186     ** a buffer overread.  */
83187     pSample->n = sqlite3_column_bytes(pStmt, 4);
83188     pSample->p = sqlite3DbMallocZero(db, pSample->n + 2);
83189     if( pSample->p==0 ){
83190       sqlite3_finalize(pStmt);
83191       return SQLITE_NOMEM;
83192     }
83193     memcpy(pSample->p, sqlite3_column_blob(pStmt, 4), pSample->n);
83194     pIdx->nSample++;
83195   }
83196   rc = sqlite3_finalize(pStmt);
83197   if( rc==SQLITE_OK ) initAvgEq(pPrevIdx);
83198   return rc;
83199 }
83200 
83201 /*
83202 ** Load content from the sqlite_stat4 and sqlite_stat3 tables into
83203 ** the Index.aSample[] arrays of all indices.
83204 */
83205 static int loadStat4(sqlite3 *db, const char *zDb){
83206   int rc = SQLITE_OK;             /* Result codes from subroutines */
83207 
83208   assert( db->lookaside.bEnabled==0 );
83209   if( sqlite3FindTable(db, "sqlite_stat4", zDb) ){
83210     rc = loadStatTbl(db, 0,
83211       "SELECT idx,count(*) FROM %Q.sqlite_stat4 GROUP BY idx",
83212       "SELECT idx,neq,nlt,ndlt,sample FROM %Q.sqlite_stat4",
83213       zDb
83214     );
83215   }
83216 
83217   if( rc==SQLITE_OK && sqlite3FindTable(db, "sqlite_stat3", zDb) ){
83218     rc = loadStatTbl(db, 1,
83219       "SELECT idx,count(*) FROM %Q.sqlite_stat3 GROUP BY idx",
83220       "SELECT idx,neq,nlt,ndlt,sqlite_record(sample) FROM %Q.sqlite_stat3",
83221       zDb
83222     );
83223   }
83224 
83225   return rc;
83226 }
83227 #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
83228 
83229 /*
83230 ** Load the content of the sqlite_stat1 and sqlite_stat3/4 tables. The
83231 ** contents of sqlite_stat1 are used to populate the Index.aiRowEst[]
83232 ** arrays. The contents of sqlite_stat3/4 are used to populate the
83233 ** Index.aSample[] arrays.
83234 **
83235 ** If the sqlite_stat1 table is not present in the database, SQLITE_ERROR
83236 ** is returned. In this case, even if SQLITE_ENABLE_STAT3/4 was defined
83237 ** during compilation and the sqlite_stat3/4 table is present, no data is
83238 ** read from it.
83239 **
83240 ** If SQLITE_ENABLE_STAT3/4 was defined during compilation and the
83241 ** sqlite_stat4 table is not present in the database, SQLITE_ERROR is
83242 ** returned. However, in this case, data is read from the sqlite_stat1
83243 ** table (if it is present) before returning.
83244 **
83245 ** If an OOM error occurs, this function always sets db->mallocFailed.
83246 ** This means if the caller does not care about other errors, the return
83247 ** code may be ignored.
83248 */
83249 SQLITE_PRIVATE int sqlite3AnalysisLoad(sqlite3 *db, int iDb){
83250   analysisInfo sInfo;
83251   HashElem *i;
83252   char *zSql;
83253   int rc;
83254 
83255   assert( iDb>=0 && iDb<db->nDb );
83256   assert( db->aDb[iDb].pBt!=0 );
83257 
83258   /* Clear any prior statistics */
83259   assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
83260   for(i=sqliteHashFirst(&db->aDb[iDb].pSchema->idxHash);i;i=sqliteHashNext(i)){
83261     Index *pIdx = sqliteHashData(i);
83262     sqlite3DefaultRowEst(pIdx);
83263 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
83264     sqlite3DeleteIndexSamples(db, pIdx);
83265     pIdx->aSample = 0;
83266 #endif
83267   }
83268 
83269   /* Check to make sure the sqlite_stat1 table exists */
83270   sInfo.db = db;
83271   sInfo.zDatabase = db->aDb[iDb].zName;
83272   if( sqlite3FindTable(db, "sqlite_stat1", sInfo.zDatabase)==0 ){
83273     return SQLITE_ERROR;
83274   }
83275 
83276   /* Load new statistics out of the sqlite_stat1 table */
83277   zSql = sqlite3MPrintf(db,
83278       "SELECT tbl,idx,stat FROM %Q.sqlite_stat1", sInfo.zDatabase);
83279   if( zSql==0 ){
83280     rc = SQLITE_NOMEM;
83281   }else{
83282     rc = sqlite3_exec(db, zSql, analysisLoader, &sInfo, 0);
83283     sqlite3DbFree(db, zSql);
83284   }
83285 
83286 
83287   /* Load the statistics from the sqlite_stat4 table. */
83288 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
83289   if( rc==SQLITE_OK ){
83290     int lookasideEnabled = db->lookaside.bEnabled;
83291     db->lookaside.bEnabled = 0;
83292     rc = loadStat4(db, sInfo.zDatabase);
83293     db->lookaside.bEnabled = lookasideEnabled;
83294   }
83295 #endif
83296 
83297   if( rc==SQLITE_NOMEM ){
83298     db->mallocFailed = 1;
83299   }
83300   return rc;
83301 }
83302 
83303 
83304 #endif /* SQLITE_OMIT_ANALYZE */
83305 
83306 /************** End of analyze.c *********************************************/
83307 /************** Begin file attach.c ******************************************/
83308 /*
83309 ** 2003 April 6
83310 **
83311 ** The author disclaims copyright to this source code.  In place of
83312 ** a legal notice, here is a blessing:
83313 **
83314 **    May you do good and not evil.
83315 **    May you find forgiveness for yourself and forgive others.
83316 **    May you share freely, never taking more than you give.
83317 **
83318 *************************************************************************
83319 ** This file contains code used to implement the ATTACH and DETACH commands.
83320 */
83321 
83322 #ifndef SQLITE_OMIT_ATTACH
83323 /*
83324 ** Resolve an expression that was part of an ATTACH or DETACH statement. This
83325 ** is slightly different from resolving a normal SQL expression, because simple
83326 ** identifiers are treated as strings, not possible column names or aliases.
83327 **
83328 ** i.e. if the parser sees:
83329 **
83330 **     ATTACH DATABASE abc AS def
83331 **
83332 ** it treats the two expressions as literal strings 'abc' and 'def' instead of
83333 ** looking for columns of the same name.
83334 **
83335 ** This only applies to the root node of pExpr, so the statement:
83336 **
83337 **     ATTACH DATABASE abc||def AS 'db2'
83338 **
83339 ** will fail because neither abc or def can be resolved.
83340 */
83341 static int resolveAttachExpr(NameContext *pName, Expr *pExpr)
83342 {
83343   int rc = SQLITE_OK;
83344   if( pExpr ){
83345     if( pExpr->op!=TK_ID ){
83346       rc = sqlite3ResolveExprNames(pName, pExpr);
83347     }else{
83348       pExpr->op = TK_STRING;
83349     }
83350   }
83351   return rc;
83352 }
83353 
83354 /*
83355 ** An SQL user-function registered to do the work of an ATTACH statement. The
83356 ** three arguments to the function come directly from an attach statement:
83357 **
83358 **     ATTACH DATABASE x AS y KEY z
83359 **
83360 **     SELECT sqlite_attach(x, y, z)
83361 **
83362 ** If the optional "KEY z" syntax is omitted, an SQL NULL is passed as the
83363 ** third argument.
83364 */
83365 static void attachFunc(
83366   sqlite3_context *context,
83367   int NotUsed,
83368   sqlite3_value **argv
83369 ){
83370   int i;
83371   int rc = 0;
83372   sqlite3 *db = sqlite3_context_db_handle(context);
83373   const char *zName;
83374   const char *zFile;
83375   char *zPath = 0;
83376   char *zErr = 0;
83377   unsigned int flags;
83378   Db *aNew;
83379   char *zErrDyn = 0;
83380   sqlite3_vfs *pVfs;
83381 
83382   UNUSED_PARAMETER(NotUsed);
83383 
83384   zFile = (const char *)sqlite3_value_text(argv[0]);
83385   zName = (const char *)sqlite3_value_text(argv[1]);
83386   if( zFile==0 ) zFile = "";
83387   if( zName==0 ) zName = "";
83388 
83389   /* Check for the following errors:
83390   **
83391   **     * Too many attached databases,
83392   **     * Transaction currently open
83393   **     * Specified database name already being used.
83394   */
83395   if( db->nDb>=db->aLimit[SQLITE_LIMIT_ATTACHED]+2 ){
83396     zErrDyn = sqlite3MPrintf(db, "too many attached databases - max %d",
83397       db->aLimit[SQLITE_LIMIT_ATTACHED]
83398     );
83399     goto attach_error;
83400   }
83401   if( !db->autoCommit ){
83402     zErrDyn = sqlite3MPrintf(db, "cannot ATTACH database within transaction");
83403     goto attach_error;
83404   }
83405   for(i=0; i<db->nDb; i++){
83406     char *z = db->aDb[i].zName;
83407     assert( z && zName );
83408     if( sqlite3StrICmp(z, zName)==0 ){
83409       zErrDyn = sqlite3MPrintf(db, "database %s is already in use", zName);
83410       goto attach_error;
83411     }
83412   }
83413 
83414   /* Allocate the new entry in the db->aDb[] array and initialize the schema
83415   ** hash tables.
83416   */
83417   if( db->aDb==db->aDbStatic ){
83418     aNew = sqlite3DbMallocRaw(db, sizeof(db->aDb[0])*3 );
83419     if( aNew==0 ) return;
83420     memcpy(aNew, db->aDb, sizeof(db->aDb[0])*2);
83421   }else{
83422     aNew = sqlite3DbRealloc(db, db->aDb, sizeof(db->aDb[0])*(db->nDb+1) );
83423     if( aNew==0 ) return;
83424   }
83425   db->aDb = aNew;
83426   aNew = &db->aDb[db->nDb];
83427   memset(aNew, 0, sizeof(*aNew));
83428 
83429   /* Open the database file. If the btree is successfully opened, use
83430   ** it to obtain the database schema. At this point the schema may
83431   ** or may not be initialized.
83432   */
83433   flags = db->openFlags;
83434   rc = sqlite3ParseUri(db->pVfs->zName, zFile, &flags, &pVfs, &zPath, &zErr);
83435   if( rc!=SQLITE_OK ){
83436     if( rc==SQLITE_NOMEM ) db->mallocFailed = 1;
83437     sqlite3_result_error(context, zErr, -1);
83438     sqlite3_free(zErr);
83439     return;
83440   }
83441   assert( pVfs );
83442   flags |= SQLITE_OPEN_MAIN_DB;
83443   rc = sqlite3BtreeOpen(pVfs, zPath, db, &aNew->pBt, 0, flags);
83444   sqlite3_free( zPath );
83445   db->nDb++;
83446   if( rc==SQLITE_CONSTRAINT ){
83447     rc = SQLITE_ERROR;
83448     zErrDyn = sqlite3MPrintf(db, "database is already attached");
83449   }else if( rc==SQLITE_OK ){
83450     Pager *pPager;
83451     aNew->pSchema = sqlite3SchemaGet(db, aNew->pBt);
83452     if( !aNew->pSchema ){
83453       rc = SQLITE_NOMEM;
83454     }else if( aNew->pSchema->file_format && aNew->pSchema->enc!=ENC(db) ){
83455       zErrDyn = sqlite3MPrintf(db,
83456         "attached databases must use the same text encoding as main database");
83457       rc = SQLITE_ERROR;
83458     }
83459     pPager = sqlite3BtreePager(aNew->pBt);
83460     sqlite3PagerLockingMode(pPager, db->dfltLockMode);
83461     sqlite3BtreeSecureDelete(aNew->pBt,
83462                              sqlite3BtreeSecureDelete(db->aDb[0].pBt,-1) );
83463 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
83464     sqlite3BtreeSetPagerFlags(aNew->pBt, 3 | (db->flags & PAGER_FLAGS_MASK));
83465 #endif
83466   }
83467   aNew->safety_level = 3;
83468   aNew->zName = sqlite3DbStrDup(db, zName);
83469   if( rc==SQLITE_OK && aNew->zName==0 ){
83470     rc = SQLITE_NOMEM;
83471   }
83472 
83473 
83474 #ifdef SQLITE_HAS_CODEC
83475   if( rc==SQLITE_OK ){
83476     extern int sqlite3CodecAttach(sqlite3*, int, const void*, int);
83477     extern void sqlite3CodecGetKey(sqlite3*, int, void**, int*);
83478     int nKey;
83479     char *zKey;
83480     int t = sqlite3_value_type(argv[2]);
83481     switch( t ){
83482       case SQLITE_INTEGER:
83483       case SQLITE_FLOAT:
83484         zErrDyn = sqlite3DbStrDup(db, "Invalid key value");
83485         rc = SQLITE_ERROR;
83486         break;
83487 
83488       case SQLITE_TEXT:
83489       case SQLITE_BLOB:
83490         nKey = sqlite3_value_bytes(argv[2]);
83491         zKey = (char *)sqlite3_value_blob(argv[2]);
83492         rc = sqlite3CodecAttach(db, db->nDb-1, zKey, nKey);
83493         break;
83494 
83495       case SQLITE_NULL:
83496         /* No key specified.  Use the key from the main database */
83497         sqlite3CodecGetKey(db, 0, (void**)&zKey, &nKey);
83498         if( nKey>0 || sqlite3BtreeGetReserve(db->aDb[0].pBt)>0 ){
83499           rc = sqlite3CodecAttach(db, db->nDb-1, zKey, nKey);
83500         }
83501         break;
83502     }
83503   }
83504 #endif
83505 
83506   /* If the file was opened successfully, read the schema for the new database.
83507   ** If this fails, or if opening the file failed, then close the file and
83508   ** remove the entry from the db->aDb[] array. i.e. put everything back the way
83509   ** we found it.
83510   */
83511   if( rc==SQLITE_OK ){
83512     sqlite3BtreeEnterAll(db);
83513     rc = sqlite3Init(db, &zErrDyn);
83514     sqlite3BtreeLeaveAll(db);
83515   }
83516   if( rc ){
83517     int iDb = db->nDb - 1;
83518     assert( iDb>=2 );
83519     if( db->aDb[iDb].pBt ){
83520       sqlite3BtreeClose(db->aDb[iDb].pBt);
83521       db->aDb[iDb].pBt = 0;
83522       db->aDb[iDb].pSchema = 0;
83523     }
83524     sqlite3ResetAllSchemasOfConnection(db);
83525     db->nDb = iDb;
83526     if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
83527       db->mallocFailed = 1;
83528       sqlite3DbFree(db, zErrDyn);
83529       zErrDyn = sqlite3MPrintf(db, "out of memory");
83530     }else if( zErrDyn==0 ){
83531       zErrDyn = sqlite3MPrintf(db, "unable to open database: %s", zFile);
83532     }
83533     goto attach_error;
83534   }
83535 
83536   return;
83537 
83538 attach_error:
83539   /* Return an error if we get here */
83540   if( zErrDyn ){
83541     sqlite3_result_error(context, zErrDyn, -1);
83542     sqlite3DbFree(db, zErrDyn);
83543   }
83544   if( rc ) sqlite3_result_error_code(context, rc);
83545 }
83546 
83547 /*
83548 ** An SQL user-function registered to do the work of an DETACH statement. The
83549 ** three arguments to the function come directly from a detach statement:
83550 **
83551 **     DETACH DATABASE x
83552 **
83553 **     SELECT sqlite_detach(x)
83554 */
83555 static void detachFunc(
83556   sqlite3_context *context,
83557   int NotUsed,
83558   sqlite3_value **argv
83559 ){
83560   const char *zName = (const char *)sqlite3_value_text(argv[0]);
83561   sqlite3 *db = sqlite3_context_db_handle(context);
83562   int i;
83563   Db *pDb = 0;
83564   char zErr[128];
83565 
83566   UNUSED_PARAMETER(NotUsed);
83567 
83568   if( zName==0 ) zName = "";
83569   for(i=0; i<db->nDb; i++){
83570     pDb = &db->aDb[i];
83571     if( pDb->pBt==0 ) continue;
83572     if( sqlite3StrICmp(pDb->zName, zName)==0 ) break;
83573   }
83574 
83575   if( i>=db->nDb ){
83576     sqlite3_snprintf(sizeof(zErr),zErr, "no such database: %s", zName);
83577     goto detach_error;
83578   }
83579   if( i<2 ){
83580     sqlite3_snprintf(sizeof(zErr),zErr, "cannot detach database %s", zName);
83581     goto detach_error;
83582   }
83583   if( !db->autoCommit ){
83584     sqlite3_snprintf(sizeof(zErr), zErr,
83585                      "cannot DETACH database within transaction");
83586     goto detach_error;
83587   }
83588   if( sqlite3BtreeIsInReadTrans(pDb->pBt) || sqlite3BtreeIsInBackup(pDb->pBt) ){
83589     sqlite3_snprintf(sizeof(zErr),zErr, "database %s is locked", zName);
83590     goto detach_error;
83591   }
83592 
83593   sqlite3BtreeClose(pDb->pBt);
83594   pDb->pBt = 0;
83595   pDb->pSchema = 0;
83596   sqlite3ResetAllSchemasOfConnection(db);
83597   return;
83598 
83599 detach_error:
83600   sqlite3_result_error(context, zErr, -1);
83601 }
83602 
83603 /*
83604 ** This procedure generates VDBE code for a single invocation of either the
83605 ** sqlite_detach() or sqlite_attach() SQL user functions.
83606 */
83607 static void codeAttach(
83608   Parse *pParse,       /* The parser context */
83609   int type,            /* Either SQLITE_ATTACH or SQLITE_DETACH */
83610   FuncDef const *pFunc,/* FuncDef wrapper for detachFunc() or attachFunc() */
83611   Expr *pAuthArg,      /* Expression to pass to authorization callback */
83612   Expr *pFilename,     /* Name of database file */
83613   Expr *pDbname,       /* Name of the database to use internally */
83614   Expr *pKey           /* Database key for encryption extension */
83615 ){
83616   int rc;
83617   NameContext sName;
83618   Vdbe *v;
83619   sqlite3* db = pParse->db;
83620   int regArgs;
83621 
83622   memset(&sName, 0, sizeof(NameContext));
83623   sName.pParse = pParse;
83624 
83625   if(
83626       SQLITE_OK!=(rc = resolveAttachExpr(&sName, pFilename)) ||
83627       SQLITE_OK!=(rc = resolveAttachExpr(&sName, pDbname)) ||
83628       SQLITE_OK!=(rc = resolveAttachExpr(&sName, pKey))
83629   ){
83630     pParse->nErr++;
83631     goto attach_end;
83632   }
83633 
83634 #ifndef SQLITE_OMIT_AUTHORIZATION
83635   if( pAuthArg ){
83636     char *zAuthArg;
83637     if( pAuthArg->op==TK_STRING ){
83638       zAuthArg = pAuthArg->u.zToken;
83639     }else{
83640       zAuthArg = 0;
83641     }
83642     rc = sqlite3AuthCheck(pParse, type, zAuthArg, 0, 0);
83643     if(rc!=SQLITE_OK ){
83644       goto attach_end;
83645     }
83646   }
83647 #endif /* SQLITE_OMIT_AUTHORIZATION */
83648 
83649 
83650   v = sqlite3GetVdbe(pParse);
83651   regArgs = sqlite3GetTempRange(pParse, 4);
83652   sqlite3ExprCode(pParse, pFilename, regArgs);
83653   sqlite3ExprCode(pParse, pDbname, regArgs+1);
83654   sqlite3ExprCode(pParse, pKey, regArgs+2);
83655 
83656   assert( v || db->mallocFailed );
83657   if( v ){
83658     sqlite3VdbeAddOp3(v, OP_Function, 0, regArgs+3-pFunc->nArg, regArgs+3);
83659     assert( pFunc->nArg==-1 || (pFunc->nArg&0xff)==pFunc->nArg );
83660     sqlite3VdbeChangeP5(v, (u8)(pFunc->nArg));
83661     sqlite3VdbeChangeP4(v, -1, (char *)pFunc, P4_FUNCDEF);
83662 
83663     /* Code an OP_Expire. For an ATTACH statement, set P1 to true (expire this
83664     ** statement only). For DETACH, set it to false (expire all existing
83665     ** statements).
83666     */
83667     sqlite3VdbeAddOp1(v, OP_Expire, (type==SQLITE_ATTACH));
83668   }
83669 
83670 attach_end:
83671   sqlite3ExprDelete(db, pFilename);
83672   sqlite3ExprDelete(db, pDbname);
83673   sqlite3ExprDelete(db, pKey);
83674 }
83675 
83676 /*
83677 ** Called by the parser to compile a DETACH statement.
83678 **
83679 **     DETACH pDbname
83680 */
83681 SQLITE_PRIVATE void sqlite3Detach(Parse *pParse, Expr *pDbname){
83682   static const FuncDef detach_func = {
83683     1,                /* nArg */
83684     SQLITE_UTF8,      /* funcFlags */
83685     0,                /* pUserData */
83686     0,                /* pNext */
83687     detachFunc,       /* xFunc */
83688     0,                /* xStep */
83689     0,                /* xFinalize */
83690     "sqlite_detach",  /* zName */
83691     0,                /* pHash */
83692     0                 /* pDestructor */
83693   };
83694   codeAttach(pParse, SQLITE_DETACH, &detach_func, pDbname, 0, 0, pDbname);
83695 }
83696 
83697 /*
83698 ** Called by the parser to compile an ATTACH statement.
83699 **
83700 **     ATTACH p AS pDbname KEY pKey
83701 */
83702 SQLITE_PRIVATE void sqlite3Attach(Parse *pParse, Expr *p, Expr *pDbname, Expr *pKey){
83703   static const FuncDef attach_func = {
83704     3,                /* nArg */
83705     SQLITE_UTF8,      /* funcFlags */
83706     0,                /* pUserData */
83707     0,                /* pNext */
83708     attachFunc,       /* xFunc */
83709     0,                /* xStep */
83710     0,                /* xFinalize */
83711     "sqlite_attach",  /* zName */
83712     0,                /* pHash */
83713     0                 /* pDestructor */
83714   };
83715   codeAttach(pParse, SQLITE_ATTACH, &attach_func, p, p, pDbname, pKey);
83716 }
83717 #endif /* SQLITE_OMIT_ATTACH */
83718 
83719 /*
83720 ** Initialize a DbFixer structure.  This routine must be called prior
83721 ** to passing the structure to one of the sqliteFixAAAA() routines below.
83722 */
83723 SQLITE_PRIVATE void sqlite3FixInit(
83724   DbFixer *pFix,      /* The fixer to be initialized */
83725   Parse *pParse,      /* Error messages will be written here */
83726   int iDb,            /* This is the database that must be used */
83727   const char *zType,  /* "view", "trigger", or "index" */
83728   const Token *pName  /* Name of the view, trigger, or index */
83729 ){
83730   sqlite3 *db;
83731 
83732   db = pParse->db;
83733   assert( db->nDb>iDb );
83734   pFix->pParse = pParse;
83735   pFix->zDb = db->aDb[iDb].zName;
83736   pFix->pSchema = db->aDb[iDb].pSchema;
83737   pFix->zType = zType;
83738   pFix->pName = pName;
83739   pFix->bVarOnly = (iDb==1);
83740 }
83741 
83742 /*
83743 ** The following set of routines walk through the parse tree and assign
83744 ** a specific database to all table references where the database name
83745 ** was left unspecified in the original SQL statement.  The pFix structure
83746 ** must have been initialized by a prior call to sqlite3FixInit().
83747 **
83748 ** These routines are used to make sure that an index, trigger, or
83749 ** view in one database does not refer to objects in a different database.
83750 ** (Exception: indices, triggers, and views in the TEMP database are
83751 ** allowed to refer to anything.)  If a reference is explicitly made
83752 ** to an object in a different database, an error message is added to
83753 ** pParse->zErrMsg and these routines return non-zero.  If everything
83754 ** checks out, these routines return 0.
83755 */
83756 SQLITE_PRIVATE int sqlite3FixSrcList(
83757   DbFixer *pFix,       /* Context of the fixation */
83758   SrcList *pList       /* The Source list to check and modify */
83759 ){
83760   int i;
83761   const char *zDb;
83762   struct SrcList_item *pItem;
83763 
83764   if( NEVER(pList==0) ) return 0;
83765   zDb = pFix->zDb;
83766   for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){
83767     if( pFix->bVarOnly==0 ){
83768       if( pItem->zDatabase && sqlite3StrICmp(pItem->zDatabase, zDb) ){
83769         sqlite3ErrorMsg(pFix->pParse,
83770             "%s %T cannot reference objects in database %s",
83771             pFix->zType, pFix->pName, pItem->zDatabase);
83772         return 1;
83773       }
83774       sqlite3DbFree(pFix->pParse->db, pItem->zDatabase);
83775       pItem->zDatabase = 0;
83776       pItem->pSchema = pFix->pSchema;
83777     }
83778 #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER)
83779     if( sqlite3FixSelect(pFix, pItem->pSelect) ) return 1;
83780     if( sqlite3FixExpr(pFix, pItem->pOn) ) return 1;
83781 #endif
83782   }
83783   return 0;
83784 }
83785 #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_TRIGGER)
83786 SQLITE_PRIVATE int sqlite3FixSelect(
83787   DbFixer *pFix,       /* Context of the fixation */
83788   Select *pSelect      /* The SELECT statement to be fixed to one database */
83789 ){
83790   while( pSelect ){
83791     if( sqlite3FixExprList(pFix, pSelect->pEList) ){
83792       return 1;
83793     }
83794     if( sqlite3FixSrcList(pFix, pSelect->pSrc) ){
83795       return 1;
83796     }
83797     if( sqlite3FixExpr(pFix, pSelect->pWhere) ){
83798       return 1;
83799     }
83800     if( sqlite3FixExprList(pFix, pSelect->pGroupBy) ){
83801       return 1;
83802     }
83803     if( sqlite3FixExpr(pFix, pSelect->pHaving) ){
83804       return 1;
83805     }
83806     if( sqlite3FixExprList(pFix, pSelect->pOrderBy) ){
83807       return 1;
83808     }
83809     if( sqlite3FixExpr(pFix, pSelect->pLimit) ){
83810       return 1;
83811     }
83812     if( sqlite3FixExpr(pFix, pSelect->pOffset) ){
83813       return 1;
83814     }
83815     pSelect = pSelect->pPrior;
83816   }
83817   return 0;
83818 }
83819 SQLITE_PRIVATE int sqlite3FixExpr(
83820   DbFixer *pFix,     /* Context of the fixation */
83821   Expr *pExpr        /* The expression to be fixed to one database */
83822 ){
83823   while( pExpr ){
83824     if( pExpr->op==TK_VARIABLE ){
83825       if( pFix->pParse->db->init.busy ){
83826         pExpr->op = TK_NULL;
83827       }else{
83828         sqlite3ErrorMsg(pFix->pParse, "%s cannot use variables", pFix->zType);
83829         return 1;
83830       }
83831     }
83832     if( ExprHasProperty(pExpr, EP_TokenOnly) ) break;
83833     if( ExprHasProperty(pExpr, EP_xIsSelect) ){
83834       if( sqlite3FixSelect(pFix, pExpr->x.pSelect) ) return 1;
83835     }else{
83836       if( sqlite3FixExprList(pFix, pExpr->x.pList) ) return 1;
83837     }
83838     if( sqlite3FixExpr(pFix, pExpr->pRight) ){
83839       return 1;
83840     }
83841     pExpr = pExpr->pLeft;
83842   }
83843   return 0;
83844 }
83845 SQLITE_PRIVATE int sqlite3FixExprList(
83846   DbFixer *pFix,     /* Context of the fixation */
83847   ExprList *pList    /* The expression to be fixed to one database */
83848 ){
83849   int i;
83850   struct ExprList_item *pItem;
83851   if( pList==0 ) return 0;
83852   for(i=0, pItem=pList->a; i<pList->nExpr; i++, pItem++){
83853     if( sqlite3FixExpr(pFix, pItem->pExpr) ){
83854       return 1;
83855     }
83856   }
83857   return 0;
83858 }
83859 #endif
83860 
83861 #ifndef SQLITE_OMIT_TRIGGER
83862 SQLITE_PRIVATE int sqlite3FixTriggerStep(
83863   DbFixer *pFix,     /* Context of the fixation */
83864   TriggerStep *pStep /* The trigger step be fixed to one database */
83865 ){
83866   while( pStep ){
83867     if( sqlite3FixSelect(pFix, pStep->pSelect) ){
83868       return 1;
83869     }
83870     if( sqlite3FixExpr(pFix, pStep->pWhere) ){
83871       return 1;
83872     }
83873     if( sqlite3FixExprList(pFix, pStep->pExprList) ){
83874       return 1;
83875     }
83876     pStep = pStep->pNext;
83877   }
83878   return 0;
83879 }
83880 #endif
83881 
83882 /************** End of attach.c **********************************************/
83883 /************** Begin file auth.c ********************************************/
83884 /*
83885 ** 2003 January 11
83886 **
83887 ** The author disclaims copyright to this source code.  In place of
83888 ** a legal notice, here is a blessing:
83889 **
83890 **    May you do good and not evil.
83891 **    May you find forgiveness for yourself and forgive others.
83892 **    May you share freely, never taking more than you give.
83893 **
83894 *************************************************************************
83895 ** This file contains code used to implement the sqlite3_set_authorizer()
83896 ** API.  This facility is an optional feature of the library.  Embedded
83897 ** systems that do not need this facility may omit it by recompiling
83898 ** the library with -DSQLITE_OMIT_AUTHORIZATION=1
83899 */
83900 
83901 /*
83902 ** All of the code in this file may be omitted by defining a single
83903 ** macro.
83904 */
83905 #ifndef SQLITE_OMIT_AUTHORIZATION
83906 
83907 /*
83908 ** Set or clear the access authorization function.
83909 **
83910 ** The access authorization function is be called during the compilation
83911 ** phase to verify that the user has read and/or write access permission on
83912 ** various fields of the database.  The first argument to the auth function
83913 ** is a copy of the 3rd argument to this routine.  The second argument
83914 ** to the auth function is one of these constants:
83915 **
83916 **       SQLITE_CREATE_INDEX
83917 **       SQLITE_CREATE_TABLE
83918 **       SQLITE_CREATE_TEMP_INDEX
83919 **       SQLITE_CREATE_TEMP_TABLE
83920 **       SQLITE_CREATE_TEMP_TRIGGER
83921 **       SQLITE_CREATE_TEMP_VIEW
83922 **       SQLITE_CREATE_TRIGGER
83923 **       SQLITE_CREATE_VIEW
83924 **       SQLITE_DELETE
83925 **       SQLITE_DROP_INDEX
83926 **       SQLITE_DROP_TABLE
83927 **       SQLITE_DROP_TEMP_INDEX
83928 **       SQLITE_DROP_TEMP_TABLE
83929 **       SQLITE_DROP_TEMP_TRIGGER
83930 **       SQLITE_DROP_TEMP_VIEW
83931 **       SQLITE_DROP_TRIGGER
83932 **       SQLITE_DROP_VIEW
83933 **       SQLITE_INSERT
83934 **       SQLITE_PRAGMA
83935 **       SQLITE_READ
83936 **       SQLITE_SELECT
83937 **       SQLITE_TRANSACTION
83938 **       SQLITE_UPDATE
83939 **
83940 ** The third and fourth arguments to the auth function are the name of
83941 ** the table and the column that are being accessed.  The auth function
83942 ** should return either SQLITE_OK, SQLITE_DENY, or SQLITE_IGNORE.  If
83943 ** SQLITE_OK is returned, it means that access is allowed.  SQLITE_DENY
83944 ** means that the SQL statement will never-run - the sqlite3_exec() call
83945 ** will return with an error.  SQLITE_IGNORE means that the SQL statement
83946 ** should run but attempts to read the specified column will return NULL
83947 ** and attempts to write the column will be ignored.
83948 **
83949 ** Setting the auth function to NULL disables this hook.  The default
83950 ** setting of the auth function is NULL.
83951 */
83952 SQLITE_API int sqlite3_set_authorizer(
83953   sqlite3 *db,
83954   int (*xAuth)(void*,int,const char*,const char*,const char*,const char*),
83955   void *pArg
83956 ){
83957   sqlite3_mutex_enter(db->mutex);
83958   db->xAuth = xAuth;
83959   db->pAuthArg = pArg;
83960   sqlite3ExpirePreparedStatements(db);
83961   sqlite3_mutex_leave(db->mutex);
83962   return SQLITE_OK;
83963 }
83964 
83965 /*
83966 ** Write an error message into pParse->zErrMsg that explains that the
83967 ** user-supplied authorization function returned an illegal value.
83968 */
83969 static void sqliteAuthBadReturnCode(Parse *pParse){
83970   sqlite3ErrorMsg(pParse, "authorizer malfunction");
83971   pParse->rc = SQLITE_ERROR;
83972 }
83973 
83974 /*
83975 ** Invoke the authorization callback for permission to read column zCol from
83976 ** table zTab in database zDb. This function assumes that an authorization
83977 ** callback has been registered (i.e. that sqlite3.xAuth is not NULL).
83978 **
83979 ** If SQLITE_IGNORE is returned and pExpr is not NULL, then pExpr is changed
83980 ** to an SQL NULL expression. Otherwise, if pExpr is NULL, then SQLITE_IGNORE
83981 ** is treated as SQLITE_DENY. In this case an error is left in pParse.
83982 */
83983 SQLITE_PRIVATE int sqlite3AuthReadCol(
83984   Parse *pParse,                  /* The parser context */
83985   const char *zTab,               /* Table name */
83986   const char *zCol,               /* Column name */
83987   int iDb                         /* Index of containing database. */
83988 ){
83989   sqlite3 *db = pParse->db;       /* Database handle */
83990   char *zDb = db->aDb[iDb].zName; /* Name of attached database */
83991   int rc;                         /* Auth callback return code */
83992 
83993   rc = db->xAuth(db->pAuthArg, SQLITE_READ, zTab,zCol,zDb,pParse->zAuthContext);
83994   if( rc==SQLITE_DENY ){
83995     if( db->nDb>2 || iDb!=0 ){
83996       sqlite3ErrorMsg(pParse, "access to %s.%s.%s is prohibited",zDb,zTab,zCol);
83997     }else{
83998       sqlite3ErrorMsg(pParse, "access to %s.%s is prohibited", zTab, zCol);
83999     }
84000     pParse->rc = SQLITE_AUTH;
84001   }else if( rc!=SQLITE_IGNORE && rc!=SQLITE_OK ){
84002     sqliteAuthBadReturnCode(pParse);
84003   }
84004   return rc;
84005 }
84006 
84007 /*
84008 ** The pExpr should be a TK_COLUMN expression.  The table referred to
84009 ** is in pTabList or else it is the NEW or OLD table of a trigger.
84010 ** Check to see if it is OK to read this particular column.
84011 **
84012 ** If the auth function returns SQLITE_IGNORE, change the TK_COLUMN
84013 ** instruction into a TK_NULL.  If the auth function returns SQLITE_DENY,
84014 ** then generate an error.
84015 */
84016 SQLITE_PRIVATE void sqlite3AuthRead(
84017   Parse *pParse,        /* The parser context */
84018   Expr *pExpr,          /* The expression to check authorization on */
84019   Schema *pSchema,      /* The schema of the expression */
84020   SrcList *pTabList     /* All table that pExpr might refer to */
84021 ){
84022   sqlite3 *db = pParse->db;
84023   Table *pTab = 0;      /* The table being read */
84024   const char *zCol;     /* Name of the column of the table */
84025   int iSrc;             /* Index in pTabList->a[] of table being read */
84026   int iDb;              /* The index of the database the expression refers to */
84027   int iCol;             /* Index of column in table */
84028 
84029   if( db->xAuth==0 ) return;
84030   iDb = sqlite3SchemaToIndex(pParse->db, pSchema);
84031   if( iDb<0 ){
84032     /* An attempt to read a column out of a subquery or other
84033     ** temporary table. */
84034     return;
84035   }
84036 
84037   assert( pExpr->op==TK_COLUMN || pExpr->op==TK_TRIGGER );
84038   if( pExpr->op==TK_TRIGGER ){
84039     pTab = pParse->pTriggerTab;
84040   }else{
84041     assert( pTabList );
84042     for(iSrc=0; ALWAYS(iSrc<pTabList->nSrc); iSrc++){
84043       if( pExpr->iTable==pTabList->a[iSrc].iCursor ){
84044         pTab = pTabList->a[iSrc].pTab;
84045         break;
84046       }
84047     }
84048   }
84049   iCol = pExpr->iColumn;
84050   if( NEVER(pTab==0) ) return;
84051 
84052   if( iCol>=0 ){
84053     assert( iCol<pTab->nCol );
84054     zCol = pTab->aCol[iCol].zName;
84055   }else if( pTab->iPKey>=0 ){
84056     assert( pTab->iPKey<pTab->nCol );
84057     zCol = pTab->aCol[pTab->iPKey].zName;
84058   }else{
84059     zCol = "ROWID";
84060   }
84061   assert( iDb>=0 && iDb<db->nDb );
84062   if( SQLITE_IGNORE==sqlite3AuthReadCol(pParse, pTab->zName, zCol, iDb) ){
84063     pExpr->op = TK_NULL;
84064   }
84065 }
84066 
84067 /*
84068 ** Do an authorization check using the code and arguments given.  Return
84069 ** either SQLITE_OK (zero) or SQLITE_IGNORE or SQLITE_DENY.  If SQLITE_DENY
84070 ** is returned, then the error count and error message in pParse are
84071 ** modified appropriately.
84072 */
84073 SQLITE_PRIVATE int sqlite3AuthCheck(
84074   Parse *pParse,
84075   int code,
84076   const char *zArg1,
84077   const char *zArg2,
84078   const char *zArg3
84079 ){
84080   sqlite3 *db = pParse->db;
84081   int rc;
84082 
84083   /* Don't do any authorization checks if the database is initialising
84084   ** or if the parser is being invoked from within sqlite3_declare_vtab.
84085   */
84086   if( db->init.busy || IN_DECLARE_VTAB ){
84087     return SQLITE_OK;
84088   }
84089 
84090   if( db->xAuth==0 ){
84091     return SQLITE_OK;
84092   }
84093   rc = db->xAuth(db->pAuthArg, code, zArg1, zArg2, zArg3, pParse->zAuthContext);
84094   if( rc==SQLITE_DENY ){
84095     sqlite3ErrorMsg(pParse, "not authorized");
84096     pParse->rc = SQLITE_AUTH;
84097   }else if( rc!=SQLITE_OK && rc!=SQLITE_IGNORE ){
84098     rc = SQLITE_DENY;
84099     sqliteAuthBadReturnCode(pParse);
84100   }
84101   return rc;
84102 }
84103 
84104 /*
84105 ** Push an authorization context.  After this routine is called, the
84106 ** zArg3 argument to authorization callbacks will be zContext until
84107 ** popped.  Or if pParse==0, this routine is a no-op.
84108 */
84109 SQLITE_PRIVATE void sqlite3AuthContextPush(
84110   Parse *pParse,
84111   AuthContext *pContext,
84112   const char *zContext
84113 ){
84114   assert( pParse );
84115   pContext->pParse = pParse;
84116   pContext->zAuthContext = pParse->zAuthContext;
84117   pParse->zAuthContext = zContext;
84118 }
84119 
84120 /*
84121 ** Pop an authorization context that was previously pushed
84122 ** by sqlite3AuthContextPush
84123 */
84124 SQLITE_PRIVATE void sqlite3AuthContextPop(AuthContext *pContext){
84125   if( pContext->pParse ){
84126     pContext->pParse->zAuthContext = pContext->zAuthContext;
84127     pContext->pParse = 0;
84128   }
84129 }
84130 
84131 #endif /* SQLITE_OMIT_AUTHORIZATION */
84132 
84133 /************** End of auth.c ************************************************/
84134 /************** Begin file build.c *******************************************/
84135 /*
84136 ** 2001 September 15
84137 **
84138 ** The author disclaims copyright to this source code.  In place of
84139 ** a legal notice, here is a blessing:
84140 **
84141 **    May you do good and not evil.
84142 **    May you find forgiveness for yourself and forgive others.
84143 **    May you share freely, never taking more than you give.
84144 **
84145 *************************************************************************
84146 ** This file contains C code routines that are called by the SQLite parser
84147 ** when syntax rules are reduced.  The routines in this file handle the
84148 ** following kinds of SQL syntax:
84149 **
84150 **     CREATE TABLE
84151 **     DROP TABLE
84152 **     CREATE INDEX
84153 **     DROP INDEX
84154 **     creating ID lists
84155 **     BEGIN TRANSACTION
84156 **     COMMIT
84157 **     ROLLBACK
84158 */
84159 
84160 /*
84161 ** This routine is called when a new SQL statement is beginning to
84162 ** be parsed.  Initialize the pParse structure as needed.
84163 */
84164 SQLITE_PRIVATE void sqlite3BeginParse(Parse *pParse, int explainFlag){
84165   pParse->explain = (u8)explainFlag;
84166   pParse->nVar = 0;
84167 }
84168 
84169 #ifndef SQLITE_OMIT_SHARED_CACHE
84170 /*
84171 ** The TableLock structure is only used by the sqlite3TableLock() and
84172 ** codeTableLocks() functions.
84173 */
84174 struct TableLock {
84175   int iDb;             /* The database containing the table to be locked */
84176   int iTab;            /* The root page of the table to be locked */
84177   u8 isWriteLock;      /* True for write lock.  False for a read lock */
84178   const char *zName;   /* Name of the table */
84179 };
84180 
84181 /*
84182 ** Record the fact that we want to lock a table at run-time.
84183 **
84184 ** The table to be locked has root page iTab and is found in database iDb.
84185 ** A read or a write lock can be taken depending on isWritelock.
84186 **
84187 ** This routine just records the fact that the lock is desired.  The
84188 ** code to make the lock occur is generated by a later call to
84189 ** codeTableLocks() which occurs during sqlite3FinishCoding().
84190 */
84191 SQLITE_PRIVATE void sqlite3TableLock(
84192   Parse *pParse,     /* Parsing context */
84193   int iDb,           /* Index of the database containing the table to lock */
84194   int iTab,          /* Root page number of the table to be locked */
84195   u8 isWriteLock,    /* True for a write lock */
84196   const char *zName  /* Name of the table to be locked */
84197 ){
84198   Parse *pToplevel = sqlite3ParseToplevel(pParse);
84199   int i;
84200   int nBytes;
84201   TableLock *p;
84202   assert( iDb>=0 );
84203 
84204   for(i=0; i<pToplevel->nTableLock; i++){
84205     p = &pToplevel->aTableLock[i];
84206     if( p->iDb==iDb && p->iTab==iTab ){
84207       p->isWriteLock = (p->isWriteLock || isWriteLock);
84208       return;
84209     }
84210   }
84211 
84212   nBytes = sizeof(TableLock) * (pToplevel->nTableLock+1);
84213   pToplevel->aTableLock =
84214       sqlite3DbReallocOrFree(pToplevel->db, pToplevel->aTableLock, nBytes);
84215   if( pToplevel->aTableLock ){
84216     p = &pToplevel->aTableLock[pToplevel->nTableLock++];
84217     p->iDb = iDb;
84218     p->iTab = iTab;
84219     p->isWriteLock = isWriteLock;
84220     p->zName = zName;
84221   }else{
84222     pToplevel->nTableLock = 0;
84223     pToplevel->db->mallocFailed = 1;
84224   }
84225 }
84226 
84227 /*
84228 ** Code an OP_TableLock instruction for each table locked by the
84229 ** statement (configured by calls to sqlite3TableLock()).
84230 */
84231 static void codeTableLocks(Parse *pParse){
84232   int i;
84233   Vdbe *pVdbe;
84234 
84235   pVdbe = sqlite3GetVdbe(pParse);
84236   assert( pVdbe!=0 ); /* sqlite3GetVdbe cannot fail: VDBE already allocated */
84237 
84238   for(i=0; i<pParse->nTableLock; i++){
84239     TableLock *p = &pParse->aTableLock[i];
84240     int p1 = p->iDb;
84241     sqlite3VdbeAddOp4(pVdbe, OP_TableLock, p1, p->iTab, p->isWriteLock,
84242                       p->zName, P4_STATIC);
84243   }
84244 }
84245 #else
84246   #define codeTableLocks(x)
84247 #endif
84248 
84249 /*
84250 ** This routine is called after a single SQL statement has been
84251 ** parsed and a VDBE program to execute that statement has been
84252 ** prepared.  This routine puts the finishing touches on the
84253 ** VDBE program and resets the pParse structure for the next
84254 ** parse.
84255 **
84256 ** Note that if an error occurred, it might be the case that
84257 ** no VDBE code was generated.
84258 */
84259 SQLITE_PRIVATE void sqlite3FinishCoding(Parse *pParse){
84260   sqlite3 *db;
84261   Vdbe *v;
84262 
84263   assert( pParse->pToplevel==0 );
84264   db = pParse->db;
84265   if( db->mallocFailed ) return;
84266   if( pParse->nested ) return;
84267   if( pParse->nErr ) return;
84268 
84269   /* Begin by generating some termination code at the end of the
84270   ** vdbe program
84271   */
84272   v = sqlite3GetVdbe(pParse);
84273   assert( !pParse->isMultiWrite
84274        || sqlite3VdbeAssertMayAbort(v, pParse->mayAbort));
84275   if( v ){
84276     while( sqlite3VdbeDeletePriorOpcode(v, OP_Close) ){}
84277     sqlite3VdbeAddOp0(v, OP_Halt);
84278 
84279     /* The cookie mask contains one bit for each database file open.
84280     ** (Bit 0 is for main, bit 1 is for temp, and so forth.)  Bits are
84281     ** set for each database that is used.  Generate code to start a
84282     ** transaction on each used database and to verify the schema cookie
84283     ** on each used database.
84284     */
84285     if( pParse->cookieGoto>0 ){
84286       yDbMask mask;
84287       int iDb, i, addr;
84288       sqlite3VdbeJumpHere(v, pParse->cookieGoto-1);
84289       for(iDb=0, mask=1; iDb<db->nDb; mask<<=1, iDb++){
84290         if( (mask & pParse->cookieMask)==0 ) continue;
84291         sqlite3VdbeUsesBtree(v, iDb);
84292         sqlite3VdbeAddOp2(v,OP_Transaction, iDb, (mask & pParse->writeMask)!=0);
84293         if( db->init.busy==0 ){
84294           assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
84295           sqlite3VdbeAddOp3(v, OP_VerifyCookie,
84296                             iDb, pParse->cookieValue[iDb],
84297                             db->aDb[iDb].pSchema->iGeneration);
84298         }
84299       }
84300 #ifndef SQLITE_OMIT_VIRTUALTABLE
84301       for(i=0; i<pParse->nVtabLock; i++){
84302         char *vtab = (char *)sqlite3GetVTable(db, pParse->apVtabLock[i]);
84303         sqlite3VdbeAddOp4(v, OP_VBegin, 0, 0, 0, vtab, P4_VTAB);
84304       }
84305       pParse->nVtabLock = 0;
84306 #endif
84307 
84308       /* Once all the cookies have been verified and transactions opened,
84309       ** obtain the required table-locks. This is a no-op unless the
84310       ** shared-cache feature is enabled.
84311       */
84312       codeTableLocks(pParse);
84313 
84314       /* Initialize any AUTOINCREMENT data structures required.
84315       */
84316       sqlite3AutoincrementBegin(pParse);
84317 
84318       /* Code constant expressions that where factored out of inner loops */
84319       addr = pParse->cookieGoto;
84320       if( pParse->pConstExpr ){
84321         ExprList *pEL = pParse->pConstExpr;
84322         pParse->cookieGoto = 0;
84323         for(i=0; i<pEL->nExpr; i++){
84324           sqlite3ExprCode(pParse, pEL->a[i].pExpr, pEL->a[i].u.iConstExprReg);
84325         }
84326       }
84327 
84328       /* Finally, jump back to the beginning of the executable code. */
84329       sqlite3VdbeAddOp2(v, OP_Goto, 0, addr);
84330     }
84331   }
84332 
84333 
84334   /* Get the VDBE program ready for execution
84335   */
84336   if( v && ALWAYS(pParse->nErr==0) && !db->mallocFailed ){
84337     assert( pParse->iCacheLevel==0 );  /* Disables and re-enables match */
84338     /* A minimum of one cursor is required if autoincrement is used
84339     *  See ticket [a696379c1f08866] */
84340     if( pParse->pAinc!=0 && pParse->nTab==0 ) pParse->nTab = 1;
84341     sqlite3VdbeMakeReady(v, pParse);
84342     pParse->rc = SQLITE_DONE;
84343     pParse->colNamesSet = 0;
84344   }else{
84345     pParse->rc = SQLITE_ERROR;
84346   }
84347   pParse->nTab = 0;
84348   pParse->nMem = 0;
84349   pParse->nSet = 0;
84350   pParse->nVar = 0;
84351   pParse->cookieMask = 0;
84352   pParse->cookieGoto = 0;
84353 }
84354 
84355 /*
84356 ** Run the parser and code generator recursively in order to generate
84357 ** code for the SQL statement given onto the end of the pParse context
84358 ** currently under construction.  When the parser is run recursively
84359 ** this way, the final OP_Halt is not appended and other initialization
84360 ** and finalization steps are omitted because those are handling by the
84361 ** outermost parser.
84362 **
84363 ** Not everything is nestable.  This facility is designed to permit
84364 ** INSERT, UPDATE, and DELETE operations against SQLITE_MASTER.  Use
84365 ** care if you decide to try to use this routine for some other purposes.
84366 */
84367 SQLITE_PRIVATE void sqlite3NestedParse(Parse *pParse, const char *zFormat, ...){
84368   va_list ap;
84369   char *zSql;
84370   char *zErrMsg = 0;
84371   sqlite3 *db = pParse->db;
84372 # define SAVE_SZ  (sizeof(Parse) - offsetof(Parse,nVar))
84373   char saveBuf[SAVE_SZ];
84374 
84375   if( pParse->nErr ) return;
84376   assert( pParse->nested<10 );  /* Nesting should only be of limited depth */
84377   va_start(ap, zFormat);
84378   zSql = sqlite3VMPrintf(db, zFormat, ap);
84379   va_end(ap);
84380   if( zSql==0 ){
84381     return;   /* A malloc must have failed */
84382   }
84383   pParse->nested++;
84384   memcpy(saveBuf, &pParse->nVar, SAVE_SZ);
84385   memset(&pParse->nVar, 0, SAVE_SZ);
84386   sqlite3RunParser(pParse, zSql, &zErrMsg);
84387   sqlite3DbFree(db, zErrMsg);
84388   sqlite3DbFree(db, zSql);
84389   memcpy(&pParse->nVar, saveBuf, SAVE_SZ);
84390   pParse->nested--;
84391 }
84392 
84393 /*
84394 ** Locate the in-memory structure that describes a particular database
84395 ** table given the name of that table and (optionally) the name of the
84396 ** database containing the table.  Return NULL if not found.
84397 **
84398 ** If zDatabase is 0, all databases are searched for the table and the
84399 ** first matching table is returned.  (No checking for duplicate table
84400 ** names is done.)  The search order is TEMP first, then MAIN, then any
84401 ** auxiliary databases added using the ATTACH command.
84402 **
84403 ** See also sqlite3LocateTable().
84404 */
84405 SQLITE_PRIVATE Table *sqlite3FindTable(sqlite3 *db, const char *zName, const char *zDatabase){
84406   Table *p = 0;
84407   int i;
84408   int nName;
84409   assert( zName!=0 );
84410   nName = sqlite3Strlen30(zName);
84411   /* All mutexes are required for schema access.  Make sure we hold them. */
84412   assert( zDatabase!=0 || sqlite3BtreeHoldsAllMutexes(db) );
84413   for(i=OMIT_TEMPDB; i<db->nDb; i++){
84414     int j = (i<2) ? i^1 : i;   /* Search TEMP before MAIN */
84415     if( zDatabase!=0 && sqlite3StrICmp(zDatabase, db->aDb[j].zName) ) continue;
84416     assert( sqlite3SchemaMutexHeld(db, j, 0) );
84417     p = sqlite3HashFind(&db->aDb[j].pSchema->tblHash, zName, nName);
84418     if( p ) break;
84419   }
84420   return p;
84421 }
84422 
84423 /*
84424 ** Locate the in-memory structure that describes a particular database
84425 ** table given the name of that table and (optionally) the name of the
84426 ** database containing the table.  Return NULL if not found.  Also leave an
84427 ** error message in pParse->zErrMsg.
84428 **
84429 ** The difference between this routine and sqlite3FindTable() is that this
84430 ** routine leaves an error message in pParse->zErrMsg where
84431 ** sqlite3FindTable() does not.
84432 */
84433 SQLITE_PRIVATE Table *sqlite3LocateTable(
84434   Parse *pParse,         /* context in which to report errors */
84435   int isView,            /* True if looking for a VIEW rather than a TABLE */
84436   const char *zName,     /* Name of the table we are looking for */
84437   const char *zDbase     /* Name of the database.  Might be NULL */
84438 ){
84439   Table *p;
84440 
84441   /* Read the database schema. If an error occurs, leave an error message
84442   ** and code in pParse and return NULL. */
84443   if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
84444     return 0;
84445   }
84446 
84447   p = sqlite3FindTable(pParse->db, zName, zDbase);
84448   if( p==0 ){
84449     const char *zMsg = isView ? "no such view" : "no such table";
84450     if( zDbase ){
84451       sqlite3ErrorMsg(pParse, "%s: %s.%s", zMsg, zDbase, zName);
84452     }else{
84453       sqlite3ErrorMsg(pParse, "%s: %s", zMsg, zName);
84454     }
84455     pParse->checkSchema = 1;
84456   }
84457   return p;
84458 }
84459 
84460 /*
84461 ** Locate the table identified by *p.
84462 **
84463 ** This is a wrapper around sqlite3LocateTable(). The difference between
84464 ** sqlite3LocateTable() and this function is that this function restricts
84465 ** the search to schema (p->pSchema) if it is not NULL. p->pSchema may be
84466 ** non-NULL if it is part of a view or trigger program definition. See
84467 ** sqlite3FixSrcList() for details.
84468 */
84469 SQLITE_PRIVATE Table *sqlite3LocateTableItem(
84470   Parse *pParse,
84471   int isView,
84472   struct SrcList_item *p
84473 ){
84474   const char *zDb;
84475   assert( p->pSchema==0 || p->zDatabase==0 );
84476   if( p->pSchema ){
84477     int iDb = sqlite3SchemaToIndex(pParse->db, p->pSchema);
84478     zDb = pParse->db->aDb[iDb].zName;
84479   }else{
84480     zDb = p->zDatabase;
84481   }
84482   return sqlite3LocateTable(pParse, isView, p->zName, zDb);
84483 }
84484 
84485 /*
84486 ** Locate the in-memory structure that describes
84487 ** a particular index given the name of that index
84488 ** and the name of the database that contains the index.
84489 ** Return NULL if not found.
84490 **
84491 ** If zDatabase is 0, all databases are searched for the
84492 ** table and the first matching index is returned.  (No checking
84493 ** for duplicate index names is done.)  The search order is
84494 ** TEMP first, then MAIN, then any auxiliary databases added
84495 ** using the ATTACH command.
84496 */
84497 SQLITE_PRIVATE Index *sqlite3FindIndex(sqlite3 *db, const char *zName, const char *zDb){
84498   Index *p = 0;
84499   int i;
84500   int nName = sqlite3Strlen30(zName);
84501   /* All mutexes are required for schema access.  Make sure we hold them. */
84502   assert( zDb!=0 || sqlite3BtreeHoldsAllMutexes(db) );
84503   for(i=OMIT_TEMPDB; i<db->nDb; i++){
84504     int j = (i<2) ? i^1 : i;  /* Search TEMP before MAIN */
84505     Schema *pSchema = db->aDb[j].pSchema;
84506     assert( pSchema );
84507     if( zDb && sqlite3StrICmp(zDb, db->aDb[j].zName) ) continue;
84508     assert( sqlite3SchemaMutexHeld(db, j, 0) );
84509     p = sqlite3HashFind(&pSchema->idxHash, zName, nName);
84510     if( p ) break;
84511   }
84512   return p;
84513 }
84514 
84515 /*
84516 ** Reclaim the memory used by an index
84517 */
84518 static void freeIndex(sqlite3 *db, Index *p){
84519 #ifndef SQLITE_OMIT_ANALYZE
84520   sqlite3DeleteIndexSamples(db, p);
84521 #endif
84522   if( db==0 || db->pnBytesFreed==0 ) sqlite3KeyInfoUnref(p->pKeyInfo);
84523   sqlite3ExprDelete(db, p->pPartIdxWhere);
84524   sqlite3DbFree(db, p->zColAff);
84525   if( p->isResized ) sqlite3DbFree(db, p->azColl);
84526   sqlite3DbFree(db, p);
84527 }
84528 
84529 /*
84530 ** For the index called zIdxName which is found in the database iDb,
84531 ** unlike that index from its Table then remove the index from
84532 ** the index hash table and free all memory structures associated
84533 ** with the index.
84534 */
84535 SQLITE_PRIVATE void sqlite3UnlinkAndDeleteIndex(sqlite3 *db, int iDb, const char *zIdxName){
84536   Index *pIndex;
84537   int len;
84538   Hash *pHash;
84539 
84540   assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
84541   pHash = &db->aDb[iDb].pSchema->idxHash;
84542   len = sqlite3Strlen30(zIdxName);
84543   pIndex = sqlite3HashInsert(pHash, zIdxName, len, 0);
84544   if( ALWAYS(pIndex) ){
84545     if( pIndex->pTable->pIndex==pIndex ){
84546       pIndex->pTable->pIndex = pIndex->pNext;
84547     }else{
84548       Index *p;
84549       /* Justification of ALWAYS();  The index must be on the list of
84550       ** indices. */
84551       p = pIndex->pTable->pIndex;
84552       while( ALWAYS(p) && p->pNext!=pIndex ){ p = p->pNext; }
84553       if( ALWAYS(p && p->pNext==pIndex) ){
84554         p->pNext = pIndex->pNext;
84555       }
84556     }
84557     freeIndex(db, pIndex);
84558   }
84559   db->flags |= SQLITE_InternChanges;
84560 }
84561 
84562 /*
84563 ** Look through the list of open database files in db->aDb[] and if
84564 ** any have been closed, remove them from the list.  Reallocate the
84565 ** db->aDb[] structure to a smaller size, if possible.
84566 **
84567 ** Entry 0 (the "main" database) and entry 1 (the "temp" database)
84568 ** are never candidates for being collapsed.
84569 */
84570 SQLITE_PRIVATE void sqlite3CollapseDatabaseArray(sqlite3 *db){
84571   int i, j;
84572   for(i=j=2; i<db->nDb; i++){
84573     struct Db *pDb = &db->aDb[i];
84574     if( pDb->pBt==0 ){
84575       sqlite3DbFree(db, pDb->zName);
84576       pDb->zName = 0;
84577       continue;
84578     }
84579     if( j<i ){
84580       db->aDb[j] = db->aDb[i];
84581     }
84582     j++;
84583   }
84584   memset(&db->aDb[j], 0, (db->nDb-j)*sizeof(db->aDb[j]));
84585   db->nDb = j;
84586   if( db->nDb<=2 && db->aDb!=db->aDbStatic ){
84587     memcpy(db->aDbStatic, db->aDb, 2*sizeof(db->aDb[0]));
84588     sqlite3DbFree(db, db->aDb);
84589     db->aDb = db->aDbStatic;
84590   }
84591 }
84592 
84593 /*
84594 ** Reset the schema for the database at index iDb.  Also reset the
84595 ** TEMP schema.
84596 */
84597 SQLITE_PRIVATE void sqlite3ResetOneSchema(sqlite3 *db, int iDb){
84598   Db *pDb;
84599   assert( iDb<db->nDb );
84600 
84601   /* Case 1:  Reset the single schema identified by iDb */
84602   pDb = &db->aDb[iDb];
84603   assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
84604   assert( pDb->pSchema!=0 );
84605   sqlite3SchemaClear(pDb->pSchema);
84606 
84607   /* If any database other than TEMP is reset, then also reset TEMP
84608   ** since TEMP might be holding triggers that reference tables in the
84609   ** other database.
84610   */
84611   if( iDb!=1 ){
84612     pDb = &db->aDb[1];
84613     assert( pDb->pSchema!=0 );
84614     sqlite3SchemaClear(pDb->pSchema);
84615   }
84616   return;
84617 }
84618 
84619 /*
84620 ** Erase all schema information from all attached databases (including
84621 ** "main" and "temp") for a single database connection.
84622 */
84623 SQLITE_PRIVATE void sqlite3ResetAllSchemasOfConnection(sqlite3 *db){
84624   int i;
84625   sqlite3BtreeEnterAll(db);
84626   for(i=0; i<db->nDb; i++){
84627     Db *pDb = &db->aDb[i];
84628     if( pDb->pSchema ){
84629       sqlite3SchemaClear(pDb->pSchema);
84630     }
84631   }
84632   db->flags &= ~SQLITE_InternChanges;
84633   sqlite3VtabUnlockList(db);
84634   sqlite3BtreeLeaveAll(db);
84635   sqlite3CollapseDatabaseArray(db);
84636 }
84637 
84638 /*
84639 ** This routine is called when a commit occurs.
84640 */
84641 SQLITE_PRIVATE void sqlite3CommitInternalChanges(sqlite3 *db){
84642   db->flags &= ~SQLITE_InternChanges;
84643 }
84644 
84645 /*
84646 ** Delete memory allocated for the column names of a table or view (the
84647 ** Table.aCol[] array).
84648 */
84649 static void sqliteDeleteColumnNames(sqlite3 *db, Table *pTable){
84650   int i;
84651   Column *pCol;
84652   assert( pTable!=0 );
84653   if( (pCol = pTable->aCol)!=0 ){
84654     for(i=0; i<pTable->nCol; i++, pCol++){
84655       sqlite3DbFree(db, pCol->zName);
84656       sqlite3ExprDelete(db, pCol->pDflt);
84657       sqlite3DbFree(db, pCol->zDflt);
84658       sqlite3DbFree(db, pCol->zType);
84659       sqlite3DbFree(db, pCol->zColl);
84660     }
84661     sqlite3DbFree(db, pTable->aCol);
84662   }
84663 }
84664 
84665 /*
84666 ** Remove the memory data structures associated with the given
84667 ** Table.  No changes are made to disk by this routine.
84668 **
84669 ** This routine just deletes the data structure.  It does not unlink
84670 ** the table data structure from the hash table.  But it does destroy
84671 ** memory structures of the indices and foreign keys associated with
84672 ** the table.
84673 **
84674 ** The db parameter is optional.  It is needed if the Table object
84675 ** contains lookaside memory.  (Table objects in the schema do not use
84676 ** lookaside memory, but some ephemeral Table objects do.)  Or the
84677 ** db parameter can be used with db->pnBytesFreed to measure the memory
84678 ** used by the Table object.
84679 */
84680 SQLITE_PRIVATE void sqlite3DeleteTable(sqlite3 *db, Table *pTable){
84681   Index *pIndex, *pNext;
84682   TESTONLY( int nLookaside; ) /* Used to verify lookaside not used for schema */
84683 
84684   assert( !pTable || pTable->nRef>0 );
84685 
84686   /* Do not delete the table until the reference count reaches zero. */
84687   if( !pTable ) return;
84688   if( ((!db || db->pnBytesFreed==0) && (--pTable->nRef)>0) ) return;
84689 
84690   /* Record the number of outstanding lookaside allocations in schema Tables
84691   ** prior to doing any free() operations.  Since schema Tables do not use
84692   ** lookaside, this number should not change. */
84693   TESTONLY( nLookaside = (db && (pTable->tabFlags & TF_Ephemeral)==0) ?
84694                          db->lookaside.nOut : 0 );
84695 
84696   /* Delete all indices associated with this table. */
84697   for(pIndex = pTable->pIndex; pIndex; pIndex=pNext){
84698     pNext = pIndex->pNext;
84699     assert( pIndex->pSchema==pTable->pSchema );
84700     if( !db || db->pnBytesFreed==0 ){
84701       char *zName = pIndex->zName;
84702       TESTONLY ( Index *pOld = ) sqlite3HashInsert(
84703          &pIndex->pSchema->idxHash, zName, sqlite3Strlen30(zName), 0
84704       );
84705       assert( db==0 || sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) );
84706       assert( pOld==pIndex || pOld==0 );
84707     }
84708     freeIndex(db, pIndex);
84709   }
84710 
84711   /* Delete any foreign keys attached to this table. */
84712   sqlite3FkDelete(db, pTable);
84713 
84714   /* Delete the Table structure itself.
84715   */
84716   sqliteDeleteColumnNames(db, pTable);
84717   sqlite3DbFree(db, pTable->zName);
84718   sqlite3DbFree(db, pTable->zColAff);
84719   sqlite3SelectDelete(db, pTable->pSelect);
84720 #ifndef SQLITE_OMIT_CHECK
84721   sqlite3ExprListDelete(db, pTable->pCheck);
84722 #endif
84723 #ifndef SQLITE_OMIT_VIRTUALTABLE
84724   sqlite3VtabClear(db, pTable);
84725 #endif
84726   sqlite3DbFree(db, pTable);
84727 
84728   /* Verify that no lookaside memory was used by schema tables */
84729   assert( nLookaside==0 || nLookaside==db->lookaside.nOut );
84730 }
84731 
84732 /*
84733 ** Unlink the given table from the hash tables and the delete the
84734 ** table structure with all its indices and foreign keys.
84735 */
84736 SQLITE_PRIVATE void sqlite3UnlinkAndDeleteTable(sqlite3 *db, int iDb, const char *zTabName){
84737   Table *p;
84738   Db *pDb;
84739 
84740   assert( db!=0 );
84741   assert( iDb>=0 && iDb<db->nDb );
84742   assert( zTabName );
84743   assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
84744   testcase( zTabName[0]==0 );  /* Zero-length table names are allowed */
84745   pDb = &db->aDb[iDb];
84746   p = sqlite3HashInsert(&pDb->pSchema->tblHash, zTabName,
84747                         sqlite3Strlen30(zTabName),0);
84748   sqlite3DeleteTable(db, p);
84749   db->flags |= SQLITE_InternChanges;
84750 }
84751 
84752 /*
84753 ** Given a token, return a string that consists of the text of that
84754 ** token.  Space to hold the returned string
84755 ** is obtained from sqliteMalloc() and must be freed by the calling
84756 ** function.
84757 **
84758 ** Any quotation marks (ex:  "name", 'name', [name], or `name`) that
84759 ** surround the body of the token are removed.
84760 **
84761 ** Tokens are often just pointers into the original SQL text and so
84762 ** are not \000 terminated and are not persistent.  The returned string
84763 ** is \000 terminated and is persistent.
84764 */
84765 SQLITE_PRIVATE char *sqlite3NameFromToken(sqlite3 *db, Token *pName){
84766   char *zName;
84767   if( pName ){
84768     zName = sqlite3DbStrNDup(db, (char*)pName->z, pName->n);
84769     sqlite3Dequote(zName);
84770   }else{
84771     zName = 0;
84772   }
84773   return zName;
84774 }
84775 
84776 /*
84777 ** Open the sqlite_master table stored in database number iDb for
84778 ** writing. The table is opened using cursor 0.
84779 */
84780 SQLITE_PRIVATE void sqlite3OpenMasterTable(Parse *p, int iDb){
84781   Vdbe *v = sqlite3GetVdbe(p);
84782   sqlite3TableLock(p, iDb, MASTER_ROOT, 1, SCHEMA_TABLE(iDb));
84783   sqlite3VdbeAddOp4Int(v, OP_OpenWrite, 0, MASTER_ROOT, iDb, 5);
84784   if( p->nTab==0 ){
84785     p->nTab = 1;
84786   }
84787 }
84788 
84789 /*
84790 ** Parameter zName points to a nul-terminated buffer containing the name
84791 ** of a database ("main", "temp" or the name of an attached db). This
84792 ** function returns the index of the named database in db->aDb[], or
84793 ** -1 if the named db cannot be found.
84794 */
84795 SQLITE_PRIVATE int sqlite3FindDbName(sqlite3 *db, const char *zName){
84796   int i = -1;         /* Database number */
84797   if( zName ){
84798     Db *pDb;
84799     int n = sqlite3Strlen30(zName);
84800     for(i=(db->nDb-1), pDb=&db->aDb[i]; i>=0; i--, pDb--){
84801       if( (!OMIT_TEMPDB || i!=1 ) && n==sqlite3Strlen30(pDb->zName) &&
84802           0==sqlite3StrICmp(pDb->zName, zName) ){
84803         break;
84804       }
84805     }
84806   }
84807   return i;
84808 }
84809 
84810 /*
84811 ** The token *pName contains the name of a database (either "main" or
84812 ** "temp" or the name of an attached db). This routine returns the
84813 ** index of the named database in db->aDb[], or -1 if the named db
84814 ** does not exist.
84815 */
84816 SQLITE_PRIVATE int sqlite3FindDb(sqlite3 *db, Token *pName){
84817   int i;                               /* Database number */
84818   char *zName;                         /* Name we are searching for */
84819   zName = sqlite3NameFromToken(db, pName);
84820   i = sqlite3FindDbName(db, zName);
84821   sqlite3DbFree(db, zName);
84822   return i;
84823 }
84824 
84825 /* The table or view or trigger name is passed to this routine via tokens
84826 ** pName1 and pName2. If the table name was fully qualified, for example:
84827 **
84828 ** CREATE TABLE xxx.yyy (...);
84829 **
84830 ** Then pName1 is set to "xxx" and pName2 "yyy". On the other hand if
84831 ** the table name is not fully qualified, i.e.:
84832 **
84833 ** CREATE TABLE yyy(...);
84834 **
84835 ** Then pName1 is set to "yyy" and pName2 is "".
84836 **
84837 ** This routine sets the *ppUnqual pointer to point at the token (pName1 or
84838 ** pName2) that stores the unqualified table name.  The index of the
84839 ** database "xxx" is returned.
84840 */
84841 SQLITE_PRIVATE int sqlite3TwoPartName(
84842   Parse *pParse,      /* Parsing and code generating context */
84843   Token *pName1,      /* The "xxx" in the name "xxx.yyy" or "xxx" */
84844   Token *pName2,      /* The "yyy" in the name "xxx.yyy" */
84845   Token **pUnqual     /* Write the unqualified object name here */
84846 ){
84847   int iDb;                    /* Database holding the object */
84848   sqlite3 *db = pParse->db;
84849 
84850   if( ALWAYS(pName2!=0) && pName2->n>0 ){
84851     if( db->init.busy ) {
84852       sqlite3ErrorMsg(pParse, "corrupt database");
84853       pParse->nErr++;
84854       return -1;
84855     }
84856     *pUnqual = pName2;
84857     iDb = sqlite3FindDb(db, pName1);
84858     if( iDb<0 ){
84859       sqlite3ErrorMsg(pParse, "unknown database %T", pName1);
84860       pParse->nErr++;
84861       return -1;
84862     }
84863   }else{
84864     assert( db->init.iDb==0 || db->init.busy );
84865     iDb = db->init.iDb;
84866     *pUnqual = pName1;
84867   }
84868   return iDb;
84869 }
84870 
84871 /*
84872 ** This routine is used to check if the UTF-8 string zName is a legal
84873 ** unqualified name for a new schema object (table, index, view or
84874 ** trigger). All names are legal except those that begin with the string
84875 ** "sqlite_" (in upper, lower or mixed case). This portion of the namespace
84876 ** is reserved for internal use.
84877 */
84878 SQLITE_PRIVATE int sqlite3CheckObjectName(Parse *pParse, const char *zName){
84879   if( !pParse->db->init.busy && pParse->nested==0
84880           && (pParse->db->flags & SQLITE_WriteSchema)==0
84881           && 0==sqlite3StrNICmp(zName, "sqlite_", 7) ){
84882     sqlite3ErrorMsg(pParse, "object name reserved for internal use: %s", zName);
84883     return SQLITE_ERROR;
84884   }
84885   return SQLITE_OK;
84886 }
84887 
84888 /*
84889 ** Return the PRIMARY KEY index of a table
84890 */
84891 SQLITE_PRIVATE Index *sqlite3PrimaryKeyIndex(Table *pTab){
84892   Index *p;
84893   for(p=pTab->pIndex; p && p->autoIndex!=2; p=p->pNext){}
84894   return p;
84895 }
84896 
84897 /*
84898 ** Return the column of index pIdx that corresponds to table
84899 ** column iCol.  Return -1 if not found.
84900 */
84901 SQLITE_PRIVATE i16 sqlite3ColumnOfIndex(Index *pIdx, i16 iCol){
84902   int i;
84903   for(i=0; i<pIdx->nColumn; i++){
84904     if( iCol==pIdx->aiColumn[i] ) return i;
84905   }
84906   return -1;
84907 }
84908 
84909 /*
84910 ** Begin constructing a new table representation in memory.  This is
84911 ** the first of several action routines that get called in response
84912 ** to a CREATE TABLE statement.  In particular, this routine is called
84913 ** after seeing tokens "CREATE" and "TABLE" and the table name. The isTemp
84914 ** flag is true if the table should be stored in the auxiliary database
84915 ** file instead of in the main database file.  This is normally the case
84916 ** when the "TEMP" or "TEMPORARY" keyword occurs in between
84917 ** CREATE and TABLE.
84918 **
84919 ** The new table record is initialized and put in pParse->pNewTable.
84920 ** As more of the CREATE TABLE statement is parsed, additional action
84921 ** routines will be called to add more information to this record.
84922 ** At the end of the CREATE TABLE statement, the sqlite3EndTable() routine
84923 ** is called to complete the construction of the new table record.
84924 */
84925 SQLITE_PRIVATE void sqlite3StartTable(
84926   Parse *pParse,   /* Parser context */
84927   Token *pName1,   /* First part of the name of the table or view */
84928   Token *pName2,   /* Second part of the name of the table or view */
84929   int isTemp,      /* True if this is a TEMP table */
84930   int isView,      /* True if this is a VIEW */
84931   int isVirtual,   /* True if this is a VIRTUAL table */
84932   int noErr        /* Do nothing if table already exists */
84933 ){
84934   Table *pTable;
84935   char *zName = 0; /* The name of the new table */
84936   sqlite3 *db = pParse->db;
84937   Vdbe *v;
84938   int iDb;         /* Database number to create the table in */
84939   Token *pName;    /* Unqualified name of the table to create */
84940 
84941   /* The table or view name to create is passed to this routine via tokens
84942   ** pName1 and pName2. If the table name was fully qualified, for example:
84943   **
84944   ** CREATE TABLE xxx.yyy (...);
84945   **
84946   ** Then pName1 is set to "xxx" and pName2 "yyy". On the other hand if
84947   ** the table name is not fully qualified, i.e.:
84948   **
84949   ** CREATE TABLE yyy(...);
84950   **
84951   ** Then pName1 is set to "yyy" and pName2 is "".
84952   **
84953   ** The call below sets the pName pointer to point at the token (pName1 or
84954   ** pName2) that stores the unqualified table name. The variable iDb is
84955   ** set to the index of the database that the table or view is to be
84956   ** created in.
84957   */
84958   iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
84959   if( iDb<0 ) return;
84960   if( !OMIT_TEMPDB && isTemp && pName2->n>0 && iDb!=1 ){
84961     /* If creating a temp table, the name may not be qualified. Unless
84962     ** the database name is "temp" anyway.  */
84963     sqlite3ErrorMsg(pParse, "temporary table name must be unqualified");
84964     return;
84965   }
84966   if( !OMIT_TEMPDB && isTemp ) iDb = 1;
84967 
84968   pParse->sNameToken = *pName;
84969   zName = sqlite3NameFromToken(db, pName);
84970   if( zName==0 ) return;
84971   if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
84972     goto begin_table_error;
84973   }
84974   if( db->init.iDb==1 ) isTemp = 1;
84975 #ifndef SQLITE_OMIT_AUTHORIZATION
84976   assert( (isTemp & 1)==isTemp );
84977   {
84978     int code;
84979     char *zDb = db->aDb[iDb].zName;
84980     if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(isTemp), 0, zDb) ){
84981       goto begin_table_error;
84982     }
84983     if( isView ){
84984       if( !OMIT_TEMPDB && isTemp ){
84985         code = SQLITE_CREATE_TEMP_VIEW;
84986       }else{
84987         code = SQLITE_CREATE_VIEW;
84988       }
84989     }else{
84990       if( !OMIT_TEMPDB && isTemp ){
84991         code = SQLITE_CREATE_TEMP_TABLE;
84992       }else{
84993         code = SQLITE_CREATE_TABLE;
84994       }
84995     }
84996     if( !isVirtual && sqlite3AuthCheck(pParse, code, zName, 0, zDb) ){
84997       goto begin_table_error;
84998     }
84999   }
85000 #endif
85001 
85002   /* Make sure the new table name does not collide with an existing
85003   ** index or table name in the same database.  Issue an error message if
85004   ** it does. The exception is if the statement being parsed was passed
85005   ** to an sqlite3_declare_vtab() call. In that case only the column names
85006   ** and types will be used, so there is no need to test for namespace
85007   ** collisions.
85008   */
85009   if( !IN_DECLARE_VTAB ){
85010     char *zDb = db->aDb[iDb].zName;
85011     if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
85012       goto begin_table_error;
85013     }
85014     pTable = sqlite3FindTable(db, zName, zDb);
85015     if( pTable ){
85016       if( !noErr ){
85017         sqlite3ErrorMsg(pParse, "table %T already exists", pName);
85018       }else{
85019         assert( !db->init.busy );
85020         sqlite3CodeVerifySchema(pParse, iDb);
85021       }
85022       goto begin_table_error;
85023     }
85024     if( sqlite3FindIndex(db, zName, zDb)!=0 ){
85025       sqlite3ErrorMsg(pParse, "there is already an index named %s", zName);
85026       goto begin_table_error;
85027     }
85028   }
85029 
85030   pTable = sqlite3DbMallocZero(db, sizeof(Table));
85031   if( pTable==0 ){
85032     db->mallocFailed = 1;
85033     pParse->rc = SQLITE_NOMEM;
85034     pParse->nErr++;
85035     goto begin_table_error;
85036   }
85037   pTable->zName = zName;
85038   pTable->iPKey = -1;
85039   pTable->pSchema = db->aDb[iDb].pSchema;
85040   pTable->nRef = 1;
85041   pTable->nRowEst = 1048576;
85042   assert( pParse->pNewTable==0 );
85043   pParse->pNewTable = pTable;
85044 
85045   /* If this is the magic sqlite_sequence table used by autoincrement,
85046   ** then record a pointer to this table in the main database structure
85047   ** so that INSERT can find the table easily.
85048   */
85049 #ifndef SQLITE_OMIT_AUTOINCREMENT
85050   if( !pParse->nested && strcmp(zName, "sqlite_sequence")==0 ){
85051     assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
85052     pTable->pSchema->pSeqTab = pTable;
85053   }
85054 #endif
85055 
85056   /* Begin generating the code that will insert the table record into
85057   ** the SQLITE_MASTER table.  Note in particular that we must go ahead
85058   ** and allocate the record number for the table entry now.  Before any
85059   ** PRIMARY KEY or UNIQUE keywords are parsed.  Those keywords will cause
85060   ** indices to be created and the table record must come before the
85061   ** indices.  Hence, the record number for the table must be allocated
85062   ** now.
85063   */
85064   if( !db->init.busy && (v = sqlite3GetVdbe(pParse))!=0 ){
85065     int j1;
85066     int fileFormat;
85067     int reg1, reg2, reg3;
85068     sqlite3BeginWriteOperation(pParse, 0, iDb);
85069 
85070 #ifndef SQLITE_OMIT_VIRTUALTABLE
85071     if( isVirtual ){
85072       sqlite3VdbeAddOp0(v, OP_VBegin);
85073     }
85074 #endif
85075 
85076     /* If the file format and encoding in the database have not been set,
85077     ** set them now.
85078     */
85079     reg1 = pParse->regRowid = ++pParse->nMem;
85080     reg2 = pParse->regRoot = ++pParse->nMem;
85081     reg3 = ++pParse->nMem;
85082     sqlite3VdbeAddOp3(v, OP_ReadCookie, iDb, reg3, BTREE_FILE_FORMAT);
85083     sqlite3VdbeUsesBtree(v, iDb);
85084     j1 = sqlite3VdbeAddOp1(v, OP_If, reg3);
85085     fileFormat = (db->flags & SQLITE_LegacyFileFmt)!=0 ?
85086                   1 : SQLITE_MAX_FILE_FORMAT;
85087     sqlite3VdbeAddOp2(v, OP_Integer, fileFormat, reg3);
85088     sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_FILE_FORMAT, reg3);
85089     sqlite3VdbeAddOp2(v, OP_Integer, ENC(db), reg3);
85090     sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_TEXT_ENCODING, reg3);
85091     sqlite3VdbeJumpHere(v, j1);
85092 
85093     /* This just creates a place-holder record in the sqlite_master table.
85094     ** The record created does not contain anything yet.  It will be replaced
85095     ** by the real entry in code generated at sqlite3EndTable().
85096     **
85097     ** The rowid for the new entry is left in register pParse->regRowid.
85098     ** The root page number of the new table is left in reg pParse->regRoot.
85099     ** The rowid and root page number values are needed by the code that
85100     ** sqlite3EndTable will generate.
85101     */
85102 #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
85103     if( isView || isVirtual ){
85104       sqlite3VdbeAddOp2(v, OP_Integer, 0, reg2);
85105     }else
85106 #endif
85107     {
85108       pParse->addrCrTab = sqlite3VdbeAddOp2(v, OP_CreateTable, iDb, reg2);
85109     }
85110     sqlite3OpenMasterTable(pParse, iDb);
85111     sqlite3VdbeAddOp2(v, OP_NewRowid, 0, reg1);
85112     sqlite3VdbeAddOp2(v, OP_Null, 0, reg3);
85113     sqlite3VdbeAddOp3(v, OP_Insert, 0, reg3, reg1);
85114     sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
85115     sqlite3VdbeAddOp0(v, OP_Close);
85116   }
85117 
85118   /* Normal (non-error) return. */
85119   return;
85120 
85121   /* If an error occurs, we jump here */
85122 begin_table_error:
85123   sqlite3DbFree(db, zName);
85124   return;
85125 }
85126 
85127 /*
85128 ** This macro is used to compare two strings in a case-insensitive manner.
85129 ** It is slightly faster than calling sqlite3StrICmp() directly, but
85130 ** produces larger code.
85131 **
85132 ** WARNING: This macro is not compatible with the strcmp() family. It
85133 ** returns true if the two strings are equal, otherwise false.
85134 */
85135 #define STRICMP(x, y) (\
85136 sqlite3UpperToLower[*(unsigned char *)(x)]==   \
85137 sqlite3UpperToLower[*(unsigned char *)(y)]     \
85138 && sqlite3StrICmp((x)+1,(y)+1)==0 )
85139 
85140 /*
85141 ** Add a new column to the table currently being constructed.
85142 **
85143 ** The parser calls this routine once for each column declaration
85144 ** in a CREATE TABLE statement.  sqlite3StartTable() gets called
85145 ** first to get things going.  Then this routine is called for each
85146 ** column.
85147 */
85148 SQLITE_PRIVATE void sqlite3AddColumn(Parse *pParse, Token *pName){
85149   Table *p;
85150   int i;
85151   char *z;
85152   Column *pCol;
85153   sqlite3 *db = pParse->db;
85154   if( (p = pParse->pNewTable)==0 ) return;
85155 #if SQLITE_MAX_COLUMN
85156   if( p->nCol+1>db->aLimit[SQLITE_LIMIT_COLUMN] ){
85157     sqlite3ErrorMsg(pParse, "too many columns on %s", p->zName);
85158     return;
85159   }
85160 #endif
85161   z = sqlite3NameFromToken(db, pName);
85162   if( z==0 ) return;
85163   for(i=0; i<p->nCol; i++){
85164     if( STRICMP(z, p->aCol[i].zName) ){
85165       sqlite3ErrorMsg(pParse, "duplicate column name: %s", z);
85166       sqlite3DbFree(db, z);
85167       return;
85168     }
85169   }
85170   if( (p->nCol & 0x7)==0 ){
85171     Column *aNew;
85172     aNew = sqlite3DbRealloc(db,p->aCol,(p->nCol+8)*sizeof(p->aCol[0]));
85173     if( aNew==0 ){
85174       sqlite3DbFree(db, z);
85175       return;
85176     }
85177     p->aCol = aNew;
85178   }
85179   pCol = &p->aCol[p->nCol];
85180   memset(pCol, 0, sizeof(p->aCol[0]));
85181   pCol->zName = z;
85182 
85183   /* If there is no type specified, columns have the default affinity
85184   ** 'NONE'. If there is a type specified, then sqlite3AddColumnType() will
85185   ** be called next to set pCol->affinity correctly.
85186   */
85187   pCol->affinity = SQLITE_AFF_NONE;
85188   pCol->szEst = 1;
85189   p->nCol++;
85190 }
85191 
85192 /*
85193 ** This routine is called by the parser while in the middle of
85194 ** parsing a CREATE TABLE statement.  A "NOT NULL" constraint has
85195 ** been seen on a column.  This routine sets the notNull flag on
85196 ** the column currently under construction.
85197 */
85198 SQLITE_PRIVATE void sqlite3AddNotNull(Parse *pParse, int onError){
85199   Table *p;
85200   p = pParse->pNewTable;
85201   if( p==0 || NEVER(p->nCol<1) ) return;
85202   p->aCol[p->nCol-1].notNull = (u8)onError;
85203 }
85204 
85205 /*
85206 ** Scan the column type name zType (length nType) and return the
85207 ** associated affinity type.
85208 **
85209 ** This routine does a case-independent search of zType for the
85210 ** substrings in the following table. If one of the substrings is
85211 ** found, the corresponding affinity is returned. If zType contains
85212 ** more than one of the substrings, entries toward the top of
85213 ** the table take priority. For example, if zType is 'BLOBINT',
85214 ** SQLITE_AFF_INTEGER is returned.
85215 **
85216 ** Substring     | Affinity
85217 ** --------------------------------
85218 ** 'INT'         | SQLITE_AFF_INTEGER
85219 ** 'CHAR'        | SQLITE_AFF_TEXT
85220 ** 'CLOB'        | SQLITE_AFF_TEXT
85221 ** 'TEXT'        | SQLITE_AFF_TEXT
85222 ** 'BLOB'        | SQLITE_AFF_NONE
85223 ** 'REAL'        | SQLITE_AFF_REAL
85224 ** 'FLOA'        | SQLITE_AFF_REAL
85225 ** 'DOUB'        | SQLITE_AFF_REAL
85226 **
85227 ** If none of the substrings in the above table are found,
85228 ** SQLITE_AFF_NUMERIC is returned.
85229 */
85230 SQLITE_PRIVATE char sqlite3AffinityType(const char *zIn, u8 *pszEst){
85231   u32 h = 0;
85232   char aff = SQLITE_AFF_NUMERIC;
85233   const char *zChar = 0;
85234 
85235   if( zIn==0 ) return aff;
85236   while( zIn[0] ){
85237     h = (h<<8) + sqlite3UpperToLower[(*zIn)&0xff];
85238     zIn++;
85239     if( h==(('c'<<24)+('h'<<16)+('a'<<8)+'r') ){             /* CHAR */
85240       aff = SQLITE_AFF_TEXT;
85241       zChar = zIn;
85242     }else if( h==(('c'<<24)+('l'<<16)+('o'<<8)+'b') ){       /* CLOB */
85243       aff = SQLITE_AFF_TEXT;
85244     }else if( h==(('t'<<24)+('e'<<16)+('x'<<8)+'t') ){       /* TEXT */
85245       aff = SQLITE_AFF_TEXT;
85246     }else if( h==(('b'<<24)+('l'<<16)+('o'<<8)+'b')          /* BLOB */
85247         && (aff==SQLITE_AFF_NUMERIC || aff==SQLITE_AFF_REAL) ){
85248       aff = SQLITE_AFF_NONE;
85249       if( zIn[0]=='(' ) zChar = zIn;
85250 #ifndef SQLITE_OMIT_FLOATING_POINT
85251     }else if( h==(('r'<<24)+('e'<<16)+('a'<<8)+'l')          /* REAL */
85252         && aff==SQLITE_AFF_NUMERIC ){
85253       aff = SQLITE_AFF_REAL;
85254     }else if( h==(('f'<<24)+('l'<<16)+('o'<<8)+'a')          /* FLOA */
85255         && aff==SQLITE_AFF_NUMERIC ){
85256       aff = SQLITE_AFF_REAL;
85257     }else if( h==(('d'<<24)+('o'<<16)+('u'<<8)+'b')          /* DOUB */
85258         && aff==SQLITE_AFF_NUMERIC ){
85259       aff = SQLITE_AFF_REAL;
85260 #endif
85261     }else if( (h&0x00FFFFFF)==(('i'<<16)+('n'<<8)+'t') ){    /* INT */
85262       aff = SQLITE_AFF_INTEGER;
85263       break;
85264     }
85265   }
85266 
85267   /* If pszEst is not NULL, store an estimate of the field size.  The
85268   ** estimate is scaled so that the size of an integer is 1.  */
85269   if( pszEst ){
85270     *pszEst = 1;   /* default size is approx 4 bytes */
85271     if( aff<=SQLITE_AFF_NONE ){
85272       if( zChar ){
85273         while( zChar[0] ){
85274           if( sqlite3Isdigit(zChar[0]) ){
85275             int v = 0;
85276             sqlite3GetInt32(zChar, &v);
85277             v = v/4 + 1;
85278             if( v>255 ) v = 255;
85279             *pszEst = v; /* BLOB(k), VARCHAR(k), CHAR(k) -> r=(k/4+1) */
85280             break;
85281           }
85282           zChar++;
85283         }
85284       }else{
85285         *pszEst = 5;   /* BLOB, TEXT, CLOB -> r=5  (approx 20 bytes)*/
85286       }
85287     }
85288   }
85289   return aff;
85290 }
85291 
85292 /*
85293 ** This routine is called by the parser while in the middle of
85294 ** parsing a CREATE TABLE statement.  The pFirst token is the first
85295 ** token in the sequence of tokens that describe the type of the
85296 ** column currently under construction.   pLast is the last token
85297 ** in the sequence.  Use this information to construct a string
85298 ** that contains the typename of the column and store that string
85299 ** in zType.
85300 */
85301 SQLITE_PRIVATE void sqlite3AddColumnType(Parse *pParse, Token *pType){
85302   Table *p;
85303   Column *pCol;
85304 
85305   p = pParse->pNewTable;
85306   if( p==0 || NEVER(p->nCol<1) ) return;
85307   pCol = &p->aCol[p->nCol-1];
85308   assert( pCol->zType==0 );
85309   pCol->zType = sqlite3NameFromToken(pParse->db, pType);
85310   pCol->affinity = sqlite3AffinityType(pCol->zType, &pCol->szEst);
85311 }
85312 
85313 /*
85314 ** The expression is the default value for the most recently added column
85315 ** of the table currently under construction.
85316 **
85317 ** Default value expressions must be constant.  Raise an exception if this
85318 ** is not the case.
85319 **
85320 ** This routine is called by the parser while in the middle of
85321 ** parsing a CREATE TABLE statement.
85322 */
85323 SQLITE_PRIVATE void sqlite3AddDefaultValue(Parse *pParse, ExprSpan *pSpan){
85324   Table *p;
85325   Column *pCol;
85326   sqlite3 *db = pParse->db;
85327   p = pParse->pNewTable;
85328   if( p!=0 ){
85329     pCol = &(p->aCol[p->nCol-1]);
85330     if( !sqlite3ExprIsConstantOrFunction(pSpan->pExpr) ){
85331       sqlite3ErrorMsg(pParse, "default value of column [%s] is not constant",
85332           pCol->zName);
85333     }else{
85334       /* A copy of pExpr is used instead of the original, as pExpr contains
85335       ** tokens that point to volatile memory. The 'span' of the expression
85336       ** is required by pragma table_info.
85337       */
85338       sqlite3ExprDelete(db, pCol->pDflt);
85339       pCol->pDflt = sqlite3ExprDup(db, pSpan->pExpr, EXPRDUP_REDUCE);
85340       sqlite3DbFree(db, pCol->zDflt);
85341       pCol->zDflt = sqlite3DbStrNDup(db, (char*)pSpan->zStart,
85342                                      (int)(pSpan->zEnd - pSpan->zStart));
85343     }
85344   }
85345   sqlite3ExprDelete(db, pSpan->pExpr);
85346 }
85347 
85348 /*
85349 ** Designate the PRIMARY KEY for the table.  pList is a list of names
85350 ** of columns that form the primary key.  If pList is NULL, then the
85351 ** most recently added column of the table is the primary key.
85352 **
85353 ** A table can have at most one primary key.  If the table already has
85354 ** a primary key (and this is the second primary key) then create an
85355 ** error.
85356 **
85357 ** If the PRIMARY KEY is on a single column whose datatype is INTEGER,
85358 ** then we will try to use that column as the rowid.  Set the Table.iPKey
85359 ** field of the table under construction to be the index of the
85360 ** INTEGER PRIMARY KEY column.  Table.iPKey is set to -1 if there is
85361 ** no INTEGER PRIMARY KEY.
85362 **
85363 ** If the key is not an INTEGER PRIMARY KEY, then create a unique
85364 ** index for the key.  No index is created for INTEGER PRIMARY KEYs.
85365 */
85366 SQLITE_PRIVATE void sqlite3AddPrimaryKey(
85367   Parse *pParse,    /* Parsing context */
85368   ExprList *pList,  /* List of field names to be indexed */
85369   int onError,      /* What to do with a uniqueness conflict */
85370   int autoInc,      /* True if the AUTOINCREMENT keyword is present */
85371   int sortOrder     /* SQLITE_SO_ASC or SQLITE_SO_DESC */
85372 ){
85373   Table *pTab = pParse->pNewTable;
85374   char *zType = 0;
85375   int iCol = -1, i;
85376   int nTerm;
85377   if( pTab==0 || IN_DECLARE_VTAB ) goto primary_key_exit;
85378   if( pTab->tabFlags & TF_HasPrimaryKey ){
85379     sqlite3ErrorMsg(pParse,
85380       "table \"%s\" has more than one primary key", pTab->zName);
85381     goto primary_key_exit;
85382   }
85383   pTab->tabFlags |= TF_HasPrimaryKey;
85384   if( pList==0 ){
85385     iCol = pTab->nCol - 1;
85386     pTab->aCol[iCol].colFlags |= COLFLAG_PRIMKEY;
85387     zType = pTab->aCol[iCol].zType;
85388     nTerm = 1;
85389   }else{
85390     nTerm = pList->nExpr;
85391     for(i=0; i<nTerm; i++){
85392       for(iCol=0; iCol<pTab->nCol; iCol++){
85393         if( sqlite3StrICmp(pList->a[i].zName, pTab->aCol[iCol].zName)==0 ){
85394           pTab->aCol[iCol].colFlags |= COLFLAG_PRIMKEY;
85395           zType = pTab->aCol[iCol].zType;
85396           break;
85397         }
85398       }
85399     }
85400   }
85401   if( nTerm==1
85402    && zType && sqlite3StrICmp(zType, "INTEGER")==0
85403    && sortOrder==SQLITE_SO_ASC
85404   ){
85405     pTab->iPKey = iCol;
85406     pTab->keyConf = (u8)onError;
85407     assert( autoInc==0 || autoInc==1 );
85408     pTab->tabFlags |= autoInc*TF_Autoincrement;
85409     if( pList ) pParse->iPkSortOrder = pList->a[0].sortOrder;
85410   }else if( autoInc ){
85411 #ifndef SQLITE_OMIT_AUTOINCREMENT
85412     sqlite3ErrorMsg(pParse, "AUTOINCREMENT is only allowed on an "
85413        "INTEGER PRIMARY KEY");
85414 #endif
85415   }else{
85416     Vdbe *v = pParse->pVdbe;
85417     Index *p;
85418     if( v ) pParse->addrSkipPK = sqlite3VdbeAddOp0(v, OP_Noop);
85419     p = sqlite3CreateIndex(pParse, 0, 0, 0, pList, onError, 0,
85420                            0, sortOrder, 0);
85421     if( p ){
85422       p->autoIndex = 2;
85423       if( v ) sqlite3VdbeJumpHere(v, pParse->addrSkipPK);
85424     }
85425     pList = 0;
85426   }
85427 
85428 primary_key_exit:
85429   sqlite3ExprListDelete(pParse->db, pList);
85430   return;
85431 }
85432 
85433 /*
85434 ** Add a new CHECK constraint to the table currently under construction.
85435 */
85436 SQLITE_PRIVATE void sqlite3AddCheckConstraint(
85437   Parse *pParse,    /* Parsing context */
85438   Expr *pCheckExpr  /* The check expression */
85439 ){
85440 #ifndef SQLITE_OMIT_CHECK
85441   Table *pTab = pParse->pNewTable;
85442   if( pTab && !IN_DECLARE_VTAB ){
85443     pTab->pCheck = sqlite3ExprListAppend(pParse, pTab->pCheck, pCheckExpr);
85444     if( pParse->constraintName.n ){
85445       sqlite3ExprListSetName(pParse, pTab->pCheck, &pParse->constraintName, 1);
85446     }
85447   }else
85448 #endif
85449   {
85450     sqlite3ExprDelete(pParse->db, pCheckExpr);
85451   }
85452 }
85453 
85454 /*
85455 ** Set the collation function of the most recently parsed table column
85456 ** to the CollSeq given.
85457 */
85458 SQLITE_PRIVATE void sqlite3AddCollateType(Parse *pParse, Token *pToken){
85459   Table *p;
85460   int i;
85461   char *zColl;              /* Dequoted name of collation sequence */
85462   sqlite3 *db;
85463 
85464   if( (p = pParse->pNewTable)==0 ) return;
85465   i = p->nCol-1;
85466   db = pParse->db;
85467   zColl = sqlite3NameFromToken(db, pToken);
85468   if( !zColl ) return;
85469 
85470   if( sqlite3LocateCollSeq(pParse, zColl) ){
85471     Index *pIdx;
85472     sqlite3DbFree(db, p->aCol[i].zColl);
85473     p->aCol[i].zColl = zColl;
85474 
85475     /* If the column is declared as "<name> PRIMARY KEY COLLATE <type>",
85476     ** then an index may have been created on this column before the
85477     ** collation type was added. Correct this if it is the case.
85478     */
85479     for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){
85480       assert( pIdx->nKeyCol==1 );
85481       if( pIdx->aiColumn[0]==i ){
85482         pIdx->azColl[0] = p->aCol[i].zColl;
85483       }
85484     }
85485   }else{
85486     sqlite3DbFree(db, zColl);
85487   }
85488 }
85489 
85490 /*
85491 ** This function returns the collation sequence for database native text
85492 ** encoding identified by the string zName, length nName.
85493 **
85494 ** If the requested collation sequence is not available, or not available
85495 ** in the database native encoding, the collation factory is invoked to
85496 ** request it. If the collation factory does not supply such a sequence,
85497 ** and the sequence is available in another text encoding, then that is
85498 ** returned instead.
85499 **
85500 ** If no versions of the requested collations sequence are available, or
85501 ** another error occurs, NULL is returned and an error message written into
85502 ** pParse.
85503 **
85504 ** This routine is a wrapper around sqlite3FindCollSeq().  This routine
85505 ** invokes the collation factory if the named collation cannot be found
85506 ** and generates an error message.
85507 **
85508 ** See also: sqlite3FindCollSeq(), sqlite3GetCollSeq()
85509 */
85510 SQLITE_PRIVATE CollSeq *sqlite3LocateCollSeq(Parse *pParse, const char *zName){
85511   sqlite3 *db = pParse->db;
85512   u8 enc = ENC(db);
85513   u8 initbusy = db->init.busy;
85514   CollSeq *pColl;
85515 
85516   pColl = sqlite3FindCollSeq(db, enc, zName, initbusy);
85517   if( !initbusy && (!pColl || !pColl->xCmp) ){
85518     pColl = sqlite3GetCollSeq(pParse, enc, pColl, zName);
85519   }
85520 
85521   return pColl;
85522 }
85523 
85524 
85525 /*
85526 ** Generate code that will increment the schema cookie.
85527 **
85528 ** The schema cookie is used to determine when the schema for the
85529 ** database changes.  After each schema change, the cookie value
85530 ** changes.  When a process first reads the schema it records the
85531 ** cookie.  Thereafter, whenever it goes to access the database,
85532 ** it checks the cookie to make sure the schema has not changed
85533 ** since it was last read.
85534 **
85535 ** This plan is not completely bullet-proof.  It is possible for
85536 ** the schema to change multiple times and for the cookie to be
85537 ** set back to prior value.  But schema changes are infrequent
85538 ** and the probability of hitting the same cookie value is only
85539 ** 1 chance in 2^32.  So we're safe enough.
85540 */
85541 SQLITE_PRIVATE void sqlite3ChangeCookie(Parse *pParse, int iDb){
85542   int r1 = sqlite3GetTempReg(pParse);
85543   sqlite3 *db = pParse->db;
85544   Vdbe *v = pParse->pVdbe;
85545   assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
85546   sqlite3VdbeAddOp2(v, OP_Integer, db->aDb[iDb].pSchema->schema_cookie+1, r1);
85547   sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_SCHEMA_VERSION, r1);
85548   sqlite3ReleaseTempReg(pParse, r1);
85549 }
85550 
85551 /*
85552 ** Measure the number of characters needed to output the given
85553 ** identifier.  The number returned includes any quotes used
85554 ** but does not include the null terminator.
85555 **
85556 ** The estimate is conservative.  It might be larger that what is
85557 ** really needed.
85558 */
85559 static int identLength(const char *z){
85560   int n;
85561   for(n=0; *z; n++, z++){
85562     if( *z=='"' ){ n++; }
85563   }
85564   return n + 2;
85565 }
85566 
85567 /*
85568 ** The first parameter is a pointer to an output buffer. The second
85569 ** parameter is a pointer to an integer that contains the offset at
85570 ** which to write into the output buffer. This function copies the
85571 ** nul-terminated string pointed to by the third parameter, zSignedIdent,
85572 ** to the specified offset in the buffer and updates *pIdx to refer
85573 ** to the first byte after the last byte written before returning.
85574 **
85575 ** If the string zSignedIdent consists entirely of alpha-numeric
85576 ** characters, does not begin with a digit and is not an SQL keyword,
85577 ** then it is copied to the output buffer exactly as it is. Otherwise,
85578 ** it is quoted using double-quotes.
85579 */
85580 static void identPut(char *z, int *pIdx, char *zSignedIdent){
85581   unsigned char *zIdent = (unsigned char*)zSignedIdent;
85582   int i, j, needQuote;
85583   i = *pIdx;
85584 
85585   for(j=0; zIdent[j]; j++){
85586     if( !sqlite3Isalnum(zIdent[j]) && zIdent[j]!='_' ) break;
85587   }
85588   needQuote = sqlite3Isdigit(zIdent[0])
85589             || sqlite3KeywordCode(zIdent, j)!=TK_ID
85590             || zIdent[j]!=0
85591             || j==0;
85592 
85593   if( needQuote ) z[i++] = '"';
85594   for(j=0; zIdent[j]; j++){
85595     z[i++] = zIdent[j];
85596     if( zIdent[j]=='"' ) z[i++] = '"';
85597   }
85598   if( needQuote ) z[i++] = '"';
85599   z[i] = 0;
85600   *pIdx = i;
85601 }
85602 
85603 /*
85604 ** Generate a CREATE TABLE statement appropriate for the given
85605 ** table.  Memory to hold the text of the statement is obtained
85606 ** from sqliteMalloc() and must be freed by the calling function.
85607 */
85608 static char *createTableStmt(sqlite3 *db, Table *p){
85609   int i, k, n;
85610   char *zStmt;
85611   char *zSep, *zSep2, *zEnd;
85612   Column *pCol;
85613   n = 0;
85614   for(pCol = p->aCol, i=0; i<p->nCol; i++, pCol++){
85615     n += identLength(pCol->zName) + 5;
85616   }
85617   n += identLength(p->zName);
85618   if( n<50 ){
85619     zSep = "";
85620     zSep2 = ",";
85621     zEnd = ")";
85622   }else{
85623     zSep = "\n  ";
85624     zSep2 = ",\n  ";
85625     zEnd = "\n)";
85626   }
85627   n += 35 + 6*p->nCol;
85628   zStmt = sqlite3DbMallocRaw(0, n);
85629   if( zStmt==0 ){
85630     db->mallocFailed = 1;
85631     return 0;
85632   }
85633   sqlite3_snprintf(n, zStmt, "CREATE TABLE ");
85634   k = sqlite3Strlen30(zStmt);
85635   identPut(zStmt, &k, p->zName);
85636   zStmt[k++] = '(';
85637   for(pCol=p->aCol, i=0; i<p->nCol; i++, pCol++){
85638     static const char * const azType[] = {
85639         /* SQLITE_AFF_TEXT    */ " TEXT",
85640         /* SQLITE_AFF_NONE    */ "",
85641         /* SQLITE_AFF_NUMERIC */ " NUM",
85642         /* SQLITE_AFF_INTEGER */ " INT",
85643         /* SQLITE_AFF_REAL    */ " REAL"
85644     };
85645     int len;
85646     const char *zType;
85647 
85648     sqlite3_snprintf(n-k, &zStmt[k], zSep);
85649     k += sqlite3Strlen30(&zStmt[k]);
85650     zSep = zSep2;
85651     identPut(zStmt, &k, pCol->zName);
85652     assert( pCol->affinity-SQLITE_AFF_TEXT >= 0 );
85653     assert( pCol->affinity-SQLITE_AFF_TEXT < ArraySize(azType) );
85654     testcase( pCol->affinity==SQLITE_AFF_TEXT );
85655     testcase( pCol->affinity==SQLITE_AFF_NONE );
85656     testcase( pCol->affinity==SQLITE_AFF_NUMERIC );
85657     testcase( pCol->affinity==SQLITE_AFF_INTEGER );
85658     testcase( pCol->affinity==SQLITE_AFF_REAL );
85659 
85660     zType = azType[pCol->affinity - SQLITE_AFF_TEXT];
85661     len = sqlite3Strlen30(zType);
85662     assert( pCol->affinity==SQLITE_AFF_NONE
85663             || pCol->affinity==sqlite3AffinityType(zType, 0) );
85664     memcpy(&zStmt[k], zType, len);
85665     k += len;
85666     assert( k<=n );
85667   }
85668   sqlite3_snprintf(n-k, &zStmt[k], "%s", zEnd);
85669   return zStmt;
85670 }
85671 
85672 /*
85673 ** Resize an Index object to hold N columns total.  Return SQLITE_OK
85674 ** on success and SQLITE_NOMEM on an OOM error.
85675 */
85676 static int resizeIndexObject(sqlite3 *db, Index *pIdx, int N){
85677   char *zExtra;
85678   int nByte;
85679   if( pIdx->nColumn>=N ) return SQLITE_OK;
85680   assert( pIdx->isResized==0 );
85681   nByte = (sizeof(char*) + sizeof(i16) + 1)*N;
85682   zExtra = sqlite3DbMallocZero(db, nByte);
85683   if( zExtra==0 ) return SQLITE_NOMEM;
85684   memcpy(zExtra, pIdx->azColl, sizeof(char*)*pIdx->nColumn);
85685   pIdx->azColl = (char**)zExtra;
85686   zExtra += sizeof(char*)*N;
85687   memcpy(zExtra, pIdx->aiColumn, sizeof(i16)*pIdx->nColumn);
85688   pIdx->aiColumn = (i16*)zExtra;
85689   zExtra += sizeof(i16)*N;
85690   memcpy(zExtra, pIdx->aSortOrder, pIdx->nColumn);
85691   pIdx->aSortOrder = (u8*)zExtra;
85692   pIdx->nColumn = N;
85693   pIdx->isResized = 1;
85694   return SQLITE_OK;
85695 }
85696 
85697 /*
85698 ** Estimate the total row width for a table.
85699 */
85700 static void estimateTableWidth(Table *pTab){
85701   unsigned wTable = 0;
85702   const Column *pTabCol;
85703   int i;
85704   for(i=pTab->nCol, pTabCol=pTab->aCol; i>0; i--, pTabCol++){
85705     wTable += pTabCol->szEst;
85706   }
85707   if( pTab->iPKey<0 ) wTable++;
85708   pTab->szTabRow = sqlite3LogEst(wTable*4);
85709 }
85710 
85711 /*
85712 ** Estimate the average size of a row for an index.
85713 */
85714 static void estimateIndexWidth(Index *pIdx){
85715   unsigned wIndex = 0;
85716   int i;
85717   const Column *aCol = pIdx->pTable->aCol;
85718   for(i=0; i<pIdx->nColumn; i++){
85719     i16 x = pIdx->aiColumn[i];
85720     assert( x<pIdx->pTable->nCol );
85721     wIndex += x<0 ? 1 : aCol[pIdx->aiColumn[i]].szEst;
85722   }
85723   pIdx->szIdxRow = sqlite3LogEst(wIndex*4);
85724 }
85725 
85726 /* Return true if value x is found any of the first nCol entries of aiCol[]
85727 */
85728 static int hasColumn(const i16 *aiCol, int nCol, int x){
85729   while( nCol-- > 0 ) if( x==*(aiCol++) ) return 1;
85730   return 0;
85731 }
85732 
85733 /*
85734 ** This routine runs at the end of parsing a CREATE TABLE statement that
85735 ** has a WITHOUT ROWID clause.  The job of this routine is to convert both
85736 ** internal schema data structures and the generated VDBE code so that they
85737 ** are appropriate for a WITHOUT ROWID table instead of a rowid table.
85738 ** Changes include:
85739 **
85740 **     (1)  Convert the OP_CreateTable into an OP_CreateIndex.  There is
85741 **          no rowid btree for a WITHOUT ROWID.  Instead, the canonical
85742 **          data storage is a covering index btree.
85743 **     (2)  Bypass the creation of the sqlite_master table entry
85744 **          for the PRIMARY KEY as the the primary key index is now
85745 **          identified by the sqlite_master table entry of the table itself.
85746 **     (3)  Set the Index.tnum of the PRIMARY KEY Index object in the
85747 **          schema to the rootpage from the main table.
85748 **     (4)  Set all columns of the PRIMARY KEY schema object to be NOT NULL.
85749 **     (5)  Add all table columns to the PRIMARY KEY Index object
85750 **          so that the PRIMARY KEY is a covering index.  The surplus
85751 **          columns are part of KeyInfo.nXField and are not used for
85752 **          sorting or lookup or uniqueness checks.
85753 **     (6)  Replace the rowid tail on all automatically generated UNIQUE
85754 **          indices with the PRIMARY KEY columns.
85755 */
85756 static void convertToWithoutRowidTable(Parse *pParse, Table *pTab){
85757   Index *pIdx;
85758   Index *pPk;
85759   int nPk;
85760   int i, j;
85761   sqlite3 *db = pParse->db;
85762   Vdbe *v = pParse->pVdbe;
85763 
85764   /* Convert the OP_CreateTable opcode that would normally create the
85765   ** root-page for the table into a OP_CreateIndex opcode.  The index
85766   ** created will become the PRIMARY KEY index.
85767   */
85768   if( pParse->addrCrTab ){
85769     assert( v );
85770     sqlite3VdbeGetOp(v, pParse->addrCrTab)->opcode = OP_CreateIndex;
85771   }
85772 
85773   /* Bypass the creation of the PRIMARY KEY btree and the sqlite_master
85774   ** table entry.
85775   */
85776   if( pParse->addrSkipPK ){
85777     assert( v );
85778     sqlite3VdbeGetOp(v, pParse->addrSkipPK)->opcode = OP_Goto;
85779   }
85780 
85781   /* Locate the PRIMARY KEY index.  Or, if this table was originally
85782   ** an INTEGER PRIMARY KEY table, create a new PRIMARY KEY index.
85783   */
85784   if( pTab->iPKey>=0 ){
85785     ExprList *pList;
85786     pList = sqlite3ExprListAppend(pParse, 0, 0);
85787     if( pList==0 ) return;
85788     pList->a[0].zName = sqlite3DbStrDup(pParse->db,
85789                                         pTab->aCol[pTab->iPKey].zName);
85790     pList->a[0].sortOrder = pParse->iPkSortOrder;
85791     assert( pParse->pNewTable==pTab );
85792     pPk = sqlite3CreateIndex(pParse, 0, 0, 0, pList, pTab->keyConf, 0, 0, 0, 0);
85793     if( pPk==0 ) return;
85794     pPk->autoIndex = 2;
85795     pTab->iPKey = -1;
85796   }else{
85797     pPk = sqlite3PrimaryKeyIndex(pTab);
85798   }
85799   pPk->isCovering = 1;
85800   assert( pPk!=0 );
85801   nPk = pPk->nKeyCol;
85802 
85803   /* Make sure every column of the PRIMARY KEY is NOT NULL */
85804   for(i=0; i<nPk; i++){
85805     pTab->aCol[pPk->aiColumn[i]].notNull = 1;
85806   }
85807   pPk->uniqNotNull = 1;
85808 
85809   /* The root page of the PRIMARY KEY is the table root page */
85810   pPk->tnum = pTab->tnum;
85811 
85812   /* Update the in-memory representation of all UNIQUE indices by converting
85813   ** the final rowid column into one or more columns of the PRIMARY KEY.
85814   */
85815   for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
85816     int n;
85817     if( pIdx->autoIndex==2 ) continue;
85818     for(i=n=0; i<nPk; i++){
85819       if( !hasColumn(pIdx->aiColumn, pIdx->nKeyCol, pPk->aiColumn[i]) ) n++;
85820     }
85821     if( n==0 ){
85822       /* This index is a superset of the primary key */
85823       pIdx->nColumn = pIdx->nKeyCol;
85824       continue;
85825     }
85826     if( resizeIndexObject(db, pIdx, pIdx->nKeyCol+n) ) return;
85827     for(i=0, j=pIdx->nKeyCol; i<nPk; i++){
85828       if( !hasColumn(pIdx->aiColumn, pIdx->nKeyCol, pPk->aiColumn[i]) ){
85829         pIdx->aiColumn[j] = pPk->aiColumn[i];
85830         pIdx->azColl[j] = pPk->azColl[i];
85831         j++;
85832       }
85833     }
85834     assert( pIdx->nColumn>=pIdx->nKeyCol+n );
85835     assert( pIdx->nColumn>=j );
85836   }
85837 
85838   /* Add all table columns to the PRIMARY KEY index
85839   */
85840   if( nPk<pTab->nCol ){
85841     if( resizeIndexObject(db, pPk, pTab->nCol) ) return;
85842     for(i=0, j=nPk; i<pTab->nCol; i++){
85843       if( !hasColumn(pPk->aiColumn, j, i) ){
85844         assert( j<pPk->nColumn );
85845         pPk->aiColumn[j] = i;
85846         pPk->azColl[j] = "BINARY";
85847         j++;
85848       }
85849     }
85850     assert( pPk->nColumn==j );
85851     assert( pTab->nCol==j );
85852   }else{
85853     pPk->nColumn = pTab->nCol;
85854   }
85855 }
85856 
85857 /*
85858 ** This routine is called to report the final ")" that terminates
85859 ** a CREATE TABLE statement.
85860 **
85861 ** The table structure that other action routines have been building
85862 ** is added to the internal hash tables, assuming no errors have
85863 ** occurred.
85864 **
85865 ** An entry for the table is made in the master table on disk, unless
85866 ** this is a temporary table or db->init.busy==1.  When db->init.busy==1
85867 ** it means we are reading the sqlite_master table because we just
85868 ** connected to the database or because the sqlite_master table has
85869 ** recently changed, so the entry for this table already exists in
85870 ** the sqlite_master table.  We do not want to create it again.
85871 **
85872 ** If the pSelect argument is not NULL, it means that this routine
85873 ** was called to create a table generated from a
85874 ** "CREATE TABLE ... AS SELECT ..." statement.  The column names of
85875 ** the new table will match the result set of the SELECT.
85876 */
85877 SQLITE_PRIVATE void sqlite3EndTable(
85878   Parse *pParse,          /* Parse context */
85879   Token *pCons,           /* The ',' token after the last column defn. */
85880   Token *pEnd,            /* The ')' before options in the CREATE TABLE */
85881   u8 tabOpts,             /* Extra table options. Usually 0. */
85882   Select *pSelect         /* Select from a "CREATE ... AS SELECT" */
85883 ){
85884   Table *p;                 /* The new table */
85885   sqlite3 *db = pParse->db; /* The database connection */
85886   int iDb;                  /* Database in which the table lives */
85887   Index *pIdx;              /* An implied index of the table */
85888 
85889   if( (pEnd==0 && pSelect==0) || db->mallocFailed ){
85890     return;
85891   }
85892   p = pParse->pNewTable;
85893   if( p==0 ) return;
85894 
85895   assert( !db->init.busy || !pSelect );
85896 
85897   /* If the db->init.busy is 1 it means we are reading the SQL off the
85898   ** "sqlite_master" or "sqlite_temp_master" table on the disk.
85899   ** So do not write to the disk again.  Extract the root page number
85900   ** for the table from the db->init.newTnum field.  (The page number
85901   ** should have been put there by the sqliteOpenCb routine.)
85902   */
85903   if( db->init.busy ){
85904     p->tnum = db->init.newTnum;
85905   }
85906 
85907   /* Special processing for WITHOUT ROWID Tables */
85908   if( tabOpts & TF_WithoutRowid ){
85909     if( (p->tabFlags & TF_Autoincrement) ){
85910       sqlite3ErrorMsg(pParse,
85911           "AUTOINCREMENT not allowed on WITHOUT ROWID tables");
85912       return;
85913     }
85914     if( (p->tabFlags & TF_HasPrimaryKey)==0 ){
85915       sqlite3ErrorMsg(pParse, "PRIMARY KEY missing on table %s", p->zName);
85916     }else{
85917       p->tabFlags |= TF_WithoutRowid;
85918       convertToWithoutRowidTable(pParse, p);
85919     }
85920   }
85921 
85922   iDb = sqlite3SchemaToIndex(db, p->pSchema);
85923 
85924 #ifndef SQLITE_OMIT_CHECK
85925   /* Resolve names in all CHECK constraint expressions.
85926   */
85927   if( p->pCheck ){
85928     sqlite3ResolveSelfReference(pParse, p, NC_IsCheck, 0, p->pCheck);
85929   }
85930 #endif /* !defined(SQLITE_OMIT_CHECK) */
85931 
85932   /* Estimate the average row size for the table and for all implied indices */
85933   estimateTableWidth(p);
85934   for(pIdx=p->pIndex; pIdx; pIdx=pIdx->pNext){
85935     estimateIndexWidth(pIdx);
85936   }
85937 
85938   /* If not initializing, then create a record for the new table
85939   ** in the SQLITE_MASTER table of the database.
85940   **
85941   ** If this is a TEMPORARY table, write the entry into the auxiliary
85942   ** file instead of into the main database file.
85943   */
85944   if( !db->init.busy ){
85945     int n;
85946     Vdbe *v;
85947     char *zType;    /* "view" or "table" */
85948     char *zType2;   /* "VIEW" or "TABLE" */
85949     char *zStmt;    /* Text of the CREATE TABLE or CREATE VIEW statement */
85950 
85951     v = sqlite3GetVdbe(pParse);
85952     if( NEVER(v==0) ) return;
85953 
85954     sqlite3VdbeAddOp1(v, OP_Close, 0);
85955 
85956     /*
85957     ** Initialize zType for the new view or table.
85958     */
85959     if( p->pSelect==0 ){
85960       /* A regular table */
85961       zType = "table";
85962       zType2 = "TABLE";
85963 #ifndef SQLITE_OMIT_VIEW
85964     }else{
85965       /* A view */
85966       zType = "view";
85967       zType2 = "VIEW";
85968 #endif
85969     }
85970 
85971     /* If this is a CREATE TABLE xx AS SELECT ..., execute the SELECT
85972     ** statement to populate the new table. The root-page number for the
85973     ** new table is in register pParse->regRoot.
85974     **
85975     ** Once the SELECT has been coded by sqlite3Select(), it is in a
85976     ** suitable state to query for the column names and types to be used
85977     ** by the new table.
85978     **
85979     ** A shared-cache write-lock is not required to write to the new table,
85980     ** as a schema-lock must have already been obtained to create it. Since
85981     ** a schema-lock excludes all other database users, the write-lock would
85982     ** be redundant.
85983     */
85984     if( pSelect ){
85985       SelectDest dest;
85986       Table *pSelTab;
85987 
85988       assert(pParse->nTab==1);
85989       sqlite3VdbeAddOp3(v, OP_OpenWrite, 1, pParse->regRoot, iDb);
85990       sqlite3VdbeChangeP5(v, OPFLAG_P2ISREG);
85991       pParse->nTab = 2;
85992       sqlite3SelectDestInit(&dest, SRT_Table, 1);
85993       sqlite3Select(pParse, pSelect, &dest);
85994       sqlite3VdbeAddOp1(v, OP_Close, 1);
85995       if( pParse->nErr==0 ){
85996         pSelTab = sqlite3ResultSetOfSelect(pParse, pSelect);
85997         if( pSelTab==0 ) return;
85998         assert( p->aCol==0 );
85999         p->nCol = pSelTab->nCol;
86000         p->aCol = pSelTab->aCol;
86001         pSelTab->nCol = 0;
86002         pSelTab->aCol = 0;
86003         sqlite3DeleteTable(db, pSelTab);
86004       }
86005     }
86006 
86007     /* Compute the complete text of the CREATE statement */
86008     if( pSelect ){
86009       zStmt = createTableStmt(db, p);
86010     }else{
86011       Token *pEnd2 = tabOpts ? &pParse->sLastToken : pEnd;
86012       n = (int)(pEnd2->z - pParse->sNameToken.z);
86013       if( pEnd2->z[0]!=';' ) n += pEnd2->n;
86014       zStmt = sqlite3MPrintf(db,
86015           "CREATE %s %.*s", zType2, n, pParse->sNameToken.z
86016       );
86017     }
86018 
86019     /* A slot for the record has already been allocated in the
86020     ** SQLITE_MASTER table.  We just need to update that slot with all
86021     ** the information we've collected.
86022     */
86023     sqlite3NestedParse(pParse,
86024       "UPDATE %Q.%s "
86025          "SET type='%s', name=%Q, tbl_name=%Q, rootpage=#%d, sql=%Q "
86026        "WHERE rowid=#%d",
86027       db->aDb[iDb].zName, SCHEMA_TABLE(iDb),
86028       zType,
86029       p->zName,
86030       p->zName,
86031       pParse->regRoot,
86032       zStmt,
86033       pParse->regRowid
86034     );
86035     sqlite3DbFree(db, zStmt);
86036     sqlite3ChangeCookie(pParse, iDb);
86037 
86038 #ifndef SQLITE_OMIT_AUTOINCREMENT
86039     /* Check to see if we need to create an sqlite_sequence table for
86040     ** keeping track of autoincrement keys.
86041     */
86042     if( p->tabFlags & TF_Autoincrement ){
86043       Db *pDb = &db->aDb[iDb];
86044       assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
86045       if( pDb->pSchema->pSeqTab==0 ){
86046         sqlite3NestedParse(pParse,
86047           "CREATE TABLE %Q.sqlite_sequence(name,seq)",
86048           pDb->zName
86049         );
86050       }
86051     }
86052 #endif
86053 
86054     /* Reparse everything to update our internal data structures */
86055     sqlite3VdbeAddParseSchemaOp(v, iDb,
86056            sqlite3MPrintf(db, "tbl_name='%q' AND type!='trigger'", p->zName));
86057   }
86058 
86059 
86060   /* Add the table to the in-memory representation of the database.
86061   */
86062   if( db->init.busy ){
86063     Table *pOld;
86064     Schema *pSchema = p->pSchema;
86065     assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
86066     pOld = sqlite3HashInsert(&pSchema->tblHash, p->zName,
86067                              sqlite3Strlen30(p->zName),p);
86068     if( pOld ){
86069       assert( p==pOld );  /* Malloc must have failed inside HashInsert() */
86070       db->mallocFailed = 1;
86071       return;
86072     }
86073     pParse->pNewTable = 0;
86074     db->flags |= SQLITE_InternChanges;
86075 
86076 #ifndef SQLITE_OMIT_ALTERTABLE
86077     if( !p->pSelect ){
86078       const char *zName = (const char *)pParse->sNameToken.z;
86079       int nName;
86080       assert( !pSelect && pCons && pEnd );
86081       if( pCons->z==0 ){
86082         pCons = pEnd;
86083       }
86084       nName = (int)((const char *)pCons->z - zName);
86085       p->addColOffset = 13 + sqlite3Utf8CharLen(zName, nName);
86086     }
86087 #endif
86088   }
86089 }
86090 
86091 #ifndef SQLITE_OMIT_VIEW
86092 /*
86093 ** The parser calls this routine in order to create a new VIEW
86094 */
86095 SQLITE_PRIVATE void sqlite3CreateView(
86096   Parse *pParse,     /* The parsing context */
86097   Token *pBegin,     /* The CREATE token that begins the statement */
86098   Token *pName1,     /* The token that holds the name of the view */
86099   Token *pName2,     /* The token that holds the name of the view */
86100   Select *pSelect,   /* A SELECT statement that will become the new view */
86101   int isTemp,        /* TRUE for a TEMPORARY view */
86102   int noErr          /* Suppress error messages if VIEW already exists */
86103 ){
86104   Table *p;
86105   int n;
86106   const char *z;
86107   Token sEnd;
86108   DbFixer sFix;
86109   Token *pName = 0;
86110   int iDb;
86111   sqlite3 *db = pParse->db;
86112 
86113   if( pParse->nVar>0 ){
86114     sqlite3ErrorMsg(pParse, "parameters are not allowed in views");
86115     sqlite3SelectDelete(db, pSelect);
86116     return;
86117   }
86118   sqlite3StartTable(pParse, pName1, pName2, isTemp, 1, 0, noErr);
86119   p = pParse->pNewTable;
86120   if( p==0 || pParse->nErr ){
86121     sqlite3SelectDelete(db, pSelect);
86122     return;
86123   }
86124   sqlite3TwoPartName(pParse, pName1, pName2, &pName);
86125   iDb = sqlite3SchemaToIndex(db, p->pSchema);
86126   sqlite3FixInit(&sFix, pParse, iDb, "view", pName);
86127   if( sqlite3FixSelect(&sFix, pSelect) ){
86128     sqlite3SelectDelete(db, pSelect);
86129     return;
86130   }
86131 
86132   /* Make a copy of the entire SELECT statement that defines the view.
86133   ** This will force all the Expr.token.z values to be dynamically
86134   ** allocated rather than point to the input string - which means that
86135   ** they will persist after the current sqlite3_exec() call returns.
86136   */
86137   p->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
86138   sqlite3SelectDelete(db, pSelect);
86139   if( db->mallocFailed ){
86140     return;
86141   }
86142   if( !db->init.busy ){
86143     sqlite3ViewGetColumnNames(pParse, p);
86144   }
86145 
86146   /* Locate the end of the CREATE VIEW statement.  Make sEnd point to
86147   ** the end.
86148   */
86149   sEnd = pParse->sLastToken;
86150   if( ALWAYS(sEnd.z[0]!=0) && sEnd.z[0]!=';' ){
86151     sEnd.z += sEnd.n;
86152   }
86153   sEnd.n = 0;
86154   n = (int)(sEnd.z - pBegin->z);
86155   z = pBegin->z;
86156   while( ALWAYS(n>0) && sqlite3Isspace(z[n-1]) ){ n--; }
86157   sEnd.z = &z[n-1];
86158   sEnd.n = 1;
86159 
86160   /* Use sqlite3EndTable() to add the view to the SQLITE_MASTER table */
86161   sqlite3EndTable(pParse, 0, &sEnd, 0, 0);
86162   return;
86163 }
86164 #endif /* SQLITE_OMIT_VIEW */
86165 
86166 #if !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE)
86167 /*
86168 ** The Table structure pTable is really a VIEW.  Fill in the names of
86169 ** the columns of the view in the pTable structure.  Return the number
86170 ** of errors.  If an error is seen leave an error message in pParse->zErrMsg.
86171 */
86172 SQLITE_PRIVATE int sqlite3ViewGetColumnNames(Parse *pParse, Table *pTable){
86173   Table *pSelTab;   /* A fake table from which we get the result set */
86174   Select *pSel;     /* Copy of the SELECT that implements the view */
86175   int nErr = 0;     /* Number of errors encountered */
86176   int n;            /* Temporarily holds the number of cursors assigned */
86177   sqlite3 *db = pParse->db;  /* Database connection for malloc errors */
86178   int (*xAuth)(void*,int,const char*,const char*,const char*,const char*);
86179 
86180   assert( pTable );
86181 
86182 #ifndef SQLITE_OMIT_VIRTUALTABLE
86183   if( sqlite3VtabCallConnect(pParse, pTable) ){
86184     return SQLITE_ERROR;
86185   }
86186   if( IsVirtual(pTable) ) return 0;
86187 #endif
86188 
86189 #ifndef SQLITE_OMIT_VIEW
86190   /* A positive nCol means the columns names for this view are
86191   ** already known.
86192   */
86193   if( pTable->nCol>0 ) return 0;
86194 
86195   /* A negative nCol is a special marker meaning that we are currently
86196   ** trying to compute the column names.  If we enter this routine with
86197   ** a negative nCol, it means two or more views form a loop, like this:
86198   **
86199   **     CREATE VIEW one AS SELECT * FROM two;
86200   **     CREATE VIEW two AS SELECT * FROM one;
86201   **
86202   ** Actually, the error above is now caught prior to reaching this point.
86203   ** But the following test is still important as it does come up
86204   ** in the following:
86205   **
86206   **     CREATE TABLE main.ex1(a);
86207   **     CREATE TEMP VIEW ex1 AS SELECT a FROM ex1;
86208   **     SELECT * FROM temp.ex1;
86209   */
86210   if( pTable->nCol<0 ){
86211     sqlite3ErrorMsg(pParse, "view %s is circularly defined", pTable->zName);
86212     return 1;
86213   }
86214   assert( pTable->nCol>=0 );
86215 
86216   /* If we get this far, it means we need to compute the table names.
86217   ** Note that the call to sqlite3ResultSetOfSelect() will expand any
86218   ** "*" elements in the results set of the view and will assign cursors
86219   ** to the elements of the FROM clause.  But we do not want these changes
86220   ** to be permanent.  So the computation is done on a copy of the SELECT
86221   ** statement that defines the view.
86222   */
86223   assert( pTable->pSelect );
86224   pSel = sqlite3SelectDup(db, pTable->pSelect, 0);
86225   if( pSel ){
86226     u8 enableLookaside = db->lookaside.bEnabled;
86227     n = pParse->nTab;
86228     sqlite3SrcListAssignCursors(pParse, pSel->pSrc);
86229     pTable->nCol = -1;
86230     db->lookaside.bEnabled = 0;
86231 #ifndef SQLITE_OMIT_AUTHORIZATION
86232     xAuth = db->xAuth;
86233     db->xAuth = 0;
86234     pSelTab = sqlite3ResultSetOfSelect(pParse, pSel);
86235     db->xAuth = xAuth;
86236 #else
86237     pSelTab = sqlite3ResultSetOfSelect(pParse, pSel);
86238 #endif
86239     db->lookaside.bEnabled = enableLookaside;
86240     pParse->nTab = n;
86241     if( pSelTab ){
86242       assert( pTable->aCol==0 );
86243       pTable->nCol = pSelTab->nCol;
86244       pTable->aCol = pSelTab->aCol;
86245       pSelTab->nCol = 0;
86246       pSelTab->aCol = 0;
86247       sqlite3DeleteTable(db, pSelTab);
86248       assert( sqlite3SchemaMutexHeld(db, 0, pTable->pSchema) );
86249       pTable->pSchema->flags |= DB_UnresetViews;
86250     }else{
86251       pTable->nCol = 0;
86252       nErr++;
86253     }
86254     sqlite3SelectDelete(db, pSel);
86255   } else {
86256     nErr++;
86257   }
86258 #endif /* SQLITE_OMIT_VIEW */
86259   return nErr;
86260 }
86261 #endif /* !defined(SQLITE_OMIT_VIEW) || !defined(SQLITE_OMIT_VIRTUALTABLE) */
86262 
86263 #ifndef SQLITE_OMIT_VIEW
86264 /*
86265 ** Clear the column names from every VIEW in database idx.
86266 */
86267 static void sqliteViewResetAll(sqlite3 *db, int idx){
86268   HashElem *i;
86269   assert( sqlite3SchemaMutexHeld(db, idx, 0) );
86270   if( !DbHasProperty(db, idx, DB_UnresetViews) ) return;
86271   for(i=sqliteHashFirst(&db->aDb[idx].pSchema->tblHash); i;i=sqliteHashNext(i)){
86272     Table *pTab = sqliteHashData(i);
86273     if( pTab->pSelect ){
86274       sqliteDeleteColumnNames(db, pTab);
86275       pTab->aCol = 0;
86276       pTab->nCol = 0;
86277     }
86278   }
86279   DbClearProperty(db, idx, DB_UnresetViews);
86280 }
86281 #else
86282 # define sqliteViewResetAll(A,B)
86283 #endif /* SQLITE_OMIT_VIEW */
86284 
86285 /*
86286 ** This function is called by the VDBE to adjust the internal schema
86287 ** used by SQLite when the btree layer moves a table root page. The
86288 ** root-page of a table or index in database iDb has changed from iFrom
86289 ** to iTo.
86290 **
86291 ** Ticket #1728:  The symbol table might still contain information
86292 ** on tables and/or indices that are the process of being deleted.
86293 ** If you are unlucky, one of those deleted indices or tables might
86294 ** have the same rootpage number as the real table or index that is
86295 ** being moved.  So we cannot stop searching after the first match
86296 ** because the first match might be for one of the deleted indices
86297 ** or tables and not the table/index that is actually being moved.
86298 ** We must continue looping until all tables and indices with
86299 ** rootpage==iFrom have been converted to have a rootpage of iTo
86300 ** in order to be certain that we got the right one.
86301 */
86302 #ifndef SQLITE_OMIT_AUTOVACUUM
86303 SQLITE_PRIVATE void sqlite3RootPageMoved(sqlite3 *db, int iDb, int iFrom, int iTo){
86304   HashElem *pElem;
86305   Hash *pHash;
86306   Db *pDb;
86307 
86308   assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
86309   pDb = &db->aDb[iDb];
86310   pHash = &pDb->pSchema->tblHash;
86311   for(pElem=sqliteHashFirst(pHash); pElem; pElem=sqliteHashNext(pElem)){
86312     Table *pTab = sqliteHashData(pElem);
86313     if( pTab->tnum==iFrom ){
86314       pTab->tnum = iTo;
86315     }
86316   }
86317   pHash = &pDb->pSchema->idxHash;
86318   for(pElem=sqliteHashFirst(pHash); pElem; pElem=sqliteHashNext(pElem)){
86319     Index *pIdx = sqliteHashData(pElem);
86320     if( pIdx->tnum==iFrom ){
86321       pIdx->tnum = iTo;
86322     }
86323   }
86324 }
86325 #endif
86326 
86327 /*
86328 ** Write code to erase the table with root-page iTable from database iDb.
86329 ** Also write code to modify the sqlite_master table and internal schema
86330 ** if a root-page of another table is moved by the btree-layer whilst
86331 ** erasing iTable (this can happen with an auto-vacuum database).
86332 */
86333 static void destroyRootPage(Parse *pParse, int iTable, int iDb){
86334   Vdbe *v = sqlite3GetVdbe(pParse);
86335   int r1 = sqlite3GetTempReg(pParse);
86336   sqlite3VdbeAddOp3(v, OP_Destroy, iTable, r1, iDb);
86337   sqlite3MayAbort(pParse);
86338 #ifndef SQLITE_OMIT_AUTOVACUUM
86339   /* OP_Destroy stores an in integer r1. If this integer
86340   ** is non-zero, then it is the root page number of a table moved to
86341   ** location iTable. The following code modifies the sqlite_master table to
86342   ** reflect this.
86343   **
86344   ** The "#NNN" in the SQL is a special constant that means whatever value
86345   ** is in register NNN.  See grammar rules associated with the TK_REGISTER
86346   ** token for additional information.
86347   */
86348   sqlite3NestedParse(pParse,
86349      "UPDATE %Q.%s SET rootpage=%d WHERE #%d AND rootpage=#%d",
86350      pParse->db->aDb[iDb].zName, SCHEMA_TABLE(iDb), iTable, r1, r1);
86351 #endif
86352   sqlite3ReleaseTempReg(pParse, r1);
86353 }
86354 
86355 /*
86356 ** Write VDBE code to erase table pTab and all associated indices on disk.
86357 ** Code to update the sqlite_master tables and internal schema definitions
86358 ** in case a root-page belonging to another table is moved by the btree layer
86359 ** is also added (this can happen with an auto-vacuum database).
86360 */
86361 static void destroyTable(Parse *pParse, Table *pTab){
86362 #ifdef SQLITE_OMIT_AUTOVACUUM
86363   Index *pIdx;
86364   int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
86365   destroyRootPage(pParse, pTab->tnum, iDb);
86366   for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
86367     destroyRootPage(pParse, pIdx->tnum, iDb);
86368   }
86369 #else
86370   /* If the database may be auto-vacuum capable (if SQLITE_OMIT_AUTOVACUUM
86371   ** is not defined), then it is important to call OP_Destroy on the
86372   ** table and index root-pages in order, starting with the numerically
86373   ** largest root-page number. This guarantees that none of the root-pages
86374   ** to be destroyed is relocated by an earlier OP_Destroy. i.e. if the
86375   ** following were coded:
86376   **
86377   ** OP_Destroy 4 0
86378   ** ...
86379   ** OP_Destroy 5 0
86380   **
86381   ** and root page 5 happened to be the largest root-page number in the
86382   ** database, then root page 5 would be moved to page 4 by the
86383   ** "OP_Destroy 4 0" opcode. The subsequent "OP_Destroy 5 0" would hit
86384   ** a free-list page.
86385   */
86386   int iTab = pTab->tnum;
86387   int iDestroyed = 0;
86388 
86389   while( 1 ){
86390     Index *pIdx;
86391     int iLargest = 0;
86392 
86393     if( iDestroyed==0 || iTab<iDestroyed ){
86394       iLargest = iTab;
86395     }
86396     for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
86397       int iIdx = pIdx->tnum;
86398       assert( pIdx->pSchema==pTab->pSchema );
86399       if( (iDestroyed==0 || (iIdx<iDestroyed)) && iIdx>iLargest ){
86400         iLargest = iIdx;
86401       }
86402     }
86403     if( iLargest==0 ){
86404       return;
86405     }else{
86406       int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
86407       assert( iDb>=0 && iDb<pParse->db->nDb );
86408       destroyRootPage(pParse, iLargest, iDb);
86409       iDestroyed = iLargest;
86410     }
86411   }
86412 #endif
86413 }
86414 
86415 /*
86416 ** Remove entries from the sqlite_statN tables (for N in (1,2,3))
86417 ** after a DROP INDEX or DROP TABLE command.
86418 */
86419 static void sqlite3ClearStatTables(
86420   Parse *pParse,         /* The parsing context */
86421   int iDb,               /* The database number */
86422   const char *zType,     /* "idx" or "tbl" */
86423   const char *zName      /* Name of index or table */
86424 ){
86425   int i;
86426   const char *zDbName = pParse->db->aDb[iDb].zName;
86427   for(i=1; i<=4; i++){
86428     char zTab[24];
86429     sqlite3_snprintf(sizeof(zTab),zTab,"sqlite_stat%d",i);
86430     if( sqlite3FindTable(pParse->db, zTab, zDbName) ){
86431       sqlite3NestedParse(pParse,
86432         "DELETE FROM %Q.%s WHERE %s=%Q",
86433         zDbName, zTab, zType, zName
86434       );
86435     }
86436   }
86437 }
86438 
86439 /*
86440 ** Generate code to drop a table.
86441 */
86442 SQLITE_PRIVATE void sqlite3CodeDropTable(Parse *pParse, Table *pTab, int iDb, int isView){
86443   Vdbe *v;
86444   sqlite3 *db = pParse->db;
86445   Trigger *pTrigger;
86446   Db *pDb = &db->aDb[iDb];
86447 
86448   v = sqlite3GetVdbe(pParse);
86449   assert( v!=0 );
86450   sqlite3BeginWriteOperation(pParse, 1, iDb);
86451 
86452 #ifndef SQLITE_OMIT_VIRTUALTABLE
86453   if( IsVirtual(pTab) ){
86454     sqlite3VdbeAddOp0(v, OP_VBegin);
86455   }
86456 #endif
86457 
86458   /* Drop all triggers associated with the table being dropped. Code
86459   ** is generated to remove entries from sqlite_master and/or
86460   ** sqlite_temp_master if required.
86461   */
86462   pTrigger = sqlite3TriggerList(pParse, pTab);
86463   while( pTrigger ){
86464     assert( pTrigger->pSchema==pTab->pSchema ||
86465         pTrigger->pSchema==db->aDb[1].pSchema );
86466     sqlite3DropTriggerPtr(pParse, pTrigger);
86467     pTrigger = pTrigger->pNext;
86468   }
86469 
86470 #ifndef SQLITE_OMIT_AUTOINCREMENT
86471   /* Remove any entries of the sqlite_sequence table associated with
86472   ** the table being dropped. This is done before the table is dropped
86473   ** at the btree level, in case the sqlite_sequence table needs to
86474   ** move as a result of the drop (can happen in auto-vacuum mode).
86475   */
86476   if( pTab->tabFlags & TF_Autoincrement ){
86477     sqlite3NestedParse(pParse,
86478       "DELETE FROM %Q.sqlite_sequence WHERE name=%Q",
86479       pDb->zName, pTab->zName
86480     );
86481   }
86482 #endif
86483 
86484   /* Drop all SQLITE_MASTER table and index entries that refer to the
86485   ** table. The program name loops through the master table and deletes
86486   ** every row that refers to a table of the same name as the one being
86487   ** dropped. Triggers are handled separately because a trigger can be
86488   ** created in the temp database that refers to a table in another
86489   ** database.
86490   */
86491   sqlite3NestedParse(pParse,
86492       "DELETE FROM %Q.%s WHERE tbl_name=%Q and type!='trigger'",
86493       pDb->zName, SCHEMA_TABLE(iDb), pTab->zName);
86494   if( !isView && !IsVirtual(pTab) ){
86495     destroyTable(pParse, pTab);
86496   }
86497 
86498   /* Remove the table entry from SQLite's internal schema and modify
86499   ** the schema cookie.
86500   */
86501   if( IsVirtual(pTab) ){
86502     sqlite3VdbeAddOp4(v, OP_VDestroy, iDb, 0, 0, pTab->zName, 0);
86503   }
86504   sqlite3VdbeAddOp4(v, OP_DropTable, iDb, 0, 0, pTab->zName, 0);
86505   sqlite3ChangeCookie(pParse, iDb);
86506   sqliteViewResetAll(db, iDb);
86507 }
86508 
86509 /*
86510 ** This routine is called to do the work of a DROP TABLE statement.
86511 ** pName is the name of the table to be dropped.
86512 */
86513 SQLITE_PRIVATE void sqlite3DropTable(Parse *pParse, SrcList *pName, int isView, int noErr){
86514   Table *pTab;
86515   Vdbe *v;
86516   sqlite3 *db = pParse->db;
86517   int iDb;
86518 
86519   if( db->mallocFailed ){
86520     goto exit_drop_table;
86521   }
86522   assert( pParse->nErr==0 );
86523   assert( pName->nSrc==1 );
86524   if( noErr ) db->suppressErr++;
86525   pTab = sqlite3LocateTableItem(pParse, isView, &pName->a[0]);
86526   if( noErr ) db->suppressErr--;
86527 
86528   if( pTab==0 ){
86529     if( noErr ) sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].zDatabase);
86530     goto exit_drop_table;
86531   }
86532   iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
86533   assert( iDb>=0 && iDb<db->nDb );
86534 
86535   /* If pTab is a virtual table, call ViewGetColumnNames() to ensure
86536   ** it is initialized.
86537   */
86538   if( IsVirtual(pTab) && sqlite3ViewGetColumnNames(pParse, pTab) ){
86539     goto exit_drop_table;
86540   }
86541 #ifndef SQLITE_OMIT_AUTHORIZATION
86542   {
86543     int code;
86544     const char *zTab = SCHEMA_TABLE(iDb);
86545     const char *zDb = db->aDb[iDb].zName;
86546     const char *zArg2 = 0;
86547     if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb)){
86548       goto exit_drop_table;
86549     }
86550     if( isView ){
86551       if( !OMIT_TEMPDB && iDb==1 ){
86552         code = SQLITE_DROP_TEMP_VIEW;
86553       }else{
86554         code = SQLITE_DROP_VIEW;
86555       }
86556 #ifndef SQLITE_OMIT_VIRTUALTABLE
86557     }else if( IsVirtual(pTab) ){
86558       code = SQLITE_DROP_VTABLE;
86559       zArg2 = sqlite3GetVTable(db, pTab)->pMod->zName;
86560 #endif
86561     }else{
86562       if( !OMIT_TEMPDB && iDb==1 ){
86563         code = SQLITE_DROP_TEMP_TABLE;
86564       }else{
86565         code = SQLITE_DROP_TABLE;
86566       }
86567     }
86568     if( sqlite3AuthCheck(pParse, code, pTab->zName, zArg2, zDb) ){
86569       goto exit_drop_table;
86570     }
86571     if( sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb) ){
86572       goto exit_drop_table;
86573     }
86574   }
86575 #endif
86576   if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0
86577     && sqlite3StrNICmp(pTab->zName, "sqlite_stat", 11)!=0 ){
86578     sqlite3ErrorMsg(pParse, "table %s may not be dropped", pTab->zName);
86579     goto exit_drop_table;
86580   }
86581 
86582 #ifndef SQLITE_OMIT_VIEW
86583   /* Ensure DROP TABLE is not used on a view, and DROP VIEW is not used
86584   ** on a table.
86585   */
86586   if( isView && pTab->pSelect==0 ){
86587     sqlite3ErrorMsg(pParse, "use DROP TABLE to delete table %s", pTab->zName);
86588     goto exit_drop_table;
86589   }
86590   if( !isView && pTab->pSelect ){
86591     sqlite3ErrorMsg(pParse, "use DROP VIEW to delete view %s", pTab->zName);
86592     goto exit_drop_table;
86593   }
86594 #endif
86595 
86596   /* Generate code to remove the table from the master table
86597   ** on disk.
86598   */
86599   v = sqlite3GetVdbe(pParse);
86600   if( v ){
86601     sqlite3BeginWriteOperation(pParse, 1, iDb);
86602     sqlite3ClearStatTables(pParse, iDb, "tbl", pTab->zName);
86603     sqlite3FkDropTable(pParse, pName, pTab);
86604     sqlite3CodeDropTable(pParse, pTab, iDb, isView);
86605   }
86606 
86607 exit_drop_table:
86608   sqlite3SrcListDelete(db, pName);
86609 }
86610 
86611 /*
86612 ** This routine is called to create a new foreign key on the table
86613 ** currently under construction.  pFromCol determines which columns
86614 ** in the current table point to the foreign key.  If pFromCol==0 then
86615 ** connect the key to the last column inserted.  pTo is the name of
86616 ** the table referred to (a.k.a the "parent" table).  pToCol is a list
86617 ** of tables in the parent pTo table.  flags contains all
86618 ** information about the conflict resolution algorithms specified
86619 ** in the ON DELETE, ON UPDATE and ON INSERT clauses.
86620 **
86621 ** An FKey structure is created and added to the table currently
86622 ** under construction in the pParse->pNewTable field.
86623 **
86624 ** The foreign key is set for IMMEDIATE processing.  A subsequent call
86625 ** to sqlite3DeferForeignKey() might change this to DEFERRED.
86626 */
86627 SQLITE_PRIVATE void sqlite3CreateForeignKey(
86628   Parse *pParse,       /* Parsing context */
86629   ExprList *pFromCol,  /* Columns in this table that point to other table */
86630   Token *pTo,          /* Name of the other table */
86631   ExprList *pToCol,    /* Columns in the other table */
86632   int flags            /* Conflict resolution algorithms. */
86633 ){
86634   sqlite3 *db = pParse->db;
86635 #ifndef SQLITE_OMIT_FOREIGN_KEY
86636   FKey *pFKey = 0;
86637   FKey *pNextTo;
86638   Table *p = pParse->pNewTable;
86639   int nByte;
86640   int i;
86641   int nCol;
86642   char *z;
86643 
86644   assert( pTo!=0 );
86645   if( p==0 || IN_DECLARE_VTAB ) goto fk_end;
86646   if( pFromCol==0 ){
86647     int iCol = p->nCol-1;
86648     if( NEVER(iCol<0) ) goto fk_end;
86649     if( pToCol && pToCol->nExpr!=1 ){
86650       sqlite3ErrorMsg(pParse, "foreign key on %s"
86651          " should reference only one column of table %T",
86652          p->aCol[iCol].zName, pTo);
86653       goto fk_end;
86654     }
86655     nCol = 1;
86656   }else if( pToCol && pToCol->nExpr!=pFromCol->nExpr ){
86657     sqlite3ErrorMsg(pParse,
86658         "number of columns in foreign key does not match the number of "
86659         "columns in the referenced table");
86660     goto fk_end;
86661   }else{
86662     nCol = pFromCol->nExpr;
86663   }
86664   nByte = sizeof(*pFKey) + (nCol-1)*sizeof(pFKey->aCol[0]) + pTo->n + 1;
86665   if( pToCol ){
86666     for(i=0; i<pToCol->nExpr; i++){
86667       nByte += sqlite3Strlen30(pToCol->a[i].zName) + 1;
86668     }
86669   }
86670   pFKey = sqlite3DbMallocZero(db, nByte );
86671   if( pFKey==0 ){
86672     goto fk_end;
86673   }
86674   pFKey->pFrom = p;
86675   pFKey->pNextFrom = p->pFKey;
86676   z = (char*)&pFKey->aCol[nCol];
86677   pFKey->zTo = z;
86678   memcpy(z, pTo->z, pTo->n);
86679   z[pTo->n] = 0;
86680   sqlite3Dequote(z);
86681   z += pTo->n+1;
86682   pFKey->nCol = nCol;
86683   if( pFromCol==0 ){
86684     pFKey->aCol[0].iFrom = p->nCol-1;
86685   }else{
86686     for(i=0; i<nCol; i++){
86687       int j;
86688       for(j=0; j<p->nCol; j++){
86689         if( sqlite3StrICmp(p->aCol[j].zName, pFromCol->a[i].zName)==0 ){
86690           pFKey->aCol[i].iFrom = j;
86691           break;
86692         }
86693       }
86694       if( j>=p->nCol ){
86695         sqlite3ErrorMsg(pParse,
86696           "unknown column \"%s\" in foreign key definition",
86697           pFromCol->a[i].zName);
86698         goto fk_end;
86699       }
86700     }
86701   }
86702   if( pToCol ){
86703     for(i=0; i<nCol; i++){
86704       int n = sqlite3Strlen30(pToCol->a[i].zName);
86705       pFKey->aCol[i].zCol = z;
86706       memcpy(z, pToCol->a[i].zName, n);
86707       z[n] = 0;
86708       z += n+1;
86709     }
86710   }
86711   pFKey->isDeferred = 0;
86712   pFKey->aAction[0] = (u8)(flags & 0xff);            /* ON DELETE action */
86713   pFKey->aAction[1] = (u8)((flags >> 8 ) & 0xff);    /* ON UPDATE action */
86714 
86715   assert( sqlite3SchemaMutexHeld(db, 0, p->pSchema) );
86716   pNextTo = (FKey *)sqlite3HashInsert(&p->pSchema->fkeyHash,
86717       pFKey->zTo, sqlite3Strlen30(pFKey->zTo), (void *)pFKey
86718   );
86719   if( pNextTo==pFKey ){
86720     db->mallocFailed = 1;
86721     goto fk_end;
86722   }
86723   if( pNextTo ){
86724     assert( pNextTo->pPrevTo==0 );
86725     pFKey->pNextTo = pNextTo;
86726     pNextTo->pPrevTo = pFKey;
86727   }
86728 
86729   /* Link the foreign key to the table as the last step.
86730   */
86731   p->pFKey = pFKey;
86732   pFKey = 0;
86733 
86734 fk_end:
86735   sqlite3DbFree(db, pFKey);
86736 #endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
86737   sqlite3ExprListDelete(db, pFromCol);
86738   sqlite3ExprListDelete(db, pToCol);
86739 }
86740 
86741 /*
86742 ** This routine is called when an INITIALLY IMMEDIATE or INITIALLY DEFERRED
86743 ** clause is seen as part of a foreign key definition.  The isDeferred
86744 ** parameter is 1 for INITIALLY DEFERRED and 0 for INITIALLY IMMEDIATE.
86745 ** The behavior of the most recently created foreign key is adjusted
86746 ** accordingly.
86747 */
86748 SQLITE_PRIVATE void sqlite3DeferForeignKey(Parse *pParse, int isDeferred){
86749 #ifndef SQLITE_OMIT_FOREIGN_KEY
86750   Table *pTab;
86751   FKey *pFKey;
86752   if( (pTab = pParse->pNewTable)==0 || (pFKey = pTab->pFKey)==0 ) return;
86753   assert( isDeferred==0 || isDeferred==1 ); /* EV: R-30323-21917 */
86754   pFKey->isDeferred = (u8)isDeferred;
86755 #endif
86756 }
86757 
86758 /*
86759 ** Generate code that will erase and refill index *pIdx.  This is
86760 ** used to initialize a newly created index or to recompute the
86761 ** content of an index in response to a REINDEX command.
86762 **
86763 ** if memRootPage is not negative, it means that the index is newly
86764 ** created.  The register specified by memRootPage contains the
86765 ** root page number of the index.  If memRootPage is negative, then
86766 ** the index already exists and must be cleared before being refilled and
86767 ** the root page number of the index is taken from pIndex->tnum.
86768 */
86769 static void sqlite3RefillIndex(Parse *pParse, Index *pIndex, int memRootPage){
86770   Table *pTab = pIndex->pTable;  /* The table that is indexed */
86771   int iTab = pParse->nTab++;     /* Btree cursor used for pTab */
86772   int iIdx = pParse->nTab++;     /* Btree cursor used for pIndex */
86773   int iSorter;                   /* Cursor opened by OpenSorter (if in use) */
86774   int addr1;                     /* Address of top of loop */
86775   int addr2;                     /* Address to jump to for next iteration */
86776   int tnum;                      /* Root page of index */
86777   int iPartIdxLabel;             /* Jump to this label to skip a row */
86778   Vdbe *v;                       /* Generate code into this virtual machine */
86779   KeyInfo *pKey;                 /* KeyInfo for index */
86780   int regRecord;                 /* Register holding assemblied index record */
86781   sqlite3 *db = pParse->db;      /* The database connection */
86782   int iDb = sqlite3SchemaToIndex(db, pIndex->pSchema);
86783 
86784 #ifndef SQLITE_OMIT_AUTHORIZATION
86785   if( sqlite3AuthCheck(pParse, SQLITE_REINDEX, pIndex->zName, 0,
86786       db->aDb[iDb].zName ) ){
86787     return;
86788   }
86789 #endif
86790 
86791   /* Require a write-lock on the table to perform this operation */
86792   sqlite3TableLock(pParse, iDb, pTab->tnum, 1, pTab->zName);
86793 
86794   v = sqlite3GetVdbe(pParse);
86795   if( v==0 ) return;
86796   if( memRootPage>=0 ){
86797     tnum = memRootPage;
86798   }else{
86799     tnum = pIndex->tnum;
86800   }
86801   pKey = sqlite3KeyInfoOfIndex(pParse, pIndex);
86802 
86803   /* Open the sorter cursor if we are to use one. */
86804   iSorter = pParse->nTab++;
86805   sqlite3VdbeAddOp4(v, OP_SorterOpen, iSorter, 0, 0, (char*)
86806                     sqlite3KeyInfoRef(pKey), P4_KEYINFO);
86807 
86808   /* Open the table. Loop through all rows of the table, inserting index
86809   ** records into the sorter. */
86810   sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead);
86811   addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iTab, 0);
86812   regRecord = sqlite3GetTempReg(pParse);
86813 
86814   sqlite3GenerateIndexKey(pParse,pIndex,iTab,regRecord,0,&iPartIdxLabel,0,0);
86815   sqlite3VdbeAddOp2(v, OP_SorterInsert, iSorter, regRecord);
86816   sqlite3VdbeResolveLabel(v, iPartIdxLabel);
86817   sqlite3VdbeAddOp2(v, OP_Next, iTab, addr1+1);
86818   sqlite3VdbeJumpHere(v, addr1);
86819   if( memRootPage<0 ) sqlite3VdbeAddOp2(v, OP_Clear, tnum, iDb);
86820   sqlite3VdbeAddOp4(v, OP_OpenWrite, iIdx, tnum, iDb,
86821                     (char *)pKey, P4_KEYINFO);
86822   sqlite3VdbeChangeP5(v, OPFLAG_BULKCSR|((memRootPage>=0)?OPFLAG_P2ISREG:0));
86823 
86824   addr1 = sqlite3VdbeAddOp2(v, OP_SorterSort, iSorter, 0);
86825   assert( pKey!=0 || db->mallocFailed || pParse->nErr );
86826   if( pIndex->onError!=OE_None && pKey!=0 ){
86827     int j2 = sqlite3VdbeCurrentAddr(v) + 3;
86828     sqlite3VdbeAddOp2(v, OP_Goto, 0, j2);
86829     addr2 = sqlite3VdbeCurrentAddr(v);
86830     sqlite3VdbeAddOp4Int(v, OP_SorterCompare, iSorter, j2, regRecord,
86831                          pKey->nField - pIndex->nKeyCol);
86832     sqlite3UniqueConstraint(pParse, OE_Abort, pIndex);
86833   }else{
86834     addr2 = sqlite3VdbeCurrentAddr(v);
86835   }
86836   sqlite3VdbeAddOp2(v, OP_SorterData, iSorter, regRecord);
86837   sqlite3VdbeAddOp3(v, OP_IdxInsert, iIdx, regRecord, 1);
86838   sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
86839   sqlite3ReleaseTempReg(pParse, regRecord);
86840   sqlite3VdbeAddOp2(v, OP_SorterNext, iSorter, addr2);
86841   sqlite3VdbeJumpHere(v, addr1);
86842 
86843   sqlite3VdbeAddOp1(v, OP_Close, iTab);
86844   sqlite3VdbeAddOp1(v, OP_Close, iIdx);
86845   sqlite3VdbeAddOp1(v, OP_Close, iSorter);
86846 }
86847 
86848 /*
86849 ** Allocate heap space to hold an Index object with nCol columns.
86850 **
86851 ** Increase the allocation size to provide an extra nExtra bytes
86852 ** of 8-byte aligned space after the Index object and return a
86853 ** pointer to this extra space in *ppExtra.
86854 */
86855 SQLITE_PRIVATE Index *sqlite3AllocateIndexObject(
86856   sqlite3 *db,         /* Database connection */
86857   i16 nCol,            /* Total number of columns in the index */
86858   int nExtra,          /* Number of bytes of extra space to alloc */
86859   char **ppExtra       /* Pointer to the "extra" space */
86860 ){
86861   Index *p;            /* Allocated index object */
86862   int nByte;           /* Bytes of space for Index object + arrays */
86863 
86864   nByte = ROUND8(sizeof(Index)) +              /* Index structure  */
86865           ROUND8(sizeof(char*)*nCol) +         /* Index.azColl     */
86866           ROUND8(sizeof(tRowcnt)*(nCol+1) +    /* Index.aiRowEst   */
86867                  sizeof(i16)*nCol +            /* Index.aiColumn   */
86868                  sizeof(u8)*nCol);             /* Index.aSortOrder */
86869   p = sqlite3DbMallocZero(db, nByte + nExtra);
86870   if( p ){
86871     char *pExtra = ((char*)p)+ROUND8(sizeof(Index));
86872     p->azColl = (char**)pExtra;      pExtra += ROUND8(sizeof(char*)*nCol);
86873     p->aiRowEst = (tRowcnt*)pExtra;  pExtra += sizeof(tRowcnt)*(nCol+1);
86874     p->aiColumn = (i16*)pExtra;      pExtra += sizeof(i16)*nCol;
86875     p->aSortOrder = (u8*)pExtra;
86876     p->nColumn = nCol;
86877     p->nKeyCol = nCol - 1;
86878     *ppExtra = ((char*)p) + nByte;
86879   }
86880   return p;
86881 }
86882 
86883 /*
86884 ** Create a new index for an SQL table.  pName1.pName2 is the name of the index
86885 ** and pTblList is the name of the table that is to be indexed.  Both will
86886 ** be NULL for a primary key or an index that is created to satisfy a
86887 ** UNIQUE constraint.  If pTable and pIndex are NULL, use pParse->pNewTable
86888 ** as the table to be indexed.  pParse->pNewTable is a table that is
86889 ** currently being constructed by a CREATE TABLE statement.
86890 **
86891 ** pList is a list of columns to be indexed.  pList will be NULL if this
86892 ** is a primary key or unique-constraint on the most recent column added
86893 ** to the table currently under construction.
86894 **
86895 ** If the index is created successfully, return a pointer to the new Index
86896 ** structure. This is used by sqlite3AddPrimaryKey() to mark the index
86897 ** as the tables primary key (Index.autoIndex==2).
86898 */
86899 SQLITE_PRIVATE Index *sqlite3CreateIndex(
86900   Parse *pParse,     /* All information about this parse */
86901   Token *pName1,     /* First part of index name. May be NULL */
86902   Token *pName2,     /* Second part of index name. May be NULL */
86903   SrcList *pTblName, /* Table to index. Use pParse->pNewTable if 0 */
86904   ExprList *pList,   /* A list of columns to be indexed */
86905   int onError,       /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
86906   Token *pStart,     /* The CREATE token that begins this statement */
86907   Expr *pPIWhere,    /* WHERE clause for partial indices */
86908   int sortOrder,     /* Sort order of primary key when pList==NULL */
86909   int ifNotExist     /* Omit error if index already exists */
86910 ){
86911   Index *pRet = 0;     /* Pointer to return */
86912   Table *pTab = 0;     /* Table to be indexed */
86913   Index *pIndex = 0;   /* The index to be created */
86914   char *zName = 0;     /* Name of the index */
86915   int nName;           /* Number of characters in zName */
86916   int i, j;
86917   DbFixer sFix;        /* For assigning database names to pTable */
86918   int sortOrderMask;   /* 1 to honor DESC in index.  0 to ignore. */
86919   sqlite3 *db = pParse->db;
86920   Db *pDb;             /* The specific table containing the indexed database */
86921   int iDb;             /* Index of the database that is being written */
86922   Token *pName = 0;    /* Unqualified name of the index to create */
86923   struct ExprList_item *pListItem; /* For looping over pList */
86924   const Column *pTabCol;           /* A column in the table */
86925   int nExtra = 0;                  /* Space allocated for zExtra[] */
86926   int nExtraCol;                   /* Number of extra columns needed */
86927   char *zExtra = 0;                /* Extra space after the Index object */
86928   Index *pPk = 0;      /* PRIMARY KEY index for WITHOUT ROWID tables */
86929 
86930   assert( pParse->nErr==0 );      /* Never called with prior errors */
86931   if( db->mallocFailed || IN_DECLARE_VTAB ){
86932     goto exit_create_index;
86933   }
86934   if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
86935     goto exit_create_index;
86936   }
86937 
86938   /*
86939   ** Find the table that is to be indexed.  Return early if not found.
86940   */
86941   if( pTblName!=0 ){
86942 
86943     /* Use the two-part index name to determine the database
86944     ** to search for the table. 'Fix' the table name to this db
86945     ** before looking up the table.
86946     */
86947     assert( pName1 && pName2 );
86948     iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
86949     if( iDb<0 ) goto exit_create_index;
86950     assert( pName && pName->z );
86951 
86952 #ifndef SQLITE_OMIT_TEMPDB
86953     /* If the index name was unqualified, check if the table
86954     ** is a temp table. If so, set the database to 1. Do not do this
86955     ** if initialising a database schema.
86956     */
86957     if( !db->init.busy ){
86958       pTab = sqlite3SrcListLookup(pParse, pTblName);
86959       if( pName2->n==0 && pTab && pTab->pSchema==db->aDb[1].pSchema ){
86960         iDb = 1;
86961       }
86962     }
86963 #endif
86964 
86965     sqlite3FixInit(&sFix, pParse, iDb, "index", pName);
86966     if( sqlite3FixSrcList(&sFix, pTblName) ){
86967       /* Because the parser constructs pTblName from a single identifier,
86968       ** sqlite3FixSrcList can never fail. */
86969       assert(0);
86970     }
86971     pTab = sqlite3LocateTableItem(pParse, 0, &pTblName->a[0]);
86972     assert( db->mallocFailed==0 || pTab==0 );
86973     if( pTab==0 ) goto exit_create_index;
86974     if( iDb==1 && db->aDb[iDb].pSchema!=pTab->pSchema ){
86975       sqlite3ErrorMsg(pParse,
86976            "cannot create a TEMP index on non-TEMP table \"%s\"",
86977            pTab->zName);
86978       goto exit_create_index;
86979     }
86980     if( !HasRowid(pTab) ) pPk = sqlite3PrimaryKeyIndex(pTab);
86981   }else{
86982     assert( pName==0 );
86983     assert( pStart==0 );
86984     pTab = pParse->pNewTable;
86985     if( !pTab ) goto exit_create_index;
86986     iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
86987   }
86988   pDb = &db->aDb[iDb];
86989 
86990   assert( pTab!=0 );
86991   assert( pParse->nErr==0 );
86992   if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0
86993        && sqlite3StrNICmp(&pTab->zName[7],"altertab_",9)!=0 ){
86994     sqlite3ErrorMsg(pParse, "table %s may not be indexed", pTab->zName);
86995     goto exit_create_index;
86996   }
86997 #ifndef SQLITE_OMIT_VIEW
86998   if( pTab->pSelect ){
86999     sqlite3ErrorMsg(pParse, "views may not be indexed");
87000     goto exit_create_index;
87001   }
87002 #endif
87003 #ifndef SQLITE_OMIT_VIRTUALTABLE
87004   if( IsVirtual(pTab) ){
87005     sqlite3ErrorMsg(pParse, "virtual tables may not be indexed");
87006     goto exit_create_index;
87007   }
87008 #endif
87009 
87010   /*
87011   ** Find the name of the index.  Make sure there is not already another
87012   ** index or table with the same name.
87013   **
87014   ** Exception:  If we are reading the names of permanent indices from the
87015   ** sqlite_master table (because some other process changed the schema) and
87016   ** one of the index names collides with the name of a temporary table or
87017   ** index, then we will continue to process this index.
87018   **
87019   ** If pName==0 it means that we are
87020   ** dealing with a primary key or UNIQUE constraint.  We have to invent our
87021   ** own name.
87022   */
87023   if( pName ){
87024     zName = sqlite3NameFromToken(db, pName);
87025     if( zName==0 ) goto exit_create_index;
87026     assert( pName->z!=0 );
87027     if( SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
87028       goto exit_create_index;
87029     }
87030     if( !db->init.busy ){
87031       if( sqlite3FindTable(db, zName, 0)!=0 ){
87032         sqlite3ErrorMsg(pParse, "there is already a table named %s", zName);
87033         goto exit_create_index;
87034       }
87035     }
87036     if( sqlite3FindIndex(db, zName, pDb->zName)!=0 ){
87037       if( !ifNotExist ){
87038         sqlite3ErrorMsg(pParse, "index %s already exists", zName);
87039       }else{
87040         assert( !db->init.busy );
87041         sqlite3CodeVerifySchema(pParse, iDb);
87042       }
87043       goto exit_create_index;
87044     }
87045   }else{
87046     int n;
87047     Index *pLoop;
87048     for(pLoop=pTab->pIndex, n=1; pLoop; pLoop=pLoop->pNext, n++){}
87049     zName = sqlite3MPrintf(db, "sqlite_autoindex_%s_%d", pTab->zName, n);
87050     if( zName==0 ){
87051       goto exit_create_index;
87052     }
87053   }
87054 
87055   /* Check for authorization to create an index.
87056   */
87057 #ifndef SQLITE_OMIT_AUTHORIZATION
87058   {
87059     const char *zDb = pDb->zName;
87060     if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(iDb), 0, zDb) ){
87061       goto exit_create_index;
87062     }
87063     i = SQLITE_CREATE_INDEX;
87064     if( !OMIT_TEMPDB && iDb==1 ) i = SQLITE_CREATE_TEMP_INDEX;
87065     if( sqlite3AuthCheck(pParse, i, zName, pTab->zName, zDb) ){
87066       goto exit_create_index;
87067     }
87068   }
87069 #endif
87070 
87071   /* If pList==0, it means this routine was called to make a primary
87072   ** key out of the last column added to the table under construction.
87073   ** So create a fake list to simulate this.
87074   */
87075   if( pList==0 ){
87076     pList = sqlite3ExprListAppend(pParse, 0, 0);
87077     if( pList==0 ) goto exit_create_index;
87078     pList->a[0].zName = sqlite3DbStrDup(pParse->db,
87079                                         pTab->aCol[pTab->nCol-1].zName);
87080     pList->a[0].sortOrder = (u8)sortOrder;
87081   }
87082 
87083   /* Figure out how many bytes of space are required to store explicitly
87084   ** specified collation sequence names.
87085   */
87086   for(i=0; i<pList->nExpr; i++){
87087     Expr *pExpr = pList->a[i].pExpr;
87088     if( pExpr ){
87089       assert( pExpr->op==TK_COLLATE );
87090       nExtra += (1 + sqlite3Strlen30(pExpr->u.zToken));
87091     }
87092   }
87093 
87094   /*
87095   ** Allocate the index structure.
87096   */
87097   nName = sqlite3Strlen30(zName);
87098   nExtraCol = pPk ? pPk->nKeyCol : 1;
87099   pIndex = sqlite3AllocateIndexObject(db, pList->nExpr + nExtraCol,
87100                                       nName + nExtra + 1, &zExtra);
87101   if( db->mallocFailed ){
87102     goto exit_create_index;
87103   }
87104   assert( EIGHT_BYTE_ALIGNMENT(pIndex->aiRowEst) );
87105   assert( EIGHT_BYTE_ALIGNMENT(pIndex->azColl) );
87106   pIndex->zName = zExtra;
87107   zExtra += nName + 1;
87108   memcpy(pIndex->zName, zName, nName+1);
87109   pIndex->pTable = pTab;
87110   pIndex->onError = (u8)onError;
87111   pIndex->uniqNotNull = onError!=OE_None;
87112   pIndex->autoIndex = (u8)(pName==0);
87113   pIndex->pSchema = db->aDb[iDb].pSchema;
87114   pIndex->nKeyCol = pList->nExpr;
87115   if( pPIWhere ){
87116     sqlite3ResolveSelfReference(pParse, pTab, NC_PartIdx, pPIWhere, 0);
87117     pIndex->pPartIdxWhere = pPIWhere;
87118     pPIWhere = 0;
87119   }
87120   assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
87121 
87122   /* Check to see if we should honor DESC requests on index columns
87123   */
87124   if( pDb->pSchema->file_format>=4 ){
87125     sortOrderMask = -1;   /* Honor DESC */
87126   }else{
87127     sortOrderMask = 0;    /* Ignore DESC */
87128   }
87129 
87130   /* Scan the names of the columns of the table to be indexed and
87131   ** load the column indices into the Index structure.  Report an error
87132   ** if any column is not found.
87133   **
87134   ** TODO:  Add a test to make sure that the same column is not named
87135   ** more than once within the same index.  Only the first instance of
87136   ** the column will ever be used by the optimizer.  Note that using the
87137   ** same column more than once cannot be an error because that would
87138   ** break backwards compatibility - it needs to be a warning.
87139   */
87140   for(i=0, pListItem=pList->a; i<pList->nExpr; i++, pListItem++){
87141     const char *zColName = pListItem->zName;
87142     int requestedSortOrder;
87143     char *zColl;                   /* Collation sequence name */
87144 
87145     for(j=0, pTabCol=pTab->aCol; j<pTab->nCol; j++, pTabCol++){
87146       if( sqlite3StrICmp(zColName, pTabCol->zName)==0 ) break;
87147     }
87148     if( j>=pTab->nCol ){
87149       sqlite3ErrorMsg(pParse, "table %s has no column named %s",
87150         pTab->zName, zColName);
87151       pParse->checkSchema = 1;
87152       goto exit_create_index;
87153     }
87154     assert( pTab->nCol<=0x7fff && j<=0x7fff );
87155     pIndex->aiColumn[i] = (i16)j;
87156     if( pListItem->pExpr ){
87157       int nColl;
87158       assert( pListItem->pExpr->op==TK_COLLATE );
87159       zColl = pListItem->pExpr->u.zToken;
87160       nColl = sqlite3Strlen30(zColl) + 1;
87161       assert( nExtra>=nColl );
87162       memcpy(zExtra, zColl, nColl);
87163       zColl = zExtra;
87164       zExtra += nColl;
87165       nExtra -= nColl;
87166     }else{
87167       zColl = pTab->aCol[j].zColl;
87168       if( !zColl ) zColl = "BINARY";
87169     }
87170     if( !db->init.busy && !sqlite3LocateCollSeq(pParse, zColl) ){
87171       goto exit_create_index;
87172     }
87173     pIndex->azColl[i] = zColl;
87174     requestedSortOrder = pListItem->sortOrder & sortOrderMask;
87175     pIndex->aSortOrder[i] = (u8)requestedSortOrder;
87176     if( pTab->aCol[j].notNull==0 ) pIndex->uniqNotNull = 0;
87177   }
87178   if( pPk ){
87179     for(j=0; j<pPk->nKeyCol; j++){
87180       int x = pPk->aiColumn[j];
87181       if( hasColumn(pIndex->aiColumn, pIndex->nKeyCol, x) ){
87182         pIndex->nColumn--;
87183       }else{
87184         pIndex->aiColumn[i] = x;
87185         pIndex->azColl[i] = pPk->azColl[j];
87186         pIndex->aSortOrder[i] = pPk->aSortOrder[j];
87187         i++;
87188       }
87189     }
87190     assert( i==pIndex->nColumn );
87191   }else{
87192     pIndex->aiColumn[i] = -1;
87193     pIndex->azColl[i] = "BINARY";
87194   }
87195   sqlite3DefaultRowEst(pIndex);
87196   if( pParse->pNewTable==0 ) estimateIndexWidth(pIndex);
87197 
87198   if( pTab==pParse->pNewTable ){
87199     /* This routine has been called to create an automatic index as a
87200     ** result of a PRIMARY KEY or UNIQUE clause on a column definition, or
87201     ** a PRIMARY KEY or UNIQUE clause following the column definitions.
87202     ** i.e. one of:
87203     **
87204     ** CREATE TABLE t(x PRIMARY KEY, y);
87205     ** CREATE TABLE t(x, y, UNIQUE(x, y));
87206     **
87207     ** Either way, check to see if the table already has such an index. If
87208     ** so, don't bother creating this one. This only applies to
87209     ** automatically created indices. Users can do as they wish with
87210     ** explicit indices.
87211     **
87212     ** Two UNIQUE or PRIMARY KEY constraints are considered equivalent
87213     ** (and thus suppressing the second one) even if they have different
87214     ** sort orders.
87215     **
87216     ** If there are different collating sequences or if the columns of
87217     ** the constraint occur in different orders, then the constraints are
87218     ** considered distinct and both result in separate indices.
87219     */
87220     Index *pIdx;
87221     for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
87222       int k;
87223       assert( pIdx->onError!=OE_None );
87224       assert( pIdx->autoIndex );
87225       assert( pIndex->onError!=OE_None );
87226 
87227       if( pIdx->nKeyCol!=pIndex->nKeyCol ) continue;
87228       for(k=0; k<pIdx->nKeyCol; k++){
87229         const char *z1;
87230         const char *z2;
87231         if( pIdx->aiColumn[k]!=pIndex->aiColumn[k] ) break;
87232         z1 = pIdx->azColl[k];
87233         z2 = pIndex->azColl[k];
87234         if( z1!=z2 && sqlite3StrICmp(z1, z2) ) break;
87235       }
87236       if( k==pIdx->nKeyCol ){
87237         if( pIdx->onError!=pIndex->onError ){
87238           /* This constraint creates the same index as a previous
87239           ** constraint specified somewhere in the CREATE TABLE statement.
87240           ** However the ON CONFLICT clauses are different. If both this
87241           ** constraint and the previous equivalent constraint have explicit
87242           ** ON CONFLICT clauses this is an error. Otherwise, use the
87243           ** explicitly specified behavior for the index.
87244           */
87245           if( !(pIdx->onError==OE_Default || pIndex->onError==OE_Default) ){
87246             sqlite3ErrorMsg(pParse,
87247                 "conflicting ON CONFLICT clauses specified", 0);
87248           }
87249           if( pIdx->onError==OE_Default ){
87250             pIdx->onError = pIndex->onError;
87251           }
87252         }
87253         goto exit_create_index;
87254       }
87255     }
87256   }
87257 
87258   /* Link the new Index structure to its table and to the other
87259   ** in-memory database structures.
87260   */
87261   if( db->init.busy ){
87262     Index *p;
87263     assert( sqlite3SchemaMutexHeld(db, 0, pIndex->pSchema) );
87264     p = sqlite3HashInsert(&pIndex->pSchema->idxHash,
87265                           pIndex->zName, sqlite3Strlen30(pIndex->zName),
87266                           pIndex);
87267     if( p ){
87268       assert( p==pIndex );  /* Malloc must have failed */
87269       db->mallocFailed = 1;
87270       goto exit_create_index;
87271     }
87272     db->flags |= SQLITE_InternChanges;
87273     if( pTblName!=0 ){
87274       pIndex->tnum = db->init.newTnum;
87275     }
87276   }
87277 
87278   /* If this is the initial CREATE INDEX statement (or CREATE TABLE if the
87279   ** index is an implied index for a UNIQUE or PRIMARY KEY constraint) then
87280   ** emit code to allocate the index rootpage on disk and make an entry for
87281   ** the index in the sqlite_master table and populate the index with
87282   ** content.  But, do not do this if we are simply reading the sqlite_master
87283   ** table to parse the schema, or if this index is the PRIMARY KEY index
87284   ** of a WITHOUT ROWID table.
87285   **
87286   ** If pTblName==0 it means this index is generated as an implied PRIMARY KEY
87287   ** or UNIQUE index in a CREATE TABLE statement.  Since the table
87288   ** has just been created, it contains no data and the index initialization
87289   ** step can be skipped.
87290   */
87291   else if( pParse->nErr==0 && (HasRowid(pTab) || pTblName!=0) ){
87292     Vdbe *v;
87293     char *zStmt;
87294     int iMem = ++pParse->nMem;
87295 
87296     v = sqlite3GetVdbe(pParse);
87297     if( v==0 ) goto exit_create_index;
87298 
87299 
87300     /* Create the rootpage for the index
87301     */
87302     sqlite3BeginWriteOperation(pParse, 1, iDb);
87303     sqlite3VdbeAddOp2(v, OP_CreateIndex, iDb, iMem);
87304 
87305     /* Gather the complete text of the CREATE INDEX statement into
87306     ** the zStmt variable
87307     */
87308     if( pStart ){
87309       int n = (int)(pParse->sLastToken.z - pName->z) + pParse->sLastToken.n;
87310       if( pName->z[n-1]==';' ) n--;
87311       /* A named index with an explicit CREATE INDEX statement */
87312       zStmt = sqlite3MPrintf(db, "CREATE%s INDEX %.*s",
87313         onError==OE_None ? "" : " UNIQUE", n, pName->z);
87314     }else{
87315       /* An automatic index created by a PRIMARY KEY or UNIQUE constraint */
87316       /* zStmt = sqlite3MPrintf(""); */
87317       zStmt = 0;
87318     }
87319 
87320     /* Add an entry in sqlite_master for this index
87321     */
87322     sqlite3NestedParse(pParse,
87323         "INSERT INTO %Q.%s VALUES('index',%Q,%Q,#%d,%Q);",
87324         db->aDb[iDb].zName, SCHEMA_TABLE(iDb),
87325         pIndex->zName,
87326         pTab->zName,
87327         iMem,
87328         zStmt
87329     );
87330     sqlite3DbFree(db, zStmt);
87331 
87332     /* Fill the index with data and reparse the schema. Code an OP_Expire
87333     ** to invalidate all pre-compiled statements.
87334     */
87335     if( pTblName ){
87336       sqlite3RefillIndex(pParse, pIndex, iMem);
87337       sqlite3ChangeCookie(pParse, iDb);
87338       sqlite3VdbeAddParseSchemaOp(v, iDb,
87339          sqlite3MPrintf(db, "name='%q' AND type='index'", pIndex->zName));
87340       sqlite3VdbeAddOp1(v, OP_Expire, 0);
87341     }
87342   }
87343 
87344   /* When adding an index to the list of indices for a table, make
87345   ** sure all indices labeled OE_Replace come after all those labeled
87346   ** OE_Ignore.  This is necessary for the correct constraint check
87347   ** processing (in sqlite3GenerateConstraintChecks()) as part of
87348   ** UPDATE and INSERT statements.
87349   */
87350   if( db->init.busy || pTblName==0 ){
87351     if( onError!=OE_Replace || pTab->pIndex==0
87352          || pTab->pIndex->onError==OE_Replace){
87353       pIndex->pNext = pTab->pIndex;
87354       pTab->pIndex = pIndex;
87355     }else{
87356       Index *pOther = pTab->pIndex;
87357       while( pOther->pNext && pOther->pNext->onError!=OE_Replace ){
87358         pOther = pOther->pNext;
87359       }
87360       pIndex->pNext = pOther->pNext;
87361       pOther->pNext = pIndex;
87362     }
87363     pRet = pIndex;
87364     pIndex = 0;
87365   }
87366 
87367   /* Clean up before exiting */
87368 exit_create_index:
87369   if( pIndex ) freeIndex(db, pIndex);
87370   sqlite3ExprDelete(db, pPIWhere);
87371   sqlite3ExprListDelete(db, pList);
87372   sqlite3SrcListDelete(db, pTblName);
87373   sqlite3DbFree(db, zName);
87374   return pRet;
87375 }
87376 
87377 /*
87378 ** Fill the Index.aiRowEst[] array with default information - information
87379 ** to be used when we have not run the ANALYZE command.
87380 **
87381 ** aiRowEst[0] is suppose to contain the number of elements in the index.
87382 ** Since we do not know, guess 1 million.  aiRowEst[1] is an estimate of the
87383 ** number of rows in the table that match any particular value of the
87384 ** first column of the index.  aiRowEst[2] is an estimate of the number
87385 ** of rows that match any particular combiniation of the first 2 columns
87386 ** of the index.  And so forth.  It must always be the case that
87387 *
87388 **           aiRowEst[N]<=aiRowEst[N-1]
87389 **           aiRowEst[N]>=1
87390 **
87391 ** Apart from that, we have little to go on besides intuition as to
87392 ** how aiRowEst[] should be initialized.  The numbers generated here
87393 ** are based on typical values found in actual indices.
87394 */
87395 SQLITE_PRIVATE void sqlite3DefaultRowEst(Index *pIdx){
87396   tRowcnt *a = pIdx->aiRowEst;
87397   int i;
87398   tRowcnt n;
87399   assert( a!=0 );
87400   a[0] = pIdx->pTable->nRowEst;
87401   if( a[0]<10 ) a[0] = 10;
87402   n = 10;
87403   for(i=1; i<=pIdx->nKeyCol; i++){
87404     a[i] = n;
87405     if( n>5 ) n--;
87406   }
87407   if( pIdx->onError!=OE_None ){
87408     a[pIdx->nKeyCol] = 1;
87409   }
87410 }
87411 
87412 /*
87413 ** This routine will drop an existing named index.  This routine
87414 ** implements the DROP INDEX statement.
87415 */
87416 SQLITE_PRIVATE void sqlite3DropIndex(Parse *pParse, SrcList *pName, int ifExists){
87417   Index *pIndex;
87418   Vdbe *v;
87419   sqlite3 *db = pParse->db;
87420   int iDb;
87421 
87422   assert( pParse->nErr==0 );   /* Never called with prior errors */
87423   if( db->mallocFailed ){
87424     goto exit_drop_index;
87425   }
87426   assert( pName->nSrc==1 );
87427   if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
87428     goto exit_drop_index;
87429   }
87430   pIndex = sqlite3FindIndex(db, pName->a[0].zName, pName->a[0].zDatabase);
87431   if( pIndex==0 ){
87432     if( !ifExists ){
87433       sqlite3ErrorMsg(pParse, "no such index: %S", pName, 0);
87434     }else{
87435       sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].zDatabase);
87436     }
87437     pParse->checkSchema = 1;
87438     goto exit_drop_index;
87439   }
87440   if( pIndex->autoIndex ){
87441     sqlite3ErrorMsg(pParse, "index associated with UNIQUE "
87442       "or PRIMARY KEY constraint cannot be dropped", 0);
87443     goto exit_drop_index;
87444   }
87445   iDb = sqlite3SchemaToIndex(db, pIndex->pSchema);
87446 #ifndef SQLITE_OMIT_AUTHORIZATION
87447   {
87448     int code = SQLITE_DROP_INDEX;
87449     Table *pTab = pIndex->pTable;
87450     const char *zDb = db->aDb[iDb].zName;
87451     const char *zTab = SCHEMA_TABLE(iDb);
87452     if( sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){
87453       goto exit_drop_index;
87454     }
87455     if( !OMIT_TEMPDB && iDb ) code = SQLITE_DROP_TEMP_INDEX;
87456     if( sqlite3AuthCheck(pParse, code, pIndex->zName, pTab->zName, zDb) ){
87457       goto exit_drop_index;
87458     }
87459   }
87460 #endif
87461 
87462   /* Generate code to remove the index and from the master table */
87463   v = sqlite3GetVdbe(pParse);
87464   if( v ){
87465     sqlite3BeginWriteOperation(pParse, 1, iDb);
87466     sqlite3NestedParse(pParse,
87467        "DELETE FROM %Q.%s WHERE name=%Q AND type='index'",
87468        db->aDb[iDb].zName, SCHEMA_TABLE(iDb), pIndex->zName
87469     );
87470     sqlite3ClearStatTables(pParse, iDb, "idx", pIndex->zName);
87471     sqlite3ChangeCookie(pParse, iDb);
87472     destroyRootPage(pParse, pIndex->tnum, iDb);
87473     sqlite3VdbeAddOp4(v, OP_DropIndex, iDb, 0, 0, pIndex->zName, 0);
87474   }
87475 
87476 exit_drop_index:
87477   sqlite3SrcListDelete(db, pName);
87478 }
87479 
87480 /*
87481 ** pArray is a pointer to an array of objects. Each object in the
87482 ** array is szEntry bytes in size. This routine uses sqlite3DbRealloc()
87483 ** to extend the array so that there is space for a new object at the end.
87484 **
87485 ** When this function is called, *pnEntry contains the current size of
87486 ** the array (in entries - so the allocation is ((*pnEntry) * szEntry) bytes
87487 ** in total).
87488 **
87489 ** If the realloc() is successful (i.e. if no OOM condition occurs), the
87490 ** space allocated for the new object is zeroed, *pnEntry updated to
87491 ** reflect the new size of the array and a pointer to the new allocation
87492 ** returned. *pIdx is set to the index of the new array entry in this case.
87493 **
87494 ** Otherwise, if the realloc() fails, *pIdx is set to -1, *pnEntry remains
87495 ** unchanged and a copy of pArray returned.
87496 */
87497 SQLITE_PRIVATE void *sqlite3ArrayAllocate(
87498   sqlite3 *db,      /* Connection to notify of malloc failures */
87499   void *pArray,     /* Array of objects.  Might be reallocated */
87500   int szEntry,      /* Size of each object in the array */
87501   int *pnEntry,     /* Number of objects currently in use */
87502   int *pIdx         /* Write the index of a new slot here */
87503 ){
87504   char *z;
87505   int n = *pnEntry;
87506   if( (n & (n-1))==0 ){
87507     int sz = (n==0) ? 1 : 2*n;
87508     void *pNew = sqlite3DbRealloc(db, pArray, sz*szEntry);
87509     if( pNew==0 ){
87510       *pIdx = -1;
87511       return pArray;
87512     }
87513     pArray = pNew;
87514   }
87515   z = (char*)pArray;
87516   memset(&z[n * szEntry], 0, szEntry);
87517   *pIdx = n;
87518   ++*pnEntry;
87519   return pArray;
87520 }
87521 
87522 /*
87523 ** Append a new element to the given IdList.  Create a new IdList if
87524 ** need be.
87525 **
87526 ** A new IdList is returned, or NULL if malloc() fails.
87527 */
87528 SQLITE_PRIVATE IdList *sqlite3IdListAppend(sqlite3 *db, IdList *pList, Token *pToken){
87529   int i;
87530   if( pList==0 ){
87531     pList = sqlite3DbMallocZero(db, sizeof(IdList) );
87532     if( pList==0 ) return 0;
87533   }
87534   pList->a = sqlite3ArrayAllocate(
87535       db,
87536       pList->a,
87537       sizeof(pList->a[0]),
87538       &pList->nId,
87539       &i
87540   );
87541   if( i<0 ){
87542     sqlite3IdListDelete(db, pList);
87543     return 0;
87544   }
87545   pList->a[i].zName = sqlite3NameFromToken(db, pToken);
87546   return pList;
87547 }
87548 
87549 /*
87550 ** Delete an IdList.
87551 */
87552 SQLITE_PRIVATE void sqlite3IdListDelete(sqlite3 *db, IdList *pList){
87553   int i;
87554   if( pList==0 ) return;
87555   for(i=0; i<pList->nId; i++){
87556     sqlite3DbFree(db, pList->a[i].zName);
87557   }
87558   sqlite3DbFree(db, pList->a);
87559   sqlite3DbFree(db, pList);
87560 }
87561 
87562 /*
87563 ** Return the index in pList of the identifier named zId.  Return -1
87564 ** if not found.
87565 */
87566 SQLITE_PRIVATE int sqlite3IdListIndex(IdList *pList, const char *zName){
87567   int i;
87568   if( pList==0 ) return -1;
87569   for(i=0; i<pList->nId; i++){
87570     if( sqlite3StrICmp(pList->a[i].zName, zName)==0 ) return i;
87571   }
87572   return -1;
87573 }
87574 
87575 /*
87576 ** Expand the space allocated for the given SrcList object by
87577 ** creating nExtra new slots beginning at iStart.  iStart is zero based.
87578 ** New slots are zeroed.
87579 **
87580 ** For example, suppose a SrcList initially contains two entries: A,B.
87581 ** To append 3 new entries onto the end, do this:
87582 **
87583 **    sqlite3SrcListEnlarge(db, pSrclist, 3, 2);
87584 **
87585 ** After the call above it would contain:  A, B, nil, nil, nil.
87586 ** If the iStart argument had been 1 instead of 2, then the result
87587 ** would have been:  A, nil, nil, nil, B.  To prepend the new slots,
87588 ** the iStart value would be 0.  The result then would
87589 ** be: nil, nil, nil, A, B.
87590 **
87591 ** If a memory allocation fails the SrcList is unchanged.  The
87592 ** db->mallocFailed flag will be set to true.
87593 */
87594 SQLITE_PRIVATE SrcList *sqlite3SrcListEnlarge(
87595   sqlite3 *db,       /* Database connection to notify of OOM errors */
87596   SrcList *pSrc,     /* The SrcList to be enlarged */
87597   int nExtra,        /* Number of new slots to add to pSrc->a[] */
87598   int iStart         /* Index in pSrc->a[] of first new slot */
87599 ){
87600   int i;
87601 
87602   /* Sanity checking on calling parameters */
87603   assert( iStart>=0 );
87604   assert( nExtra>=1 );
87605   assert( pSrc!=0 );
87606   assert( iStart<=pSrc->nSrc );
87607 
87608   /* Allocate additional space if needed */
87609   if( pSrc->nSrc+nExtra>pSrc->nAlloc ){
87610     SrcList *pNew;
87611     int nAlloc = pSrc->nSrc+nExtra;
87612     int nGot;
87613     pNew = sqlite3DbRealloc(db, pSrc,
87614                sizeof(*pSrc) + (nAlloc-1)*sizeof(pSrc->a[0]) );
87615     if( pNew==0 ){
87616       assert( db->mallocFailed );
87617       return pSrc;
87618     }
87619     pSrc = pNew;
87620     nGot = (sqlite3DbMallocSize(db, pNew) - sizeof(*pSrc))/sizeof(pSrc->a[0])+1;
87621     pSrc->nAlloc = (u8)nGot;
87622   }
87623 
87624   /* Move existing slots that come after the newly inserted slots
87625   ** out of the way */
87626   for(i=pSrc->nSrc-1; i>=iStart; i--){
87627     pSrc->a[i+nExtra] = pSrc->a[i];
87628   }
87629   pSrc->nSrc += (i8)nExtra;
87630 
87631   /* Zero the newly allocated slots */
87632   memset(&pSrc->a[iStart], 0, sizeof(pSrc->a[0])*nExtra);
87633   for(i=iStart; i<iStart+nExtra; i++){
87634     pSrc->a[i].iCursor = -1;
87635   }
87636 
87637   /* Return a pointer to the enlarged SrcList */
87638   return pSrc;
87639 }
87640 
87641 
87642 /*
87643 ** Append a new table name to the given SrcList.  Create a new SrcList if
87644 ** need be.  A new entry is created in the SrcList even if pTable is NULL.
87645 **
87646 ** A SrcList is returned, or NULL if there is an OOM error.  The returned
87647 ** SrcList might be the same as the SrcList that was input or it might be
87648 ** a new one.  If an OOM error does occurs, then the prior value of pList
87649 ** that is input to this routine is automatically freed.
87650 **
87651 ** If pDatabase is not null, it means that the table has an optional
87652 ** database name prefix.  Like this:  "database.table".  The pDatabase
87653 ** points to the table name and the pTable points to the database name.
87654 ** The SrcList.a[].zName field is filled with the table name which might
87655 ** come from pTable (if pDatabase is NULL) or from pDatabase.
87656 ** SrcList.a[].zDatabase is filled with the database name from pTable,
87657 ** or with NULL if no database is specified.
87658 **
87659 ** In other words, if call like this:
87660 **
87661 **         sqlite3SrcListAppend(D,A,B,0);
87662 **
87663 ** Then B is a table name and the database name is unspecified.  If called
87664 ** like this:
87665 **
87666 **         sqlite3SrcListAppend(D,A,B,C);
87667 **
87668 ** Then C is the table name and B is the database name.  If C is defined
87669 ** then so is B.  In other words, we never have a case where:
87670 **
87671 **         sqlite3SrcListAppend(D,A,0,C);
87672 **
87673 ** Both pTable and pDatabase are assumed to be quoted.  They are dequoted
87674 ** before being added to the SrcList.
87675 */
87676 SQLITE_PRIVATE SrcList *sqlite3SrcListAppend(
87677   sqlite3 *db,        /* Connection to notify of malloc failures */
87678   SrcList *pList,     /* Append to this SrcList. NULL creates a new SrcList */
87679   Token *pTable,      /* Table to append */
87680   Token *pDatabase    /* Database of the table */
87681 ){
87682   struct SrcList_item *pItem;
87683   assert( pDatabase==0 || pTable!=0 );  /* Cannot have C without B */
87684   if( pList==0 ){
87685     pList = sqlite3DbMallocZero(db, sizeof(SrcList) );
87686     if( pList==0 ) return 0;
87687     pList->nAlloc = 1;
87688   }
87689   pList = sqlite3SrcListEnlarge(db, pList, 1, pList->nSrc);
87690   if( db->mallocFailed ){
87691     sqlite3SrcListDelete(db, pList);
87692     return 0;
87693   }
87694   pItem = &pList->a[pList->nSrc-1];
87695   if( pDatabase && pDatabase->z==0 ){
87696     pDatabase = 0;
87697   }
87698   if( pDatabase ){
87699     Token *pTemp = pDatabase;
87700     pDatabase = pTable;
87701     pTable = pTemp;
87702   }
87703   pItem->zName = sqlite3NameFromToken(db, pTable);
87704   pItem->zDatabase = sqlite3NameFromToken(db, pDatabase);
87705   return pList;
87706 }
87707 
87708 /*
87709 ** Assign VdbeCursor index numbers to all tables in a SrcList
87710 */
87711 SQLITE_PRIVATE void sqlite3SrcListAssignCursors(Parse *pParse, SrcList *pList){
87712   int i;
87713   struct SrcList_item *pItem;
87714   assert(pList || pParse->db->mallocFailed );
87715   if( pList ){
87716     for(i=0, pItem=pList->a; i<pList->nSrc; i++, pItem++){
87717       if( pItem->iCursor>=0 ) break;
87718       pItem->iCursor = pParse->nTab++;
87719       if( pItem->pSelect ){
87720         sqlite3SrcListAssignCursors(pParse, pItem->pSelect->pSrc);
87721       }
87722     }
87723   }
87724 }
87725 
87726 /*
87727 ** Delete an entire SrcList including all its substructure.
87728 */
87729 SQLITE_PRIVATE void sqlite3SrcListDelete(sqlite3 *db, SrcList *pList){
87730   int i;
87731   struct SrcList_item *pItem;
87732   if( pList==0 ) return;
87733   for(pItem=pList->a, i=0; i<pList->nSrc; i++, pItem++){
87734     sqlite3DbFree(db, pItem->zDatabase);
87735     sqlite3DbFree(db, pItem->zName);
87736     sqlite3DbFree(db, pItem->zAlias);
87737     sqlite3DbFree(db, pItem->zIndex);
87738     sqlite3DeleteTable(db, pItem->pTab);
87739     sqlite3SelectDelete(db, pItem->pSelect);
87740     sqlite3ExprDelete(db, pItem->pOn);
87741     sqlite3IdListDelete(db, pItem->pUsing);
87742   }
87743   sqlite3DbFree(db, pList);
87744 }
87745 
87746 /*
87747 ** This routine is called by the parser to add a new term to the
87748 ** end of a growing FROM clause.  The "p" parameter is the part of
87749 ** the FROM clause that has already been constructed.  "p" is NULL
87750 ** if this is the first term of the FROM clause.  pTable and pDatabase
87751 ** are the name of the table and database named in the FROM clause term.
87752 ** pDatabase is NULL if the database name qualifier is missing - the
87753 ** usual case.  If the term has a alias, then pAlias points to the
87754 ** alias token.  If the term is a subquery, then pSubquery is the
87755 ** SELECT statement that the subquery encodes.  The pTable and
87756 ** pDatabase parameters are NULL for subqueries.  The pOn and pUsing
87757 ** parameters are the content of the ON and USING clauses.
87758 **
87759 ** Return a new SrcList which encodes is the FROM with the new
87760 ** term added.
87761 */
87762 SQLITE_PRIVATE SrcList *sqlite3SrcListAppendFromTerm(
87763   Parse *pParse,          /* Parsing context */
87764   SrcList *p,             /* The left part of the FROM clause already seen */
87765   Token *pTable,          /* Name of the table to add to the FROM clause */
87766   Token *pDatabase,       /* Name of the database containing pTable */
87767   Token *pAlias,          /* The right-hand side of the AS subexpression */
87768   Select *pSubquery,      /* A subquery used in place of a table name */
87769   Expr *pOn,              /* The ON clause of a join */
87770   IdList *pUsing          /* The USING clause of a join */
87771 ){
87772   struct SrcList_item *pItem;
87773   sqlite3 *db = pParse->db;
87774   if( !p && (pOn || pUsing) ){
87775     sqlite3ErrorMsg(pParse, "a JOIN clause is required before %s",
87776       (pOn ? "ON" : "USING")
87777     );
87778     goto append_from_error;
87779   }
87780   p = sqlite3SrcListAppend(db, p, pTable, pDatabase);
87781   if( p==0 || NEVER(p->nSrc==0) ){
87782     goto append_from_error;
87783   }
87784   pItem = &p->a[p->nSrc-1];
87785   assert( pAlias!=0 );
87786   if( pAlias->n ){
87787     pItem->zAlias = sqlite3NameFromToken(db, pAlias);
87788   }
87789   pItem->pSelect = pSubquery;
87790   pItem->pOn = pOn;
87791   pItem->pUsing = pUsing;
87792   return p;
87793 
87794  append_from_error:
87795   assert( p==0 );
87796   sqlite3ExprDelete(db, pOn);
87797   sqlite3IdListDelete(db, pUsing);
87798   sqlite3SelectDelete(db, pSubquery);
87799   return 0;
87800 }
87801 
87802 /*
87803 ** Add an INDEXED BY or NOT INDEXED clause to the most recently added
87804 ** element of the source-list passed as the second argument.
87805 */
87806 SQLITE_PRIVATE void sqlite3SrcListIndexedBy(Parse *pParse, SrcList *p, Token *pIndexedBy){
87807   assert( pIndexedBy!=0 );
87808   if( p && ALWAYS(p->nSrc>0) ){
87809     struct SrcList_item *pItem = &p->a[p->nSrc-1];
87810     assert( pItem->notIndexed==0 && pItem->zIndex==0 );
87811     if( pIndexedBy->n==1 && !pIndexedBy->z ){
87812       /* A "NOT INDEXED" clause was supplied. See parse.y
87813       ** construct "indexed_opt" for details. */
87814       pItem->notIndexed = 1;
87815     }else{
87816       pItem->zIndex = sqlite3NameFromToken(pParse->db, pIndexedBy);
87817     }
87818   }
87819 }
87820 
87821 /*
87822 ** When building up a FROM clause in the parser, the join operator
87823 ** is initially attached to the left operand.  But the code generator
87824 ** expects the join operator to be on the right operand.  This routine
87825 ** Shifts all join operators from left to right for an entire FROM
87826 ** clause.
87827 **
87828 ** Example: Suppose the join is like this:
87829 **
87830 **           A natural cross join B
87831 **
87832 ** The operator is "natural cross join".  The A and B operands are stored
87833 ** in p->a[0] and p->a[1], respectively.  The parser initially stores the
87834 ** operator with A.  This routine shifts that operator over to B.
87835 */
87836 SQLITE_PRIVATE void sqlite3SrcListShiftJoinType(SrcList *p){
87837   if( p ){
87838     int i;
87839     assert( p->a || p->nSrc==0 );
87840     for(i=p->nSrc-1; i>0; i--){
87841       p->a[i].jointype = p->a[i-1].jointype;
87842     }
87843     p->a[0].jointype = 0;
87844   }
87845 }
87846 
87847 /*
87848 ** Begin a transaction
87849 */
87850 SQLITE_PRIVATE void sqlite3BeginTransaction(Parse *pParse, int type){
87851   sqlite3 *db;
87852   Vdbe *v;
87853   int i;
87854 
87855   assert( pParse!=0 );
87856   db = pParse->db;
87857   assert( db!=0 );
87858 /*  if( db->aDb[0].pBt==0 ) return; */
87859   if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "BEGIN", 0, 0) ){
87860     return;
87861   }
87862   v = sqlite3GetVdbe(pParse);
87863   if( !v ) return;
87864   if( type!=TK_DEFERRED ){
87865     for(i=0; i<db->nDb; i++){
87866       sqlite3VdbeAddOp2(v, OP_Transaction, i, (type==TK_EXCLUSIVE)+1);
87867       sqlite3VdbeUsesBtree(v, i);
87868     }
87869   }
87870   sqlite3VdbeAddOp2(v, OP_AutoCommit, 0, 0);
87871 }
87872 
87873 /*
87874 ** Commit a transaction
87875 */
87876 SQLITE_PRIVATE void sqlite3CommitTransaction(Parse *pParse){
87877   Vdbe *v;
87878 
87879   assert( pParse!=0 );
87880   assert( pParse->db!=0 );
87881   if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "COMMIT", 0, 0) ){
87882     return;
87883   }
87884   v = sqlite3GetVdbe(pParse);
87885   if( v ){
87886     sqlite3VdbeAddOp2(v, OP_AutoCommit, 1, 0);
87887   }
87888 }
87889 
87890 /*
87891 ** Rollback a transaction
87892 */
87893 SQLITE_PRIVATE void sqlite3RollbackTransaction(Parse *pParse){
87894   Vdbe *v;
87895 
87896   assert( pParse!=0 );
87897   assert( pParse->db!=0 );
87898   if( sqlite3AuthCheck(pParse, SQLITE_TRANSACTION, "ROLLBACK", 0, 0) ){
87899     return;
87900   }
87901   v = sqlite3GetVdbe(pParse);
87902   if( v ){
87903     sqlite3VdbeAddOp2(v, OP_AutoCommit, 1, 1);
87904   }
87905 }
87906 
87907 /*
87908 ** This function is called by the parser when it parses a command to create,
87909 ** release or rollback an SQL savepoint.
87910 */
87911 SQLITE_PRIVATE void sqlite3Savepoint(Parse *pParse, int op, Token *pName){
87912   char *zName = sqlite3NameFromToken(pParse->db, pName);
87913   if( zName ){
87914     Vdbe *v = sqlite3GetVdbe(pParse);
87915 #ifndef SQLITE_OMIT_AUTHORIZATION
87916     static const char * const az[] = { "BEGIN", "RELEASE", "ROLLBACK" };
87917     assert( !SAVEPOINT_BEGIN && SAVEPOINT_RELEASE==1 && SAVEPOINT_ROLLBACK==2 );
87918 #endif
87919     if( !v || sqlite3AuthCheck(pParse, SQLITE_SAVEPOINT, az[op], zName, 0) ){
87920       sqlite3DbFree(pParse->db, zName);
87921       return;
87922     }
87923     sqlite3VdbeAddOp4(v, OP_Savepoint, op, 0, 0, zName, P4_DYNAMIC);
87924   }
87925 }
87926 
87927 /*
87928 ** Make sure the TEMP database is open and available for use.  Return
87929 ** the number of errors.  Leave any error messages in the pParse structure.
87930 */
87931 SQLITE_PRIVATE int sqlite3OpenTempDatabase(Parse *pParse){
87932   sqlite3 *db = pParse->db;
87933   if( db->aDb[1].pBt==0 && !pParse->explain ){
87934     int rc;
87935     Btree *pBt;
87936     static const int flags =
87937           SQLITE_OPEN_READWRITE |
87938           SQLITE_OPEN_CREATE |
87939           SQLITE_OPEN_EXCLUSIVE |
87940           SQLITE_OPEN_DELETEONCLOSE |
87941           SQLITE_OPEN_TEMP_DB;
87942 
87943     rc = sqlite3BtreeOpen(db->pVfs, 0, db, &pBt, 0, flags);
87944     if( rc!=SQLITE_OK ){
87945       sqlite3ErrorMsg(pParse, "unable to open a temporary database "
87946         "file for storing temporary tables");
87947       pParse->rc = rc;
87948       return 1;
87949     }
87950     db->aDb[1].pBt = pBt;
87951     assert( db->aDb[1].pSchema );
87952     if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize, -1, 0) ){
87953       db->mallocFailed = 1;
87954       return 1;
87955     }
87956   }
87957   return 0;
87958 }
87959 
87960 /*
87961 ** Generate VDBE code that will verify the schema cookie and start
87962 ** a read-transaction for all named database files.
87963 **
87964 ** It is important that all schema cookies be verified and all
87965 ** read transactions be started before anything else happens in
87966 ** the VDBE program.  But this routine can be called after much other
87967 ** code has been generated.  So here is what we do:
87968 **
87969 ** The first time this routine is called, we code an OP_Goto that
87970 ** will jump to a subroutine at the end of the program.  Then we
87971 ** record every database that needs its schema verified in the
87972 ** pParse->cookieMask field.  Later, after all other code has been
87973 ** generated, the subroutine that does the cookie verifications and
87974 ** starts the transactions will be coded and the OP_Goto P2 value
87975 ** will be made to point to that subroutine.  The generation of the
87976 ** cookie verification subroutine code happens in sqlite3FinishCoding().
87977 **
87978 ** If iDb<0 then code the OP_Goto only - don't set flag to verify the
87979 ** schema on any databases.  This can be used to position the OP_Goto
87980 ** early in the code, before we know if any database tables will be used.
87981 */
87982 SQLITE_PRIVATE void sqlite3CodeVerifySchema(Parse *pParse, int iDb){
87983   Parse *pToplevel = sqlite3ParseToplevel(pParse);
87984 
87985 #ifndef SQLITE_OMIT_TRIGGER
87986   if( pToplevel!=pParse ){
87987     /* This branch is taken if a trigger is currently being coded. In this
87988     ** case, set cookieGoto to a non-zero value to show that this function
87989     ** has been called. This is used by the sqlite3ExprCodeConstants()
87990     ** function. */
87991     pParse->cookieGoto = -1;
87992   }
87993 #endif
87994   if( pToplevel->cookieGoto==0 ){
87995     Vdbe *v = sqlite3GetVdbe(pToplevel);
87996     if( v==0 ) return;  /* This only happens if there was a prior error */
87997     pToplevel->cookieGoto = sqlite3VdbeAddOp2(v, OP_Goto, 0, 0)+1;
87998   }
87999   if( iDb>=0 ){
88000     sqlite3 *db = pToplevel->db;
88001     yDbMask mask;
88002 
88003     assert( iDb<db->nDb );
88004     assert( db->aDb[iDb].pBt!=0 || iDb==1 );
88005     assert( iDb<SQLITE_MAX_ATTACHED+2 );
88006     assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
88007     mask = ((yDbMask)1)<<iDb;
88008     if( (pToplevel->cookieMask & mask)==0 ){
88009       pToplevel->cookieMask |= mask;
88010       pToplevel->cookieValue[iDb] = db->aDb[iDb].pSchema->schema_cookie;
88011       if( !OMIT_TEMPDB && iDb==1 ){
88012         sqlite3OpenTempDatabase(pToplevel);
88013       }
88014     }
88015   }
88016 }
88017 
88018 /*
88019 ** If argument zDb is NULL, then call sqlite3CodeVerifySchema() for each
88020 ** attached database. Otherwise, invoke it for the database named zDb only.
88021 */
88022 SQLITE_PRIVATE void sqlite3CodeVerifyNamedSchema(Parse *pParse, const char *zDb){
88023   sqlite3 *db = pParse->db;
88024   int i;
88025   for(i=0; i<db->nDb; i++){
88026     Db *pDb = &db->aDb[i];
88027     if( pDb->pBt && (!zDb || 0==sqlite3StrICmp(zDb, pDb->zName)) ){
88028       sqlite3CodeVerifySchema(pParse, i);
88029     }
88030   }
88031 }
88032 
88033 /*
88034 ** Generate VDBE code that prepares for doing an operation that
88035 ** might change the database.
88036 **
88037 ** This routine starts a new transaction if we are not already within
88038 ** a transaction.  If we are already within a transaction, then a checkpoint
88039 ** is set if the setStatement parameter is true.  A checkpoint should
88040 ** be set for operations that might fail (due to a constraint) part of
88041 ** the way through and which will need to undo some writes without having to
88042 ** rollback the whole transaction.  For operations where all constraints
88043 ** can be checked before any changes are made to the database, it is never
88044 ** necessary to undo a write and the checkpoint should not be set.
88045 */
88046 SQLITE_PRIVATE void sqlite3BeginWriteOperation(Parse *pParse, int setStatement, int iDb){
88047   Parse *pToplevel = sqlite3ParseToplevel(pParse);
88048   sqlite3CodeVerifySchema(pParse, iDb);
88049   pToplevel->writeMask |= ((yDbMask)1)<<iDb;
88050   pToplevel->isMultiWrite |= setStatement;
88051 }
88052 
88053 /*
88054 ** Indicate that the statement currently under construction might write
88055 ** more than one entry (example: deleting one row then inserting another,
88056 ** inserting multiple rows in a table, or inserting a row and index entries.)
88057 ** If an abort occurs after some of these writes have completed, then it will
88058 ** be necessary to undo the completed writes.
88059 */
88060 SQLITE_PRIVATE void sqlite3MultiWrite(Parse *pParse){
88061   Parse *pToplevel = sqlite3ParseToplevel(pParse);
88062   pToplevel->isMultiWrite = 1;
88063 }
88064 
88065 /*
88066 ** The code generator calls this routine if is discovers that it is
88067 ** possible to abort a statement prior to completion.  In order to
88068 ** perform this abort without corrupting the database, we need to make
88069 ** sure that the statement is protected by a statement transaction.
88070 **
88071 ** Technically, we only need to set the mayAbort flag if the
88072 ** isMultiWrite flag was previously set.  There is a time dependency
88073 ** such that the abort must occur after the multiwrite.  This makes
88074 ** some statements involving the REPLACE conflict resolution algorithm
88075 ** go a little faster.  But taking advantage of this time dependency
88076 ** makes it more difficult to prove that the code is correct (in
88077 ** particular, it prevents us from writing an effective
88078 ** implementation of sqlite3AssertMayAbort()) and so we have chosen
88079 ** to take the safe route and skip the optimization.
88080 */
88081 SQLITE_PRIVATE void sqlite3MayAbort(Parse *pParse){
88082   Parse *pToplevel = sqlite3ParseToplevel(pParse);
88083   pToplevel->mayAbort = 1;
88084 }
88085 
88086 /*
88087 ** Code an OP_Halt that causes the vdbe to return an SQLITE_CONSTRAINT
88088 ** error. The onError parameter determines which (if any) of the statement
88089 ** and/or current transaction is rolled back.
88090 */
88091 SQLITE_PRIVATE void sqlite3HaltConstraint(
88092   Parse *pParse,    /* Parsing context */
88093   int errCode,      /* extended error code */
88094   int onError,      /* Constraint type */
88095   char *p4,         /* Error message */
88096   i8 p4type,        /* P4_STATIC or P4_TRANSIENT */
88097   u8 p5Errmsg       /* P5_ErrMsg type */
88098 ){
88099   Vdbe *v = sqlite3GetVdbe(pParse);
88100   assert( (errCode&0xff)==SQLITE_CONSTRAINT );
88101   if( onError==OE_Abort ){
88102     sqlite3MayAbort(pParse);
88103   }
88104   sqlite3VdbeAddOp4(v, OP_Halt, errCode, onError, 0, p4, p4type);
88105   if( p5Errmsg ) sqlite3VdbeChangeP5(v, p5Errmsg);
88106 }
88107 
88108 /*
88109 ** Code an OP_Halt due to UNIQUE or PRIMARY KEY constraint violation.
88110 */
88111 SQLITE_PRIVATE void sqlite3UniqueConstraint(
88112   Parse *pParse,    /* Parsing context */
88113   int onError,      /* Constraint type */
88114   Index *pIdx       /* The index that triggers the constraint */
88115 ){
88116   char *zErr;
88117   int j;
88118   StrAccum errMsg;
88119   Table *pTab = pIdx->pTable;
88120 
88121   sqlite3StrAccumInit(&errMsg, 0, 0, 200);
88122   errMsg.db = pParse->db;
88123   for(j=0; j<pIdx->nKeyCol; j++){
88124     char *zCol = pTab->aCol[pIdx->aiColumn[j]].zName;
88125     if( j ) sqlite3StrAccumAppend(&errMsg, ", ", 2);
88126     sqlite3StrAccumAppendAll(&errMsg, pTab->zName);
88127     sqlite3StrAccumAppend(&errMsg, ".", 1);
88128     sqlite3StrAccumAppendAll(&errMsg, zCol);
88129   }
88130   zErr = sqlite3StrAccumFinish(&errMsg);
88131   sqlite3HaltConstraint(pParse,
88132     (pIdx->autoIndex==2)?SQLITE_CONSTRAINT_PRIMARYKEY:SQLITE_CONSTRAINT_UNIQUE,
88133     onError, zErr, P4_DYNAMIC, P5_ConstraintUnique);
88134 }
88135 
88136 
88137 /*
88138 ** Code an OP_Halt due to non-unique rowid.
88139 */
88140 SQLITE_PRIVATE void sqlite3RowidConstraint(
88141   Parse *pParse,    /* Parsing context */
88142   int onError,      /* Conflict resolution algorithm */
88143   Table *pTab       /* The table with the non-unique rowid */
88144 ){
88145   char *zMsg;
88146   int rc;
88147   if( pTab->iPKey>=0 ){
88148     zMsg = sqlite3MPrintf(pParse->db, "%s.%s", pTab->zName,
88149                           pTab->aCol[pTab->iPKey].zName);
88150     rc = SQLITE_CONSTRAINT_PRIMARYKEY;
88151   }else{
88152     zMsg = sqlite3MPrintf(pParse->db, "%s.rowid", pTab->zName);
88153     rc = SQLITE_CONSTRAINT_ROWID;
88154   }
88155   sqlite3HaltConstraint(pParse, rc, onError, zMsg, P4_DYNAMIC,
88156                         P5_ConstraintUnique);
88157 }
88158 
88159 /*
88160 ** Check to see if pIndex uses the collating sequence pColl.  Return
88161 ** true if it does and false if it does not.
88162 */
88163 #ifndef SQLITE_OMIT_REINDEX
88164 static int collationMatch(const char *zColl, Index *pIndex){
88165   int i;
88166   assert( zColl!=0 );
88167   for(i=0; i<pIndex->nColumn; i++){
88168     const char *z = pIndex->azColl[i];
88169     assert( z!=0 || pIndex->aiColumn[i]<0 );
88170     if( pIndex->aiColumn[i]>=0 && 0==sqlite3StrICmp(z, zColl) ){
88171       return 1;
88172     }
88173   }
88174   return 0;
88175 }
88176 #endif
88177 
88178 /*
88179 ** Recompute all indices of pTab that use the collating sequence pColl.
88180 ** If pColl==0 then recompute all indices of pTab.
88181 */
88182 #ifndef SQLITE_OMIT_REINDEX
88183 static void reindexTable(Parse *pParse, Table *pTab, char const *zColl){
88184   Index *pIndex;              /* An index associated with pTab */
88185 
88186   for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){
88187     if( zColl==0 || collationMatch(zColl, pIndex) ){
88188       int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
88189       sqlite3BeginWriteOperation(pParse, 0, iDb);
88190       sqlite3RefillIndex(pParse, pIndex, -1);
88191     }
88192   }
88193 }
88194 #endif
88195 
88196 /*
88197 ** Recompute all indices of all tables in all databases where the
88198 ** indices use the collating sequence pColl.  If pColl==0 then recompute
88199 ** all indices everywhere.
88200 */
88201 #ifndef SQLITE_OMIT_REINDEX
88202 static void reindexDatabases(Parse *pParse, char const *zColl){
88203   Db *pDb;                    /* A single database */
88204   int iDb;                    /* The database index number */
88205   sqlite3 *db = pParse->db;   /* The database connection */
88206   HashElem *k;                /* For looping over tables in pDb */
88207   Table *pTab;                /* A table in the database */
88208 
88209   assert( sqlite3BtreeHoldsAllMutexes(db) );  /* Needed for schema access */
88210   for(iDb=0, pDb=db->aDb; iDb<db->nDb; iDb++, pDb++){
88211     assert( pDb!=0 );
88212     for(k=sqliteHashFirst(&pDb->pSchema->tblHash);  k; k=sqliteHashNext(k)){
88213       pTab = (Table*)sqliteHashData(k);
88214       reindexTable(pParse, pTab, zColl);
88215     }
88216   }
88217 }
88218 #endif
88219 
88220 /*
88221 ** Generate code for the REINDEX command.
88222 **
88223 **        REINDEX                            -- 1
88224 **        REINDEX  <collation>               -- 2
88225 **        REINDEX  ?<database>.?<tablename>  -- 3
88226 **        REINDEX  ?<database>.?<indexname>  -- 4
88227 **
88228 ** Form 1 causes all indices in all attached databases to be rebuilt.
88229 ** Form 2 rebuilds all indices in all databases that use the named
88230 ** collating function.  Forms 3 and 4 rebuild the named index or all
88231 ** indices associated with the named table.
88232 */
88233 #ifndef SQLITE_OMIT_REINDEX
88234 SQLITE_PRIVATE void sqlite3Reindex(Parse *pParse, Token *pName1, Token *pName2){
88235   CollSeq *pColl;             /* Collating sequence to be reindexed, or NULL */
88236   char *z;                    /* Name of a table or index */
88237   const char *zDb;            /* Name of the database */
88238   Table *pTab;                /* A table in the database */
88239   Index *pIndex;              /* An index associated with pTab */
88240   int iDb;                    /* The database index number */
88241   sqlite3 *db = pParse->db;   /* The database connection */
88242   Token *pObjName;            /* Name of the table or index to be reindexed */
88243 
88244   /* Read the database schema. If an error occurs, leave an error message
88245   ** and code in pParse and return NULL. */
88246   if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
88247     return;
88248   }
88249 
88250   if( pName1==0 ){
88251     reindexDatabases(pParse, 0);
88252     return;
88253   }else if( NEVER(pName2==0) || pName2->z==0 ){
88254     char *zColl;
88255     assert( pName1->z );
88256     zColl = sqlite3NameFromToken(pParse->db, pName1);
88257     if( !zColl ) return;
88258     pColl = sqlite3FindCollSeq(db, ENC(db), zColl, 0);
88259     if( pColl ){
88260       reindexDatabases(pParse, zColl);
88261       sqlite3DbFree(db, zColl);
88262       return;
88263     }
88264     sqlite3DbFree(db, zColl);
88265   }
88266   iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pObjName);
88267   if( iDb<0 ) return;
88268   z = sqlite3NameFromToken(db, pObjName);
88269   if( z==0 ) return;
88270   zDb = db->aDb[iDb].zName;
88271   pTab = sqlite3FindTable(db, z, zDb);
88272   if( pTab ){
88273     reindexTable(pParse, pTab, 0);
88274     sqlite3DbFree(db, z);
88275     return;
88276   }
88277   pIndex = sqlite3FindIndex(db, z, zDb);
88278   sqlite3DbFree(db, z);
88279   if( pIndex ){
88280     sqlite3BeginWriteOperation(pParse, 0, iDb);
88281     sqlite3RefillIndex(pParse, pIndex, -1);
88282     return;
88283   }
88284   sqlite3ErrorMsg(pParse, "unable to identify the object to be reindexed");
88285 }
88286 #endif
88287 
88288 /*
88289 ** Return a KeyInfo structure that is appropriate for the given Index.
88290 **
88291 ** The KeyInfo structure for an index is cached in the Index object.
88292 ** So there might be multiple references to the returned pointer.  The
88293 ** caller should not try to modify the KeyInfo object.
88294 **
88295 ** The caller should invoke sqlite3KeyInfoUnref() on the returned object
88296 ** when it has finished using it.
88297 */
88298 SQLITE_PRIVATE KeyInfo *sqlite3KeyInfoOfIndex(Parse *pParse, Index *pIdx){
88299   if( pParse->nErr ) return 0;
88300 #ifndef SQLITE_OMIT_SHARED_CACHE
88301   if( pIdx->pKeyInfo && pIdx->pKeyInfo->db!=pParse->db ){
88302     sqlite3KeyInfoUnref(pIdx->pKeyInfo);
88303     pIdx->pKeyInfo = 0;
88304   }
88305 #endif
88306   if( pIdx->pKeyInfo==0 ){
88307     int i;
88308     int nCol = pIdx->nColumn;
88309     int nKey = pIdx->nKeyCol;
88310     KeyInfo *pKey;
88311     if( pIdx->uniqNotNull ){
88312       pKey = sqlite3KeyInfoAlloc(pParse->db, nKey, nCol-nKey);
88313     }else{
88314       pKey = sqlite3KeyInfoAlloc(pParse->db, nCol, 0);
88315     }
88316     if( pKey ){
88317       assert( sqlite3KeyInfoIsWriteable(pKey) );
88318       for(i=0; i<nCol; i++){
88319         char *zColl = pIdx->azColl[i];
88320         assert( zColl!=0 );
88321         pKey->aColl[i] = strcmp(zColl,"BINARY")==0 ? 0 :
88322                           sqlite3LocateCollSeq(pParse, zColl);
88323         pKey->aSortOrder[i] = pIdx->aSortOrder[i];
88324       }
88325       if( pParse->nErr ){
88326         sqlite3KeyInfoUnref(pKey);
88327       }else{
88328         pIdx->pKeyInfo = pKey;
88329       }
88330     }
88331   }
88332   return sqlite3KeyInfoRef(pIdx->pKeyInfo);
88333 }
88334 
88335 #ifndef SQLITE_OMIT_CTE
88336 /*
88337 ** This routine is invoked once per CTE by the parser while parsing a
88338 ** WITH clause.
88339 */
88340 SQLITE_PRIVATE With *sqlite3WithAdd(
88341   Parse *pParse,          /* Parsing context */
88342   With *pWith,            /* Existing WITH clause, or NULL */
88343   Token *pName,           /* Name of the common-table */
88344   ExprList *pArglist,     /* Optional column name list for the table */
88345   Select *pQuery          /* Query used to initialize the table */
88346 ){
88347   sqlite3 *db = pParse->db;
88348   With *pNew;
88349   char *zName;
88350 
88351   /* Check that the CTE name is unique within this WITH clause. If
88352   ** not, store an error in the Parse structure. */
88353   zName = sqlite3NameFromToken(pParse->db, pName);
88354   if( zName && pWith ){
88355     int i;
88356     for(i=0; i<pWith->nCte; i++){
88357       if( sqlite3StrICmp(zName, pWith->a[i].zName)==0 ){
88358         sqlite3ErrorMsg(pParse, "duplicate WITH table name: %s", zName);
88359       }
88360     }
88361   }
88362 
88363   if( pWith ){
88364     int nByte = sizeof(*pWith) + (sizeof(pWith->a[1]) * pWith->nCte);
88365     pNew = sqlite3DbRealloc(db, pWith, nByte);
88366   }else{
88367     pNew = sqlite3DbMallocZero(db, sizeof(*pWith));
88368   }
88369   assert( zName!=0 || pNew==0 );
88370   assert( db->mallocFailed==0 || pNew==0 );
88371 
88372   if( pNew==0 ){
88373     sqlite3ExprListDelete(db, pArglist);
88374     sqlite3SelectDelete(db, pQuery);
88375     sqlite3DbFree(db, zName);
88376     pNew = pWith;
88377   }else{
88378     pNew->a[pNew->nCte].pSelect = pQuery;
88379     pNew->a[pNew->nCte].pCols = pArglist;
88380     pNew->a[pNew->nCte].zName = zName;
88381     pNew->a[pNew->nCte].zErr = 0;
88382     pNew->nCte++;
88383   }
88384 
88385   return pNew;
88386 }
88387 
88388 /*
88389 ** Free the contents of the With object passed as the second argument.
88390 */
88391 SQLITE_PRIVATE void sqlite3WithDelete(sqlite3 *db, With *pWith){
88392   if( pWith ){
88393     int i;
88394     for(i=0; i<pWith->nCte; i++){
88395       struct Cte *pCte = &pWith->a[i];
88396       sqlite3ExprListDelete(db, pCte->pCols);
88397       sqlite3SelectDelete(db, pCte->pSelect);
88398       sqlite3DbFree(db, pCte->zName);
88399     }
88400     sqlite3DbFree(db, pWith);
88401   }
88402 }
88403 #endif /* !defined(SQLITE_OMIT_CTE) */
88404 
88405 /************** End of build.c ***********************************************/
88406 /************** Begin file callback.c ****************************************/
88407 /*
88408 ** 2005 May 23
88409 **
88410 ** The author disclaims copyright to this source code.  In place of
88411 ** a legal notice, here is a blessing:
88412 **
88413 **    May you do good and not evil.
88414 **    May you find forgiveness for yourself and forgive others.
88415 **    May you share freely, never taking more than you give.
88416 **
88417 *************************************************************************
88418 **
88419 ** This file contains functions used to access the internal hash tables
88420 ** of user defined functions and collation sequences.
88421 */
88422 
88423 
88424 /*
88425 ** Invoke the 'collation needed' callback to request a collation sequence
88426 ** in the encoding enc of name zName, length nName.
88427 */
88428 static void callCollNeeded(sqlite3 *db, int enc, const char *zName){
88429   assert( !db->xCollNeeded || !db->xCollNeeded16 );
88430   if( db->xCollNeeded ){
88431     char *zExternal = sqlite3DbStrDup(db, zName);
88432     if( !zExternal ) return;
88433     db->xCollNeeded(db->pCollNeededArg, db, enc, zExternal);
88434     sqlite3DbFree(db, zExternal);
88435   }
88436 #ifndef SQLITE_OMIT_UTF16
88437   if( db->xCollNeeded16 ){
88438     char const *zExternal;
88439     sqlite3_value *pTmp = sqlite3ValueNew(db);
88440     sqlite3ValueSetStr(pTmp, -1, zName, SQLITE_UTF8, SQLITE_STATIC);
88441     zExternal = sqlite3ValueText(pTmp, SQLITE_UTF16NATIVE);
88442     if( zExternal ){
88443       db->xCollNeeded16(db->pCollNeededArg, db, (int)ENC(db), zExternal);
88444     }
88445     sqlite3ValueFree(pTmp);
88446   }
88447 #endif
88448 }
88449 
88450 /*
88451 ** This routine is called if the collation factory fails to deliver a
88452 ** collation function in the best encoding but there may be other versions
88453 ** of this collation function (for other text encodings) available. Use one
88454 ** of these instead if they exist. Avoid a UTF-8 <-> UTF-16 conversion if
88455 ** possible.
88456 */
88457 static int synthCollSeq(sqlite3 *db, CollSeq *pColl){
88458   CollSeq *pColl2;
88459   char *z = pColl->zName;
88460   int i;
88461   static const u8 aEnc[] = { SQLITE_UTF16BE, SQLITE_UTF16LE, SQLITE_UTF8 };
88462   for(i=0; i<3; i++){
88463     pColl2 = sqlite3FindCollSeq(db, aEnc[i], z, 0);
88464     if( pColl2->xCmp!=0 ){
88465       memcpy(pColl, pColl2, sizeof(CollSeq));
88466       pColl->xDel = 0;         /* Do not copy the destructor */
88467       return SQLITE_OK;
88468     }
88469   }
88470   return SQLITE_ERROR;
88471 }
88472 
88473 /*
88474 ** This function is responsible for invoking the collation factory callback
88475 ** or substituting a collation sequence of a different encoding when the
88476 ** requested collation sequence is not available in the desired encoding.
88477 **
88478 ** If it is not NULL, then pColl must point to the database native encoding
88479 ** collation sequence with name zName, length nName.
88480 **
88481 ** The return value is either the collation sequence to be used in database
88482 ** db for collation type name zName, length nName, or NULL, if no collation
88483 ** sequence can be found.  If no collation is found, leave an error message.
88484 **
88485 ** See also: sqlite3LocateCollSeq(), sqlite3FindCollSeq()
88486 */
88487 SQLITE_PRIVATE CollSeq *sqlite3GetCollSeq(
88488   Parse *pParse,        /* Parsing context */
88489   u8 enc,               /* The desired encoding for the collating sequence */
88490   CollSeq *pColl,       /* Collating sequence with native encoding, or NULL */
88491   const char *zName     /* Collating sequence name */
88492 ){
88493   CollSeq *p;
88494   sqlite3 *db = pParse->db;
88495 
88496   p = pColl;
88497   if( !p ){
88498     p = sqlite3FindCollSeq(db, enc, zName, 0);
88499   }
88500   if( !p || !p->xCmp ){
88501     /* No collation sequence of this type for this encoding is registered.
88502     ** Call the collation factory to see if it can supply us with one.
88503     */
88504     callCollNeeded(db, enc, zName);
88505     p = sqlite3FindCollSeq(db, enc, zName, 0);
88506   }
88507   if( p && !p->xCmp && synthCollSeq(db, p) ){
88508     p = 0;
88509   }
88510   assert( !p || p->xCmp );
88511   if( p==0 ){
88512     sqlite3ErrorMsg(pParse, "no such collation sequence: %s", zName);
88513   }
88514   return p;
88515 }
88516 
88517 /*
88518 ** This routine is called on a collation sequence before it is used to
88519 ** check that it is defined. An undefined collation sequence exists when
88520 ** a database is loaded that contains references to collation sequences
88521 ** that have not been defined by sqlite3_create_collation() etc.
88522 **
88523 ** If required, this routine calls the 'collation needed' callback to
88524 ** request a definition of the collating sequence. If this doesn't work,
88525 ** an equivalent collating sequence that uses a text encoding different
88526 ** from the main database is substituted, if one is available.
88527 */
88528 SQLITE_PRIVATE int sqlite3CheckCollSeq(Parse *pParse, CollSeq *pColl){
88529   if( pColl ){
88530     const char *zName = pColl->zName;
88531     sqlite3 *db = pParse->db;
88532     CollSeq *p = sqlite3GetCollSeq(pParse, ENC(db), pColl, zName);
88533     if( !p ){
88534       return SQLITE_ERROR;
88535     }
88536     assert( p==pColl );
88537   }
88538   return SQLITE_OK;
88539 }
88540 
88541 
88542 
88543 /*
88544 ** Locate and return an entry from the db.aCollSeq hash table. If the entry
88545 ** specified by zName and nName is not found and parameter 'create' is
88546 ** true, then create a new entry. Otherwise return NULL.
88547 **
88548 ** Each pointer stored in the sqlite3.aCollSeq hash table contains an
88549 ** array of three CollSeq structures. The first is the collation sequence
88550 ** prefferred for UTF-8, the second UTF-16le, and the third UTF-16be.
88551 **
88552 ** Stored immediately after the three collation sequences is a copy of
88553 ** the collation sequence name. A pointer to this string is stored in
88554 ** each collation sequence structure.
88555 */
88556 static CollSeq *findCollSeqEntry(
88557   sqlite3 *db,          /* Database connection */
88558   const char *zName,    /* Name of the collating sequence */
88559   int create            /* Create a new entry if true */
88560 ){
88561   CollSeq *pColl;
88562   int nName = sqlite3Strlen30(zName);
88563   pColl = sqlite3HashFind(&db->aCollSeq, zName, nName);
88564 
88565   if( 0==pColl && create ){
88566     pColl = sqlite3DbMallocZero(db, 3*sizeof(*pColl) + nName + 1 );
88567     if( pColl ){
88568       CollSeq *pDel = 0;
88569       pColl[0].zName = (char*)&pColl[3];
88570       pColl[0].enc = SQLITE_UTF8;
88571       pColl[1].zName = (char*)&pColl[3];
88572       pColl[1].enc = SQLITE_UTF16LE;
88573       pColl[2].zName = (char*)&pColl[3];
88574       pColl[2].enc = SQLITE_UTF16BE;
88575       memcpy(pColl[0].zName, zName, nName);
88576       pColl[0].zName[nName] = 0;
88577       pDel = sqlite3HashInsert(&db->aCollSeq, pColl[0].zName, nName, pColl);
88578 
88579       /* If a malloc() failure occurred in sqlite3HashInsert(), it will
88580       ** return the pColl pointer to be deleted (because it wasn't added
88581       ** to the hash table).
88582       */
88583       assert( pDel==0 || pDel==pColl );
88584       if( pDel!=0 ){
88585         db->mallocFailed = 1;
88586         sqlite3DbFree(db, pDel);
88587         pColl = 0;
88588       }
88589     }
88590   }
88591   return pColl;
88592 }
88593 
88594 /*
88595 ** Parameter zName points to a UTF-8 encoded string nName bytes long.
88596 ** Return the CollSeq* pointer for the collation sequence named zName
88597 ** for the encoding 'enc' from the database 'db'.
88598 **
88599 ** If the entry specified is not found and 'create' is true, then create a
88600 ** new entry.  Otherwise return NULL.
88601 **
88602 ** A separate function sqlite3LocateCollSeq() is a wrapper around
88603 ** this routine.  sqlite3LocateCollSeq() invokes the collation factory
88604 ** if necessary and generates an error message if the collating sequence
88605 ** cannot be found.
88606 **
88607 ** See also: sqlite3LocateCollSeq(), sqlite3GetCollSeq()
88608 */
88609 SQLITE_PRIVATE CollSeq *sqlite3FindCollSeq(
88610   sqlite3 *db,
88611   u8 enc,
88612   const char *zName,
88613   int create
88614 ){
88615   CollSeq *pColl;
88616   if( zName ){
88617     pColl = findCollSeqEntry(db, zName, create);
88618   }else{
88619     pColl = db->pDfltColl;
88620   }
88621   assert( SQLITE_UTF8==1 && SQLITE_UTF16LE==2 && SQLITE_UTF16BE==3 );
88622   assert( enc>=SQLITE_UTF8 && enc<=SQLITE_UTF16BE );
88623   if( pColl ) pColl += enc-1;
88624   return pColl;
88625 }
88626 
88627 /* During the search for the best function definition, this procedure
88628 ** is called to test how well the function passed as the first argument
88629 ** matches the request for a function with nArg arguments in a system
88630 ** that uses encoding enc. The value returned indicates how well the
88631 ** request is matched. A higher value indicates a better match.
88632 **
88633 ** If nArg is -1 that means to only return a match (non-zero) if p->nArg
88634 ** is also -1.  In other words, we are searching for a function that
88635 ** takes a variable number of arguments.
88636 **
88637 ** If nArg is -2 that means that we are searching for any function
88638 ** regardless of the number of arguments it uses, so return a positive
88639 ** match score for any
88640 **
88641 ** The returned value is always between 0 and 6, as follows:
88642 **
88643 ** 0: Not a match.
88644 ** 1: UTF8/16 conversion required and function takes any number of arguments.
88645 ** 2: UTF16 byte order change required and function takes any number of args.
88646 ** 3: encoding matches and function takes any number of arguments
88647 ** 4: UTF8/16 conversion required - argument count matches exactly
88648 ** 5: UTF16 byte order conversion required - argument count matches exactly
88649 ** 6: Perfect match:  encoding and argument count match exactly.
88650 **
88651 ** If nArg==(-2) then any function with a non-null xStep or xFunc is
88652 ** a perfect match and any function with both xStep and xFunc NULL is
88653 ** a non-match.
88654 */
88655 #define FUNC_PERFECT_MATCH 6  /* The score for a perfect match */
88656 static int matchQuality(
88657   FuncDef *p,     /* The function we are evaluating for match quality */
88658   int nArg,       /* Desired number of arguments.  (-1)==any */
88659   u8 enc          /* Desired text encoding */
88660 ){
88661   int match;
88662 
88663   /* nArg of -2 is a special case */
88664   if( nArg==(-2) ) return (p->xFunc==0 && p->xStep==0) ? 0 : FUNC_PERFECT_MATCH;
88665 
88666   /* Wrong number of arguments means "no match" */
88667   if( p->nArg!=nArg && p->nArg>=0 ) return 0;
88668 
88669   /* Give a better score to a function with a specific number of arguments
88670   ** than to function that accepts any number of arguments. */
88671   if( p->nArg==nArg ){
88672     match = 4;
88673   }else{
88674     match = 1;
88675   }
88676 
88677   /* Bonus points if the text encoding matches */
88678   if( enc==(p->funcFlags & SQLITE_FUNC_ENCMASK) ){
88679     match += 2;  /* Exact encoding match */
88680   }else if( (enc & p->funcFlags & 2)!=0 ){
88681     match += 1;  /* Both are UTF16, but with different byte orders */
88682   }
88683 
88684   return match;
88685 }
88686 
88687 /*
88688 ** Search a FuncDefHash for a function with the given name.  Return
88689 ** a pointer to the matching FuncDef if found, or 0 if there is no match.
88690 */
88691 static FuncDef *functionSearch(
88692   FuncDefHash *pHash,  /* Hash table to search */
88693   int h,               /* Hash of the name */
88694   const char *zFunc,   /* Name of function */
88695   int nFunc            /* Number of bytes in zFunc */
88696 ){
88697   FuncDef *p;
88698   for(p=pHash->a[h]; p; p=p->pHash){
88699     if( sqlite3StrNICmp(p->zName, zFunc, nFunc)==0 && p->zName[nFunc]==0 ){
88700       return p;
88701     }
88702   }
88703   return 0;
88704 }
88705 
88706 /*
88707 ** Insert a new FuncDef into a FuncDefHash hash table.
88708 */
88709 SQLITE_PRIVATE void sqlite3FuncDefInsert(
88710   FuncDefHash *pHash,  /* The hash table into which to insert */
88711   FuncDef *pDef        /* The function definition to insert */
88712 ){
88713   FuncDef *pOther;
88714   int nName = sqlite3Strlen30(pDef->zName);
88715   u8 c1 = (u8)pDef->zName[0];
88716   int h = (sqlite3UpperToLower[c1] + nName) % ArraySize(pHash->a);
88717   pOther = functionSearch(pHash, h, pDef->zName, nName);
88718   if( pOther ){
88719     assert( pOther!=pDef && pOther->pNext!=pDef );
88720     pDef->pNext = pOther->pNext;
88721     pOther->pNext = pDef;
88722   }else{
88723     pDef->pNext = 0;
88724     pDef->pHash = pHash->a[h];
88725     pHash->a[h] = pDef;
88726   }
88727 }
88728 
88729 
88730 
88731 /*
88732 ** Locate a user function given a name, a number of arguments and a flag
88733 ** indicating whether the function prefers UTF-16 over UTF-8.  Return a
88734 ** pointer to the FuncDef structure that defines that function, or return
88735 ** NULL if the function does not exist.
88736 **
88737 ** If the createFlag argument is true, then a new (blank) FuncDef
88738 ** structure is created and liked into the "db" structure if a
88739 ** no matching function previously existed.
88740 **
88741 ** If nArg is -2, then the first valid function found is returned.  A
88742 ** function is valid if either xFunc or xStep is non-zero.  The nArg==(-2)
88743 ** case is used to see if zName is a valid function name for some number
88744 ** of arguments.  If nArg is -2, then createFlag must be 0.
88745 **
88746 ** If createFlag is false, then a function with the required name and
88747 ** number of arguments may be returned even if the eTextRep flag does not
88748 ** match that requested.
88749 */
88750 SQLITE_PRIVATE FuncDef *sqlite3FindFunction(
88751   sqlite3 *db,       /* An open database */
88752   const char *zName, /* Name of the function.  Not null-terminated */
88753   int nName,         /* Number of characters in the name */
88754   int nArg,          /* Number of arguments.  -1 means any number */
88755   u8 enc,            /* Preferred text encoding */
88756   u8 createFlag      /* Create new entry if true and does not otherwise exist */
88757 ){
88758   FuncDef *p;         /* Iterator variable */
88759   FuncDef *pBest = 0; /* Best match found so far */
88760   int bestScore = 0;  /* Score of best match */
88761   int h;              /* Hash value */
88762 
88763   assert( nArg>=(-2) );
88764   assert( nArg>=(-1) || createFlag==0 );
88765   h = (sqlite3UpperToLower[(u8)zName[0]] + nName) % ArraySize(db->aFunc.a);
88766 
88767   /* First search for a match amongst the application-defined functions.
88768   */
88769   p = functionSearch(&db->aFunc, h, zName, nName);
88770   while( p ){
88771     int score = matchQuality(p, nArg, enc);
88772     if( score>bestScore ){
88773       pBest = p;
88774       bestScore = score;
88775     }
88776     p = p->pNext;
88777   }
88778 
88779   /* If no match is found, search the built-in functions.
88780   **
88781   ** If the SQLITE_PreferBuiltin flag is set, then search the built-in
88782   ** functions even if a prior app-defined function was found.  And give
88783   ** priority to built-in functions.
88784   **
88785   ** Except, if createFlag is true, that means that we are trying to
88786   ** install a new function.  Whatever FuncDef structure is returned it will
88787   ** have fields overwritten with new information appropriate for the
88788   ** new function.  But the FuncDefs for built-in functions are read-only.
88789   ** So we must not search for built-ins when creating a new function.
88790   */
88791   if( !createFlag && (pBest==0 || (db->flags & SQLITE_PreferBuiltin)!=0) ){
88792     FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions);
88793     bestScore = 0;
88794     p = functionSearch(pHash, h, zName, nName);
88795     while( p ){
88796       int score = matchQuality(p, nArg, enc);
88797       if( score>bestScore ){
88798         pBest = p;
88799         bestScore = score;
88800       }
88801       p = p->pNext;
88802     }
88803   }
88804 
88805   /* If the createFlag parameter is true and the search did not reveal an
88806   ** exact match for the name, number of arguments and encoding, then add a
88807   ** new entry to the hash table and return it.
88808   */
88809   if( createFlag && bestScore<FUNC_PERFECT_MATCH &&
88810       (pBest = sqlite3DbMallocZero(db, sizeof(*pBest)+nName+1))!=0 ){
88811     pBest->zName = (char *)&pBest[1];
88812     pBest->nArg = (u16)nArg;
88813     pBest->funcFlags = enc;
88814     memcpy(pBest->zName, zName, nName);
88815     pBest->zName[nName] = 0;
88816     sqlite3FuncDefInsert(&db->aFunc, pBest);
88817   }
88818 
88819   if( pBest && (pBest->xStep || pBest->xFunc || createFlag) ){
88820     return pBest;
88821   }
88822   return 0;
88823 }
88824 
88825 /*
88826 ** Free all resources held by the schema structure. The void* argument points
88827 ** at a Schema struct. This function does not call sqlite3DbFree(db, ) on the
88828 ** pointer itself, it just cleans up subsidiary resources (i.e. the contents
88829 ** of the schema hash tables).
88830 **
88831 ** The Schema.cache_size variable is not cleared.
88832 */
88833 SQLITE_PRIVATE void sqlite3SchemaClear(void *p){
88834   Hash temp1;
88835   Hash temp2;
88836   HashElem *pElem;
88837   Schema *pSchema = (Schema *)p;
88838 
88839   temp1 = pSchema->tblHash;
88840   temp2 = pSchema->trigHash;
88841   sqlite3HashInit(&pSchema->trigHash);
88842   sqlite3HashClear(&pSchema->idxHash);
88843   for(pElem=sqliteHashFirst(&temp2); pElem; pElem=sqliteHashNext(pElem)){
88844     sqlite3DeleteTrigger(0, (Trigger*)sqliteHashData(pElem));
88845   }
88846   sqlite3HashClear(&temp2);
88847   sqlite3HashInit(&pSchema->tblHash);
88848   for(pElem=sqliteHashFirst(&temp1); pElem; pElem=sqliteHashNext(pElem)){
88849     Table *pTab = sqliteHashData(pElem);
88850     sqlite3DeleteTable(0, pTab);
88851   }
88852   sqlite3HashClear(&temp1);
88853   sqlite3HashClear(&pSchema->fkeyHash);
88854   pSchema->pSeqTab = 0;
88855   if( pSchema->flags & DB_SchemaLoaded ){
88856     pSchema->iGeneration++;
88857     pSchema->flags &= ~DB_SchemaLoaded;
88858   }
88859 }
88860 
88861 /*
88862 ** Find and return the schema associated with a BTree.  Create
88863 ** a new one if necessary.
88864 */
88865 SQLITE_PRIVATE Schema *sqlite3SchemaGet(sqlite3 *db, Btree *pBt){
88866   Schema * p;
88867   if( pBt ){
88868     p = (Schema *)sqlite3BtreeSchema(pBt, sizeof(Schema), sqlite3SchemaClear);
88869   }else{
88870     p = (Schema *)sqlite3DbMallocZero(0, sizeof(Schema));
88871   }
88872   if( !p ){
88873     db->mallocFailed = 1;
88874   }else if ( 0==p->file_format ){
88875     sqlite3HashInit(&p->tblHash);
88876     sqlite3HashInit(&p->idxHash);
88877     sqlite3HashInit(&p->trigHash);
88878     sqlite3HashInit(&p->fkeyHash);
88879     p->enc = SQLITE_UTF8;
88880   }
88881   return p;
88882 }
88883 
88884 /************** End of callback.c ********************************************/
88885 /************** Begin file delete.c ******************************************/
88886 /*
88887 ** 2001 September 15
88888 **
88889 ** The author disclaims copyright to this source code.  In place of
88890 ** a legal notice, here is a blessing:
88891 **
88892 **    May you do good and not evil.
88893 **    May you find forgiveness for yourself and forgive others.
88894 **    May you share freely, never taking more than you give.
88895 **
88896 *************************************************************************
88897 ** This file contains C code routines that are called by the parser
88898 ** in order to generate code for DELETE FROM statements.
88899 */
88900 
88901 /*
88902 ** While a SrcList can in general represent multiple tables and subqueries
88903 ** (as in the FROM clause of a SELECT statement) in this case it contains
88904 ** the name of a single table, as one might find in an INSERT, DELETE,
88905 ** or UPDATE statement.  Look up that table in the symbol table and
88906 ** return a pointer.  Set an error message and return NULL if the table
88907 ** name is not found or if any other error occurs.
88908 **
88909 ** The following fields are initialized appropriate in pSrc:
88910 **
88911 **    pSrc->a[0].pTab       Pointer to the Table object
88912 **    pSrc->a[0].pIndex     Pointer to the INDEXED BY index, if there is one
88913 **
88914 */
88915 SQLITE_PRIVATE Table *sqlite3SrcListLookup(Parse *pParse, SrcList *pSrc){
88916   struct SrcList_item *pItem = pSrc->a;
88917   Table *pTab;
88918   assert( pItem && pSrc->nSrc==1 );
88919   pTab = sqlite3LocateTableItem(pParse, 0, pItem);
88920   sqlite3DeleteTable(pParse->db, pItem->pTab);
88921   pItem->pTab = pTab;
88922   if( pTab ){
88923     pTab->nRef++;
88924   }
88925   if( sqlite3IndexedByLookup(pParse, pItem) ){
88926     pTab = 0;
88927   }
88928   return pTab;
88929 }
88930 
88931 /*
88932 ** Check to make sure the given table is writable.  If it is not
88933 ** writable, generate an error message and return 1.  If it is
88934 ** writable return 0;
88935 */
88936 SQLITE_PRIVATE int sqlite3IsReadOnly(Parse *pParse, Table *pTab, int viewOk){
88937   /* A table is not writable under the following circumstances:
88938   **
88939   **   1) It is a virtual table and no implementation of the xUpdate method
88940   **      has been provided, or
88941   **   2) It is a system table (i.e. sqlite_master), this call is not
88942   **      part of a nested parse and writable_schema pragma has not
88943   **      been specified.
88944   **
88945   ** In either case leave an error message in pParse and return non-zero.
88946   */
88947   if( ( IsVirtual(pTab)
88948      && sqlite3GetVTable(pParse->db, pTab)->pMod->pModule->xUpdate==0 )
88949    || ( (pTab->tabFlags & TF_Readonly)!=0
88950      && (pParse->db->flags & SQLITE_WriteSchema)==0
88951      && pParse->nested==0 )
88952   ){
88953     sqlite3ErrorMsg(pParse, "table %s may not be modified", pTab->zName);
88954     return 1;
88955   }
88956 
88957 #ifndef SQLITE_OMIT_VIEW
88958   if( !viewOk && pTab->pSelect ){
88959     sqlite3ErrorMsg(pParse,"cannot modify %s because it is a view",pTab->zName);
88960     return 1;
88961   }
88962 #endif
88963   return 0;
88964 }
88965 
88966 
88967 #if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
88968 /*
88969 ** Evaluate a view and store its result in an ephemeral table.  The
88970 ** pWhere argument is an optional WHERE clause that restricts the
88971 ** set of rows in the view that are to be added to the ephemeral table.
88972 */
88973 SQLITE_PRIVATE void sqlite3MaterializeView(
88974   Parse *pParse,       /* Parsing context */
88975   Table *pView,        /* View definition */
88976   Expr *pWhere,        /* Optional WHERE clause to be added */
88977   int iCur             /* Cursor number for ephemerial table */
88978 ){
88979   SelectDest dest;
88980   Select *pSel;
88981   SrcList *pFrom;
88982   sqlite3 *db = pParse->db;
88983   int iDb = sqlite3SchemaToIndex(db, pView->pSchema);
88984 
88985   pWhere = sqlite3ExprDup(db, pWhere, 0);
88986   pFrom = sqlite3SrcListAppend(db, 0, 0, 0);
88987 
88988   if( pFrom ){
88989     assert( pFrom->nSrc==1 );
88990     pFrom->a[0].zName = sqlite3DbStrDup(db, pView->zName);
88991     pFrom->a[0].zDatabase = sqlite3DbStrDup(db, db->aDb[iDb].zName);
88992     assert( pFrom->a[0].pOn==0 );
88993     assert( pFrom->a[0].pUsing==0 );
88994   }
88995 
88996   pSel = sqlite3SelectNew(pParse, 0, pFrom, pWhere, 0, 0, 0, 0, 0, 0);
88997   if( pSel ) pSel->selFlags |= SF_Materialize;
88998 
88999   sqlite3SelectDestInit(&dest, SRT_EphemTab, iCur);
89000   sqlite3Select(pParse, pSel, &dest);
89001   sqlite3SelectDelete(db, pSel);
89002 }
89003 #endif /* !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER) */
89004 
89005 #if defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) && !defined(SQLITE_OMIT_SUBQUERY)
89006 /*
89007 ** Generate an expression tree to implement the WHERE, ORDER BY,
89008 ** and LIMIT/OFFSET portion of DELETE and UPDATE statements.
89009 **
89010 **     DELETE FROM table_wxyz WHERE a<5 ORDER BY a LIMIT 1;
89011 **                            \__________________________/
89012 **                               pLimitWhere (pInClause)
89013 */
89014 SQLITE_PRIVATE Expr *sqlite3LimitWhere(
89015   Parse *pParse,               /* The parser context */
89016   SrcList *pSrc,               /* the FROM clause -- which tables to scan */
89017   Expr *pWhere,                /* The WHERE clause.  May be null */
89018   ExprList *pOrderBy,          /* The ORDER BY clause.  May be null */
89019   Expr *pLimit,                /* The LIMIT clause.  May be null */
89020   Expr *pOffset,               /* The OFFSET clause.  May be null */
89021   char *zStmtType              /* Either DELETE or UPDATE.  For err msgs. */
89022 ){
89023   Expr *pWhereRowid = NULL;    /* WHERE rowid .. */
89024   Expr *pInClause = NULL;      /* WHERE rowid IN ( select ) */
89025   Expr *pSelectRowid = NULL;   /* SELECT rowid ... */
89026   ExprList *pEList = NULL;     /* Expression list contaning only pSelectRowid */
89027   SrcList *pSelectSrc = NULL;  /* SELECT rowid FROM x ... (dup of pSrc) */
89028   Select *pSelect = NULL;      /* Complete SELECT tree */
89029 
89030   /* Check that there isn't an ORDER BY without a LIMIT clause.
89031   */
89032   if( pOrderBy && (pLimit == 0) ) {
89033     sqlite3ErrorMsg(pParse, "ORDER BY without LIMIT on %s", zStmtType);
89034     goto limit_where_cleanup_2;
89035   }
89036 
89037   /* We only need to generate a select expression if there
89038   ** is a limit/offset term to enforce.
89039   */
89040   if( pLimit == 0 ) {
89041     /* if pLimit is null, pOffset will always be null as well. */
89042     assert( pOffset == 0 );
89043     return pWhere;
89044   }
89045 
89046   /* Generate a select expression tree to enforce the limit/offset
89047   ** term for the DELETE or UPDATE statement.  For example:
89048   **   DELETE FROM table_a WHERE col1=1 ORDER BY col2 LIMIT 1 OFFSET 1
89049   ** becomes:
89050   **   DELETE FROM table_a WHERE rowid IN (
89051   **     SELECT rowid FROM table_a WHERE col1=1 ORDER BY col2 LIMIT 1 OFFSET 1
89052   **   );
89053   */
89054 
89055   pSelectRowid = sqlite3PExpr(pParse, TK_ROW, 0, 0, 0);
89056   if( pSelectRowid == 0 ) goto limit_where_cleanup_2;
89057   pEList = sqlite3ExprListAppend(pParse, 0, pSelectRowid);
89058   if( pEList == 0 ) goto limit_where_cleanup_2;
89059 
89060   /* duplicate the FROM clause as it is needed by both the DELETE/UPDATE tree
89061   ** and the SELECT subtree. */
89062   pSelectSrc = sqlite3SrcListDup(pParse->db, pSrc, 0);
89063   if( pSelectSrc == 0 ) {
89064     sqlite3ExprListDelete(pParse->db, pEList);
89065     goto limit_where_cleanup_2;
89066   }
89067 
89068   /* generate the SELECT expression tree. */
89069   pSelect = sqlite3SelectNew(pParse,pEList,pSelectSrc,pWhere,0,0,
89070                              pOrderBy,0,pLimit,pOffset);
89071   if( pSelect == 0 ) return 0;
89072 
89073   /* now generate the new WHERE rowid IN clause for the DELETE/UDPATE */
89074   pWhereRowid = sqlite3PExpr(pParse, TK_ROW, 0, 0, 0);
89075   if( pWhereRowid == 0 ) goto limit_where_cleanup_1;
89076   pInClause = sqlite3PExpr(pParse, TK_IN, pWhereRowid, 0, 0);
89077   if( pInClause == 0 ) goto limit_where_cleanup_1;
89078 
89079   pInClause->x.pSelect = pSelect;
89080   pInClause->flags |= EP_xIsSelect;
89081   sqlite3ExprSetHeight(pParse, pInClause);
89082   return pInClause;
89083 
89084   /* something went wrong. clean up anything allocated. */
89085 limit_where_cleanup_1:
89086   sqlite3SelectDelete(pParse->db, pSelect);
89087   return 0;
89088 
89089 limit_where_cleanup_2:
89090   sqlite3ExprDelete(pParse->db, pWhere);
89091   sqlite3ExprListDelete(pParse->db, pOrderBy);
89092   sqlite3ExprDelete(pParse->db, pLimit);
89093   sqlite3ExprDelete(pParse->db, pOffset);
89094   return 0;
89095 }
89096 #endif /* defined(SQLITE_ENABLE_UPDATE_DELETE_LIMIT) */
89097        /*      && !defined(SQLITE_OMIT_SUBQUERY) */
89098 
89099 /*
89100 ** Generate code for a DELETE FROM statement.
89101 **
89102 **     DELETE FROM table_wxyz WHERE a<5 AND b NOT NULL;
89103 **                 \________/       \________________/
89104 **                  pTabList              pWhere
89105 */
89106 SQLITE_PRIVATE void sqlite3DeleteFrom(
89107   Parse *pParse,         /* The parser context */
89108   SrcList *pTabList,     /* The table from which we should delete things */
89109   Expr *pWhere           /* The WHERE clause.  May be null */
89110 ){
89111   Vdbe *v;               /* The virtual database engine */
89112   Table *pTab;           /* The table from which records will be deleted */
89113   const char *zDb;       /* Name of database holding pTab */
89114   int i;                 /* Loop counter */
89115   WhereInfo *pWInfo;     /* Information about the WHERE clause */
89116   Index *pIdx;           /* For looping over indices of the table */
89117   int iTabCur;           /* Cursor number for the table */
89118   int iDataCur;          /* VDBE cursor for the canonical data source */
89119   int iIdxCur;           /* Cursor number of the first index */
89120   int nIdx;              /* Number of indices */
89121   sqlite3 *db;           /* Main database structure */
89122   AuthContext sContext;  /* Authorization context */
89123   NameContext sNC;       /* Name context to resolve expressions in */
89124   int iDb;               /* Database number */
89125   int memCnt = -1;       /* Memory cell used for change counting */
89126   int rcauth;            /* Value returned by authorization callback */
89127   int okOnePass;         /* True for one-pass algorithm without the FIFO */
89128   int aiCurOnePass[2];   /* The write cursors opened by WHERE_ONEPASS */
89129   u8 *aToOpen = 0;       /* Open cursor iTabCur+j if aToOpen[j] is true */
89130   Index *pPk;            /* The PRIMARY KEY index on the table */
89131   int iPk = 0;           /* First of nPk registers holding PRIMARY KEY value */
89132   i16 nPk = 1;           /* Number of columns in the PRIMARY KEY */
89133   int iKey;              /* Memory cell holding key of row to be deleted */
89134   i16 nKey;              /* Number of memory cells in the row key */
89135   int iEphCur = 0;       /* Ephemeral table holding all primary key values */
89136   int iRowSet = 0;       /* Register for rowset of rows to delete */
89137   int addrBypass = 0;    /* Address of jump over the delete logic */
89138   int addrLoop = 0;      /* Top of the delete loop */
89139   int addrDelete = 0;    /* Jump directly to the delete logic */
89140   int addrEphOpen = 0;   /* Instruction to open the Ephermeral table */
89141 
89142 #ifndef SQLITE_OMIT_TRIGGER
89143   int isView;                  /* True if attempting to delete from a view */
89144   Trigger *pTrigger;           /* List of table triggers, if required */
89145 #endif
89146 
89147   memset(&sContext, 0, sizeof(sContext));
89148   db = pParse->db;
89149   if( pParse->nErr || db->mallocFailed ){
89150     goto delete_from_cleanup;
89151   }
89152   assert( pTabList->nSrc==1 );
89153 
89154   /* Locate the table which we want to delete.  This table has to be
89155   ** put in an SrcList structure because some of the subroutines we
89156   ** will be calling are designed to work with multiple tables and expect
89157   ** an SrcList* parameter instead of just a Table* parameter.
89158   */
89159   pTab = sqlite3SrcListLookup(pParse, pTabList);
89160   if( pTab==0 )  goto delete_from_cleanup;
89161 
89162   /* Figure out if we have any triggers and if the table being
89163   ** deleted from is a view
89164   */
89165 #ifndef SQLITE_OMIT_TRIGGER
89166   pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0);
89167   isView = pTab->pSelect!=0;
89168 #else
89169 # define pTrigger 0
89170 # define isView 0
89171 #endif
89172 #ifdef SQLITE_OMIT_VIEW
89173 # undef isView
89174 # define isView 0
89175 #endif
89176 
89177   /* If pTab is really a view, make sure it has been initialized.
89178   */
89179   if( sqlite3ViewGetColumnNames(pParse, pTab) ){
89180     goto delete_from_cleanup;
89181   }
89182 
89183   if( sqlite3IsReadOnly(pParse, pTab, (pTrigger?1:0)) ){
89184     goto delete_from_cleanup;
89185   }
89186   iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
89187   assert( iDb<db->nDb );
89188   zDb = db->aDb[iDb].zName;
89189   rcauth = sqlite3AuthCheck(pParse, SQLITE_DELETE, pTab->zName, 0, zDb);
89190   assert( rcauth==SQLITE_OK || rcauth==SQLITE_DENY || rcauth==SQLITE_IGNORE );
89191   if( rcauth==SQLITE_DENY ){
89192     goto delete_from_cleanup;
89193   }
89194   assert(!isView || pTrigger);
89195 
89196   /* Assign cursor numbers to the table and all its indices.
89197   */
89198   assert( pTabList->nSrc==1 );
89199   iTabCur = pTabList->a[0].iCursor = pParse->nTab++;
89200   for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){
89201     pParse->nTab++;
89202   }
89203 
89204   /* Start the view context
89205   */
89206   if( isView ){
89207     sqlite3AuthContextPush(pParse, &sContext, pTab->zName);
89208   }
89209 
89210   /* Begin generating code.
89211   */
89212   v = sqlite3GetVdbe(pParse);
89213   if( v==0 ){
89214     goto delete_from_cleanup;
89215   }
89216   if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
89217   sqlite3BeginWriteOperation(pParse, 1, iDb);
89218 
89219   /* If we are trying to delete from a view, realize that view into
89220   ** a ephemeral table.
89221   */
89222 #if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
89223   if( isView ){
89224     sqlite3MaterializeView(pParse, pTab, pWhere, iTabCur);
89225     iDataCur = iIdxCur = iTabCur;
89226   }
89227 #endif
89228 
89229   /* Resolve the column names in the WHERE clause.
89230   */
89231   memset(&sNC, 0, sizeof(sNC));
89232   sNC.pParse = pParse;
89233   sNC.pSrcList = pTabList;
89234   if( sqlite3ResolveExprNames(&sNC, pWhere) ){
89235     goto delete_from_cleanup;
89236   }
89237 
89238   /* Initialize the counter of the number of rows deleted, if
89239   ** we are counting rows.
89240   */
89241   if( db->flags & SQLITE_CountRows ){
89242     memCnt = ++pParse->nMem;
89243     sqlite3VdbeAddOp2(v, OP_Integer, 0, memCnt);
89244   }
89245 
89246 #ifndef SQLITE_OMIT_TRUNCATE_OPTIMIZATION
89247   /* Special case: A DELETE without a WHERE clause deletes everything.
89248   ** It is easier just to erase the whole table. Prior to version 3.6.5,
89249   ** this optimization caused the row change count (the value returned by
89250   ** API function sqlite3_count_changes) to be set incorrectly.  */
89251   if( rcauth==SQLITE_OK && pWhere==0 && !pTrigger && !IsVirtual(pTab)
89252    && 0==sqlite3FkRequired(pParse, pTab, 0, 0)
89253   ){
89254     assert( !isView );
89255     sqlite3TableLock(pParse, iDb, pTab->tnum, 1, pTab->zName);
89256     if( HasRowid(pTab) ){
89257       sqlite3VdbeAddOp4(v, OP_Clear, pTab->tnum, iDb, memCnt,
89258                         pTab->zName, P4_STATIC);
89259     }
89260     for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
89261       assert( pIdx->pSchema==pTab->pSchema );
89262       sqlite3VdbeAddOp2(v, OP_Clear, pIdx->tnum, iDb);
89263     }
89264   }else
89265 #endif /* SQLITE_OMIT_TRUNCATE_OPTIMIZATION */
89266   {
89267     if( HasRowid(pTab) ){
89268       /* For a rowid table, initialize the RowSet to an empty set */
89269       pPk = 0;
89270       nPk = 1;
89271       iRowSet = ++pParse->nMem;
89272       sqlite3VdbeAddOp2(v, OP_Null, 0, iRowSet);
89273     }else{
89274       /* For a WITHOUT ROWID table, create an ephermeral table used to
89275       ** hold all primary keys for rows to be deleted. */
89276       pPk = sqlite3PrimaryKeyIndex(pTab);
89277       assert( pPk!=0 );
89278       nPk = pPk->nKeyCol;
89279       iPk = pParse->nMem+1;
89280       pParse->nMem += nPk;
89281       iEphCur = pParse->nTab++;
89282       addrEphOpen = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, iEphCur, nPk);
89283       sqlite3VdbeSetP4KeyInfo(pParse, pPk);
89284     }
89285 
89286     /* Construct a query to find the rowid or primary key for every row
89287     ** to be deleted, based on the WHERE clause.
89288     */
89289     pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0, 0,
89290                                WHERE_ONEPASS_DESIRED|WHERE_DUPLICATES_OK,
89291                                iTabCur+1);
89292     if( pWInfo==0 ) goto delete_from_cleanup;
89293     okOnePass = sqlite3WhereOkOnePass(pWInfo, aiCurOnePass);
89294 
89295     /* Keep track of the number of rows to be deleted */
89296     if( db->flags & SQLITE_CountRows ){
89297       sqlite3VdbeAddOp2(v, OP_AddImm, memCnt, 1);
89298     }
89299 
89300     /* Extract the rowid or primary key for the current row */
89301     if( pPk ){
89302       for(i=0; i<nPk; i++){
89303         sqlite3ExprCodeGetColumnOfTable(v, pTab, iTabCur,
89304                                         pPk->aiColumn[i], iPk+i);
89305       }
89306       iKey = iPk;
89307     }else{
89308       iKey = pParse->nMem + 1;
89309       iKey = sqlite3ExprCodeGetColumn(pParse, pTab, -1, iTabCur, iKey, 0);
89310       if( iKey>pParse->nMem ) pParse->nMem = iKey;
89311     }
89312 
89313     if( okOnePass ){
89314       /* For ONEPASS, no need to store the rowid/primary-key.  There is only
89315       ** one, so just keep it in its register(s) and fall through to the
89316       ** delete code.
89317       */
89318       nKey = nPk; /* OP_Found will use an unpacked key */
89319       aToOpen = sqlite3DbMallocRaw(db, nIdx+2);
89320       if( aToOpen==0 ){
89321         sqlite3WhereEnd(pWInfo);
89322         goto delete_from_cleanup;
89323       }
89324       memset(aToOpen, 1, nIdx+1);
89325       aToOpen[nIdx+1] = 0;
89326       if( aiCurOnePass[0]>=0 ) aToOpen[aiCurOnePass[0]-iTabCur] = 0;
89327       if( aiCurOnePass[1]>=0 ) aToOpen[aiCurOnePass[1]-iTabCur] = 0;
89328       if( addrEphOpen ) sqlite3VdbeChangeToNoop(v, addrEphOpen);
89329       addrDelete = sqlite3VdbeAddOp0(v, OP_Goto); /* Jump to DELETE logic */
89330     }else if( pPk ){
89331       /* Construct a composite key for the row to be deleted and remember it */
89332       iKey = ++pParse->nMem;
89333       nKey = 0;   /* Zero tells OP_Found to use a composite key */
89334       sqlite3VdbeAddOp4(v, OP_MakeRecord, iPk, nPk, iKey,
89335                         sqlite3IndexAffinityStr(v, pPk), P4_TRANSIENT);
89336       sqlite3VdbeAddOp2(v, OP_IdxInsert, iEphCur, iKey);
89337     }else{
89338       /* Get the rowid of the row to be deleted and remember it in the RowSet */
89339       nKey = 1;  /* OP_Seek always uses a single rowid */
89340       sqlite3VdbeAddOp2(v, OP_RowSetAdd, iRowSet, iKey);
89341     }
89342 
89343     /* End of the WHERE loop */
89344     sqlite3WhereEnd(pWInfo);
89345     if( okOnePass ){
89346       /* Bypass the delete logic below if the WHERE loop found zero rows */
89347       addrBypass = sqlite3VdbeMakeLabel(v);
89348       sqlite3VdbeAddOp2(v, OP_Goto, 0, addrBypass);
89349       sqlite3VdbeJumpHere(v, addrDelete);
89350     }
89351 
89352     /* Unless this is a view, open cursors for the table we are
89353     ** deleting from and all its indices. If this is a view, then the
89354     ** only effect this statement has is to fire the INSTEAD OF
89355     ** triggers.
89356     */
89357     if( !isView ){
89358       sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, iTabCur, aToOpen,
89359                                  &iDataCur, &iIdxCur);
89360       assert( pPk || iDataCur==iTabCur );
89361       assert( pPk || iIdxCur==iDataCur+1 );
89362     }
89363 
89364     /* Set up a loop over the rowids/primary-keys that were found in the
89365     ** where-clause loop above.
89366     */
89367     if( okOnePass ){
89368       /* Just one row.  Hence the top-of-loop is a no-op */
89369       assert( nKey==nPk ); /* OP_Found will use an unpacked key */
89370       if( aToOpen[iDataCur-iTabCur] ){
89371         assert( pPk!=0 );
89372         sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, addrBypass, iKey, nKey);
89373       }
89374     }else if( pPk ){
89375       addrLoop = sqlite3VdbeAddOp1(v, OP_Rewind, iEphCur);
89376       sqlite3VdbeAddOp2(v, OP_RowKey, iEphCur, iKey);
89377       assert( nKey==0 );  /* OP_Found will use a composite key */
89378     }else{
89379       addrLoop = sqlite3VdbeAddOp3(v, OP_RowSetRead, iRowSet, 0, iKey);
89380       assert( nKey==1 );
89381     }
89382 
89383     /* Delete the row */
89384 #ifndef SQLITE_OMIT_VIRTUALTABLE
89385     if( IsVirtual(pTab) ){
89386       const char *pVTab = (const char *)sqlite3GetVTable(db, pTab);
89387       sqlite3VtabMakeWritable(pParse, pTab);
89388       sqlite3VdbeAddOp4(v, OP_VUpdate, 0, 1, iKey, pVTab, P4_VTAB);
89389       sqlite3VdbeChangeP5(v, OE_Abort);
89390       sqlite3MayAbort(pParse);
89391     }else
89392 #endif
89393     {
89394       int count = (pParse->nested==0);    /* True to count changes */
89395       sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur,
89396                                iKey, nKey, count, OE_Default, okOnePass);
89397     }
89398 
89399     /* End of the loop over all rowids/primary-keys. */
89400     if( okOnePass ){
89401       sqlite3VdbeResolveLabel(v, addrBypass);
89402     }else if( pPk ){
89403       sqlite3VdbeAddOp2(v, OP_Next, iEphCur, addrLoop+1);
89404       sqlite3VdbeJumpHere(v, addrLoop);
89405     }else{
89406       sqlite3VdbeAddOp2(v, OP_Goto, 0, addrLoop);
89407       sqlite3VdbeJumpHere(v, addrLoop);
89408     }
89409 
89410     /* Close the cursors open on the table and its indexes. */
89411     if( !isView && !IsVirtual(pTab) ){
89412       if( !pPk ) sqlite3VdbeAddOp1(v, OP_Close, iDataCur);
89413       for(i=0, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){
89414         sqlite3VdbeAddOp1(v, OP_Close, iIdxCur + i);
89415       }
89416     }
89417   } /* End non-truncate path */
89418 
89419   /* Update the sqlite_sequence table by storing the content of the
89420   ** maximum rowid counter values recorded while inserting into
89421   ** autoincrement tables.
89422   */
89423   if( pParse->nested==0 && pParse->pTriggerTab==0 ){
89424     sqlite3AutoincrementEnd(pParse);
89425   }
89426 
89427   /* Return the number of rows that were deleted. If this routine is
89428   ** generating code because of a call to sqlite3NestedParse(), do not
89429   ** invoke the callback function.
89430   */
89431   if( (db->flags&SQLITE_CountRows) && !pParse->nested && !pParse->pTriggerTab ){
89432     sqlite3VdbeAddOp2(v, OP_ResultRow, memCnt, 1);
89433     sqlite3VdbeSetNumCols(v, 1);
89434     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows deleted", SQLITE_STATIC);
89435   }
89436 
89437 delete_from_cleanup:
89438   sqlite3AuthContextPop(&sContext);
89439   sqlite3SrcListDelete(db, pTabList);
89440   sqlite3ExprDelete(db, pWhere);
89441   sqlite3DbFree(db, aToOpen);
89442   return;
89443 }
89444 /* Make sure "isView" and other macros defined above are undefined. Otherwise
89445 ** thely may interfere with compilation of other functions in this file
89446 ** (or in another file, if this file becomes part of the amalgamation).  */
89447 #ifdef isView
89448  #undef isView
89449 #endif
89450 #ifdef pTrigger
89451  #undef pTrigger
89452 #endif
89453 
89454 /*
89455 ** This routine generates VDBE code that causes a single row of a
89456 ** single table to be deleted.  Both the original table entry and
89457 ** all indices are removed.
89458 **
89459 ** Preconditions:
89460 **
89461 **   1.  iDataCur is an open cursor on the btree that is the canonical data
89462 **       store for the table.  (This will be either the table itself,
89463 **       in the case of a rowid table, or the PRIMARY KEY index in the case
89464 **       of a WITHOUT ROWID table.)
89465 **
89466 **   2.  Read/write cursors for all indices of pTab must be open as
89467 **       cursor number iIdxCur+i for the i-th index.
89468 **
89469 **   3.  The primary key for the row to be deleted must be stored in a
89470 **       sequence of nPk memory cells starting at iPk.  If nPk==0 that means
89471 **       that a search record formed from OP_MakeRecord is contained in the
89472 **       single memory location iPk.
89473 */
89474 SQLITE_PRIVATE void sqlite3GenerateRowDelete(
89475   Parse *pParse,     /* Parsing context */
89476   Table *pTab,       /* Table containing the row to be deleted */
89477   Trigger *pTrigger, /* List of triggers to (potentially) fire */
89478   int iDataCur,      /* Cursor from which column data is extracted */
89479   int iIdxCur,       /* First index cursor */
89480   int iPk,           /* First memory cell containing the PRIMARY KEY */
89481   i16 nPk,           /* Number of PRIMARY KEY memory cells */
89482   u8 count,          /* If non-zero, increment the row change counter */
89483   u8 onconf,         /* Default ON CONFLICT policy for triggers */
89484   u8 bNoSeek         /* iDataCur is already pointing to the row to delete */
89485 ){
89486   Vdbe *v = pParse->pVdbe;        /* Vdbe */
89487   int iOld = 0;                   /* First register in OLD.* array */
89488   int iLabel;                     /* Label resolved to end of generated code */
89489   u8 opSeek;                      /* Seek opcode */
89490 
89491   /* Vdbe is guaranteed to have been allocated by this stage. */
89492   assert( v );
89493   VdbeModuleComment((v, "BEGIN: GenRowDel(%d,%d,%d,%d)",
89494                          iDataCur, iIdxCur, iPk, (int)nPk));
89495 
89496   /* Seek cursor iCur to the row to delete. If this row no longer exists
89497   ** (this can happen if a trigger program has already deleted it), do
89498   ** not attempt to delete it or fire any DELETE triggers.  */
89499   iLabel = sqlite3VdbeMakeLabel(v);
89500   opSeek = HasRowid(pTab) ? OP_NotExists : OP_NotFound;
89501   if( !bNoSeek ) sqlite3VdbeAddOp4Int(v, opSeek, iDataCur, iLabel, iPk, nPk);
89502 
89503   /* If there are any triggers to fire, allocate a range of registers to
89504   ** use for the old.* references in the triggers.  */
89505   if( sqlite3FkRequired(pParse, pTab, 0, 0) || pTrigger ){
89506     u32 mask;                     /* Mask of OLD.* columns in use */
89507     int iCol;                     /* Iterator used while populating OLD.* */
89508     int addrStart;                /* Start of BEFORE trigger programs */
89509 
89510     /* TODO: Could use temporary registers here. Also could attempt to
89511     ** avoid copying the contents of the rowid register.  */
89512     mask = sqlite3TriggerColmask(
89513         pParse, pTrigger, 0, 0, TRIGGER_BEFORE|TRIGGER_AFTER, pTab, onconf
89514     );
89515     mask |= sqlite3FkOldmask(pParse, pTab);
89516     iOld = pParse->nMem+1;
89517     pParse->nMem += (1 + pTab->nCol);
89518 
89519     /* Populate the OLD.* pseudo-table register array. These values will be
89520     ** used by any BEFORE and AFTER triggers that exist.  */
89521     sqlite3VdbeAddOp2(v, OP_Copy, iPk, iOld);
89522     for(iCol=0; iCol<pTab->nCol; iCol++){
89523       testcase( mask!=0xffffffff && iCol==31 );
89524       testcase( mask!=0xffffffff && iCol==32 );
89525       if( mask==0xffffffff || (iCol<=31 && (mask & MASKBIT32(iCol))!=0) ){
89526         sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, iCol, iOld+iCol+1);
89527       }
89528     }
89529 
89530     /* Invoke BEFORE DELETE trigger programs. */
89531     addrStart = sqlite3VdbeCurrentAddr(v);
89532     sqlite3CodeRowTrigger(pParse, pTrigger,
89533         TK_DELETE, 0, TRIGGER_BEFORE, pTab, iOld, onconf, iLabel
89534     );
89535 
89536     /* If any BEFORE triggers were coded, then seek the cursor to the
89537     ** row to be deleted again. It may be that the BEFORE triggers moved
89538     ** the cursor or of already deleted the row that the cursor was
89539     ** pointing to.
89540     */
89541     if( addrStart<sqlite3VdbeCurrentAddr(v) ){
89542       sqlite3VdbeAddOp4Int(v, opSeek, iDataCur, iLabel, iPk, nPk);
89543     }
89544 
89545     /* Do FK processing. This call checks that any FK constraints that
89546     ** refer to this table (i.e. constraints attached to other tables)
89547     ** are not violated by deleting this row.  */
89548     sqlite3FkCheck(pParse, pTab, iOld, 0, 0, 0);
89549   }
89550 
89551   /* Delete the index and table entries. Skip this step if pTab is really
89552   ** a view (in which case the only effect of the DELETE statement is to
89553   ** fire the INSTEAD OF triggers).  */
89554   if( pTab->pSelect==0 ){
89555     sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur, 0);
89556     sqlite3VdbeAddOp2(v, OP_Delete, iDataCur, (count?OPFLAG_NCHANGE:0));
89557     if( count ){
89558       sqlite3VdbeChangeP4(v, -1, pTab->zName, P4_TRANSIENT);
89559     }
89560   }
89561 
89562   /* Do any ON CASCADE, SET NULL or SET DEFAULT operations required to
89563   ** handle rows (possibly in other tables) that refer via a foreign key
89564   ** to the row just deleted. */
89565   sqlite3FkActions(pParse, pTab, 0, iOld, 0, 0);
89566 
89567   /* Invoke AFTER DELETE trigger programs. */
89568   sqlite3CodeRowTrigger(pParse, pTrigger,
89569       TK_DELETE, 0, TRIGGER_AFTER, pTab, iOld, onconf, iLabel
89570   );
89571 
89572   /* Jump here if the row had already been deleted before any BEFORE
89573   ** trigger programs were invoked. Or if a trigger program throws a
89574   ** RAISE(IGNORE) exception.  */
89575   sqlite3VdbeResolveLabel(v, iLabel);
89576   VdbeModuleComment((v, "END: GenRowDel()"));
89577 }
89578 
89579 /*
89580 ** This routine generates VDBE code that causes the deletion of all
89581 ** index entries associated with a single row of a single table, pTab
89582 **
89583 ** Preconditions:
89584 **
89585 **   1.  A read/write cursor "iDataCur" must be open on the canonical storage
89586 **       btree for the table pTab.  (This will be either the table itself
89587 **       for rowid tables or to the primary key index for WITHOUT ROWID
89588 **       tables.)
89589 **
89590 **   2.  Read/write cursors for all indices of pTab must be open as
89591 **       cursor number iIdxCur+i for the i-th index.  (The pTab->pIndex
89592 **       index is the 0-th index.)
89593 **
89594 **   3.  The "iDataCur" cursor must be already be positioned on the row
89595 **       that is to be deleted.
89596 */
89597 SQLITE_PRIVATE void sqlite3GenerateRowIndexDelete(
89598   Parse *pParse,     /* Parsing and code generating context */
89599   Table *pTab,       /* Table containing the row to be deleted */
89600   int iDataCur,      /* Cursor of table holding data. */
89601   int iIdxCur,       /* First index cursor */
89602   int *aRegIdx       /* Only delete if aRegIdx!=0 && aRegIdx[i]>0 */
89603 ){
89604   int i;             /* Index loop counter */
89605   int r1 = -1;       /* Register holding an index key */
89606   int iPartIdxLabel; /* Jump destination for skipping partial index entries */
89607   Index *pIdx;       /* Current index */
89608   Index *pPrior = 0; /* Prior index */
89609   Vdbe *v;           /* The prepared statement under construction */
89610   Index *pPk;        /* PRIMARY KEY index, or NULL for rowid tables */
89611 
89612   v = pParse->pVdbe;
89613   pPk = HasRowid(pTab) ? 0 : sqlite3PrimaryKeyIndex(pTab);
89614   for(i=0, pIdx=pTab->pIndex; pIdx; i++, pIdx=pIdx->pNext){
89615     assert( iIdxCur+i!=iDataCur || pPk==pIdx );
89616     if( aRegIdx!=0 && aRegIdx[i]==0 ) continue;
89617     if( pIdx==pPk ) continue;
89618     VdbeModuleComment((v, "GenRowIdxDel for %s", pIdx->zName));
89619     r1 = sqlite3GenerateIndexKey(pParse, pIdx, iDataCur, 0, 1,
89620                                  &iPartIdxLabel, pPrior, r1);
89621     sqlite3VdbeAddOp3(v, OP_IdxDelete, iIdxCur+i, r1,
89622                       pIdx->uniqNotNull ? pIdx->nKeyCol : pIdx->nColumn);
89623     sqlite3VdbeResolveLabel(v, iPartIdxLabel);
89624     pPrior = pIdx;
89625   }
89626 }
89627 
89628 /*
89629 ** Generate code that will assemble an index key and stores it in register
89630 ** regOut.  The key with be for index pIdx which is an index on pTab.
89631 ** iCur is the index of a cursor open on the pTab table and pointing to
89632 ** the entry that needs indexing.  If pTab is a WITHOUT ROWID table, then
89633 ** iCur must be the cursor of the PRIMARY KEY index.
89634 **
89635 ** Return a register number which is the first in a block of
89636 ** registers that holds the elements of the index key.  The
89637 ** block of registers has already been deallocated by the time
89638 ** this routine returns.
89639 **
89640 ** If *piPartIdxLabel is not NULL, fill it in with a label and jump
89641 ** to that label if pIdx is a partial index that should be skipped.
89642 ** A partial index should be skipped if its WHERE clause evaluates
89643 ** to false or null.  If pIdx is not a partial index, *piPartIdxLabel
89644 ** will be set to zero which is an empty label that is ignored by
89645 ** sqlite3VdbeResolveLabel().
89646 **
89647 ** The pPrior and regPrior parameters are used to implement a cache to
89648 ** avoid unnecessary register loads.  If pPrior is not NULL, then it is
89649 ** a pointer to a different index for which an index key has just been
89650 ** computed into register regPrior.  If the current pIdx index is generating
89651 ** its key into the same sequence of registers and if pPrior and pIdx share
89652 ** a column in common, then the register corresponding to that column already
89653 ** holds the correct value and the loading of that register is skipped.
89654 ** This optimization is helpful when doing a DELETE or an INTEGRITY_CHECK
89655 ** on a table with multiple indices, and especially with the ROWID or
89656 ** PRIMARY KEY columns of the index.
89657 */
89658 SQLITE_PRIVATE int sqlite3GenerateIndexKey(
89659   Parse *pParse,       /* Parsing context */
89660   Index *pIdx,         /* The index for which to generate a key */
89661   int iDataCur,        /* Cursor number from which to take column data */
89662   int regOut,          /* Put the new key into this register if not 0 */
89663   int prefixOnly,      /* Compute only a unique prefix of the key */
89664   int *piPartIdxLabel, /* OUT: Jump to this label to skip partial index */
89665   Index *pPrior,       /* Previously generated index key */
89666   int regPrior         /* Register holding previous generated key */
89667 ){
89668   Vdbe *v = pParse->pVdbe;
89669   int j;
89670   Table *pTab = pIdx->pTable;
89671   int regBase;
89672   int nCol;
89673 
89674   if( piPartIdxLabel ){
89675     if( pIdx->pPartIdxWhere ){
89676       *piPartIdxLabel = sqlite3VdbeMakeLabel(v);
89677       pParse->iPartIdxTab = iDataCur;
89678       sqlite3ExprIfFalse(pParse, pIdx->pPartIdxWhere, *piPartIdxLabel,
89679                          SQLITE_JUMPIFNULL);
89680     }else{
89681       *piPartIdxLabel = 0;
89682     }
89683   }
89684   nCol = (prefixOnly && pIdx->uniqNotNull) ? pIdx->nKeyCol : pIdx->nColumn;
89685   regBase = sqlite3GetTempRange(pParse, nCol);
89686   if( pPrior && (regBase!=regPrior || pPrior->pPartIdxWhere) ) pPrior = 0;
89687   for(j=0; j<nCol; j++){
89688     if( pPrior && pPrior->aiColumn[j]==pIdx->aiColumn[j] ) continue;
89689     sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, pIdx->aiColumn[j],
89690                                     regBase+j);
89691     /* If the column affinity is REAL but the number is an integer, then it
89692     ** might be stored in the table as an integer (using a compact
89693     ** representation) then converted to REAL by an OP_RealAffinity opcode.
89694     ** But we are getting ready to store this value back into an index, where
89695     ** it should be converted by to INTEGER again.  So omit the OP_RealAffinity
89696     ** opcode if it is present */
89697     sqlite3VdbeDeletePriorOpcode(v, OP_RealAffinity);
89698   }
89699   if( regOut ){
89700     sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nCol, regOut);
89701   }
89702   sqlite3ReleaseTempRange(pParse, regBase, nCol);
89703   return regBase;
89704 }
89705 
89706 /************** End of delete.c **********************************************/
89707 /************** Begin file func.c ********************************************/
89708 /*
89709 ** 2002 February 23
89710 **
89711 ** The author disclaims copyright to this source code.  In place of
89712 ** a legal notice, here is a blessing:
89713 **
89714 **    May you do good and not evil.
89715 **    May you find forgiveness for yourself and forgive others.
89716 **    May you share freely, never taking more than you give.
89717 **
89718 *************************************************************************
89719 ** This file contains the C functions that implement various SQL
89720 ** functions of SQLite.
89721 **
89722 ** There is only one exported symbol in this file - the function
89723 ** sqliteRegisterBuildinFunctions() found at the bottom of the file.
89724 ** All other code has file scope.
89725 */
89726 /* #include <stdlib.h> */
89727 /* #include <assert.h> */
89728 
89729 /*
89730 ** Return the collating function associated with a function.
89731 */
89732 static CollSeq *sqlite3GetFuncCollSeq(sqlite3_context *context){
89733   return context->pColl;
89734 }
89735 
89736 /*
89737 ** Indicate that the accumulator load should be skipped on this
89738 ** iteration of the aggregate loop.
89739 */
89740 static void sqlite3SkipAccumulatorLoad(sqlite3_context *context){
89741   context->skipFlag = 1;
89742 }
89743 
89744 /*
89745 ** Implementation of the non-aggregate min() and max() functions
89746 */
89747 static void minmaxFunc(
89748   sqlite3_context *context,
89749   int argc,
89750   sqlite3_value **argv
89751 ){
89752   int i;
89753   int mask;    /* 0 for min() or 0xffffffff for max() */
89754   int iBest;
89755   CollSeq *pColl;
89756 
89757   assert( argc>1 );
89758   mask = sqlite3_user_data(context)==0 ? 0 : -1;
89759   pColl = sqlite3GetFuncCollSeq(context);
89760   assert( pColl );
89761   assert( mask==-1 || mask==0 );
89762   iBest = 0;
89763   if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
89764   for(i=1; i<argc; i++){
89765     if( sqlite3_value_type(argv[i])==SQLITE_NULL ) return;
89766     if( (sqlite3MemCompare(argv[iBest], argv[i], pColl)^mask)>=0 ){
89767       testcase( mask==0 );
89768       iBest = i;
89769     }
89770   }
89771   sqlite3_result_value(context, argv[iBest]);
89772 }
89773 
89774 /*
89775 ** Return the type of the argument.
89776 */
89777 static void typeofFunc(
89778   sqlite3_context *context,
89779   int NotUsed,
89780   sqlite3_value **argv
89781 ){
89782   const char *z = 0;
89783   UNUSED_PARAMETER(NotUsed);
89784   switch( sqlite3_value_type(argv[0]) ){
89785     case SQLITE_INTEGER: z = "integer"; break;
89786     case SQLITE_TEXT:    z = "text";    break;
89787     case SQLITE_FLOAT:   z = "real";    break;
89788     case SQLITE_BLOB:    z = "blob";    break;
89789     default:             z = "null";    break;
89790   }
89791   sqlite3_result_text(context, z, -1, SQLITE_STATIC);
89792 }
89793 
89794 
89795 /*
89796 ** Implementation of the length() function
89797 */
89798 static void lengthFunc(
89799   sqlite3_context *context,
89800   int argc,
89801   sqlite3_value **argv
89802 ){
89803   int len;
89804 
89805   assert( argc==1 );
89806   UNUSED_PARAMETER(argc);
89807   switch( sqlite3_value_type(argv[0]) ){
89808     case SQLITE_BLOB:
89809     case SQLITE_INTEGER:
89810     case SQLITE_FLOAT: {
89811       sqlite3_result_int(context, sqlite3_value_bytes(argv[0]));
89812       break;
89813     }
89814     case SQLITE_TEXT: {
89815       const unsigned char *z = sqlite3_value_text(argv[0]);
89816       if( z==0 ) return;
89817       len = 0;
89818       while( *z ){
89819         len++;
89820         SQLITE_SKIP_UTF8(z);
89821       }
89822       sqlite3_result_int(context, len);
89823       break;
89824     }
89825     default: {
89826       sqlite3_result_null(context);
89827       break;
89828     }
89829   }
89830 }
89831 
89832 /*
89833 ** Implementation of the abs() function.
89834 **
89835 ** IMP: R-23979-26855 The abs(X) function returns the absolute value of
89836 ** the numeric argument X.
89837 */
89838 static void absFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
89839   assert( argc==1 );
89840   UNUSED_PARAMETER(argc);
89841   switch( sqlite3_value_type(argv[0]) ){
89842     case SQLITE_INTEGER: {
89843       i64 iVal = sqlite3_value_int64(argv[0]);
89844       if( iVal<0 ){
89845         if( iVal==SMALLEST_INT64 ){
89846           /* IMP: R-31676-45509 If X is the integer -9223372036854775808
89847           ** then abs(X) throws an integer overflow error since there is no
89848           ** equivalent positive 64-bit two complement value. */
89849           sqlite3_result_error(context, "integer overflow", -1);
89850           return;
89851         }
89852         iVal = -iVal;
89853       }
89854       sqlite3_result_int64(context, iVal);
89855       break;
89856     }
89857     case SQLITE_NULL: {
89858       /* IMP: R-37434-19929 Abs(X) returns NULL if X is NULL. */
89859       sqlite3_result_null(context);
89860       break;
89861     }
89862     default: {
89863       /* Because sqlite3_value_double() returns 0.0 if the argument is not
89864       ** something that can be converted into a number, we have:
89865       ** IMP: R-57326-31541 Abs(X) return 0.0 if X is a string or blob that
89866       ** cannot be converted to a numeric value.
89867       */
89868       double rVal = sqlite3_value_double(argv[0]);
89869       if( rVal<0 ) rVal = -rVal;
89870       sqlite3_result_double(context, rVal);
89871       break;
89872     }
89873   }
89874 }
89875 
89876 /*
89877 ** Implementation of the instr() function.
89878 **
89879 ** instr(haystack,needle) finds the first occurrence of needle
89880 ** in haystack and returns the number of previous characters plus 1,
89881 ** or 0 if needle does not occur within haystack.
89882 **
89883 ** If both haystack and needle are BLOBs, then the result is one more than
89884 ** the number of bytes in haystack prior to the first occurrence of needle,
89885 ** or 0 if needle never occurs in haystack.
89886 */
89887 static void instrFunc(
89888   sqlite3_context *context,
89889   int argc,
89890   sqlite3_value **argv
89891 ){
89892   const unsigned char *zHaystack;
89893   const unsigned char *zNeedle;
89894   int nHaystack;
89895   int nNeedle;
89896   int typeHaystack, typeNeedle;
89897   int N = 1;
89898   int isText;
89899 
89900   UNUSED_PARAMETER(argc);
89901   typeHaystack = sqlite3_value_type(argv[0]);
89902   typeNeedle = sqlite3_value_type(argv[1]);
89903   if( typeHaystack==SQLITE_NULL || typeNeedle==SQLITE_NULL ) return;
89904   nHaystack = sqlite3_value_bytes(argv[0]);
89905   nNeedle = sqlite3_value_bytes(argv[1]);
89906   if( typeHaystack==SQLITE_BLOB && typeNeedle==SQLITE_BLOB ){
89907     zHaystack = sqlite3_value_blob(argv[0]);
89908     zNeedle = sqlite3_value_blob(argv[1]);
89909     isText = 0;
89910   }else{
89911     zHaystack = sqlite3_value_text(argv[0]);
89912     zNeedle = sqlite3_value_text(argv[1]);
89913     isText = 1;
89914   }
89915   while( nNeedle<=nHaystack && memcmp(zHaystack, zNeedle, nNeedle)!=0 ){
89916     N++;
89917     do{
89918       nHaystack--;
89919       zHaystack++;
89920     }while( isText && (zHaystack[0]&0xc0)==0x80 );
89921   }
89922   if( nNeedle>nHaystack ) N = 0;
89923   sqlite3_result_int(context, N);
89924 }
89925 
89926 /*
89927 ** Implementation of the printf() function.
89928 */
89929 static void printfFunc(
89930   sqlite3_context *context,
89931   int argc,
89932   sqlite3_value **argv
89933 ){
89934   PrintfArguments x;
89935   StrAccum str;
89936   const char *zFormat;
89937   int n;
89938 
89939   if( argc>=1 && (zFormat = (const char*)sqlite3_value_text(argv[0]))!=0 ){
89940     x.nArg = argc-1;
89941     x.nUsed = 0;
89942     x.apArg = argv+1;
89943     sqlite3StrAccumInit(&str, 0, 0, SQLITE_MAX_LENGTH);
89944     str.db = sqlite3_context_db_handle(context);
89945     sqlite3XPrintf(&str, SQLITE_PRINTF_SQLFUNC, zFormat, &x);
89946     n = str.nChar;
89947     sqlite3_result_text(context, sqlite3StrAccumFinish(&str), n,
89948                         SQLITE_DYNAMIC);
89949   }
89950 }
89951 
89952 /*
89953 ** Implementation of the substr() function.
89954 **
89955 ** substr(x,p1,p2)  returns p2 characters of x[] beginning with p1.
89956 ** p1 is 1-indexed.  So substr(x,1,1) returns the first character
89957 ** of x.  If x is text, then we actually count UTF-8 characters.
89958 ** If x is a blob, then we count bytes.
89959 **
89960 ** If p1 is negative, then we begin abs(p1) from the end of x[].
89961 **
89962 ** If p2 is negative, return the p2 characters preceding p1.
89963 */
89964 static void substrFunc(
89965   sqlite3_context *context,
89966   int argc,
89967   sqlite3_value **argv
89968 ){
89969   const unsigned char *z;
89970   const unsigned char *z2;
89971   int len;
89972   int p0type;
89973   i64 p1, p2;
89974   int negP2 = 0;
89975 
89976   assert( argc==3 || argc==2 );
89977   if( sqlite3_value_type(argv[1])==SQLITE_NULL
89978    || (argc==3 && sqlite3_value_type(argv[2])==SQLITE_NULL)
89979   ){
89980     return;
89981   }
89982   p0type = sqlite3_value_type(argv[0]);
89983   p1 = sqlite3_value_int(argv[1]);
89984   if( p0type==SQLITE_BLOB ){
89985     len = sqlite3_value_bytes(argv[0]);
89986     z = sqlite3_value_blob(argv[0]);
89987     if( z==0 ) return;
89988     assert( len==sqlite3_value_bytes(argv[0]) );
89989   }else{
89990     z = sqlite3_value_text(argv[0]);
89991     if( z==0 ) return;
89992     len = 0;
89993     if( p1<0 ){
89994       for(z2=z; *z2; len++){
89995         SQLITE_SKIP_UTF8(z2);
89996       }
89997     }
89998   }
89999   if( argc==3 ){
90000     p2 = sqlite3_value_int(argv[2]);
90001     if( p2<0 ){
90002       p2 = -p2;
90003       negP2 = 1;
90004     }
90005   }else{
90006     p2 = sqlite3_context_db_handle(context)->aLimit[SQLITE_LIMIT_LENGTH];
90007   }
90008   if( p1<0 ){
90009     p1 += len;
90010     if( p1<0 ){
90011       p2 += p1;
90012       if( p2<0 ) p2 = 0;
90013       p1 = 0;
90014     }
90015   }else if( p1>0 ){
90016     p1--;
90017   }else if( p2>0 ){
90018     p2--;
90019   }
90020   if( negP2 ){
90021     p1 -= p2;
90022     if( p1<0 ){
90023       p2 += p1;
90024       p1 = 0;
90025     }
90026   }
90027   assert( p1>=0 && p2>=0 );
90028   if( p0type!=SQLITE_BLOB ){
90029     while( *z && p1 ){
90030       SQLITE_SKIP_UTF8(z);
90031       p1--;
90032     }
90033     for(z2=z; *z2 && p2; p2--){
90034       SQLITE_SKIP_UTF8(z2);
90035     }
90036     sqlite3_result_text(context, (char*)z, (int)(z2-z), SQLITE_TRANSIENT);
90037   }else{
90038     if( p1+p2>len ){
90039       p2 = len-p1;
90040       if( p2<0 ) p2 = 0;
90041     }
90042     sqlite3_result_blob(context, (char*)&z[p1], (int)p2, SQLITE_TRANSIENT);
90043   }
90044 }
90045 
90046 /*
90047 ** Implementation of the round() function
90048 */
90049 #ifndef SQLITE_OMIT_FLOATING_POINT
90050 static void roundFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
90051   int n = 0;
90052   double r;
90053   char *zBuf;
90054   assert( argc==1 || argc==2 );
90055   if( argc==2 ){
90056     if( SQLITE_NULL==sqlite3_value_type(argv[1]) ) return;
90057     n = sqlite3_value_int(argv[1]);
90058     if( n>30 ) n = 30;
90059     if( n<0 ) n = 0;
90060   }
90061   if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
90062   r = sqlite3_value_double(argv[0]);
90063   /* If Y==0 and X will fit in a 64-bit int,
90064   ** handle the rounding directly,
90065   ** otherwise use printf.
90066   */
90067   if( n==0 && r>=0 && r<LARGEST_INT64-1 ){
90068     r = (double)((sqlite_int64)(r+0.5));
90069   }else if( n==0 && r<0 && (-r)<LARGEST_INT64-1 ){
90070     r = -(double)((sqlite_int64)((-r)+0.5));
90071   }else{
90072     zBuf = sqlite3_mprintf("%.*f",n,r);
90073     if( zBuf==0 ){
90074       sqlite3_result_error_nomem(context);
90075       return;
90076     }
90077     sqlite3AtoF(zBuf, &r, sqlite3Strlen30(zBuf), SQLITE_UTF8);
90078     sqlite3_free(zBuf);
90079   }
90080   sqlite3_result_double(context, r);
90081 }
90082 #endif
90083 
90084 /*
90085 ** Allocate nByte bytes of space using sqlite3_malloc(). If the
90086 ** allocation fails, call sqlite3_result_error_nomem() to notify
90087 ** the database handle that malloc() has failed and return NULL.
90088 ** If nByte is larger than the maximum string or blob length, then
90089 ** raise an SQLITE_TOOBIG exception and return NULL.
90090 */
90091 static void *contextMalloc(sqlite3_context *context, i64 nByte){
90092   char *z;
90093   sqlite3 *db = sqlite3_context_db_handle(context);
90094   assert( nByte>0 );
90095   testcase( nByte==db->aLimit[SQLITE_LIMIT_LENGTH] );
90096   testcase( nByte==db->aLimit[SQLITE_LIMIT_LENGTH]+1 );
90097   if( nByte>db->aLimit[SQLITE_LIMIT_LENGTH] ){
90098     sqlite3_result_error_toobig(context);
90099     z = 0;
90100   }else{
90101     z = sqlite3Malloc((int)nByte);
90102     if( !z ){
90103       sqlite3_result_error_nomem(context);
90104     }
90105   }
90106   return z;
90107 }
90108 
90109 /*
90110 ** Implementation of the upper() and lower() SQL functions.
90111 */
90112 static void upperFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
90113   char *z1;
90114   const char *z2;
90115   int i, n;
90116   UNUSED_PARAMETER(argc);
90117   z2 = (char*)sqlite3_value_text(argv[0]);
90118   n = sqlite3_value_bytes(argv[0]);
90119   /* Verify that the call to _bytes() does not invalidate the _text() pointer */
90120   assert( z2==(char*)sqlite3_value_text(argv[0]) );
90121   if( z2 ){
90122     z1 = contextMalloc(context, ((i64)n)+1);
90123     if( z1 ){
90124       for(i=0; i<n; i++){
90125         z1[i] = (char)sqlite3Toupper(z2[i]);
90126       }
90127       sqlite3_result_text(context, z1, n, sqlite3_free);
90128     }
90129   }
90130 }
90131 static void lowerFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
90132   char *z1;
90133   const char *z2;
90134   int i, n;
90135   UNUSED_PARAMETER(argc);
90136   z2 = (char*)sqlite3_value_text(argv[0]);
90137   n = sqlite3_value_bytes(argv[0]);
90138   /* Verify that the call to _bytes() does not invalidate the _text() pointer */
90139   assert( z2==(char*)sqlite3_value_text(argv[0]) );
90140   if( z2 ){
90141     z1 = contextMalloc(context, ((i64)n)+1);
90142     if( z1 ){
90143       for(i=0; i<n; i++){
90144         z1[i] = sqlite3Tolower(z2[i]);
90145       }
90146       sqlite3_result_text(context, z1, n, sqlite3_free);
90147     }
90148   }
90149 }
90150 
90151 /*
90152 ** Some functions like COALESCE() and IFNULL() and UNLIKELY() are implemented
90153 ** as VDBE code so that unused argument values do not have to be computed.
90154 ** However, we still need some kind of function implementation for this
90155 ** routines in the function table.  The noopFunc macro provides this.
90156 ** noopFunc will never be called so it doesn't matter what the implementation
90157 ** is.  We might as well use the "version()" function as a substitute.
90158 */
90159 #define noopFunc versionFunc   /* Substitute function - never called */
90160 
90161 /*
90162 ** Implementation of random().  Return a random integer.
90163 */
90164 static void randomFunc(
90165   sqlite3_context *context,
90166   int NotUsed,
90167   sqlite3_value **NotUsed2
90168 ){
90169   sqlite_int64 r;
90170   UNUSED_PARAMETER2(NotUsed, NotUsed2);
90171   sqlite3_randomness(sizeof(r), &r);
90172   if( r<0 ){
90173     /* We need to prevent a random number of 0x8000000000000000
90174     ** (or -9223372036854775808) since when you do abs() of that
90175     ** number of you get the same value back again.  To do this
90176     ** in a way that is testable, mask the sign bit off of negative
90177     ** values, resulting in a positive value.  Then take the
90178     ** 2s complement of that positive value.  The end result can
90179     ** therefore be no less than -9223372036854775807.
90180     */
90181     r = -(r & LARGEST_INT64);
90182   }
90183   sqlite3_result_int64(context, r);
90184 }
90185 
90186 /*
90187 ** Implementation of randomblob(N).  Return a random blob
90188 ** that is N bytes long.
90189 */
90190 static void randomBlob(
90191   sqlite3_context *context,
90192   int argc,
90193   sqlite3_value **argv
90194 ){
90195   int n;
90196   unsigned char *p;
90197   assert( argc==1 );
90198   UNUSED_PARAMETER(argc);
90199   n = sqlite3_value_int(argv[0]);
90200   if( n<1 ){
90201     n = 1;
90202   }
90203   p = contextMalloc(context, n);
90204   if( p ){
90205     sqlite3_randomness(n, p);
90206     sqlite3_result_blob(context, (char*)p, n, sqlite3_free);
90207   }
90208 }
90209 
90210 /*
90211 ** Implementation of the last_insert_rowid() SQL function.  The return
90212 ** value is the same as the sqlite3_last_insert_rowid() API function.
90213 */
90214 static void last_insert_rowid(
90215   sqlite3_context *context,
90216   int NotUsed,
90217   sqlite3_value **NotUsed2
90218 ){
90219   sqlite3 *db = sqlite3_context_db_handle(context);
90220   UNUSED_PARAMETER2(NotUsed, NotUsed2);
90221   /* IMP: R-51513-12026 The last_insert_rowid() SQL function is a
90222   ** wrapper around the sqlite3_last_insert_rowid() C/C++ interface
90223   ** function. */
90224   sqlite3_result_int64(context, sqlite3_last_insert_rowid(db));
90225 }
90226 
90227 /*
90228 ** Implementation of the changes() SQL function.
90229 **
90230 ** IMP: R-62073-11209 The changes() SQL function is a wrapper
90231 ** around the sqlite3_changes() C/C++ function and hence follows the same
90232 ** rules for counting changes.
90233 */
90234 static void changes(
90235   sqlite3_context *context,
90236   int NotUsed,
90237   sqlite3_value **NotUsed2
90238 ){
90239   sqlite3 *db = sqlite3_context_db_handle(context);
90240   UNUSED_PARAMETER2(NotUsed, NotUsed2);
90241   sqlite3_result_int(context, sqlite3_changes(db));
90242 }
90243 
90244 /*
90245 ** Implementation of the total_changes() SQL function.  The return value is
90246 ** the same as the sqlite3_total_changes() API function.
90247 */
90248 static void total_changes(
90249   sqlite3_context *context,
90250   int NotUsed,
90251   sqlite3_value **NotUsed2
90252 ){
90253   sqlite3 *db = sqlite3_context_db_handle(context);
90254   UNUSED_PARAMETER2(NotUsed, NotUsed2);
90255   /* IMP: R-52756-41993 This function is a wrapper around the
90256   ** sqlite3_total_changes() C/C++ interface. */
90257   sqlite3_result_int(context, sqlite3_total_changes(db));
90258 }
90259 
90260 /*
90261 ** A structure defining how to do GLOB-style comparisons.
90262 */
90263 struct compareInfo {
90264   u8 matchAll;
90265   u8 matchOne;
90266   u8 matchSet;
90267   u8 noCase;
90268 };
90269 
90270 /*
90271 ** For LIKE and GLOB matching on EBCDIC machines, assume that every
90272 ** character is exactly one byte in size.  Also, all characters are
90273 ** able to participate in upper-case-to-lower-case mappings in EBCDIC
90274 ** whereas only characters less than 0x80 do in ASCII.
90275 */
90276 #if defined(SQLITE_EBCDIC)
90277 # define sqlite3Utf8Read(A)    (*((*A)++))
90278 # define GlobUpperToLower(A)   A = sqlite3UpperToLower[A]
90279 #else
90280 # define GlobUpperToLower(A)   if( !((A)&~0x7f) ){ A = sqlite3UpperToLower[A]; }
90281 #endif
90282 
90283 static const struct compareInfo globInfo = { '*', '?', '[', 0 };
90284 /* The correct SQL-92 behavior is for the LIKE operator to ignore
90285 ** case.  Thus  'a' LIKE 'A' would be true. */
90286 static const struct compareInfo likeInfoNorm = { '%', '_',   0, 1 };
90287 /* If SQLITE_CASE_SENSITIVE_LIKE is defined, then the LIKE operator
90288 ** is case sensitive causing 'a' LIKE 'A' to be false */
90289 static const struct compareInfo likeInfoAlt = { '%', '_',   0, 0 };
90290 
90291 /*
90292 ** Compare two UTF-8 strings for equality where the first string can
90293 ** potentially be a "glob" expression.  Return true (1) if they
90294 ** are the same and false (0) if they are different.
90295 **
90296 ** Globbing rules:
90297 **
90298 **      '*'       Matches any sequence of zero or more characters.
90299 **
90300 **      '?'       Matches exactly one character.
90301 **
90302 **     [...]      Matches one character from the enclosed list of
90303 **                characters.
90304 **
90305 **     [^...]     Matches one character not in the enclosed list.
90306 **
90307 ** With the [...] and [^...] matching, a ']' character can be included
90308 ** in the list by making it the first character after '[' or '^'.  A
90309 ** range of characters can be specified using '-'.  Example:
90310 ** "[a-z]" matches any single lower-case letter.  To match a '-', make
90311 ** it the last character in the list.
90312 **
90313 ** This routine is usually quick, but can be N**2 in the worst case.
90314 **
90315 ** Hints: to match '*' or '?', put them in "[]".  Like this:
90316 **
90317 **         abc[*]xyz        Matches "abc*xyz" only
90318 */
90319 static int patternCompare(
90320   const u8 *zPattern,              /* The glob pattern */
90321   const u8 *zString,               /* The string to compare against the glob */
90322   const struct compareInfo *pInfo, /* Information about how to do the compare */
90323   u32 esc                          /* The escape character */
90324 ){
90325   u32 c, c2;
90326   int invert;
90327   int seen;
90328   u8 matchOne = pInfo->matchOne;
90329   u8 matchAll = pInfo->matchAll;
90330   u8 matchSet = pInfo->matchSet;
90331   u8 noCase = pInfo->noCase;
90332   int prevEscape = 0;     /* True if the previous character was 'escape' */
90333 
90334   while( (c = sqlite3Utf8Read(&zPattern))!=0 ){
90335     if( c==matchAll && !prevEscape ){
90336       while( (c=sqlite3Utf8Read(&zPattern)) == matchAll
90337                || c == matchOne ){
90338         if( c==matchOne && sqlite3Utf8Read(&zString)==0 ){
90339           return 0;
90340         }
90341       }
90342       if( c==0 ){
90343         return 1;
90344       }else if( c==esc ){
90345         c = sqlite3Utf8Read(&zPattern);
90346         if( c==0 ){
90347           return 0;
90348         }
90349       }else if( c==matchSet ){
90350         assert( esc==0 );         /* This is GLOB, not LIKE */
90351         assert( matchSet<0x80 );  /* '[' is a single-byte character */
90352         while( *zString && patternCompare(&zPattern[-1],zString,pInfo,esc)==0 ){
90353           SQLITE_SKIP_UTF8(zString);
90354         }
90355         return *zString!=0;
90356       }
90357       while( (c2 = sqlite3Utf8Read(&zString))!=0 ){
90358         if( noCase ){
90359           GlobUpperToLower(c2);
90360           GlobUpperToLower(c);
90361           while( c2 != 0 && c2 != c ){
90362             c2 = sqlite3Utf8Read(&zString);
90363             GlobUpperToLower(c2);
90364           }
90365         }else{
90366           while( c2 != 0 && c2 != c ){
90367             c2 = sqlite3Utf8Read(&zString);
90368           }
90369         }
90370         if( c2==0 ) return 0;
90371         if( patternCompare(zPattern,zString,pInfo,esc) ) return 1;
90372       }
90373       return 0;
90374     }else if( c==matchOne && !prevEscape ){
90375       if( sqlite3Utf8Read(&zString)==0 ){
90376         return 0;
90377       }
90378     }else if( c==matchSet ){
90379       u32 prior_c = 0;
90380       assert( esc==0 );    /* This only occurs for GLOB, not LIKE */
90381       seen = 0;
90382       invert = 0;
90383       c = sqlite3Utf8Read(&zString);
90384       if( c==0 ) return 0;
90385       c2 = sqlite3Utf8Read(&zPattern);
90386       if( c2=='^' ){
90387         invert = 1;
90388         c2 = sqlite3Utf8Read(&zPattern);
90389       }
90390       if( c2==']' ){
90391         if( c==']' ) seen = 1;
90392         c2 = sqlite3Utf8Read(&zPattern);
90393       }
90394       while( c2 && c2!=']' ){
90395         if( c2=='-' && zPattern[0]!=']' && zPattern[0]!=0 && prior_c>0 ){
90396           c2 = sqlite3Utf8Read(&zPattern);
90397           if( c>=prior_c && c<=c2 ) seen = 1;
90398           prior_c = 0;
90399         }else{
90400           if( c==c2 ){
90401             seen = 1;
90402           }
90403           prior_c = c2;
90404         }
90405         c2 = sqlite3Utf8Read(&zPattern);
90406       }
90407       if( c2==0 || (seen ^ invert)==0 ){
90408         return 0;
90409       }
90410     }else if( esc==c && !prevEscape ){
90411       prevEscape = 1;
90412     }else{
90413       c2 = sqlite3Utf8Read(&zString);
90414       if( noCase ){
90415         GlobUpperToLower(c);
90416         GlobUpperToLower(c2);
90417       }
90418       if( c!=c2 ){
90419         return 0;
90420       }
90421       prevEscape = 0;
90422     }
90423   }
90424   return *zString==0;
90425 }
90426 
90427 /*
90428 ** The sqlite3_strglob() interface.
90429 */
90430 SQLITE_API int sqlite3_strglob(const char *zGlobPattern, const char *zString){
90431   return patternCompare((u8*)zGlobPattern, (u8*)zString, &globInfo, 0)==0;
90432 }
90433 
90434 /*
90435 ** Count the number of times that the LIKE operator (or GLOB which is
90436 ** just a variation of LIKE) gets called.  This is used for testing
90437 ** only.
90438 */
90439 #ifdef SQLITE_TEST
90440 SQLITE_API int sqlite3_like_count = 0;
90441 #endif
90442 
90443 
90444 /*
90445 ** Implementation of the like() SQL function.  This function implements
90446 ** the build-in LIKE operator.  The first argument to the function is the
90447 ** pattern and the second argument is the string.  So, the SQL statements:
90448 **
90449 **       A LIKE B
90450 **
90451 ** is implemented as like(B,A).
90452 **
90453 ** This same function (with a different compareInfo structure) computes
90454 ** the GLOB operator.
90455 */
90456 static void likeFunc(
90457   sqlite3_context *context,
90458   int argc,
90459   sqlite3_value **argv
90460 ){
90461   const unsigned char *zA, *zB;
90462   u32 escape = 0;
90463   int nPat;
90464   sqlite3 *db = sqlite3_context_db_handle(context);
90465 
90466   zB = sqlite3_value_text(argv[0]);
90467   zA = sqlite3_value_text(argv[1]);
90468 
90469   /* Limit the length of the LIKE or GLOB pattern to avoid problems
90470   ** of deep recursion and N*N behavior in patternCompare().
90471   */
90472   nPat = sqlite3_value_bytes(argv[0]);
90473   testcase( nPat==db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH] );
90474   testcase( nPat==db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]+1 );
90475   if( nPat > db->aLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH] ){
90476     sqlite3_result_error(context, "LIKE or GLOB pattern too complex", -1);
90477     return;
90478   }
90479   assert( zB==sqlite3_value_text(argv[0]) );  /* Encoding did not change */
90480 
90481   if( argc==3 ){
90482     /* The escape character string must consist of a single UTF-8 character.
90483     ** Otherwise, return an error.
90484     */
90485     const unsigned char *zEsc = sqlite3_value_text(argv[2]);
90486     if( zEsc==0 ) return;
90487     if( sqlite3Utf8CharLen((char*)zEsc, -1)!=1 ){
90488       sqlite3_result_error(context,
90489           "ESCAPE expression must be a single character", -1);
90490       return;
90491     }
90492     escape = sqlite3Utf8Read(&zEsc);
90493   }
90494   if( zA && zB ){
90495     struct compareInfo *pInfo = sqlite3_user_data(context);
90496 #ifdef SQLITE_TEST
90497     sqlite3_like_count++;
90498 #endif
90499 
90500     sqlite3_result_int(context, patternCompare(zB, zA, pInfo, escape));
90501   }
90502 }
90503 
90504 /*
90505 ** Implementation of the NULLIF(x,y) function.  The result is the first
90506 ** argument if the arguments are different.  The result is NULL if the
90507 ** arguments are equal to each other.
90508 */
90509 static void nullifFunc(
90510   sqlite3_context *context,
90511   int NotUsed,
90512   sqlite3_value **argv
90513 ){
90514   CollSeq *pColl = sqlite3GetFuncCollSeq(context);
90515   UNUSED_PARAMETER(NotUsed);
90516   if( sqlite3MemCompare(argv[0], argv[1], pColl)!=0 ){
90517     sqlite3_result_value(context, argv[0]);
90518   }
90519 }
90520 
90521 /*
90522 ** Implementation of the sqlite_version() function.  The result is the version
90523 ** of the SQLite library that is running.
90524 */
90525 static void versionFunc(
90526   sqlite3_context *context,
90527   int NotUsed,
90528   sqlite3_value **NotUsed2
90529 ){
90530   UNUSED_PARAMETER2(NotUsed, NotUsed2);
90531   /* IMP: R-48699-48617 This function is an SQL wrapper around the
90532   ** sqlite3_libversion() C-interface. */
90533   sqlite3_result_text(context, sqlite3_libversion(), -1, SQLITE_STATIC);
90534 }
90535 
90536 /*
90537 ** Implementation of the sqlite_source_id() function. The result is a string
90538 ** that identifies the particular version of the source code used to build
90539 ** SQLite.
90540 */
90541 static void sourceidFunc(
90542   sqlite3_context *context,
90543   int NotUsed,
90544   sqlite3_value **NotUsed2
90545 ){
90546   UNUSED_PARAMETER2(NotUsed, NotUsed2);
90547   /* IMP: R-24470-31136 This function is an SQL wrapper around the
90548   ** sqlite3_sourceid() C interface. */
90549   sqlite3_result_text(context, sqlite3_sourceid(), -1, SQLITE_STATIC);
90550 }
90551 
90552 /*
90553 ** Implementation of the sqlite_log() function.  This is a wrapper around
90554 ** sqlite3_log().  The return value is NULL.  The function exists purely for
90555 ** its side-effects.
90556 */
90557 static void errlogFunc(
90558   sqlite3_context *context,
90559   int argc,
90560   sqlite3_value **argv
90561 ){
90562   UNUSED_PARAMETER(argc);
90563   UNUSED_PARAMETER(context);
90564   sqlite3_log(sqlite3_value_int(argv[0]), "%s", sqlite3_value_text(argv[1]));
90565 }
90566 
90567 /*
90568 ** Implementation of the sqlite_compileoption_used() function.
90569 ** The result is an integer that identifies if the compiler option
90570 ** was used to build SQLite.
90571 */
90572 #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
90573 static void compileoptionusedFunc(
90574   sqlite3_context *context,
90575   int argc,
90576   sqlite3_value **argv
90577 ){
90578   const char *zOptName;
90579   assert( argc==1 );
90580   UNUSED_PARAMETER(argc);
90581   /* IMP: R-39564-36305 The sqlite_compileoption_used() SQL
90582   ** function is a wrapper around the sqlite3_compileoption_used() C/C++
90583   ** function.
90584   */
90585   if( (zOptName = (const char*)sqlite3_value_text(argv[0]))!=0 ){
90586     sqlite3_result_int(context, sqlite3_compileoption_used(zOptName));
90587   }
90588 }
90589 #endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
90590 
90591 /*
90592 ** Implementation of the sqlite_compileoption_get() function.
90593 ** The result is a string that identifies the compiler options
90594 ** used to build SQLite.
90595 */
90596 #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
90597 static void compileoptiongetFunc(
90598   sqlite3_context *context,
90599   int argc,
90600   sqlite3_value **argv
90601 ){
90602   int n;
90603   assert( argc==1 );
90604   UNUSED_PARAMETER(argc);
90605   /* IMP: R-04922-24076 The sqlite_compileoption_get() SQL function
90606   ** is a wrapper around the sqlite3_compileoption_get() C/C++ function.
90607   */
90608   n = sqlite3_value_int(argv[0]);
90609   sqlite3_result_text(context, sqlite3_compileoption_get(n), -1, SQLITE_STATIC);
90610 }
90611 #endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
90612 
90613 /* Array for converting from half-bytes (nybbles) into ASCII hex
90614 ** digits. */
90615 static const char hexdigits[] = {
90616   '0', '1', '2', '3', '4', '5', '6', '7',
90617   '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
90618 };
90619 
90620 /*
90621 ** Implementation of the QUOTE() function.  This function takes a single
90622 ** argument.  If the argument is numeric, the return value is the same as
90623 ** the argument.  If the argument is NULL, the return value is the string
90624 ** "NULL".  Otherwise, the argument is enclosed in single quotes with
90625 ** single-quote escapes.
90626 */
90627 static void quoteFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
90628   assert( argc==1 );
90629   UNUSED_PARAMETER(argc);
90630   switch( sqlite3_value_type(argv[0]) ){
90631     case SQLITE_FLOAT: {
90632       double r1, r2;
90633       char zBuf[50];
90634       r1 = sqlite3_value_double(argv[0]);
90635       sqlite3_snprintf(sizeof(zBuf), zBuf, "%!.15g", r1);
90636       sqlite3AtoF(zBuf, &r2, 20, SQLITE_UTF8);
90637       if( r1!=r2 ){
90638         sqlite3_snprintf(sizeof(zBuf), zBuf, "%!.20e", r1);
90639       }
90640       sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
90641       break;
90642     }
90643     case SQLITE_INTEGER: {
90644       sqlite3_result_value(context, argv[0]);
90645       break;
90646     }
90647     case SQLITE_BLOB: {
90648       char *zText = 0;
90649       char const *zBlob = sqlite3_value_blob(argv[0]);
90650       int nBlob = sqlite3_value_bytes(argv[0]);
90651       assert( zBlob==sqlite3_value_blob(argv[0]) ); /* No encoding change */
90652       zText = (char *)contextMalloc(context, (2*(i64)nBlob)+4);
90653       if( zText ){
90654         int i;
90655         for(i=0; i<nBlob; i++){
90656           zText[(i*2)+2] = hexdigits[(zBlob[i]>>4)&0x0F];
90657           zText[(i*2)+3] = hexdigits[(zBlob[i])&0x0F];
90658         }
90659         zText[(nBlob*2)+2] = '\'';
90660         zText[(nBlob*2)+3] = '\0';
90661         zText[0] = 'X';
90662         zText[1] = '\'';
90663         sqlite3_result_text(context, zText, -1, SQLITE_TRANSIENT);
90664         sqlite3_free(zText);
90665       }
90666       break;
90667     }
90668     case SQLITE_TEXT: {
90669       int i,j;
90670       u64 n;
90671       const unsigned char *zArg = sqlite3_value_text(argv[0]);
90672       char *z;
90673 
90674       if( zArg==0 ) return;
90675       for(i=0, n=0; zArg[i]; i++){ if( zArg[i]=='\'' ) n++; }
90676       z = contextMalloc(context, ((i64)i)+((i64)n)+3);
90677       if( z ){
90678         z[0] = '\'';
90679         for(i=0, j=1; zArg[i]; i++){
90680           z[j++] = zArg[i];
90681           if( zArg[i]=='\'' ){
90682             z[j++] = '\'';
90683           }
90684         }
90685         z[j++] = '\'';
90686         z[j] = 0;
90687         sqlite3_result_text(context, z, j, sqlite3_free);
90688       }
90689       break;
90690     }
90691     default: {
90692       assert( sqlite3_value_type(argv[0])==SQLITE_NULL );
90693       sqlite3_result_text(context, "NULL", 4, SQLITE_STATIC);
90694       break;
90695     }
90696   }
90697 }
90698 
90699 /*
90700 ** The unicode() function.  Return the integer unicode code-point value
90701 ** for the first character of the input string.
90702 */
90703 static void unicodeFunc(
90704   sqlite3_context *context,
90705   int argc,
90706   sqlite3_value **argv
90707 ){
90708   const unsigned char *z = sqlite3_value_text(argv[0]);
90709   (void)argc;
90710   if( z && z[0] ) sqlite3_result_int(context, sqlite3Utf8Read(&z));
90711 }
90712 
90713 /*
90714 ** The char() function takes zero or more arguments, each of which is
90715 ** an integer.  It constructs a string where each character of the string
90716 ** is the unicode character for the corresponding integer argument.
90717 */
90718 static void charFunc(
90719   sqlite3_context *context,
90720   int argc,
90721   sqlite3_value **argv
90722 ){
90723   unsigned char *z, *zOut;
90724   int i;
90725   zOut = z = sqlite3_malloc( argc*4 );
90726   if( z==0 ){
90727     sqlite3_result_error_nomem(context);
90728     return;
90729   }
90730   for(i=0; i<argc; i++){
90731     sqlite3_int64 x;
90732     unsigned c;
90733     x = sqlite3_value_int64(argv[i]);
90734     if( x<0 || x>0x10ffff ) x = 0xfffd;
90735     c = (unsigned)(x & 0x1fffff);
90736     if( c<0x00080 ){
90737       *zOut++ = (u8)(c&0xFF);
90738     }else if( c<0x00800 ){
90739       *zOut++ = 0xC0 + (u8)((c>>6)&0x1F);
90740       *zOut++ = 0x80 + (u8)(c & 0x3F);
90741     }else if( c<0x10000 ){
90742       *zOut++ = 0xE0 + (u8)((c>>12)&0x0F);
90743       *zOut++ = 0x80 + (u8)((c>>6) & 0x3F);
90744       *zOut++ = 0x80 + (u8)(c & 0x3F);
90745     }else{
90746       *zOut++ = 0xF0 + (u8)((c>>18) & 0x07);
90747       *zOut++ = 0x80 + (u8)((c>>12) & 0x3F);
90748       *zOut++ = 0x80 + (u8)((c>>6) & 0x3F);
90749       *zOut++ = 0x80 + (u8)(c & 0x3F);
90750     }                                                    \
90751   }
90752   sqlite3_result_text(context, (char*)z, (int)(zOut-z), sqlite3_free);
90753 }
90754 
90755 /*
90756 ** The hex() function.  Interpret the argument as a blob.  Return
90757 ** a hexadecimal rendering as text.
90758 */
90759 static void hexFunc(
90760   sqlite3_context *context,
90761   int argc,
90762   sqlite3_value **argv
90763 ){
90764   int i, n;
90765   const unsigned char *pBlob;
90766   char *zHex, *z;
90767   assert( argc==1 );
90768   UNUSED_PARAMETER(argc);
90769   pBlob = sqlite3_value_blob(argv[0]);
90770   n = sqlite3_value_bytes(argv[0]);
90771   assert( pBlob==sqlite3_value_blob(argv[0]) );  /* No encoding change */
90772   z = zHex = contextMalloc(context, ((i64)n)*2 + 1);
90773   if( zHex ){
90774     for(i=0; i<n; i++, pBlob++){
90775       unsigned char c = *pBlob;
90776       *(z++) = hexdigits[(c>>4)&0xf];
90777       *(z++) = hexdigits[c&0xf];
90778     }
90779     *z = 0;
90780     sqlite3_result_text(context, zHex, n*2, sqlite3_free);
90781   }
90782 }
90783 
90784 /*
90785 ** The zeroblob(N) function returns a zero-filled blob of size N bytes.
90786 */
90787 static void zeroblobFunc(
90788   sqlite3_context *context,
90789   int argc,
90790   sqlite3_value **argv
90791 ){
90792   i64 n;
90793   sqlite3 *db = sqlite3_context_db_handle(context);
90794   assert( argc==1 );
90795   UNUSED_PARAMETER(argc);
90796   n = sqlite3_value_int64(argv[0]);
90797   testcase( n==db->aLimit[SQLITE_LIMIT_LENGTH] );
90798   testcase( n==db->aLimit[SQLITE_LIMIT_LENGTH]+1 );
90799   if( n>db->aLimit[SQLITE_LIMIT_LENGTH] ){
90800     sqlite3_result_error_toobig(context);
90801   }else{
90802     sqlite3_result_zeroblob(context, (int)n); /* IMP: R-00293-64994 */
90803   }
90804 }
90805 
90806 /*
90807 ** The replace() function.  Three arguments are all strings: call
90808 ** them A, B, and C. The result is also a string which is derived
90809 ** from A by replacing every occurrence of B with C.  The match
90810 ** must be exact.  Collating sequences are not used.
90811 */
90812 static void replaceFunc(
90813   sqlite3_context *context,
90814   int argc,
90815   sqlite3_value **argv
90816 ){
90817   const unsigned char *zStr;        /* The input string A */
90818   const unsigned char *zPattern;    /* The pattern string B */
90819   const unsigned char *zRep;        /* The replacement string C */
90820   unsigned char *zOut;              /* The output */
90821   int nStr;                /* Size of zStr */
90822   int nPattern;            /* Size of zPattern */
90823   int nRep;                /* Size of zRep */
90824   i64 nOut;                /* Maximum size of zOut */
90825   int loopLimit;           /* Last zStr[] that might match zPattern[] */
90826   int i, j;                /* Loop counters */
90827 
90828   assert( argc==3 );
90829   UNUSED_PARAMETER(argc);
90830   zStr = sqlite3_value_text(argv[0]);
90831   if( zStr==0 ) return;
90832   nStr = sqlite3_value_bytes(argv[0]);
90833   assert( zStr==sqlite3_value_text(argv[0]) );  /* No encoding change */
90834   zPattern = sqlite3_value_text(argv[1]);
90835   if( zPattern==0 ){
90836     assert( sqlite3_value_type(argv[1])==SQLITE_NULL
90837             || sqlite3_context_db_handle(context)->mallocFailed );
90838     return;
90839   }
90840   if( zPattern[0]==0 ){
90841     assert( sqlite3_value_type(argv[1])!=SQLITE_NULL );
90842     sqlite3_result_value(context, argv[0]);
90843     return;
90844   }
90845   nPattern = sqlite3_value_bytes(argv[1]);
90846   assert( zPattern==sqlite3_value_text(argv[1]) );  /* No encoding change */
90847   zRep = sqlite3_value_text(argv[2]);
90848   if( zRep==0 ) return;
90849   nRep = sqlite3_value_bytes(argv[2]);
90850   assert( zRep==sqlite3_value_text(argv[2]) );
90851   nOut = nStr + 1;
90852   assert( nOut<SQLITE_MAX_LENGTH );
90853   zOut = contextMalloc(context, (i64)nOut);
90854   if( zOut==0 ){
90855     return;
90856   }
90857   loopLimit = nStr - nPattern;
90858   for(i=j=0; i<=loopLimit; i++){
90859     if( zStr[i]!=zPattern[0] || memcmp(&zStr[i], zPattern, nPattern) ){
90860       zOut[j++] = zStr[i];
90861     }else{
90862       u8 *zOld;
90863       sqlite3 *db = sqlite3_context_db_handle(context);
90864       nOut += nRep - nPattern;
90865       testcase( nOut-1==db->aLimit[SQLITE_LIMIT_LENGTH] );
90866       testcase( nOut-2==db->aLimit[SQLITE_LIMIT_LENGTH] );
90867       if( nOut-1>db->aLimit[SQLITE_LIMIT_LENGTH] ){
90868         sqlite3_result_error_toobig(context);
90869         sqlite3_free(zOut);
90870         return;
90871       }
90872       zOld = zOut;
90873       zOut = sqlite3_realloc(zOut, (int)nOut);
90874       if( zOut==0 ){
90875         sqlite3_result_error_nomem(context);
90876         sqlite3_free(zOld);
90877         return;
90878       }
90879       memcpy(&zOut[j], zRep, nRep);
90880       j += nRep;
90881       i += nPattern-1;
90882     }
90883   }
90884   assert( j+nStr-i+1==nOut );
90885   memcpy(&zOut[j], &zStr[i], nStr-i);
90886   j += nStr - i;
90887   assert( j<=nOut );
90888   zOut[j] = 0;
90889   sqlite3_result_text(context, (char*)zOut, j, sqlite3_free);
90890 }
90891 
90892 /*
90893 ** Implementation of the TRIM(), LTRIM(), and RTRIM() functions.
90894 ** The userdata is 0x1 for left trim, 0x2 for right trim, 0x3 for both.
90895 */
90896 static void trimFunc(
90897   sqlite3_context *context,
90898   int argc,
90899   sqlite3_value **argv
90900 ){
90901   const unsigned char *zIn;         /* Input string */
90902   const unsigned char *zCharSet;    /* Set of characters to trim */
90903   int nIn;                          /* Number of bytes in input */
90904   int flags;                        /* 1: trimleft  2: trimright  3: trim */
90905   int i;                            /* Loop counter */
90906   unsigned char *aLen = 0;          /* Length of each character in zCharSet */
90907   unsigned char **azChar = 0;       /* Individual characters in zCharSet */
90908   int nChar;                        /* Number of characters in zCharSet */
90909 
90910   if( sqlite3_value_type(argv[0])==SQLITE_NULL ){
90911     return;
90912   }
90913   zIn = sqlite3_value_text(argv[0]);
90914   if( zIn==0 ) return;
90915   nIn = sqlite3_value_bytes(argv[0]);
90916   assert( zIn==sqlite3_value_text(argv[0]) );
90917   if( argc==1 ){
90918     static const unsigned char lenOne[] = { 1 };
90919     static unsigned char * const azOne[] = { (u8*)" " };
90920     nChar = 1;
90921     aLen = (u8*)lenOne;
90922     azChar = (unsigned char **)azOne;
90923     zCharSet = 0;
90924   }else if( (zCharSet = sqlite3_value_text(argv[1]))==0 ){
90925     return;
90926   }else{
90927     const unsigned char *z;
90928     for(z=zCharSet, nChar=0; *z; nChar++){
90929       SQLITE_SKIP_UTF8(z);
90930     }
90931     if( nChar>0 ){
90932       azChar = contextMalloc(context, ((i64)nChar)*(sizeof(char*)+1));
90933       if( azChar==0 ){
90934         return;
90935       }
90936       aLen = (unsigned char*)&azChar[nChar];
90937       for(z=zCharSet, nChar=0; *z; nChar++){
90938         azChar[nChar] = (unsigned char *)z;
90939         SQLITE_SKIP_UTF8(z);
90940         aLen[nChar] = (u8)(z - azChar[nChar]);
90941       }
90942     }
90943   }
90944   if( nChar>0 ){
90945     flags = SQLITE_PTR_TO_INT(sqlite3_user_data(context));
90946     if( flags & 1 ){
90947       while( nIn>0 ){
90948         int len = 0;
90949         for(i=0; i<nChar; i++){
90950           len = aLen[i];
90951           if( len<=nIn && memcmp(zIn, azChar[i], len)==0 ) break;
90952         }
90953         if( i>=nChar ) break;
90954         zIn += len;
90955         nIn -= len;
90956       }
90957     }
90958     if( flags & 2 ){
90959       while( nIn>0 ){
90960         int len = 0;
90961         for(i=0; i<nChar; i++){
90962           len = aLen[i];
90963           if( len<=nIn && memcmp(&zIn[nIn-len],azChar[i],len)==0 ) break;
90964         }
90965         if( i>=nChar ) break;
90966         nIn -= len;
90967       }
90968     }
90969     if( zCharSet ){
90970       sqlite3_free(azChar);
90971     }
90972   }
90973   sqlite3_result_text(context, (char*)zIn, nIn, SQLITE_TRANSIENT);
90974 }
90975 
90976 
90977 /* IMP: R-25361-16150 This function is omitted from SQLite by default. It
90978 ** is only available if the SQLITE_SOUNDEX compile-time option is used
90979 ** when SQLite is built.
90980 */
90981 #ifdef SQLITE_SOUNDEX
90982 /*
90983 ** Compute the soundex encoding of a word.
90984 **
90985 ** IMP: R-59782-00072 The soundex(X) function returns a string that is the
90986 ** soundex encoding of the string X.
90987 */
90988 static void soundexFunc(
90989   sqlite3_context *context,
90990   int argc,
90991   sqlite3_value **argv
90992 ){
90993   char zResult[8];
90994   const u8 *zIn;
90995   int i, j;
90996   static const unsigned char iCode[] = {
90997     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
90998     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
90999     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
91000     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
91001     0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
91002     1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
91003     0, 0, 1, 2, 3, 0, 1, 2, 0, 0, 2, 2, 4, 5, 5, 0,
91004     1, 2, 6, 2, 3, 0, 1, 0, 2, 0, 2, 0, 0, 0, 0, 0,
91005   };
91006   assert( argc==1 );
91007   zIn = (u8*)sqlite3_value_text(argv[0]);
91008   if( zIn==0 ) zIn = (u8*)"";
91009   for(i=0; zIn[i] && !sqlite3Isalpha(zIn[i]); i++){}
91010   if( zIn[i] ){
91011     u8 prevcode = iCode[zIn[i]&0x7f];
91012     zResult[0] = sqlite3Toupper(zIn[i]);
91013     for(j=1; j<4 && zIn[i]; i++){
91014       int code = iCode[zIn[i]&0x7f];
91015       if( code>0 ){
91016         if( code!=prevcode ){
91017           prevcode = code;
91018           zResult[j++] = code + '0';
91019         }
91020       }else{
91021         prevcode = 0;
91022       }
91023     }
91024     while( j<4 ){
91025       zResult[j++] = '0';
91026     }
91027     zResult[j] = 0;
91028     sqlite3_result_text(context, zResult, 4, SQLITE_TRANSIENT);
91029   }else{
91030     /* IMP: R-64894-50321 The string "?000" is returned if the argument
91031     ** is NULL or contains no ASCII alphabetic characters. */
91032     sqlite3_result_text(context, "?000", 4, SQLITE_STATIC);
91033   }
91034 }
91035 #endif /* SQLITE_SOUNDEX */
91036 
91037 #ifndef SQLITE_OMIT_LOAD_EXTENSION
91038 /*
91039 ** A function that loads a shared-library extension then returns NULL.
91040 */
91041 static void loadExt(sqlite3_context *context, int argc, sqlite3_value **argv){
91042   const char *zFile = (const char *)sqlite3_value_text(argv[0]);
91043   const char *zProc;
91044   sqlite3 *db = sqlite3_context_db_handle(context);
91045   char *zErrMsg = 0;
91046 
91047   if( argc==2 ){
91048     zProc = (const char *)sqlite3_value_text(argv[1]);
91049   }else{
91050     zProc = 0;
91051   }
91052   if( zFile && sqlite3_load_extension(db, zFile, zProc, &zErrMsg) ){
91053     sqlite3_result_error(context, zErrMsg, -1);
91054     sqlite3_free(zErrMsg);
91055   }
91056 }
91057 #endif
91058 
91059 
91060 /*
91061 ** An instance of the following structure holds the context of a
91062 ** sum() or avg() aggregate computation.
91063 */
91064 typedef struct SumCtx SumCtx;
91065 struct SumCtx {
91066   double rSum;      /* Floating point sum */
91067   i64 iSum;         /* Integer sum */
91068   i64 cnt;          /* Number of elements summed */
91069   u8 overflow;      /* True if integer overflow seen */
91070   u8 approx;        /* True if non-integer value was input to the sum */
91071 };
91072 
91073 /*
91074 ** Routines used to compute the sum, average, and total.
91075 **
91076 ** The SUM() function follows the (broken) SQL standard which means
91077 ** that it returns NULL if it sums over no inputs.  TOTAL returns
91078 ** 0.0 in that case.  In addition, TOTAL always returns a float where
91079 ** SUM might return an integer if it never encounters a floating point
91080 ** value.  TOTAL never fails, but SUM might through an exception if
91081 ** it overflows an integer.
91082 */
91083 static void sumStep(sqlite3_context *context, int argc, sqlite3_value **argv){
91084   SumCtx *p;
91085   int type;
91086   assert( argc==1 );
91087   UNUSED_PARAMETER(argc);
91088   p = sqlite3_aggregate_context(context, sizeof(*p));
91089   type = sqlite3_value_numeric_type(argv[0]);
91090   if( p && type!=SQLITE_NULL ){
91091     p->cnt++;
91092     if( type==SQLITE_INTEGER ){
91093       i64 v = sqlite3_value_int64(argv[0]);
91094       p->rSum += v;
91095       if( (p->approx|p->overflow)==0 && sqlite3AddInt64(&p->iSum, v) ){
91096         p->overflow = 1;
91097       }
91098     }else{
91099       p->rSum += sqlite3_value_double(argv[0]);
91100       p->approx = 1;
91101     }
91102   }
91103 }
91104 static void sumFinalize(sqlite3_context *context){
91105   SumCtx *p;
91106   p = sqlite3_aggregate_context(context, 0);
91107   if( p && p->cnt>0 ){
91108     if( p->overflow ){
91109       sqlite3_result_error(context,"integer overflow",-1);
91110     }else if( p->approx ){
91111       sqlite3_result_double(context, p->rSum);
91112     }else{
91113       sqlite3_result_int64(context, p->iSum);
91114     }
91115   }
91116 }
91117 static void avgFinalize(sqlite3_context *context){
91118   SumCtx *p;
91119   p = sqlite3_aggregate_context(context, 0);
91120   if( p && p->cnt>0 ){
91121     sqlite3_result_double(context, p->rSum/(double)p->cnt);
91122   }
91123 }
91124 static void totalFinalize(sqlite3_context *context){
91125   SumCtx *p;
91126   p = sqlite3_aggregate_context(context, 0);
91127   /* (double)0 In case of SQLITE_OMIT_FLOATING_POINT... */
91128   sqlite3_result_double(context, p ? p->rSum : (double)0);
91129 }
91130 
91131 /*
91132 ** The following structure keeps track of state information for the
91133 ** count() aggregate function.
91134 */
91135 typedef struct CountCtx CountCtx;
91136 struct CountCtx {
91137   i64 n;
91138 };
91139 
91140 /*
91141 ** Routines to implement the count() aggregate function.
91142 */
91143 static void countStep(sqlite3_context *context, int argc, sqlite3_value **argv){
91144   CountCtx *p;
91145   p = sqlite3_aggregate_context(context, sizeof(*p));
91146   if( (argc==0 || SQLITE_NULL!=sqlite3_value_type(argv[0])) && p ){
91147     p->n++;
91148   }
91149 
91150 #ifndef SQLITE_OMIT_DEPRECATED
91151   /* The sqlite3_aggregate_count() function is deprecated.  But just to make
91152   ** sure it still operates correctly, verify that its count agrees with our
91153   ** internal count when using count(*) and when the total count can be
91154   ** expressed as a 32-bit integer. */
91155   assert( argc==1 || p==0 || p->n>0x7fffffff
91156           || p->n==sqlite3_aggregate_count(context) );
91157 #endif
91158 }
91159 static void countFinalize(sqlite3_context *context){
91160   CountCtx *p;
91161   p = sqlite3_aggregate_context(context, 0);
91162   sqlite3_result_int64(context, p ? p->n : 0);
91163 }
91164 
91165 /*
91166 ** Routines to implement min() and max() aggregate functions.
91167 */
91168 static void minmaxStep(
91169   sqlite3_context *context,
91170   int NotUsed,
91171   sqlite3_value **argv
91172 ){
91173   Mem *pArg  = (Mem *)argv[0];
91174   Mem *pBest;
91175   UNUSED_PARAMETER(NotUsed);
91176 
91177   pBest = (Mem *)sqlite3_aggregate_context(context, sizeof(*pBest));
91178   if( !pBest ) return;
91179 
91180   if( sqlite3_value_type(argv[0])==SQLITE_NULL ){
91181     if( pBest->flags ) sqlite3SkipAccumulatorLoad(context);
91182   }else if( pBest->flags ){
91183     int max;
91184     int cmp;
91185     CollSeq *pColl = sqlite3GetFuncCollSeq(context);
91186     /* This step function is used for both the min() and max() aggregates,
91187     ** the only difference between the two being that the sense of the
91188     ** comparison is inverted. For the max() aggregate, the
91189     ** sqlite3_user_data() function returns (void *)-1. For min() it
91190     ** returns (void *)db, where db is the sqlite3* database pointer.
91191     ** Therefore the next statement sets variable 'max' to 1 for the max()
91192     ** aggregate, or 0 for min().
91193     */
91194     max = sqlite3_user_data(context)!=0;
91195     cmp = sqlite3MemCompare(pBest, pArg, pColl);
91196     if( (max && cmp<0) || (!max && cmp>0) ){
91197       sqlite3VdbeMemCopy(pBest, pArg);
91198     }else{
91199       sqlite3SkipAccumulatorLoad(context);
91200     }
91201   }else{
91202     sqlite3VdbeMemCopy(pBest, pArg);
91203   }
91204 }
91205 static void minMaxFinalize(sqlite3_context *context){
91206   sqlite3_value *pRes;
91207   pRes = (sqlite3_value *)sqlite3_aggregate_context(context, 0);
91208   if( pRes ){
91209     if( pRes->flags ){
91210       sqlite3_result_value(context, pRes);
91211     }
91212     sqlite3VdbeMemRelease(pRes);
91213   }
91214 }
91215 
91216 /*
91217 ** group_concat(EXPR, ?SEPARATOR?)
91218 */
91219 static void groupConcatStep(
91220   sqlite3_context *context,
91221   int argc,
91222   sqlite3_value **argv
91223 ){
91224   const char *zVal;
91225   StrAccum *pAccum;
91226   const char *zSep;
91227   int nVal, nSep;
91228   assert( argc==1 || argc==2 );
91229   if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
91230   pAccum = (StrAccum*)sqlite3_aggregate_context(context, sizeof(*pAccum));
91231 
91232   if( pAccum ){
91233     sqlite3 *db = sqlite3_context_db_handle(context);
91234     int firstTerm = pAccum->useMalloc==0;
91235     pAccum->useMalloc = 2;
91236     pAccum->mxAlloc = db->aLimit[SQLITE_LIMIT_LENGTH];
91237     if( !firstTerm ){
91238       if( argc==2 ){
91239         zSep = (char*)sqlite3_value_text(argv[1]);
91240         nSep = sqlite3_value_bytes(argv[1]);
91241       }else{
91242         zSep = ",";
91243         nSep = 1;
91244       }
91245       if( nSep ) sqlite3StrAccumAppend(pAccum, zSep, nSep);
91246     }
91247     zVal = (char*)sqlite3_value_text(argv[0]);
91248     nVal = sqlite3_value_bytes(argv[0]);
91249     if( nVal ) sqlite3StrAccumAppend(pAccum, zVal, nVal);
91250   }
91251 }
91252 static void groupConcatFinalize(sqlite3_context *context){
91253   StrAccum *pAccum;
91254   pAccum = sqlite3_aggregate_context(context, 0);
91255   if( pAccum ){
91256     if( pAccum->accError==STRACCUM_TOOBIG ){
91257       sqlite3_result_error_toobig(context);
91258     }else if( pAccum->accError==STRACCUM_NOMEM ){
91259       sqlite3_result_error_nomem(context);
91260     }else{
91261       sqlite3_result_text(context, sqlite3StrAccumFinish(pAccum), -1,
91262                           sqlite3_free);
91263     }
91264   }
91265 }
91266 
91267 /*
91268 ** This routine does per-connection function registration.  Most
91269 ** of the built-in functions above are part of the global function set.
91270 ** This routine only deals with those that are not global.
91271 */
91272 SQLITE_PRIVATE void sqlite3RegisterBuiltinFunctions(sqlite3 *db){
91273   int rc = sqlite3_overload_function(db, "MATCH", 2);
91274   assert( rc==SQLITE_NOMEM || rc==SQLITE_OK );
91275   if( rc==SQLITE_NOMEM ){
91276     db->mallocFailed = 1;
91277   }
91278 }
91279 
91280 /*
91281 ** Set the LIKEOPT flag on the 2-argument function with the given name.
91282 */
91283 static void setLikeOptFlag(sqlite3 *db, const char *zName, u8 flagVal){
91284   FuncDef *pDef;
91285   pDef = sqlite3FindFunction(db, zName, sqlite3Strlen30(zName),
91286                              2, SQLITE_UTF8, 0);
91287   if( ALWAYS(pDef) ){
91288     pDef->funcFlags |= flagVal;
91289   }
91290 }
91291 
91292 /*
91293 ** Register the built-in LIKE and GLOB functions.  The caseSensitive
91294 ** parameter determines whether or not the LIKE operator is case
91295 ** sensitive.  GLOB is always case sensitive.
91296 */
91297 SQLITE_PRIVATE void sqlite3RegisterLikeFunctions(sqlite3 *db, int caseSensitive){
91298   struct compareInfo *pInfo;
91299   if( caseSensitive ){
91300     pInfo = (struct compareInfo*)&likeInfoAlt;
91301   }else{
91302     pInfo = (struct compareInfo*)&likeInfoNorm;
91303   }
91304   sqlite3CreateFunc(db, "like", 2, SQLITE_UTF8, pInfo, likeFunc, 0, 0, 0);
91305   sqlite3CreateFunc(db, "like", 3, SQLITE_UTF8, pInfo, likeFunc, 0, 0, 0);
91306   sqlite3CreateFunc(db, "glob", 2, SQLITE_UTF8,
91307       (struct compareInfo*)&globInfo, likeFunc, 0, 0, 0);
91308   setLikeOptFlag(db, "glob", SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE);
91309   setLikeOptFlag(db, "like",
91310       caseSensitive ? (SQLITE_FUNC_LIKE | SQLITE_FUNC_CASE) : SQLITE_FUNC_LIKE);
91311 }
91312 
91313 /*
91314 ** pExpr points to an expression which implements a function.  If
91315 ** it is appropriate to apply the LIKE optimization to that function
91316 ** then set aWc[0] through aWc[2] to the wildcard characters and
91317 ** return TRUE.  If the function is not a LIKE-style function then
91318 ** return FALSE.
91319 */
91320 SQLITE_PRIVATE int sqlite3IsLikeFunction(sqlite3 *db, Expr *pExpr, int *pIsNocase, char *aWc){
91321   FuncDef *pDef;
91322   if( pExpr->op!=TK_FUNCTION
91323    || !pExpr->x.pList
91324    || pExpr->x.pList->nExpr!=2
91325   ){
91326     return 0;
91327   }
91328   assert( !ExprHasProperty(pExpr, EP_xIsSelect) );
91329   pDef = sqlite3FindFunction(db, pExpr->u.zToken,
91330                              sqlite3Strlen30(pExpr->u.zToken),
91331                              2, SQLITE_UTF8, 0);
91332   if( NEVER(pDef==0) || (pDef->funcFlags & SQLITE_FUNC_LIKE)==0 ){
91333     return 0;
91334   }
91335 
91336   /* The memcpy() statement assumes that the wildcard characters are
91337   ** the first three statements in the compareInfo structure.  The
91338   ** asserts() that follow verify that assumption
91339   */
91340   memcpy(aWc, pDef->pUserData, 3);
91341   assert( (char*)&likeInfoAlt == (char*)&likeInfoAlt.matchAll );
91342   assert( &((char*)&likeInfoAlt)[1] == (char*)&likeInfoAlt.matchOne );
91343   assert( &((char*)&likeInfoAlt)[2] == (char*)&likeInfoAlt.matchSet );
91344   *pIsNocase = (pDef->funcFlags & SQLITE_FUNC_CASE)==0;
91345   return 1;
91346 }
91347 
91348 /*
91349 ** All all of the FuncDef structures in the aBuiltinFunc[] array above
91350 ** to the global function hash table.  This occurs at start-time (as
91351 ** a consequence of calling sqlite3_initialize()).
91352 **
91353 ** After this routine runs
91354 */
91355 SQLITE_PRIVATE void sqlite3RegisterGlobalFunctions(void){
91356   /*
91357   ** The following array holds FuncDef structures for all of the functions
91358   ** defined in this file.
91359   **
91360   ** The array cannot be constant since changes are made to the
91361   ** FuncDef.pHash elements at start-time.  The elements of this array
91362   ** are read-only after initialization is complete.
91363   */
91364   static SQLITE_WSD FuncDef aBuiltinFunc[] = {
91365     FUNCTION(ltrim,              1, 1, 0, trimFunc         ),
91366     FUNCTION(ltrim,              2, 1, 0, trimFunc         ),
91367     FUNCTION(rtrim,              1, 2, 0, trimFunc         ),
91368     FUNCTION(rtrim,              2, 2, 0, trimFunc         ),
91369     FUNCTION(trim,               1, 3, 0, trimFunc         ),
91370     FUNCTION(trim,               2, 3, 0, trimFunc         ),
91371     FUNCTION(min,               -1, 0, 1, minmaxFunc       ),
91372     FUNCTION(min,                0, 0, 1, 0                ),
91373     AGGREGATE(min,               1, 0, 1, minmaxStep,      minMaxFinalize ),
91374     FUNCTION(max,               -1, 1, 1, minmaxFunc       ),
91375     FUNCTION(max,                0, 1, 1, 0                ),
91376     AGGREGATE(max,               1, 1, 1, minmaxStep,      minMaxFinalize ),
91377     FUNCTION2(typeof,            1, 0, 0, typeofFunc,  SQLITE_FUNC_TYPEOF),
91378     FUNCTION2(length,            1, 0, 0, lengthFunc,  SQLITE_FUNC_LENGTH),
91379     FUNCTION(instr,              2, 0, 0, instrFunc        ),
91380     FUNCTION(substr,             2, 0, 0, substrFunc       ),
91381     FUNCTION(substr,             3, 0, 0, substrFunc       ),
91382     FUNCTION(printf,            -1, 0, 0, printfFunc       ),
91383     FUNCTION(unicode,            1, 0, 0, unicodeFunc      ),
91384     FUNCTION(char,              -1, 0, 0, charFunc         ),
91385     FUNCTION(abs,                1, 0, 0, absFunc          ),
91386 #ifndef SQLITE_OMIT_FLOATING_POINT
91387     FUNCTION(round,              1, 0, 0, roundFunc        ),
91388     FUNCTION(round,              2, 0, 0, roundFunc        ),
91389 #endif
91390     FUNCTION(upper,              1, 0, 0, upperFunc        ),
91391     FUNCTION(lower,              1, 0, 0, lowerFunc        ),
91392     FUNCTION(coalesce,           1, 0, 0, 0                ),
91393     FUNCTION(coalesce,           0, 0, 0, 0                ),
91394     FUNCTION2(coalesce,         -1, 0, 0, noopFunc,  SQLITE_FUNC_COALESCE),
91395     FUNCTION(hex,                1, 0, 0, hexFunc          ),
91396     FUNCTION2(ifnull,            2, 0, 0, noopFunc,  SQLITE_FUNC_COALESCE),
91397     FUNCTION2(unlikely,          1, 0, 0, noopFunc,  SQLITE_FUNC_UNLIKELY),
91398     FUNCTION2(likelihood,        2, 0, 0, noopFunc,  SQLITE_FUNC_UNLIKELY),
91399     VFUNCTION(random,            0, 0, 0, randomFunc       ),
91400     VFUNCTION(randomblob,        1, 0, 0, randomBlob       ),
91401     FUNCTION(nullif,             2, 0, 1, nullifFunc       ),
91402     FUNCTION(sqlite_version,     0, 0, 0, versionFunc      ),
91403     FUNCTION(sqlite_source_id,   0, 0, 0, sourceidFunc     ),
91404     FUNCTION(sqlite_log,         2, 0, 0, errlogFunc       ),
91405 #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
91406     FUNCTION(sqlite_compileoption_used,1, 0, 0, compileoptionusedFunc  ),
91407     FUNCTION(sqlite_compileoption_get, 1, 0, 0, compileoptiongetFunc  ),
91408 #endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
91409     FUNCTION(quote,              1, 0, 0, quoteFunc        ),
91410     VFUNCTION(last_insert_rowid, 0, 0, 0, last_insert_rowid),
91411     VFUNCTION(changes,           0, 0, 0, changes          ),
91412     VFUNCTION(total_changes,     0, 0, 0, total_changes    ),
91413     FUNCTION(replace,            3, 0, 0, replaceFunc      ),
91414     FUNCTION(zeroblob,           1, 0, 0, zeroblobFunc     ),
91415   #ifdef SQLITE_SOUNDEX
91416     FUNCTION(soundex,            1, 0, 0, soundexFunc      ),
91417   #endif
91418   #ifndef SQLITE_OMIT_LOAD_EXTENSION
91419     FUNCTION(load_extension,     1, 0, 0, loadExt          ),
91420     FUNCTION(load_extension,     2, 0, 0, loadExt          ),
91421   #endif
91422     AGGREGATE(sum,               1, 0, 0, sumStep,         sumFinalize    ),
91423     AGGREGATE(total,             1, 0, 0, sumStep,         totalFinalize    ),
91424     AGGREGATE(avg,               1, 0, 0, sumStep,         avgFinalize    ),
91425  /* AGGREGATE(count,             0, 0, 0, countStep,       countFinalize  ), */
91426     {0,SQLITE_UTF8|SQLITE_FUNC_COUNT,0,0,0,countStep,countFinalize,"count",0,0},
91427     AGGREGATE(count,             1, 0, 0, countStep,       countFinalize  ),
91428     AGGREGATE(group_concat,      1, 0, 0, groupConcatStep, groupConcatFinalize),
91429     AGGREGATE(group_concat,      2, 0, 0, groupConcatStep, groupConcatFinalize),
91430 
91431     LIKEFUNC(glob, 2, &globInfo, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE),
91432   #ifdef SQLITE_CASE_SENSITIVE_LIKE
91433     LIKEFUNC(like, 2, &likeInfoAlt, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE),
91434     LIKEFUNC(like, 3, &likeInfoAlt, SQLITE_FUNC_LIKE|SQLITE_FUNC_CASE),
91435   #else
91436     LIKEFUNC(like, 2, &likeInfoNorm, SQLITE_FUNC_LIKE),
91437     LIKEFUNC(like, 3, &likeInfoNorm, SQLITE_FUNC_LIKE),
91438   #endif
91439   };
91440 
91441   int i;
91442   FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions);
91443   FuncDef *aFunc = (FuncDef*)&GLOBAL(FuncDef, aBuiltinFunc);
91444 
91445   for(i=0; i<ArraySize(aBuiltinFunc); i++){
91446     sqlite3FuncDefInsert(pHash, &aFunc[i]);
91447   }
91448   sqlite3RegisterDateTimeFunctions();
91449 #ifndef SQLITE_OMIT_ALTERTABLE
91450   sqlite3AlterFunctions();
91451 #endif
91452 #if defined(SQLITE_ENABLE_STAT3) || defined(SQLITE_ENABLE_STAT4)
91453   sqlite3AnalyzeFunctions();
91454 #endif
91455 }
91456 
91457 /************** End of func.c ************************************************/
91458 /************** Begin file fkey.c ********************************************/
91459 /*
91460 **
91461 ** The author disclaims copyright to this source code.  In place of
91462 ** a legal notice, here is a blessing:
91463 **
91464 **    May you do good and not evil.
91465 **    May you find forgiveness for yourself and forgive others.
91466 **    May you share freely, never taking more than you give.
91467 **
91468 *************************************************************************
91469 ** This file contains code used by the compiler to add foreign key
91470 ** support to compiled SQL statements.
91471 */
91472 
91473 #ifndef SQLITE_OMIT_FOREIGN_KEY
91474 #ifndef SQLITE_OMIT_TRIGGER
91475 
91476 /*
91477 ** Deferred and Immediate FKs
91478 ** --------------------------
91479 **
91480 ** Foreign keys in SQLite come in two flavours: deferred and immediate.
91481 ** If an immediate foreign key constraint is violated,
91482 ** SQLITE_CONSTRAINT_FOREIGNKEY is returned and the current
91483 ** statement transaction rolled back. If a
91484 ** deferred foreign key constraint is violated, no action is taken
91485 ** immediately. However if the application attempts to commit the
91486 ** transaction before fixing the constraint violation, the attempt fails.
91487 **
91488 ** Deferred constraints are implemented using a simple counter associated
91489 ** with the database handle. The counter is set to zero each time a
91490 ** database transaction is opened. Each time a statement is executed
91491 ** that causes a foreign key violation, the counter is incremented. Each
91492 ** time a statement is executed that removes an existing violation from
91493 ** the database, the counter is decremented. When the transaction is
91494 ** committed, the commit fails if the current value of the counter is
91495 ** greater than zero. This scheme has two big drawbacks:
91496 **
91497 **   * When a commit fails due to a deferred foreign key constraint,
91498 **     there is no way to tell which foreign constraint is not satisfied,
91499 **     or which row it is not satisfied for.
91500 **
91501 **   * If the database contains foreign key violations when the
91502 **     transaction is opened, this may cause the mechanism to malfunction.
91503 **
91504 ** Despite these problems, this approach is adopted as it seems simpler
91505 ** than the alternatives.
91506 **
91507 ** INSERT operations:
91508 **
91509 **   I.1) For each FK for which the table is the child table, search
91510 **        the parent table for a match. If none is found increment the
91511 **        constraint counter.
91512 **
91513 **   I.2) For each FK for which the table is the parent table,
91514 **        search the child table for rows that correspond to the new
91515 **        row in the parent table. Decrement the counter for each row
91516 **        found (as the constraint is now satisfied).
91517 **
91518 ** DELETE operations:
91519 **
91520 **   D.1) For each FK for which the table is the child table,
91521 **        search the parent table for a row that corresponds to the
91522 **        deleted row in the child table. If such a row is not found,
91523 **        decrement the counter.
91524 **
91525 **   D.2) For each FK for which the table is the parent table, search
91526 **        the child table for rows that correspond to the deleted row
91527 **        in the parent table. For each found increment the counter.
91528 **
91529 ** UPDATE operations:
91530 **
91531 **   An UPDATE command requires that all 4 steps above are taken, but only
91532 **   for FK constraints for which the affected columns are actually
91533 **   modified (values must be compared at runtime).
91534 **
91535 ** Note that I.1 and D.1 are very similar operations, as are I.2 and D.2.
91536 ** This simplifies the implementation a bit.
91537 **
91538 ** For the purposes of immediate FK constraints, the OR REPLACE conflict
91539 ** resolution is considered to delete rows before the new row is inserted.
91540 ** If a delete caused by OR REPLACE violates an FK constraint, an exception
91541 ** is thrown, even if the FK constraint would be satisfied after the new
91542 ** row is inserted.
91543 **
91544 ** Immediate constraints are usually handled similarly. The only difference
91545 ** is that the counter used is stored as part of each individual statement
91546 ** object (struct Vdbe). If, after the statement has run, its immediate
91547 ** constraint counter is greater than zero,
91548 ** it returns SQLITE_CONSTRAINT_FOREIGNKEY
91549 ** and the statement transaction is rolled back. An exception is an INSERT
91550 ** statement that inserts a single row only (no triggers). In this case,
91551 ** instead of using a counter, an exception is thrown immediately if the
91552 ** INSERT violates a foreign key constraint. This is necessary as such
91553 ** an INSERT does not open a statement transaction.
91554 **
91555 ** TODO: How should dropping a table be handled? How should renaming a
91556 ** table be handled?
91557 **
91558 **
91559 ** Query API Notes
91560 ** ---------------
91561 **
91562 ** Before coding an UPDATE or DELETE row operation, the code-generator
91563 ** for those two operations needs to know whether or not the operation
91564 ** requires any FK processing and, if so, which columns of the original
91565 ** row are required by the FK processing VDBE code (i.e. if FKs were
91566 ** implemented using triggers, which of the old.* columns would be
91567 ** accessed). No information is required by the code-generator before
91568 ** coding an INSERT operation. The functions used by the UPDATE/DELETE
91569 ** generation code to query for this information are:
91570 **
91571 **   sqlite3FkRequired() - Test to see if FK processing is required.
91572 **   sqlite3FkOldmask()  - Query for the set of required old.* columns.
91573 **
91574 **
91575 ** Externally accessible module functions
91576 ** --------------------------------------
91577 **
91578 **   sqlite3FkCheck()    - Check for foreign key violations.
91579 **   sqlite3FkActions()  - Code triggers for ON UPDATE/ON DELETE actions.
91580 **   sqlite3FkDelete()   - Delete an FKey structure.
91581 */
91582 
91583 /*
91584 ** VDBE Calling Convention
91585 ** -----------------------
91586 **
91587 ** Example:
91588 **
91589 **   For the following INSERT statement:
91590 **
91591 **     CREATE TABLE t1(a, b INTEGER PRIMARY KEY, c);
91592 **     INSERT INTO t1 VALUES(1, 2, 3.1);
91593 **
91594 **   Register (x):        2    (type integer)
91595 **   Register (x+1):      1    (type integer)
91596 **   Register (x+2):      NULL (type NULL)
91597 **   Register (x+3):      3.1  (type real)
91598 */
91599 
91600 /*
91601 ** A foreign key constraint requires that the key columns in the parent
91602 ** table are collectively subject to a UNIQUE or PRIMARY KEY constraint.
91603 ** Given that pParent is the parent table for foreign key constraint pFKey,
91604 ** search the schema for a unique index on the parent key columns.
91605 **
91606 ** If successful, zero is returned. If the parent key is an INTEGER PRIMARY
91607 ** KEY column, then output variable *ppIdx is set to NULL. Otherwise, *ppIdx
91608 ** is set to point to the unique index.
91609 **
91610 ** If the parent key consists of a single column (the foreign key constraint
91611 ** is not a composite foreign key), output variable *paiCol is set to NULL.
91612 ** Otherwise, it is set to point to an allocated array of size N, where
91613 ** N is the number of columns in the parent key. The first element of the
91614 ** array is the index of the child table column that is mapped by the FK
91615 ** constraint to the parent table column stored in the left-most column
91616 ** of index *ppIdx. The second element of the array is the index of the
91617 ** child table column that corresponds to the second left-most column of
91618 ** *ppIdx, and so on.
91619 **
91620 ** If the required index cannot be found, either because:
91621 **
91622 **   1) The named parent key columns do not exist, or
91623 **
91624 **   2) The named parent key columns do exist, but are not subject to a
91625 **      UNIQUE or PRIMARY KEY constraint, or
91626 **
91627 **   3) No parent key columns were provided explicitly as part of the
91628 **      foreign key definition, and the parent table does not have a
91629 **      PRIMARY KEY, or
91630 **
91631 **   4) No parent key columns were provided explicitly as part of the
91632 **      foreign key definition, and the PRIMARY KEY of the parent table
91633 **      consists of a a different number of columns to the child key in
91634 **      the child table.
91635 **
91636 ** then non-zero is returned, and a "foreign key mismatch" error loaded
91637 ** into pParse. If an OOM error occurs, non-zero is returned and the
91638 ** pParse->db->mallocFailed flag is set.
91639 */
91640 SQLITE_PRIVATE int sqlite3FkLocateIndex(
91641   Parse *pParse,                  /* Parse context to store any error in */
91642   Table *pParent,                 /* Parent table of FK constraint pFKey */
91643   FKey *pFKey,                    /* Foreign key to find index for */
91644   Index **ppIdx,                  /* OUT: Unique index on parent table */
91645   int **paiCol                    /* OUT: Map of index columns in pFKey */
91646 ){
91647   Index *pIdx = 0;                    /* Value to return via *ppIdx */
91648   int *aiCol = 0;                     /* Value to return via *paiCol */
91649   int nCol = pFKey->nCol;             /* Number of columns in parent key */
91650   char *zKey = pFKey->aCol[0].zCol;   /* Name of left-most parent key column */
91651 
91652   /* The caller is responsible for zeroing output parameters. */
91653   assert( ppIdx && *ppIdx==0 );
91654   assert( !paiCol || *paiCol==0 );
91655   assert( pParse );
91656 
91657   /* If this is a non-composite (single column) foreign key, check if it
91658   ** maps to the INTEGER PRIMARY KEY of table pParent. If so, leave *ppIdx
91659   ** and *paiCol set to zero and return early.
91660   **
91661   ** Otherwise, for a composite foreign key (more than one column), allocate
91662   ** space for the aiCol array (returned via output parameter *paiCol).
91663   ** Non-composite foreign keys do not require the aiCol array.
91664   */
91665   if( nCol==1 ){
91666     /* The FK maps to the IPK if any of the following are true:
91667     **
91668     **   1) There is an INTEGER PRIMARY KEY column and the FK is implicitly
91669     **      mapped to the primary key of table pParent, or
91670     **   2) The FK is explicitly mapped to a column declared as INTEGER
91671     **      PRIMARY KEY.
91672     */
91673     if( pParent->iPKey>=0 ){
91674       if( !zKey ) return 0;
91675       if( !sqlite3StrICmp(pParent->aCol[pParent->iPKey].zName, zKey) ) return 0;
91676     }
91677   }else if( paiCol ){
91678     assert( nCol>1 );
91679     aiCol = (int *)sqlite3DbMallocRaw(pParse->db, nCol*sizeof(int));
91680     if( !aiCol ) return 1;
91681     *paiCol = aiCol;
91682   }
91683 
91684   for(pIdx=pParent->pIndex; pIdx; pIdx=pIdx->pNext){
91685     if( pIdx->nKeyCol==nCol && pIdx->onError!=OE_None ){
91686       /* pIdx is a UNIQUE index (or a PRIMARY KEY) and has the right number
91687       ** of columns. If each indexed column corresponds to a foreign key
91688       ** column of pFKey, then this index is a winner.  */
91689 
91690       if( zKey==0 ){
91691         /* If zKey is NULL, then this foreign key is implicitly mapped to
91692         ** the PRIMARY KEY of table pParent. The PRIMARY KEY index may be
91693         ** identified by the test (Index.autoIndex==2).  */
91694         if( pIdx->autoIndex==2 ){
91695           if( aiCol ){
91696             int i;
91697             for(i=0; i<nCol; i++) aiCol[i] = pFKey->aCol[i].iFrom;
91698           }
91699           break;
91700         }
91701       }else{
91702         /* If zKey is non-NULL, then this foreign key was declared to
91703         ** map to an explicit list of columns in table pParent. Check if this
91704         ** index matches those columns. Also, check that the index uses
91705         ** the default collation sequences for each column. */
91706         int i, j;
91707         for(i=0; i<nCol; i++){
91708           i16 iCol = pIdx->aiColumn[i];     /* Index of column in parent tbl */
91709           char *zDfltColl;                  /* Def. collation for column */
91710           char *zIdxCol;                    /* Name of indexed column */
91711 
91712           /* If the index uses a collation sequence that is different from
91713           ** the default collation sequence for the column, this index is
91714           ** unusable. Bail out early in this case.  */
91715           zDfltColl = pParent->aCol[iCol].zColl;
91716           if( !zDfltColl ){
91717             zDfltColl = "BINARY";
91718           }
91719           if( sqlite3StrICmp(pIdx->azColl[i], zDfltColl) ) break;
91720 
91721           zIdxCol = pParent->aCol[iCol].zName;
91722           for(j=0; j<nCol; j++){
91723             if( sqlite3StrICmp(pFKey->aCol[j].zCol, zIdxCol)==0 ){
91724               if( aiCol ) aiCol[i] = pFKey->aCol[j].iFrom;
91725               break;
91726             }
91727           }
91728           if( j==nCol ) break;
91729         }
91730         if( i==nCol ) break;      /* pIdx is usable */
91731       }
91732     }
91733   }
91734 
91735   if( !pIdx ){
91736     if( !pParse->disableTriggers ){
91737       sqlite3ErrorMsg(pParse,
91738            "foreign key mismatch - \"%w\" referencing \"%w\"",
91739            pFKey->pFrom->zName, pFKey->zTo);
91740     }
91741     sqlite3DbFree(pParse->db, aiCol);
91742     return 1;
91743   }
91744 
91745   *ppIdx = pIdx;
91746   return 0;
91747 }
91748 
91749 /*
91750 ** This function is called when a row is inserted into or deleted from the
91751 ** child table of foreign key constraint pFKey. If an SQL UPDATE is executed
91752 ** on the child table of pFKey, this function is invoked twice for each row
91753 ** affected - once to "delete" the old row, and then again to "insert" the
91754 ** new row.
91755 **
91756 ** Each time it is called, this function generates VDBE code to locate the
91757 ** row in the parent table that corresponds to the row being inserted into
91758 ** or deleted from the child table. If the parent row can be found, no
91759 ** special action is taken. Otherwise, if the parent row can *not* be
91760 ** found in the parent table:
91761 **
91762 **   Operation | FK type   | Action taken
91763 **   --------------------------------------------------------------------------
91764 **   INSERT      immediate   Increment the "immediate constraint counter".
91765 **
91766 **   DELETE      immediate   Decrement the "immediate constraint counter".
91767 **
91768 **   INSERT      deferred    Increment the "deferred constraint counter".
91769 **
91770 **   DELETE      deferred    Decrement the "deferred constraint counter".
91771 **
91772 ** These operations are identified in the comment at the top of this file
91773 ** (fkey.c) as "I.1" and "D.1".
91774 */
91775 static void fkLookupParent(
91776   Parse *pParse,        /* Parse context */
91777   int iDb,              /* Index of database housing pTab */
91778   Table *pTab,          /* Parent table of FK pFKey */
91779   Index *pIdx,          /* Unique index on parent key columns in pTab */
91780   FKey *pFKey,          /* Foreign key constraint */
91781   int *aiCol,           /* Map from parent key columns to child table columns */
91782   int regData,          /* Address of array containing child table row */
91783   int nIncr,            /* Increment constraint counter by this */
91784   int isIgnore          /* If true, pretend pTab contains all NULL values */
91785 ){
91786   int i;                                    /* Iterator variable */
91787   Vdbe *v = sqlite3GetVdbe(pParse);         /* Vdbe to add code to */
91788   int iCur = pParse->nTab - 1;              /* Cursor number to use */
91789   int iOk = sqlite3VdbeMakeLabel(v);        /* jump here if parent key found */
91790 
91791   /* If nIncr is less than zero, then check at runtime if there are any
91792   ** outstanding constraints to resolve. If there are not, there is no need
91793   ** to check if deleting this row resolves any outstanding violations.
91794   **
91795   ** Check if any of the key columns in the child table row are NULL. If
91796   ** any are, then the constraint is considered satisfied. No need to
91797   ** search for a matching row in the parent table.  */
91798   if( nIncr<0 ){
91799     sqlite3VdbeAddOp2(v, OP_FkIfZero, pFKey->isDeferred, iOk);
91800   }
91801   for(i=0; i<pFKey->nCol; i++){
91802     int iReg = aiCol[i] + regData + 1;
91803     sqlite3VdbeAddOp2(v, OP_IsNull, iReg, iOk);
91804   }
91805 
91806   if( isIgnore==0 ){
91807     if( pIdx==0 ){
91808       /* If pIdx is NULL, then the parent key is the INTEGER PRIMARY KEY
91809       ** column of the parent table (table pTab).  */
91810       int iMustBeInt;               /* Address of MustBeInt instruction */
91811       int regTemp = sqlite3GetTempReg(pParse);
91812 
91813       /* Invoke MustBeInt to coerce the child key value to an integer (i.e.
91814       ** apply the affinity of the parent key). If this fails, then there
91815       ** is no matching parent key. Before using MustBeInt, make a copy of
91816       ** the value. Otherwise, the value inserted into the child key column
91817       ** will have INTEGER affinity applied to it, which may not be correct.  */
91818       sqlite3VdbeAddOp2(v, OP_SCopy, aiCol[0]+1+regData, regTemp);
91819       iMustBeInt = sqlite3VdbeAddOp2(v, OP_MustBeInt, regTemp, 0);
91820 
91821       /* If the parent table is the same as the child table, and we are about
91822       ** to increment the constraint-counter (i.e. this is an INSERT operation),
91823       ** then check if the row being inserted matches itself. If so, do not
91824       ** increment the constraint-counter.  */
91825       if( pTab==pFKey->pFrom && nIncr==1 ){
91826         sqlite3VdbeAddOp3(v, OP_Eq, regData, iOk, regTemp);
91827       }
91828 
91829       sqlite3OpenTable(pParse, iCur, iDb, pTab, OP_OpenRead);
91830       sqlite3VdbeAddOp3(v, OP_NotExists, iCur, 0, regTemp);
91831       sqlite3VdbeAddOp2(v, OP_Goto, 0, iOk);
91832       sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
91833       sqlite3VdbeJumpHere(v, iMustBeInt);
91834       sqlite3ReleaseTempReg(pParse, regTemp);
91835     }else{
91836       int nCol = pFKey->nCol;
91837       int regTemp = sqlite3GetTempRange(pParse, nCol);
91838       int regRec = sqlite3GetTempReg(pParse);
91839 
91840       sqlite3VdbeAddOp3(v, OP_OpenRead, iCur, pIdx->tnum, iDb);
91841       sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
91842       for(i=0; i<nCol; i++){
91843         sqlite3VdbeAddOp2(v, OP_Copy, aiCol[i]+1+regData, regTemp+i);
91844       }
91845 
91846       /* If the parent table is the same as the child table, and we are about
91847       ** to increment the constraint-counter (i.e. this is an INSERT operation),
91848       ** then check if the row being inserted matches itself. If so, do not
91849       ** increment the constraint-counter.
91850       **
91851       ** If any of the parent-key values are NULL, then the row cannot match
91852       ** itself. So set JUMPIFNULL to make sure we do the OP_Found if any
91853       ** of the parent-key values are NULL (at this point it is known that
91854       ** none of the child key values are).
91855       */
91856       if( pTab==pFKey->pFrom && nIncr==1 ){
91857         int iJump = sqlite3VdbeCurrentAddr(v) + nCol + 1;
91858         for(i=0; i<nCol; i++){
91859           int iChild = aiCol[i]+1+regData;
91860           int iParent = pIdx->aiColumn[i]+1+regData;
91861           assert( aiCol[i]!=pTab->iPKey );
91862           if( pIdx->aiColumn[i]==pTab->iPKey ){
91863             /* The parent key is a composite key that includes the IPK column */
91864             iParent = regData;
91865           }
91866           sqlite3VdbeAddOp3(v, OP_Ne, iChild, iJump, iParent);
91867           sqlite3VdbeChangeP5(v, SQLITE_JUMPIFNULL);
91868         }
91869         sqlite3VdbeAddOp2(v, OP_Goto, 0, iOk);
91870       }
91871 
91872       sqlite3VdbeAddOp3(v, OP_MakeRecord, regTemp, nCol, regRec);
91873       sqlite3VdbeChangeP4(v, -1, sqlite3IndexAffinityStr(v,pIdx), P4_TRANSIENT);
91874       sqlite3VdbeAddOp4Int(v, OP_Found, iCur, iOk, regRec, 0);
91875 
91876       sqlite3ReleaseTempReg(pParse, regRec);
91877       sqlite3ReleaseTempRange(pParse, regTemp, nCol);
91878     }
91879   }
91880 
91881   if( !pFKey->isDeferred && !(pParse->db->flags & SQLITE_DeferFKs)
91882    && !pParse->pToplevel
91883    && !pParse->isMultiWrite
91884   ){
91885     /* Special case: If this is an INSERT statement that will insert exactly
91886     ** one row into the table, raise a constraint immediately instead of
91887     ** incrementing a counter. This is necessary as the VM code is being
91888     ** generated for will not open a statement transaction.  */
91889     assert( nIncr==1 );
91890     sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_FOREIGNKEY,
91891         OE_Abort, 0, P4_STATIC, P5_ConstraintFK);
91892   }else{
91893     if( nIncr>0 && pFKey->isDeferred==0 ){
91894       sqlite3ParseToplevel(pParse)->mayAbort = 1;
91895     }
91896     sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, nIncr);
91897   }
91898 
91899   sqlite3VdbeResolveLabel(v, iOk);
91900   sqlite3VdbeAddOp1(v, OP_Close, iCur);
91901 }
91902 
91903 
91904 /*
91905 ** Return an Expr object that refers to a memory register corresponding
91906 ** to column iCol of table pTab.
91907 **
91908 ** regBase is the first of an array of register that contains the data
91909 ** for pTab.  regBase itself holds the rowid.  regBase+1 holds the first
91910 ** column.  regBase+2 holds the second column, and so forth.
91911 */
91912 static Expr *exprTableRegister(
91913   Parse *pParse,     /* Parsing and code generating context */
91914   Table *pTab,       /* The table whose content is at r[regBase]... */
91915   int regBase,       /* Contents of table pTab */
91916   i16 iCol           /* Which column of pTab is desired */
91917 ){
91918   Expr *pExpr;
91919   Column *pCol;
91920   const char *zColl;
91921   sqlite3 *db = pParse->db;
91922 
91923   pExpr = sqlite3Expr(db, TK_REGISTER, 0);
91924   if( pExpr ){
91925     if( iCol>=0 && iCol!=pTab->iPKey ){
91926       pCol = &pTab->aCol[iCol];
91927       pExpr->iTable = regBase + iCol + 1;
91928       pExpr->affinity = pCol->affinity;
91929       zColl = pCol->zColl;
91930       if( zColl==0 ) zColl = db->pDfltColl->zName;
91931       pExpr = sqlite3ExprAddCollateString(pParse, pExpr, zColl);
91932     }else{
91933       pExpr->iTable = regBase;
91934       pExpr->affinity = SQLITE_AFF_INTEGER;
91935     }
91936   }
91937   return pExpr;
91938 }
91939 
91940 /*
91941 ** Return an Expr object that refers to column iCol of table pTab which
91942 ** has cursor iCur.
91943 */
91944 static Expr *exprTableColumn(
91945   sqlite3 *db,      /* The database connection */
91946   Table *pTab,      /* The table whose column is desired */
91947   int iCursor,      /* The open cursor on the table */
91948   i16 iCol          /* The column that is wanted */
91949 ){
91950   Expr *pExpr = sqlite3Expr(db, TK_COLUMN, 0);
91951   if( pExpr ){
91952     pExpr->pTab = pTab;
91953     pExpr->iTable = iCursor;
91954     pExpr->iColumn = iCol;
91955   }
91956   return pExpr;
91957 }
91958 
91959 /*
91960 ** This function is called to generate code executed when a row is deleted
91961 ** from the parent table of foreign key constraint pFKey and, if pFKey is
91962 ** deferred, when a row is inserted into the same table. When generating
91963 ** code for an SQL UPDATE operation, this function may be called twice -
91964 ** once to "delete" the old row and once to "insert" the new row.
91965 **
91966 ** The code generated by this function scans through the rows in the child
91967 ** table that correspond to the parent table row being deleted or inserted.
91968 ** For each child row found, one of the following actions is taken:
91969 **
91970 **   Operation | FK type   | Action taken
91971 **   --------------------------------------------------------------------------
91972 **   DELETE      immediate   Increment the "immediate constraint counter".
91973 **                           Or, if the ON (UPDATE|DELETE) action is RESTRICT,
91974 **                           throw a "FOREIGN KEY constraint failed" exception.
91975 **
91976 **   INSERT      immediate   Decrement the "immediate constraint counter".
91977 **
91978 **   DELETE      deferred    Increment the "deferred constraint counter".
91979 **                           Or, if the ON (UPDATE|DELETE) action is RESTRICT,
91980 **                           throw a "FOREIGN KEY constraint failed" exception.
91981 **
91982 **   INSERT      deferred    Decrement the "deferred constraint counter".
91983 **
91984 ** These operations are identified in the comment at the top of this file
91985 ** (fkey.c) as "I.2" and "D.2".
91986 */
91987 static void fkScanChildren(
91988   Parse *pParse,                  /* Parse context */
91989   SrcList *pSrc,                  /* The child table to be scanned */
91990   Table *pTab,                    /* The parent table */
91991   Index *pIdx,                    /* Index on parent covering the foreign key */
91992   FKey *pFKey,                    /* The foreign key linking pSrc to pTab */
91993   int *aiCol,                     /* Map from pIdx cols to child table cols */
91994   int regData,                    /* Parent row data starts here */
91995   int nIncr                       /* Amount to increment deferred counter by */
91996 ){
91997   sqlite3 *db = pParse->db;       /* Database handle */
91998   int i;                          /* Iterator variable */
91999   Expr *pWhere = 0;               /* WHERE clause to scan with */
92000   NameContext sNameContext;       /* Context used to resolve WHERE clause */
92001   WhereInfo *pWInfo;              /* Context used by sqlite3WhereXXX() */
92002   int iFkIfZero = 0;              /* Address of OP_FkIfZero */
92003   Vdbe *v = sqlite3GetVdbe(pParse);
92004 
92005   assert( pIdx==0 || pIdx->pTable==pTab );
92006   assert( pIdx==0 || pIdx->nKeyCol==pFKey->nCol );
92007   assert( pIdx!=0 || pFKey->nCol==1 );
92008   assert( pIdx!=0 || HasRowid(pTab) );
92009 
92010   if( nIncr<0 ){
92011     iFkIfZero = sqlite3VdbeAddOp2(v, OP_FkIfZero, pFKey->isDeferred, 0);
92012   }
92013 
92014   /* Create an Expr object representing an SQL expression like:
92015   **
92016   **   <parent-key1> = <child-key1> AND <parent-key2> = <child-key2> ...
92017   **
92018   ** The collation sequence used for the comparison should be that of
92019   ** the parent key columns. The affinity of the parent key column should
92020   ** be applied to each child key value before the comparison takes place.
92021   */
92022   for(i=0; i<pFKey->nCol; i++){
92023     Expr *pLeft;                  /* Value from parent table row */
92024     Expr *pRight;                 /* Column ref to child table */
92025     Expr *pEq;                    /* Expression (pLeft = pRight) */
92026     i16 iCol;                     /* Index of column in child table */
92027     const char *zCol;             /* Name of column in child table */
92028 
92029     iCol = pIdx ? pIdx->aiColumn[i] : -1;
92030     pLeft = exprTableRegister(pParse, pTab, regData, iCol);
92031     iCol = aiCol ? aiCol[i] : pFKey->aCol[0].iFrom;
92032     assert( iCol>=0 );
92033     zCol = pFKey->pFrom->aCol[iCol].zName;
92034     pRight = sqlite3Expr(db, TK_ID, zCol);
92035     pEq = sqlite3PExpr(pParse, TK_EQ, pLeft, pRight, 0);
92036     pWhere = sqlite3ExprAnd(db, pWhere, pEq);
92037   }
92038 
92039   /* If the child table is the same as the parent table, then add terms
92040   ** to the WHERE clause that prevent this entry from being scanned.
92041   ** The added WHERE clause terms are like this:
92042   **
92043   **     $current_rowid!=rowid
92044   **     NOT( $current_a==a AND $current_b==b AND ... )
92045   **
92046   ** The first form is used for rowid tables.  The second form is used
92047   ** for WITHOUT ROWID tables.  In the second form, the primary key is
92048   ** (a,b,...)
92049   */
92050   if( pTab==pFKey->pFrom && nIncr>0 ){
92051     Expr *pNe;                    /* Expression (pLeft != pRight) */
92052     Expr *pLeft;                  /* Value from parent table row */
92053     Expr *pRight;                 /* Column ref to child table */
92054     if( HasRowid(pTab) ){
92055       pLeft = exprTableRegister(pParse, pTab, regData, -1);
92056       pRight = exprTableColumn(db, pTab, pSrc->a[0].iCursor, -1);
92057       pNe = sqlite3PExpr(pParse, TK_NE, pLeft, pRight, 0);
92058     }else{
92059       Expr *pEq, *pAll = 0;
92060       Index *pPk = sqlite3PrimaryKeyIndex(pTab);
92061       assert( pIdx!=0 );
92062       for(i=0; i<pPk->nKeyCol; i++){
92063         i16 iCol = pIdx->aiColumn[i];
92064         pLeft = exprTableRegister(pParse, pTab, regData, iCol);
92065         pRight = exprTableColumn(db, pTab, pSrc->a[0].iCursor, iCol);
92066         pEq = sqlite3PExpr(pParse, TK_EQ, pLeft, pRight, 0);
92067         pAll = sqlite3ExprAnd(db, pAll, pEq);
92068       }
92069       pNe = sqlite3PExpr(pParse, TK_NOT, pAll, 0, 0);
92070     }
92071     pWhere = sqlite3ExprAnd(db, pWhere, pNe);
92072   }
92073 
92074   /* Resolve the references in the WHERE clause. */
92075   memset(&sNameContext, 0, sizeof(NameContext));
92076   sNameContext.pSrcList = pSrc;
92077   sNameContext.pParse = pParse;
92078   sqlite3ResolveExprNames(&sNameContext, pWhere);
92079 
92080   /* Create VDBE to loop through the entries in pSrc that match the WHERE
92081   ** clause. If the constraint is not deferred, throw an exception for
92082   ** each row found. Otherwise, for deferred constraints, increment the
92083   ** deferred constraint counter by nIncr for each row selected.  */
92084   pWInfo = sqlite3WhereBegin(pParse, pSrc, pWhere, 0, 0, 0, 0);
92085   if( nIncr>0 && pFKey->isDeferred==0 ){
92086     sqlite3ParseToplevel(pParse)->mayAbort = 1;
92087   }
92088   sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, nIncr);
92089   if( pWInfo ){
92090     sqlite3WhereEnd(pWInfo);
92091   }
92092 
92093   /* Clean up the WHERE clause constructed above. */
92094   sqlite3ExprDelete(db, pWhere);
92095   if( iFkIfZero ){
92096     sqlite3VdbeJumpHere(v, iFkIfZero);
92097   }
92098 }
92099 
92100 /*
92101 ** This function returns a linked list of FKey objects (connected by
92102 ** FKey.pNextTo) holding all children of table pTab.  For example,
92103 ** given the following schema:
92104 **
92105 **   CREATE TABLE t1(a PRIMARY KEY);
92106 **   CREATE TABLE t2(b REFERENCES t1(a);
92107 **
92108 ** Calling this function with table "t1" as an argument returns a pointer
92109 ** to the FKey structure representing the foreign key constraint on table
92110 ** "t2". Calling this function with "t2" as the argument would return a
92111 ** NULL pointer (as there are no FK constraints for which t2 is the parent
92112 ** table).
92113 */
92114 SQLITE_PRIVATE FKey *sqlite3FkReferences(Table *pTab){
92115   int nName = sqlite3Strlen30(pTab->zName);
92116   return (FKey *)sqlite3HashFind(&pTab->pSchema->fkeyHash, pTab->zName, nName);
92117 }
92118 
92119 /*
92120 ** The second argument is a Trigger structure allocated by the
92121 ** fkActionTrigger() routine. This function deletes the Trigger structure
92122 ** and all of its sub-components.
92123 **
92124 ** The Trigger structure or any of its sub-components may be allocated from
92125 ** the lookaside buffer belonging to database handle dbMem.
92126 */
92127 static void fkTriggerDelete(sqlite3 *dbMem, Trigger *p){
92128   if( p ){
92129     TriggerStep *pStep = p->step_list;
92130     sqlite3ExprDelete(dbMem, pStep->pWhere);
92131     sqlite3ExprListDelete(dbMem, pStep->pExprList);
92132     sqlite3SelectDelete(dbMem, pStep->pSelect);
92133     sqlite3ExprDelete(dbMem, p->pWhen);
92134     sqlite3DbFree(dbMem, p);
92135   }
92136 }
92137 
92138 /*
92139 ** This function is called to generate code that runs when table pTab is
92140 ** being dropped from the database. The SrcList passed as the second argument
92141 ** to this function contains a single entry guaranteed to resolve to
92142 ** table pTab.
92143 **
92144 ** Normally, no code is required. However, if either
92145 **
92146 **   (a) The table is the parent table of a FK constraint, or
92147 **   (b) The table is the child table of a deferred FK constraint and it is
92148 **       determined at runtime that there are outstanding deferred FK
92149 **       constraint violations in the database,
92150 **
92151 ** then the equivalent of "DELETE FROM <tbl>" is executed before dropping
92152 ** the table from the database. Triggers are disabled while running this
92153 ** DELETE, but foreign key actions are not.
92154 */
92155 SQLITE_PRIVATE void sqlite3FkDropTable(Parse *pParse, SrcList *pName, Table *pTab){
92156   sqlite3 *db = pParse->db;
92157   if( (db->flags&SQLITE_ForeignKeys) && !IsVirtual(pTab) && !pTab->pSelect ){
92158     int iSkip = 0;
92159     Vdbe *v = sqlite3GetVdbe(pParse);
92160 
92161     assert( v );                  /* VDBE has already been allocated */
92162     if( sqlite3FkReferences(pTab)==0 ){
92163       /* Search for a deferred foreign key constraint for which this table
92164       ** is the child table. If one cannot be found, return without
92165       ** generating any VDBE code. If one can be found, then jump over
92166       ** the entire DELETE if there are no outstanding deferred constraints
92167       ** when this statement is run.  */
92168       FKey *p;
92169       for(p=pTab->pFKey; p; p=p->pNextFrom){
92170         if( p->isDeferred || (db->flags & SQLITE_DeferFKs) ) break;
92171       }
92172       if( !p ) return;
92173       iSkip = sqlite3VdbeMakeLabel(v);
92174       sqlite3VdbeAddOp2(v, OP_FkIfZero, 1, iSkip);
92175     }
92176 
92177     pParse->disableTriggers = 1;
92178     sqlite3DeleteFrom(pParse, sqlite3SrcListDup(db, pName, 0), 0);
92179     pParse->disableTriggers = 0;
92180 
92181     /* If the DELETE has generated immediate foreign key constraint
92182     ** violations, halt the VDBE and return an error at this point, before
92183     ** any modifications to the schema are made. This is because statement
92184     ** transactions are not able to rollback schema changes.
92185     **
92186     ** If the SQLITE_DeferFKs flag is set, then this is not required, as
92187     ** the statement transaction will not be rolled back even if FK
92188     ** constraints are violated.
92189     */
92190     if( (db->flags & SQLITE_DeferFKs)==0 ){
92191       sqlite3VdbeAddOp2(v, OP_FkIfZero, 0, sqlite3VdbeCurrentAddr(v)+2);
92192       sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_FOREIGNKEY,
92193           OE_Abort, 0, P4_STATIC, P5_ConstraintFK);
92194     }
92195 
92196     if( iSkip ){
92197       sqlite3VdbeResolveLabel(v, iSkip);
92198     }
92199   }
92200 }
92201 
92202 
92203 /*
92204 ** The second argument points to an FKey object representing a foreign key
92205 ** for which pTab is the child table. An UPDATE statement against pTab
92206 ** is currently being processed. For each column of the table that is
92207 ** actually updated, the corresponding element in the aChange[] array
92208 ** is zero or greater (if a column is unmodified the corresponding element
92209 ** is set to -1). If the rowid column is modified by the UPDATE statement
92210 ** the bChngRowid argument is non-zero.
92211 **
92212 ** This function returns true if any of the columns that are part of the
92213 ** child key for FK constraint *p are modified.
92214 */
92215 static int fkChildIsModified(
92216   Table *pTab,                    /* Table being updated */
92217   FKey *p,                        /* Foreign key for which pTab is the child */
92218   int *aChange,                   /* Array indicating modified columns */
92219   int bChngRowid                  /* True if rowid is modified by this update */
92220 ){
92221   int i;
92222   for(i=0; i<p->nCol; i++){
92223     int iChildKey = p->aCol[i].iFrom;
92224     if( aChange[iChildKey]>=0 ) return 1;
92225     if( iChildKey==pTab->iPKey && bChngRowid ) return 1;
92226   }
92227   return 0;
92228 }
92229 
92230 /*
92231 ** The second argument points to an FKey object representing a foreign key
92232 ** for which pTab is the parent table. An UPDATE statement against pTab
92233 ** is currently being processed. For each column of the table that is
92234 ** actually updated, the corresponding element in the aChange[] array
92235 ** is zero or greater (if a column is unmodified the corresponding element
92236 ** is set to -1). If the rowid column is modified by the UPDATE statement
92237 ** the bChngRowid argument is non-zero.
92238 **
92239 ** This function returns true if any of the columns that are part of the
92240 ** parent key for FK constraint *p are modified.
92241 */
92242 static int fkParentIsModified(
92243   Table *pTab,
92244   FKey *p,
92245   int *aChange,
92246   int bChngRowid
92247 ){
92248   int i;
92249   for(i=0; i<p->nCol; i++){
92250     char *zKey = p->aCol[i].zCol;
92251     int iKey;
92252     for(iKey=0; iKey<pTab->nCol; iKey++){
92253       if( aChange[iKey]>=0 || (iKey==pTab->iPKey && bChngRowid) ){
92254         Column *pCol = &pTab->aCol[iKey];
92255         if( zKey ){
92256           if( 0==sqlite3StrICmp(pCol->zName, zKey) ) return 1;
92257         }else if( pCol->colFlags & COLFLAG_PRIMKEY ){
92258           return 1;
92259         }
92260       }
92261     }
92262   }
92263   return 0;
92264 }
92265 
92266 /*
92267 ** This function is called when inserting, deleting or updating a row of
92268 ** table pTab to generate VDBE code to perform foreign key constraint
92269 ** processing for the operation.
92270 **
92271 ** For a DELETE operation, parameter regOld is passed the index of the
92272 ** first register in an array of (pTab->nCol+1) registers containing the
92273 ** rowid of the row being deleted, followed by each of the column values
92274 ** of the row being deleted, from left to right. Parameter regNew is passed
92275 ** zero in this case.
92276 **
92277 ** For an INSERT operation, regOld is passed zero and regNew is passed the
92278 ** first register of an array of (pTab->nCol+1) registers containing the new
92279 ** row data.
92280 **
92281 ** For an UPDATE operation, this function is called twice. Once before
92282 ** the original record is deleted from the table using the calling convention
92283 ** described for DELETE. Then again after the original record is deleted
92284 ** but before the new record is inserted using the INSERT convention.
92285 */
92286 SQLITE_PRIVATE void sqlite3FkCheck(
92287   Parse *pParse,                  /* Parse context */
92288   Table *pTab,                    /* Row is being deleted from this table */
92289   int regOld,                     /* Previous row data is stored here */
92290   int regNew,                     /* New row data is stored here */
92291   int *aChange,                   /* Array indicating UPDATEd columns (or 0) */
92292   int bChngRowid                  /* True if rowid is UPDATEd */
92293 ){
92294   sqlite3 *db = pParse->db;       /* Database handle */
92295   FKey *pFKey;                    /* Used to iterate through FKs */
92296   int iDb;                        /* Index of database containing pTab */
92297   const char *zDb;                /* Name of database containing pTab */
92298   int isIgnoreErrors = pParse->disableTriggers;
92299 
92300   /* Exactly one of regOld and regNew should be non-zero. */
92301   assert( (regOld==0)!=(regNew==0) );
92302 
92303   /* If foreign-keys are disabled, this function is a no-op. */
92304   if( (db->flags&SQLITE_ForeignKeys)==0 ) return;
92305 
92306   iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
92307   zDb = db->aDb[iDb].zName;
92308 
92309   /* Loop through all the foreign key constraints for which pTab is the
92310   ** child table (the table that the foreign key definition is part of).  */
92311   for(pFKey=pTab->pFKey; pFKey; pFKey=pFKey->pNextFrom){
92312     Table *pTo;                   /* Parent table of foreign key pFKey */
92313     Index *pIdx = 0;              /* Index on key columns in pTo */
92314     int *aiFree = 0;
92315     int *aiCol;
92316     int iCol;
92317     int i;
92318     int isIgnore = 0;
92319 
92320     if( aChange
92321      && sqlite3_stricmp(pTab->zName, pFKey->zTo)!=0
92322      && fkChildIsModified(pTab, pFKey, aChange, bChngRowid)==0
92323     ){
92324       continue;
92325     }
92326 
92327     /* Find the parent table of this foreign key. Also find a unique index
92328     ** on the parent key columns in the parent table. If either of these
92329     ** schema items cannot be located, set an error in pParse and return
92330     ** early.  */
92331     if( pParse->disableTriggers ){
92332       pTo = sqlite3FindTable(db, pFKey->zTo, zDb);
92333     }else{
92334       pTo = sqlite3LocateTable(pParse, 0, pFKey->zTo, zDb);
92335     }
92336     if( !pTo || sqlite3FkLocateIndex(pParse, pTo, pFKey, &pIdx, &aiFree) ){
92337       assert( isIgnoreErrors==0 || (regOld!=0 && regNew==0) );
92338       if( !isIgnoreErrors || db->mallocFailed ) return;
92339       if( pTo==0 ){
92340         /* If isIgnoreErrors is true, then a table is being dropped. In this
92341         ** case SQLite runs a "DELETE FROM xxx" on the table being dropped
92342         ** before actually dropping it in order to check FK constraints.
92343         ** If the parent table of an FK constraint on the current table is
92344         ** missing, behave as if it is empty. i.e. decrement the relevant
92345         ** FK counter for each row of the current table with non-NULL keys.
92346         */
92347         Vdbe *v = sqlite3GetVdbe(pParse);
92348         int iJump = sqlite3VdbeCurrentAddr(v) + pFKey->nCol + 1;
92349         for(i=0; i<pFKey->nCol; i++){
92350           int iReg = pFKey->aCol[i].iFrom + regOld + 1;
92351           sqlite3VdbeAddOp2(v, OP_IsNull, iReg, iJump);
92352         }
92353         sqlite3VdbeAddOp2(v, OP_FkCounter, pFKey->isDeferred, -1);
92354       }
92355       continue;
92356     }
92357     assert( pFKey->nCol==1 || (aiFree && pIdx) );
92358 
92359     if( aiFree ){
92360       aiCol = aiFree;
92361     }else{
92362       iCol = pFKey->aCol[0].iFrom;
92363       aiCol = &iCol;
92364     }
92365     for(i=0; i<pFKey->nCol; i++){
92366       if( aiCol[i]==pTab->iPKey ){
92367         aiCol[i] = -1;
92368       }
92369 #ifndef SQLITE_OMIT_AUTHORIZATION
92370       /* Request permission to read the parent key columns. If the
92371       ** authorization callback returns SQLITE_IGNORE, behave as if any
92372       ** values read from the parent table are NULL. */
92373       if( db->xAuth ){
92374         int rcauth;
92375         char *zCol = pTo->aCol[pIdx ? pIdx->aiColumn[i] : pTo->iPKey].zName;
92376         rcauth = sqlite3AuthReadCol(pParse, pTo->zName, zCol, iDb);
92377         isIgnore = (rcauth==SQLITE_IGNORE);
92378       }
92379 #endif
92380     }
92381 
92382     /* Take a shared-cache advisory read-lock on the parent table. Allocate
92383     ** a cursor to use to search the unique index on the parent key columns
92384     ** in the parent table.  */
92385     sqlite3TableLock(pParse, iDb, pTo->tnum, 0, pTo->zName);
92386     pParse->nTab++;
92387 
92388     if( regOld!=0 ){
92389       /* A row is being removed from the child table. Search for the parent.
92390       ** If the parent does not exist, removing the child row resolves an
92391       ** outstanding foreign key constraint violation. */
92392       fkLookupParent(pParse, iDb, pTo, pIdx, pFKey, aiCol, regOld, -1,isIgnore);
92393     }
92394     if( regNew!=0 ){
92395       /* A row is being added to the child table. If a parent row cannot
92396       ** be found, adding the child row has violated the FK constraint. */
92397       fkLookupParent(pParse, iDb, pTo, pIdx, pFKey, aiCol, regNew, +1,isIgnore);
92398     }
92399 
92400     sqlite3DbFree(db, aiFree);
92401   }
92402 
92403   /* Loop through all the foreign key constraints that refer to this table.
92404   ** (the "child" constraints) */
92405   for(pFKey = sqlite3FkReferences(pTab); pFKey; pFKey=pFKey->pNextTo){
92406     Index *pIdx = 0;              /* Foreign key index for pFKey */
92407     SrcList *pSrc;
92408     int *aiCol = 0;
92409 
92410     if( aChange && fkParentIsModified(pTab, pFKey, aChange, bChngRowid)==0 ){
92411       continue;
92412     }
92413 
92414     if( !pFKey->isDeferred && !(db->flags & SQLITE_DeferFKs)
92415      && !pParse->pToplevel && !pParse->isMultiWrite
92416     ){
92417       assert( regOld==0 && regNew!=0 );
92418       /* Inserting a single row into a parent table cannot cause an immediate
92419       ** foreign key violation. So do nothing in this case.  */
92420       continue;
92421     }
92422 
92423     if( sqlite3FkLocateIndex(pParse, pTab, pFKey, &pIdx, &aiCol) ){
92424       if( !isIgnoreErrors || db->mallocFailed ) return;
92425       continue;
92426     }
92427     assert( aiCol || pFKey->nCol==1 );
92428 
92429     /* Create a SrcList structure containing the child table.  We need the
92430     ** child table as a SrcList for sqlite3WhereBegin() */
92431     pSrc = sqlite3SrcListAppend(db, 0, 0, 0);
92432     if( pSrc ){
92433       struct SrcList_item *pItem = pSrc->a;
92434       pItem->pTab = pFKey->pFrom;
92435       pItem->zName = pFKey->pFrom->zName;
92436       pItem->pTab->nRef++;
92437       pItem->iCursor = pParse->nTab++;
92438 
92439       if( regNew!=0 ){
92440         fkScanChildren(pParse, pSrc, pTab, pIdx, pFKey, aiCol, regNew, -1);
92441       }
92442       if( regOld!=0 ){
92443         /* If there is a RESTRICT action configured for the current operation
92444         ** on the parent table of this FK, then throw an exception
92445         ** immediately if the FK constraint is violated, even if this is a
92446         ** deferred trigger. That's what RESTRICT means. To defer checking
92447         ** the constraint, the FK should specify NO ACTION (represented
92448         ** using OE_None). NO ACTION is the default.  */
92449         fkScanChildren(pParse, pSrc, pTab, pIdx, pFKey, aiCol, regOld, 1);
92450       }
92451       pItem->zName = 0;
92452       sqlite3SrcListDelete(db, pSrc);
92453     }
92454     sqlite3DbFree(db, aiCol);
92455   }
92456 }
92457 
92458 #define COLUMN_MASK(x) (((x)>31) ? 0xffffffff : ((u32)1<<(x)))
92459 
92460 /*
92461 ** This function is called before generating code to update or delete a
92462 ** row contained in table pTab.
92463 */
92464 SQLITE_PRIVATE u32 sqlite3FkOldmask(
92465   Parse *pParse,                  /* Parse context */
92466   Table *pTab                     /* Table being modified */
92467 ){
92468   u32 mask = 0;
92469   if( pParse->db->flags&SQLITE_ForeignKeys ){
92470     FKey *p;
92471     int i;
92472     for(p=pTab->pFKey; p; p=p->pNextFrom){
92473       for(i=0; i<p->nCol; i++) mask |= COLUMN_MASK(p->aCol[i].iFrom);
92474     }
92475     for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
92476       Index *pIdx = 0;
92477       sqlite3FkLocateIndex(pParse, pTab, p, &pIdx, 0);
92478       if( pIdx ){
92479         for(i=0; i<pIdx->nKeyCol; i++) mask |= COLUMN_MASK(pIdx->aiColumn[i]);
92480       }
92481     }
92482   }
92483   return mask;
92484 }
92485 
92486 
92487 /*
92488 ** This function is called before generating code to update or delete a
92489 ** row contained in table pTab. If the operation is a DELETE, then
92490 ** parameter aChange is passed a NULL value. For an UPDATE, aChange points
92491 ** to an array of size N, where N is the number of columns in table pTab.
92492 ** If the i'th column is not modified by the UPDATE, then the corresponding
92493 ** entry in the aChange[] array is set to -1. If the column is modified,
92494 ** the value is 0 or greater. Parameter chngRowid is set to true if the
92495 ** UPDATE statement modifies the rowid fields of the table.
92496 **
92497 ** If any foreign key processing will be required, this function returns
92498 ** true. If there is no foreign key related processing, this function
92499 ** returns false.
92500 */
92501 SQLITE_PRIVATE int sqlite3FkRequired(
92502   Parse *pParse,                  /* Parse context */
92503   Table *pTab,                    /* Table being modified */
92504   int *aChange,                   /* Non-NULL for UPDATE operations */
92505   int chngRowid                   /* True for UPDATE that affects rowid */
92506 ){
92507   if( pParse->db->flags&SQLITE_ForeignKeys ){
92508     if( !aChange ){
92509       /* A DELETE operation. Foreign key processing is required if the
92510       ** table in question is either the child or parent table for any
92511       ** foreign key constraint.  */
92512       return (sqlite3FkReferences(pTab) || pTab->pFKey);
92513     }else{
92514       /* This is an UPDATE. Foreign key processing is only required if the
92515       ** operation modifies one or more child or parent key columns. */
92516       FKey *p;
92517 
92518       /* Check if any child key columns are being modified. */
92519       for(p=pTab->pFKey; p; p=p->pNextFrom){
92520         if( fkChildIsModified(pTab, p, aChange, chngRowid) ) return 1;
92521       }
92522 
92523       /* Check if any parent key columns are being modified. */
92524       for(p=sqlite3FkReferences(pTab); p; p=p->pNextTo){
92525         if( fkParentIsModified(pTab, p, aChange, chngRowid) ) return 1;
92526       }
92527     }
92528   }
92529   return 0;
92530 }
92531 
92532 /*
92533 ** This function is called when an UPDATE or DELETE operation is being
92534 ** compiled on table pTab, which is the parent table of foreign-key pFKey.
92535 ** If the current operation is an UPDATE, then the pChanges parameter is
92536 ** passed a pointer to the list of columns being modified. If it is a
92537 ** DELETE, pChanges is passed a NULL pointer.
92538 **
92539 ** It returns a pointer to a Trigger structure containing a trigger
92540 ** equivalent to the ON UPDATE or ON DELETE action specified by pFKey.
92541 ** If the action is "NO ACTION" or "RESTRICT", then a NULL pointer is
92542 ** returned (these actions require no special handling by the triggers
92543 ** sub-system, code for them is created by fkScanChildren()).
92544 **
92545 ** For example, if pFKey is the foreign key and pTab is table "p" in
92546 ** the following schema:
92547 **
92548 **   CREATE TABLE p(pk PRIMARY KEY);
92549 **   CREATE TABLE c(ck REFERENCES p ON DELETE CASCADE);
92550 **
92551 ** then the returned trigger structure is equivalent to:
92552 **
92553 **   CREATE TRIGGER ... DELETE ON p BEGIN
92554 **     DELETE FROM c WHERE ck = old.pk;
92555 **   END;
92556 **
92557 ** The returned pointer is cached as part of the foreign key object. It
92558 ** is eventually freed along with the rest of the foreign key object by
92559 ** sqlite3FkDelete().
92560 */
92561 static Trigger *fkActionTrigger(
92562   Parse *pParse,                  /* Parse context */
92563   Table *pTab,                    /* Table being updated or deleted from */
92564   FKey *pFKey,                    /* Foreign key to get action for */
92565   ExprList *pChanges              /* Change-list for UPDATE, NULL for DELETE */
92566 ){
92567   sqlite3 *db = pParse->db;       /* Database handle */
92568   int action;                     /* One of OE_None, OE_Cascade etc. */
92569   Trigger *pTrigger;              /* Trigger definition to return */
92570   int iAction = (pChanges!=0);    /* 1 for UPDATE, 0 for DELETE */
92571 
92572   action = pFKey->aAction[iAction];
92573   pTrigger = pFKey->apTrigger[iAction];
92574 
92575   if( action!=OE_None && !pTrigger ){
92576     u8 enableLookaside;           /* Copy of db->lookaside.bEnabled */
92577     char const *zFrom;            /* Name of child table */
92578     int nFrom;                    /* Length in bytes of zFrom */
92579     Index *pIdx = 0;              /* Parent key index for this FK */
92580     int *aiCol = 0;               /* child table cols -> parent key cols */
92581     TriggerStep *pStep = 0;        /* First (only) step of trigger program */
92582     Expr *pWhere = 0;             /* WHERE clause of trigger step */
92583     ExprList *pList = 0;          /* Changes list if ON UPDATE CASCADE */
92584     Select *pSelect = 0;          /* If RESTRICT, "SELECT RAISE(...)" */
92585     int i;                        /* Iterator variable */
92586     Expr *pWhen = 0;              /* WHEN clause for the trigger */
92587 
92588     if( sqlite3FkLocateIndex(pParse, pTab, pFKey, &pIdx, &aiCol) ) return 0;
92589     assert( aiCol || pFKey->nCol==1 );
92590 
92591     for(i=0; i<pFKey->nCol; i++){
92592       Token tOld = { "old", 3 };  /* Literal "old" token */
92593       Token tNew = { "new", 3 };  /* Literal "new" token */
92594       Token tFromCol;             /* Name of column in child table */
92595       Token tToCol;               /* Name of column in parent table */
92596       int iFromCol;               /* Idx of column in child table */
92597       Expr *pEq;                  /* tFromCol = OLD.tToCol */
92598 
92599       iFromCol = aiCol ? aiCol[i] : pFKey->aCol[0].iFrom;
92600       assert( iFromCol>=0 );
92601       tToCol.z = pIdx ? pTab->aCol[pIdx->aiColumn[i]].zName : "oid";
92602       tFromCol.z = pFKey->pFrom->aCol[iFromCol].zName;
92603 
92604       tToCol.n = sqlite3Strlen30(tToCol.z);
92605       tFromCol.n = sqlite3Strlen30(tFromCol.z);
92606 
92607       /* Create the expression "OLD.zToCol = zFromCol". It is important
92608       ** that the "OLD.zToCol" term is on the LHS of the = operator, so
92609       ** that the affinity and collation sequence associated with the
92610       ** parent table are used for the comparison. */
92611       pEq = sqlite3PExpr(pParse, TK_EQ,
92612           sqlite3PExpr(pParse, TK_DOT,
92613             sqlite3PExpr(pParse, TK_ID, 0, 0, &tOld),
92614             sqlite3PExpr(pParse, TK_ID, 0, 0, &tToCol)
92615           , 0),
92616           sqlite3PExpr(pParse, TK_ID, 0, 0, &tFromCol)
92617       , 0);
92618       pWhere = sqlite3ExprAnd(db, pWhere, pEq);
92619 
92620       /* For ON UPDATE, construct the next term of the WHEN clause.
92621       ** The final WHEN clause will be like this:
92622       **
92623       **    WHEN NOT(old.col1 IS new.col1 AND ... AND old.colN IS new.colN)
92624       */
92625       if( pChanges ){
92626         pEq = sqlite3PExpr(pParse, TK_IS,
92627             sqlite3PExpr(pParse, TK_DOT,
92628               sqlite3PExpr(pParse, TK_ID, 0, 0, &tOld),
92629               sqlite3PExpr(pParse, TK_ID, 0, 0, &tToCol),
92630               0),
92631             sqlite3PExpr(pParse, TK_DOT,
92632               sqlite3PExpr(pParse, TK_ID, 0, 0, &tNew),
92633               sqlite3PExpr(pParse, TK_ID, 0, 0, &tToCol),
92634               0),
92635             0);
92636         pWhen = sqlite3ExprAnd(db, pWhen, pEq);
92637       }
92638 
92639       if( action!=OE_Restrict && (action!=OE_Cascade || pChanges) ){
92640         Expr *pNew;
92641         if( action==OE_Cascade ){
92642           pNew = sqlite3PExpr(pParse, TK_DOT,
92643             sqlite3PExpr(pParse, TK_ID, 0, 0, &tNew),
92644             sqlite3PExpr(pParse, TK_ID, 0, 0, &tToCol)
92645           , 0);
92646         }else if( action==OE_SetDflt ){
92647           Expr *pDflt = pFKey->pFrom->aCol[iFromCol].pDflt;
92648           if( pDflt ){
92649             pNew = sqlite3ExprDup(db, pDflt, 0);
92650           }else{
92651             pNew = sqlite3PExpr(pParse, TK_NULL, 0, 0, 0);
92652           }
92653         }else{
92654           pNew = sqlite3PExpr(pParse, TK_NULL, 0, 0, 0);
92655         }
92656         pList = sqlite3ExprListAppend(pParse, pList, pNew);
92657         sqlite3ExprListSetName(pParse, pList, &tFromCol, 0);
92658       }
92659     }
92660     sqlite3DbFree(db, aiCol);
92661 
92662     zFrom = pFKey->pFrom->zName;
92663     nFrom = sqlite3Strlen30(zFrom);
92664 
92665     if( action==OE_Restrict ){
92666       Token tFrom;
92667       Expr *pRaise;
92668 
92669       tFrom.z = zFrom;
92670       tFrom.n = nFrom;
92671       pRaise = sqlite3Expr(db, TK_RAISE, "FOREIGN KEY constraint failed");
92672       if( pRaise ){
92673         pRaise->affinity = OE_Abort;
92674       }
92675       pSelect = sqlite3SelectNew(pParse,
92676           sqlite3ExprListAppend(pParse, 0, pRaise),
92677           sqlite3SrcListAppend(db, 0, &tFrom, 0),
92678           pWhere,
92679           0, 0, 0, 0, 0, 0
92680       );
92681       pWhere = 0;
92682     }
92683 
92684     /* Disable lookaside memory allocation */
92685     enableLookaside = db->lookaside.bEnabled;
92686     db->lookaside.bEnabled = 0;
92687 
92688     pTrigger = (Trigger *)sqlite3DbMallocZero(db,
92689         sizeof(Trigger) +         /* struct Trigger */
92690         sizeof(TriggerStep) +     /* Single step in trigger program */
92691         nFrom + 1                 /* Space for pStep->target.z */
92692     );
92693     if( pTrigger ){
92694       pStep = pTrigger->step_list = (TriggerStep *)&pTrigger[1];
92695       pStep->target.z = (char *)&pStep[1];
92696       pStep->target.n = nFrom;
92697       memcpy((char *)pStep->target.z, zFrom, nFrom);
92698 
92699       pStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
92700       pStep->pExprList = sqlite3ExprListDup(db, pList, EXPRDUP_REDUCE);
92701       pStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
92702       if( pWhen ){
92703         pWhen = sqlite3PExpr(pParse, TK_NOT, pWhen, 0, 0);
92704         pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE);
92705       }
92706     }
92707 
92708     /* Re-enable the lookaside buffer, if it was disabled earlier. */
92709     db->lookaside.bEnabled = enableLookaside;
92710 
92711     sqlite3ExprDelete(db, pWhere);
92712     sqlite3ExprDelete(db, pWhen);
92713     sqlite3ExprListDelete(db, pList);
92714     sqlite3SelectDelete(db, pSelect);
92715     if( db->mallocFailed==1 ){
92716       fkTriggerDelete(db, pTrigger);
92717       return 0;
92718     }
92719     assert( pStep!=0 );
92720 
92721     switch( action ){
92722       case OE_Restrict:
92723         pStep->op = TK_SELECT;
92724         break;
92725       case OE_Cascade:
92726         if( !pChanges ){
92727           pStep->op = TK_DELETE;
92728           break;
92729         }
92730       default:
92731         pStep->op = TK_UPDATE;
92732     }
92733     pStep->pTrig = pTrigger;
92734     pTrigger->pSchema = pTab->pSchema;
92735     pTrigger->pTabSchema = pTab->pSchema;
92736     pFKey->apTrigger[iAction] = pTrigger;
92737     pTrigger->op = (pChanges ? TK_UPDATE : TK_DELETE);
92738   }
92739 
92740   return pTrigger;
92741 }
92742 
92743 /*
92744 ** This function is called when deleting or updating a row to implement
92745 ** any required CASCADE, SET NULL or SET DEFAULT actions.
92746 */
92747 SQLITE_PRIVATE void sqlite3FkActions(
92748   Parse *pParse,                  /* Parse context */
92749   Table *pTab,                    /* Table being updated or deleted from */
92750   ExprList *pChanges,             /* Change-list for UPDATE, NULL for DELETE */
92751   int regOld,                     /* Address of array containing old row */
92752   int *aChange,                   /* Array indicating UPDATEd columns (or 0) */
92753   int bChngRowid                  /* True if rowid is UPDATEd */
92754 ){
92755   /* If foreign-key support is enabled, iterate through all FKs that
92756   ** refer to table pTab. If there is an action associated with the FK
92757   ** for this operation (either update or delete), invoke the associated
92758   ** trigger sub-program.  */
92759   if( pParse->db->flags&SQLITE_ForeignKeys ){
92760     FKey *pFKey;                  /* Iterator variable */
92761     for(pFKey = sqlite3FkReferences(pTab); pFKey; pFKey=pFKey->pNextTo){
92762       if( aChange==0 || fkParentIsModified(pTab, pFKey, aChange, bChngRowid) ){
92763         Trigger *pAct = fkActionTrigger(pParse, pTab, pFKey, pChanges);
92764         if( pAct ){
92765           sqlite3CodeRowTriggerDirect(pParse, pAct, pTab, regOld, OE_Abort, 0);
92766         }
92767       }
92768     }
92769   }
92770 }
92771 
92772 #endif /* ifndef SQLITE_OMIT_TRIGGER */
92773 
92774 /*
92775 ** Free all memory associated with foreign key definitions attached to
92776 ** table pTab. Remove the deleted foreign keys from the Schema.fkeyHash
92777 ** hash table.
92778 */
92779 SQLITE_PRIVATE void sqlite3FkDelete(sqlite3 *db, Table *pTab){
92780   FKey *pFKey;                    /* Iterator variable */
92781   FKey *pNext;                    /* Copy of pFKey->pNextFrom */
92782 
92783   assert( db==0 || sqlite3SchemaMutexHeld(db, 0, pTab->pSchema) );
92784   for(pFKey=pTab->pFKey; pFKey; pFKey=pNext){
92785 
92786     /* Remove the FK from the fkeyHash hash table. */
92787     if( !db || db->pnBytesFreed==0 ){
92788       if( pFKey->pPrevTo ){
92789         pFKey->pPrevTo->pNextTo = pFKey->pNextTo;
92790       }else{
92791         void *p = (void *)pFKey->pNextTo;
92792         const char *z = (p ? pFKey->pNextTo->zTo : pFKey->zTo);
92793         sqlite3HashInsert(&pTab->pSchema->fkeyHash, z, sqlite3Strlen30(z), p);
92794       }
92795       if( pFKey->pNextTo ){
92796         pFKey->pNextTo->pPrevTo = pFKey->pPrevTo;
92797       }
92798     }
92799 
92800     /* EV: R-30323-21917 Each foreign key constraint in SQLite is
92801     ** classified as either immediate or deferred.
92802     */
92803     assert( pFKey->isDeferred==0 || pFKey->isDeferred==1 );
92804 
92805     /* Delete any triggers created to implement actions for this FK. */
92806 #ifndef SQLITE_OMIT_TRIGGER
92807     fkTriggerDelete(db, pFKey->apTrigger[0]);
92808     fkTriggerDelete(db, pFKey->apTrigger[1]);
92809 #endif
92810 
92811     pNext = pFKey->pNextFrom;
92812     sqlite3DbFree(db, pFKey);
92813   }
92814 }
92815 #endif /* ifndef SQLITE_OMIT_FOREIGN_KEY */
92816 
92817 /************** End of fkey.c ************************************************/
92818 /************** Begin file insert.c ******************************************/
92819 /*
92820 ** 2001 September 15
92821 **
92822 ** The author disclaims copyright to this source code.  In place of
92823 ** a legal notice, here is a blessing:
92824 **
92825 **    May you do good and not evil.
92826 **    May you find forgiveness for yourself and forgive others.
92827 **    May you share freely, never taking more than you give.
92828 **
92829 *************************************************************************
92830 ** This file contains C code routines that are called by the parser
92831 ** to handle INSERT statements in SQLite.
92832 */
92833 
92834 /*
92835 ** Generate code that will
92836 **
92837 **   (1) acquire a lock for table pTab then
92838 **   (2) open pTab as cursor iCur.
92839 **
92840 ** If pTab is a WITHOUT ROWID table, then it is the PRIMARY KEY index
92841 ** for that table that is actually opened.
92842 */
92843 SQLITE_PRIVATE void sqlite3OpenTable(
92844   Parse *pParse,  /* Generate code into this VDBE */
92845   int iCur,       /* The cursor number of the table */
92846   int iDb,        /* The database index in sqlite3.aDb[] */
92847   Table *pTab,    /* The table to be opened */
92848   int opcode      /* OP_OpenRead or OP_OpenWrite */
92849 ){
92850   Vdbe *v;
92851   assert( !IsVirtual(pTab) );
92852   v = sqlite3GetVdbe(pParse);
92853   assert( opcode==OP_OpenWrite || opcode==OP_OpenRead );
92854   sqlite3TableLock(pParse, iDb, pTab->tnum,
92855                    (opcode==OP_OpenWrite)?1:0, pTab->zName);
92856   if( HasRowid(pTab) ){
92857     sqlite3VdbeAddOp4Int(v, opcode, iCur, pTab->tnum, iDb, pTab->nCol);
92858     VdbeComment((v, "%s", pTab->zName));
92859   }else{
92860     Index *pPk = sqlite3PrimaryKeyIndex(pTab);
92861     assert( pPk!=0 );
92862     assert( pPk->tnum=pTab->tnum );
92863     sqlite3VdbeAddOp3(v, opcode, iCur, pPk->tnum, iDb);
92864     sqlite3VdbeSetP4KeyInfo(pParse, pPk);
92865     VdbeComment((v, "%s", pTab->zName));
92866   }
92867 }
92868 
92869 /*
92870 ** Return a pointer to the column affinity string associated with index
92871 ** pIdx. A column affinity string has one character for each column in
92872 ** the table, according to the affinity of the column:
92873 **
92874 **  Character      Column affinity
92875 **  ------------------------------
92876 **  'a'            TEXT
92877 **  'b'            NONE
92878 **  'c'            NUMERIC
92879 **  'd'            INTEGER
92880 **  'e'            REAL
92881 **
92882 ** An extra 'd' is appended to the end of the string to cover the
92883 ** rowid that appears as the last column in every index.
92884 **
92885 ** Memory for the buffer containing the column index affinity string
92886 ** is managed along with the rest of the Index structure. It will be
92887 ** released when sqlite3DeleteIndex() is called.
92888 */
92889 SQLITE_PRIVATE const char *sqlite3IndexAffinityStr(Vdbe *v, Index *pIdx){
92890   if( !pIdx->zColAff ){
92891     /* The first time a column affinity string for a particular index is
92892     ** required, it is allocated and populated here. It is then stored as
92893     ** a member of the Index structure for subsequent use.
92894     **
92895     ** The column affinity string will eventually be deleted by
92896     ** sqliteDeleteIndex() when the Index structure itself is cleaned
92897     ** up.
92898     */
92899     int n;
92900     Table *pTab = pIdx->pTable;
92901     sqlite3 *db = sqlite3VdbeDb(v);
92902     pIdx->zColAff = (char *)sqlite3DbMallocRaw(0, pIdx->nColumn+1);
92903     if( !pIdx->zColAff ){
92904       db->mallocFailed = 1;
92905       return 0;
92906     }
92907     for(n=0; n<pIdx->nColumn; n++){
92908       i16 x = pIdx->aiColumn[n];
92909       pIdx->zColAff[n] = x<0 ? SQLITE_AFF_INTEGER : pTab->aCol[x].affinity;
92910     }
92911     pIdx->zColAff[n] = 0;
92912   }
92913 
92914   return pIdx->zColAff;
92915 }
92916 
92917 /*
92918 ** Set P4 of the most recently inserted opcode to a column affinity
92919 ** string for table pTab. A column affinity string has one character
92920 ** for each column indexed by the index, according to the affinity of the
92921 ** column:
92922 **
92923 **  Character      Column affinity
92924 **  ------------------------------
92925 **  'a'            TEXT
92926 **  'b'            NONE
92927 **  'c'            NUMERIC
92928 **  'd'            INTEGER
92929 **  'e'            REAL
92930 */
92931 SQLITE_PRIVATE void sqlite3TableAffinityStr(Vdbe *v, Table *pTab){
92932   /* The first time a column affinity string for a particular table
92933   ** is required, it is allocated and populated here. It is then
92934   ** stored as a member of the Table structure for subsequent use.
92935   **
92936   ** The column affinity string will eventually be deleted by
92937   ** sqlite3DeleteTable() when the Table structure itself is cleaned up.
92938   */
92939   if( !pTab->zColAff ){
92940     char *zColAff;
92941     int i;
92942     sqlite3 *db = sqlite3VdbeDb(v);
92943 
92944     zColAff = (char *)sqlite3DbMallocRaw(0, pTab->nCol+1);
92945     if( !zColAff ){
92946       db->mallocFailed = 1;
92947       return;
92948     }
92949 
92950     for(i=0; i<pTab->nCol; i++){
92951       zColAff[i] = pTab->aCol[i].affinity;
92952     }
92953     zColAff[pTab->nCol] = '\0';
92954 
92955     pTab->zColAff = zColAff;
92956   }
92957 
92958   sqlite3VdbeChangeP4(v, -1, pTab->zColAff, P4_TRANSIENT);
92959 }
92960 
92961 /*
92962 ** Return non-zero if the table pTab in database iDb or any of its indices
92963 ** have been opened at any point in the VDBE program beginning at location
92964 ** iStartAddr throught the end of the program.  This is used to see if
92965 ** a statement of the form  "INSERT INTO <iDb, pTab> SELECT ..." can
92966 ** run without using temporary table for the results of the SELECT.
92967 */
92968 static int readsTable(Parse *p, int iStartAddr, int iDb, Table *pTab){
92969   Vdbe *v = sqlite3GetVdbe(p);
92970   int i;
92971   int iEnd = sqlite3VdbeCurrentAddr(v);
92972 #ifndef SQLITE_OMIT_VIRTUALTABLE
92973   VTable *pVTab = IsVirtual(pTab) ? sqlite3GetVTable(p->db, pTab) : 0;
92974 #endif
92975 
92976   for(i=iStartAddr; i<iEnd; i++){
92977     VdbeOp *pOp = sqlite3VdbeGetOp(v, i);
92978     assert( pOp!=0 );
92979     if( pOp->opcode==OP_OpenRead && pOp->p3==iDb ){
92980       Index *pIndex;
92981       int tnum = pOp->p2;
92982       if( tnum==pTab->tnum ){
92983         return 1;
92984       }
92985       for(pIndex=pTab->pIndex; pIndex; pIndex=pIndex->pNext){
92986         if( tnum==pIndex->tnum ){
92987           return 1;
92988         }
92989       }
92990     }
92991 #ifndef SQLITE_OMIT_VIRTUALTABLE
92992     if( pOp->opcode==OP_VOpen && pOp->p4.pVtab==pVTab ){
92993       assert( pOp->p4.pVtab!=0 );
92994       assert( pOp->p4type==P4_VTAB );
92995       return 1;
92996     }
92997 #endif
92998   }
92999   return 0;
93000 }
93001 
93002 #ifndef SQLITE_OMIT_AUTOINCREMENT
93003 /*
93004 ** Locate or create an AutoincInfo structure associated with table pTab
93005 ** which is in database iDb.  Return the register number for the register
93006 ** that holds the maximum rowid.
93007 **
93008 ** There is at most one AutoincInfo structure per table even if the
93009 ** same table is autoincremented multiple times due to inserts within
93010 ** triggers.  A new AutoincInfo structure is created if this is the
93011 ** first use of table pTab.  On 2nd and subsequent uses, the original
93012 ** AutoincInfo structure is used.
93013 **
93014 ** Three memory locations are allocated:
93015 **
93016 **   (1)  Register to hold the name of the pTab table.
93017 **   (2)  Register to hold the maximum ROWID of pTab.
93018 **   (3)  Register to hold the rowid in sqlite_sequence of pTab
93019 **
93020 ** The 2nd register is the one that is returned.  That is all the
93021 ** insert routine needs to know about.
93022 */
93023 static int autoIncBegin(
93024   Parse *pParse,      /* Parsing context */
93025   int iDb,            /* Index of the database holding pTab */
93026   Table *pTab         /* The table we are writing to */
93027 ){
93028   int memId = 0;      /* Register holding maximum rowid */
93029   if( pTab->tabFlags & TF_Autoincrement ){
93030     Parse *pToplevel = sqlite3ParseToplevel(pParse);
93031     AutoincInfo *pInfo;
93032 
93033     pInfo = pToplevel->pAinc;
93034     while( pInfo && pInfo->pTab!=pTab ){ pInfo = pInfo->pNext; }
93035     if( pInfo==0 ){
93036       pInfo = sqlite3DbMallocRaw(pParse->db, sizeof(*pInfo));
93037       if( pInfo==0 ) return 0;
93038       pInfo->pNext = pToplevel->pAinc;
93039       pToplevel->pAinc = pInfo;
93040       pInfo->pTab = pTab;
93041       pInfo->iDb = iDb;
93042       pToplevel->nMem++;                  /* Register to hold name of table */
93043       pInfo->regCtr = ++pToplevel->nMem;  /* Max rowid register */
93044       pToplevel->nMem++;                  /* Rowid in sqlite_sequence */
93045     }
93046     memId = pInfo->regCtr;
93047   }
93048   return memId;
93049 }
93050 
93051 /*
93052 ** This routine generates code that will initialize all of the
93053 ** register used by the autoincrement tracker.
93054 */
93055 SQLITE_PRIVATE void sqlite3AutoincrementBegin(Parse *pParse){
93056   AutoincInfo *p;            /* Information about an AUTOINCREMENT */
93057   sqlite3 *db = pParse->db;  /* The database connection */
93058   Db *pDb;                   /* Database only autoinc table */
93059   int memId;                 /* Register holding max rowid */
93060   int addr;                  /* A VDBE address */
93061   Vdbe *v = pParse->pVdbe;   /* VDBE under construction */
93062 
93063   /* This routine is never called during trigger-generation.  It is
93064   ** only called from the top-level */
93065   assert( pParse->pTriggerTab==0 );
93066   assert( pParse==sqlite3ParseToplevel(pParse) );
93067 
93068   assert( v );   /* We failed long ago if this is not so */
93069   for(p = pParse->pAinc; p; p = p->pNext){
93070     pDb = &db->aDb[p->iDb];
93071     memId = p->regCtr;
93072     assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) );
93073     sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenRead);
93074     sqlite3VdbeAddOp3(v, OP_Null, 0, memId, memId+1);
93075     addr = sqlite3VdbeCurrentAddr(v);
93076     sqlite3VdbeAddOp4(v, OP_String8, 0, memId-1, 0, p->pTab->zName, 0);
93077     sqlite3VdbeAddOp2(v, OP_Rewind, 0, addr+9);
93078     sqlite3VdbeAddOp3(v, OP_Column, 0, 0, memId);
93079     sqlite3VdbeAddOp3(v, OP_Ne, memId-1, addr+7, memId);
93080     sqlite3VdbeChangeP5(v, SQLITE_JUMPIFNULL);
93081     sqlite3VdbeAddOp2(v, OP_Rowid, 0, memId+1);
93082     sqlite3VdbeAddOp3(v, OP_Column, 0, 1, memId);
93083     sqlite3VdbeAddOp2(v, OP_Goto, 0, addr+9);
93084     sqlite3VdbeAddOp2(v, OP_Next, 0, addr+2);
93085     sqlite3VdbeAddOp2(v, OP_Integer, 0, memId);
93086     sqlite3VdbeAddOp0(v, OP_Close);
93087   }
93088 }
93089 
93090 /*
93091 ** Update the maximum rowid for an autoincrement calculation.
93092 **
93093 ** This routine should be called when the top of the stack holds a
93094 ** new rowid that is about to be inserted.  If that new rowid is
93095 ** larger than the maximum rowid in the memId memory cell, then the
93096 ** memory cell is updated.  The stack is unchanged.
93097 */
93098 static void autoIncStep(Parse *pParse, int memId, int regRowid){
93099   if( memId>0 ){
93100     sqlite3VdbeAddOp2(pParse->pVdbe, OP_MemMax, memId, regRowid);
93101   }
93102 }
93103 
93104 /*
93105 ** This routine generates the code needed to write autoincrement
93106 ** maximum rowid values back into the sqlite_sequence register.
93107 ** Every statement that might do an INSERT into an autoincrement
93108 ** table (either directly or through triggers) needs to call this
93109 ** routine just before the "exit" code.
93110 */
93111 SQLITE_PRIVATE void sqlite3AutoincrementEnd(Parse *pParse){
93112   AutoincInfo *p;
93113   Vdbe *v = pParse->pVdbe;
93114   sqlite3 *db = pParse->db;
93115 
93116   assert( v );
93117   for(p = pParse->pAinc; p; p = p->pNext){
93118     Db *pDb = &db->aDb[p->iDb];
93119     int j1, j2, j3, j4, j5;
93120     int iRec;
93121     int memId = p->regCtr;
93122 
93123     iRec = sqlite3GetTempReg(pParse);
93124     assert( sqlite3SchemaMutexHeld(db, 0, pDb->pSchema) );
93125     sqlite3OpenTable(pParse, 0, p->iDb, pDb->pSchema->pSeqTab, OP_OpenWrite);
93126     j1 = sqlite3VdbeAddOp1(v, OP_NotNull, memId+1);
93127     j2 = sqlite3VdbeAddOp0(v, OP_Rewind);
93128     j3 = sqlite3VdbeAddOp3(v, OP_Column, 0, 0, iRec);
93129     j4 = sqlite3VdbeAddOp3(v, OP_Eq, memId-1, 0, iRec);
93130     sqlite3VdbeAddOp2(v, OP_Next, 0, j3);
93131     sqlite3VdbeJumpHere(v, j2);
93132     sqlite3VdbeAddOp2(v, OP_NewRowid, 0, memId+1);
93133     j5 = sqlite3VdbeAddOp0(v, OP_Goto);
93134     sqlite3VdbeJumpHere(v, j4);
93135     sqlite3VdbeAddOp2(v, OP_Rowid, 0, memId+1);
93136     sqlite3VdbeJumpHere(v, j1);
93137     sqlite3VdbeJumpHere(v, j5);
93138     sqlite3VdbeAddOp3(v, OP_MakeRecord, memId-1, 2, iRec);
93139     sqlite3VdbeAddOp3(v, OP_Insert, 0, iRec, memId+1);
93140     sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
93141     sqlite3VdbeAddOp0(v, OP_Close);
93142     sqlite3ReleaseTempReg(pParse, iRec);
93143   }
93144 }
93145 #else
93146 /*
93147 ** If SQLITE_OMIT_AUTOINCREMENT is defined, then the three routines
93148 ** above are all no-ops
93149 */
93150 # define autoIncBegin(A,B,C) (0)
93151 # define autoIncStep(A,B,C)
93152 #endif /* SQLITE_OMIT_AUTOINCREMENT */
93153 
93154 
93155 /*
93156 ** Generate code for a co-routine that will evaluate a subquery one
93157 ** row at a time.
93158 **
93159 ** The pSelect parameter is the subquery that the co-routine will evaluation.
93160 ** Information about the location of co-routine and the registers it will use
93161 ** is returned by filling in the pDest object.
93162 **
93163 ** Registers are allocated as follows:
93164 **
93165 **   pDest->iSDParm      The register holding the next entry-point of the
93166 **                       co-routine.  Run the co-routine to its next breakpoint
93167 **                       by calling "OP_Yield $X" where $X is pDest->iSDParm.
93168 **
93169 **   pDest->iSDParm+1    The register holding the "completed" flag for the
93170 **                       co-routine. This register is 0 if the previous Yield
93171 **                       generated a new result row, or 1 if the subquery
93172 **                       has completed.  If the Yield is called again
93173 **                       after this register becomes 1, then the VDBE will
93174 **                       halt with an SQLITE_INTERNAL error.
93175 **
93176 **   pDest->iSdst        First result register.
93177 **
93178 **   pDest->nSdst        Number of result registers.
93179 **
93180 ** This routine handles all of the register allocation and fills in the
93181 ** pDest structure appropriately.
93182 **
93183 ** Here is a schematic of the generated code assuming that X is the
93184 ** co-routine entry-point register reg[pDest->iSDParm], that EOF is the
93185 ** completed flag reg[pDest->iSDParm+1], and R and S are the range of
93186 ** registers that hold the result set, reg[pDest->iSdst] through
93187 ** reg[pDest->iSdst+pDest->nSdst-1]:
93188 **
93189 **         X <- A
93190 **         EOF <- 0
93191 **         goto B
93192 **      A: setup for the SELECT
93193 **         loop rows in the SELECT
93194 **           load results into registers R..S
93195 **           yield X
93196 **         end loop
93197 **         cleanup after the SELECT
93198 **         EOF <- 1
93199 **         yield X
93200 **         halt-error
93201 **      B:
93202 **
93203 ** To use this subroutine, the caller generates code as follows:
93204 **
93205 **         [ Co-routine generated by this subroutine, shown above ]
93206 **      S: yield X
93207 **         if EOF goto E
93208 **         if skip this row, goto C
93209 **         if terminate loop, goto E
93210 **         deal with this row
93211 **      C: goto S
93212 **      E:
93213 */
93214 SQLITE_PRIVATE int sqlite3CodeCoroutine(Parse *pParse, Select *pSelect, SelectDest *pDest){
93215   int regYield;       /* Register holding co-routine entry-point */
93216   int regEof;         /* Register holding co-routine completion flag */
93217   int addrTop;        /* Top of the co-routine */
93218   int j1;             /* Jump instruction */
93219   int rc;             /* Result code */
93220   Vdbe *v;            /* VDBE under construction */
93221 
93222   regYield = ++pParse->nMem;
93223   regEof = ++pParse->nMem;
93224   v = sqlite3GetVdbe(pParse);
93225   addrTop = sqlite3VdbeCurrentAddr(v);
93226   sqlite3VdbeAddOp2(v, OP_Integer, addrTop+2, regYield); /* X <- A */
93227   VdbeComment((v, "Co-routine entry point"));
93228   sqlite3VdbeAddOp2(v, OP_Integer, 0, regEof);           /* EOF <- 0 */
93229   VdbeComment((v, "Co-routine completion flag"));
93230   sqlite3SelectDestInit(pDest, SRT_Coroutine, regYield);
93231   j1 = sqlite3VdbeAddOp2(v, OP_Goto, 0, 0);
93232   rc = sqlite3Select(pParse, pSelect, pDest);
93233   assert( pParse->nErr==0 || rc );
93234   if( pParse->db->mallocFailed && rc==SQLITE_OK ) rc = SQLITE_NOMEM;
93235   if( rc ) return rc;
93236   sqlite3VdbeAddOp2(v, OP_Integer, 1, regEof);            /* EOF <- 1 */
93237   sqlite3VdbeAddOp1(v, OP_Yield, regYield);   /* yield X */
93238   sqlite3VdbeAddOp2(v, OP_Halt, SQLITE_INTERNAL, OE_Abort);
93239   VdbeComment((v, "End of coroutine"));
93240   sqlite3VdbeJumpHere(v, j1);                             /* label B: */
93241   return rc;
93242 }
93243 
93244 
93245 
93246 /* Forward declaration */
93247 static int xferOptimization(
93248   Parse *pParse,        /* Parser context */
93249   Table *pDest,         /* The table we are inserting into */
93250   Select *pSelect,      /* A SELECT statement to use as the data source */
93251   int onError,          /* How to handle constraint errors */
93252   int iDbDest           /* The database of pDest */
93253 );
93254 
93255 /*
93256 ** This routine is called to handle SQL of the following forms:
93257 **
93258 **    insert into TABLE (IDLIST) values(EXPRLIST)
93259 **    insert into TABLE (IDLIST) select
93260 **
93261 ** The IDLIST following the table name is always optional.  If omitted,
93262 ** then a list of all columns for the table is substituted.  The IDLIST
93263 ** appears in the pColumn parameter.  pColumn is NULL if IDLIST is omitted.
93264 **
93265 ** The pList parameter holds EXPRLIST in the first form of the INSERT
93266 ** statement above, and pSelect is NULL.  For the second form, pList is
93267 ** NULL and pSelect is a pointer to the select statement used to generate
93268 ** data for the insert.
93269 **
93270 ** The code generated follows one of four templates.  For a simple
93271 ** insert with data coming from a VALUES clause, the code executes
93272 ** once straight down through.  Pseudo-code follows (we call this
93273 ** the "1st template"):
93274 **
93275 **         open write cursor to <table> and its indices
93276 **         put VALUES clause expressions into registers
93277 **         write the resulting record into <table>
93278 **         cleanup
93279 **
93280 ** The three remaining templates assume the statement is of the form
93281 **
93282 **   INSERT INTO <table> SELECT ...
93283 **
93284 ** If the SELECT clause is of the restricted form "SELECT * FROM <table2>" -
93285 ** in other words if the SELECT pulls all columns from a single table
93286 ** and there is no WHERE or LIMIT or GROUP BY or ORDER BY clauses, and
93287 ** if <table2> and <table1> are distinct tables but have identical
93288 ** schemas, including all the same indices, then a special optimization
93289 ** is invoked that copies raw records from <table2> over to <table1>.
93290 ** See the xferOptimization() function for the implementation of this
93291 ** template.  This is the 2nd template.
93292 **
93293 **         open a write cursor to <table>
93294 **         open read cursor on <table2>
93295 **         transfer all records in <table2> over to <table>
93296 **         close cursors
93297 **         foreach index on <table>
93298 **           open a write cursor on the <table> index
93299 **           open a read cursor on the corresponding <table2> index
93300 **           transfer all records from the read to the write cursors
93301 **           close cursors
93302 **         end foreach
93303 **
93304 ** The 3rd template is for when the second template does not apply
93305 ** and the SELECT clause does not read from <table> at any time.
93306 ** The generated code follows this template:
93307 **
93308 **         EOF <- 0
93309 **         X <- A
93310 **         goto B
93311 **      A: setup for the SELECT
93312 **         loop over the rows in the SELECT
93313 **           load values into registers R..R+n
93314 **           yield X
93315 **         end loop
93316 **         cleanup after the SELECT
93317 **         EOF <- 1
93318 **         yield X
93319 **         goto A
93320 **      B: open write cursor to <table> and its indices
93321 **      C: yield X
93322 **         if EOF goto D
93323 **         insert the select result into <table> from R..R+n
93324 **         goto C
93325 **      D: cleanup
93326 **
93327 ** The 4th template is used if the insert statement takes its
93328 ** values from a SELECT but the data is being inserted into a table
93329 ** that is also read as part of the SELECT.  In the third form,
93330 ** we have to use a intermediate table to store the results of
93331 ** the select.  The template is like this:
93332 **
93333 **         EOF <- 0
93334 **         X <- A
93335 **         goto B
93336 **      A: setup for the SELECT
93337 **         loop over the tables in the SELECT
93338 **           load value into register R..R+n
93339 **           yield X
93340 **         end loop
93341 **         cleanup after the SELECT
93342 **         EOF <- 1
93343 **         yield X
93344 **         halt-error
93345 **      B: open temp table
93346 **      L: yield X
93347 **         if EOF goto M
93348 **         insert row from R..R+n into temp table
93349 **         goto L
93350 **      M: open write cursor to <table> and its indices
93351 **         rewind temp table
93352 **      C: loop over rows of intermediate table
93353 **           transfer values form intermediate table into <table>
93354 **         end loop
93355 **      D: cleanup
93356 */
93357 SQLITE_PRIVATE void sqlite3Insert(
93358   Parse *pParse,        /* Parser context */
93359   SrcList *pTabList,    /* Name of table into which we are inserting */
93360   Select *pSelect,      /* A SELECT statement to use as the data source */
93361   IdList *pColumn,      /* Column names corresponding to IDLIST. */
93362   int onError           /* How to handle constraint errors */
93363 ){
93364   sqlite3 *db;          /* The main database structure */
93365   Table *pTab;          /* The table to insert into.  aka TABLE */
93366   char *zTab;           /* Name of the table into which we are inserting */
93367   const char *zDb;      /* Name of the database holding this table */
93368   int i, j, idx;        /* Loop counters */
93369   Vdbe *v;              /* Generate code into this virtual machine */
93370   Index *pIdx;          /* For looping over indices of the table */
93371   int nColumn;          /* Number of columns in the data */
93372   int nHidden = 0;      /* Number of hidden columns if TABLE is virtual */
93373   int iDataCur = 0;     /* VDBE cursor that is the main data repository */
93374   int iIdxCur = 0;      /* First index cursor */
93375   int ipkColumn = -1;   /* Column that is the INTEGER PRIMARY KEY */
93376   int endOfLoop;        /* Label for the end of the insertion loop */
93377   int useTempTable = 0; /* Store SELECT results in intermediate table */
93378   int srcTab = 0;       /* Data comes from this temporary cursor if >=0 */
93379   int addrInsTop = 0;   /* Jump to label "D" */
93380   int addrCont = 0;     /* Top of insert loop. Label "C" in templates 3 and 4 */
93381   int addrSelect = 0;   /* Address of coroutine that implements the SELECT */
93382   SelectDest dest;      /* Destination for SELECT on rhs of INSERT */
93383   int iDb;              /* Index of database holding TABLE */
93384   Db *pDb;              /* The database containing table being inserted into */
93385   int appendFlag = 0;   /* True if the insert is likely to be an append */
93386   int withoutRowid;     /* 0 for normal table.  1 for WITHOUT ROWID table */
93387   ExprList *pList = 0;  /* List of VALUES() to be inserted  */
93388 
93389   /* Register allocations */
93390   int regFromSelect = 0;/* Base register for data coming from SELECT */
93391   int regAutoinc = 0;   /* Register holding the AUTOINCREMENT counter */
93392   int regRowCount = 0;  /* Memory cell used for the row counter */
93393   int regIns;           /* Block of regs holding rowid+data being inserted */
93394   int regRowid;         /* registers holding insert rowid */
93395   int regData;          /* register holding first column to insert */
93396   int regEof = 0;       /* Register recording end of SELECT data */
93397   int *aRegIdx = 0;     /* One register allocated to each index */
93398 
93399 #ifndef SQLITE_OMIT_TRIGGER
93400   int isView;                 /* True if attempting to insert into a view */
93401   Trigger *pTrigger;          /* List of triggers on pTab, if required */
93402   int tmask;                  /* Mask of trigger times */
93403 #endif
93404 
93405   db = pParse->db;
93406   memset(&dest, 0, sizeof(dest));
93407   if( pParse->nErr || db->mallocFailed ){
93408     goto insert_cleanup;
93409   }
93410 
93411   /* If the Select object is really just a simple VALUES() list with a
93412   ** single row values (the common case) then keep that one row of values
93413   ** and go ahead and discard the Select object
93414   */
93415   if( pSelect && (pSelect->selFlags & SF_Values)!=0 && pSelect->pPrior==0 ){
93416     pList = pSelect->pEList;
93417     pSelect->pEList = 0;
93418     sqlite3SelectDelete(db, pSelect);
93419     pSelect = 0;
93420   }
93421 
93422   /* Locate the table into which we will be inserting new information.
93423   */
93424   assert( pTabList->nSrc==1 );
93425   zTab = pTabList->a[0].zName;
93426   if( NEVER(zTab==0) ) goto insert_cleanup;
93427   pTab = sqlite3SrcListLookup(pParse, pTabList);
93428   if( pTab==0 ){
93429     goto insert_cleanup;
93430   }
93431   iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
93432   assert( iDb<db->nDb );
93433   pDb = &db->aDb[iDb];
93434   zDb = pDb->zName;
93435   if( sqlite3AuthCheck(pParse, SQLITE_INSERT, pTab->zName, 0, zDb) ){
93436     goto insert_cleanup;
93437   }
93438   withoutRowid = !HasRowid(pTab);
93439 
93440   /* Figure out if we have any triggers and if the table being
93441   ** inserted into is a view
93442   */
93443 #ifndef SQLITE_OMIT_TRIGGER
93444   pTrigger = sqlite3TriggersExist(pParse, pTab, TK_INSERT, 0, &tmask);
93445   isView = pTab->pSelect!=0;
93446 #else
93447 # define pTrigger 0
93448 # define tmask 0
93449 # define isView 0
93450 #endif
93451 #ifdef SQLITE_OMIT_VIEW
93452 # undef isView
93453 # define isView 0
93454 #endif
93455   assert( (pTrigger && tmask) || (pTrigger==0 && tmask==0) );
93456 
93457   /* If pTab is really a view, make sure it has been initialized.
93458   ** ViewGetColumnNames() is a no-op if pTab is not a view.
93459   */
93460   if( sqlite3ViewGetColumnNames(pParse, pTab) ){
93461     goto insert_cleanup;
93462   }
93463 
93464   /* Cannot insert into a read-only table.
93465   */
93466   if( sqlite3IsReadOnly(pParse, pTab, tmask) ){
93467     goto insert_cleanup;
93468   }
93469 
93470   /* Allocate a VDBE
93471   */
93472   v = sqlite3GetVdbe(pParse);
93473   if( v==0 ) goto insert_cleanup;
93474   if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
93475   sqlite3BeginWriteOperation(pParse, pSelect || pTrigger, iDb);
93476 
93477 #ifndef SQLITE_OMIT_XFER_OPT
93478   /* If the statement is of the form
93479   **
93480   **       INSERT INTO <table1> SELECT * FROM <table2>;
93481   **
93482   ** Then special optimizations can be applied that make the transfer
93483   ** very fast and which reduce fragmentation of indices.
93484   **
93485   ** This is the 2nd template.
93486   */
93487   if( pColumn==0 && xferOptimization(pParse, pTab, pSelect, onError, iDb) ){
93488     assert( !pTrigger );
93489     assert( pList==0 );
93490     goto insert_end;
93491   }
93492 #endif /* SQLITE_OMIT_XFER_OPT */
93493 
93494   /* If this is an AUTOINCREMENT table, look up the sequence number in the
93495   ** sqlite_sequence table and store it in memory cell regAutoinc.
93496   */
93497   regAutoinc = autoIncBegin(pParse, iDb, pTab);
93498 
93499   /* Figure out how many columns of data are supplied.  If the data
93500   ** is coming from a SELECT statement, then generate a co-routine that
93501   ** produces a single row of the SELECT on each invocation.  The
93502   ** co-routine is the common header to the 3rd and 4th templates.
93503   */
93504   if( pSelect ){
93505     /* Data is coming from a SELECT.  Generate a co-routine to run the SELECT */
93506     int rc = sqlite3CodeCoroutine(pParse, pSelect, &dest);
93507     if( rc ) goto insert_cleanup;
93508 
93509     regEof = dest.iSDParm + 1;
93510     regFromSelect = dest.iSdst;
93511     assert( pSelect->pEList );
93512     nColumn = pSelect->pEList->nExpr;
93513     assert( dest.nSdst==nColumn );
93514 
93515     /* Set useTempTable to TRUE if the result of the SELECT statement
93516     ** should be written into a temporary table (template 4).  Set to
93517     ** FALSE if each output row of the SELECT can be written directly into
93518     ** the destination table (template 3).
93519     **
93520     ** A temp table must be used if the table being updated is also one
93521     ** of the tables being read by the SELECT statement.  Also use a
93522     ** temp table in the case of row triggers.
93523     */
93524     if( pTrigger || readsTable(pParse, addrSelect, iDb, pTab) ){
93525       useTempTable = 1;
93526     }
93527 
93528     if( useTempTable ){
93529       /* Invoke the coroutine to extract information from the SELECT
93530       ** and add it to a transient table srcTab.  The code generated
93531       ** here is from the 4th template:
93532       **
93533       **      B: open temp table
93534       **      L: yield X
93535       **         if EOF goto M
93536       **         insert row from R..R+n into temp table
93537       **         goto L
93538       **      M: ...
93539       */
93540       int regRec;          /* Register to hold packed record */
93541       int regTempRowid;    /* Register to hold temp table ROWID */
93542       int addrTop;         /* Label "L" */
93543       int addrIf;          /* Address of jump to M */
93544 
93545       srcTab = pParse->nTab++;
93546       regRec = sqlite3GetTempReg(pParse);
93547       regTempRowid = sqlite3GetTempReg(pParse);
93548       sqlite3VdbeAddOp2(v, OP_OpenEphemeral, srcTab, nColumn);
93549       addrTop = sqlite3VdbeAddOp1(v, OP_Yield, dest.iSDParm);
93550       addrIf = sqlite3VdbeAddOp1(v, OP_If, regEof);
93551       sqlite3VdbeAddOp3(v, OP_MakeRecord, regFromSelect, nColumn, regRec);
93552       sqlite3VdbeAddOp2(v, OP_NewRowid, srcTab, regTempRowid);
93553       sqlite3VdbeAddOp3(v, OP_Insert, srcTab, regRec, regTempRowid);
93554       sqlite3VdbeAddOp2(v, OP_Goto, 0, addrTop);
93555       sqlite3VdbeJumpHere(v, addrIf);
93556       sqlite3ReleaseTempReg(pParse, regRec);
93557       sqlite3ReleaseTempReg(pParse, regTempRowid);
93558     }
93559   }else{
93560     /* This is the case if the data for the INSERT is coming from a VALUES
93561     ** clause
93562     */
93563     NameContext sNC;
93564     memset(&sNC, 0, sizeof(sNC));
93565     sNC.pParse = pParse;
93566     srcTab = -1;
93567     assert( useTempTable==0 );
93568     nColumn = pList ? pList->nExpr : 0;
93569     for(i=0; i<nColumn; i++){
93570       if( sqlite3ResolveExprNames(&sNC, pList->a[i].pExpr) ){
93571         goto insert_cleanup;
93572       }
93573     }
93574   }
93575 
93576   /* Make sure the number of columns in the source data matches the number
93577   ** of columns to be inserted into the table.
93578   */
93579   if( IsVirtual(pTab) ){
93580     for(i=0; i<pTab->nCol; i++){
93581       nHidden += (IsHiddenColumn(&pTab->aCol[i]) ? 1 : 0);
93582     }
93583   }
93584   if( pColumn==0 && nColumn && nColumn!=(pTab->nCol-nHidden) ){
93585     sqlite3ErrorMsg(pParse,
93586        "table %S has %d columns but %d values were supplied",
93587        pTabList, 0, pTab->nCol-nHidden, nColumn);
93588     goto insert_cleanup;
93589   }
93590   if( pColumn!=0 && nColumn!=pColumn->nId ){
93591     sqlite3ErrorMsg(pParse, "%d values for %d columns", nColumn, pColumn->nId);
93592     goto insert_cleanup;
93593   }
93594 
93595   /* If the INSERT statement included an IDLIST term, then make sure
93596   ** all elements of the IDLIST really are columns of the table and
93597   ** remember the column indices.
93598   **
93599   ** If the table has an INTEGER PRIMARY KEY column and that column
93600   ** is named in the IDLIST, then record in the ipkColumn variable
93601   ** the index into IDLIST of the primary key column.  ipkColumn is
93602   ** the index of the primary key as it appears in IDLIST, not as
93603   ** is appears in the original table.  (The index of the INTEGER
93604   ** PRIMARY KEY in the original table is pTab->iPKey.)
93605   */
93606   if( pColumn ){
93607     for(i=0; i<pColumn->nId; i++){
93608       pColumn->a[i].idx = -1;
93609     }
93610     for(i=0; i<pColumn->nId; i++){
93611       for(j=0; j<pTab->nCol; j++){
93612         if( sqlite3StrICmp(pColumn->a[i].zName, pTab->aCol[j].zName)==0 ){
93613           pColumn->a[i].idx = j;
93614           if( j==pTab->iPKey ){
93615             ipkColumn = i;  assert( !withoutRowid );
93616           }
93617           break;
93618         }
93619       }
93620       if( j>=pTab->nCol ){
93621         if( sqlite3IsRowid(pColumn->a[i].zName) && !withoutRowid ){
93622           ipkColumn = i;
93623         }else{
93624           sqlite3ErrorMsg(pParse, "table %S has no column named %s",
93625               pTabList, 0, pColumn->a[i].zName);
93626           pParse->checkSchema = 1;
93627           goto insert_cleanup;
93628         }
93629       }
93630     }
93631   }
93632 
93633   /* If there is no IDLIST term but the table has an integer primary
93634   ** key, the set the ipkColumn variable to the integer primary key
93635   ** column index in the original table definition.
93636   */
93637   if( pColumn==0 && nColumn>0 ){
93638     ipkColumn = pTab->iPKey;
93639   }
93640 
93641   /* Initialize the count of rows to be inserted
93642   */
93643   if( db->flags & SQLITE_CountRows ){
93644     regRowCount = ++pParse->nMem;
93645     sqlite3VdbeAddOp2(v, OP_Integer, 0, regRowCount);
93646   }
93647 
93648   /* If this is not a view, open the table and and all indices */
93649   if( !isView ){
93650     int nIdx;
93651     nIdx = sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, -1, 0,
93652                                       &iDataCur, &iIdxCur);
93653     aRegIdx = sqlite3DbMallocRaw(db, sizeof(int)*(nIdx+1));
93654     if( aRegIdx==0 ){
93655       goto insert_cleanup;
93656     }
93657     for(i=0; i<nIdx; i++){
93658       aRegIdx[i] = ++pParse->nMem;
93659     }
93660   }
93661 
93662   /* This is the top of the main insertion loop */
93663   if( useTempTable ){
93664     /* This block codes the top of loop only.  The complete loop is the
93665     ** following pseudocode (template 4):
93666     **
93667     **         rewind temp table
93668     **      C: loop over rows of intermediate table
93669     **           transfer values form intermediate table into <table>
93670     **         end loop
93671     **      D: ...
93672     */
93673     addrInsTop = sqlite3VdbeAddOp1(v, OP_Rewind, srcTab);
93674     addrCont = sqlite3VdbeCurrentAddr(v);
93675   }else if( pSelect ){
93676     /* This block codes the top of loop only.  The complete loop is the
93677     ** following pseudocode (template 3):
93678     **
93679     **      C: yield X
93680     **         if EOF goto D
93681     **         insert the select result into <table> from R..R+n
93682     **         goto C
93683     **      D: ...
93684     */
93685     addrCont = sqlite3VdbeAddOp1(v, OP_Yield, dest.iSDParm);
93686     addrInsTop = sqlite3VdbeAddOp1(v, OP_If, regEof);
93687   }
93688 
93689   /* Allocate registers for holding the rowid of the new row,
93690   ** the content of the new row, and the assemblied row record.
93691   */
93692   regRowid = regIns = pParse->nMem+1;
93693   pParse->nMem += pTab->nCol + 1;
93694   if( IsVirtual(pTab) ){
93695     regRowid++;
93696     pParse->nMem++;
93697   }
93698   regData = regRowid+1;
93699 
93700   /* Run the BEFORE and INSTEAD OF triggers, if there are any
93701   */
93702   endOfLoop = sqlite3VdbeMakeLabel(v);
93703   if( tmask & TRIGGER_BEFORE ){
93704     int regCols = sqlite3GetTempRange(pParse, pTab->nCol+1);
93705 
93706     /* build the NEW.* reference row.  Note that if there is an INTEGER
93707     ** PRIMARY KEY into which a NULL is being inserted, that NULL will be
93708     ** translated into a unique ID for the row.  But on a BEFORE trigger,
93709     ** we do not know what the unique ID will be (because the insert has
93710     ** not happened yet) so we substitute a rowid of -1
93711     */
93712     if( ipkColumn<0 ){
93713       sqlite3VdbeAddOp2(v, OP_Integer, -1, regCols);
93714     }else{
93715       int j1;
93716       assert( !withoutRowid );
93717       if( useTempTable ){
93718         sqlite3VdbeAddOp3(v, OP_Column, srcTab, ipkColumn, regCols);
93719       }else{
93720         assert( pSelect==0 );  /* Otherwise useTempTable is true */
93721         sqlite3ExprCode(pParse, pList->a[ipkColumn].pExpr, regCols);
93722       }
93723       j1 = sqlite3VdbeAddOp1(v, OP_NotNull, regCols);
93724       sqlite3VdbeAddOp2(v, OP_Integer, -1, regCols);
93725       sqlite3VdbeJumpHere(v, j1);
93726       sqlite3VdbeAddOp1(v, OP_MustBeInt, regCols);
93727     }
93728 
93729     /* Cannot have triggers on a virtual table. If it were possible,
93730     ** this block would have to account for hidden column.
93731     */
93732     assert( !IsVirtual(pTab) );
93733 
93734     /* Create the new column data
93735     */
93736     for(i=0; i<pTab->nCol; i++){
93737       if( pColumn==0 ){
93738         j = i;
93739       }else{
93740         for(j=0; j<pColumn->nId; j++){
93741           if( pColumn->a[j].idx==i ) break;
93742         }
93743       }
93744       if( (!useTempTable && !pList) || (pColumn && j>=pColumn->nId) ){
93745         sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, regCols+i+1);
93746       }else if( useTempTable ){
93747         sqlite3VdbeAddOp3(v, OP_Column, srcTab, j, regCols+i+1);
93748       }else{
93749         assert( pSelect==0 ); /* Otherwise useTempTable is true */
93750         sqlite3ExprCodeAndCache(pParse, pList->a[j].pExpr, regCols+i+1);
93751       }
93752     }
93753 
93754     /* If this is an INSERT on a view with an INSTEAD OF INSERT trigger,
93755     ** do not attempt any conversions before assembling the record.
93756     ** If this is a real table, attempt conversions as required by the
93757     ** table column affinities.
93758     */
93759     if( !isView ){
93760       sqlite3VdbeAddOp2(v, OP_Affinity, regCols+1, pTab->nCol);
93761       sqlite3TableAffinityStr(v, pTab);
93762     }
93763 
93764     /* Fire BEFORE or INSTEAD OF triggers */
93765     sqlite3CodeRowTrigger(pParse, pTrigger, TK_INSERT, 0, TRIGGER_BEFORE,
93766         pTab, regCols-pTab->nCol-1, onError, endOfLoop);
93767 
93768     sqlite3ReleaseTempRange(pParse, regCols, pTab->nCol+1);
93769   }
93770 
93771   /* Compute the content of the next row to insert into a range of
93772   ** registers beginning at regIns.
93773   */
93774   if( !isView ){
93775     if( IsVirtual(pTab) ){
93776       /* The row that the VUpdate opcode will delete: none */
93777       sqlite3VdbeAddOp2(v, OP_Null, 0, regIns);
93778     }
93779     if( ipkColumn>=0 ){
93780       if( useTempTable ){
93781         sqlite3VdbeAddOp3(v, OP_Column, srcTab, ipkColumn, regRowid);
93782       }else if( pSelect ){
93783         sqlite3VdbeAddOp2(v, OP_SCopy, regFromSelect+ipkColumn, regRowid);
93784       }else{
93785         VdbeOp *pOp;
93786         sqlite3ExprCode(pParse, pList->a[ipkColumn].pExpr, regRowid);
93787         pOp = sqlite3VdbeGetOp(v, -1);
93788         if( ALWAYS(pOp) && pOp->opcode==OP_Null && !IsVirtual(pTab) ){
93789           appendFlag = 1;
93790           pOp->opcode = OP_NewRowid;
93791           pOp->p1 = iDataCur;
93792           pOp->p2 = regRowid;
93793           pOp->p3 = regAutoinc;
93794         }
93795       }
93796       /* If the PRIMARY KEY expression is NULL, then use OP_NewRowid
93797       ** to generate a unique primary key value.
93798       */
93799       if( !appendFlag ){
93800         int j1;
93801         if( !IsVirtual(pTab) ){
93802           j1 = sqlite3VdbeAddOp1(v, OP_NotNull, regRowid);
93803           sqlite3VdbeAddOp3(v, OP_NewRowid, iDataCur, regRowid, regAutoinc);
93804           sqlite3VdbeJumpHere(v, j1);
93805         }else{
93806           j1 = sqlite3VdbeCurrentAddr(v);
93807           sqlite3VdbeAddOp2(v, OP_IsNull, regRowid, j1+2);
93808         }
93809         sqlite3VdbeAddOp1(v, OP_MustBeInt, regRowid);
93810       }
93811     }else if( IsVirtual(pTab) || withoutRowid ){
93812       sqlite3VdbeAddOp2(v, OP_Null, 0, regRowid);
93813     }else{
93814       sqlite3VdbeAddOp3(v, OP_NewRowid, iDataCur, regRowid, regAutoinc);
93815       appendFlag = 1;
93816     }
93817     autoIncStep(pParse, regAutoinc, regRowid);
93818 
93819     /* Compute data for all columns of the new entry, beginning
93820     ** with the first column.
93821     */
93822     nHidden = 0;
93823     for(i=0; i<pTab->nCol; i++){
93824       int iRegStore = regRowid+1+i;
93825       if( i==pTab->iPKey ){
93826         /* The value of the INTEGER PRIMARY KEY column is always a NULL.
93827         ** Whenever this column is read, the rowid will be substituted
93828         ** in its place.  Hence, fill this column with a NULL to avoid
93829         ** taking up data space with information that will never be used. */
93830         sqlite3VdbeAddOp2(v, OP_Null, 0, iRegStore);
93831         continue;
93832       }
93833       if( pColumn==0 ){
93834         if( IsHiddenColumn(&pTab->aCol[i]) ){
93835           assert( IsVirtual(pTab) );
93836           j = -1;
93837           nHidden++;
93838         }else{
93839           j = i - nHidden;
93840         }
93841       }else{
93842         for(j=0; j<pColumn->nId; j++){
93843           if( pColumn->a[j].idx==i ) break;
93844         }
93845       }
93846       if( j<0 || nColumn==0 || (pColumn && j>=pColumn->nId) ){
93847         sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, iRegStore);
93848       }else if( useTempTable ){
93849         sqlite3VdbeAddOp3(v, OP_Column, srcTab, j, iRegStore);
93850       }else if( pSelect ){
93851         sqlite3VdbeAddOp2(v, OP_SCopy, regFromSelect+j, iRegStore);
93852       }else{
93853         sqlite3ExprCode(pParse, pList->a[j].pExpr, iRegStore);
93854       }
93855     }
93856 
93857     /* Generate code to check constraints and generate index keys and
93858     ** do the insertion.
93859     */
93860 #ifndef SQLITE_OMIT_VIRTUALTABLE
93861     if( IsVirtual(pTab) ){
93862       const char *pVTab = (const char *)sqlite3GetVTable(db, pTab);
93863       sqlite3VtabMakeWritable(pParse, pTab);
93864       sqlite3VdbeAddOp4(v, OP_VUpdate, 1, pTab->nCol+2, regIns, pVTab, P4_VTAB);
93865       sqlite3VdbeChangeP5(v, onError==OE_Default ? OE_Abort : onError);
93866       sqlite3MayAbort(pParse);
93867     }else
93868 #endif
93869     {
93870       int isReplace;    /* Set to true if constraints may cause a replace */
93871       sqlite3GenerateConstraintChecks(pParse, pTab, aRegIdx, iDataCur, iIdxCur,
93872           regIns, 0, ipkColumn>=0, onError, endOfLoop, &isReplace
93873       );
93874       sqlite3FkCheck(pParse, pTab, 0, regIns, 0, 0);
93875       sqlite3CompleteInsertion(pParse, pTab, iDataCur, iIdxCur,
93876                                regIns, aRegIdx, 0, appendFlag, isReplace==0);
93877     }
93878   }
93879 
93880   /* Update the count of rows that are inserted
93881   */
93882   if( (db->flags & SQLITE_CountRows)!=0 ){
93883     sqlite3VdbeAddOp2(v, OP_AddImm, regRowCount, 1);
93884   }
93885 
93886   if( pTrigger ){
93887     /* Code AFTER triggers */
93888     sqlite3CodeRowTrigger(pParse, pTrigger, TK_INSERT, 0, TRIGGER_AFTER,
93889         pTab, regData-2-pTab->nCol, onError, endOfLoop);
93890   }
93891 
93892   /* The bottom of the main insertion loop, if the data source
93893   ** is a SELECT statement.
93894   */
93895   sqlite3VdbeResolveLabel(v, endOfLoop);
93896   if( useTempTable ){
93897     sqlite3VdbeAddOp2(v, OP_Next, srcTab, addrCont);
93898     sqlite3VdbeJumpHere(v, addrInsTop);
93899     sqlite3VdbeAddOp1(v, OP_Close, srcTab);
93900   }else if( pSelect ){
93901     sqlite3VdbeAddOp2(v, OP_Goto, 0, addrCont);
93902     sqlite3VdbeJumpHere(v, addrInsTop);
93903   }
93904 
93905   if( !IsVirtual(pTab) && !isView ){
93906     /* Close all tables opened */
93907     if( iDataCur<iIdxCur ) sqlite3VdbeAddOp1(v, OP_Close, iDataCur);
93908     for(idx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, idx++){
93909       sqlite3VdbeAddOp1(v, OP_Close, idx+iIdxCur);
93910     }
93911   }
93912 
93913 insert_end:
93914   /* Update the sqlite_sequence table by storing the content of the
93915   ** maximum rowid counter values recorded while inserting into
93916   ** autoincrement tables.
93917   */
93918   if( pParse->nested==0 && pParse->pTriggerTab==0 ){
93919     sqlite3AutoincrementEnd(pParse);
93920   }
93921 
93922   /*
93923   ** Return the number of rows inserted. If this routine is
93924   ** generating code because of a call to sqlite3NestedParse(), do not
93925   ** invoke the callback function.
93926   */
93927   if( (db->flags&SQLITE_CountRows) && !pParse->nested && !pParse->pTriggerTab ){
93928     sqlite3VdbeAddOp2(v, OP_ResultRow, regRowCount, 1);
93929     sqlite3VdbeSetNumCols(v, 1);
93930     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows inserted", SQLITE_STATIC);
93931   }
93932 
93933 insert_cleanup:
93934   sqlite3SrcListDelete(db, pTabList);
93935   sqlite3ExprListDelete(db, pList);
93936   sqlite3SelectDelete(db, pSelect);
93937   sqlite3IdListDelete(db, pColumn);
93938   sqlite3DbFree(db, aRegIdx);
93939 }
93940 
93941 /* Make sure "isView" and other macros defined above are undefined. Otherwise
93942 ** thely may interfere with compilation of other functions in this file
93943 ** (or in another file, if this file becomes part of the amalgamation).  */
93944 #ifdef isView
93945  #undef isView
93946 #endif
93947 #ifdef pTrigger
93948  #undef pTrigger
93949 #endif
93950 #ifdef tmask
93951  #undef tmask
93952 #endif
93953 
93954 /*
93955 ** Generate code to do constraint checks prior to an INSERT or an UPDATE
93956 ** on table pTab.
93957 **
93958 ** The regNewData parameter is the first register in a range that contains
93959 ** the data to be inserted or the data after the update.  There will be
93960 ** pTab->nCol+1 registers in this range.  The first register (the one
93961 ** that regNewData points to) will contain the new rowid, or NULL in the
93962 ** case of a WITHOUT ROWID table.  The second register in the range will
93963 ** contain the content of the first table column.  The third register will
93964 ** contain the content of the second table column.  And so forth.
93965 **
93966 ** The regOldData parameter is similar to regNewData except that it contains
93967 ** the data prior to an UPDATE rather than afterwards.  regOldData is zero
93968 ** for an INSERT.  This routine can distinguish between UPDATE and INSERT by
93969 ** checking regOldData for zero.
93970 **
93971 ** For an UPDATE, the pkChng boolean is true if the true primary key (the
93972 ** rowid for a normal table or the PRIMARY KEY for a WITHOUT ROWID table)
93973 ** might be modified by the UPDATE.  If pkChng is false, then the key of
93974 ** the iDataCur content table is guaranteed to be unchanged by the UPDATE.
93975 **
93976 ** For an INSERT, the pkChng boolean indicates whether or not the rowid
93977 ** was explicitly specified as part of the INSERT statement.  If pkChng
93978 ** is zero, it means that the either rowid is computed automatically or
93979 ** that the table is a WITHOUT ROWID table and has no rowid.  On an INSERT,
93980 ** pkChng will only be true if the INSERT statement provides an integer
93981 ** value for either the rowid column or its INTEGER PRIMARY KEY alias.
93982 **
93983 ** The code generated by this routine will store new index entries into
93984 ** registers identified by aRegIdx[].  No index entry is created for
93985 ** indices where aRegIdx[i]==0.  The order of indices in aRegIdx[] is
93986 ** the same as the order of indices on the linked list of indices
93987 ** at pTab->pIndex.
93988 **
93989 ** The caller must have already opened writeable cursors on the main
93990 ** table and all applicable indices (that is to say, all indices for which
93991 ** aRegIdx[] is not zero).  iDataCur is the cursor for the main table when
93992 ** inserting or updating a rowid table, or the cursor for the PRIMARY KEY
93993 ** index when operating on a WITHOUT ROWID table.  iIdxCur is the cursor
93994 ** for the first index in the pTab->pIndex list.  Cursors for other indices
93995 ** are at iIdxCur+N for the N-th element of the pTab->pIndex list.
93996 **
93997 ** This routine also generates code to check constraints.  NOT NULL,
93998 ** CHECK, and UNIQUE constraints are all checked.  If a constraint fails,
93999 ** then the appropriate action is performed.  There are five possible
94000 ** actions: ROLLBACK, ABORT, FAIL, REPLACE, and IGNORE.
94001 **
94002 **  Constraint type  Action       What Happens
94003 **  ---------------  ----------   ----------------------------------------
94004 **  any              ROLLBACK     The current transaction is rolled back and
94005 **                                sqlite3_step() returns immediately with a
94006 **                                return code of SQLITE_CONSTRAINT.
94007 **
94008 **  any              ABORT        Back out changes from the current command
94009 **                                only (do not do a complete rollback) then
94010 **                                cause sqlite3_step() to return immediately
94011 **                                with SQLITE_CONSTRAINT.
94012 **
94013 **  any              FAIL         Sqlite3_step() returns immediately with a
94014 **                                return code of SQLITE_CONSTRAINT.  The
94015 **                                transaction is not rolled back and any
94016 **                                changes to prior rows are retained.
94017 **
94018 **  any              IGNORE       The attempt in insert or update the current
94019 **                                row is skipped, without throwing an error.
94020 **                                Processing continues with the next row.
94021 **                                (There is an immediate jump to ignoreDest.)
94022 **
94023 **  NOT NULL         REPLACE      The NULL value is replace by the default
94024 **                                value for that column.  If the default value
94025 **                                is NULL, the action is the same as ABORT.
94026 **
94027 **  UNIQUE           REPLACE      The other row that conflicts with the row
94028 **                                being inserted is removed.
94029 **
94030 **  CHECK            REPLACE      Illegal.  The results in an exception.
94031 **
94032 ** Which action to take is determined by the overrideError parameter.
94033 ** Or if overrideError==OE_Default, then the pParse->onError parameter
94034 ** is used.  Or if pParse->onError==OE_Default then the onError value
94035 ** for the constraint is used.
94036 */
94037 SQLITE_PRIVATE void sqlite3GenerateConstraintChecks(
94038   Parse *pParse,       /* The parser context */
94039   Table *pTab,         /* The table being inserted or updated */
94040   int *aRegIdx,        /* Use register aRegIdx[i] for index i.  0 for unused */
94041   int iDataCur,        /* Canonical data cursor (main table or PK index) */
94042   int iIdxCur,         /* First index cursor */
94043   int regNewData,      /* First register in a range holding values to insert */
94044   int regOldData,      /* Previous content.  0 for INSERTs */
94045   u8 pkChng,           /* Non-zero if the rowid or PRIMARY KEY changed */
94046   u8 overrideError,    /* Override onError to this if not OE_Default */
94047   int ignoreDest,      /* Jump to this label on an OE_Ignore resolution */
94048   int *pbMayReplace    /* OUT: Set to true if constraint may cause a replace */
94049 ){
94050   Vdbe *v;             /* VDBE under constrution */
94051   Index *pIdx;         /* Pointer to one of the indices */
94052   Index *pPk = 0;      /* The PRIMARY KEY index */
94053   sqlite3 *db;         /* Database connection */
94054   int i;               /* loop counter */
94055   int ix;              /* Index loop counter */
94056   int nCol;            /* Number of columns */
94057   int onError;         /* Conflict resolution strategy */
94058   int j1;              /* Addresss of jump instruction */
94059   int seenReplace = 0; /* True if REPLACE is used to resolve INT PK conflict */
94060   int nPkField;        /* Number of fields in PRIMARY KEY. 1 for ROWID tables */
94061   int ipkTop = 0;      /* Top of the rowid change constraint check */
94062   int ipkBottom = 0;   /* Bottom of the rowid change constraint check */
94063   u8 isUpdate;         /* True if this is an UPDATE operation */
94064   int regRowid = -1;   /* Register holding ROWID value */
94065 
94066   isUpdate = regOldData!=0;
94067   db = pParse->db;
94068   v = sqlite3GetVdbe(pParse);
94069   assert( v!=0 );
94070   assert( pTab->pSelect==0 );  /* This table is not a VIEW */
94071   nCol = pTab->nCol;
94072 
94073   /* pPk is the PRIMARY KEY index for WITHOUT ROWID tables and NULL for
94074   ** normal rowid tables.  nPkField is the number of key fields in the
94075   ** pPk index or 1 for a rowid table.  In other words, nPkField is the
94076   ** number of fields in the true primary key of the table. */
94077   if( HasRowid(pTab) ){
94078     pPk = 0;
94079     nPkField = 1;
94080   }else{
94081     pPk = sqlite3PrimaryKeyIndex(pTab);
94082     nPkField = pPk->nKeyCol;
94083   }
94084 
94085   /* Record that this module has started */
94086   VdbeModuleComment((v, "BEGIN: GenCnstCks(%d,%d,%d,%d,%d)",
94087                      iDataCur, iIdxCur, regNewData, regOldData, pkChng));
94088 
94089   /* Test all NOT NULL constraints.
94090   */
94091   for(i=0; i<nCol; i++){
94092     if( i==pTab->iPKey ){
94093       continue;
94094     }
94095     onError = pTab->aCol[i].notNull;
94096     if( onError==OE_None ) continue;
94097     if( overrideError!=OE_Default ){
94098       onError = overrideError;
94099     }else if( onError==OE_Default ){
94100       onError = OE_Abort;
94101     }
94102     if( onError==OE_Replace && pTab->aCol[i].pDflt==0 ){
94103       onError = OE_Abort;
94104     }
94105     assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail
94106         || onError==OE_Ignore || onError==OE_Replace );
94107     switch( onError ){
94108       case OE_Abort:
94109         sqlite3MayAbort(pParse);
94110         /* Fall through */
94111       case OE_Rollback:
94112       case OE_Fail: {
94113         char *zMsg = sqlite3MPrintf(db, "%s.%s", pTab->zName,
94114                                     pTab->aCol[i].zName);
94115         sqlite3VdbeAddOp4(v, OP_HaltIfNull, SQLITE_CONSTRAINT_NOTNULL, onError,
94116                           regNewData+1+i, zMsg, P4_DYNAMIC);
94117         sqlite3VdbeChangeP5(v, P5_ConstraintNotNull);
94118         break;
94119       }
94120       case OE_Ignore: {
94121         sqlite3VdbeAddOp2(v, OP_IsNull, regNewData+1+i, ignoreDest);
94122         break;
94123       }
94124       default: {
94125         assert( onError==OE_Replace );
94126         j1 = sqlite3VdbeAddOp1(v, OP_NotNull, regNewData+1+i);
94127         sqlite3ExprCode(pParse, pTab->aCol[i].pDflt, regNewData+1+i);
94128         sqlite3VdbeJumpHere(v, j1);
94129         break;
94130       }
94131     }
94132   }
94133 
94134   /* Test all CHECK constraints
94135   */
94136 #ifndef SQLITE_OMIT_CHECK
94137   if( pTab->pCheck && (db->flags & SQLITE_IgnoreChecks)==0 ){
94138     ExprList *pCheck = pTab->pCheck;
94139     pParse->ckBase = regNewData+1;
94140     onError = overrideError!=OE_Default ? overrideError : OE_Abort;
94141     for(i=0; i<pCheck->nExpr; i++){
94142       int allOk = sqlite3VdbeMakeLabel(v);
94143       sqlite3ExprIfTrue(pParse, pCheck->a[i].pExpr, allOk, SQLITE_JUMPIFNULL);
94144       if( onError==OE_Ignore ){
94145         sqlite3VdbeAddOp2(v, OP_Goto, 0, ignoreDest);
94146       }else{
94147         char *zName = pCheck->a[i].zName;
94148         if( zName==0 ) zName = pTab->zName;
94149         if( onError==OE_Replace ) onError = OE_Abort; /* IMP: R-15569-63625 */
94150         sqlite3HaltConstraint(pParse, SQLITE_CONSTRAINT_CHECK,
94151                               onError, zName, P4_TRANSIENT,
94152                               P5_ConstraintCheck);
94153       }
94154       sqlite3VdbeResolveLabel(v, allOk);
94155     }
94156   }
94157 #endif /* !defined(SQLITE_OMIT_CHECK) */
94158 
94159   /* If rowid is changing, make sure the new rowid does not previously
94160   ** exist in the table.
94161   */
94162   if( pkChng && pPk==0 ){
94163     int addrRowidOk = sqlite3VdbeMakeLabel(v);
94164 
94165     /* Figure out what action to take in case of a rowid collision */
94166     onError = pTab->keyConf;
94167     if( overrideError!=OE_Default ){
94168       onError = overrideError;
94169     }else if( onError==OE_Default ){
94170       onError = OE_Abort;
94171     }
94172 
94173     if( isUpdate ){
94174       /* pkChng!=0 does not mean that the rowid has change, only that
94175       ** it might have changed.  Skip the conflict logic below if the rowid
94176       ** is unchanged. */
94177       sqlite3VdbeAddOp3(v, OP_Eq, regNewData, addrRowidOk, regOldData);
94178     }
94179 
94180     /* If the response to a rowid conflict is REPLACE but the response
94181     ** to some other UNIQUE constraint is FAIL or IGNORE, then we need
94182     ** to defer the running of the rowid conflict checking until after
94183     ** the UNIQUE constraints have run.
94184     */
94185     if( onError==OE_Replace && overrideError!=OE_Replace ){
94186       for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
94187         if( pIdx->onError==OE_Ignore || pIdx->onError==OE_Fail ){
94188           ipkTop = sqlite3VdbeAddOp0(v, OP_Goto);
94189           break;
94190         }
94191       }
94192     }
94193 
94194     /* Check to see if the new rowid already exists in the table.  Skip
94195     ** the following conflict logic if it does not. */
94196     sqlite3VdbeAddOp3(v, OP_NotExists, iDataCur, addrRowidOk, regNewData);
94197 
94198     /* Generate code that deals with a rowid collision */
94199     switch( onError ){
94200       default: {
94201         onError = OE_Abort;
94202         /* Fall thru into the next case */
94203       }
94204       case OE_Rollback:
94205       case OE_Abort:
94206       case OE_Fail: {
94207         sqlite3RowidConstraint(pParse, onError, pTab);
94208         break;
94209       }
94210       case OE_Replace: {
94211         /* If there are DELETE triggers on this table and the
94212         ** recursive-triggers flag is set, call GenerateRowDelete() to
94213         ** remove the conflicting row from the table. This will fire
94214         ** the triggers and remove both the table and index b-tree entries.
94215         **
94216         ** Otherwise, if there are no triggers or the recursive-triggers
94217         ** flag is not set, but the table has one or more indexes, call
94218         ** GenerateRowIndexDelete(). This removes the index b-tree entries
94219         ** only. The table b-tree entry will be replaced by the new entry
94220         ** when it is inserted.
94221         **
94222         ** If either GenerateRowDelete() or GenerateRowIndexDelete() is called,
94223         ** also invoke MultiWrite() to indicate that this VDBE may require
94224         ** statement rollback (if the statement is aborted after the delete
94225         ** takes place). Earlier versions called sqlite3MultiWrite() regardless,
94226         ** but being more selective here allows statements like:
94227         **
94228         **   REPLACE INTO t(rowid) VALUES($newrowid)
94229         **
94230         ** to run without a statement journal if there are no indexes on the
94231         ** table.
94232         */
94233         Trigger *pTrigger = 0;
94234         if( db->flags&SQLITE_RecTriggers ){
94235           pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0);
94236         }
94237         if( pTrigger || sqlite3FkRequired(pParse, pTab, 0, 0) ){
94238           sqlite3MultiWrite(pParse);
94239           sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur,
94240                                    regNewData, 1, 0, OE_Replace, 1);
94241         }else if( pTab->pIndex ){
94242           sqlite3MultiWrite(pParse);
94243           sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur, 0);
94244         }
94245         seenReplace = 1;
94246         break;
94247       }
94248       case OE_Ignore: {
94249         /*assert( seenReplace==0 );*/
94250         sqlite3VdbeAddOp2(v, OP_Goto, 0, ignoreDest);
94251         break;
94252       }
94253     }
94254     sqlite3VdbeResolveLabel(v, addrRowidOk);
94255     if( ipkTop ){
94256       ipkBottom = sqlite3VdbeAddOp0(v, OP_Goto);
94257       sqlite3VdbeJumpHere(v, ipkTop);
94258     }
94259   }
94260 
94261   /* Test all UNIQUE constraints by creating entries for each UNIQUE
94262   ** index and making sure that duplicate entries do not already exist.
94263   ** Compute the revised record entries for indices as we go.
94264   **
94265   ** This loop also handles the case of the PRIMARY KEY index for a
94266   ** WITHOUT ROWID table.
94267   */
94268   for(ix=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, ix++){
94269     int regIdx;          /* Range of registers hold conent for pIdx */
94270     int regR;            /* Range of registers holding conflicting PK */
94271     int iThisCur;        /* Cursor for this UNIQUE index */
94272     int addrUniqueOk;    /* Jump here if the UNIQUE constraint is satisfied */
94273 
94274     if( aRegIdx[ix]==0 ) continue;  /* Skip indices that do not change */
94275     iThisCur = iIdxCur+ix;
94276     addrUniqueOk = sqlite3VdbeMakeLabel(v);
94277 
94278     /* Skip partial indices for which the WHERE clause is not true */
94279     if( pIdx->pPartIdxWhere ){
94280       sqlite3VdbeAddOp2(v, OP_Null, 0, aRegIdx[ix]);
94281       pParse->ckBase = regNewData+1;
94282       sqlite3ExprIfFalse(pParse, pIdx->pPartIdxWhere, addrUniqueOk,
94283                          SQLITE_JUMPIFNULL);
94284       pParse->ckBase = 0;
94285     }
94286 
94287     /* Create a record for this index entry as it should appear after
94288     ** the insert or update.  Store that record in the aRegIdx[ix] register
94289     */
94290     regIdx = sqlite3GetTempRange(pParse, pIdx->nColumn);
94291     for(i=0; i<pIdx->nColumn; i++){
94292       int iField = pIdx->aiColumn[i];
94293       int x;
94294       if( iField<0 || iField==pTab->iPKey ){
94295         if( regRowid==regIdx+i ) continue; /* ROWID already in regIdx+i */
94296         x = regNewData;
94297         regRowid =  pIdx->pPartIdxWhere ? -1 : regIdx+i;
94298       }else{
94299         x = iField + regNewData + 1;
94300       }
94301       sqlite3VdbeAddOp2(v, OP_SCopy, x, regIdx+i);
94302       VdbeComment((v, "%s", iField<0 ? "rowid" : pTab->aCol[iField].zName));
94303     }
94304     sqlite3VdbeAddOp3(v, OP_MakeRecord, regIdx, pIdx->nColumn, aRegIdx[ix]);
94305     sqlite3VdbeChangeP4(v, -1, sqlite3IndexAffinityStr(v, pIdx), P4_TRANSIENT);
94306     VdbeComment((v, "for %s", pIdx->zName));
94307     sqlite3ExprCacheAffinityChange(pParse, regIdx, pIdx->nColumn);
94308 
94309     /* In an UPDATE operation, if this index is the PRIMARY KEY index
94310     ** of a WITHOUT ROWID table and there has been no change the
94311     ** primary key, then no collision is possible.  The collision detection
94312     ** logic below can all be skipped. */
94313     if( isUpdate && pPk==pIdx && pkChng==0 ){
94314       sqlite3VdbeResolveLabel(v, addrUniqueOk);
94315       continue;
94316     }
94317 
94318     /* Find out what action to take in case there is a uniqueness conflict */
94319     onError = pIdx->onError;
94320     if( onError==OE_None ){
94321       sqlite3ReleaseTempRange(pParse, regIdx, pIdx->nColumn);
94322       sqlite3VdbeResolveLabel(v, addrUniqueOk);
94323       continue;  /* pIdx is not a UNIQUE index */
94324     }
94325     if( overrideError!=OE_Default ){
94326       onError = overrideError;
94327     }else if( onError==OE_Default ){
94328       onError = OE_Abort;
94329     }
94330 
94331     /* Check to see if the new index entry will be unique */
94332     sqlite3VdbeAddOp4Int(v, OP_NoConflict, iThisCur, addrUniqueOk,
94333                          regIdx, pIdx->nKeyCol);
94334 
94335     /* Generate code to handle collisions */
94336     regR = (pIdx==pPk) ? regIdx : sqlite3GetTempRange(pParse, nPkField);
94337     if( isUpdate || onError==OE_Replace ){
94338       if( HasRowid(pTab) ){
94339         sqlite3VdbeAddOp2(v, OP_IdxRowid, iThisCur, regR);
94340         /* Conflict only if the rowid of the existing index entry
94341         ** is different from old-rowid */
94342         if( isUpdate ){
94343           sqlite3VdbeAddOp3(v, OP_Eq, regR, addrUniqueOk, regOldData);
94344         }
94345       }else{
94346         int x;
94347         /* Extract the PRIMARY KEY from the end of the index entry and
94348         ** store it in registers regR..regR+nPk-1 */
94349         if( pIdx!=pPk ){
94350           for(i=0; i<pPk->nKeyCol; i++){
94351             x = sqlite3ColumnOfIndex(pIdx, pPk->aiColumn[i]);
94352             sqlite3VdbeAddOp3(v, OP_Column, iThisCur, x, regR+i);
94353             VdbeComment((v, "%s.%s", pTab->zName,
94354                          pTab->aCol[pPk->aiColumn[i]].zName));
94355           }
94356         }
94357         if( isUpdate ){
94358           /* If currently processing the PRIMARY KEY of a WITHOUT ROWID
94359           ** table, only conflict if the new PRIMARY KEY values are actually
94360           ** different from the old.
94361           **
94362           ** For a UNIQUE index, only conflict if the PRIMARY KEY values
94363           ** of the matched index row are different from the original PRIMARY
94364           ** KEY values of this row before the update.  */
94365           int addrJump = sqlite3VdbeCurrentAddr(v)+pPk->nKeyCol;
94366           int op = OP_Ne;
94367           int regCmp = (pIdx->autoIndex==2 ? regIdx : regR);
94368 
94369           for(i=0; i<pPk->nKeyCol; i++){
94370             char *p4 = (char*)sqlite3LocateCollSeq(pParse, pPk->azColl[i]);
94371             x = pPk->aiColumn[i];
94372             if( i==(pPk->nKeyCol-1) ){
94373               addrJump = addrUniqueOk;
94374               op = OP_Eq;
94375             }
94376             sqlite3VdbeAddOp4(v, op,
94377                 regOldData+1+x, addrJump, regCmp+i, p4, P4_COLLSEQ
94378             );
94379           }
94380         }
94381       }
94382     }
94383 
94384     /* Generate code that executes if the new index entry is not unique */
94385     assert( onError==OE_Rollback || onError==OE_Abort || onError==OE_Fail
94386         || onError==OE_Ignore || onError==OE_Replace );
94387     switch( onError ){
94388       case OE_Rollback:
94389       case OE_Abort:
94390       case OE_Fail: {
94391         sqlite3UniqueConstraint(pParse, onError, pIdx);
94392         break;
94393       }
94394       case OE_Ignore: {
94395         sqlite3VdbeAddOp2(v, OP_Goto, 0, ignoreDest);
94396         break;
94397       }
94398       default: {
94399         Trigger *pTrigger = 0;
94400         assert( onError==OE_Replace );
94401         sqlite3MultiWrite(pParse);
94402         if( db->flags&SQLITE_RecTriggers ){
94403           pTrigger = sqlite3TriggersExist(pParse, pTab, TK_DELETE, 0, 0);
94404         }
94405         sqlite3GenerateRowDelete(pParse, pTab, pTrigger, iDataCur, iIdxCur,
94406                                  regR, nPkField, 0, OE_Replace, pIdx==pPk);
94407         seenReplace = 1;
94408         break;
94409       }
94410     }
94411     sqlite3VdbeResolveLabel(v, addrUniqueOk);
94412     sqlite3ReleaseTempRange(pParse, regIdx, pIdx->nColumn);
94413     if( regR!=regIdx ) sqlite3ReleaseTempRange(pParse, regR, nPkField);
94414   }
94415   if( ipkTop ){
94416     sqlite3VdbeAddOp2(v, OP_Goto, 0, ipkTop+1);
94417     sqlite3VdbeJumpHere(v, ipkBottom);
94418   }
94419 
94420   *pbMayReplace = seenReplace;
94421   VdbeModuleComment((v, "END: GenCnstCks(%d)", seenReplace));
94422 }
94423 
94424 /*
94425 ** This routine generates code to finish the INSERT or UPDATE operation
94426 ** that was started by a prior call to sqlite3GenerateConstraintChecks.
94427 ** A consecutive range of registers starting at regNewData contains the
94428 ** rowid and the content to be inserted.
94429 **
94430 ** The arguments to this routine should be the same as the first six
94431 ** arguments to sqlite3GenerateConstraintChecks.
94432 */
94433 SQLITE_PRIVATE void sqlite3CompleteInsertion(
94434   Parse *pParse,      /* The parser context */
94435   Table *pTab,        /* the table into which we are inserting */
94436   int iDataCur,       /* Cursor of the canonical data source */
94437   int iIdxCur,        /* First index cursor */
94438   int regNewData,     /* Range of content */
94439   int *aRegIdx,       /* Register used by each index.  0 for unused indices */
94440   int isUpdate,       /* True for UPDATE, False for INSERT */
94441   int appendBias,     /* True if this is likely to be an append */
94442   int useSeekResult   /* True to set the USESEEKRESULT flag on OP_[Idx]Insert */
94443 ){
94444   Vdbe *v;            /* Prepared statements under construction */
94445   Index *pIdx;        /* An index being inserted or updated */
94446   u8 pik_flags;       /* flag values passed to the btree insert */
94447   int regData;        /* Content registers (after the rowid) */
94448   int regRec;         /* Register holding assemblied record for the table */
94449   int i;              /* Loop counter */
94450 
94451   v = sqlite3GetVdbe(pParse);
94452   assert( v!=0 );
94453   assert( pTab->pSelect==0 );  /* This table is not a VIEW */
94454   for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
94455     if( aRegIdx[i]==0 ) continue;
94456     if( pIdx->pPartIdxWhere ){
94457       sqlite3VdbeAddOp2(v, OP_IsNull, aRegIdx[i], sqlite3VdbeCurrentAddr(v)+2);
94458     }
94459     sqlite3VdbeAddOp2(v, OP_IdxInsert, iIdxCur+i, aRegIdx[i]);
94460     pik_flags = 0;
94461     if( useSeekResult ) pik_flags = OPFLAG_USESEEKRESULT;
94462     if( pIdx->autoIndex==2 && !HasRowid(pTab) ){
94463       assert( pParse->nested==0 );
94464       pik_flags |= OPFLAG_NCHANGE;
94465     }
94466     if( pik_flags )  sqlite3VdbeChangeP5(v, pik_flags);
94467   }
94468   if( !HasRowid(pTab) ) return;
94469   regData = regNewData + 1;
94470   regRec = sqlite3GetTempReg(pParse);
94471   sqlite3VdbeAddOp3(v, OP_MakeRecord, regData, pTab->nCol, regRec);
94472   sqlite3TableAffinityStr(v, pTab);
94473   sqlite3ExprCacheAffinityChange(pParse, regData, pTab->nCol);
94474   if( pParse->nested ){
94475     pik_flags = 0;
94476   }else{
94477     pik_flags = OPFLAG_NCHANGE;
94478     pik_flags |= (isUpdate?OPFLAG_ISUPDATE:OPFLAG_LASTROWID);
94479   }
94480   if( appendBias ){
94481     pik_flags |= OPFLAG_APPEND;
94482   }
94483   if( useSeekResult ){
94484     pik_flags |= OPFLAG_USESEEKRESULT;
94485   }
94486   sqlite3VdbeAddOp3(v, OP_Insert, iDataCur, regRec, regNewData);
94487   if( !pParse->nested ){
94488     sqlite3VdbeChangeP4(v, -1, pTab->zName, P4_TRANSIENT);
94489   }
94490   sqlite3VdbeChangeP5(v, pik_flags);
94491 }
94492 
94493 /*
94494 ** Allocate cursors for the pTab table and all its indices and generate
94495 ** code to open and initialized those cursors.
94496 **
94497 ** The cursor for the object that contains the complete data (normally
94498 ** the table itself, but the PRIMARY KEY index in the case of a WITHOUT
94499 ** ROWID table) is returned in *piDataCur.  The first index cursor is
94500 ** returned in *piIdxCur.  The number of indices is returned.
94501 **
94502 ** Use iBase as the first cursor (either the *piDataCur for rowid tables
94503 ** or the first index for WITHOUT ROWID tables) if it is non-negative.
94504 ** If iBase is negative, then allocate the next available cursor.
94505 **
94506 ** For a rowid table, *piDataCur will be exactly one less than *piIdxCur.
94507 ** For a WITHOUT ROWID table, *piDataCur will be somewhere in the range
94508 ** of *piIdxCurs, depending on where the PRIMARY KEY index appears on the
94509 ** pTab->pIndex list.
94510 */
94511 SQLITE_PRIVATE int sqlite3OpenTableAndIndices(
94512   Parse *pParse,   /* Parsing context */
94513   Table *pTab,     /* Table to be opened */
94514   int op,          /* OP_OpenRead or OP_OpenWrite */
94515   int iBase,       /* Use this for the table cursor, if there is one */
94516   u8 *aToOpen,     /* If not NULL: boolean for each table and index */
94517   int *piDataCur,  /* Write the database source cursor number here */
94518   int *piIdxCur    /* Write the first index cursor number here */
94519 ){
94520   int i;
94521   int iDb;
94522   int iDataCur;
94523   Index *pIdx;
94524   Vdbe *v;
94525 
94526   assert( op==OP_OpenRead || op==OP_OpenWrite );
94527   if( IsVirtual(pTab) ){
94528     assert( aToOpen==0 );
94529     *piDataCur = 0;
94530     *piIdxCur = 1;
94531     return 0;
94532   }
94533   iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
94534   v = sqlite3GetVdbe(pParse);
94535   assert( v!=0 );
94536   if( iBase<0 ) iBase = pParse->nTab;
94537   iDataCur = iBase++;
94538   if( piDataCur ) *piDataCur = iDataCur;
94539   if( HasRowid(pTab) && (aToOpen==0 || aToOpen[0]) ){
94540     sqlite3OpenTable(pParse, iDataCur, iDb, pTab, op);
94541   }else{
94542     sqlite3TableLock(pParse, iDb, pTab->tnum, op==OP_OpenWrite, pTab->zName);
94543   }
94544   if( piIdxCur ) *piIdxCur = iBase;
94545   for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
94546     int iIdxCur = iBase++;
94547     assert( pIdx->pSchema==pTab->pSchema );
94548     if( pIdx->autoIndex==2 && !HasRowid(pTab) && piDataCur ){
94549       *piDataCur = iIdxCur;
94550     }
94551     if( aToOpen==0 || aToOpen[i+1] ){
94552       sqlite3VdbeAddOp3(v, op, iIdxCur, pIdx->tnum, iDb);
94553       sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
94554       VdbeComment((v, "%s", pIdx->zName));
94555     }
94556   }
94557   if( iBase>pParse->nTab ) pParse->nTab = iBase;
94558   return i;
94559 }
94560 
94561 
94562 #ifdef SQLITE_TEST
94563 /*
94564 ** The following global variable is incremented whenever the
94565 ** transfer optimization is used.  This is used for testing
94566 ** purposes only - to make sure the transfer optimization really
94567 ** is happening when it is suppose to.
94568 */
94569 SQLITE_API int sqlite3_xferopt_count;
94570 #endif /* SQLITE_TEST */
94571 
94572 
94573 #ifndef SQLITE_OMIT_XFER_OPT
94574 /*
94575 ** Check to collation names to see if they are compatible.
94576 */
94577 static int xferCompatibleCollation(const char *z1, const char *z2){
94578   if( z1==0 ){
94579     return z2==0;
94580   }
94581   if( z2==0 ){
94582     return 0;
94583   }
94584   return sqlite3StrICmp(z1, z2)==0;
94585 }
94586 
94587 
94588 /*
94589 ** Check to see if index pSrc is compatible as a source of data
94590 ** for index pDest in an insert transfer optimization.  The rules
94591 ** for a compatible index:
94592 **
94593 **    *   The index is over the same set of columns
94594 **    *   The same DESC and ASC markings occurs on all columns
94595 **    *   The same onError processing (OE_Abort, OE_Ignore, etc)
94596 **    *   The same collating sequence on each column
94597 **    *   The index has the exact same WHERE clause
94598 */
94599 static int xferCompatibleIndex(Index *pDest, Index *pSrc){
94600   int i;
94601   assert( pDest && pSrc );
94602   assert( pDest->pTable!=pSrc->pTable );
94603   if( pDest->nKeyCol!=pSrc->nKeyCol ){
94604     return 0;   /* Different number of columns */
94605   }
94606   if( pDest->onError!=pSrc->onError ){
94607     return 0;   /* Different conflict resolution strategies */
94608   }
94609   for(i=0; i<pSrc->nKeyCol; i++){
94610     if( pSrc->aiColumn[i]!=pDest->aiColumn[i] ){
94611       return 0;   /* Different columns indexed */
94612     }
94613     if( pSrc->aSortOrder[i]!=pDest->aSortOrder[i] ){
94614       return 0;   /* Different sort orders */
94615     }
94616     if( !xferCompatibleCollation(pSrc->azColl[i],pDest->azColl[i]) ){
94617       return 0;   /* Different collating sequences */
94618     }
94619   }
94620   if( sqlite3ExprCompare(pSrc->pPartIdxWhere, pDest->pPartIdxWhere, -1) ){
94621     return 0;     /* Different WHERE clauses */
94622   }
94623 
94624   /* If no test above fails then the indices must be compatible */
94625   return 1;
94626 }
94627 
94628 /*
94629 ** Attempt the transfer optimization on INSERTs of the form
94630 **
94631 **     INSERT INTO tab1 SELECT * FROM tab2;
94632 **
94633 ** The xfer optimization transfers raw records from tab2 over to tab1.
94634 ** Columns are not decoded and reassemblied, which greatly improves
94635 ** performance.  Raw index records are transferred in the same way.
94636 **
94637 ** The xfer optimization is only attempted if tab1 and tab2 are compatible.
94638 ** There are lots of rules for determining compatibility - see comments
94639 ** embedded in the code for details.
94640 **
94641 ** This routine returns TRUE if the optimization is guaranteed to be used.
94642 ** Sometimes the xfer optimization will only work if the destination table
94643 ** is empty - a factor that can only be determined at run-time.  In that
94644 ** case, this routine generates code for the xfer optimization but also
94645 ** does a test to see if the destination table is empty and jumps over the
94646 ** xfer optimization code if the test fails.  In that case, this routine
94647 ** returns FALSE so that the caller will know to go ahead and generate
94648 ** an unoptimized transfer.  This routine also returns FALSE if there
94649 ** is no chance that the xfer optimization can be applied.
94650 **
94651 ** This optimization is particularly useful at making VACUUM run faster.
94652 */
94653 static int xferOptimization(
94654   Parse *pParse,        /* Parser context */
94655   Table *pDest,         /* The table we are inserting into */
94656   Select *pSelect,      /* A SELECT statement to use as the data source */
94657   int onError,          /* How to handle constraint errors */
94658   int iDbDest           /* The database of pDest */
94659 ){
94660   ExprList *pEList;                /* The result set of the SELECT */
94661   Table *pSrc;                     /* The table in the FROM clause of SELECT */
94662   Index *pSrcIdx, *pDestIdx;       /* Source and destination indices */
94663   struct SrcList_item *pItem;      /* An element of pSelect->pSrc */
94664   int i;                           /* Loop counter */
94665   int iDbSrc;                      /* The database of pSrc */
94666   int iSrc, iDest;                 /* Cursors from source and destination */
94667   int addr1, addr2;                /* Loop addresses */
94668   int emptyDestTest = 0;           /* Address of test for empty pDest */
94669   int emptySrcTest = 0;            /* Address of test for empty pSrc */
94670   Vdbe *v;                         /* The VDBE we are building */
94671   int regAutoinc;                  /* Memory register used by AUTOINC */
94672   int destHasUniqueIdx = 0;        /* True if pDest has a UNIQUE index */
94673   int regData, regRowid;           /* Registers holding data and rowid */
94674 
94675   if( pSelect==0 ){
94676     return 0;   /* Must be of the form  INSERT INTO ... SELECT ... */
94677   }
94678   if( pParse->pWith || pSelect->pWith ){
94679     /* Do not attempt to process this query if there are an WITH clauses
94680     ** attached to it. Proceeding may generate a false "no such table: xxx"
94681     ** error if pSelect reads from a CTE named "xxx".  */
94682     return 0;
94683   }
94684   if( sqlite3TriggerList(pParse, pDest) ){
94685     return 0;   /* tab1 must not have triggers */
94686   }
94687 #ifndef SQLITE_OMIT_VIRTUALTABLE
94688   if( pDest->tabFlags & TF_Virtual ){
94689     return 0;   /* tab1 must not be a virtual table */
94690   }
94691 #endif
94692   if( onError==OE_Default ){
94693     if( pDest->iPKey>=0 ) onError = pDest->keyConf;
94694     if( onError==OE_Default ) onError = OE_Abort;
94695   }
94696   assert(pSelect->pSrc);   /* allocated even if there is no FROM clause */
94697   if( pSelect->pSrc->nSrc!=1 ){
94698     return 0;   /* FROM clause must have exactly one term */
94699   }
94700   if( pSelect->pSrc->a[0].pSelect ){
94701     return 0;   /* FROM clause cannot contain a subquery */
94702   }
94703   if( pSelect->pWhere ){
94704     return 0;   /* SELECT may not have a WHERE clause */
94705   }
94706   if( pSelect->pOrderBy ){
94707     return 0;   /* SELECT may not have an ORDER BY clause */
94708   }
94709   /* Do not need to test for a HAVING clause.  If HAVING is present but
94710   ** there is no ORDER BY, we will get an error. */
94711   if( pSelect->pGroupBy ){
94712     return 0;   /* SELECT may not have a GROUP BY clause */
94713   }
94714   if( pSelect->pLimit ){
94715     return 0;   /* SELECT may not have a LIMIT clause */
94716   }
94717   assert( pSelect->pOffset==0 );  /* Must be so if pLimit==0 */
94718   if( pSelect->pPrior ){
94719     return 0;   /* SELECT may not be a compound query */
94720   }
94721   if( pSelect->selFlags & SF_Distinct ){
94722     return 0;   /* SELECT may not be DISTINCT */
94723   }
94724   pEList = pSelect->pEList;
94725   assert( pEList!=0 );
94726   if( pEList->nExpr!=1 ){
94727     return 0;   /* The result set must have exactly one column */
94728   }
94729   assert( pEList->a[0].pExpr );
94730   if( pEList->a[0].pExpr->op!=TK_ALL ){
94731     return 0;   /* The result set must be the special operator "*" */
94732   }
94733 
94734   /* At this point we have established that the statement is of the
94735   ** correct syntactic form to participate in this optimization.  Now
94736   ** we have to check the semantics.
94737   */
94738   pItem = pSelect->pSrc->a;
94739   pSrc = sqlite3LocateTableItem(pParse, 0, pItem);
94740   if( pSrc==0 ){
94741     return 0;   /* FROM clause does not contain a real table */
94742   }
94743   if( pSrc==pDest ){
94744     return 0;   /* tab1 and tab2 may not be the same table */
94745   }
94746   if( HasRowid(pDest)!=HasRowid(pSrc) ){
94747     return 0;   /* source and destination must both be WITHOUT ROWID or not */
94748   }
94749 #ifndef SQLITE_OMIT_VIRTUALTABLE
94750   if( pSrc->tabFlags & TF_Virtual ){
94751     return 0;   /* tab2 must not be a virtual table */
94752   }
94753 #endif
94754   if( pSrc->pSelect ){
94755     return 0;   /* tab2 may not be a view */
94756   }
94757   if( pDest->nCol!=pSrc->nCol ){
94758     return 0;   /* Number of columns must be the same in tab1 and tab2 */
94759   }
94760   if( pDest->iPKey!=pSrc->iPKey ){
94761     return 0;   /* Both tables must have the same INTEGER PRIMARY KEY */
94762   }
94763   for(i=0; i<pDest->nCol; i++){
94764     if( pDest->aCol[i].affinity!=pSrc->aCol[i].affinity ){
94765       return 0;    /* Affinity must be the same on all columns */
94766     }
94767     if( !xferCompatibleCollation(pDest->aCol[i].zColl, pSrc->aCol[i].zColl) ){
94768       return 0;    /* Collating sequence must be the same on all columns */
94769     }
94770     if( pDest->aCol[i].notNull && !pSrc->aCol[i].notNull ){
94771       return 0;    /* tab2 must be NOT NULL if tab1 is */
94772     }
94773   }
94774   for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
94775     if( pDestIdx->onError!=OE_None ){
94776       destHasUniqueIdx = 1;
94777     }
94778     for(pSrcIdx=pSrc->pIndex; pSrcIdx; pSrcIdx=pSrcIdx->pNext){
94779       if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break;
94780     }
94781     if( pSrcIdx==0 ){
94782       return 0;    /* pDestIdx has no corresponding index in pSrc */
94783     }
94784   }
94785 #ifndef SQLITE_OMIT_CHECK
94786   if( pDest->pCheck && sqlite3ExprListCompare(pSrc->pCheck,pDest->pCheck,-1) ){
94787     return 0;   /* Tables have different CHECK constraints.  Ticket #2252 */
94788   }
94789 #endif
94790 #ifndef SQLITE_OMIT_FOREIGN_KEY
94791   /* Disallow the transfer optimization if the destination table constains
94792   ** any foreign key constraints.  This is more restrictive than necessary.
94793   ** But the main beneficiary of the transfer optimization is the VACUUM
94794   ** command, and the VACUUM command disables foreign key constraints.  So
94795   ** the extra complication to make this rule less restrictive is probably
94796   ** not worth the effort.  Ticket [6284df89debdfa61db8073e062908af0c9b6118e]
94797   */
94798   if( (pParse->db->flags & SQLITE_ForeignKeys)!=0 && pDest->pFKey!=0 ){
94799     return 0;
94800   }
94801 #endif
94802   if( (pParse->db->flags & SQLITE_CountRows)!=0 ){
94803     return 0;  /* xfer opt does not play well with PRAGMA count_changes */
94804   }
94805 
94806   /* If we get this far, it means that the xfer optimization is at
94807   ** least a possibility, though it might only work if the destination
94808   ** table (tab1) is initially empty.
94809   */
94810 #ifdef SQLITE_TEST
94811   sqlite3_xferopt_count++;
94812 #endif
94813   iDbSrc = sqlite3SchemaToIndex(pParse->db, pSrc->pSchema);
94814   v = sqlite3GetVdbe(pParse);
94815   sqlite3CodeVerifySchema(pParse, iDbSrc);
94816   iSrc = pParse->nTab++;
94817   iDest = pParse->nTab++;
94818   regAutoinc = autoIncBegin(pParse, iDbDest, pDest);
94819   regData = sqlite3GetTempReg(pParse);
94820   regRowid = sqlite3GetTempReg(pParse);
94821   sqlite3OpenTable(pParse, iDest, iDbDest, pDest, OP_OpenWrite);
94822   assert( HasRowid(pDest) || destHasUniqueIdx );
94823   if( (pDest->iPKey<0 && pDest->pIndex!=0)          /* (1) */
94824    || destHasUniqueIdx                              /* (2) */
94825    || (onError!=OE_Abort && onError!=OE_Rollback)   /* (3) */
94826   ){
94827     /* In some circumstances, we are able to run the xfer optimization
94828     ** only if the destination table is initially empty.  This code makes
94829     ** that determination.  Conditions under which the destination must
94830     ** be empty:
94831     **
94832     ** (1) There is no INTEGER PRIMARY KEY but there are indices.
94833     **     (If the destination is not initially empty, the rowid fields
94834     **     of index entries might need to change.)
94835     **
94836     ** (2) The destination has a unique index.  (The xfer optimization
94837     **     is unable to test uniqueness.)
94838     **
94839     ** (3) onError is something other than OE_Abort and OE_Rollback.
94840     */
94841     addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iDest, 0);
94842     emptyDestTest = sqlite3VdbeAddOp2(v, OP_Goto, 0, 0);
94843     sqlite3VdbeJumpHere(v, addr1);
94844   }
94845   if( HasRowid(pSrc) ){
94846     sqlite3OpenTable(pParse, iSrc, iDbSrc, pSrc, OP_OpenRead);
94847     emptySrcTest = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0);
94848     if( pDest->iPKey>=0 ){
94849       addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid);
94850       addr2 = sqlite3VdbeAddOp3(v, OP_NotExists, iDest, 0, regRowid);
94851       sqlite3RowidConstraint(pParse, onError, pDest);
94852       sqlite3VdbeJumpHere(v, addr2);
94853       autoIncStep(pParse, regAutoinc, regRowid);
94854     }else if( pDest->pIndex==0 ){
94855       addr1 = sqlite3VdbeAddOp2(v, OP_NewRowid, iDest, regRowid);
94856     }else{
94857       addr1 = sqlite3VdbeAddOp2(v, OP_Rowid, iSrc, regRowid);
94858       assert( (pDest->tabFlags & TF_Autoincrement)==0 );
94859     }
94860     sqlite3VdbeAddOp2(v, OP_RowData, iSrc, regData);
94861     sqlite3VdbeAddOp3(v, OP_Insert, iDest, regData, regRowid);
94862     sqlite3VdbeChangeP5(v, OPFLAG_NCHANGE|OPFLAG_LASTROWID|OPFLAG_APPEND);
94863     sqlite3VdbeChangeP4(v, -1, pDest->zName, 0);
94864     sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1);
94865     sqlite3VdbeAddOp2(v, OP_Close, iSrc, 0);
94866     sqlite3VdbeAddOp2(v, OP_Close, iDest, 0);
94867   }else{
94868     sqlite3TableLock(pParse, iDbDest, pDest->tnum, 1, pDest->zName);
94869     sqlite3TableLock(pParse, iDbSrc, pSrc->tnum, 0, pSrc->zName);
94870   }
94871   for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
94872     for(pSrcIdx=pSrc->pIndex; ALWAYS(pSrcIdx); pSrcIdx=pSrcIdx->pNext){
94873       if( xferCompatibleIndex(pDestIdx, pSrcIdx) ) break;
94874     }
94875     assert( pSrcIdx );
94876     sqlite3VdbeAddOp3(v, OP_OpenRead, iSrc, pSrcIdx->tnum, iDbSrc);
94877     sqlite3VdbeSetP4KeyInfo(pParse, pSrcIdx);
94878     VdbeComment((v, "%s", pSrcIdx->zName));
94879     sqlite3VdbeAddOp3(v, OP_OpenWrite, iDest, pDestIdx->tnum, iDbDest);
94880     sqlite3VdbeSetP4KeyInfo(pParse, pDestIdx);
94881     sqlite3VdbeChangeP5(v, OPFLAG_BULKCSR);
94882     VdbeComment((v, "%s", pDestIdx->zName));
94883     addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iSrc, 0);
94884     sqlite3VdbeAddOp2(v, OP_RowKey, iSrc, regData);
94885     sqlite3VdbeAddOp3(v, OP_IdxInsert, iDest, regData, 1);
94886     sqlite3VdbeAddOp2(v, OP_Next, iSrc, addr1+1);
94887     sqlite3VdbeJumpHere(v, addr1);
94888     sqlite3VdbeAddOp2(v, OP_Close, iSrc, 0);
94889     sqlite3VdbeAddOp2(v, OP_Close, iDest, 0);
94890   }
94891   sqlite3VdbeJumpHere(v, emptySrcTest);
94892   sqlite3ReleaseTempReg(pParse, regRowid);
94893   sqlite3ReleaseTempReg(pParse, regData);
94894   if( emptyDestTest ){
94895     sqlite3VdbeAddOp2(v, OP_Halt, SQLITE_OK, 0);
94896     sqlite3VdbeJumpHere(v, emptyDestTest);
94897     sqlite3VdbeAddOp2(v, OP_Close, iDest, 0);
94898     return 0;
94899   }else{
94900     return 1;
94901   }
94902 }
94903 #endif /* SQLITE_OMIT_XFER_OPT */
94904 
94905 /************** End of insert.c **********************************************/
94906 /************** Begin file legacy.c ******************************************/
94907 /*
94908 ** 2001 September 15
94909 **
94910 ** The author disclaims copyright to this source code.  In place of
94911 ** a legal notice, here is a blessing:
94912 **
94913 **    May you do good and not evil.
94914 **    May you find forgiveness for yourself and forgive others.
94915 **    May you share freely, never taking more than you give.
94916 **
94917 *************************************************************************
94918 ** Main file for the SQLite library.  The routines in this file
94919 ** implement the programmer interface to the library.  Routines in
94920 ** other files are for internal use by SQLite and should not be
94921 ** accessed by users of the library.
94922 */
94923 
94924 
94925 /*
94926 ** Execute SQL code.  Return one of the SQLITE_ success/failure
94927 ** codes.  Also write an error message into memory obtained from
94928 ** malloc() and make *pzErrMsg point to that message.
94929 **
94930 ** If the SQL is a query, then for each row in the query result
94931 ** the xCallback() function is called.  pArg becomes the first
94932 ** argument to xCallback().  If xCallback=NULL then no callback
94933 ** is invoked, even for queries.
94934 */
94935 SQLITE_API int sqlite3_exec(
94936   sqlite3 *db,                /* The database on which the SQL executes */
94937   const char *zSql,           /* The SQL to be executed */
94938   sqlite3_callback xCallback, /* Invoke this callback routine */
94939   void *pArg,                 /* First argument to xCallback() */
94940   char **pzErrMsg             /* Write error messages here */
94941 ){
94942   int rc = SQLITE_OK;         /* Return code */
94943   const char *zLeftover;      /* Tail of unprocessed SQL */
94944   sqlite3_stmt *pStmt = 0;    /* The current SQL statement */
94945   char **azCols = 0;          /* Names of result columns */
94946   int callbackIsInit;         /* True if callback data is initialized */
94947 
94948   if( !sqlite3SafetyCheckOk(db) ) return SQLITE_MISUSE_BKPT;
94949   if( zSql==0 ) zSql = "";
94950 
94951   sqlite3_mutex_enter(db->mutex);
94952   sqlite3Error(db, SQLITE_OK, 0);
94953   while( rc==SQLITE_OK && zSql[0] ){
94954     int nCol;
94955     char **azVals = 0;
94956 
94957     pStmt = 0;
94958     rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zLeftover);
94959     assert( rc==SQLITE_OK || pStmt==0 );
94960     if( rc!=SQLITE_OK ){
94961       continue;
94962     }
94963     if( !pStmt ){
94964       /* this happens for a comment or white-space */
94965       zSql = zLeftover;
94966       continue;
94967     }
94968 
94969     callbackIsInit = 0;
94970     nCol = sqlite3_column_count(pStmt);
94971 
94972     while( 1 ){
94973       int i;
94974       rc = sqlite3_step(pStmt);
94975 
94976       /* Invoke the callback function if required */
94977       if( xCallback && (SQLITE_ROW==rc ||
94978           (SQLITE_DONE==rc && !callbackIsInit
94979                            && db->flags&SQLITE_NullCallback)) ){
94980         if( !callbackIsInit ){
94981           azCols = sqlite3DbMallocZero(db, 2*nCol*sizeof(const char*) + 1);
94982           if( azCols==0 ){
94983             goto exec_out;
94984           }
94985           for(i=0; i<nCol; i++){
94986             azCols[i] = (char *)sqlite3_column_name(pStmt, i);
94987             /* sqlite3VdbeSetColName() installs column names as UTF8
94988             ** strings so there is no way for sqlite3_column_name() to fail. */
94989             assert( azCols[i]!=0 );
94990           }
94991           callbackIsInit = 1;
94992         }
94993         if( rc==SQLITE_ROW ){
94994           azVals = &azCols[nCol];
94995           for(i=0; i<nCol; i++){
94996             azVals[i] = (char *)sqlite3_column_text(pStmt, i);
94997             if( !azVals[i] && sqlite3_column_type(pStmt, i)!=SQLITE_NULL ){
94998               db->mallocFailed = 1;
94999               goto exec_out;
95000             }
95001           }
95002         }
95003         if( xCallback(pArg, nCol, azVals, azCols) ){
95004           rc = SQLITE_ABORT;
95005           sqlite3VdbeFinalize((Vdbe *)pStmt);
95006           pStmt = 0;
95007           sqlite3Error(db, SQLITE_ABORT, 0);
95008           goto exec_out;
95009         }
95010       }
95011 
95012       if( rc!=SQLITE_ROW ){
95013         rc = sqlite3VdbeFinalize((Vdbe *)pStmt);
95014         pStmt = 0;
95015         zSql = zLeftover;
95016         while( sqlite3Isspace(zSql[0]) ) zSql++;
95017         break;
95018       }
95019     }
95020 
95021     sqlite3DbFree(db, azCols);
95022     azCols = 0;
95023   }
95024 
95025 exec_out:
95026   if( pStmt ) sqlite3VdbeFinalize((Vdbe *)pStmt);
95027   sqlite3DbFree(db, azCols);
95028 
95029   rc = sqlite3ApiExit(db, rc);
95030   if( rc!=SQLITE_OK && ALWAYS(rc==sqlite3_errcode(db)) && pzErrMsg ){
95031     int nErrMsg = 1 + sqlite3Strlen30(sqlite3_errmsg(db));
95032     *pzErrMsg = sqlite3Malloc(nErrMsg);
95033     if( *pzErrMsg ){
95034       memcpy(*pzErrMsg, sqlite3_errmsg(db), nErrMsg);
95035     }else{
95036       rc = SQLITE_NOMEM;
95037       sqlite3Error(db, SQLITE_NOMEM, 0);
95038     }
95039   }else if( pzErrMsg ){
95040     *pzErrMsg = 0;
95041   }
95042 
95043   assert( (rc&db->errMask)==rc );
95044   sqlite3_mutex_leave(db->mutex);
95045   return rc;
95046 }
95047 
95048 /************** End of legacy.c **********************************************/
95049 /************** Begin file loadext.c *****************************************/
95050 /*
95051 ** 2006 June 7
95052 **
95053 ** The author disclaims copyright to this source code.  In place of
95054 ** a legal notice, here is a blessing:
95055 **
95056 **    May you do good and not evil.
95057 **    May you find forgiveness for yourself and forgive others.
95058 **    May you share freely, never taking more than you give.
95059 **
95060 *************************************************************************
95061 ** This file contains code used to dynamically load extensions into
95062 ** the SQLite library.
95063 */
95064 
95065 #ifndef SQLITE_CORE
95066   #define SQLITE_CORE 1  /* Disable the API redefinition in sqlite3ext.h */
95067 #endif
95068 /************** Include sqlite3ext.h in the middle of loadext.c **************/
95069 /************** Begin file sqlite3ext.h **************************************/
95070 /*
95071 ** 2006 June 7
95072 **
95073 ** The author disclaims copyright to this source code.  In place of
95074 ** a legal notice, here is a blessing:
95075 **
95076 **    May you do good and not evil.
95077 **    May you find forgiveness for yourself and forgive others.
95078 **    May you share freely, never taking more than you give.
95079 **
95080 *************************************************************************
95081 ** This header file defines the SQLite interface for use by
95082 ** shared libraries that want to be imported as extensions into
95083 ** an SQLite instance.  Shared libraries that intend to be loaded
95084 ** as extensions by SQLite should #include this file instead of
95085 ** sqlite3.h.
95086 */
95087 #ifndef _SQLITE3EXT_H_
95088 #define _SQLITE3EXT_H_
95089 
95090 typedef struct sqlite3_api_routines sqlite3_api_routines;
95091 
95092 /*
95093 ** The following structure holds pointers to all of the SQLite API
95094 ** routines.
95095 **
95096 ** WARNING:  In order to maintain backwards compatibility, add new
95097 ** interfaces to the end of this structure only.  If you insert new
95098 ** interfaces in the middle of this structure, then older different
95099 ** versions of SQLite will not be able to load each others' shared
95100 ** libraries!
95101 */
95102 struct sqlite3_api_routines {
95103   void * (*aggregate_context)(sqlite3_context*,int nBytes);
95104   int  (*aggregate_count)(sqlite3_context*);
95105   int  (*bind_blob)(sqlite3_stmt*,int,const void*,int n,void(*)(void*));
95106   int  (*bind_double)(sqlite3_stmt*,int,double);
95107   int  (*bind_int)(sqlite3_stmt*,int,int);
95108   int  (*bind_int64)(sqlite3_stmt*,int,sqlite_int64);
95109   int  (*bind_null)(sqlite3_stmt*,int);
95110   int  (*bind_parameter_count)(sqlite3_stmt*);
95111   int  (*bind_parameter_index)(sqlite3_stmt*,const char*zName);
95112   const char * (*bind_parameter_name)(sqlite3_stmt*,int);
95113   int  (*bind_text)(sqlite3_stmt*,int,const char*,int n,void(*)(void*));
95114   int  (*bind_text16)(sqlite3_stmt*,int,const void*,int,void(*)(void*));
95115   int  (*bind_value)(sqlite3_stmt*,int,const sqlite3_value*);
95116   int  (*busy_handler)(sqlite3*,int(*)(void*,int),void*);
95117   int  (*busy_timeout)(sqlite3*,int ms);
95118   int  (*changes)(sqlite3*);
95119   int  (*close)(sqlite3*);
95120   int  (*collation_needed)(sqlite3*,void*,void(*)(void*,sqlite3*,
95121                            int eTextRep,const char*));
95122   int  (*collation_needed16)(sqlite3*,void*,void(*)(void*,sqlite3*,
95123                              int eTextRep,const void*));
95124   const void * (*column_blob)(sqlite3_stmt*,int iCol);
95125   int  (*column_bytes)(sqlite3_stmt*,int iCol);
95126   int  (*column_bytes16)(sqlite3_stmt*,int iCol);
95127   int  (*column_count)(sqlite3_stmt*pStmt);
95128   const char * (*column_database_name)(sqlite3_stmt*,int);
95129   const void * (*column_database_name16)(sqlite3_stmt*,int);
95130   const char * (*column_decltype)(sqlite3_stmt*,int i);
95131   const void * (*column_decltype16)(sqlite3_stmt*,int);
95132   double  (*column_double)(sqlite3_stmt*,int iCol);
95133   int  (*column_int)(sqlite3_stmt*,int iCol);
95134   sqlite_int64  (*column_int64)(sqlite3_stmt*,int iCol);
95135   const char * (*column_name)(sqlite3_stmt*,int);
95136   const void * (*column_name16)(sqlite3_stmt*,int);
95137   const char * (*column_origin_name)(sqlite3_stmt*,int);
95138   const void * (*column_origin_name16)(sqlite3_stmt*,int);
95139   const char * (*column_table_name)(sqlite3_stmt*,int);
95140   const void * (*column_table_name16)(sqlite3_stmt*,int);
95141   const unsigned char * (*column_text)(sqlite3_stmt*,int iCol);
95142   const void * (*column_text16)(sqlite3_stmt*,int iCol);
95143   int  (*column_type)(sqlite3_stmt*,int iCol);
95144   sqlite3_value* (*column_value)(sqlite3_stmt*,int iCol);
95145   void * (*commit_hook)(sqlite3*,int(*)(void*),void*);
95146   int  (*complete)(const char*sql);
95147   int  (*complete16)(const void*sql);
95148   int  (*create_collation)(sqlite3*,const char*,int,void*,
95149                            int(*)(void*,int,const void*,int,const void*));
95150   int  (*create_collation16)(sqlite3*,const void*,int,void*,
95151                              int(*)(void*,int,const void*,int,const void*));
95152   int  (*create_function)(sqlite3*,const char*,int,int,void*,
95153                           void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
95154                           void (*xStep)(sqlite3_context*,int,sqlite3_value**),
95155                           void (*xFinal)(sqlite3_context*));
95156   int  (*create_function16)(sqlite3*,const void*,int,int,void*,
95157                             void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
95158                             void (*xStep)(sqlite3_context*,int,sqlite3_value**),
95159                             void (*xFinal)(sqlite3_context*));
95160   int (*create_module)(sqlite3*,const char*,const sqlite3_module*,void*);
95161   int  (*data_count)(sqlite3_stmt*pStmt);
95162   sqlite3 * (*db_handle)(sqlite3_stmt*);
95163   int (*declare_vtab)(sqlite3*,const char*);
95164   int  (*enable_shared_cache)(int);
95165   int  (*errcode)(sqlite3*db);
95166   const char * (*errmsg)(sqlite3*);
95167   const void * (*errmsg16)(sqlite3*);
95168   int  (*exec)(sqlite3*,const char*,sqlite3_callback,void*,char**);
95169   int  (*expired)(sqlite3_stmt*);
95170   int  (*finalize)(sqlite3_stmt*pStmt);
95171   void  (*free)(void*);
95172   void  (*free_table)(char**result);
95173   int  (*get_autocommit)(sqlite3*);
95174   void * (*get_auxdata)(sqlite3_context*,int);
95175   int  (*get_table)(sqlite3*,const char*,char***,int*,int*,char**);
95176   int  (*global_recover)(void);
95177   void  (*interruptx)(sqlite3*);
95178   sqlite_int64  (*last_insert_rowid)(sqlite3*);
95179   const char * (*libversion)(void);
95180   int  (*libversion_number)(void);
95181   void *(*malloc)(int);
95182   char * (*mprintf)(const char*,...);
95183   int  (*open)(const char*,sqlite3**);
95184   int  (*open16)(const void*,sqlite3**);
95185   int  (*prepare)(sqlite3*,const char*,int,sqlite3_stmt**,const char**);
95186   int  (*prepare16)(sqlite3*,const void*,int,sqlite3_stmt**,const void**);
95187   void * (*profile)(sqlite3*,void(*)(void*,const char*,sqlite_uint64),void*);
95188   void  (*progress_handler)(sqlite3*,int,int(*)(void*),void*);
95189   void *(*realloc)(void*,int);
95190   int  (*reset)(sqlite3_stmt*pStmt);
95191   void  (*result_blob)(sqlite3_context*,const void*,int,void(*)(void*));
95192   void  (*result_double)(sqlite3_context*,double);
95193   void  (*result_error)(sqlite3_context*,const char*,int);
95194   void  (*result_error16)(sqlite3_context*,const void*,int);
95195   void  (*result_int)(sqlite3_context*,int);
95196   void  (*result_int64)(sqlite3_context*,sqlite_int64);
95197   void  (*result_null)(sqlite3_context*);
95198   void  (*result_text)(sqlite3_context*,const char*,int,void(*)(void*));
95199   void  (*result_text16)(sqlite3_context*,const void*,int,void(*)(void*));
95200   void  (*result_text16be)(sqlite3_context*,const void*,int,void(*)(void*));
95201   void  (*result_text16le)(sqlite3_context*,const void*,int,void(*)(void*));
95202   void  (*result_value)(sqlite3_context*,sqlite3_value*);
95203   void * (*rollback_hook)(sqlite3*,void(*)(void*),void*);
95204   int  (*set_authorizer)(sqlite3*,int(*)(void*,int,const char*,const char*,
95205                          const char*,const char*),void*);
95206   void  (*set_auxdata)(sqlite3_context*,int,void*,void (*)(void*));
95207   char * (*snprintf)(int,char*,const char*,...);
95208   int  (*step)(sqlite3_stmt*);
95209   int  (*table_column_metadata)(sqlite3*,const char*,const char*,const char*,
95210                                 char const**,char const**,int*,int*,int*);
95211   void  (*thread_cleanup)(void);
95212   int  (*total_changes)(sqlite3*);
95213   void * (*trace)(sqlite3*,void(*xTrace)(void*,const char*),void*);
95214   int  (*transfer_bindings)(sqlite3_stmt*,sqlite3_stmt*);
95215   void * (*update_hook)(sqlite3*,void(*)(void*,int ,char const*,char const*,
95216                                          sqlite_int64),void*);
95217   void * (*user_data)(sqlite3_context*);
95218   const void * (*value_blob)(sqlite3_value*);
95219   int  (*value_bytes)(sqlite3_value*);
95220   int  (*value_bytes16)(sqlite3_value*);
95221   double  (*value_double)(sqlite3_value*);
95222   int  (*value_int)(sqlite3_value*);
95223   sqlite_int64  (*value_int64)(sqlite3_value*);
95224   int  (*value_numeric_type)(sqlite3_value*);
95225   const unsigned char * (*value_text)(sqlite3_value*);
95226   const void * (*value_text16)(sqlite3_value*);
95227   const void * (*value_text16be)(sqlite3_value*);
95228   const void * (*value_text16le)(sqlite3_value*);
95229   int  (*value_type)(sqlite3_value*);
95230   char *(*vmprintf)(const char*,va_list);
95231   /* Added ??? */
95232   int (*overload_function)(sqlite3*, const char *zFuncName, int nArg);
95233   /* Added by 3.3.13 */
95234   int (*prepare_v2)(sqlite3*,const char*,int,sqlite3_stmt**,const char**);
95235   int (*prepare16_v2)(sqlite3*,const void*,int,sqlite3_stmt**,const void**);
95236   int (*clear_bindings)(sqlite3_stmt*);
95237   /* Added by 3.4.1 */
95238   int (*create_module_v2)(sqlite3*,const char*,const sqlite3_module*,void*,
95239                           void (*xDestroy)(void *));
95240   /* Added by 3.5.0 */
95241   int (*bind_zeroblob)(sqlite3_stmt*,int,int);
95242   int (*blob_bytes)(sqlite3_blob*);
95243   int (*blob_close)(sqlite3_blob*);
95244   int (*blob_open)(sqlite3*,const char*,const char*,const char*,sqlite3_int64,
95245                    int,sqlite3_blob**);
95246   int (*blob_read)(sqlite3_blob*,void*,int,int);
95247   int (*blob_write)(sqlite3_blob*,const void*,int,int);
95248   int (*create_collation_v2)(sqlite3*,const char*,int,void*,
95249                              int(*)(void*,int,const void*,int,const void*),
95250                              void(*)(void*));
95251   int (*file_control)(sqlite3*,const char*,int,void*);
95252   sqlite3_int64 (*memory_highwater)(int);
95253   sqlite3_int64 (*memory_used)(void);
95254   sqlite3_mutex *(*mutex_alloc)(int);
95255   void (*mutex_enter)(sqlite3_mutex*);
95256   void (*mutex_free)(sqlite3_mutex*);
95257   void (*mutex_leave)(sqlite3_mutex*);
95258   int (*mutex_try)(sqlite3_mutex*);
95259   int (*open_v2)(const char*,sqlite3**,int,const char*);
95260   int (*release_memory)(int);
95261   void (*result_error_nomem)(sqlite3_context*);
95262   void (*result_error_toobig)(sqlite3_context*);
95263   int (*sleep)(int);
95264   void (*soft_heap_limit)(int);
95265   sqlite3_vfs *(*vfs_find)(const char*);
95266   int (*vfs_register)(sqlite3_vfs*,int);
95267   int (*vfs_unregister)(sqlite3_vfs*);
95268   int (*xthreadsafe)(void);
95269   void (*result_zeroblob)(sqlite3_context*,int);
95270   void (*result_error_code)(sqlite3_context*,int);
95271   int (*test_control)(int, ...);
95272   void (*randomness)(int,void*);
95273   sqlite3 *(*context_db_handle)(sqlite3_context*);
95274   int (*extended_result_codes)(sqlite3*,int);
95275   int (*limit)(sqlite3*,int,int);
95276   sqlite3_stmt *(*next_stmt)(sqlite3*,sqlite3_stmt*);
95277   const char *(*sql)(sqlite3_stmt*);
95278   int (*status)(int,int*,int*,int);
95279   int (*backup_finish)(sqlite3_backup*);
95280   sqlite3_backup *(*backup_init)(sqlite3*,const char*,sqlite3*,const char*);
95281   int (*backup_pagecount)(sqlite3_backup*);
95282   int (*backup_remaining)(sqlite3_backup*);
95283   int (*backup_step)(sqlite3_backup*,int);
95284   const char *(*compileoption_get)(int);
95285   int (*compileoption_used)(const char*);
95286   int (*create_function_v2)(sqlite3*,const char*,int,int,void*,
95287                             void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
95288                             void (*xStep)(sqlite3_context*,int,sqlite3_value**),
95289                             void (*xFinal)(sqlite3_context*),
95290                             void(*xDestroy)(void*));
95291   int (*db_config)(sqlite3*,int,...);
95292   sqlite3_mutex *(*db_mutex)(sqlite3*);
95293   int (*db_status)(sqlite3*,int,int*,int*,int);
95294   int (*extended_errcode)(sqlite3*);
95295   void (*log)(int,const char*,...);
95296   sqlite3_int64 (*soft_heap_limit64)(sqlite3_int64);
95297   const char *(*sourceid)(void);
95298   int (*stmt_status)(sqlite3_stmt*,int,int);
95299   int (*strnicmp)(const char*,const char*,int);
95300   int (*unlock_notify)(sqlite3*,void(*)(void**,int),void*);
95301   int (*wal_autocheckpoint)(sqlite3*,int);
95302   int (*wal_checkpoint)(sqlite3*,const char*);
95303   void *(*wal_hook)(sqlite3*,int(*)(void*,sqlite3*,const char*,int),void*);
95304   int (*blob_reopen)(sqlite3_blob*,sqlite3_int64);
95305   int (*vtab_config)(sqlite3*,int op,...);
95306   int (*vtab_on_conflict)(sqlite3*);
95307   /* Version 3.7.16 and later */
95308   int (*close_v2)(sqlite3*);
95309   const char *(*db_filename)(sqlite3*,const char*);
95310   int (*db_readonly)(sqlite3*,const char*);
95311   int (*db_release_memory)(sqlite3*);
95312   const char *(*errstr)(int);
95313   int (*stmt_busy)(sqlite3_stmt*);
95314   int (*stmt_readonly)(sqlite3_stmt*);
95315   int (*stricmp)(const char*,const char*);
95316   int (*uri_boolean)(const char*,const char*,int);
95317   sqlite3_int64 (*uri_int64)(const char*,const char*,sqlite3_int64);
95318   const char *(*uri_parameter)(const char*,const char*);
95319   char *(*vsnprintf)(int,char*,const char*,va_list);
95320   int (*wal_checkpoint_v2)(sqlite3*,const char*,int,int*,int*);
95321 };
95322 
95323 /*
95324 ** The following macros redefine the API routines so that they are
95325 ** redirected throught the global sqlite3_api structure.
95326 **
95327 ** This header file is also used by the loadext.c source file
95328 ** (part of the main SQLite library - not an extension) so that
95329 ** it can get access to the sqlite3_api_routines structure
95330 ** definition.  But the main library does not want to redefine
95331 ** the API.  So the redefinition macros are only valid if the
95332 ** SQLITE_CORE macros is undefined.
95333 */
95334 #ifndef SQLITE_CORE
95335 #define sqlite3_aggregate_context      sqlite3_api->aggregate_context
95336 #ifndef SQLITE_OMIT_DEPRECATED
95337 #define sqlite3_aggregate_count        sqlite3_api->aggregate_count
95338 #endif
95339 #define sqlite3_bind_blob              sqlite3_api->bind_blob
95340 #define sqlite3_bind_double            sqlite3_api->bind_double
95341 #define sqlite3_bind_int               sqlite3_api->bind_int
95342 #define sqlite3_bind_int64             sqlite3_api->bind_int64
95343 #define sqlite3_bind_null              sqlite3_api->bind_null
95344 #define sqlite3_bind_parameter_count   sqlite3_api->bind_parameter_count
95345 #define sqlite3_bind_parameter_index   sqlite3_api->bind_parameter_index
95346 #define sqlite3_bind_parameter_name    sqlite3_api->bind_parameter_name
95347 #define sqlite3_bind_text              sqlite3_api->bind_text
95348 #define sqlite3_bind_text16            sqlite3_api->bind_text16
95349 #define sqlite3_bind_value             sqlite3_api->bind_value
95350 #define sqlite3_busy_handler           sqlite3_api->busy_handler
95351 #define sqlite3_busy_timeout           sqlite3_api->busy_timeout
95352 #define sqlite3_changes                sqlite3_api->changes
95353 #define sqlite3_close                  sqlite3_api->close
95354 #define sqlite3_collation_needed       sqlite3_api->collation_needed
95355 #define sqlite3_collation_needed16     sqlite3_api->collation_needed16
95356 #define sqlite3_column_blob            sqlite3_api->column_blob
95357 #define sqlite3_column_bytes           sqlite3_api->column_bytes
95358 #define sqlite3_column_bytes16         sqlite3_api->column_bytes16
95359 #define sqlite3_column_count           sqlite3_api->column_count
95360 #define sqlite3_column_database_name   sqlite3_api->column_database_name
95361 #define sqlite3_column_database_name16 sqlite3_api->column_database_name16
95362 #define sqlite3_column_decltype        sqlite3_api->column_decltype
95363 #define sqlite3_column_decltype16      sqlite3_api->column_decltype16
95364 #define sqlite3_column_double          sqlite3_api->column_double
95365 #define sqlite3_column_int             sqlite3_api->column_int
95366 #define sqlite3_column_int64           sqlite3_api->column_int64
95367 #define sqlite3_column_name            sqlite3_api->column_name
95368 #define sqlite3_column_name16          sqlite3_api->column_name16
95369 #define sqlite3_column_origin_name     sqlite3_api->column_origin_name
95370 #define sqlite3_column_origin_name16   sqlite3_api->column_origin_name16
95371 #define sqlite3_column_table_name      sqlite3_api->column_table_name
95372 #define sqlite3_column_table_name16    sqlite3_api->column_table_name16
95373 #define sqlite3_column_text            sqlite3_api->column_text
95374 #define sqlite3_column_text16          sqlite3_api->column_text16
95375 #define sqlite3_column_type            sqlite3_api->column_type
95376 #define sqlite3_column_value           sqlite3_api->column_value
95377 #define sqlite3_commit_hook            sqlite3_api->commit_hook
95378 #define sqlite3_complete               sqlite3_api->complete
95379 #define sqlite3_complete16             sqlite3_api->complete16
95380 #define sqlite3_create_collation       sqlite3_api->create_collation
95381 #define sqlite3_create_collation16     sqlite3_api->create_collation16
95382 #define sqlite3_create_function        sqlite3_api->create_function
95383 #define sqlite3_create_function16      sqlite3_api->create_function16
95384 #define sqlite3_create_module          sqlite3_api->create_module
95385 #define sqlite3_create_module_v2       sqlite3_api->create_module_v2
95386 #define sqlite3_data_count             sqlite3_api->data_count
95387 #define sqlite3_db_handle              sqlite3_api->db_handle
95388 #define sqlite3_declare_vtab           sqlite3_api->declare_vtab
95389 #define sqlite3_enable_shared_cache    sqlite3_api->enable_shared_cache
95390 #define sqlite3_errcode                sqlite3_api->errcode
95391 #define sqlite3_errmsg                 sqlite3_api->errmsg
95392 #define sqlite3_errmsg16               sqlite3_api->errmsg16
95393 #define sqlite3_exec                   sqlite3_api->exec
95394 #ifndef SQLITE_OMIT_DEPRECATED
95395 #define sqlite3_expired                sqlite3_api->expired
95396 #endif
95397 #define sqlite3_finalize               sqlite3_api->finalize
95398 #define sqlite3_free                   sqlite3_api->free
95399 #define sqlite3_free_table             sqlite3_api->free_table
95400 #define sqlite3_get_autocommit         sqlite3_api->get_autocommit
95401 #define sqlite3_get_auxdata            sqlite3_api->get_auxdata
95402 #define sqlite3_get_table              sqlite3_api->get_table
95403 #ifndef SQLITE_OMIT_DEPRECATED
95404 #define sqlite3_global_recover         sqlite3_api->global_recover
95405 #endif
95406 #define sqlite3_interrupt              sqlite3_api->interruptx
95407 #define sqlite3_last_insert_rowid      sqlite3_api->last_insert_rowid
95408 #define sqlite3_libversion             sqlite3_api->libversion
95409 #define sqlite3_libversion_number      sqlite3_api->libversion_number
95410 #define sqlite3_malloc                 sqlite3_api->malloc
95411 #define sqlite3_mprintf                sqlite3_api->mprintf
95412 #define sqlite3_open                   sqlite3_api->open
95413 #define sqlite3_open16                 sqlite3_api->open16
95414 #define sqlite3_prepare                sqlite3_api->prepare
95415 #define sqlite3_prepare16              sqlite3_api->prepare16
95416 #define sqlite3_prepare_v2             sqlite3_api->prepare_v2
95417 #define sqlite3_prepare16_v2           sqlite3_api->prepare16_v2
95418 #define sqlite3_profile                sqlite3_api->profile
95419 #define sqlite3_progress_handler       sqlite3_api->progress_handler
95420 #define sqlite3_realloc                sqlite3_api->realloc
95421 #define sqlite3_reset                  sqlite3_api->reset
95422 #define sqlite3_result_blob            sqlite3_api->result_blob
95423 #define sqlite3_result_double          sqlite3_api->result_double
95424 #define sqlite3_result_error           sqlite3_api->result_error
95425 #define sqlite3_result_error16         sqlite3_api->result_error16
95426 #define sqlite3_result_int             sqlite3_api->result_int
95427 #define sqlite3_result_int64           sqlite3_api->result_int64
95428 #define sqlite3_result_null            sqlite3_api->result_null
95429 #define sqlite3_result_text            sqlite3_api->result_text
95430 #define sqlite3_result_text16          sqlite3_api->result_text16
95431 #define sqlite3_result_text16be        sqlite3_api->result_text16be
95432 #define sqlite3_result_text16le        sqlite3_api->result_text16le
95433 #define sqlite3_result_value           sqlite3_api->result_value
95434 #define sqlite3_rollback_hook          sqlite3_api->rollback_hook
95435 #define sqlite3_set_authorizer         sqlite3_api->set_authorizer
95436 #define sqlite3_set_auxdata            sqlite3_api->set_auxdata
95437 #define sqlite3_snprintf               sqlite3_api->snprintf
95438 #define sqlite3_step                   sqlite3_api->step
95439 #define sqlite3_table_column_metadata  sqlite3_api->table_column_metadata
95440 #define sqlite3_thread_cleanup         sqlite3_api->thread_cleanup
95441 #define sqlite3_total_changes          sqlite3_api->total_changes
95442 #define sqlite3_trace                  sqlite3_api->trace
95443 #ifndef SQLITE_OMIT_DEPRECATED
95444 #define sqlite3_transfer_bindings      sqlite3_api->transfer_bindings
95445 #endif
95446 #define sqlite3_update_hook            sqlite3_api->update_hook
95447 #define sqlite3_user_data              sqlite3_api->user_data
95448 #define sqlite3_value_blob             sqlite3_api->value_blob
95449 #define sqlite3_value_bytes            sqlite3_api->value_bytes
95450 #define sqlite3_value_bytes16          sqlite3_api->value_bytes16
95451 #define sqlite3_value_double           sqlite3_api->value_double
95452 #define sqlite3_value_int              sqlite3_api->value_int
95453 #define sqlite3_value_int64            sqlite3_api->value_int64
95454 #define sqlite3_value_numeric_type     sqlite3_api->value_numeric_type
95455 #define sqlite3_value_text             sqlite3_api->value_text
95456 #define sqlite3_value_text16           sqlite3_api->value_text16
95457 #define sqlite3_value_text16be         sqlite3_api->value_text16be
95458 #define sqlite3_value_text16le         sqlite3_api->value_text16le
95459 #define sqlite3_value_type             sqlite3_api->value_type
95460 #define sqlite3_vmprintf               sqlite3_api->vmprintf
95461 #define sqlite3_overload_function      sqlite3_api->overload_function
95462 #define sqlite3_prepare_v2             sqlite3_api->prepare_v2
95463 #define sqlite3_prepare16_v2           sqlite3_api->prepare16_v2
95464 #define sqlite3_clear_bindings         sqlite3_api->clear_bindings
95465 #define sqlite3_bind_zeroblob          sqlite3_api->bind_zeroblob
95466 #define sqlite3_blob_bytes             sqlite3_api->blob_bytes
95467 #define sqlite3_blob_close             sqlite3_api->blob_close
95468 #define sqlite3_blob_open              sqlite3_api->blob_open
95469 #define sqlite3_blob_read              sqlite3_api->blob_read
95470 #define sqlite3_blob_write             sqlite3_api->blob_write
95471 #define sqlite3_create_collation_v2    sqlite3_api->create_collation_v2
95472 #define sqlite3_file_control           sqlite3_api->file_control
95473 #define sqlite3_memory_highwater       sqlite3_api->memory_highwater
95474 #define sqlite3_memory_used            sqlite3_api->memory_used
95475 #define sqlite3_mutex_alloc            sqlite3_api->mutex_alloc
95476 #define sqlite3_mutex_enter            sqlite3_api->mutex_enter
95477 #define sqlite3_mutex_free             sqlite3_api->mutex_free
95478 #define sqlite3_mutex_leave            sqlite3_api->mutex_leave
95479 #define sqlite3_mutex_try              sqlite3_api->mutex_try
95480 #define sqlite3_open_v2                sqlite3_api->open_v2
95481 #define sqlite3_release_memory         sqlite3_api->release_memory
95482 #define sqlite3_result_error_nomem     sqlite3_api->result_error_nomem
95483 #define sqlite3_result_error_toobig    sqlite3_api->result_error_toobig
95484 #define sqlite3_sleep                  sqlite3_api->sleep
95485 #define sqlite3_soft_heap_limit        sqlite3_api->soft_heap_limit
95486 #define sqlite3_vfs_find               sqlite3_api->vfs_find
95487 #define sqlite3_vfs_register           sqlite3_api->vfs_register
95488 #define sqlite3_vfs_unregister         sqlite3_api->vfs_unregister
95489 #define sqlite3_threadsafe             sqlite3_api->xthreadsafe
95490 #define sqlite3_result_zeroblob        sqlite3_api->result_zeroblob
95491 #define sqlite3_result_error_code      sqlite3_api->result_error_code
95492 #define sqlite3_test_control           sqlite3_api->test_control
95493 #define sqlite3_randomness             sqlite3_api->randomness
95494 #define sqlite3_context_db_handle      sqlite3_api->context_db_handle
95495 #define sqlite3_extended_result_codes  sqlite3_api->extended_result_codes
95496 #define sqlite3_limit                  sqlite3_api->limit
95497 #define sqlite3_next_stmt              sqlite3_api->next_stmt
95498 #define sqlite3_sql                    sqlite3_api->sql
95499 #define sqlite3_status                 sqlite3_api->status
95500 #define sqlite3_backup_finish          sqlite3_api->backup_finish
95501 #define sqlite3_backup_init            sqlite3_api->backup_init
95502 #define sqlite3_backup_pagecount       sqlite3_api->backup_pagecount
95503 #define sqlite3_backup_remaining       sqlite3_api->backup_remaining
95504 #define sqlite3_backup_step            sqlite3_api->backup_step
95505 #define sqlite3_compileoption_get      sqlite3_api->compileoption_get
95506 #define sqlite3_compileoption_used     sqlite3_api->compileoption_used
95507 #define sqlite3_create_function_v2     sqlite3_api->create_function_v2
95508 #define sqlite3_db_config              sqlite3_api->db_config
95509 #define sqlite3_db_mutex               sqlite3_api->db_mutex
95510 #define sqlite3_db_status              sqlite3_api->db_status
95511 #define sqlite3_extended_errcode       sqlite3_api->extended_errcode
95512 #define sqlite3_log                    sqlite3_api->log
95513 #define sqlite3_soft_heap_limit64      sqlite3_api->soft_heap_limit64
95514 #define sqlite3_sourceid               sqlite3_api->sourceid
95515 #define sqlite3_stmt_status            sqlite3_api->stmt_status
95516 #define sqlite3_strnicmp               sqlite3_api->strnicmp
95517 #define sqlite3_unlock_notify          sqlite3_api->unlock_notify
95518 #define sqlite3_wal_autocheckpoint     sqlite3_api->wal_autocheckpoint
95519 #define sqlite3_wal_checkpoint         sqlite3_api->wal_checkpoint
95520 #define sqlite3_wal_hook               sqlite3_api->wal_hook
95521 #define sqlite3_blob_reopen            sqlite3_api->blob_reopen
95522 #define sqlite3_vtab_config            sqlite3_api->vtab_config
95523 #define sqlite3_vtab_on_conflict       sqlite3_api->vtab_on_conflict
95524 /* Version 3.7.16 and later */
95525 #define sqlite3_close_v2               sqlite3_api->close_v2
95526 #define sqlite3_db_filename            sqlite3_api->db_filename
95527 #define sqlite3_db_readonly            sqlite3_api->db_readonly
95528 #define sqlite3_db_release_memory      sqlite3_api->db_release_memory
95529 #define sqlite3_errstr                 sqlite3_api->errstr
95530 #define sqlite3_stmt_busy              sqlite3_api->stmt_busy
95531 #define sqlite3_stmt_readonly          sqlite3_api->stmt_readonly
95532 #define sqlite3_stricmp                sqlite3_api->stricmp
95533 #define sqlite3_uri_boolean            sqlite3_api->uri_boolean
95534 #define sqlite3_uri_int64              sqlite3_api->uri_int64
95535 #define sqlite3_uri_parameter          sqlite3_api->uri_parameter
95536 #define sqlite3_uri_vsnprintf          sqlite3_api->vsnprintf
95537 #define sqlite3_wal_checkpoint_v2      sqlite3_api->wal_checkpoint_v2
95538 #endif /* SQLITE_CORE */
95539 
95540 #ifndef SQLITE_CORE
95541   /* This case when the file really is being compiled as a loadable
95542   ** extension */
95543 # define SQLITE_EXTENSION_INIT1     const sqlite3_api_routines *sqlite3_api=0;
95544 # define SQLITE_EXTENSION_INIT2(v)  sqlite3_api=v;
95545 # define SQLITE_EXTENSION_INIT3     \
95546     extern const sqlite3_api_routines *sqlite3_api;
95547 #else
95548   /* This case when the file is being statically linked into the
95549   ** application */
95550 # define SQLITE_EXTENSION_INIT1     /*no-op*/
95551 # define SQLITE_EXTENSION_INIT2(v)  (void)v; /* unused parameter */
95552 # define SQLITE_EXTENSION_INIT3     /*no-op*/
95553 #endif
95554 
95555 #endif /* _SQLITE3EXT_H_ */
95556 
95557 /************** End of sqlite3ext.h ******************************************/
95558 /************** Continuing where we left off in loadext.c ********************/
95559 /* #include <string.h> */
95560 
95561 #ifndef SQLITE_OMIT_LOAD_EXTENSION
95562 
95563 /*
95564 ** Some API routines are omitted when various features are
95565 ** excluded from a build of SQLite.  Substitute a NULL pointer
95566 ** for any missing APIs.
95567 */
95568 #ifndef SQLITE_ENABLE_COLUMN_METADATA
95569 # define sqlite3_column_database_name   0
95570 # define sqlite3_column_database_name16 0
95571 # define sqlite3_column_table_name      0
95572 # define sqlite3_column_table_name16    0
95573 # define sqlite3_column_origin_name     0
95574 # define sqlite3_column_origin_name16   0
95575 # define sqlite3_table_column_metadata  0
95576 #endif
95577 
95578 #ifdef SQLITE_OMIT_AUTHORIZATION
95579 # define sqlite3_set_authorizer         0
95580 #endif
95581 
95582 #ifdef SQLITE_OMIT_UTF16
95583 # define sqlite3_bind_text16            0
95584 # define sqlite3_collation_needed16     0
95585 # define sqlite3_column_decltype16      0
95586 # define sqlite3_column_name16          0
95587 # define sqlite3_column_text16          0
95588 # define sqlite3_complete16             0
95589 # define sqlite3_create_collation16     0
95590 # define sqlite3_create_function16      0
95591 # define sqlite3_errmsg16               0
95592 # define sqlite3_open16                 0
95593 # define sqlite3_prepare16              0
95594 # define sqlite3_prepare16_v2           0
95595 # define sqlite3_result_error16         0
95596 # define sqlite3_result_text16          0
95597 # define sqlite3_result_text16be        0
95598 # define sqlite3_result_text16le        0
95599 # define sqlite3_value_text16           0
95600 # define sqlite3_value_text16be         0
95601 # define sqlite3_value_text16le         0
95602 # define sqlite3_column_database_name16 0
95603 # define sqlite3_column_table_name16    0
95604 # define sqlite3_column_origin_name16   0
95605 #endif
95606 
95607 #ifdef SQLITE_OMIT_COMPLETE
95608 # define sqlite3_complete 0
95609 # define sqlite3_complete16 0
95610 #endif
95611 
95612 #ifdef SQLITE_OMIT_DECLTYPE
95613 # define sqlite3_column_decltype16      0
95614 # define sqlite3_column_decltype        0
95615 #endif
95616 
95617 #ifdef SQLITE_OMIT_PROGRESS_CALLBACK
95618 # define sqlite3_progress_handler 0
95619 #endif
95620 
95621 #ifdef SQLITE_OMIT_VIRTUALTABLE
95622 # define sqlite3_create_module 0
95623 # define sqlite3_create_module_v2 0
95624 # define sqlite3_declare_vtab 0
95625 # define sqlite3_vtab_config 0
95626 # define sqlite3_vtab_on_conflict 0
95627 #endif
95628 
95629 #ifdef SQLITE_OMIT_SHARED_CACHE
95630 # define sqlite3_enable_shared_cache 0
95631 #endif
95632 
95633 #ifdef SQLITE_OMIT_TRACE
95634 # define sqlite3_profile       0
95635 # define sqlite3_trace         0
95636 #endif
95637 
95638 #ifdef SQLITE_OMIT_GET_TABLE
95639 # define sqlite3_free_table    0
95640 # define sqlite3_get_table     0
95641 #endif
95642 
95643 #ifdef SQLITE_OMIT_INCRBLOB
95644 #define sqlite3_bind_zeroblob  0
95645 #define sqlite3_blob_bytes     0
95646 #define sqlite3_blob_close     0
95647 #define sqlite3_blob_open      0
95648 #define sqlite3_blob_read      0
95649 #define sqlite3_blob_write     0
95650 #define sqlite3_blob_reopen    0
95651 #endif
95652 
95653 /*
95654 ** The following structure contains pointers to all SQLite API routines.
95655 ** A pointer to this structure is passed into extensions when they are
95656 ** loaded so that the extension can make calls back into the SQLite
95657 ** library.
95658 **
95659 ** When adding new APIs, add them to the bottom of this structure
95660 ** in order to preserve backwards compatibility.
95661 **
95662 ** Extensions that use newer APIs should first call the
95663 ** sqlite3_libversion_number() to make sure that the API they
95664 ** intend to use is supported by the library.  Extensions should
95665 ** also check to make sure that the pointer to the function is
95666 ** not NULL before calling it.
95667 */
95668 static const sqlite3_api_routines sqlite3Apis = {
95669   sqlite3_aggregate_context,
95670 #ifndef SQLITE_OMIT_DEPRECATED
95671   sqlite3_aggregate_count,
95672 #else
95673   0,
95674 #endif
95675   sqlite3_bind_blob,
95676   sqlite3_bind_double,
95677   sqlite3_bind_int,
95678   sqlite3_bind_int64,
95679   sqlite3_bind_null,
95680   sqlite3_bind_parameter_count,
95681   sqlite3_bind_parameter_index,
95682   sqlite3_bind_parameter_name,
95683   sqlite3_bind_text,
95684   sqlite3_bind_text16,
95685   sqlite3_bind_value,
95686   sqlite3_busy_handler,
95687   sqlite3_busy_timeout,
95688   sqlite3_changes,
95689   sqlite3_close,
95690   sqlite3_collation_needed,
95691   sqlite3_collation_needed16,
95692   sqlite3_column_blob,
95693   sqlite3_column_bytes,
95694   sqlite3_column_bytes16,
95695   sqlite3_column_count,
95696   sqlite3_column_database_name,
95697   sqlite3_column_database_name16,
95698   sqlite3_column_decltype,
95699   sqlite3_column_decltype16,
95700   sqlite3_column_double,
95701   sqlite3_column_int,
95702   sqlite3_column_int64,
95703   sqlite3_column_name,
95704   sqlite3_column_name16,
95705   sqlite3_column_origin_name,
95706   sqlite3_column_origin_name16,
95707   sqlite3_column_table_name,
95708   sqlite3_column_table_name16,
95709   sqlite3_column_text,
95710   sqlite3_column_text16,
95711   sqlite3_column_type,
95712   sqlite3_column_value,
95713   sqlite3_commit_hook,
95714   sqlite3_complete,
95715   sqlite3_complete16,
95716   sqlite3_create_collation,
95717   sqlite3_create_collation16,
95718   sqlite3_create_function,
95719   sqlite3_create_function16,
95720   sqlite3_create_module,
95721   sqlite3_data_count,
95722   sqlite3_db_handle,
95723   sqlite3_declare_vtab,
95724   sqlite3_enable_shared_cache,
95725   sqlite3_errcode,
95726   sqlite3_errmsg,
95727   sqlite3_errmsg16,
95728   sqlite3_exec,
95729 #ifndef SQLITE_OMIT_DEPRECATED
95730   sqlite3_expired,
95731 #else
95732   0,
95733 #endif
95734   sqlite3_finalize,
95735   sqlite3_free,
95736   sqlite3_free_table,
95737   sqlite3_get_autocommit,
95738   sqlite3_get_auxdata,
95739   sqlite3_get_table,
95740   0,     /* Was sqlite3_global_recover(), but that function is deprecated */
95741   sqlite3_interrupt,
95742   sqlite3_last_insert_rowid,
95743   sqlite3_libversion,
95744   sqlite3_libversion_number,
95745   sqlite3_malloc,
95746   sqlite3_mprintf,
95747   sqlite3_open,
95748   sqlite3_open16,
95749   sqlite3_prepare,
95750   sqlite3_prepare16,
95751   sqlite3_profile,
95752   sqlite3_progress_handler,
95753   sqlite3_realloc,
95754   sqlite3_reset,
95755   sqlite3_result_blob,
95756   sqlite3_result_double,
95757   sqlite3_result_error,
95758   sqlite3_result_error16,
95759   sqlite3_result_int,
95760   sqlite3_result_int64,
95761   sqlite3_result_null,
95762   sqlite3_result_text,
95763   sqlite3_result_text16,
95764   sqlite3_result_text16be,
95765   sqlite3_result_text16le,
95766   sqlite3_result_value,
95767   sqlite3_rollback_hook,
95768   sqlite3_set_authorizer,
95769   sqlite3_set_auxdata,
95770   sqlite3_snprintf,
95771   sqlite3_step,
95772   sqlite3_table_column_metadata,
95773 #ifndef SQLITE_OMIT_DEPRECATED
95774   sqlite3_thread_cleanup,
95775 #else
95776   0,
95777 #endif
95778   sqlite3_total_changes,
95779   sqlite3_trace,
95780 #ifndef SQLITE_OMIT_DEPRECATED
95781   sqlite3_transfer_bindings,
95782 #else
95783   0,
95784 #endif
95785   sqlite3_update_hook,
95786   sqlite3_user_data,
95787   sqlite3_value_blob,
95788   sqlite3_value_bytes,
95789   sqlite3_value_bytes16,
95790   sqlite3_value_double,
95791   sqlite3_value_int,
95792   sqlite3_value_int64,
95793   sqlite3_value_numeric_type,
95794   sqlite3_value_text,
95795   sqlite3_value_text16,
95796   sqlite3_value_text16be,
95797   sqlite3_value_text16le,
95798   sqlite3_value_type,
95799   sqlite3_vmprintf,
95800   /*
95801   ** The original API set ends here.  All extensions can call any
95802   ** of the APIs above provided that the pointer is not NULL.  But
95803   ** before calling APIs that follow, extension should check the
95804   ** sqlite3_libversion_number() to make sure they are dealing with
95805   ** a library that is new enough to support that API.
95806   *************************************************************************
95807   */
95808   sqlite3_overload_function,
95809 
95810   /*
95811   ** Added after 3.3.13
95812   */
95813   sqlite3_prepare_v2,
95814   sqlite3_prepare16_v2,
95815   sqlite3_clear_bindings,
95816 
95817   /*
95818   ** Added for 3.4.1
95819   */
95820   sqlite3_create_module_v2,
95821 
95822   /*
95823   ** Added for 3.5.0
95824   */
95825   sqlite3_bind_zeroblob,
95826   sqlite3_blob_bytes,
95827   sqlite3_blob_close,
95828   sqlite3_blob_open,
95829   sqlite3_blob_read,
95830   sqlite3_blob_write,
95831   sqlite3_create_collation_v2,
95832   sqlite3_file_control,
95833   sqlite3_memory_highwater,
95834   sqlite3_memory_used,
95835 #ifdef SQLITE_MUTEX_OMIT
95836   0,
95837   0,
95838   0,
95839   0,
95840   0,
95841 #else
95842   sqlite3_mutex_alloc,
95843   sqlite3_mutex_enter,
95844   sqlite3_mutex_free,
95845   sqlite3_mutex_leave,
95846   sqlite3_mutex_try,
95847 #endif
95848   sqlite3_open_v2,
95849   sqlite3_release_memory,
95850   sqlite3_result_error_nomem,
95851   sqlite3_result_error_toobig,
95852   sqlite3_sleep,
95853   sqlite3_soft_heap_limit,
95854   sqlite3_vfs_find,
95855   sqlite3_vfs_register,
95856   sqlite3_vfs_unregister,
95857 
95858   /*
95859   ** Added for 3.5.8
95860   */
95861   sqlite3_threadsafe,
95862   sqlite3_result_zeroblob,
95863   sqlite3_result_error_code,
95864   sqlite3_test_control,
95865   sqlite3_randomness,
95866   sqlite3_context_db_handle,
95867 
95868   /*
95869   ** Added for 3.6.0
95870   */
95871   sqlite3_extended_result_codes,
95872   sqlite3_limit,
95873   sqlite3_next_stmt,
95874   sqlite3_sql,
95875   sqlite3_status,
95876 
95877   /*
95878   ** Added for 3.7.4
95879   */
95880   sqlite3_backup_finish,
95881   sqlite3_backup_init,
95882   sqlite3_backup_pagecount,
95883   sqlite3_backup_remaining,
95884   sqlite3_backup_step,
95885 #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
95886   sqlite3_compileoption_get,
95887   sqlite3_compileoption_used,
95888 #else
95889   0,
95890   0,
95891 #endif
95892   sqlite3_create_function_v2,
95893   sqlite3_db_config,
95894   sqlite3_db_mutex,
95895   sqlite3_db_status,
95896   sqlite3_extended_errcode,
95897   sqlite3_log,
95898   sqlite3_soft_heap_limit64,
95899   sqlite3_sourceid,
95900   sqlite3_stmt_status,
95901   sqlite3_strnicmp,
95902 #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
95903   sqlite3_unlock_notify,
95904 #else
95905   0,
95906 #endif
95907 #ifndef SQLITE_OMIT_WAL
95908   sqlite3_wal_autocheckpoint,
95909   sqlite3_wal_checkpoint,
95910   sqlite3_wal_hook,
95911 #else
95912   0,
95913   0,
95914   0,
95915 #endif
95916   sqlite3_blob_reopen,
95917   sqlite3_vtab_config,
95918   sqlite3_vtab_on_conflict,
95919   sqlite3_close_v2,
95920   sqlite3_db_filename,
95921   sqlite3_db_readonly,
95922   sqlite3_db_release_memory,
95923   sqlite3_errstr,
95924   sqlite3_stmt_busy,
95925   sqlite3_stmt_readonly,
95926   sqlite3_stricmp,
95927   sqlite3_uri_boolean,
95928   sqlite3_uri_int64,
95929   sqlite3_uri_parameter,
95930   sqlite3_vsnprintf,
95931   sqlite3_wal_checkpoint_v2
95932 };
95933 
95934 /*
95935 ** Attempt to load an SQLite extension library contained in the file
95936 ** zFile.  The entry point is zProc.  zProc may be 0 in which case a
95937 ** default entry point name (sqlite3_extension_init) is used.  Use
95938 ** of the default name is recommended.
95939 **
95940 ** Return SQLITE_OK on success and SQLITE_ERROR if something goes wrong.
95941 **
95942 ** If an error occurs and pzErrMsg is not 0, then fill *pzErrMsg with
95943 ** error message text.  The calling function should free this memory
95944 ** by calling sqlite3DbFree(db, ).
95945 */
95946 static int sqlite3LoadExtension(
95947   sqlite3 *db,          /* Load the extension into this database connection */
95948   const char *zFile,    /* Name of the shared library containing extension */
95949   const char *zProc,    /* Entry point.  Use "sqlite3_extension_init" if 0 */
95950   char **pzErrMsg       /* Put error message here if not 0 */
95951 ){
95952   sqlite3_vfs *pVfs = db->pVfs;
95953   void *handle;
95954   int (*xInit)(sqlite3*,char**,const sqlite3_api_routines*);
95955   char *zErrmsg = 0;
95956   const char *zEntry;
95957   char *zAltEntry = 0;
95958   void **aHandle;
95959   int nMsg = 300 + sqlite3Strlen30(zFile);
95960   int ii;
95961 
95962   /* Shared library endings to try if zFile cannot be loaded as written */
95963   static const char *azEndings[] = {
95964 #if SQLITE_OS_WIN
95965      "dll"
95966 #elif defined(__APPLE__)
95967      "dylib"
95968 #else
95969      "so"
95970 #endif
95971   };
95972 
95973 
95974   if( pzErrMsg ) *pzErrMsg = 0;
95975 
95976   /* Ticket #1863.  To avoid a creating security problems for older
95977   ** applications that relink against newer versions of SQLite, the
95978   ** ability to run load_extension is turned off by default.  One
95979   ** must call sqlite3_enable_load_extension() to turn on extension
95980   ** loading.  Otherwise you get the following error.
95981   */
95982   if( (db->flags & SQLITE_LoadExtension)==0 ){
95983     if( pzErrMsg ){
95984       *pzErrMsg = sqlite3_mprintf("not authorized");
95985     }
95986     return SQLITE_ERROR;
95987   }
95988 
95989   zEntry = zProc ? zProc : "sqlite3_extension_init";
95990 
95991   handle = sqlite3OsDlOpen(pVfs, zFile);
95992 #if SQLITE_OS_UNIX || SQLITE_OS_WIN
95993   for(ii=0; ii<ArraySize(azEndings) && handle==0; ii++){
95994     char *zAltFile = sqlite3_mprintf("%s.%s", zFile, azEndings[ii]);
95995     if( zAltFile==0 ) return SQLITE_NOMEM;
95996     handle = sqlite3OsDlOpen(pVfs, zAltFile);
95997     sqlite3_free(zAltFile);
95998   }
95999 #endif
96000   if( handle==0 ){
96001     if( pzErrMsg ){
96002       *pzErrMsg = zErrmsg = sqlite3_malloc(nMsg);
96003       if( zErrmsg ){
96004         sqlite3_snprintf(nMsg, zErrmsg,
96005             "unable to open shared library [%s]", zFile);
96006         sqlite3OsDlError(pVfs, nMsg-1, zErrmsg);
96007       }
96008     }
96009     return SQLITE_ERROR;
96010   }
96011   xInit = (int(*)(sqlite3*,char**,const sqlite3_api_routines*))
96012                    sqlite3OsDlSym(pVfs, handle, zEntry);
96013 
96014   /* If no entry point was specified and the default legacy
96015   ** entry point name "sqlite3_extension_init" was not found, then
96016   ** construct an entry point name "sqlite3_X_init" where the X is
96017   ** replaced by the lowercase value of every ASCII alphabetic
96018   ** character in the filename after the last "/" upto the first ".",
96019   ** and eliding the first three characters if they are "lib".
96020   ** Examples:
96021   **
96022   **    /usr/local/lib/libExample5.4.3.so ==>  sqlite3_example_init
96023   **    C:/lib/mathfuncs.dll              ==>  sqlite3_mathfuncs_init
96024   */
96025   if( xInit==0 && zProc==0 ){
96026     int iFile, iEntry, c;
96027     int ncFile = sqlite3Strlen30(zFile);
96028     zAltEntry = sqlite3_malloc(ncFile+30);
96029     if( zAltEntry==0 ){
96030       sqlite3OsDlClose(pVfs, handle);
96031       return SQLITE_NOMEM;
96032     }
96033     memcpy(zAltEntry, "sqlite3_", 8);
96034     for(iFile=ncFile-1; iFile>=0 && zFile[iFile]!='/'; iFile--){}
96035     iFile++;
96036     if( sqlite3_strnicmp(zFile+iFile, "lib", 3)==0 ) iFile += 3;
96037     for(iEntry=8; (c = zFile[iFile])!=0 && c!='.'; iFile++){
96038       if( sqlite3Isalpha(c) ){
96039         zAltEntry[iEntry++] = (char)sqlite3UpperToLower[(unsigned)c];
96040       }
96041     }
96042     memcpy(zAltEntry+iEntry, "_init", 6);
96043     zEntry = zAltEntry;
96044     xInit = (int(*)(sqlite3*,char**,const sqlite3_api_routines*))
96045                      sqlite3OsDlSym(pVfs, handle, zEntry);
96046   }
96047   if( xInit==0 ){
96048     if( pzErrMsg ){
96049       nMsg += sqlite3Strlen30(zEntry);
96050       *pzErrMsg = zErrmsg = sqlite3_malloc(nMsg);
96051       if( zErrmsg ){
96052         sqlite3_snprintf(nMsg, zErrmsg,
96053             "no entry point [%s] in shared library [%s]", zEntry, zFile);
96054         sqlite3OsDlError(pVfs, nMsg-1, zErrmsg);
96055       }
96056     }
96057     sqlite3OsDlClose(pVfs, handle);
96058     sqlite3_free(zAltEntry);
96059     return SQLITE_ERROR;
96060   }
96061   sqlite3_free(zAltEntry);
96062   if( xInit(db, &zErrmsg, &sqlite3Apis) ){
96063     if( pzErrMsg ){
96064       *pzErrMsg = sqlite3_mprintf("error during initialization: %s", zErrmsg);
96065     }
96066     sqlite3_free(zErrmsg);
96067     sqlite3OsDlClose(pVfs, handle);
96068     return SQLITE_ERROR;
96069   }
96070 
96071   /* Append the new shared library handle to the db->aExtension array. */
96072   aHandle = sqlite3DbMallocZero(db, sizeof(handle)*(db->nExtension+1));
96073   if( aHandle==0 ){
96074     return SQLITE_NOMEM;
96075   }
96076   if( db->nExtension>0 ){
96077     memcpy(aHandle, db->aExtension, sizeof(handle)*db->nExtension);
96078   }
96079   sqlite3DbFree(db, db->aExtension);
96080   db->aExtension = aHandle;
96081 
96082   db->aExtension[db->nExtension++] = handle;
96083   return SQLITE_OK;
96084 }
96085 SQLITE_API int sqlite3_load_extension(
96086   sqlite3 *db,          /* Load the extension into this database connection */
96087   const char *zFile,    /* Name of the shared library containing extension */
96088   const char *zProc,    /* Entry point.  Use "sqlite3_extension_init" if 0 */
96089   char **pzErrMsg       /* Put error message here if not 0 */
96090 ){
96091   int rc;
96092   sqlite3_mutex_enter(db->mutex);
96093   rc = sqlite3LoadExtension(db, zFile, zProc, pzErrMsg);
96094   rc = sqlite3ApiExit(db, rc);
96095   sqlite3_mutex_leave(db->mutex);
96096   return rc;
96097 }
96098 
96099 /*
96100 ** Call this routine when the database connection is closing in order
96101 ** to clean up loaded extensions
96102 */
96103 SQLITE_PRIVATE void sqlite3CloseExtensions(sqlite3 *db){
96104   int i;
96105   assert( sqlite3_mutex_held(db->mutex) );
96106   for(i=0; i<db->nExtension; i++){
96107     sqlite3OsDlClose(db->pVfs, db->aExtension[i]);
96108   }
96109   sqlite3DbFree(db, db->aExtension);
96110 }
96111 
96112 /*
96113 ** Enable or disable extension loading.  Extension loading is disabled by
96114 ** default so as not to open security holes in older applications.
96115 */
96116 SQLITE_API int sqlite3_enable_load_extension(sqlite3 *db, int onoff){
96117   sqlite3_mutex_enter(db->mutex);
96118   if( onoff ){
96119     db->flags |= SQLITE_LoadExtension;
96120   }else{
96121     db->flags &= ~SQLITE_LoadExtension;
96122   }
96123   sqlite3_mutex_leave(db->mutex);
96124   return SQLITE_OK;
96125 }
96126 
96127 #endif /* SQLITE_OMIT_LOAD_EXTENSION */
96128 
96129 /*
96130 ** The auto-extension code added regardless of whether or not extension
96131 ** loading is supported.  We need a dummy sqlite3Apis pointer for that
96132 ** code if regular extension loading is not available.  This is that
96133 ** dummy pointer.
96134 */
96135 #ifdef SQLITE_OMIT_LOAD_EXTENSION
96136 static const sqlite3_api_routines sqlite3Apis = { 0 };
96137 #endif
96138 
96139 
96140 /*
96141 ** The following object holds the list of automatically loaded
96142 ** extensions.
96143 **
96144 ** This list is shared across threads.  The SQLITE_MUTEX_STATIC_MASTER
96145 ** mutex must be held while accessing this list.
96146 */
96147 typedef struct sqlite3AutoExtList sqlite3AutoExtList;
96148 static SQLITE_WSD struct sqlite3AutoExtList {
96149   int nExt;              /* Number of entries in aExt[] */
96150   void (**aExt)(void);   /* Pointers to the extension init functions */
96151 } sqlite3Autoext = { 0, 0 };
96152 
96153 /* The "wsdAutoext" macro will resolve to the autoextension
96154 ** state vector.  If writable static data is unsupported on the target,
96155 ** we have to locate the state vector at run-time.  In the more common
96156 ** case where writable static data is supported, wsdStat can refer directly
96157 ** to the "sqlite3Autoext" state vector declared above.
96158 */
96159 #ifdef SQLITE_OMIT_WSD
96160 # define wsdAutoextInit \
96161   sqlite3AutoExtList *x = &GLOBAL(sqlite3AutoExtList,sqlite3Autoext)
96162 # define wsdAutoext x[0]
96163 #else
96164 # define wsdAutoextInit
96165 # define wsdAutoext sqlite3Autoext
96166 #endif
96167 
96168 
96169 /*
96170 ** Register a statically linked extension that is automatically
96171 ** loaded by every new database connection.
96172 */
96173 SQLITE_API int sqlite3_auto_extension(void (*xInit)(void)){
96174   int rc = SQLITE_OK;
96175 #ifndef SQLITE_OMIT_AUTOINIT
96176   rc = sqlite3_initialize();
96177   if( rc ){
96178     return rc;
96179   }else
96180 #endif
96181   {
96182     int i;
96183 #if SQLITE_THREADSAFE
96184     sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
96185 #endif
96186     wsdAutoextInit;
96187     sqlite3_mutex_enter(mutex);
96188     for(i=0; i<wsdAutoext.nExt; i++){
96189       if( wsdAutoext.aExt[i]==xInit ) break;
96190     }
96191     if( i==wsdAutoext.nExt ){
96192       int nByte = (wsdAutoext.nExt+1)*sizeof(wsdAutoext.aExt[0]);
96193       void (**aNew)(void);
96194       aNew = sqlite3_realloc(wsdAutoext.aExt, nByte);
96195       if( aNew==0 ){
96196         rc = SQLITE_NOMEM;
96197       }else{
96198         wsdAutoext.aExt = aNew;
96199         wsdAutoext.aExt[wsdAutoext.nExt] = xInit;
96200         wsdAutoext.nExt++;
96201       }
96202     }
96203     sqlite3_mutex_leave(mutex);
96204     assert( (rc&0xff)==rc );
96205     return rc;
96206   }
96207 }
96208 
96209 /*
96210 ** Cancel a prior call to sqlite3_auto_extension.  Remove xInit from the
96211 ** set of routines that is invoked for each new database connection, if it
96212 ** is currently on the list.  If xInit is not on the list, then this
96213 ** routine is a no-op.
96214 **
96215 ** Return 1 if xInit was found on the list and removed.  Return 0 if xInit
96216 ** was not on the list.
96217 */
96218 SQLITE_API int sqlite3_cancel_auto_extension(void (*xInit)(void)){
96219 #if SQLITE_THREADSAFE
96220   sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
96221 #endif
96222   int i;
96223   int n = 0;
96224   wsdAutoextInit;
96225   sqlite3_mutex_enter(mutex);
96226   for(i=wsdAutoext.nExt-1; i>=0; i--){
96227     if( wsdAutoext.aExt[i]==xInit ){
96228       wsdAutoext.nExt--;
96229       wsdAutoext.aExt[i] = wsdAutoext.aExt[wsdAutoext.nExt];
96230       n++;
96231       break;
96232     }
96233   }
96234   sqlite3_mutex_leave(mutex);
96235   return n;
96236 }
96237 
96238 /*
96239 ** Reset the automatic extension loading mechanism.
96240 */
96241 SQLITE_API void sqlite3_reset_auto_extension(void){
96242 #ifndef SQLITE_OMIT_AUTOINIT
96243   if( sqlite3_initialize()==SQLITE_OK )
96244 #endif
96245   {
96246 #if SQLITE_THREADSAFE
96247     sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
96248 #endif
96249     wsdAutoextInit;
96250     sqlite3_mutex_enter(mutex);
96251     sqlite3_free(wsdAutoext.aExt);
96252     wsdAutoext.aExt = 0;
96253     wsdAutoext.nExt = 0;
96254     sqlite3_mutex_leave(mutex);
96255   }
96256 }
96257 
96258 /*
96259 ** Load all automatic extensions.
96260 **
96261 ** If anything goes wrong, set an error in the database connection.
96262 */
96263 SQLITE_PRIVATE void sqlite3AutoLoadExtensions(sqlite3 *db){
96264   int i;
96265   int go = 1;
96266   int rc;
96267   int (*xInit)(sqlite3*,char**,const sqlite3_api_routines*);
96268 
96269   wsdAutoextInit;
96270   if( wsdAutoext.nExt==0 ){
96271     /* Common case: early out without every having to acquire a mutex */
96272     return;
96273   }
96274   for(i=0; go; i++){
96275     char *zErrmsg;
96276 #if SQLITE_THREADSAFE
96277     sqlite3_mutex *mutex = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);
96278 #endif
96279     sqlite3_mutex_enter(mutex);
96280     if( i>=wsdAutoext.nExt ){
96281       xInit = 0;
96282       go = 0;
96283     }else{
96284       xInit = (int(*)(sqlite3*,char**,const sqlite3_api_routines*))
96285               wsdAutoext.aExt[i];
96286     }
96287     sqlite3_mutex_leave(mutex);
96288     zErrmsg = 0;
96289     if( xInit && (rc = xInit(db, &zErrmsg, &sqlite3Apis))!=0 ){
96290       sqlite3Error(db, rc,
96291             "automatic extension loading failed: %s", zErrmsg);
96292       go = 0;
96293     }
96294     sqlite3_free(zErrmsg);
96295   }
96296 }
96297 
96298 /************** End of loadext.c *********************************************/
96299 /************** Begin file pragma.c ******************************************/
96300 /*
96301 ** 2003 April 6
96302 **
96303 ** The author disclaims copyright to this source code.  In place of
96304 ** a legal notice, here is a blessing:
96305 **
96306 **    May you do good and not evil.
96307 **    May you find forgiveness for yourself and forgive others.
96308 **    May you share freely, never taking more than you give.
96309 **
96310 *************************************************************************
96311 ** This file contains code used to implement the PRAGMA command.
96312 */
96313 
96314 #if !defined(SQLITE_ENABLE_LOCKING_STYLE)
96315 #  if defined(__APPLE__)
96316 #    define SQLITE_ENABLE_LOCKING_STYLE 1
96317 #  else
96318 #    define SQLITE_ENABLE_LOCKING_STYLE 0
96319 #  endif
96320 #endif
96321 
96322 /***************************************************************************
96323 ** The next block of code, including the PragTyp_XXXX macro definitions and
96324 ** the aPragmaName[] object is composed of generated code. DO NOT EDIT.
96325 **
96326 ** To add new pragmas, edit the code in ../tool/mkpragmatab.tcl and rerun
96327 ** that script.  Then copy/paste the output in place of the following:
96328 */
96329 #define PragTyp_HEADER_VALUE                   0
96330 #define PragTyp_AUTO_VACUUM                    1
96331 #define PragTyp_FLAG                           2
96332 #define PragTyp_BUSY_TIMEOUT                   3
96333 #define PragTyp_CACHE_SIZE                     4
96334 #define PragTyp_CASE_SENSITIVE_LIKE            5
96335 #define PragTyp_COLLATION_LIST                 6
96336 #define PragTyp_COMPILE_OPTIONS                7
96337 #define PragTyp_DATA_STORE_DIRECTORY           8
96338 #define PragTyp_DATABASE_LIST                  9
96339 #define PragTyp_DEFAULT_CACHE_SIZE            10
96340 #define PragTyp_ENCODING                      11
96341 #define PragTyp_FOREIGN_KEY_CHECK             12
96342 #define PragTyp_FOREIGN_KEY_LIST              13
96343 #define PragTyp_INCREMENTAL_VACUUM            14
96344 #define PragTyp_INDEX_INFO                    15
96345 #define PragTyp_INDEX_LIST                    16
96346 #define PragTyp_INTEGRITY_CHECK               17
96347 #define PragTyp_JOURNAL_MODE                  18
96348 #define PragTyp_JOURNAL_SIZE_LIMIT            19
96349 #define PragTyp_LOCK_PROXY_FILE               20
96350 #define PragTyp_LOCKING_MODE                  21
96351 #define PragTyp_PAGE_COUNT                    22
96352 #define PragTyp_MMAP_SIZE                     23
96353 #define PragTyp_PAGE_SIZE                     24
96354 #define PragTyp_SECURE_DELETE                 25
96355 #define PragTyp_SHRINK_MEMORY                 26
96356 #define PragTyp_SOFT_HEAP_LIMIT               27
96357 #define PragTyp_STATS                         28
96358 #define PragTyp_SYNCHRONOUS                   29
96359 #define PragTyp_TABLE_INFO                    30
96360 #define PragTyp_TEMP_STORE                    31
96361 #define PragTyp_TEMP_STORE_DIRECTORY          32
96362 #define PragTyp_WAL_AUTOCHECKPOINT            33
96363 #define PragTyp_WAL_CHECKPOINT                34
96364 #define PragTyp_ACTIVATE_EXTENSIONS           35
96365 #define PragTyp_HEXKEY                        36
96366 #define PragTyp_KEY                           37
96367 #define PragTyp_REKEY                         38
96368 #define PragTyp_LOCK_STATUS                   39
96369 #define PragTyp_PARSER_TRACE                  40
96370 #define PragFlag_NeedSchema           0x01
96371 static const struct sPragmaNames {
96372   const char *const zName;  /* Name of pragma */
96373   u8 ePragTyp;              /* PragTyp_XXX value */
96374   u8 mPragFlag;             /* Zero or more PragFlag_XXX values */
96375   u32 iArg;                 /* Extra argument */
96376 } aPragmaNames[] = {
96377 #if defined(SQLITE_HAS_CODEC) || defined(SQLITE_ENABLE_CEROD)
96378   { /* zName:     */ "activate_extensions",
96379     /* ePragTyp:  */ PragTyp_ACTIVATE_EXTENSIONS,
96380     /* ePragFlag: */ 0,
96381     /* iArg:      */ 0 },
96382 #endif
96383 #if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
96384   { /* zName:     */ "application_id",
96385     /* ePragTyp:  */ PragTyp_HEADER_VALUE,
96386     /* ePragFlag: */ 0,
96387     /* iArg:      */ 0 },
96388 #endif
96389 #if !defined(SQLITE_OMIT_AUTOVACUUM)
96390   { /* zName:     */ "auto_vacuum",
96391     /* ePragTyp:  */ PragTyp_AUTO_VACUUM,
96392     /* ePragFlag: */ PragFlag_NeedSchema,
96393     /* iArg:      */ 0 },
96394 #endif
96395 #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
96396 #if !defined(SQLITE_OMIT_AUTOMATIC_INDEX)
96397   { /* zName:     */ "automatic_index",
96398     /* ePragTyp:  */ PragTyp_FLAG,
96399     /* ePragFlag: */ 0,
96400     /* iArg:      */ SQLITE_AutoIndex },
96401 #endif
96402 #endif
96403   { /* zName:     */ "busy_timeout",
96404     /* ePragTyp:  */ PragTyp_BUSY_TIMEOUT,
96405     /* ePragFlag: */ 0,
96406     /* iArg:      */ 0 },
96407 #if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
96408   { /* zName:     */ "cache_size",
96409     /* ePragTyp:  */ PragTyp_CACHE_SIZE,
96410     /* ePragFlag: */ PragFlag_NeedSchema,
96411     /* iArg:      */ 0 },
96412 #endif
96413 #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
96414   { /* zName:     */ "cache_spill",
96415     /* ePragTyp:  */ PragTyp_FLAG,
96416     /* ePragFlag: */ 0,
96417     /* iArg:      */ SQLITE_CacheSpill },
96418 #endif
96419   { /* zName:     */ "case_sensitive_like",
96420     /* ePragTyp:  */ PragTyp_CASE_SENSITIVE_LIKE,
96421     /* ePragFlag: */ 0,
96422     /* iArg:      */ 0 },
96423 #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
96424   { /* zName:     */ "checkpoint_fullfsync",
96425     /* ePragTyp:  */ PragTyp_FLAG,
96426     /* ePragFlag: */ 0,
96427     /* iArg:      */ SQLITE_CkptFullFSync },
96428 #endif
96429 #if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
96430   { /* zName:     */ "collation_list",
96431     /* ePragTyp:  */ PragTyp_COLLATION_LIST,
96432     /* ePragFlag: */ 0,
96433     /* iArg:      */ 0 },
96434 #endif
96435 #if !defined(SQLITE_OMIT_COMPILEOPTION_DIAGS)
96436   { /* zName:     */ "compile_options",
96437     /* ePragTyp:  */ PragTyp_COMPILE_OPTIONS,
96438     /* ePragFlag: */ 0,
96439     /* iArg:      */ 0 },
96440 #endif
96441 #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
96442   { /* zName:     */ "count_changes",
96443     /* ePragTyp:  */ PragTyp_FLAG,
96444     /* ePragFlag: */ 0,
96445     /* iArg:      */ SQLITE_CountRows },
96446 #endif
96447 #if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && SQLITE_OS_WIN
96448   { /* zName:     */ "data_store_directory",
96449     /* ePragTyp:  */ PragTyp_DATA_STORE_DIRECTORY,
96450     /* ePragFlag: */ 0,
96451     /* iArg:      */ 0 },
96452 #endif
96453 #if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
96454   { /* zName:     */ "database_list",
96455     /* ePragTyp:  */ PragTyp_DATABASE_LIST,
96456     /* ePragFlag: */ PragFlag_NeedSchema,
96457     /* iArg:      */ 0 },
96458 #endif
96459 #if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && !defined(SQLITE_OMIT_DEPRECATED)
96460   { /* zName:     */ "default_cache_size",
96461     /* ePragTyp:  */ PragTyp_DEFAULT_CACHE_SIZE,
96462     /* ePragFlag: */ PragFlag_NeedSchema,
96463     /* iArg:      */ 0 },
96464 #endif
96465 #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
96466 #if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
96467   { /* zName:     */ "defer_foreign_keys",
96468     /* ePragTyp:  */ PragTyp_FLAG,
96469     /* ePragFlag: */ 0,
96470     /* iArg:      */ SQLITE_DeferFKs },
96471 #endif
96472 #endif
96473 #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
96474   { /* zName:     */ "empty_result_callbacks",
96475     /* ePragTyp:  */ PragTyp_FLAG,
96476     /* ePragFlag: */ 0,
96477     /* iArg:      */ SQLITE_NullCallback },
96478 #endif
96479 #if !defined(SQLITE_OMIT_UTF16)
96480   { /* zName:     */ "encoding",
96481     /* ePragTyp:  */ PragTyp_ENCODING,
96482     /* ePragFlag: */ 0,
96483     /* iArg:      */ 0 },
96484 #endif
96485 #if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
96486   { /* zName:     */ "foreign_key_check",
96487     /* ePragTyp:  */ PragTyp_FOREIGN_KEY_CHECK,
96488     /* ePragFlag: */ PragFlag_NeedSchema,
96489     /* iArg:      */ 0 },
96490 #endif
96491 #if !defined(SQLITE_OMIT_FOREIGN_KEY)
96492   { /* zName:     */ "foreign_key_list",
96493     /* ePragTyp:  */ PragTyp_FOREIGN_KEY_LIST,
96494     /* ePragFlag: */ PragFlag_NeedSchema,
96495     /* iArg:      */ 0 },
96496 #endif
96497 #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
96498 #if !defined(SQLITE_OMIT_FOREIGN_KEY) && !defined(SQLITE_OMIT_TRIGGER)
96499   { /* zName:     */ "foreign_keys",
96500     /* ePragTyp:  */ PragTyp_FLAG,
96501     /* ePragFlag: */ 0,
96502     /* iArg:      */ SQLITE_ForeignKeys },
96503 #endif
96504 #endif
96505 #if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
96506   { /* zName:     */ "freelist_count",
96507     /* ePragTyp:  */ PragTyp_HEADER_VALUE,
96508     /* ePragFlag: */ 0,
96509     /* iArg:      */ 0 },
96510 #endif
96511 #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
96512   { /* zName:     */ "full_column_names",
96513     /* ePragTyp:  */ PragTyp_FLAG,
96514     /* ePragFlag: */ 0,
96515     /* iArg:      */ SQLITE_FullColNames },
96516   { /* zName:     */ "fullfsync",
96517     /* ePragTyp:  */ PragTyp_FLAG,
96518     /* ePragFlag: */ 0,
96519     /* iArg:      */ SQLITE_FullFSync },
96520 #endif
96521 #if defined(SQLITE_HAS_CODEC)
96522   { /* zName:     */ "hexkey",
96523     /* ePragTyp:  */ PragTyp_HEXKEY,
96524     /* ePragFlag: */ 0,
96525     /* iArg:      */ 0 },
96526   { /* zName:     */ "hexrekey",
96527     /* ePragTyp:  */ PragTyp_HEXKEY,
96528     /* ePragFlag: */ 0,
96529     /* iArg:      */ 0 },
96530 #endif
96531 #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
96532 #if !defined(SQLITE_OMIT_CHECK)
96533   { /* zName:     */ "ignore_check_constraints",
96534     /* ePragTyp:  */ PragTyp_FLAG,
96535     /* ePragFlag: */ 0,
96536     /* iArg:      */ SQLITE_IgnoreChecks },
96537 #endif
96538 #endif
96539 #if !defined(SQLITE_OMIT_AUTOVACUUM)
96540   { /* zName:     */ "incremental_vacuum",
96541     /* ePragTyp:  */ PragTyp_INCREMENTAL_VACUUM,
96542     /* ePragFlag: */ PragFlag_NeedSchema,
96543     /* iArg:      */ 0 },
96544 #endif
96545 #if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
96546   { /* zName:     */ "index_info",
96547     /* ePragTyp:  */ PragTyp_INDEX_INFO,
96548     /* ePragFlag: */ PragFlag_NeedSchema,
96549     /* iArg:      */ 0 },
96550   { /* zName:     */ "index_list",
96551     /* ePragTyp:  */ PragTyp_INDEX_LIST,
96552     /* ePragFlag: */ PragFlag_NeedSchema,
96553     /* iArg:      */ 0 },
96554 #endif
96555 #if !defined(SQLITE_OMIT_INTEGRITY_CHECK)
96556   { /* zName:     */ "integrity_check",
96557     /* ePragTyp:  */ PragTyp_INTEGRITY_CHECK,
96558     /* ePragFlag: */ PragFlag_NeedSchema,
96559     /* iArg:      */ 0 },
96560 #endif
96561 #if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
96562   { /* zName:     */ "journal_mode",
96563     /* ePragTyp:  */ PragTyp_JOURNAL_MODE,
96564     /* ePragFlag: */ PragFlag_NeedSchema,
96565     /* iArg:      */ 0 },
96566   { /* zName:     */ "journal_size_limit",
96567     /* ePragTyp:  */ PragTyp_JOURNAL_SIZE_LIMIT,
96568     /* ePragFlag: */ 0,
96569     /* iArg:      */ 0 },
96570 #endif
96571 #if defined(SQLITE_HAS_CODEC)
96572   { /* zName:     */ "key",
96573     /* ePragTyp:  */ PragTyp_KEY,
96574     /* ePragFlag: */ 0,
96575     /* iArg:      */ 0 },
96576 #endif
96577 #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
96578   { /* zName:     */ "legacy_file_format",
96579     /* ePragTyp:  */ PragTyp_FLAG,
96580     /* ePragFlag: */ 0,
96581     /* iArg:      */ SQLITE_LegacyFileFmt },
96582 #endif
96583 #if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && SQLITE_ENABLE_LOCKING_STYLE
96584   { /* zName:     */ "lock_proxy_file",
96585     /* ePragTyp:  */ PragTyp_LOCK_PROXY_FILE,
96586     /* ePragFlag: */ 0,
96587     /* iArg:      */ 0 },
96588 #endif
96589 #if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
96590   { /* zName:     */ "lock_status",
96591     /* ePragTyp:  */ PragTyp_LOCK_STATUS,
96592     /* ePragFlag: */ 0,
96593     /* iArg:      */ 0 },
96594 #endif
96595 #if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
96596   { /* zName:     */ "locking_mode",
96597     /* ePragTyp:  */ PragTyp_LOCKING_MODE,
96598     /* ePragFlag: */ 0,
96599     /* iArg:      */ 0 },
96600   { /* zName:     */ "max_page_count",
96601     /* ePragTyp:  */ PragTyp_PAGE_COUNT,
96602     /* ePragFlag: */ PragFlag_NeedSchema,
96603     /* iArg:      */ 0 },
96604   { /* zName:     */ "mmap_size",
96605     /* ePragTyp:  */ PragTyp_MMAP_SIZE,
96606     /* ePragFlag: */ 0,
96607     /* iArg:      */ 0 },
96608   { /* zName:     */ "page_count",
96609     /* ePragTyp:  */ PragTyp_PAGE_COUNT,
96610     /* ePragFlag: */ PragFlag_NeedSchema,
96611     /* iArg:      */ 0 },
96612   { /* zName:     */ "page_size",
96613     /* ePragTyp:  */ PragTyp_PAGE_SIZE,
96614     /* ePragFlag: */ 0,
96615     /* iArg:      */ 0 },
96616 #endif
96617 #if defined(SQLITE_DEBUG)
96618   { /* zName:     */ "parser_trace",
96619     /* ePragTyp:  */ PragTyp_PARSER_TRACE,
96620     /* ePragFlag: */ 0,
96621     /* iArg:      */ 0 },
96622 #endif
96623 #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
96624   { /* zName:     */ "query_only",
96625     /* ePragTyp:  */ PragTyp_FLAG,
96626     /* ePragFlag: */ 0,
96627     /* iArg:      */ SQLITE_QueryOnly },
96628 #endif
96629 #if !defined(SQLITE_OMIT_INTEGRITY_CHECK)
96630   { /* zName:     */ "quick_check",
96631     /* ePragTyp:  */ PragTyp_INTEGRITY_CHECK,
96632     /* ePragFlag: */ PragFlag_NeedSchema,
96633     /* iArg:      */ 0 },
96634 #endif
96635 #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
96636   { /* zName:     */ "read_uncommitted",
96637     /* ePragTyp:  */ PragTyp_FLAG,
96638     /* ePragFlag: */ 0,
96639     /* iArg:      */ SQLITE_ReadUncommitted },
96640   { /* zName:     */ "recursive_triggers",
96641     /* ePragTyp:  */ PragTyp_FLAG,
96642     /* ePragFlag: */ 0,
96643     /* iArg:      */ SQLITE_RecTriggers },
96644 #endif
96645 #if defined(SQLITE_HAS_CODEC)
96646   { /* zName:     */ "rekey",
96647     /* ePragTyp:  */ PragTyp_REKEY,
96648     /* ePragFlag: */ 0,
96649     /* iArg:      */ 0 },
96650 #endif
96651 #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
96652   { /* zName:     */ "reverse_unordered_selects",
96653     /* ePragTyp:  */ PragTyp_FLAG,
96654     /* ePragFlag: */ 0,
96655     /* iArg:      */ SQLITE_ReverseOrder },
96656 #endif
96657 #if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
96658   { /* zName:     */ "schema_version",
96659     /* ePragTyp:  */ PragTyp_HEADER_VALUE,
96660     /* ePragFlag: */ 0,
96661     /* iArg:      */ 0 },
96662 #endif
96663 #if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
96664   { /* zName:     */ "secure_delete",
96665     /* ePragTyp:  */ PragTyp_SECURE_DELETE,
96666     /* ePragFlag: */ 0,
96667     /* iArg:      */ 0 },
96668 #endif
96669 #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
96670   { /* zName:     */ "short_column_names",
96671     /* ePragTyp:  */ PragTyp_FLAG,
96672     /* ePragFlag: */ 0,
96673     /* iArg:      */ SQLITE_ShortColNames },
96674 #endif
96675   { /* zName:     */ "shrink_memory",
96676     /* ePragTyp:  */ PragTyp_SHRINK_MEMORY,
96677     /* ePragFlag: */ 0,
96678     /* iArg:      */ 0 },
96679   { /* zName:     */ "soft_heap_limit",
96680     /* ePragTyp:  */ PragTyp_SOFT_HEAP_LIMIT,
96681     /* ePragFlag: */ 0,
96682     /* iArg:      */ 0 },
96683 #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
96684 #if defined(SQLITE_DEBUG)
96685   { /* zName:     */ "sql_trace",
96686     /* ePragTyp:  */ PragTyp_FLAG,
96687     /* ePragFlag: */ 0,
96688     /* iArg:      */ SQLITE_SqlTrace },
96689 #endif
96690 #endif
96691 #if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
96692   { /* zName:     */ "stats",
96693     /* ePragTyp:  */ PragTyp_STATS,
96694     /* ePragFlag: */ PragFlag_NeedSchema,
96695     /* iArg:      */ 0 },
96696 #endif
96697 #if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
96698   { /* zName:     */ "synchronous",
96699     /* ePragTyp:  */ PragTyp_SYNCHRONOUS,
96700     /* ePragFlag: */ PragFlag_NeedSchema,
96701     /* iArg:      */ 0 },
96702 #endif
96703 #if !defined(SQLITE_OMIT_SCHEMA_PRAGMAS)
96704   { /* zName:     */ "table_info",
96705     /* ePragTyp:  */ PragTyp_TABLE_INFO,
96706     /* ePragFlag: */ PragFlag_NeedSchema,
96707     /* iArg:      */ 0 },
96708 #endif
96709 #if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
96710   { /* zName:     */ "temp_store",
96711     /* ePragTyp:  */ PragTyp_TEMP_STORE,
96712     /* ePragFlag: */ 0,
96713     /* iArg:      */ 0 },
96714   { /* zName:     */ "temp_store_directory",
96715     /* ePragTyp:  */ PragTyp_TEMP_STORE_DIRECTORY,
96716     /* ePragFlag: */ 0,
96717     /* iArg:      */ 0 },
96718 #endif
96719 #if !defined(SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS)
96720   { /* zName:     */ "user_version",
96721     /* ePragTyp:  */ PragTyp_HEADER_VALUE,
96722     /* ePragFlag: */ 0,
96723     /* iArg:      */ 0 },
96724 #endif
96725 #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
96726 #if defined(SQLITE_DEBUG)
96727   { /* zName:     */ "vdbe_addoptrace",
96728     /* ePragTyp:  */ PragTyp_FLAG,
96729     /* ePragFlag: */ 0,
96730     /* iArg:      */ SQLITE_VdbeAddopTrace },
96731   { /* zName:     */ "vdbe_debug",
96732     /* ePragTyp:  */ PragTyp_FLAG,
96733     /* ePragFlag: */ 0,
96734     /* iArg:      */ SQLITE_SqlTrace|SQLITE_VdbeListing|SQLITE_VdbeTrace },
96735   { /* zName:     */ "vdbe_eqp",
96736     /* ePragTyp:  */ PragTyp_FLAG,
96737     /* ePragFlag: */ 0,
96738     /* iArg:      */ SQLITE_VdbeEQP },
96739   { /* zName:     */ "vdbe_listing",
96740     /* ePragTyp:  */ PragTyp_FLAG,
96741     /* ePragFlag: */ 0,
96742     /* iArg:      */ SQLITE_VdbeListing },
96743   { /* zName:     */ "vdbe_trace",
96744     /* ePragTyp:  */ PragTyp_FLAG,
96745     /* ePragFlag: */ 0,
96746     /* iArg:      */ SQLITE_VdbeTrace },
96747 #endif
96748 #endif
96749 #if !defined(SQLITE_OMIT_WAL)
96750   { /* zName:     */ "wal_autocheckpoint",
96751     /* ePragTyp:  */ PragTyp_WAL_AUTOCHECKPOINT,
96752     /* ePragFlag: */ 0,
96753     /* iArg:      */ 0 },
96754   { /* zName:     */ "wal_checkpoint",
96755     /* ePragTyp:  */ PragTyp_WAL_CHECKPOINT,
96756     /* ePragFlag: */ PragFlag_NeedSchema,
96757     /* iArg:      */ 0 },
96758 #endif
96759 #if !defined(SQLITE_OMIT_FLAG_PRAGMAS)
96760   { /* zName:     */ "writable_schema",
96761     /* ePragTyp:  */ PragTyp_FLAG,
96762     /* ePragFlag: */ 0,
96763     /* iArg:      */ SQLITE_WriteSchema|SQLITE_RecoveryMode },
96764 #endif
96765 };
96766 /* Number of pragmas: 56 on by default, 69 total. */
96767 /* End of the automatically generated pragma table.
96768 ***************************************************************************/
96769 
96770 /*
96771 ** Interpret the given string as a safety level.  Return 0 for OFF,
96772 ** 1 for ON or NORMAL and 2 for FULL.  Return 1 for an empty or
96773 ** unrecognized string argument.  The FULL option is disallowed
96774 ** if the omitFull parameter it 1.
96775 **
96776 ** Note that the values returned are one less that the values that
96777 ** should be passed into sqlite3BtreeSetSafetyLevel().  The is done
96778 ** to support legacy SQL code.  The safety level used to be boolean
96779 ** and older scripts may have used numbers 0 for OFF and 1 for ON.
96780 */
96781 static u8 getSafetyLevel(const char *z, int omitFull, int dflt){
96782                              /* 123456789 123456789 */
96783   static const char zText[] = "onoffalseyestruefull";
96784   static const u8 iOffset[] = {0, 1, 2, 4, 9, 12, 16};
96785   static const u8 iLength[] = {2, 2, 3, 5, 3, 4, 4};
96786   static const u8 iValue[] =  {1, 0, 0, 0, 1, 1, 2};
96787   int i, n;
96788   if( sqlite3Isdigit(*z) ){
96789     return (u8)sqlite3Atoi(z);
96790   }
96791   n = sqlite3Strlen30(z);
96792   for(i=0; i<ArraySize(iLength)-omitFull; i++){
96793     if( iLength[i]==n && sqlite3StrNICmp(&zText[iOffset[i]],z,n)==0 ){
96794       return iValue[i];
96795     }
96796   }
96797   return dflt;
96798 }
96799 
96800 /*
96801 ** Interpret the given string as a boolean value.
96802 */
96803 SQLITE_PRIVATE u8 sqlite3GetBoolean(const char *z, int dflt){
96804   return getSafetyLevel(z,1,dflt)!=0;
96805 }
96806 
96807 /* The sqlite3GetBoolean() function is used by other modules but the
96808 ** remainder of this file is specific to PRAGMA processing.  So omit
96809 ** the rest of the file if PRAGMAs are omitted from the build.
96810 */
96811 #if !defined(SQLITE_OMIT_PRAGMA)
96812 
96813 /*
96814 ** Interpret the given string as a locking mode value.
96815 */
96816 static int getLockingMode(const char *z){
96817   if( z ){
96818     if( 0==sqlite3StrICmp(z, "exclusive") ) return PAGER_LOCKINGMODE_EXCLUSIVE;
96819     if( 0==sqlite3StrICmp(z, "normal") ) return PAGER_LOCKINGMODE_NORMAL;
96820   }
96821   return PAGER_LOCKINGMODE_QUERY;
96822 }
96823 
96824 #ifndef SQLITE_OMIT_AUTOVACUUM
96825 /*
96826 ** Interpret the given string as an auto-vacuum mode value.
96827 **
96828 ** The following strings, "none", "full" and "incremental" are
96829 ** acceptable, as are their numeric equivalents: 0, 1 and 2 respectively.
96830 */
96831 static int getAutoVacuum(const char *z){
96832   int i;
96833   if( 0==sqlite3StrICmp(z, "none") ) return BTREE_AUTOVACUUM_NONE;
96834   if( 0==sqlite3StrICmp(z, "full") ) return BTREE_AUTOVACUUM_FULL;
96835   if( 0==sqlite3StrICmp(z, "incremental") ) return BTREE_AUTOVACUUM_INCR;
96836   i = sqlite3Atoi(z);
96837   return (u8)((i>=0&&i<=2)?i:0);
96838 }
96839 #endif /* ifndef SQLITE_OMIT_AUTOVACUUM */
96840 
96841 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
96842 /*
96843 ** Interpret the given string as a temp db location. Return 1 for file
96844 ** backed temporary databases, 2 for the Red-Black tree in memory database
96845 ** and 0 to use the compile-time default.
96846 */
96847 static int getTempStore(const char *z){
96848   if( z[0]>='0' && z[0]<='2' ){
96849     return z[0] - '0';
96850   }else if( sqlite3StrICmp(z, "file")==0 ){
96851     return 1;
96852   }else if( sqlite3StrICmp(z, "memory")==0 ){
96853     return 2;
96854   }else{
96855     return 0;
96856   }
96857 }
96858 #endif /* SQLITE_PAGER_PRAGMAS */
96859 
96860 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
96861 /*
96862 ** Invalidate temp storage, either when the temp storage is changed
96863 ** from default, or when 'file' and the temp_store_directory has changed
96864 */
96865 static int invalidateTempStorage(Parse *pParse){
96866   sqlite3 *db = pParse->db;
96867   if( db->aDb[1].pBt!=0 ){
96868     if( !db->autoCommit || sqlite3BtreeIsInReadTrans(db->aDb[1].pBt) ){
96869       sqlite3ErrorMsg(pParse, "temporary storage cannot be changed "
96870         "from within a transaction");
96871       return SQLITE_ERROR;
96872     }
96873     sqlite3BtreeClose(db->aDb[1].pBt);
96874     db->aDb[1].pBt = 0;
96875     sqlite3ResetAllSchemasOfConnection(db);
96876   }
96877   return SQLITE_OK;
96878 }
96879 #endif /* SQLITE_PAGER_PRAGMAS */
96880 
96881 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
96882 /*
96883 ** If the TEMP database is open, close it and mark the database schema
96884 ** as needing reloading.  This must be done when using the SQLITE_TEMP_STORE
96885 ** or DEFAULT_TEMP_STORE pragmas.
96886 */
96887 static int changeTempStorage(Parse *pParse, const char *zStorageType){
96888   int ts = getTempStore(zStorageType);
96889   sqlite3 *db = pParse->db;
96890   if( db->temp_store==ts ) return SQLITE_OK;
96891   if( invalidateTempStorage( pParse ) != SQLITE_OK ){
96892     return SQLITE_ERROR;
96893   }
96894   db->temp_store = (u8)ts;
96895   return SQLITE_OK;
96896 }
96897 #endif /* SQLITE_PAGER_PRAGMAS */
96898 
96899 /*
96900 ** Generate code to return a single integer value.
96901 */
96902 static void returnSingleInt(Parse *pParse, const char *zLabel, i64 value){
96903   Vdbe *v = sqlite3GetVdbe(pParse);
96904   int mem = ++pParse->nMem;
96905   i64 *pI64 = sqlite3DbMallocRaw(pParse->db, sizeof(value));
96906   if( pI64 ){
96907     memcpy(pI64, &value, sizeof(value));
96908   }
96909   sqlite3VdbeAddOp4(v, OP_Int64, 0, mem, 0, (char*)pI64, P4_INT64);
96910   sqlite3VdbeSetNumCols(v, 1);
96911   sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLabel, SQLITE_STATIC);
96912   sqlite3VdbeAddOp2(v, OP_ResultRow, mem, 1);
96913 }
96914 
96915 
96916 /*
96917 ** Set the safety_level and pager flags for pager iDb.  Or if iDb<0
96918 ** set these values for all pagers.
96919 */
96920 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
96921 static void setAllPagerFlags(sqlite3 *db){
96922   if( db->autoCommit ){
96923     Db *pDb = db->aDb;
96924     int n = db->nDb;
96925     assert( SQLITE_FullFSync==PAGER_FULLFSYNC );
96926     assert( SQLITE_CkptFullFSync==PAGER_CKPT_FULLFSYNC );
96927     assert( SQLITE_CacheSpill==PAGER_CACHESPILL );
96928     assert( (PAGER_FULLFSYNC | PAGER_CKPT_FULLFSYNC | PAGER_CACHESPILL)
96929              ==  PAGER_FLAGS_MASK );
96930     assert( (pDb->safety_level & PAGER_SYNCHRONOUS_MASK)==pDb->safety_level );
96931     while( (n--) > 0 ){
96932       if( pDb->pBt ){
96933         sqlite3BtreeSetPagerFlags(pDb->pBt,
96934                  pDb->safety_level | (db->flags & PAGER_FLAGS_MASK) );
96935       }
96936       pDb++;
96937     }
96938   }
96939 }
96940 #else
96941 # define setAllPagerFlags(X)  /* no-op */
96942 #endif
96943 
96944 
96945 /*
96946 ** Return a human-readable name for a constraint resolution action.
96947 */
96948 #ifndef SQLITE_OMIT_FOREIGN_KEY
96949 static const char *actionName(u8 action){
96950   const char *zName;
96951   switch( action ){
96952     case OE_SetNull:  zName = "SET NULL";        break;
96953     case OE_SetDflt:  zName = "SET DEFAULT";     break;
96954     case OE_Cascade:  zName = "CASCADE";         break;
96955     case OE_Restrict: zName = "RESTRICT";        break;
96956     default:          zName = "NO ACTION";
96957                       assert( action==OE_None ); break;
96958   }
96959   return zName;
96960 }
96961 #endif
96962 
96963 
96964 /*
96965 ** Parameter eMode must be one of the PAGER_JOURNALMODE_XXX constants
96966 ** defined in pager.h. This function returns the associated lowercase
96967 ** journal-mode name.
96968 */
96969 SQLITE_PRIVATE const char *sqlite3JournalModename(int eMode){
96970   static char * const azModeName[] = {
96971     "delete", "persist", "off", "truncate", "memory"
96972 #ifndef SQLITE_OMIT_WAL
96973      , "wal"
96974 #endif
96975   };
96976   assert( PAGER_JOURNALMODE_DELETE==0 );
96977   assert( PAGER_JOURNALMODE_PERSIST==1 );
96978   assert( PAGER_JOURNALMODE_OFF==2 );
96979   assert( PAGER_JOURNALMODE_TRUNCATE==3 );
96980   assert( PAGER_JOURNALMODE_MEMORY==4 );
96981   assert( PAGER_JOURNALMODE_WAL==5 );
96982   assert( eMode>=0 && eMode<=ArraySize(azModeName) );
96983 
96984   if( eMode==ArraySize(azModeName) ) return 0;
96985   return azModeName[eMode];
96986 }
96987 
96988 /*
96989 ** Process a pragma statement.
96990 **
96991 ** Pragmas are of this form:
96992 **
96993 **      PRAGMA [database.]id [= value]
96994 **
96995 ** The identifier might also be a string.  The value is a string, and
96996 ** identifier, or a number.  If minusFlag is true, then the value is
96997 ** a number that was preceded by a minus sign.
96998 **
96999 ** If the left side is "database.id" then pId1 is the database name
97000 ** and pId2 is the id.  If the left side is just "id" then pId1 is the
97001 ** id and pId2 is any empty string.
97002 */
97003 SQLITE_PRIVATE void sqlite3Pragma(
97004   Parse *pParse,
97005   Token *pId1,        /* First part of [database.]id field */
97006   Token *pId2,        /* Second part of [database.]id field, or NULL */
97007   Token *pValue,      /* Token for <value>, or NULL */
97008   int minusFlag       /* True if a '-' sign preceded <value> */
97009 ){
97010   char *zLeft = 0;       /* Nul-terminated UTF-8 string <id> */
97011   char *zRight = 0;      /* Nul-terminated UTF-8 string <value>, or NULL */
97012   const char *zDb = 0;   /* The database name */
97013   Token *pId;            /* Pointer to <id> token */
97014   char *aFcntl[4];       /* Argument to SQLITE_FCNTL_PRAGMA */
97015   int iDb;               /* Database index for <database> */
97016   int lwr, upr, mid;           /* Binary search bounds */
97017   int rc;                      /* return value form SQLITE_FCNTL_PRAGMA */
97018   sqlite3 *db = pParse->db;    /* The database connection */
97019   Db *pDb;                     /* The specific database being pragmaed */
97020   Vdbe *v = sqlite3GetVdbe(pParse);  /* Prepared statement */
97021 
97022   if( v==0 ) return;
97023   sqlite3VdbeRunOnlyOnce(v);
97024   pParse->nMem = 2;
97025 
97026   /* Interpret the [database.] part of the pragma statement. iDb is the
97027   ** index of the database this pragma is being applied to in db.aDb[]. */
97028   iDb = sqlite3TwoPartName(pParse, pId1, pId2, &pId);
97029   if( iDb<0 ) return;
97030   pDb = &db->aDb[iDb];
97031 
97032   /* If the temp database has been explicitly named as part of the
97033   ** pragma, make sure it is open.
97034   */
97035   if( iDb==1 && sqlite3OpenTempDatabase(pParse) ){
97036     return;
97037   }
97038 
97039   zLeft = sqlite3NameFromToken(db, pId);
97040   if( !zLeft ) return;
97041   if( minusFlag ){
97042     zRight = sqlite3MPrintf(db, "-%T", pValue);
97043   }else{
97044     zRight = sqlite3NameFromToken(db, pValue);
97045   }
97046 
97047   assert( pId2 );
97048   zDb = pId2->n>0 ? pDb->zName : 0;
97049   if( sqlite3AuthCheck(pParse, SQLITE_PRAGMA, zLeft, zRight, zDb) ){
97050     goto pragma_out;
97051   }
97052 
97053   /* Send an SQLITE_FCNTL_PRAGMA file-control to the underlying VFS
97054   ** connection.  If it returns SQLITE_OK, then assume that the VFS
97055   ** handled the pragma and generate a no-op prepared statement.
97056   */
97057   aFcntl[0] = 0;
97058   aFcntl[1] = zLeft;
97059   aFcntl[2] = zRight;
97060   aFcntl[3] = 0;
97061   db->busyHandler.nBusy = 0;
97062   rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_PRAGMA, (void*)aFcntl);
97063   if( rc==SQLITE_OK ){
97064     if( aFcntl[0] ){
97065       int mem = ++pParse->nMem;
97066       sqlite3VdbeAddOp4(v, OP_String8, 0, mem, 0, aFcntl[0], 0);
97067       sqlite3VdbeSetNumCols(v, 1);
97068       sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "result", SQLITE_STATIC);
97069       sqlite3VdbeAddOp2(v, OP_ResultRow, mem, 1);
97070       sqlite3_free(aFcntl[0]);
97071     }
97072     goto pragma_out;
97073   }
97074   if( rc!=SQLITE_NOTFOUND ){
97075     if( aFcntl[0] ){
97076       sqlite3ErrorMsg(pParse, "%s", aFcntl[0]);
97077       sqlite3_free(aFcntl[0]);
97078     }
97079     pParse->nErr++;
97080     pParse->rc = rc;
97081     goto pragma_out;
97082   }
97083 
97084   /* Locate the pragma in the lookup table */
97085   lwr = 0;
97086   upr = ArraySize(aPragmaNames)-1;
97087   while( lwr<=upr ){
97088     mid = (lwr+upr)/2;
97089     rc = sqlite3_stricmp(zLeft, aPragmaNames[mid].zName);
97090     if( rc==0 ) break;
97091     if( rc<0 ){
97092       upr = mid - 1;
97093     }else{
97094       lwr = mid + 1;
97095     }
97096   }
97097   if( lwr>upr ) goto pragma_out;
97098 
97099   /* Make sure the database schema is loaded if the pragma requires that */
97100   if( (aPragmaNames[mid].mPragFlag & PragFlag_NeedSchema)!=0 ){
97101     if( sqlite3ReadSchema(pParse) ) goto pragma_out;
97102   }
97103 
97104   /* Jump to the appropriate pragma handler */
97105   switch( aPragmaNames[mid].ePragTyp ){
97106 
97107 #if !defined(SQLITE_OMIT_PAGER_PRAGMAS) && !defined(SQLITE_OMIT_DEPRECATED)
97108   /*
97109   **  PRAGMA [database.]default_cache_size
97110   **  PRAGMA [database.]default_cache_size=N
97111   **
97112   ** The first form reports the current persistent setting for the
97113   ** page cache size.  The value returned is the maximum number of
97114   ** pages in the page cache.  The second form sets both the current
97115   ** page cache size value and the persistent page cache size value
97116   ** stored in the database file.
97117   **
97118   ** Older versions of SQLite would set the default cache size to a
97119   ** negative number to indicate synchronous=OFF.  These days, synchronous
97120   ** is always on by default regardless of the sign of the default cache
97121   ** size.  But continue to take the absolute value of the default cache
97122   ** size of historical compatibility.
97123   */
97124   case PragTyp_DEFAULT_CACHE_SIZE: {
97125     static const VdbeOpList getCacheSize[] = {
97126       { OP_Transaction, 0, 0,        0},                         /* 0 */
97127       { OP_ReadCookie,  0, 1,        BTREE_DEFAULT_CACHE_SIZE},  /* 1 */
97128       { OP_IfPos,       1, 8,        0},
97129       { OP_Integer,     0, 2,        0},
97130       { OP_Subtract,    1, 2,        1},
97131       { OP_IfPos,       1, 8,        0},
97132       { OP_Integer,     0, 1,        0},                         /* 6 */
97133       { OP_Noop,        0, 0,        0},
97134       { OP_ResultRow,   1, 1,        0},
97135     };
97136     int addr;
97137     sqlite3VdbeUsesBtree(v, iDb);
97138     if( !zRight ){
97139       sqlite3VdbeSetNumCols(v, 1);
97140       sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "cache_size", SQLITE_STATIC);
97141       pParse->nMem += 2;
97142       addr = sqlite3VdbeAddOpList(v, ArraySize(getCacheSize), getCacheSize);
97143       sqlite3VdbeChangeP1(v, addr, iDb);
97144       sqlite3VdbeChangeP1(v, addr+1, iDb);
97145       sqlite3VdbeChangeP1(v, addr+6, SQLITE_DEFAULT_CACHE_SIZE);
97146     }else{
97147       int size = sqlite3AbsInt32(sqlite3Atoi(zRight));
97148       sqlite3BeginWriteOperation(pParse, 0, iDb);
97149       sqlite3VdbeAddOp2(v, OP_Integer, size, 1);
97150       sqlite3VdbeAddOp3(v, OP_SetCookie, iDb, BTREE_DEFAULT_CACHE_SIZE, 1);
97151       assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
97152       pDb->pSchema->cache_size = size;
97153       sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
97154     }
97155     break;
97156   }
97157 #endif /* !SQLITE_OMIT_PAGER_PRAGMAS && !SQLITE_OMIT_DEPRECATED */
97158 
97159 #if !defined(SQLITE_OMIT_PAGER_PRAGMAS)
97160   /*
97161   **  PRAGMA [database.]page_size
97162   **  PRAGMA [database.]page_size=N
97163   **
97164   ** The first form reports the current setting for the
97165   ** database page size in bytes.  The second form sets the
97166   ** database page size value.  The value can only be set if
97167   ** the database has not yet been created.
97168   */
97169   case PragTyp_PAGE_SIZE: {
97170     Btree *pBt = pDb->pBt;
97171     assert( pBt!=0 );
97172     if( !zRight ){
97173       int size = ALWAYS(pBt) ? sqlite3BtreeGetPageSize(pBt) : 0;
97174       returnSingleInt(pParse, "page_size", size);
97175     }else{
97176       /* Malloc may fail when setting the page-size, as there is an internal
97177       ** buffer that the pager module resizes using sqlite3_realloc().
97178       */
97179       db->nextPagesize = sqlite3Atoi(zRight);
97180       if( SQLITE_NOMEM==sqlite3BtreeSetPageSize(pBt, db->nextPagesize,-1,0) ){
97181         db->mallocFailed = 1;
97182       }
97183     }
97184     break;
97185   }
97186 
97187   /*
97188   **  PRAGMA [database.]secure_delete
97189   **  PRAGMA [database.]secure_delete=ON/OFF
97190   **
97191   ** The first form reports the current setting for the
97192   ** secure_delete flag.  The second form changes the secure_delete
97193   ** flag setting and reports thenew value.
97194   */
97195   case PragTyp_SECURE_DELETE: {
97196     Btree *pBt = pDb->pBt;
97197     int b = -1;
97198     assert( pBt!=0 );
97199     if( zRight ){
97200       b = sqlite3GetBoolean(zRight, 0);
97201     }
97202     if( pId2->n==0 && b>=0 ){
97203       int ii;
97204       for(ii=0; ii<db->nDb; ii++){
97205         sqlite3BtreeSecureDelete(db->aDb[ii].pBt, b);
97206       }
97207     }
97208     b = sqlite3BtreeSecureDelete(pBt, b);
97209     returnSingleInt(pParse, "secure_delete", b);
97210     break;
97211   }
97212 
97213   /*
97214   **  PRAGMA [database.]max_page_count
97215   **  PRAGMA [database.]max_page_count=N
97216   **
97217   ** The first form reports the current setting for the
97218   ** maximum number of pages in the database file.  The
97219   ** second form attempts to change this setting.  Both
97220   ** forms return the current setting.
97221   **
97222   ** The absolute value of N is used.  This is undocumented and might
97223   ** change.  The only purpose is to provide an easy way to test
97224   ** the sqlite3AbsInt32() function.
97225   **
97226   **  PRAGMA [database.]page_count
97227   **
97228   ** Return the number of pages in the specified database.
97229   */
97230   case PragTyp_PAGE_COUNT: {
97231     int iReg;
97232     sqlite3CodeVerifySchema(pParse, iDb);
97233     iReg = ++pParse->nMem;
97234     if( sqlite3Tolower(zLeft[0])=='p' ){
97235       sqlite3VdbeAddOp2(v, OP_Pagecount, iDb, iReg);
97236     }else{
97237       sqlite3VdbeAddOp3(v, OP_MaxPgcnt, iDb, iReg,
97238                         sqlite3AbsInt32(sqlite3Atoi(zRight)));
97239     }
97240     sqlite3VdbeAddOp2(v, OP_ResultRow, iReg, 1);
97241     sqlite3VdbeSetNumCols(v, 1);
97242     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLeft, SQLITE_TRANSIENT);
97243     break;
97244   }
97245 
97246   /*
97247   **  PRAGMA [database.]locking_mode
97248   **  PRAGMA [database.]locking_mode = (normal|exclusive)
97249   */
97250   case PragTyp_LOCKING_MODE: {
97251     const char *zRet = "normal";
97252     int eMode = getLockingMode(zRight);
97253 
97254     if( pId2->n==0 && eMode==PAGER_LOCKINGMODE_QUERY ){
97255       /* Simple "PRAGMA locking_mode;" statement. This is a query for
97256       ** the current default locking mode (which may be different to
97257       ** the locking-mode of the main database).
97258       */
97259       eMode = db->dfltLockMode;
97260     }else{
97261       Pager *pPager;
97262       if( pId2->n==0 ){
97263         /* This indicates that no database name was specified as part
97264         ** of the PRAGMA command. In this case the locking-mode must be
97265         ** set on all attached databases, as well as the main db file.
97266         **
97267         ** Also, the sqlite3.dfltLockMode variable is set so that
97268         ** any subsequently attached databases also use the specified
97269         ** locking mode.
97270         */
97271         int ii;
97272         assert(pDb==&db->aDb[0]);
97273         for(ii=2; ii<db->nDb; ii++){
97274           pPager = sqlite3BtreePager(db->aDb[ii].pBt);
97275           sqlite3PagerLockingMode(pPager, eMode);
97276         }
97277         db->dfltLockMode = (u8)eMode;
97278       }
97279       pPager = sqlite3BtreePager(pDb->pBt);
97280       eMode = sqlite3PagerLockingMode(pPager, eMode);
97281     }
97282 
97283     assert( eMode==PAGER_LOCKINGMODE_NORMAL
97284             || eMode==PAGER_LOCKINGMODE_EXCLUSIVE );
97285     if( eMode==PAGER_LOCKINGMODE_EXCLUSIVE ){
97286       zRet = "exclusive";
97287     }
97288     sqlite3VdbeSetNumCols(v, 1);
97289     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "locking_mode", SQLITE_STATIC);
97290     sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, zRet, 0);
97291     sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
97292     break;
97293   }
97294 
97295   /*
97296   **  PRAGMA [database.]journal_mode
97297   **  PRAGMA [database.]journal_mode =
97298   **                      (delete|persist|off|truncate|memory|wal|off)
97299   */
97300   case PragTyp_JOURNAL_MODE: {
97301     int eMode;        /* One of the PAGER_JOURNALMODE_XXX symbols */
97302     int ii;           /* Loop counter */
97303 
97304     sqlite3VdbeSetNumCols(v, 1);
97305     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "journal_mode", SQLITE_STATIC);
97306 
97307     if( zRight==0 ){
97308       /* If there is no "=MODE" part of the pragma, do a query for the
97309       ** current mode */
97310       eMode = PAGER_JOURNALMODE_QUERY;
97311     }else{
97312       const char *zMode;
97313       int n = sqlite3Strlen30(zRight);
97314       for(eMode=0; (zMode = sqlite3JournalModename(eMode))!=0; eMode++){
97315         if( sqlite3StrNICmp(zRight, zMode, n)==0 ) break;
97316       }
97317       if( !zMode ){
97318         /* If the "=MODE" part does not match any known journal mode,
97319         ** then do a query */
97320         eMode = PAGER_JOURNALMODE_QUERY;
97321       }
97322     }
97323     if( eMode==PAGER_JOURNALMODE_QUERY && pId2->n==0 ){
97324       /* Convert "PRAGMA journal_mode" into "PRAGMA main.journal_mode" */
97325       iDb = 0;
97326       pId2->n = 1;
97327     }
97328     for(ii=db->nDb-1; ii>=0; ii--){
97329       if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
97330         sqlite3VdbeUsesBtree(v, ii);
97331         sqlite3VdbeAddOp3(v, OP_JournalMode, ii, 1, eMode);
97332       }
97333     }
97334     sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
97335     break;
97336   }
97337 
97338   /*
97339   **  PRAGMA [database.]journal_size_limit
97340   **  PRAGMA [database.]journal_size_limit=N
97341   **
97342   ** Get or set the size limit on rollback journal files.
97343   */
97344   case PragTyp_JOURNAL_SIZE_LIMIT: {
97345     Pager *pPager = sqlite3BtreePager(pDb->pBt);
97346     i64 iLimit = -2;
97347     if( zRight ){
97348       sqlite3Atoi64(zRight, &iLimit, sqlite3Strlen30(zRight), SQLITE_UTF8);
97349       if( iLimit<-1 ) iLimit = -1;
97350     }
97351     iLimit = sqlite3PagerJournalSizeLimit(pPager, iLimit);
97352     returnSingleInt(pParse, "journal_size_limit", iLimit);
97353     break;
97354   }
97355 
97356 #endif /* SQLITE_OMIT_PAGER_PRAGMAS */
97357 
97358   /*
97359   **  PRAGMA [database.]auto_vacuum
97360   **  PRAGMA [database.]auto_vacuum=N
97361   **
97362   ** Get or set the value of the database 'auto-vacuum' parameter.
97363   ** The value is one of:  0 NONE 1 FULL 2 INCREMENTAL
97364   */
97365 #ifndef SQLITE_OMIT_AUTOVACUUM
97366   case PragTyp_AUTO_VACUUM: {
97367     Btree *pBt = pDb->pBt;
97368     assert( pBt!=0 );
97369     if( !zRight ){
97370       returnSingleInt(pParse, "auto_vacuum", sqlite3BtreeGetAutoVacuum(pBt));
97371     }else{
97372       int eAuto = getAutoVacuum(zRight);
97373       assert( eAuto>=0 && eAuto<=2 );
97374       db->nextAutovac = (u8)eAuto;
97375       /* Call SetAutoVacuum() to set initialize the internal auto and
97376       ** incr-vacuum flags. This is required in case this connection
97377       ** creates the database file. It is important that it is created
97378       ** as an auto-vacuum capable db.
97379       */
97380       rc = sqlite3BtreeSetAutoVacuum(pBt, eAuto);
97381       if( rc==SQLITE_OK && (eAuto==1 || eAuto==2) ){
97382         /* When setting the auto_vacuum mode to either "full" or
97383         ** "incremental", write the value of meta[6] in the database
97384         ** file. Before writing to meta[6], check that meta[3] indicates
97385         ** that this really is an auto-vacuum capable database.
97386         */
97387         static const VdbeOpList setMeta6[] = {
97388           { OP_Transaction,    0,         1,                 0},    /* 0 */
97389           { OP_ReadCookie,     0,         1,         BTREE_LARGEST_ROOT_PAGE},
97390           { OP_If,             1,         0,                 0},    /* 2 */
97391           { OP_Halt,           SQLITE_OK, OE_Abort,          0},    /* 3 */
97392           { OP_Integer,        0,         1,                 0},    /* 4 */
97393           { OP_SetCookie,      0,         BTREE_INCR_VACUUM, 1},    /* 5 */
97394         };
97395         int iAddr;
97396         iAddr = sqlite3VdbeAddOpList(v, ArraySize(setMeta6), setMeta6);
97397         sqlite3VdbeChangeP1(v, iAddr, iDb);
97398         sqlite3VdbeChangeP1(v, iAddr+1, iDb);
97399         sqlite3VdbeChangeP2(v, iAddr+2, iAddr+4);
97400         sqlite3VdbeChangeP1(v, iAddr+4, eAuto-1);
97401         sqlite3VdbeChangeP1(v, iAddr+5, iDb);
97402         sqlite3VdbeUsesBtree(v, iDb);
97403       }
97404     }
97405     break;
97406   }
97407 #endif
97408 
97409   /*
97410   **  PRAGMA [database.]incremental_vacuum(N)
97411   **
97412   ** Do N steps of incremental vacuuming on a database.
97413   */
97414 #ifndef SQLITE_OMIT_AUTOVACUUM
97415   case PragTyp_INCREMENTAL_VACUUM: {
97416     int iLimit, addr;
97417     if( zRight==0 || !sqlite3GetInt32(zRight, &iLimit) || iLimit<=0 ){
97418       iLimit = 0x7fffffff;
97419     }
97420     sqlite3BeginWriteOperation(pParse, 0, iDb);
97421     sqlite3VdbeAddOp2(v, OP_Integer, iLimit, 1);
97422     addr = sqlite3VdbeAddOp1(v, OP_IncrVacuum, iDb);
97423     sqlite3VdbeAddOp1(v, OP_ResultRow, 1);
97424     sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1);
97425     sqlite3VdbeAddOp2(v, OP_IfPos, 1, addr);
97426     sqlite3VdbeJumpHere(v, addr);
97427     break;
97428   }
97429 #endif
97430 
97431 #ifndef SQLITE_OMIT_PAGER_PRAGMAS
97432   /*
97433   **  PRAGMA [database.]cache_size
97434   **  PRAGMA [database.]cache_size=N
97435   **
97436   ** The first form reports the current local setting for the
97437   ** page cache size. The second form sets the local
97438   ** page cache size value.  If N is positive then that is the
97439   ** number of pages in the cache.  If N is negative, then the
97440   ** number of pages is adjusted so that the cache uses -N kibibytes
97441   ** of memory.
97442   */
97443   case PragTyp_CACHE_SIZE: {
97444     assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
97445     if( !zRight ){
97446       returnSingleInt(pParse, "cache_size", pDb->pSchema->cache_size);
97447     }else{
97448       int size = sqlite3Atoi(zRight);
97449       pDb->pSchema->cache_size = size;
97450       sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
97451     }
97452     break;
97453   }
97454 
97455   /*
97456   **  PRAGMA [database.]mmap_size(N)
97457   **
97458   ** Used to set mapping size limit. The mapping size limit is
97459   ** used to limit the aggregate size of all memory mapped regions of the
97460   ** database file. If this parameter is set to zero, then memory mapping
97461   ** is not used at all.  If N is negative, then the default memory map
97462   ** limit determined by sqlite3_config(SQLITE_CONFIG_MMAP_SIZE) is set.
97463   ** The parameter N is measured in bytes.
97464   **
97465   ** This value is advisory.  The underlying VFS is free to memory map
97466   ** as little or as much as it wants.  Except, if N is set to 0 then the
97467   ** upper layers will never invoke the xFetch interfaces to the VFS.
97468   */
97469   case PragTyp_MMAP_SIZE: {
97470     sqlite3_int64 sz;
97471 #if SQLITE_MAX_MMAP_SIZE>0
97472     assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
97473     if( zRight ){
97474       int ii;
97475       sqlite3Atoi64(zRight, &sz, sqlite3Strlen30(zRight), SQLITE_UTF8);
97476       if( sz<0 ) sz = sqlite3GlobalConfig.szMmap;
97477       if( pId2->n==0 ) db->szMmap = sz;
97478       for(ii=db->nDb-1; ii>=0; ii--){
97479         if( db->aDb[ii].pBt && (ii==iDb || pId2->n==0) ){
97480           sqlite3BtreeSetMmapLimit(db->aDb[ii].pBt, sz);
97481         }
97482       }
97483     }
97484     sz = -1;
97485     rc = sqlite3_file_control(db, zDb, SQLITE_FCNTL_MMAP_SIZE, &sz);
97486 #else
97487     sz = 0;
97488     rc = SQLITE_OK;
97489 #endif
97490     if( rc==SQLITE_OK ){
97491       returnSingleInt(pParse, "mmap_size", sz);
97492     }else if( rc!=SQLITE_NOTFOUND ){
97493       pParse->nErr++;
97494       pParse->rc = rc;
97495     }
97496     break;
97497   }
97498 
97499   /*
97500   **   PRAGMA temp_store
97501   **   PRAGMA temp_store = "default"|"memory"|"file"
97502   **
97503   ** Return or set the local value of the temp_store flag.  Changing
97504   ** the local value does not make changes to the disk file and the default
97505   ** value will be restored the next time the database is opened.
97506   **
97507   ** Note that it is possible for the library compile-time options to
97508   ** override this setting
97509   */
97510   case PragTyp_TEMP_STORE: {
97511     if( !zRight ){
97512       returnSingleInt(pParse, "temp_store", db->temp_store);
97513     }else{
97514       changeTempStorage(pParse, zRight);
97515     }
97516     break;
97517   }
97518 
97519   /*
97520   **   PRAGMA temp_store_directory
97521   **   PRAGMA temp_store_directory = ""|"directory_name"
97522   **
97523   ** Return or set the local value of the temp_store_directory flag.  Changing
97524   ** the value sets a specific directory to be used for temporary files.
97525   ** Setting to a null string reverts to the default temporary directory search.
97526   ** If temporary directory is changed, then invalidateTempStorage.
97527   **
97528   */
97529   case PragTyp_TEMP_STORE_DIRECTORY: {
97530     if( !zRight ){
97531       if( sqlite3_temp_directory ){
97532         sqlite3VdbeSetNumCols(v, 1);
97533         sqlite3VdbeSetColName(v, 0, COLNAME_NAME,
97534             "temp_store_directory", SQLITE_STATIC);
97535         sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, sqlite3_temp_directory, 0);
97536         sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
97537       }
97538     }else{
97539 #ifndef SQLITE_OMIT_WSD
97540       if( zRight[0] ){
97541         int res;
97542         rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
97543         if( rc!=SQLITE_OK || res==0 ){
97544           sqlite3ErrorMsg(pParse, "not a writable directory");
97545           goto pragma_out;
97546         }
97547       }
97548       if( SQLITE_TEMP_STORE==0
97549        || (SQLITE_TEMP_STORE==1 && db->temp_store<=1)
97550        || (SQLITE_TEMP_STORE==2 && db->temp_store==1)
97551       ){
97552         invalidateTempStorage(pParse);
97553       }
97554       sqlite3_free(sqlite3_temp_directory);
97555       if( zRight[0] ){
97556         sqlite3_temp_directory = sqlite3_mprintf("%s", zRight);
97557       }else{
97558         sqlite3_temp_directory = 0;
97559       }
97560 #endif /* SQLITE_OMIT_WSD */
97561     }
97562     break;
97563   }
97564 
97565 #if SQLITE_OS_WIN
97566   /*
97567   **   PRAGMA data_store_directory
97568   **   PRAGMA data_store_directory = ""|"directory_name"
97569   **
97570   ** Return or set the local value of the data_store_directory flag.  Changing
97571   ** the value sets a specific directory to be used for database files that
97572   ** were specified with a relative pathname.  Setting to a null string reverts
97573   ** to the default database directory, which for database files specified with
97574   ** a relative path will probably be based on the current directory for the
97575   ** process.  Database file specified with an absolute path are not impacted
97576   ** by this setting, regardless of its value.
97577   **
97578   */
97579   case PragTyp_DATA_STORE_DIRECTORY: {
97580     if( !zRight ){
97581       if( sqlite3_data_directory ){
97582         sqlite3VdbeSetNumCols(v, 1);
97583         sqlite3VdbeSetColName(v, 0, COLNAME_NAME,
97584             "data_store_directory", SQLITE_STATIC);
97585         sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, sqlite3_data_directory, 0);
97586         sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
97587       }
97588     }else{
97589 #ifndef SQLITE_OMIT_WSD
97590       if( zRight[0] ){
97591         int res;
97592         rc = sqlite3OsAccess(db->pVfs, zRight, SQLITE_ACCESS_READWRITE, &res);
97593         if( rc!=SQLITE_OK || res==0 ){
97594           sqlite3ErrorMsg(pParse, "not a writable directory");
97595           goto pragma_out;
97596         }
97597       }
97598       sqlite3_free(sqlite3_data_directory);
97599       if( zRight[0] ){
97600         sqlite3_data_directory = sqlite3_mprintf("%s", zRight);
97601       }else{
97602         sqlite3_data_directory = 0;
97603       }
97604 #endif /* SQLITE_OMIT_WSD */
97605     }
97606     break;
97607   }
97608 #endif
97609 
97610 #if SQLITE_ENABLE_LOCKING_STYLE
97611   /*
97612   **   PRAGMA [database.]lock_proxy_file
97613   **   PRAGMA [database.]lock_proxy_file = ":auto:"|"lock_file_path"
97614   **
97615   ** Return or set the value of the lock_proxy_file flag.  Changing
97616   ** the value sets a specific file to be used for database access locks.
97617   **
97618   */
97619   case PragTyp_LOCK_PROXY_FILE: {
97620     if( !zRight ){
97621       Pager *pPager = sqlite3BtreePager(pDb->pBt);
97622       char *proxy_file_path = NULL;
97623       sqlite3_file *pFile = sqlite3PagerFile(pPager);
97624       sqlite3OsFileControlHint(pFile, SQLITE_GET_LOCKPROXYFILE,
97625                            &proxy_file_path);
97626 
97627       if( proxy_file_path ){
97628         sqlite3VdbeSetNumCols(v, 1);
97629         sqlite3VdbeSetColName(v, 0, COLNAME_NAME,
97630                               "lock_proxy_file", SQLITE_STATIC);
97631         sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, proxy_file_path, 0);
97632         sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
97633       }
97634     }else{
97635       Pager *pPager = sqlite3BtreePager(pDb->pBt);
97636       sqlite3_file *pFile = sqlite3PagerFile(pPager);
97637       int res;
97638       if( zRight[0] ){
97639         res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE,
97640                                      zRight);
97641       } else {
97642         res=sqlite3OsFileControl(pFile, SQLITE_SET_LOCKPROXYFILE,
97643                                      NULL);
97644       }
97645       if( res!=SQLITE_OK ){
97646         sqlite3ErrorMsg(pParse, "failed to set lock proxy file");
97647         goto pragma_out;
97648       }
97649     }
97650     break;
97651   }
97652 #endif /* SQLITE_ENABLE_LOCKING_STYLE */
97653 
97654   /*
97655   **   PRAGMA [database.]synchronous
97656   **   PRAGMA [database.]synchronous=OFF|ON|NORMAL|FULL
97657   **
97658   ** Return or set the local value of the synchronous flag.  Changing
97659   ** the local value does not make changes to the disk file and the
97660   ** default value will be restored the next time the database is
97661   ** opened.
97662   */
97663   case PragTyp_SYNCHRONOUS: {
97664     if( !zRight ){
97665       returnSingleInt(pParse, "synchronous", pDb->safety_level-1);
97666     }else{
97667       if( !db->autoCommit ){
97668         sqlite3ErrorMsg(pParse,
97669             "Safety level may not be changed inside a transaction");
97670       }else{
97671         pDb->safety_level = getSafetyLevel(zRight,0,1)+1;
97672         setAllPagerFlags(db);
97673       }
97674     }
97675     break;
97676   }
97677 #endif /* SQLITE_OMIT_PAGER_PRAGMAS */
97678 
97679 #ifndef SQLITE_OMIT_FLAG_PRAGMAS
97680   case PragTyp_FLAG: {
97681     if( zRight==0 ){
97682       returnSingleInt(pParse, aPragmaNames[mid].zName,
97683                      (db->flags & aPragmaNames[mid].iArg)!=0 );
97684     }else{
97685       int mask = aPragmaNames[mid].iArg;    /* Mask of bits to set or clear. */
97686       if( db->autoCommit==0 ){
97687         /* Foreign key support may not be enabled or disabled while not
97688         ** in auto-commit mode.  */
97689         mask &= ~(SQLITE_ForeignKeys);
97690       }
97691 
97692       if( sqlite3GetBoolean(zRight, 0) ){
97693         db->flags |= mask;
97694       }else{
97695         db->flags &= ~mask;
97696         if( mask==SQLITE_DeferFKs ) db->nDeferredImmCons = 0;
97697       }
97698 
97699       /* Many of the flag-pragmas modify the code generated by the SQL
97700       ** compiler (eg. count_changes). So add an opcode to expire all
97701       ** compiled SQL statements after modifying a pragma value.
97702       */
97703       sqlite3VdbeAddOp2(v, OP_Expire, 0, 0);
97704       setAllPagerFlags(db);
97705     }
97706     break;
97707   }
97708 #endif /* SQLITE_OMIT_FLAG_PRAGMAS */
97709 
97710 #ifndef SQLITE_OMIT_SCHEMA_PRAGMAS
97711   /*
97712   **   PRAGMA table_info(<table>)
97713   **
97714   ** Return a single row for each column of the named table. The columns of
97715   ** the returned data set are:
97716   **
97717   ** cid:        Column id (numbered from left to right, starting at 0)
97718   ** name:       Column name
97719   ** type:       Column declaration type.
97720   ** notnull:    True if 'NOT NULL' is part of column declaration
97721   ** dflt_value: The default value for the column, if any.
97722   */
97723   case PragTyp_TABLE_INFO: if( zRight ){
97724     Table *pTab;
97725     pTab = sqlite3FindTable(db, zRight, zDb);
97726     if( pTab ){
97727       int i, k;
97728       int nHidden = 0;
97729       Column *pCol;
97730       Index *pPk = sqlite3PrimaryKeyIndex(pTab);
97731       sqlite3VdbeSetNumCols(v, 6);
97732       pParse->nMem = 6;
97733       sqlite3CodeVerifySchema(pParse, iDb);
97734       sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "cid", SQLITE_STATIC);
97735       sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
97736       sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "type", SQLITE_STATIC);
97737       sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "notnull", SQLITE_STATIC);
97738       sqlite3VdbeSetColName(v, 4, COLNAME_NAME, "dflt_value", SQLITE_STATIC);
97739       sqlite3VdbeSetColName(v, 5, COLNAME_NAME, "pk", SQLITE_STATIC);
97740       sqlite3ViewGetColumnNames(pParse, pTab);
97741       for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
97742         if( IsHiddenColumn(pCol) ){
97743           nHidden++;
97744           continue;
97745         }
97746         sqlite3VdbeAddOp2(v, OP_Integer, i-nHidden, 1);
97747         sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pCol->zName, 0);
97748         sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
97749            pCol->zType ? pCol->zType : "", 0);
97750         sqlite3VdbeAddOp2(v, OP_Integer, (pCol->notNull ? 1 : 0), 4);
97751         if( pCol->zDflt ){
97752           sqlite3VdbeAddOp4(v, OP_String8, 0, 5, 0, (char*)pCol->zDflt, 0);
97753         }else{
97754           sqlite3VdbeAddOp2(v, OP_Null, 0, 5);
97755         }
97756         if( (pCol->colFlags & COLFLAG_PRIMKEY)==0 ){
97757           k = 0;
97758         }else if( pPk==0 ){
97759           k = 1;
97760         }else{
97761           for(k=1; ALWAYS(k<=pTab->nCol) && pPk->aiColumn[k-1]!=i; k++){}
97762         }
97763         sqlite3VdbeAddOp2(v, OP_Integer, k, 6);
97764         sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 6);
97765       }
97766     }
97767   }
97768   break;
97769 
97770   case PragTyp_STATS: {
97771     Index *pIdx;
97772     HashElem *i;
97773     v = sqlite3GetVdbe(pParse);
97774     sqlite3VdbeSetNumCols(v, 4);
97775     pParse->nMem = 4;
97776     sqlite3CodeVerifySchema(pParse, iDb);
97777     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "table", SQLITE_STATIC);
97778     sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "index", SQLITE_STATIC);
97779     sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "width", SQLITE_STATIC);
97780     sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "height", SQLITE_STATIC);
97781     for(i=sqliteHashFirst(&pDb->pSchema->tblHash); i; i=sqliteHashNext(i)){
97782       Table *pTab = sqliteHashData(i);
97783       sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, pTab->zName, 0);
97784       sqlite3VdbeAddOp2(v, OP_Null, 0, 2);
97785       sqlite3VdbeAddOp2(v, OP_Integer,
97786                            (int)sqlite3LogEstToInt(pTab->szTabRow), 3);
97787       sqlite3VdbeAddOp2(v, OP_Integer, (int)pTab->nRowEst, 4);
97788       sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 4);
97789       for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
97790         sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pIdx->zName, 0);
97791         sqlite3VdbeAddOp2(v, OP_Integer,
97792                              (int)sqlite3LogEstToInt(pIdx->szIdxRow), 3);
97793         sqlite3VdbeAddOp2(v, OP_Integer, (int)pIdx->aiRowEst[0], 4);
97794         sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 4);
97795       }
97796     }
97797   }
97798   break;
97799 
97800   case PragTyp_INDEX_INFO: if( zRight ){
97801     Index *pIdx;
97802     Table *pTab;
97803     pIdx = sqlite3FindIndex(db, zRight, zDb);
97804     if( pIdx ){
97805       int i;
97806       pTab = pIdx->pTable;
97807       sqlite3VdbeSetNumCols(v, 3);
97808       pParse->nMem = 3;
97809       sqlite3CodeVerifySchema(pParse, iDb);
97810       sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seqno", SQLITE_STATIC);
97811       sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "cid", SQLITE_STATIC);
97812       sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "name", SQLITE_STATIC);
97813       for(i=0; i<pIdx->nKeyCol; i++){
97814         i16 cnum = pIdx->aiColumn[i];
97815         sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
97816         sqlite3VdbeAddOp2(v, OP_Integer, cnum, 2);
97817         assert( pTab->nCol>cnum );
97818         sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, pTab->aCol[cnum].zName, 0);
97819         sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
97820       }
97821     }
97822   }
97823   break;
97824 
97825   case PragTyp_INDEX_LIST: if( zRight ){
97826     Index *pIdx;
97827     Table *pTab;
97828     int i;
97829     pTab = sqlite3FindTable(db, zRight, zDb);
97830     if( pTab ){
97831       v = sqlite3GetVdbe(pParse);
97832       sqlite3VdbeSetNumCols(v, 3);
97833       pParse->nMem = 3;
97834       sqlite3CodeVerifySchema(pParse, iDb);
97835       sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC);
97836       sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
97837       sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "unique", SQLITE_STATIC);
97838       for(pIdx=pTab->pIndex, i=0; pIdx; pIdx=pIdx->pNext, i++){
97839         sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
97840         sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pIdx->zName, 0);
97841         sqlite3VdbeAddOp2(v, OP_Integer, pIdx->onError!=OE_None, 3);
97842         sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
97843       }
97844     }
97845   }
97846   break;
97847 
97848   case PragTyp_DATABASE_LIST: {
97849     int i;
97850     sqlite3VdbeSetNumCols(v, 3);
97851     pParse->nMem = 3;
97852     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC);
97853     sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
97854     sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "file", SQLITE_STATIC);
97855     for(i=0; i<db->nDb; i++){
97856       if( db->aDb[i].pBt==0 ) continue;
97857       assert( db->aDb[i].zName!=0 );
97858       sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
97859       sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, db->aDb[i].zName, 0);
97860       sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
97861            sqlite3BtreeGetFilename(db->aDb[i].pBt), 0);
97862       sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
97863     }
97864   }
97865   break;
97866 
97867   case PragTyp_COLLATION_LIST: {
97868     int i = 0;
97869     HashElem *p;
97870     sqlite3VdbeSetNumCols(v, 2);
97871     pParse->nMem = 2;
97872     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "seq", SQLITE_STATIC);
97873     sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "name", SQLITE_STATIC);
97874     for(p=sqliteHashFirst(&db->aCollSeq); p; p=sqliteHashNext(p)){
97875       CollSeq *pColl = (CollSeq *)sqliteHashData(p);
97876       sqlite3VdbeAddOp2(v, OP_Integer, i++, 1);
97877       sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pColl->zName, 0);
97878       sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2);
97879     }
97880   }
97881   break;
97882 #endif /* SQLITE_OMIT_SCHEMA_PRAGMAS */
97883 
97884 #ifndef SQLITE_OMIT_FOREIGN_KEY
97885   case PragTyp_FOREIGN_KEY_LIST: if( zRight ){
97886     FKey *pFK;
97887     Table *pTab;
97888     pTab = sqlite3FindTable(db, zRight, zDb);
97889     if( pTab ){
97890       v = sqlite3GetVdbe(pParse);
97891       pFK = pTab->pFKey;
97892       if( pFK ){
97893         int i = 0;
97894         sqlite3VdbeSetNumCols(v, 8);
97895         pParse->nMem = 8;
97896         sqlite3CodeVerifySchema(pParse, iDb);
97897         sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "id", SQLITE_STATIC);
97898         sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "seq", SQLITE_STATIC);
97899         sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "table", SQLITE_STATIC);
97900         sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "from", SQLITE_STATIC);
97901         sqlite3VdbeSetColName(v, 4, COLNAME_NAME, "to", SQLITE_STATIC);
97902         sqlite3VdbeSetColName(v, 5, COLNAME_NAME, "on_update", SQLITE_STATIC);
97903         sqlite3VdbeSetColName(v, 6, COLNAME_NAME, "on_delete", SQLITE_STATIC);
97904         sqlite3VdbeSetColName(v, 7, COLNAME_NAME, "match", SQLITE_STATIC);
97905         while(pFK){
97906           int j;
97907           for(j=0; j<pFK->nCol; j++){
97908             char *zCol = pFK->aCol[j].zCol;
97909             char *zOnDelete = (char *)actionName(pFK->aAction[0]);
97910             char *zOnUpdate = (char *)actionName(pFK->aAction[1]);
97911             sqlite3VdbeAddOp2(v, OP_Integer, i, 1);
97912             sqlite3VdbeAddOp2(v, OP_Integer, j, 2);
97913             sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, pFK->zTo, 0);
97914             sqlite3VdbeAddOp4(v, OP_String8, 0, 4, 0,
97915                               pTab->aCol[pFK->aCol[j].iFrom].zName, 0);
97916             sqlite3VdbeAddOp4(v, zCol ? OP_String8 : OP_Null, 0, 5, 0, zCol, 0);
97917             sqlite3VdbeAddOp4(v, OP_String8, 0, 6, 0, zOnUpdate, 0);
97918             sqlite3VdbeAddOp4(v, OP_String8, 0, 7, 0, zOnDelete, 0);
97919             sqlite3VdbeAddOp4(v, OP_String8, 0, 8, 0, "NONE", 0);
97920             sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 8);
97921           }
97922           ++i;
97923           pFK = pFK->pNextFrom;
97924         }
97925       }
97926     }
97927   }
97928   break;
97929 #endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
97930 
97931 #ifndef SQLITE_OMIT_FOREIGN_KEY
97932 #ifndef SQLITE_OMIT_TRIGGER
97933   case PragTyp_FOREIGN_KEY_CHECK: {
97934     FKey *pFK;             /* A foreign key constraint */
97935     Table *pTab;           /* Child table contain "REFERENCES" keyword */
97936     Table *pParent;        /* Parent table that child points to */
97937     Index *pIdx;           /* Index in the parent table */
97938     int i;                 /* Loop counter:  Foreign key number for pTab */
97939     int j;                 /* Loop counter:  Field of the foreign key */
97940     HashElem *k;           /* Loop counter:  Next table in schema */
97941     int x;                 /* result variable */
97942     int regResult;         /* 3 registers to hold a result row */
97943     int regKey;            /* Register to hold key for checking the FK */
97944     int regRow;            /* Registers to hold a row from pTab */
97945     int addrTop;           /* Top of a loop checking foreign keys */
97946     int addrOk;            /* Jump here if the key is OK */
97947     int *aiCols;           /* child to parent column mapping */
97948 
97949     regResult = pParse->nMem+1;
97950     pParse->nMem += 4;
97951     regKey = ++pParse->nMem;
97952     regRow = ++pParse->nMem;
97953     v = sqlite3GetVdbe(pParse);
97954     sqlite3VdbeSetNumCols(v, 4);
97955     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "table", SQLITE_STATIC);
97956     sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "rowid", SQLITE_STATIC);
97957     sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "parent", SQLITE_STATIC);
97958     sqlite3VdbeSetColName(v, 3, COLNAME_NAME, "fkid", SQLITE_STATIC);
97959     sqlite3CodeVerifySchema(pParse, iDb);
97960     k = sqliteHashFirst(&db->aDb[iDb].pSchema->tblHash);
97961     while( k ){
97962       if( zRight ){
97963         pTab = sqlite3LocateTable(pParse, 0, zRight, zDb);
97964         k = 0;
97965       }else{
97966         pTab = (Table*)sqliteHashData(k);
97967         k = sqliteHashNext(k);
97968       }
97969       if( pTab==0 || pTab->pFKey==0 ) continue;
97970       sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
97971       if( pTab->nCol+regRow>pParse->nMem ) pParse->nMem = pTab->nCol + regRow;
97972       sqlite3OpenTable(pParse, 0, iDb, pTab, OP_OpenRead);
97973       sqlite3VdbeAddOp4(v, OP_String8, 0, regResult, 0, pTab->zName,
97974                         P4_TRANSIENT);
97975       for(i=1, pFK=pTab->pFKey; pFK; i++, pFK=pFK->pNextFrom){
97976         pParent = sqlite3FindTable(db, pFK->zTo, zDb);
97977         if( pParent==0 ) continue;
97978         pIdx = 0;
97979         sqlite3TableLock(pParse, iDb, pParent->tnum, 0, pParent->zName);
97980         x = sqlite3FkLocateIndex(pParse, pParent, pFK, &pIdx, 0);
97981         if( x==0 ){
97982           if( pIdx==0 ){
97983             sqlite3OpenTable(pParse, i, iDb, pParent, OP_OpenRead);
97984           }else{
97985             sqlite3VdbeAddOp3(v, OP_OpenRead, i, pIdx->tnum, iDb);
97986             sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
97987           }
97988         }else{
97989           k = 0;
97990           break;
97991         }
97992       }
97993       assert( pParse->nErr>0 || pFK==0 );
97994       if( pFK ) break;
97995       if( pParse->nTab<i ) pParse->nTab = i;
97996       addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, 0);
97997       for(i=1, pFK=pTab->pFKey; pFK; i++, pFK=pFK->pNextFrom){
97998         pParent = sqlite3FindTable(db, pFK->zTo, zDb);
97999         pIdx = 0;
98000         aiCols = 0;
98001         if( pParent ){
98002           x = sqlite3FkLocateIndex(pParse, pParent, pFK, &pIdx, &aiCols);
98003           assert( x==0 );
98004         }
98005         addrOk = sqlite3VdbeMakeLabel(v);
98006         if( pParent && pIdx==0 ){
98007           int iKey = pFK->aCol[0].iFrom;
98008           assert( iKey>=0 && iKey<pTab->nCol );
98009           if( iKey!=pTab->iPKey ){
98010             sqlite3VdbeAddOp3(v, OP_Column, 0, iKey, regRow);
98011             sqlite3ColumnDefault(v, pTab, iKey, regRow);
98012             sqlite3VdbeAddOp2(v, OP_IsNull, regRow, addrOk);
98013             sqlite3VdbeAddOp2(v, OP_MustBeInt, regRow,
98014                sqlite3VdbeCurrentAddr(v)+3);
98015           }else{
98016             sqlite3VdbeAddOp2(v, OP_Rowid, 0, regRow);
98017           }
98018           sqlite3VdbeAddOp3(v, OP_NotExists, i, 0, regRow);
98019           sqlite3VdbeAddOp2(v, OP_Goto, 0, addrOk);
98020           sqlite3VdbeJumpHere(v, sqlite3VdbeCurrentAddr(v)-2);
98021         }else{
98022           for(j=0; j<pFK->nCol; j++){
98023             sqlite3ExprCodeGetColumnOfTable(v, pTab, 0,
98024                             aiCols ? aiCols[j] : pFK->aCol[j].iFrom, regRow+j);
98025             sqlite3VdbeAddOp2(v, OP_IsNull, regRow+j, addrOk);
98026           }
98027           if( pParent ){
98028             sqlite3VdbeAddOp3(v, OP_MakeRecord, regRow, pFK->nCol, regKey);
98029             sqlite3VdbeChangeP4(v, -1,
98030                      sqlite3IndexAffinityStr(v,pIdx), P4_TRANSIENT);
98031             sqlite3VdbeAddOp4Int(v, OP_Found, i, addrOk, regKey, 0);
98032           }
98033         }
98034         sqlite3VdbeAddOp2(v, OP_Rowid, 0, regResult+1);
98035         sqlite3VdbeAddOp4(v, OP_String8, 0, regResult+2, 0,
98036                           pFK->zTo, P4_TRANSIENT);
98037         sqlite3VdbeAddOp2(v, OP_Integer, i-1, regResult+3);
98038         sqlite3VdbeAddOp2(v, OP_ResultRow, regResult, 4);
98039         sqlite3VdbeResolveLabel(v, addrOk);
98040         sqlite3DbFree(db, aiCols);
98041       }
98042       sqlite3VdbeAddOp2(v, OP_Next, 0, addrTop+1);
98043       sqlite3VdbeJumpHere(v, addrTop);
98044     }
98045   }
98046   break;
98047 #endif /* !defined(SQLITE_OMIT_TRIGGER) */
98048 #endif /* !defined(SQLITE_OMIT_FOREIGN_KEY) */
98049 
98050 #ifndef NDEBUG
98051   case PragTyp_PARSER_TRACE: {
98052     if( zRight ){
98053       if( sqlite3GetBoolean(zRight, 0) ){
98054         sqlite3ParserTrace(stderr, "parser: ");
98055       }else{
98056         sqlite3ParserTrace(0, 0);
98057       }
98058     }
98059   }
98060   break;
98061 #endif
98062 
98063   /* Reinstall the LIKE and GLOB functions.  The variant of LIKE
98064   ** used will be case sensitive or not depending on the RHS.
98065   */
98066   case PragTyp_CASE_SENSITIVE_LIKE: {
98067     if( zRight ){
98068       sqlite3RegisterLikeFunctions(db, sqlite3GetBoolean(zRight, 0));
98069     }
98070   }
98071   break;
98072 
98073 #ifndef SQLITE_INTEGRITY_CHECK_ERROR_MAX
98074 # define SQLITE_INTEGRITY_CHECK_ERROR_MAX 100
98075 #endif
98076 
98077 #ifndef SQLITE_OMIT_INTEGRITY_CHECK
98078   /* Pragma "quick_check" is reduced version of
98079   ** integrity_check designed to detect most database corruption
98080   ** without most of the overhead of a full integrity-check.
98081   */
98082   case PragTyp_INTEGRITY_CHECK: {
98083     int i, j, addr, mxErr;
98084 
98085     /* Code that appears at the end of the integrity check.  If no error
98086     ** messages have been generated, output OK.  Otherwise output the
98087     ** error message
98088     */
98089     static const VdbeOpList endCode[] = {
98090       { OP_AddImm,      1, 0,        0},    /* 0 */
98091       { OP_IfNeg,       1, 0,        0},    /* 1 */
98092       { OP_String8,     0, 3,        0},    /* 2 */
98093       { OP_ResultRow,   3, 1,        0},
98094     };
98095 
98096     int isQuick = (sqlite3Tolower(zLeft[0])=='q');
98097 
98098     /* If the PRAGMA command was of the form "PRAGMA <db>.integrity_check",
98099     ** then iDb is set to the index of the database identified by <db>.
98100     ** In this case, the integrity of database iDb only is verified by
98101     ** the VDBE created below.
98102     **
98103     ** Otherwise, if the command was simply "PRAGMA integrity_check" (or
98104     ** "PRAGMA quick_check"), then iDb is set to 0. In this case, set iDb
98105     ** to -1 here, to indicate that the VDBE should verify the integrity
98106     ** of all attached databases.  */
98107     assert( iDb>=0 );
98108     assert( iDb==0 || pId2->z );
98109     if( pId2->z==0 ) iDb = -1;
98110 
98111     /* Initialize the VDBE program */
98112     pParse->nMem = 6;
98113     sqlite3VdbeSetNumCols(v, 1);
98114     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "integrity_check", SQLITE_STATIC);
98115 
98116     /* Set the maximum error count */
98117     mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
98118     if( zRight ){
98119       sqlite3GetInt32(zRight, &mxErr);
98120       if( mxErr<=0 ){
98121         mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
98122       }
98123     }
98124     sqlite3VdbeAddOp2(v, OP_Integer, mxErr, 1);  /* reg[1] holds errors left */
98125 
98126     /* Do an integrity check on each database file */
98127     for(i=0; i<db->nDb; i++){
98128       HashElem *x;
98129       Hash *pTbls;
98130       int cnt = 0;
98131 
98132       if( OMIT_TEMPDB && i==1 ) continue;
98133       if( iDb>=0 && i!=iDb ) continue;
98134 
98135       sqlite3CodeVerifySchema(pParse, i);
98136       addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1); /* Halt if out of errors */
98137       sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
98138       sqlite3VdbeJumpHere(v, addr);
98139 
98140       /* Do an integrity check of the B-Tree
98141       **
98142       ** Begin by filling registers 2, 3, ... with the root pages numbers
98143       ** for all tables and indices in the database.
98144       */
98145       assert( sqlite3SchemaMutexHeld(db, i, 0) );
98146       pTbls = &db->aDb[i].pSchema->tblHash;
98147       for(x=sqliteHashFirst(pTbls); x; x=sqliteHashNext(x)){
98148         Table *pTab = sqliteHashData(x);
98149         Index *pIdx;
98150         if( HasRowid(pTab) ){
98151           sqlite3VdbeAddOp2(v, OP_Integer, pTab->tnum, 2+cnt);
98152           VdbeComment((v, "%s", pTab->zName));
98153           cnt++;
98154         }
98155         for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
98156           sqlite3VdbeAddOp2(v, OP_Integer, pIdx->tnum, 2+cnt);
98157           VdbeComment((v, "%s", pIdx->zName));
98158           cnt++;
98159         }
98160       }
98161 
98162       /* Make sure sufficient number of registers have been allocated */
98163       pParse->nMem = MAX( pParse->nMem, cnt+8 );
98164 
98165       /* Do the b-tree integrity checks */
98166       sqlite3VdbeAddOp3(v, OP_IntegrityCk, 2, cnt, 1);
98167       sqlite3VdbeChangeP5(v, (u8)i);
98168       addr = sqlite3VdbeAddOp1(v, OP_IsNull, 2);
98169       sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0,
98170          sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zName),
98171          P4_DYNAMIC);
98172       sqlite3VdbeAddOp2(v, OP_Move, 2, 4);
98173       sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 2);
98174       sqlite3VdbeAddOp2(v, OP_ResultRow, 2, 1);
98175       sqlite3VdbeJumpHere(v, addr);
98176 
98177       /* Make sure all the indices are constructed correctly.
98178       */
98179       for(x=sqliteHashFirst(pTbls); x && !isQuick; x=sqliteHashNext(x)){
98180         Table *pTab = sqliteHashData(x);
98181         Index *pIdx, *pPk;
98182         Index *pPrior = 0;
98183         int loopTop;
98184         int iDataCur, iIdxCur;
98185         int r1 = -1;
98186 
98187         if( pTab->pIndex==0 ) continue;
98188         pPk = HasRowid(pTab) ? 0 : sqlite3PrimaryKeyIndex(pTab);
98189         addr = sqlite3VdbeAddOp1(v, OP_IfPos, 1);  /* Stop if out of errors */
98190         sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
98191         sqlite3VdbeJumpHere(v, addr);
98192         sqlite3ExprCacheClear(pParse);
98193         sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenRead,
98194                                    1, 0, &iDataCur, &iIdxCur);
98195         sqlite3VdbeAddOp2(v, OP_Integer, 0, 7);
98196         for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
98197           sqlite3VdbeAddOp2(v, OP_Integer, 0, 8+j); /* index entries counter */
98198         }
98199         pParse->nMem = MAX(pParse->nMem, 8+j);
98200         sqlite3VdbeAddOp2(v, OP_Rewind, iDataCur, 0);
98201         loopTop = sqlite3VdbeAddOp2(v, OP_AddImm, 7, 1);
98202         for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
98203           int jmp2, jmp3, jmp4;
98204           if( pPk==pIdx ) continue;
98205           r1 = sqlite3GenerateIndexKey(pParse, pIdx, iDataCur, 0, 0, &jmp3,
98206                                        pPrior, r1);
98207           pPrior = pIdx;
98208           sqlite3VdbeAddOp2(v, OP_AddImm, 8+j, 1);  /* increment entry count */
98209           jmp2 = sqlite3VdbeAddOp4Int(v, OP_Found, iIdxCur+j, 0, r1,
98210                                       pIdx->nColumn);
98211           sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1); /* Decrement error limit */
98212           sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, "row ", P4_STATIC);
98213           sqlite3VdbeAddOp3(v, OP_Concat, 7, 3, 3);
98214           sqlite3VdbeAddOp4(v, OP_String8, 0, 4, 0, " missing from index ",
98215                             P4_STATIC);
98216           sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 3);
98217           sqlite3VdbeAddOp4(v, OP_String8, 0, 4, 0, pIdx->zName, P4_TRANSIENT);
98218           sqlite3VdbeAddOp3(v, OP_Concat, 4, 3, 3);
98219           sqlite3VdbeAddOp2(v, OP_ResultRow, 3, 1);
98220           jmp4 = sqlite3VdbeAddOp1(v, OP_IfPos, 1);
98221           sqlite3VdbeAddOp0(v, OP_Halt);
98222           sqlite3VdbeJumpHere(v, jmp4);
98223           sqlite3VdbeJumpHere(v, jmp2);
98224           sqlite3VdbeResolveLabel(v, jmp3);
98225         }
98226         sqlite3VdbeAddOp2(v, OP_Next, iDataCur, loopTop);
98227         sqlite3VdbeJumpHere(v, loopTop-1);
98228 #ifndef SQLITE_OMIT_BTREECOUNT
98229         sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0,
98230                      "wrong # of entries in index ", P4_STATIC);
98231         for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
98232           if( pPk==pIdx ) continue;
98233           addr = sqlite3VdbeCurrentAddr(v);
98234           sqlite3VdbeAddOp2(v, OP_IfPos, 1, addr+2);
98235           sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
98236           sqlite3VdbeAddOp2(v, OP_Count, iIdxCur+j, 3);
98237           sqlite3VdbeAddOp3(v, OP_Eq, 8+j, addr+8, 3);
98238           sqlite3VdbeAddOp2(v, OP_AddImm, 1, -1);
98239           sqlite3VdbeAddOp4(v, OP_String8, 0, 3, 0, pIdx->zName, P4_TRANSIENT);
98240           sqlite3VdbeAddOp3(v, OP_Concat, 3, 2, 7);
98241           sqlite3VdbeAddOp2(v, OP_ResultRow, 7, 1);
98242         }
98243 #endif /* SQLITE_OMIT_BTREECOUNT */
98244       }
98245     }
98246     addr = sqlite3VdbeAddOpList(v, ArraySize(endCode), endCode);
98247     sqlite3VdbeChangeP2(v, addr, -mxErr);
98248     sqlite3VdbeJumpHere(v, addr+1);
98249     sqlite3VdbeChangeP4(v, addr+2, "ok", P4_STATIC);
98250   }
98251   break;
98252 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */
98253 
98254 #ifndef SQLITE_OMIT_UTF16
98255   /*
98256   **   PRAGMA encoding
98257   **   PRAGMA encoding = "utf-8"|"utf-16"|"utf-16le"|"utf-16be"
98258   **
98259   ** In its first form, this pragma returns the encoding of the main
98260   ** database. If the database is not initialized, it is initialized now.
98261   **
98262   ** The second form of this pragma is a no-op if the main database file
98263   ** has not already been initialized. In this case it sets the default
98264   ** encoding that will be used for the main database file if a new file
98265   ** is created. If an existing main database file is opened, then the
98266   ** default text encoding for the existing database is used.
98267   **
98268   ** In all cases new databases created using the ATTACH command are
98269   ** created to use the same default text encoding as the main database. If
98270   ** the main database has not been initialized and/or created when ATTACH
98271   ** is executed, this is done before the ATTACH operation.
98272   **
98273   ** In the second form this pragma sets the text encoding to be used in
98274   ** new database files created using this database handle. It is only
98275   ** useful if invoked immediately after the main database i
98276   */
98277   case PragTyp_ENCODING: {
98278     static const struct EncName {
98279       char *zName;
98280       u8 enc;
98281     } encnames[] = {
98282       { "UTF8",     SQLITE_UTF8        },
98283       { "UTF-8",    SQLITE_UTF8        },  /* Must be element [1] */
98284       { "UTF-16le", SQLITE_UTF16LE     },  /* Must be element [2] */
98285       { "UTF-16be", SQLITE_UTF16BE     },  /* Must be element [3] */
98286       { "UTF16le",  SQLITE_UTF16LE     },
98287       { "UTF16be",  SQLITE_UTF16BE     },
98288       { "UTF-16",   0                  }, /* SQLITE_UTF16NATIVE */
98289       { "UTF16",    0                  }, /* SQLITE_UTF16NATIVE */
98290       { 0, 0 }
98291     };
98292     const struct EncName *pEnc;
98293     if( !zRight ){    /* "PRAGMA encoding" */
98294       if( sqlite3ReadSchema(pParse) ) goto pragma_out;
98295       sqlite3VdbeSetNumCols(v, 1);
98296       sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "encoding", SQLITE_STATIC);
98297       sqlite3VdbeAddOp2(v, OP_String8, 0, 1);
98298       assert( encnames[SQLITE_UTF8].enc==SQLITE_UTF8 );
98299       assert( encnames[SQLITE_UTF16LE].enc==SQLITE_UTF16LE );
98300       assert( encnames[SQLITE_UTF16BE].enc==SQLITE_UTF16BE );
98301       sqlite3VdbeChangeP4(v, -1, encnames[ENC(pParse->db)].zName, P4_STATIC);
98302       sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
98303     }else{                        /* "PRAGMA encoding = XXX" */
98304       /* Only change the value of sqlite.enc if the database handle is not
98305       ** initialized. If the main database exists, the new sqlite.enc value
98306       ** will be overwritten when the schema is next loaded. If it does not
98307       ** already exists, it will be created to use the new encoding value.
98308       */
98309       if(
98310         !(DbHasProperty(db, 0, DB_SchemaLoaded)) ||
98311         DbHasProperty(db, 0, DB_Empty)
98312       ){
98313         for(pEnc=&encnames[0]; pEnc->zName; pEnc++){
98314           if( 0==sqlite3StrICmp(zRight, pEnc->zName) ){
98315             ENC(pParse->db) = pEnc->enc ? pEnc->enc : SQLITE_UTF16NATIVE;
98316             break;
98317           }
98318         }
98319         if( !pEnc->zName ){
98320           sqlite3ErrorMsg(pParse, "unsupported encoding: %s", zRight);
98321         }
98322       }
98323     }
98324   }
98325   break;
98326 #endif /* SQLITE_OMIT_UTF16 */
98327 
98328 #ifndef SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS
98329   /*
98330   **   PRAGMA [database.]schema_version
98331   **   PRAGMA [database.]schema_version = <integer>
98332   **
98333   **   PRAGMA [database.]user_version
98334   **   PRAGMA [database.]user_version = <integer>
98335   **
98336   **   PRAGMA [database.]freelist_count = <integer>
98337   **
98338   **   PRAGMA [database.]application_id
98339   **   PRAGMA [database.]application_id = <integer>
98340   **
98341   ** The pragma's schema_version and user_version are used to set or get
98342   ** the value of the schema-version and user-version, respectively. Both
98343   ** the schema-version and the user-version are 32-bit signed integers
98344   ** stored in the database header.
98345   **
98346   ** The schema-cookie is usually only manipulated internally by SQLite. It
98347   ** is incremented by SQLite whenever the database schema is modified (by
98348   ** creating or dropping a table or index). The schema version is used by
98349   ** SQLite each time a query is executed to ensure that the internal cache
98350   ** of the schema used when compiling the SQL query matches the schema of
98351   ** the database against which the compiled query is actually executed.
98352   ** Subverting this mechanism by using "PRAGMA schema_version" to modify
98353   ** the schema-version is potentially dangerous and may lead to program
98354   ** crashes or database corruption. Use with caution!
98355   **
98356   ** The user-version is not used internally by SQLite. It may be used by
98357   ** applications for any purpose.
98358   */
98359   case PragTyp_HEADER_VALUE: {
98360     int iCookie;   /* Cookie index. 1 for schema-cookie, 6 for user-cookie. */
98361     sqlite3VdbeUsesBtree(v, iDb);
98362     switch( zLeft[0] ){
98363       case 'a': case 'A':
98364         iCookie = BTREE_APPLICATION_ID;
98365         break;
98366       case 'f': case 'F':
98367         iCookie = BTREE_FREE_PAGE_COUNT;
98368         break;
98369       case 's': case 'S':
98370         iCookie = BTREE_SCHEMA_VERSION;
98371         break;
98372       default:
98373         iCookie = BTREE_USER_VERSION;
98374         break;
98375     }
98376 
98377     if( zRight && iCookie!=BTREE_FREE_PAGE_COUNT ){
98378       /* Write the specified cookie value */
98379       static const VdbeOpList setCookie[] = {
98380         { OP_Transaction,    0,  1,  0},    /* 0 */
98381         { OP_Integer,        0,  1,  0},    /* 1 */
98382         { OP_SetCookie,      0,  0,  1},    /* 2 */
98383       };
98384       int addr = sqlite3VdbeAddOpList(v, ArraySize(setCookie), setCookie);
98385       sqlite3VdbeChangeP1(v, addr, iDb);
98386       sqlite3VdbeChangeP1(v, addr+1, sqlite3Atoi(zRight));
98387       sqlite3VdbeChangeP1(v, addr+2, iDb);
98388       sqlite3VdbeChangeP2(v, addr+2, iCookie);
98389     }else{
98390       /* Read the specified cookie value */
98391       static const VdbeOpList readCookie[] = {
98392         { OP_Transaction,     0,  0,  0},    /* 0 */
98393         { OP_ReadCookie,      0,  1,  0},    /* 1 */
98394         { OP_ResultRow,       1,  1,  0}
98395       };
98396       int addr = sqlite3VdbeAddOpList(v, ArraySize(readCookie), readCookie);
98397       sqlite3VdbeChangeP1(v, addr, iDb);
98398       sqlite3VdbeChangeP1(v, addr+1, iDb);
98399       sqlite3VdbeChangeP3(v, addr+1, iCookie);
98400       sqlite3VdbeSetNumCols(v, 1);
98401       sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLeft, SQLITE_TRANSIENT);
98402     }
98403   }
98404   break;
98405 #endif /* SQLITE_OMIT_SCHEMA_VERSION_PRAGMAS */
98406 
98407 #ifndef SQLITE_OMIT_COMPILEOPTION_DIAGS
98408   /*
98409   **   PRAGMA compile_options
98410   **
98411   ** Return the names of all compile-time options used in this build,
98412   ** one option per row.
98413   */
98414   case PragTyp_COMPILE_OPTIONS: {
98415     int i = 0;
98416     const char *zOpt;
98417     sqlite3VdbeSetNumCols(v, 1);
98418     pParse->nMem = 1;
98419     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "compile_option", SQLITE_STATIC);
98420     while( (zOpt = sqlite3_compileoption_get(i++))!=0 ){
98421       sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, zOpt, 0);
98422       sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
98423     }
98424   }
98425   break;
98426 #endif /* SQLITE_OMIT_COMPILEOPTION_DIAGS */
98427 
98428 #ifndef SQLITE_OMIT_WAL
98429   /*
98430   **   PRAGMA [database.]wal_checkpoint = passive|full|restart
98431   **
98432   ** Checkpoint the database.
98433   */
98434   case PragTyp_WAL_CHECKPOINT: {
98435     int iBt = (pId2->z?iDb:SQLITE_MAX_ATTACHED);
98436     int eMode = SQLITE_CHECKPOINT_PASSIVE;
98437     if( zRight ){
98438       if( sqlite3StrICmp(zRight, "full")==0 ){
98439         eMode = SQLITE_CHECKPOINT_FULL;
98440       }else if( sqlite3StrICmp(zRight, "restart")==0 ){
98441         eMode = SQLITE_CHECKPOINT_RESTART;
98442       }
98443     }
98444     sqlite3VdbeSetNumCols(v, 3);
98445     pParse->nMem = 3;
98446     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "busy", SQLITE_STATIC);
98447     sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "log", SQLITE_STATIC);
98448     sqlite3VdbeSetColName(v, 2, COLNAME_NAME, "checkpointed", SQLITE_STATIC);
98449 
98450     sqlite3VdbeAddOp3(v, OP_Checkpoint, iBt, eMode, 1);
98451     sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 3);
98452   }
98453   break;
98454 
98455   /*
98456   **   PRAGMA wal_autocheckpoint
98457   **   PRAGMA wal_autocheckpoint = N
98458   **
98459   ** Configure a database connection to automatically checkpoint a database
98460   ** after accumulating N frames in the log. Or query for the current value
98461   ** of N.
98462   */
98463   case PragTyp_WAL_AUTOCHECKPOINT: {
98464     if( zRight ){
98465       sqlite3_wal_autocheckpoint(db, sqlite3Atoi(zRight));
98466     }
98467     returnSingleInt(pParse, "wal_autocheckpoint",
98468        db->xWalCallback==sqlite3WalDefaultHook ?
98469            SQLITE_PTR_TO_INT(db->pWalArg) : 0);
98470   }
98471   break;
98472 #endif
98473 
98474   /*
98475   **  PRAGMA shrink_memory
98476   **
98477   ** This pragma attempts to free as much memory as possible from the
98478   ** current database connection.
98479   */
98480   case PragTyp_SHRINK_MEMORY: {
98481     sqlite3_db_release_memory(db);
98482     break;
98483   }
98484 
98485   /*
98486   **   PRAGMA busy_timeout
98487   **   PRAGMA busy_timeout = N
98488   **
98489   ** Call sqlite3_busy_timeout(db, N).  Return the current timeout value
98490   ** if one is set.  If no busy handler or a different busy handler is set
98491   ** then 0 is returned.  Setting the busy_timeout to 0 or negative
98492   ** disables the timeout.
98493   */
98494   /*case PragTyp_BUSY_TIMEOUT*/ default: {
98495     assert( aPragmaNames[mid].ePragTyp==PragTyp_BUSY_TIMEOUT );
98496     if( zRight ){
98497       sqlite3_busy_timeout(db, sqlite3Atoi(zRight));
98498     }
98499     returnSingleInt(pParse, "timeout",  db->busyTimeout);
98500     break;
98501   }
98502 
98503   /*
98504   **   PRAGMA soft_heap_limit
98505   **   PRAGMA soft_heap_limit = N
98506   **
98507   ** Call sqlite3_soft_heap_limit64(N).  Return the result.  If N is omitted,
98508   ** use -1.
98509   */
98510   case PragTyp_SOFT_HEAP_LIMIT: {
98511     sqlite3_int64 N;
98512     if( zRight && sqlite3Atoi64(zRight, &N, 1000000, SQLITE_UTF8)==SQLITE_OK ){
98513       sqlite3_soft_heap_limit64(N);
98514     }
98515     returnSingleInt(pParse, "soft_heap_limit",  sqlite3_soft_heap_limit64(-1));
98516     break;
98517   }
98518 
98519 #if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
98520   /*
98521   ** Report the current state of file logs for all databases
98522   */
98523   case PragTyp_LOCK_STATUS: {
98524     static const char *const azLockName[] = {
98525       "unlocked", "shared", "reserved", "pending", "exclusive"
98526     };
98527     int i;
98528     sqlite3VdbeSetNumCols(v, 2);
98529     pParse->nMem = 2;
98530     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "database", SQLITE_STATIC);
98531     sqlite3VdbeSetColName(v, 1, COLNAME_NAME, "status", SQLITE_STATIC);
98532     for(i=0; i<db->nDb; i++){
98533       Btree *pBt;
98534       const char *zState = "unknown";
98535       int j;
98536       if( db->aDb[i].zName==0 ) continue;
98537       sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, db->aDb[i].zName, P4_STATIC);
98538       pBt = db->aDb[i].pBt;
98539       if( pBt==0 || sqlite3BtreePager(pBt)==0 ){
98540         zState = "closed";
98541       }else if( sqlite3_file_control(db, i ? db->aDb[i].zName : 0,
98542                                      SQLITE_FCNTL_LOCKSTATE, &j)==SQLITE_OK ){
98543          zState = azLockName[j];
98544       }
98545       sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, zState, P4_STATIC);
98546       sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 2);
98547     }
98548     break;
98549   }
98550 #endif
98551 
98552 #ifdef SQLITE_HAS_CODEC
98553   case PragTyp_KEY: {
98554     if( zRight ) sqlite3_key_v2(db, zDb, zRight, sqlite3Strlen30(zRight));
98555     break;
98556   }
98557   case PragTyp_REKEY: {
98558     if( zRight ) sqlite3_rekey_v2(db, zDb, zRight, sqlite3Strlen30(zRight));
98559     break;
98560   }
98561   case PragTyp_HEXKEY: {
98562     if( zRight ){
98563       u8 iByte;
98564       int i;
98565       char zKey[40];
98566       for(i=0, iByte=0; i<sizeof(zKey)*2 && sqlite3Isxdigit(zRight[i]); i++){
98567         iByte = (iByte<<4) + sqlite3HexToInt(zRight[i]);
98568         if( (i&1)!=0 ) zKey[i/2] = iByte;
98569       }
98570       if( (zLeft[3] & 0xf)==0xb ){
98571         sqlite3_key_v2(db, zDb, zKey, i/2);
98572       }else{
98573         sqlite3_rekey_v2(db, zDb, zKey, i/2);
98574       }
98575     }
98576     break;
98577   }
98578 #endif
98579 #if defined(SQLITE_HAS_CODEC) || defined(SQLITE_ENABLE_CEROD)
98580   case PragTyp_ACTIVATE_EXTENSIONS: if( zRight ){
98581 #ifdef SQLITE_HAS_CODEC
98582     if( sqlite3StrNICmp(zRight, "see-", 4)==0 ){
98583       sqlite3_activate_see(&zRight[4]);
98584     }
98585 #endif
98586 #ifdef SQLITE_ENABLE_CEROD
98587     if( sqlite3StrNICmp(zRight, "cerod-", 6)==0 ){
98588       sqlite3_activate_cerod(&zRight[6]);
98589     }
98590 #endif
98591   }
98592   break;
98593 #endif
98594 
98595   } /* End of the PRAGMA switch */
98596 
98597 pragma_out:
98598   sqlite3DbFree(db, zLeft);
98599   sqlite3DbFree(db, zRight);
98600 }
98601 
98602 #endif /* SQLITE_OMIT_PRAGMA */
98603 
98604 /************** End of pragma.c **********************************************/
98605 /************** Begin file prepare.c *****************************************/
98606 /*
98607 ** 2005 May 25
98608 **
98609 ** The author disclaims copyright to this source code.  In place of
98610 ** a legal notice, here is a blessing:
98611 **
98612 **    May you do good and not evil.
98613 **    May you find forgiveness for yourself and forgive others.
98614 **    May you share freely, never taking more than you give.
98615 **
98616 *************************************************************************
98617 ** This file contains the implementation of the sqlite3_prepare()
98618 ** interface, and routines that contribute to loading the database schema
98619 ** from disk.
98620 */
98621 
98622 /*
98623 ** Fill the InitData structure with an error message that indicates
98624 ** that the database is corrupt.
98625 */
98626 static void corruptSchema(
98627   InitData *pData,     /* Initialization context */
98628   const char *zObj,    /* Object being parsed at the point of error */
98629   const char *zExtra   /* Error information */
98630 ){
98631   sqlite3 *db = pData->db;
98632   if( !db->mallocFailed && (db->flags & SQLITE_RecoveryMode)==0 ){
98633     if( zObj==0 ) zObj = "?";
98634     sqlite3SetString(pData->pzErrMsg, db,
98635       "malformed database schema (%s)", zObj);
98636     if( zExtra ){
98637       *pData->pzErrMsg = sqlite3MAppendf(db, *pData->pzErrMsg,
98638                                  "%s - %s", *pData->pzErrMsg, zExtra);
98639     }
98640   }
98641   pData->rc = db->mallocFailed ? SQLITE_NOMEM : SQLITE_CORRUPT_BKPT;
98642 }
98643 
98644 /*
98645 ** This is the callback routine for the code that initializes the
98646 ** database.  See sqlite3Init() below for additional information.
98647 ** This routine is also called from the OP_ParseSchema opcode of the VDBE.
98648 **
98649 ** Each callback contains the following information:
98650 **
98651 **     argv[0] = name of thing being created
98652 **     argv[1] = root page number for table or index. 0 for trigger or view.
98653 **     argv[2] = SQL text for the CREATE statement.
98654 **
98655 */
98656 SQLITE_PRIVATE int sqlite3InitCallback(void *pInit, int argc, char **argv, char **NotUsed){
98657   InitData *pData = (InitData*)pInit;
98658   sqlite3 *db = pData->db;
98659   int iDb = pData->iDb;
98660 
98661   assert( argc==3 );
98662   UNUSED_PARAMETER2(NotUsed, argc);
98663   assert( sqlite3_mutex_held(db->mutex) );
98664   DbClearProperty(db, iDb, DB_Empty);
98665   if( db->mallocFailed ){
98666     corruptSchema(pData, argv[0], 0);
98667     return 1;
98668   }
98669 
98670   assert( iDb>=0 && iDb<db->nDb );
98671   if( argv==0 ) return 0;   /* Might happen if EMPTY_RESULT_CALLBACKS are on */
98672   if( argv[1]==0 ){
98673     corruptSchema(pData, argv[0], 0);
98674   }else if( argv[2] && argv[2][0] ){
98675     /* Call the parser to process a CREATE TABLE, INDEX or VIEW.
98676     ** But because db->init.busy is set to 1, no VDBE code is generated
98677     ** or executed.  All the parser does is build the internal data
98678     ** structures that describe the table, index, or view.
98679     */
98680     int rc;
98681     sqlite3_stmt *pStmt;
98682     TESTONLY(int rcp);            /* Return code from sqlite3_prepare() */
98683 
98684     assert( db->init.busy );
98685     db->init.iDb = iDb;
98686     db->init.newTnum = sqlite3Atoi(argv[1]);
98687     db->init.orphanTrigger = 0;
98688     TESTONLY(rcp = ) sqlite3_prepare(db, argv[2], -1, &pStmt, 0);
98689     rc = db->errCode;
98690     assert( (rc&0xFF)==(rcp&0xFF) );
98691     db->init.iDb = 0;
98692     if( SQLITE_OK!=rc ){
98693       if( db->init.orphanTrigger ){
98694         assert( iDb==1 );
98695       }else{
98696         pData->rc = rc;
98697         if( rc==SQLITE_NOMEM ){
98698           db->mallocFailed = 1;
98699         }else if( rc!=SQLITE_INTERRUPT && (rc&0xFF)!=SQLITE_LOCKED ){
98700           corruptSchema(pData, argv[0], sqlite3_errmsg(db));
98701         }
98702       }
98703     }
98704     sqlite3_finalize(pStmt);
98705   }else if( argv[0]==0 ){
98706     corruptSchema(pData, 0, 0);
98707   }else{
98708     /* If the SQL column is blank it means this is an index that
98709     ** was created to be the PRIMARY KEY or to fulfill a UNIQUE
98710     ** constraint for a CREATE TABLE.  The index should have already
98711     ** been created when we processed the CREATE TABLE.  All we have
98712     ** to do here is record the root page number for that index.
98713     */
98714     Index *pIndex;
98715     pIndex = sqlite3FindIndex(db, argv[0], db->aDb[iDb].zName);
98716     if( pIndex==0 ){
98717       /* This can occur if there exists an index on a TEMP table which
98718       ** has the same name as another index on a permanent index.  Since
98719       ** the permanent table is hidden by the TEMP table, we can also
98720       ** safely ignore the index on the permanent table.
98721       */
98722       /* Do Nothing */;
98723     }else if( sqlite3GetInt32(argv[1], &pIndex->tnum)==0 ){
98724       corruptSchema(pData, argv[0], "invalid rootpage");
98725     }
98726   }
98727   return 0;
98728 }
98729 
98730 /*
98731 ** Attempt to read the database schema and initialize internal
98732 ** data structures for a single database file.  The index of the
98733 ** database file is given by iDb.  iDb==0 is used for the main
98734 ** database.  iDb==1 should never be used.  iDb>=2 is used for
98735 ** auxiliary databases.  Return one of the SQLITE_ error codes to
98736 ** indicate success or failure.
98737 */
98738 static int sqlite3InitOne(sqlite3 *db, int iDb, char **pzErrMsg){
98739   int rc;
98740   int i;
98741 #ifndef SQLITE_OMIT_DEPRECATED
98742   int size;
98743 #endif
98744   Table *pTab;
98745   Db *pDb;
98746   char const *azArg[4];
98747   int meta[5];
98748   InitData initData;
98749   char const *zMasterSchema;
98750   char const *zMasterName;
98751   int openedTransaction = 0;
98752 
98753   /*
98754   ** The master database table has a structure like this
98755   */
98756   static const char master_schema[] =
98757      "CREATE TABLE sqlite_master(\n"
98758      "  type text,\n"
98759      "  name text,\n"
98760      "  tbl_name text,\n"
98761      "  rootpage integer,\n"
98762      "  sql text\n"
98763      ")"
98764   ;
98765 #ifndef SQLITE_OMIT_TEMPDB
98766   static const char temp_master_schema[] =
98767      "CREATE TEMP TABLE sqlite_temp_master(\n"
98768      "  type text,\n"
98769      "  name text,\n"
98770      "  tbl_name text,\n"
98771      "  rootpage integer,\n"
98772      "  sql text\n"
98773      ")"
98774   ;
98775 #else
98776   #define temp_master_schema 0
98777 #endif
98778 
98779   assert( iDb>=0 && iDb<db->nDb );
98780   assert( db->aDb[iDb].pSchema );
98781   assert( sqlite3_mutex_held(db->mutex) );
98782   assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );
98783 
98784   /* zMasterSchema and zInitScript are set to point at the master schema
98785   ** and initialisation script appropriate for the database being
98786   ** initialized. zMasterName is the name of the master table.
98787   */
98788   if( !OMIT_TEMPDB && iDb==1 ){
98789     zMasterSchema = temp_master_schema;
98790   }else{
98791     zMasterSchema = master_schema;
98792   }
98793   zMasterName = SCHEMA_TABLE(iDb);
98794 
98795   /* Construct the schema tables.  */
98796   azArg[0] = zMasterName;
98797   azArg[1] = "1";
98798   azArg[2] = zMasterSchema;
98799   azArg[3] = 0;
98800   initData.db = db;
98801   initData.iDb = iDb;
98802   initData.rc = SQLITE_OK;
98803   initData.pzErrMsg = pzErrMsg;
98804   sqlite3InitCallback(&initData, 3, (char **)azArg, 0);
98805   if( initData.rc ){
98806     rc = initData.rc;
98807     goto error_out;
98808   }
98809   pTab = sqlite3FindTable(db, zMasterName, db->aDb[iDb].zName);
98810   if( ALWAYS(pTab) ){
98811     pTab->tabFlags |= TF_Readonly;
98812   }
98813 
98814   /* Create a cursor to hold the database open
98815   */
98816   pDb = &db->aDb[iDb];
98817   if( pDb->pBt==0 ){
98818     if( !OMIT_TEMPDB && ALWAYS(iDb==1) ){
98819       DbSetProperty(db, 1, DB_SchemaLoaded);
98820     }
98821     return SQLITE_OK;
98822   }
98823 
98824   /* If there is not already a read-only (or read-write) transaction opened
98825   ** on the b-tree database, open one now. If a transaction is opened, it
98826   ** will be closed before this function returns.  */
98827   sqlite3BtreeEnter(pDb->pBt);
98828   if( !sqlite3BtreeIsInReadTrans(pDb->pBt) ){
98829     rc = sqlite3BtreeBeginTrans(pDb->pBt, 0);
98830     if( rc!=SQLITE_OK ){
98831       sqlite3SetString(pzErrMsg, db, "%s", sqlite3ErrStr(rc));
98832       goto initone_error_out;
98833     }
98834     openedTransaction = 1;
98835   }
98836 
98837   /* Get the database meta information.
98838   **
98839   ** Meta values are as follows:
98840   **    meta[0]   Schema cookie.  Changes with each schema change.
98841   **    meta[1]   File format of schema layer.
98842   **    meta[2]   Size of the page cache.
98843   **    meta[3]   Largest rootpage (auto/incr_vacuum mode)
98844   **    meta[4]   Db text encoding. 1:UTF-8 2:UTF-16LE 3:UTF-16BE
98845   **    meta[5]   User version
98846   **    meta[6]   Incremental vacuum mode
98847   **    meta[7]   unused
98848   **    meta[8]   unused
98849   **    meta[9]   unused
98850   **
98851   ** Note: The #defined SQLITE_UTF* symbols in sqliteInt.h correspond to
98852   ** the possible values of meta[4].
98853   */
98854   for(i=0; i<ArraySize(meta); i++){
98855     sqlite3BtreeGetMeta(pDb->pBt, i+1, (u32 *)&meta[i]);
98856   }
98857   pDb->pSchema->schema_cookie = meta[BTREE_SCHEMA_VERSION-1];
98858 
98859   /* If opening a non-empty database, check the text encoding. For the
98860   ** main database, set sqlite3.enc to the encoding of the main database.
98861   ** For an attached db, it is an error if the encoding is not the same
98862   ** as sqlite3.enc.
98863   */
98864   if( meta[BTREE_TEXT_ENCODING-1] ){  /* text encoding */
98865     if( iDb==0 ){
98866 #ifndef SQLITE_OMIT_UTF16
98867       u8 encoding;
98868       /* If opening the main database, set ENC(db). */
98869       encoding = (u8)meta[BTREE_TEXT_ENCODING-1] & 3;
98870       if( encoding==0 ) encoding = SQLITE_UTF8;
98871       ENC(db) = encoding;
98872 #else
98873       ENC(db) = SQLITE_UTF8;
98874 #endif
98875     }else{
98876       /* If opening an attached database, the encoding much match ENC(db) */
98877       if( meta[BTREE_TEXT_ENCODING-1]!=ENC(db) ){
98878         sqlite3SetString(pzErrMsg, db, "attached databases must use the same"
98879             " text encoding as main database");
98880         rc = SQLITE_ERROR;
98881         goto initone_error_out;
98882       }
98883     }
98884   }else{
98885     DbSetProperty(db, iDb, DB_Empty);
98886   }
98887   pDb->pSchema->enc = ENC(db);
98888 
98889   if( pDb->pSchema->cache_size==0 ){
98890 #ifndef SQLITE_OMIT_DEPRECATED
98891     size = sqlite3AbsInt32(meta[BTREE_DEFAULT_CACHE_SIZE-1]);
98892     if( size==0 ){ size = SQLITE_DEFAULT_CACHE_SIZE; }
98893     pDb->pSchema->cache_size = size;
98894 #else
98895     pDb->pSchema->cache_size = SQLITE_DEFAULT_CACHE_SIZE;
98896 #endif
98897     sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
98898   }
98899 
98900   /*
98901   ** file_format==1    Version 3.0.0.
98902   ** file_format==2    Version 3.1.3.  // ALTER TABLE ADD COLUMN
98903   ** file_format==3    Version 3.1.4.  // ditto but with non-NULL defaults
98904   ** file_format==4    Version 3.3.0.  // DESC indices.  Boolean constants
98905   */
98906   pDb->pSchema->file_format = (u8)meta[BTREE_FILE_FORMAT-1];
98907   if( pDb->pSchema->file_format==0 ){
98908     pDb->pSchema->file_format = 1;
98909   }
98910   if( pDb->pSchema->file_format>SQLITE_MAX_FILE_FORMAT ){
98911     sqlite3SetString(pzErrMsg, db, "unsupported file format");
98912     rc = SQLITE_ERROR;
98913     goto initone_error_out;
98914   }
98915 
98916   /* Ticket #2804:  When we open a database in the newer file format,
98917   ** clear the legacy_file_format pragma flag so that a VACUUM will
98918   ** not downgrade the database and thus invalidate any descending
98919   ** indices that the user might have created.
98920   */
98921   if( iDb==0 && meta[BTREE_FILE_FORMAT-1]>=4 ){
98922     db->flags &= ~SQLITE_LegacyFileFmt;
98923   }
98924 
98925   /* Read the schema information out of the schema tables
98926   */
98927   assert( db->init.busy );
98928   {
98929     char *zSql;
98930     zSql = sqlite3MPrintf(db,
98931         "SELECT name, rootpage, sql FROM '%q'.%s ORDER BY rowid",
98932         db->aDb[iDb].zName, zMasterName);
98933 #ifndef SQLITE_OMIT_AUTHORIZATION
98934     {
98935       int (*xAuth)(void*,int,const char*,const char*,const char*,const char*);
98936       xAuth = db->xAuth;
98937       db->xAuth = 0;
98938 #endif
98939       rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
98940 #ifndef SQLITE_OMIT_AUTHORIZATION
98941       db->xAuth = xAuth;
98942     }
98943 #endif
98944     if( rc==SQLITE_OK ) rc = initData.rc;
98945     sqlite3DbFree(db, zSql);
98946 #ifndef SQLITE_OMIT_ANALYZE
98947     if( rc==SQLITE_OK ){
98948       sqlite3AnalysisLoad(db, iDb);
98949     }
98950 #endif
98951   }
98952   if( db->mallocFailed ){
98953     rc = SQLITE_NOMEM;
98954     sqlite3ResetAllSchemasOfConnection(db);
98955   }
98956   if( rc==SQLITE_OK || (db->flags&SQLITE_RecoveryMode)){
98957     /* Black magic: If the SQLITE_RecoveryMode flag is set, then consider
98958     ** the schema loaded, even if errors occurred. In this situation the
98959     ** current sqlite3_prepare() operation will fail, but the following one
98960     ** will attempt to compile the supplied statement against whatever subset
98961     ** of the schema was loaded before the error occurred. The primary
98962     ** purpose of this is to allow access to the sqlite_master table
98963     ** even when its contents have been corrupted.
98964     */
98965     DbSetProperty(db, iDb, DB_SchemaLoaded);
98966     rc = SQLITE_OK;
98967   }
98968 
98969   /* Jump here for an error that occurs after successfully allocating
98970   ** curMain and calling sqlite3BtreeEnter(). For an error that occurs
98971   ** before that point, jump to error_out.
98972   */
98973 initone_error_out:
98974   if( openedTransaction ){
98975     sqlite3BtreeCommit(pDb->pBt);
98976   }
98977   sqlite3BtreeLeave(pDb->pBt);
98978 
98979 error_out:
98980   if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
98981     db->mallocFailed = 1;
98982   }
98983   return rc;
98984 }
98985 
98986 /*
98987 ** Initialize all database files - the main database file, the file
98988 ** used to store temporary tables, and any additional database files
98989 ** created using ATTACH statements.  Return a success code.  If an
98990 ** error occurs, write an error message into *pzErrMsg.
98991 **
98992 ** After a database is initialized, the DB_SchemaLoaded bit is set
98993 ** bit is set in the flags field of the Db structure. If the database
98994 ** file was of zero-length, then the DB_Empty flag is also set.
98995 */
98996 SQLITE_PRIVATE int sqlite3Init(sqlite3 *db, char **pzErrMsg){
98997   int i, rc;
98998   int commit_internal = !(db->flags&SQLITE_InternChanges);
98999 
99000   assert( sqlite3_mutex_held(db->mutex) );
99001   rc = SQLITE_OK;
99002   db->init.busy = 1;
99003   for(i=0; rc==SQLITE_OK && i<db->nDb; i++){
99004     if( DbHasProperty(db, i, DB_SchemaLoaded) || i==1 ) continue;
99005     rc = sqlite3InitOne(db, i, pzErrMsg);
99006     if( rc ){
99007       sqlite3ResetOneSchema(db, i);
99008     }
99009   }
99010 
99011   /* Once all the other databases have been initialized, load the schema
99012   ** for the TEMP database. This is loaded last, as the TEMP database
99013   ** schema may contain references to objects in other databases.
99014   */
99015 #ifndef SQLITE_OMIT_TEMPDB
99016   if( rc==SQLITE_OK && ALWAYS(db->nDb>1)
99017                     && !DbHasProperty(db, 1, DB_SchemaLoaded) ){
99018     rc = sqlite3InitOne(db, 1, pzErrMsg);
99019     if( rc ){
99020       sqlite3ResetOneSchema(db, 1);
99021     }
99022   }
99023 #endif
99024 
99025   db->init.busy = 0;
99026   if( rc==SQLITE_OK && commit_internal ){
99027     sqlite3CommitInternalChanges(db);
99028   }
99029 
99030   return rc;
99031 }
99032 
99033 /*
99034 ** This routine is a no-op if the database schema is already initialized.
99035 ** Otherwise, the schema is loaded. An error code is returned.
99036 */
99037 SQLITE_PRIVATE int sqlite3ReadSchema(Parse *pParse){
99038   int rc = SQLITE_OK;
99039   sqlite3 *db = pParse->db;
99040   assert( sqlite3_mutex_held(db->mutex) );
99041   if( !db->init.busy ){
99042     rc = sqlite3Init(db, &pParse->zErrMsg);
99043   }
99044   if( rc!=SQLITE_OK ){
99045     pParse->rc = rc;
99046     pParse->nErr++;
99047   }
99048   return rc;
99049 }
99050 
99051 
99052 /*
99053 ** Check schema cookies in all databases.  If any cookie is out
99054 ** of date set pParse->rc to SQLITE_SCHEMA.  If all schema cookies
99055 ** make no changes to pParse->rc.
99056 */
99057 static void schemaIsValid(Parse *pParse){
99058   sqlite3 *db = pParse->db;
99059   int iDb;
99060   int rc;
99061   int cookie;
99062 
99063   assert( pParse->checkSchema );
99064   assert( sqlite3_mutex_held(db->mutex) );
99065   for(iDb=0; iDb<db->nDb; iDb++){
99066     int openedTransaction = 0;         /* True if a transaction is opened */
99067     Btree *pBt = db->aDb[iDb].pBt;     /* Btree database to read cookie from */
99068     if( pBt==0 ) continue;
99069 
99070     /* If there is not already a read-only (or read-write) transaction opened
99071     ** on the b-tree database, open one now. If a transaction is opened, it
99072     ** will be closed immediately after reading the meta-value. */
99073     if( !sqlite3BtreeIsInReadTrans(pBt) ){
99074       rc = sqlite3BtreeBeginTrans(pBt, 0);
99075       if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ){
99076         db->mallocFailed = 1;
99077       }
99078       if( rc!=SQLITE_OK ) return;
99079       openedTransaction = 1;
99080     }
99081 
99082     /* Read the schema cookie from the database. If it does not match the
99083     ** value stored as part of the in-memory schema representation,
99084     ** set Parse.rc to SQLITE_SCHEMA. */
99085     sqlite3BtreeGetMeta(pBt, BTREE_SCHEMA_VERSION, (u32 *)&cookie);
99086     assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
99087     if( cookie!=db->aDb[iDb].pSchema->schema_cookie ){
99088       sqlite3ResetOneSchema(db, iDb);
99089       pParse->rc = SQLITE_SCHEMA;
99090     }
99091 
99092     /* Close the transaction, if one was opened. */
99093     if( openedTransaction ){
99094       sqlite3BtreeCommit(pBt);
99095     }
99096   }
99097 }
99098 
99099 /*
99100 ** Convert a schema pointer into the iDb index that indicates
99101 ** which database file in db->aDb[] the schema refers to.
99102 **
99103 ** If the same database is attached more than once, the first
99104 ** attached database is returned.
99105 */
99106 SQLITE_PRIVATE int sqlite3SchemaToIndex(sqlite3 *db, Schema *pSchema){
99107   int i = -1000000;
99108 
99109   /* If pSchema is NULL, then return -1000000. This happens when code in
99110   ** expr.c is trying to resolve a reference to a transient table (i.e. one
99111   ** created by a sub-select). In this case the return value of this
99112   ** function should never be used.
99113   **
99114   ** We return -1000000 instead of the more usual -1 simply because using
99115   ** -1000000 as the incorrect index into db->aDb[] is much
99116   ** more likely to cause a segfault than -1 (of course there are assert()
99117   ** statements too, but it never hurts to play the odds).
99118   */
99119   assert( sqlite3_mutex_held(db->mutex) );
99120   if( pSchema ){
99121     for(i=0; ALWAYS(i<db->nDb); i++){
99122       if( db->aDb[i].pSchema==pSchema ){
99123         break;
99124       }
99125     }
99126     assert( i>=0 && i<db->nDb );
99127   }
99128   return i;
99129 }
99130 
99131 /*
99132 ** Free all memory allocations in the pParse object
99133 */
99134 SQLITE_PRIVATE void sqlite3ParserReset(Parse *pParse){
99135   if( pParse ){
99136     sqlite3 *db = pParse->db;
99137     sqlite3DbFree(db, pParse->aLabel);
99138     sqlite3ExprListDelete(db, pParse->pConstExpr);
99139   }
99140 }
99141 
99142 /*
99143 ** Compile the UTF-8 encoded SQL statement zSql into a statement handle.
99144 */
99145 static int sqlite3Prepare(
99146   sqlite3 *db,              /* Database handle. */
99147   const char *zSql,         /* UTF-8 encoded SQL statement. */
99148   int nBytes,               /* Length of zSql in bytes. */
99149   int saveSqlFlag,          /* True to copy SQL text into the sqlite3_stmt */
99150   Vdbe *pReprepare,         /* VM being reprepared */
99151   sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
99152   const char **pzTail       /* OUT: End of parsed string */
99153 ){
99154   Parse *pParse;            /* Parsing context */
99155   char *zErrMsg = 0;        /* Error message */
99156   int rc = SQLITE_OK;       /* Result code */
99157   int i;                    /* Loop counter */
99158 
99159   /* Allocate the parsing context */
99160   pParse = sqlite3StackAllocZero(db, sizeof(*pParse));
99161   if( pParse==0 ){
99162     rc = SQLITE_NOMEM;
99163     goto end_prepare;
99164   }
99165   pParse->pReprepare = pReprepare;
99166   assert( ppStmt && *ppStmt==0 );
99167   assert( !db->mallocFailed );
99168   assert( sqlite3_mutex_held(db->mutex) );
99169 
99170   /* Check to verify that it is possible to get a read lock on all
99171   ** database schemas.  The inability to get a read lock indicates that
99172   ** some other database connection is holding a write-lock, which in
99173   ** turn means that the other connection has made uncommitted changes
99174   ** to the schema.
99175   **
99176   ** Were we to proceed and prepare the statement against the uncommitted
99177   ** schema changes and if those schema changes are subsequently rolled
99178   ** back and different changes are made in their place, then when this
99179   ** prepared statement goes to run the schema cookie would fail to detect
99180   ** the schema change.  Disaster would follow.
99181   **
99182   ** This thread is currently holding mutexes on all Btrees (because
99183   ** of the sqlite3BtreeEnterAll() in sqlite3LockAndPrepare()) so it
99184   ** is not possible for another thread to start a new schema change
99185   ** while this routine is running.  Hence, we do not need to hold
99186   ** locks on the schema, we just need to make sure nobody else is
99187   ** holding them.
99188   **
99189   ** Note that setting READ_UNCOMMITTED overrides most lock detection,
99190   ** but it does *not* override schema lock detection, so this all still
99191   ** works even if READ_UNCOMMITTED is set.
99192   */
99193   for(i=0; i<db->nDb; i++) {
99194     Btree *pBt = db->aDb[i].pBt;
99195     if( pBt ){
99196       assert( sqlite3BtreeHoldsMutex(pBt) );
99197       rc = sqlite3BtreeSchemaLocked(pBt);
99198       if( rc ){
99199         const char *zDb = db->aDb[i].zName;
99200         sqlite3Error(db, rc, "database schema is locked: %s", zDb);
99201         testcase( db->flags & SQLITE_ReadUncommitted );
99202         goto end_prepare;
99203       }
99204     }
99205   }
99206 
99207   sqlite3VtabUnlockList(db);
99208 
99209   pParse->db = db;
99210   pParse->nQueryLoop = 0;  /* Logarithmic, so 0 really means 1 */
99211   if( nBytes>=0 && (nBytes==0 || zSql[nBytes-1]!=0) ){
99212     char *zSqlCopy;
99213     int mxLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
99214     testcase( nBytes==mxLen );
99215     testcase( nBytes==mxLen+1 );
99216     if( nBytes>mxLen ){
99217       sqlite3Error(db, SQLITE_TOOBIG, "statement too long");
99218       rc = sqlite3ApiExit(db, SQLITE_TOOBIG);
99219       goto end_prepare;
99220     }
99221     zSqlCopy = sqlite3DbStrNDup(db, zSql, nBytes);
99222     if( zSqlCopy ){
99223       sqlite3RunParser(pParse, zSqlCopy, &zErrMsg);
99224       sqlite3DbFree(db, zSqlCopy);
99225       pParse->zTail = &zSql[pParse->zTail-zSqlCopy];
99226     }else{
99227       pParse->zTail = &zSql[nBytes];
99228     }
99229   }else{
99230     sqlite3RunParser(pParse, zSql, &zErrMsg);
99231   }
99232   assert( 0==pParse->nQueryLoop );
99233 
99234   if( db->mallocFailed ){
99235     pParse->rc = SQLITE_NOMEM;
99236   }
99237   if( pParse->rc==SQLITE_DONE ) pParse->rc = SQLITE_OK;
99238   if( pParse->checkSchema ){
99239     schemaIsValid(pParse);
99240   }
99241   if( db->mallocFailed ){
99242     pParse->rc = SQLITE_NOMEM;
99243   }
99244   if( pzTail ){
99245     *pzTail = pParse->zTail;
99246   }
99247   rc = pParse->rc;
99248 
99249 #ifndef SQLITE_OMIT_EXPLAIN
99250   if( rc==SQLITE_OK && pParse->pVdbe && pParse->explain ){
99251     static const char * const azColName[] = {
99252        "addr", "opcode", "p1", "p2", "p3", "p4", "p5", "comment",
99253        "selectid", "order", "from", "detail"
99254     };
99255     int iFirst, mx;
99256     if( pParse->explain==2 ){
99257       sqlite3VdbeSetNumCols(pParse->pVdbe, 4);
99258       iFirst = 8;
99259       mx = 12;
99260     }else{
99261       sqlite3VdbeSetNumCols(pParse->pVdbe, 8);
99262       iFirst = 0;
99263       mx = 8;
99264     }
99265     for(i=iFirst; i<mx; i++){
99266       sqlite3VdbeSetColName(pParse->pVdbe, i-iFirst, COLNAME_NAME,
99267                             azColName[i], SQLITE_STATIC);
99268     }
99269   }
99270 #endif
99271 
99272   if( db->init.busy==0 ){
99273     Vdbe *pVdbe = pParse->pVdbe;
99274     sqlite3VdbeSetSql(pVdbe, zSql, (int)(pParse->zTail-zSql), saveSqlFlag);
99275   }
99276   if( pParse->pVdbe && (rc!=SQLITE_OK || db->mallocFailed) ){
99277     sqlite3VdbeFinalize(pParse->pVdbe);
99278     assert(!(*ppStmt));
99279   }else{
99280     *ppStmt = (sqlite3_stmt*)pParse->pVdbe;
99281   }
99282 
99283   if( zErrMsg ){
99284     sqlite3Error(db, rc, "%s", zErrMsg);
99285     sqlite3DbFree(db, zErrMsg);
99286   }else{
99287     sqlite3Error(db, rc, 0);
99288   }
99289 
99290   /* Delete any TriggerPrg structures allocated while parsing this statement. */
99291   while( pParse->pTriggerPrg ){
99292     TriggerPrg *pT = pParse->pTriggerPrg;
99293     pParse->pTriggerPrg = pT->pNext;
99294     sqlite3DbFree(db, pT);
99295   }
99296 
99297 end_prepare:
99298 
99299   sqlite3ParserReset(pParse);
99300   sqlite3StackFree(db, pParse);
99301   rc = sqlite3ApiExit(db, rc);
99302   assert( (rc&db->errMask)==rc );
99303   return rc;
99304 }
99305 static int sqlite3LockAndPrepare(
99306   sqlite3 *db,              /* Database handle. */
99307   const char *zSql,         /* UTF-8 encoded SQL statement. */
99308   int nBytes,               /* Length of zSql in bytes. */
99309   int saveSqlFlag,          /* True to copy SQL text into the sqlite3_stmt */
99310   Vdbe *pOld,               /* VM being reprepared */
99311   sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
99312   const char **pzTail       /* OUT: End of parsed string */
99313 ){
99314   int rc;
99315   assert( ppStmt!=0 );
99316   *ppStmt = 0;
99317   if( !sqlite3SafetyCheckOk(db) ){
99318     return SQLITE_MISUSE_BKPT;
99319   }
99320   sqlite3_mutex_enter(db->mutex);
99321   sqlite3BtreeEnterAll(db);
99322   rc = sqlite3Prepare(db, zSql, nBytes, saveSqlFlag, pOld, ppStmt, pzTail);
99323   if( rc==SQLITE_SCHEMA ){
99324     sqlite3_finalize(*ppStmt);
99325     rc = sqlite3Prepare(db, zSql, nBytes, saveSqlFlag, pOld, ppStmt, pzTail);
99326   }
99327   sqlite3BtreeLeaveAll(db);
99328   sqlite3_mutex_leave(db->mutex);
99329   assert( rc==SQLITE_OK || *ppStmt==0 );
99330   return rc;
99331 }
99332 
99333 /*
99334 ** Rerun the compilation of a statement after a schema change.
99335 **
99336 ** If the statement is successfully recompiled, return SQLITE_OK. Otherwise,
99337 ** if the statement cannot be recompiled because another connection has
99338 ** locked the sqlite3_master table, return SQLITE_LOCKED. If any other error
99339 ** occurs, return SQLITE_SCHEMA.
99340 */
99341 SQLITE_PRIVATE int sqlite3Reprepare(Vdbe *p){
99342   int rc;
99343   sqlite3_stmt *pNew;
99344   const char *zSql;
99345   sqlite3 *db;
99346 
99347   assert( sqlite3_mutex_held(sqlite3VdbeDb(p)->mutex) );
99348   zSql = sqlite3_sql((sqlite3_stmt *)p);
99349   assert( zSql!=0 );  /* Reprepare only called for prepare_v2() statements */
99350   db = sqlite3VdbeDb(p);
99351   assert( sqlite3_mutex_held(db->mutex) );
99352   rc = sqlite3LockAndPrepare(db, zSql, -1, 0, p, &pNew, 0);
99353   if( rc ){
99354     if( rc==SQLITE_NOMEM ){
99355       db->mallocFailed = 1;
99356     }
99357     assert( pNew==0 );
99358     return rc;
99359   }else{
99360     assert( pNew!=0 );
99361   }
99362   sqlite3VdbeSwap((Vdbe*)pNew, p);
99363   sqlite3TransferBindings(pNew, (sqlite3_stmt*)p);
99364   sqlite3VdbeResetStepResult((Vdbe*)pNew);
99365   sqlite3VdbeFinalize((Vdbe*)pNew);
99366   return SQLITE_OK;
99367 }
99368 
99369 
99370 /*
99371 ** Two versions of the official API.  Legacy and new use.  In the legacy
99372 ** version, the original SQL text is not saved in the prepared statement
99373 ** and so if a schema change occurs, SQLITE_SCHEMA is returned by
99374 ** sqlite3_step().  In the new version, the original SQL text is retained
99375 ** and the statement is automatically recompiled if an schema change
99376 ** occurs.
99377 */
99378 SQLITE_API int sqlite3_prepare(
99379   sqlite3 *db,              /* Database handle. */
99380   const char *zSql,         /* UTF-8 encoded SQL statement. */
99381   int nBytes,               /* Length of zSql in bytes. */
99382   sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
99383   const char **pzTail       /* OUT: End of parsed string */
99384 ){
99385   int rc;
99386   rc = sqlite3LockAndPrepare(db,zSql,nBytes,0,0,ppStmt,pzTail);
99387   assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 );  /* VERIFY: F13021 */
99388   return rc;
99389 }
99390 SQLITE_API int sqlite3_prepare_v2(
99391   sqlite3 *db,              /* Database handle. */
99392   const char *zSql,         /* UTF-8 encoded SQL statement. */
99393   int nBytes,               /* Length of zSql in bytes. */
99394   sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
99395   const char **pzTail       /* OUT: End of parsed string */
99396 ){
99397   int rc;
99398   rc = sqlite3LockAndPrepare(db,zSql,nBytes,1,0,ppStmt,pzTail);
99399   assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 );  /* VERIFY: F13021 */
99400   return rc;
99401 }
99402 
99403 
99404 #ifndef SQLITE_OMIT_UTF16
99405 /*
99406 ** Compile the UTF-16 encoded SQL statement zSql into a statement handle.
99407 */
99408 static int sqlite3Prepare16(
99409   sqlite3 *db,              /* Database handle. */
99410   const void *zSql,         /* UTF-16 encoded SQL statement. */
99411   int nBytes,               /* Length of zSql in bytes. */
99412   int saveSqlFlag,          /* True to save SQL text into the sqlite3_stmt */
99413   sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
99414   const void **pzTail       /* OUT: End of parsed string */
99415 ){
99416   /* This function currently works by first transforming the UTF-16
99417   ** encoded string to UTF-8, then invoking sqlite3_prepare(). The
99418   ** tricky bit is figuring out the pointer to return in *pzTail.
99419   */
99420   char *zSql8;
99421   const char *zTail8 = 0;
99422   int rc = SQLITE_OK;
99423 
99424   assert( ppStmt );
99425   *ppStmt = 0;
99426   if( !sqlite3SafetyCheckOk(db) ){
99427     return SQLITE_MISUSE_BKPT;
99428   }
99429   if( nBytes>=0 ){
99430     int sz;
99431     const char *z = (const char*)zSql;
99432     for(sz=0; sz<nBytes && (z[sz]!=0 || z[sz+1]!=0); sz += 2){}
99433     nBytes = sz;
99434   }
99435   sqlite3_mutex_enter(db->mutex);
99436   zSql8 = sqlite3Utf16to8(db, zSql, nBytes, SQLITE_UTF16NATIVE);
99437   if( zSql8 ){
99438     rc = sqlite3LockAndPrepare(db, zSql8, -1, saveSqlFlag, 0, ppStmt, &zTail8);
99439   }
99440 
99441   if( zTail8 && pzTail ){
99442     /* If sqlite3_prepare returns a tail pointer, we calculate the
99443     ** equivalent pointer into the UTF-16 string by counting the unicode
99444     ** characters between zSql8 and zTail8, and then returning a pointer
99445     ** the same number of characters into the UTF-16 string.
99446     */
99447     int chars_parsed = sqlite3Utf8CharLen(zSql8, (int)(zTail8-zSql8));
99448     *pzTail = (u8 *)zSql + sqlite3Utf16ByteLen(zSql, chars_parsed);
99449   }
99450   sqlite3DbFree(db, zSql8);
99451   rc = sqlite3ApiExit(db, rc);
99452   sqlite3_mutex_leave(db->mutex);
99453   return rc;
99454 }
99455 
99456 /*
99457 ** Two versions of the official API.  Legacy and new use.  In the legacy
99458 ** version, the original SQL text is not saved in the prepared statement
99459 ** and so if a schema change occurs, SQLITE_SCHEMA is returned by
99460 ** sqlite3_step().  In the new version, the original SQL text is retained
99461 ** and the statement is automatically recompiled if an schema change
99462 ** occurs.
99463 */
99464 SQLITE_API int sqlite3_prepare16(
99465   sqlite3 *db,              /* Database handle. */
99466   const void *zSql,         /* UTF-16 encoded SQL statement. */
99467   int nBytes,               /* Length of zSql in bytes. */
99468   sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
99469   const void **pzTail       /* OUT: End of parsed string */
99470 ){
99471   int rc;
99472   rc = sqlite3Prepare16(db,zSql,nBytes,0,ppStmt,pzTail);
99473   assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 );  /* VERIFY: F13021 */
99474   return rc;
99475 }
99476 SQLITE_API int sqlite3_prepare16_v2(
99477   sqlite3 *db,              /* Database handle. */
99478   const void *zSql,         /* UTF-16 encoded SQL statement. */
99479   int nBytes,               /* Length of zSql in bytes. */
99480   sqlite3_stmt **ppStmt,    /* OUT: A pointer to the prepared statement */
99481   const void **pzTail       /* OUT: End of parsed string */
99482 ){
99483   int rc;
99484   rc = sqlite3Prepare16(db,zSql,nBytes,1,ppStmt,pzTail);
99485   assert( rc==SQLITE_OK || ppStmt==0 || *ppStmt==0 );  /* VERIFY: F13021 */
99486   return rc;
99487 }
99488 
99489 #endif /* SQLITE_OMIT_UTF16 */
99490 
99491 /************** End of prepare.c *********************************************/
99492 /************** Begin file select.c ******************************************/
99493 /*
99494 ** 2001 September 15
99495 **
99496 ** The author disclaims copyright to this source code.  In place of
99497 ** a legal notice, here is a blessing:
99498 **
99499 **    May you do good and not evil.
99500 **    May you find forgiveness for yourself and forgive others.
99501 **    May you share freely, never taking more than you give.
99502 **
99503 *************************************************************************
99504 ** This file contains C code routines that are called by the parser
99505 ** to handle SELECT statements in SQLite.
99506 */
99507 
99508 
99509 /*
99510 ** Delete all the content of a Select structure but do not deallocate
99511 ** the select structure itself.
99512 */
99513 static void clearSelect(sqlite3 *db, Select *p){
99514   sqlite3ExprListDelete(db, p->pEList);
99515   sqlite3SrcListDelete(db, p->pSrc);
99516   sqlite3ExprDelete(db, p->pWhere);
99517   sqlite3ExprListDelete(db, p->pGroupBy);
99518   sqlite3ExprDelete(db, p->pHaving);
99519   sqlite3ExprListDelete(db, p->pOrderBy);
99520   sqlite3SelectDelete(db, p->pPrior);
99521   sqlite3ExprDelete(db, p->pLimit);
99522   sqlite3ExprDelete(db, p->pOffset);
99523   sqlite3WithDelete(db, p->pWith);
99524 }
99525 
99526 /*
99527 ** Initialize a SelectDest structure.
99528 */
99529 SQLITE_PRIVATE void sqlite3SelectDestInit(SelectDest *pDest, int eDest, int iParm){
99530   pDest->eDest = (u8)eDest;
99531   pDest->iSDParm = iParm;
99532   pDest->affSdst = 0;
99533   pDest->iSdst = 0;
99534   pDest->nSdst = 0;
99535 }
99536 
99537 
99538 /*
99539 ** Allocate a new Select structure and return a pointer to that
99540 ** structure.
99541 */
99542 SQLITE_PRIVATE Select *sqlite3SelectNew(
99543   Parse *pParse,        /* Parsing context */
99544   ExprList *pEList,     /* which columns to include in the result */
99545   SrcList *pSrc,        /* the FROM clause -- which tables to scan */
99546   Expr *pWhere,         /* the WHERE clause */
99547   ExprList *pGroupBy,   /* the GROUP BY clause */
99548   Expr *pHaving,        /* the HAVING clause */
99549   ExprList *pOrderBy,   /* the ORDER BY clause */
99550   u16 selFlags,         /* Flag parameters, such as SF_Distinct */
99551   Expr *pLimit,         /* LIMIT value.  NULL means not used */
99552   Expr *pOffset         /* OFFSET value.  NULL means no offset */
99553 ){
99554   Select *pNew;
99555   Select standin;
99556   sqlite3 *db = pParse->db;
99557   pNew = sqlite3DbMallocZero(db, sizeof(*pNew) );
99558   assert( db->mallocFailed || !pOffset || pLimit ); /* OFFSET implies LIMIT */
99559   if( pNew==0 ){
99560     assert( db->mallocFailed );
99561     pNew = &standin;
99562     memset(pNew, 0, sizeof(*pNew));
99563   }
99564   if( pEList==0 ){
99565     pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(db,TK_ALL,0));
99566   }
99567   pNew->pEList = pEList;
99568   if( pSrc==0 ) pSrc = sqlite3DbMallocZero(db, sizeof(*pSrc));
99569   pNew->pSrc = pSrc;
99570   pNew->pWhere = pWhere;
99571   pNew->pGroupBy = pGroupBy;
99572   pNew->pHaving = pHaving;
99573   pNew->pOrderBy = pOrderBy;
99574   pNew->selFlags = selFlags;
99575   pNew->op = TK_SELECT;
99576   pNew->pLimit = pLimit;
99577   pNew->pOffset = pOffset;
99578   assert( pOffset==0 || pLimit!=0 );
99579   pNew->addrOpenEphm[0] = -1;
99580   pNew->addrOpenEphm[1] = -1;
99581   pNew->addrOpenEphm[2] = -1;
99582   if( db->mallocFailed ) {
99583     clearSelect(db, pNew);
99584     if( pNew!=&standin ) sqlite3DbFree(db, pNew);
99585     pNew = 0;
99586   }else{
99587     assert( pNew->pSrc!=0 || pParse->nErr>0 );
99588   }
99589   assert( pNew!=&standin );
99590   return pNew;
99591 }
99592 
99593 /*
99594 ** Delete the given Select structure and all of its substructures.
99595 */
99596 SQLITE_PRIVATE void sqlite3SelectDelete(sqlite3 *db, Select *p){
99597   if( p ){
99598     clearSelect(db, p);
99599     sqlite3DbFree(db, p);
99600   }
99601 }
99602 
99603 /*
99604 ** Given 1 to 3 identifiers preceding the JOIN keyword, determine the
99605 ** type of join.  Return an integer constant that expresses that type
99606 ** in terms of the following bit values:
99607 **
99608 **     JT_INNER
99609 **     JT_CROSS
99610 **     JT_OUTER
99611 **     JT_NATURAL
99612 **     JT_LEFT
99613 **     JT_RIGHT
99614 **
99615 ** A full outer join is the combination of JT_LEFT and JT_RIGHT.
99616 **
99617 ** If an illegal or unsupported join type is seen, then still return
99618 ** a join type, but put an error in the pParse structure.
99619 */
99620 SQLITE_PRIVATE int sqlite3JoinType(Parse *pParse, Token *pA, Token *pB, Token *pC){
99621   int jointype = 0;
99622   Token *apAll[3];
99623   Token *p;
99624                              /*   0123456789 123456789 123456789 123 */
99625   static const char zKeyText[] = "naturaleftouterightfullinnercross";
99626   static const struct {
99627     u8 i;        /* Beginning of keyword text in zKeyText[] */
99628     u8 nChar;    /* Length of the keyword in characters */
99629     u8 code;     /* Join type mask */
99630   } aKeyword[] = {
99631     /* natural */ { 0,  7, JT_NATURAL                },
99632     /* left    */ { 6,  4, JT_LEFT|JT_OUTER          },
99633     /* outer   */ { 10, 5, JT_OUTER                  },
99634     /* right   */ { 14, 5, JT_RIGHT|JT_OUTER         },
99635     /* full    */ { 19, 4, JT_LEFT|JT_RIGHT|JT_OUTER },
99636     /* inner   */ { 23, 5, JT_INNER                  },
99637     /* cross   */ { 28, 5, JT_INNER|JT_CROSS         },
99638   };
99639   int i, j;
99640   apAll[0] = pA;
99641   apAll[1] = pB;
99642   apAll[2] = pC;
99643   for(i=0; i<3 && apAll[i]; i++){
99644     p = apAll[i];
99645     for(j=0; j<ArraySize(aKeyword); j++){
99646       if( p->n==aKeyword[j].nChar
99647           && sqlite3StrNICmp((char*)p->z, &zKeyText[aKeyword[j].i], p->n)==0 ){
99648         jointype |= aKeyword[j].code;
99649         break;
99650       }
99651     }
99652     testcase( j==0 || j==1 || j==2 || j==3 || j==4 || j==5 || j==6 );
99653     if( j>=ArraySize(aKeyword) ){
99654       jointype |= JT_ERROR;
99655       break;
99656     }
99657   }
99658   if(
99659      (jointype & (JT_INNER|JT_OUTER))==(JT_INNER|JT_OUTER) ||
99660      (jointype & JT_ERROR)!=0
99661   ){
99662     const char *zSp = " ";
99663     assert( pB!=0 );
99664     if( pC==0 ){ zSp++; }
99665     sqlite3ErrorMsg(pParse, "unknown or unsupported join type: "
99666        "%T %T%s%T", pA, pB, zSp, pC);
99667     jointype = JT_INNER;
99668   }else if( (jointype & JT_OUTER)!=0
99669          && (jointype & (JT_LEFT|JT_RIGHT))!=JT_LEFT ){
99670     sqlite3ErrorMsg(pParse,
99671       "RIGHT and FULL OUTER JOINs are not currently supported");
99672     jointype = JT_INNER;
99673   }
99674   return jointype;
99675 }
99676 
99677 /*
99678 ** Return the index of a column in a table.  Return -1 if the column
99679 ** is not contained in the table.
99680 */
99681 static int columnIndex(Table *pTab, const char *zCol){
99682   int i;
99683   for(i=0; i<pTab->nCol; i++){
99684     if( sqlite3StrICmp(pTab->aCol[i].zName, zCol)==0 ) return i;
99685   }
99686   return -1;
99687 }
99688 
99689 /*
99690 ** Search the first N tables in pSrc, from left to right, looking for a
99691 ** table that has a column named zCol.
99692 **
99693 ** When found, set *piTab and *piCol to the table index and column index
99694 ** of the matching column and return TRUE.
99695 **
99696 ** If not found, return FALSE.
99697 */
99698 static int tableAndColumnIndex(
99699   SrcList *pSrc,       /* Array of tables to search */
99700   int N,               /* Number of tables in pSrc->a[] to search */
99701   const char *zCol,    /* Name of the column we are looking for */
99702   int *piTab,          /* Write index of pSrc->a[] here */
99703   int *piCol           /* Write index of pSrc->a[*piTab].pTab->aCol[] here */
99704 ){
99705   int i;               /* For looping over tables in pSrc */
99706   int iCol;            /* Index of column matching zCol */
99707 
99708   assert( (piTab==0)==(piCol==0) );  /* Both or neither are NULL */
99709   for(i=0; i<N; i++){
99710     iCol = columnIndex(pSrc->a[i].pTab, zCol);
99711     if( iCol>=0 ){
99712       if( piTab ){
99713         *piTab = i;
99714         *piCol = iCol;
99715       }
99716       return 1;
99717     }
99718   }
99719   return 0;
99720 }
99721 
99722 /*
99723 ** This function is used to add terms implied by JOIN syntax to the
99724 ** WHERE clause expression of a SELECT statement. The new term, which
99725 ** is ANDed with the existing WHERE clause, is of the form:
99726 **
99727 **    (tab1.col1 = tab2.col2)
99728 **
99729 ** where tab1 is the iSrc'th table in SrcList pSrc and tab2 is the
99730 ** (iSrc+1)'th. Column col1 is column iColLeft of tab1, and col2 is
99731 ** column iColRight of tab2.
99732 */
99733 static void addWhereTerm(
99734   Parse *pParse,                  /* Parsing context */
99735   SrcList *pSrc,                  /* List of tables in FROM clause */
99736   int iLeft,                      /* Index of first table to join in pSrc */
99737   int iColLeft,                   /* Index of column in first table */
99738   int iRight,                     /* Index of second table in pSrc */
99739   int iColRight,                  /* Index of column in second table */
99740   int isOuterJoin,                /* True if this is an OUTER join */
99741   Expr **ppWhere                  /* IN/OUT: The WHERE clause to add to */
99742 ){
99743   sqlite3 *db = pParse->db;
99744   Expr *pE1;
99745   Expr *pE2;
99746   Expr *pEq;
99747 
99748   assert( iLeft<iRight );
99749   assert( pSrc->nSrc>iRight );
99750   assert( pSrc->a[iLeft].pTab );
99751   assert( pSrc->a[iRight].pTab );
99752 
99753   pE1 = sqlite3CreateColumnExpr(db, pSrc, iLeft, iColLeft);
99754   pE2 = sqlite3CreateColumnExpr(db, pSrc, iRight, iColRight);
99755 
99756   pEq = sqlite3PExpr(pParse, TK_EQ, pE1, pE2, 0);
99757   if( pEq && isOuterJoin ){
99758     ExprSetProperty(pEq, EP_FromJoin);
99759     assert( !ExprHasProperty(pEq, EP_TokenOnly|EP_Reduced) );
99760     ExprSetVVAProperty(pEq, EP_NoReduce);
99761     pEq->iRightJoinTable = (i16)pE2->iTable;
99762   }
99763   *ppWhere = sqlite3ExprAnd(db, *ppWhere, pEq);
99764 }
99765 
99766 /*
99767 ** Set the EP_FromJoin property on all terms of the given expression.
99768 ** And set the Expr.iRightJoinTable to iTable for every term in the
99769 ** expression.
99770 **
99771 ** The EP_FromJoin property is used on terms of an expression to tell
99772 ** the LEFT OUTER JOIN processing logic that this term is part of the
99773 ** join restriction specified in the ON or USING clause and not a part
99774 ** of the more general WHERE clause.  These terms are moved over to the
99775 ** WHERE clause during join processing but we need to remember that they
99776 ** originated in the ON or USING clause.
99777 **
99778 ** The Expr.iRightJoinTable tells the WHERE clause processing that the
99779 ** expression depends on table iRightJoinTable even if that table is not
99780 ** explicitly mentioned in the expression.  That information is needed
99781 ** for cases like this:
99782 **
99783 **    SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.b AND t1.x=5
99784 **
99785 ** The where clause needs to defer the handling of the t1.x=5
99786 ** term until after the t2 loop of the join.  In that way, a
99787 ** NULL t2 row will be inserted whenever t1.x!=5.  If we do not
99788 ** defer the handling of t1.x=5, it will be processed immediately
99789 ** after the t1 loop and rows with t1.x!=5 will never appear in
99790 ** the output, which is incorrect.
99791 */
99792 static void setJoinExpr(Expr *p, int iTable){
99793   while( p ){
99794     ExprSetProperty(p, EP_FromJoin);
99795     assert( !ExprHasProperty(p, EP_TokenOnly|EP_Reduced) );
99796     ExprSetVVAProperty(p, EP_NoReduce);
99797     p->iRightJoinTable = (i16)iTable;
99798     setJoinExpr(p->pLeft, iTable);
99799     p = p->pRight;
99800   }
99801 }
99802 
99803 /*
99804 ** This routine processes the join information for a SELECT statement.
99805 ** ON and USING clauses are converted into extra terms of the WHERE clause.
99806 ** NATURAL joins also create extra WHERE clause terms.
99807 **
99808 ** The terms of a FROM clause are contained in the Select.pSrc structure.
99809 ** The left most table is the first entry in Select.pSrc.  The right-most
99810 ** table is the last entry.  The join operator is held in the entry to
99811 ** the left.  Thus entry 0 contains the join operator for the join between
99812 ** entries 0 and 1.  Any ON or USING clauses associated with the join are
99813 ** also attached to the left entry.
99814 **
99815 ** This routine returns the number of errors encountered.
99816 */
99817 static int sqliteProcessJoin(Parse *pParse, Select *p){
99818   SrcList *pSrc;                  /* All tables in the FROM clause */
99819   int i, j;                       /* Loop counters */
99820   struct SrcList_item *pLeft;     /* Left table being joined */
99821   struct SrcList_item *pRight;    /* Right table being joined */
99822 
99823   pSrc = p->pSrc;
99824   pLeft = &pSrc->a[0];
99825   pRight = &pLeft[1];
99826   for(i=0; i<pSrc->nSrc-1; i++, pRight++, pLeft++){
99827     Table *pLeftTab = pLeft->pTab;
99828     Table *pRightTab = pRight->pTab;
99829     int isOuter;
99830 
99831     if( NEVER(pLeftTab==0 || pRightTab==0) ) continue;
99832     isOuter = (pRight->jointype & JT_OUTER)!=0;
99833 
99834     /* When the NATURAL keyword is present, add WHERE clause terms for
99835     ** every column that the two tables have in common.
99836     */
99837     if( pRight->jointype & JT_NATURAL ){
99838       if( pRight->pOn || pRight->pUsing ){
99839         sqlite3ErrorMsg(pParse, "a NATURAL join may not have "
99840            "an ON or USING clause", 0);
99841         return 1;
99842       }
99843       for(j=0; j<pRightTab->nCol; j++){
99844         char *zName;   /* Name of column in the right table */
99845         int iLeft;     /* Matching left table */
99846         int iLeftCol;  /* Matching column in the left table */
99847 
99848         zName = pRightTab->aCol[j].zName;
99849         if( tableAndColumnIndex(pSrc, i+1, zName, &iLeft, &iLeftCol) ){
99850           addWhereTerm(pParse, pSrc, iLeft, iLeftCol, i+1, j,
99851                        isOuter, &p->pWhere);
99852         }
99853       }
99854     }
99855 
99856     /* Disallow both ON and USING clauses in the same join
99857     */
99858     if( pRight->pOn && pRight->pUsing ){
99859       sqlite3ErrorMsg(pParse, "cannot have both ON and USING "
99860         "clauses in the same join");
99861       return 1;
99862     }
99863 
99864     /* Add the ON clause to the end of the WHERE clause, connected by
99865     ** an AND operator.
99866     */
99867     if( pRight->pOn ){
99868       if( isOuter ) setJoinExpr(pRight->pOn, pRight->iCursor);
99869       p->pWhere = sqlite3ExprAnd(pParse->db, p->pWhere, pRight->pOn);
99870       pRight->pOn = 0;
99871     }
99872 
99873     /* Create extra terms on the WHERE clause for each column named
99874     ** in the USING clause.  Example: If the two tables to be joined are
99875     ** A and B and the USING clause names X, Y, and Z, then add this
99876     ** to the WHERE clause:    A.X=B.X AND A.Y=B.Y AND A.Z=B.Z
99877     ** Report an error if any column mentioned in the USING clause is
99878     ** not contained in both tables to be joined.
99879     */
99880     if( pRight->pUsing ){
99881       IdList *pList = pRight->pUsing;
99882       for(j=0; j<pList->nId; j++){
99883         char *zName;     /* Name of the term in the USING clause */
99884         int iLeft;       /* Table on the left with matching column name */
99885         int iLeftCol;    /* Column number of matching column on the left */
99886         int iRightCol;   /* Column number of matching column on the right */
99887 
99888         zName = pList->a[j].zName;
99889         iRightCol = columnIndex(pRightTab, zName);
99890         if( iRightCol<0
99891          || !tableAndColumnIndex(pSrc, i+1, zName, &iLeft, &iLeftCol)
99892         ){
99893           sqlite3ErrorMsg(pParse, "cannot join using column %s - column "
99894             "not present in both tables", zName);
99895           return 1;
99896         }
99897         addWhereTerm(pParse, pSrc, iLeft, iLeftCol, i+1, iRightCol,
99898                      isOuter, &p->pWhere);
99899       }
99900     }
99901   }
99902   return 0;
99903 }
99904 
99905 /*
99906 ** Insert code into "v" that will push the record on the top of the
99907 ** stack into the sorter.
99908 */
99909 static void pushOntoSorter(
99910   Parse *pParse,         /* Parser context */
99911   ExprList *pOrderBy,    /* The ORDER BY clause */
99912   Select *pSelect,       /* The whole SELECT statement */
99913   int regData            /* Register holding data to be sorted */
99914 ){
99915   Vdbe *v = pParse->pVdbe;
99916   int nExpr = pOrderBy->nExpr;
99917   int regBase = sqlite3GetTempRange(pParse, nExpr+2);
99918   int regRecord = sqlite3GetTempReg(pParse);
99919   int op;
99920   sqlite3ExprCacheClear(pParse);
99921   sqlite3ExprCodeExprList(pParse, pOrderBy, regBase, 0);
99922   sqlite3VdbeAddOp2(v, OP_Sequence, pOrderBy->iECursor, regBase+nExpr);
99923   sqlite3ExprCodeMove(pParse, regData, regBase+nExpr+1, 1);
99924   sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nExpr + 2, regRecord);
99925   if( pSelect->selFlags & SF_UseSorter ){
99926     op = OP_SorterInsert;
99927   }else{
99928     op = OP_IdxInsert;
99929   }
99930   sqlite3VdbeAddOp2(v, op, pOrderBy->iECursor, regRecord);
99931   sqlite3ReleaseTempReg(pParse, regRecord);
99932   sqlite3ReleaseTempRange(pParse, regBase, nExpr+2);
99933   if( pSelect->iLimit ){
99934     int addr1, addr2;
99935     int iLimit;
99936     if( pSelect->iOffset ){
99937       iLimit = pSelect->iOffset+1;
99938     }else{
99939       iLimit = pSelect->iLimit;
99940     }
99941     addr1 = sqlite3VdbeAddOp1(v, OP_IfZero, iLimit);
99942     sqlite3VdbeAddOp2(v, OP_AddImm, iLimit, -1);
99943     addr2 = sqlite3VdbeAddOp0(v, OP_Goto);
99944     sqlite3VdbeJumpHere(v, addr1);
99945     sqlite3VdbeAddOp1(v, OP_Last, pOrderBy->iECursor);
99946     sqlite3VdbeAddOp1(v, OP_Delete, pOrderBy->iECursor);
99947     sqlite3VdbeJumpHere(v, addr2);
99948   }
99949 }
99950 
99951 /*
99952 ** Add code to implement the OFFSET
99953 */
99954 static void codeOffset(
99955   Vdbe *v,          /* Generate code into this VM */
99956   int iOffset,      /* Register holding the offset counter */
99957   int iContinue     /* Jump here to skip the current record */
99958 ){
99959   if( iOffset>0 && iContinue!=0 ){
99960     int addr;
99961     sqlite3VdbeAddOp2(v, OP_AddImm, iOffset, -1);
99962     addr = sqlite3VdbeAddOp1(v, OP_IfNeg, iOffset);
99963     sqlite3VdbeAddOp2(v, OP_Goto, 0, iContinue);
99964     VdbeComment((v, "skip OFFSET records"));
99965     sqlite3VdbeJumpHere(v, addr);
99966   }
99967 }
99968 
99969 /*
99970 ** Add code that will check to make sure the N registers starting at iMem
99971 ** form a distinct entry.  iTab is a sorting index that holds previously
99972 ** seen combinations of the N values.  A new entry is made in iTab
99973 ** if the current N values are new.
99974 **
99975 ** A jump to addrRepeat is made and the N+1 values are popped from the
99976 ** stack if the top N elements are not distinct.
99977 */
99978 static void codeDistinct(
99979   Parse *pParse,     /* Parsing and code generating context */
99980   int iTab,          /* A sorting index used to test for distinctness */
99981   int addrRepeat,    /* Jump to here if not distinct */
99982   int N,             /* Number of elements */
99983   int iMem           /* First element */
99984 ){
99985   Vdbe *v;
99986   int r1;
99987 
99988   v = pParse->pVdbe;
99989   r1 = sqlite3GetTempReg(pParse);
99990   sqlite3VdbeAddOp4Int(v, OP_Found, iTab, addrRepeat, iMem, N);
99991   sqlite3VdbeAddOp3(v, OP_MakeRecord, iMem, N, r1);
99992   sqlite3VdbeAddOp2(v, OP_IdxInsert, iTab, r1);
99993   sqlite3ReleaseTempReg(pParse, r1);
99994 }
99995 
99996 #ifndef SQLITE_OMIT_SUBQUERY
99997 /*
99998 ** Generate an error message when a SELECT is used within a subexpression
99999 ** (example:  "a IN (SELECT * FROM table)") but it has more than 1 result
100000 ** column.  We do this in a subroutine because the error used to occur
100001 ** in multiple places.  (The error only occurs in one place now, but we
100002 ** retain the subroutine to minimize code disruption.)
100003 */
100004 static int checkForMultiColumnSelectError(
100005   Parse *pParse,       /* Parse context. */
100006   SelectDest *pDest,   /* Destination of SELECT results */
100007   int nExpr            /* Number of result columns returned by SELECT */
100008 ){
100009   int eDest = pDest->eDest;
100010   if( nExpr>1 && (eDest==SRT_Mem || eDest==SRT_Set) ){
100011     sqlite3ErrorMsg(pParse, "only a single result allowed for "
100012        "a SELECT that is part of an expression");
100013     return 1;
100014   }else{
100015     return 0;
100016   }
100017 }
100018 #endif
100019 
100020 /*
100021 ** An instance of the following object is used to record information about
100022 ** how to process the DISTINCT keyword, to simplify passing that information
100023 ** into the selectInnerLoop() routine.
100024 */
100025 typedef struct DistinctCtx DistinctCtx;
100026 struct DistinctCtx {
100027   u8 isTnct;      /* True if the DISTINCT keyword is present */
100028   u8 eTnctType;   /* One of the WHERE_DISTINCT_* operators */
100029   int tabTnct;    /* Ephemeral table used for DISTINCT processing */
100030   int addrTnct;   /* Address of OP_OpenEphemeral opcode for tabTnct */
100031 };
100032 
100033 /*
100034 ** This routine generates the code for the inside of the inner loop
100035 ** of a SELECT.
100036 **
100037 ** If srcTab is negative, then the pEList expressions
100038 ** are evaluated in order to get the data for this row.  If srcTab is
100039 ** zero or more, then data is pulled from srcTab and pEList is used only
100040 ** to get number columns and the datatype for each column.
100041 */
100042 static void selectInnerLoop(
100043   Parse *pParse,          /* The parser context */
100044   Select *p,              /* The complete select statement being coded */
100045   ExprList *pEList,       /* List of values being extracted */
100046   int srcTab,             /* Pull data from this table */
100047   ExprList *pOrderBy,     /* If not NULL, sort results using this key */
100048   DistinctCtx *pDistinct, /* If not NULL, info on how to process DISTINCT */
100049   SelectDest *pDest,      /* How to dispose of the results */
100050   int iContinue,          /* Jump here to continue with next row */
100051   int iBreak              /* Jump here to break out of the inner loop */
100052 ){
100053   Vdbe *v = pParse->pVdbe;
100054   int i;
100055   int hasDistinct;        /* True if the DISTINCT keyword is present */
100056   int regResult;              /* Start of memory holding result set */
100057   int eDest = pDest->eDest;   /* How to dispose of results */
100058   int iParm = pDest->iSDParm; /* First argument to disposal method */
100059   int nResultCol;             /* Number of result columns */
100060 
100061   assert( v );
100062   assert( pEList!=0 );
100063   hasDistinct = pDistinct ? pDistinct->eTnctType : WHERE_DISTINCT_NOOP;
100064   if( pOrderBy==0 && !hasDistinct ){
100065     codeOffset(v, p->iOffset, iContinue);
100066   }
100067 
100068   /* Pull the requested columns.
100069   */
100070   nResultCol = pEList->nExpr;
100071   if( pDest->iSdst==0 ){
100072     pDest->iSdst = pParse->nMem+1;
100073     pDest->nSdst = nResultCol;
100074     pParse->nMem += nResultCol;
100075   }else{
100076     assert( pDest->nSdst==nResultCol );
100077   }
100078   regResult = pDest->iSdst;
100079   if( srcTab>=0 ){
100080     for(i=0; i<nResultCol; i++){
100081       sqlite3VdbeAddOp3(v, OP_Column, srcTab, i, regResult+i);
100082       VdbeComment((v, "%s", pEList->a[i].zName));
100083     }
100084   }else if( eDest!=SRT_Exists ){
100085     /* If the destination is an EXISTS(...) expression, the actual
100086     ** values returned by the SELECT are not required.
100087     */
100088     sqlite3ExprCodeExprList(pParse, pEList, regResult,
100089                             (eDest==SRT_Output)?SQLITE_ECEL_DUP:0);
100090   }
100091 
100092   /* If the DISTINCT keyword was present on the SELECT statement
100093   ** and this row has been seen before, then do not make this row
100094   ** part of the result.
100095   */
100096   if( hasDistinct ){
100097     switch( pDistinct->eTnctType ){
100098       case WHERE_DISTINCT_ORDERED: {
100099         VdbeOp *pOp;            /* No longer required OpenEphemeral instr. */
100100         int iJump;              /* Jump destination */
100101         int regPrev;            /* Previous row content */
100102 
100103         /* Allocate space for the previous row */
100104         regPrev = pParse->nMem+1;
100105         pParse->nMem += nResultCol;
100106 
100107         /* Change the OP_OpenEphemeral coded earlier to an OP_Null
100108         ** sets the MEM_Cleared bit on the first register of the
100109         ** previous value.  This will cause the OP_Ne below to always
100110         ** fail on the first iteration of the loop even if the first
100111         ** row is all NULLs.
100112         */
100113         sqlite3VdbeChangeToNoop(v, pDistinct->addrTnct);
100114         pOp = sqlite3VdbeGetOp(v, pDistinct->addrTnct);
100115         pOp->opcode = OP_Null;
100116         pOp->p1 = 1;
100117         pOp->p2 = regPrev;
100118 
100119         iJump = sqlite3VdbeCurrentAddr(v) + nResultCol;
100120         for(i=0; i<nResultCol; i++){
100121           CollSeq *pColl = sqlite3ExprCollSeq(pParse, pEList->a[i].pExpr);
100122           if( i<nResultCol-1 ){
100123             sqlite3VdbeAddOp3(v, OP_Ne, regResult+i, iJump, regPrev+i);
100124           }else{
100125             sqlite3VdbeAddOp3(v, OP_Eq, regResult+i, iContinue, regPrev+i);
100126           }
100127           sqlite3VdbeChangeP4(v, -1, (const char *)pColl, P4_COLLSEQ);
100128           sqlite3VdbeChangeP5(v, SQLITE_NULLEQ);
100129         }
100130         assert( sqlite3VdbeCurrentAddr(v)==iJump );
100131         sqlite3VdbeAddOp3(v, OP_Copy, regResult, regPrev, nResultCol-1);
100132         break;
100133       }
100134 
100135       case WHERE_DISTINCT_UNIQUE: {
100136         sqlite3VdbeChangeToNoop(v, pDistinct->addrTnct);
100137         break;
100138       }
100139 
100140       default: {
100141         assert( pDistinct->eTnctType==WHERE_DISTINCT_UNORDERED );
100142         codeDistinct(pParse, pDistinct->tabTnct, iContinue, nResultCol, regResult);
100143         break;
100144       }
100145     }
100146     if( pOrderBy==0 ){
100147       codeOffset(v, p->iOffset, iContinue);
100148     }
100149   }
100150 
100151   switch( eDest ){
100152     /* In this mode, write each query result to the key of the temporary
100153     ** table iParm.
100154     */
100155 #ifndef SQLITE_OMIT_COMPOUND_SELECT
100156     case SRT_Union: {
100157       int r1;
100158       r1 = sqlite3GetTempReg(pParse);
100159       sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nResultCol, r1);
100160       sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm, r1);
100161       sqlite3ReleaseTempReg(pParse, r1);
100162       break;
100163     }
100164 
100165     /* Construct a record from the query result, but instead of
100166     ** saving that record, use it as a key to delete elements from
100167     ** the temporary table iParm.
100168     */
100169     case SRT_Except: {
100170       sqlite3VdbeAddOp3(v, OP_IdxDelete, iParm, regResult, nResultCol);
100171       break;
100172     }
100173 #endif /* SQLITE_OMIT_COMPOUND_SELECT */
100174 
100175     /* Store the result as data using a unique key.
100176     */
100177     case SRT_DistTable:
100178     case SRT_Table:
100179     case SRT_EphemTab: {
100180       int r1 = sqlite3GetTempReg(pParse);
100181       testcase( eDest==SRT_Table );
100182       testcase( eDest==SRT_EphemTab );
100183       sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nResultCol, r1);
100184 #ifndef SQLITE_OMIT_CTE
100185       if( eDest==SRT_DistTable ){
100186         /* If the destination is DistTable, then cursor (iParm+1) is open
100187         ** on an ephemeral index. If the current row is already present
100188         ** in the index, do not write it to the output. If not, add the
100189         ** current row to the index and proceed with writing it to the
100190         ** output table as well.  */
100191         int addr = sqlite3VdbeCurrentAddr(v) + 4;
100192         sqlite3VdbeAddOp4Int(v, OP_Found, iParm+1, addr, r1, 0);
100193         sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm+1, r1);
100194         assert( pOrderBy==0 );
100195       }
100196 #endif
100197       if( pOrderBy ){
100198         pushOntoSorter(pParse, pOrderBy, p, r1);
100199       }else{
100200         int r2 = sqlite3GetTempReg(pParse);
100201         sqlite3VdbeAddOp2(v, OP_NewRowid, iParm, r2);
100202         sqlite3VdbeAddOp3(v, OP_Insert, iParm, r1, r2);
100203         sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
100204         sqlite3ReleaseTempReg(pParse, r2);
100205       }
100206       sqlite3ReleaseTempReg(pParse, r1);
100207       break;
100208     }
100209 
100210 #ifndef SQLITE_OMIT_SUBQUERY
100211     /* If we are creating a set for an "expr IN (SELECT ...)" construct,
100212     ** then there should be a single item on the stack.  Write this
100213     ** item into the set table with bogus data.
100214     */
100215     case SRT_Set: {
100216       assert( nResultCol==1 );
100217       pDest->affSdst =
100218                   sqlite3CompareAffinity(pEList->a[0].pExpr, pDest->affSdst);
100219       if( pOrderBy ){
100220         /* At first glance you would think we could optimize out the
100221         ** ORDER BY in this case since the order of entries in the set
100222         ** does not matter.  But there might be a LIMIT clause, in which
100223         ** case the order does matter */
100224         pushOntoSorter(pParse, pOrderBy, p, regResult);
100225       }else{
100226         int r1 = sqlite3GetTempReg(pParse);
100227         sqlite3VdbeAddOp4(v, OP_MakeRecord, regResult,1,r1, &pDest->affSdst, 1);
100228         sqlite3ExprCacheAffinityChange(pParse, regResult, 1);
100229         sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm, r1);
100230         sqlite3ReleaseTempReg(pParse, r1);
100231       }
100232       break;
100233     }
100234 
100235     /* If any row exist in the result set, record that fact and abort.
100236     */
100237     case SRT_Exists: {
100238       sqlite3VdbeAddOp2(v, OP_Integer, 1, iParm);
100239       /* The LIMIT clause will terminate the loop for us */
100240       break;
100241     }
100242 
100243     /* If this is a scalar select that is part of an expression, then
100244     ** store the results in the appropriate memory cell and break out
100245     ** of the scan loop.
100246     */
100247     case SRT_Mem: {
100248       assert( nResultCol==1 );
100249       if( pOrderBy ){
100250         pushOntoSorter(pParse, pOrderBy, p, regResult);
100251       }else{
100252         sqlite3ExprCodeMove(pParse, regResult, iParm, 1);
100253         /* The LIMIT clause will jump out of the loop for us */
100254       }
100255       break;
100256     }
100257 #endif /* #ifndef SQLITE_OMIT_SUBQUERY */
100258 
100259     /* Send the data to the callback function or to a subroutine.  In the
100260     ** case of a subroutine, the subroutine itself is responsible for
100261     ** popping the data from the stack.
100262     */
100263     case SRT_Coroutine:
100264     case SRT_Output: {
100265       testcase( eDest==SRT_Coroutine );
100266       testcase( eDest==SRT_Output );
100267       if( pOrderBy ){
100268         int r1 = sqlite3GetTempReg(pParse);
100269         sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nResultCol, r1);
100270         pushOntoSorter(pParse, pOrderBy, p, r1);
100271         sqlite3ReleaseTempReg(pParse, r1);
100272       }else if( eDest==SRT_Coroutine ){
100273         sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm);
100274       }else{
100275         sqlite3VdbeAddOp2(v, OP_ResultRow, regResult, nResultCol);
100276         sqlite3ExprCacheAffinityChange(pParse, regResult, nResultCol);
100277       }
100278       break;
100279     }
100280 
100281 #ifndef SQLITE_OMIT_CTE
100282     /* Write the results into a priority queue that is order according to
100283     ** pDest->pOrderBy (in pSO).  pDest->iSDParm (in iParm) is the cursor for an
100284     ** index with pSO->nExpr+2 columns.  Build a key using pSO for the first
100285     ** pSO->nExpr columns, then make sure all keys are unique by adding a
100286     ** final OP_Sequence column.  The last column is the record as a blob.
100287     */
100288     case SRT_DistQueue:
100289     case SRT_Queue: {
100290       int nKey;
100291       int r1, r2, r3;
100292       int addrTest = 0;
100293       ExprList *pSO;
100294       pSO = pDest->pOrderBy;
100295       assert( pSO );
100296       nKey = pSO->nExpr;
100297       r1 = sqlite3GetTempReg(pParse);
100298       r2 = sqlite3GetTempRange(pParse, nKey+2);
100299       r3 = r2+nKey+1;
100300       sqlite3VdbeAddOp3(v, OP_MakeRecord, regResult, nResultCol, r3);
100301       if( eDest==SRT_DistQueue ){
100302         /* If the destination is DistQueue, then cursor (iParm+1) is open
100303         ** on a second ephemeral index that holds all values every previously
100304         ** added to the queue.  Only add this new value if it has never before
100305         ** been added */
100306         addrTest = sqlite3VdbeAddOp4Int(v, OP_Found, iParm+1, 0, r3, 0);
100307         sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm+1, r3);
100308         sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
100309       }
100310       for(i=0; i<nKey; i++){
100311         sqlite3VdbeAddOp2(v, OP_SCopy,
100312                           regResult + pSO->a[i].u.x.iOrderByCol - 1,
100313                           r2+i);
100314       }
100315       sqlite3VdbeAddOp2(v, OP_Sequence, iParm, r2+nKey);
100316       sqlite3VdbeAddOp3(v, OP_MakeRecord, r2, nKey+2, r1);
100317       sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm, r1);
100318       if( addrTest ) sqlite3VdbeJumpHere(v, addrTest);
100319       sqlite3ReleaseTempReg(pParse, r1);
100320       sqlite3ReleaseTempRange(pParse, r2, nKey+2);
100321       break;
100322     }
100323 #endif /* SQLITE_OMIT_CTE */
100324 
100325 
100326 
100327 #if !defined(SQLITE_OMIT_TRIGGER)
100328     /* Discard the results.  This is used for SELECT statements inside
100329     ** the body of a TRIGGER.  The purpose of such selects is to call
100330     ** user-defined functions that have side effects.  We do not care
100331     ** about the actual results of the select.
100332     */
100333     default: {
100334       assert( eDest==SRT_Discard );
100335       break;
100336     }
100337 #endif
100338   }
100339 
100340   /* Jump to the end of the loop if the LIMIT is reached.  Except, if
100341   ** there is a sorter, in which case the sorter has already limited
100342   ** the output for us.
100343   */
100344   if( pOrderBy==0 && p->iLimit ){
100345     sqlite3VdbeAddOp3(v, OP_IfZero, p->iLimit, iBreak, -1);
100346   }
100347 }
100348 
100349 /*
100350 ** Allocate a KeyInfo object sufficient for an index of N key columns and
100351 ** X extra columns.
100352 */
100353 SQLITE_PRIVATE KeyInfo *sqlite3KeyInfoAlloc(sqlite3 *db, int N, int X){
100354   KeyInfo *p = sqlite3DbMallocZero(0,
100355                    sizeof(KeyInfo) + (N+X)*(sizeof(CollSeq*)+1));
100356   if( p ){
100357     p->aSortOrder = (u8*)&p->aColl[N+X];
100358     p->nField = (u16)N;
100359     p->nXField = (u16)X;
100360     p->enc = ENC(db);
100361     p->db = db;
100362     p->nRef = 1;
100363   }else{
100364     db->mallocFailed = 1;
100365   }
100366   return p;
100367 }
100368 
100369 /*
100370 ** Deallocate a KeyInfo object
100371 */
100372 SQLITE_PRIVATE void sqlite3KeyInfoUnref(KeyInfo *p){
100373   if( p ){
100374     assert( p->nRef>0 );
100375     p->nRef--;
100376     if( p->nRef==0 ) sqlite3DbFree(0, p);
100377   }
100378 }
100379 
100380 /*
100381 ** Make a new pointer to a KeyInfo object
100382 */
100383 SQLITE_PRIVATE KeyInfo *sqlite3KeyInfoRef(KeyInfo *p){
100384   if( p ){
100385     assert( p->nRef>0 );
100386     p->nRef++;
100387   }
100388   return p;
100389 }
100390 
100391 #ifdef SQLITE_DEBUG
100392 /*
100393 ** Return TRUE if a KeyInfo object can be change.  The KeyInfo object
100394 ** can only be changed if this is just a single reference to the object.
100395 **
100396 ** This routine is used only inside of assert() statements.
100397 */
100398 SQLITE_PRIVATE int sqlite3KeyInfoIsWriteable(KeyInfo *p){ return p->nRef==1; }
100399 #endif /* SQLITE_DEBUG */
100400 
100401 /*
100402 ** Given an expression list, generate a KeyInfo structure that records
100403 ** the collating sequence for each expression in that expression list.
100404 **
100405 ** If the ExprList is an ORDER BY or GROUP BY clause then the resulting
100406 ** KeyInfo structure is appropriate for initializing a virtual index to
100407 ** implement that clause.  If the ExprList is the result set of a SELECT
100408 ** then the KeyInfo structure is appropriate for initializing a virtual
100409 ** index to implement a DISTINCT test.
100410 **
100411 ** Space to hold the KeyInfo structure is obtain from malloc.  The calling
100412 ** function is responsible for seeing that this structure is eventually
100413 ** freed.
100414 */
100415 static KeyInfo *keyInfoFromExprList(Parse *pParse, ExprList *pList, int nExtra){
100416   int nExpr;
100417   KeyInfo *pInfo;
100418   struct ExprList_item *pItem;
100419   sqlite3 *db = pParse->db;
100420   int i;
100421 
100422   nExpr = pList->nExpr;
100423   pInfo = sqlite3KeyInfoAlloc(db, nExpr+nExtra, 1);
100424   if( pInfo ){
100425     assert( sqlite3KeyInfoIsWriteable(pInfo) );
100426     for(i=0, pItem=pList->a; i<nExpr; i++, pItem++){
100427       CollSeq *pColl;
100428       pColl = sqlite3ExprCollSeq(pParse, pItem->pExpr);
100429       if( !pColl ) pColl = db->pDfltColl;
100430       pInfo->aColl[i] = pColl;
100431       pInfo->aSortOrder[i] = pItem->sortOrder;
100432     }
100433   }
100434   return pInfo;
100435 }
100436 
100437 #ifndef SQLITE_OMIT_COMPOUND_SELECT
100438 /*
100439 ** Name of the connection operator, used for error messages.
100440 */
100441 static const char *selectOpName(int id){
100442   char *z;
100443   switch( id ){
100444     case TK_ALL:       z = "UNION ALL";   break;
100445     case TK_INTERSECT: z = "INTERSECT";   break;
100446     case TK_EXCEPT:    z = "EXCEPT";      break;
100447     default:           z = "UNION";       break;
100448   }
100449   return z;
100450 }
100451 #endif /* SQLITE_OMIT_COMPOUND_SELECT */
100452 
100453 #ifndef SQLITE_OMIT_EXPLAIN
100454 /*
100455 ** Unless an "EXPLAIN QUERY PLAN" command is being processed, this function
100456 ** is a no-op. Otherwise, it adds a single row of output to the EQP result,
100457 ** where the caption is of the form:
100458 **
100459 **   "USE TEMP B-TREE FOR xxx"
100460 **
100461 ** where xxx is one of "DISTINCT", "ORDER BY" or "GROUP BY". Exactly which
100462 ** is determined by the zUsage argument.
100463 */
100464 static void explainTempTable(Parse *pParse, const char *zUsage){
100465   if( pParse->explain==2 ){
100466     Vdbe *v = pParse->pVdbe;
100467     char *zMsg = sqlite3MPrintf(pParse->db, "USE TEMP B-TREE FOR %s", zUsage);
100468     sqlite3VdbeAddOp4(v, OP_Explain, pParse->iSelectId, 0, 0, zMsg, P4_DYNAMIC);
100469   }
100470 }
100471 
100472 /*
100473 ** Assign expression b to lvalue a. A second, no-op, version of this macro
100474 ** is provided when SQLITE_OMIT_EXPLAIN is defined. This allows the code
100475 ** in sqlite3Select() to assign values to structure member variables that
100476 ** only exist if SQLITE_OMIT_EXPLAIN is not defined without polluting the
100477 ** code with #ifndef directives.
100478 */
100479 # define explainSetInteger(a, b) a = b
100480 
100481 #else
100482 /* No-op versions of the explainXXX() functions and macros. */
100483 # define explainTempTable(y,z)
100484 # define explainSetInteger(y,z)
100485 #endif
100486 
100487 #if !defined(SQLITE_OMIT_EXPLAIN) && !defined(SQLITE_OMIT_COMPOUND_SELECT)
100488 /*
100489 ** Unless an "EXPLAIN QUERY PLAN" command is being processed, this function
100490 ** is a no-op. Otherwise, it adds a single row of output to the EQP result,
100491 ** where the caption is of one of the two forms:
100492 **
100493 **   "COMPOSITE SUBQUERIES iSub1 and iSub2 (op)"
100494 **   "COMPOSITE SUBQUERIES iSub1 and iSub2 USING TEMP B-TREE (op)"
100495 **
100496 ** where iSub1 and iSub2 are the integers passed as the corresponding
100497 ** function parameters, and op is the text representation of the parameter
100498 ** of the same name. The parameter "op" must be one of TK_UNION, TK_EXCEPT,
100499 ** TK_INTERSECT or TK_ALL. The first form is used if argument bUseTmp is
100500 ** false, or the second form if it is true.
100501 */
100502 static void explainComposite(
100503   Parse *pParse,                  /* Parse context */
100504   int op,                         /* One of TK_UNION, TK_EXCEPT etc. */
100505   int iSub1,                      /* Subquery id 1 */
100506   int iSub2,                      /* Subquery id 2 */
100507   int bUseTmp                     /* True if a temp table was used */
100508 ){
100509   assert( op==TK_UNION || op==TK_EXCEPT || op==TK_INTERSECT || op==TK_ALL );
100510   if( pParse->explain==2 ){
100511     Vdbe *v = pParse->pVdbe;
100512     char *zMsg = sqlite3MPrintf(
100513         pParse->db, "COMPOUND SUBQUERIES %d AND %d %s(%s)", iSub1, iSub2,
100514         bUseTmp?"USING TEMP B-TREE ":"", selectOpName(op)
100515     );
100516     sqlite3VdbeAddOp4(v, OP_Explain, pParse->iSelectId, 0, 0, zMsg, P4_DYNAMIC);
100517   }
100518 }
100519 #else
100520 /* No-op versions of the explainXXX() functions and macros. */
100521 # define explainComposite(v,w,x,y,z)
100522 #endif
100523 
100524 /*
100525 ** If the inner loop was generated using a non-null pOrderBy argument,
100526 ** then the results were placed in a sorter.  After the loop is terminated
100527 ** we need to run the sorter and output the results.  The following
100528 ** routine generates the code needed to do that.
100529 */
100530 static void generateSortTail(
100531   Parse *pParse,    /* Parsing context */
100532   Select *p,        /* The SELECT statement */
100533   Vdbe *v,          /* Generate code into this VDBE */
100534   int nColumn,      /* Number of columns of data */
100535   SelectDest *pDest /* Write the sorted results here */
100536 ){
100537   int addrBreak = sqlite3VdbeMakeLabel(v);     /* Jump here to exit loop */
100538   int addrContinue = sqlite3VdbeMakeLabel(v);  /* Jump here for next cycle */
100539   int addr;
100540   int iTab;
100541   int pseudoTab = 0;
100542   ExprList *pOrderBy = p->pOrderBy;
100543 
100544   int eDest = pDest->eDest;
100545   int iParm = pDest->iSDParm;
100546 
100547   int regRow;
100548   int regRowid;
100549 
100550   iTab = pOrderBy->iECursor;
100551   regRow = sqlite3GetTempReg(pParse);
100552   if( eDest==SRT_Output || eDest==SRT_Coroutine ){
100553     pseudoTab = pParse->nTab++;
100554     sqlite3VdbeAddOp3(v, OP_OpenPseudo, pseudoTab, regRow, nColumn);
100555     regRowid = 0;
100556   }else{
100557     regRowid = sqlite3GetTempReg(pParse);
100558   }
100559   if( p->selFlags & SF_UseSorter ){
100560     int regSortOut = ++pParse->nMem;
100561     int ptab2 = pParse->nTab++;
100562     sqlite3VdbeAddOp3(v, OP_OpenPseudo, ptab2, regSortOut, pOrderBy->nExpr+2);
100563     addr = 1 + sqlite3VdbeAddOp2(v, OP_SorterSort, iTab, addrBreak);
100564     codeOffset(v, p->iOffset, addrContinue);
100565     sqlite3VdbeAddOp2(v, OP_SorterData, iTab, regSortOut);
100566     sqlite3VdbeAddOp3(v, OP_Column, ptab2, pOrderBy->nExpr+1, regRow);
100567     sqlite3VdbeChangeP5(v, OPFLAG_CLEARCACHE);
100568   }else{
100569     addr = 1 + sqlite3VdbeAddOp2(v, OP_Sort, iTab, addrBreak);
100570     codeOffset(v, p->iOffset, addrContinue);
100571     sqlite3VdbeAddOp3(v, OP_Column, iTab, pOrderBy->nExpr+1, regRow);
100572   }
100573   switch( eDest ){
100574     case SRT_Table:
100575     case SRT_EphemTab: {
100576       testcase( eDest==SRT_Table );
100577       testcase( eDest==SRT_EphemTab );
100578       sqlite3VdbeAddOp2(v, OP_NewRowid, iParm, regRowid);
100579       sqlite3VdbeAddOp3(v, OP_Insert, iParm, regRow, regRowid);
100580       sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
100581       break;
100582     }
100583 #ifndef SQLITE_OMIT_SUBQUERY
100584     case SRT_Set: {
100585       assert( nColumn==1 );
100586       sqlite3VdbeAddOp4(v, OP_MakeRecord, regRow, 1, regRowid,
100587                         &pDest->affSdst, 1);
100588       sqlite3ExprCacheAffinityChange(pParse, regRow, 1);
100589       sqlite3VdbeAddOp2(v, OP_IdxInsert, iParm, regRowid);
100590       break;
100591     }
100592     case SRT_Mem: {
100593       assert( nColumn==1 );
100594       sqlite3ExprCodeMove(pParse, regRow, iParm, 1);
100595       /* The LIMIT clause will terminate the loop for us */
100596       break;
100597     }
100598 #endif
100599     default: {
100600       int i;
100601       assert( eDest==SRT_Output || eDest==SRT_Coroutine );
100602       testcase( eDest==SRT_Output );
100603       testcase( eDest==SRT_Coroutine );
100604       for(i=0; i<nColumn; i++){
100605         assert( regRow!=pDest->iSdst+i );
100606         sqlite3VdbeAddOp3(v, OP_Column, pseudoTab, i, pDest->iSdst+i);
100607         if( i==0 ){
100608           sqlite3VdbeChangeP5(v, OPFLAG_CLEARCACHE);
100609         }
100610       }
100611       if( eDest==SRT_Output ){
100612         sqlite3VdbeAddOp2(v, OP_ResultRow, pDest->iSdst, nColumn);
100613         sqlite3ExprCacheAffinityChange(pParse, pDest->iSdst, nColumn);
100614       }else{
100615         sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm);
100616       }
100617       break;
100618     }
100619   }
100620   sqlite3ReleaseTempReg(pParse, regRow);
100621   sqlite3ReleaseTempReg(pParse, regRowid);
100622 
100623   /* The bottom of the loop
100624   */
100625   sqlite3VdbeResolveLabel(v, addrContinue);
100626   if( p->selFlags & SF_UseSorter ){
100627     sqlite3VdbeAddOp2(v, OP_SorterNext, iTab, addr);
100628   }else{
100629     sqlite3VdbeAddOp2(v, OP_Next, iTab, addr);
100630   }
100631   sqlite3VdbeResolveLabel(v, addrBreak);
100632   if( eDest==SRT_Output || eDest==SRT_Coroutine ){
100633     sqlite3VdbeAddOp2(v, OP_Close, pseudoTab, 0);
100634   }
100635 }
100636 
100637 /*
100638 ** Return a pointer to a string containing the 'declaration type' of the
100639 ** expression pExpr. The string may be treated as static by the caller.
100640 **
100641 ** Also try to estimate the size of the returned value and return that
100642 ** result in *pEstWidth.
100643 **
100644 ** The declaration type is the exact datatype definition extracted from the
100645 ** original CREATE TABLE statement if the expression is a column. The
100646 ** declaration type for a ROWID field is INTEGER. Exactly when an expression
100647 ** is considered a column can be complex in the presence of subqueries. The
100648 ** result-set expression in all of the following SELECT statements is
100649 ** considered a column by this function.
100650 **
100651 **   SELECT col FROM tbl;
100652 **   SELECT (SELECT col FROM tbl;
100653 **   SELECT (SELECT col FROM tbl);
100654 **   SELECT abc FROM (SELECT col AS abc FROM tbl);
100655 **
100656 ** The declaration type for any expression other than a column is NULL.
100657 **
100658 ** This routine has either 3 or 6 parameters depending on whether or not
100659 ** the SQLITE_ENABLE_COLUMN_METADATA compile-time option is used.
100660 */
100661 #ifdef SQLITE_ENABLE_COLUMN_METADATA
100662 # define columnType(A,B,C,D,E,F) columnTypeImpl(A,B,C,D,E,F)
100663 static const char *columnTypeImpl(
100664   NameContext *pNC,
100665   Expr *pExpr,
100666   const char **pzOrigDb,
100667   const char **pzOrigTab,
100668   const char **pzOrigCol,
100669   u8 *pEstWidth
100670 ){
100671   char const *zOrigDb = 0;
100672   char const *zOrigTab = 0;
100673   char const *zOrigCol = 0;
100674 #else /* if !defined(SQLITE_ENABLE_COLUMN_METADATA) */
100675 # define columnType(A,B,C,D,E,F) columnTypeImpl(A,B,F)
100676 static const char *columnTypeImpl(
100677   NameContext *pNC,
100678   Expr *pExpr,
100679   u8 *pEstWidth
100680 ){
100681 #endif /* !defined(SQLITE_ENABLE_COLUMN_METADATA) */
100682   char const *zType = 0;
100683   int j;
100684   u8 estWidth = 1;
100685 
100686   if( NEVER(pExpr==0) || pNC->pSrcList==0 ) return 0;
100687   switch( pExpr->op ){
100688     case TK_AGG_COLUMN:
100689     case TK_COLUMN: {
100690       /* The expression is a column. Locate the table the column is being
100691       ** extracted from in NameContext.pSrcList. This table may be real
100692       ** database table or a subquery.
100693       */
100694       Table *pTab = 0;            /* Table structure column is extracted from */
100695       Select *pS = 0;             /* Select the column is extracted from */
100696       int iCol = pExpr->iColumn;  /* Index of column in pTab */
100697       testcase( pExpr->op==TK_AGG_COLUMN );
100698       testcase( pExpr->op==TK_COLUMN );
100699       while( pNC && !pTab ){
100700         SrcList *pTabList = pNC->pSrcList;
100701         for(j=0;j<pTabList->nSrc && pTabList->a[j].iCursor!=pExpr->iTable;j++);
100702         if( j<pTabList->nSrc ){
100703           pTab = pTabList->a[j].pTab;
100704           pS = pTabList->a[j].pSelect;
100705         }else{
100706           pNC = pNC->pNext;
100707         }
100708       }
100709 
100710       if( pTab==0 ){
100711         /* At one time, code such as "SELECT new.x" within a trigger would
100712         ** cause this condition to run.  Since then, we have restructured how
100713         ** trigger code is generated and so this condition is no longer
100714         ** possible. However, it can still be true for statements like
100715         ** the following:
100716         **
100717         **   CREATE TABLE t1(col INTEGER);
100718         **   SELECT (SELECT t1.col) FROM FROM t1;
100719         **
100720         ** when columnType() is called on the expression "t1.col" in the
100721         ** sub-select. In this case, set the column type to NULL, even
100722         ** though it should really be "INTEGER".
100723         **
100724         ** This is not a problem, as the column type of "t1.col" is never
100725         ** used. When columnType() is called on the expression
100726         ** "(SELECT t1.col)", the correct type is returned (see the TK_SELECT
100727         ** branch below.  */
100728         break;
100729       }
100730 
100731       assert( pTab && pExpr->pTab==pTab );
100732       if( pS ){
100733         /* The "table" is actually a sub-select or a view in the FROM clause
100734         ** of the SELECT statement. Return the declaration type and origin
100735         ** data for the result-set column of the sub-select.
100736         */
100737         if( iCol>=0 && ALWAYS(iCol<pS->pEList->nExpr) ){
100738           /* If iCol is less than zero, then the expression requests the
100739           ** rowid of the sub-select or view. This expression is legal (see
100740           ** test case misc2.2.2) - it always evaluates to NULL.
100741           */
100742           NameContext sNC;
100743           Expr *p = pS->pEList->a[iCol].pExpr;
100744           sNC.pSrcList = pS->pSrc;
100745           sNC.pNext = pNC;
100746           sNC.pParse = pNC->pParse;
100747           zType = columnType(&sNC, p,&zOrigDb,&zOrigTab,&zOrigCol, &estWidth);
100748         }
100749       }else if( pTab->pSchema ){
100750         /* A real table */
100751         assert( !pS );
100752         if( iCol<0 ) iCol = pTab->iPKey;
100753         assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) );
100754 #ifdef SQLITE_ENABLE_COLUMN_METADATA
100755         if( iCol<0 ){
100756           zType = "INTEGER";
100757           zOrigCol = "rowid";
100758         }else{
100759           zType = pTab->aCol[iCol].zType;
100760           zOrigCol = pTab->aCol[iCol].zName;
100761           estWidth = pTab->aCol[iCol].szEst;
100762         }
100763         zOrigTab = pTab->zName;
100764         if( pNC->pParse ){
100765           int iDb = sqlite3SchemaToIndex(pNC->pParse->db, pTab->pSchema);
100766           zOrigDb = pNC->pParse->db->aDb[iDb].zName;
100767         }
100768 #else
100769         if( iCol<0 ){
100770           zType = "INTEGER";
100771         }else{
100772           zType = pTab->aCol[iCol].zType;
100773           estWidth = pTab->aCol[iCol].szEst;
100774         }
100775 #endif
100776       }
100777       break;
100778     }
100779 #ifndef SQLITE_OMIT_SUBQUERY
100780     case TK_SELECT: {
100781       /* The expression is a sub-select. Return the declaration type and
100782       ** origin info for the single column in the result set of the SELECT
100783       ** statement.
100784       */
100785       NameContext sNC;
100786       Select *pS = pExpr->x.pSelect;
100787       Expr *p = pS->pEList->a[0].pExpr;
100788       assert( ExprHasProperty(pExpr, EP_xIsSelect) );
100789       sNC.pSrcList = pS->pSrc;
100790       sNC.pNext = pNC;
100791       sNC.pParse = pNC->pParse;
100792       zType = columnType(&sNC, p, &zOrigDb, &zOrigTab, &zOrigCol, &estWidth);
100793       break;
100794     }
100795 #endif
100796   }
100797 
100798 #ifdef SQLITE_ENABLE_COLUMN_METADATA
100799   if( pzOrigDb ){
100800     assert( pzOrigTab && pzOrigCol );
100801     *pzOrigDb = zOrigDb;
100802     *pzOrigTab = zOrigTab;
100803     *pzOrigCol = zOrigCol;
100804   }
100805 #endif
100806   if( pEstWidth ) *pEstWidth = estWidth;
100807   return zType;
100808 }
100809 
100810 /*
100811 ** Generate code that will tell the VDBE the declaration types of columns
100812 ** in the result set.
100813 */
100814 static void generateColumnTypes(
100815   Parse *pParse,      /* Parser context */
100816   SrcList *pTabList,  /* List of tables */
100817   ExprList *pEList    /* Expressions defining the result set */
100818 ){
100819 #ifndef SQLITE_OMIT_DECLTYPE
100820   Vdbe *v = pParse->pVdbe;
100821   int i;
100822   NameContext sNC;
100823   sNC.pSrcList = pTabList;
100824   sNC.pParse = pParse;
100825   for(i=0; i<pEList->nExpr; i++){
100826     Expr *p = pEList->a[i].pExpr;
100827     const char *zType;
100828 #ifdef SQLITE_ENABLE_COLUMN_METADATA
100829     const char *zOrigDb = 0;
100830     const char *zOrigTab = 0;
100831     const char *zOrigCol = 0;
100832     zType = columnType(&sNC, p, &zOrigDb, &zOrigTab, &zOrigCol, 0);
100833 
100834     /* The vdbe must make its own copy of the column-type and other
100835     ** column specific strings, in case the schema is reset before this
100836     ** virtual machine is deleted.
100837     */
100838     sqlite3VdbeSetColName(v, i, COLNAME_DATABASE, zOrigDb, SQLITE_TRANSIENT);
100839     sqlite3VdbeSetColName(v, i, COLNAME_TABLE, zOrigTab, SQLITE_TRANSIENT);
100840     sqlite3VdbeSetColName(v, i, COLNAME_COLUMN, zOrigCol, SQLITE_TRANSIENT);
100841 #else
100842     zType = columnType(&sNC, p, 0, 0, 0, 0);
100843 #endif
100844     sqlite3VdbeSetColName(v, i, COLNAME_DECLTYPE, zType, SQLITE_TRANSIENT);
100845   }
100846 #endif /* !defined(SQLITE_OMIT_DECLTYPE) */
100847 }
100848 
100849 /*
100850 ** Generate code that will tell the VDBE the names of columns
100851 ** in the result set.  This information is used to provide the
100852 ** azCol[] values in the callback.
100853 */
100854 static void generateColumnNames(
100855   Parse *pParse,      /* Parser context */
100856   SrcList *pTabList,  /* List of tables */
100857   ExprList *pEList    /* Expressions defining the result set */
100858 ){
100859   Vdbe *v = pParse->pVdbe;
100860   int i, j;
100861   sqlite3 *db = pParse->db;
100862   int fullNames, shortNames;
100863 
100864 #ifndef SQLITE_OMIT_EXPLAIN
100865   /* If this is an EXPLAIN, skip this step */
100866   if( pParse->explain ){
100867     return;
100868   }
100869 #endif
100870 
100871   if( pParse->colNamesSet || NEVER(v==0) || db->mallocFailed ) return;
100872   pParse->colNamesSet = 1;
100873   fullNames = (db->flags & SQLITE_FullColNames)!=0;
100874   shortNames = (db->flags & SQLITE_ShortColNames)!=0;
100875   sqlite3VdbeSetNumCols(v, pEList->nExpr);
100876   for(i=0; i<pEList->nExpr; i++){
100877     Expr *p;
100878     p = pEList->a[i].pExpr;
100879     if( NEVER(p==0) ) continue;
100880     if( pEList->a[i].zName ){
100881       char *zName = pEList->a[i].zName;
100882       sqlite3VdbeSetColName(v, i, COLNAME_NAME, zName, SQLITE_TRANSIENT);
100883     }else if( (p->op==TK_COLUMN || p->op==TK_AGG_COLUMN) && pTabList ){
100884       Table *pTab;
100885       char *zCol;
100886       int iCol = p->iColumn;
100887       for(j=0; ALWAYS(j<pTabList->nSrc); j++){
100888         if( pTabList->a[j].iCursor==p->iTable ) break;
100889       }
100890       assert( j<pTabList->nSrc );
100891       pTab = pTabList->a[j].pTab;
100892       if( iCol<0 ) iCol = pTab->iPKey;
100893       assert( iCol==-1 || (iCol>=0 && iCol<pTab->nCol) );
100894       if( iCol<0 ){
100895         zCol = "rowid";
100896       }else{
100897         zCol = pTab->aCol[iCol].zName;
100898       }
100899       if( !shortNames && !fullNames ){
100900         sqlite3VdbeSetColName(v, i, COLNAME_NAME,
100901             sqlite3DbStrDup(db, pEList->a[i].zSpan), SQLITE_DYNAMIC);
100902       }else if( fullNames ){
100903         char *zName = 0;
100904         zName = sqlite3MPrintf(db, "%s.%s", pTab->zName, zCol);
100905         sqlite3VdbeSetColName(v, i, COLNAME_NAME, zName, SQLITE_DYNAMIC);
100906       }else{
100907         sqlite3VdbeSetColName(v, i, COLNAME_NAME, zCol, SQLITE_TRANSIENT);
100908       }
100909     }else{
100910       const char *z = pEList->a[i].zSpan;
100911       z = z==0 ? sqlite3MPrintf(db, "column%d", i+1) : sqlite3DbStrDup(db, z);
100912       sqlite3VdbeSetColName(v, i, COLNAME_NAME, z, SQLITE_DYNAMIC);
100913     }
100914   }
100915   generateColumnTypes(pParse, pTabList, pEList);
100916 }
100917 
100918 /*
100919 ** Given a an expression list (which is really the list of expressions
100920 ** that form the result set of a SELECT statement) compute appropriate
100921 ** column names for a table that would hold the expression list.
100922 **
100923 ** All column names will be unique.
100924 **
100925 ** Only the column names are computed.  Column.zType, Column.zColl,
100926 ** and other fields of Column are zeroed.
100927 **
100928 ** Return SQLITE_OK on success.  If a memory allocation error occurs,
100929 ** store NULL in *paCol and 0 in *pnCol and return SQLITE_NOMEM.
100930 */
100931 static int selectColumnsFromExprList(
100932   Parse *pParse,          /* Parsing context */
100933   ExprList *pEList,       /* Expr list from which to derive column names */
100934   i16 *pnCol,             /* Write the number of columns here */
100935   Column **paCol          /* Write the new column list here */
100936 ){
100937   sqlite3 *db = pParse->db;   /* Database connection */
100938   int i, j;                   /* Loop counters */
100939   int cnt;                    /* Index added to make the name unique */
100940   Column *aCol, *pCol;        /* For looping over result columns */
100941   int nCol;                   /* Number of columns in the result set */
100942   Expr *p;                    /* Expression for a single result column */
100943   char *zName;                /* Column name */
100944   int nName;                  /* Size of name in zName[] */
100945 
100946   if( pEList ){
100947     nCol = pEList->nExpr;
100948     aCol = sqlite3DbMallocZero(db, sizeof(aCol[0])*nCol);
100949     testcase( aCol==0 );
100950   }else{
100951     nCol = 0;
100952     aCol = 0;
100953   }
100954   *pnCol = nCol;
100955   *paCol = aCol;
100956 
100957   for(i=0, pCol=aCol; i<nCol; i++, pCol++){
100958     /* Get an appropriate name for the column
100959     */
100960     p = sqlite3ExprSkipCollate(pEList->a[i].pExpr);
100961     if( (zName = pEList->a[i].zName)!=0 ){
100962       /* If the column contains an "AS <name>" phrase, use <name> as the name */
100963       zName = sqlite3DbStrDup(db, zName);
100964     }else{
100965       Expr *pColExpr = p;  /* The expression that is the result column name */
100966       Table *pTab;         /* Table associated with this expression */
100967       while( pColExpr->op==TK_DOT ){
100968         pColExpr = pColExpr->pRight;
100969         assert( pColExpr!=0 );
100970       }
100971       if( pColExpr->op==TK_COLUMN && ALWAYS(pColExpr->pTab!=0) ){
100972         /* For columns use the column name name */
100973         int iCol = pColExpr->iColumn;
100974         pTab = pColExpr->pTab;
100975         if( iCol<0 ) iCol = pTab->iPKey;
100976         zName = sqlite3MPrintf(db, "%s",
100977                  iCol>=0 ? pTab->aCol[iCol].zName : "rowid");
100978       }else if( pColExpr->op==TK_ID ){
100979         assert( !ExprHasProperty(pColExpr, EP_IntValue) );
100980         zName = sqlite3MPrintf(db, "%s", pColExpr->u.zToken);
100981       }else{
100982         /* Use the original text of the column expression as its name */
100983         zName = sqlite3MPrintf(db, "%s", pEList->a[i].zSpan);
100984       }
100985     }
100986     if( db->mallocFailed ){
100987       sqlite3DbFree(db, zName);
100988       break;
100989     }
100990 
100991     /* Make sure the column name is unique.  If the name is not unique,
100992     ** append a integer to the name so that it becomes unique.
100993     */
100994     nName = sqlite3Strlen30(zName);
100995     for(j=cnt=0; j<i; j++){
100996       if( sqlite3StrICmp(aCol[j].zName, zName)==0 ){
100997         char *zNewName;
100998         int k;
100999         for(k=nName-1; k>1 && sqlite3Isdigit(zName[k]); k--){}
101000         if( zName[k]==':' ) nName = k;
101001         zName[nName] = 0;
101002         zNewName = sqlite3MPrintf(db, "%s:%d", zName, ++cnt);
101003         sqlite3DbFree(db, zName);
101004         zName = zNewName;
101005         j = -1;
101006         if( zName==0 ) break;
101007       }
101008     }
101009     pCol->zName = zName;
101010   }
101011   if( db->mallocFailed ){
101012     for(j=0; j<i; j++){
101013       sqlite3DbFree(db, aCol[j].zName);
101014     }
101015     sqlite3DbFree(db, aCol);
101016     *paCol = 0;
101017     *pnCol = 0;
101018     return SQLITE_NOMEM;
101019   }
101020   return SQLITE_OK;
101021 }
101022 
101023 /*
101024 ** Add type and collation information to a column list based on
101025 ** a SELECT statement.
101026 **
101027 ** The column list presumably came from selectColumnNamesFromExprList().
101028 ** The column list has only names, not types or collations.  This
101029 ** routine goes through and adds the types and collations.
101030 **
101031 ** This routine requires that all identifiers in the SELECT
101032 ** statement be resolved.
101033 */
101034 static void selectAddColumnTypeAndCollation(
101035   Parse *pParse,        /* Parsing contexts */
101036   Table *pTab,          /* Add column type information to this table */
101037   Select *pSelect       /* SELECT used to determine types and collations */
101038 ){
101039   sqlite3 *db = pParse->db;
101040   NameContext sNC;
101041   Column *pCol;
101042   CollSeq *pColl;
101043   int i;
101044   Expr *p;
101045   struct ExprList_item *a;
101046   u64 szAll = 0;
101047 
101048   assert( pSelect!=0 );
101049   assert( (pSelect->selFlags & SF_Resolved)!=0 );
101050   assert( pTab->nCol==pSelect->pEList->nExpr || db->mallocFailed );
101051   if( db->mallocFailed ) return;
101052   memset(&sNC, 0, sizeof(sNC));
101053   sNC.pSrcList = pSelect->pSrc;
101054   a = pSelect->pEList->a;
101055   for(i=0, pCol=pTab->aCol; i<pTab->nCol; i++, pCol++){
101056     p = a[i].pExpr;
101057     pCol->zType = sqlite3DbStrDup(db, columnType(&sNC, p,0,0,0, &pCol->szEst));
101058     szAll += pCol->szEst;
101059     pCol->affinity = sqlite3ExprAffinity(p);
101060     if( pCol->affinity==0 ) pCol->affinity = SQLITE_AFF_NONE;
101061     pColl = sqlite3ExprCollSeq(pParse, p);
101062     if( pColl ){
101063       pCol->zColl = sqlite3DbStrDup(db, pColl->zName);
101064     }
101065   }
101066   pTab->szTabRow = sqlite3LogEst(szAll*4);
101067 }
101068 
101069 /*
101070 ** Given a SELECT statement, generate a Table structure that describes
101071 ** the result set of that SELECT.
101072 */
101073 SQLITE_PRIVATE Table *sqlite3ResultSetOfSelect(Parse *pParse, Select *pSelect){
101074   Table *pTab;
101075   sqlite3 *db = pParse->db;
101076   int savedFlags;
101077 
101078   savedFlags = db->flags;
101079   db->flags &= ~SQLITE_FullColNames;
101080   db->flags |= SQLITE_ShortColNames;
101081   sqlite3SelectPrep(pParse, pSelect, 0);
101082   if( pParse->nErr ) return 0;
101083   while( pSelect->pPrior ) pSelect = pSelect->pPrior;
101084   db->flags = savedFlags;
101085   pTab = sqlite3DbMallocZero(db, sizeof(Table) );
101086   if( pTab==0 ){
101087     return 0;
101088   }
101089   /* The sqlite3ResultSetOfSelect() is only used n contexts where lookaside
101090   ** is disabled */
101091   assert( db->lookaside.bEnabled==0 );
101092   pTab->nRef = 1;
101093   pTab->zName = 0;
101094   pTab->nRowEst = 1048576;
101095   selectColumnsFromExprList(pParse, pSelect->pEList, &pTab->nCol, &pTab->aCol);
101096   selectAddColumnTypeAndCollation(pParse, pTab, pSelect);
101097   pTab->iPKey = -1;
101098   if( db->mallocFailed ){
101099     sqlite3DeleteTable(db, pTab);
101100     return 0;
101101   }
101102   return pTab;
101103 }
101104 
101105 /*
101106 ** Get a VDBE for the given parser context.  Create a new one if necessary.
101107 ** If an error occurs, return NULL and leave a message in pParse.
101108 */
101109 SQLITE_PRIVATE Vdbe *sqlite3GetVdbe(Parse *pParse){
101110   Vdbe *v = pParse->pVdbe;
101111   if( v==0 ){
101112     v = pParse->pVdbe = sqlite3VdbeCreate(pParse);
101113 #ifndef SQLITE_OMIT_TRACE
101114     if( v ){
101115       sqlite3VdbeAddOp0(v, OP_Trace);
101116     }
101117 #endif
101118   }
101119   return v;
101120 }
101121 
101122 
101123 /*
101124 ** Compute the iLimit and iOffset fields of the SELECT based on the
101125 ** pLimit and pOffset expressions.  pLimit and pOffset hold the expressions
101126 ** that appear in the original SQL statement after the LIMIT and OFFSET
101127 ** keywords.  Or NULL if those keywords are omitted. iLimit and iOffset
101128 ** are the integer memory register numbers for counters used to compute
101129 ** the limit and offset.  If there is no limit and/or offset, then
101130 ** iLimit and iOffset are negative.
101131 **
101132 ** This routine changes the values of iLimit and iOffset only if
101133 ** a limit or offset is defined by pLimit and pOffset.  iLimit and
101134 ** iOffset should have been preset to appropriate default values (zero)
101135 ** prior to calling this routine.
101136 **
101137 ** The iOffset register (if it exists) is initialized to the value
101138 ** of the OFFSET.  The iLimit register is initialized to LIMIT.  Register
101139 ** iOffset+1 is initialized to LIMIT+OFFSET.
101140 **
101141 ** Only if pLimit!=0 or pOffset!=0 do the limit registers get
101142 ** redefined.  The UNION ALL operator uses this property to force
101143 ** the reuse of the same limit and offset registers across multiple
101144 ** SELECT statements.
101145 */
101146 static void computeLimitRegisters(Parse *pParse, Select *p, int iBreak){
101147   Vdbe *v = 0;
101148   int iLimit = 0;
101149   int iOffset;
101150   int addr1, n;
101151   if( p->iLimit ) return;
101152 
101153   /*
101154   ** "LIMIT -1" always shows all rows.  There is some
101155   ** controversy about what the correct behavior should be.
101156   ** The current implementation interprets "LIMIT 0" to mean
101157   ** no rows.
101158   */
101159   sqlite3ExprCacheClear(pParse);
101160   assert( p->pOffset==0 || p->pLimit!=0 );
101161   if( p->pLimit ){
101162     p->iLimit = iLimit = ++pParse->nMem;
101163     v = sqlite3GetVdbe(pParse);
101164     assert( v!=0 );
101165     if( sqlite3ExprIsInteger(p->pLimit, &n) ){
101166       sqlite3VdbeAddOp2(v, OP_Integer, n, iLimit);
101167       VdbeComment((v, "LIMIT counter"));
101168       if( n==0 ){
101169         sqlite3VdbeAddOp2(v, OP_Goto, 0, iBreak);
101170       }else if( n>=0 && p->nSelectRow>(u64)n ){
101171         p->nSelectRow = n;
101172       }
101173     }else{
101174       sqlite3ExprCode(pParse, p->pLimit, iLimit);
101175       sqlite3VdbeAddOp1(v, OP_MustBeInt, iLimit);
101176       VdbeComment((v, "LIMIT counter"));
101177       sqlite3VdbeAddOp2(v, OP_IfZero, iLimit, iBreak);
101178     }
101179     if( p->pOffset ){
101180       p->iOffset = iOffset = ++pParse->nMem;
101181       pParse->nMem++;   /* Allocate an extra register for limit+offset */
101182       sqlite3ExprCode(pParse, p->pOffset, iOffset);
101183       sqlite3VdbeAddOp1(v, OP_MustBeInt, iOffset);
101184       VdbeComment((v, "OFFSET counter"));
101185       addr1 = sqlite3VdbeAddOp1(v, OP_IfPos, iOffset);
101186       sqlite3VdbeAddOp2(v, OP_Integer, 0, iOffset);
101187       sqlite3VdbeJumpHere(v, addr1);
101188       sqlite3VdbeAddOp3(v, OP_Add, iLimit, iOffset, iOffset+1);
101189       VdbeComment((v, "LIMIT+OFFSET"));
101190       addr1 = sqlite3VdbeAddOp1(v, OP_IfPos, iLimit);
101191       sqlite3VdbeAddOp2(v, OP_Integer, -1, iOffset+1);
101192       sqlite3VdbeJumpHere(v, addr1);
101193     }
101194   }
101195 }
101196 
101197 #ifndef SQLITE_OMIT_COMPOUND_SELECT
101198 /*
101199 ** Return the appropriate collating sequence for the iCol-th column of
101200 ** the result set for the compound-select statement "p".  Return NULL if
101201 ** the column has no default collating sequence.
101202 **
101203 ** The collating sequence for the compound select is taken from the
101204 ** left-most term of the select that has a collating sequence.
101205 */
101206 static CollSeq *multiSelectCollSeq(Parse *pParse, Select *p, int iCol){
101207   CollSeq *pRet;
101208   if( p->pPrior ){
101209     pRet = multiSelectCollSeq(pParse, p->pPrior, iCol);
101210   }else{
101211     pRet = 0;
101212   }
101213   assert( iCol>=0 );
101214   if( pRet==0 && iCol<p->pEList->nExpr ){
101215     pRet = sqlite3ExprCollSeq(pParse, p->pEList->a[iCol].pExpr);
101216   }
101217   return pRet;
101218 }
101219 
101220 /*
101221 ** The select statement passed as the second parameter is a compound SELECT
101222 ** with an ORDER BY clause. This function allocates and returns a KeyInfo
101223 ** structure suitable for implementing the ORDER BY.
101224 **
101225 ** Space to hold the KeyInfo structure is obtained from malloc. The calling
101226 ** function is responsible for ensuring that this structure is eventually
101227 ** freed.
101228 */
101229 static KeyInfo *multiSelectOrderByKeyInfo(Parse *pParse, Select *p, int nExtra){
101230   ExprList *pOrderBy = p->pOrderBy;
101231   int nOrderBy = p->pOrderBy->nExpr;
101232   sqlite3 *db = pParse->db;
101233   KeyInfo *pRet = sqlite3KeyInfoAlloc(db, nOrderBy+nExtra, 1);
101234   if( pRet ){
101235     int i;
101236     for(i=0; i<nOrderBy; i++){
101237       struct ExprList_item *pItem = &pOrderBy->a[i];
101238       Expr *pTerm = pItem->pExpr;
101239       CollSeq *pColl;
101240 
101241       if( pTerm->flags & EP_Collate ){
101242         pColl = sqlite3ExprCollSeq(pParse, pTerm);
101243       }else{
101244         pColl = multiSelectCollSeq(pParse, p, pItem->u.x.iOrderByCol-1);
101245         if( pColl==0 ) pColl = db->pDfltColl;
101246         pOrderBy->a[i].pExpr =
101247           sqlite3ExprAddCollateString(pParse, pTerm, pColl->zName);
101248       }
101249       assert( sqlite3KeyInfoIsWriteable(pRet) );
101250       pRet->aColl[i] = pColl;
101251       pRet->aSortOrder[i] = pOrderBy->a[i].sortOrder;
101252     }
101253   }
101254 
101255   return pRet;
101256 }
101257 
101258 #ifndef SQLITE_OMIT_CTE
101259 /*
101260 ** This routine generates VDBE code to compute the content of a WITH RECURSIVE
101261 ** query of the form:
101262 **
101263 **   <recursive-table> AS (<setup-query> UNION [ALL] <recursive-query>)
101264 **                         \___________/             \_______________/
101265 **                           p->pPrior                      p
101266 **
101267 **
101268 ** There is exactly one reference to the recursive-table in the FROM clause
101269 ** of recursive-query, marked with the SrcList->a[].isRecursive flag.
101270 **
101271 ** The setup-query runs once to generate an initial set of rows that go
101272 ** into a Queue table.  Rows are extracted from the Queue table one by
101273 ** one.  Each row extracted from Queue is output to pDest.  Then the single
101274 ** extracted row (now in the iCurrent table) becomes the content of the
101275 ** recursive-table for a recursive-query run.  The output of the recursive-query
101276 ** is added back into the Queue table.  Then another row is extracted from Queue
101277 ** and the iteration continues until the Queue table is empty.
101278 **
101279 ** If the compound query operator is UNION then no duplicate rows are ever
101280 ** inserted into the Queue table.  The iDistinct table keeps a copy of all rows
101281 ** that have ever been inserted into Queue and causes duplicates to be
101282 ** discarded.  If the operator is UNION ALL, then duplicates are allowed.
101283 **
101284 ** If the query has an ORDER BY, then entries in the Queue table are kept in
101285 ** ORDER BY order and the first entry is extracted for each cycle.  Without
101286 ** an ORDER BY, the Queue table is just a FIFO.
101287 **
101288 ** If a LIMIT clause is provided, then the iteration stops after LIMIT rows
101289 ** have been output to pDest.  A LIMIT of zero means to output no rows and a
101290 ** negative LIMIT means to output all rows.  If there is also an OFFSET clause
101291 ** with a positive value, then the first OFFSET outputs are discarded rather
101292 ** than being sent to pDest.  The LIMIT count does not begin until after OFFSET
101293 ** rows have been skipped.
101294 */
101295 static void generateWithRecursiveQuery(
101296   Parse *pParse,        /* Parsing context */
101297   Select *p,            /* The recursive SELECT to be coded */
101298   SelectDest *pDest     /* What to do with query results */
101299 ){
101300   SrcList *pSrc = p->pSrc;      /* The FROM clause of the recursive query */
101301   int nCol = p->pEList->nExpr;  /* Number of columns in the recursive table */
101302   Vdbe *v = pParse->pVdbe;      /* The prepared statement under construction */
101303   Select *pSetup = p->pPrior;   /* The setup query */
101304   int addrTop;                  /* Top of the loop */
101305   int addrCont, addrBreak;      /* CONTINUE and BREAK addresses */
101306   int iCurrent = 0;             /* The Current table */
101307   int regCurrent;               /* Register holding Current table */
101308   int iQueue;                   /* The Queue table */
101309   int iDistinct = 0;            /* To ensure unique results if UNION */
101310   int eDest = SRT_Table;        /* How to write to Queue */
101311   SelectDest destQueue;         /* SelectDest targetting the Queue table */
101312   int i;                        /* Loop counter */
101313   int rc;                       /* Result code */
101314   ExprList *pOrderBy;           /* The ORDER BY clause */
101315   Expr *pLimit, *pOffset;       /* Saved LIMIT and OFFSET */
101316   int regLimit, regOffset;      /* Registers used by LIMIT and OFFSET */
101317 
101318   /* Obtain authorization to do a recursive query */
101319   if( sqlite3AuthCheck(pParse, SQLITE_RECURSIVE, 0, 0, 0) ) return;
101320 
101321   /* Process the LIMIT and OFFSET clauses, if they exist */
101322   addrBreak = sqlite3VdbeMakeLabel(v);
101323   computeLimitRegisters(pParse, p, addrBreak);
101324   pLimit = p->pLimit;
101325   pOffset = p->pOffset;
101326   regLimit = p->iLimit;
101327   regOffset = p->iOffset;
101328   p->pLimit = p->pOffset = 0;
101329   p->iLimit = p->iOffset = 0;
101330   pOrderBy = p->pOrderBy;
101331 
101332   /* Locate the cursor number of the Current table */
101333   for(i=0; ALWAYS(i<pSrc->nSrc); i++){
101334     if( pSrc->a[i].isRecursive ){
101335       iCurrent = pSrc->a[i].iCursor;
101336       break;
101337     }
101338   }
101339 
101340   /* Allocate cursors numbers for Queue and Distinct.  The cursor number for
101341   ** the Distinct table must be exactly one greater than Queue in order
101342   ** for the SRT_DistTable and SRT_DistQueue destinations to work. */
101343   iQueue = pParse->nTab++;
101344   if( p->op==TK_UNION ){
101345     eDest = pOrderBy ? SRT_DistQueue : SRT_DistTable;
101346     iDistinct = pParse->nTab++;
101347   }else{
101348     eDest = pOrderBy ? SRT_Queue : SRT_Table;
101349   }
101350   sqlite3SelectDestInit(&destQueue, eDest, iQueue);
101351 
101352   /* Allocate cursors for Current, Queue, and Distinct. */
101353   regCurrent = ++pParse->nMem;
101354   sqlite3VdbeAddOp3(v, OP_OpenPseudo, iCurrent, regCurrent, nCol);
101355   if( pOrderBy ){
101356     KeyInfo *pKeyInfo = multiSelectOrderByKeyInfo(pParse, p, 1);
101357     sqlite3VdbeAddOp4(v, OP_OpenEphemeral, iQueue, pOrderBy->nExpr+2, 0,
101358                       (char*)pKeyInfo, P4_KEYINFO);
101359     destQueue.pOrderBy = pOrderBy;
101360   }else{
101361     sqlite3VdbeAddOp2(v, OP_OpenEphemeral, iQueue, nCol);
101362   }
101363   VdbeComment((v, "Queue table"));
101364   if( iDistinct ){
101365     p->addrOpenEphm[0] = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, iDistinct, 0);
101366     p->selFlags |= SF_UsesEphemeral;
101367   }
101368 
101369   /* Detach the ORDER BY clause from the compound SELECT */
101370   p->pOrderBy = 0;
101371 
101372   /* Store the results of the setup-query in Queue. */
101373   rc = sqlite3Select(pParse, pSetup, &destQueue);
101374   if( rc ) goto end_of_recursive_query;
101375 
101376   /* Find the next row in the Queue and output that row */
101377   addrTop = sqlite3VdbeAddOp2(v, OP_Rewind, iQueue, addrBreak);
101378 
101379   /* Transfer the next row in Queue over to Current */
101380   sqlite3VdbeAddOp1(v, OP_NullRow, iCurrent); /* To reset column cache */
101381   if( pOrderBy ){
101382     sqlite3VdbeAddOp3(v, OP_Column, iQueue, pOrderBy->nExpr+1, regCurrent);
101383   }else{
101384     sqlite3VdbeAddOp2(v, OP_RowData, iQueue, regCurrent);
101385   }
101386   sqlite3VdbeAddOp1(v, OP_Delete, iQueue);
101387 
101388   /* Output the single row in Current */
101389   addrCont = sqlite3VdbeMakeLabel(v);
101390   codeOffset(v, regOffset, addrCont);
101391   selectInnerLoop(pParse, p, p->pEList, iCurrent,
101392       0, 0, pDest, addrCont, addrBreak);
101393   if( regLimit ) sqlite3VdbeAddOp3(v, OP_IfZero, regLimit, addrBreak, -1);
101394   sqlite3VdbeResolveLabel(v, addrCont);
101395 
101396   /* Execute the recursive SELECT taking the single row in Current as
101397   ** the value for the recursive-table. Store the results in the Queue.
101398   */
101399   p->pPrior = 0;
101400   sqlite3Select(pParse, p, &destQueue);
101401   assert( p->pPrior==0 );
101402   p->pPrior = pSetup;
101403 
101404   /* Keep running the loop until the Queue is empty */
101405   sqlite3VdbeAddOp2(v, OP_Goto, 0, addrTop);
101406   sqlite3VdbeResolveLabel(v, addrBreak);
101407 
101408 end_of_recursive_query:
101409   p->pOrderBy = pOrderBy;
101410   p->pLimit = pLimit;
101411   p->pOffset = pOffset;
101412   return;
101413 }
101414 #endif /* SQLITE_OMIT_CTE */
101415 
101416 /* Forward references */
101417 static int multiSelectOrderBy(
101418   Parse *pParse,        /* Parsing context */
101419   Select *p,            /* The right-most of SELECTs to be coded */
101420   SelectDest *pDest     /* What to do with query results */
101421 );
101422 
101423 
101424 /*
101425 ** This routine is called to process a compound query form from
101426 ** two or more separate queries using UNION, UNION ALL, EXCEPT, or
101427 ** INTERSECT
101428 **
101429 ** "p" points to the right-most of the two queries.  the query on the
101430 ** left is p->pPrior.  The left query could also be a compound query
101431 ** in which case this routine will be called recursively.
101432 **
101433 ** The results of the total query are to be written into a destination
101434 ** of type eDest with parameter iParm.
101435 **
101436 ** Example 1:  Consider a three-way compound SQL statement.
101437 **
101438 **     SELECT a FROM t1 UNION SELECT b FROM t2 UNION SELECT c FROM t3
101439 **
101440 ** This statement is parsed up as follows:
101441 **
101442 **     SELECT c FROM t3
101443 **      |
101444 **      `----->  SELECT b FROM t2
101445 **                |
101446 **                `------>  SELECT a FROM t1
101447 **
101448 ** The arrows in the diagram above represent the Select.pPrior pointer.
101449 ** So if this routine is called with p equal to the t3 query, then
101450 ** pPrior will be the t2 query.  p->op will be TK_UNION in this case.
101451 **
101452 ** Notice that because of the way SQLite parses compound SELECTs, the
101453 ** individual selects always group from left to right.
101454 */
101455 static int multiSelect(
101456   Parse *pParse,        /* Parsing context */
101457   Select *p,            /* The right-most of SELECTs to be coded */
101458   SelectDest *pDest     /* What to do with query results */
101459 ){
101460   int rc = SQLITE_OK;   /* Success code from a subroutine */
101461   Select *pPrior;       /* Another SELECT immediately to our left */
101462   Vdbe *v;              /* Generate code to this VDBE */
101463   SelectDest dest;      /* Alternative data destination */
101464   Select *pDelete = 0;  /* Chain of simple selects to delete */
101465   sqlite3 *db;          /* Database connection */
101466 #ifndef SQLITE_OMIT_EXPLAIN
101467   int iSub1 = 0;        /* EQP id of left-hand query */
101468   int iSub2 = 0;        /* EQP id of right-hand query */
101469 #endif
101470 
101471   /* Make sure there is no ORDER BY or LIMIT clause on prior SELECTs.  Only
101472   ** the last (right-most) SELECT in the series may have an ORDER BY or LIMIT.
101473   */
101474   assert( p && p->pPrior );  /* Calling function guarantees this much */
101475   assert( (p->selFlags & SF_Recursive)==0 || p->op==TK_ALL || p->op==TK_UNION );
101476   db = pParse->db;
101477   pPrior = p->pPrior;
101478   assert( pPrior->pRightmost!=pPrior );
101479   assert( pPrior->pRightmost==p->pRightmost );
101480   dest = *pDest;
101481   if( pPrior->pOrderBy ){
101482     sqlite3ErrorMsg(pParse,"ORDER BY clause should come after %s not before",
101483       selectOpName(p->op));
101484     rc = 1;
101485     goto multi_select_end;
101486   }
101487   if( pPrior->pLimit ){
101488     sqlite3ErrorMsg(pParse,"LIMIT clause should come after %s not before",
101489       selectOpName(p->op));
101490     rc = 1;
101491     goto multi_select_end;
101492   }
101493 
101494   v = sqlite3GetVdbe(pParse);
101495   assert( v!=0 );  /* The VDBE already created by calling function */
101496 
101497   /* Create the destination temporary table if necessary
101498   */
101499   if( dest.eDest==SRT_EphemTab ){
101500     assert( p->pEList );
101501     sqlite3VdbeAddOp2(v, OP_OpenEphemeral, dest.iSDParm, p->pEList->nExpr);
101502     sqlite3VdbeChangeP5(v, BTREE_UNORDERED);
101503     dest.eDest = SRT_Table;
101504   }
101505 
101506   /* Make sure all SELECTs in the statement have the same number of elements
101507   ** in their result sets.
101508   */
101509   assert( p->pEList && pPrior->pEList );
101510   if( p->pEList->nExpr!=pPrior->pEList->nExpr ){
101511     if( p->selFlags & SF_Values ){
101512       sqlite3ErrorMsg(pParse, "all VALUES must have the same number of terms");
101513     }else{
101514       sqlite3ErrorMsg(pParse, "SELECTs to the left and right of %s"
101515         " do not have the same number of result columns", selectOpName(p->op));
101516     }
101517     rc = 1;
101518     goto multi_select_end;
101519   }
101520 
101521 #ifndef SQLITE_OMIT_CTE
101522   if( p->selFlags & SF_Recursive ){
101523     generateWithRecursiveQuery(pParse, p, &dest);
101524   }else
101525 #endif
101526 
101527   /* Compound SELECTs that have an ORDER BY clause are handled separately.
101528   */
101529   if( p->pOrderBy ){
101530     return multiSelectOrderBy(pParse, p, pDest);
101531   }else
101532 
101533   /* Generate code for the left and right SELECT statements.
101534   */
101535   switch( p->op ){
101536     case TK_ALL: {
101537       int addr = 0;
101538       int nLimit;
101539       assert( !pPrior->pLimit );
101540       pPrior->iLimit = p->iLimit;
101541       pPrior->iOffset = p->iOffset;
101542       pPrior->pLimit = p->pLimit;
101543       pPrior->pOffset = p->pOffset;
101544       explainSetInteger(iSub1, pParse->iNextSelectId);
101545       rc = sqlite3Select(pParse, pPrior, &dest);
101546       p->pLimit = 0;
101547       p->pOffset = 0;
101548       if( rc ){
101549         goto multi_select_end;
101550       }
101551       p->pPrior = 0;
101552       p->iLimit = pPrior->iLimit;
101553       p->iOffset = pPrior->iOffset;
101554       if( p->iLimit ){
101555         addr = sqlite3VdbeAddOp1(v, OP_IfZero, p->iLimit);
101556         VdbeComment((v, "Jump ahead if LIMIT reached"));
101557       }
101558       explainSetInteger(iSub2, pParse->iNextSelectId);
101559       rc = sqlite3Select(pParse, p, &dest);
101560       testcase( rc!=SQLITE_OK );
101561       pDelete = p->pPrior;
101562       p->pPrior = pPrior;
101563       p->nSelectRow += pPrior->nSelectRow;
101564       if( pPrior->pLimit
101565        && sqlite3ExprIsInteger(pPrior->pLimit, &nLimit)
101566        && nLimit>0 && p->nSelectRow > (u64)nLimit
101567       ){
101568         p->nSelectRow = nLimit;
101569       }
101570       if( addr ){
101571         sqlite3VdbeJumpHere(v, addr);
101572       }
101573       break;
101574     }
101575     case TK_EXCEPT:
101576     case TK_UNION: {
101577       int unionTab;    /* Cursor number of the temporary table holding result */
101578       u8 op = 0;       /* One of the SRT_ operations to apply to self */
101579       int priorOp;     /* The SRT_ operation to apply to prior selects */
101580       Expr *pLimit, *pOffset; /* Saved values of p->nLimit and p->nOffset */
101581       int addr;
101582       SelectDest uniondest;
101583 
101584       testcase( p->op==TK_EXCEPT );
101585       testcase( p->op==TK_UNION );
101586       priorOp = SRT_Union;
101587       if( dest.eDest==priorOp && ALWAYS(!p->pLimit &&!p->pOffset) ){
101588         /* We can reuse a temporary table generated by a SELECT to our
101589         ** right.
101590         */
101591         assert( p->pRightmost!=p );  /* Can only happen for leftward elements
101592                                      ** of a 3-way or more compound */
101593         assert( p->pLimit==0 );      /* Not allowed on leftward elements */
101594         assert( p->pOffset==0 );     /* Not allowed on leftward elements */
101595         unionTab = dest.iSDParm;
101596       }else{
101597         /* We will need to create our own temporary table to hold the
101598         ** intermediate results.
101599         */
101600         unionTab = pParse->nTab++;
101601         assert( p->pOrderBy==0 );
101602         addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, unionTab, 0);
101603         assert( p->addrOpenEphm[0] == -1 );
101604         p->addrOpenEphm[0] = addr;
101605         p->pRightmost->selFlags |= SF_UsesEphemeral;
101606         assert( p->pEList );
101607       }
101608 
101609       /* Code the SELECT statements to our left
101610       */
101611       assert( !pPrior->pOrderBy );
101612       sqlite3SelectDestInit(&uniondest, priorOp, unionTab);
101613       explainSetInteger(iSub1, pParse->iNextSelectId);
101614       rc = sqlite3Select(pParse, pPrior, &uniondest);
101615       if( rc ){
101616         goto multi_select_end;
101617       }
101618 
101619       /* Code the current SELECT statement
101620       */
101621       if( p->op==TK_EXCEPT ){
101622         op = SRT_Except;
101623       }else{
101624         assert( p->op==TK_UNION );
101625         op = SRT_Union;
101626       }
101627       p->pPrior = 0;
101628       pLimit = p->pLimit;
101629       p->pLimit = 0;
101630       pOffset = p->pOffset;
101631       p->pOffset = 0;
101632       uniondest.eDest = op;
101633       explainSetInteger(iSub2, pParse->iNextSelectId);
101634       rc = sqlite3Select(pParse, p, &uniondest);
101635       testcase( rc!=SQLITE_OK );
101636       /* Query flattening in sqlite3Select() might refill p->pOrderBy.
101637       ** Be sure to delete p->pOrderBy, therefore, to avoid a memory leak. */
101638       sqlite3ExprListDelete(db, p->pOrderBy);
101639       pDelete = p->pPrior;
101640       p->pPrior = pPrior;
101641       p->pOrderBy = 0;
101642       if( p->op==TK_UNION ) p->nSelectRow += pPrior->nSelectRow;
101643       sqlite3ExprDelete(db, p->pLimit);
101644       p->pLimit = pLimit;
101645       p->pOffset = pOffset;
101646       p->iLimit = 0;
101647       p->iOffset = 0;
101648 
101649       /* Convert the data in the temporary table into whatever form
101650       ** it is that we currently need.
101651       */
101652       assert( unionTab==dest.iSDParm || dest.eDest!=priorOp );
101653       if( dest.eDest!=priorOp ){
101654         int iCont, iBreak, iStart;
101655         assert( p->pEList );
101656         if( dest.eDest==SRT_Output ){
101657           Select *pFirst = p;
101658           while( pFirst->pPrior ) pFirst = pFirst->pPrior;
101659           generateColumnNames(pParse, 0, pFirst->pEList);
101660         }
101661         iBreak = sqlite3VdbeMakeLabel(v);
101662         iCont = sqlite3VdbeMakeLabel(v);
101663         computeLimitRegisters(pParse, p, iBreak);
101664         sqlite3VdbeAddOp2(v, OP_Rewind, unionTab, iBreak);
101665         iStart = sqlite3VdbeCurrentAddr(v);
101666         selectInnerLoop(pParse, p, p->pEList, unionTab,
101667                         0, 0, &dest, iCont, iBreak);
101668         sqlite3VdbeResolveLabel(v, iCont);
101669         sqlite3VdbeAddOp2(v, OP_Next, unionTab, iStart);
101670         sqlite3VdbeResolveLabel(v, iBreak);
101671         sqlite3VdbeAddOp2(v, OP_Close, unionTab, 0);
101672       }
101673       break;
101674     }
101675     default: assert( p->op==TK_INTERSECT ); {
101676       int tab1, tab2;
101677       int iCont, iBreak, iStart;
101678       Expr *pLimit, *pOffset;
101679       int addr;
101680       SelectDest intersectdest;
101681       int r1;
101682 
101683       /* INTERSECT is different from the others since it requires
101684       ** two temporary tables.  Hence it has its own case.  Begin
101685       ** by allocating the tables we will need.
101686       */
101687       tab1 = pParse->nTab++;
101688       tab2 = pParse->nTab++;
101689       assert( p->pOrderBy==0 );
101690 
101691       addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, tab1, 0);
101692       assert( p->addrOpenEphm[0] == -1 );
101693       p->addrOpenEphm[0] = addr;
101694       p->pRightmost->selFlags |= SF_UsesEphemeral;
101695       assert( p->pEList );
101696 
101697       /* Code the SELECTs to our left into temporary table "tab1".
101698       */
101699       sqlite3SelectDestInit(&intersectdest, SRT_Union, tab1);
101700       explainSetInteger(iSub1, pParse->iNextSelectId);
101701       rc = sqlite3Select(pParse, pPrior, &intersectdest);
101702       if( rc ){
101703         goto multi_select_end;
101704       }
101705 
101706       /* Code the current SELECT into temporary table "tab2"
101707       */
101708       addr = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, tab2, 0);
101709       assert( p->addrOpenEphm[1] == -1 );
101710       p->addrOpenEphm[1] = addr;
101711       p->pPrior = 0;
101712       pLimit = p->pLimit;
101713       p->pLimit = 0;
101714       pOffset = p->pOffset;
101715       p->pOffset = 0;
101716       intersectdest.iSDParm = tab2;
101717       explainSetInteger(iSub2, pParse->iNextSelectId);
101718       rc = sqlite3Select(pParse, p, &intersectdest);
101719       testcase( rc!=SQLITE_OK );
101720       pDelete = p->pPrior;
101721       p->pPrior = pPrior;
101722       if( p->nSelectRow>pPrior->nSelectRow ) p->nSelectRow = pPrior->nSelectRow;
101723       sqlite3ExprDelete(db, p->pLimit);
101724       p->pLimit = pLimit;
101725       p->pOffset = pOffset;
101726 
101727       /* Generate code to take the intersection of the two temporary
101728       ** tables.
101729       */
101730       assert( p->pEList );
101731       if( dest.eDest==SRT_Output ){
101732         Select *pFirst = p;
101733         while( pFirst->pPrior ) pFirst = pFirst->pPrior;
101734         generateColumnNames(pParse, 0, pFirst->pEList);
101735       }
101736       iBreak = sqlite3VdbeMakeLabel(v);
101737       iCont = sqlite3VdbeMakeLabel(v);
101738       computeLimitRegisters(pParse, p, iBreak);
101739       sqlite3VdbeAddOp2(v, OP_Rewind, tab1, iBreak);
101740       r1 = sqlite3GetTempReg(pParse);
101741       iStart = sqlite3VdbeAddOp2(v, OP_RowKey, tab1, r1);
101742       sqlite3VdbeAddOp4Int(v, OP_NotFound, tab2, iCont, r1, 0);
101743       sqlite3ReleaseTempReg(pParse, r1);
101744       selectInnerLoop(pParse, p, p->pEList, tab1,
101745                       0, 0, &dest, iCont, iBreak);
101746       sqlite3VdbeResolveLabel(v, iCont);
101747       sqlite3VdbeAddOp2(v, OP_Next, tab1, iStart);
101748       sqlite3VdbeResolveLabel(v, iBreak);
101749       sqlite3VdbeAddOp2(v, OP_Close, tab2, 0);
101750       sqlite3VdbeAddOp2(v, OP_Close, tab1, 0);
101751       break;
101752     }
101753   }
101754 
101755   explainComposite(pParse, p->op, iSub1, iSub2, p->op!=TK_ALL);
101756 
101757   /* Compute collating sequences used by
101758   ** temporary tables needed to implement the compound select.
101759   ** Attach the KeyInfo structure to all temporary tables.
101760   **
101761   ** This section is run by the right-most SELECT statement only.
101762   ** SELECT statements to the left always skip this part.  The right-most
101763   ** SELECT might also skip this part if it has no ORDER BY clause and
101764   ** no temp tables are required.
101765   */
101766   if( p->selFlags & SF_UsesEphemeral ){
101767     int i;                        /* Loop counter */
101768     KeyInfo *pKeyInfo;            /* Collating sequence for the result set */
101769     Select *pLoop;                /* For looping through SELECT statements */
101770     CollSeq **apColl;             /* For looping through pKeyInfo->aColl[] */
101771     int nCol;                     /* Number of columns in result set */
101772 
101773     assert( p->pRightmost==p );
101774     nCol = p->pEList->nExpr;
101775     pKeyInfo = sqlite3KeyInfoAlloc(db, nCol, 1);
101776     if( !pKeyInfo ){
101777       rc = SQLITE_NOMEM;
101778       goto multi_select_end;
101779     }
101780     for(i=0, apColl=pKeyInfo->aColl; i<nCol; i++, apColl++){
101781       *apColl = multiSelectCollSeq(pParse, p, i);
101782       if( 0==*apColl ){
101783         *apColl = db->pDfltColl;
101784       }
101785     }
101786 
101787     for(pLoop=p; pLoop; pLoop=pLoop->pPrior){
101788       for(i=0; i<2; i++){
101789         int addr = pLoop->addrOpenEphm[i];
101790         if( addr<0 ){
101791           /* If [0] is unused then [1] is also unused.  So we can
101792           ** always safely abort as soon as the first unused slot is found */
101793           assert( pLoop->addrOpenEphm[1]<0 );
101794           break;
101795         }
101796         sqlite3VdbeChangeP2(v, addr, nCol);
101797         sqlite3VdbeChangeP4(v, addr, (char*)sqlite3KeyInfoRef(pKeyInfo),
101798                             P4_KEYINFO);
101799         pLoop->addrOpenEphm[i] = -1;
101800       }
101801     }
101802     sqlite3KeyInfoUnref(pKeyInfo);
101803   }
101804 
101805 multi_select_end:
101806   pDest->iSdst = dest.iSdst;
101807   pDest->nSdst = dest.nSdst;
101808   sqlite3SelectDelete(db, pDelete);
101809   return rc;
101810 }
101811 #endif /* SQLITE_OMIT_COMPOUND_SELECT */
101812 
101813 /*
101814 ** Code an output subroutine for a coroutine implementation of a
101815 ** SELECT statment.
101816 **
101817 ** The data to be output is contained in pIn->iSdst.  There are
101818 ** pIn->nSdst columns to be output.  pDest is where the output should
101819 ** be sent.
101820 **
101821 ** regReturn is the number of the register holding the subroutine
101822 ** return address.
101823 **
101824 ** If regPrev>0 then it is the first register in a vector that
101825 ** records the previous output.  mem[regPrev] is a flag that is false
101826 ** if there has been no previous output.  If regPrev>0 then code is
101827 ** generated to suppress duplicates.  pKeyInfo is used for comparing
101828 ** keys.
101829 **
101830 ** If the LIMIT found in p->iLimit is reached, jump immediately to
101831 ** iBreak.
101832 */
101833 static int generateOutputSubroutine(
101834   Parse *pParse,          /* Parsing context */
101835   Select *p,              /* The SELECT statement */
101836   SelectDest *pIn,        /* Coroutine supplying data */
101837   SelectDest *pDest,      /* Where to send the data */
101838   int regReturn,          /* The return address register */
101839   int regPrev,            /* Previous result register.  No uniqueness if 0 */
101840   KeyInfo *pKeyInfo,      /* For comparing with previous entry */
101841   int iBreak              /* Jump here if we hit the LIMIT */
101842 ){
101843   Vdbe *v = pParse->pVdbe;
101844   int iContinue;
101845   int addr;
101846 
101847   addr = sqlite3VdbeCurrentAddr(v);
101848   iContinue = sqlite3VdbeMakeLabel(v);
101849 
101850   /* Suppress duplicates for UNION, EXCEPT, and INTERSECT
101851   */
101852   if( regPrev ){
101853     int j1, j2;
101854     j1 = sqlite3VdbeAddOp1(v, OP_IfNot, regPrev);
101855     j2 = sqlite3VdbeAddOp4(v, OP_Compare, pIn->iSdst, regPrev+1, pIn->nSdst,
101856                               (char*)sqlite3KeyInfoRef(pKeyInfo), P4_KEYINFO);
101857     sqlite3VdbeAddOp3(v, OP_Jump, j2+2, iContinue, j2+2);
101858     sqlite3VdbeJumpHere(v, j1);
101859     sqlite3VdbeAddOp3(v, OP_Copy, pIn->iSdst, regPrev+1, pIn->nSdst-1);
101860     sqlite3VdbeAddOp2(v, OP_Integer, 1, regPrev);
101861   }
101862   if( pParse->db->mallocFailed ) return 0;
101863 
101864   /* Suppress the first OFFSET entries if there is an OFFSET clause
101865   */
101866   codeOffset(v, p->iOffset, iContinue);
101867 
101868   switch( pDest->eDest ){
101869     /* Store the result as data using a unique key.
101870     */
101871     case SRT_Table:
101872     case SRT_EphemTab: {
101873       int r1 = sqlite3GetTempReg(pParse);
101874       int r2 = sqlite3GetTempReg(pParse);
101875       testcase( pDest->eDest==SRT_Table );
101876       testcase( pDest->eDest==SRT_EphemTab );
101877       sqlite3VdbeAddOp3(v, OP_MakeRecord, pIn->iSdst, pIn->nSdst, r1);
101878       sqlite3VdbeAddOp2(v, OP_NewRowid, pDest->iSDParm, r2);
101879       sqlite3VdbeAddOp3(v, OP_Insert, pDest->iSDParm, r1, r2);
101880       sqlite3VdbeChangeP5(v, OPFLAG_APPEND);
101881       sqlite3ReleaseTempReg(pParse, r2);
101882       sqlite3ReleaseTempReg(pParse, r1);
101883       break;
101884     }
101885 
101886 #ifndef SQLITE_OMIT_SUBQUERY
101887     /* If we are creating a set for an "expr IN (SELECT ...)" construct,
101888     ** then there should be a single item on the stack.  Write this
101889     ** item into the set table with bogus data.
101890     */
101891     case SRT_Set: {
101892       int r1;
101893       assert( pIn->nSdst==1 );
101894       pDest->affSdst =
101895          sqlite3CompareAffinity(p->pEList->a[0].pExpr, pDest->affSdst);
101896       r1 = sqlite3GetTempReg(pParse);
101897       sqlite3VdbeAddOp4(v, OP_MakeRecord, pIn->iSdst, 1, r1, &pDest->affSdst,1);
101898       sqlite3ExprCacheAffinityChange(pParse, pIn->iSdst, 1);
101899       sqlite3VdbeAddOp2(v, OP_IdxInsert, pDest->iSDParm, r1);
101900       sqlite3ReleaseTempReg(pParse, r1);
101901       break;
101902     }
101903 
101904 #if 0  /* Never occurs on an ORDER BY query */
101905     /* If any row exist in the result set, record that fact and abort.
101906     */
101907     case SRT_Exists: {
101908       sqlite3VdbeAddOp2(v, OP_Integer, 1, pDest->iSDParm);
101909       /* The LIMIT clause will terminate the loop for us */
101910       break;
101911     }
101912 #endif
101913 
101914     /* If this is a scalar select that is part of an expression, then
101915     ** store the results in the appropriate memory cell and break out
101916     ** of the scan loop.
101917     */
101918     case SRT_Mem: {
101919       assert( pIn->nSdst==1 );
101920       sqlite3ExprCodeMove(pParse, pIn->iSdst, pDest->iSDParm, 1);
101921       /* The LIMIT clause will jump out of the loop for us */
101922       break;
101923     }
101924 #endif /* #ifndef SQLITE_OMIT_SUBQUERY */
101925 
101926     /* The results are stored in a sequence of registers
101927     ** starting at pDest->iSdst.  Then the co-routine yields.
101928     */
101929     case SRT_Coroutine: {
101930       if( pDest->iSdst==0 ){
101931         pDest->iSdst = sqlite3GetTempRange(pParse, pIn->nSdst);
101932         pDest->nSdst = pIn->nSdst;
101933       }
101934       sqlite3ExprCodeMove(pParse, pIn->iSdst, pDest->iSdst, pDest->nSdst);
101935       sqlite3VdbeAddOp1(v, OP_Yield, pDest->iSDParm);
101936       break;
101937     }
101938 
101939     /* If none of the above, then the result destination must be
101940     ** SRT_Output.  This routine is never called with any other
101941     ** destination other than the ones handled above or SRT_Output.
101942     **
101943     ** For SRT_Output, results are stored in a sequence of registers.
101944     ** Then the OP_ResultRow opcode is used to cause sqlite3_step() to
101945     ** return the next row of result.
101946     */
101947     default: {
101948       assert( pDest->eDest==SRT_Output );
101949       sqlite3VdbeAddOp2(v, OP_ResultRow, pIn->iSdst, pIn->nSdst);
101950       sqlite3ExprCacheAffinityChange(pParse, pIn->iSdst, pIn->nSdst);
101951       break;
101952     }
101953   }
101954 
101955   /* Jump to the end of the loop if the LIMIT is reached.
101956   */
101957   if( p->iLimit ){
101958     sqlite3VdbeAddOp3(v, OP_IfZero, p->iLimit, iBreak, -1);
101959   }
101960 
101961   /* Generate the subroutine return
101962   */
101963   sqlite3VdbeResolveLabel(v, iContinue);
101964   sqlite3VdbeAddOp1(v, OP_Return, regReturn);
101965 
101966   return addr;
101967 }
101968 
101969 /*
101970 ** Alternative compound select code generator for cases when there
101971 ** is an ORDER BY clause.
101972 **
101973 ** We assume a query of the following form:
101974 **
101975 **      <selectA>  <operator>  <selectB>  ORDER BY <orderbylist>
101976 **
101977 ** <operator> is one of UNION ALL, UNION, EXCEPT, or INTERSECT.  The idea
101978 ** is to code both <selectA> and <selectB> with the ORDER BY clause as
101979 ** co-routines.  Then run the co-routines in parallel and merge the results
101980 ** into the output.  In addition to the two coroutines (called selectA and
101981 ** selectB) there are 7 subroutines:
101982 **
101983 **    outA:    Move the output of the selectA coroutine into the output
101984 **             of the compound query.
101985 **
101986 **    outB:    Move the output of the selectB coroutine into the output
101987 **             of the compound query.  (Only generated for UNION and
101988 **             UNION ALL.  EXCEPT and INSERTSECT never output a row that
101989 **             appears only in B.)
101990 **
101991 **    AltB:    Called when there is data from both coroutines and A<B.
101992 **
101993 **    AeqB:    Called when there is data from both coroutines and A==B.
101994 **
101995 **    AgtB:    Called when there is data from both coroutines and A>B.
101996 **
101997 **    EofA:    Called when data is exhausted from selectA.
101998 **
101999 **    EofB:    Called when data is exhausted from selectB.
102000 **
102001 ** The implementation of the latter five subroutines depend on which
102002 ** <operator> is used:
102003 **
102004 **
102005 **             UNION ALL         UNION            EXCEPT          INTERSECT
102006 **          -------------  -----------------  --------------  -----------------
102007 **   AltB:   outA, nextA      outA, nextA       outA, nextA         nextA
102008 **
102009 **   AeqB:   outA, nextA         nextA             nextA         outA, nextA
102010 **
102011 **   AgtB:   outB, nextB      outB, nextB          nextB            nextB
102012 **
102013 **   EofA:   outB, nextB      outB, nextB          halt             halt
102014 **
102015 **   EofB:   outA, nextA      outA, nextA       outA, nextA         halt
102016 **
102017 ** In the AltB, AeqB, and AgtB subroutines, an EOF on A following nextA
102018 ** causes an immediate jump to EofA and an EOF on B following nextB causes
102019 ** an immediate jump to EofB.  Within EofA and EofB, and EOF on entry or
102020 ** following nextX causes a jump to the end of the select processing.
102021 **
102022 ** Duplicate removal in the UNION, EXCEPT, and INTERSECT cases is handled
102023 ** within the output subroutine.  The regPrev register set holds the previously
102024 ** output value.  A comparison is made against this value and the output
102025 ** is skipped if the next results would be the same as the previous.
102026 **
102027 ** The implementation plan is to implement the two coroutines and seven
102028 ** subroutines first, then put the control logic at the bottom.  Like this:
102029 **
102030 **          goto Init
102031 **     coA: coroutine for left query (A)
102032 **     coB: coroutine for right query (B)
102033 **    outA: output one row of A
102034 **    outB: output one row of B (UNION and UNION ALL only)
102035 **    EofA: ...
102036 **    EofB: ...
102037 **    AltB: ...
102038 **    AeqB: ...
102039 **    AgtB: ...
102040 **    Init: initialize coroutine registers
102041 **          yield coA
102042 **          if eof(A) goto EofA
102043 **          yield coB
102044 **          if eof(B) goto EofB
102045 **    Cmpr: Compare A, B
102046 **          Jump AltB, AeqB, AgtB
102047 **     End: ...
102048 **
102049 ** We call AltB, AeqB, AgtB, EofA, and EofB "subroutines" but they are not
102050 ** actually called using Gosub and they do not Return.  EofA and EofB loop
102051 ** until all data is exhausted then jump to the "end" labe.  AltB, AeqB,
102052 ** and AgtB jump to either L2 or to one of EofA or EofB.
102053 */
102054 #ifndef SQLITE_OMIT_COMPOUND_SELECT
102055 static int multiSelectOrderBy(
102056   Parse *pParse,        /* Parsing context */
102057   Select *p,            /* The right-most of SELECTs to be coded */
102058   SelectDest *pDest     /* What to do with query results */
102059 ){
102060   int i, j;             /* Loop counters */
102061   Select *pPrior;       /* Another SELECT immediately to our left */
102062   Vdbe *v;              /* Generate code to this VDBE */
102063   SelectDest destA;     /* Destination for coroutine A */
102064   SelectDest destB;     /* Destination for coroutine B */
102065   int regAddrA;         /* Address register for select-A coroutine */
102066   int regEofA;          /* Flag to indicate when select-A is complete */
102067   int regAddrB;         /* Address register for select-B coroutine */
102068   int regEofB;          /* Flag to indicate when select-B is complete */
102069   int addrSelectA;      /* Address of the select-A coroutine */
102070   int addrSelectB;      /* Address of the select-B coroutine */
102071   int regOutA;          /* Address register for the output-A subroutine */
102072   int regOutB;          /* Address register for the output-B subroutine */
102073   int addrOutA;         /* Address of the output-A subroutine */
102074   int addrOutB = 0;     /* Address of the output-B subroutine */
102075   int addrEofA;         /* Address of the select-A-exhausted subroutine */
102076   int addrEofB;         /* Address of the select-B-exhausted subroutine */
102077   int addrAltB;         /* Address of the A<B subroutine */
102078   int addrAeqB;         /* Address of the A==B subroutine */
102079   int addrAgtB;         /* Address of the A>B subroutine */
102080   int regLimitA;        /* Limit register for select-A */
102081   int regLimitB;        /* Limit register for select-A */
102082   int regPrev;          /* A range of registers to hold previous output */
102083   int savedLimit;       /* Saved value of p->iLimit */
102084   int savedOffset;      /* Saved value of p->iOffset */
102085   int labelCmpr;        /* Label for the start of the merge algorithm */
102086   int labelEnd;         /* Label for the end of the overall SELECT stmt */
102087   int j1;               /* Jump instructions that get retargetted */
102088   int op;               /* One of TK_ALL, TK_UNION, TK_EXCEPT, TK_INTERSECT */
102089   KeyInfo *pKeyDup = 0; /* Comparison information for duplicate removal */
102090   KeyInfo *pKeyMerge;   /* Comparison information for merging rows */
102091   sqlite3 *db;          /* Database connection */
102092   ExprList *pOrderBy;   /* The ORDER BY clause */
102093   int nOrderBy;         /* Number of terms in the ORDER BY clause */
102094   int *aPermute;        /* Mapping from ORDER BY terms to result set columns */
102095 #ifndef SQLITE_OMIT_EXPLAIN
102096   int iSub1;            /* EQP id of left-hand query */
102097   int iSub2;            /* EQP id of right-hand query */
102098 #endif
102099 
102100   assert( p->pOrderBy!=0 );
102101   assert( pKeyDup==0 ); /* "Managed" code needs this.  Ticket #3382. */
102102   db = pParse->db;
102103   v = pParse->pVdbe;
102104   assert( v!=0 );       /* Already thrown the error if VDBE alloc failed */
102105   labelEnd = sqlite3VdbeMakeLabel(v);
102106   labelCmpr = sqlite3VdbeMakeLabel(v);
102107 
102108 
102109   /* Patch up the ORDER BY clause
102110   */
102111   op = p->op;
102112   pPrior = p->pPrior;
102113   assert( pPrior->pOrderBy==0 );
102114   pOrderBy = p->pOrderBy;
102115   assert( pOrderBy );
102116   nOrderBy = pOrderBy->nExpr;
102117 
102118   /* For operators other than UNION ALL we have to make sure that
102119   ** the ORDER BY clause covers every term of the result set.  Add
102120   ** terms to the ORDER BY clause as necessary.
102121   */
102122   if( op!=TK_ALL ){
102123     for(i=1; db->mallocFailed==0 && i<=p->pEList->nExpr; i++){
102124       struct ExprList_item *pItem;
102125       for(j=0, pItem=pOrderBy->a; j<nOrderBy; j++, pItem++){
102126         assert( pItem->u.x.iOrderByCol>0 );
102127         if( pItem->u.x.iOrderByCol==i ) break;
102128       }
102129       if( j==nOrderBy ){
102130         Expr *pNew = sqlite3Expr(db, TK_INTEGER, 0);
102131         if( pNew==0 ) return SQLITE_NOMEM;
102132         pNew->flags |= EP_IntValue;
102133         pNew->u.iValue = i;
102134         pOrderBy = sqlite3ExprListAppend(pParse, pOrderBy, pNew);
102135         if( pOrderBy ) pOrderBy->a[nOrderBy++].u.x.iOrderByCol = (u16)i;
102136       }
102137     }
102138   }
102139 
102140   /* Compute the comparison permutation and keyinfo that is used with
102141   ** the permutation used to determine if the next
102142   ** row of results comes from selectA or selectB.  Also add explicit
102143   ** collations to the ORDER BY clause terms so that when the subqueries
102144   ** to the right and the left are evaluated, they use the correct
102145   ** collation.
102146   */
102147   aPermute = sqlite3DbMallocRaw(db, sizeof(int)*nOrderBy);
102148   if( aPermute ){
102149     struct ExprList_item *pItem;
102150     for(i=0, pItem=pOrderBy->a; i<nOrderBy; i++, pItem++){
102151       assert( pItem->u.x.iOrderByCol>0
102152           && pItem->u.x.iOrderByCol<=p->pEList->nExpr );
102153       aPermute[i] = pItem->u.x.iOrderByCol - 1;
102154     }
102155     pKeyMerge = multiSelectOrderByKeyInfo(pParse, p, 1);
102156   }else{
102157     pKeyMerge = 0;
102158   }
102159 
102160   /* Reattach the ORDER BY clause to the query.
102161   */
102162   p->pOrderBy = pOrderBy;
102163   pPrior->pOrderBy = sqlite3ExprListDup(pParse->db, pOrderBy, 0);
102164 
102165   /* Allocate a range of temporary registers and the KeyInfo needed
102166   ** for the logic that removes duplicate result rows when the
102167   ** operator is UNION, EXCEPT, or INTERSECT (but not UNION ALL).
102168   */
102169   if( op==TK_ALL ){
102170     regPrev = 0;
102171   }else{
102172     int nExpr = p->pEList->nExpr;
102173     assert( nOrderBy>=nExpr || db->mallocFailed );
102174     regPrev = pParse->nMem+1;
102175     pParse->nMem += nExpr+1;
102176     sqlite3VdbeAddOp2(v, OP_Integer, 0, regPrev);
102177     pKeyDup = sqlite3KeyInfoAlloc(db, nExpr, 1);
102178     if( pKeyDup ){
102179       assert( sqlite3KeyInfoIsWriteable(pKeyDup) );
102180       for(i=0; i<nExpr; i++){
102181         pKeyDup->aColl[i] = multiSelectCollSeq(pParse, p, i);
102182         pKeyDup->aSortOrder[i] = 0;
102183       }
102184     }
102185   }
102186 
102187   /* Separate the left and the right query from one another
102188   */
102189   p->pPrior = 0;
102190   sqlite3ResolveOrderGroupBy(pParse, p, p->pOrderBy, "ORDER");
102191   if( pPrior->pPrior==0 ){
102192     sqlite3ResolveOrderGroupBy(pParse, pPrior, pPrior->pOrderBy, "ORDER");
102193   }
102194 
102195   /* Compute the limit registers */
102196   computeLimitRegisters(pParse, p, labelEnd);
102197   if( p->iLimit && op==TK_ALL ){
102198     regLimitA = ++pParse->nMem;
102199     regLimitB = ++pParse->nMem;
102200     sqlite3VdbeAddOp2(v, OP_Copy, p->iOffset ? p->iOffset+1 : p->iLimit,
102201                                   regLimitA);
102202     sqlite3VdbeAddOp2(v, OP_Copy, regLimitA, regLimitB);
102203   }else{
102204     regLimitA = regLimitB = 0;
102205   }
102206   sqlite3ExprDelete(db, p->pLimit);
102207   p->pLimit = 0;
102208   sqlite3ExprDelete(db, p->pOffset);
102209   p->pOffset = 0;
102210 
102211   regAddrA = ++pParse->nMem;
102212   regEofA = ++pParse->nMem;
102213   regAddrB = ++pParse->nMem;
102214   regEofB = ++pParse->nMem;
102215   regOutA = ++pParse->nMem;
102216   regOutB = ++pParse->nMem;
102217   sqlite3SelectDestInit(&destA, SRT_Coroutine, regAddrA);
102218   sqlite3SelectDestInit(&destB, SRT_Coroutine, regAddrB);
102219 
102220   /* Jump past the various subroutines and coroutines to the main
102221   ** merge loop
102222   */
102223   j1 = sqlite3VdbeAddOp0(v, OP_Goto);
102224   addrSelectA = sqlite3VdbeCurrentAddr(v);
102225 
102226 
102227   /* Generate a coroutine to evaluate the SELECT statement to the
102228   ** left of the compound operator - the "A" select.
102229   */
102230   VdbeNoopComment((v, "Begin coroutine for left SELECT"));
102231   pPrior->iLimit = regLimitA;
102232   explainSetInteger(iSub1, pParse->iNextSelectId);
102233   sqlite3Select(pParse, pPrior, &destA);
102234   sqlite3VdbeAddOp2(v, OP_Integer, 1, regEofA);
102235   sqlite3VdbeAddOp1(v, OP_Yield, regAddrA);
102236   VdbeNoopComment((v, "End coroutine for left SELECT"));
102237 
102238   /* Generate a coroutine to evaluate the SELECT statement on
102239   ** the right - the "B" select
102240   */
102241   addrSelectB = sqlite3VdbeCurrentAddr(v);
102242   VdbeNoopComment((v, "Begin coroutine for right SELECT"));
102243   savedLimit = p->iLimit;
102244   savedOffset = p->iOffset;
102245   p->iLimit = regLimitB;
102246   p->iOffset = 0;
102247   explainSetInteger(iSub2, pParse->iNextSelectId);
102248   sqlite3Select(pParse, p, &destB);
102249   p->iLimit = savedLimit;
102250   p->iOffset = savedOffset;
102251   sqlite3VdbeAddOp2(v, OP_Integer, 1, regEofB);
102252   sqlite3VdbeAddOp1(v, OP_Yield, regAddrB);
102253   VdbeNoopComment((v, "End coroutine for right SELECT"));
102254 
102255   /* Generate a subroutine that outputs the current row of the A
102256   ** select as the next output row of the compound select.
102257   */
102258   VdbeNoopComment((v, "Output routine for A"));
102259   addrOutA = generateOutputSubroutine(pParse,
102260                  p, &destA, pDest, regOutA,
102261                  regPrev, pKeyDup, labelEnd);
102262 
102263   /* Generate a subroutine that outputs the current row of the B
102264   ** select as the next output row of the compound select.
102265   */
102266   if( op==TK_ALL || op==TK_UNION ){
102267     VdbeNoopComment((v, "Output routine for B"));
102268     addrOutB = generateOutputSubroutine(pParse,
102269                  p, &destB, pDest, regOutB,
102270                  regPrev, pKeyDup, labelEnd);
102271   }
102272   sqlite3KeyInfoUnref(pKeyDup);
102273 
102274   /* Generate a subroutine to run when the results from select A
102275   ** are exhausted and only data in select B remains.
102276   */
102277   VdbeNoopComment((v, "eof-A subroutine"));
102278   if( op==TK_EXCEPT || op==TK_INTERSECT ){
102279     addrEofA = sqlite3VdbeAddOp2(v, OP_Goto, 0, labelEnd);
102280   }else{
102281     addrEofA = sqlite3VdbeAddOp2(v, OP_If, regEofB, labelEnd);
102282     sqlite3VdbeAddOp2(v, OP_Gosub, regOutB, addrOutB);
102283     sqlite3VdbeAddOp1(v, OP_Yield, regAddrB);
102284     sqlite3VdbeAddOp2(v, OP_Goto, 0, addrEofA);
102285     p->nSelectRow += pPrior->nSelectRow;
102286   }
102287 
102288   /* Generate a subroutine to run when the results from select B
102289   ** are exhausted and only data in select A remains.
102290   */
102291   if( op==TK_INTERSECT ){
102292     addrEofB = addrEofA;
102293     if( p->nSelectRow > pPrior->nSelectRow ) p->nSelectRow = pPrior->nSelectRow;
102294   }else{
102295     VdbeNoopComment((v, "eof-B subroutine"));
102296     addrEofB = sqlite3VdbeAddOp2(v, OP_If, regEofA, labelEnd);
102297     sqlite3VdbeAddOp2(v, OP_Gosub, regOutA, addrOutA);
102298     sqlite3VdbeAddOp1(v, OP_Yield, regAddrA);
102299     sqlite3VdbeAddOp2(v, OP_Goto, 0, addrEofB);
102300   }
102301 
102302   /* Generate code to handle the case of A<B
102303   */
102304   VdbeNoopComment((v, "A-lt-B subroutine"));
102305   addrAltB = sqlite3VdbeAddOp2(v, OP_Gosub, regOutA, addrOutA);
102306   sqlite3VdbeAddOp1(v, OP_Yield, regAddrA);
102307   sqlite3VdbeAddOp2(v, OP_If, regEofA, addrEofA);
102308   sqlite3VdbeAddOp2(v, OP_Goto, 0, labelCmpr);
102309 
102310   /* Generate code to handle the case of A==B
102311   */
102312   if( op==TK_ALL ){
102313     addrAeqB = addrAltB;
102314   }else if( op==TK_INTERSECT ){
102315     addrAeqB = addrAltB;
102316     addrAltB++;
102317   }else{
102318     VdbeNoopComment((v, "A-eq-B subroutine"));
102319     addrAeqB =
102320     sqlite3VdbeAddOp1(v, OP_Yield, regAddrA);
102321     sqlite3VdbeAddOp2(v, OP_If, regEofA, addrEofA);
102322     sqlite3VdbeAddOp2(v, OP_Goto, 0, labelCmpr);
102323   }
102324 
102325   /* Generate code to handle the case of A>B
102326   */
102327   VdbeNoopComment((v, "A-gt-B subroutine"));
102328   addrAgtB = sqlite3VdbeCurrentAddr(v);
102329   if( op==TK_ALL || op==TK_UNION ){
102330     sqlite3VdbeAddOp2(v, OP_Gosub, regOutB, addrOutB);
102331   }
102332   sqlite3VdbeAddOp1(v, OP_Yield, regAddrB);
102333   sqlite3VdbeAddOp2(v, OP_If, regEofB, addrEofB);
102334   sqlite3VdbeAddOp2(v, OP_Goto, 0, labelCmpr);
102335 
102336   /* This code runs once to initialize everything.
102337   */
102338   sqlite3VdbeJumpHere(v, j1);
102339   sqlite3VdbeAddOp2(v, OP_Integer, 0, regEofA);
102340   sqlite3VdbeAddOp2(v, OP_Integer, 0, regEofB);
102341   sqlite3VdbeAddOp2(v, OP_Gosub, regAddrA, addrSelectA);
102342   sqlite3VdbeAddOp2(v, OP_Gosub, regAddrB, addrSelectB);
102343   sqlite3VdbeAddOp2(v, OP_If, regEofA, addrEofA);
102344   sqlite3VdbeAddOp2(v, OP_If, regEofB, addrEofB);
102345 
102346   /* Implement the main merge loop
102347   */
102348   sqlite3VdbeResolveLabel(v, labelCmpr);
102349   sqlite3VdbeAddOp4(v, OP_Permutation, 0, 0, 0, (char*)aPermute, P4_INTARRAY);
102350   sqlite3VdbeAddOp4(v, OP_Compare, destA.iSdst, destB.iSdst, nOrderBy,
102351                          (char*)pKeyMerge, P4_KEYINFO);
102352   sqlite3VdbeChangeP5(v, OPFLAG_PERMUTE);
102353   sqlite3VdbeAddOp3(v, OP_Jump, addrAltB, addrAeqB, addrAgtB);
102354 
102355   /* Jump to the this point in order to terminate the query.
102356   */
102357   sqlite3VdbeResolveLabel(v, labelEnd);
102358 
102359   /* Set the number of output columns
102360   */
102361   if( pDest->eDest==SRT_Output ){
102362     Select *pFirst = pPrior;
102363     while( pFirst->pPrior ) pFirst = pFirst->pPrior;
102364     generateColumnNames(pParse, 0, pFirst->pEList);
102365   }
102366 
102367   /* Reassembly the compound query so that it will be freed correctly
102368   ** by the calling function */
102369   if( p->pPrior ){
102370     sqlite3SelectDelete(db, p->pPrior);
102371   }
102372   p->pPrior = pPrior;
102373 
102374   /*** TBD:  Insert subroutine calls to close cursors on incomplete
102375   **** subqueries ****/
102376   explainComposite(pParse, p->op, iSub1, iSub2, 0);
102377   return SQLITE_OK;
102378 }
102379 #endif
102380 
102381 #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
102382 /* Forward Declarations */
102383 static void substExprList(sqlite3*, ExprList*, int, ExprList*);
102384 static void substSelect(sqlite3*, Select *, int, ExprList *);
102385 
102386 /*
102387 ** Scan through the expression pExpr.  Replace every reference to
102388 ** a column in table number iTable with a copy of the iColumn-th
102389 ** entry in pEList.  (But leave references to the ROWID column
102390 ** unchanged.)
102391 **
102392 ** This routine is part of the flattening procedure.  A subquery
102393 ** whose result set is defined by pEList appears as entry in the
102394 ** FROM clause of a SELECT such that the VDBE cursor assigned to that
102395 ** FORM clause entry is iTable.  This routine make the necessary
102396 ** changes to pExpr so that it refers directly to the source table
102397 ** of the subquery rather the result set of the subquery.
102398 */
102399 static Expr *substExpr(
102400   sqlite3 *db,        /* Report malloc errors to this connection */
102401   Expr *pExpr,        /* Expr in which substitution occurs */
102402   int iTable,         /* Table to be substituted */
102403   ExprList *pEList    /* Substitute expressions */
102404 ){
102405   if( pExpr==0 ) return 0;
102406   if( pExpr->op==TK_COLUMN && pExpr->iTable==iTable ){
102407     if( pExpr->iColumn<0 ){
102408       pExpr->op = TK_NULL;
102409     }else{
102410       Expr *pNew;
102411       assert( pEList!=0 && pExpr->iColumn<pEList->nExpr );
102412       assert( pExpr->pLeft==0 && pExpr->pRight==0 );
102413       pNew = sqlite3ExprDup(db, pEList->a[pExpr->iColumn].pExpr, 0);
102414       sqlite3ExprDelete(db, pExpr);
102415       pExpr = pNew;
102416     }
102417   }else{
102418     pExpr->pLeft = substExpr(db, pExpr->pLeft, iTable, pEList);
102419     pExpr->pRight = substExpr(db, pExpr->pRight, iTable, pEList);
102420     if( ExprHasProperty(pExpr, EP_xIsSelect) ){
102421       substSelect(db, pExpr->x.pSelect, iTable, pEList);
102422     }else{
102423       substExprList(db, pExpr->x.pList, iTable, pEList);
102424     }
102425   }
102426   return pExpr;
102427 }
102428 static void substExprList(
102429   sqlite3 *db,         /* Report malloc errors here */
102430   ExprList *pList,     /* List to scan and in which to make substitutes */
102431   int iTable,          /* Table to be substituted */
102432   ExprList *pEList     /* Substitute values */
102433 ){
102434   int i;
102435   if( pList==0 ) return;
102436   for(i=0; i<pList->nExpr; i++){
102437     pList->a[i].pExpr = substExpr(db, pList->a[i].pExpr, iTable, pEList);
102438   }
102439 }
102440 static void substSelect(
102441   sqlite3 *db,         /* Report malloc errors here */
102442   Select *p,           /* SELECT statement in which to make substitutions */
102443   int iTable,          /* Table to be replaced */
102444   ExprList *pEList     /* Substitute values */
102445 ){
102446   SrcList *pSrc;
102447   struct SrcList_item *pItem;
102448   int i;
102449   if( !p ) return;
102450   substExprList(db, p->pEList, iTable, pEList);
102451   substExprList(db, p->pGroupBy, iTable, pEList);
102452   substExprList(db, p->pOrderBy, iTable, pEList);
102453   p->pHaving = substExpr(db, p->pHaving, iTable, pEList);
102454   p->pWhere = substExpr(db, p->pWhere, iTable, pEList);
102455   substSelect(db, p->pPrior, iTable, pEList);
102456   pSrc = p->pSrc;
102457   assert( pSrc );  /* Even for (SELECT 1) we have: pSrc!=0 but pSrc->nSrc==0 */
102458   if( ALWAYS(pSrc) ){
102459     for(i=pSrc->nSrc, pItem=pSrc->a; i>0; i--, pItem++){
102460       substSelect(db, pItem->pSelect, iTable, pEList);
102461     }
102462   }
102463 }
102464 #endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
102465 
102466 #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
102467 /*
102468 ** This routine attempts to flatten subqueries as a performance optimization.
102469 ** This routine returns 1 if it makes changes and 0 if no flattening occurs.
102470 **
102471 ** To understand the concept of flattening, consider the following
102472 ** query:
102473 **
102474 **     SELECT a FROM (SELECT x+y AS a FROM t1 WHERE z<100) WHERE a>5
102475 **
102476 ** The default way of implementing this query is to execute the
102477 ** subquery first and store the results in a temporary table, then
102478 ** run the outer query on that temporary table.  This requires two
102479 ** passes over the data.  Furthermore, because the temporary table
102480 ** has no indices, the WHERE clause on the outer query cannot be
102481 ** optimized.
102482 **
102483 ** This routine attempts to rewrite queries such as the above into
102484 ** a single flat select, like this:
102485 **
102486 **     SELECT x+y AS a FROM t1 WHERE z<100 AND a>5
102487 **
102488 ** The code generated for this simpification gives the same result
102489 ** but only has to scan the data once.  And because indices might
102490 ** exist on the table t1, a complete scan of the data might be
102491 ** avoided.
102492 **
102493 ** Flattening is only attempted if all of the following are true:
102494 **
102495 **   (1)  The subquery and the outer query do not both use aggregates.
102496 **
102497 **   (2)  The subquery is not an aggregate or the outer query is not a join.
102498 **
102499 **   (3)  The subquery is not the right operand of a left outer join
102500 **        (Originally ticket #306.  Strengthened by ticket #3300)
102501 **
102502 **   (4)  The subquery is not DISTINCT.
102503 **
102504 **  (**)  At one point restrictions (4) and (5) defined a subset of DISTINCT
102505 **        sub-queries that were excluded from this optimization. Restriction
102506 **        (4) has since been expanded to exclude all DISTINCT subqueries.
102507 **
102508 **   (6)  The subquery does not use aggregates or the outer query is not
102509 **        DISTINCT.
102510 **
102511 **   (7)  The subquery has a FROM clause.  TODO:  For subqueries without
102512 **        A FROM clause, consider adding a FROM close with the special
102513 **        table sqlite_once that consists of a single row containing a
102514 **        single NULL.
102515 **
102516 **   (8)  The subquery does not use LIMIT or the outer query is not a join.
102517 **
102518 **   (9)  The subquery does not use LIMIT or the outer query does not use
102519 **        aggregates.
102520 **
102521 **  (10)  The subquery does not use aggregates or the outer query does not
102522 **        use LIMIT.
102523 **
102524 **  (11)  The subquery and the outer query do not both have ORDER BY clauses.
102525 **
102526 **  (**)  Not implemented.  Subsumed into restriction (3).  Was previously
102527 **        a separate restriction deriving from ticket #350.
102528 **
102529 **  (13)  The subquery and outer query do not both use LIMIT.
102530 **
102531 **  (14)  The subquery does not use OFFSET.
102532 **
102533 **  (15)  The outer query is not part of a compound select or the
102534 **        subquery does not have a LIMIT clause.
102535 **        (See ticket #2339 and ticket [02a8e81d44]).
102536 **
102537 **  (16)  The outer query is not an aggregate or the subquery does
102538 **        not contain ORDER BY.  (Ticket #2942)  This used to not matter
102539 **        until we introduced the group_concat() function.
102540 **
102541 **  (17)  The sub-query is not a compound select, or it is a UNION ALL
102542 **        compound clause made up entirely of non-aggregate queries, and
102543 **        the parent query:
102544 **
102545 **          * is not itself part of a compound select,
102546 **          * is not an aggregate or DISTINCT query, and
102547 **          * is not a join
102548 **
102549 **        The parent and sub-query may contain WHERE clauses. Subject to
102550 **        rules (11), (13) and (14), they may also contain ORDER BY,
102551 **        LIMIT and OFFSET clauses.  The subquery cannot use any compound
102552 **        operator other than UNION ALL because all the other compound
102553 **        operators have an implied DISTINCT which is disallowed by
102554 **        restriction (4).
102555 **
102556 **        Also, each component of the sub-query must return the same number
102557 **        of result columns. This is actually a requirement for any compound
102558 **        SELECT statement, but all the code here does is make sure that no
102559 **        such (illegal) sub-query is flattened. The caller will detect the
102560 **        syntax error and return a detailed message.
102561 **
102562 **  (18)  If the sub-query is a compound select, then all terms of the
102563 **        ORDER by clause of the parent must be simple references to
102564 **        columns of the sub-query.
102565 **
102566 **  (19)  The subquery does not use LIMIT or the outer query does not
102567 **        have a WHERE clause.
102568 **
102569 **  (20)  If the sub-query is a compound select, then it must not use
102570 **        an ORDER BY clause.  Ticket #3773.  We could relax this constraint
102571 **        somewhat by saying that the terms of the ORDER BY clause must
102572 **        appear as unmodified result columns in the outer query.  But we
102573 **        have other optimizations in mind to deal with that case.
102574 **
102575 **  (21)  The subquery does not use LIMIT or the outer query is not
102576 **        DISTINCT.  (See ticket [752e1646fc]).
102577 **
102578 **  (22)  The subquery is not a recursive CTE.
102579 **
102580 **  (23)  The parent is not a recursive CTE, or the sub-query is not a
102581 **        compound query. This restriction is because transforming the
102582 **        parent to a compound query confuses the code that handles
102583 **        recursive queries in multiSelect().
102584 **
102585 **
102586 ** In this routine, the "p" parameter is a pointer to the outer query.
102587 ** The subquery is p->pSrc->a[iFrom].  isAgg is true if the outer query
102588 ** uses aggregates and subqueryIsAgg is true if the subquery uses aggregates.
102589 **
102590 ** If flattening is not attempted, this routine is a no-op and returns 0.
102591 ** If flattening is attempted this routine returns 1.
102592 **
102593 ** All of the expression analysis must occur on both the outer query and
102594 ** the subquery before this routine runs.
102595 */
102596 static int flattenSubquery(
102597   Parse *pParse,       /* Parsing context */
102598   Select *p,           /* The parent or outer SELECT statement */
102599   int iFrom,           /* Index in p->pSrc->a[] of the inner subquery */
102600   int isAgg,           /* True if outer SELECT uses aggregate functions */
102601   int subqueryIsAgg    /* True if the subquery uses aggregate functions */
102602 ){
102603   const char *zSavedAuthContext = pParse->zAuthContext;
102604   Select *pParent;
102605   Select *pSub;       /* The inner query or "subquery" */
102606   Select *pSub1;      /* Pointer to the rightmost select in sub-query */
102607   SrcList *pSrc;      /* The FROM clause of the outer query */
102608   SrcList *pSubSrc;   /* The FROM clause of the subquery */
102609   ExprList *pList;    /* The result set of the outer query */
102610   int iParent;        /* VDBE cursor number of the pSub result set temp table */
102611   int i;              /* Loop counter */
102612   Expr *pWhere;                    /* The WHERE clause */
102613   struct SrcList_item *pSubitem;   /* The subquery */
102614   sqlite3 *db = pParse->db;
102615 
102616   /* Check to see if flattening is permitted.  Return 0 if not.
102617   */
102618   assert( p!=0 );
102619   assert( p->pPrior==0 );  /* Unable to flatten compound queries */
102620   if( OptimizationDisabled(db, SQLITE_QueryFlattener) ) return 0;
102621   pSrc = p->pSrc;
102622   assert( pSrc && iFrom>=0 && iFrom<pSrc->nSrc );
102623   pSubitem = &pSrc->a[iFrom];
102624   iParent = pSubitem->iCursor;
102625   pSub = pSubitem->pSelect;
102626   assert( pSub!=0 );
102627   if( isAgg && subqueryIsAgg ) return 0;                 /* Restriction (1)  */
102628   if( subqueryIsAgg && pSrc->nSrc>1 ) return 0;          /* Restriction (2)  */
102629   pSubSrc = pSub->pSrc;
102630   assert( pSubSrc );
102631   /* Prior to version 3.1.2, when LIMIT and OFFSET had to be simple constants,
102632   ** not arbitrary expresssions, we allowed some combining of LIMIT and OFFSET
102633   ** because they could be computed at compile-time.  But when LIMIT and OFFSET
102634   ** became arbitrary expressions, we were forced to add restrictions (13)
102635   ** and (14). */
102636   if( pSub->pLimit && p->pLimit ) return 0;              /* Restriction (13) */
102637   if( pSub->pOffset ) return 0;                          /* Restriction (14) */
102638   if( p->pRightmost && pSub->pLimit ){
102639     return 0;                                            /* Restriction (15) */
102640   }
102641   if( pSubSrc->nSrc==0 ) return 0;                       /* Restriction (7)  */
102642   if( pSub->selFlags & SF_Distinct ) return 0;           /* Restriction (5)  */
102643   if( pSub->pLimit && (pSrc->nSrc>1 || isAgg) ){
102644      return 0;         /* Restrictions (8)(9) */
102645   }
102646   if( (p->selFlags & SF_Distinct)!=0 && subqueryIsAgg ){
102647      return 0;         /* Restriction (6)  */
102648   }
102649   if( p->pOrderBy && pSub->pOrderBy ){
102650      return 0;                                           /* Restriction (11) */
102651   }
102652   if( isAgg && pSub->pOrderBy ) return 0;                /* Restriction (16) */
102653   if( pSub->pLimit && p->pWhere ) return 0;              /* Restriction (19) */
102654   if( pSub->pLimit && (p->selFlags & SF_Distinct)!=0 ){
102655      return 0;         /* Restriction (21) */
102656   }
102657   if( pSub->selFlags & SF_Recursive ) return 0;          /* Restriction (22)  */
102658   if( (p->selFlags & SF_Recursive) && pSub->pPrior ) return 0;       /* (23)  */
102659 
102660   /* OBSOLETE COMMENT 1:
102661   ** Restriction 3:  If the subquery is a join, make sure the subquery is
102662   ** not used as the right operand of an outer join.  Examples of why this
102663   ** is not allowed:
102664   **
102665   **         t1 LEFT OUTER JOIN (t2 JOIN t3)
102666   **
102667   ** If we flatten the above, we would get
102668   **
102669   **         (t1 LEFT OUTER JOIN t2) JOIN t3
102670   **
102671   ** which is not at all the same thing.
102672   **
102673   ** OBSOLETE COMMENT 2:
102674   ** Restriction 12:  If the subquery is the right operand of a left outer
102675   ** join, make sure the subquery has no WHERE clause.
102676   ** An examples of why this is not allowed:
102677   **
102678   **         t1 LEFT OUTER JOIN (SELECT * FROM t2 WHERE t2.x>0)
102679   **
102680   ** If we flatten the above, we would get
102681   **
102682   **         (t1 LEFT OUTER JOIN t2) WHERE t2.x>0
102683   **
102684   ** But the t2.x>0 test will always fail on a NULL row of t2, which
102685   ** effectively converts the OUTER JOIN into an INNER JOIN.
102686   **
102687   ** THIS OVERRIDES OBSOLETE COMMENTS 1 AND 2 ABOVE:
102688   ** Ticket #3300 shows that flattening the right term of a LEFT JOIN
102689   ** is fraught with danger.  Best to avoid the whole thing.  If the
102690   ** subquery is the right term of a LEFT JOIN, then do not flatten.
102691   */
102692   if( (pSubitem->jointype & JT_OUTER)!=0 ){
102693     return 0;
102694   }
102695 
102696   /* Restriction 17: If the sub-query is a compound SELECT, then it must
102697   ** use only the UNION ALL operator. And none of the simple select queries
102698   ** that make up the compound SELECT are allowed to be aggregate or distinct
102699   ** queries.
102700   */
102701   if( pSub->pPrior ){
102702     if( pSub->pOrderBy ){
102703       return 0;  /* Restriction 20 */
102704     }
102705     if( isAgg || (p->selFlags & SF_Distinct)!=0 || pSrc->nSrc!=1 ){
102706       return 0;
102707     }
102708     for(pSub1=pSub; pSub1; pSub1=pSub1->pPrior){
102709       testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct );
102710       testcase( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))==SF_Aggregate );
102711       assert( pSub->pSrc!=0 );
102712       if( (pSub1->selFlags & (SF_Distinct|SF_Aggregate))!=0
102713        || (pSub1->pPrior && pSub1->op!=TK_ALL)
102714        || pSub1->pSrc->nSrc<1
102715        || pSub->pEList->nExpr!=pSub1->pEList->nExpr
102716       ){
102717         return 0;
102718       }
102719       testcase( pSub1->pSrc->nSrc>1 );
102720     }
102721 
102722     /* Restriction 18. */
102723     if( p->pOrderBy ){
102724       int ii;
102725       for(ii=0; ii<p->pOrderBy->nExpr; ii++){
102726         if( p->pOrderBy->a[ii].u.x.iOrderByCol==0 ) return 0;
102727       }
102728     }
102729   }
102730 
102731   /***** If we reach this point, flattening is permitted. *****/
102732 
102733   /* Authorize the subquery */
102734   pParse->zAuthContext = pSubitem->zName;
102735   TESTONLY(i =) sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0);
102736   testcase( i==SQLITE_DENY );
102737   pParse->zAuthContext = zSavedAuthContext;
102738 
102739   /* If the sub-query is a compound SELECT statement, then (by restrictions
102740   ** 17 and 18 above) it must be a UNION ALL and the parent query must
102741   ** be of the form:
102742   **
102743   **     SELECT <expr-list> FROM (<sub-query>) <where-clause>
102744   **
102745   ** followed by any ORDER BY, LIMIT and/or OFFSET clauses. This block
102746   ** creates N-1 copies of the parent query without any ORDER BY, LIMIT or
102747   ** OFFSET clauses and joins them to the left-hand-side of the original
102748   ** using UNION ALL operators. In this case N is the number of simple
102749   ** select statements in the compound sub-query.
102750   **
102751   ** Example:
102752   **
102753   **     SELECT a+1 FROM (
102754   **        SELECT x FROM tab
102755   **        UNION ALL
102756   **        SELECT y FROM tab
102757   **        UNION ALL
102758   **        SELECT abs(z*2) FROM tab2
102759   **     ) WHERE a!=5 ORDER BY 1
102760   **
102761   ** Transformed into:
102762   **
102763   **     SELECT x+1 FROM tab WHERE x+1!=5
102764   **     UNION ALL
102765   **     SELECT y+1 FROM tab WHERE y+1!=5
102766   **     UNION ALL
102767   **     SELECT abs(z*2)+1 FROM tab2 WHERE abs(z*2)+1!=5
102768   **     ORDER BY 1
102769   **
102770   ** We call this the "compound-subquery flattening".
102771   */
102772   for(pSub=pSub->pPrior; pSub; pSub=pSub->pPrior){
102773     Select *pNew;
102774     ExprList *pOrderBy = p->pOrderBy;
102775     Expr *pLimit = p->pLimit;
102776     Expr *pOffset = p->pOffset;
102777     Select *pPrior = p->pPrior;
102778     p->pOrderBy = 0;
102779     p->pSrc = 0;
102780     p->pPrior = 0;
102781     p->pLimit = 0;
102782     p->pOffset = 0;
102783     pNew = sqlite3SelectDup(db, p, 0);
102784     p->pOffset = pOffset;
102785     p->pLimit = pLimit;
102786     p->pOrderBy = pOrderBy;
102787     p->pSrc = pSrc;
102788     p->op = TK_ALL;
102789     p->pRightmost = 0;
102790     if( pNew==0 ){
102791       pNew = pPrior;
102792     }else{
102793       pNew->pPrior = pPrior;
102794       pNew->pRightmost = 0;
102795     }
102796     p->pPrior = pNew;
102797     if( db->mallocFailed ) return 1;
102798   }
102799 
102800   /* Begin flattening the iFrom-th entry of the FROM clause
102801   ** in the outer query.
102802   */
102803   pSub = pSub1 = pSubitem->pSelect;
102804 
102805   /* Delete the transient table structure associated with the
102806   ** subquery
102807   */
102808   sqlite3DbFree(db, pSubitem->zDatabase);
102809   sqlite3DbFree(db, pSubitem->zName);
102810   sqlite3DbFree(db, pSubitem->zAlias);
102811   pSubitem->zDatabase = 0;
102812   pSubitem->zName = 0;
102813   pSubitem->zAlias = 0;
102814   pSubitem->pSelect = 0;
102815 
102816   /* Defer deleting the Table object associated with the
102817   ** subquery until code generation is
102818   ** complete, since there may still exist Expr.pTab entries that
102819   ** refer to the subquery even after flattening.  Ticket #3346.
102820   **
102821   ** pSubitem->pTab is always non-NULL by test restrictions and tests above.
102822   */
102823   if( ALWAYS(pSubitem->pTab!=0) ){
102824     Table *pTabToDel = pSubitem->pTab;
102825     if( pTabToDel->nRef==1 ){
102826       Parse *pToplevel = sqlite3ParseToplevel(pParse);
102827       pTabToDel->pNextZombie = pToplevel->pZombieTab;
102828       pToplevel->pZombieTab = pTabToDel;
102829     }else{
102830       pTabToDel->nRef--;
102831     }
102832     pSubitem->pTab = 0;
102833   }
102834 
102835   /* The following loop runs once for each term in a compound-subquery
102836   ** flattening (as described above).  If we are doing a different kind
102837   ** of flattening - a flattening other than a compound-subquery flattening -
102838   ** then this loop only runs once.
102839   **
102840   ** This loop moves all of the FROM elements of the subquery into the
102841   ** the FROM clause of the outer query.  Before doing this, remember
102842   ** the cursor number for the original outer query FROM element in
102843   ** iParent.  The iParent cursor will never be used.  Subsequent code
102844   ** will scan expressions looking for iParent references and replace
102845   ** those references with expressions that resolve to the subquery FROM
102846   ** elements we are now copying in.
102847   */
102848   for(pParent=p; pParent; pParent=pParent->pPrior, pSub=pSub->pPrior){
102849     int nSubSrc;
102850     u8 jointype = 0;
102851     pSubSrc = pSub->pSrc;     /* FROM clause of subquery */
102852     nSubSrc = pSubSrc->nSrc;  /* Number of terms in subquery FROM clause */
102853     pSrc = pParent->pSrc;     /* FROM clause of the outer query */
102854 
102855     if( pSrc ){
102856       assert( pParent==p );  /* First time through the loop */
102857       jointype = pSubitem->jointype;
102858     }else{
102859       assert( pParent!=p );  /* 2nd and subsequent times through the loop */
102860       pSrc = pParent->pSrc = sqlite3SrcListAppend(db, 0, 0, 0);
102861       if( pSrc==0 ){
102862         assert( db->mallocFailed );
102863         break;
102864       }
102865     }
102866 
102867     /* The subquery uses a single slot of the FROM clause of the outer
102868     ** query.  If the subquery has more than one element in its FROM clause,
102869     ** then expand the outer query to make space for it to hold all elements
102870     ** of the subquery.
102871     **
102872     ** Example:
102873     **
102874     **    SELECT * FROM tabA, (SELECT * FROM sub1, sub2), tabB;
102875     **
102876     ** The outer query has 3 slots in its FROM clause.  One slot of the
102877     ** outer query (the middle slot) is used by the subquery.  The next
102878     ** block of code will expand the out query to 4 slots.  The middle
102879     ** slot is expanded to two slots in order to make space for the
102880     ** two elements in the FROM clause of the subquery.
102881     */
102882     if( nSubSrc>1 ){
102883       pParent->pSrc = pSrc = sqlite3SrcListEnlarge(db, pSrc, nSubSrc-1,iFrom+1);
102884       if( db->mallocFailed ){
102885         break;
102886       }
102887     }
102888 
102889     /* Transfer the FROM clause terms from the subquery into the
102890     ** outer query.
102891     */
102892     for(i=0; i<nSubSrc; i++){
102893       sqlite3IdListDelete(db, pSrc->a[i+iFrom].pUsing);
102894       pSrc->a[i+iFrom] = pSubSrc->a[i];
102895       memset(&pSubSrc->a[i], 0, sizeof(pSubSrc->a[i]));
102896     }
102897     pSrc->a[iFrom].jointype = jointype;
102898 
102899     /* Now begin substituting subquery result set expressions for
102900     ** references to the iParent in the outer query.
102901     **
102902     ** Example:
102903     **
102904     **   SELECT a+5, b*10 FROM (SELECT x*3 AS a, y+10 AS b FROM t1) WHERE a>b;
102905     **   \                     \_____________ subquery __________/          /
102906     **    \_____________________ outer query ______________________________/
102907     **
102908     ** We look at every expression in the outer query and every place we see
102909     ** "a" we substitute "x*3" and every place we see "b" we substitute "y+10".
102910     */
102911     pList = pParent->pEList;
102912     for(i=0; i<pList->nExpr; i++){
102913       if( pList->a[i].zName==0 ){
102914         char *zName = sqlite3DbStrDup(db, pList->a[i].zSpan);
102915         sqlite3Dequote(zName);
102916         pList->a[i].zName = zName;
102917       }
102918     }
102919     substExprList(db, pParent->pEList, iParent, pSub->pEList);
102920     if( isAgg ){
102921       substExprList(db, pParent->pGroupBy, iParent, pSub->pEList);
102922       pParent->pHaving = substExpr(db, pParent->pHaving, iParent, pSub->pEList);
102923     }
102924     if( pSub->pOrderBy ){
102925       assert( pParent->pOrderBy==0 );
102926       pParent->pOrderBy = pSub->pOrderBy;
102927       pSub->pOrderBy = 0;
102928     }else if( pParent->pOrderBy ){
102929       substExprList(db, pParent->pOrderBy, iParent, pSub->pEList);
102930     }
102931     if( pSub->pWhere ){
102932       pWhere = sqlite3ExprDup(db, pSub->pWhere, 0);
102933     }else{
102934       pWhere = 0;
102935     }
102936     if( subqueryIsAgg ){
102937       assert( pParent->pHaving==0 );
102938       pParent->pHaving = pParent->pWhere;
102939       pParent->pWhere = pWhere;
102940       pParent->pHaving = substExpr(db, pParent->pHaving, iParent, pSub->pEList);
102941       pParent->pHaving = sqlite3ExprAnd(db, pParent->pHaving,
102942                                   sqlite3ExprDup(db, pSub->pHaving, 0));
102943       assert( pParent->pGroupBy==0 );
102944       pParent->pGroupBy = sqlite3ExprListDup(db, pSub->pGroupBy, 0);
102945     }else{
102946       pParent->pWhere = substExpr(db, pParent->pWhere, iParent, pSub->pEList);
102947       pParent->pWhere = sqlite3ExprAnd(db, pParent->pWhere, pWhere);
102948     }
102949 
102950     /* The flattened query is distinct if either the inner or the
102951     ** outer query is distinct.
102952     */
102953     pParent->selFlags |= pSub->selFlags & SF_Distinct;
102954 
102955     /*
102956     ** SELECT ... FROM (SELECT ... LIMIT a OFFSET b) LIMIT x OFFSET y;
102957     **
102958     ** One is tempted to try to add a and b to combine the limits.  But this
102959     ** does not work if either limit is negative.
102960     */
102961     if( pSub->pLimit ){
102962       pParent->pLimit = pSub->pLimit;
102963       pSub->pLimit = 0;
102964     }
102965   }
102966 
102967   /* Finially, delete what is left of the subquery and return
102968   ** success.
102969   */
102970   sqlite3SelectDelete(db, pSub1);
102971 
102972   return 1;
102973 }
102974 #endif /* !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW) */
102975 
102976 /*
102977 ** Based on the contents of the AggInfo structure indicated by the first
102978 ** argument, this function checks if the following are true:
102979 **
102980 **    * the query contains just a single aggregate function,
102981 **    * the aggregate function is either min() or max(), and
102982 **    * the argument to the aggregate function is a column value.
102983 **
102984 ** If all of the above are true, then WHERE_ORDERBY_MIN or WHERE_ORDERBY_MAX
102985 ** is returned as appropriate. Also, *ppMinMax is set to point to the
102986 ** list of arguments passed to the aggregate before returning.
102987 **
102988 ** Or, if the conditions above are not met, *ppMinMax is set to 0 and
102989 ** WHERE_ORDERBY_NORMAL is returned.
102990 */
102991 static u8 minMaxQuery(AggInfo *pAggInfo, ExprList **ppMinMax){
102992   int eRet = WHERE_ORDERBY_NORMAL;          /* Return value */
102993 
102994   *ppMinMax = 0;
102995   if( pAggInfo->nFunc==1 ){
102996     Expr *pExpr = pAggInfo->aFunc[0].pExpr; /* Aggregate function */
102997     ExprList *pEList = pExpr->x.pList;      /* Arguments to agg function */
102998 
102999     assert( pExpr->op==TK_AGG_FUNCTION );
103000     if( pEList && pEList->nExpr==1 && pEList->a[0].pExpr->op==TK_AGG_COLUMN ){
103001       const char *zFunc = pExpr->u.zToken;
103002       if( sqlite3StrICmp(zFunc, "min")==0 ){
103003         eRet = WHERE_ORDERBY_MIN;
103004         *ppMinMax = pEList;
103005       }else if( sqlite3StrICmp(zFunc, "max")==0 ){
103006         eRet = WHERE_ORDERBY_MAX;
103007         *ppMinMax = pEList;
103008       }
103009     }
103010   }
103011 
103012   assert( *ppMinMax==0 || (*ppMinMax)->nExpr==1 );
103013   return eRet;
103014 }
103015 
103016 /*
103017 ** The select statement passed as the first argument is an aggregate query.
103018 ** The second argment is the associated aggregate-info object. This
103019 ** function tests if the SELECT is of the form:
103020 **
103021 **   SELECT count(*) FROM <tbl>
103022 **
103023 ** where table is a database table, not a sub-select or view. If the query
103024 ** does match this pattern, then a pointer to the Table object representing
103025 ** <tbl> is returned. Otherwise, 0 is returned.
103026 */
103027 static Table *isSimpleCount(Select *p, AggInfo *pAggInfo){
103028   Table *pTab;
103029   Expr *pExpr;
103030 
103031   assert( !p->pGroupBy );
103032 
103033   if( p->pWhere || p->pEList->nExpr!=1
103034    || p->pSrc->nSrc!=1 || p->pSrc->a[0].pSelect
103035   ){
103036     return 0;
103037   }
103038   pTab = p->pSrc->a[0].pTab;
103039   pExpr = p->pEList->a[0].pExpr;
103040   assert( pTab && !pTab->pSelect && pExpr );
103041 
103042   if( IsVirtual(pTab) ) return 0;
103043   if( pExpr->op!=TK_AGG_FUNCTION ) return 0;
103044   if( NEVER(pAggInfo->nFunc==0) ) return 0;
103045   if( (pAggInfo->aFunc[0].pFunc->funcFlags&SQLITE_FUNC_COUNT)==0 ) return 0;
103046   if( pExpr->flags&EP_Distinct ) return 0;
103047 
103048   return pTab;
103049 }
103050 
103051 /*
103052 ** If the source-list item passed as an argument was augmented with an
103053 ** INDEXED BY clause, then try to locate the specified index. If there
103054 ** was such a clause and the named index cannot be found, return
103055 ** SQLITE_ERROR and leave an error in pParse. Otherwise, populate
103056 ** pFrom->pIndex and return SQLITE_OK.
103057 */
103058 SQLITE_PRIVATE int sqlite3IndexedByLookup(Parse *pParse, struct SrcList_item *pFrom){
103059   if( pFrom->pTab && pFrom->zIndex ){
103060     Table *pTab = pFrom->pTab;
103061     char *zIndex = pFrom->zIndex;
103062     Index *pIdx;
103063     for(pIdx=pTab->pIndex;
103064         pIdx && sqlite3StrICmp(pIdx->zName, zIndex);
103065         pIdx=pIdx->pNext
103066     );
103067     if( !pIdx ){
103068       sqlite3ErrorMsg(pParse, "no such index: %s", zIndex, 0);
103069       pParse->checkSchema = 1;
103070       return SQLITE_ERROR;
103071     }
103072     pFrom->pIndex = pIdx;
103073   }
103074   return SQLITE_OK;
103075 }
103076 /*
103077 ** Detect compound SELECT statements that use an ORDER BY clause with
103078 ** an alternative collating sequence.
103079 **
103080 **    SELECT ... FROM t1 EXCEPT SELECT ... FROM t2 ORDER BY .. COLLATE ...
103081 **
103082 ** These are rewritten as a subquery:
103083 **
103084 **    SELECT * FROM (SELECT ... FROM t1 EXCEPT SELECT ... FROM t2)
103085 **     ORDER BY ... COLLATE ...
103086 **
103087 ** This transformation is necessary because the multiSelectOrderBy() routine
103088 ** above that generates the code for a compound SELECT with an ORDER BY clause
103089 ** uses a merge algorithm that requires the same collating sequence on the
103090 ** result columns as on the ORDER BY clause.  See ticket
103091 ** http://www.sqlite.org/src/info/6709574d2a
103092 **
103093 ** This transformation is only needed for EXCEPT, INTERSECT, and UNION.
103094 ** The UNION ALL operator works fine with multiSelectOrderBy() even when
103095 ** there are COLLATE terms in the ORDER BY.
103096 */
103097 static int convertCompoundSelectToSubquery(Walker *pWalker, Select *p){
103098   int i;
103099   Select *pNew;
103100   Select *pX;
103101   sqlite3 *db;
103102   struct ExprList_item *a;
103103   SrcList *pNewSrc;
103104   Parse *pParse;
103105   Token dummy;
103106 
103107   if( p->pPrior==0 ) return WRC_Continue;
103108   if( p->pOrderBy==0 ) return WRC_Continue;
103109   for(pX=p; pX && (pX->op==TK_ALL || pX->op==TK_SELECT); pX=pX->pPrior){}
103110   if( pX==0 ) return WRC_Continue;
103111   a = p->pOrderBy->a;
103112   for(i=p->pOrderBy->nExpr-1; i>=0; i--){
103113     if( a[i].pExpr->flags & EP_Collate ) break;
103114   }
103115   if( i<0 ) return WRC_Continue;
103116 
103117   /* If we reach this point, that means the transformation is required. */
103118 
103119   pParse = pWalker->pParse;
103120   db = pParse->db;
103121   pNew = sqlite3DbMallocZero(db, sizeof(*pNew) );
103122   if( pNew==0 ) return WRC_Abort;
103123   memset(&dummy, 0, sizeof(dummy));
103124   pNewSrc = sqlite3SrcListAppendFromTerm(pParse,0,0,0,&dummy,pNew,0,0);
103125   if( pNewSrc==0 ) return WRC_Abort;
103126   *pNew = *p;
103127   p->pSrc = pNewSrc;
103128   p->pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(db, TK_ALL, 0));
103129   p->op = TK_SELECT;
103130   p->pWhere = 0;
103131   pNew->pGroupBy = 0;
103132   pNew->pHaving = 0;
103133   pNew->pOrderBy = 0;
103134   p->pPrior = 0;
103135   pNew->pLimit = 0;
103136   pNew->pOffset = 0;
103137   return WRC_Continue;
103138 }
103139 
103140 #ifndef SQLITE_OMIT_CTE
103141 /*
103142 ** Argument pWith (which may be NULL) points to a linked list of nested
103143 ** WITH contexts, from inner to outermost. If the table identified by
103144 ** FROM clause element pItem is really a common-table-expression (CTE)
103145 ** then return a pointer to the CTE definition for that table. Otherwise
103146 ** return NULL.
103147 **
103148 ** If a non-NULL value is returned, set *ppContext to point to the With
103149 ** object that the returned CTE belongs to.
103150 */
103151 static struct Cte *searchWith(
103152   With *pWith,                    /* Current outermost WITH clause */
103153   struct SrcList_item *pItem,     /* FROM clause element to resolve */
103154   With **ppContext                /* OUT: WITH clause return value belongs to */
103155 ){
103156   const char *zName;
103157   if( pItem->zDatabase==0 && (zName = pItem->zName)!=0 ){
103158     With *p;
103159     for(p=pWith; p; p=p->pOuter){
103160       int i;
103161       for(i=0; i<p->nCte; i++){
103162         if( sqlite3StrICmp(zName, p->a[i].zName)==0 ){
103163           *ppContext = p;
103164           return &p->a[i];
103165         }
103166       }
103167     }
103168   }
103169   return 0;
103170 }
103171 
103172 /* The code generator maintains a stack of active WITH clauses
103173 ** with the inner-most WITH clause being at the top of the stack.
103174 **
103175 ** This routine pushes the WITH clause passed as the second argument
103176 ** onto the top of the stack. If argument bFree is true, then this
103177 ** WITH clause will never be popped from the stack. In this case it
103178 ** should be freed along with the Parse object. In other cases, when
103179 ** bFree==0, the With object will be freed along with the SELECT
103180 ** statement with which it is associated.
103181 */
103182 SQLITE_PRIVATE void sqlite3WithPush(Parse *pParse, With *pWith, u8 bFree){
103183   assert( bFree==0 || pParse->pWith==0 );
103184   if( pWith ){
103185     pWith->pOuter = pParse->pWith;
103186     pParse->pWith = pWith;
103187     pParse->bFreeWith = bFree;
103188   }
103189 }
103190 
103191 /*
103192 ** This function checks if argument pFrom refers to a CTE declared by
103193 ** a WITH clause on the stack currently maintained by the parser. And,
103194 ** if currently processing a CTE expression, if it is a recursive
103195 ** reference to the current CTE.
103196 **
103197 ** If pFrom falls into either of the two categories above, pFrom->pTab
103198 ** and other fields are populated accordingly. The caller should check
103199 ** (pFrom->pTab!=0) to determine whether or not a successful match
103200 ** was found.
103201 **
103202 ** Whether or not a match is found, SQLITE_OK is returned if no error
103203 ** occurs. If an error does occur, an error message is stored in the
103204 ** parser and some error code other than SQLITE_OK returned.
103205 */
103206 static int withExpand(
103207   Walker *pWalker,
103208   struct SrcList_item *pFrom
103209 ){
103210   Parse *pParse = pWalker->pParse;
103211   sqlite3 *db = pParse->db;
103212   struct Cte *pCte;               /* Matched CTE (or NULL if no match) */
103213   With *pWith;                    /* WITH clause that pCte belongs to */
103214 
103215   assert( pFrom->pTab==0 );
103216 
103217   pCte = searchWith(pParse->pWith, pFrom, &pWith);
103218   if( pCte ){
103219     Table *pTab;
103220     ExprList *pEList;
103221     Select *pSel;
103222     Select *pLeft;                /* Left-most SELECT statement */
103223     int bMayRecursive;            /* True if compound joined by UNION [ALL] */
103224     With *pSavedWith;             /* Initial value of pParse->pWith */
103225 
103226     /* If pCte->zErr is non-NULL at this point, then this is an illegal
103227     ** recursive reference to CTE pCte. Leave an error in pParse and return
103228     ** early. If pCte->zErr is NULL, then this is not a recursive reference.
103229     ** In this case, proceed.  */
103230     if( pCte->zErr ){
103231       sqlite3ErrorMsg(pParse, pCte->zErr, pCte->zName);
103232       return SQLITE_ERROR;
103233     }
103234 
103235     assert( pFrom->pTab==0 );
103236     pFrom->pTab = pTab = sqlite3DbMallocZero(db, sizeof(Table));
103237     if( pTab==0 ) return WRC_Abort;
103238     pTab->nRef = 1;
103239     pTab->zName = sqlite3DbStrDup(db, pCte->zName);
103240     pTab->iPKey = -1;
103241     pTab->nRowEst = 1048576;
103242     pTab->tabFlags |= TF_Ephemeral;
103243     pFrom->pSelect = sqlite3SelectDup(db, pCte->pSelect, 0);
103244     if( db->mallocFailed ) return SQLITE_NOMEM;
103245     assert( pFrom->pSelect );
103246 
103247     /* Check if this is a recursive CTE. */
103248     pSel = pFrom->pSelect;
103249     bMayRecursive = ( pSel->op==TK_ALL || pSel->op==TK_UNION );
103250     if( bMayRecursive ){
103251       int i;
103252       SrcList *pSrc = pFrom->pSelect->pSrc;
103253       for(i=0; i<pSrc->nSrc; i++){
103254         struct SrcList_item *pItem = &pSrc->a[i];
103255         if( pItem->zDatabase==0
103256          && pItem->zName!=0
103257          && 0==sqlite3StrICmp(pItem->zName, pCte->zName)
103258           ){
103259           pItem->pTab = pTab;
103260           pItem->isRecursive = 1;
103261           pTab->nRef++;
103262           pSel->selFlags |= SF_Recursive;
103263         }
103264       }
103265     }
103266 
103267     /* Only one recursive reference is permitted. */
103268     if( pTab->nRef>2 ){
103269       sqlite3ErrorMsg(
103270           pParse, "multiple references to recursive table: %s", pCte->zName
103271       );
103272       return SQLITE_ERROR;
103273     }
103274     assert( pTab->nRef==1 || ((pSel->selFlags&SF_Recursive) && pTab->nRef==2 ));
103275 
103276     pCte->zErr = "circular reference: %s";
103277     pSavedWith = pParse->pWith;
103278     pParse->pWith = pWith;
103279     sqlite3WalkSelect(pWalker, bMayRecursive ? pSel->pPrior : pSel);
103280 
103281     for(pLeft=pSel; pLeft->pPrior; pLeft=pLeft->pPrior);
103282     pEList = pLeft->pEList;
103283     if( pCte->pCols ){
103284       if( pEList->nExpr!=pCte->pCols->nExpr ){
103285         sqlite3ErrorMsg(pParse, "table %s has %d values for %d columns",
103286             pCte->zName, pEList->nExpr, pCte->pCols->nExpr
103287         );
103288         pParse->pWith = pSavedWith;
103289         return SQLITE_ERROR;
103290       }
103291       pEList = pCte->pCols;
103292     }
103293 
103294     selectColumnsFromExprList(pParse, pEList, &pTab->nCol, &pTab->aCol);
103295     if( bMayRecursive ){
103296       if( pSel->selFlags & SF_Recursive ){
103297         pCte->zErr = "multiple recursive references: %s";
103298       }else{
103299         pCte->zErr = "recursive reference in a subquery: %s";
103300       }
103301       sqlite3WalkSelect(pWalker, pSel);
103302     }
103303     pCte->zErr = 0;
103304     pParse->pWith = pSavedWith;
103305   }
103306 
103307   return SQLITE_OK;
103308 }
103309 #endif
103310 
103311 #ifndef SQLITE_OMIT_CTE
103312 /*
103313 ** If the SELECT passed as the second argument has an associated WITH
103314 ** clause, pop it from the stack stored as part of the Parse object.
103315 **
103316 ** This function is used as the xSelectCallback2() callback by
103317 ** sqlite3SelectExpand() when walking a SELECT tree to resolve table
103318 ** names and other FROM clause elements.
103319 */
103320 static void selectPopWith(Walker *pWalker, Select *p){
103321   Parse *pParse = pWalker->pParse;
103322   if( p->pWith ){
103323     assert( pParse->pWith==p->pWith );
103324     pParse->pWith = p->pWith->pOuter;
103325   }
103326 }
103327 #else
103328 #define selectPopWith 0
103329 #endif
103330 
103331 /*
103332 ** This routine is a Walker callback for "expanding" a SELECT statement.
103333 ** "Expanding" means to do the following:
103334 **
103335 **    (1)  Make sure VDBE cursor numbers have been assigned to every
103336 **         element of the FROM clause.
103337 **
103338 **    (2)  Fill in the pTabList->a[].pTab fields in the SrcList that
103339 **         defines FROM clause.  When views appear in the FROM clause,
103340 **         fill pTabList->a[].pSelect with a copy of the SELECT statement
103341 **         that implements the view.  A copy is made of the view's SELECT
103342 **         statement so that we can freely modify or delete that statement
103343 **         without worrying about messing up the presistent representation
103344 **         of the view.
103345 **
103346 **    (3)  Add terms to the WHERE clause to accomodate the NATURAL keyword
103347 **         on joins and the ON and USING clause of joins.
103348 **
103349 **    (4)  Scan the list of columns in the result set (pEList) looking
103350 **         for instances of the "*" operator or the TABLE.* operator.
103351 **         If found, expand each "*" to be every column in every table
103352 **         and TABLE.* to be every column in TABLE.
103353 **
103354 */
103355 static int selectExpander(Walker *pWalker, Select *p){
103356   Parse *pParse = pWalker->pParse;
103357   int i, j, k;
103358   SrcList *pTabList;
103359   ExprList *pEList;
103360   struct SrcList_item *pFrom;
103361   sqlite3 *db = pParse->db;
103362   Expr *pE, *pRight, *pExpr;
103363   u16 selFlags = p->selFlags;
103364 
103365   p->selFlags |= SF_Expanded;
103366   if( db->mallocFailed  ){
103367     return WRC_Abort;
103368   }
103369   if( NEVER(p->pSrc==0) || (selFlags & SF_Expanded)!=0 ){
103370     return WRC_Prune;
103371   }
103372   pTabList = p->pSrc;
103373   pEList = p->pEList;
103374   sqlite3WithPush(pParse, p->pWith, 0);
103375 
103376   /* Make sure cursor numbers have been assigned to all entries in
103377   ** the FROM clause of the SELECT statement.
103378   */
103379   sqlite3SrcListAssignCursors(pParse, pTabList);
103380 
103381   /* Look up every table named in the FROM clause of the select.  If
103382   ** an entry of the FROM clause is a subquery instead of a table or view,
103383   ** then create a transient table structure to describe the subquery.
103384   */
103385   for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
103386     Table *pTab;
103387     assert( pFrom->isRecursive==0 || pFrom->pTab );
103388     if( pFrom->isRecursive ) continue;
103389     if( pFrom->pTab!=0 ){
103390       /* This statement has already been prepared.  There is no need
103391       ** to go further. */
103392       assert( i==0 );
103393 #ifndef SQLITE_OMIT_CTE
103394       selectPopWith(pWalker, p);
103395 #endif
103396       return WRC_Prune;
103397     }
103398 #ifndef SQLITE_OMIT_CTE
103399     if( withExpand(pWalker, pFrom) ) return WRC_Abort;
103400     if( pFrom->pTab ) {} else
103401 #endif
103402     if( pFrom->zName==0 ){
103403 #ifndef SQLITE_OMIT_SUBQUERY
103404       Select *pSel = pFrom->pSelect;
103405       /* A sub-query in the FROM clause of a SELECT */
103406       assert( pSel!=0 );
103407       assert( pFrom->pTab==0 );
103408       sqlite3WalkSelect(pWalker, pSel);
103409       pFrom->pTab = pTab = sqlite3DbMallocZero(db, sizeof(Table));
103410       if( pTab==0 ) return WRC_Abort;
103411       pTab->nRef = 1;
103412       pTab->zName = sqlite3MPrintf(db, "sqlite_sq_%p", (void*)pTab);
103413       while( pSel->pPrior ){ pSel = pSel->pPrior; }
103414       selectColumnsFromExprList(pParse, pSel->pEList, &pTab->nCol, &pTab->aCol);
103415       pTab->iPKey = -1;
103416       pTab->nRowEst = 1048576;
103417       pTab->tabFlags |= TF_Ephemeral;
103418 #endif
103419     }else{
103420       /* An ordinary table or view name in the FROM clause */
103421       assert( pFrom->pTab==0 );
103422       pFrom->pTab = pTab = sqlite3LocateTableItem(pParse, 0, pFrom);
103423       if( pTab==0 ) return WRC_Abort;
103424       if( pTab->nRef==0xffff ){
103425         sqlite3ErrorMsg(pParse, "too many references to \"%s\": max 65535",
103426            pTab->zName);
103427         pFrom->pTab = 0;
103428         return WRC_Abort;
103429       }
103430       pTab->nRef++;
103431 #if !defined(SQLITE_OMIT_VIEW) || !defined (SQLITE_OMIT_VIRTUALTABLE)
103432       if( pTab->pSelect || IsVirtual(pTab) ){
103433         /* We reach here if the named table is a really a view */
103434         if( sqlite3ViewGetColumnNames(pParse, pTab) ) return WRC_Abort;
103435         assert( pFrom->pSelect==0 );
103436         pFrom->pSelect = sqlite3SelectDup(db, pTab->pSelect, 0);
103437         sqlite3WalkSelect(pWalker, pFrom->pSelect);
103438       }
103439 #endif
103440     }
103441 
103442     /* Locate the index named by the INDEXED BY clause, if any. */
103443     if( sqlite3IndexedByLookup(pParse, pFrom) ){
103444       return WRC_Abort;
103445     }
103446   }
103447 
103448   /* Process NATURAL keywords, and ON and USING clauses of joins.
103449   */
103450   if( db->mallocFailed || sqliteProcessJoin(pParse, p) ){
103451     return WRC_Abort;
103452   }
103453 
103454   /* For every "*" that occurs in the column list, insert the names of
103455   ** all columns in all tables.  And for every TABLE.* insert the names
103456   ** of all columns in TABLE.  The parser inserted a special expression
103457   ** with the TK_ALL operator for each "*" that it found in the column list.
103458   ** The following code just has to locate the TK_ALL expressions and expand
103459   ** each one to the list of all columns in all tables.
103460   **
103461   ** The first loop just checks to see if there are any "*" operators
103462   ** that need expanding.
103463   */
103464   for(k=0; k<pEList->nExpr; k++){
103465     pE = pEList->a[k].pExpr;
103466     if( pE->op==TK_ALL ) break;
103467     assert( pE->op!=TK_DOT || pE->pRight!=0 );
103468     assert( pE->op!=TK_DOT || (pE->pLeft!=0 && pE->pLeft->op==TK_ID) );
103469     if( pE->op==TK_DOT && pE->pRight->op==TK_ALL ) break;
103470   }
103471   if( k<pEList->nExpr ){
103472     /*
103473     ** If we get here it means the result set contains one or more "*"
103474     ** operators that need to be expanded.  Loop through each expression
103475     ** in the result set and expand them one by one.
103476     */
103477     struct ExprList_item *a = pEList->a;
103478     ExprList *pNew = 0;
103479     int flags = pParse->db->flags;
103480     int longNames = (flags & SQLITE_FullColNames)!=0
103481                       && (flags & SQLITE_ShortColNames)==0;
103482 
103483     /* When processing FROM-clause subqueries, it is always the case
103484     ** that full_column_names=OFF and short_column_names=ON.  The
103485     ** sqlite3ResultSetOfSelect() routine makes it so. */
103486     assert( (p->selFlags & SF_NestedFrom)==0
103487           || ((flags & SQLITE_FullColNames)==0 &&
103488               (flags & SQLITE_ShortColNames)!=0) );
103489 
103490     for(k=0; k<pEList->nExpr; k++){
103491       pE = a[k].pExpr;
103492       pRight = pE->pRight;
103493       assert( pE->op!=TK_DOT || pRight!=0 );
103494       if( pE->op!=TK_ALL && (pE->op!=TK_DOT || pRight->op!=TK_ALL) ){
103495         /* This particular expression does not need to be expanded.
103496         */
103497         pNew = sqlite3ExprListAppend(pParse, pNew, a[k].pExpr);
103498         if( pNew ){
103499           pNew->a[pNew->nExpr-1].zName = a[k].zName;
103500           pNew->a[pNew->nExpr-1].zSpan = a[k].zSpan;
103501           a[k].zName = 0;
103502           a[k].zSpan = 0;
103503         }
103504         a[k].pExpr = 0;
103505       }else{
103506         /* This expression is a "*" or a "TABLE.*" and needs to be
103507         ** expanded. */
103508         int tableSeen = 0;      /* Set to 1 when TABLE matches */
103509         char *zTName = 0;       /* text of name of TABLE */
103510         if( pE->op==TK_DOT ){
103511           assert( pE->pLeft!=0 );
103512           assert( !ExprHasProperty(pE->pLeft, EP_IntValue) );
103513           zTName = pE->pLeft->u.zToken;
103514         }
103515         for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
103516           Table *pTab = pFrom->pTab;
103517           Select *pSub = pFrom->pSelect;
103518           char *zTabName = pFrom->zAlias;
103519           const char *zSchemaName = 0;
103520           int iDb;
103521           if( zTabName==0 ){
103522             zTabName = pTab->zName;
103523           }
103524           if( db->mallocFailed ) break;
103525           if( pSub==0 || (pSub->selFlags & SF_NestedFrom)==0 ){
103526             pSub = 0;
103527             if( zTName && sqlite3StrICmp(zTName, zTabName)!=0 ){
103528               continue;
103529             }
103530             iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
103531             zSchemaName = iDb>=0 ? db->aDb[iDb].zName : "*";
103532           }
103533           for(j=0; j<pTab->nCol; j++){
103534             char *zName = pTab->aCol[j].zName;
103535             char *zColname;  /* The computed column name */
103536             char *zToFree;   /* Malloced string that needs to be freed */
103537             Token sColname;  /* Computed column name as a token */
103538 
103539             assert( zName );
103540             if( zTName && pSub
103541              && sqlite3MatchSpanName(pSub->pEList->a[j].zSpan, 0, zTName, 0)==0
103542             ){
103543               continue;
103544             }
103545 
103546             /* If a column is marked as 'hidden' (currently only possible
103547             ** for virtual tables), do not include it in the expanded
103548             ** result-set list.
103549             */
103550             if( IsHiddenColumn(&pTab->aCol[j]) ){
103551               assert(IsVirtual(pTab));
103552               continue;
103553             }
103554             tableSeen = 1;
103555 
103556             if( i>0 && zTName==0 ){
103557               if( (pFrom->jointype & JT_NATURAL)!=0
103558                 && tableAndColumnIndex(pTabList, i, zName, 0, 0)
103559               ){
103560                 /* In a NATURAL join, omit the join columns from the
103561                 ** table to the right of the join */
103562                 continue;
103563               }
103564               if( sqlite3IdListIndex(pFrom->pUsing, zName)>=0 ){
103565                 /* In a join with a USING clause, omit columns in the
103566                 ** using clause from the table on the right. */
103567                 continue;
103568               }
103569             }
103570             pRight = sqlite3Expr(db, TK_ID, zName);
103571             zColname = zName;
103572             zToFree = 0;
103573             if( longNames || pTabList->nSrc>1 ){
103574               Expr *pLeft;
103575               pLeft = sqlite3Expr(db, TK_ID, zTabName);
103576               pExpr = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight, 0);
103577               if( zSchemaName ){
103578                 pLeft = sqlite3Expr(db, TK_ID, zSchemaName);
103579                 pExpr = sqlite3PExpr(pParse, TK_DOT, pLeft, pExpr, 0);
103580               }
103581               if( longNames ){
103582                 zColname = sqlite3MPrintf(db, "%s.%s", zTabName, zName);
103583                 zToFree = zColname;
103584               }
103585             }else{
103586               pExpr = pRight;
103587             }
103588             pNew = sqlite3ExprListAppend(pParse, pNew, pExpr);
103589             sColname.z = zColname;
103590             sColname.n = sqlite3Strlen30(zColname);
103591             sqlite3ExprListSetName(pParse, pNew, &sColname, 0);
103592             if( pNew && (p->selFlags & SF_NestedFrom)!=0 ){
103593               struct ExprList_item *pX = &pNew->a[pNew->nExpr-1];
103594               if( pSub ){
103595                 pX->zSpan = sqlite3DbStrDup(db, pSub->pEList->a[j].zSpan);
103596                 testcase( pX->zSpan==0 );
103597               }else{
103598                 pX->zSpan = sqlite3MPrintf(db, "%s.%s.%s",
103599                                            zSchemaName, zTabName, zColname);
103600                 testcase( pX->zSpan==0 );
103601               }
103602               pX->bSpanIsTab = 1;
103603             }
103604             sqlite3DbFree(db, zToFree);
103605           }
103606         }
103607         if( !tableSeen ){
103608           if( zTName ){
103609             sqlite3ErrorMsg(pParse, "no such table: %s", zTName);
103610           }else{
103611             sqlite3ErrorMsg(pParse, "no tables specified");
103612           }
103613         }
103614       }
103615     }
103616     sqlite3ExprListDelete(db, pEList);
103617     p->pEList = pNew;
103618   }
103619 #if SQLITE_MAX_COLUMN
103620   if( p->pEList && p->pEList->nExpr>db->aLimit[SQLITE_LIMIT_COLUMN] ){
103621     sqlite3ErrorMsg(pParse, "too many columns in result set");
103622   }
103623 #endif
103624   return WRC_Continue;
103625 }
103626 
103627 /*
103628 ** No-op routine for the parse-tree walker.
103629 **
103630 ** When this routine is the Walker.xExprCallback then expression trees
103631 ** are walked without any actions being taken at each node.  Presumably,
103632 ** when this routine is used for Walker.xExprCallback then
103633 ** Walker.xSelectCallback is set to do something useful for every
103634 ** subquery in the parser tree.
103635 */
103636 static int exprWalkNoop(Walker *NotUsed, Expr *NotUsed2){
103637   UNUSED_PARAMETER2(NotUsed, NotUsed2);
103638   return WRC_Continue;
103639 }
103640 
103641 /*
103642 ** This routine "expands" a SELECT statement and all of its subqueries.
103643 ** For additional information on what it means to "expand" a SELECT
103644 ** statement, see the comment on the selectExpand worker callback above.
103645 **
103646 ** Expanding a SELECT statement is the first step in processing a
103647 ** SELECT statement.  The SELECT statement must be expanded before
103648 ** name resolution is performed.
103649 **
103650 ** If anything goes wrong, an error message is written into pParse.
103651 ** The calling function can detect the problem by looking at pParse->nErr
103652 ** and/or pParse->db->mallocFailed.
103653 */
103654 static void sqlite3SelectExpand(Parse *pParse, Select *pSelect){
103655   Walker w;
103656   memset(&w, 0, sizeof(w));
103657   w.xExprCallback = exprWalkNoop;
103658   w.pParse = pParse;
103659   if( pParse->hasCompound ){
103660     w.xSelectCallback = convertCompoundSelectToSubquery;
103661     sqlite3WalkSelect(&w, pSelect);
103662   }
103663   w.xSelectCallback = selectExpander;
103664   w.xSelectCallback2 = selectPopWith;
103665   sqlite3WalkSelect(&w, pSelect);
103666 }
103667 
103668 
103669 #ifndef SQLITE_OMIT_SUBQUERY
103670 /*
103671 ** This is a Walker.xSelectCallback callback for the sqlite3SelectTypeInfo()
103672 ** interface.
103673 **
103674 ** For each FROM-clause subquery, add Column.zType and Column.zColl
103675 ** information to the Table structure that represents the result set
103676 ** of that subquery.
103677 **
103678 ** The Table structure that represents the result set was constructed
103679 ** by selectExpander() but the type and collation information was omitted
103680 ** at that point because identifiers had not yet been resolved.  This
103681 ** routine is called after identifier resolution.
103682 */
103683 static void selectAddSubqueryTypeInfo(Walker *pWalker, Select *p){
103684   Parse *pParse;
103685   int i;
103686   SrcList *pTabList;
103687   struct SrcList_item *pFrom;
103688 
103689   assert( p->selFlags & SF_Resolved );
103690   if( (p->selFlags & SF_HasTypeInfo)==0 ){
103691     p->selFlags |= SF_HasTypeInfo;
103692     pParse = pWalker->pParse;
103693     pTabList = p->pSrc;
103694     for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
103695       Table *pTab = pFrom->pTab;
103696       if( ALWAYS(pTab!=0) && (pTab->tabFlags & TF_Ephemeral)!=0 ){
103697         /* A sub-query in the FROM clause of a SELECT */
103698         Select *pSel = pFrom->pSelect;
103699         if( pSel ){
103700           while( pSel->pPrior ) pSel = pSel->pPrior;
103701           selectAddColumnTypeAndCollation(pParse, pTab, pSel);
103702         }
103703       }
103704     }
103705   }
103706 }
103707 #endif
103708 
103709 
103710 /*
103711 ** This routine adds datatype and collating sequence information to
103712 ** the Table structures of all FROM-clause subqueries in a
103713 ** SELECT statement.
103714 **
103715 ** Use this routine after name resolution.
103716 */
103717 static void sqlite3SelectAddTypeInfo(Parse *pParse, Select *pSelect){
103718 #ifndef SQLITE_OMIT_SUBQUERY
103719   Walker w;
103720   memset(&w, 0, sizeof(w));
103721   w.xSelectCallback2 = selectAddSubqueryTypeInfo;
103722   w.xExprCallback = exprWalkNoop;
103723   w.pParse = pParse;
103724   sqlite3WalkSelect(&w, pSelect);
103725 #endif
103726 }
103727 
103728 
103729 /*
103730 ** This routine sets up a SELECT statement for processing.  The
103731 ** following is accomplished:
103732 **
103733 **     *  VDBE Cursor numbers are assigned to all FROM-clause terms.
103734 **     *  Ephemeral Table objects are created for all FROM-clause subqueries.
103735 **     *  ON and USING clauses are shifted into WHERE statements
103736 **     *  Wildcards "*" and "TABLE.*" in result sets are expanded.
103737 **     *  Identifiers in expression are matched to tables.
103738 **
103739 ** This routine acts recursively on all subqueries within the SELECT.
103740 */
103741 SQLITE_PRIVATE void sqlite3SelectPrep(
103742   Parse *pParse,         /* The parser context */
103743   Select *p,             /* The SELECT statement being coded. */
103744   NameContext *pOuterNC  /* Name context for container */
103745 ){
103746   sqlite3 *db;
103747   if( NEVER(p==0) ) return;
103748   db = pParse->db;
103749   if( db->mallocFailed ) return;
103750   if( p->selFlags & SF_HasTypeInfo ) return;
103751   sqlite3SelectExpand(pParse, p);
103752   if( pParse->nErr || db->mallocFailed ) return;
103753   sqlite3ResolveSelectNames(pParse, p, pOuterNC);
103754   if( pParse->nErr || db->mallocFailed ) return;
103755   sqlite3SelectAddTypeInfo(pParse, p);
103756 }
103757 
103758 /*
103759 ** Reset the aggregate accumulator.
103760 **
103761 ** The aggregate accumulator is a set of memory cells that hold
103762 ** intermediate results while calculating an aggregate.  This
103763 ** routine generates code that stores NULLs in all of those memory
103764 ** cells.
103765 */
103766 static void resetAccumulator(Parse *pParse, AggInfo *pAggInfo){
103767   Vdbe *v = pParse->pVdbe;
103768   int i;
103769   struct AggInfo_func *pFunc;
103770   int nReg = pAggInfo->nFunc + pAggInfo->nColumn;
103771   if( nReg==0 ) return;
103772 #ifdef SQLITE_DEBUG
103773   /* Verify that all AggInfo registers are within the range specified by
103774   ** AggInfo.mnReg..AggInfo.mxReg */
103775   assert( nReg==pAggInfo->mxReg-pAggInfo->mnReg+1 );
103776   for(i=0; i<pAggInfo->nColumn; i++){
103777     assert( pAggInfo->aCol[i].iMem>=pAggInfo->mnReg
103778          && pAggInfo->aCol[i].iMem<=pAggInfo->mxReg );
103779   }
103780   for(i=0; i<pAggInfo->nFunc; i++){
103781     assert( pAggInfo->aFunc[i].iMem>=pAggInfo->mnReg
103782          && pAggInfo->aFunc[i].iMem<=pAggInfo->mxReg );
103783   }
103784 #endif
103785   sqlite3VdbeAddOp3(v, OP_Null, 0, pAggInfo->mnReg, pAggInfo->mxReg);
103786   for(pFunc=pAggInfo->aFunc, i=0; i<pAggInfo->nFunc; i++, pFunc++){
103787     if( pFunc->iDistinct>=0 ){
103788       Expr *pE = pFunc->pExpr;
103789       assert( !ExprHasProperty(pE, EP_xIsSelect) );
103790       if( pE->x.pList==0 || pE->x.pList->nExpr!=1 ){
103791         sqlite3ErrorMsg(pParse, "DISTINCT aggregates must have exactly one "
103792            "argument");
103793         pFunc->iDistinct = -1;
103794       }else{
103795         KeyInfo *pKeyInfo = keyInfoFromExprList(pParse, pE->x.pList, 0);
103796         sqlite3VdbeAddOp4(v, OP_OpenEphemeral, pFunc->iDistinct, 0, 0,
103797                           (char*)pKeyInfo, P4_KEYINFO);
103798       }
103799     }
103800   }
103801 }
103802 
103803 /*
103804 ** Invoke the OP_AggFinalize opcode for every aggregate function
103805 ** in the AggInfo structure.
103806 */
103807 static void finalizeAggFunctions(Parse *pParse, AggInfo *pAggInfo){
103808   Vdbe *v = pParse->pVdbe;
103809   int i;
103810   struct AggInfo_func *pF;
103811   for(i=0, pF=pAggInfo->aFunc; i<pAggInfo->nFunc; i++, pF++){
103812     ExprList *pList = pF->pExpr->x.pList;
103813     assert( !ExprHasProperty(pF->pExpr, EP_xIsSelect) );
103814     sqlite3VdbeAddOp4(v, OP_AggFinal, pF->iMem, pList ? pList->nExpr : 0, 0,
103815                       (void*)pF->pFunc, P4_FUNCDEF);
103816   }
103817 }
103818 
103819 /*
103820 ** Update the accumulator memory cells for an aggregate based on
103821 ** the current cursor position.
103822 */
103823 static void updateAccumulator(Parse *pParse, AggInfo *pAggInfo){
103824   Vdbe *v = pParse->pVdbe;
103825   int i;
103826   int regHit = 0;
103827   int addrHitTest = 0;
103828   struct AggInfo_func *pF;
103829   struct AggInfo_col *pC;
103830 
103831   pAggInfo->directMode = 1;
103832   for(i=0, pF=pAggInfo->aFunc; i<pAggInfo->nFunc; i++, pF++){
103833     int nArg;
103834     int addrNext = 0;
103835     int regAgg;
103836     ExprList *pList = pF->pExpr->x.pList;
103837     assert( !ExprHasProperty(pF->pExpr, EP_xIsSelect) );
103838     if( pList ){
103839       nArg = pList->nExpr;
103840       regAgg = sqlite3GetTempRange(pParse, nArg);
103841       sqlite3ExprCodeExprList(pParse, pList, regAgg, SQLITE_ECEL_DUP);
103842     }else{
103843       nArg = 0;
103844       regAgg = 0;
103845     }
103846     if( pF->iDistinct>=0 ){
103847       addrNext = sqlite3VdbeMakeLabel(v);
103848       assert( nArg==1 );
103849       codeDistinct(pParse, pF->iDistinct, addrNext, 1, regAgg);
103850     }
103851     if( pF->pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL ){
103852       CollSeq *pColl = 0;
103853       struct ExprList_item *pItem;
103854       int j;
103855       assert( pList!=0 );  /* pList!=0 if pF->pFunc has NEEDCOLL */
103856       for(j=0, pItem=pList->a; !pColl && j<nArg; j++, pItem++){
103857         pColl = sqlite3ExprCollSeq(pParse, pItem->pExpr);
103858       }
103859       if( !pColl ){
103860         pColl = pParse->db->pDfltColl;
103861       }
103862       if( regHit==0 && pAggInfo->nAccumulator ) regHit = ++pParse->nMem;
103863       sqlite3VdbeAddOp4(v, OP_CollSeq, regHit, 0, 0, (char *)pColl, P4_COLLSEQ);
103864     }
103865     sqlite3VdbeAddOp4(v, OP_AggStep, 0, regAgg, pF->iMem,
103866                       (void*)pF->pFunc, P4_FUNCDEF);
103867     sqlite3VdbeChangeP5(v, (u8)nArg);
103868     sqlite3ExprCacheAffinityChange(pParse, regAgg, nArg);
103869     sqlite3ReleaseTempRange(pParse, regAgg, nArg);
103870     if( addrNext ){
103871       sqlite3VdbeResolveLabel(v, addrNext);
103872       sqlite3ExprCacheClear(pParse);
103873     }
103874   }
103875 
103876   /* Before populating the accumulator registers, clear the column cache.
103877   ** Otherwise, if any of the required column values are already present
103878   ** in registers, sqlite3ExprCode() may use OP_SCopy to copy the value
103879   ** to pC->iMem. But by the time the value is used, the original register
103880   ** may have been used, invalidating the underlying buffer holding the
103881   ** text or blob value. See ticket [883034dcb5].
103882   **
103883   ** Another solution would be to change the OP_SCopy used to copy cached
103884   ** values to an OP_Copy.
103885   */
103886   if( regHit ){
103887     addrHitTest = sqlite3VdbeAddOp1(v, OP_If, regHit);
103888   }
103889   sqlite3ExprCacheClear(pParse);
103890   for(i=0, pC=pAggInfo->aCol; i<pAggInfo->nAccumulator; i++, pC++){
103891     sqlite3ExprCode(pParse, pC->pExpr, pC->iMem);
103892   }
103893   pAggInfo->directMode = 0;
103894   sqlite3ExprCacheClear(pParse);
103895   if( addrHitTest ){
103896     sqlite3VdbeJumpHere(v, addrHitTest);
103897   }
103898 }
103899 
103900 /*
103901 ** Add a single OP_Explain instruction to the VDBE to explain a simple
103902 ** count(*) query ("SELECT count(*) FROM pTab").
103903 */
103904 #ifndef SQLITE_OMIT_EXPLAIN
103905 static void explainSimpleCount(
103906   Parse *pParse,                  /* Parse context */
103907   Table *pTab,                    /* Table being queried */
103908   Index *pIdx                     /* Index used to optimize scan, or NULL */
103909 ){
103910   if( pParse->explain==2 ){
103911     char *zEqp = sqlite3MPrintf(pParse->db, "SCAN TABLE %s%s%s",
103912         pTab->zName,
103913         pIdx ? " USING COVERING INDEX " : "",
103914         pIdx ? pIdx->zName : ""
103915     );
103916     sqlite3VdbeAddOp4(
103917         pParse->pVdbe, OP_Explain, pParse->iSelectId, 0, 0, zEqp, P4_DYNAMIC
103918     );
103919   }
103920 }
103921 #else
103922 # define explainSimpleCount(a,b,c)
103923 #endif
103924 
103925 /*
103926 ** Generate code for the SELECT statement given in the p argument.
103927 **
103928 ** The results are returned according to the SelectDest structure.
103929 ** See comments in sqliteInt.h for further information.
103930 **
103931 ** This routine returns the number of errors.  If any errors are
103932 ** encountered, then an appropriate error message is left in
103933 ** pParse->zErrMsg.
103934 **
103935 ** This routine does NOT free the Select structure passed in.  The
103936 ** calling function needs to do that.
103937 */
103938 SQLITE_PRIVATE int sqlite3Select(
103939   Parse *pParse,         /* The parser context */
103940   Select *p,             /* The SELECT statement being coded. */
103941   SelectDest *pDest      /* What to do with the query results */
103942 ){
103943   int i, j;              /* Loop counters */
103944   WhereInfo *pWInfo;     /* Return from sqlite3WhereBegin() */
103945   Vdbe *v;               /* The virtual machine under construction */
103946   int isAgg;             /* True for select lists like "count(*)" */
103947   ExprList *pEList;      /* List of columns to extract. */
103948   SrcList *pTabList;     /* List of tables to select from */
103949   Expr *pWhere;          /* The WHERE clause.  May be NULL */
103950   ExprList *pOrderBy;    /* The ORDER BY clause.  May be NULL */
103951   ExprList *pGroupBy;    /* The GROUP BY clause.  May be NULL */
103952   Expr *pHaving;         /* The HAVING clause.  May be NULL */
103953   int rc = 1;            /* Value to return from this function */
103954   int addrSortIndex;     /* Address of an OP_OpenEphemeral instruction */
103955   DistinctCtx sDistinct; /* Info on how to code the DISTINCT keyword */
103956   AggInfo sAggInfo;      /* Information used by aggregate queries */
103957   int iEnd;              /* Address of the end of the query */
103958   sqlite3 *db;           /* The database connection */
103959 
103960 #ifndef SQLITE_OMIT_EXPLAIN
103961   int iRestoreSelectId = pParse->iSelectId;
103962   pParse->iSelectId = pParse->iNextSelectId++;
103963 #endif
103964 
103965   db = pParse->db;
103966   if( p==0 || db->mallocFailed || pParse->nErr ){
103967     return 1;
103968   }
103969   if( sqlite3AuthCheck(pParse, SQLITE_SELECT, 0, 0, 0) ) return 1;
103970   memset(&sAggInfo, 0, sizeof(sAggInfo));
103971 
103972   if( IgnorableOrderby(pDest) ){
103973     assert(pDest->eDest==SRT_Exists || pDest->eDest==SRT_Union ||
103974            pDest->eDest==SRT_Except || pDest->eDest==SRT_Discard);
103975     /* If ORDER BY makes no difference in the output then neither does
103976     ** DISTINCT so it can be removed too. */
103977     sqlite3ExprListDelete(db, p->pOrderBy);
103978     p->pOrderBy = 0;
103979     p->selFlags &= ~SF_Distinct;
103980   }
103981   sqlite3SelectPrep(pParse, p, 0);
103982   pOrderBy = p->pOrderBy;
103983   pTabList = p->pSrc;
103984   pEList = p->pEList;
103985   if( pParse->nErr || db->mallocFailed ){
103986     goto select_end;
103987   }
103988   isAgg = (p->selFlags & SF_Aggregate)!=0;
103989   assert( pEList!=0 );
103990 
103991   /* Begin generating code.
103992   */
103993   v = sqlite3GetVdbe(pParse);
103994   if( v==0 ) goto select_end;
103995 
103996   /* If writing to memory or generating a set
103997   ** only a single column may be output.
103998   */
103999 #ifndef SQLITE_OMIT_SUBQUERY
104000   if( checkForMultiColumnSelectError(pParse, pDest, pEList->nExpr) ){
104001     goto select_end;
104002   }
104003 #endif
104004 
104005   /* Generate code for all sub-queries in the FROM clause
104006   */
104007 #if !defined(SQLITE_OMIT_SUBQUERY) || !defined(SQLITE_OMIT_VIEW)
104008   for(i=0; !p->pPrior && i<pTabList->nSrc; i++){
104009     struct SrcList_item *pItem = &pTabList->a[i];
104010     SelectDest dest;
104011     Select *pSub = pItem->pSelect;
104012     int isAggSub;
104013 
104014     if( pSub==0 ) continue;
104015 
104016     /* Sometimes the code for a subquery will be generated more than
104017     ** once, if the subquery is part of the WHERE clause in a LEFT JOIN,
104018     ** for example.  In that case, do not regenerate the code to manifest
104019     ** a view or the co-routine to implement a view.  The first instance
104020     ** is sufficient, though the subroutine to manifest the view does need
104021     ** to be invoked again. */
104022     if( pItem->addrFillSub ){
104023       if( pItem->viaCoroutine==0 ){
104024         sqlite3VdbeAddOp2(v, OP_Gosub, pItem->regReturn, pItem->addrFillSub);
104025       }
104026       continue;
104027     }
104028 
104029     /* Increment Parse.nHeight by the height of the largest expression
104030     ** tree referred to by this, the parent select. The child select
104031     ** may contain expression trees of at most
104032     ** (SQLITE_MAX_EXPR_DEPTH-Parse.nHeight) height. This is a bit
104033     ** more conservative than necessary, but much easier than enforcing
104034     ** an exact limit.
104035     */
104036     pParse->nHeight += sqlite3SelectExprHeight(p);
104037 
104038     isAggSub = (pSub->selFlags & SF_Aggregate)!=0;
104039     if( flattenSubquery(pParse, p, i, isAgg, isAggSub) ){
104040       /* This subquery can be absorbed into its parent. */
104041       if( isAggSub ){
104042         isAgg = 1;
104043         p->selFlags |= SF_Aggregate;
104044       }
104045       i = -1;
104046     }else if( pTabList->nSrc==1 && (p->selFlags & SF_Materialize)==0
104047       && OptimizationEnabled(db, SQLITE_SubqCoroutine)
104048     ){
104049       /* Implement a co-routine that will return a single row of the result
104050       ** set on each invocation.
104051       */
104052       int addrTop;
104053       int addrEof;
104054       pItem->regReturn = ++pParse->nMem;
104055       addrEof = ++pParse->nMem;
104056       /* Before coding the OP_Goto to jump to the start of the main routine,
104057       ** ensure that the jump to the verify-schema routine has already
104058       ** been coded. Otherwise, the verify-schema would likely be coded as
104059       ** part of the co-routine. If the main routine then accessed the
104060       ** database before invoking the co-routine for the first time (for
104061       ** example to initialize a LIMIT register from a sub-select), it would
104062       ** be doing so without having verified the schema version and obtained
104063       ** the required db locks. See ticket d6b36be38.  */
104064       sqlite3CodeVerifySchema(pParse, -1);
104065       sqlite3VdbeAddOp0(v, OP_Goto);
104066       addrTop = sqlite3VdbeAddOp1(v, OP_OpenPseudo, pItem->iCursor);
104067       sqlite3VdbeChangeP5(v, 1);
104068       VdbeComment((v, "coroutine for %s", pItem->pTab->zName));
104069       pItem->addrFillSub = addrTop;
104070       sqlite3VdbeAddOp2(v, OP_Integer, 0, addrEof);
104071       sqlite3VdbeChangeP5(v, 1);
104072       sqlite3SelectDestInit(&dest, SRT_Coroutine, pItem->regReturn);
104073       explainSetInteger(pItem->iSelectId, (u8)pParse->iNextSelectId);
104074       sqlite3Select(pParse, pSub, &dest);
104075       pItem->pTab->nRowEst = (unsigned)pSub->nSelectRow;
104076       pItem->viaCoroutine = 1;
104077       sqlite3VdbeChangeP2(v, addrTop, dest.iSdst);
104078       sqlite3VdbeChangeP3(v, addrTop, dest.nSdst);
104079       sqlite3VdbeAddOp2(v, OP_Integer, 1, addrEof);
104080       sqlite3VdbeAddOp1(v, OP_Yield, pItem->regReturn);
104081       VdbeComment((v, "end %s", pItem->pTab->zName));
104082       sqlite3VdbeJumpHere(v, addrTop-1);
104083       sqlite3ClearTempRegCache(pParse);
104084     }else{
104085       /* Generate a subroutine that will fill an ephemeral table with
104086       ** the content of this subquery.  pItem->addrFillSub will point
104087       ** to the address of the generated subroutine.  pItem->regReturn
104088       ** is a register allocated to hold the subroutine return address
104089       */
104090       int topAddr;
104091       int onceAddr = 0;
104092       int retAddr;
104093       assert( pItem->addrFillSub==0 );
104094       pItem->regReturn = ++pParse->nMem;
104095       topAddr = sqlite3VdbeAddOp2(v, OP_Integer, 0, pItem->regReturn);
104096       pItem->addrFillSub = topAddr+1;
104097       VdbeNoopComment((v, "materialize %s", pItem->pTab->zName));
104098       if( pItem->isCorrelated==0 ){
104099         /* If the subquery is not correlated and if we are not inside of
104100         ** a trigger, then we only need to compute the value of the subquery
104101         ** once. */
104102         onceAddr = sqlite3CodeOnce(pParse);
104103       }
104104       sqlite3SelectDestInit(&dest, SRT_EphemTab, pItem->iCursor);
104105       explainSetInteger(pItem->iSelectId, (u8)pParse->iNextSelectId);
104106       sqlite3Select(pParse, pSub, &dest);
104107       pItem->pTab->nRowEst = (unsigned)pSub->nSelectRow;
104108       if( onceAddr ) sqlite3VdbeJumpHere(v, onceAddr);
104109       retAddr = sqlite3VdbeAddOp1(v, OP_Return, pItem->regReturn);
104110       VdbeComment((v, "end %s", pItem->pTab->zName));
104111       sqlite3VdbeChangeP1(v, topAddr, retAddr);
104112       sqlite3ClearTempRegCache(pParse);
104113     }
104114     if( /*pParse->nErr ||*/ db->mallocFailed ){
104115       goto select_end;
104116     }
104117     pParse->nHeight -= sqlite3SelectExprHeight(p);
104118     pTabList = p->pSrc;
104119     if( !IgnorableOrderby(pDest) ){
104120       pOrderBy = p->pOrderBy;
104121     }
104122   }
104123   pEList = p->pEList;
104124 #endif
104125   pWhere = p->pWhere;
104126   pGroupBy = p->pGroupBy;
104127   pHaving = p->pHaving;
104128   sDistinct.isTnct = (p->selFlags & SF_Distinct)!=0;
104129 
104130 #ifndef SQLITE_OMIT_COMPOUND_SELECT
104131   /* If there is are a sequence of queries, do the earlier ones first.
104132   */
104133   if( p->pPrior ){
104134     if( p->pRightmost==0 ){
104135       Select *pLoop, *pRight = 0;
104136       int cnt = 0;
104137       int mxSelect;
104138       for(pLoop=p; pLoop; pLoop=pLoop->pPrior, cnt++){
104139         pLoop->pRightmost = p;
104140         pLoop->pNext = pRight;
104141         pRight = pLoop;
104142       }
104143       mxSelect = db->aLimit[SQLITE_LIMIT_COMPOUND_SELECT];
104144       if( mxSelect && cnt>mxSelect ){
104145         sqlite3ErrorMsg(pParse, "too many terms in compound SELECT");
104146         goto select_end;
104147       }
104148     }
104149     rc = multiSelect(pParse, p, pDest);
104150     explainSetInteger(pParse->iSelectId, iRestoreSelectId);
104151     return rc;
104152   }
104153 #endif
104154 
104155   /* If there is both a GROUP BY and an ORDER BY clause and they are
104156   ** identical, then disable the ORDER BY clause since the GROUP BY
104157   ** will cause elements to come out in the correct order.  This is
104158   ** an optimization - the correct answer should result regardless.
104159   ** Use the SQLITE_GroupByOrder flag with SQLITE_TESTCTRL_OPTIMIZER
104160   ** to disable this optimization for testing purposes.
104161   */
104162   if( sqlite3ExprListCompare(p->pGroupBy, pOrderBy, -1)==0
104163          && OptimizationEnabled(db, SQLITE_GroupByOrder) ){
104164     pOrderBy = 0;
104165   }
104166 
104167   /* If the query is DISTINCT with an ORDER BY but is not an aggregate, and
104168   ** if the select-list is the same as the ORDER BY list, then this query
104169   ** can be rewritten as a GROUP BY. In other words, this:
104170   **
104171   **     SELECT DISTINCT xyz FROM ... ORDER BY xyz
104172   **
104173   ** is transformed to:
104174   **
104175   **     SELECT xyz FROM ... GROUP BY xyz
104176   **
104177   ** The second form is preferred as a single index (or temp-table) may be
104178   ** used for both the ORDER BY and DISTINCT processing. As originally
104179   ** written the query must use a temp-table for at least one of the ORDER
104180   ** BY and DISTINCT, and an index or separate temp-table for the other.
104181   */
104182   if( (p->selFlags & (SF_Distinct|SF_Aggregate))==SF_Distinct
104183    && sqlite3ExprListCompare(pOrderBy, p->pEList, -1)==0
104184   ){
104185     p->selFlags &= ~SF_Distinct;
104186     p->pGroupBy = sqlite3ExprListDup(db, p->pEList, 0);
104187     pGroupBy = p->pGroupBy;
104188     pOrderBy = 0;
104189     /* Notice that even thought SF_Distinct has been cleared from p->selFlags,
104190     ** the sDistinct.isTnct is still set.  Hence, isTnct represents the
104191     ** original setting of the SF_Distinct flag, not the current setting */
104192     assert( sDistinct.isTnct );
104193   }
104194 
104195   /* If there is an ORDER BY clause, then this sorting
104196   ** index might end up being unused if the data can be
104197   ** extracted in pre-sorted order.  If that is the case, then the
104198   ** OP_OpenEphemeral instruction will be changed to an OP_Noop once
104199   ** we figure out that the sorting index is not needed.  The addrSortIndex
104200   ** variable is used to facilitate that change.
104201   */
104202   if( pOrderBy ){
104203     KeyInfo *pKeyInfo;
104204     pKeyInfo = keyInfoFromExprList(pParse, pOrderBy, 0);
104205     pOrderBy->iECursor = pParse->nTab++;
104206     p->addrOpenEphm[2] = addrSortIndex =
104207       sqlite3VdbeAddOp4(v, OP_OpenEphemeral,
104208                            pOrderBy->iECursor, pOrderBy->nExpr+2, 0,
104209                            (char*)pKeyInfo, P4_KEYINFO);
104210   }else{
104211     addrSortIndex = -1;
104212   }
104213 
104214   /* If the output is destined for a temporary table, open that table.
104215   */
104216   if( pDest->eDest==SRT_EphemTab ){
104217     sqlite3VdbeAddOp2(v, OP_OpenEphemeral, pDest->iSDParm, pEList->nExpr);
104218   }
104219 
104220   /* Set the limiter.
104221   */
104222   iEnd = sqlite3VdbeMakeLabel(v);
104223   p->nSelectRow = LARGEST_INT64;
104224   computeLimitRegisters(pParse, p, iEnd);
104225   if( p->iLimit==0 && addrSortIndex>=0 ){
104226     sqlite3VdbeGetOp(v, addrSortIndex)->opcode = OP_SorterOpen;
104227     p->selFlags |= SF_UseSorter;
104228   }
104229 
104230   /* Open a virtual index to use for the distinct set.
104231   */
104232   if( p->selFlags & SF_Distinct ){
104233     sDistinct.tabTnct = pParse->nTab++;
104234     sDistinct.addrTnct = sqlite3VdbeAddOp4(v, OP_OpenEphemeral,
104235                                 sDistinct.tabTnct, 0, 0,
104236                                 (char*)keyInfoFromExprList(pParse, p->pEList, 0),
104237                                 P4_KEYINFO);
104238     sqlite3VdbeChangeP5(v, BTREE_UNORDERED);
104239     sDistinct.eTnctType = WHERE_DISTINCT_UNORDERED;
104240   }else{
104241     sDistinct.eTnctType = WHERE_DISTINCT_NOOP;
104242   }
104243 
104244   if( !isAgg && pGroupBy==0 ){
104245     /* No aggregate functions and no GROUP BY clause */
104246     u16 wctrlFlags = (sDistinct.isTnct ? WHERE_WANT_DISTINCT : 0);
104247 
104248     /* Begin the database scan. */
104249     pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pOrderBy, p->pEList,
104250                                wctrlFlags, 0);
104251     if( pWInfo==0 ) goto select_end;
104252     if( sqlite3WhereOutputRowCount(pWInfo) < p->nSelectRow ){
104253       p->nSelectRow = sqlite3WhereOutputRowCount(pWInfo);
104254     }
104255     if( sDistinct.isTnct && sqlite3WhereIsDistinct(pWInfo) ){
104256       sDistinct.eTnctType = sqlite3WhereIsDistinct(pWInfo);
104257     }
104258     if( pOrderBy && sqlite3WhereIsOrdered(pWInfo) ) pOrderBy = 0;
104259 
104260     /* If sorting index that was created by a prior OP_OpenEphemeral
104261     ** instruction ended up not being needed, then change the OP_OpenEphemeral
104262     ** into an OP_Noop.
104263     */
104264     if( addrSortIndex>=0 && pOrderBy==0 ){
104265       sqlite3VdbeChangeToNoop(v, addrSortIndex);
104266       p->addrOpenEphm[2] = -1;
104267     }
104268 
104269     /* Use the standard inner loop. */
104270     selectInnerLoop(pParse, p, pEList, -1, pOrderBy, &sDistinct, pDest,
104271                     sqlite3WhereContinueLabel(pWInfo),
104272                     sqlite3WhereBreakLabel(pWInfo));
104273 
104274     /* End the database scan loop.
104275     */
104276     sqlite3WhereEnd(pWInfo);
104277   }else{
104278     /* This case when there exist aggregate functions or a GROUP BY clause
104279     ** or both */
104280     NameContext sNC;    /* Name context for processing aggregate information */
104281     int iAMem;          /* First Mem address for storing current GROUP BY */
104282     int iBMem;          /* First Mem address for previous GROUP BY */
104283     int iUseFlag;       /* Mem address holding flag indicating that at least
104284                         ** one row of the input to the aggregator has been
104285                         ** processed */
104286     int iAbortFlag;     /* Mem address which causes query abort if positive */
104287     int groupBySort;    /* Rows come from source in GROUP BY order */
104288     int addrEnd;        /* End of processing for this SELECT */
104289     int sortPTab = 0;   /* Pseudotable used to decode sorting results */
104290     int sortOut = 0;    /* Output register from the sorter */
104291 
104292     /* Remove any and all aliases between the result set and the
104293     ** GROUP BY clause.
104294     */
104295     if( pGroupBy ){
104296       int k;                        /* Loop counter */
104297       struct ExprList_item *pItem;  /* For looping over expression in a list */
104298 
104299       for(k=p->pEList->nExpr, pItem=p->pEList->a; k>0; k--, pItem++){
104300         pItem->u.x.iAlias = 0;
104301       }
104302       for(k=pGroupBy->nExpr, pItem=pGroupBy->a; k>0; k--, pItem++){
104303         pItem->u.x.iAlias = 0;
104304       }
104305       if( p->nSelectRow>100 ) p->nSelectRow = 100;
104306     }else{
104307       p->nSelectRow = 1;
104308     }
104309 
104310 
104311     /* Create a label to jump to when we want to abort the query */
104312     addrEnd = sqlite3VdbeMakeLabel(v);
104313 
104314     /* Convert TK_COLUMN nodes into TK_AGG_COLUMN and make entries in
104315     ** sAggInfo for all TK_AGG_FUNCTION nodes in expressions of the
104316     ** SELECT statement.
104317     */
104318     memset(&sNC, 0, sizeof(sNC));
104319     sNC.pParse = pParse;
104320     sNC.pSrcList = pTabList;
104321     sNC.pAggInfo = &sAggInfo;
104322     sAggInfo.mnReg = pParse->nMem+1;
104323     sAggInfo.nSortingColumn = pGroupBy ? pGroupBy->nExpr+1 : 0;
104324     sAggInfo.pGroupBy = pGroupBy;
104325     sqlite3ExprAnalyzeAggList(&sNC, pEList);
104326     sqlite3ExprAnalyzeAggList(&sNC, pOrderBy);
104327     if( pHaving ){
104328       sqlite3ExprAnalyzeAggregates(&sNC, pHaving);
104329     }
104330     sAggInfo.nAccumulator = sAggInfo.nColumn;
104331     for(i=0; i<sAggInfo.nFunc; i++){
104332       assert( !ExprHasProperty(sAggInfo.aFunc[i].pExpr, EP_xIsSelect) );
104333       sNC.ncFlags |= NC_InAggFunc;
104334       sqlite3ExprAnalyzeAggList(&sNC, sAggInfo.aFunc[i].pExpr->x.pList);
104335       sNC.ncFlags &= ~NC_InAggFunc;
104336     }
104337     sAggInfo.mxReg = pParse->nMem;
104338     if( db->mallocFailed ) goto select_end;
104339 
104340     /* Processing for aggregates with GROUP BY is very different and
104341     ** much more complex than aggregates without a GROUP BY.
104342     */
104343     if( pGroupBy ){
104344       KeyInfo *pKeyInfo;  /* Keying information for the group by clause */
104345       int j1;             /* A-vs-B comparision jump */
104346       int addrOutputRow;  /* Start of subroutine that outputs a result row */
104347       int regOutputRow;   /* Return address register for output subroutine */
104348       int addrSetAbort;   /* Set the abort flag and return */
104349       int addrTopOfLoop;  /* Top of the input loop */
104350       int addrSortingIdx; /* The OP_OpenEphemeral for the sorting index */
104351       int addrReset;      /* Subroutine for resetting the accumulator */
104352       int regReset;       /* Return address register for reset subroutine */
104353 
104354       /* If there is a GROUP BY clause we might need a sorting index to
104355       ** implement it.  Allocate that sorting index now.  If it turns out
104356       ** that we do not need it after all, the OP_SorterOpen instruction
104357       ** will be converted into a Noop.
104358       */
104359       sAggInfo.sortingIdx = pParse->nTab++;
104360       pKeyInfo = keyInfoFromExprList(pParse, pGroupBy, 0);
104361       addrSortingIdx = sqlite3VdbeAddOp4(v, OP_SorterOpen,
104362           sAggInfo.sortingIdx, sAggInfo.nSortingColumn,
104363           0, (char*)pKeyInfo, P4_KEYINFO);
104364 
104365       /* Initialize memory locations used by GROUP BY aggregate processing
104366       */
104367       iUseFlag = ++pParse->nMem;
104368       iAbortFlag = ++pParse->nMem;
104369       regOutputRow = ++pParse->nMem;
104370       addrOutputRow = sqlite3VdbeMakeLabel(v);
104371       regReset = ++pParse->nMem;
104372       addrReset = sqlite3VdbeMakeLabel(v);
104373       iAMem = pParse->nMem + 1;
104374       pParse->nMem += pGroupBy->nExpr;
104375       iBMem = pParse->nMem + 1;
104376       pParse->nMem += pGroupBy->nExpr;
104377       sqlite3VdbeAddOp2(v, OP_Integer, 0, iAbortFlag);
104378       VdbeComment((v, "clear abort flag"));
104379       sqlite3VdbeAddOp2(v, OP_Integer, 0, iUseFlag);
104380       VdbeComment((v, "indicate accumulator empty"));
104381       sqlite3VdbeAddOp3(v, OP_Null, 0, iAMem, iAMem+pGroupBy->nExpr-1);
104382 
104383       /* Begin a loop that will extract all source rows in GROUP BY order.
104384       ** This might involve two separate loops with an OP_Sort in between, or
104385       ** it might be a single loop that uses an index to extract information
104386       ** in the right order to begin with.
104387       */
104388       sqlite3VdbeAddOp2(v, OP_Gosub, regReset, addrReset);
104389       pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pGroupBy, 0,
104390                                  WHERE_GROUPBY, 0);
104391       if( pWInfo==0 ) goto select_end;
104392       if( sqlite3WhereIsOrdered(pWInfo) ){
104393         /* The optimizer is able to deliver rows in group by order so
104394         ** we do not have to sort.  The OP_OpenEphemeral table will be
104395         ** cancelled later because we still need to use the pKeyInfo
104396         */
104397         groupBySort = 0;
104398       }else{
104399         /* Rows are coming out in undetermined order.  We have to push
104400         ** each row into a sorting index, terminate the first loop,
104401         ** then loop over the sorting index in order to get the output
104402         ** in sorted order
104403         */
104404         int regBase;
104405         int regRecord;
104406         int nCol;
104407         int nGroupBy;
104408 
104409         explainTempTable(pParse,
104410             (sDistinct.isTnct && (p->selFlags&SF_Distinct)==0) ?
104411                     "DISTINCT" : "GROUP BY");
104412 
104413         groupBySort = 1;
104414         nGroupBy = pGroupBy->nExpr;
104415         nCol = nGroupBy + 1;
104416         j = nGroupBy+1;
104417         for(i=0; i<sAggInfo.nColumn; i++){
104418           if( sAggInfo.aCol[i].iSorterColumn>=j ){
104419             nCol++;
104420             j++;
104421           }
104422         }
104423         regBase = sqlite3GetTempRange(pParse, nCol);
104424         sqlite3ExprCacheClear(pParse);
104425         sqlite3ExprCodeExprList(pParse, pGroupBy, regBase, 0);
104426         sqlite3VdbeAddOp2(v, OP_Sequence, sAggInfo.sortingIdx,regBase+nGroupBy);
104427         j = nGroupBy+1;
104428         for(i=0; i<sAggInfo.nColumn; i++){
104429           struct AggInfo_col *pCol = &sAggInfo.aCol[i];
104430           if( pCol->iSorterColumn>=j ){
104431             int r1 = j + regBase;
104432             int r2;
104433 
104434             r2 = sqlite3ExprCodeGetColumn(pParse,
104435                                pCol->pTab, pCol->iColumn, pCol->iTable, r1, 0);
104436             if( r1!=r2 ){
104437               sqlite3VdbeAddOp2(v, OP_SCopy, r2, r1);
104438             }
104439             j++;
104440           }
104441         }
104442         regRecord = sqlite3GetTempReg(pParse);
104443         sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase, nCol, regRecord);
104444         sqlite3VdbeAddOp2(v, OP_SorterInsert, sAggInfo.sortingIdx, regRecord);
104445         sqlite3ReleaseTempReg(pParse, regRecord);
104446         sqlite3ReleaseTempRange(pParse, regBase, nCol);
104447         sqlite3WhereEnd(pWInfo);
104448         sAggInfo.sortingIdxPTab = sortPTab = pParse->nTab++;
104449         sortOut = sqlite3GetTempReg(pParse);
104450         sqlite3VdbeAddOp3(v, OP_OpenPseudo, sortPTab, sortOut, nCol);
104451         sqlite3VdbeAddOp2(v, OP_SorterSort, sAggInfo.sortingIdx, addrEnd);
104452         VdbeComment((v, "GROUP BY sort"));
104453         sAggInfo.useSortingIdx = 1;
104454         sqlite3ExprCacheClear(pParse);
104455       }
104456 
104457       /* Evaluate the current GROUP BY terms and store in b0, b1, b2...
104458       ** (b0 is memory location iBMem+0, b1 is iBMem+1, and so forth)
104459       ** Then compare the current GROUP BY terms against the GROUP BY terms
104460       ** from the previous row currently stored in a0, a1, a2...
104461       */
104462       addrTopOfLoop = sqlite3VdbeCurrentAddr(v);
104463       sqlite3ExprCacheClear(pParse);
104464       if( groupBySort ){
104465         sqlite3VdbeAddOp2(v, OP_SorterData, sAggInfo.sortingIdx, sortOut);
104466       }
104467       for(j=0; j<pGroupBy->nExpr; j++){
104468         if( groupBySort ){
104469           sqlite3VdbeAddOp3(v, OP_Column, sortPTab, j, iBMem+j);
104470           if( j==0 ) sqlite3VdbeChangeP5(v, OPFLAG_CLEARCACHE);
104471         }else{
104472           sAggInfo.directMode = 1;
104473           sqlite3ExprCode(pParse, pGroupBy->a[j].pExpr, iBMem+j);
104474         }
104475       }
104476       sqlite3VdbeAddOp4(v, OP_Compare, iAMem, iBMem, pGroupBy->nExpr,
104477                           (char*)sqlite3KeyInfoRef(pKeyInfo), P4_KEYINFO);
104478       j1 = sqlite3VdbeCurrentAddr(v);
104479       sqlite3VdbeAddOp3(v, OP_Jump, j1+1, 0, j1+1);
104480 
104481       /* Generate code that runs whenever the GROUP BY changes.
104482       ** Changes in the GROUP BY are detected by the previous code
104483       ** block.  If there were no changes, this block is skipped.
104484       **
104485       ** This code copies current group by terms in b0,b1,b2,...
104486       ** over to a0,a1,a2.  It then calls the output subroutine
104487       ** and resets the aggregate accumulator registers in preparation
104488       ** for the next GROUP BY batch.
104489       */
104490       sqlite3ExprCodeMove(pParse, iBMem, iAMem, pGroupBy->nExpr);
104491       sqlite3VdbeAddOp2(v, OP_Gosub, regOutputRow, addrOutputRow);
104492       VdbeComment((v, "output one row"));
104493       sqlite3VdbeAddOp2(v, OP_IfPos, iAbortFlag, addrEnd);
104494       VdbeComment((v, "check abort flag"));
104495       sqlite3VdbeAddOp2(v, OP_Gosub, regReset, addrReset);
104496       VdbeComment((v, "reset accumulator"));
104497 
104498       /* Update the aggregate accumulators based on the content of
104499       ** the current row
104500       */
104501       sqlite3VdbeJumpHere(v, j1);
104502       updateAccumulator(pParse, &sAggInfo);
104503       sqlite3VdbeAddOp2(v, OP_Integer, 1, iUseFlag);
104504       VdbeComment((v, "indicate data in accumulator"));
104505 
104506       /* End of the loop
104507       */
104508       if( groupBySort ){
104509         sqlite3VdbeAddOp2(v, OP_SorterNext, sAggInfo.sortingIdx, addrTopOfLoop);
104510       }else{
104511         sqlite3WhereEnd(pWInfo);
104512         sqlite3VdbeChangeToNoop(v, addrSortingIdx);
104513       }
104514 
104515       /* Output the final row of result
104516       */
104517       sqlite3VdbeAddOp2(v, OP_Gosub, regOutputRow, addrOutputRow);
104518       VdbeComment((v, "output final row"));
104519 
104520       /* Jump over the subroutines
104521       */
104522       sqlite3VdbeAddOp2(v, OP_Goto, 0, addrEnd);
104523 
104524       /* Generate a subroutine that outputs a single row of the result
104525       ** set.  This subroutine first looks at the iUseFlag.  If iUseFlag
104526       ** is less than or equal to zero, the subroutine is a no-op.  If
104527       ** the processing calls for the query to abort, this subroutine
104528       ** increments the iAbortFlag memory location before returning in
104529       ** order to signal the caller to abort.
104530       */
104531       addrSetAbort = sqlite3VdbeCurrentAddr(v);
104532       sqlite3VdbeAddOp2(v, OP_Integer, 1, iAbortFlag);
104533       VdbeComment((v, "set abort flag"));
104534       sqlite3VdbeAddOp1(v, OP_Return, regOutputRow);
104535       sqlite3VdbeResolveLabel(v, addrOutputRow);
104536       addrOutputRow = sqlite3VdbeCurrentAddr(v);
104537       sqlite3VdbeAddOp2(v, OP_IfPos, iUseFlag, addrOutputRow+2);
104538       VdbeComment((v, "Groupby result generator entry point"));
104539       sqlite3VdbeAddOp1(v, OP_Return, regOutputRow);
104540       finalizeAggFunctions(pParse, &sAggInfo);
104541       sqlite3ExprIfFalse(pParse, pHaving, addrOutputRow+1, SQLITE_JUMPIFNULL);
104542       selectInnerLoop(pParse, p, p->pEList, -1, pOrderBy,
104543                       &sDistinct, pDest,
104544                       addrOutputRow+1, addrSetAbort);
104545       sqlite3VdbeAddOp1(v, OP_Return, regOutputRow);
104546       VdbeComment((v, "end groupby result generator"));
104547 
104548       /* Generate a subroutine that will reset the group-by accumulator
104549       */
104550       sqlite3VdbeResolveLabel(v, addrReset);
104551       resetAccumulator(pParse, &sAggInfo);
104552       sqlite3VdbeAddOp1(v, OP_Return, regReset);
104553 
104554     } /* endif pGroupBy.  Begin aggregate queries without GROUP BY: */
104555     else {
104556       ExprList *pDel = 0;
104557 #ifndef SQLITE_OMIT_BTREECOUNT
104558       Table *pTab;
104559       if( (pTab = isSimpleCount(p, &sAggInfo))!=0 ){
104560         /* If isSimpleCount() returns a pointer to a Table structure, then
104561         ** the SQL statement is of the form:
104562         **
104563         **   SELECT count(*) FROM <tbl>
104564         **
104565         ** where the Table structure returned represents table <tbl>.
104566         **
104567         ** This statement is so common that it is optimized specially. The
104568         ** OP_Count instruction is executed either on the intkey table that
104569         ** contains the data for table <tbl> or on one of its indexes. It
104570         ** is better to execute the op on an index, as indexes are almost
104571         ** always spread across less pages than their corresponding tables.
104572         */
104573         const int iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
104574         const int iCsr = pParse->nTab++;     /* Cursor to scan b-tree */
104575         Index *pIdx;                         /* Iterator variable */
104576         KeyInfo *pKeyInfo = 0;               /* Keyinfo for scanned index */
104577         Index *pBest = 0;                    /* Best index found so far */
104578         int iRoot = pTab->tnum;              /* Root page of scanned b-tree */
104579 
104580         sqlite3CodeVerifySchema(pParse, iDb);
104581         sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
104582 
104583         /* Search for the index that has the lowest scan cost.
104584         **
104585         ** (2011-04-15) Do not do a full scan of an unordered index.
104586         **
104587         ** (2013-10-03) Do not count the entries in a partial index.
104588         **
104589         ** In practice the KeyInfo structure will not be used. It is only
104590         ** passed to keep OP_OpenRead happy.
104591         */
104592         if( !HasRowid(pTab) ) pBest = sqlite3PrimaryKeyIndex(pTab);
104593         for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
104594           if( pIdx->bUnordered==0
104595            && pIdx->szIdxRow<pTab->szTabRow
104596            && pIdx->pPartIdxWhere==0
104597            && (!pBest || pIdx->szIdxRow<pBest->szIdxRow)
104598           ){
104599             pBest = pIdx;
104600           }
104601         }
104602         if( pBest ){
104603           iRoot = pBest->tnum;
104604           pKeyInfo = sqlite3KeyInfoOfIndex(pParse, pBest);
104605         }
104606 
104607         /* Open a read-only cursor, execute the OP_Count, close the cursor. */
104608         sqlite3VdbeAddOp4Int(v, OP_OpenRead, iCsr, iRoot, iDb, 1);
104609         if( pKeyInfo ){
104610           sqlite3VdbeChangeP4(v, -1, (char *)pKeyInfo, P4_KEYINFO);
104611         }
104612         sqlite3VdbeAddOp2(v, OP_Count, iCsr, sAggInfo.aFunc[0].iMem);
104613         sqlite3VdbeAddOp1(v, OP_Close, iCsr);
104614         explainSimpleCount(pParse, pTab, pBest);
104615       }else
104616 #endif /* SQLITE_OMIT_BTREECOUNT */
104617       {
104618         /* Check if the query is of one of the following forms:
104619         **
104620         **   SELECT min(x) FROM ...
104621         **   SELECT max(x) FROM ...
104622         **
104623         ** If it is, then ask the code in where.c to attempt to sort results
104624         ** as if there was an "ORDER ON x" or "ORDER ON x DESC" clause.
104625         ** If where.c is able to produce results sorted in this order, then
104626         ** add vdbe code to break out of the processing loop after the
104627         ** first iteration (since the first iteration of the loop is
104628         ** guaranteed to operate on the row with the minimum or maximum
104629         ** value of x, the only row required).
104630         **
104631         ** A special flag must be passed to sqlite3WhereBegin() to slightly
104632         ** modify behavior as follows:
104633         **
104634         **   + If the query is a "SELECT min(x)", then the loop coded by
104635         **     where.c should not iterate over any values with a NULL value
104636         **     for x.
104637         **
104638         **   + The optimizer code in where.c (the thing that decides which
104639         **     index or indices to use) should place a different priority on
104640         **     satisfying the 'ORDER BY' clause than it does in other cases.
104641         **     Refer to code and comments in where.c for details.
104642         */
104643         ExprList *pMinMax = 0;
104644         u8 flag = WHERE_ORDERBY_NORMAL;
104645 
104646         assert( p->pGroupBy==0 );
104647         assert( flag==0 );
104648         if( p->pHaving==0 ){
104649           flag = minMaxQuery(&sAggInfo, &pMinMax);
104650         }
104651         assert( flag==0 || (pMinMax!=0 && pMinMax->nExpr==1) );
104652 
104653         if( flag ){
104654           pMinMax = sqlite3ExprListDup(db, pMinMax, 0);
104655           pDel = pMinMax;
104656           if( pMinMax && !db->mallocFailed ){
104657             pMinMax->a[0].sortOrder = flag!=WHERE_ORDERBY_MIN ?1:0;
104658             pMinMax->a[0].pExpr->op = TK_COLUMN;
104659           }
104660         }
104661 
104662         /* This case runs if the aggregate has no GROUP BY clause.  The
104663         ** processing is much simpler since there is only a single row
104664         ** of output.
104665         */
104666         resetAccumulator(pParse, &sAggInfo);
104667         pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pMinMax,0,flag,0);
104668         if( pWInfo==0 ){
104669           sqlite3ExprListDelete(db, pDel);
104670           goto select_end;
104671         }
104672         updateAccumulator(pParse, &sAggInfo);
104673         assert( pMinMax==0 || pMinMax->nExpr==1 );
104674         if( sqlite3WhereIsOrdered(pWInfo) ){
104675           sqlite3VdbeAddOp2(v, OP_Goto, 0, sqlite3WhereBreakLabel(pWInfo));
104676           VdbeComment((v, "%s() by index",
104677                 (flag==WHERE_ORDERBY_MIN?"min":"max")));
104678         }
104679         sqlite3WhereEnd(pWInfo);
104680         finalizeAggFunctions(pParse, &sAggInfo);
104681       }
104682 
104683       pOrderBy = 0;
104684       sqlite3ExprIfFalse(pParse, pHaving, addrEnd, SQLITE_JUMPIFNULL);
104685       selectInnerLoop(pParse, p, p->pEList, -1, 0, 0,
104686                       pDest, addrEnd, addrEnd);
104687       sqlite3ExprListDelete(db, pDel);
104688     }
104689     sqlite3VdbeResolveLabel(v, addrEnd);
104690 
104691   } /* endif aggregate query */
104692 
104693   if( sDistinct.eTnctType==WHERE_DISTINCT_UNORDERED ){
104694     explainTempTable(pParse, "DISTINCT");
104695   }
104696 
104697   /* If there is an ORDER BY clause, then we need to sort the results
104698   ** and send them to the callback one by one.
104699   */
104700   if( pOrderBy ){
104701     explainTempTable(pParse, "ORDER BY");
104702     generateSortTail(pParse, p, v, pEList->nExpr, pDest);
104703   }
104704 
104705   /* Jump here to skip this query
104706   */
104707   sqlite3VdbeResolveLabel(v, iEnd);
104708 
104709   /* The SELECT was successfully coded.   Set the return code to 0
104710   ** to indicate no errors.
104711   */
104712   rc = 0;
104713 
104714   /* Control jumps to here if an error is encountered above, or upon
104715   ** successful coding of the SELECT.
104716   */
104717 select_end:
104718   explainSetInteger(pParse->iSelectId, iRestoreSelectId);
104719 
104720   /* Identify column names if results of the SELECT are to be output.
104721   */
104722   if( rc==SQLITE_OK && pDest->eDest==SRT_Output ){
104723     generateColumnNames(pParse, pTabList, pEList);
104724   }
104725 
104726   sqlite3DbFree(db, sAggInfo.aCol);
104727   sqlite3DbFree(db, sAggInfo.aFunc);
104728   return rc;
104729 }
104730 
104731 #if defined(SQLITE_ENABLE_TREE_EXPLAIN)
104732 /*
104733 ** Generate a human-readable description of a the Select object.
104734 */
104735 static void explainOneSelect(Vdbe *pVdbe, Select *p){
104736   sqlite3ExplainPrintf(pVdbe, "SELECT ");
104737   if( p->selFlags & (SF_Distinct|SF_Aggregate) ){
104738     if( p->selFlags & SF_Distinct ){
104739       sqlite3ExplainPrintf(pVdbe, "DISTINCT ");
104740     }
104741     if( p->selFlags & SF_Aggregate ){
104742       sqlite3ExplainPrintf(pVdbe, "agg_flag ");
104743     }
104744     sqlite3ExplainNL(pVdbe);
104745     sqlite3ExplainPrintf(pVdbe, "   ");
104746   }
104747   sqlite3ExplainExprList(pVdbe, p->pEList);
104748   sqlite3ExplainNL(pVdbe);
104749   if( p->pSrc && p->pSrc->nSrc ){
104750     int i;
104751     sqlite3ExplainPrintf(pVdbe, "FROM ");
104752     sqlite3ExplainPush(pVdbe);
104753     for(i=0; i<p->pSrc->nSrc; i++){
104754       struct SrcList_item *pItem = &p->pSrc->a[i];
104755       sqlite3ExplainPrintf(pVdbe, "{%d,*} = ", pItem->iCursor);
104756       if( pItem->pSelect ){
104757         sqlite3ExplainSelect(pVdbe, pItem->pSelect);
104758         if( pItem->pTab ){
104759           sqlite3ExplainPrintf(pVdbe, " (tabname=%s)", pItem->pTab->zName);
104760         }
104761       }else if( pItem->zName ){
104762         sqlite3ExplainPrintf(pVdbe, "%s", pItem->zName);
104763       }
104764       if( pItem->zAlias ){
104765         sqlite3ExplainPrintf(pVdbe, " (AS %s)", pItem->zAlias);
104766       }
104767       if( pItem->jointype & JT_LEFT ){
104768         sqlite3ExplainPrintf(pVdbe, " LEFT-JOIN");
104769       }
104770       sqlite3ExplainNL(pVdbe);
104771     }
104772     sqlite3ExplainPop(pVdbe);
104773   }
104774   if( p->pWhere ){
104775     sqlite3ExplainPrintf(pVdbe, "WHERE ");
104776     sqlite3ExplainExpr(pVdbe, p->pWhere);
104777     sqlite3ExplainNL(pVdbe);
104778   }
104779   if( p->pGroupBy ){
104780     sqlite3ExplainPrintf(pVdbe, "GROUPBY ");
104781     sqlite3ExplainExprList(pVdbe, p->pGroupBy);
104782     sqlite3ExplainNL(pVdbe);
104783   }
104784   if( p->pHaving ){
104785     sqlite3ExplainPrintf(pVdbe, "HAVING ");
104786     sqlite3ExplainExpr(pVdbe, p->pHaving);
104787     sqlite3ExplainNL(pVdbe);
104788   }
104789   if( p->pOrderBy ){
104790     sqlite3ExplainPrintf(pVdbe, "ORDERBY ");
104791     sqlite3ExplainExprList(pVdbe, p->pOrderBy);
104792     sqlite3ExplainNL(pVdbe);
104793   }
104794   if( p->pLimit ){
104795     sqlite3ExplainPrintf(pVdbe, "LIMIT ");
104796     sqlite3ExplainExpr(pVdbe, p->pLimit);
104797     sqlite3ExplainNL(pVdbe);
104798   }
104799   if( p->pOffset ){
104800     sqlite3ExplainPrintf(pVdbe, "OFFSET ");
104801     sqlite3ExplainExpr(pVdbe, p->pOffset);
104802     sqlite3ExplainNL(pVdbe);
104803   }
104804 }
104805 SQLITE_PRIVATE void sqlite3ExplainSelect(Vdbe *pVdbe, Select *p){
104806   if( p==0 ){
104807     sqlite3ExplainPrintf(pVdbe, "(null-select)");
104808     return;
104809   }
104810   while( p->pPrior ){
104811     p->pPrior->pNext = p;
104812     p = p->pPrior;
104813   }
104814   sqlite3ExplainPush(pVdbe);
104815   while( p ){
104816     explainOneSelect(pVdbe, p);
104817     p = p->pNext;
104818     if( p==0 ) break;
104819     sqlite3ExplainNL(pVdbe);
104820     sqlite3ExplainPrintf(pVdbe, "%s\n", selectOpName(p->op));
104821   }
104822   sqlite3ExplainPrintf(pVdbe, "END");
104823   sqlite3ExplainPop(pVdbe);
104824 }
104825 
104826 /* End of the structure debug printing code
104827 *****************************************************************************/
104828 #endif /* defined(SQLITE_ENABLE_TREE_EXPLAIN) */
104829 
104830 /************** End of select.c **********************************************/
104831 /************** Begin file table.c *******************************************/
104832 /*
104833 ** 2001 September 15
104834 **
104835 ** The author disclaims copyright to this source code.  In place of
104836 ** a legal notice, here is a blessing:
104837 **
104838 **    May you do good and not evil.
104839 **    May you find forgiveness for yourself and forgive others.
104840 **    May you share freely, never taking more than you give.
104841 **
104842 *************************************************************************
104843 ** This file contains the sqlite3_get_table() and sqlite3_free_table()
104844 ** interface routines.  These are just wrappers around the main
104845 ** interface routine of sqlite3_exec().
104846 **
104847 ** These routines are in a separate files so that they will not be linked
104848 ** if they are not used.
104849 */
104850 /* #include <stdlib.h> */
104851 /* #include <string.h> */
104852 
104853 #ifndef SQLITE_OMIT_GET_TABLE
104854 
104855 /*
104856 ** This structure is used to pass data from sqlite3_get_table() through
104857 ** to the callback function is uses to build the result.
104858 */
104859 typedef struct TabResult {
104860   char **azResult;   /* Accumulated output */
104861   char *zErrMsg;     /* Error message text, if an error occurs */
104862   int nAlloc;        /* Slots allocated for azResult[] */
104863   int nRow;          /* Number of rows in the result */
104864   int nColumn;       /* Number of columns in the result */
104865   int nData;         /* Slots used in azResult[].  (nRow+1)*nColumn */
104866   int rc;            /* Return code from sqlite3_exec() */
104867 } TabResult;
104868 
104869 /*
104870 ** This routine is called once for each row in the result table.  Its job
104871 ** is to fill in the TabResult structure appropriately, allocating new
104872 ** memory as necessary.
104873 */
104874 static int sqlite3_get_table_cb(void *pArg, int nCol, char **argv, char **colv){
104875   TabResult *p = (TabResult*)pArg;  /* Result accumulator */
104876   int need;                         /* Slots needed in p->azResult[] */
104877   int i;                            /* Loop counter */
104878   char *z;                          /* A single column of result */
104879 
104880   /* Make sure there is enough space in p->azResult to hold everything
104881   ** we need to remember from this invocation of the callback.
104882   */
104883   if( p->nRow==0 && argv!=0 ){
104884     need = nCol*2;
104885   }else{
104886     need = nCol;
104887   }
104888   if( p->nData + need > p->nAlloc ){
104889     char **azNew;
104890     p->nAlloc = p->nAlloc*2 + need;
104891     azNew = sqlite3_realloc( p->azResult, sizeof(char*)*p->nAlloc );
104892     if( azNew==0 ) goto malloc_failed;
104893     p->azResult = azNew;
104894   }
104895 
104896   /* If this is the first row, then generate an extra row containing
104897   ** the names of all columns.
104898   */
104899   if( p->nRow==0 ){
104900     p->nColumn = nCol;
104901     for(i=0; i<nCol; i++){
104902       z = sqlite3_mprintf("%s", colv[i]);
104903       if( z==0 ) goto malloc_failed;
104904       p->azResult[p->nData++] = z;
104905     }
104906   }else if( p->nColumn!=nCol ){
104907     sqlite3_free(p->zErrMsg);
104908     p->zErrMsg = sqlite3_mprintf(
104909        "sqlite3_get_table() called with two or more incompatible queries"
104910     );
104911     p->rc = SQLITE_ERROR;
104912     return 1;
104913   }
104914 
104915   /* Copy over the row data
104916   */
104917   if( argv!=0 ){
104918     for(i=0; i<nCol; i++){
104919       if( argv[i]==0 ){
104920         z = 0;
104921       }else{
104922         int n = sqlite3Strlen30(argv[i])+1;
104923         z = sqlite3_malloc( n );
104924         if( z==0 ) goto malloc_failed;
104925         memcpy(z, argv[i], n);
104926       }
104927       p->azResult[p->nData++] = z;
104928     }
104929     p->nRow++;
104930   }
104931   return 0;
104932 
104933 malloc_failed:
104934   p->rc = SQLITE_NOMEM;
104935   return 1;
104936 }
104937 
104938 /*
104939 ** Query the database.  But instead of invoking a callback for each row,
104940 ** malloc() for space to hold the result and return the entire results
104941 ** at the conclusion of the call.
104942 **
104943 ** The result that is written to ***pazResult is held in memory obtained
104944 ** from malloc().  But the caller cannot free this memory directly.
104945 ** Instead, the entire table should be passed to sqlite3_free_table() when
104946 ** the calling procedure is finished using it.
104947 */
104948 SQLITE_API int sqlite3_get_table(
104949   sqlite3 *db,                /* The database on which the SQL executes */
104950   const char *zSql,           /* The SQL to be executed */
104951   char ***pazResult,          /* Write the result table here */
104952   int *pnRow,                 /* Write the number of rows in the result here */
104953   int *pnColumn,              /* Write the number of columns of result here */
104954   char **pzErrMsg             /* Write error messages here */
104955 ){
104956   int rc;
104957   TabResult res;
104958 
104959   *pazResult = 0;
104960   if( pnColumn ) *pnColumn = 0;
104961   if( pnRow ) *pnRow = 0;
104962   if( pzErrMsg ) *pzErrMsg = 0;
104963   res.zErrMsg = 0;
104964   res.nRow = 0;
104965   res.nColumn = 0;
104966   res.nData = 1;
104967   res.nAlloc = 20;
104968   res.rc = SQLITE_OK;
104969   res.azResult = sqlite3_malloc(sizeof(char*)*res.nAlloc );
104970   if( res.azResult==0 ){
104971      db->errCode = SQLITE_NOMEM;
104972      return SQLITE_NOMEM;
104973   }
104974   res.azResult[0] = 0;
104975   rc = sqlite3_exec(db, zSql, sqlite3_get_table_cb, &res, pzErrMsg);
104976   assert( sizeof(res.azResult[0])>= sizeof(res.nData) );
104977   res.azResult[0] = SQLITE_INT_TO_PTR(res.nData);
104978   if( (rc&0xff)==SQLITE_ABORT ){
104979     sqlite3_free_table(&res.azResult[1]);
104980     if( res.zErrMsg ){
104981       if( pzErrMsg ){
104982         sqlite3_free(*pzErrMsg);
104983         *pzErrMsg = sqlite3_mprintf("%s",res.zErrMsg);
104984       }
104985       sqlite3_free(res.zErrMsg);
104986     }
104987     db->errCode = res.rc;  /* Assume 32-bit assignment is atomic */
104988     return res.rc;
104989   }
104990   sqlite3_free(res.zErrMsg);
104991   if( rc!=SQLITE_OK ){
104992     sqlite3_free_table(&res.azResult[1]);
104993     return rc;
104994   }
104995   if( res.nAlloc>res.nData ){
104996     char **azNew;
104997     azNew = sqlite3_realloc( res.azResult, sizeof(char*)*res.nData );
104998     if( azNew==0 ){
104999       sqlite3_free_table(&res.azResult[1]);
105000       db->errCode = SQLITE_NOMEM;
105001       return SQLITE_NOMEM;
105002     }
105003     res.azResult = azNew;
105004   }
105005   *pazResult = &res.azResult[1];
105006   if( pnColumn ) *pnColumn = res.nColumn;
105007   if( pnRow ) *pnRow = res.nRow;
105008   return rc;
105009 }
105010 
105011 /*
105012 ** This routine frees the space the sqlite3_get_table() malloced.
105013 */
105014 SQLITE_API void sqlite3_free_table(
105015   char **azResult            /* Result returned from from sqlite3_get_table() */
105016 ){
105017   if( azResult ){
105018     int i, n;
105019     azResult--;
105020     assert( azResult!=0 );
105021     n = SQLITE_PTR_TO_INT(azResult[0]);
105022     for(i=1; i<n; i++){ if( azResult[i] ) sqlite3_free(azResult[i]); }
105023     sqlite3_free(azResult);
105024   }
105025 }
105026 
105027 #endif /* SQLITE_OMIT_GET_TABLE */
105028 
105029 /************** End of table.c ***********************************************/
105030 /************** Begin file trigger.c *****************************************/
105031 /*
105032 **
105033 ** The author disclaims copyright to this source code.  In place of
105034 ** a legal notice, here is a blessing:
105035 **
105036 **    May you do good and not evil.
105037 **    May you find forgiveness for yourself and forgive others.
105038 **    May you share freely, never taking more than you give.
105039 **
105040 *************************************************************************
105041 ** This file contains the implementation for TRIGGERs
105042 */
105043 
105044 #ifndef SQLITE_OMIT_TRIGGER
105045 /*
105046 ** Delete a linked list of TriggerStep structures.
105047 */
105048 SQLITE_PRIVATE void sqlite3DeleteTriggerStep(sqlite3 *db, TriggerStep *pTriggerStep){
105049   while( pTriggerStep ){
105050     TriggerStep * pTmp = pTriggerStep;
105051     pTriggerStep = pTriggerStep->pNext;
105052 
105053     sqlite3ExprDelete(db, pTmp->pWhere);
105054     sqlite3ExprListDelete(db, pTmp->pExprList);
105055     sqlite3SelectDelete(db, pTmp->pSelect);
105056     sqlite3IdListDelete(db, pTmp->pIdList);
105057 
105058     sqlite3DbFree(db, pTmp);
105059   }
105060 }
105061 
105062 /*
105063 ** Given table pTab, return a list of all the triggers attached to
105064 ** the table. The list is connected by Trigger.pNext pointers.
105065 **
105066 ** All of the triggers on pTab that are in the same database as pTab
105067 ** are already attached to pTab->pTrigger.  But there might be additional
105068 ** triggers on pTab in the TEMP schema.  This routine prepends all
105069 ** TEMP triggers on pTab to the beginning of the pTab->pTrigger list
105070 ** and returns the combined list.
105071 **
105072 ** To state it another way:  This routine returns a list of all triggers
105073 ** that fire off of pTab.  The list will include any TEMP triggers on
105074 ** pTab as well as the triggers lised in pTab->pTrigger.
105075 */
105076 SQLITE_PRIVATE Trigger *sqlite3TriggerList(Parse *pParse, Table *pTab){
105077   Schema * const pTmpSchema = pParse->db->aDb[1].pSchema;
105078   Trigger *pList = 0;                  /* List of triggers to return */
105079 
105080   if( pParse->disableTriggers ){
105081     return 0;
105082   }
105083 
105084   if( pTmpSchema!=pTab->pSchema ){
105085     HashElem *p;
105086     assert( sqlite3SchemaMutexHeld(pParse->db, 0, pTmpSchema) );
105087     for(p=sqliteHashFirst(&pTmpSchema->trigHash); p; p=sqliteHashNext(p)){
105088       Trigger *pTrig = (Trigger *)sqliteHashData(p);
105089       if( pTrig->pTabSchema==pTab->pSchema
105090        && 0==sqlite3StrICmp(pTrig->table, pTab->zName)
105091       ){
105092         pTrig->pNext = (pList ? pList : pTab->pTrigger);
105093         pList = pTrig;
105094       }
105095     }
105096   }
105097 
105098   return (pList ? pList : pTab->pTrigger);
105099 }
105100 
105101 /*
105102 ** This is called by the parser when it sees a CREATE TRIGGER statement
105103 ** up to the point of the BEGIN before the trigger actions.  A Trigger
105104 ** structure is generated based on the information available and stored
105105 ** in pParse->pNewTrigger.  After the trigger actions have been parsed, the
105106 ** sqlite3FinishTrigger() function is called to complete the trigger
105107 ** construction process.
105108 */
105109 SQLITE_PRIVATE void sqlite3BeginTrigger(
105110   Parse *pParse,      /* The parse context of the CREATE TRIGGER statement */
105111   Token *pName1,      /* The name of the trigger */
105112   Token *pName2,      /* The name of the trigger */
105113   int tr_tm,          /* One of TK_BEFORE, TK_AFTER, TK_INSTEAD */
105114   int op,             /* One of TK_INSERT, TK_UPDATE, TK_DELETE */
105115   IdList *pColumns,   /* column list if this is an UPDATE OF trigger */
105116   SrcList *pTableName,/* The name of the table/view the trigger applies to */
105117   Expr *pWhen,        /* WHEN clause */
105118   int isTemp,         /* True if the TEMPORARY keyword is present */
105119   int noErr           /* Suppress errors if the trigger already exists */
105120 ){
105121   Trigger *pTrigger = 0;  /* The new trigger */
105122   Table *pTab;            /* Table that the trigger fires off of */
105123   char *zName = 0;        /* Name of the trigger */
105124   sqlite3 *db = pParse->db;  /* The database connection */
105125   int iDb;                /* The database to store the trigger in */
105126   Token *pName;           /* The unqualified db name */
105127   DbFixer sFix;           /* State vector for the DB fixer */
105128   int iTabDb;             /* Index of the database holding pTab */
105129 
105130   assert( pName1!=0 );   /* pName1->z might be NULL, but not pName1 itself */
105131   assert( pName2!=0 );
105132   assert( op==TK_INSERT || op==TK_UPDATE || op==TK_DELETE );
105133   assert( op>0 && op<0xff );
105134   if( isTemp ){
105135     /* If TEMP was specified, then the trigger name may not be qualified. */
105136     if( pName2->n>0 ){
105137       sqlite3ErrorMsg(pParse, "temporary trigger may not have qualified name");
105138       goto trigger_cleanup;
105139     }
105140     iDb = 1;
105141     pName = pName1;
105142   }else{
105143     /* Figure out the db that the trigger will be created in */
105144     iDb = sqlite3TwoPartName(pParse, pName1, pName2, &pName);
105145     if( iDb<0 ){
105146       goto trigger_cleanup;
105147     }
105148   }
105149   if( !pTableName || db->mallocFailed ){
105150     goto trigger_cleanup;
105151   }
105152 
105153   /* A long-standing parser bug is that this syntax was allowed:
105154   **
105155   **    CREATE TRIGGER attached.demo AFTER INSERT ON attached.tab ....
105156   **                                                 ^^^^^^^^
105157   **
105158   ** To maintain backwards compatibility, ignore the database
105159   ** name on pTableName if we are reparsing our of SQLITE_MASTER.
105160   */
105161   if( db->init.busy && iDb!=1 ){
105162     sqlite3DbFree(db, pTableName->a[0].zDatabase);
105163     pTableName->a[0].zDatabase = 0;
105164   }
105165 
105166   /* If the trigger name was unqualified, and the table is a temp table,
105167   ** then set iDb to 1 to create the trigger in the temporary database.
105168   ** If sqlite3SrcListLookup() returns 0, indicating the table does not
105169   ** exist, the error is caught by the block below.
105170   */
105171   pTab = sqlite3SrcListLookup(pParse, pTableName);
105172   if( db->init.busy==0 && pName2->n==0 && pTab
105173         && pTab->pSchema==db->aDb[1].pSchema ){
105174     iDb = 1;
105175   }
105176 
105177   /* Ensure the table name matches database name and that the table exists */
105178   if( db->mallocFailed ) goto trigger_cleanup;
105179   assert( pTableName->nSrc==1 );
105180   sqlite3FixInit(&sFix, pParse, iDb, "trigger", pName);
105181   if( sqlite3FixSrcList(&sFix, pTableName) ){
105182     goto trigger_cleanup;
105183   }
105184   pTab = sqlite3SrcListLookup(pParse, pTableName);
105185   if( !pTab ){
105186     /* The table does not exist. */
105187     if( db->init.iDb==1 ){
105188       /* Ticket #3810.
105189       ** Normally, whenever a table is dropped, all associated triggers are
105190       ** dropped too.  But if a TEMP trigger is created on a non-TEMP table
105191       ** and the table is dropped by a different database connection, the
105192       ** trigger is not visible to the database connection that does the
105193       ** drop so the trigger cannot be dropped.  This results in an
105194       ** "orphaned trigger" - a trigger whose associated table is missing.
105195       */
105196       db->init.orphanTrigger = 1;
105197     }
105198     goto trigger_cleanup;
105199   }
105200   if( IsVirtual(pTab) ){
105201     sqlite3ErrorMsg(pParse, "cannot create triggers on virtual tables");
105202     goto trigger_cleanup;
105203   }
105204 
105205   /* Check that the trigger name is not reserved and that no trigger of the
105206   ** specified name exists */
105207   zName = sqlite3NameFromToken(db, pName);
105208   if( !zName || SQLITE_OK!=sqlite3CheckObjectName(pParse, zName) ){
105209     goto trigger_cleanup;
105210   }
105211   assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
105212   if( sqlite3HashFind(&(db->aDb[iDb].pSchema->trigHash),
105213                       zName, sqlite3Strlen30(zName)) ){
105214     if( !noErr ){
105215       sqlite3ErrorMsg(pParse, "trigger %T already exists", pName);
105216     }else{
105217       assert( !db->init.busy );
105218       sqlite3CodeVerifySchema(pParse, iDb);
105219     }
105220     goto trigger_cleanup;
105221   }
105222 
105223   /* Do not create a trigger on a system table */
105224   if( sqlite3StrNICmp(pTab->zName, "sqlite_", 7)==0 ){
105225     sqlite3ErrorMsg(pParse, "cannot create trigger on system table");
105226     pParse->nErr++;
105227     goto trigger_cleanup;
105228   }
105229 
105230   /* INSTEAD of triggers are only for views and views only support INSTEAD
105231   ** of triggers.
105232   */
105233   if( pTab->pSelect && tr_tm!=TK_INSTEAD ){
105234     sqlite3ErrorMsg(pParse, "cannot create %s trigger on view: %S",
105235         (tr_tm == TK_BEFORE)?"BEFORE":"AFTER", pTableName, 0);
105236     goto trigger_cleanup;
105237   }
105238   if( !pTab->pSelect && tr_tm==TK_INSTEAD ){
105239     sqlite3ErrorMsg(pParse, "cannot create INSTEAD OF"
105240         " trigger on table: %S", pTableName, 0);
105241     goto trigger_cleanup;
105242   }
105243   iTabDb = sqlite3SchemaToIndex(db, pTab->pSchema);
105244 
105245 #ifndef SQLITE_OMIT_AUTHORIZATION
105246   {
105247     int code = SQLITE_CREATE_TRIGGER;
105248     const char *zDb = db->aDb[iTabDb].zName;
105249     const char *zDbTrig = isTemp ? db->aDb[1].zName : zDb;
105250     if( iTabDb==1 || isTemp ) code = SQLITE_CREATE_TEMP_TRIGGER;
105251     if( sqlite3AuthCheck(pParse, code, zName, pTab->zName, zDbTrig) ){
105252       goto trigger_cleanup;
105253     }
105254     if( sqlite3AuthCheck(pParse, SQLITE_INSERT, SCHEMA_TABLE(iTabDb),0,zDb)){
105255       goto trigger_cleanup;
105256     }
105257   }
105258 #endif
105259 
105260   /* INSTEAD OF triggers can only appear on views and BEFORE triggers
105261   ** cannot appear on views.  So we might as well translate every
105262   ** INSTEAD OF trigger into a BEFORE trigger.  It simplifies code
105263   ** elsewhere.
105264   */
105265   if (tr_tm == TK_INSTEAD){
105266     tr_tm = TK_BEFORE;
105267   }
105268 
105269   /* Build the Trigger object */
105270   pTrigger = (Trigger*)sqlite3DbMallocZero(db, sizeof(Trigger));
105271   if( pTrigger==0 ) goto trigger_cleanup;
105272   pTrigger->zName = zName;
105273   zName = 0;
105274   pTrigger->table = sqlite3DbStrDup(db, pTableName->a[0].zName);
105275   pTrigger->pSchema = db->aDb[iDb].pSchema;
105276   pTrigger->pTabSchema = pTab->pSchema;
105277   pTrigger->op = (u8)op;
105278   pTrigger->tr_tm = tr_tm==TK_BEFORE ? TRIGGER_BEFORE : TRIGGER_AFTER;
105279   pTrigger->pWhen = sqlite3ExprDup(db, pWhen, EXPRDUP_REDUCE);
105280   pTrigger->pColumns = sqlite3IdListDup(db, pColumns);
105281   assert( pParse->pNewTrigger==0 );
105282   pParse->pNewTrigger = pTrigger;
105283 
105284 trigger_cleanup:
105285   sqlite3DbFree(db, zName);
105286   sqlite3SrcListDelete(db, pTableName);
105287   sqlite3IdListDelete(db, pColumns);
105288   sqlite3ExprDelete(db, pWhen);
105289   if( !pParse->pNewTrigger ){
105290     sqlite3DeleteTrigger(db, pTrigger);
105291   }else{
105292     assert( pParse->pNewTrigger==pTrigger );
105293   }
105294 }
105295 
105296 /*
105297 ** This routine is called after all of the trigger actions have been parsed
105298 ** in order to complete the process of building the trigger.
105299 */
105300 SQLITE_PRIVATE void sqlite3FinishTrigger(
105301   Parse *pParse,          /* Parser context */
105302   TriggerStep *pStepList, /* The triggered program */
105303   Token *pAll             /* Token that describes the complete CREATE TRIGGER */
105304 ){
105305   Trigger *pTrig = pParse->pNewTrigger;   /* Trigger being finished */
105306   char *zName;                            /* Name of trigger */
105307   sqlite3 *db = pParse->db;               /* The database */
105308   DbFixer sFix;                           /* Fixer object */
105309   int iDb;                                /* Database containing the trigger */
105310   Token nameToken;                        /* Trigger name for error reporting */
105311 
105312   pParse->pNewTrigger = 0;
105313   if( NEVER(pParse->nErr) || !pTrig ) goto triggerfinish_cleanup;
105314   zName = pTrig->zName;
105315   iDb = sqlite3SchemaToIndex(pParse->db, pTrig->pSchema);
105316   pTrig->step_list = pStepList;
105317   while( pStepList ){
105318     pStepList->pTrig = pTrig;
105319     pStepList = pStepList->pNext;
105320   }
105321   nameToken.z = pTrig->zName;
105322   nameToken.n = sqlite3Strlen30(nameToken.z);
105323   sqlite3FixInit(&sFix, pParse, iDb, "trigger", &nameToken);
105324   if( sqlite3FixTriggerStep(&sFix, pTrig->step_list)
105325    || sqlite3FixExpr(&sFix, pTrig->pWhen)
105326   ){
105327     goto triggerfinish_cleanup;
105328   }
105329 
105330   /* if we are not initializing,
105331   ** build the sqlite_master entry
105332   */
105333   if( !db->init.busy ){
105334     Vdbe *v;
105335     char *z;
105336 
105337     /* Make an entry in the sqlite_master table */
105338     v = sqlite3GetVdbe(pParse);
105339     if( v==0 ) goto triggerfinish_cleanup;
105340     sqlite3BeginWriteOperation(pParse, 0, iDb);
105341     z = sqlite3DbStrNDup(db, (char*)pAll->z, pAll->n);
105342     sqlite3NestedParse(pParse,
105343        "INSERT INTO %Q.%s VALUES('trigger',%Q,%Q,0,'CREATE TRIGGER %q')",
105344        db->aDb[iDb].zName, SCHEMA_TABLE(iDb), zName,
105345        pTrig->table, z);
105346     sqlite3DbFree(db, z);
105347     sqlite3ChangeCookie(pParse, iDb);
105348     sqlite3VdbeAddParseSchemaOp(v, iDb,
105349         sqlite3MPrintf(db, "type='trigger' AND name='%q'", zName));
105350   }
105351 
105352   if( db->init.busy ){
105353     Trigger *pLink = pTrig;
105354     Hash *pHash = &db->aDb[iDb].pSchema->trigHash;
105355     assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
105356     pTrig = sqlite3HashInsert(pHash, zName, sqlite3Strlen30(zName), pTrig);
105357     if( pTrig ){
105358       db->mallocFailed = 1;
105359     }else if( pLink->pSchema==pLink->pTabSchema ){
105360       Table *pTab;
105361       int n = sqlite3Strlen30(pLink->table);
105362       pTab = sqlite3HashFind(&pLink->pTabSchema->tblHash, pLink->table, n);
105363       assert( pTab!=0 );
105364       pLink->pNext = pTab->pTrigger;
105365       pTab->pTrigger = pLink;
105366     }
105367   }
105368 
105369 triggerfinish_cleanup:
105370   sqlite3DeleteTrigger(db, pTrig);
105371   assert( !pParse->pNewTrigger );
105372   sqlite3DeleteTriggerStep(db, pStepList);
105373 }
105374 
105375 /*
105376 ** Turn a SELECT statement (that the pSelect parameter points to) into
105377 ** a trigger step.  Return a pointer to a TriggerStep structure.
105378 **
105379 ** The parser calls this routine when it finds a SELECT statement in
105380 ** body of a TRIGGER.
105381 */
105382 SQLITE_PRIVATE TriggerStep *sqlite3TriggerSelectStep(sqlite3 *db, Select *pSelect){
105383   TriggerStep *pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep));
105384   if( pTriggerStep==0 ) {
105385     sqlite3SelectDelete(db, pSelect);
105386     return 0;
105387   }
105388   pTriggerStep->op = TK_SELECT;
105389   pTriggerStep->pSelect = pSelect;
105390   pTriggerStep->orconf = OE_Default;
105391   return pTriggerStep;
105392 }
105393 
105394 /*
105395 ** Allocate space to hold a new trigger step.  The allocated space
105396 ** holds both the TriggerStep object and the TriggerStep.target.z string.
105397 **
105398 ** If an OOM error occurs, NULL is returned and db->mallocFailed is set.
105399 */
105400 static TriggerStep *triggerStepAllocate(
105401   sqlite3 *db,                /* Database connection */
105402   u8 op,                      /* Trigger opcode */
105403   Token *pName                /* The target name */
105404 ){
105405   TriggerStep *pTriggerStep;
105406 
105407   pTriggerStep = sqlite3DbMallocZero(db, sizeof(TriggerStep) + pName->n);
105408   if( pTriggerStep ){
105409     char *z = (char*)&pTriggerStep[1];
105410     memcpy(z, pName->z, pName->n);
105411     pTriggerStep->target.z = z;
105412     pTriggerStep->target.n = pName->n;
105413     pTriggerStep->op = op;
105414   }
105415   return pTriggerStep;
105416 }
105417 
105418 /*
105419 ** Build a trigger step out of an INSERT statement.  Return a pointer
105420 ** to the new trigger step.
105421 **
105422 ** The parser calls this routine when it sees an INSERT inside the
105423 ** body of a trigger.
105424 */
105425 SQLITE_PRIVATE TriggerStep *sqlite3TriggerInsertStep(
105426   sqlite3 *db,        /* The database connection */
105427   Token *pTableName,  /* Name of the table into which we insert */
105428   IdList *pColumn,    /* List of columns in pTableName to insert into */
105429   Select *pSelect,    /* A SELECT statement that supplies values */
105430   u8 orconf           /* The conflict algorithm (OE_Abort, OE_Replace, etc.) */
105431 ){
105432   TriggerStep *pTriggerStep;
105433 
105434   assert(pSelect != 0 || db->mallocFailed);
105435 
105436   pTriggerStep = triggerStepAllocate(db, TK_INSERT, pTableName);
105437   if( pTriggerStep ){
105438     pTriggerStep->pSelect = sqlite3SelectDup(db, pSelect, EXPRDUP_REDUCE);
105439     pTriggerStep->pIdList = pColumn;
105440     pTriggerStep->orconf = orconf;
105441   }else{
105442     sqlite3IdListDelete(db, pColumn);
105443   }
105444   sqlite3SelectDelete(db, pSelect);
105445 
105446   return pTriggerStep;
105447 }
105448 
105449 /*
105450 ** Construct a trigger step that implements an UPDATE statement and return
105451 ** a pointer to that trigger step.  The parser calls this routine when it
105452 ** sees an UPDATE statement inside the body of a CREATE TRIGGER.
105453 */
105454 SQLITE_PRIVATE TriggerStep *sqlite3TriggerUpdateStep(
105455   sqlite3 *db,         /* The database connection */
105456   Token *pTableName,   /* Name of the table to be updated */
105457   ExprList *pEList,    /* The SET clause: list of column and new values */
105458   Expr *pWhere,        /* The WHERE clause */
105459   u8 orconf            /* The conflict algorithm. (OE_Abort, OE_Ignore, etc) */
105460 ){
105461   TriggerStep *pTriggerStep;
105462 
105463   pTriggerStep = triggerStepAllocate(db, TK_UPDATE, pTableName);
105464   if( pTriggerStep ){
105465     pTriggerStep->pExprList = sqlite3ExprListDup(db, pEList, EXPRDUP_REDUCE);
105466     pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
105467     pTriggerStep->orconf = orconf;
105468   }
105469   sqlite3ExprListDelete(db, pEList);
105470   sqlite3ExprDelete(db, pWhere);
105471   return pTriggerStep;
105472 }
105473 
105474 /*
105475 ** Construct a trigger step that implements a DELETE statement and return
105476 ** a pointer to that trigger step.  The parser calls this routine when it
105477 ** sees a DELETE statement inside the body of a CREATE TRIGGER.
105478 */
105479 SQLITE_PRIVATE TriggerStep *sqlite3TriggerDeleteStep(
105480   sqlite3 *db,            /* Database connection */
105481   Token *pTableName,      /* The table from which rows are deleted */
105482   Expr *pWhere            /* The WHERE clause */
105483 ){
105484   TriggerStep *pTriggerStep;
105485 
105486   pTriggerStep = triggerStepAllocate(db, TK_DELETE, pTableName);
105487   if( pTriggerStep ){
105488     pTriggerStep->pWhere = sqlite3ExprDup(db, pWhere, EXPRDUP_REDUCE);
105489     pTriggerStep->orconf = OE_Default;
105490   }
105491   sqlite3ExprDelete(db, pWhere);
105492   return pTriggerStep;
105493 }
105494 
105495 /*
105496 ** Recursively delete a Trigger structure
105497 */
105498 SQLITE_PRIVATE void sqlite3DeleteTrigger(sqlite3 *db, Trigger *pTrigger){
105499   if( pTrigger==0 ) return;
105500   sqlite3DeleteTriggerStep(db, pTrigger->step_list);
105501   sqlite3DbFree(db, pTrigger->zName);
105502   sqlite3DbFree(db, pTrigger->table);
105503   sqlite3ExprDelete(db, pTrigger->pWhen);
105504   sqlite3IdListDelete(db, pTrigger->pColumns);
105505   sqlite3DbFree(db, pTrigger);
105506 }
105507 
105508 /*
105509 ** This function is called to drop a trigger from the database schema.
105510 **
105511 ** This may be called directly from the parser and therefore identifies
105512 ** the trigger by name.  The sqlite3DropTriggerPtr() routine does the
105513 ** same job as this routine except it takes a pointer to the trigger
105514 ** instead of the trigger name.
105515 **/
105516 SQLITE_PRIVATE void sqlite3DropTrigger(Parse *pParse, SrcList *pName, int noErr){
105517   Trigger *pTrigger = 0;
105518   int i;
105519   const char *zDb;
105520   const char *zName;
105521   int nName;
105522   sqlite3 *db = pParse->db;
105523 
105524   if( db->mallocFailed ) goto drop_trigger_cleanup;
105525   if( SQLITE_OK!=sqlite3ReadSchema(pParse) ){
105526     goto drop_trigger_cleanup;
105527   }
105528 
105529   assert( pName->nSrc==1 );
105530   zDb = pName->a[0].zDatabase;
105531   zName = pName->a[0].zName;
105532   nName = sqlite3Strlen30(zName);
105533   assert( zDb!=0 || sqlite3BtreeHoldsAllMutexes(db) );
105534   for(i=OMIT_TEMPDB; i<db->nDb; i++){
105535     int j = (i<2) ? i^1 : i;  /* Search TEMP before MAIN */
105536     if( zDb && sqlite3StrICmp(db->aDb[j].zName, zDb) ) continue;
105537     assert( sqlite3SchemaMutexHeld(db, j, 0) );
105538     pTrigger = sqlite3HashFind(&(db->aDb[j].pSchema->trigHash), zName, nName);
105539     if( pTrigger ) break;
105540   }
105541   if( !pTrigger ){
105542     if( !noErr ){
105543       sqlite3ErrorMsg(pParse, "no such trigger: %S", pName, 0);
105544     }else{
105545       sqlite3CodeVerifyNamedSchema(pParse, zDb);
105546     }
105547     pParse->checkSchema = 1;
105548     goto drop_trigger_cleanup;
105549   }
105550   sqlite3DropTriggerPtr(pParse, pTrigger);
105551 
105552 drop_trigger_cleanup:
105553   sqlite3SrcListDelete(db, pName);
105554 }
105555 
105556 /*
105557 ** Return a pointer to the Table structure for the table that a trigger
105558 ** is set on.
105559 */
105560 static Table *tableOfTrigger(Trigger *pTrigger){
105561   int n = sqlite3Strlen30(pTrigger->table);
105562   return sqlite3HashFind(&pTrigger->pTabSchema->tblHash, pTrigger->table, n);
105563 }
105564 
105565 
105566 /*
105567 ** Drop a trigger given a pointer to that trigger.
105568 */
105569 SQLITE_PRIVATE void sqlite3DropTriggerPtr(Parse *pParse, Trigger *pTrigger){
105570   Table   *pTable;
105571   Vdbe *v;
105572   sqlite3 *db = pParse->db;
105573   int iDb;
105574 
105575   iDb = sqlite3SchemaToIndex(pParse->db, pTrigger->pSchema);
105576   assert( iDb>=0 && iDb<db->nDb );
105577   pTable = tableOfTrigger(pTrigger);
105578   assert( pTable );
105579   assert( pTable->pSchema==pTrigger->pSchema || iDb==1 );
105580 #ifndef SQLITE_OMIT_AUTHORIZATION
105581   {
105582     int code = SQLITE_DROP_TRIGGER;
105583     const char *zDb = db->aDb[iDb].zName;
105584     const char *zTab = SCHEMA_TABLE(iDb);
105585     if( iDb==1 ) code = SQLITE_DROP_TEMP_TRIGGER;
105586     if( sqlite3AuthCheck(pParse, code, pTrigger->zName, pTable->zName, zDb) ||
105587       sqlite3AuthCheck(pParse, SQLITE_DELETE, zTab, 0, zDb) ){
105588       return;
105589     }
105590   }
105591 #endif
105592 
105593   /* Generate code to destroy the database record of the trigger.
105594   */
105595   assert( pTable!=0 );
105596   if( (v = sqlite3GetVdbe(pParse))!=0 ){
105597     int base;
105598     static const VdbeOpList dropTrigger[] = {
105599       { OP_Rewind,     0, ADDR(9),  0},
105600       { OP_String8,    0, 1,        0}, /* 1 */
105601       { OP_Column,     0, 1,        2},
105602       { OP_Ne,         2, ADDR(8),  1},
105603       { OP_String8,    0, 1,        0}, /* 4: "trigger" */
105604       { OP_Column,     0, 0,        2},
105605       { OP_Ne,         2, ADDR(8),  1},
105606       { OP_Delete,     0, 0,        0},
105607       { OP_Next,       0, ADDR(1),  0}, /* 8 */
105608     };
105609 
105610     sqlite3BeginWriteOperation(pParse, 0, iDb);
105611     sqlite3OpenMasterTable(pParse, iDb);
105612     base = sqlite3VdbeAddOpList(v,  ArraySize(dropTrigger), dropTrigger);
105613     sqlite3VdbeChangeP4(v, base+1, pTrigger->zName, P4_TRANSIENT);
105614     sqlite3VdbeChangeP4(v, base+4, "trigger", P4_STATIC);
105615     sqlite3ChangeCookie(pParse, iDb);
105616     sqlite3VdbeAddOp2(v, OP_Close, 0, 0);
105617     sqlite3VdbeAddOp4(v, OP_DropTrigger, iDb, 0, 0, pTrigger->zName, 0);
105618     if( pParse->nMem<3 ){
105619       pParse->nMem = 3;
105620     }
105621   }
105622 }
105623 
105624 /*
105625 ** Remove a trigger from the hash tables of the sqlite* pointer.
105626 */
105627 SQLITE_PRIVATE void sqlite3UnlinkAndDeleteTrigger(sqlite3 *db, int iDb, const char *zName){
105628   Trigger *pTrigger;
105629   Hash *pHash;
105630 
105631   assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
105632   pHash = &(db->aDb[iDb].pSchema->trigHash);
105633   pTrigger = sqlite3HashInsert(pHash, zName, sqlite3Strlen30(zName), 0);
105634   if( ALWAYS(pTrigger) ){
105635     if( pTrigger->pSchema==pTrigger->pTabSchema ){
105636       Table *pTab = tableOfTrigger(pTrigger);
105637       Trigger **pp;
105638       for(pp=&pTab->pTrigger; *pp!=pTrigger; pp=&((*pp)->pNext));
105639       *pp = (*pp)->pNext;
105640     }
105641     sqlite3DeleteTrigger(db, pTrigger);
105642     db->flags |= SQLITE_InternChanges;
105643   }
105644 }
105645 
105646 /*
105647 ** pEList is the SET clause of an UPDATE statement.  Each entry
105648 ** in pEList is of the format <id>=<expr>.  If any of the entries
105649 ** in pEList have an <id> which matches an identifier in pIdList,
105650 ** then return TRUE.  If pIdList==NULL, then it is considered a
105651 ** wildcard that matches anything.  Likewise if pEList==NULL then
105652 ** it matches anything so always return true.  Return false only
105653 ** if there is no match.
105654 */
105655 static int checkColumnOverlap(IdList *pIdList, ExprList *pEList){
105656   int e;
105657   if( pIdList==0 || NEVER(pEList==0) ) return 1;
105658   for(e=0; e<pEList->nExpr; e++){
105659     if( sqlite3IdListIndex(pIdList, pEList->a[e].zName)>=0 ) return 1;
105660   }
105661   return 0;
105662 }
105663 
105664 /*
105665 ** Return a list of all triggers on table pTab if there exists at least
105666 ** one trigger that must be fired when an operation of type 'op' is
105667 ** performed on the table, and, if that operation is an UPDATE, if at
105668 ** least one of the columns in pChanges is being modified.
105669 */
105670 SQLITE_PRIVATE Trigger *sqlite3TriggersExist(
105671   Parse *pParse,          /* Parse context */
105672   Table *pTab,            /* The table the contains the triggers */
105673   int op,                 /* one of TK_DELETE, TK_INSERT, TK_UPDATE */
105674   ExprList *pChanges,     /* Columns that change in an UPDATE statement */
105675   int *pMask              /* OUT: Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
105676 ){
105677   int mask = 0;
105678   Trigger *pList = 0;
105679   Trigger *p;
105680 
105681   if( (pParse->db->flags & SQLITE_EnableTrigger)!=0 ){
105682     pList = sqlite3TriggerList(pParse, pTab);
105683   }
105684   assert( pList==0 || IsVirtual(pTab)==0 );
105685   for(p=pList; p; p=p->pNext){
105686     if( p->op==op && checkColumnOverlap(p->pColumns, pChanges) ){
105687       mask |= p->tr_tm;
105688     }
105689   }
105690   if( pMask ){
105691     *pMask = mask;
105692   }
105693   return (mask ? pList : 0);
105694 }
105695 
105696 /*
105697 ** Convert the pStep->target token into a SrcList and return a pointer
105698 ** to that SrcList.
105699 **
105700 ** This routine adds a specific database name, if needed, to the target when
105701 ** forming the SrcList.  This prevents a trigger in one database from
105702 ** referring to a target in another database.  An exception is when the
105703 ** trigger is in TEMP in which case it can refer to any other database it
105704 ** wants.
105705 */
105706 static SrcList *targetSrcList(
105707   Parse *pParse,       /* The parsing context */
105708   TriggerStep *pStep   /* The trigger containing the target token */
105709 ){
105710   int iDb;             /* Index of the database to use */
105711   SrcList *pSrc;       /* SrcList to be returned */
105712 
105713   pSrc = sqlite3SrcListAppend(pParse->db, 0, &pStep->target, 0);
105714   if( pSrc ){
105715     assert( pSrc->nSrc>0 );
105716     assert( pSrc->a!=0 );
105717     iDb = sqlite3SchemaToIndex(pParse->db, pStep->pTrig->pSchema);
105718     if( iDb==0 || iDb>=2 ){
105719       sqlite3 *db = pParse->db;
105720       assert( iDb<pParse->db->nDb );
105721       pSrc->a[pSrc->nSrc-1].zDatabase = sqlite3DbStrDup(db, db->aDb[iDb].zName);
105722     }
105723   }
105724   return pSrc;
105725 }
105726 
105727 /*
105728 ** Generate VDBE code for the statements inside the body of a single
105729 ** trigger.
105730 */
105731 static int codeTriggerProgram(
105732   Parse *pParse,            /* The parser context */
105733   TriggerStep *pStepList,   /* List of statements inside the trigger body */
105734   int orconf                /* Conflict algorithm. (OE_Abort, etc) */
105735 ){
105736   TriggerStep *pStep;
105737   Vdbe *v = pParse->pVdbe;
105738   sqlite3 *db = pParse->db;
105739 
105740   assert( pParse->pTriggerTab && pParse->pToplevel );
105741   assert( pStepList );
105742   assert( v!=0 );
105743   for(pStep=pStepList; pStep; pStep=pStep->pNext){
105744     /* Figure out the ON CONFLICT policy that will be used for this step
105745     ** of the trigger program. If the statement that caused this trigger
105746     ** to fire had an explicit ON CONFLICT, then use it. Otherwise, use
105747     ** the ON CONFLICT policy that was specified as part of the trigger
105748     ** step statement. Example:
105749     **
105750     **   CREATE TRIGGER AFTER INSERT ON t1 BEGIN;
105751     **     INSERT OR REPLACE INTO t2 VALUES(new.a, new.b);
105752     **   END;
105753     **
105754     **   INSERT INTO t1 ... ;            -- insert into t2 uses REPLACE policy
105755     **   INSERT OR IGNORE INTO t1 ... ;  -- insert into t2 uses IGNORE policy
105756     */
105757     pParse->eOrconf = (orconf==OE_Default)?pStep->orconf:(u8)orconf;
105758 
105759     /* Clear the cookieGoto flag. When coding triggers, the cookieGoto
105760     ** variable is used as a flag to indicate to sqlite3ExprCodeConstants()
105761     ** that it is not safe to refactor constants (this happens after the
105762     ** start of the first loop in the SQL statement is coded - at that
105763     ** point code may be conditionally executed, so it is no longer safe to
105764     ** initialize constant register values).  */
105765     assert( pParse->cookieGoto==0 || pParse->cookieGoto==-1 );
105766     pParse->cookieGoto = 0;
105767 
105768     switch( pStep->op ){
105769       case TK_UPDATE: {
105770         sqlite3Update(pParse,
105771           targetSrcList(pParse, pStep),
105772           sqlite3ExprListDup(db, pStep->pExprList, 0),
105773           sqlite3ExprDup(db, pStep->pWhere, 0),
105774           pParse->eOrconf
105775         );
105776         break;
105777       }
105778       case TK_INSERT: {
105779         sqlite3Insert(pParse,
105780           targetSrcList(pParse, pStep),
105781           sqlite3SelectDup(db, pStep->pSelect, 0),
105782           sqlite3IdListDup(db, pStep->pIdList),
105783           pParse->eOrconf
105784         );
105785         break;
105786       }
105787       case TK_DELETE: {
105788         sqlite3DeleteFrom(pParse,
105789           targetSrcList(pParse, pStep),
105790           sqlite3ExprDup(db, pStep->pWhere, 0)
105791         );
105792         break;
105793       }
105794       default: assert( pStep->op==TK_SELECT ); {
105795         SelectDest sDest;
105796         Select *pSelect = sqlite3SelectDup(db, pStep->pSelect, 0);
105797         sqlite3SelectDestInit(&sDest, SRT_Discard, 0);
105798         sqlite3Select(pParse, pSelect, &sDest);
105799         sqlite3SelectDelete(db, pSelect);
105800         break;
105801       }
105802     }
105803     if( pStep->op!=TK_SELECT ){
105804       sqlite3VdbeAddOp0(v, OP_ResetCount);
105805     }
105806   }
105807 
105808   return 0;
105809 }
105810 
105811 #ifdef SQLITE_ENABLE_EXPLAIN_COMMENTS
105812 /*
105813 ** This function is used to add VdbeComment() annotations to a VDBE
105814 ** program. It is not used in production code, only for debugging.
105815 */
105816 static const char *onErrorText(int onError){
105817   switch( onError ){
105818     case OE_Abort:    return "abort";
105819     case OE_Rollback: return "rollback";
105820     case OE_Fail:     return "fail";
105821     case OE_Replace:  return "replace";
105822     case OE_Ignore:   return "ignore";
105823     case OE_Default:  return "default";
105824   }
105825   return "n/a";
105826 }
105827 #endif
105828 
105829 /*
105830 ** Parse context structure pFrom has just been used to create a sub-vdbe
105831 ** (trigger program). If an error has occurred, transfer error information
105832 ** from pFrom to pTo.
105833 */
105834 static void transferParseError(Parse *pTo, Parse *pFrom){
105835   assert( pFrom->zErrMsg==0 || pFrom->nErr );
105836   assert( pTo->zErrMsg==0 || pTo->nErr );
105837   if( pTo->nErr==0 ){
105838     pTo->zErrMsg = pFrom->zErrMsg;
105839     pTo->nErr = pFrom->nErr;
105840   }else{
105841     sqlite3DbFree(pFrom->db, pFrom->zErrMsg);
105842   }
105843 }
105844 
105845 /*
105846 ** Create and populate a new TriggerPrg object with a sub-program
105847 ** implementing trigger pTrigger with ON CONFLICT policy orconf.
105848 */
105849 static TriggerPrg *codeRowTrigger(
105850   Parse *pParse,       /* Current parse context */
105851   Trigger *pTrigger,   /* Trigger to code */
105852   Table *pTab,         /* The table pTrigger is attached to */
105853   int orconf           /* ON CONFLICT policy to code trigger program with */
105854 ){
105855   Parse *pTop = sqlite3ParseToplevel(pParse);
105856   sqlite3 *db = pParse->db;   /* Database handle */
105857   TriggerPrg *pPrg;           /* Value to return */
105858   Expr *pWhen = 0;            /* Duplicate of trigger WHEN expression */
105859   Vdbe *v;                    /* Temporary VM */
105860   NameContext sNC;            /* Name context for sub-vdbe */
105861   SubProgram *pProgram = 0;   /* Sub-vdbe for trigger program */
105862   Parse *pSubParse;           /* Parse context for sub-vdbe */
105863   int iEndTrigger = 0;        /* Label to jump to if WHEN is false */
105864 
105865   assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) );
105866   assert( pTop->pVdbe );
105867 
105868   /* Allocate the TriggerPrg and SubProgram objects. To ensure that they
105869   ** are freed if an error occurs, link them into the Parse.pTriggerPrg
105870   ** list of the top-level Parse object sooner rather than later.  */
105871   pPrg = sqlite3DbMallocZero(db, sizeof(TriggerPrg));
105872   if( !pPrg ) return 0;
105873   pPrg->pNext = pTop->pTriggerPrg;
105874   pTop->pTriggerPrg = pPrg;
105875   pPrg->pProgram = pProgram = sqlite3DbMallocZero(db, sizeof(SubProgram));
105876   if( !pProgram ) return 0;
105877   sqlite3VdbeLinkSubProgram(pTop->pVdbe, pProgram);
105878   pPrg->pTrigger = pTrigger;
105879   pPrg->orconf = orconf;
105880   pPrg->aColmask[0] = 0xffffffff;
105881   pPrg->aColmask[1] = 0xffffffff;
105882 
105883   /* Allocate and populate a new Parse context to use for coding the
105884   ** trigger sub-program.  */
105885   pSubParse = sqlite3StackAllocZero(db, sizeof(Parse));
105886   if( !pSubParse ) return 0;
105887   memset(&sNC, 0, sizeof(sNC));
105888   sNC.pParse = pSubParse;
105889   pSubParse->db = db;
105890   pSubParse->pTriggerTab = pTab;
105891   pSubParse->pToplevel = pTop;
105892   pSubParse->zAuthContext = pTrigger->zName;
105893   pSubParse->eTriggerOp = pTrigger->op;
105894   pSubParse->nQueryLoop = pParse->nQueryLoop;
105895 
105896   v = sqlite3GetVdbe(pSubParse);
105897   if( v ){
105898     VdbeComment((v, "Start: %s.%s (%s %s%s%s ON %s)",
105899       pTrigger->zName, onErrorText(orconf),
105900       (pTrigger->tr_tm==TRIGGER_BEFORE ? "BEFORE" : "AFTER"),
105901         (pTrigger->op==TK_UPDATE ? "UPDATE" : ""),
105902         (pTrigger->op==TK_INSERT ? "INSERT" : ""),
105903         (pTrigger->op==TK_DELETE ? "DELETE" : ""),
105904       pTab->zName
105905     ));
105906 #ifndef SQLITE_OMIT_TRACE
105907     sqlite3VdbeChangeP4(v, -1,
105908       sqlite3MPrintf(db, "-- TRIGGER %s", pTrigger->zName), P4_DYNAMIC
105909     );
105910 #endif
105911 
105912     /* If one was specified, code the WHEN clause. If it evaluates to false
105913     ** (or NULL) the sub-vdbe is immediately halted by jumping to the
105914     ** OP_Halt inserted at the end of the program.  */
105915     if( pTrigger->pWhen ){
105916       pWhen = sqlite3ExprDup(db, pTrigger->pWhen, 0);
105917       if( SQLITE_OK==sqlite3ResolveExprNames(&sNC, pWhen)
105918        && db->mallocFailed==0
105919       ){
105920         iEndTrigger = sqlite3VdbeMakeLabel(v);
105921         sqlite3ExprIfFalse(pSubParse, pWhen, iEndTrigger, SQLITE_JUMPIFNULL);
105922       }
105923       sqlite3ExprDelete(db, pWhen);
105924     }
105925 
105926     /* Code the trigger program into the sub-vdbe. */
105927     codeTriggerProgram(pSubParse, pTrigger->step_list, orconf);
105928 
105929     /* Insert an OP_Halt at the end of the sub-program. */
105930     if( iEndTrigger ){
105931       sqlite3VdbeResolveLabel(v, iEndTrigger);
105932     }
105933     sqlite3VdbeAddOp0(v, OP_Halt);
105934     VdbeComment((v, "End: %s.%s", pTrigger->zName, onErrorText(orconf)));
105935 
105936     transferParseError(pParse, pSubParse);
105937     if( db->mallocFailed==0 ){
105938       pProgram->aOp = sqlite3VdbeTakeOpArray(v, &pProgram->nOp, &pTop->nMaxArg);
105939     }
105940     pProgram->nMem = pSubParse->nMem;
105941     pProgram->nCsr = pSubParse->nTab;
105942     pProgram->nOnce = pSubParse->nOnce;
105943     pProgram->token = (void *)pTrigger;
105944     pPrg->aColmask[0] = pSubParse->oldmask;
105945     pPrg->aColmask[1] = pSubParse->newmask;
105946     sqlite3VdbeDelete(v);
105947   }
105948 
105949   assert( !pSubParse->pAinc       && !pSubParse->pZombieTab );
105950   assert( !pSubParse->pTriggerPrg && !pSubParse->nMaxArg );
105951   sqlite3ParserReset(pSubParse);
105952   sqlite3StackFree(db, pSubParse);
105953 
105954   return pPrg;
105955 }
105956 
105957 /*
105958 ** Return a pointer to a TriggerPrg object containing the sub-program for
105959 ** trigger pTrigger with default ON CONFLICT algorithm orconf. If no such
105960 ** TriggerPrg object exists, a new object is allocated and populated before
105961 ** being returned.
105962 */
105963 static TriggerPrg *getRowTrigger(
105964   Parse *pParse,       /* Current parse context */
105965   Trigger *pTrigger,   /* Trigger to code */
105966   Table *pTab,         /* The table trigger pTrigger is attached to */
105967   int orconf           /* ON CONFLICT algorithm. */
105968 ){
105969   Parse *pRoot = sqlite3ParseToplevel(pParse);
105970   TriggerPrg *pPrg;
105971 
105972   assert( pTrigger->zName==0 || pTab==tableOfTrigger(pTrigger) );
105973 
105974   /* It may be that this trigger has already been coded (or is in the
105975   ** process of being coded). If this is the case, then an entry with
105976   ** a matching TriggerPrg.pTrigger field will be present somewhere
105977   ** in the Parse.pTriggerPrg list. Search for such an entry.  */
105978   for(pPrg=pRoot->pTriggerPrg;
105979       pPrg && (pPrg->pTrigger!=pTrigger || pPrg->orconf!=orconf);
105980       pPrg=pPrg->pNext
105981   );
105982 
105983   /* If an existing TriggerPrg could not be located, create a new one. */
105984   if( !pPrg ){
105985     pPrg = codeRowTrigger(pParse, pTrigger, pTab, orconf);
105986   }
105987 
105988   return pPrg;
105989 }
105990 
105991 /*
105992 ** Generate code for the trigger program associated with trigger p on
105993 ** table pTab. The reg, orconf and ignoreJump parameters passed to this
105994 ** function are the same as those described in the header function for
105995 ** sqlite3CodeRowTrigger()
105996 */
105997 SQLITE_PRIVATE void sqlite3CodeRowTriggerDirect(
105998   Parse *pParse,       /* Parse context */
105999   Trigger *p,          /* Trigger to code */
106000   Table *pTab,         /* The table to code triggers from */
106001   int reg,             /* Reg array containing OLD.* and NEW.* values */
106002   int orconf,          /* ON CONFLICT policy */
106003   int ignoreJump       /* Instruction to jump to for RAISE(IGNORE) */
106004 ){
106005   Vdbe *v = sqlite3GetVdbe(pParse); /* Main VM */
106006   TriggerPrg *pPrg;
106007   pPrg = getRowTrigger(pParse, p, pTab, orconf);
106008   assert( pPrg || pParse->nErr || pParse->db->mallocFailed );
106009 
106010   /* Code the OP_Program opcode in the parent VDBE. P4 of the OP_Program
106011   ** is a pointer to the sub-vdbe containing the trigger program.  */
106012   if( pPrg ){
106013     int bRecursive = (p->zName && 0==(pParse->db->flags&SQLITE_RecTriggers));
106014 
106015     sqlite3VdbeAddOp3(v, OP_Program, reg, ignoreJump, ++pParse->nMem);
106016     sqlite3VdbeChangeP4(v, -1, (const char *)pPrg->pProgram, P4_SUBPROGRAM);
106017     VdbeComment(
106018         (v, "Call: %s.%s", (p->zName?p->zName:"fkey"), onErrorText(orconf)));
106019 
106020     /* Set the P5 operand of the OP_Program instruction to non-zero if
106021     ** recursive invocation of this trigger program is disallowed. Recursive
106022     ** invocation is disallowed if (a) the sub-program is really a trigger,
106023     ** not a foreign key action, and (b) the flag to enable recursive triggers
106024     ** is clear.  */
106025     sqlite3VdbeChangeP5(v, (u8)bRecursive);
106026   }
106027 }
106028 
106029 /*
106030 ** This is called to code the required FOR EACH ROW triggers for an operation
106031 ** on table pTab. The operation to code triggers for (INSERT, UPDATE or DELETE)
106032 ** is given by the op parameter. The tr_tm parameter determines whether the
106033 ** BEFORE or AFTER triggers are coded. If the operation is an UPDATE, then
106034 ** parameter pChanges is passed the list of columns being modified.
106035 **
106036 ** If there are no triggers that fire at the specified time for the specified
106037 ** operation on pTab, this function is a no-op.
106038 **
106039 ** The reg argument is the address of the first in an array of registers
106040 ** that contain the values substituted for the new.* and old.* references
106041 ** in the trigger program. If N is the number of columns in table pTab
106042 ** (a copy of pTab->nCol), then registers are populated as follows:
106043 **
106044 **   Register       Contains
106045 **   ------------------------------------------------------
106046 **   reg+0          OLD.rowid
106047 **   reg+1          OLD.* value of left-most column of pTab
106048 **   ...            ...
106049 **   reg+N          OLD.* value of right-most column of pTab
106050 **   reg+N+1        NEW.rowid
106051 **   reg+N+2        OLD.* value of left-most column of pTab
106052 **   ...            ...
106053 **   reg+N+N+1      NEW.* value of right-most column of pTab
106054 **
106055 ** For ON DELETE triggers, the registers containing the NEW.* values will
106056 ** never be accessed by the trigger program, so they are not allocated or
106057 ** populated by the caller (there is no data to populate them with anyway).
106058 ** Similarly, for ON INSERT triggers the values stored in the OLD.* registers
106059 ** are never accessed, and so are not allocated by the caller. So, for an
106060 ** ON INSERT trigger, the value passed to this function as parameter reg
106061 ** is not a readable register, although registers (reg+N) through
106062 ** (reg+N+N+1) are.
106063 **
106064 ** Parameter orconf is the default conflict resolution algorithm for the
106065 ** trigger program to use (REPLACE, IGNORE etc.). Parameter ignoreJump
106066 ** is the instruction that control should jump to if a trigger program
106067 ** raises an IGNORE exception.
106068 */
106069 SQLITE_PRIVATE void sqlite3CodeRowTrigger(
106070   Parse *pParse,       /* Parse context */
106071   Trigger *pTrigger,   /* List of triggers on table pTab */
106072   int op,              /* One of TK_UPDATE, TK_INSERT, TK_DELETE */
106073   ExprList *pChanges,  /* Changes list for any UPDATE OF triggers */
106074   int tr_tm,           /* One of TRIGGER_BEFORE, TRIGGER_AFTER */
106075   Table *pTab,         /* The table to code triggers from */
106076   int reg,             /* The first in an array of registers (see above) */
106077   int orconf,          /* ON CONFLICT policy */
106078   int ignoreJump       /* Instruction to jump to for RAISE(IGNORE) */
106079 ){
106080   Trigger *p;          /* Used to iterate through pTrigger list */
106081 
106082   assert( op==TK_UPDATE || op==TK_INSERT || op==TK_DELETE );
106083   assert( tr_tm==TRIGGER_BEFORE || tr_tm==TRIGGER_AFTER );
106084   assert( (op==TK_UPDATE)==(pChanges!=0) );
106085 
106086   for(p=pTrigger; p; p=p->pNext){
106087 
106088     /* Sanity checking:  The schema for the trigger and for the table are
106089     ** always defined.  The trigger must be in the same schema as the table
106090     ** or else it must be a TEMP trigger. */
106091     assert( p->pSchema!=0 );
106092     assert( p->pTabSchema!=0 );
106093     assert( p->pSchema==p->pTabSchema
106094          || p->pSchema==pParse->db->aDb[1].pSchema );
106095 
106096     /* Determine whether we should code this trigger */
106097     if( p->op==op
106098      && p->tr_tm==tr_tm
106099      && checkColumnOverlap(p->pColumns, pChanges)
106100     ){
106101       sqlite3CodeRowTriggerDirect(pParse, p, pTab, reg, orconf, ignoreJump);
106102     }
106103   }
106104 }
106105 
106106 /*
106107 ** Triggers may access values stored in the old.* or new.* pseudo-table.
106108 ** This function returns a 32-bit bitmask indicating which columns of the
106109 ** old.* or new.* tables actually are used by triggers. This information
106110 ** may be used by the caller, for example, to avoid having to load the entire
106111 ** old.* record into memory when executing an UPDATE or DELETE command.
106112 **
106113 ** Bit 0 of the returned mask is set if the left-most column of the
106114 ** table may be accessed using an [old|new].<col> reference. Bit 1 is set if
106115 ** the second leftmost column value is required, and so on. If there
106116 ** are more than 32 columns in the table, and at least one of the columns
106117 ** with an index greater than 32 may be accessed, 0xffffffff is returned.
106118 **
106119 ** It is not possible to determine if the old.rowid or new.rowid column is
106120 ** accessed by triggers. The caller must always assume that it is.
106121 **
106122 ** Parameter isNew must be either 1 or 0. If it is 0, then the mask returned
106123 ** applies to the old.* table. If 1, the new.* table.
106124 **
106125 ** Parameter tr_tm must be a mask with one or both of the TRIGGER_BEFORE
106126 ** and TRIGGER_AFTER bits set. Values accessed by BEFORE triggers are only
106127 ** included in the returned mask if the TRIGGER_BEFORE bit is set in the
106128 ** tr_tm parameter. Similarly, values accessed by AFTER triggers are only
106129 ** included in the returned mask if the TRIGGER_AFTER bit is set in tr_tm.
106130 */
106131 SQLITE_PRIVATE u32 sqlite3TriggerColmask(
106132   Parse *pParse,       /* Parse context */
106133   Trigger *pTrigger,   /* List of triggers on table pTab */
106134   ExprList *pChanges,  /* Changes list for any UPDATE OF triggers */
106135   int isNew,           /* 1 for new.* ref mask, 0 for old.* ref mask */
106136   int tr_tm,           /* Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
106137   Table *pTab,         /* The table to code triggers from */
106138   int orconf           /* Default ON CONFLICT policy for trigger steps */
106139 ){
106140   const int op = pChanges ? TK_UPDATE : TK_DELETE;
106141   u32 mask = 0;
106142   Trigger *p;
106143 
106144   assert( isNew==1 || isNew==0 );
106145   for(p=pTrigger; p; p=p->pNext){
106146     if( p->op==op && (tr_tm&p->tr_tm)
106147      && checkColumnOverlap(p->pColumns,pChanges)
106148     ){
106149       TriggerPrg *pPrg;
106150       pPrg = getRowTrigger(pParse, p, pTab, orconf);
106151       if( pPrg ){
106152         mask |= pPrg->aColmask[isNew];
106153       }
106154     }
106155   }
106156 
106157   return mask;
106158 }
106159 
106160 #endif /* !defined(SQLITE_OMIT_TRIGGER) */
106161 
106162 /************** End of trigger.c *********************************************/
106163 /************** Begin file update.c ******************************************/
106164 /*
106165 ** 2001 September 15
106166 **
106167 ** The author disclaims copyright to this source code.  In place of
106168 ** a legal notice, here is a blessing:
106169 **
106170 **    May you do good and not evil.
106171 **    May you find forgiveness for yourself and forgive others.
106172 **    May you share freely, never taking more than you give.
106173 **
106174 *************************************************************************
106175 ** This file contains C code routines that are called by the parser
106176 ** to handle UPDATE statements.
106177 */
106178 
106179 #ifndef SQLITE_OMIT_VIRTUALTABLE
106180 /* Forward declaration */
106181 static void updateVirtualTable(
106182   Parse *pParse,       /* The parsing context */
106183   SrcList *pSrc,       /* The virtual table to be modified */
106184   Table *pTab,         /* The virtual table */
106185   ExprList *pChanges,  /* The columns to change in the UPDATE statement */
106186   Expr *pRowidExpr,    /* Expression used to recompute the rowid */
106187   int *aXRef,          /* Mapping from columns of pTab to entries in pChanges */
106188   Expr *pWhere,        /* WHERE clause of the UPDATE statement */
106189   int onError          /* ON CONFLICT strategy */
106190 );
106191 #endif /* SQLITE_OMIT_VIRTUALTABLE */
106192 
106193 /*
106194 ** The most recently coded instruction was an OP_Column to retrieve the
106195 ** i-th column of table pTab. This routine sets the P4 parameter of the
106196 ** OP_Column to the default value, if any.
106197 **
106198 ** The default value of a column is specified by a DEFAULT clause in the
106199 ** column definition. This was either supplied by the user when the table
106200 ** was created, or added later to the table definition by an ALTER TABLE
106201 ** command. If the latter, then the row-records in the table btree on disk
106202 ** may not contain a value for the column and the default value, taken
106203 ** from the P4 parameter of the OP_Column instruction, is returned instead.
106204 ** If the former, then all row-records are guaranteed to include a value
106205 ** for the column and the P4 value is not required.
106206 **
106207 ** Column definitions created by an ALTER TABLE command may only have
106208 ** literal default values specified: a number, null or a string. (If a more
106209 ** complicated default expression value was provided, it is evaluated
106210 ** when the ALTER TABLE is executed and one of the literal values written
106211 ** into the sqlite_master table.)
106212 **
106213 ** Therefore, the P4 parameter is only required if the default value for
106214 ** the column is a literal number, string or null. The sqlite3ValueFromExpr()
106215 ** function is capable of transforming these types of expressions into
106216 ** sqlite3_value objects.
106217 **
106218 ** If parameter iReg is not negative, code an OP_RealAffinity instruction
106219 ** on register iReg. This is used when an equivalent integer value is
106220 ** stored in place of an 8-byte floating point value in order to save
106221 ** space.
106222 */
106223 SQLITE_PRIVATE void sqlite3ColumnDefault(Vdbe *v, Table *pTab, int i, int iReg){
106224   assert( pTab!=0 );
106225   if( !pTab->pSelect ){
106226     sqlite3_value *pValue = 0;
106227     u8 enc = ENC(sqlite3VdbeDb(v));
106228     Column *pCol = &pTab->aCol[i];
106229     VdbeComment((v, "%s.%s", pTab->zName, pCol->zName));
106230     assert( i<pTab->nCol );
106231     sqlite3ValueFromExpr(sqlite3VdbeDb(v), pCol->pDflt, enc,
106232                          pCol->affinity, &pValue);
106233     if( pValue ){
106234       sqlite3VdbeChangeP4(v, -1, (const char *)pValue, P4_MEM);
106235     }
106236 #ifndef SQLITE_OMIT_FLOATING_POINT
106237     if( pTab->aCol[i].affinity==SQLITE_AFF_REAL ){
106238       sqlite3VdbeAddOp1(v, OP_RealAffinity, iReg);
106239     }
106240 #endif
106241   }
106242 }
106243 
106244 /*
106245 ** Process an UPDATE statement.
106246 **
106247 **   UPDATE OR IGNORE table_wxyz SET a=b, c=d WHERE e<5 AND f NOT NULL;
106248 **          \_______/ \________/     \______/       \________________/
106249 *            onError   pTabList      pChanges             pWhere
106250 */
106251 SQLITE_PRIVATE void sqlite3Update(
106252   Parse *pParse,         /* The parser context */
106253   SrcList *pTabList,     /* The table in which we should change things */
106254   ExprList *pChanges,    /* Things to be changed */
106255   Expr *pWhere,          /* The WHERE clause.  May be null */
106256   int onError            /* How to handle constraint errors */
106257 ){
106258   int i, j;              /* Loop counters */
106259   Table *pTab;           /* The table to be updated */
106260   int addrTop = 0;       /* VDBE instruction address of the start of the loop */
106261   WhereInfo *pWInfo;     /* Information about the WHERE clause */
106262   Vdbe *v;               /* The virtual database engine */
106263   Index *pIdx;           /* For looping over indices */
106264   Index *pPk;            /* The PRIMARY KEY index for WITHOUT ROWID tables */
106265   int nIdx;              /* Number of indices that need updating */
106266   int iBaseCur;          /* Base cursor number */
106267   int iDataCur;          /* Cursor for the canonical data btree */
106268   int iIdxCur;           /* Cursor for the first index */
106269   sqlite3 *db;           /* The database structure */
106270   int *aRegIdx = 0;      /* One register assigned to each index to be updated */
106271   int *aXRef = 0;        /* aXRef[i] is the index in pChanges->a[] of the
106272                          ** an expression for the i-th column of the table.
106273                          ** aXRef[i]==-1 if the i-th column is not changed. */
106274   u8 *aToOpen;           /* 1 for tables and indices to be opened */
106275   u8 chngPk;             /* PRIMARY KEY changed in a WITHOUT ROWID table */
106276   u8 chngRowid;          /* Rowid changed in a normal table */
106277   u8 chngKey;            /* Either chngPk or chngRowid */
106278   Expr *pRowidExpr = 0;  /* Expression defining the new record number */
106279   AuthContext sContext;  /* The authorization context */
106280   NameContext sNC;       /* The name-context to resolve expressions in */
106281   int iDb;               /* Database containing the table being updated */
106282   int okOnePass;         /* True for one-pass algorithm without the FIFO */
106283   int hasFK;             /* True if foreign key processing is required */
106284   int labelBreak;        /* Jump here to break out of UPDATE loop */
106285   int labelContinue;     /* Jump here to continue next step of UPDATE loop */
106286 
106287 #ifndef SQLITE_OMIT_TRIGGER
106288   int isView;            /* True when updating a view (INSTEAD OF trigger) */
106289   Trigger *pTrigger;     /* List of triggers on pTab, if required */
106290   int tmask;             /* Mask of TRIGGER_BEFORE|TRIGGER_AFTER */
106291 #endif
106292   int newmask;           /* Mask of NEW.* columns accessed by BEFORE triggers */
106293   int iEph = 0;          /* Ephemeral table holding all primary key values */
106294   int nKey = 0;          /* Number of elements in regKey for WITHOUT ROWID */
106295   int aiCurOnePass[2];   /* The write cursors opened by WHERE_ONEPASS */
106296 
106297   /* Register Allocations */
106298   int regRowCount = 0;   /* A count of rows changed */
106299   int regOldRowid;       /* The old rowid */
106300   int regNewRowid;       /* The new rowid */
106301   int regNew;            /* Content of the NEW.* table in triggers */
106302   int regOld = 0;        /* Content of OLD.* table in triggers */
106303   int regRowSet = 0;     /* Rowset of rows to be updated */
106304   int regKey = 0;        /* composite PRIMARY KEY value */
106305 
106306   memset(&sContext, 0, sizeof(sContext));
106307   db = pParse->db;
106308   if( pParse->nErr || db->mallocFailed ){
106309     goto update_cleanup;
106310   }
106311   assert( pTabList->nSrc==1 );
106312 
106313   /* Locate the table which we want to update.
106314   */
106315   pTab = sqlite3SrcListLookup(pParse, pTabList);
106316   if( pTab==0 ) goto update_cleanup;
106317   iDb = sqlite3SchemaToIndex(pParse->db, pTab->pSchema);
106318 
106319   /* Figure out if we have any triggers and if the table being
106320   ** updated is a view.
106321   */
106322 #ifndef SQLITE_OMIT_TRIGGER
106323   pTrigger = sqlite3TriggersExist(pParse, pTab, TK_UPDATE, pChanges, &tmask);
106324   isView = pTab->pSelect!=0;
106325   assert( pTrigger || tmask==0 );
106326 #else
106327 # define pTrigger 0
106328 # define isView 0
106329 # define tmask 0
106330 #endif
106331 #ifdef SQLITE_OMIT_VIEW
106332 # undef isView
106333 # define isView 0
106334 #endif
106335 
106336   if( sqlite3ViewGetColumnNames(pParse, pTab) ){
106337     goto update_cleanup;
106338   }
106339   if( sqlite3IsReadOnly(pParse, pTab, tmask) ){
106340     goto update_cleanup;
106341   }
106342 
106343   /* Allocate a cursors for the main database table and for all indices.
106344   ** The index cursors might not be used, but if they are used they
106345   ** need to occur right after the database cursor.  So go ahead and
106346   ** allocate enough space, just in case.
106347   */
106348   pTabList->a[0].iCursor = iBaseCur = iDataCur = pParse->nTab++;
106349   iIdxCur = iDataCur+1;
106350   pPk = HasRowid(pTab) ? 0 : sqlite3PrimaryKeyIndex(pTab);
106351   for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){
106352     if( pIdx->autoIndex==2 && pPk!=0 ){
106353       iDataCur = pParse->nTab;
106354       pTabList->a[0].iCursor = iDataCur;
106355     }
106356     pParse->nTab++;
106357   }
106358 
106359   /* Allocate space for aXRef[], aRegIdx[], and aToOpen[].
106360   ** Initialize aXRef[] and aToOpen[] to their default values.
106361   */
106362   aXRef = sqlite3DbMallocRaw(db, sizeof(int) * (pTab->nCol+nIdx) + nIdx+2 );
106363   if( aXRef==0 ) goto update_cleanup;
106364   aRegIdx = aXRef+pTab->nCol;
106365   aToOpen = (u8*)(aRegIdx+nIdx);
106366   memset(aToOpen, 1, nIdx+1);
106367   aToOpen[nIdx+1] = 0;
106368   for(i=0; i<pTab->nCol; i++) aXRef[i] = -1;
106369 
106370   /* Initialize the name-context */
106371   memset(&sNC, 0, sizeof(sNC));
106372   sNC.pParse = pParse;
106373   sNC.pSrcList = pTabList;
106374 
106375   /* Resolve the column names in all the expressions of the
106376   ** of the UPDATE statement.  Also find the column index
106377   ** for each column to be updated in the pChanges array.  For each
106378   ** column to be updated, make sure we have authorization to change
106379   ** that column.
106380   */
106381   chngRowid = chngPk = 0;
106382   for(i=0; i<pChanges->nExpr; i++){
106383     if( sqlite3ResolveExprNames(&sNC, pChanges->a[i].pExpr) ){
106384       goto update_cleanup;
106385     }
106386     for(j=0; j<pTab->nCol; j++){
106387       if( sqlite3StrICmp(pTab->aCol[j].zName, pChanges->a[i].zName)==0 ){
106388         if( j==pTab->iPKey ){
106389           chngRowid = 1;
106390           pRowidExpr = pChanges->a[i].pExpr;
106391         }else if( pPk && (pTab->aCol[j].colFlags & COLFLAG_PRIMKEY)!=0 ){
106392           chngPk = 1;
106393         }
106394         aXRef[j] = i;
106395         break;
106396       }
106397     }
106398     if( j>=pTab->nCol ){
106399       if( pPk==0 && sqlite3IsRowid(pChanges->a[i].zName) ){
106400         j = -1;
106401         chngRowid = 1;
106402         pRowidExpr = pChanges->a[i].pExpr;
106403       }else{
106404         sqlite3ErrorMsg(pParse, "no such column: %s", pChanges->a[i].zName);
106405         pParse->checkSchema = 1;
106406         goto update_cleanup;
106407       }
106408     }
106409 #ifndef SQLITE_OMIT_AUTHORIZATION
106410     {
106411       int rc;
106412       rc = sqlite3AuthCheck(pParse, SQLITE_UPDATE, pTab->zName,
106413                             j<0 ? "ROWID" : pTab->aCol[j].zName,
106414                             db->aDb[iDb].zName);
106415       if( rc==SQLITE_DENY ){
106416         goto update_cleanup;
106417       }else if( rc==SQLITE_IGNORE ){
106418         aXRef[j] = -1;
106419       }
106420     }
106421 #endif
106422   }
106423   assert( (chngRowid & chngPk)==0 );
106424   assert( chngRowid==0 || chngRowid==1 );
106425   assert( chngPk==0 || chngPk==1 );
106426   chngKey = chngRowid + chngPk;
106427 
106428   /* The SET expressions are not actually used inside the WHERE loop.
106429   ** So reset the colUsed mask
106430   */
106431   pTabList->a[0].colUsed = 0;
106432 
106433   hasFK = sqlite3FkRequired(pParse, pTab, aXRef, chngKey);
106434 
106435   /* There is one entry in the aRegIdx[] array for each index on the table
106436   ** being updated.  Fill in aRegIdx[] with a register number that will hold
106437   ** the key for accessing each index.
106438   */
106439   for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
106440     int reg;
106441     if( chngKey || hasFK || pIdx->pPartIdxWhere || pIdx==pPk ){
106442       reg = ++pParse->nMem;
106443     }else{
106444       reg = 0;
106445       for(i=0; i<pIdx->nKeyCol; i++){
106446         if( aXRef[pIdx->aiColumn[i]]>=0 ){
106447           reg = ++pParse->nMem;
106448           break;
106449         }
106450       }
106451     }
106452     if( reg==0 ) aToOpen[j+1] = 0;
106453     aRegIdx[j] = reg;
106454   }
106455 
106456   /* Begin generating code. */
106457   v = sqlite3GetVdbe(pParse);
106458   if( v==0 ) goto update_cleanup;
106459   if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
106460   sqlite3BeginWriteOperation(pParse, 1, iDb);
106461 
106462 #ifndef SQLITE_OMIT_VIRTUALTABLE
106463   /* Virtual tables must be handled separately */
106464   if( IsVirtual(pTab) ){
106465     updateVirtualTable(pParse, pTabList, pTab, pChanges, pRowidExpr, aXRef,
106466                        pWhere, onError);
106467     pWhere = 0;
106468     pTabList = 0;
106469     goto update_cleanup;
106470   }
106471 #endif
106472 
106473   /* Allocate required registers. */
106474   regRowSet = ++pParse->nMem;
106475   regOldRowid = regNewRowid = ++pParse->nMem;
106476   if( chngPk || pTrigger || hasFK ){
106477     regOld = pParse->nMem + 1;
106478     pParse->nMem += pTab->nCol;
106479   }
106480   if( chngKey || pTrigger || hasFK ){
106481     regNewRowid = ++pParse->nMem;
106482   }
106483   regNew = pParse->nMem + 1;
106484   pParse->nMem += pTab->nCol;
106485 
106486   /* Start the view context. */
106487   if( isView ){
106488     sqlite3AuthContextPush(pParse, &sContext, pTab->zName);
106489   }
106490 
106491   /* If we are trying to update a view, realize that view into
106492   ** a ephemeral table.
106493   */
106494 #if !defined(SQLITE_OMIT_VIEW) && !defined(SQLITE_OMIT_TRIGGER)
106495   if( isView ){
106496     sqlite3MaterializeView(pParse, pTab, pWhere, iDataCur);
106497   }
106498 #endif
106499 
106500   /* Resolve the column names in all the expressions in the
106501   ** WHERE clause.
106502   */
106503   if( sqlite3ResolveExprNames(&sNC, pWhere) ){
106504     goto update_cleanup;
106505   }
106506 
106507   /* Begin the database scan
106508   */
106509   if( HasRowid(pTab) ){
106510     sqlite3VdbeAddOp3(v, OP_Null, 0, regRowSet, regOldRowid);
106511     pWInfo = sqlite3WhereBegin(
106512         pParse, pTabList, pWhere, 0, 0, WHERE_ONEPASS_DESIRED, iIdxCur
106513     );
106514     if( pWInfo==0 ) goto update_cleanup;
106515     okOnePass = sqlite3WhereOkOnePass(pWInfo, aiCurOnePass);
106516 
106517     /* Remember the rowid of every item to be updated.
106518     */
106519     sqlite3VdbeAddOp2(v, OP_Rowid, iDataCur, regOldRowid);
106520     if( !okOnePass ){
106521       sqlite3VdbeAddOp2(v, OP_RowSetAdd, regRowSet, regOldRowid);
106522     }
106523 
106524     /* End the database scan loop.
106525     */
106526     sqlite3WhereEnd(pWInfo);
106527   }else{
106528     int iPk;         /* First of nPk memory cells holding PRIMARY KEY value */
106529     i16 nPk;         /* Number of components of the PRIMARY KEY */
106530     int addrOpen;    /* Address of the OpenEphemeral instruction */
106531 
106532     assert( pPk!=0 );
106533     nPk = pPk->nKeyCol;
106534     iPk = pParse->nMem+1;
106535     pParse->nMem += nPk;
106536     regKey = ++pParse->nMem;
106537     iEph = pParse->nTab++;
106538     sqlite3VdbeAddOp2(v, OP_Null, 0, iPk);
106539     addrOpen = sqlite3VdbeAddOp2(v, OP_OpenEphemeral, iEph, nPk);
106540     sqlite3VdbeSetP4KeyInfo(pParse, pPk);
106541     pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, 0, 0,
106542                                WHERE_ONEPASS_DESIRED, iIdxCur);
106543     if( pWInfo==0 ) goto update_cleanup;
106544     okOnePass = sqlite3WhereOkOnePass(pWInfo, aiCurOnePass);
106545     for(i=0; i<nPk; i++){
106546       sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, pPk->aiColumn[i],
106547                                       iPk+i);
106548     }
106549     if( okOnePass ){
106550       sqlite3VdbeChangeToNoop(v, addrOpen);
106551       nKey = nPk;
106552       regKey = iPk;
106553     }else{
106554       sqlite3VdbeAddOp4(v, OP_MakeRecord, iPk, nPk, regKey,
106555                         sqlite3IndexAffinityStr(v, pPk), P4_TRANSIENT);
106556       sqlite3VdbeAddOp2(v, OP_IdxInsert, iEph, regKey);
106557     }
106558     sqlite3WhereEnd(pWInfo);
106559   }
106560 
106561   /* Initialize the count of updated rows
106562   */
106563   if( (db->flags & SQLITE_CountRows) && !pParse->pTriggerTab ){
106564     regRowCount = ++pParse->nMem;
106565     sqlite3VdbeAddOp2(v, OP_Integer, 0, regRowCount);
106566   }
106567 
106568   labelBreak = sqlite3VdbeMakeLabel(v);
106569   if( !isView ){
106570     /*
106571     ** Open every index that needs updating.  Note that if any
106572     ** index could potentially invoke a REPLACE conflict resolution
106573     ** action, then we need to open all indices because we might need
106574     ** to be deleting some records.
106575     */
106576     if( onError==OE_Replace ){
106577       memset(aToOpen, 1, nIdx+1);
106578     }else{
106579       for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
106580         if( pIdx->onError==OE_Replace ){
106581           memset(aToOpen, 1, nIdx+1);
106582           break;
106583         }
106584       }
106585     }
106586     if( okOnePass ){
106587       if( aiCurOnePass[0]>=0 ) aToOpen[aiCurOnePass[0]-iBaseCur] = 0;
106588       if( aiCurOnePass[1]>=0 ) aToOpen[aiCurOnePass[1]-iBaseCur] = 0;
106589     }
106590     sqlite3OpenTableAndIndices(pParse, pTab, OP_OpenWrite, iBaseCur, aToOpen,
106591                                0, 0);
106592   }
106593 
106594   /* Top of the update loop */
106595   if( okOnePass ){
106596     if( aToOpen[iDataCur-iBaseCur] ){
106597       assert( pPk!=0 );
106598       sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, labelBreak, regKey, nKey);
106599     }
106600     labelContinue = labelBreak;
106601     sqlite3VdbeAddOp2(v, OP_IsNull, pPk ? regKey : regOldRowid, labelBreak);
106602   }else if( pPk ){
106603     labelContinue = sqlite3VdbeMakeLabel(v);
106604     sqlite3VdbeAddOp2(v, OP_Rewind, iEph, labelBreak);
106605     addrTop = sqlite3VdbeAddOp2(v, OP_RowKey, iEph, regKey);
106606     sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, labelContinue, regKey, 0);
106607   }else{
106608     labelContinue = sqlite3VdbeAddOp3(v, OP_RowSetRead, regRowSet, labelBreak,
106609                              regOldRowid);
106610     sqlite3VdbeAddOp3(v, OP_NotExists, iDataCur, labelContinue, regOldRowid);
106611   }
106612 
106613   /* If the record number will change, set register regNewRowid to
106614   ** contain the new value. If the record number is not being modified,
106615   ** then regNewRowid is the same register as regOldRowid, which is
106616   ** already populated.  */
106617   assert( chngKey || pTrigger || hasFK || regOldRowid==regNewRowid );
106618   if( chngRowid ){
106619     sqlite3ExprCode(pParse, pRowidExpr, regNewRowid);
106620     sqlite3VdbeAddOp1(v, OP_MustBeInt, regNewRowid);
106621   }
106622 
106623   /* Compute the old pre-UPDATE content of the row being changed, if that
106624   ** information is needed */
106625   if( chngPk || hasFK || pTrigger ){
106626     u32 oldmask = (hasFK ? sqlite3FkOldmask(pParse, pTab) : 0);
106627     oldmask |= sqlite3TriggerColmask(pParse,
106628         pTrigger, pChanges, 0, TRIGGER_BEFORE|TRIGGER_AFTER, pTab, onError
106629     );
106630     for(i=0; i<pTab->nCol; i++){
106631       if( oldmask==0xffffffff
106632        || (i<32 && (oldmask & MASKBIT32(i))!=0)
106633        || (pTab->aCol[i].colFlags & COLFLAG_PRIMKEY)!=0
106634       ){
106635         testcase(  oldmask!=0xffffffff && i==31 );
106636         sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, i, regOld+i);
106637       }else{
106638         sqlite3VdbeAddOp2(v, OP_Null, 0, regOld+i);
106639       }
106640     }
106641     if( chngRowid==0 && pPk==0 ){
106642       sqlite3VdbeAddOp2(v, OP_Copy, regOldRowid, regNewRowid);
106643     }
106644   }
106645 
106646   /* Populate the array of registers beginning at regNew with the new
106647   ** row data. This array is used to check constaints, create the new
106648   ** table and index records, and as the values for any new.* references
106649   ** made by triggers.
106650   **
106651   ** If there are one or more BEFORE triggers, then do not populate the
106652   ** registers associated with columns that are (a) not modified by
106653   ** this UPDATE statement and (b) not accessed by new.* references. The
106654   ** values for registers not modified by the UPDATE must be reloaded from
106655   ** the database after the BEFORE triggers are fired anyway (as the trigger
106656   ** may have modified them). So not loading those that are not going to
106657   ** be used eliminates some redundant opcodes.
106658   */
106659   newmask = sqlite3TriggerColmask(
106660       pParse, pTrigger, pChanges, 1, TRIGGER_BEFORE, pTab, onError
106661   );
106662   /*sqlite3VdbeAddOp3(v, OP_Null, 0, regNew, regNew+pTab->nCol-1);*/
106663   for(i=0; i<pTab->nCol; i++){
106664     if( i==pTab->iPKey ){
106665       sqlite3VdbeAddOp2(v, OP_Null, 0, regNew+i);
106666     }else{
106667       j = aXRef[i];
106668       if( j>=0 ){
106669         sqlite3ExprCode(pParse, pChanges->a[j].pExpr, regNew+i);
106670       }else if( 0==(tmask&TRIGGER_BEFORE) || i>31 || (newmask & MASKBIT32(i)) ){
106671         /* This branch loads the value of a column that will not be changed
106672         ** into a register. This is done if there are no BEFORE triggers, or
106673         ** if there are one or more BEFORE triggers that use this value via
106674         ** a new.* reference in a trigger program.
106675         */
106676         testcase( i==31 );
106677         testcase( i==32 );
106678         sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, i, regNew+i);
106679       }else{
106680         sqlite3VdbeAddOp2(v, OP_Null, 0, regNew+i);
106681       }
106682     }
106683   }
106684 
106685   /* Fire any BEFORE UPDATE triggers. This happens before constraints are
106686   ** verified. One could argue that this is wrong.
106687   */
106688   if( tmask&TRIGGER_BEFORE ){
106689     sqlite3VdbeAddOp2(v, OP_Affinity, regNew, pTab->nCol);
106690     sqlite3TableAffinityStr(v, pTab);
106691     sqlite3CodeRowTrigger(pParse, pTrigger, TK_UPDATE, pChanges,
106692         TRIGGER_BEFORE, pTab, regOldRowid, onError, labelContinue);
106693 
106694     /* The row-trigger may have deleted the row being updated. In this
106695     ** case, jump to the next row. No updates or AFTER triggers are
106696     ** required. This behavior - what happens when the row being updated
106697     ** is deleted or renamed by a BEFORE trigger - is left undefined in the
106698     ** documentation.
106699     */
106700     if( pPk ){
106701       sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, labelContinue,regKey,nKey);
106702     }else{
106703       sqlite3VdbeAddOp3(v, OP_NotExists, iDataCur, labelContinue, regOldRowid);
106704     }
106705 
106706     /* If it did not delete it, the row-trigger may still have modified
106707     ** some of the columns of the row being updated. Load the values for
106708     ** all columns not modified by the update statement into their
106709     ** registers in case this has happened.
106710     */
106711     for(i=0; i<pTab->nCol; i++){
106712       if( aXRef[i]<0 && i!=pTab->iPKey ){
106713         sqlite3ExprCodeGetColumnOfTable(v, pTab, iDataCur, i, regNew+i);
106714       }
106715     }
106716   }
106717 
106718   if( !isView ){
106719     int j1 = 0;           /* Address of jump instruction */
106720     int bReplace = 0;     /* True if REPLACE conflict resolution might happen */
106721 
106722     /* Do constraint checks. */
106723     assert( regOldRowid>0 );
106724     sqlite3GenerateConstraintChecks(pParse, pTab, aRegIdx, iDataCur, iIdxCur,
106725         regNewRowid, regOldRowid, chngKey, onError, labelContinue, &bReplace);
106726 
106727     /* Do FK constraint checks. */
106728     if( hasFK ){
106729       sqlite3FkCheck(pParse, pTab, regOldRowid, 0, aXRef, chngKey);
106730     }
106731 
106732     /* Delete the index entries associated with the current record.  */
106733     if( bReplace || chngKey ){
106734       if( pPk ){
106735         j1 = sqlite3VdbeAddOp4Int(v, OP_NotFound, iDataCur, 0, regKey, nKey);
106736       }else{
106737         j1 = sqlite3VdbeAddOp3(v, OP_NotExists, iDataCur, 0, regOldRowid);
106738       }
106739     }
106740     sqlite3GenerateRowIndexDelete(pParse, pTab, iDataCur, iIdxCur, aRegIdx);
106741 
106742     /* If changing the record number, delete the old record.  */
106743     if( hasFK || chngKey || pPk!=0 ){
106744       sqlite3VdbeAddOp2(v, OP_Delete, iDataCur, 0);
106745     }
106746     if( bReplace || chngKey ){
106747       sqlite3VdbeJumpHere(v, j1);
106748     }
106749 
106750     if( hasFK ){
106751       sqlite3FkCheck(pParse, pTab, 0, regNewRowid, aXRef, chngKey);
106752     }
106753 
106754     /* Insert the new index entries and the new record. */
106755     sqlite3CompleteInsertion(pParse, pTab, iDataCur, iIdxCur,
106756                              regNewRowid, aRegIdx, 1, 0, 0);
106757 
106758     /* Do any ON CASCADE, SET NULL or SET DEFAULT operations required to
106759     ** handle rows (possibly in other tables) that refer via a foreign key
106760     ** to the row just updated. */
106761     if( hasFK ){
106762       sqlite3FkActions(pParse, pTab, pChanges, regOldRowid, aXRef, chngKey);
106763     }
106764   }
106765 
106766   /* Increment the row counter
106767   */
106768   if( (db->flags & SQLITE_CountRows) && !pParse->pTriggerTab){
106769     sqlite3VdbeAddOp2(v, OP_AddImm, regRowCount, 1);
106770   }
106771 
106772   sqlite3CodeRowTrigger(pParse, pTrigger, TK_UPDATE, pChanges,
106773       TRIGGER_AFTER, pTab, regOldRowid, onError, labelContinue);
106774 
106775   /* Repeat the above with the next record to be updated, until
106776   ** all record selected by the WHERE clause have been updated.
106777   */
106778   if( okOnePass ){
106779     /* Nothing to do at end-of-loop for a single-pass */
106780   }else if( pPk ){
106781     sqlite3VdbeResolveLabel(v, labelContinue);
106782     sqlite3VdbeAddOp2(v, OP_Next, iEph, addrTop);
106783   }else{
106784     sqlite3VdbeAddOp2(v, OP_Goto, 0, labelContinue);
106785   }
106786   sqlite3VdbeResolveLabel(v, labelBreak);
106787 
106788   /* Close all tables */
106789   for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
106790     assert( aRegIdx );
106791     if( aToOpen[i+1] ){
106792       sqlite3VdbeAddOp2(v, OP_Close, iIdxCur+i, 0);
106793     }
106794   }
106795   if( iDataCur<iIdxCur ) sqlite3VdbeAddOp2(v, OP_Close, iDataCur, 0);
106796 
106797   /* Update the sqlite_sequence table by storing the content of the
106798   ** maximum rowid counter values recorded while inserting into
106799   ** autoincrement tables.
106800   */
106801   if( pParse->nested==0 && pParse->pTriggerTab==0 ){
106802     sqlite3AutoincrementEnd(pParse);
106803   }
106804 
106805   /*
106806   ** Return the number of rows that were changed. If this routine is
106807   ** generating code because of a call to sqlite3NestedParse(), do not
106808   ** invoke the callback function.
106809   */
106810   if( (db->flags&SQLITE_CountRows) && !pParse->pTriggerTab && !pParse->nested ){
106811     sqlite3VdbeAddOp2(v, OP_ResultRow, regRowCount, 1);
106812     sqlite3VdbeSetNumCols(v, 1);
106813     sqlite3VdbeSetColName(v, 0, COLNAME_NAME, "rows updated", SQLITE_STATIC);
106814   }
106815 
106816 update_cleanup:
106817   sqlite3AuthContextPop(&sContext);
106818   sqlite3DbFree(db, aXRef); /* Also frees aRegIdx[] and aToOpen[] */
106819   sqlite3SrcListDelete(db, pTabList);
106820   sqlite3ExprListDelete(db, pChanges);
106821   sqlite3ExprDelete(db, pWhere);
106822   return;
106823 }
106824 /* Make sure "isView" and other macros defined above are undefined. Otherwise
106825 ** thely may interfere with compilation of other functions in this file
106826 ** (or in another file, if this file becomes part of the amalgamation).  */
106827 #ifdef isView
106828  #undef isView
106829 #endif
106830 #ifdef pTrigger
106831  #undef pTrigger
106832 #endif
106833 
106834 #ifndef SQLITE_OMIT_VIRTUALTABLE
106835 /*
106836 ** Generate code for an UPDATE of a virtual table.
106837 **
106838 ** The strategy is that we create an ephemerial table that contains
106839 ** for each row to be changed:
106840 **
106841 **   (A)  The original rowid of that row.
106842 **   (B)  The revised rowid for the row. (note1)
106843 **   (C)  The content of every column in the row.
106844 **
106845 ** Then we loop over this ephemeral table and for each row in
106846 ** the ephermeral table call VUpdate.
106847 **
106848 ** When finished, drop the ephemeral table.
106849 **
106850 ** (note1) Actually, if we know in advance that (A) is always the same
106851 ** as (B) we only store (A), then duplicate (A) when pulling
106852 ** it out of the ephemeral table before calling VUpdate.
106853 */
106854 static void updateVirtualTable(
106855   Parse *pParse,       /* The parsing context */
106856   SrcList *pSrc,       /* The virtual table to be modified */
106857   Table *pTab,         /* The virtual table */
106858   ExprList *pChanges,  /* The columns to change in the UPDATE statement */
106859   Expr *pRowid,        /* Expression used to recompute the rowid */
106860   int *aXRef,          /* Mapping from columns of pTab to entries in pChanges */
106861   Expr *pWhere,        /* WHERE clause of the UPDATE statement */
106862   int onError          /* ON CONFLICT strategy */
106863 ){
106864   Vdbe *v = pParse->pVdbe;  /* Virtual machine under construction */
106865   ExprList *pEList = 0;     /* The result set of the SELECT statement */
106866   Select *pSelect = 0;      /* The SELECT statement */
106867   Expr *pExpr;              /* Temporary expression */
106868   int ephemTab;             /* Table holding the result of the SELECT */
106869   int i;                    /* Loop counter */
106870   int addr;                 /* Address of top of loop */
106871   int iReg;                 /* First register in set passed to OP_VUpdate */
106872   sqlite3 *db = pParse->db; /* Database connection */
106873   const char *pVTab = (const char*)sqlite3GetVTable(db, pTab);
106874   SelectDest dest;
106875 
106876   /* Construct the SELECT statement that will find the new values for
106877   ** all updated rows.
106878   */
106879   pEList = sqlite3ExprListAppend(pParse, 0, sqlite3Expr(db, TK_ID, "_rowid_"));
106880   if( pRowid ){
106881     pEList = sqlite3ExprListAppend(pParse, pEList,
106882                                    sqlite3ExprDup(db, pRowid, 0));
106883   }
106884   assert( pTab->iPKey<0 );
106885   for(i=0; i<pTab->nCol; i++){
106886     if( aXRef[i]>=0 ){
106887       pExpr = sqlite3ExprDup(db, pChanges->a[aXRef[i]].pExpr, 0);
106888     }else{
106889       pExpr = sqlite3Expr(db, TK_ID, pTab->aCol[i].zName);
106890     }
106891     pEList = sqlite3ExprListAppend(pParse, pEList, pExpr);
106892   }
106893   pSelect = sqlite3SelectNew(pParse, pEList, pSrc, pWhere, 0, 0, 0, 0, 0, 0);
106894 
106895   /* Create the ephemeral table into which the update results will
106896   ** be stored.
106897   */
106898   assert( v );
106899   ephemTab = pParse->nTab++;
106900   sqlite3VdbeAddOp2(v, OP_OpenEphemeral, ephemTab, pTab->nCol+1+(pRowid!=0));
106901   sqlite3VdbeChangeP5(v, BTREE_UNORDERED);
106902 
106903   /* fill the ephemeral table
106904   */
106905   sqlite3SelectDestInit(&dest, SRT_Table, ephemTab);
106906   sqlite3Select(pParse, pSelect, &dest);
106907 
106908   /* Generate code to scan the ephemeral table and call VUpdate. */
106909   iReg = ++pParse->nMem;
106910   pParse->nMem += pTab->nCol+1;
106911   addr = sqlite3VdbeAddOp2(v, OP_Rewind, ephemTab, 0);
106912   sqlite3VdbeAddOp3(v, OP_Column,  ephemTab, 0, iReg);
106913   sqlite3VdbeAddOp3(v, OP_Column, ephemTab, (pRowid?1:0), iReg+1);
106914   for(i=0; i<pTab->nCol; i++){
106915     sqlite3VdbeAddOp3(v, OP_Column, ephemTab, i+1+(pRowid!=0), iReg+2+i);
106916   }
106917   sqlite3VtabMakeWritable(pParse, pTab);
106918   sqlite3VdbeAddOp4(v, OP_VUpdate, 0, pTab->nCol+2, iReg, pVTab, P4_VTAB);
106919   sqlite3VdbeChangeP5(v, onError==OE_Default ? OE_Abort : onError);
106920   sqlite3MayAbort(pParse);
106921   sqlite3VdbeAddOp2(v, OP_Next, ephemTab, addr+1);
106922   sqlite3VdbeJumpHere(v, addr);
106923   sqlite3VdbeAddOp2(v, OP_Close, ephemTab, 0);
106924 
106925   /* Cleanup */
106926   sqlite3SelectDelete(db, pSelect);
106927 }
106928 #endif /* SQLITE_OMIT_VIRTUALTABLE */
106929 
106930 /************** End of update.c **********************************************/
106931 /************** Begin file vacuum.c ******************************************/
106932 /*
106933 ** 2003 April 6
106934 **
106935 ** The author disclaims copyright to this source code.  In place of
106936 ** a legal notice, here is a blessing:
106937 **
106938 **    May you do good and not evil.
106939 **    May you find forgiveness for yourself and forgive others.
106940 **    May you share freely, never taking more than you give.
106941 **
106942 *************************************************************************
106943 ** This file contains code used to implement the VACUUM command.
106944 **
106945 ** Most of the code in this file may be omitted by defining the
106946 ** SQLITE_OMIT_VACUUM macro.
106947 */
106948 
106949 #if !defined(SQLITE_OMIT_VACUUM) && !defined(SQLITE_OMIT_ATTACH)
106950 /*
106951 ** Finalize a prepared statement.  If there was an error, store the
106952 ** text of the error message in *pzErrMsg.  Return the result code.
106953 */
106954 static int vacuumFinalize(sqlite3 *db, sqlite3_stmt *pStmt, char **pzErrMsg){
106955   int rc;
106956   rc = sqlite3VdbeFinalize((Vdbe*)pStmt);
106957   if( rc ){
106958     sqlite3SetString(pzErrMsg, db, sqlite3_errmsg(db));
106959   }
106960   return rc;
106961 }
106962 
106963 /*
106964 ** Execute zSql on database db. Return an error code.
106965 */
106966 static int execSql(sqlite3 *db, char **pzErrMsg, const char *zSql){
106967   sqlite3_stmt *pStmt;
106968   VVA_ONLY( int rc; )
106969   if( !zSql ){
106970     return SQLITE_NOMEM;
106971   }
106972   if( SQLITE_OK!=sqlite3_prepare(db, zSql, -1, &pStmt, 0) ){
106973     sqlite3SetString(pzErrMsg, db, sqlite3_errmsg(db));
106974     return sqlite3_errcode(db);
106975   }
106976   VVA_ONLY( rc = ) sqlite3_step(pStmt);
106977   assert( rc!=SQLITE_ROW || (db->flags&SQLITE_CountRows) );
106978   return vacuumFinalize(db, pStmt, pzErrMsg);
106979 }
106980 
106981 /*
106982 ** Execute zSql on database db. The statement returns exactly
106983 ** one column. Execute this as SQL on the same database.
106984 */
106985 static int execExecSql(sqlite3 *db, char **pzErrMsg, const char *zSql){
106986   sqlite3_stmt *pStmt;
106987   int rc;
106988 
106989   rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
106990   if( rc!=SQLITE_OK ) return rc;
106991 
106992   while( SQLITE_ROW==sqlite3_step(pStmt) ){
106993     rc = execSql(db, pzErrMsg, (char*)sqlite3_column_text(pStmt, 0));
106994     if( rc!=SQLITE_OK ){
106995       vacuumFinalize(db, pStmt, pzErrMsg);
106996       return rc;
106997     }
106998   }
106999 
107000   return vacuumFinalize(db, pStmt, pzErrMsg);
107001 }
107002 
107003 /*
107004 ** The VACUUM command is used to clean up the database,
107005 ** collapse free space, etc.  It is modelled after the VACUUM command
107006 ** in PostgreSQL.  The VACUUM command works as follows:
107007 **
107008 **   (1)  Create a new transient database file
107009 **   (2)  Copy all content from the database being vacuumed into
107010 **        the new transient database file
107011 **   (3)  Copy content from the transient database back into the
107012 **        original database.
107013 **
107014 ** The transient database requires temporary disk space approximately
107015 ** equal to the size of the original database.  The copy operation of
107016 ** step (3) requires additional temporary disk space approximately equal
107017 ** to the size of the original database for the rollback journal.
107018 ** Hence, temporary disk space that is approximately 2x the size of the
107019 ** orginal database is required.  Every page of the database is written
107020 ** approximately 3 times:  Once for step (2) and twice for step (3).
107021 ** Two writes per page are required in step (3) because the original
107022 ** database content must be written into the rollback journal prior to
107023 ** overwriting the database with the vacuumed content.
107024 **
107025 ** Only 1x temporary space and only 1x writes would be required if
107026 ** the copy of step (3) were replace by deleting the original database
107027 ** and renaming the transient database as the original.  But that will
107028 ** not work if other processes are attached to the original database.
107029 ** And a power loss in between deleting the original and renaming the
107030 ** transient would cause the database file to appear to be deleted
107031 ** following reboot.
107032 */
107033 SQLITE_PRIVATE void sqlite3Vacuum(Parse *pParse){
107034   Vdbe *v = sqlite3GetVdbe(pParse);
107035   if( v ){
107036     sqlite3VdbeAddOp2(v, OP_Vacuum, 0, 0);
107037     sqlite3VdbeUsesBtree(v, 0);
107038   }
107039   return;
107040 }
107041 
107042 /*
107043 ** This routine implements the OP_Vacuum opcode of the VDBE.
107044 */
107045 SQLITE_PRIVATE int sqlite3RunVacuum(char **pzErrMsg, sqlite3 *db){
107046   int rc = SQLITE_OK;     /* Return code from service routines */
107047   Btree *pMain;           /* The database being vacuumed */
107048   Btree *pTemp;           /* The temporary database we vacuum into */
107049   char *zSql = 0;         /* SQL statements */
107050   int saved_flags;        /* Saved value of the db->flags */
107051   int saved_nChange;      /* Saved value of db->nChange */
107052   int saved_nTotalChange; /* Saved value of db->nTotalChange */
107053   void (*saved_xTrace)(void*,const char*);  /* Saved db->xTrace */
107054   Db *pDb = 0;            /* Database to detach at end of vacuum */
107055   int isMemDb;            /* True if vacuuming a :memory: database */
107056   int nRes;               /* Bytes of reserved space at the end of each page */
107057   int nDb;                /* Number of attached databases */
107058 
107059   if( !db->autoCommit ){
107060     sqlite3SetString(pzErrMsg, db, "cannot VACUUM from within a transaction");
107061     return SQLITE_ERROR;
107062   }
107063   if( db->nVdbeActive>1 ){
107064     sqlite3SetString(pzErrMsg, db,"cannot VACUUM - SQL statements in progress");
107065     return SQLITE_ERROR;
107066   }
107067 
107068   /* Save the current value of the database flags so that it can be
107069   ** restored before returning. Then set the writable-schema flag, and
107070   ** disable CHECK and foreign key constraints.  */
107071   saved_flags = db->flags;
107072   saved_nChange = db->nChange;
107073   saved_nTotalChange = db->nTotalChange;
107074   saved_xTrace = db->xTrace;
107075   db->flags |= SQLITE_WriteSchema | SQLITE_IgnoreChecks | SQLITE_PreferBuiltin;
107076   db->flags &= ~(SQLITE_ForeignKeys | SQLITE_ReverseOrder);
107077   db->xTrace = 0;
107078 
107079   pMain = db->aDb[0].pBt;
107080   isMemDb = sqlite3PagerIsMemdb(sqlite3BtreePager(pMain));
107081 
107082   /* Attach the temporary database as 'vacuum_db'. The synchronous pragma
107083   ** can be set to 'off' for this file, as it is not recovered if a crash
107084   ** occurs anyway. The integrity of the database is maintained by a
107085   ** (possibly synchronous) transaction opened on the main database before
107086   ** sqlite3BtreeCopyFile() is called.
107087   **
107088   ** An optimisation would be to use a non-journaled pager.
107089   ** (Later:) I tried setting "PRAGMA vacuum_db.journal_mode=OFF" but
107090   ** that actually made the VACUUM run slower.  Very little journalling
107091   ** actually occurs when doing a vacuum since the vacuum_db is initially
107092   ** empty.  Only the journal header is written.  Apparently it takes more
107093   ** time to parse and run the PRAGMA to turn journalling off than it does
107094   ** to write the journal header file.
107095   */
107096   nDb = db->nDb;
107097   if( sqlite3TempInMemory(db) ){
107098     zSql = "ATTACH ':memory:' AS vacuum_db;";
107099   }else{
107100     zSql = "ATTACH '' AS vacuum_db;";
107101   }
107102   rc = execSql(db, pzErrMsg, zSql);
107103   if( db->nDb>nDb ){
107104     pDb = &db->aDb[db->nDb-1];
107105     assert( strcmp(pDb->zName,"vacuum_db")==0 );
107106   }
107107   if( rc!=SQLITE_OK ) goto end_of_vacuum;
107108   pTemp = db->aDb[db->nDb-1].pBt;
107109 
107110   /* The call to execSql() to attach the temp database has left the file
107111   ** locked (as there was more than one active statement when the transaction
107112   ** to read the schema was concluded. Unlock it here so that this doesn't
107113   ** cause problems for the call to BtreeSetPageSize() below.  */
107114   sqlite3BtreeCommit(pTemp);
107115 
107116   nRes = sqlite3BtreeGetReserve(pMain);
107117 
107118   /* A VACUUM cannot change the pagesize of an encrypted database. */
107119 #ifdef SQLITE_HAS_CODEC
107120   if( db->nextPagesize ){
107121     extern void sqlite3CodecGetKey(sqlite3*, int, void**, int*);
107122     int nKey;
107123     char *zKey;
107124     sqlite3CodecGetKey(db, 0, (void**)&zKey, &nKey);
107125     if( nKey ) db->nextPagesize = 0;
107126   }
107127 #endif
107128 
107129   rc = execSql(db, pzErrMsg, "PRAGMA vacuum_db.synchronous=OFF");
107130   if( rc!=SQLITE_OK ) goto end_of_vacuum;
107131 
107132   /* Begin a transaction and take an exclusive lock on the main database
107133   ** file. This is done before the sqlite3BtreeGetPageSize(pMain) call below,
107134   ** to ensure that we do not try to change the page-size on a WAL database.
107135   */
107136   rc = execSql(db, pzErrMsg, "BEGIN;");
107137   if( rc!=SQLITE_OK ) goto end_of_vacuum;
107138   rc = sqlite3BtreeBeginTrans(pMain, 2);
107139   if( rc!=SQLITE_OK ) goto end_of_vacuum;
107140 
107141   /* Do not attempt to change the page size for a WAL database */
107142   if( sqlite3PagerGetJournalMode(sqlite3BtreePager(pMain))
107143                                                ==PAGER_JOURNALMODE_WAL ){
107144     db->nextPagesize = 0;
107145   }
107146 
107147   if( sqlite3BtreeSetPageSize(pTemp, sqlite3BtreeGetPageSize(pMain), nRes, 0)
107148    || (!isMemDb && sqlite3BtreeSetPageSize(pTemp, db->nextPagesize, nRes, 0))
107149    || NEVER(db->mallocFailed)
107150   ){
107151     rc = SQLITE_NOMEM;
107152     goto end_of_vacuum;
107153   }
107154 
107155 #ifndef SQLITE_OMIT_AUTOVACUUM
107156   sqlite3BtreeSetAutoVacuum(pTemp, db->nextAutovac>=0 ? db->nextAutovac :
107157                                            sqlite3BtreeGetAutoVacuum(pMain));
107158 #endif
107159 
107160   /* Query the schema of the main database. Create a mirror schema
107161   ** in the temporary database.
107162   */
107163   rc = execExecSql(db, pzErrMsg,
107164       "SELECT 'CREATE TABLE vacuum_db.' || substr(sql,14) "
107165       "  FROM sqlite_master WHERE type='table' AND name!='sqlite_sequence'"
107166       "   AND coalesce(rootpage,1)>0"
107167   );
107168   if( rc!=SQLITE_OK ) goto end_of_vacuum;
107169   rc = execExecSql(db, pzErrMsg,
107170       "SELECT 'CREATE INDEX vacuum_db.' || substr(sql,14)"
107171       "  FROM sqlite_master WHERE sql LIKE 'CREATE INDEX %' ");
107172   if( rc!=SQLITE_OK ) goto end_of_vacuum;
107173   rc = execExecSql(db, pzErrMsg,
107174       "SELECT 'CREATE UNIQUE INDEX vacuum_db.' || substr(sql,21) "
107175       "  FROM sqlite_master WHERE sql LIKE 'CREATE UNIQUE INDEX %'");
107176   if( rc!=SQLITE_OK ) goto end_of_vacuum;
107177 
107178   /* Loop through the tables in the main database. For each, do
107179   ** an "INSERT INTO vacuum_db.xxx SELECT * FROM main.xxx;" to copy
107180   ** the contents to the temporary database.
107181   */
107182   rc = execExecSql(db, pzErrMsg,
107183       "SELECT 'INSERT INTO vacuum_db.' || quote(name) "
107184       "|| ' SELECT * FROM main.' || quote(name) || ';'"
107185       "FROM main.sqlite_master "
107186       "WHERE type = 'table' AND name!='sqlite_sequence' "
107187       "  AND coalesce(rootpage,1)>0"
107188   );
107189   if( rc!=SQLITE_OK ) goto end_of_vacuum;
107190 
107191   /* Copy over the sequence table
107192   */
107193   rc = execExecSql(db, pzErrMsg,
107194       "SELECT 'DELETE FROM vacuum_db.' || quote(name) || ';' "
107195       "FROM vacuum_db.sqlite_master WHERE name='sqlite_sequence' "
107196   );
107197   if( rc!=SQLITE_OK ) goto end_of_vacuum;
107198   rc = execExecSql(db, pzErrMsg,
107199       "SELECT 'INSERT INTO vacuum_db.' || quote(name) "
107200       "|| ' SELECT * FROM main.' || quote(name) || ';' "
107201       "FROM vacuum_db.sqlite_master WHERE name=='sqlite_sequence';"
107202   );
107203   if( rc!=SQLITE_OK ) goto end_of_vacuum;
107204 
107205 
107206   /* Copy the triggers, views, and virtual tables from the main database
107207   ** over to the temporary database.  None of these objects has any
107208   ** associated storage, so all we have to do is copy their entries
107209   ** from the SQLITE_MASTER table.
107210   */
107211   rc = execSql(db, pzErrMsg,
107212       "INSERT INTO vacuum_db.sqlite_master "
107213       "  SELECT type, name, tbl_name, rootpage, sql"
107214       "    FROM main.sqlite_master"
107215       "   WHERE type='view' OR type='trigger'"
107216       "      OR (type='table' AND rootpage=0)"
107217   );
107218   if( rc ) goto end_of_vacuum;
107219 
107220   /* At this point, there is a write transaction open on both the
107221   ** vacuum database and the main database. Assuming no error occurs,
107222   ** both transactions are closed by this block - the main database
107223   ** transaction by sqlite3BtreeCopyFile() and the other by an explicit
107224   ** call to sqlite3BtreeCommit().
107225   */
107226   {
107227     u32 meta;
107228     int i;
107229 
107230     /* This array determines which meta meta values are preserved in the
107231     ** vacuum.  Even entries are the meta value number and odd entries
107232     ** are an increment to apply to the meta value after the vacuum.
107233     ** The increment is used to increase the schema cookie so that other
107234     ** connections to the same database will know to reread the schema.
107235     */
107236     static const unsigned char aCopy[] = {
107237        BTREE_SCHEMA_VERSION,     1,  /* Add one to the old schema cookie */
107238        BTREE_DEFAULT_CACHE_SIZE, 0,  /* Preserve the default page cache size */
107239        BTREE_TEXT_ENCODING,      0,  /* Preserve the text encoding */
107240        BTREE_USER_VERSION,       0,  /* Preserve the user version */
107241        BTREE_APPLICATION_ID,     0,  /* Preserve the application id */
107242     };
107243 
107244     assert( 1==sqlite3BtreeIsInTrans(pTemp) );
107245     assert( 1==sqlite3BtreeIsInTrans(pMain) );
107246 
107247     /* Copy Btree meta values */
107248     for(i=0; i<ArraySize(aCopy); i+=2){
107249       /* GetMeta() and UpdateMeta() cannot fail in this context because
107250       ** we already have page 1 loaded into cache and marked dirty. */
107251       sqlite3BtreeGetMeta(pMain, aCopy[i], &meta);
107252       rc = sqlite3BtreeUpdateMeta(pTemp, aCopy[i], meta+aCopy[i+1]);
107253       if( NEVER(rc!=SQLITE_OK) ) goto end_of_vacuum;
107254     }
107255 
107256     rc = sqlite3BtreeCopyFile(pMain, pTemp);
107257     if( rc!=SQLITE_OK ) goto end_of_vacuum;
107258     rc = sqlite3BtreeCommit(pTemp);
107259     if( rc!=SQLITE_OK ) goto end_of_vacuum;
107260 #ifndef SQLITE_OMIT_AUTOVACUUM
107261     sqlite3BtreeSetAutoVacuum(pMain, sqlite3BtreeGetAutoVacuum(pTemp));
107262 #endif
107263   }
107264 
107265   assert( rc==SQLITE_OK );
107266   rc = sqlite3BtreeSetPageSize(pMain, sqlite3BtreeGetPageSize(pTemp), nRes,1);
107267 
107268 end_of_vacuum:
107269   /* Restore the original value of db->flags */
107270   db->flags = saved_flags;
107271   db->nChange = saved_nChange;
107272   db->nTotalChange = saved_nTotalChange;
107273   db->xTrace = saved_xTrace;
107274   sqlite3BtreeSetPageSize(pMain, -1, -1, 1);
107275 
107276   /* Currently there is an SQL level transaction open on the vacuum
107277   ** database. No locks are held on any other files (since the main file
107278   ** was committed at the btree level). So it safe to end the transaction
107279   ** by manually setting the autoCommit flag to true and detaching the
107280   ** vacuum database. The vacuum_db journal file is deleted when the pager
107281   ** is closed by the DETACH.
107282   */
107283   db->autoCommit = 1;
107284 
107285   if( pDb ){
107286     sqlite3BtreeClose(pDb->pBt);
107287     pDb->pBt = 0;
107288     pDb->pSchema = 0;
107289   }
107290 
107291   /* This both clears the schemas and reduces the size of the db->aDb[]
107292   ** array. */
107293   sqlite3ResetAllSchemasOfConnection(db);
107294 
107295   return rc;
107296 }
107297 
107298 #endif  /* SQLITE_OMIT_VACUUM && SQLITE_OMIT_ATTACH */
107299 
107300 /************** End of vacuum.c **********************************************/
107301 /************** Begin file vtab.c ********************************************/
107302 /*
107303 ** 2006 June 10
107304 **
107305 ** The author disclaims copyright to this source code.  In place of
107306 ** a legal notice, here is a blessing:
107307 **
107308 **    May you do good and not evil.
107309 **    May you find forgiveness for yourself and forgive others.
107310 **    May you share freely, never taking more than you give.
107311 **
107312 *************************************************************************
107313 ** This file contains code used to help implement virtual tables.
107314 */
107315 #ifndef SQLITE_OMIT_VIRTUALTABLE
107316 
107317 /*
107318 ** Before a virtual table xCreate() or xConnect() method is invoked, the
107319 ** sqlite3.pVtabCtx member variable is set to point to an instance of
107320 ** this struct allocated on the stack. It is used by the implementation of
107321 ** the sqlite3_declare_vtab() and sqlite3_vtab_config() APIs, both of which
107322 ** are invoked only from within xCreate and xConnect methods.
107323 */
107324 struct VtabCtx {
107325   VTable *pVTable;    /* The virtual table being constructed */
107326   Table *pTab;        /* The Table object to which the virtual table belongs */
107327 };
107328 
107329 /*
107330 ** The actual function that does the work of creating a new module.
107331 ** This function implements the sqlite3_create_module() and
107332 ** sqlite3_create_module_v2() interfaces.
107333 */
107334 static int createModule(
107335   sqlite3 *db,                    /* Database in which module is registered */
107336   const char *zName,              /* Name assigned to this module */
107337   const sqlite3_module *pModule,  /* The definition of the module */
107338   void *pAux,                     /* Context pointer for xCreate/xConnect */
107339   void (*xDestroy)(void *)        /* Module destructor function */
107340 ){
107341   int rc = SQLITE_OK;
107342   int nName;
107343 
107344   sqlite3_mutex_enter(db->mutex);
107345   nName = sqlite3Strlen30(zName);
107346   if( sqlite3HashFind(&db->aModule, zName, nName) ){
107347     rc = SQLITE_MISUSE_BKPT;
107348   }else{
107349     Module *pMod;
107350     pMod = (Module *)sqlite3DbMallocRaw(db, sizeof(Module) + nName + 1);
107351     if( pMod ){
107352       Module *pDel;
107353       char *zCopy = (char *)(&pMod[1]);
107354       memcpy(zCopy, zName, nName+1);
107355       pMod->zName = zCopy;
107356       pMod->pModule = pModule;
107357       pMod->pAux = pAux;
107358       pMod->xDestroy = xDestroy;
107359       pDel = (Module *)sqlite3HashInsert(&db->aModule,zCopy,nName,(void*)pMod);
107360       assert( pDel==0 || pDel==pMod );
107361       if( pDel ){
107362         db->mallocFailed = 1;
107363         sqlite3DbFree(db, pDel);
107364       }
107365     }
107366   }
107367   rc = sqlite3ApiExit(db, rc);
107368   if( rc!=SQLITE_OK && xDestroy ) xDestroy(pAux);
107369 
107370   sqlite3_mutex_leave(db->mutex);
107371   return rc;
107372 }
107373 
107374 
107375 /*
107376 ** External API function used to create a new virtual-table module.
107377 */
107378 SQLITE_API int sqlite3_create_module(
107379   sqlite3 *db,                    /* Database in which module is registered */
107380   const char *zName,              /* Name assigned to this module */
107381   const sqlite3_module *pModule,  /* The definition of the module */
107382   void *pAux                      /* Context pointer for xCreate/xConnect */
107383 ){
107384   return createModule(db, zName, pModule, pAux, 0);
107385 }
107386 
107387 /*
107388 ** External API function used to create a new virtual-table module.
107389 */
107390 SQLITE_API int sqlite3_create_module_v2(
107391   sqlite3 *db,                    /* Database in which module is registered */
107392   const char *zName,              /* Name assigned to this module */
107393   const sqlite3_module *pModule,  /* The definition of the module */
107394   void *pAux,                     /* Context pointer for xCreate/xConnect */
107395   void (*xDestroy)(void *)        /* Module destructor function */
107396 ){
107397   return createModule(db, zName, pModule, pAux, xDestroy);
107398 }
107399 
107400 /*
107401 ** Lock the virtual table so that it cannot be disconnected.
107402 ** Locks nest.  Every lock should have a corresponding unlock.
107403 ** If an unlock is omitted, resources leaks will occur.
107404 **
107405 ** If a disconnect is attempted while a virtual table is locked,
107406 ** the disconnect is deferred until all locks have been removed.
107407 */
107408 SQLITE_PRIVATE void sqlite3VtabLock(VTable *pVTab){
107409   pVTab->nRef++;
107410 }
107411 
107412 
107413 /*
107414 ** pTab is a pointer to a Table structure representing a virtual-table.
107415 ** Return a pointer to the VTable object used by connection db to access
107416 ** this virtual-table, if one has been created, or NULL otherwise.
107417 */
107418 SQLITE_PRIVATE VTable *sqlite3GetVTable(sqlite3 *db, Table *pTab){
107419   VTable *pVtab;
107420   assert( IsVirtual(pTab) );
107421   for(pVtab=pTab->pVTable; pVtab && pVtab->db!=db; pVtab=pVtab->pNext);
107422   return pVtab;
107423 }
107424 
107425 /*
107426 ** Decrement the ref-count on a virtual table object. When the ref-count
107427 ** reaches zero, call the xDisconnect() method to delete the object.
107428 */
107429 SQLITE_PRIVATE void sqlite3VtabUnlock(VTable *pVTab){
107430   sqlite3 *db = pVTab->db;
107431 
107432   assert( db );
107433   assert( pVTab->nRef>0 );
107434   assert( db->magic==SQLITE_MAGIC_OPEN || db->magic==SQLITE_MAGIC_ZOMBIE );
107435 
107436   pVTab->nRef--;
107437   if( pVTab->nRef==0 ){
107438     sqlite3_vtab *p = pVTab->pVtab;
107439     if( p ){
107440       p->pModule->xDisconnect(p);
107441     }
107442     sqlite3DbFree(db, pVTab);
107443   }
107444 }
107445 
107446 /*
107447 ** Table p is a virtual table. This function moves all elements in the
107448 ** p->pVTable list to the sqlite3.pDisconnect lists of their associated
107449 ** database connections to be disconnected at the next opportunity.
107450 ** Except, if argument db is not NULL, then the entry associated with
107451 ** connection db is left in the p->pVTable list.
107452 */
107453 static VTable *vtabDisconnectAll(sqlite3 *db, Table *p){
107454   VTable *pRet = 0;
107455   VTable *pVTable = p->pVTable;
107456   p->pVTable = 0;
107457 
107458   /* Assert that the mutex (if any) associated with the BtShared database
107459   ** that contains table p is held by the caller. See header comments
107460   ** above function sqlite3VtabUnlockList() for an explanation of why
107461   ** this makes it safe to access the sqlite3.pDisconnect list of any
107462   ** database connection that may have an entry in the p->pVTable list.
107463   */
107464   assert( db==0 || sqlite3SchemaMutexHeld(db, 0, p->pSchema) );
107465 
107466   while( pVTable ){
107467     sqlite3 *db2 = pVTable->db;
107468     VTable *pNext = pVTable->pNext;
107469     assert( db2 );
107470     if( db2==db ){
107471       pRet = pVTable;
107472       p->pVTable = pRet;
107473       pRet->pNext = 0;
107474     }else{
107475       pVTable->pNext = db2->pDisconnect;
107476       db2->pDisconnect = pVTable;
107477     }
107478     pVTable = pNext;
107479   }
107480 
107481   assert( !db || pRet );
107482   return pRet;
107483 }
107484 
107485 /*
107486 ** Table *p is a virtual table. This function removes the VTable object
107487 ** for table *p associated with database connection db from the linked
107488 ** list in p->pVTab. It also decrements the VTable ref count. This is
107489 ** used when closing database connection db to free all of its VTable
107490 ** objects without disturbing the rest of the Schema object (which may
107491 ** be being used by other shared-cache connections).
107492 */
107493 SQLITE_PRIVATE void sqlite3VtabDisconnect(sqlite3 *db, Table *p){
107494   VTable **ppVTab;
107495 
107496   assert( IsVirtual(p) );
107497   assert( sqlite3BtreeHoldsAllMutexes(db) );
107498   assert( sqlite3_mutex_held(db->mutex) );
107499 
107500   for(ppVTab=&p->pVTable; *ppVTab; ppVTab=&(*ppVTab)->pNext){
107501     if( (*ppVTab)->db==db  ){
107502       VTable *pVTab = *ppVTab;
107503       *ppVTab = pVTab->pNext;
107504       sqlite3VtabUnlock(pVTab);
107505       break;
107506     }
107507   }
107508 }
107509 
107510 
107511 /*
107512 ** Disconnect all the virtual table objects in the sqlite3.pDisconnect list.
107513 **
107514 ** This function may only be called when the mutexes associated with all
107515 ** shared b-tree databases opened using connection db are held by the
107516 ** caller. This is done to protect the sqlite3.pDisconnect list. The
107517 ** sqlite3.pDisconnect list is accessed only as follows:
107518 **
107519 **   1) By this function. In this case, all BtShared mutexes and the mutex
107520 **      associated with the database handle itself must be held.
107521 **
107522 **   2) By function vtabDisconnectAll(), when it adds a VTable entry to
107523 **      the sqlite3.pDisconnect list. In this case either the BtShared mutex
107524 **      associated with the database the virtual table is stored in is held
107525 **      or, if the virtual table is stored in a non-sharable database, then
107526 **      the database handle mutex is held.
107527 **
107528 ** As a result, a sqlite3.pDisconnect cannot be accessed simultaneously
107529 ** by multiple threads. It is thread-safe.
107530 */
107531 SQLITE_PRIVATE void sqlite3VtabUnlockList(sqlite3 *db){
107532   VTable *p = db->pDisconnect;
107533   db->pDisconnect = 0;
107534 
107535   assert( sqlite3BtreeHoldsAllMutexes(db) );
107536   assert( sqlite3_mutex_held(db->mutex) );
107537 
107538   if( p ){
107539     sqlite3ExpirePreparedStatements(db);
107540     do {
107541       VTable *pNext = p->pNext;
107542       sqlite3VtabUnlock(p);
107543       p = pNext;
107544     }while( p );
107545   }
107546 }
107547 
107548 /*
107549 ** Clear any and all virtual-table information from the Table record.
107550 ** This routine is called, for example, just before deleting the Table
107551 ** record.
107552 **
107553 ** Since it is a virtual-table, the Table structure contains a pointer
107554 ** to the head of a linked list of VTable structures. Each VTable
107555 ** structure is associated with a single sqlite3* user of the schema.
107556 ** The reference count of the VTable structure associated with database
107557 ** connection db is decremented immediately (which may lead to the
107558 ** structure being xDisconnected and free). Any other VTable structures
107559 ** in the list are moved to the sqlite3.pDisconnect list of the associated
107560 ** database connection.
107561 */
107562 SQLITE_PRIVATE void sqlite3VtabClear(sqlite3 *db, Table *p){
107563   if( !db || db->pnBytesFreed==0 ) vtabDisconnectAll(0, p);
107564   if( p->azModuleArg ){
107565     int i;
107566     for(i=0; i<p->nModuleArg; i++){
107567       if( i!=1 ) sqlite3DbFree(db, p->azModuleArg[i]);
107568     }
107569     sqlite3DbFree(db, p->azModuleArg);
107570   }
107571 }
107572 
107573 /*
107574 ** Add a new module argument to pTable->azModuleArg[].
107575 ** The string is not copied - the pointer is stored.  The
107576 ** string will be freed automatically when the table is
107577 ** deleted.
107578 */
107579 static void addModuleArgument(sqlite3 *db, Table *pTable, char *zArg){
107580   int i = pTable->nModuleArg++;
107581   int nBytes = sizeof(char *)*(1+pTable->nModuleArg);
107582   char **azModuleArg;
107583   azModuleArg = sqlite3DbRealloc(db, pTable->azModuleArg, nBytes);
107584   if( azModuleArg==0 ){
107585     int j;
107586     for(j=0; j<i; j++){
107587       sqlite3DbFree(db, pTable->azModuleArg[j]);
107588     }
107589     sqlite3DbFree(db, zArg);
107590     sqlite3DbFree(db, pTable->azModuleArg);
107591     pTable->nModuleArg = 0;
107592   }else{
107593     azModuleArg[i] = zArg;
107594     azModuleArg[i+1] = 0;
107595   }
107596   pTable->azModuleArg = azModuleArg;
107597 }
107598 
107599 /*
107600 ** The parser calls this routine when it first sees a CREATE VIRTUAL TABLE
107601 ** statement.  The module name has been parsed, but the optional list
107602 ** of parameters that follow the module name are still pending.
107603 */
107604 SQLITE_PRIVATE void sqlite3VtabBeginParse(
107605   Parse *pParse,        /* Parsing context */
107606   Token *pName1,        /* Name of new table, or database name */
107607   Token *pName2,        /* Name of new table or NULL */
107608   Token *pModuleName,   /* Name of the module for the virtual table */
107609   int ifNotExists       /* No error if the table already exists */
107610 ){
107611   int iDb;              /* The database the table is being created in */
107612   Table *pTable;        /* The new virtual table */
107613   sqlite3 *db;          /* Database connection */
107614 
107615   sqlite3StartTable(pParse, pName1, pName2, 0, 0, 1, ifNotExists);
107616   pTable = pParse->pNewTable;
107617   if( pTable==0 ) return;
107618   assert( 0==pTable->pIndex );
107619 
107620   db = pParse->db;
107621   iDb = sqlite3SchemaToIndex(db, pTable->pSchema);
107622   assert( iDb>=0 );
107623 
107624   pTable->tabFlags |= TF_Virtual;
107625   pTable->nModuleArg = 0;
107626   addModuleArgument(db, pTable, sqlite3NameFromToken(db, pModuleName));
107627   addModuleArgument(db, pTable, 0);
107628   addModuleArgument(db, pTable, sqlite3DbStrDup(db, pTable->zName));
107629   pParse->sNameToken.n = (int)(&pModuleName->z[pModuleName->n] - pName1->z);
107630 
107631 #ifndef SQLITE_OMIT_AUTHORIZATION
107632   /* Creating a virtual table invokes the authorization callback twice.
107633   ** The first invocation, to obtain permission to INSERT a row into the
107634   ** sqlite_master table, has already been made by sqlite3StartTable().
107635   ** The second call, to obtain permission to create the table, is made now.
107636   */
107637   if( pTable->azModuleArg ){
107638     sqlite3AuthCheck(pParse, SQLITE_CREATE_VTABLE, pTable->zName,
107639             pTable->azModuleArg[0], pParse->db->aDb[iDb].zName);
107640   }
107641 #endif
107642 }
107643 
107644 /*
107645 ** This routine takes the module argument that has been accumulating
107646 ** in pParse->zArg[] and appends it to the list of arguments on the
107647 ** virtual table currently under construction in pParse->pTable.
107648 */
107649 static void addArgumentToVtab(Parse *pParse){
107650   if( pParse->sArg.z && pParse->pNewTable ){
107651     const char *z = (const char*)pParse->sArg.z;
107652     int n = pParse->sArg.n;
107653     sqlite3 *db = pParse->db;
107654     addModuleArgument(db, pParse->pNewTable, sqlite3DbStrNDup(db, z, n));
107655   }
107656 }
107657 
107658 /*
107659 ** The parser calls this routine after the CREATE VIRTUAL TABLE statement
107660 ** has been completely parsed.
107661 */
107662 SQLITE_PRIVATE void sqlite3VtabFinishParse(Parse *pParse, Token *pEnd){
107663   Table *pTab = pParse->pNewTable;  /* The table being constructed */
107664   sqlite3 *db = pParse->db;         /* The database connection */
107665 
107666   if( pTab==0 ) return;
107667   addArgumentToVtab(pParse);
107668   pParse->sArg.z = 0;
107669   if( pTab->nModuleArg<1 ) return;
107670 
107671   /* If the CREATE VIRTUAL TABLE statement is being entered for the
107672   ** first time (in other words if the virtual table is actually being
107673   ** created now instead of just being read out of sqlite_master) then
107674   ** do additional initialization work and store the statement text
107675   ** in the sqlite_master table.
107676   */
107677   if( !db->init.busy ){
107678     char *zStmt;
107679     char *zWhere;
107680     int iDb;
107681     Vdbe *v;
107682 
107683     /* Compute the complete text of the CREATE VIRTUAL TABLE statement */
107684     if( pEnd ){
107685       pParse->sNameToken.n = (int)(pEnd->z - pParse->sNameToken.z) + pEnd->n;
107686     }
107687     zStmt = sqlite3MPrintf(db, "CREATE VIRTUAL TABLE %T", &pParse->sNameToken);
107688 
107689     /* A slot for the record has already been allocated in the
107690     ** SQLITE_MASTER table.  We just need to update that slot with all
107691     ** the information we've collected.
107692     **
107693     ** The VM register number pParse->regRowid holds the rowid of an
107694     ** entry in the sqlite_master table tht was created for this vtab
107695     ** by sqlite3StartTable().
107696     */
107697     iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
107698     sqlite3NestedParse(pParse,
107699       "UPDATE %Q.%s "
107700          "SET type='table', name=%Q, tbl_name=%Q, rootpage=0, sql=%Q "
107701        "WHERE rowid=#%d",
107702       db->aDb[iDb].zName, SCHEMA_TABLE(iDb),
107703       pTab->zName,
107704       pTab->zName,
107705       zStmt,
107706       pParse->regRowid
107707     );
107708     sqlite3DbFree(db, zStmt);
107709     v = sqlite3GetVdbe(pParse);
107710     sqlite3ChangeCookie(pParse, iDb);
107711 
107712     sqlite3VdbeAddOp2(v, OP_Expire, 0, 0);
107713     zWhere = sqlite3MPrintf(db, "name='%q' AND type='table'", pTab->zName);
107714     sqlite3VdbeAddParseSchemaOp(v, iDb, zWhere);
107715     sqlite3VdbeAddOp4(v, OP_VCreate, iDb, 0, 0,
107716                          pTab->zName, sqlite3Strlen30(pTab->zName) + 1);
107717   }
107718 
107719   /* If we are rereading the sqlite_master table create the in-memory
107720   ** record of the table. The xConnect() method is not called until
107721   ** the first time the virtual table is used in an SQL statement. This
107722   ** allows a schema that contains virtual tables to be loaded before
107723   ** the required virtual table implementations are registered.  */
107724   else {
107725     Table *pOld;
107726     Schema *pSchema = pTab->pSchema;
107727     const char *zName = pTab->zName;
107728     int nName = sqlite3Strlen30(zName);
107729     assert( sqlite3SchemaMutexHeld(db, 0, pSchema) );
107730     pOld = sqlite3HashInsert(&pSchema->tblHash, zName, nName, pTab);
107731     if( pOld ){
107732       db->mallocFailed = 1;
107733       assert( pTab==pOld );  /* Malloc must have failed inside HashInsert() */
107734       return;
107735     }
107736     pParse->pNewTable = 0;
107737   }
107738 }
107739 
107740 /*
107741 ** The parser calls this routine when it sees the first token
107742 ** of an argument to the module name in a CREATE VIRTUAL TABLE statement.
107743 */
107744 SQLITE_PRIVATE void sqlite3VtabArgInit(Parse *pParse){
107745   addArgumentToVtab(pParse);
107746   pParse->sArg.z = 0;
107747   pParse->sArg.n = 0;
107748 }
107749 
107750 /*
107751 ** The parser calls this routine for each token after the first token
107752 ** in an argument to the module name in a CREATE VIRTUAL TABLE statement.
107753 */
107754 SQLITE_PRIVATE void sqlite3VtabArgExtend(Parse *pParse, Token *p){
107755   Token *pArg = &pParse->sArg;
107756   if( pArg->z==0 ){
107757     pArg->z = p->z;
107758     pArg->n = p->n;
107759   }else{
107760     assert(pArg->z < p->z);
107761     pArg->n = (int)(&p->z[p->n] - pArg->z);
107762   }
107763 }
107764 
107765 /*
107766 ** Invoke a virtual table constructor (either xCreate or xConnect). The
107767 ** pointer to the function to invoke is passed as the fourth parameter
107768 ** to this procedure.
107769 */
107770 static int vtabCallConstructor(
107771   sqlite3 *db,
107772   Table *pTab,
107773   Module *pMod,
107774   int (*xConstruct)(sqlite3*,void*,int,const char*const*,sqlite3_vtab**,char**),
107775   char **pzErr
107776 ){
107777   VtabCtx sCtx, *pPriorCtx;
107778   VTable *pVTable;
107779   int rc;
107780   const char *const*azArg = (const char *const*)pTab->azModuleArg;
107781   int nArg = pTab->nModuleArg;
107782   char *zErr = 0;
107783   char *zModuleName = sqlite3MPrintf(db, "%s", pTab->zName);
107784   int iDb;
107785 
107786   if( !zModuleName ){
107787     return SQLITE_NOMEM;
107788   }
107789 
107790   pVTable = sqlite3DbMallocZero(db, sizeof(VTable));
107791   if( !pVTable ){
107792     sqlite3DbFree(db, zModuleName);
107793     return SQLITE_NOMEM;
107794   }
107795   pVTable->db = db;
107796   pVTable->pMod = pMod;
107797 
107798   iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
107799   pTab->azModuleArg[1] = db->aDb[iDb].zName;
107800 
107801   /* Invoke the virtual table constructor */
107802   assert( &db->pVtabCtx );
107803   assert( xConstruct );
107804   sCtx.pTab = pTab;
107805   sCtx.pVTable = pVTable;
107806   pPriorCtx = db->pVtabCtx;
107807   db->pVtabCtx = &sCtx;
107808   rc = xConstruct(db, pMod->pAux, nArg, azArg, &pVTable->pVtab, &zErr);
107809   db->pVtabCtx = pPriorCtx;
107810   if( rc==SQLITE_NOMEM ) db->mallocFailed = 1;
107811 
107812   if( SQLITE_OK!=rc ){
107813     if( zErr==0 ){
107814       *pzErr = sqlite3MPrintf(db, "vtable constructor failed: %s", zModuleName);
107815     }else {
107816       *pzErr = sqlite3MPrintf(db, "%s", zErr);
107817       sqlite3_free(zErr);
107818     }
107819     sqlite3DbFree(db, pVTable);
107820   }else if( ALWAYS(pVTable->pVtab) ){
107821     /* Justification of ALWAYS():  A correct vtab constructor must allocate
107822     ** the sqlite3_vtab object if successful.  */
107823     pVTable->pVtab->pModule = pMod->pModule;
107824     pVTable->nRef = 1;
107825     if( sCtx.pTab ){
107826       const char *zFormat = "vtable constructor did not declare schema: %s";
107827       *pzErr = sqlite3MPrintf(db, zFormat, pTab->zName);
107828       sqlite3VtabUnlock(pVTable);
107829       rc = SQLITE_ERROR;
107830     }else{
107831       int iCol;
107832       /* If everything went according to plan, link the new VTable structure
107833       ** into the linked list headed by pTab->pVTable. Then loop through the
107834       ** columns of the table to see if any of them contain the token "hidden".
107835       ** If so, set the Column COLFLAG_HIDDEN flag and remove the token from
107836       ** the type string.  */
107837       pVTable->pNext = pTab->pVTable;
107838       pTab->pVTable = pVTable;
107839 
107840       for(iCol=0; iCol<pTab->nCol; iCol++){
107841         char *zType = pTab->aCol[iCol].zType;
107842         int nType;
107843         int i = 0;
107844         if( !zType ) continue;
107845         nType = sqlite3Strlen30(zType);
107846         if( sqlite3StrNICmp("hidden", zType, 6)||(zType[6] && zType[6]!=' ') ){
107847           for(i=0; i<nType; i++){
107848             if( (0==sqlite3StrNICmp(" hidden", &zType[i], 7))
107849              && (zType[i+7]=='\0' || zType[i+7]==' ')
107850             ){
107851               i++;
107852               break;
107853             }
107854           }
107855         }
107856         if( i<nType ){
107857           int j;
107858           int nDel = 6 + (zType[i+6] ? 1 : 0);
107859           for(j=i; (j+nDel)<=nType; j++){
107860             zType[j] = zType[j+nDel];
107861           }
107862           if( zType[i]=='\0' && i>0 ){
107863             assert(zType[i-1]==' ');
107864             zType[i-1] = '\0';
107865           }
107866           pTab->aCol[iCol].colFlags |= COLFLAG_HIDDEN;
107867         }
107868       }
107869     }
107870   }
107871 
107872   sqlite3DbFree(db, zModuleName);
107873   return rc;
107874 }
107875 
107876 /*
107877 ** This function is invoked by the parser to call the xConnect() method
107878 ** of the virtual table pTab. If an error occurs, an error code is returned
107879 ** and an error left in pParse.
107880 **
107881 ** This call is a no-op if table pTab is not a virtual table.
107882 */
107883 SQLITE_PRIVATE int sqlite3VtabCallConnect(Parse *pParse, Table *pTab){
107884   sqlite3 *db = pParse->db;
107885   const char *zMod;
107886   Module *pMod;
107887   int rc;
107888 
107889   assert( pTab );
107890   if( (pTab->tabFlags & TF_Virtual)==0 || sqlite3GetVTable(db, pTab) ){
107891     return SQLITE_OK;
107892   }
107893 
107894   /* Locate the required virtual table module */
107895   zMod = pTab->azModuleArg[0];
107896   pMod = (Module*)sqlite3HashFind(&db->aModule, zMod, sqlite3Strlen30(zMod));
107897 
107898   if( !pMod ){
107899     const char *zModule = pTab->azModuleArg[0];
107900     sqlite3ErrorMsg(pParse, "no such module: %s", zModule);
107901     rc = SQLITE_ERROR;
107902   }else{
107903     char *zErr = 0;
107904     rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xConnect, &zErr);
107905     if( rc!=SQLITE_OK ){
107906       sqlite3ErrorMsg(pParse, "%s", zErr);
107907     }
107908     sqlite3DbFree(db, zErr);
107909   }
107910 
107911   return rc;
107912 }
107913 /*
107914 ** Grow the db->aVTrans[] array so that there is room for at least one
107915 ** more v-table. Return SQLITE_NOMEM if a malloc fails, or SQLITE_OK otherwise.
107916 */
107917 static int growVTrans(sqlite3 *db){
107918   const int ARRAY_INCR = 5;
107919 
107920   /* Grow the sqlite3.aVTrans array if required */
107921   if( (db->nVTrans%ARRAY_INCR)==0 ){
107922     VTable **aVTrans;
107923     int nBytes = sizeof(sqlite3_vtab *) * (db->nVTrans + ARRAY_INCR);
107924     aVTrans = sqlite3DbRealloc(db, (void *)db->aVTrans, nBytes);
107925     if( !aVTrans ){
107926       return SQLITE_NOMEM;
107927     }
107928     memset(&aVTrans[db->nVTrans], 0, sizeof(sqlite3_vtab *)*ARRAY_INCR);
107929     db->aVTrans = aVTrans;
107930   }
107931 
107932   return SQLITE_OK;
107933 }
107934 
107935 /*
107936 ** Add the virtual table pVTab to the array sqlite3.aVTrans[]. Space should
107937 ** have already been reserved using growVTrans().
107938 */
107939 static void addToVTrans(sqlite3 *db, VTable *pVTab){
107940   /* Add pVtab to the end of sqlite3.aVTrans */
107941   db->aVTrans[db->nVTrans++] = pVTab;
107942   sqlite3VtabLock(pVTab);
107943 }
107944 
107945 /*
107946 ** This function is invoked by the vdbe to call the xCreate method
107947 ** of the virtual table named zTab in database iDb.
107948 **
107949 ** If an error occurs, *pzErr is set to point an an English language
107950 ** description of the error and an SQLITE_XXX error code is returned.
107951 ** In this case the caller must call sqlite3DbFree(db, ) on *pzErr.
107952 */
107953 SQLITE_PRIVATE int sqlite3VtabCallCreate(sqlite3 *db, int iDb, const char *zTab, char **pzErr){
107954   int rc = SQLITE_OK;
107955   Table *pTab;
107956   Module *pMod;
107957   const char *zMod;
107958 
107959   pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zName);
107960   assert( pTab && (pTab->tabFlags & TF_Virtual)!=0 && !pTab->pVTable );
107961 
107962   /* Locate the required virtual table module */
107963   zMod = pTab->azModuleArg[0];
107964   pMod = (Module*)sqlite3HashFind(&db->aModule, zMod, sqlite3Strlen30(zMod));
107965 
107966   /* If the module has been registered and includes a Create method,
107967   ** invoke it now. If the module has not been registered, return an
107968   ** error. Otherwise, do nothing.
107969   */
107970   if( !pMod ){
107971     *pzErr = sqlite3MPrintf(db, "no such module: %s", zMod);
107972     rc = SQLITE_ERROR;
107973   }else{
107974     rc = vtabCallConstructor(db, pTab, pMod, pMod->pModule->xCreate, pzErr);
107975   }
107976 
107977   /* Justification of ALWAYS():  The xConstructor method is required to
107978   ** create a valid sqlite3_vtab if it returns SQLITE_OK. */
107979   if( rc==SQLITE_OK && ALWAYS(sqlite3GetVTable(db, pTab)) ){
107980     rc = growVTrans(db);
107981     if( rc==SQLITE_OK ){
107982       addToVTrans(db, sqlite3GetVTable(db, pTab));
107983     }
107984   }
107985 
107986   return rc;
107987 }
107988 
107989 /*
107990 ** This function is used to set the schema of a virtual table.  It is only
107991 ** valid to call this function from within the xCreate() or xConnect() of a
107992 ** virtual table module.
107993 */
107994 SQLITE_API int sqlite3_declare_vtab(sqlite3 *db, const char *zCreateTable){
107995   Parse *pParse;
107996 
107997   int rc = SQLITE_OK;
107998   Table *pTab;
107999   char *zErr = 0;
108000 
108001   sqlite3_mutex_enter(db->mutex);
108002   if( !db->pVtabCtx || !(pTab = db->pVtabCtx->pTab) ){
108003     sqlite3Error(db, SQLITE_MISUSE, 0);
108004     sqlite3_mutex_leave(db->mutex);
108005     return SQLITE_MISUSE_BKPT;
108006   }
108007   assert( (pTab->tabFlags & TF_Virtual)!=0 );
108008 
108009   pParse = sqlite3StackAllocZero(db, sizeof(*pParse));
108010   if( pParse==0 ){
108011     rc = SQLITE_NOMEM;
108012   }else{
108013     pParse->declareVtab = 1;
108014     pParse->db = db;
108015     pParse->nQueryLoop = 1;
108016 
108017     if( SQLITE_OK==sqlite3RunParser(pParse, zCreateTable, &zErr)
108018      && pParse->pNewTable
108019      && !db->mallocFailed
108020      && !pParse->pNewTable->pSelect
108021      && (pParse->pNewTable->tabFlags & TF_Virtual)==0
108022     ){
108023       if( !pTab->aCol ){
108024         pTab->aCol = pParse->pNewTable->aCol;
108025         pTab->nCol = pParse->pNewTable->nCol;
108026         pParse->pNewTable->nCol = 0;
108027         pParse->pNewTable->aCol = 0;
108028       }
108029       db->pVtabCtx->pTab = 0;
108030     }else{
108031       sqlite3Error(db, SQLITE_ERROR, (zErr ? "%s" : 0), zErr);
108032       sqlite3DbFree(db, zErr);
108033       rc = SQLITE_ERROR;
108034     }
108035     pParse->declareVtab = 0;
108036 
108037     if( pParse->pVdbe ){
108038       sqlite3VdbeFinalize(pParse->pVdbe);
108039     }
108040     sqlite3DeleteTable(db, pParse->pNewTable);
108041     sqlite3ParserReset(pParse);
108042     sqlite3StackFree(db, pParse);
108043   }
108044 
108045   assert( (rc&0xff)==rc );
108046   rc = sqlite3ApiExit(db, rc);
108047   sqlite3_mutex_leave(db->mutex);
108048   return rc;
108049 }
108050 
108051 /*
108052 ** This function is invoked by the vdbe to call the xDestroy method
108053 ** of the virtual table named zTab in database iDb. This occurs
108054 ** when a DROP TABLE is mentioned.
108055 **
108056 ** This call is a no-op if zTab is not a virtual table.
108057 */
108058 SQLITE_PRIVATE int sqlite3VtabCallDestroy(sqlite3 *db, int iDb, const char *zTab){
108059   int rc = SQLITE_OK;
108060   Table *pTab;
108061 
108062   pTab = sqlite3FindTable(db, zTab, db->aDb[iDb].zName);
108063   if( ALWAYS(pTab!=0 && pTab->pVTable!=0) ){
108064     VTable *p = vtabDisconnectAll(db, pTab);
108065 
108066     assert( rc==SQLITE_OK );
108067     rc = p->pMod->pModule->xDestroy(p->pVtab);
108068 
108069     /* Remove the sqlite3_vtab* from the aVTrans[] array, if applicable */
108070     if( rc==SQLITE_OK ){
108071       assert( pTab->pVTable==p && p->pNext==0 );
108072       p->pVtab = 0;
108073       pTab->pVTable = 0;
108074       sqlite3VtabUnlock(p);
108075     }
108076   }
108077 
108078   return rc;
108079 }
108080 
108081 /*
108082 ** This function invokes either the xRollback or xCommit method
108083 ** of each of the virtual tables in the sqlite3.aVTrans array. The method
108084 ** called is identified by the second argument, "offset", which is
108085 ** the offset of the method to call in the sqlite3_module structure.
108086 **
108087 ** The array is cleared after invoking the callbacks.
108088 */
108089 static void callFinaliser(sqlite3 *db, int offset){
108090   int i;
108091   if( db->aVTrans ){
108092     for(i=0; i<db->nVTrans; i++){
108093       VTable *pVTab = db->aVTrans[i];
108094       sqlite3_vtab *p = pVTab->pVtab;
108095       if( p ){
108096         int (*x)(sqlite3_vtab *);
108097         x = *(int (**)(sqlite3_vtab *))((char *)p->pModule + offset);
108098         if( x ) x(p);
108099       }
108100       pVTab->iSavepoint = 0;
108101       sqlite3VtabUnlock(pVTab);
108102     }
108103     sqlite3DbFree(db, db->aVTrans);
108104     db->nVTrans = 0;
108105     db->aVTrans = 0;
108106   }
108107 }
108108 
108109 /*
108110 ** Invoke the xSync method of all virtual tables in the sqlite3.aVTrans
108111 ** array. Return the error code for the first error that occurs, or
108112 ** SQLITE_OK if all xSync operations are successful.
108113 **
108114 ** If an error message is available, leave it in p->zErrMsg.
108115 */
108116 SQLITE_PRIVATE int sqlite3VtabSync(sqlite3 *db, Vdbe *p){
108117   int i;
108118   int rc = SQLITE_OK;
108119   VTable **aVTrans = db->aVTrans;
108120 
108121   db->aVTrans = 0;
108122   for(i=0; rc==SQLITE_OK && i<db->nVTrans; i++){
108123     int (*x)(sqlite3_vtab *);
108124     sqlite3_vtab *pVtab = aVTrans[i]->pVtab;
108125     if( pVtab && (x = pVtab->pModule->xSync)!=0 ){
108126       rc = x(pVtab);
108127       sqlite3VtabImportErrmsg(p, pVtab);
108128     }
108129   }
108130   db->aVTrans = aVTrans;
108131   return rc;
108132 }
108133 
108134 /*
108135 ** Invoke the xRollback method of all virtual tables in the
108136 ** sqlite3.aVTrans array. Then clear the array itself.
108137 */
108138 SQLITE_PRIVATE int sqlite3VtabRollback(sqlite3 *db){
108139   callFinaliser(db, offsetof(sqlite3_module,xRollback));
108140   return SQLITE_OK;
108141 }
108142 
108143 /*
108144 ** Invoke the xCommit method of all virtual tables in the
108145 ** sqlite3.aVTrans array. Then clear the array itself.
108146 */
108147 SQLITE_PRIVATE int sqlite3VtabCommit(sqlite3 *db){
108148   callFinaliser(db, offsetof(sqlite3_module,xCommit));
108149   return SQLITE_OK;
108150 }
108151 
108152 /*
108153 ** If the virtual table pVtab supports the transaction interface
108154 ** (xBegin/xRollback/xCommit and optionally xSync) and a transaction is
108155 ** not currently open, invoke the xBegin method now.
108156 **
108157 ** If the xBegin call is successful, place the sqlite3_vtab pointer
108158 ** in the sqlite3.aVTrans array.
108159 */
108160 SQLITE_PRIVATE int sqlite3VtabBegin(sqlite3 *db, VTable *pVTab){
108161   int rc = SQLITE_OK;
108162   const sqlite3_module *pModule;
108163 
108164   /* Special case: If db->aVTrans is NULL and db->nVTrans is greater
108165   ** than zero, then this function is being called from within a
108166   ** virtual module xSync() callback. It is illegal to write to
108167   ** virtual module tables in this case, so return SQLITE_LOCKED.
108168   */
108169   if( sqlite3VtabInSync(db) ){
108170     return SQLITE_LOCKED;
108171   }
108172   if( !pVTab ){
108173     return SQLITE_OK;
108174   }
108175   pModule = pVTab->pVtab->pModule;
108176 
108177   if( pModule->xBegin ){
108178     int i;
108179 
108180     /* If pVtab is already in the aVTrans array, return early */
108181     for(i=0; i<db->nVTrans; i++){
108182       if( db->aVTrans[i]==pVTab ){
108183         return SQLITE_OK;
108184       }
108185     }
108186 
108187     /* Invoke the xBegin method. If successful, add the vtab to the
108188     ** sqlite3.aVTrans[] array. */
108189     rc = growVTrans(db);
108190     if( rc==SQLITE_OK ){
108191       rc = pModule->xBegin(pVTab->pVtab);
108192       if( rc==SQLITE_OK ){
108193         addToVTrans(db, pVTab);
108194       }
108195     }
108196   }
108197   return rc;
108198 }
108199 
108200 /*
108201 ** Invoke either the xSavepoint, xRollbackTo or xRelease method of all
108202 ** virtual tables that currently have an open transaction. Pass iSavepoint
108203 ** as the second argument to the virtual table method invoked.
108204 **
108205 ** If op is SAVEPOINT_BEGIN, the xSavepoint method is invoked. If it is
108206 ** SAVEPOINT_ROLLBACK, the xRollbackTo method. Otherwise, if op is
108207 ** SAVEPOINT_RELEASE, then the xRelease method of each virtual table with
108208 ** an open transaction is invoked.
108209 **
108210 ** If any virtual table method returns an error code other than SQLITE_OK,
108211 ** processing is abandoned and the error returned to the caller of this
108212 ** function immediately. If all calls to virtual table methods are successful,
108213 ** SQLITE_OK is returned.
108214 */
108215 SQLITE_PRIVATE int sqlite3VtabSavepoint(sqlite3 *db, int op, int iSavepoint){
108216   int rc = SQLITE_OK;
108217 
108218   assert( op==SAVEPOINT_RELEASE||op==SAVEPOINT_ROLLBACK||op==SAVEPOINT_BEGIN );
108219   assert( iSavepoint>=0 );
108220   if( db->aVTrans ){
108221     int i;
108222     for(i=0; rc==SQLITE_OK && i<db->nVTrans; i++){
108223       VTable *pVTab = db->aVTrans[i];
108224       const sqlite3_module *pMod = pVTab->pMod->pModule;
108225       if( pVTab->pVtab && pMod->iVersion>=2 ){
108226         int (*xMethod)(sqlite3_vtab *, int);
108227         switch( op ){
108228           case SAVEPOINT_BEGIN:
108229             xMethod = pMod->xSavepoint;
108230             pVTab->iSavepoint = iSavepoint+1;
108231             break;
108232           case SAVEPOINT_ROLLBACK:
108233             xMethod = pMod->xRollbackTo;
108234             break;
108235           default:
108236             xMethod = pMod->xRelease;
108237             break;
108238         }
108239         if( xMethod && pVTab->iSavepoint>iSavepoint ){
108240           rc = xMethod(pVTab->pVtab, iSavepoint);
108241         }
108242       }
108243     }
108244   }
108245   return rc;
108246 }
108247 
108248 /*
108249 ** The first parameter (pDef) is a function implementation.  The
108250 ** second parameter (pExpr) is the first argument to this function.
108251 ** If pExpr is a column in a virtual table, then let the virtual
108252 ** table implementation have an opportunity to overload the function.
108253 **
108254 ** This routine is used to allow virtual table implementations to
108255 ** overload MATCH, LIKE, GLOB, and REGEXP operators.
108256 **
108257 ** Return either the pDef argument (indicating no change) or a
108258 ** new FuncDef structure that is marked as ephemeral using the
108259 ** SQLITE_FUNC_EPHEM flag.
108260 */
108261 SQLITE_PRIVATE FuncDef *sqlite3VtabOverloadFunction(
108262   sqlite3 *db,    /* Database connection for reporting malloc problems */
108263   FuncDef *pDef,  /* Function to possibly overload */
108264   int nArg,       /* Number of arguments to the function */
108265   Expr *pExpr     /* First argument to the function */
108266 ){
108267   Table *pTab;
108268   sqlite3_vtab *pVtab;
108269   sqlite3_module *pMod;
108270   void (*xFunc)(sqlite3_context*,int,sqlite3_value**) = 0;
108271   void *pArg = 0;
108272   FuncDef *pNew;
108273   int rc = 0;
108274   char *zLowerName;
108275   unsigned char *z;
108276 
108277 
108278   /* Check to see the left operand is a column in a virtual table */
108279   if( NEVER(pExpr==0) ) return pDef;
108280   if( pExpr->op!=TK_COLUMN ) return pDef;
108281   pTab = pExpr->pTab;
108282   if( NEVER(pTab==0) ) return pDef;
108283   if( (pTab->tabFlags & TF_Virtual)==0 ) return pDef;
108284   pVtab = sqlite3GetVTable(db, pTab)->pVtab;
108285   assert( pVtab!=0 );
108286   assert( pVtab->pModule!=0 );
108287   pMod = (sqlite3_module *)pVtab->pModule;
108288   if( pMod->xFindFunction==0 ) return pDef;
108289 
108290   /* Call the xFindFunction method on the virtual table implementation
108291   ** to see if the implementation wants to overload this function
108292   */
108293   zLowerName = sqlite3DbStrDup(db, pDef->zName);
108294   if( zLowerName ){
108295     for(z=(unsigned char*)zLowerName; *z; z++){
108296       *z = sqlite3UpperToLower[*z];
108297     }
108298     rc = pMod->xFindFunction(pVtab, nArg, zLowerName, &xFunc, &pArg);
108299     sqlite3DbFree(db, zLowerName);
108300   }
108301   if( rc==0 ){
108302     return pDef;
108303   }
108304 
108305   /* Create a new ephemeral function definition for the overloaded
108306   ** function */
108307   pNew = sqlite3DbMallocZero(db, sizeof(*pNew)
108308                              + sqlite3Strlen30(pDef->zName) + 1);
108309   if( pNew==0 ){
108310     return pDef;
108311   }
108312   *pNew = *pDef;
108313   pNew->zName = (char *)&pNew[1];
108314   memcpy(pNew->zName, pDef->zName, sqlite3Strlen30(pDef->zName)+1);
108315   pNew->xFunc = xFunc;
108316   pNew->pUserData = pArg;
108317   pNew->funcFlags |= SQLITE_FUNC_EPHEM;
108318   return pNew;
108319 }
108320 
108321 /*
108322 ** Make sure virtual table pTab is contained in the pParse->apVirtualLock[]
108323 ** array so that an OP_VBegin will get generated for it.  Add pTab to the
108324 ** array if it is missing.  If pTab is already in the array, this routine
108325 ** is a no-op.
108326 */
108327 SQLITE_PRIVATE void sqlite3VtabMakeWritable(Parse *pParse, Table *pTab){
108328   Parse *pToplevel = sqlite3ParseToplevel(pParse);
108329   int i, n;
108330   Table **apVtabLock;
108331 
108332   assert( IsVirtual(pTab) );
108333   for(i=0; i<pToplevel->nVtabLock; i++){
108334     if( pTab==pToplevel->apVtabLock[i] ) return;
108335   }
108336   n = (pToplevel->nVtabLock+1)*sizeof(pToplevel->apVtabLock[0]);
108337   apVtabLock = sqlite3_realloc(pToplevel->apVtabLock, n);
108338   if( apVtabLock ){
108339     pToplevel->apVtabLock = apVtabLock;
108340     pToplevel->apVtabLock[pToplevel->nVtabLock++] = pTab;
108341   }else{
108342     pToplevel->db->mallocFailed = 1;
108343   }
108344 }
108345 
108346 /*
108347 ** Return the ON CONFLICT resolution mode in effect for the virtual
108348 ** table update operation currently in progress.
108349 **
108350 ** The results of this routine are undefined unless it is called from
108351 ** within an xUpdate method.
108352 */
108353 SQLITE_API int sqlite3_vtab_on_conflict(sqlite3 *db){
108354   static const unsigned char aMap[] = {
108355     SQLITE_ROLLBACK, SQLITE_ABORT, SQLITE_FAIL, SQLITE_IGNORE, SQLITE_REPLACE
108356   };
108357   assert( OE_Rollback==1 && OE_Abort==2 && OE_Fail==3 );
108358   assert( OE_Ignore==4 && OE_Replace==5 );
108359   assert( db->vtabOnConflict>=1 && db->vtabOnConflict<=5 );
108360   return (int)aMap[db->vtabOnConflict-1];
108361 }
108362 
108363 /*
108364 ** Call from within the xCreate() or xConnect() methods to provide
108365 ** the SQLite core with additional information about the behavior
108366 ** of the virtual table being implemented.
108367 */
108368 SQLITE_API int sqlite3_vtab_config(sqlite3 *db, int op, ...){
108369   va_list ap;
108370   int rc = SQLITE_OK;
108371 
108372   sqlite3_mutex_enter(db->mutex);
108373 
108374   va_start(ap, op);
108375   switch( op ){
108376     case SQLITE_VTAB_CONSTRAINT_SUPPORT: {
108377       VtabCtx *p = db->pVtabCtx;
108378       if( !p ){
108379         rc = SQLITE_MISUSE_BKPT;
108380       }else{
108381         assert( p->pTab==0 || (p->pTab->tabFlags & TF_Virtual)!=0 );
108382         p->pVTable->bConstraint = (u8)va_arg(ap, int);
108383       }
108384       break;
108385     }
108386     default:
108387       rc = SQLITE_MISUSE_BKPT;
108388       break;
108389   }
108390   va_end(ap);
108391 
108392   if( rc!=SQLITE_OK ) sqlite3Error(db, rc, 0);
108393   sqlite3_mutex_leave(db->mutex);
108394   return rc;
108395 }
108396 
108397 #endif /* SQLITE_OMIT_VIRTUALTABLE */
108398 
108399 /************** End of vtab.c ************************************************/
108400 /************** Begin file where.c *******************************************/
108401 /*
108402 ** 2001 September 15
108403 **
108404 ** The author disclaims copyright to this source code.  In place of
108405 ** a legal notice, here is a blessing:
108406 **
108407 **    May you do good and not evil.
108408 **    May you find forgiveness for yourself and forgive others.
108409 **    May you share freely, never taking more than you give.
108410 **
108411 *************************************************************************
108412 ** This module contains C code that generates VDBE code used to process
108413 ** the WHERE clause of SQL statements.  This module is responsible for
108414 ** generating the code that loops through a table looking for applicable
108415 ** rows.  Indices are selected and used to speed the search when doing
108416 ** so is applicable.  Because this module is responsible for selecting
108417 ** indices, you might also think of this module as the "query optimizer".
108418 */
108419 /************** Include whereInt.h in the middle of where.c ******************/
108420 /************** Begin file whereInt.h ****************************************/
108421 /*
108422 ** 2013-11-12
108423 **
108424 ** The author disclaims copyright to this source code.  In place of
108425 ** a legal notice, here is a blessing:
108426 **
108427 **    May you do good and not evil.
108428 **    May you find forgiveness for yourself and forgive others.
108429 **    May you share freely, never taking more than you give.
108430 **
108431 *************************************************************************
108432 **
108433 ** This file contains structure and macro definitions for the query
108434 ** planner logic in "where.c".  These definitions are broken out into
108435 ** a separate source file for easier editing.
108436 */
108437 
108438 /*
108439 ** Trace output macros
108440 */
108441 #if defined(SQLITE_TEST) || defined(SQLITE_DEBUG)
108442 /***/ int sqlite3WhereTrace = 0;
108443 #endif
108444 #if defined(SQLITE_DEBUG) \
108445     && (defined(SQLITE_TEST) || defined(SQLITE_ENABLE_WHERETRACE))
108446 # define WHERETRACE(K,X)  if(sqlite3WhereTrace&(K)) sqlite3DebugPrintf X
108447 # define WHERETRACE_ENABLED 1
108448 #else
108449 # define WHERETRACE(K,X)
108450 #endif
108451 
108452 /* Forward references
108453 */
108454 typedef struct WhereClause WhereClause;
108455 typedef struct WhereMaskSet WhereMaskSet;
108456 typedef struct WhereOrInfo WhereOrInfo;
108457 typedef struct WhereAndInfo WhereAndInfo;
108458 typedef struct WhereLevel WhereLevel;
108459 typedef struct WhereLoop WhereLoop;
108460 typedef struct WherePath WherePath;
108461 typedef struct WhereTerm WhereTerm;
108462 typedef struct WhereLoopBuilder WhereLoopBuilder;
108463 typedef struct WhereScan WhereScan;
108464 typedef struct WhereOrCost WhereOrCost;
108465 typedef struct WhereOrSet WhereOrSet;
108466 
108467 /*
108468 ** This object contains information needed to implement a single nested
108469 ** loop in WHERE clause.
108470 **
108471 ** Contrast this object with WhereLoop.  This object describes the
108472 ** implementation of the loop.  WhereLoop describes the algorithm.
108473 ** This object contains a pointer to the WhereLoop algorithm as one of
108474 ** its elements.
108475 **
108476 ** The WhereInfo object contains a single instance of this object for
108477 ** each term in the FROM clause (which is to say, for each of the
108478 ** nested loops as implemented).  The order of WhereLevel objects determines
108479 ** the loop nested order, with WhereInfo.a[0] being the outer loop and
108480 ** WhereInfo.a[WhereInfo.nLevel-1] being the inner loop.
108481 */
108482 struct WhereLevel {
108483   int iLeftJoin;        /* Memory cell used to implement LEFT OUTER JOIN */
108484   int iTabCur;          /* The VDBE cursor used to access the table */
108485   int iIdxCur;          /* The VDBE cursor used to access pIdx */
108486   int addrBrk;          /* Jump here to break out of the loop */
108487   int addrNxt;          /* Jump here to start the next IN combination */
108488   int addrSkip;         /* Jump here for next iteration of skip-scan */
108489   int addrCont;         /* Jump here to continue with the next loop cycle */
108490   int addrFirst;        /* First instruction of interior of the loop */
108491   int addrBody;         /* Beginning of the body of this loop */
108492   u8 iFrom;             /* Which entry in the FROM clause */
108493   u8 op, p5;            /* Opcode and P5 of the opcode that ends the loop */
108494   int p1, p2;           /* Operands of the opcode used to ends the loop */
108495   union {               /* Information that depends on pWLoop->wsFlags */
108496     struct {
108497       int nIn;              /* Number of entries in aInLoop[] */
108498       struct InLoop {
108499         int iCur;              /* The VDBE cursor used by this IN operator */
108500         int addrInTop;         /* Top of the IN loop */
108501         u8 eEndLoopOp;         /* IN Loop terminator. OP_Next or OP_Prev */
108502       } *aInLoop;           /* Information about each nested IN operator */
108503     } in;                 /* Used when pWLoop->wsFlags&WHERE_IN_ABLE */
108504     Index *pCovidx;       /* Possible covering index for WHERE_MULTI_OR */
108505   } u;
108506   struct WhereLoop *pWLoop;  /* The selected WhereLoop object */
108507   Bitmask notReady;          /* FROM entries not usable at this level */
108508 };
108509 
108510 /*
108511 ** Each instance of this object represents an algorithm for evaluating one
108512 ** term of a join.  Every term of the FROM clause will have at least
108513 ** one corresponding WhereLoop object (unless INDEXED BY constraints
108514 ** prevent a query solution - which is an error) and many terms of the
108515 ** FROM clause will have multiple WhereLoop objects, each describing a
108516 ** potential way of implementing that FROM-clause term, together with
108517 ** dependencies and cost estimates for using the chosen algorithm.
108518 **
108519 ** Query planning consists of building up a collection of these WhereLoop
108520 ** objects, then computing a particular sequence of WhereLoop objects, with
108521 ** one WhereLoop object per FROM clause term, that satisfy all dependencies
108522 ** and that minimize the overall cost.
108523 */
108524 struct WhereLoop {
108525   Bitmask prereq;       /* Bitmask of other loops that must run first */
108526   Bitmask maskSelf;     /* Bitmask identifying table iTab */
108527 #ifdef SQLITE_DEBUG
108528   char cId;             /* Symbolic ID of this loop for debugging use */
108529 #endif
108530   u8 iTab;              /* Position in FROM clause of table for this loop */
108531   u8 iSortIdx;          /* Sorting index number.  0==None */
108532   LogEst rSetup;        /* One-time setup cost (ex: create transient index) */
108533   LogEst rRun;          /* Cost of running each loop */
108534   LogEst nOut;          /* Estimated number of output rows */
108535   union {
108536     struct {               /* Information for internal btree tables */
108537       u16 nEq;               /* Number of equality constraints */
108538       u16 nSkip;             /* Number of initial index columns to skip */
108539       Index *pIndex;         /* Index used, or NULL */
108540     } btree;
108541     struct {               /* Information for virtual tables */
108542       int idxNum;            /* Index number */
108543       u8 needFree;           /* True if sqlite3_free(idxStr) is needed */
108544       u8 isOrdered;          /* True if satisfies ORDER BY */
108545       u16 omitMask;          /* Terms that may be omitted */
108546       char *idxStr;          /* Index identifier string */
108547     } vtab;
108548   } u;
108549   u32 wsFlags;          /* WHERE_* flags describing the plan */
108550   u16 nLTerm;           /* Number of entries in aLTerm[] */
108551   /**** whereLoopXfer() copies fields above ***********************/
108552 # define WHERE_LOOP_XFER_SZ offsetof(WhereLoop,nLSlot)
108553   u16 nLSlot;           /* Number of slots allocated for aLTerm[] */
108554   WhereTerm **aLTerm;   /* WhereTerms used */
108555   WhereLoop *pNextLoop; /* Next WhereLoop object in the WhereClause */
108556   WhereTerm *aLTermSpace[4];  /* Initial aLTerm[] space */
108557 };
108558 
108559 /* This object holds the prerequisites and the cost of running a
108560 ** subquery on one operand of an OR operator in the WHERE clause.
108561 ** See WhereOrSet for additional information
108562 */
108563 struct WhereOrCost {
108564   Bitmask prereq;     /* Prerequisites */
108565   LogEst rRun;        /* Cost of running this subquery */
108566   LogEst nOut;        /* Number of outputs for this subquery */
108567 };
108568 
108569 /* The WhereOrSet object holds a set of possible WhereOrCosts that
108570 ** correspond to the subquery(s) of OR-clause processing.  Only the
108571 ** best N_OR_COST elements are retained.
108572 */
108573 #define N_OR_COST 3
108574 struct WhereOrSet {
108575   u16 n;                      /* Number of valid a[] entries */
108576   WhereOrCost a[N_OR_COST];   /* Set of best costs */
108577 };
108578 
108579 
108580 /* Forward declaration of methods */
108581 static int whereLoopResize(sqlite3*, WhereLoop*, int);
108582 
108583 /*
108584 ** Each instance of this object holds a sequence of WhereLoop objects
108585 ** that implement some or all of a query plan.
108586 **
108587 ** Think of each WhereLoop object as a node in a graph with arcs
108588 ** showing dependencies and costs for travelling between nodes.  (That is
108589 ** not a completely accurate description because WhereLoop costs are a
108590 ** vector, not a scalar, and because dependencies are many-to-one, not
108591 ** one-to-one as are graph nodes.  But it is a useful visualization aid.)
108592 ** Then a WherePath object is a path through the graph that visits some
108593 ** or all of the WhereLoop objects once.
108594 **
108595 ** The "solver" works by creating the N best WherePath objects of length
108596 ** 1.  Then using those as a basis to compute the N best WherePath objects
108597 ** of length 2.  And so forth until the length of WherePaths equals the
108598 ** number of nodes in the FROM clause.  The best (lowest cost) WherePath
108599 ** at the end is the choosen query plan.
108600 */
108601 struct WherePath {
108602   Bitmask maskLoop;     /* Bitmask of all WhereLoop objects in this path */
108603   Bitmask revLoop;      /* aLoop[]s that should be reversed for ORDER BY */
108604   LogEst nRow;          /* Estimated number of rows generated by this path */
108605   LogEst rCost;         /* Total cost of this path */
108606   u8 isOrdered;         /* True if this path satisfies ORDER BY */
108607   u8 isOrderedValid;    /* True if the isOrdered field is valid */
108608   WhereLoop **aLoop;    /* Array of WhereLoop objects implementing this path */
108609 };
108610 
108611 /*
108612 ** The query generator uses an array of instances of this structure to
108613 ** help it analyze the subexpressions of the WHERE clause.  Each WHERE
108614 ** clause subexpression is separated from the others by AND operators,
108615 ** usually, or sometimes subexpressions separated by OR.
108616 **
108617 ** All WhereTerms are collected into a single WhereClause structure.
108618 ** The following identity holds:
108619 **
108620 **        WhereTerm.pWC->a[WhereTerm.idx] == WhereTerm
108621 **
108622 ** When a term is of the form:
108623 **
108624 **              X <op> <expr>
108625 **
108626 ** where X is a column name and <op> is one of certain operators,
108627 ** then WhereTerm.leftCursor and WhereTerm.u.leftColumn record the
108628 ** cursor number and column number for X.  WhereTerm.eOperator records
108629 ** the <op> using a bitmask encoding defined by WO_xxx below.  The
108630 ** use of a bitmask encoding for the operator allows us to search
108631 ** quickly for terms that match any of several different operators.
108632 **
108633 ** A WhereTerm might also be two or more subterms connected by OR:
108634 **
108635 **         (t1.X <op> <expr>) OR (t1.Y <op> <expr>) OR ....
108636 **
108637 ** In this second case, wtFlag has the TERM_ORINFO bit set and eOperator==WO_OR
108638 ** and the WhereTerm.u.pOrInfo field points to auxiliary information that
108639 ** is collected about the OR clause.
108640 **
108641 ** If a term in the WHERE clause does not match either of the two previous
108642 ** categories, then eOperator==0.  The WhereTerm.pExpr field is still set
108643 ** to the original subexpression content and wtFlags is set up appropriately
108644 ** but no other fields in the WhereTerm object are meaningful.
108645 **
108646 ** When eOperator!=0, prereqRight and prereqAll record sets of cursor numbers,
108647 ** but they do so indirectly.  A single WhereMaskSet structure translates
108648 ** cursor number into bits and the translated bit is stored in the prereq
108649 ** fields.  The translation is used in order to maximize the number of
108650 ** bits that will fit in a Bitmask.  The VDBE cursor numbers might be
108651 ** spread out over the non-negative integers.  For example, the cursor
108652 ** numbers might be 3, 8, 9, 10, 20, 23, 41, and 45.  The WhereMaskSet
108653 ** translates these sparse cursor numbers into consecutive integers
108654 ** beginning with 0 in order to make the best possible use of the available
108655 ** bits in the Bitmask.  So, in the example above, the cursor numbers
108656 ** would be mapped into integers 0 through 7.
108657 **
108658 ** The number of terms in a join is limited by the number of bits
108659 ** in prereqRight and prereqAll.  The default is 64 bits, hence SQLite
108660 ** is only able to process joins with 64 or fewer tables.
108661 */
108662 struct WhereTerm {
108663   Expr *pExpr;            /* Pointer to the subexpression that is this term */
108664   int iParent;            /* Disable pWC->a[iParent] when this term disabled */
108665   int leftCursor;         /* Cursor number of X in "X <op> <expr>" */
108666   union {
108667     int leftColumn;         /* Column number of X in "X <op> <expr>" */
108668     WhereOrInfo *pOrInfo;   /* Extra information if (eOperator & WO_OR)!=0 */
108669     WhereAndInfo *pAndInfo; /* Extra information if (eOperator& WO_AND)!=0 */
108670   } u;
108671   LogEst truthProb;       /* Probability of truth for this expression */
108672   u16 eOperator;          /* A WO_xx value describing <op> */
108673   u8 wtFlags;             /* TERM_xxx bit flags.  See below */
108674   u8 nChild;              /* Number of children that must disable us */
108675   WhereClause *pWC;       /* The clause this term is part of */
108676   Bitmask prereqRight;    /* Bitmask of tables used by pExpr->pRight */
108677   Bitmask prereqAll;      /* Bitmask of tables referenced by pExpr */
108678 };
108679 
108680 /*
108681 ** Allowed values of WhereTerm.wtFlags
108682 */
108683 #define TERM_DYNAMIC    0x01   /* Need to call sqlite3ExprDelete(db, pExpr) */
108684 #define TERM_VIRTUAL    0x02   /* Added by the optimizer.  Do not code */
108685 #define TERM_CODED      0x04   /* This term is already coded */
108686 #define TERM_COPIED     0x08   /* Has a child */
108687 #define TERM_ORINFO     0x10   /* Need to free the WhereTerm.u.pOrInfo object */
108688 #define TERM_ANDINFO    0x20   /* Need to free the WhereTerm.u.pAndInfo obj */
108689 #define TERM_OR_OK      0x40   /* Used during OR-clause processing */
108690 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
108691 #  define TERM_VNULL    0x80   /* Manufactured x>NULL or x<=NULL term */
108692 #else
108693 #  define TERM_VNULL    0x00   /* Disabled if not using stat3 */
108694 #endif
108695 
108696 /*
108697 ** An instance of the WhereScan object is used as an iterator for locating
108698 ** terms in the WHERE clause that are useful to the query planner.
108699 */
108700 struct WhereScan {
108701   WhereClause *pOrigWC;      /* Original, innermost WhereClause */
108702   WhereClause *pWC;          /* WhereClause currently being scanned */
108703   char *zCollName;           /* Required collating sequence, if not NULL */
108704   char idxaff;               /* Must match this affinity, if zCollName!=NULL */
108705   unsigned char nEquiv;      /* Number of entries in aEquiv[] */
108706   unsigned char iEquiv;      /* Next unused slot in aEquiv[] */
108707   u32 opMask;                /* Acceptable operators */
108708   int k;                     /* Resume scanning at this->pWC->a[this->k] */
108709   int aEquiv[22];            /* Cursor,Column pairs for equivalence classes */
108710 };
108711 
108712 /*
108713 ** An instance of the following structure holds all information about a
108714 ** WHERE clause.  Mostly this is a container for one or more WhereTerms.
108715 **
108716 ** Explanation of pOuter:  For a WHERE clause of the form
108717 **
108718 **           a AND ((b AND c) OR (d AND e)) AND f
108719 **
108720 ** There are separate WhereClause objects for the whole clause and for
108721 ** the subclauses "(b AND c)" and "(d AND e)".  The pOuter field of the
108722 ** subclauses points to the WhereClause object for the whole clause.
108723 */
108724 struct WhereClause {
108725   WhereInfo *pWInfo;       /* WHERE clause processing context */
108726   WhereClause *pOuter;     /* Outer conjunction */
108727   u8 op;                   /* Split operator.  TK_AND or TK_OR */
108728   int nTerm;               /* Number of terms */
108729   int nSlot;               /* Number of entries in a[] */
108730   WhereTerm *a;            /* Each a[] describes a term of the WHERE cluase */
108731 #if defined(SQLITE_SMALL_STACK)
108732   WhereTerm aStatic[1];    /* Initial static space for a[] */
108733 #else
108734   WhereTerm aStatic[8];    /* Initial static space for a[] */
108735 #endif
108736 };
108737 
108738 /*
108739 ** A WhereTerm with eOperator==WO_OR has its u.pOrInfo pointer set to
108740 ** a dynamically allocated instance of the following structure.
108741 */
108742 struct WhereOrInfo {
108743   WhereClause wc;          /* Decomposition into subterms */
108744   Bitmask indexable;       /* Bitmask of all indexable tables in the clause */
108745 };
108746 
108747 /*
108748 ** A WhereTerm with eOperator==WO_AND has its u.pAndInfo pointer set to
108749 ** a dynamically allocated instance of the following structure.
108750 */
108751 struct WhereAndInfo {
108752   WhereClause wc;          /* The subexpression broken out */
108753 };
108754 
108755 /*
108756 ** An instance of the following structure keeps track of a mapping
108757 ** between VDBE cursor numbers and bits of the bitmasks in WhereTerm.
108758 **
108759 ** The VDBE cursor numbers are small integers contained in
108760 ** SrcList_item.iCursor and Expr.iTable fields.  For any given WHERE
108761 ** clause, the cursor numbers might not begin with 0 and they might
108762 ** contain gaps in the numbering sequence.  But we want to make maximum
108763 ** use of the bits in our bitmasks.  This structure provides a mapping
108764 ** from the sparse cursor numbers into consecutive integers beginning
108765 ** with 0.
108766 **
108767 ** If WhereMaskSet.ix[A]==B it means that The A-th bit of a Bitmask
108768 ** corresponds VDBE cursor number B.  The A-th bit of a bitmask is 1<<A.
108769 **
108770 ** For example, if the WHERE clause expression used these VDBE
108771 ** cursors:  4, 5, 8, 29, 57, 73.  Then the  WhereMaskSet structure
108772 ** would map those cursor numbers into bits 0 through 5.
108773 **
108774 ** Note that the mapping is not necessarily ordered.  In the example
108775 ** above, the mapping might go like this:  4->3, 5->1, 8->2, 29->0,
108776 ** 57->5, 73->4.  Or one of 719 other combinations might be used. It
108777 ** does not really matter.  What is important is that sparse cursor
108778 ** numbers all get mapped into bit numbers that begin with 0 and contain
108779 ** no gaps.
108780 */
108781 struct WhereMaskSet {
108782   int n;                        /* Number of assigned cursor values */
108783   int ix[BMS];                  /* Cursor assigned to each bit */
108784 };
108785 
108786 /*
108787 ** This object is a convenience wrapper holding all information needed
108788 ** to construct WhereLoop objects for a particular query.
108789 */
108790 struct WhereLoopBuilder {
108791   WhereInfo *pWInfo;        /* Information about this WHERE */
108792   WhereClause *pWC;         /* WHERE clause terms */
108793   ExprList *pOrderBy;       /* ORDER BY clause */
108794   WhereLoop *pNew;          /* Template WhereLoop */
108795   WhereOrSet *pOrSet;       /* Record best loops here, if not NULL */
108796 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
108797   UnpackedRecord *pRec;     /* Probe for stat4 (if required) */
108798   int nRecValid;            /* Number of valid fields currently in pRec */
108799 #endif
108800 };
108801 
108802 /*
108803 ** The WHERE clause processing routine has two halves.  The
108804 ** first part does the start of the WHERE loop and the second
108805 ** half does the tail of the WHERE loop.  An instance of
108806 ** this structure is returned by the first half and passed
108807 ** into the second half to give some continuity.
108808 **
108809 ** An instance of this object holds the complete state of the query
108810 ** planner.
108811 */
108812 struct WhereInfo {
108813   Parse *pParse;            /* Parsing and code generating context */
108814   SrcList *pTabList;        /* List of tables in the join */
108815   ExprList *pOrderBy;       /* The ORDER BY clause or NULL */
108816   ExprList *pResultSet;     /* Result set. DISTINCT operates on these */
108817   WhereLoop *pLoops;        /* List of all WhereLoop objects */
108818   Bitmask revMask;          /* Mask of ORDER BY terms that need reversing */
108819   LogEst nRowOut;           /* Estimated number of output rows */
108820   u16 wctrlFlags;           /* Flags originally passed to sqlite3WhereBegin() */
108821   u8 bOBSat;                /* ORDER BY satisfied by indices */
108822   u8 okOnePass;             /* Ok to use one-pass algorithm for UPDATE/DELETE */
108823   u8 untestedTerms;         /* Not all WHERE terms resolved by outer loop */
108824   u8 eDistinct;             /* One of the WHERE_DISTINCT_* values below */
108825   u8 nLevel;                /* Number of nested loop */
108826   int iTop;                 /* The very beginning of the WHERE loop */
108827   int iContinue;            /* Jump here to continue with next record */
108828   int iBreak;               /* Jump here to break out of the loop */
108829   int savedNQueryLoop;      /* pParse->nQueryLoop outside the WHERE loop */
108830   int aiCurOnePass[2];      /* OP_OpenWrite cursors for the ONEPASS opt */
108831   WhereMaskSet sMaskSet;    /* Map cursor numbers to bitmasks */
108832   WhereClause sWC;          /* Decomposition of the WHERE clause */
108833   WhereLevel a[1];          /* Information about each nest loop in WHERE */
108834 };
108835 
108836 /*
108837 ** Bitmasks for the operators on WhereTerm objects.  These are all
108838 ** operators that are of interest to the query planner.  An
108839 ** OR-ed combination of these values can be used when searching for
108840 ** particular WhereTerms within a WhereClause.
108841 */
108842 #define WO_IN     0x001
108843 #define WO_EQ     0x002
108844 #define WO_LT     (WO_EQ<<(TK_LT-TK_EQ))
108845 #define WO_LE     (WO_EQ<<(TK_LE-TK_EQ))
108846 #define WO_GT     (WO_EQ<<(TK_GT-TK_EQ))
108847 #define WO_GE     (WO_EQ<<(TK_GE-TK_EQ))
108848 #define WO_MATCH  0x040
108849 #define WO_ISNULL 0x080
108850 #define WO_OR     0x100       /* Two or more OR-connected terms */
108851 #define WO_AND    0x200       /* Two or more AND-connected terms */
108852 #define WO_EQUIV  0x400       /* Of the form A==B, both columns */
108853 #define WO_NOOP   0x800       /* This term does not restrict search space */
108854 
108855 #define WO_ALL    0xfff       /* Mask of all possible WO_* values */
108856 #define WO_SINGLE 0x0ff       /* Mask of all non-compound WO_* values */
108857 
108858 /*
108859 ** These are definitions of bits in the WhereLoop.wsFlags field.
108860 ** The particular combination of bits in each WhereLoop help to
108861 ** determine the algorithm that WhereLoop represents.
108862 */
108863 #define WHERE_COLUMN_EQ    0x00000001  /* x=EXPR */
108864 #define WHERE_COLUMN_RANGE 0x00000002  /* x<EXPR and/or x>EXPR */
108865 #define WHERE_COLUMN_IN    0x00000004  /* x IN (...) */
108866 #define WHERE_COLUMN_NULL  0x00000008  /* x IS NULL */
108867 #define WHERE_CONSTRAINT   0x0000000f  /* Any of the WHERE_COLUMN_xxx values */
108868 #define WHERE_TOP_LIMIT    0x00000010  /* x<EXPR or x<=EXPR constraint */
108869 #define WHERE_BTM_LIMIT    0x00000020  /* x>EXPR or x>=EXPR constraint */
108870 #define WHERE_BOTH_LIMIT   0x00000030  /* Both x>EXPR and x<EXPR */
108871 #define WHERE_IDX_ONLY     0x00000040  /* Use index only - omit table */
108872 #define WHERE_IPK          0x00000100  /* x is the INTEGER PRIMARY KEY */
108873 #define WHERE_INDEXED      0x00000200  /* WhereLoop.u.btree.pIndex is valid */
108874 #define WHERE_VIRTUALTABLE 0x00000400  /* WhereLoop.u.vtab is valid */
108875 #define WHERE_IN_ABLE      0x00000800  /* Able to support an IN operator */
108876 #define WHERE_ONEROW       0x00001000  /* Selects no more than one row */
108877 #define WHERE_MULTI_OR     0x00002000  /* OR using multiple indices */
108878 #define WHERE_AUTO_INDEX   0x00004000  /* Uses an ephemeral index */
108879 #define WHERE_SKIPSCAN     0x00008000  /* Uses the skip-scan algorithm */
108880 
108881 /************** End of whereInt.h ********************************************/
108882 /************** Continuing where we left off in where.c **********************/
108883 
108884 /*
108885 ** Return the estimated number of output rows from a WHERE clause
108886 */
108887 SQLITE_PRIVATE u64 sqlite3WhereOutputRowCount(WhereInfo *pWInfo){
108888   return sqlite3LogEstToInt(pWInfo->nRowOut);
108889 }
108890 
108891 /*
108892 ** Return one of the WHERE_DISTINCT_xxxxx values to indicate how this
108893 ** WHERE clause returns outputs for DISTINCT processing.
108894 */
108895 SQLITE_PRIVATE int sqlite3WhereIsDistinct(WhereInfo *pWInfo){
108896   return pWInfo->eDistinct;
108897 }
108898 
108899 /*
108900 ** Return TRUE if the WHERE clause returns rows in ORDER BY order.
108901 ** Return FALSE if the output needs to be sorted.
108902 */
108903 SQLITE_PRIVATE int sqlite3WhereIsOrdered(WhereInfo *pWInfo){
108904   return pWInfo->bOBSat!=0;
108905 }
108906 
108907 /*
108908 ** Return the VDBE address or label to jump to in order to continue
108909 ** immediately with the next row of a WHERE clause.
108910 */
108911 SQLITE_PRIVATE int sqlite3WhereContinueLabel(WhereInfo *pWInfo){
108912   return pWInfo->iContinue;
108913 }
108914 
108915 /*
108916 ** Return the VDBE address or label to jump to in order to break
108917 ** out of a WHERE loop.
108918 */
108919 SQLITE_PRIVATE int sqlite3WhereBreakLabel(WhereInfo *pWInfo){
108920   return pWInfo->iBreak;
108921 }
108922 
108923 /*
108924 ** Return TRUE if an UPDATE or DELETE statement can operate directly on
108925 ** the rowids returned by a WHERE clause.  Return FALSE if doing an
108926 ** UPDATE or DELETE might change subsequent WHERE clause results.
108927 **
108928 ** If the ONEPASS optimization is used (if this routine returns true)
108929 ** then also write the indices of open cursors used by ONEPASS
108930 ** into aiCur[0] and aiCur[1].  iaCur[0] gets the cursor of the data
108931 ** table and iaCur[1] gets the cursor used by an auxiliary index.
108932 ** Either value may be -1, indicating that cursor is not used.
108933 ** Any cursors returned will have been opened for writing.
108934 **
108935 ** aiCur[0] and aiCur[1] both get -1 if the where-clause logic is
108936 ** unable to use the ONEPASS optimization.
108937 */
108938 SQLITE_PRIVATE int sqlite3WhereOkOnePass(WhereInfo *pWInfo, int *aiCur){
108939   memcpy(aiCur, pWInfo->aiCurOnePass, sizeof(int)*2);
108940   return pWInfo->okOnePass;
108941 }
108942 
108943 /*
108944 ** Move the content of pSrc into pDest
108945 */
108946 static void whereOrMove(WhereOrSet *pDest, WhereOrSet *pSrc){
108947   pDest->n = pSrc->n;
108948   memcpy(pDest->a, pSrc->a, pDest->n*sizeof(pDest->a[0]));
108949 }
108950 
108951 /*
108952 ** Try to insert a new prerequisite/cost entry into the WhereOrSet pSet.
108953 **
108954 ** The new entry might overwrite an existing entry, or it might be
108955 ** appended, or it might be discarded.  Do whatever is the right thing
108956 ** so that pSet keeps the N_OR_COST best entries seen so far.
108957 */
108958 static int whereOrInsert(
108959   WhereOrSet *pSet,      /* The WhereOrSet to be updated */
108960   Bitmask prereq,        /* Prerequisites of the new entry */
108961   LogEst rRun,           /* Run-cost of the new entry */
108962   LogEst nOut            /* Number of outputs for the new entry */
108963 ){
108964   u16 i;
108965   WhereOrCost *p;
108966   for(i=pSet->n, p=pSet->a; i>0; i--, p++){
108967     if( rRun<=p->rRun && (prereq & p->prereq)==prereq ){
108968       goto whereOrInsert_done;
108969     }
108970     if( p->rRun<=rRun && (p->prereq & prereq)==p->prereq ){
108971       return 0;
108972     }
108973   }
108974   if( pSet->n<N_OR_COST ){
108975     p = &pSet->a[pSet->n++];
108976     p->nOut = nOut;
108977   }else{
108978     p = pSet->a;
108979     for(i=1; i<pSet->n; i++){
108980       if( p->rRun>pSet->a[i].rRun ) p = pSet->a + i;
108981     }
108982     if( p->rRun<=rRun ) return 0;
108983   }
108984 whereOrInsert_done:
108985   p->prereq = prereq;
108986   p->rRun = rRun;
108987   if( p->nOut>nOut ) p->nOut = nOut;
108988   return 1;
108989 }
108990 
108991 /*
108992 ** Initialize a preallocated WhereClause structure.
108993 */
108994 static void whereClauseInit(
108995   WhereClause *pWC,        /* The WhereClause to be initialized */
108996   WhereInfo *pWInfo        /* The WHERE processing context */
108997 ){
108998   pWC->pWInfo = pWInfo;
108999   pWC->pOuter = 0;
109000   pWC->nTerm = 0;
109001   pWC->nSlot = ArraySize(pWC->aStatic);
109002   pWC->a = pWC->aStatic;
109003 }
109004 
109005 /* Forward reference */
109006 static void whereClauseClear(WhereClause*);
109007 
109008 /*
109009 ** Deallocate all memory associated with a WhereOrInfo object.
109010 */
109011 static void whereOrInfoDelete(sqlite3 *db, WhereOrInfo *p){
109012   whereClauseClear(&p->wc);
109013   sqlite3DbFree(db, p);
109014 }
109015 
109016 /*
109017 ** Deallocate all memory associated with a WhereAndInfo object.
109018 */
109019 static void whereAndInfoDelete(sqlite3 *db, WhereAndInfo *p){
109020   whereClauseClear(&p->wc);
109021   sqlite3DbFree(db, p);
109022 }
109023 
109024 /*
109025 ** Deallocate a WhereClause structure.  The WhereClause structure
109026 ** itself is not freed.  This routine is the inverse of whereClauseInit().
109027 */
109028 static void whereClauseClear(WhereClause *pWC){
109029   int i;
109030   WhereTerm *a;
109031   sqlite3 *db = pWC->pWInfo->pParse->db;
109032   for(i=pWC->nTerm-1, a=pWC->a; i>=0; i--, a++){
109033     if( a->wtFlags & TERM_DYNAMIC ){
109034       sqlite3ExprDelete(db, a->pExpr);
109035     }
109036     if( a->wtFlags & TERM_ORINFO ){
109037       whereOrInfoDelete(db, a->u.pOrInfo);
109038     }else if( a->wtFlags & TERM_ANDINFO ){
109039       whereAndInfoDelete(db, a->u.pAndInfo);
109040     }
109041   }
109042   if( pWC->a!=pWC->aStatic ){
109043     sqlite3DbFree(db, pWC->a);
109044   }
109045 }
109046 
109047 /*
109048 ** Add a single new WhereTerm entry to the WhereClause object pWC.
109049 ** The new WhereTerm object is constructed from Expr p and with wtFlags.
109050 ** The index in pWC->a[] of the new WhereTerm is returned on success.
109051 ** 0 is returned if the new WhereTerm could not be added due to a memory
109052 ** allocation error.  The memory allocation failure will be recorded in
109053 ** the db->mallocFailed flag so that higher-level functions can detect it.
109054 **
109055 ** This routine will increase the size of the pWC->a[] array as necessary.
109056 **
109057 ** If the wtFlags argument includes TERM_DYNAMIC, then responsibility
109058 ** for freeing the expression p is assumed by the WhereClause object pWC.
109059 ** This is true even if this routine fails to allocate a new WhereTerm.
109060 **
109061 ** WARNING:  This routine might reallocate the space used to store
109062 ** WhereTerms.  All pointers to WhereTerms should be invalidated after
109063 ** calling this routine.  Such pointers may be reinitialized by referencing
109064 ** the pWC->a[] array.
109065 */
109066 static int whereClauseInsert(WhereClause *pWC, Expr *p, u8 wtFlags){
109067   WhereTerm *pTerm;
109068   int idx;
109069   testcase( wtFlags & TERM_VIRTUAL );
109070   if( pWC->nTerm>=pWC->nSlot ){
109071     WhereTerm *pOld = pWC->a;
109072     sqlite3 *db = pWC->pWInfo->pParse->db;
109073     pWC->a = sqlite3DbMallocRaw(db, sizeof(pWC->a[0])*pWC->nSlot*2 );
109074     if( pWC->a==0 ){
109075       if( wtFlags & TERM_DYNAMIC ){
109076         sqlite3ExprDelete(db, p);
109077       }
109078       pWC->a = pOld;
109079       return 0;
109080     }
109081     memcpy(pWC->a, pOld, sizeof(pWC->a[0])*pWC->nTerm);
109082     if( pOld!=pWC->aStatic ){
109083       sqlite3DbFree(db, pOld);
109084     }
109085     pWC->nSlot = sqlite3DbMallocSize(db, pWC->a)/sizeof(pWC->a[0]);
109086   }
109087   pTerm = &pWC->a[idx = pWC->nTerm++];
109088   if( p && ExprHasProperty(p, EP_Unlikely) ){
109089     pTerm->truthProb = sqlite3LogEst(p->iTable) - 99;
109090   }else{
109091     pTerm->truthProb = -1;
109092   }
109093   pTerm->pExpr = sqlite3ExprSkipCollate(p);
109094   pTerm->wtFlags = wtFlags;
109095   pTerm->pWC = pWC;
109096   pTerm->iParent = -1;
109097   return idx;
109098 }
109099 
109100 /*
109101 ** This routine identifies subexpressions in the WHERE clause where
109102 ** each subexpression is separated by the AND operator or some other
109103 ** operator specified in the op parameter.  The WhereClause structure
109104 ** is filled with pointers to subexpressions.  For example:
109105 **
109106 **    WHERE  a=='hello' AND coalesce(b,11)<10 AND (c+12!=d OR c==22)
109107 **           \________/     \_______________/     \________________/
109108 **            slot[0]            slot[1]               slot[2]
109109 **
109110 ** The original WHERE clause in pExpr is unaltered.  All this routine
109111 ** does is make slot[] entries point to substructure within pExpr.
109112 **
109113 ** In the previous sentence and in the diagram, "slot[]" refers to
109114 ** the WhereClause.a[] array.  The slot[] array grows as needed to contain
109115 ** all terms of the WHERE clause.
109116 */
109117 static void whereSplit(WhereClause *pWC, Expr *pExpr, u8 op){
109118   pWC->op = op;
109119   if( pExpr==0 ) return;
109120   if( pExpr->op!=op ){
109121     whereClauseInsert(pWC, pExpr, 0);
109122   }else{
109123     whereSplit(pWC, pExpr->pLeft, op);
109124     whereSplit(pWC, pExpr->pRight, op);
109125   }
109126 }
109127 
109128 /*
109129 ** Initialize a WhereMaskSet object
109130 */
109131 #define initMaskSet(P)  (P)->n=0
109132 
109133 /*
109134 ** Return the bitmask for the given cursor number.  Return 0 if
109135 ** iCursor is not in the set.
109136 */
109137 static Bitmask getMask(WhereMaskSet *pMaskSet, int iCursor){
109138   int i;
109139   assert( pMaskSet->n<=(int)sizeof(Bitmask)*8 );
109140   for(i=0; i<pMaskSet->n; i++){
109141     if( pMaskSet->ix[i]==iCursor ){
109142       return MASKBIT(i);
109143     }
109144   }
109145   return 0;
109146 }
109147 
109148 /*
109149 ** Create a new mask for cursor iCursor.
109150 **
109151 ** There is one cursor per table in the FROM clause.  The number of
109152 ** tables in the FROM clause is limited by a test early in the
109153 ** sqlite3WhereBegin() routine.  So we know that the pMaskSet->ix[]
109154 ** array will never overflow.
109155 */
109156 static void createMask(WhereMaskSet *pMaskSet, int iCursor){
109157   assert( pMaskSet->n < ArraySize(pMaskSet->ix) );
109158   pMaskSet->ix[pMaskSet->n++] = iCursor;
109159 }
109160 
109161 /*
109162 ** These routines walk (recursively) an expression tree and generate
109163 ** a bitmask indicating which tables are used in that expression
109164 ** tree.
109165 */
109166 static Bitmask exprListTableUsage(WhereMaskSet*, ExprList*);
109167 static Bitmask exprSelectTableUsage(WhereMaskSet*, Select*);
109168 static Bitmask exprTableUsage(WhereMaskSet *pMaskSet, Expr *p){
109169   Bitmask mask = 0;
109170   if( p==0 ) return 0;
109171   if( p->op==TK_COLUMN ){
109172     mask = getMask(pMaskSet, p->iTable);
109173     return mask;
109174   }
109175   mask = exprTableUsage(pMaskSet, p->pRight);
109176   mask |= exprTableUsage(pMaskSet, p->pLeft);
109177   if( ExprHasProperty(p, EP_xIsSelect) ){
109178     mask |= exprSelectTableUsage(pMaskSet, p->x.pSelect);
109179   }else{
109180     mask |= exprListTableUsage(pMaskSet, p->x.pList);
109181   }
109182   return mask;
109183 }
109184 static Bitmask exprListTableUsage(WhereMaskSet *pMaskSet, ExprList *pList){
109185   int i;
109186   Bitmask mask = 0;
109187   if( pList ){
109188     for(i=0; i<pList->nExpr; i++){
109189       mask |= exprTableUsage(pMaskSet, pList->a[i].pExpr);
109190     }
109191   }
109192   return mask;
109193 }
109194 static Bitmask exprSelectTableUsage(WhereMaskSet *pMaskSet, Select *pS){
109195   Bitmask mask = 0;
109196   while( pS ){
109197     SrcList *pSrc = pS->pSrc;
109198     mask |= exprListTableUsage(pMaskSet, pS->pEList);
109199     mask |= exprListTableUsage(pMaskSet, pS->pGroupBy);
109200     mask |= exprListTableUsage(pMaskSet, pS->pOrderBy);
109201     mask |= exprTableUsage(pMaskSet, pS->pWhere);
109202     mask |= exprTableUsage(pMaskSet, pS->pHaving);
109203     if( ALWAYS(pSrc!=0) ){
109204       int i;
109205       for(i=0; i<pSrc->nSrc; i++){
109206         mask |= exprSelectTableUsage(pMaskSet, pSrc->a[i].pSelect);
109207         mask |= exprTableUsage(pMaskSet, pSrc->a[i].pOn);
109208       }
109209     }
109210     pS = pS->pPrior;
109211   }
109212   return mask;
109213 }
109214 
109215 /*
109216 ** Return TRUE if the given operator is one of the operators that is
109217 ** allowed for an indexable WHERE clause term.  The allowed operators are
109218 ** "=", "<", ">", "<=", ">=", "IN", and "IS NULL"
109219 */
109220 static int allowedOp(int op){
109221   assert( TK_GT>TK_EQ && TK_GT<TK_GE );
109222   assert( TK_LT>TK_EQ && TK_LT<TK_GE );
109223   assert( TK_LE>TK_EQ && TK_LE<TK_GE );
109224   assert( TK_GE==TK_EQ+4 );
109225   return op==TK_IN || (op>=TK_EQ && op<=TK_GE) || op==TK_ISNULL;
109226 }
109227 
109228 /*
109229 ** Swap two objects of type TYPE.
109230 */
109231 #define SWAP(TYPE,A,B) {TYPE t=A; A=B; B=t;}
109232 
109233 /*
109234 ** Commute a comparison operator.  Expressions of the form "X op Y"
109235 ** are converted into "Y op X".
109236 **
109237 ** If left/right precedence rules come into play when determining the
109238 ** collating sequence, then COLLATE operators are adjusted to ensure
109239 ** that the collating sequence does not change.  For example:
109240 ** "Y collate NOCASE op X" becomes "X op Y" because any collation sequence on
109241 ** the left hand side of a comparison overrides any collation sequence
109242 ** attached to the right. For the same reason the EP_Collate flag
109243 ** is not commuted.
109244 */
109245 static void exprCommute(Parse *pParse, Expr *pExpr){
109246   u16 expRight = (pExpr->pRight->flags & EP_Collate);
109247   u16 expLeft = (pExpr->pLeft->flags & EP_Collate);
109248   assert( allowedOp(pExpr->op) && pExpr->op!=TK_IN );
109249   if( expRight==expLeft ){
109250     /* Either X and Y both have COLLATE operator or neither do */
109251     if( expRight ){
109252       /* Both X and Y have COLLATE operators.  Make sure X is always
109253       ** used by clearing the EP_Collate flag from Y. */
109254       pExpr->pRight->flags &= ~EP_Collate;
109255     }else if( sqlite3ExprCollSeq(pParse, pExpr->pLeft)!=0 ){
109256       /* Neither X nor Y have COLLATE operators, but X has a non-default
109257       ** collating sequence.  So add the EP_Collate marker on X to cause
109258       ** it to be searched first. */
109259       pExpr->pLeft->flags |= EP_Collate;
109260     }
109261   }
109262   SWAP(Expr*,pExpr->pRight,pExpr->pLeft);
109263   if( pExpr->op>=TK_GT ){
109264     assert( TK_LT==TK_GT+2 );
109265     assert( TK_GE==TK_LE+2 );
109266     assert( TK_GT>TK_EQ );
109267     assert( TK_GT<TK_LE );
109268     assert( pExpr->op>=TK_GT && pExpr->op<=TK_GE );
109269     pExpr->op = ((pExpr->op-TK_GT)^2)+TK_GT;
109270   }
109271 }
109272 
109273 /*
109274 ** Translate from TK_xx operator to WO_xx bitmask.
109275 */
109276 static u16 operatorMask(int op){
109277   u16 c;
109278   assert( allowedOp(op) );
109279   if( op==TK_IN ){
109280     c = WO_IN;
109281   }else if( op==TK_ISNULL ){
109282     c = WO_ISNULL;
109283   }else{
109284     assert( (WO_EQ<<(op-TK_EQ)) < 0x7fff );
109285     c = (u16)(WO_EQ<<(op-TK_EQ));
109286   }
109287   assert( op!=TK_ISNULL || c==WO_ISNULL );
109288   assert( op!=TK_IN || c==WO_IN );
109289   assert( op!=TK_EQ || c==WO_EQ );
109290   assert( op!=TK_LT || c==WO_LT );
109291   assert( op!=TK_LE || c==WO_LE );
109292   assert( op!=TK_GT || c==WO_GT );
109293   assert( op!=TK_GE || c==WO_GE );
109294   return c;
109295 }
109296 
109297 /*
109298 ** Advance to the next WhereTerm that matches according to the criteria
109299 ** established when the pScan object was initialized by whereScanInit().
109300 ** Return NULL if there are no more matching WhereTerms.
109301 */
109302 static WhereTerm *whereScanNext(WhereScan *pScan){
109303   int iCur;            /* The cursor on the LHS of the term */
109304   int iColumn;         /* The column on the LHS of the term.  -1 for IPK */
109305   Expr *pX;            /* An expression being tested */
109306   WhereClause *pWC;    /* Shorthand for pScan->pWC */
109307   WhereTerm *pTerm;    /* The term being tested */
109308   int k = pScan->k;    /* Where to start scanning */
109309 
109310   while( pScan->iEquiv<=pScan->nEquiv ){
109311     iCur = pScan->aEquiv[pScan->iEquiv-2];
109312     iColumn = pScan->aEquiv[pScan->iEquiv-1];
109313     while( (pWC = pScan->pWC)!=0 ){
109314       for(pTerm=pWC->a+k; k<pWC->nTerm; k++, pTerm++){
109315         if( pTerm->leftCursor==iCur
109316          && pTerm->u.leftColumn==iColumn
109317          && (pScan->iEquiv<=2 || !ExprHasProperty(pTerm->pExpr, EP_FromJoin))
109318         ){
109319           if( (pTerm->eOperator & WO_EQUIV)!=0
109320            && pScan->nEquiv<ArraySize(pScan->aEquiv)
109321           ){
109322             int j;
109323             pX = sqlite3ExprSkipCollate(pTerm->pExpr->pRight);
109324             assert( pX->op==TK_COLUMN );
109325             for(j=0; j<pScan->nEquiv; j+=2){
109326               if( pScan->aEquiv[j]==pX->iTable
109327                && pScan->aEquiv[j+1]==pX->iColumn ){
109328                   break;
109329               }
109330             }
109331             if( j==pScan->nEquiv ){
109332               pScan->aEquiv[j] = pX->iTable;
109333               pScan->aEquiv[j+1] = pX->iColumn;
109334               pScan->nEquiv += 2;
109335             }
109336           }
109337           if( (pTerm->eOperator & pScan->opMask)!=0 ){
109338             /* Verify the affinity and collating sequence match */
109339             if( pScan->zCollName && (pTerm->eOperator & WO_ISNULL)==0 ){
109340               CollSeq *pColl;
109341               Parse *pParse = pWC->pWInfo->pParse;
109342               pX = pTerm->pExpr;
109343               if( !sqlite3IndexAffinityOk(pX, pScan->idxaff) ){
109344                 continue;
109345               }
109346               assert(pX->pLeft);
109347               pColl = sqlite3BinaryCompareCollSeq(pParse,
109348                                                   pX->pLeft, pX->pRight);
109349               if( pColl==0 ) pColl = pParse->db->pDfltColl;
109350               if( sqlite3StrICmp(pColl->zName, pScan->zCollName) ){
109351                 continue;
109352               }
109353             }
109354             if( (pTerm->eOperator & WO_EQ)!=0
109355              && (pX = pTerm->pExpr->pRight)->op==TK_COLUMN
109356              && pX->iTable==pScan->aEquiv[0]
109357              && pX->iColumn==pScan->aEquiv[1]
109358             ){
109359               continue;
109360             }
109361             pScan->k = k+1;
109362             return pTerm;
109363           }
109364         }
109365       }
109366       pScan->pWC = pScan->pWC->pOuter;
109367       k = 0;
109368     }
109369     pScan->pWC = pScan->pOrigWC;
109370     k = 0;
109371     pScan->iEquiv += 2;
109372   }
109373   return 0;
109374 }
109375 
109376 /*
109377 ** Initialize a WHERE clause scanner object.  Return a pointer to the
109378 ** first match.  Return NULL if there are no matches.
109379 **
109380 ** The scanner will be searching the WHERE clause pWC.  It will look
109381 ** for terms of the form "X <op> <expr>" where X is column iColumn of table
109382 ** iCur.  The <op> must be one of the operators described by opMask.
109383 **
109384 ** If the search is for X and the WHERE clause contains terms of the
109385 ** form X=Y then this routine might also return terms of the form
109386 ** "Y <op> <expr>".  The number of levels of transitivity is limited,
109387 ** but is enough to handle most commonly occurring SQL statements.
109388 **
109389 ** If X is not the INTEGER PRIMARY KEY then X must be compatible with
109390 ** index pIdx.
109391 */
109392 static WhereTerm *whereScanInit(
109393   WhereScan *pScan,       /* The WhereScan object being initialized */
109394   WhereClause *pWC,       /* The WHERE clause to be scanned */
109395   int iCur,               /* Cursor to scan for */
109396   int iColumn,            /* Column to scan for */
109397   u32 opMask,             /* Operator(s) to scan for */
109398   Index *pIdx             /* Must be compatible with this index */
109399 ){
109400   int j;
109401 
109402   /* memset(pScan, 0, sizeof(*pScan)); */
109403   pScan->pOrigWC = pWC;
109404   pScan->pWC = pWC;
109405   if( pIdx && iColumn>=0 ){
109406     pScan->idxaff = pIdx->pTable->aCol[iColumn].affinity;
109407     for(j=0; pIdx->aiColumn[j]!=iColumn; j++){
109408       if( NEVER(j>=pIdx->nKeyCol) ) return 0;
109409     }
109410     pScan->zCollName = pIdx->azColl[j];
109411   }else{
109412     pScan->idxaff = 0;
109413     pScan->zCollName = 0;
109414   }
109415   pScan->opMask = opMask;
109416   pScan->k = 0;
109417   pScan->aEquiv[0] = iCur;
109418   pScan->aEquiv[1] = iColumn;
109419   pScan->nEquiv = 2;
109420   pScan->iEquiv = 2;
109421   return whereScanNext(pScan);
109422 }
109423 
109424 /*
109425 ** Search for a term in the WHERE clause that is of the form "X <op> <expr>"
109426 ** where X is a reference to the iColumn of table iCur and <op> is one of
109427 ** the WO_xx operator codes specified by the op parameter.
109428 ** Return a pointer to the term.  Return 0 if not found.
109429 **
109430 ** The term returned might by Y=<expr> if there is another constraint in
109431 ** the WHERE clause that specifies that X=Y.  Any such constraints will be
109432 ** identified by the WO_EQUIV bit in the pTerm->eOperator field.  The
109433 ** aEquiv[] array holds X and all its equivalents, with each SQL variable
109434 ** taking up two slots in aEquiv[].  The first slot is for the cursor number
109435 ** and the second is for the column number.  There are 22 slots in aEquiv[]
109436 ** so that means we can look for X plus up to 10 other equivalent values.
109437 ** Hence a search for X will return <expr> if X=A1 and A1=A2 and A2=A3
109438 ** and ... and A9=A10 and A10=<expr>.
109439 **
109440 ** If there are multiple terms in the WHERE clause of the form "X <op> <expr>"
109441 ** then try for the one with no dependencies on <expr> - in other words where
109442 ** <expr> is a constant expression of some kind.  Only return entries of
109443 ** the form "X <op> Y" where Y is a column in another table if no terms of
109444 ** the form "X <op> <const-expr>" exist.   If no terms with a constant RHS
109445 ** exist, try to return a term that does not use WO_EQUIV.
109446 */
109447 static WhereTerm *findTerm(
109448   WhereClause *pWC,     /* The WHERE clause to be searched */
109449   int iCur,             /* Cursor number of LHS */
109450   int iColumn,          /* Column number of LHS */
109451   Bitmask notReady,     /* RHS must not overlap with this mask */
109452   u32 op,               /* Mask of WO_xx values describing operator */
109453   Index *pIdx           /* Must be compatible with this index, if not NULL */
109454 ){
109455   WhereTerm *pResult = 0;
109456   WhereTerm *p;
109457   WhereScan scan;
109458 
109459   p = whereScanInit(&scan, pWC, iCur, iColumn, op, pIdx);
109460   while( p ){
109461     if( (p->prereqRight & notReady)==0 ){
109462       if( p->prereqRight==0 && (p->eOperator&WO_EQ)!=0 ){
109463         return p;
109464       }
109465       if( pResult==0 ) pResult = p;
109466     }
109467     p = whereScanNext(&scan);
109468   }
109469   return pResult;
109470 }
109471 
109472 /* Forward reference */
109473 static void exprAnalyze(SrcList*, WhereClause*, int);
109474 
109475 /*
109476 ** Call exprAnalyze on all terms in a WHERE clause.
109477 */
109478 static void exprAnalyzeAll(
109479   SrcList *pTabList,       /* the FROM clause */
109480   WhereClause *pWC         /* the WHERE clause to be analyzed */
109481 ){
109482   int i;
109483   for(i=pWC->nTerm-1; i>=0; i--){
109484     exprAnalyze(pTabList, pWC, i);
109485   }
109486 }
109487 
109488 #ifndef SQLITE_OMIT_LIKE_OPTIMIZATION
109489 /*
109490 ** Check to see if the given expression is a LIKE or GLOB operator that
109491 ** can be optimized using inequality constraints.  Return TRUE if it is
109492 ** so and false if not.
109493 **
109494 ** In order for the operator to be optimizible, the RHS must be a string
109495 ** literal that does not begin with a wildcard.
109496 */
109497 static int isLikeOrGlob(
109498   Parse *pParse,    /* Parsing and code generating context */
109499   Expr *pExpr,      /* Test this expression */
109500   Expr **ppPrefix,  /* Pointer to TK_STRING expression with pattern prefix */
109501   int *pisComplete, /* True if the only wildcard is % in the last character */
109502   int *pnoCase      /* True if uppercase is equivalent to lowercase */
109503 ){
109504   const char *z = 0;         /* String on RHS of LIKE operator */
109505   Expr *pRight, *pLeft;      /* Right and left size of LIKE operator */
109506   ExprList *pList;           /* List of operands to the LIKE operator */
109507   int c;                     /* One character in z[] */
109508   int cnt;                   /* Number of non-wildcard prefix characters */
109509   char wc[3];                /* Wildcard characters */
109510   sqlite3 *db = pParse->db;  /* Database connection */
109511   sqlite3_value *pVal = 0;
109512   int op;                    /* Opcode of pRight */
109513 
109514   if( !sqlite3IsLikeFunction(db, pExpr, pnoCase, wc) ){
109515     return 0;
109516   }
109517 #ifdef SQLITE_EBCDIC
109518   if( *pnoCase ) return 0;
109519 #endif
109520   pList = pExpr->x.pList;
109521   pLeft = pList->a[1].pExpr;
109522   if( pLeft->op!=TK_COLUMN
109523    || sqlite3ExprAffinity(pLeft)!=SQLITE_AFF_TEXT
109524    || IsVirtual(pLeft->pTab)
109525   ){
109526     /* IMP: R-02065-49465 The left-hand side of the LIKE or GLOB operator must
109527     ** be the name of an indexed column with TEXT affinity. */
109528     return 0;
109529   }
109530   assert( pLeft->iColumn!=(-1) ); /* Because IPK never has AFF_TEXT */
109531 
109532   pRight = sqlite3ExprSkipCollate(pList->a[0].pExpr);
109533   op = pRight->op;
109534   if( op==TK_VARIABLE ){
109535     Vdbe *pReprepare = pParse->pReprepare;
109536     int iCol = pRight->iColumn;
109537     pVal = sqlite3VdbeGetBoundValue(pReprepare, iCol, SQLITE_AFF_NONE);
109538     if( pVal && sqlite3_value_type(pVal)==SQLITE_TEXT ){
109539       z = (char *)sqlite3_value_text(pVal);
109540     }
109541     sqlite3VdbeSetVarmask(pParse->pVdbe, iCol);
109542     assert( pRight->op==TK_VARIABLE || pRight->op==TK_REGISTER );
109543   }else if( op==TK_STRING ){
109544     z = pRight->u.zToken;
109545   }
109546   if( z ){
109547     cnt = 0;
109548     while( (c=z[cnt])!=0 && c!=wc[0] && c!=wc[1] && c!=wc[2] ){
109549       cnt++;
109550     }
109551     if( cnt!=0 && 255!=(u8)z[cnt-1] ){
109552       Expr *pPrefix;
109553       *pisComplete = c==wc[0] && z[cnt+1]==0;
109554       pPrefix = sqlite3Expr(db, TK_STRING, z);
109555       if( pPrefix ) pPrefix->u.zToken[cnt] = 0;
109556       *ppPrefix = pPrefix;
109557       if( op==TK_VARIABLE ){
109558         Vdbe *v = pParse->pVdbe;
109559         sqlite3VdbeSetVarmask(v, pRight->iColumn);
109560         if( *pisComplete && pRight->u.zToken[1] ){
109561           /* If the rhs of the LIKE expression is a variable, and the current
109562           ** value of the variable means there is no need to invoke the LIKE
109563           ** function, then no OP_Variable will be added to the program.
109564           ** This causes problems for the sqlite3_bind_parameter_name()
109565           ** API. To workaround them, add a dummy OP_Variable here.
109566           */
109567           int r1 = sqlite3GetTempReg(pParse);
109568           sqlite3ExprCodeTarget(pParse, pRight, r1);
109569           sqlite3VdbeChangeP3(v, sqlite3VdbeCurrentAddr(v)-1, 0);
109570           sqlite3ReleaseTempReg(pParse, r1);
109571         }
109572       }
109573     }else{
109574       z = 0;
109575     }
109576   }
109577 
109578   sqlite3ValueFree(pVal);
109579   return (z!=0);
109580 }
109581 #endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */
109582 
109583 
109584 #ifndef SQLITE_OMIT_VIRTUALTABLE
109585 /*
109586 ** Check to see if the given expression is of the form
109587 **
109588 **         column MATCH expr
109589 **
109590 ** If it is then return TRUE.  If not, return FALSE.
109591 */
109592 static int isMatchOfColumn(
109593   Expr *pExpr      /* Test this expression */
109594 ){
109595   ExprList *pList;
109596 
109597   if( pExpr->op!=TK_FUNCTION ){
109598     return 0;
109599   }
109600   if( sqlite3StrICmp(pExpr->u.zToken,"match")!=0 ){
109601     return 0;
109602   }
109603   pList = pExpr->x.pList;
109604   if( pList->nExpr!=2 ){
109605     return 0;
109606   }
109607   if( pList->a[1].pExpr->op != TK_COLUMN ){
109608     return 0;
109609   }
109610   return 1;
109611 }
109612 #endif /* SQLITE_OMIT_VIRTUALTABLE */
109613 
109614 /*
109615 ** If the pBase expression originated in the ON or USING clause of
109616 ** a join, then transfer the appropriate markings over to derived.
109617 */
109618 static void transferJoinMarkings(Expr *pDerived, Expr *pBase){
109619   if( pDerived ){
109620     pDerived->flags |= pBase->flags & EP_FromJoin;
109621     pDerived->iRightJoinTable = pBase->iRightJoinTable;
109622   }
109623 }
109624 
109625 #if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY)
109626 /*
109627 ** Analyze a term that consists of two or more OR-connected
109628 ** subterms.  So in:
109629 **
109630 **     ... WHERE  (a=5) AND (b=7 OR c=9 OR d=13) AND (d=13)
109631 **                          ^^^^^^^^^^^^^^^^^^^^
109632 **
109633 ** This routine analyzes terms such as the middle term in the above example.
109634 ** A WhereOrTerm object is computed and attached to the term under
109635 ** analysis, regardless of the outcome of the analysis.  Hence:
109636 **
109637 **     WhereTerm.wtFlags   |=  TERM_ORINFO
109638 **     WhereTerm.u.pOrInfo  =  a dynamically allocated WhereOrTerm object
109639 **
109640 ** The term being analyzed must have two or more of OR-connected subterms.
109641 ** A single subterm might be a set of AND-connected sub-subterms.
109642 ** Examples of terms under analysis:
109643 **
109644 **     (A)     t1.x=t2.y OR t1.x=t2.z OR t1.y=15 OR t1.z=t3.a+5
109645 **     (B)     x=expr1 OR expr2=x OR x=expr3
109646 **     (C)     t1.x=t2.y OR (t1.x=t2.z AND t1.y=15)
109647 **     (D)     x=expr1 OR (y>11 AND y<22 AND z LIKE '*hello*')
109648 **     (E)     (p.a=1 AND q.b=2 AND r.c=3) OR (p.x=4 AND q.y=5 AND r.z=6)
109649 **
109650 ** CASE 1:
109651 **
109652 ** If all subterms are of the form T.C=expr for some single column of C and
109653 ** a single table T (as shown in example B above) then create a new virtual
109654 ** term that is an equivalent IN expression.  In other words, if the term
109655 ** being analyzed is:
109656 **
109657 **      x = expr1  OR  expr2 = x  OR  x = expr3
109658 **
109659 ** then create a new virtual term like this:
109660 **
109661 **      x IN (expr1,expr2,expr3)
109662 **
109663 ** CASE 2:
109664 **
109665 ** If all subterms are indexable by a single table T, then set
109666 **
109667 **     WhereTerm.eOperator              =  WO_OR
109668 **     WhereTerm.u.pOrInfo->indexable  |=  the cursor number for table T
109669 **
109670 ** A subterm is "indexable" if it is of the form
109671 ** "T.C <op> <expr>" where C is any column of table T and
109672 ** <op> is one of "=", "<", "<=", ">", ">=", "IS NULL", or "IN".
109673 ** A subterm is also indexable if it is an AND of two or more
109674 ** subsubterms at least one of which is indexable.  Indexable AND
109675 ** subterms have their eOperator set to WO_AND and they have
109676 ** u.pAndInfo set to a dynamically allocated WhereAndTerm object.
109677 **
109678 ** From another point of view, "indexable" means that the subterm could
109679 ** potentially be used with an index if an appropriate index exists.
109680 ** This analysis does not consider whether or not the index exists; that
109681 ** is decided elsewhere.  This analysis only looks at whether subterms
109682 ** appropriate for indexing exist.
109683 **
109684 ** All examples A through E above satisfy case 2.  But if a term
109685 ** also statisfies case 1 (such as B) we know that the optimizer will
109686 ** always prefer case 1, so in that case we pretend that case 2 is not
109687 ** satisfied.
109688 **
109689 ** It might be the case that multiple tables are indexable.  For example,
109690 ** (E) above is indexable on tables P, Q, and R.
109691 **
109692 ** Terms that satisfy case 2 are candidates for lookup by using
109693 ** separate indices to find rowids for each subterm and composing
109694 ** the union of all rowids using a RowSet object.  This is similar
109695 ** to "bitmap indices" in other database engines.
109696 **
109697 ** OTHERWISE:
109698 **
109699 ** If neither case 1 nor case 2 apply, then leave the eOperator set to
109700 ** zero.  This term is not useful for search.
109701 */
109702 static void exprAnalyzeOrTerm(
109703   SrcList *pSrc,            /* the FROM clause */
109704   WhereClause *pWC,         /* the complete WHERE clause */
109705   int idxTerm               /* Index of the OR-term to be analyzed */
109706 ){
109707   WhereInfo *pWInfo = pWC->pWInfo;        /* WHERE clause processing context */
109708   Parse *pParse = pWInfo->pParse;         /* Parser context */
109709   sqlite3 *db = pParse->db;               /* Database connection */
109710   WhereTerm *pTerm = &pWC->a[idxTerm];    /* The term to be analyzed */
109711   Expr *pExpr = pTerm->pExpr;             /* The expression of the term */
109712   int i;                                  /* Loop counters */
109713   WhereClause *pOrWc;       /* Breakup of pTerm into subterms */
109714   WhereTerm *pOrTerm;       /* A Sub-term within the pOrWc */
109715   WhereOrInfo *pOrInfo;     /* Additional information associated with pTerm */
109716   Bitmask chngToIN;         /* Tables that might satisfy case 1 */
109717   Bitmask indexable;        /* Tables that are indexable, satisfying case 2 */
109718 
109719   /*
109720   ** Break the OR clause into its separate subterms.  The subterms are
109721   ** stored in a WhereClause structure containing within the WhereOrInfo
109722   ** object that is attached to the original OR clause term.
109723   */
109724   assert( (pTerm->wtFlags & (TERM_DYNAMIC|TERM_ORINFO|TERM_ANDINFO))==0 );
109725   assert( pExpr->op==TK_OR );
109726   pTerm->u.pOrInfo = pOrInfo = sqlite3DbMallocZero(db, sizeof(*pOrInfo));
109727   if( pOrInfo==0 ) return;
109728   pTerm->wtFlags |= TERM_ORINFO;
109729   pOrWc = &pOrInfo->wc;
109730   whereClauseInit(pOrWc, pWInfo);
109731   whereSplit(pOrWc, pExpr, TK_OR);
109732   exprAnalyzeAll(pSrc, pOrWc);
109733   if( db->mallocFailed ) return;
109734   assert( pOrWc->nTerm>=2 );
109735 
109736   /*
109737   ** Compute the set of tables that might satisfy cases 1 or 2.
109738   */
109739   indexable = ~(Bitmask)0;
109740   chngToIN = ~(Bitmask)0;
109741   for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0 && indexable; i--, pOrTerm++){
109742     if( (pOrTerm->eOperator & WO_SINGLE)==0 ){
109743       WhereAndInfo *pAndInfo;
109744       assert( (pOrTerm->wtFlags & (TERM_ANDINFO|TERM_ORINFO))==0 );
109745       chngToIN = 0;
109746       pAndInfo = sqlite3DbMallocRaw(db, sizeof(*pAndInfo));
109747       if( pAndInfo ){
109748         WhereClause *pAndWC;
109749         WhereTerm *pAndTerm;
109750         int j;
109751         Bitmask b = 0;
109752         pOrTerm->u.pAndInfo = pAndInfo;
109753         pOrTerm->wtFlags |= TERM_ANDINFO;
109754         pOrTerm->eOperator = WO_AND;
109755         pAndWC = &pAndInfo->wc;
109756         whereClauseInit(pAndWC, pWC->pWInfo);
109757         whereSplit(pAndWC, pOrTerm->pExpr, TK_AND);
109758         exprAnalyzeAll(pSrc, pAndWC);
109759         pAndWC->pOuter = pWC;
109760         testcase( db->mallocFailed );
109761         if( !db->mallocFailed ){
109762           for(j=0, pAndTerm=pAndWC->a; j<pAndWC->nTerm; j++, pAndTerm++){
109763             assert( pAndTerm->pExpr );
109764             if( allowedOp(pAndTerm->pExpr->op) ){
109765               b |= getMask(&pWInfo->sMaskSet, pAndTerm->leftCursor);
109766             }
109767           }
109768         }
109769         indexable &= b;
109770       }
109771     }else if( pOrTerm->wtFlags & TERM_COPIED ){
109772       /* Skip this term for now.  We revisit it when we process the
109773       ** corresponding TERM_VIRTUAL term */
109774     }else{
109775       Bitmask b;
109776       b = getMask(&pWInfo->sMaskSet, pOrTerm->leftCursor);
109777       if( pOrTerm->wtFlags & TERM_VIRTUAL ){
109778         WhereTerm *pOther = &pOrWc->a[pOrTerm->iParent];
109779         b |= getMask(&pWInfo->sMaskSet, pOther->leftCursor);
109780       }
109781       indexable &= b;
109782       if( (pOrTerm->eOperator & WO_EQ)==0 ){
109783         chngToIN = 0;
109784       }else{
109785         chngToIN &= b;
109786       }
109787     }
109788   }
109789 
109790   /*
109791   ** Record the set of tables that satisfy case 2.  The set might be
109792   ** empty.
109793   */
109794   pOrInfo->indexable = indexable;
109795   pTerm->eOperator = indexable==0 ? 0 : WO_OR;
109796 
109797   /*
109798   ** chngToIN holds a set of tables that *might* satisfy case 1.  But
109799   ** we have to do some additional checking to see if case 1 really
109800   ** is satisfied.
109801   **
109802   ** chngToIN will hold either 0, 1, or 2 bits.  The 0-bit case means
109803   ** that there is no possibility of transforming the OR clause into an
109804   ** IN operator because one or more terms in the OR clause contain
109805   ** something other than == on a column in the single table.  The 1-bit
109806   ** case means that every term of the OR clause is of the form
109807   ** "table.column=expr" for some single table.  The one bit that is set
109808   ** will correspond to the common table.  We still need to check to make
109809   ** sure the same column is used on all terms.  The 2-bit case is when
109810   ** the all terms are of the form "table1.column=table2.column".  It
109811   ** might be possible to form an IN operator with either table1.column
109812   ** or table2.column as the LHS if either is common to every term of
109813   ** the OR clause.
109814   **
109815   ** Note that terms of the form "table.column1=table.column2" (the
109816   ** same table on both sizes of the ==) cannot be optimized.
109817   */
109818   if( chngToIN ){
109819     int okToChngToIN = 0;     /* True if the conversion to IN is valid */
109820     int iColumn = -1;         /* Column index on lhs of IN operator */
109821     int iCursor = -1;         /* Table cursor common to all terms */
109822     int j = 0;                /* Loop counter */
109823 
109824     /* Search for a table and column that appears on one side or the
109825     ** other of the == operator in every subterm.  That table and column
109826     ** will be recorded in iCursor and iColumn.  There might not be any
109827     ** such table and column.  Set okToChngToIN if an appropriate table
109828     ** and column is found but leave okToChngToIN false if not found.
109829     */
109830     for(j=0; j<2 && !okToChngToIN; j++){
109831       pOrTerm = pOrWc->a;
109832       for(i=pOrWc->nTerm-1; i>=0; i--, pOrTerm++){
109833         assert( pOrTerm->eOperator & WO_EQ );
109834         pOrTerm->wtFlags &= ~TERM_OR_OK;
109835         if( pOrTerm->leftCursor==iCursor ){
109836           /* This is the 2-bit case and we are on the second iteration and
109837           ** current term is from the first iteration.  So skip this term. */
109838           assert( j==1 );
109839           continue;
109840         }
109841         if( (chngToIN & getMask(&pWInfo->sMaskSet, pOrTerm->leftCursor))==0 ){
109842           /* This term must be of the form t1.a==t2.b where t2 is in the
109843           ** chngToIN set but t1 is not.  This term will be either preceeded
109844           ** or follwed by an inverted copy (t2.b==t1.a).  Skip this term
109845           ** and use its inversion. */
109846           testcase( pOrTerm->wtFlags & TERM_COPIED );
109847           testcase( pOrTerm->wtFlags & TERM_VIRTUAL );
109848           assert( pOrTerm->wtFlags & (TERM_COPIED|TERM_VIRTUAL) );
109849           continue;
109850         }
109851         iColumn = pOrTerm->u.leftColumn;
109852         iCursor = pOrTerm->leftCursor;
109853         break;
109854       }
109855       if( i<0 ){
109856         /* No candidate table+column was found.  This can only occur
109857         ** on the second iteration */
109858         assert( j==1 );
109859         assert( IsPowerOfTwo(chngToIN) );
109860         assert( chngToIN==getMask(&pWInfo->sMaskSet, iCursor) );
109861         break;
109862       }
109863       testcase( j==1 );
109864 
109865       /* We have found a candidate table and column.  Check to see if that
109866       ** table and column is common to every term in the OR clause */
109867       okToChngToIN = 1;
109868       for(; i>=0 && okToChngToIN; i--, pOrTerm++){
109869         assert( pOrTerm->eOperator & WO_EQ );
109870         if( pOrTerm->leftCursor!=iCursor ){
109871           pOrTerm->wtFlags &= ~TERM_OR_OK;
109872         }else if( pOrTerm->u.leftColumn!=iColumn ){
109873           okToChngToIN = 0;
109874         }else{
109875           int affLeft, affRight;
109876           /* If the right-hand side is also a column, then the affinities
109877           ** of both right and left sides must be such that no type
109878           ** conversions are required on the right.  (Ticket #2249)
109879           */
109880           affRight = sqlite3ExprAffinity(pOrTerm->pExpr->pRight);
109881           affLeft = sqlite3ExprAffinity(pOrTerm->pExpr->pLeft);
109882           if( affRight!=0 && affRight!=affLeft ){
109883             okToChngToIN = 0;
109884           }else{
109885             pOrTerm->wtFlags |= TERM_OR_OK;
109886           }
109887         }
109888       }
109889     }
109890 
109891     /* At this point, okToChngToIN is true if original pTerm satisfies
109892     ** case 1.  In that case, construct a new virtual term that is
109893     ** pTerm converted into an IN operator.
109894     */
109895     if( okToChngToIN ){
109896       Expr *pDup;            /* A transient duplicate expression */
109897       ExprList *pList = 0;   /* The RHS of the IN operator */
109898       Expr *pLeft = 0;       /* The LHS of the IN operator */
109899       Expr *pNew;            /* The complete IN operator */
109900 
109901       for(i=pOrWc->nTerm-1, pOrTerm=pOrWc->a; i>=0; i--, pOrTerm++){
109902         if( (pOrTerm->wtFlags & TERM_OR_OK)==0 ) continue;
109903         assert( pOrTerm->eOperator & WO_EQ );
109904         assert( pOrTerm->leftCursor==iCursor );
109905         assert( pOrTerm->u.leftColumn==iColumn );
109906         pDup = sqlite3ExprDup(db, pOrTerm->pExpr->pRight, 0);
109907         pList = sqlite3ExprListAppend(pWInfo->pParse, pList, pDup);
109908         pLeft = pOrTerm->pExpr->pLeft;
109909       }
109910       assert( pLeft!=0 );
109911       pDup = sqlite3ExprDup(db, pLeft, 0);
109912       pNew = sqlite3PExpr(pParse, TK_IN, pDup, 0, 0);
109913       if( pNew ){
109914         int idxNew;
109915         transferJoinMarkings(pNew, pExpr);
109916         assert( !ExprHasProperty(pNew, EP_xIsSelect) );
109917         pNew->x.pList = pList;
109918         idxNew = whereClauseInsert(pWC, pNew, TERM_VIRTUAL|TERM_DYNAMIC);
109919         testcase( idxNew==0 );
109920         exprAnalyze(pSrc, pWC, idxNew);
109921         pTerm = &pWC->a[idxTerm];
109922         pWC->a[idxNew].iParent = idxTerm;
109923         pTerm->nChild = 1;
109924       }else{
109925         sqlite3ExprListDelete(db, pList);
109926       }
109927       pTerm->eOperator = WO_NOOP;  /* case 1 trumps case 2 */
109928     }
109929   }
109930 }
109931 #endif /* !SQLITE_OMIT_OR_OPTIMIZATION && !SQLITE_OMIT_SUBQUERY */
109932 
109933 /*
109934 ** The input to this routine is an WhereTerm structure with only the
109935 ** "pExpr" field filled in.  The job of this routine is to analyze the
109936 ** subexpression and populate all the other fields of the WhereTerm
109937 ** structure.
109938 **
109939 ** If the expression is of the form "<expr> <op> X" it gets commuted
109940 ** to the standard form of "X <op> <expr>".
109941 **
109942 ** If the expression is of the form "X <op> Y" where both X and Y are
109943 ** columns, then the original expression is unchanged and a new virtual
109944 ** term of the form "Y <op> X" is added to the WHERE clause and
109945 ** analyzed separately.  The original term is marked with TERM_COPIED
109946 ** and the new term is marked with TERM_DYNAMIC (because it's pExpr
109947 ** needs to be freed with the WhereClause) and TERM_VIRTUAL (because it
109948 ** is a commuted copy of a prior term.)  The original term has nChild=1
109949 ** and the copy has idxParent set to the index of the original term.
109950 */
109951 static void exprAnalyze(
109952   SrcList *pSrc,            /* the FROM clause */
109953   WhereClause *pWC,         /* the WHERE clause */
109954   int idxTerm               /* Index of the term to be analyzed */
109955 ){
109956   WhereInfo *pWInfo = pWC->pWInfo; /* WHERE clause processing context */
109957   WhereTerm *pTerm;                /* The term to be analyzed */
109958   WhereMaskSet *pMaskSet;          /* Set of table index masks */
109959   Expr *pExpr;                     /* The expression to be analyzed */
109960   Bitmask prereqLeft;              /* Prerequesites of the pExpr->pLeft */
109961   Bitmask prereqAll;               /* Prerequesites of pExpr */
109962   Bitmask extraRight = 0;          /* Extra dependencies on LEFT JOIN */
109963   Expr *pStr1 = 0;                 /* RHS of LIKE/GLOB operator */
109964   int isComplete = 0;              /* RHS of LIKE/GLOB ends with wildcard */
109965   int noCase = 0;                  /* LIKE/GLOB distinguishes case */
109966   int op;                          /* Top-level operator.  pExpr->op */
109967   Parse *pParse = pWInfo->pParse;  /* Parsing context */
109968   sqlite3 *db = pParse->db;        /* Database connection */
109969 
109970   if( db->mallocFailed ){
109971     return;
109972   }
109973   pTerm = &pWC->a[idxTerm];
109974   pMaskSet = &pWInfo->sMaskSet;
109975   pExpr = pTerm->pExpr;
109976   assert( pExpr->op!=TK_AS && pExpr->op!=TK_COLLATE );
109977   prereqLeft = exprTableUsage(pMaskSet, pExpr->pLeft);
109978   op = pExpr->op;
109979   if( op==TK_IN ){
109980     assert( pExpr->pRight==0 );
109981     if( ExprHasProperty(pExpr, EP_xIsSelect) ){
109982       pTerm->prereqRight = exprSelectTableUsage(pMaskSet, pExpr->x.pSelect);
109983     }else{
109984       pTerm->prereqRight = exprListTableUsage(pMaskSet, pExpr->x.pList);
109985     }
109986   }else if( op==TK_ISNULL ){
109987     pTerm->prereqRight = 0;
109988   }else{
109989     pTerm->prereqRight = exprTableUsage(pMaskSet, pExpr->pRight);
109990   }
109991   prereqAll = exprTableUsage(pMaskSet, pExpr);
109992   if( ExprHasProperty(pExpr, EP_FromJoin) ){
109993     Bitmask x = getMask(pMaskSet, pExpr->iRightJoinTable);
109994     prereqAll |= x;
109995     extraRight = x-1;  /* ON clause terms may not be used with an index
109996                        ** on left table of a LEFT JOIN.  Ticket #3015 */
109997   }
109998   pTerm->prereqAll = prereqAll;
109999   pTerm->leftCursor = -1;
110000   pTerm->iParent = -1;
110001   pTerm->eOperator = 0;
110002   if( allowedOp(op) ){
110003     Expr *pLeft = sqlite3ExprSkipCollate(pExpr->pLeft);
110004     Expr *pRight = sqlite3ExprSkipCollate(pExpr->pRight);
110005     u16 opMask = (pTerm->prereqRight & prereqLeft)==0 ? WO_ALL : WO_EQUIV;
110006     if( pLeft->op==TK_COLUMN ){
110007       pTerm->leftCursor = pLeft->iTable;
110008       pTerm->u.leftColumn = pLeft->iColumn;
110009       pTerm->eOperator = operatorMask(op) & opMask;
110010     }
110011     if( pRight && pRight->op==TK_COLUMN ){
110012       WhereTerm *pNew;
110013       Expr *pDup;
110014       u16 eExtraOp = 0;        /* Extra bits for pNew->eOperator */
110015       if( pTerm->leftCursor>=0 ){
110016         int idxNew;
110017         pDup = sqlite3ExprDup(db, pExpr, 0);
110018         if( db->mallocFailed ){
110019           sqlite3ExprDelete(db, pDup);
110020           return;
110021         }
110022         idxNew = whereClauseInsert(pWC, pDup, TERM_VIRTUAL|TERM_DYNAMIC);
110023         if( idxNew==0 ) return;
110024         pNew = &pWC->a[idxNew];
110025         pNew->iParent = idxTerm;
110026         pTerm = &pWC->a[idxTerm];
110027         pTerm->nChild = 1;
110028         pTerm->wtFlags |= TERM_COPIED;
110029         if( pExpr->op==TK_EQ
110030          && !ExprHasProperty(pExpr, EP_FromJoin)
110031          && OptimizationEnabled(db, SQLITE_Transitive)
110032         ){
110033           pTerm->eOperator |= WO_EQUIV;
110034           eExtraOp = WO_EQUIV;
110035         }
110036       }else{
110037         pDup = pExpr;
110038         pNew = pTerm;
110039       }
110040       exprCommute(pParse, pDup);
110041       pLeft = sqlite3ExprSkipCollate(pDup->pLeft);
110042       pNew->leftCursor = pLeft->iTable;
110043       pNew->u.leftColumn = pLeft->iColumn;
110044       testcase( (prereqLeft | extraRight) != prereqLeft );
110045       pNew->prereqRight = prereqLeft | extraRight;
110046       pNew->prereqAll = prereqAll;
110047       pNew->eOperator = (operatorMask(pDup->op) + eExtraOp) & opMask;
110048     }
110049   }
110050 
110051 #ifndef SQLITE_OMIT_BETWEEN_OPTIMIZATION
110052   /* If a term is the BETWEEN operator, create two new virtual terms
110053   ** that define the range that the BETWEEN implements.  For example:
110054   **
110055   **      a BETWEEN b AND c
110056   **
110057   ** is converted into:
110058   **
110059   **      (a BETWEEN b AND c) AND (a>=b) AND (a<=c)
110060   **
110061   ** The two new terms are added onto the end of the WhereClause object.
110062   ** The new terms are "dynamic" and are children of the original BETWEEN
110063   ** term.  That means that if the BETWEEN term is coded, the children are
110064   ** skipped.  Or, if the children are satisfied by an index, the original
110065   ** BETWEEN term is skipped.
110066   */
110067   else if( pExpr->op==TK_BETWEEN && pWC->op==TK_AND ){
110068     ExprList *pList = pExpr->x.pList;
110069     int i;
110070     static const u8 ops[] = {TK_GE, TK_LE};
110071     assert( pList!=0 );
110072     assert( pList->nExpr==2 );
110073     for(i=0; i<2; i++){
110074       Expr *pNewExpr;
110075       int idxNew;
110076       pNewExpr = sqlite3PExpr(pParse, ops[i],
110077                              sqlite3ExprDup(db, pExpr->pLeft, 0),
110078                              sqlite3ExprDup(db, pList->a[i].pExpr, 0), 0);
110079       transferJoinMarkings(pNewExpr, pExpr);
110080       idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC);
110081       testcase( idxNew==0 );
110082       exprAnalyze(pSrc, pWC, idxNew);
110083       pTerm = &pWC->a[idxTerm];
110084       pWC->a[idxNew].iParent = idxTerm;
110085     }
110086     pTerm->nChild = 2;
110087   }
110088 #endif /* SQLITE_OMIT_BETWEEN_OPTIMIZATION */
110089 
110090 #if !defined(SQLITE_OMIT_OR_OPTIMIZATION) && !defined(SQLITE_OMIT_SUBQUERY)
110091   /* Analyze a term that is composed of two or more subterms connected by
110092   ** an OR operator.
110093   */
110094   else if( pExpr->op==TK_OR ){
110095     assert( pWC->op==TK_AND );
110096     exprAnalyzeOrTerm(pSrc, pWC, idxTerm);
110097     pTerm = &pWC->a[idxTerm];
110098   }
110099 #endif /* SQLITE_OMIT_OR_OPTIMIZATION */
110100 
110101 #ifndef SQLITE_OMIT_LIKE_OPTIMIZATION
110102   /* Add constraints to reduce the search space on a LIKE or GLOB
110103   ** operator.
110104   **
110105   ** A like pattern of the form "x LIKE 'abc%'" is changed into constraints
110106   **
110107   **          x>='abc' AND x<'abd' AND x LIKE 'abc%'
110108   **
110109   ** The last character of the prefix "abc" is incremented to form the
110110   ** termination condition "abd".
110111   */
110112   if( pWC->op==TK_AND
110113    && isLikeOrGlob(pParse, pExpr, &pStr1, &isComplete, &noCase)
110114   ){
110115     Expr *pLeft;       /* LHS of LIKE/GLOB operator */
110116     Expr *pStr2;       /* Copy of pStr1 - RHS of LIKE/GLOB operator */
110117     Expr *pNewExpr1;
110118     Expr *pNewExpr2;
110119     int idxNew1;
110120     int idxNew2;
110121     Token sCollSeqName;  /* Name of collating sequence */
110122 
110123     pLeft = pExpr->x.pList->a[1].pExpr;
110124     pStr2 = sqlite3ExprDup(db, pStr1, 0);
110125     if( !db->mallocFailed ){
110126       u8 c, *pC;       /* Last character before the first wildcard */
110127       pC = (u8*)&pStr2->u.zToken[sqlite3Strlen30(pStr2->u.zToken)-1];
110128       c = *pC;
110129       if( noCase ){
110130         /* The point is to increment the last character before the first
110131         ** wildcard.  But if we increment '@', that will push it into the
110132         ** alphabetic range where case conversions will mess up the
110133         ** inequality.  To avoid this, make sure to also run the full
110134         ** LIKE on all candidate expressions by clearing the isComplete flag
110135         */
110136         if( c=='A'-1 ) isComplete = 0;
110137         c = sqlite3UpperToLower[c];
110138       }
110139       *pC = c + 1;
110140     }
110141     sCollSeqName.z = noCase ? "NOCASE" : "BINARY";
110142     sCollSeqName.n = 6;
110143     pNewExpr1 = sqlite3ExprDup(db, pLeft, 0);
110144     pNewExpr1 = sqlite3PExpr(pParse, TK_GE,
110145            sqlite3ExprAddCollateToken(pParse,pNewExpr1,&sCollSeqName),
110146            pStr1, 0);
110147     transferJoinMarkings(pNewExpr1, pExpr);
110148     idxNew1 = whereClauseInsert(pWC, pNewExpr1, TERM_VIRTUAL|TERM_DYNAMIC);
110149     testcase( idxNew1==0 );
110150     exprAnalyze(pSrc, pWC, idxNew1);
110151     pNewExpr2 = sqlite3ExprDup(db, pLeft, 0);
110152     pNewExpr2 = sqlite3PExpr(pParse, TK_LT,
110153            sqlite3ExprAddCollateToken(pParse,pNewExpr2,&sCollSeqName),
110154            pStr2, 0);
110155     transferJoinMarkings(pNewExpr2, pExpr);
110156     idxNew2 = whereClauseInsert(pWC, pNewExpr2, TERM_VIRTUAL|TERM_DYNAMIC);
110157     testcase( idxNew2==0 );
110158     exprAnalyze(pSrc, pWC, idxNew2);
110159     pTerm = &pWC->a[idxTerm];
110160     if( isComplete ){
110161       pWC->a[idxNew1].iParent = idxTerm;
110162       pWC->a[idxNew2].iParent = idxTerm;
110163       pTerm->nChild = 2;
110164     }
110165   }
110166 #endif /* SQLITE_OMIT_LIKE_OPTIMIZATION */
110167 
110168 #ifndef SQLITE_OMIT_VIRTUALTABLE
110169   /* Add a WO_MATCH auxiliary term to the constraint set if the
110170   ** current expression is of the form:  column MATCH expr.
110171   ** This information is used by the xBestIndex methods of
110172   ** virtual tables.  The native query optimizer does not attempt
110173   ** to do anything with MATCH functions.
110174   */
110175   if( isMatchOfColumn(pExpr) ){
110176     int idxNew;
110177     Expr *pRight, *pLeft;
110178     WhereTerm *pNewTerm;
110179     Bitmask prereqColumn, prereqExpr;
110180 
110181     pRight = pExpr->x.pList->a[0].pExpr;
110182     pLeft = pExpr->x.pList->a[1].pExpr;
110183     prereqExpr = exprTableUsage(pMaskSet, pRight);
110184     prereqColumn = exprTableUsage(pMaskSet, pLeft);
110185     if( (prereqExpr & prereqColumn)==0 ){
110186       Expr *pNewExpr;
110187       pNewExpr = sqlite3PExpr(pParse, TK_MATCH,
110188                               0, sqlite3ExprDup(db, pRight, 0), 0);
110189       idxNew = whereClauseInsert(pWC, pNewExpr, TERM_VIRTUAL|TERM_DYNAMIC);
110190       testcase( idxNew==0 );
110191       pNewTerm = &pWC->a[idxNew];
110192       pNewTerm->prereqRight = prereqExpr;
110193       pNewTerm->leftCursor = pLeft->iTable;
110194       pNewTerm->u.leftColumn = pLeft->iColumn;
110195       pNewTerm->eOperator = WO_MATCH;
110196       pNewTerm->iParent = idxTerm;
110197       pTerm = &pWC->a[idxTerm];
110198       pTerm->nChild = 1;
110199       pTerm->wtFlags |= TERM_COPIED;
110200       pNewTerm->prereqAll = pTerm->prereqAll;
110201     }
110202   }
110203 #endif /* SQLITE_OMIT_VIRTUALTABLE */
110204 
110205 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
110206   /* When sqlite_stat3 histogram data is available an operator of the
110207   ** form "x IS NOT NULL" can sometimes be evaluated more efficiently
110208   ** as "x>NULL" if x is not an INTEGER PRIMARY KEY.  So construct a
110209   ** virtual term of that form.
110210   **
110211   ** Note that the virtual term must be tagged with TERM_VNULL.  This
110212   ** TERM_VNULL tag will suppress the not-null check at the beginning
110213   ** of the loop.  Without the TERM_VNULL flag, the not-null check at
110214   ** the start of the loop will prevent any results from being returned.
110215   */
110216   if( pExpr->op==TK_NOTNULL
110217    && pExpr->pLeft->op==TK_COLUMN
110218    && pExpr->pLeft->iColumn>=0
110219    && OptimizationEnabled(db, SQLITE_Stat3)
110220   ){
110221     Expr *pNewExpr;
110222     Expr *pLeft = pExpr->pLeft;
110223     int idxNew;
110224     WhereTerm *pNewTerm;
110225 
110226     pNewExpr = sqlite3PExpr(pParse, TK_GT,
110227                             sqlite3ExprDup(db, pLeft, 0),
110228                             sqlite3PExpr(pParse, TK_NULL, 0, 0, 0), 0);
110229 
110230     idxNew = whereClauseInsert(pWC, pNewExpr,
110231                               TERM_VIRTUAL|TERM_DYNAMIC|TERM_VNULL);
110232     if( idxNew ){
110233       pNewTerm = &pWC->a[idxNew];
110234       pNewTerm->prereqRight = 0;
110235       pNewTerm->leftCursor = pLeft->iTable;
110236       pNewTerm->u.leftColumn = pLeft->iColumn;
110237       pNewTerm->eOperator = WO_GT;
110238       pNewTerm->iParent = idxTerm;
110239       pTerm = &pWC->a[idxTerm];
110240       pTerm->nChild = 1;
110241       pTerm->wtFlags |= TERM_COPIED;
110242       pNewTerm->prereqAll = pTerm->prereqAll;
110243     }
110244   }
110245 #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
110246 
110247   /* Prevent ON clause terms of a LEFT JOIN from being used to drive
110248   ** an index for tables to the left of the join.
110249   */
110250   pTerm->prereqRight |= extraRight;
110251 }
110252 
110253 /*
110254 ** This function searches pList for a entry that matches the iCol-th column
110255 ** of index pIdx.
110256 **
110257 ** If such an expression is found, its index in pList->a[] is returned. If
110258 ** no expression is found, -1 is returned.
110259 */
110260 static int findIndexCol(
110261   Parse *pParse,                  /* Parse context */
110262   ExprList *pList,                /* Expression list to search */
110263   int iBase,                      /* Cursor for table associated with pIdx */
110264   Index *pIdx,                    /* Index to match column of */
110265   int iCol                        /* Column of index to match */
110266 ){
110267   int i;
110268   const char *zColl = pIdx->azColl[iCol];
110269 
110270   for(i=0; i<pList->nExpr; i++){
110271     Expr *p = sqlite3ExprSkipCollate(pList->a[i].pExpr);
110272     if( p->op==TK_COLUMN
110273      && p->iColumn==pIdx->aiColumn[iCol]
110274      && p->iTable==iBase
110275     ){
110276       CollSeq *pColl = sqlite3ExprCollSeq(pParse, pList->a[i].pExpr);
110277       if( ALWAYS(pColl) && 0==sqlite3StrICmp(pColl->zName, zColl) ){
110278         return i;
110279       }
110280     }
110281   }
110282 
110283   return -1;
110284 }
110285 
110286 /*
110287 ** Return true if the DISTINCT expression-list passed as the third argument
110288 ** is redundant.
110289 **
110290 ** A DISTINCT list is redundant if the database contains some subset of
110291 ** columns that are unique and non-null.
110292 */
110293 static int isDistinctRedundant(
110294   Parse *pParse,            /* Parsing context */
110295   SrcList *pTabList,        /* The FROM clause */
110296   WhereClause *pWC,         /* The WHERE clause */
110297   ExprList *pDistinct       /* The result set that needs to be DISTINCT */
110298 ){
110299   Table *pTab;
110300   Index *pIdx;
110301   int i;
110302   int iBase;
110303 
110304   /* If there is more than one table or sub-select in the FROM clause of
110305   ** this query, then it will not be possible to show that the DISTINCT
110306   ** clause is redundant. */
110307   if( pTabList->nSrc!=1 ) return 0;
110308   iBase = pTabList->a[0].iCursor;
110309   pTab = pTabList->a[0].pTab;
110310 
110311   /* If any of the expressions is an IPK column on table iBase, then return
110312   ** true. Note: The (p->iTable==iBase) part of this test may be false if the
110313   ** current SELECT is a correlated sub-query.
110314   */
110315   for(i=0; i<pDistinct->nExpr; i++){
110316     Expr *p = sqlite3ExprSkipCollate(pDistinct->a[i].pExpr);
110317     if( p->op==TK_COLUMN && p->iTable==iBase && p->iColumn<0 ) return 1;
110318   }
110319 
110320   /* Loop through all indices on the table, checking each to see if it makes
110321   ** the DISTINCT qualifier redundant. It does so if:
110322   **
110323   **   1. The index is itself UNIQUE, and
110324   **
110325   **   2. All of the columns in the index are either part of the pDistinct
110326   **      list, or else the WHERE clause contains a term of the form "col=X",
110327   **      where X is a constant value. The collation sequences of the
110328   **      comparison and select-list expressions must match those of the index.
110329   **
110330   **   3. All of those index columns for which the WHERE clause does not
110331   **      contain a "col=X" term are subject to a NOT NULL constraint.
110332   */
110333   for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
110334     if( pIdx->onError==OE_None ) continue;
110335     for(i=0; i<pIdx->nKeyCol; i++){
110336       i16 iCol = pIdx->aiColumn[i];
110337       if( 0==findTerm(pWC, iBase, iCol, ~(Bitmask)0, WO_EQ, pIdx) ){
110338         int iIdxCol = findIndexCol(pParse, pDistinct, iBase, pIdx, i);
110339         if( iIdxCol<0 || pTab->aCol[iCol].notNull==0 ){
110340           break;
110341         }
110342       }
110343     }
110344     if( i==pIdx->nKeyCol ){
110345       /* This index implies that the DISTINCT qualifier is redundant. */
110346       return 1;
110347     }
110348   }
110349 
110350   return 0;
110351 }
110352 
110353 
110354 /*
110355 ** Estimate the logarithm of the input value to base 2.
110356 */
110357 static LogEst estLog(LogEst N){
110358   LogEst x = sqlite3LogEst(N);
110359   return x>33 ? x - 33 : 0;
110360 }
110361 
110362 /*
110363 ** Two routines for printing the content of an sqlite3_index_info
110364 ** structure.  Used for testing and debugging only.  If neither
110365 ** SQLITE_TEST or SQLITE_DEBUG are defined, then these routines
110366 ** are no-ops.
110367 */
110368 #if !defined(SQLITE_OMIT_VIRTUALTABLE) && defined(WHERETRACE_ENABLED)
110369 static void TRACE_IDX_INPUTS(sqlite3_index_info *p){
110370   int i;
110371   if( !sqlite3WhereTrace ) return;
110372   for(i=0; i<p->nConstraint; i++){
110373     sqlite3DebugPrintf("  constraint[%d]: col=%d termid=%d op=%d usabled=%d\n",
110374        i,
110375        p->aConstraint[i].iColumn,
110376        p->aConstraint[i].iTermOffset,
110377        p->aConstraint[i].op,
110378        p->aConstraint[i].usable);
110379   }
110380   for(i=0; i<p->nOrderBy; i++){
110381     sqlite3DebugPrintf("  orderby[%d]: col=%d desc=%d\n",
110382        i,
110383        p->aOrderBy[i].iColumn,
110384        p->aOrderBy[i].desc);
110385   }
110386 }
110387 static void TRACE_IDX_OUTPUTS(sqlite3_index_info *p){
110388   int i;
110389   if( !sqlite3WhereTrace ) return;
110390   for(i=0; i<p->nConstraint; i++){
110391     sqlite3DebugPrintf("  usage[%d]: argvIdx=%d omit=%d\n",
110392        i,
110393        p->aConstraintUsage[i].argvIndex,
110394        p->aConstraintUsage[i].omit);
110395   }
110396   sqlite3DebugPrintf("  idxNum=%d\n", p->idxNum);
110397   sqlite3DebugPrintf("  idxStr=%s\n", p->idxStr);
110398   sqlite3DebugPrintf("  orderByConsumed=%d\n", p->orderByConsumed);
110399   sqlite3DebugPrintf("  estimatedCost=%g\n", p->estimatedCost);
110400   sqlite3DebugPrintf("  estimatedRows=%lld\n", p->estimatedRows);
110401 }
110402 #else
110403 #define TRACE_IDX_INPUTS(A)
110404 #define TRACE_IDX_OUTPUTS(A)
110405 #endif
110406 
110407 #ifndef SQLITE_OMIT_AUTOMATIC_INDEX
110408 /*
110409 ** Return TRUE if the WHERE clause term pTerm is of a form where it
110410 ** could be used with an index to access pSrc, assuming an appropriate
110411 ** index existed.
110412 */
110413 static int termCanDriveIndex(
110414   WhereTerm *pTerm,              /* WHERE clause term to check */
110415   struct SrcList_item *pSrc,     /* Table we are trying to access */
110416   Bitmask notReady               /* Tables in outer loops of the join */
110417 ){
110418   char aff;
110419   if( pTerm->leftCursor!=pSrc->iCursor ) return 0;
110420   if( (pTerm->eOperator & WO_EQ)==0 ) return 0;
110421   if( (pTerm->prereqRight & notReady)!=0 ) return 0;
110422   if( pTerm->u.leftColumn<0 ) return 0;
110423   aff = pSrc->pTab->aCol[pTerm->u.leftColumn].affinity;
110424   if( !sqlite3IndexAffinityOk(pTerm->pExpr, aff) ) return 0;
110425   return 1;
110426 }
110427 #endif
110428 
110429 
110430 #ifndef SQLITE_OMIT_AUTOMATIC_INDEX
110431 /*
110432 ** Generate code to construct the Index object for an automatic index
110433 ** and to set up the WhereLevel object pLevel so that the code generator
110434 ** makes use of the automatic index.
110435 */
110436 static void constructAutomaticIndex(
110437   Parse *pParse,              /* The parsing context */
110438   WhereClause *pWC,           /* The WHERE clause */
110439   struct SrcList_item *pSrc,  /* The FROM clause term to get the next index */
110440   Bitmask notReady,           /* Mask of cursors that are not available */
110441   WhereLevel *pLevel          /* Write new index here */
110442 ){
110443   int nKeyCol;                /* Number of columns in the constructed index */
110444   WhereTerm *pTerm;           /* A single term of the WHERE clause */
110445   WhereTerm *pWCEnd;          /* End of pWC->a[] */
110446   Index *pIdx;                /* Object describing the transient index */
110447   Vdbe *v;                    /* Prepared statement under construction */
110448   int addrInit;               /* Address of the initialization bypass jump */
110449   Table *pTable;              /* The table being indexed */
110450   int addrTop;                /* Top of the index fill loop */
110451   int regRecord;              /* Register holding an index record */
110452   int n;                      /* Column counter */
110453   int i;                      /* Loop counter */
110454   int mxBitCol;               /* Maximum column in pSrc->colUsed */
110455   CollSeq *pColl;             /* Collating sequence to on a column */
110456   WhereLoop *pLoop;           /* The Loop object */
110457   char *zNotUsed;             /* Extra space on the end of pIdx */
110458   Bitmask idxCols;            /* Bitmap of columns used for indexing */
110459   Bitmask extraCols;          /* Bitmap of additional columns */
110460   u8 sentWarning = 0;         /* True if a warnning has been issued */
110461 
110462   /* Generate code to skip over the creation and initialization of the
110463   ** transient index on 2nd and subsequent iterations of the loop. */
110464   v = pParse->pVdbe;
110465   assert( v!=0 );
110466   addrInit = sqlite3CodeOnce(pParse);
110467 
110468   /* Count the number of columns that will be added to the index
110469   ** and used to match WHERE clause constraints */
110470   nKeyCol = 0;
110471   pTable = pSrc->pTab;
110472   pWCEnd = &pWC->a[pWC->nTerm];
110473   pLoop = pLevel->pWLoop;
110474   idxCols = 0;
110475   for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
110476     if( termCanDriveIndex(pTerm, pSrc, notReady) ){
110477       int iCol = pTerm->u.leftColumn;
110478       Bitmask cMask = iCol>=BMS ? MASKBIT(BMS-1) : MASKBIT(iCol);
110479       testcase( iCol==BMS );
110480       testcase( iCol==BMS-1 );
110481       if( !sentWarning ){
110482         sqlite3_log(SQLITE_WARNING_AUTOINDEX,
110483             "automatic index on %s(%s)", pTable->zName,
110484             pTable->aCol[iCol].zName);
110485         sentWarning = 1;
110486       }
110487       if( (idxCols & cMask)==0 ){
110488         if( whereLoopResize(pParse->db, pLoop, nKeyCol+1) ) return;
110489         pLoop->aLTerm[nKeyCol++] = pTerm;
110490         idxCols |= cMask;
110491       }
110492     }
110493   }
110494   assert( nKeyCol>0 );
110495   pLoop->u.btree.nEq = pLoop->nLTerm = nKeyCol;
110496   pLoop->wsFlags = WHERE_COLUMN_EQ | WHERE_IDX_ONLY | WHERE_INDEXED
110497                      | WHERE_AUTO_INDEX;
110498 
110499   /* Count the number of additional columns needed to create a
110500   ** covering index.  A "covering index" is an index that contains all
110501   ** columns that are needed by the query.  With a covering index, the
110502   ** original table never needs to be accessed.  Automatic indices must
110503   ** be a covering index because the index will not be updated if the
110504   ** original table changes and the index and table cannot both be used
110505   ** if they go out of sync.
110506   */
110507   extraCols = pSrc->colUsed & (~idxCols | MASKBIT(BMS-1));
110508   mxBitCol = (pTable->nCol >= BMS-1) ? BMS-1 : pTable->nCol;
110509   testcase( pTable->nCol==BMS-1 );
110510   testcase( pTable->nCol==BMS-2 );
110511   for(i=0; i<mxBitCol; i++){
110512     if( extraCols & MASKBIT(i) ) nKeyCol++;
110513   }
110514   if( pSrc->colUsed & MASKBIT(BMS-1) ){
110515     nKeyCol += pTable->nCol - BMS + 1;
110516   }
110517   pLoop->wsFlags |= WHERE_COLUMN_EQ | WHERE_IDX_ONLY;
110518 
110519   /* Construct the Index object to describe this index */
110520   pIdx = sqlite3AllocateIndexObject(pParse->db, nKeyCol+1, 0, &zNotUsed);
110521   if( pIdx==0 ) return;
110522   pLoop->u.btree.pIndex = pIdx;
110523   pIdx->zName = "auto-index";
110524   pIdx->pTable = pTable;
110525   n = 0;
110526   idxCols = 0;
110527   for(pTerm=pWC->a; pTerm<pWCEnd; pTerm++){
110528     if( termCanDriveIndex(pTerm, pSrc, notReady) ){
110529       int iCol = pTerm->u.leftColumn;
110530       Bitmask cMask = iCol>=BMS ? MASKBIT(BMS-1) : MASKBIT(iCol);
110531       testcase( iCol==BMS-1 );
110532       testcase( iCol==BMS );
110533       if( (idxCols & cMask)==0 ){
110534         Expr *pX = pTerm->pExpr;
110535         idxCols |= cMask;
110536         pIdx->aiColumn[n] = pTerm->u.leftColumn;
110537         pColl = sqlite3BinaryCompareCollSeq(pParse, pX->pLeft, pX->pRight);
110538         pIdx->azColl[n] = ALWAYS(pColl) ? pColl->zName : "BINARY";
110539         n++;
110540       }
110541     }
110542   }
110543   assert( (u32)n==pLoop->u.btree.nEq );
110544 
110545   /* Add additional columns needed to make the automatic index into
110546   ** a covering index */
110547   for(i=0; i<mxBitCol; i++){
110548     if( extraCols & MASKBIT(i) ){
110549       pIdx->aiColumn[n] = i;
110550       pIdx->azColl[n] = "BINARY";
110551       n++;
110552     }
110553   }
110554   if( pSrc->colUsed & MASKBIT(BMS-1) ){
110555     for(i=BMS-1; i<pTable->nCol; i++){
110556       pIdx->aiColumn[n] = i;
110557       pIdx->azColl[n] = "BINARY";
110558       n++;
110559     }
110560   }
110561   assert( n==nKeyCol );
110562   pIdx->aiColumn[n] = -1;
110563   pIdx->azColl[n] = "BINARY";
110564 
110565   /* Create the automatic index */
110566   assert( pLevel->iIdxCur>=0 );
110567   pLevel->iIdxCur = pParse->nTab++;
110568   sqlite3VdbeAddOp2(v, OP_OpenAutoindex, pLevel->iIdxCur, nKeyCol+1);
110569   sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
110570   VdbeComment((v, "for %s", pTable->zName));
110571 
110572   /* Fill the automatic index with content */
110573   addrTop = sqlite3VdbeAddOp1(v, OP_Rewind, pLevel->iTabCur);
110574   regRecord = sqlite3GetTempReg(pParse);
110575   sqlite3GenerateIndexKey(pParse, pIdx, pLevel->iTabCur, regRecord, 0, 0, 0, 0);
110576   sqlite3VdbeAddOp2(v, OP_IdxInsert, pLevel->iIdxCur, regRecord);
110577   sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
110578   sqlite3VdbeAddOp2(v, OP_Next, pLevel->iTabCur, addrTop+1);
110579   sqlite3VdbeChangeP5(v, SQLITE_STMTSTATUS_AUTOINDEX);
110580   sqlite3VdbeJumpHere(v, addrTop);
110581   sqlite3ReleaseTempReg(pParse, regRecord);
110582 
110583   /* Jump here when skipping the initialization */
110584   sqlite3VdbeJumpHere(v, addrInit);
110585 }
110586 #endif /* SQLITE_OMIT_AUTOMATIC_INDEX */
110587 
110588 #ifndef SQLITE_OMIT_VIRTUALTABLE
110589 /*
110590 ** Allocate and populate an sqlite3_index_info structure. It is the
110591 ** responsibility of the caller to eventually release the structure
110592 ** by passing the pointer returned by this function to sqlite3_free().
110593 */
110594 static sqlite3_index_info *allocateIndexInfo(
110595   Parse *pParse,
110596   WhereClause *pWC,
110597   struct SrcList_item *pSrc,
110598   ExprList *pOrderBy
110599 ){
110600   int i, j;
110601   int nTerm;
110602   struct sqlite3_index_constraint *pIdxCons;
110603   struct sqlite3_index_orderby *pIdxOrderBy;
110604   struct sqlite3_index_constraint_usage *pUsage;
110605   WhereTerm *pTerm;
110606   int nOrderBy;
110607   sqlite3_index_info *pIdxInfo;
110608 
110609   /* Count the number of possible WHERE clause constraints referring
110610   ** to this virtual table */
110611   for(i=nTerm=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
110612     if( pTerm->leftCursor != pSrc->iCursor ) continue;
110613     assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) );
110614     testcase( pTerm->eOperator & WO_IN );
110615     testcase( pTerm->eOperator & WO_ISNULL );
110616     testcase( pTerm->eOperator & WO_ALL );
110617     if( (pTerm->eOperator & ~(WO_ISNULL|WO_EQUIV))==0 ) continue;
110618     if( pTerm->wtFlags & TERM_VNULL ) continue;
110619     nTerm++;
110620   }
110621 
110622   /* If the ORDER BY clause contains only columns in the current
110623   ** virtual table then allocate space for the aOrderBy part of
110624   ** the sqlite3_index_info structure.
110625   */
110626   nOrderBy = 0;
110627   if( pOrderBy ){
110628     int n = pOrderBy->nExpr;
110629     for(i=0; i<n; i++){
110630       Expr *pExpr = pOrderBy->a[i].pExpr;
110631       if( pExpr->op!=TK_COLUMN || pExpr->iTable!=pSrc->iCursor ) break;
110632     }
110633     if( i==n){
110634       nOrderBy = n;
110635     }
110636   }
110637 
110638   /* Allocate the sqlite3_index_info structure
110639   */
110640   pIdxInfo = sqlite3DbMallocZero(pParse->db, sizeof(*pIdxInfo)
110641                            + (sizeof(*pIdxCons) + sizeof(*pUsage))*nTerm
110642                            + sizeof(*pIdxOrderBy)*nOrderBy );
110643   if( pIdxInfo==0 ){
110644     sqlite3ErrorMsg(pParse, "out of memory");
110645     return 0;
110646   }
110647 
110648   /* Initialize the structure.  The sqlite3_index_info structure contains
110649   ** many fields that are declared "const" to prevent xBestIndex from
110650   ** changing them.  We have to do some funky casting in order to
110651   ** initialize those fields.
110652   */
110653   pIdxCons = (struct sqlite3_index_constraint*)&pIdxInfo[1];
110654   pIdxOrderBy = (struct sqlite3_index_orderby*)&pIdxCons[nTerm];
110655   pUsage = (struct sqlite3_index_constraint_usage*)&pIdxOrderBy[nOrderBy];
110656   *(int*)&pIdxInfo->nConstraint = nTerm;
110657   *(int*)&pIdxInfo->nOrderBy = nOrderBy;
110658   *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint = pIdxCons;
110659   *(struct sqlite3_index_orderby**)&pIdxInfo->aOrderBy = pIdxOrderBy;
110660   *(struct sqlite3_index_constraint_usage**)&pIdxInfo->aConstraintUsage =
110661                                                                    pUsage;
110662 
110663   for(i=j=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
110664     u8 op;
110665     if( pTerm->leftCursor != pSrc->iCursor ) continue;
110666     assert( IsPowerOfTwo(pTerm->eOperator & ~WO_EQUIV) );
110667     testcase( pTerm->eOperator & WO_IN );
110668     testcase( pTerm->eOperator & WO_ISNULL );
110669     testcase( pTerm->eOperator & WO_ALL );
110670     if( (pTerm->eOperator & ~(WO_ISNULL|WO_EQUIV))==0 ) continue;
110671     if( pTerm->wtFlags & TERM_VNULL ) continue;
110672     pIdxCons[j].iColumn = pTerm->u.leftColumn;
110673     pIdxCons[j].iTermOffset = i;
110674     op = (u8)pTerm->eOperator & WO_ALL;
110675     if( op==WO_IN ) op = WO_EQ;
110676     pIdxCons[j].op = op;
110677     /* The direct assignment in the previous line is possible only because
110678     ** the WO_ and SQLITE_INDEX_CONSTRAINT_ codes are identical.  The
110679     ** following asserts verify this fact. */
110680     assert( WO_EQ==SQLITE_INDEX_CONSTRAINT_EQ );
110681     assert( WO_LT==SQLITE_INDEX_CONSTRAINT_LT );
110682     assert( WO_LE==SQLITE_INDEX_CONSTRAINT_LE );
110683     assert( WO_GT==SQLITE_INDEX_CONSTRAINT_GT );
110684     assert( WO_GE==SQLITE_INDEX_CONSTRAINT_GE );
110685     assert( WO_MATCH==SQLITE_INDEX_CONSTRAINT_MATCH );
110686     assert( pTerm->eOperator & (WO_IN|WO_EQ|WO_LT|WO_LE|WO_GT|WO_GE|WO_MATCH) );
110687     j++;
110688   }
110689   for(i=0; i<nOrderBy; i++){
110690     Expr *pExpr = pOrderBy->a[i].pExpr;
110691     pIdxOrderBy[i].iColumn = pExpr->iColumn;
110692     pIdxOrderBy[i].desc = pOrderBy->a[i].sortOrder;
110693   }
110694 
110695   return pIdxInfo;
110696 }
110697 
110698 /*
110699 ** The table object reference passed as the second argument to this function
110700 ** must represent a virtual table. This function invokes the xBestIndex()
110701 ** method of the virtual table with the sqlite3_index_info object that
110702 ** comes in as the 3rd argument to this function.
110703 **
110704 ** If an error occurs, pParse is populated with an error message and a
110705 ** non-zero value is returned. Otherwise, 0 is returned and the output
110706 ** part of the sqlite3_index_info structure is left populated.
110707 **
110708 ** Whether or not an error is returned, it is the responsibility of the
110709 ** caller to eventually free p->idxStr if p->needToFreeIdxStr indicates
110710 ** that this is required.
110711 */
110712 static int vtabBestIndex(Parse *pParse, Table *pTab, sqlite3_index_info *p){
110713   sqlite3_vtab *pVtab = sqlite3GetVTable(pParse->db, pTab)->pVtab;
110714   int i;
110715   int rc;
110716 
110717   TRACE_IDX_INPUTS(p);
110718   rc = pVtab->pModule->xBestIndex(pVtab, p);
110719   TRACE_IDX_OUTPUTS(p);
110720 
110721   if( rc!=SQLITE_OK ){
110722     if( rc==SQLITE_NOMEM ){
110723       pParse->db->mallocFailed = 1;
110724     }else if( !pVtab->zErrMsg ){
110725       sqlite3ErrorMsg(pParse, "%s", sqlite3ErrStr(rc));
110726     }else{
110727       sqlite3ErrorMsg(pParse, "%s", pVtab->zErrMsg);
110728     }
110729   }
110730   sqlite3_free(pVtab->zErrMsg);
110731   pVtab->zErrMsg = 0;
110732 
110733   for(i=0; i<p->nConstraint; i++){
110734     if( !p->aConstraint[i].usable && p->aConstraintUsage[i].argvIndex>0 ){
110735       sqlite3ErrorMsg(pParse,
110736           "table %s: xBestIndex returned an invalid plan", pTab->zName);
110737     }
110738   }
110739 
110740   return pParse->nErr;
110741 }
110742 #endif /* !defined(SQLITE_OMIT_VIRTUALTABLE) */
110743 
110744 
110745 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
110746 /*
110747 ** Estimate the location of a particular key among all keys in an
110748 ** index.  Store the results in aStat as follows:
110749 **
110750 **    aStat[0]      Est. number of rows less than pVal
110751 **    aStat[1]      Est. number of rows equal to pVal
110752 **
110753 ** Return SQLITE_OK on success.
110754 */
110755 static void whereKeyStats(
110756   Parse *pParse,              /* Database connection */
110757   Index *pIdx,                /* Index to consider domain of */
110758   UnpackedRecord *pRec,       /* Vector of values to consider */
110759   int roundUp,                /* Round up if true.  Round down if false */
110760   tRowcnt *aStat              /* OUT: stats written here */
110761 ){
110762   IndexSample *aSample = pIdx->aSample;
110763   int iCol;                   /* Index of required stats in anEq[] etc. */
110764   int iMin = 0;               /* Smallest sample not yet tested */
110765   int i = pIdx->nSample;      /* Smallest sample larger than or equal to pRec */
110766   int iTest;                  /* Next sample to test */
110767   int res;                    /* Result of comparison operation */
110768 
110769 #ifndef SQLITE_DEBUG
110770   UNUSED_PARAMETER( pParse );
110771 #endif
110772   assert( pRec!=0 );
110773   iCol = pRec->nField - 1;
110774   assert( pIdx->nSample>0 );
110775   assert( pRec->nField>0 && iCol<pIdx->nSampleCol );
110776   do{
110777     iTest = (iMin+i)/2;
110778     res = sqlite3VdbeRecordCompare(aSample[iTest].n, aSample[iTest].p, pRec);
110779     if( res<0 ){
110780       iMin = iTest+1;
110781     }else{
110782       i = iTest;
110783     }
110784   }while( res && iMin<i );
110785 
110786 #ifdef SQLITE_DEBUG
110787   /* The following assert statements check that the binary search code
110788   ** above found the right answer. This block serves no purpose other
110789   ** than to invoke the asserts.  */
110790   if( res==0 ){
110791     /* If (res==0) is true, then sample $i must be equal to pRec */
110792     assert( i<pIdx->nSample );
110793     assert( 0==sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec)
110794          || pParse->db->mallocFailed );
110795   }else{
110796     /* Otherwise, pRec must be smaller than sample $i and larger than
110797     ** sample ($i-1).  */
110798     assert( i==pIdx->nSample
110799          || sqlite3VdbeRecordCompare(aSample[i].n, aSample[i].p, pRec)>0
110800          || pParse->db->mallocFailed );
110801     assert( i==0
110802          || sqlite3VdbeRecordCompare(aSample[i-1].n, aSample[i-1].p, pRec)<0
110803          || pParse->db->mallocFailed );
110804   }
110805 #endif /* ifdef SQLITE_DEBUG */
110806 
110807   /* At this point, aSample[i] is the first sample that is greater than
110808   ** or equal to pVal.  Or if i==pIdx->nSample, then all samples are less
110809   ** than pVal.  If aSample[i]==pVal, then res==0.
110810   */
110811   if( res==0 ){
110812     aStat[0] = aSample[i].anLt[iCol];
110813     aStat[1] = aSample[i].anEq[iCol];
110814   }else{
110815     tRowcnt iLower, iUpper, iGap;
110816     if( i==0 ){
110817       iLower = 0;
110818       iUpper = aSample[0].anLt[iCol];
110819     }else{
110820       iUpper = i>=pIdx->nSample ? pIdx->aiRowEst[0] : aSample[i].anLt[iCol];
110821       iLower = aSample[i-1].anEq[iCol] + aSample[i-1].anLt[iCol];
110822     }
110823     aStat[1] = (pIdx->nKeyCol>iCol ? pIdx->aAvgEq[iCol] : 1);
110824     if( iLower>=iUpper ){
110825       iGap = 0;
110826     }else{
110827       iGap = iUpper - iLower;
110828     }
110829     if( roundUp ){
110830       iGap = (iGap*2)/3;
110831     }else{
110832       iGap = iGap/3;
110833     }
110834     aStat[0] = iLower + iGap;
110835   }
110836 }
110837 #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
110838 
110839 /*
110840 ** This function is used to estimate the number of rows that will be visited
110841 ** by scanning an index for a range of values. The range may have an upper
110842 ** bound, a lower bound, or both. The WHERE clause terms that set the upper
110843 ** and lower bounds are represented by pLower and pUpper respectively. For
110844 ** example, assuming that index p is on t1(a):
110845 **
110846 **   ... FROM t1 WHERE a > ? AND a < ? ...
110847 **                    |_____|   |_____|
110848 **                       |         |
110849 **                     pLower    pUpper
110850 **
110851 ** If either of the upper or lower bound is not present, then NULL is passed in
110852 ** place of the corresponding WhereTerm.
110853 **
110854 ** The value in (pBuilder->pNew->u.btree.nEq) is the index of the index
110855 ** column subject to the range constraint. Or, equivalently, the number of
110856 ** equality constraints optimized by the proposed index scan. For example,
110857 ** assuming index p is on t1(a, b), and the SQL query is:
110858 **
110859 **   ... FROM t1 WHERE a = ? AND b > ? AND b < ? ...
110860 **
110861 ** then nEq is set to 1 (as the range restricted column, b, is the second
110862 ** left-most column of the index). Or, if the query is:
110863 **
110864 **   ... FROM t1 WHERE a > ? AND a < ? ...
110865 **
110866 ** then nEq is set to 0.
110867 **
110868 ** When this function is called, *pnOut is set to the sqlite3LogEst() of the
110869 ** number of rows that the index scan is expected to visit without
110870 ** considering the range constraints. If nEq is 0, this is the number of
110871 ** rows in the index. Assuming no error occurs, *pnOut is adjusted (reduced)
110872 ** to account for the range contraints pLower and pUpper.
110873 **
110874 ** In the absence of sqlite_stat4 ANALYZE data, or if such data cannot be
110875 ** used, each range inequality reduces the search space by a factor of 4.
110876 ** Hence a pair of constraints (x>? AND x<?) reduces the expected number of
110877 ** rows visited by a factor of 16.
110878 */
110879 static int whereRangeScanEst(
110880   Parse *pParse,       /* Parsing & code generating context */
110881   WhereLoopBuilder *pBuilder,
110882   WhereTerm *pLower,   /* Lower bound on the range. ex: "x>123" Might be NULL */
110883   WhereTerm *pUpper,   /* Upper bound on the range. ex: "x<455" Might be NULL */
110884   WhereLoop *pLoop     /* Modify the .nOut and maybe .rRun fields */
110885 ){
110886   int rc = SQLITE_OK;
110887   int nOut = pLoop->nOut;
110888   LogEst nNew;
110889 
110890 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
110891   Index *p = pLoop->u.btree.pIndex;
110892   int nEq = pLoop->u.btree.nEq;
110893 
110894   if( p->nSample>0
110895    && nEq==pBuilder->nRecValid
110896    && nEq<p->nSampleCol
110897    && OptimizationEnabled(pParse->db, SQLITE_Stat3)
110898   ){
110899     UnpackedRecord *pRec = pBuilder->pRec;
110900     tRowcnt a[2];
110901     u8 aff;
110902 
110903     /* Variable iLower will be set to the estimate of the number of rows in
110904     ** the index that are less than the lower bound of the range query. The
110905     ** lower bound being the concatenation of $P and $L, where $P is the
110906     ** key-prefix formed by the nEq values matched against the nEq left-most
110907     ** columns of the index, and $L is the value in pLower.
110908     **
110909     ** Or, if pLower is NULL or $L cannot be extracted from it (because it
110910     ** is not a simple variable or literal value), the lower bound of the
110911     ** range is $P. Due to a quirk in the way whereKeyStats() works, even
110912     ** if $L is available, whereKeyStats() is called for both ($P) and
110913     ** ($P:$L) and the larger of the two returned values used.
110914     **
110915     ** Similarly, iUpper is to be set to the estimate of the number of rows
110916     ** less than the upper bound of the range query. Where the upper bound
110917     ** is either ($P) or ($P:$U). Again, even if $U is available, both values
110918     ** of iUpper are requested of whereKeyStats() and the smaller used.
110919     */
110920     tRowcnt iLower;
110921     tRowcnt iUpper;
110922 
110923     if( nEq==p->nKeyCol ){
110924       aff = SQLITE_AFF_INTEGER;
110925     }else{
110926       aff = p->pTable->aCol[p->aiColumn[nEq]].affinity;
110927     }
110928     /* Determine iLower and iUpper using ($P) only. */
110929     if( nEq==0 ){
110930       iLower = 0;
110931       iUpper = p->aiRowEst[0];
110932     }else{
110933       /* Note: this call could be optimized away - since the same values must
110934       ** have been requested when testing key $P in whereEqualScanEst().  */
110935       whereKeyStats(pParse, p, pRec, 0, a);
110936       iLower = a[0];
110937       iUpper = a[0] + a[1];
110938     }
110939 
110940     /* If possible, improve on the iLower estimate using ($P:$L). */
110941     if( pLower ){
110942       int bOk;                    /* True if value is extracted from pExpr */
110943       Expr *pExpr = pLower->pExpr->pRight;
110944       assert( (pLower->eOperator & (WO_GT|WO_GE))!=0 );
110945       rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, aff, nEq, &bOk);
110946       if( rc==SQLITE_OK && bOk ){
110947         tRowcnt iNew;
110948         whereKeyStats(pParse, p, pRec, 0, a);
110949         iNew = a[0] + ((pLower->eOperator & WO_GT) ? a[1] : 0);
110950         if( iNew>iLower ) iLower = iNew;
110951         nOut--;
110952       }
110953     }
110954 
110955     /* If possible, improve on the iUpper estimate using ($P:$U). */
110956     if( pUpper ){
110957       int bOk;                    /* True if value is extracted from pExpr */
110958       Expr *pExpr = pUpper->pExpr->pRight;
110959       assert( (pUpper->eOperator & (WO_LT|WO_LE))!=0 );
110960       rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, aff, nEq, &bOk);
110961       if( rc==SQLITE_OK && bOk ){
110962         tRowcnt iNew;
110963         whereKeyStats(pParse, p, pRec, 1, a);
110964         iNew = a[0] + ((pUpper->eOperator & WO_LE) ? a[1] : 0);
110965         if( iNew<iUpper ) iUpper = iNew;
110966         nOut--;
110967       }
110968     }
110969 
110970     pBuilder->pRec = pRec;
110971     if( rc==SQLITE_OK ){
110972       if( iUpper>iLower ){
110973         nNew = sqlite3LogEst(iUpper - iLower);
110974       }else{
110975         nNew = 10;        assert( 10==sqlite3LogEst(2) );
110976       }
110977       if( nNew<nOut ){
110978         nOut = nNew;
110979       }
110980       pLoop->nOut = (LogEst)nOut;
110981       WHERETRACE(0x10, ("range scan regions: %u..%u  est=%d\n",
110982                          (u32)iLower, (u32)iUpper, nOut));
110983       return SQLITE_OK;
110984     }
110985   }
110986 #else
110987   UNUSED_PARAMETER(pParse);
110988   UNUSED_PARAMETER(pBuilder);
110989 #endif
110990   assert( pLower || pUpper );
110991   /* TUNING:  Each inequality constraint reduces the search space 4-fold.
110992   ** A BETWEEN operator, therefore, reduces the search space 16-fold */
110993   nNew = nOut;
110994   if( pLower && (pLower->wtFlags & TERM_VNULL)==0 ){
110995     nNew -= 20;        assert( 20==sqlite3LogEst(4) );
110996     nOut--;
110997   }
110998   if( pUpper ){
110999     nNew -= 20;        assert( 20==sqlite3LogEst(4) );
111000     nOut--;
111001   }
111002   if( nNew<10 ) nNew = 10;
111003   if( nNew<nOut ) nOut = nNew;
111004   pLoop->nOut = (LogEst)nOut;
111005   return rc;
111006 }
111007 
111008 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
111009 /*
111010 ** Estimate the number of rows that will be returned based on
111011 ** an equality constraint x=VALUE and where that VALUE occurs in
111012 ** the histogram data.  This only works when x is the left-most
111013 ** column of an index and sqlite_stat3 histogram data is available
111014 ** for that index.  When pExpr==NULL that means the constraint is
111015 ** "x IS NULL" instead of "x=VALUE".
111016 **
111017 ** Write the estimated row count into *pnRow and return SQLITE_OK.
111018 ** If unable to make an estimate, leave *pnRow unchanged and return
111019 ** non-zero.
111020 **
111021 ** This routine can fail if it is unable to load a collating sequence
111022 ** required for string comparison, or if unable to allocate memory
111023 ** for a UTF conversion required for comparison.  The error is stored
111024 ** in the pParse structure.
111025 */
111026 static int whereEqualScanEst(
111027   Parse *pParse,       /* Parsing & code generating context */
111028   WhereLoopBuilder *pBuilder,
111029   Expr *pExpr,         /* Expression for VALUE in the x=VALUE constraint */
111030   tRowcnt *pnRow       /* Write the revised row estimate here */
111031 ){
111032   Index *p = pBuilder->pNew->u.btree.pIndex;
111033   int nEq = pBuilder->pNew->u.btree.nEq;
111034   UnpackedRecord *pRec = pBuilder->pRec;
111035   u8 aff;                   /* Column affinity */
111036   int rc;                   /* Subfunction return code */
111037   tRowcnt a[2];             /* Statistics */
111038   int bOk;
111039 
111040   assert( nEq>=1 );
111041   assert( nEq<=(p->nKeyCol+1) );
111042   assert( p->aSample!=0 );
111043   assert( p->nSample>0 );
111044   assert( pBuilder->nRecValid<nEq );
111045 
111046   /* If values are not available for all fields of the index to the left
111047   ** of this one, no estimate can be made. Return SQLITE_NOTFOUND. */
111048   if( pBuilder->nRecValid<(nEq-1) ){
111049     return SQLITE_NOTFOUND;
111050   }
111051 
111052   /* This is an optimization only. The call to sqlite3Stat4ProbeSetValue()
111053   ** below would return the same value.  */
111054   if( nEq>p->nKeyCol ){
111055     *pnRow = 1;
111056     return SQLITE_OK;
111057   }
111058 
111059   aff = p->pTable->aCol[p->aiColumn[nEq-1]].affinity;
111060   rc = sqlite3Stat4ProbeSetValue(pParse, p, &pRec, pExpr, aff, nEq-1, &bOk);
111061   pBuilder->pRec = pRec;
111062   if( rc!=SQLITE_OK ) return rc;
111063   if( bOk==0 ) return SQLITE_NOTFOUND;
111064   pBuilder->nRecValid = nEq;
111065 
111066   whereKeyStats(pParse, p, pRec, 0, a);
111067   WHERETRACE(0x10,("equality scan regions: %d\n", (int)a[1]));
111068   *pnRow = a[1];
111069 
111070   return rc;
111071 }
111072 #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
111073 
111074 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
111075 /*
111076 ** Estimate the number of rows that will be returned based on
111077 ** an IN constraint where the right-hand side of the IN operator
111078 ** is a list of values.  Example:
111079 **
111080 **        WHERE x IN (1,2,3,4)
111081 **
111082 ** Write the estimated row count into *pnRow and return SQLITE_OK.
111083 ** If unable to make an estimate, leave *pnRow unchanged and return
111084 ** non-zero.
111085 **
111086 ** This routine can fail if it is unable to load a collating sequence
111087 ** required for string comparison, or if unable to allocate memory
111088 ** for a UTF conversion required for comparison.  The error is stored
111089 ** in the pParse structure.
111090 */
111091 static int whereInScanEst(
111092   Parse *pParse,       /* Parsing & code generating context */
111093   WhereLoopBuilder *pBuilder,
111094   ExprList *pList,     /* The value list on the RHS of "x IN (v1,v2,v3,...)" */
111095   tRowcnt *pnRow       /* Write the revised row estimate here */
111096 ){
111097   Index *p = pBuilder->pNew->u.btree.pIndex;
111098   int nRecValid = pBuilder->nRecValid;
111099   int rc = SQLITE_OK;     /* Subfunction return code */
111100   tRowcnt nEst;           /* Number of rows for a single term */
111101   tRowcnt nRowEst = 0;    /* New estimate of the number of rows */
111102   int i;                  /* Loop counter */
111103 
111104   assert( p->aSample!=0 );
111105   for(i=0; rc==SQLITE_OK && i<pList->nExpr; i++){
111106     nEst = p->aiRowEst[0];
111107     rc = whereEqualScanEst(pParse, pBuilder, pList->a[i].pExpr, &nEst);
111108     nRowEst += nEst;
111109     pBuilder->nRecValid = nRecValid;
111110   }
111111 
111112   if( rc==SQLITE_OK ){
111113     if( nRowEst > p->aiRowEst[0] ) nRowEst = p->aiRowEst[0];
111114     *pnRow = nRowEst;
111115     WHERETRACE(0x10,("IN row estimate: est=%g\n", nRowEst));
111116   }
111117   assert( pBuilder->nRecValid==nRecValid );
111118   return rc;
111119 }
111120 #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
111121 
111122 /*
111123 ** Disable a term in the WHERE clause.  Except, do not disable the term
111124 ** if it controls a LEFT OUTER JOIN and it did not originate in the ON
111125 ** or USING clause of that join.
111126 **
111127 ** Consider the term t2.z='ok' in the following queries:
111128 **
111129 **   (1)  SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x WHERE t2.z='ok'
111130 **   (2)  SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.x AND t2.z='ok'
111131 **   (3)  SELECT * FROM t1, t2 WHERE t1.a=t2.x AND t2.z='ok'
111132 **
111133 ** The t2.z='ok' is disabled in the in (2) because it originates
111134 ** in the ON clause.  The term is disabled in (3) because it is not part
111135 ** of a LEFT OUTER JOIN.  In (1), the term is not disabled.
111136 **
111137 ** Disabling a term causes that term to not be tested in the inner loop
111138 ** of the join.  Disabling is an optimization.  When terms are satisfied
111139 ** by indices, we disable them to prevent redundant tests in the inner
111140 ** loop.  We would get the correct results if nothing were ever disabled,
111141 ** but joins might run a little slower.  The trick is to disable as much
111142 ** as we can without disabling too much.  If we disabled in (1), we'd get
111143 ** the wrong answer.  See ticket #813.
111144 */
111145 static void disableTerm(WhereLevel *pLevel, WhereTerm *pTerm){
111146   if( pTerm
111147       && (pTerm->wtFlags & TERM_CODED)==0
111148       && (pLevel->iLeftJoin==0 || ExprHasProperty(pTerm->pExpr, EP_FromJoin))
111149       && (pLevel->notReady & pTerm->prereqAll)==0
111150   ){
111151     pTerm->wtFlags |= TERM_CODED;
111152     if( pTerm->iParent>=0 ){
111153       WhereTerm *pOther = &pTerm->pWC->a[pTerm->iParent];
111154       if( (--pOther->nChild)==0 ){
111155         disableTerm(pLevel, pOther);
111156       }
111157     }
111158   }
111159 }
111160 
111161 /*
111162 ** Code an OP_Affinity opcode to apply the column affinity string zAff
111163 ** to the n registers starting at base.
111164 **
111165 ** As an optimization, SQLITE_AFF_NONE entries (which are no-ops) at the
111166 ** beginning and end of zAff are ignored.  If all entries in zAff are
111167 ** SQLITE_AFF_NONE, then no code gets generated.
111168 **
111169 ** This routine makes its own copy of zAff so that the caller is free
111170 ** to modify zAff after this routine returns.
111171 */
111172 static void codeApplyAffinity(Parse *pParse, int base, int n, char *zAff){
111173   Vdbe *v = pParse->pVdbe;
111174   if( zAff==0 ){
111175     assert( pParse->db->mallocFailed );
111176     return;
111177   }
111178   assert( v!=0 );
111179 
111180   /* Adjust base and n to skip over SQLITE_AFF_NONE entries at the beginning
111181   ** and end of the affinity string.
111182   */
111183   while( n>0 && zAff[0]==SQLITE_AFF_NONE ){
111184     n--;
111185     base++;
111186     zAff++;
111187   }
111188   while( n>1 && zAff[n-1]==SQLITE_AFF_NONE ){
111189     n--;
111190   }
111191 
111192   /* Code the OP_Affinity opcode if there is anything left to do. */
111193   if( n>0 ){
111194     sqlite3VdbeAddOp2(v, OP_Affinity, base, n);
111195     sqlite3VdbeChangeP4(v, -1, zAff, n);
111196     sqlite3ExprCacheAffinityChange(pParse, base, n);
111197   }
111198 }
111199 
111200 
111201 /*
111202 ** Generate code for a single equality term of the WHERE clause.  An equality
111203 ** term can be either X=expr or X IN (...).   pTerm is the term to be
111204 ** coded.
111205 **
111206 ** The current value for the constraint is left in register iReg.
111207 **
111208 ** For a constraint of the form X=expr, the expression is evaluated and its
111209 ** result is left on the stack.  For constraints of the form X IN (...)
111210 ** this routine sets up a loop that will iterate over all values of X.
111211 */
111212 static int codeEqualityTerm(
111213   Parse *pParse,      /* The parsing context */
111214   WhereTerm *pTerm,   /* The term of the WHERE clause to be coded */
111215   WhereLevel *pLevel, /* The level of the FROM clause we are working on */
111216   int iEq,            /* Index of the equality term within this level */
111217   int bRev,           /* True for reverse-order IN operations */
111218   int iTarget         /* Attempt to leave results in this register */
111219 ){
111220   Expr *pX = pTerm->pExpr;
111221   Vdbe *v = pParse->pVdbe;
111222   int iReg;                  /* Register holding results */
111223 
111224   assert( iTarget>0 );
111225   if( pX->op==TK_EQ ){
111226     iReg = sqlite3ExprCodeTarget(pParse, pX->pRight, iTarget);
111227   }else if( pX->op==TK_ISNULL ){
111228     iReg = iTarget;
111229     sqlite3VdbeAddOp2(v, OP_Null, 0, iReg);
111230 #ifndef SQLITE_OMIT_SUBQUERY
111231   }else{
111232     int eType;
111233     int iTab;
111234     struct InLoop *pIn;
111235     WhereLoop *pLoop = pLevel->pWLoop;
111236 
111237     if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0
111238       && pLoop->u.btree.pIndex!=0
111239       && pLoop->u.btree.pIndex->aSortOrder[iEq]
111240     ){
111241       testcase( iEq==0 );
111242       testcase( bRev );
111243       bRev = !bRev;
111244     }
111245     assert( pX->op==TK_IN );
111246     iReg = iTarget;
111247     eType = sqlite3FindInIndex(pParse, pX, 0);
111248     if( eType==IN_INDEX_INDEX_DESC ){
111249       testcase( bRev );
111250       bRev = !bRev;
111251     }
111252     iTab = pX->iTable;
111253     sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iTab, 0);
111254     assert( (pLoop->wsFlags & WHERE_MULTI_OR)==0 );
111255     pLoop->wsFlags |= WHERE_IN_ABLE;
111256     if( pLevel->u.in.nIn==0 ){
111257       pLevel->addrNxt = sqlite3VdbeMakeLabel(v);
111258     }
111259     pLevel->u.in.nIn++;
111260     pLevel->u.in.aInLoop =
111261        sqlite3DbReallocOrFree(pParse->db, pLevel->u.in.aInLoop,
111262                               sizeof(pLevel->u.in.aInLoop[0])*pLevel->u.in.nIn);
111263     pIn = pLevel->u.in.aInLoop;
111264     if( pIn ){
111265       pIn += pLevel->u.in.nIn - 1;
111266       pIn->iCur = iTab;
111267       if( eType==IN_INDEX_ROWID ){
111268         pIn->addrInTop = sqlite3VdbeAddOp2(v, OP_Rowid, iTab, iReg);
111269       }else{
111270         pIn->addrInTop = sqlite3VdbeAddOp3(v, OP_Column, iTab, 0, iReg);
111271       }
111272       pIn->eEndLoopOp = bRev ? OP_PrevIfOpen : OP_NextIfOpen;
111273       sqlite3VdbeAddOp1(v, OP_IsNull, iReg);
111274     }else{
111275       pLevel->u.in.nIn = 0;
111276     }
111277 #endif
111278   }
111279   disableTerm(pLevel, pTerm);
111280   return iReg;
111281 }
111282 
111283 /*
111284 ** Generate code that will evaluate all == and IN constraints for an
111285 ** index scan.
111286 **
111287 ** For example, consider table t1(a,b,c,d,e,f) with index i1(a,b,c).
111288 ** Suppose the WHERE clause is this:  a==5 AND b IN (1,2,3) AND c>5 AND c<10
111289 ** The index has as many as three equality constraints, but in this
111290 ** example, the third "c" value is an inequality.  So only two
111291 ** constraints are coded.  This routine will generate code to evaluate
111292 ** a==5 and b IN (1,2,3).  The current values for a and b will be stored
111293 ** in consecutive registers and the index of the first register is returned.
111294 **
111295 ** In the example above nEq==2.  But this subroutine works for any value
111296 ** of nEq including 0.  If nEq==0, this routine is nearly a no-op.
111297 ** The only thing it does is allocate the pLevel->iMem memory cell and
111298 ** compute the affinity string.
111299 **
111300 ** The nExtraReg parameter is 0 or 1.  It is 0 if all WHERE clause constraints
111301 ** are == or IN and are covered by the nEq.  nExtraReg is 1 if there is
111302 ** an inequality constraint (such as the "c>=5 AND c<10" in the example) that
111303 ** occurs after the nEq quality constraints.
111304 **
111305 ** This routine allocates a range of nEq+nExtraReg memory cells and returns
111306 ** the index of the first memory cell in that range. The code that
111307 ** calls this routine will use that memory range to store keys for
111308 ** start and termination conditions of the loop.
111309 ** key value of the loop.  If one or more IN operators appear, then
111310 ** this routine allocates an additional nEq memory cells for internal
111311 ** use.
111312 **
111313 ** Before returning, *pzAff is set to point to a buffer containing a
111314 ** copy of the column affinity string of the index allocated using
111315 ** sqlite3DbMalloc(). Except, entries in the copy of the string associated
111316 ** with equality constraints that use NONE affinity are set to
111317 ** SQLITE_AFF_NONE. This is to deal with SQL such as the following:
111318 **
111319 **   CREATE TABLE t1(a TEXT PRIMARY KEY, b);
111320 **   SELECT ... FROM t1 AS t2, t1 WHERE t1.a = t2.b;
111321 **
111322 ** In the example above, the index on t1(a) has TEXT affinity. But since
111323 ** the right hand side of the equality constraint (t2.b) has NONE affinity,
111324 ** no conversion should be attempted before using a t2.b value as part of
111325 ** a key to search the index. Hence the first byte in the returned affinity
111326 ** string in this example would be set to SQLITE_AFF_NONE.
111327 */
111328 static int codeAllEqualityTerms(
111329   Parse *pParse,        /* Parsing context */
111330   WhereLevel *pLevel,   /* Which nested loop of the FROM we are coding */
111331   int bRev,             /* Reverse the order of IN operators */
111332   int nExtraReg,        /* Number of extra registers to allocate */
111333   char **pzAff          /* OUT: Set to point to affinity string */
111334 ){
111335   u16 nEq;                      /* The number of == or IN constraints to code */
111336   u16 nSkip;                    /* Number of left-most columns to skip */
111337   Vdbe *v = pParse->pVdbe;      /* The vm under construction */
111338   Index *pIdx;                  /* The index being used for this loop */
111339   WhereTerm *pTerm;             /* A single constraint term */
111340   WhereLoop *pLoop;             /* The WhereLoop object */
111341   int j;                        /* Loop counter */
111342   int regBase;                  /* Base register */
111343   int nReg;                     /* Number of registers to allocate */
111344   char *zAff;                   /* Affinity string to return */
111345 
111346   /* This module is only called on query plans that use an index. */
111347   pLoop = pLevel->pWLoop;
111348   assert( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 );
111349   nEq = pLoop->u.btree.nEq;
111350   nSkip = pLoop->u.btree.nSkip;
111351   pIdx = pLoop->u.btree.pIndex;
111352   assert( pIdx!=0 );
111353 
111354   /* Figure out how many memory cells we will need then allocate them.
111355   */
111356   regBase = pParse->nMem + 1;
111357   nReg = pLoop->u.btree.nEq + nExtraReg;
111358   pParse->nMem += nReg;
111359 
111360   zAff = sqlite3DbStrDup(pParse->db, sqlite3IndexAffinityStr(v, pIdx));
111361   if( !zAff ){
111362     pParse->db->mallocFailed = 1;
111363   }
111364 
111365   if( nSkip ){
111366     int iIdxCur = pLevel->iIdxCur;
111367     sqlite3VdbeAddOp1(v, (bRev?OP_Last:OP_Rewind), iIdxCur);
111368     VdbeComment((v, "begin skip-scan on %s", pIdx->zName));
111369     j = sqlite3VdbeAddOp0(v, OP_Goto);
111370     pLevel->addrSkip = sqlite3VdbeAddOp4Int(v, (bRev?OP_SeekLt:OP_SeekGt),
111371                             iIdxCur, 0, regBase, nSkip);
111372     sqlite3VdbeJumpHere(v, j);
111373     for(j=0; j<nSkip; j++){
111374       sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, j, regBase+j);
111375       assert( pIdx->aiColumn[j]>=0 );
111376       VdbeComment((v, "%s", pIdx->pTable->aCol[pIdx->aiColumn[j]].zName));
111377     }
111378   }
111379 
111380   /* Evaluate the equality constraints
111381   */
111382   assert( zAff==0 || (int)strlen(zAff)>=nEq );
111383   for(j=nSkip; j<nEq; j++){
111384     int r1;
111385     pTerm = pLoop->aLTerm[j];
111386     assert( pTerm!=0 );
111387     /* The following testcase is true for indices with redundant columns.
111388     ** Ex: CREATE INDEX i1 ON t1(a,b,a); SELECT * FROM t1 WHERE a=0 AND b=0; */
111389     testcase( (pTerm->wtFlags & TERM_CODED)!=0 );
111390     testcase( pTerm->wtFlags & TERM_VIRTUAL );
111391     r1 = codeEqualityTerm(pParse, pTerm, pLevel, j, bRev, regBase+j);
111392     if( r1!=regBase+j ){
111393       if( nReg==1 ){
111394         sqlite3ReleaseTempReg(pParse, regBase);
111395         regBase = r1;
111396       }else{
111397         sqlite3VdbeAddOp2(v, OP_SCopy, r1, regBase+j);
111398       }
111399     }
111400     testcase( pTerm->eOperator & WO_ISNULL );
111401     testcase( pTerm->eOperator & WO_IN );
111402     if( (pTerm->eOperator & (WO_ISNULL|WO_IN))==0 ){
111403       Expr *pRight = pTerm->pExpr->pRight;
111404       sqlite3ExprCodeIsNullJump(v, pRight, regBase+j, pLevel->addrBrk);
111405       if( zAff ){
111406         if( sqlite3CompareAffinity(pRight, zAff[j])==SQLITE_AFF_NONE ){
111407           zAff[j] = SQLITE_AFF_NONE;
111408         }
111409         if( sqlite3ExprNeedsNoAffinityChange(pRight, zAff[j]) ){
111410           zAff[j] = SQLITE_AFF_NONE;
111411         }
111412       }
111413     }
111414   }
111415   *pzAff = zAff;
111416   return regBase;
111417 }
111418 
111419 #ifndef SQLITE_OMIT_EXPLAIN
111420 /*
111421 ** This routine is a helper for explainIndexRange() below
111422 **
111423 ** pStr holds the text of an expression that we are building up one term
111424 ** at a time.  This routine adds a new term to the end of the expression.
111425 ** Terms are separated by AND so add the "AND" text for second and subsequent
111426 ** terms only.
111427 */
111428 static void explainAppendTerm(
111429   StrAccum *pStr,             /* The text expression being built */
111430   int iTerm,                  /* Index of this term.  First is zero */
111431   const char *zColumn,        /* Name of the column */
111432   const char *zOp             /* Name of the operator */
111433 ){
111434   if( iTerm ) sqlite3StrAccumAppend(pStr, " AND ", 5);
111435   sqlite3StrAccumAppendAll(pStr, zColumn);
111436   sqlite3StrAccumAppend(pStr, zOp, 1);
111437   sqlite3StrAccumAppend(pStr, "?", 1);
111438 }
111439 
111440 /*
111441 ** Argument pLevel describes a strategy for scanning table pTab. This
111442 ** function returns a pointer to a string buffer containing a description
111443 ** of the subset of table rows scanned by the strategy in the form of an
111444 ** SQL expression. Or, if all rows are scanned, NULL is returned.
111445 **
111446 ** For example, if the query:
111447 **
111448 **   SELECT * FROM t1 WHERE a=1 AND b>2;
111449 **
111450 ** is run and there is an index on (a, b), then this function returns a
111451 ** string similar to:
111452 **
111453 **   "a=? AND b>?"
111454 **
111455 ** The returned pointer points to memory obtained from sqlite3DbMalloc().
111456 ** It is the responsibility of the caller to free the buffer when it is
111457 ** no longer required.
111458 */
111459 static char *explainIndexRange(sqlite3 *db, WhereLoop *pLoop, Table *pTab){
111460   Index *pIndex = pLoop->u.btree.pIndex;
111461   u16 nEq = pLoop->u.btree.nEq;
111462   u16 nSkip = pLoop->u.btree.nSkip;
111463   int i, j;
111464   Column *aCol = pTab->aCol;
111465   i16 *aiColumn = pIndex->aiColumn;
111466   StrAccum txt;
111467 
111468   if( nEq==0 && (pLoop->wsFlags & (WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))==0 ){
111469     return 0;
111470   }
111471   sqlite3StrAccumInit(&txt, 0, 0, SQLITE_MAX_LENGTH);
111472   txt.db = db;
111473   sqlite3StrAccumAppend(&txt, " (", 2);
111474   for(i=0; i<nEq; i++){
111475     char *z = (i==pIndex->nKeyCol ) ? "rowid" : aCol[aiColumn[i]].zName;
111476     if( i>=nSkip ){
111477       explainAppendTerm(&txt, i, z, "=");
111478     }else{
111479       if( i ) sqlite3StrAccumAppend(&txt, " AND ", 5);
111480       sqlite3StrAccumAppend(&txt, "ANY(", 4);
111481       sqlite3StrAccumAppendAll(&txt, z);
111482       sqlite3StrAccumAppend(&txt, ")", 1);
111483     }
111484   }
111485 
111486   j = i;
111487   if( pLoop->wsFlags&WHERE_BTM_LIMIT ){
111488     char *z = (j==pIndex->nKeyCol ) ? "rowid" : aCol[aiColumn[j]].zName;
111489     explainAppendTerm(&txt, i++, z, ">");
111490   }
111491   if( pLoop->wsFlags&WHERE_TOP_LIMIT ){
111492     char *z = (j==pIndex->nKeyCol ) ? "rowid" : aCol[aiColumn[j]].zName;
111493     explainAppendTerm(&txt, i, z, "<");
111494   }
111495   sqlite3StrAccumAppend(&txt, ")", 1);
111496   return sqlite3StrAccumFinish(&txt);
111497 }
111498 
111499 /*
111500 ** This function is a no-op unless currently processing an EXPLAIN QUERY PLAN
111501 ** command. If the query being compiled is an EXPLAIN QUERY PLAN, a single
111502 ** record is added to the output to describe the table scan strategy in
111503 ** pLevel.
111504 */
111505 static void explainOneScan(
111506   Parse *pParse,                  /* Parse context */
111507   SrcList *pTabList,              /* Table list this loop refers to */
111508   WhereLevel *pLevel,             /* Scan to write OP_Explain opcode for */
111509   int iLevel,                     /* Value for "level" column of output */
111510   int iFrom,                      /* Value for "from" column of output */
111511   u16 wctrlFlags                  /* Flags passed to sqlite3WhereBegin() */
111512 ){
111513 #ifndef SQLITE_DEBUG
111514   if( pParse->explain==2 )
111515 #endif
111516   {
111517     struct SrcList_item *pItem = &pTabList->a[pLevel->iFrom];
111518     Vdbe *v = pParse->pVdbe;      /* VM being constructed */
111519     sqlite3 *db = pParse->db;     /* Database handle */
111520     char *zMsg;                   /* Text to add to EQP output */
111521     int iId = pParse->iSelectId;  /* Select id (left-most output column) */
111522     int isSearch;                 /* True for a SEARCH. False for SCAN. */
111523     WhereLoop *pLoop;             /* The controlling WhereLoop object */
111524     u32 flags;                    /* Flags that describe this loop */
111525 
111526     pLoop = pLevel->pWLoop;
111527     flags = pLoop->wsFlags;
111528     if( (flags&WHERE_MULTI_OR) || (wctrlFlags&WHERE_ONETABLE_ONLY) ) return;
111529 
111530     isSearch = (flags&(WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))!=0
111531             || ((flags&WHERE_VIRTUALTABLE)==0 && (pLoop->u.btree.nEq>0))
111532             || (wctrlFlags&(WHERE_ORDERBY_MIN|WHERE_ORDERBY_MAX));
111533 
111534     zMsg = sqlite3MPrintf(db, "%s", isSearch?"SEARCH":"SCAN");
111535     if( pItem->pSelect ){
111536       zMsg = sqlite3MAppendf(db, zMsg, "%s SUBQUERY %d", zMsg,pItem->iSelectId);
111537     }else{
111538       zMsg = sqlite3MAppendf(db, zMsg, "%s TABLE %s", zMsg, pItem->zName);
111539     }
111540 
111541     if( pItem->zAlias ){
111542       zMsg = sqlite3MAppendf(db, zMsg, "%s AS %s", zMsg, pItem->zAlias);
111543     }
111544     if( (flags & (WHERE_IPK|WHERE_VIRTUALTABLE))==0
111545      && ALWAYS(pLoop->u.btree.pIndex!=0)
111546     ){
111547       char *zWhere = explainIndexRange(db, pLoop, pItem->pTab);
111548       zMsg = sqlite3MAppendf(db, zMsg,
111549                ((flags & WHERE_AUTO_INDEX) ?
111550                    "%s USING AUTOMATIC %sINDEX%.0s%s" :
111551                    "%s USING %sINDEX %s%s"),
111552                zMsg, ((flags & WHERE_IDX_ONLY) ? "COVERING " : ""),
111553                pLoop->u.btree.pIndex->zName, zWhere);
111554       sqlite3DbFree(db, zWhere);
111555     }else if( (flags & WHERE_IPK)!=0 && (flags & WHERE_CONSTRAINT)!=0 ){
111556       zMsg = sqlite3MAppendf(db, zMsg, "%s USING INTEGER PRIMARY KEY", zMsg);
111557 
111558       if( flags&(WHERE_COLUMN_EQ|WHERE_COLUMN_IN) ){
111559         zMsg = sqlite3MAppendf(db, zMsg, "%s (rowid=?)", zMsg);
111560       }else if( (flags&WHERE_BOTH_LIMIT)==WHERE_BOTH_LIMIT ){
111561         zMsg = sqlite3MAppendf(db, zMsg, "%s (rowid>? AND rowid<?)", zMsg);
111562       }else if( flags&WHERE_BTM_LIMIT ){
111563         zMsg = sqlite3MAppendf(db, zMsg, "%s (rowid>?)", zMsg);
111564       }else if( ALWAYS(flags&WHERE_TOP_LIMIT) ){
111565         zMsg = sqlite3MAppendf(db, zMsg, "%s (rowid<?)", zMsg);
111566       }
111567     }
111568 #ifndef SQLITE_OMIT_VIRTUALTABLE
111569     else if( (flags & WHERE_VIRTUALTABLE)!=0 ){
111570       zMsg = sqlite3MAppendf(db, zMsg, "%s VIRTUAL TABLE INDEX %d:%s", zMsg,
111571                   pLoop->u.vtab.idxNum, pLoop->u.vtab.idxStr);
111572     }
111573 #endif
111574     zMsg = sqlite3MAppendf(db, zMsg, "%s", zMsg);
111575     sqlite3VdbeAddOp4(v, OP_Explain, iId, iLevel, iFrom, zMsg, P4_DYNAMIC);
111576   }
111577 }
111578 #else
111579 # define explainOneScan(u,v,w,x,y,z)
111580 #endif /* SQLITE_OMIT_EXPLAIN */
111581 
111582 
111583 /*
111584 ** Generate code for the start of the iLevel-th loop in the WHERE clause
111585 ** implementation described by pWInfo.
111586 */
111587 static Bitmask codeOneLoopStart(
111588   WhereInfo *pWInfo,   /* Complete information about the WHERE clause */
111589   int iLevel,          /* Which level of pWInfo->a[] should be coded */
111590   Bitmask notReady     /* Which tables are currently available */
111591 ){
111592   int j, k;            /* Loop counters */
111593   int iCur;            /* The VDBE cursor for the table */
111594   int addrNxt;         /* Where to jump to continue with the next IN case */
111595   int omitTable;       /* True if we use the index only */
111596   int bRev;            /* True if we need to scan in reverse order */
111597   WhereLevel *pLevel;  /* The where level to be coded */
111598   WhereLoop *pLoop;    /* The WhereLoop object being coded */
111599   WhereClause *pWC;    /* Decomposition of the entire WHERE clause */
111600   WhereTerm *pTerm;               /* A WHERE clause term */
111601   Parse *pParse;                  /* Parsing context */
111602   sqlite3 *db;                    /* Database connection */
111603   Vdbe *v;                        /* The prepared stmt under constructions */
111604   struct SrcList_item *pTabItem;  /* FROM clause term being coded */
111605   int addrBrk;                    /* Jump here to break out of the loop */
111606   int addrCont;                   /* Jump here to continue with next cycle */
111607   int iRowidReg = 0;        /* Rowid is stored in this register, if not zero */
111608   int iReleaseReg = 0;      /* Temp register to free before returning */
111609 
111610   pParse = pWInfo->pParse;
111611   v = pParse->pVdbe;
111612   pWC = &pWInfo->sWC;
111613   db = pParse->db;
111614   pLevel = &pWInfo->a[iLevel];
111615   pLoop = pLevel->pWLoop;
111616   pTabItem = &pWInfo->pTabList->a[pLevel->iFrom];
111617   iCur = pTabItem->iCursor;
111618   pLevel->notReady = notReady & ~getMask(&pWInfo->sMaskSet, iCur);
111619   bRev = (pWInfo->revMask>>iLevel)&1;
111620   omitTable = (pLoop->wsFlags & WHERE_IDX_ONLY)!=0
111621            && (pWInfo->wctrlFlags & WHERE_FORCE_TABLE)==0;
111622   VdbeModuleComment((v, "Begin WHERE-loop%d: %s",iLevel,pTabItem->pTab->zName));
111623 
111624   /* Create labels for the "break" and "continue" instructions
111625   ** for the current loop.  Jump to addrBrk to break out of a loop.
111626   ** Jump to cont to go immediately to the next iteration of the
111627   ** loop.
111628   **
111629   ** When there is an IN operator, we also have a "addrNxt" label that
111630   ** means to continue with the next IN value combination.  When
111631   ** there are no IN operators in the constraints, the "addrNxt" label
111632   ** is the same as "addrBrk".
111633   */
111634   addrBrk = pLevel->addrBrk = pLevel->addrNxt = sqlite3VdbeMakeLabel(v);
111635   addrCont = pLevel->addrCont = sqlite3VdbeMakeLabel(v);
111636 
111637   /* If this is the right table of a LEFT OUTER JOIN, allocate and
111638   ** initialize a memory cell that records if this table matches any
111639   ** row of the left table of the join.
111640   */
111641   if( pLevel->iFrom>0 && (pTabItem[0].jointype & JT_LEFT)!=0 ){
111642     pLevel->iLeftJoin = ++pParse->nMem;
111643     sqlite3VdbeAddOp2(v, OP_Integer, 0, pLevel->iLeftJoin);
111644     VdbeComment((v, "init LEFT JOIN no-match flag"));
111645   }
111646 
111647   /* Special case of a FROM clause subquery implemented as a co-routine */
111648   if( pTabItem->viaCoroutine ){
111649     int regYield = pTabItem->regReturn;
111650     sqlite3VdbeAddOp2(v, OP_Integer, pTabItem->addrFillSub-1, regYield);
111651     pLevel->p2 =  sqlite3VdbeAddOp1(v, OP_Yield, regYield);
111652     VdbeComment((v, "next row of co-routine %s", pTabItem->pTab->zName));
111653     sqlite3VdbeAddOp2(v, OP_If, regYield+1, addrBrk);
111654     pLevel->op = OP_Goto;
111655   }else
111656 
111657 #ifndef SQLITE_OMIT_VIRTUALTABLE
111658   if(  (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 ){
111659     /* Case 1:  The table is a virtual-table.  Use the VFilter and VNext
111660     **          to access the data.
111661     */
111662     int iReg;   /* P3 Value for OP_VFilter */
111663     int addrNotFound;
111664     int nConstraint = pLoop->nLTerm;
111665 
111666     sqlite3ExprCachePush(pParse);
111667     iReg = sqlite3GetTempRange(pParse, nConstraint+2);
111668     addrNotFound = pLevel->addrBrk;
111669     for(j=0; j<nConstraint; j++){
111670       int iTarget = iReg+j+2;
111671       pTerm = pLoop->aLTerm[j];
111672       if( pTerm==0 ) continue;
111673       if( pTerm->eOperator & WO_IN ){
111674         codeEqualityTerm(pParse, pTerm, pLevel, j, bRev, iTarget);
111675         addrNotFound = pLevel->addrNxt;
111676       }else{
111677         sqlite3ExprCode(pParse, pTerm->pExpr->pRight, iTarget);
111678       }
111679     }
111680     sqlite3VdbeAddOp2(v, OP_Integer, pLoop->u.vtab.idxNum, iReg);
111681     sqlite3VdbeAddOp2(v, OP_Integer, nConstraint, iReg+1);
111682     sqlite3VdbeAddOp4(v, OP_VFilter, iCur, addrNotFound, iReg,
111683                       pLoop->u.vtab.idxStr,
111684                       pLoop->u.vtab.needFree ? P4_MPRINTF : P4_STATIC);
111685     pLoop->u.vtab.needFree = 0;
111686     for(j=0; j<nConstraint && j<16; j++){
111687       if( (pLoop->u.vtab.omitMask>>j)&1 ){
111688         disableTerm(pLevel, pLoop->aLTerm[j]);
111689       }
111690     }
111691     pLevel->op = OP_VNext;
111692     pLevel->p1 = iCur;
111693     pLevel->p2 = sqlite3VdbeCurrentAddr(v);
111694     sqlite3ReleaseTempRange(pParse, iReg, nConstraint+2);
111695     sqlite3ExprCachePop(pParse, 1);
111696   }else
111697 #endif /* SQLITE_OMIT_VIRTUALTABLE */
111698 
111699   if( (pLoop->wsFlags & WHERE_IPK)!=0
111700    && (pLoop->wsFlags & (WHERE_COLUMN_IN|WHERE_COLUMN_EQ))!=0
111701   ){
111702     /* Case 2:  We can directly reference a single row using an
111703     **          equality comparison against the ROWID field.  Or
111704     **          we reference multiple rows using a "rowid IN (...)"
111705     **          construct.
111706     */
111707     assert( pLoop->u.btree.nEq==1 );
111708     iReleaseReg = sqlite3GetTempReg(pParse);
111709     pTerm = pLoop->aLTerm[0];
111710     assert( pTerm!=0 );
111711     assert( pTerm->pExpr!=0 );
111712     assert( omitTable==0 );
111713     testcase( pTerm->wtFlags & TERM_VIRTUAL );
111714     iRowidReg = codeEqualityTerm(pParse, pTerm, pLevel, 0, bRev, iReleaseReg);
111715     addrNxt = pLevel->addrNxt;
111716     sqlite3VdbeAddOp2(v, OP_MustBeInt, iRowidReg, addrNxt);
111717     sqlite3VdbeAddOp3(v, OP_NotExists, iCur, addrNxt, iRowidReg);
111718     sqlite3ExprCacheAffinityChange(pParse, iRowidReg, 1);
111719     sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg);
111720     VdbeComment((v, "pk"));
111721     pLevel->op = OP_Noop;
111722   }else if( (pLoop->wsFlags & WHERE_IPK)!=0
111723          && (pLoop->wsFlags & WHERE_COLUMN_RANGE)!=0
111724   ){
111725     /* Case 3:  We have an inequality comparison against the ROWID field.
111726     */
111727     int testOp = OP_Noop;
111728     int start;
111729     int memEndValue = 0;
111730     WhereTerm *pStart, *pEnd;
111731 
111732     assert( omitTable==0 );
111733     j = 0;
111734     pStart = pEnd = 0;
111735     if( pLoop->wsFlags & WHERE_BTM_LIMIT ) pStart = pLoop->aLTerm[j++];
111736     if( pLoop->wsFlags & WHERE_TOP_LIMIT ) pEnd = pLoop->aLTerm[j++];
111737     assert( pStart!=0 || pEnd!=0 );
111738     if( bRev ){
111739       pTerm = pStart;
111740       pStart = pEnd;
111741       pEnd = pTerm;
111742     }
111743     if( pStart ){
111744       Expr *pX;             /* The expression that defines the start bound */
111745       int r1, rTemp;        /* Registers for holding the start boundary */
111746 
111747       /* The following constant maps TK_xx codes into corresponding
111748       ** seek opcodes.  It depends on a particular ordering of TK_xx
111749       */
111750       const u8 aMoveOp[] = {
111751            /* TK_GT */  OP_SeekGt,
111752            /* TK_LE */  OP_SeekLe,
111753            /* TK_LT */  OP_SeekLt,
111754            /* TK_GE */  OP_SeekGe
111755       };
111756       assert( TK_LE==TK_GT+1 );      /* Make sure the ordering.. */
111757       assert( TK_LT==TK_GT+2 );      /*  ... of the TK_xx values... */
111758       assert( TK_GE==TK_GT+3 );      /*  ... is correcct. */
111759 
111760       assert( (pStart->wtFlags & TERM_VNULL)==0 );
111761       testcase( pStart->wtFlags & TERM_VIRTUAL );
111762       pX = pStart->pExpr;
111763       assert( pX!=0 );
111764       testcase( pStart->leftCursor!=iCur ); /* transitive constraints */
111765       r1 = sqlite3ExprCodeTemp(pParse, pX->pRight, &rTemp);
111766       sqlite3VdbeAddOp3(v, aMoveOp[pX->op-TK_GT], iCur, addrBrk, r1);
111767       VdbeComment((v, "pk"));
111768       sqlite3ExprCacheAffinityChange(pParse, r1, 1);
111769       sqlite3ReleaseTempReg(pParse, rTemp);
111770       disableTerm(pLevel, pStart);
111771     }else{
111772       sqlite3VdbeAddOp2(v, bRev ? OP_Last : OP_Rewind, iCur, addrBrk);
111773     }
111774     if( pEnd ){
111775       Expr *pX;
111776       pX = pEnd->pExpr;
111777       assert( pX!=0 );
111778       assert( (pEnd->wtFlags & TERM_VNULL)==0 );
111779       testcase( pEnd->leftCursor!=iCur ); /* Transitive constraints */
111780       testcase( pEnd->wtFlags & TERM_VIRTUAL );
111781       memEndValue = ++pParse->nMem;
111782       sqlite3ExprCode(pParse, pX->pRight, memEndValue);
111783       if( pX->op==TK_LT || pX->op==TK_GT ){
111784         testOp = bRev ? OP_Le : OP_Ge;
111785       }else{
111786         testOp = bRev ? OP_Lt : OP_Gt;
111787       }
111788       disableTerm(pLevel, pEnd);
111789     }
111790     start = sqlite3VdbeCurrentAddr(v);
111791     pLevel->op = bRev ? OP_Prev : OP_Next;
111792     pLevel->p1 = iCur;
111793     pLevel->p2 = start;
111794     assert( pLevel->p5==0 );
111795     if( testOp!=OP_Noop ){
111796       iRowidReg = iReleaseReg = sqlite3GetTempReg(pParse);
111797       sqlite3VdbeAddOp2(v, OP_Rowid, iCur, iRowidReg);
111798       sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg);
111799       sqlite3VdbeAddOp3(v, testOp, memEndValue, addrBrk, iRowidReg);
111800       sqlite3VdbeChangeP5(v, SQLITE_AFF_NUMERIC | SQLITE_JUMPIFNULL);
111801     }
111802   }else if( pLoop->wsFlags & WHERE_INDEXED ){
111803     /* Case 4: A scan using an index.
111804     **
111805     **         The WHERE clause may contain zero or more equality
111806     **         terms ("==" or "IN" operators) that refer to the N
111807     **         left-most columns of the index. It may also contain
111808     **         inequality constraints (>, <, >= or <=) on the indexed
111809     **         column that immediately follows the N equalities. Only
111810     **         the right-most column can be an inequality - the rest must
111811     **         use the "==" and "IN" operators. For example, if the
111812     **         index is on (x,y,z), then the following clauses are all
111813     **         optimized:
111814     **
111815     **            x=5
111816     **            x=5 AND y=10
111817     **            x=5 AND y<10
111818     **            x=5 AND y>5 AND y<10
111819     **            x=5 AND y=5 AND z<=10
111820     **
111821     **         The z<10 term of the following cannot be used, only
111822     **         the x=5 term:
111823     **
111824     **            x=5 AND z<10
111825     **
111826     **         N may be zero if there are inequality constraints.
111827     **         If there are no inequality constraints, then N is at
111828     **         least one.
111829     **
111830     **         This case is also used when there are no WHERE clause
111831     **         constraints but an index is selected anyway, in order
111832     **         to force the output order to conform to an ORDER BY.
111833     */
111834     static const u8 aStartOp[] = {
111835       0,
111836       0,
111837       OP_Rewind,           /* 2: (!start_constraints && startEq &&  !bRev) */
111838       OP_Last,             /* 3: (!start_constraints && startEq &&   bRev) */
111839       OP_SeekGt,           /* 4: (start_constraints  && !startEq && !bRev) */
111840       OP_SeekLt,           /* 5: (start_constraints  && !startEq &&  bRev) */
111841       OP_SeekGe,           /* 6: (start_constraints  &&  startEq && !bRev) */
111842       OP_SeekLe            /* 7: (start_constraints  &&  startEq &&  bRev) */
111843     };
111844     static const u8 aEndOp[] = {
111845       OP_Noop,             /* 0: (!end_constraints) */
111846       OP_IdxGE,            /* 1: (end_constraints && !bRev) */
111847       OP_IdxLT             /* 2: (end_constraints && bRev) */
111848     };
111849     u16 nEq = pLoop->u.btree.nEq;     /* Number of == or IN terms */
111850     int isMinQuery = 0;          /* If this is an optimized SELECT min(x).. */
111851     int regBase;                 /* Base register holding constraint values */
111852     int r1;                      /* Temp register */
111853     WhereTerm *pRangeStart = 0;  /* Inequality constraint at range start */
111854     WhereTerm *pRangeEnd = 0;    /* Inequality constraint at range end */
111855     int startEq;                 /* True if range start uses ==, >= or <= */
111856     int endEq;                   /* True if range end uses ==, >= or <= */
111857     int start_constraints;       /* Start of range is constrained */
111858     int nConstraint;             /* Number of constraint terms */
111859     Index *pIdx;                 /* The index we will be using */
111860     int iIdxCur;                 /* The VDBE cursor for the index */
111861     int nExtraReg = 0;           /* Number of extra registers needed */
111862     int op;                      /* Instruction opcode */
111863     char *zStartAff;             /* Affinity for start of range constraint */
111864     char cEndAff = 0;            /* Affinity for end of range constraint */
111865 
111866     pIdx = pLoop->u.btree.pIndex;
111867     iIdxCur = pLevel->iIdxCur;
111868     assert( nEq>=pLoop->u.btree.nSkip );
111869 
111870     /* If this loop satisfies a sort order (pOrderBy) request that
111871     ** was passed to this function to implement a "SELECT min(x) ..."
111872     ** query, then the caller will only allow the loop to run for
111873     ** a single iteration. This means that the first row returned
111874     ** should not have a NULL value stored in 'x'. If column 'x' is
111875     ** the first one after the nEq equality constraints in the index,
111876     ** this requires some special handling.
111877     */
111878     if( (pWInfo->wctrlFlags&WHERE_ORDERBY_MIN)!=0
111879      && (pWInfo->bOBSat!=0)
111880      && (pIdx->nKeyCol>nEq)
111881     ){
111882       assert( pLoop->u.btree.nSkip==0 );
111883       isMinQuery = 1;
111884       nExtraReg = 1;
111885     }
111886 
111887     /* Find any inequality constraint terms for the start and end
111888     ** of the range.
111889     */
111890     j = nEq;
111891     if( pLoop->wsFlags & WHERE_BTM_LIMIT ){
111892       pRangeStart = pLoop->aLTerm[j++];
111893       nExtraReg = 1;
111894     }
111895     if( pLoop->wsFlags & WHERE_TOP_LIMIT ){
111896       pRangeEnd = pLoop->aLTerm[j++];
111897       nExtraReg = 1;
111898     }
111899 
111900     /* Generate code to evaluate all constraint terms using == or IN
111901     ** and store the values of those terms in an array of registers
111902     ** starting at regBase.
111903     */
111904     regBase = codeAllEqualityTerms(pParse,pLevel,bRev,nExtraReg,&zStartAff);
111905     assert( zStartAff==0 || sqlite3Strlen30(zStartAff)>=nEq );
111906     if( zStartAff ) cEndAff = zStartAff[nEq];
111907     addrNxt = pLevel->addrNxt;
111908 
111909     /* If we are doing a reverse order scan on an ascending index, or
111910     ** a forward order scan on a descending index, interchange the
111911     ** start and end terms (pRangeStart and pRangeEnd).
111912     */
111913     if( (nEq<pIdx->nKeyCol && bRev==(pIdx->aSortOrder[nEq]==SQLITE_SO_ASC))
111914      || (bRev && pIdx->nKeyCol==nEq)
111915     ){
111916       SWAP(WhereTerm *, pRangeEnd, pRangeStart);
111917     }
111918 
111919     testcase( pRangeStart && (pRangeStart->eOperator & WO_LE)!=0 );
111920     testcase( pRangeStart && (pRangeStart->eOperator & WO_GE)!=0 );
111921     testcase( pRangeEnd && (pRangeEnd->eOperator & WO_LE)!=0 );
111922     testcase( pRangeEnd && (pRangeEnd->eOperator & WO_GE)!=0 );
111923     startEq = !pRangeStart || pRangeStart->eOperator & (WO_LE|WO_GE);
111924     endEq =   !pRangeEnd || pRangeEnd->eOperator & (WO_LE|WO_GE);
111925     start_constraints = pRangeStart || nEq>0;
111926 
111927     /* Seek the index cursor to the start of the range. */
111928     nConstraint = nEq;
111929     if( pRangeStart ){
111930       Expr *pRight = pRangeStart->pExpr->pRight;
111931       sqlite3ExprCode(pParse, pRight, regBase+nEq);
111932       if( (pRangeStart->wtFlags & TERM_VNULL)==0 ){
111933         sqlite3ExprCodeIsNullJump(v, pRight, regBase+nEq, addrNxt);
111934       }
111935       if( zStartAff ){
111936         if( sqlite3CompareAffinity(pRight, zStartAff[nEq])==SQLITE_AFF_NONE){
111937           /* Since the comparison is to be performed with no conversions
111938           ** applied to the operands, set the affinity to apply to pRight to
111939           ** SQLITE_AFF_NONE.  */
111940           zStartAff[nEq] = SQLITE_AFF_NONE;
111941         }
111942         if( sqlite3ExprNeedsNoAffinityChange(pRight, zStartAff[nEq]) ){
111943           zStartAff[nEq] = SQLITE_AFF_NONE;
111944         }
111945       }
111946       nConstraint++;
111947       testcase( pRangeStart->wtFlags & TERM_VIRTUAL );
111948     }else if( isMinQuery ){
111949       sqlite3VdbeAddOp2(v, OP_Null, 0, regBase+nEq);
111950       nConstraint++;
111951       startEq = 0;
111952       start_constraints = 1;
111953     }
111954     codeApplyAffinity(pParse, regBase, nConstraint, zStartAff);
111955     op = aStartOp[(start_constraints<<2) + (startEq<<1) + bRev];
111956     assert( op!=0 );
111957     testcase( op==OP_Rewind );
111958     testcase( op==OP_Last );
111959     testcase( op==OP_SeekGt );
111960     testcase( op==OP_SeekGe );
111961     testcase( op==OP_SeekLe );
111962     testcase( op==OP_SeekLt );
111963     sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint);
111964 
111965     /* Load the value for the inequality constraint at the end of the
111966     ** range (if any).
111967     */
111968     nConstraint = nEq;
111969     if( pRangeEnd ){
111970       Expr *pRight = pRangeEnd->pExpr->pRight;
111971       sqlite3ExprCacheRemove(pParse, regBase+nEq, 1);
111972       sqlite3ExprCode(pParse, pRight, regBase+nEq);
111973       if( (pRangeEnd->wtFlags & TERM_VNULL)==0 ){
111974         sqlite3ExprCodeIsNullJump(v, pRight, regBase+nEq, addrNxt);
111975       }
111976       if( sqlite3CompareAffinity(pRight, cEndAff)!=SQLITE_AFF_NONE
111977        && !sqlite3ExprNeedsNoAffinityChange(pRight, cEndAff)
111978       ){
111979         codeApplyAffinity(pParse, regBase+nEq, 1, &cEndAff);
111980       }
111981       nConstraint++;
111982       testcase( pRangeEnd->wtFlags & TERM_VIRTUAL );
111983     }
111984     sqlite3DbFree(db, zStartAff);
111985 
111986     /* Top of the loop body */
111987     pLevel->p2 = sqlite3VdbeCurrentAddr(v);
111988 
111989     /* Check if the index cursor is past the end of the range. */
111990     op = aEndOp[(pRangeEnd || nEq) * (1 + bRev)];
111991     testcase( op==OP_Noop );
111992     testcase( op==OP_IdxGE );
111993     testcase( op==OP_IdxLT );
111994     if( op!=OP_Noop ){
111995       sqlite3VdbeAddOp4Int(v, op, iIdxCur, addrNxt, regBase, nConstraint);
111996       sqlite3VdbeChangeP5(v, endEq!=bRev ?1:0);
111997     }
111998 
111999     /* If there are inequality constraints, check that the value
112000     ** of the table column that the inequality contrains is not NULL.
112001     ** If it is, jump to the next iteration of the loop.
112002     */
112003     r1 = sqlite3GetTempReg(pParse);
112004     testcase( pLoop->wsFlags & WHERE_BTM_LIMIT );
112005     testcase( pLoop->wsFlags & WHERE_TOP_LIMIT );
112006     if( (pLoop->wsFlags & (WHERE_BTM_LIMIT|WHERE_TOP_LIMIT))!=0
112007      && (j = pIdx->aiColumn[nEq])>=0
112008      && pIdx->pTable->aCol[j].notNull==0
112009      && (nEq || (pLoop->wsFlags & WHERE_BTM_LIMIT)==0)
112010     ){
112011       sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, nEq, r1);
112012       VdbeComment((v, "%s", pIdx->pTable->aCol[j].zName));
112013       sqlite3VdbeAddOp2(v, OP_IsNull, r1, addrCont);
112014     }
112015     sqlite3ReleaseTempReg(pParse, r1);
112016 
112017     /* Seek the table cursor, if required */
112018     disableTerm(pLevel, pRangeStart);
112019     disableTerm(pLevel, pRangeEnd);
112020     if( omitTable ){
112021       /* pIdx is a covering index.  No need to access the main table. */
112022     }else if( HasRowid(pIdx->pTable) ){
112023       iRowidReg = iReleaseReg = sqlite3GetTempReg(pParse);
112024       sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur, iRowidReg);
112025       sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg);
112026       sqlite3VdbeAddOp2(v, OP_Seek, iCur, iRowidReg);  /* Deferred seek */
112027     }else{
112028       Index *pPk = sqlite3PrimaryKeyIndex(pIdx->pTable);
112029       iRowidReg = sqlite3GetTempRange(pParse, pPk->nKeyCol);
112030       for(j=0; j<pPk->nKeyCol; j++){
112031         k = sqlite3ColumnOfIndex(pIdx, pPk->aiColumn[j]);
112032         sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, k, iRowidReg+j);
112033       }
112034       sqlite3VdbeAddOp4Int(v, OP_NotFound, iCur, addrCont,
112035                            iRowidReg, pPk->nKeyCol);
112036     }
112037 
112038     /* Record the instruction used to terminate the loop. Disable
112039     ** WHERE clause terms made redundant by the index range scan.
112040     */
112041     if( pLoop->wsFlags & WHERE_ONEROW ){
112042       pLevel->op = OP_Noop;
112043     }else if( bRev ){
112044       pLevel->op = OP_Prev;
112045     }else{
112046       pLevel->op = OP_Next;
112047     }
112048     pLevel->p1 = iIdxCur;
112049     if( (pLoop->wsFlags & WHERE_CONSTRAINT)==0 ){
112050       pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
112051     }else{
112052       assert( pLevel->p5==0 );
112053     }
112054   }else
112055 
112056 #ifndef SQLITE_OMIT_OR_OPTIMIZATION
112057   if( pLoop->wsFlags & WHERE_MULTI_OR ){
112058     /* Case 5:  Two or more separately indexed terms connected by OR
112059     **
112060     ** Example:
112061     **
112062     **   CREATE TABLE t1(a,b,c,d);
112063     **   CREATE INDEX i1 ON t1(a);
112064     **   CREATE INDEX i2 ON t1(b);
112065     **   CREATE INDEX i3 ON t1(c);
112066     **
112067     **   SELECT * FROM t1 WHERE a=5 OR b=7 OR (c=11 AND d=13)
112068     **
112069     ** In the example, there are three indexed terms connected by OR.
112070     ** The top of the loop looks like this:
112071     **
112072     **          Null       1                # Zero the rowset in reg 1
112073     **
112074     ** Then, for each indexed term, the following. The arguments to
112075     ** RowSetTest are such that the rowid of the current row is inserted
112076     ** into the RowSet. If it is already present, control skips the
112077     ** Gosub opcode and jumps straight to the code generated by WhereEnd().
112078     **
112079     **        sqlite3WhereBegin(<term>)
112080     **          RowSetTest                  # Insert rowid into rowset
112081     **          Gosub      2 A
112082     **        sqlite3WhereEnd()
112083     **
112084     ** Following the above, code to terminate the loop. Label A, the target
112085     ** of the Gosub above, jumps to the instruction right after the Goto.
112086     **
112087     **          Null       1                # Zero the rowset in reg 1
112088     **          Goto       B                # The loop is finished.
112089     **
112090     **       A: <loop body>                 # Return data, whatever.
112091     **
112092     **          Return     2                # Jump back to the Gosub
112093     **
112094     **       B: <after the loop>
112095     **
112096     */
112097     WhereClause *pOrWc;    /* The OR-clause broken out into subterms */
112098     SrcList *pOrTab;       /* Shortened table list or OR-clause generation */
112099     Index *pCov = 0;             /* Potential covering index (or NULL) */
112100     int iCovCur = pParse->nTab++;  /* Cursor used for index scans (if any) */
112101 
112102     int regReturn = ++pParse->nMem;           /* Register used with OP_Gosub */
112103     int regRowset = 0;                        /* Register for RowSet object */
112104     int regRowid = 0;                         /* Register holding rowid */
112105     int iLoopBody = sqlite3VdbeMakeLabel(v);  /* Start of loop body */
112106     int iRetInit;                             /* Address of regReturn init */
112107     int untestedTerms = 0;             /* Some terms not completely tested */
112108     int ii;                            /* Loop counter */
112109     Expr *pAndExpr = 0;                /* An ".. AND (...)" expression */
112110 
112111     pTerm = pLoop->aLTerm[0];
112112     assert( pTerm!=0 );
112113     assert( pTerm->eOperator & WO_OR );
112114     assert( (pTerm->wtFlags & TERM_ORINFO)!=0 );
112115     pOrWc = &pTerm->u.pOrInfo->wc;
112116     pLevel->op = OP_Return;
112117     pLevel->p1 = regReturn;
112118 
112119     /* Set up a new SrcList in pOrTab containing the table being scanned
112120     ** by this loop in the a[0] slot and all notReady tables in a[1..] slots.
112121     ** This becomes the SrcList in the recursive call to sqlite3WhereBegin().
112122     */
112123     if( pWInfo->nLevel>1 ){
112124       int nNotReady;                 /* The number of notReady tables */
112125       struct SrcList_item *origSrc;     /* Original list of tables */
112126       nNotReady = pWInfo->nLevel - iLevel - 1;
112127       pOrTab = sqlite3StackAllocRaw(db,
112128                             sizeof(*pOrTab)+ nNotReady*sizeof(pOrTab->a[0]));
112129       if( pOrTab==0 ) return notReady;
112130       pOrTab->nAlloc = (u8)(nNotReady + 1);
112131       pOrTab->nSrc = pOrTab->nAlloc;
112132       memcpy(pOrTab->a, pTabItem, sizeof(*pTabItem));
112133       origSrc = pWInfo->pTabList->a;
112134       for(k=1; k<=nNotReady; k++){
112135         memcpy(&pOrTab->a[k], &origSrc[pLevel[k].iFrom], sizeof(pOrTab->a[k]));
112136       }
112137     }else{
112138       pOrTab = pWInfo->pTabList;
112139     }
112140 
112141     /* Initialize the rowset register to contain NULL. An SQL NULL is
112142     ** equivalent to an empty rowset.
112143     **
112144     ** Also initialize regReturn to contain the address of the instruction
112145     ** immediately following the OP_Return at the bottom of the loop. This
112146     ** is required in a few obscure LEFT JOIN cases where control jumps
112147     ** over the top of the loop into the body of it. In this case the
112148     ** correct response for the end-of-loop code (the OP_Return) is to
112149     ** fall through to the next instruction, just as an OP_Next does if
112150     ** called on an uninitialized cursor.
112151     */
112152     if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
112153       regRowset = ++pParse->nMem;
112154       regRowid = ++pParse->nMem;
112155       sqlite3VdbeAddOp2(v, OP_Null, 0, regRowset);
112156     }
112157     iRetInit = sqlite3VdbeAddOp2(v, OP_Integer, 0, regReturn);
112158 
112159     /* If the original WHERE clause is z of the form:  (x1 OR x2 OR ...) AND y
112160     ** Then for every term xN, evaluate as the subexpression: xN AND z
112161     ** That way, terms in y that are factored into the disjunction will
112162     ** be picked up by the recursive calls to sqlite3WhereBegin() below.
112163     **
112164     ** Actually, each subexpression is converted to "xN AND w" where w is
112165     ** the "interesting" terms of z - terms that did not originate in the
112166     ** ON or USING clause of a LEFT JOIN, and terms that are usable as
112167     ** indices.
112168     **
112169     ** This optimization also only applies if the (x1 OR x2 OR ...) term
112170     ** is not contained in the ON clause of a LEFT JOIN.
112171     ** See ticket http://www.sqlite.org/src/info/f2369304e4
112172     */
112173     if( pWC->nTerm>1 ){
112174       int iTerm;
112175       for(iTerm=0; iTerm<pWC->nTerm; iTerm++){
112176         Expr *pExpr = pWC->a[iTerm].pExpr;
112177         if( &pWC->a[iTerm] == pTerm ) continue;
112178         if( ExprHasProperty(pExpr, EP_FromJoin) ) continue;
112179         testcase( pWC->a[iTerm].wtFlags & TERM_ORINFO );
112180         testcase( pWC->a[iTerm].wtFlags & TERM_VIRTUAL );
112181         if( pWC->a[iTerm].wtFlags & (TERM_ORINFO|TERM_VIRTUAL) ) continue;
112182         if( (pWC->a[iTerm].eOperator & WO_ALL)==0 ) continue;
112183         pExpr = sqlite3ExprDup(db, pExpr, 0);
112184         pAndExpr = sqlite3ExprAnd(db, pAndExpr, pExpr);
112185       }
112186       if( pAndExpr ){
112187         pAndExpr = sqlite3PExpr(pParse, TK_AND, 0, pAndExpr, 0);
112188       }
112189     }
112190 
112191     for(ii=0; ii<pOrWc->nTerm; ii++){
112192       WhereTerm *pOrTerm = &pOrWc->a[ii];
112193       if( pOrTerm->leftCursor==iCur || (pOrTerm->eOperator & WO_AND)!=0 ){
112194         WhereInfo *pSubWInfo;          /* Info for single OR-term scan */
112195         Expr *pOrExpr = pOrTerm->pExpr;
112196         if( pAndExpr && !ExprHasProperty(pOrExpr, EP_FromJoin) ){
112197           pAndExpr->pLeft = pOrExpr;
112198           pOrExpr = pAndExpr;
112199         }
112200         /* Loop through table entries that match term pOrTerm. */
112201         pSubWInfo = sqlite3WhereBegin(pParse, pOrTab, pOrExpr, 0, 0,
112202                         WHERE_OMIT_OPEN_CLOSE | WHERE_AND_ONLY |
112203                         WHERE_FORCE_TABLE | WHERE_ONETABLE_ONLY, iCovCur);
112204         assert( pSubWInfo || pParse->nErr || db->mallocFailed );
112205         if( pSubWInfo ){
112206           WhereLoop *pSubLoop;
112207           explainOneScan(
112208               pParse, pOrTab, &pSubWInfo->a[0], iLevel, pLevel->iFrom, 0
112209           );
112210           if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
112211             int iSet = ((ii==pOrWc->nTerm-1)?-1:ii);
112212             int r;
112213             r = sqlite3ExprCodeGetColumn(pParse, pTabItem->pTab, -1, iCur,
112214                                          regRowid, 0);
112215             sqlite3VdbeAddOp4Int(v, OP_RowSetTest, regRowset,
112216                                  sqlite3VdbeCurrentAddr(v)+2, r, iSet);
112217           }
112218           sqlite3VdbeAddOp2(v, OP_Gosub, regReturn, iLoopBody);
112219 
112220           /* The pSubWInfo->untestedTerms flag means that this OR term
112221           ** contained one or more AND term from a notReady table.  The
112222           ** terms from the notReady table could not be tested and will
112223           ** need to be tested later.
112224           */
112225           if( pSubWInfo->untestedTerms ) untestedTerms = 1;
112226 
112227           /* If all of the OR-connected terms are optimized using the same
112228           ** index, and the index is opened using the same cursor number
112229           ** by each call to sqlite3WhereBegin() made by this loop, it may
112230           ** be possible to use that index as a covering index.
112231           **
112232           ** If the call to sqlite3WhereBegin() above resulted in a scan that
112233           ** uses an index, and this is either the first OR-connected term
112234           ** processed or the index is the same as that used by all previous
112235           ** terms, set pCov to the candidate covering index. Otherwise, set
112236           ** pCov to NULL to indicate that no candidate covering index will
112237           ** be available.
112238           */
112239           pSubLoop = pSubWInfo->a[0].pWLoop;
112240           assert( (pSubLoop->wsFlags & WHERE_AUTO_INDEX)==0 );
112241           if( (pSubLoop->wsFlags & WHERE_INDEXED)!=0
112242            && (ii==0 || pSubLoop->u.btree.pIndex==pCov)
112243           ){
112244             assert( pSubWInfo->a[0].iIdxCur==iCovCur );
112245             pCov = pSubLoop->u.btree.pIndex;
112246           }else{
112247             pCov = 0;
112248           }
112249 
112250           /* Finish the loop through table entries that match term pOrTerm. */
112251           sqlite3WhereEnd(pSubWInfo);
112252         }
112253       }
112254     }
112255     pLevel->u.pCovidx = pCov;
112256     if( pCov ) pLevel->iIdxCur = iCovCur;
112257     if( pAndExpr ){
112258       pAndExpr->pLeft = 0;
112259       sqlite3ExprDelete(db, pAndExpr);
112260     }
112261     sqlite3VdbeChangeP1(v, iRetInit, sqlite3VdbeCurrentAddr(v));
112262     sqlite3VdbeAddOp2(v, OP_Goto, 0, pLevel->addrBrk);
112263     sqlite3VdbeResolveLabel(v, iLoopBody);
112264 
112265     if( pWInfo->nLevel>1 ) sqlite3StackFree(db, pOrTab);
112266     if( !untestedTerms ) disableTerm(pLevel, pTerm);
112267   }else
112268 #endif /* SQLITE_OMIT_OR_OPTIMIZATION */
112269 
112270   {
112271     /* Case 6:  There is no usable index.  We must do a complete
112272     **          scan of the entire table.
112273     */
112274     static const u8 aStep[] = { OP_Next, OP_Prev };
112275     static const u8 aStart[] = { OP_Rewind, OP_Last };
112276     assert( bRev==0 || bRev==1 );
112277     if( pTabItem->isRecursive ){
112278       /* Tables marked isRecursive have only a single row that is stored in
112279       ** a pseudo-cursor.  No need to Rewind or Next such cursors. */
112280       pLevel->op = OP_Noop;
112281     }else{
112282       pLevel->op = aStep[bRev];
112283       pLevel->p1 = iCur;
112284       pLevel->p2 = 1 + sqlite3VdbeAddOp2(v, aStart[bRev], iCur, addrBrk);
112285       pLevel->p5 = SQLITE_STMTSTATUS_FULLSCAN_STEP;
112286     }
112287   }
112288 
112289   /* Insert code to test every subexpression that can be completely
112290   ** computed using the current set of tables.
112291   */
112292   for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){
112293     Expr *pE;
112294     testcase( pTerm->wtFlags & TERM_VIRTUAL );
112295     testcase( pTerm->wtFlags & TERM_CODED );
112296     if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
112297     if( (pTerm->prereqAll & pLevel->notReady)!=0 ){
112298       testcase( pWInfo->untestedTerms==0
112299                && (pWInfo->wctrlFlags & WHERE_ONETABLE_ONLY)!=0 );
112300       pWInfo->untestedTerms = 1;
112301       continue;
112302     }
112303     pE = pTerm->pExpr;
112304     assert( pE!=0 );
112305     if( pLevel->iLeftJoin && !ExprHasProperty(pE, EP_FromJoin) ){
112306       continue;
112307     }
112308     sqlite3ExprIfFalse(pParse, pE, addrCont, SQLITE_JUMPIFNULL);
112309     pTerm->wtFlags |= TERM_CODED;
112310   }
112311 
112312   /* Insert code to test for implied constraints based on transitivity
112313   ** of the "==" operator.
112314   **
112315   ** Example: If the WHERE clause contains "t1.a=t2.b" and "t2.b=123"
112316   ** and we are coding the t1 loop and the t2 loop has not yet coded,
112317   ** then we cannot use the "t1.a=t2.b" constraint, but we can code
112318   ** the implied "t1.a=123" constraint.
112319   */
112320   for(pTerm=pWC->a, j=pWC->nTerm; j>0; j--, pTerm++){
112321     Expr *pE, *pEAlt;
112322     WhereTerm *pAlt;
112323     if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
112324     if( pTerm->eOperator!=(WO_EQUIV|WO_EQ) ) continue;
112325     if( pTerm->leftCursor!=iCur ) continue;
112326     if( pLevel->iLeftJoin ) continue;
112327     pE = pTerm->pExpr;
112328     assert( !ExprHasProperty(pE, EP_FromJoin) );
112329     assert( (pTerm->prereqRight & pLevel->notReady)!=0 );
112330     pAlt = findTerm(pWC, iCur, pTerm->u.leftColumn, notReady, WO_EQ|WO_IN, 0);
112331     if( pAlt==0 ) continue;
112332     if( pAlt->wtFlags & (TERM_CODED) ) continue;
112333     testcase( pAlt->eOperator & WO_EQ );
112334     testcase( pAlt->eOperator & WO_IN );
112335     VdbeModuleComment((v, "begin transitive constraint"));
112336     pEAlt = sqlite3StackAllocRaw(db, sizeof(*pEAlt));
112337     if( pEAlt ){
112338       *pEAlt = *pAlt->pExpr;
112339       pEAlt->pLeft = pE->pLeft;
112340       sqlite3ExprIfFalse(pParse, pEAlt, addrCont, SQLITE_JUMPIFNULL);
112341       sqlite3StackFree(db, pEAlt);
112342     }
112343   }
112344 
112345   /* For a LEFT OUTER JOIN, generate code that will record the fact that
112346   ** at least one row of the right table has matched the left table.
112347   */
112348   if( pLevel->iLeftJoin ){
112349     pLevel->addrFirst = sqlite3VdbeCurrentAddr(v);
112350     sqlite3VdbeAddOp2(v, OP_Integer, 1, pLevel->iLeftJoin);
112351     VdbeComment((v, "record LEFT JOIN hit"));
112352     sqlite3ExprCacheClear(pParse);
112353     for(pTerm=pWC->a, j=0; j<pWC->nTerm; j++, pTerm++){
112354       testcase( pTerm->wtFlags & TERM_VIRTUAL );
112355       testcase( pTerm->wtFlags & TERM_CODED );
112356       if( pTerm->wtFlags & (TERM_VIRTUAL|TERM_CODED) ) continue;
112357       if( (pTerm->prereqAll & pLevel->notReady)!=0 ){
112358         assert( pWInfo->untestedTerms );
112359         continue;
112360       }
112361       assert( pTerm->pExpr );
112362       sqlite3ExprIfFalse(pParse, pTerm->pExpr, addrCont, SQLITE_JUMPIFNULL);
112363       pTerm->wtFlags |= TERM_CODED;
112364     }
112365   }
112366   sqlite3ReleaseTempReg(pParse, iReleaseReg);
112367 
112368   return pLevel->notReady;
112369 }
112370 
112371 #if defined(WHERETRACE_ENABLED) && defined(SQLITE_ENABLE_TREE_EXPLAIN)
112372 /*
112373 ** Generate "Explanation" text for a WhereTerm.
112374 */
112375 static void whereExplainTerm(Vdbe *v, WhereTerm *pTerm){
112376   char zType[4];
112377   memcpy(zType, "...", 4);
112378   if( pTerm->wtFlags & TERM_VIRTUAL ) zType[0] = 'V';
112379   if( pTerm->eOperator & WO_EQUIV  ) zType[1] = 'E';
112380   if( ExprHasProperty(pTerm->pExpr, EP_FromJoin) ) zType[2] = 'L';
112381   sqlite3ExplainPrintf(v, "%s ", zType);
112382   sqlite3ExplainExpr(v, pTerm->pExpr);
112383 }
112384 #endif /* WHERETRACE_ENABLED && SQLITE_ENABLE_TREE_EXPLAIN */
112385 
112386 
112387 #ifdef WHERETRACE_ENABLED
112388 /*
112389 ** Print a WhereLoop object for debugging purposes
112390 */
112391 static void whereLoopPrint(WhereLoop *p, WhereClause *pWC){
112392   WhereInfo *pWInfo = pWC->pWInfo;
112393   int nb = 1+(pWInfo->pTabList->nSrc+7)/8;
112394   struct SrcList_item *pItem = pWInfo->pTabList->a + p->iTab;
112395   Table *pTab = pItem->pTab;
112396   sqlite3DebugPrintf("%c%2d.%0*llx.%0*llx", p->cId,
112397                      p->iTab, nb, p->maskSelf, nb, p->prereq);
112398   sqlite3DebugPrintf(" %12s",
112399                      pItem->zAlias ? pItem->zAlias : pTab->zName);
112400   if( (p->wsFlags & WHERE_VIRTUALTABLE)==0 ){
112401      const char *zName;
112402      if( p->u.btree.pIndex && (zName = p->u.btree.pIndex->zName)!=0 ){
112403       if( strncmp(zName, "sqlite_autoindex_", 17)==0 ){
112404         int i = sqlite3Strlen30(zName) - 1;
112405         while( zName[i]!='_' ) i--;
112406         zName += i;
112407       }
112408       sqlite3DebugPrintf(".%-16s %2d", zName, p->u.btree.nEq);
112409     }else{
112410       sqlite3DebugPrintf("%20s","");
112411     }
112412   }else{
112413     char *z;
112414     if( p->u.vtab.idxStr ){
112415       z = sqlite3_mprintf("(%d,\"%s\",%x)",
112416                 p->u.vtab.idxNum, p->u.vtab.idxStr, p->u.vtab.omitMask);
112417     }else{
112418       z = sqlite3_mprintf("(%d,%x)", p->u.vtab.idxNum, p->u.vtab.omitMask);
112419     }
112420     sqlite3DebugPrintf(" %-19s", z);
112421     sqlite3_free(z);
112422   }
112423   sqlite3DebugPrintf(" f %04x N %d", p->wsFlags, p->nLTerm);
112424   sqlite3DebugPrintf(" cost %d,%d,%d\n", p->rSetup, p->rRun, p->nOut);
112425 #ifdef SQLITE_ENABLE_TREE_EXPLAIN
112426   /* If the 0x100 bit of wheretracing is set, then show all of the constraint
112427   ** expressions in the WhereLoop.aLTerm[] array.
112428   */
112429   if( p->nLTerm && (sqlite3WhereTrace & 0x100)!=0 ){  /* WHERETRACE 0x100 */
112430     int i;
112431     Vdbe *v = pWInfo->pParse->pVdbe;
112432     sqlite3ExplainBegin(v);
112433     for(i=0; i<p->nLTerm; i++){
112434       WhereTerm *pTerm = p->aLTerm[i];
112435       if( pTerm==0 ) continue;
112436       sqlite3ExplainPrintf(v, "  (%d) #%-2d ", i+1, (int)(pTerm-pWC->a));
112437       sqlite3ExplainPush(v);
112438       whereExplainTerm(v, pTerm);
112439       sqlite3ExplainPop(v);
112440       sqlite3ExplainNL(v);
112441     }
112442     sqlite3ExplainFinish(v);
112443     sqlite3DebugPrintf("%s", sqlite3VdbeExplanation(v));
112444   }
112445 #endif
112446 }
112447 #endif
112448 
112449 /*
112450 ** Convert bulk memory into a valid WhereLoop that can be passed
112451 ** to whereLoopClear harmlessly.
112452 */
112453 static void whereLoopInit(WhereLoop *p){
112454   p->aLTerm = p->aLTermSpace;
112455   p->nLTerm = 0;
112456   p->nLSlot = ArraySize(p->aLTermSpace);
112457   p->wsFlags = 0;
112458 }
112459 
112460 /*
112461 ** Clear the WhereLoop.u union.  Leave WhereLoop.pLTerm intact.
112462 */
112463 static void whereLoopClearUnion(sqlite3 *db, WhereLoop *p){
112464   if( p->wsFlags & (WHERE_VIRTUALTABLE|WHERE_AUTO_INDEX) ){
112465     if( (p->wsFlags & WHERE_VIRTUALTABLE)!=0 && p->u.vtab.needFree ){
112466       sqlite3_free(p->u.vtab.idxStr);
112467       p->u.vtab.needFree = 0;
112468       p->u.vtab.idxStr = 0;
112469     }else if( (p->wsFlags & WHERE_AUTO_INDEX)!=0 && p->u.btree.pIndex!=0 ){
112470       sqlite3DbFree(db, p->u.btree.pIndex->zColAff);
112471       sqlite3KeyInfoUnref(p->u.btree.pIndex->pKeyInfo);
112472       sqlite3DbFree(db, p->u.btree.pIndex);
112473       p->u.btree.pIndex = 0;
112474     }
112475   }
112476 }
112477 
112478 /*
112479 ** Deallocate internal memory used by a WhereLoop object
112480 */
112481 static void whereLoopClear(sqlite3 *db, WhereLoop *p){
112482   if( p->aLTerm!=p->aLTermSpace ) sqlite3DbFree(db, p->aLTerm);
112483   whereLoopClearUnion(db, p);
112484   whereLoopInit(p);
112485 }
112486 
112487 /*
112488 ** Increase the memory allocation for pLoop->aLTerm[] to be at least n.
112489 */
112490 static int whereLoopResize(sqlite3 *db, WhereLoop *p, int n){
112491   WhereTerm **paNew;
112492   if( p->nLSlot>=n ) return SQLITE_OK;
112493   n = (n+7)&~7;
112494   paNew = sqlite3DbMallocRaw(db, sizeof(p->aLTerm[0])*n);
112495   if( paNew==0 ) return SQLITE_NOMEM;
112496   memcpy(paNew, p->aLTerm, sizeof(p->aLTerm[0])*p->nLSlot);
112497   if( p->aLTerm!=p->aLTermSpace ) sqlite3DbFree(db, p->aLTerm);
112498   p->aLTerm = paNew;
112499   p->nLSlot = n;
112500   return SQLITE_OK;
112501 }
112502 
112503 /*
112504 ** Transfer content from the second pLoop into the first.
112505 */
112506 static int whereLoopXfer(sqlite3 *db, WhereLoop *pTo, WhereLoop *pFrom){
112507   whereLoopClearUnion(db, pTo);
112508   if( whereLoopResize(db, pTo, pFrom->nLTerm) ){
112509     memset(&pTo->u, 0, sizeof(pTo->u));
112510     return SQLITE_NOMEM;
112511   }
112512   memcpy(pTo, pFrom, WHERE_LOOP_XFER_SZ);
112513   memcpy(pTo->aLTerm, pFrom->aLTerm, pTo->nLTerm*sizeof(pTo->aLTerm[0]));
112514   if( pFrom->wsFlags & WHERE_VIRTUALTABLE ){
112515     pFrom->u.vtab.needFree = 0;
112516   }else if( (pFrom->wsFlags & WHERE_AUTO_INDEX)!=0 ){
112517     pFrom->u.btree.pIndex = 0;
112518   }
112519   return SQLITE_OK;
112520 }
112521 
112522 /*
112523 ** Delete a WhereLoop object
112524 */
112525 static void whereLoopDelete(sqlite3 *db, WhereLoop *p){
112526   whereLoopClear(db, p);
112527   sqlite3DbFree(db, p);
112528 }
112529 
112530 /*
112531 ** Free a WhereInfo structure
112532 */
112533 static void whereInfoFree(sqlite3 *db, WhereInfo *pWInfo){
112534   if( ALWAYS(pWInfo) ){
112535     whereClauseClear(&pWInfo->sWC);
112536     while( pWInfo->pLoops ){
112537       WhereLoop *p = pWInfo->pLoops;
112538       pWInfo->pLoops = p->pNextLoop;
112539       whereLoopDelete(db, p);
112540     }
112541     sqlite3DbFree(db, pWInfo);
112542   }
112543 }
112544 
112545 /*
112546 ** Insert or replace a WhereLoop entry using the template supplied.
112547 **
112548 ** An existing WhereLoop entry might be overwritten if the new template
112549 ** is better and has fewer dependencies.  Or the template will be ignored
112550 ** and no insert will occur if an existing WhereLoop is faster and has
112551 ** fewer dependencies than the template.  Otherwise a new WhereLoop is
112552 ** added based on the template.
112553 **
112554 ** If pBuilder->pOrSet is not NULL then we only care about only the
112555 ** prerequisites and rRun and nOut costs of the N best loops.  That
112556 ** information is gathered in the pBuilder->pOrSet object.  This special
112557 ** processing mode is used only for OR clause processing.
112558 **
112559 ** When accumulating multiple loops (when pBuilder->pOrSet is NULL) we
112560 ** still might overwrite similar loops with the new template if the
112561 ** template is better.  Loops may be overwritten if the following
112562 ** conditions are met:
112563 **
112564 **    (1)  They have the same iTab.
112565 **    (2)  They have the same iSortIdx.
112566 **    (3)  The template has same or fewer dependencies than the current loop
112567 **    (4)  The template has the same or lower cost than the current loop
112568 **    (5)  The template uses more terms of the same index but has no additional
112569 **         dependencies
112570 */
112571 static int whereLoopInsert(WhereLoopBuilder *pBuilder, WhereLoop *pTemplate){
112572   WhereLoop **ppPrev, *p, *pNext = 0;
112573   WhereInfo *pWInfo = pBuilder->pWInfo;
112574   sqlite3 *db = pWInfo->pParse->db;
112575 
112576   /* If pBuilder->pOrSet is defined, then only keep track of the costs
112577   ** and prereqs.
112578   */
112579   if( pBuilder->pOrSet!=0 ){
112580 #if WHERETRACE_ENABLED
112581     u16 n = pBuilder->pOrSet->n;
112582     int x =
112583 #endif
112584     whereOrInsert(pBuilder->pOrSet, pTemplate->prereq, pTemplate->rRun,
112585                                     pTemplate->nOut);
112586 #if WHERETRACE_ENABLED /* 0x8 */
112587     if( sqlite3WhereTrace & 0x8 ){
112588       sqlite3DebugPrintf(x?"   or-%d:  ":"   or-X:  ", n);
112589       whereLoopPrint(pTemplate, pBuilder->pWC);
112590     }
112591 #endif
112592     return SQLITE_OK;
112593   }
112594 
112595   /* Search for an existing WhereLoop to overwrite, or which takes
112596   ** priority over pTemplate.
112597   */
112598   for(ppPrev=&pWInfo->pLoops, p=*ppPrev; p; ppPrev=&p->pNextLoop, p=*ppPrev){
112599     if( p->iTab!=pTemplate->iTab || p->iSortIdx!=pTemplate->iSortIdx ){
112600       /* If either the iTab or iSortIdx values for two WhereLoop are different
112601       ** then those WhereLoops need to be considered separately.  Neither is
112602       ** a candidate to replace the other. */
112603       continue;
112604     }
112605     /* In the current implementation, the rSetup value is either zero
112606     ** or the cost of building an automatic index (NlogN) and the NlogN
112607     ** is the same for compatible WhereLoops. */
112608     assert( p->rSetup==0 || pTemplate->rSetup==0
112609                  || p->rSetup==pTemplate->rSetup );
112610 
112611     /* whereLoopAddBtree() always generates and inserts the automatic index
112612     ** case first.  Hence compatible candidate WhereLoops never have a larger
112613     ** rSetup. Call this SETUP-INVARIANT */
112614     assert( p->rSetup>=pTemplate->rSetup );
112615 
112616     if( (p->prereq & pTemplate->prereq)==p->prereq
112617      && p->rSetup<=pTemplate->rSetup
112618      && p->rRun<=pTemplate->rRun
112619      && p->nOut<=pTemplate->nOut
112620     ){
112621       /* This branch taken when p is equal or better than pTemplate in
112622       ** all of (1) dependencies (2) setup-cost, (3) run-cost, and
112623       ** (4) number of output rows. */
112624       assert( p->rSetup==pTemplate->rSetup );
112625       if( p->prereq==pTemplate->prereq
112626        && p->nLTerm<pTemplate->nLTerm
112627        && (p->wsFlags & pTemplate->wsFlags & WHERE_INDEXED)!=0
112628        && (p->u.btree.pIndex==pTemplate->u.btree.pIndex
112629           || pTemplate->rRun+p->nLTerm<=p->rRun+pTemplate->nLTerm)
112630       ){
112631         /* Overwrite an existing WhereLoop with an similar one that uses
112632         ** more terms of the index */
112633         pNext = p->pNextLoop;
112634         break;
112635       }else{
112636         /* pTemplate is not helpful.
112637         ** Return without changing or adding anything */
112638         goto whereLoopInsert_noop;
112639       }
112640     }
112641     if( (p->prereq & pTemplate->prereq)==pTemplate->prereq
112642      && p->rRun>=pTemplate->rRun
112643      && p->nOut>=pTemplate->nOut
112644     ){
112645       /* Overwrite an existing WhereLoop with a better one: one that is
112646       ** better at one of (1) dependencies, (2) setup-cost, (3) run-cost
112647       ** or (4) number of output rows, and is no worse in any of those
112648       ** categories. */
112649       assert( p->rSetup>=pTemplate->rSetup ); /* SETUP-INVARIANT above */
112650       pNext = p->pNextLoop;
112651       break;
112652     }
112653   }
112654 
112655   /* If we reach this point it means that either p[] should be overwritten
112656   ** with pTemplate[] if p[] exists, or if p==NULL then allocate a new
112657   ** WhereLoop and insert it.
112658   */
112659 #if WHERETRACE_ENABLED /* 0x8 */
112660   if( sqlite3WhereTrace & 0x8 ){
112661     if( p!=0 ){
112662       sqlite3DebugPrintf("ins-del:  ");
112663       whereLoopPrint(p, pBuilder->pWC);
112664     }
112665     sqlite3DebugPrintf("ins-new:  ");
112666     whereLoopPrint(pTemplate, pBuilder->pWC);
112667   }
112668 #endif
112669   if( p==0 ){
112670     p = sqlite3DbMallocRaw(db, sizeof(WhereLoop));
112671     if( p==0 ) return SQLITE_NOMEM;
112672     whereLoopInit(p);
112673   }
112674   whereLoopXfer(db, p, pTemplate);
112675   p->pNextLoop = pNext;
112676   *ppPrev = p;
112677   if( (p->wsFlags & WHERE_VIRTUALTABLE)==0 ){
112678     Index *pIndex = p->u.btree.pIndex;
112679     if( pIndex && pIndex->tnum==0 ){
112680       p->u.btree.pIndex = 0;
112681     }
112682   }
112683   return SQLITE_OK;
112684 
112685   /* Jump here if the insert is a no-op */
112686 whereLoopInsert_noop:
112687 #if WHERETRACE_ENABLED /* 0x8 */
112688   if( sqlite3WhereTrace & 0x8 ){
112689     sqlite3DebugPrintf("ins-noop: ");
112690     whereLoopPrint(pTemplate, pBuilder->pWC);
112691   }
112692 #endif
112693   return SQLITE_OK;
112694 }
112695 
112696 /*
112697 ** Adjust the WhereLoop.nOut value downward to account for terms of the
112698 ** WHERE clause that reference the loop but which are not used by an
112699 ** index.
112700 **
112701 ** In the current implementation, the first extra WHERE clause term reduces
112702 ** the number of output rows by a factor of 10 and each additional term
112703 ** reduces the number of output rows by sqrt(2).
112704 */
112705 static void whereLoopOutputAdjust(WhereClause *pWC, WhereLoop *pLoop){
112706   WhereTerm *pTerm, *pX;
112707   Bitmask notAllowed = ~(pLoop->prereq|pLoop->maskSelf);
112708   int i, j;
112709 
112710   if( !OptimizationEnabled(pWC->pWInfo->pParse->db, SQLITE_AdjustOutEst) ){
112711     return;
112712   }
112713   for(i=pWC->nTerm, pTerm=pWC->a; i>0; i--, pTerm++){
112714     if( (pTerm->wtFlags & TERM_VIRTUAL)!=0 ) break;
112715     if( (pTerm->prereqAll & pLoop->maskSelf)==0 ) continue;
112716     if( (pTerm->prereqAll & notAllowed)!=0 ) continue;
112717     for(j=pLoop->nLTerm-1; j>=0; j--){
112718       pX = pLoop->aLTerm[j];
112719       if( pX==0 ) continue;
112720       if( pX==pTerm ) break;
112721       if( pX->iParent>=0 && (&pWC->a[pX->iParent])==pTerm ) break;
112722     }
112723     if( j<0 ) pLoop->nOut += pTerm->truthProb;
112724   }
112725 }
112726 
112727 /*
112728 ** We have so far matched pBuilder->pNew->u.btree.nEq terms of the index pIndex.
112729 ** Try to match one more.
112730 **
112731 ** If pProbe->tnum==0, that means pIndex is a fake index used for the
112732 ** INTEGER PRIMARY KEY.
112733 */
112734 static int whereLoopAddBtreeIndex(
112735   WhereLoopBuilder *pBuilder,     /* The WhereLoop factory */
112736   struct SrcList_item *pSrc,      /* FROM clause term being analyzed */
112737   Index *pProbe,                  /* An index on pSrc */
112738   LogEst nInMul                   /* log(Number of iterations due to IN) */
112739 ){
112740   WhereInfo *pWInfo = pBuilder->pWInfo;  /* WHERE analyse context */
112741   Parse *pParse = pWInfo->pParse;        /* Parsing context */
112742   sqlite3 *db = pParse->db;       /* Database connection malloc context */
112743   WhereLoop *pNew;                /* Template WhereLoop under construction */
112744   WhereTerm *pTerm;               /* A WhereTerm under consideration */
112745   int opMask;                     /* Valid operators for constraints */
112746   WhereScan scan;                 /* Iterator for WHERE terms */
112747   Bitmask saved_prereq;           /* Original value of pNew->prereq */
112748   u16 saved_nLTerm;               /* Original value of pNew->nLTerm */
112749   u16 saved_nEq;                  /* Original value of pNew->u.btree.nEq */
112750   u16 saved_nSkip;                /* Original value of pNew->u.btree.nSkip */
112751   u32 saved_wsFlags;              /* Original value of pNew->wsFlags */
112752   LogEst saved_nOut;              /* Original value of pNew->nOut */
112753   int iCol;                       /* Index of the column in the table */
112754   int rc = SQLITE_OK;             /* Return code */
112755   LogEst nRowEst;                 /* Estimated index selectivity */
112756   LogEst rLogSize;                /* Logarithm of table size */
112757   WhereTerm *pTop = 0, *pBtm = 0; /* Top and bottom range constraints */
112758 
112759   pNew = pBuilder->pNew;
112760   if( db->mallocFailed ) return SQLITE_NOMEM;
112761 
112762   assert( (pNew->wsFlags & WHERE_VIRTUALTABLE)==0 );
112763   assert( (pNew->wsFlags & WHERE_TOP_LIMIT)==0 );
112764   if( pNew->wsFlags & WHERE_BTM_LIMIT ){
112765     opMask = WO_LT|WO_LE;
112766   }else if( pProbe->tnum<=0 || (pSrc->jointype & JT_LEFT)!=0 ){
112767     opMask = WO_EQ|WO_IN|WO_GT|WO_GE|WO_LT|WO_LE;
112768   }else{
112769     opMask = WO_EQ|WO_IN|WO_ISNULL|WO_GT|WO_GE|WO_LT|WO_LE;
112770   }
112771   if( pProbe->bUnordered ) opMask &= ~(WO_GT|WO_GE|WO_LT|WO_LE);
112772 
112773   assert( pNew->u.btree.nEq<=pProbe->nKeyCol );
112774   if( pNew->u.btree.nEq < pProbe->nKeyCol ){
112775     iCol = pProbe->aiColumn[pNew->u.btree.nEq];
112776     nRowEst = sqlite3LogEst(pProbe->aiRowEst[pNew->u.btree.nEq+1]);
112777     if( nRowEst==0 && pProbe->onError==OE_None ) nRowEst = 1;
112778   }else{
112779     iCol = -1;
112780     nRowEst = 0;
112781   }
112782   pTerm = whereScanInit(&scan, pBuilder->pWC, pSrc->iCursor, iCol,
112783                         opMask, pProbe);
112784   saved_nEq = pNew->u.btree.nEq;
112785   saved_nSkip = pNew->u.btree.nSkip;
112786   saved_nLTerm = pNew->nLTerm;
112787   saved_wsFlags = pNew->wsFlags;
112788   saved_prereq = pNew->prereq;
112789   saved_nOut = pNew->nOut;
112790   pNew->rSetup = 0;
112791   rLogSize = estLog(sqlite3LogEst(pProbe->aiRowEst[0]));
112792 
112793   /* Consider using a skip-scan if there are no WHERE clause constraints
112794   ** available for the left-most terms of the index, and if the average
112795   ** number of repeats in the left-most terms is at least 18.  The magic
112796   ** number 18 was found by experimentation to be the payoff point where
112797   ** skip-scan become faster than a full-scan.
112798   */
112799   if( pTerm==0
112800    && saved_nEq==saved_nSkip
112801    && saved_nEq+1<pProbe->nKeyCol
112802    && pProbe->aiRowEst[saved_nEq+1]>=18  /* TUNING: Minimum for skip-scan */
112803    && (rc = whereLoopResize(db, pNew, pNew->nLTerm+1))==SQLITE_OK
112804   ){
112805     LogEst nIter;
112806     pNew->u.btree.nEq++;
112807     pNew->u.btree.nSkip++;
112808     pNew->aLTerm[pNew->nLTerm++] = 0;
112809     pNew->wsFlags |= WHERE_SKIPSCAN;
112810     nIter = sqlite3LogEst(pProbe->aiRowEst[0]/pProbe->aiRowEst[saved_nEq+1]);
112811     whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, nIter);
112812   }
112813   for(; rc==SQLITE_OK && pTerm!=0; pTerm = whereScanNext(&scan)){
112814     int nIn = 0;
112815 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
112816     int nRecValid = pBuilder->nRecValid;
112817 #endif
112818     if( (pTerm->eOperator==WO_ISNULL || (pTerm->wtFlags&TERM_VNULL)!=0)
112819      && (iCol<0 || pSrc->pTab->aCol[iCol].notNull)
112820     ){
112821       continue; /* ignore IS [NOT] NULL constraints on NOT NULL columns */
112822     }
112823     if( pTerm->prereqRight & pNew->maskSelf ) continue;
112824 
112825     assert( pNew->nOut==saved_nOut );
112826 
112827     pNew->wsFlags = saved_wsFlags;
112828     pNew->u.btree.nEq = saved_nEq;
112829     pNew->nLTerm = saved_nLTerm;
112830     if( whereLoopResize(db, pNew, pNew->nLTerm+1) ) break; /* OOM */
112831     pNew->aLTerm[pNew->nLTerm++] = pTerm;
112832     pNew->prereq = (saved_prereq | pTerm->prereqRight) & ~pNew->maskSelf;
112833     pNew->rRun = rLogSize; /* Baseline cost is log2(N).  Adjustments below */
112834     if( pTerm->eOperator & WO_IN ){
112835       Expr *pExpr = pTerm->pExpr;
112836       pNew->wsFlags |= WHERE_COLUMN_IN;
112837       if( ExprHasProperty(pExpr, EP_xIsSelect) ){
112838         /* "x IN (SELECT ...)":  TUNING: the SELECT returns 25 rows */
112839         nIn = 46;  assert( 46==sqlite3LogEst(25) );
112840       }else if( ALWAYS(pExpr->x.pList && pExpr->x.pList->nExpr) ){
112841         /* "x IN (value, value, ...)" */
112842         nIn = sqlite3LogEst(pExpr->x.pList->nExpr);
112843       }
112844       pNew->rRun += nIn;
112845       pNew->u.btree.nEq++;
112846       pNew->nOut = nRowEst + nInMul + nIn;
112847     }else if( pTerm->eOperator & (WO_EQ) ){
112848       assert(
112849         (pNew->wsFlags & (WHERE_COLUMN_NULL|WHERE_COLUMN_IN|WHERE_SKIPSCAN))!=0
112850         || nInMul==0
112851       );
112852       pNew->wsFlags |= WHERE_COLUMN_EQ;
112853       if( iCol<0
112854        || (pProbe->onError!=OE_None && nInMul==0
112855            && pNew->u.btree.nEq==pProbe->nKeyCol-1)
112856       ){
112857         assert( (pNew->wsFlags & WHERE_COLUMN_IN)==0 || iCol<0 );
112858         pNew->wsFlags |= WHERE_ONEROW;
112859       }
112860       pNew->u.btree.nEq++;
112861       pNew->nOut = nRowEst + nInMul;
112862     }else if( pTerm->eOperator & (WO_ISNULL) ){
112863       pNew->wsFlags |= WHERE_COLUMN_NULL;
112864       pNew->u.btree.nEq++;
112865       /* TUNING: IS NULL selects 2 rows */
112866       nIn = 10;  assert( 10==sqlite3LogEst(2) );
112867       pNew->nOut = nRowEst + nInMul + nIn;
112868     }else if( pTerm->eOperator & (WO_GT|WO_GE) ){
112869       testcase( pTerm->eOperator & WO_GT );
112870       testcase( pTerm->eOperator & WO_GE );
112871       pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_BTM_LIMIT;
112872       pBtm = pTerm;
112873       pTop = 0;
112874     }else{
112875       assert( pTerm->eOperator & (WO_LT|WO_LE) );
112876       testcase( pTerm->eOperator & WO_LT );
112877       testcase( pTerm->eOperator & WO_LE );
112878       pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_TOP_LIMIT;
112879       pTop = pTerm;
112880       pBtm = (pNew->wsFlags & WHERE_BTM_LIMIT)!=0 ?
112881                      pNew->aLTerm[pNew->nLTerm-2] : 0;
112882     }
112883     if( pNew->wsFlags & WHERE_COLUMN_RANGE ){
112884       /* Adjust nOut and rRun for STAT3 range values */
112885       assert( pNew->nOut==saved_nOut );
112886       whereRangeScanEst(pParse, pBuilder, pBtm, pTop, pNew);
112887     }
112888 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
112889     if( nInMul==0
112890      && pProbe->nSample
112891      && pNew->u.btree.nEq<=pProbe->nSampleCol
112892      && OptimizationEnabled(db, SQLITE_Stat3)
112893     ){
112894       Expr *pExpr = pTerm->pExpr;
112895       tRowcnt nOut = 0;
112896       if( (pTerm->eOperator & (WO_EQ|WO_ISNULL))!=0 ){
112897         testcase( pTerm->eOperator & WO_EQ );
112898         testcase( pTerm->eOperator & WO_ISNULL );
112899         rc = whereEqualScanEst(pParse, pBuilder, pExpr->pRight, &nOut);
112900       }else if( (pTerm->eOperator & WO_IN)
112901              &&  !ExprHasProperty(pExpr, EP_xIsSelect)  ){
112902         rc = whereInScanEst(pParse, pBuilder, pExpr->x.pList, &nOut);
112903       }
112904       assert( nOut==0 || rc==SQLITE_OK );
112905       if( nOut ){
112906         pNew->nOut = sqlite3LogEst(nOut);
112907         if( pNew->nOut>saved_nOut ) pNew->nOut = saved_nOut;
112908       }
112909     }
112910 #endif
112911     if( (pNew->wsFlags & (WHERE_IDX_ONLY|WHERE_IPK))==0 ){
112912       /* Each row involves a step of the index, then a binary search of
112913       ** the main table */
112914       pNew->rRun =  sqlite3LogEstAdd(pNew->rRun,rLogSize>27 ? rLogSize-17 : 10);
112915     }
112916     /* Step cost for each output row */
112917     pNew->rRun = sqlite3LogEstAdd(pNew->rRun, pNew->nOut);
112918     whereLoopOutputAdjust(pBuilder->pWC, pNew);
112919     rc = whereLoopInsert(pBuilder, pNew);
112920     if( (pNew->wsFlags & WHERE_TOP_LIMIT)==0
112921      && pNew->u.btree.nEq<(pProbe->nKeyCol + (pProbe->zName!=0))
112922     ){
112923       whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, nInMul+nIn);
112924     }
112925     pNew->nOut = saved_nOut;
112926 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
112927     pBuilder->nRecValid = nRecValid;
112928 #endif
112929   }
112930   pNew->prereq = saved_prereq;
112931   pNew->u.btree.nEq = saved_nEq;
112932   pNew->u.btree.nSkip = saved_nSkip;
112933   pNew->wsFlags = saved_wsFlags;
112934   pNew->nOut = saved_nOut;
112935   pNew->nLTerm = saved_nLTerm;
112936   return rc;
112937 }
112938 
112939 /*
112940 ** Return True if it is possible that pIndex might be useful in
112941 ** implementing the ORDER BY clause in pBuilder.
112942 **
112943 ** Return False if pBuilder does not contain an ORDER BY clause or
112944 ** if there is no way for pIndex to be useful in implementing that
112945 ** ORDER BY clause.
112946 */
112947 static int indexMightHelpWithOrderBy(
112948   WhereLoopBuilder *pBuilder,
112949   Index *pIndex,
112950   int iCursor
112951 ){
112952   ExprList *pOB;
112953   int ii, jj;
112954 
112955   if( pIndex->bUnordered ) return 0;
112956   if( (pOB = pBuilder->pWInfo->pOrderBy)==0 ) return 0;
112957   for(ii=0; ii<pOB->nExpr; ii++){
112958     Expr *pExpr = sqlite3ExprSkipCollate(pOB->a[ii].pExpr);
112959     if( pExpr->op!=TK_COLUMN ) return 0;
112960     if( pExpr->iTable==iCursor ){
112961       for(jj=0; jj<pIndex->nKeyCol; jj++){
112962         if( pExpr->iColumn==pIndex->aiColumn[jj] ) return 1;
112963       }
112964     }
112965   }
112966   return 0;
112967 }
112968 
112969 /*
112970 ** Return a bitmask where 1s indicate that the corresponding column of
112971 ** the table is used by an index.  Only the first 63 columns are considered.
112972 */
112973 static Bitmask columnsInIndex(Index *pIdx){
112974   Bitmask m = 0;
112975   int j;
112976   for(j=pIdx->nColumn-1; j>=0; j--){
112977     int x = pIdx->aiColumn[j];
112978     if( x>=0 ){
112979       testcase( x==BMS-1 );
112980       testcase( x==BMS-2 );
112981       if( x<BMS-1 ) m |= MASKBIT(x);
112982     }
112983   }
112984   return m;
112985 }
112986 
112987 /* Check to see if a partial index with pPartIndexWhere can be used
112988 ** in the current query.  Return true if it can be and false if not.
112989 */
112990 static int whereUsablePartialIndex(int iTab, WhereClause *pWC, Expr *pWhere){
112991   int i;
112992   WhereTerm *pTerm;
112993   for(i=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
112994     if( sqlite3ExprImpliesExpr(pTerm->pExpr, pWhere, iTab) ) return 1;
112995   }
112996   return 0;
112997 }
112998 
112999 /*
113000 ** Add all WhereLoop objects for a single table of the join where the table
113001 ** is idenfied by pBuilder->pNew->iTab.  That table is guaranteed to be
113002 ** a b-tree table, not a virtual table.
113003 */
113004 static int whereLoopAddBtree(
113005   WhereLoopBuilder *pBuilder, /* WHERE clause information */
113006   Bitmask mExtra              /* Extra prerequesites for using this table */
113007 ){
113008   WhereInfo *pWInfo;          /* WHERE analysis context */
113009   Index *pProbe;              /* An index we are evaluating */
113010   Index sPk;                  /* A fake index object for the primary key */
113011   tRowcnt aiRowEstPk[2];      /* The aiRowEst[] value for the sPk index */
113012   i16 aiColumnPk = -1;        /* The aColumn[] value for the sPk index */
113013   SrcList *pTabList;          /* The FROM clause */
113014   struct SrcList_item *pSrc;  /* The FROM clause btree term to add */
113015   WhereLoop *pNew;            /* Template WhereLoop object */
113016   int rc = SQLITE_OK;         /* Return code */
113017   int iSortIdx = 1;           /* Index number */
113018   int b;                      /* A boolean value */
113019   LogEst rSize;               /* number of rows in the table */
113020   LogEst rLogSize;            /* Logarithm of the number of rows in the table */
113021   WhereClause *pWC;           /* The parsed WHERE clause */
113022   Table *pTab;                /* Table being queried */
113023 
113024   pNew = pBuilder->pNew;
113025   pWInfo = pBuilder->pWInfo;
113026   pTabList = pWInfo->pTabList;
113027   pSrc = pTabList->a + pNew->iTab;
113028   pTab = pSrc->pTab;
113029   pWC = pBuilder->pWC;
113030   assert( !IsVirtual(pSrc->pTab) );
113031 
113032   if( pSrc->pIndex ){
113033     /* An INDEXED BY clause specifies a particular index to use */
113034     pProbe = pSrc->pIndex;
113035   }else if( !HasRowid(pTab) ){
113036     pProbe = pTab->pIndex;
113037   }else{
113038     /* There is no INDEXED BY clause.  Create a fake Index object in local
113039     ** variable sPk to represent the rowid primary key index.  Make this
113040     ** fake index the first in a chain of Index objects with all of the real
113041     ** indices to follow */
113042     Index *pFirst;                  /* First of real indices on the table */
113043     memset(&sPk, 0, sizeof(Index));
113044     sPk.nKeyCol = 1;
113045     sPk.aiColumn = &aiColumnPk;
113046     sPk.aiRowEst = aiRowEstPk;
113047     sPk.onError = OE_Replace;
113048     sPk.pTable = pTab;
113049     aiRowEstPk[0] = pTab->nRowEst;
113050     aiRowEstPk[1] = 1;
113051     pFirst = pSrc->pTab->pIndex;
113052     if( pSrc->notIndexed==0 ){
113053       /* The real indices of the table are only considered if the
113054       ** NOT INDEXED qualifier is omitted from the FROM clause */
113055       sPk.pNext = pFirst;
113056     }
113057     pProbe = &sPk;
113058   }
113059   rSize = sqlite3LogEst(pTab->nRowEst);
113060   rLogSize = estLog(rSize);
113061 
113062 #ifndef SQLITE_OMIT_AUTOMATIC_INDEX
113063   /* Automatic indexes */
113064   if( !pBuilder->pOrSet
113065    && (pWInfo->pParse->db->flags & SQLITE_AutoIndex)!=0
113066    && pSrc->pIndex==0
113067    && !pSrc->viaCoroutine
113068    && !pSrc->notIndexed
113069    && HasRowid(pTab)
113070    && !pSrc->isCorrelated
113071    && !pSrc->isRecursive
113072   ){
113073     /* Generate auto-index WhereLoops */
113074     WhereTerm *pTerm;
113075     WhereTerm *pWCEnd = pWC->a + pWC->nTerm;
113076     for(pTerm=pWC->a; rc==SQLITE_OK && pTerm<pWCEnd; pTerm++){
113077       if( pTerm->prereqRight & pNew->maskSelf ) continue;
113078       if( termCanDriveIndex(pTerm, pSrc, 0) ){
113079         pNew->u.btree.nEq = 1;
113080         pNew->u.btree.nSkip = 0;
113081         pNew->u.btree.pIndex = 0;
113082         pNew->nLTerm = 1;
113083         pNew->aLTerm[0] = pTerm;
113084         /* TUNING: One-time cost for computing the automatic index is
113085         ** approximately 7*N*log2(N) where N is the number of rows in
113086         ** the table being indexed. */
113087         pNew->rSetup = rLogSize + rSize + 28;  assert( 28==sqlite3LogEst(7) );
113088         /* TUNING: Each index lookup yields 20 rows in the table.  This
113089         ** is more than the usual guess of 10 rows, since we have no way
113090         ** of knowning how selective the index will ultimately be.  It would
113091         ** not be unreasonable to make this value much larger. */
113092         pNew->nOut = 43;  assert( 43==sqlite3LogEst(20) );
113093         pNew->rRun = sqlite3LogEstAdd(rLogSize,pNew->nOut);
113094         pNew->wsFlags = WHERE_AUTO_INDEX;
113095         pNew->prereq = mExtra | pTerm->prereqRight;
113096         rc = whereLoopInsert(pBuilder, pNew);
113097       }
113098     }
113099   }
113100 #endif /* SQLITE_OMIT_AUTOMATIC_INDEX */
113101 
113102   /* Loop over all indices
113103   */
113104   for(; rc==SQLITE_OK && pProbe; pProbe=pProbe->pNext, iSortIdx++){
113105     if( pProbe->pPartIdxWhere!=0
113106      && !whereUsablePartialIndex(pNew->iTab, pWC, pProbe->pPartIdxWhere) ){
113107       continue;  /* Partial index inappropriate for this query */
113108     }
113109     pNew->u.btree.nEq = 0;
113110     pNew->u.btree.nSkip = 0;
113111     pNew->nLTerm = 0;
113112     pNew->iSortIdx = 0;
113113     pNew->rSetup = 0;
113114     pNew->prereq = mExtra;
113115     pNew->nOut = rSize;
113116     pNew->u.btree.pIndex = pProbe;
113117     b = indexMightHelpWithOrderBy(pBuilder, pProbe, pSrc->iCursor);
113118     /* The ONEPASS_DESIRED flags never occurs together with ORDER BY */
113119     assert( (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || b==0 );
113120     if( pProbe->tnum<=0 ){
113121       /* Integer primary key index */
113122       pNew->wsFlags = WHERE_IPK;
113123 
113124       /* Full table scan */
113125       pNew->iSortIdx = b ? iSortIdx : 0;
113126       /* TUNING: Cost of full table scan is 3*(N + log2(N)).
113127       **  +  The extra 3 factor is to encourage the use of indexed lookups
113128       **     over full scans.  FIXME */
113129       pNew->rRun = sqlite3LogEstAdd(rSize,rLogSize) + 16;
113130       whereLoopOutputAdjust(pWC, pNew);
113131       rc = whereLoopInsert(pBuilder, pNew);
113132       pNew->nOut = rSize;
113133       if( rc ) break;
113134     }else{
113135       Bitmask m;
113136       if( pProbe->isCovering ){
113137         pNew->wsFlags = WHERE_IDX_ONLY | WHERE_INDEXED;
113138         m = 0;
113139       }else{
113140         m = pSrc->colUsed & ~columnsInIndex(pProbe);
113141         pNew->wsFlags = (m==0) ? (WHERE_IDX_ONLY|WHERE_INDEXED) : WHERE_INDEXED;
113142       }
113143 
113144       /* Full scan via index */
113145       if( b
113146        || !HasRowid(pTab)
113147        || ( m==0
113148          && pProbe->bUnordered==0
113149          && (pProbe->szIdxRow<pTab->szTabRow)
113150          && (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0
113151          && sqlite3GlobalConfig.bUseCis
113152          && OptimizationEnabled(pWInfo->pParse->db, SQLITE_CoverIdxScan)
113153           )
113154       ){
113155         pNew->iSortIdx = b ? iSortIdx : 0;
113156         if( m==0 ){
113157           /* TUNING: Cost of a covering index scan is K*(N + log2(N)).
113158           **  +  The extra factor K of between 1.1 and 3.0 that depends
113159           **     on the relative sizes of the table and the index.  K
113160           **     is smaller for smaller indices, thus favoring them.
113161           */
113162           pNew->rRun = sqlite3LogEstAdd(rSize,rLogSize) + 1 +
113163                         (15*pProbe->szIdxRow)/pTab->szTabRow;
113164         }else{
113165           /* TUNING: Cost of scanning a non-covering index is (N+1)*log2(N)
113166           ** which we will simplify to just N*log2(N) */
113167           pNew->rRun = rSize + rLogSize;
113168         }
113169         whereLoopOutputAdjust(pWC, pNew);
113170         rc = whereLoopInsert(pBuilder, pNew);
113171         pNew->nOut = rSize;
113172         if( rc ) break;
113173       }
113174     }
113175 
113176     rc = whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, 0);
113177 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
113178     sqlite3Stat4ProbeFree(pBuilder->pRec);
113179     pBuilder->nRecValid = 0;
113180     pBuilder->pRec = 0;
113181 #endif
113182 
113183     /* If there was an INDEXED BY clause, then only that one index is
113184     ** considered. */
113185     if( pSrc->pIndex ) break;
113186   }
113187   return rc;
113188 }
113189 
113190 #ifndef SQLITE_OMIT_VIRTUALTABLE
113191 /*
113192 ** Add all WhereLoop objects for a table of the join identified by
113193 ** pBuilder->pNew->iTab.  That table is guaranteed to be a virtual table.
113194 */
113195 static int whereLoopAddVirtual(
113196   WhereLoopBuilder *pBuilder,  /* WHERE clause information */
113197   Bitmask mExtra
113198 ){
113199   WhereInfo *pWInfo;           /* WHERE analysis context */
113200   Parse *pParse;               /* The parsing context */
113201   WhereClause *pWC;            /* The WHERE clause */
113202   struct SrcList_item *pSrc;   /* The FROM clause term to search */
113203   Table *pTab;
113204   sqlite3 *db;
113205   sqlite3_index_info *pIdxInfo;
113206   struct sqlite3_index_constraint *pIdxCons;
113207   struct sqlite3_index_constraint_usage *pUsage;
113208   WhereTerm *pTerm;
113209   int i, j;
113210   int iTerm, mxTerm;
113211   int nConstraint;
113212   int seenIn = 0;              /* True if an IN operator is seen */
113213   int seenVar = 0;             /* True if a non-constant constraint is seen */
113214   int iPhase;                  /* 0: const w/o IN, 1: const, 2: no IN,  2: IN */
113215   WhereLoop *pNew;
113216   int rc = SQLITE_OK;
113217 
113218   pWInfo = pBuilder->pWInfo;
113219   pParse = pWInfo->pParse;
113220   db = pParse->db;
113221   pWC = pBuilder->pWC;
113222   pNew = pBuilder->pNew;
113223   pSrc = &pWInfo->pTabList->a[pNew->iTab];
113224   pTab = pSrc->pTab;
113225   assert( IsVirtual(pTab) );
113226   pIdxInfo = allocateIndexInfo(pParse, pWC, pSrc, pBuilder->pOrderBy);
113227   if( pIdxInfo==0 ) return SQLITE_NOMEM;
113228   pNew->prereq = 0;
113229   pNew->rSetup = 0;
113230   pNew->wsFlags = WHERE_VIRTUALTABLE;
113231   pNew->nLTerm = 0;
113232   pNew->u.vtab.needFree = 0;
113233   pUsage = pIdxInfo->aConstraintUsage;
113234   nConstraint = pIdxInfo->nConstraint;
113235   if( whereLoopResize(db, pNew, nConstraint) ){
113236     sqlite3DbFree(db, pIdxInfo);
113237     return SQLITE_NOMEM;
113238   }
113239 
113240   for(iPhase=0; iPhase<=3; iPhase++){
113241     if( !seenIn && (iPhase&1)!=0 ){
113242       iPhase++;
113243       if( iPhase>3 ) break;
113244     }
113245     if( !seenVar && iPhase>1 ) break;
113246     pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
113247     for(i=0; i<pIdxInfo->nConstraint; i++, pIdxCons++){
113248       j = pIdxCons->iTermOffset;
113249       pTerm = &pWC->a[j];
113250       switch( iPhase ){
113251         case 0:    /* Constants without IN operator */
113252           pIdxCons->usable = 0;
113253           if( (pTerm->eOperator & WO_IN)!=0 ){
113254             seenIn = 1;
113255           }
113256           if( pTerm->prereqRight!=0 ){
113257             seenVar = 1;
113258           }else if( (pTerm->eOperator & WO_IN)==0 ){
113259             pIdxCons->usable = 1;
113260           }
113261           break;
113262         case 1:    /* Constants with IN operators */
113263           assert( seenIn );
113264           pIdxCons->usable = (pTerm->prereqRight==0);
113265           break;
113266         case 2:    /* Variables without IN */
113267           assert( seenVar );
113268           pIdxCons->usable = (pTerm->eOperator & WO_IN)==0;
113269           break;
113270         default:   /* Variables with IN */
113271           assert( seenVar && seenIn );
113272           pIdxCons->usable = 1;
113273           break;
113274       }
113275     }
113276     memset(pUsage, 0, sizeof(pUsage[0])*pIdxInfo->nConstraint);
113277     if( pIdxInfo->needToFreeIdxStr ) sqlite3_free(pIdxInfo->idxStr);
113278     pIdxInfo->idxStr = 0;
113279     pIdxInfo->idxNum = 0;
113280     pIdxInfo->needToFreeIdxStr = 0;
113281     pIdxInfo->orderByConsumed = 0;
113282     pIdxInfo->estimatedCost = SQLITE_BIG_DBL / (double)2;
113283     pIdxInfo->estimatedRows = 25;
113284     rc = vtabBestIndex(pParse, pTab, pIdxInfo);
113285     if( rc ) goto whereLoopAddVtab_exit;
113286     pIdxCons = *(struct sqlite3_index_constraint**)&pIdxInfo->aConstraint;
113287     pNew->prereq = mExtra;
113288     mxTerm = -1;
113289     assert( pNew->nLSlot>=nConstraint );
113290     for(i=0; i<nConstraint; i++) pNew->aLTerm[i] = 0;
113291     pNew->u.vtab.omitMask = 0;
113292     for(i=0; i<nConstraint; i++, pIdxCons++){
113293       if( (iTerm = pUsage[i].argvIndex - 1)>=0 ){
113294         j = pIdxCons->iTermOffset;
113295         if( iTerm>=nConstraint
113296          || j<0
113297          || j>=pWC->nTerm
113298          || pNew->aLTerm[iTerm]!=0
113299         ){
113300           rc = SQLITE_ERROR;
113301           sqlite3ErrorMsg(pParse, "%s.xBestIndex() malfunction", pTab->zName);
113302           goto whereLoopAddVtab_exit;
113303         }
113304         testcase( iTerm==nConstraint-1 );
113305         testcase( j==0 );
113306         testcase( j==pWC->nTerm-1 );
113307         pTerm = &pWC->a[j];
113308         pNew->prereq |= pTerm->prereqRight;
113309         assert( iTerm<pNew->nLSlot );
113310         pNew->aLTerm[iTerm] = pTerm;
113311         if( iTerm>mxTerm ) mxTerm = iTerm;
113312         testcase( iTerm==15 );
113313         testcase( iTerm==16 );
113314         if( iTerm<16 && pUsage[i].omit ) pNew->u.vtab.omitMask |= 1<<iTerm;
113315         if( (pTerm->eOperator & WO_IN)!=0 ){
113316           if( pUsage[i].omit==0 ){
113317             /* Do not attempt to use an IN constraint if the virtual table
113318             ** says that the equivalent EQ constraint cannot be safely omitted.
113319             ** If we do attempt to use such a constraint, some rows might be
113320             ** repeated in the output. */
113321             break;
113322           }
113323           /* A virtual table that is constrained by an IN clause may not
113324           ** consume the ORDER BY clause because (1) the order of IN terms
113325           ** is not necessarily related to the order of output terms and
113326           ** (2) Multiple outputs from a single IN value will not merge
113327           ** together.  */
113328           pIdxInfo->orderByConsumed = 0;
113329         }
113330       }
113331     }
113332     if( i>=nConstraint ){
113333       pNew->nLTerm = mxTerm+1;
113334       assert( pNew->nLTerm<=pNew->nLSlot );
113335       pNew->u.vtab.idxNum = pIdxInfo->idxNum;
113336       pNew->u.vtab.needFree = pIdxInfo->needToFreeIdxStr;
113337       pIdxInfo->needToFreeIdxStr = 0;
113338       pNew->u.vtab.idxStr = pIdxInfo->idxStr;
113339       pNew->u.vtab.isOrdered = (u8)((pIdxInfo->nOrderBy!=0)
113340                                      && pIdxInfo->orderByConsumed);
113341       pNew->rSetup = 0;
113342       pNew->rRun = sqlite3LogEstFromDouble(pIdxInfo->estimatedCost);
113343       pNew->nOut = sqlite3LogEst(pIdxInfo->estimatedRows);
113344       whereLoopInsert(pBuilder, pNew);
113345       if( pNew->u.vtab.needFree ){
113346         sqlite3_free(pNew->u.vtab.idxStr);
113347         pNew->u.vtab.needFree = 0;
113348       }
113349     }
113350   }
113351 
113352 whereLoopAddVtab_exit:
113353   if( pIdxInfo->needToFreeIdxStr ) sqlite3_free(pIdxInfo->idxStr);
113354   sqlite3DbFree(db, pIdxInfo);
113355   return rc;
113356 }
113357 #endif /* SQLITE_OMIT_VIRTUALTABLE */
113358 
113359 /*
113360 ** Add WhereLoop entries to handle OR terms.  This works for either
113361 ** btrees or virtual tables.
113362 */
113363 static int whereLoopAddOr(WhereLoopBuilder *pBuilder, Bitmask mExtra){
113364   WhereInfo *pWInfo = pBuilder->pWInfo;
113365   WhereClause *pWC;
113366   WhereLoop *pNew;
113367   WhereTerm *pTerm, *pWCEnd;
113368   int rc = SQLITE_OK;
113369   int iCur;
113370   WhereClause tempWC;
113371   WhereLoopBuilder sSubBuild;
113372   WhereOrSet sSum, sCur, sPrev;
113373   struct SrcList_item *pItem;
113374 
113375   pWC = pBuilder->pWC;
113376   if( pWInfo->wctrlFlags & WHERE_AND_ONLY ) return SQLITE_OK;
113377   pWCEnd = pWC->a + pWC->nTerm;
113378   pNew = pBuilder->pNew;
113379   memset(&sSum, 0, sizeof(sSum));
113380   pItem = pWInfo->pTabList->a + pNew->iTab;
113381   if( !HasRowid(pItem->pTab) ) return SQLITE_OK;
113382   iCur = pItem->iCursor;
113383 
113384   for(pTerm=pWC->a; pTerm<pWCEnd && rc==SQLITE_OK; pTerm++){
113385     if( (pTerm->eOperator & WO_OR)!=0
113386      && (pTerm->u.pOrInfo->indexable & pNew->maskSelf)!=0
113387     ){
113388       WhereClause * const pOrWC = &pTerm->u.pOrInfo->wc;
113389       WhereTerm * const pOrWCEnd = &pOrWC->a[pOrWC->nTerm];
113390       WhereTerm *pOrTerm;
113391       int once = 1;
113392       int i, j;
113393 
113394       sSubBuild = *pBuilder;
113395       sSubBuild.pOrderBy = 0;
113396       sSubBuild.pOrSet = &sCur;
113397 
113398       for(pOrTerm=pOrWC->a; pOrTerm<pOrWCEnd; pOrTerm++){
113399         if( (pOrTerm->eOperator & WO_AND)!=0 ){
113400           sSubBuild.pWC = &pOrTerm->u.pAndInfo->wc;
113401         }else if( pOrTerm->leftCursor==iCur ){
113402           tempWC.pWInfo = pWC->pWInfo;
113403           tempWC.pOuter = pWC;
113404           tempWC.op = TK_AND;
113405           tempWC.nTerm = 1;
113406           tempWC.a = pOrTerm;
113407           sSubBuild.pWC = &tempWC;
113408         }else{
113409           continue;
113410         }
113411         sCur.n = 0;
113412 #ifndef SQLITE_OMIT_VIRTUALTABLE
113413         if( IsVirtual(pItem->pTab) ){
113414           rc = whereLoopAddVirtual(&sSubBuild, mExtra);
113415         }else
113416 #endif
113417         {
113418           rc = whereLoopAddBtree(&sSubBuild, mExtra);
113419         }
113420         assert( rc==SQLITE_OK || sCur.n==0 );
113421         if( sCur.n==0 ){
113422           sSum.n = 0;
113423           break;
113424         }else if( once ){
113425           whereOrMove(&sSum, &sCur);
113426           once = 0;
113427         }else{
113428           whereOrMove(&sPrev, &sSum);
113429           sSum.n = 0;
113430           for(i=0; i<sPrev.n; i++){
113431             for(j=0; j<sCur.n; j++){
113432               whereOrInsert(&sSum, sPrev.a[i].prereq | sCur.a[j].prereq,
113433                             sqlite3LogEstAdd(sPrev.a[i].rRun, sCur.a[j].rRun),
113434                             sqlite3LogEstAdd(sPrev.a[i].nOut, sCur.a[j].nOut));
113435             }
113436           }
113437         }
113438       }
113439       pNew->nLTerm = 1;
113440       pNew->aLTerm[0] = pTerm;
113441       pNew->wsFlags = WHERE_MULTI_OR;
113442       pNew->rSetup = 0;
113443       pNew->iSortIdx = 0;
113444       memset(&pNew->u, 0, sizeof(pNew->u));
113445       for(i=0; rc==SQLITE_OK && i<sSum.n; i++){
113446         /* TUNING: Multiple by 3.5 for the secondary table lookup */
113447         pNew->rRun = sSum.a[i].rRun + 18;
113448         pNew->nOut = sSum.a[i].nOut;
113449         pNew->prereq = sSum.a[i].prereq;
113450         rc = whereLoopInsert(pBuilder, pNew);
113451       }
113452     }
113453   }
113454   return rc;
113455 }
113456 
113457 /*
113458 ** Add all WhereLoop objects for all tables
113459 */
113460 static int whereLoopAddAll(WhereLoopBuilder *pBuilder){
113461   WhereInfo *pWInfo = pBuilder->pWInfo;
113462   Bitmask mExtra = 0;
113463   Bitmask mPrior = 0;
113464   int iTab;
113465   SrcList *pTabList = pWInfo->pTabList;
113466   struct SrcList_item *pItem;
113467   sqlite3 *db = pWInfo->pParse->db;
113468   int nTabList = pWInfo->nLevel;
113469   int rc = SQLITE_OK;
113470   u8 priorJoinType = 0;
113471   WhereLoop *pNew;
113472 
113473   /* Loop over the tables in the join, from left to right */
113474   pNew = pBuilder->pNew;
113475   whereLoopInit(pNew);
113476   for(iTab=0, pItem=pTabList->a; iTab<nTabList; iTab++, pItem++){
113477     pNew->iTab = iTab;
113478     pNew->maskSelf = getMask(&pWInfo->sMaskSet, pItem->iCursor);
113479     if( ((pItem->jointype|priorJoinType) & (JT_LEFT|JT_CROSS))!=0 ){
113480       mExtra = mPrior;
113481     }
113482     priorJoinType = pItem->jointype;
113483     if( IsVirtual(pItem->pTab) ){
113484       rc = whereLoopAddVirtual(pBuilder, mExtra);
113485     }else{
113486       rc = whereLoopAddBtree(pBuilder, mExtra);
113487     }
113488     if( rc==SQLITE_OK ){
113489       rc = whereLoopAddOr(pBuilder, mExtra);
113490     }
113491     mPrior |= pNew->maskSelf;
113492     if( rc || db->mallocFailed ) break;
113493   }
113494   whereLoopClear(db, pNew);
113495   return rc;
113496 }
113497 
113498 /*
113499 ** Examine a WherePath (with the addition of the extra WhereLoop of the 5th
113500 ** parameters) to see if it outputs rows in the requested ORDER BY
113501 ** (or GROUP BY) without requiring a separate sort operation.  Return:
113502 **
113503 **    0:  ORDER BY is not satisfied.  Sorting required
113504 **    1:  ORDER BY is satisfied.      Omit sorting
113505 **   -1:  Unknown at this time
113506 **
113507 ** Note that processing for WHERE_GROUPBY and WHERE_DISTINCTBY is not as
113508 ** strict.  With GROUP BY and DISTINCT the only requirement is that
113509 ** equivalent rows appear immediately adjacent to one another.  GROUP BY
113510 ** and DISTINT do not require rows to appear in any particular order as long
113511 ** as equivelent rows are grouped together.  Thus for GROUP BY and DISTINCT
113512 ** the pOrderBy terms can be matched in any order.  With ORDER BY, the
113513 ** pOrderBy terms must be matched in strict left-to-right order.
113514 */
113515 static int wherePathSatisfiesOrderBy(
113516   WhereInfo *pWInfo,    /* The WHERE clause */
113517   ExprList *pOrderBy,   /* ORDER BY or GROUP BY or DISTINCT clause to check */
113518   WherePath *pPath,     /* The WherePath to check */
113519   u16 wctrlFlags,       /* Might contain WHERE_GROUPBY or WHERE_DISTINCTBY */
113520   u16 nLoop,            /* Number of entries in pPath->aLoop[] */
113521   WhereLoop *pLast,     /* Add this WhereLoop to the end of pPath->aLoop[] */
113522   Bitmask *pRevMask     /* OUT: Mask of WhereLoops to run in reverse order */
113523 ){
113524   u8 revSet;            /* True if rev is known */
113525   u8 rev;               /* Composite sort order */
113526   u8 revIdx;            /* Index sort order */
113527   u8 isOrderDistinct;   /* All prior WhereLoops are order-distinct */
113528   u8 distinctColumns;   /* True if the loop has UNIQUE NOT NULL columns */
113529   u8 isMatch;           /* iColumn matches a term of the ORDER BY clause */
113530   u16 nKeyCol;          /* Number of key columns in pIndex */
113531   u16 nColumn;          /* Total number of ordered columns in the index */
113532   u16 nOrderBy;         /* Number terms in the ORDER BY clause */
113533   int iLoop;            /* Index of WhereLoop in pPath being processed */
113534   int i, j;             /* Loop counters */
113535   int iCur;             /* Cursor number for current WhereLoop */
113536   int iColumn;          /* A column number within table iCur */
113537   WhereLoop *pLoop = 0; /* Current WhereLoop being processed. */
113538   WhereTerm *pTerm;     /* A single term of the WHERE clause */
113539   Expr *pOBExpr;        /* An expression from the ORDER BY clause */
113540   CollSeq *pColl;       /* COLLATE function from an ORDER BY clause term */
113541   Index *pIndex;        /* The index associated with pLoop */
113542   sqlite3 *db = pWInfo->pParse->db;  /* Database connection */
113543   Bitmask obSat = 0;    /* Mask of ORDER BY terms satisfied so far */
113544   Bitmask obDone;       /* Mask of all ORDER BY terms */
113545   Bitmask orderDistinctMask;  /* Mask of all well-ordered loops */
113546   Bitmask ready;              /* Mask of inner loops */
113547 
113548   /*
113549   ** We say the WhereLoop is "one-row" if it generates no more than one
113550   ** row of output.  A WhereLoop is one-row if all of the following are true:
113551   **  (a) All index columns match with WHERE_COLUMN_EQ.
113552   **  (b) The index is unique
113553   ** Any WhereLoop with an WHERE_COLUMN_EQ constraint on the rowid is one-row.
113554   ** Every one-row WhereLoop will have the WHERE_ONEROW bit set in wsFlags.
113555   **
113556   ** We say the WhereLoop is "order-distinct" if the set of columns from
113557   ** that WhereLoop that are in the ORDER BY clause are different for every
113558   ** row of the WhereLoop.  Every one-row WhereLoop is automatically
113559   ** order-distinct.   A WhereLoop that has no columns in the ORDER BY clause
113560   ** is not order-distinct. To be order-distinct is not quite the same as being
113561   ** UNIQUE since a UNIQUE column or index can have multiple rows that
113562   ** are NULL and NULL values are equivalent for the purpose of order-distinct.
113563   ** To be order-distinct, the columns must be UNIQUE and NOT NULL.
113564   **
113565   ** The rowid for a table is always UNIQUE and NOT NULL so whenever the
113566   ** rowid appears in the ORDER BY clause, the corresponding WhereLoop is
113567   ** automatically order-distinct.
113568   */
113569 
113570   assert( pOrderBy!=0 );
113571 
113572   /* Sortability of virtual tables is determined by the xBestIndex method
113573   ** of the virtual table itself */
113574   if( pLast->wsFlags & WHERE_VIRTUALTABLE ){
113575     testcase( nLoop>0 );  /* True when outer loops are one-row and match
113576                           ** no ORDER BY terms */
113577     return pLast->u.vtab.isOrdered;
113578   }
113579   if( nLoop && OptimizationDisabled(db, SQLITE_OrderByIdxJoin) ) return 0;
113580 
113581   nOrderBy = pOrderBy->nExpr;
113582   testcase( nOrderBy==BMS-1 );
113583   if( nOrderBy>BMS-1 ) return 0;  /* Cannot optimize overly large ORDER BYs */
113584   isOrderDistinct = 1;
113585   obDone = MASKBIT(nOrderBy)-1;
113586   orderDistinctMask = 0;
113587   ready = 0;
113588   for(iLoop=0; isOrderDistinct && obSat<obDone && iLoop<=nLoop; iLoop++){
113589     if( iLoop>0 ) ready |= pLoop->maskSelf;
113590     pLoop = iLoop<nLoop ? pPath->aLoop[iLoop] : pLast;
113591     assert( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 );
113592     iCur = pWInfo->pTabList->a[pLoop->iTab].iCursor;
113593 
113594     /* Mark off any ORDER BY term X that is a column in the table of
113595     ** the current loop for which there is term in the WHERE
113596     ** clause of the form X IS NULL or X=? that reference only outer
113597     ** loops.
113598     */
113599     for(i=0; i<nOrderBy; i++){
113600       if( MASKBIT(i) & obSat ) continue;
113601       pOBExpr = sqlite3ExprSkipCollate(pOrderBy->a[i].pExpr);
113602       if( pOBExpr->op!=TK_COLUMN ) continue;
113603       if( pOBExpr->iTable!=iCur ) continue;
113604       pTerm = findTerm(&pWInfo->sWC, iCur, pOBExpr->iColumn,
113605                        ~ready, WO_EQ|WO_ISNULL, 0);
113606       if( pTerm==0 ) continue;
113607       if( (pTerm->eOperator&WO_EQ)!=0 && pOBExpr->iColumn>=0 ){
113608         const char *z1, *z2;
113609         pColl = sqlite3ExprCollSeq(pWInfo->pParse, pOrderBy->a[i].pExpr);
113610         if( !pColl ) pColl = db->pDfltColl;
113611         z1 = pColl->zName;
113612         pColl = sqlite3ExprCollSeq(pWInfo->pParse, pTerm->pExpr);
113613         if( !pColl ) pColl = db->pDfltColl;
113614         z2 = pColl->zName;
113615         if( sqlite3StrICmp(z1, z2)!=0 ) continue;
113616       }
113617       obSat |= MASKBIT(i);
113618     }
113619 
113620     if( (pLoop->wsFlags & WHERE_ONEROW)==0 ){
113621       if( pLoop->wsFlags & WHERE_IPK ){
113622         pIndex = 0;
113623         nKeyCol = 0;
113624         nColumn = 1;
113625       }else if( (pIndex = pLoop->u.btree.pIndex)==0 || pIndex->bUnordered ){
113626         return 0;
113627       }else{
113628         nKeyCol = pIndex->nKeyCol;
113629         nColumn = pIndex->nColumn;
113630         assert( nColumn==nKeyCol+1 || !HasRowid(pIndex->pTable) );
113631         assert( pIndex->aiColumn[nColumn-1]==(-1) || !HasRowid(pIndex->pTable));
113632         isOrderDistinct = pIndex->onError!=OE_None;
113633       }
113634 
113635       /* Loop through all columns of the index and deal with the ones
113636       ** that are not constrained by == or IN.
113637       */
113638       rev = revSet = 0;
113639       distinctColumns = 0;
113640       for(j=0; j<nColumn; j++){
113641         u8 bOnce;   /* True to run the ORDER BY search loop */
113642 
113643         /* Skip over == and IS NULL terms */
113644         if( j<pLoop->u.btree.nEq
113645          && pLoop->u.btree.nSkip==0
113646          && ((i = pLoop->aLTerm[j]->eOperator) & (WO_EQ|WO_ISNULL))!=0
113647         ){
113648           if( i & WO_ISNULL ){
113649             testcase( isOrderDistinct );
113650             isOrderDistinct = 0;
113651           }
113652           continue;
113653         }
113654 
113655         /* Get the column number in the table (iColumn) and sort order
113656         ** (revIdx) for the j-th column of the index.
113657         */
113658         if( pIndex ){
113659           iColumn = pIndex->aiColumn[j];
113660           revIdx = pIndex->aSortOrder[j];
113661           if( iColumn==pIndex->pTable->iPKey ) iColumn = -1;
113662         }else{
113663           iColumn = -1;
113664           revIdx = 0;
113665         }
113666 
113667         /* An unconstrained column that might be NULL means that this
113668         ** WhereLoop is not well-ordered
113669         */
113670         if( isOrderDistinct
113671          && iColumn>=0
113672          && j>=pLoop->u.btree.nEq
113673          && pIndex->pTable->aCol[iColumn].notNull==0
113674         ){
113675           isOrderDistinct = 0;
113676         }
113677 
113678         /* Find the ORDER BY term that corresponds to the j-th column
113679         ** of the index and and mark that ORDER BY term off
113680         */
113681         bOnce = 1;
113682         isMatch = 0;
113683         for(i=0; bOnce && i<nOrderBy; i++){
113684           if( MASKBIT(i) & obSat ) continue;
113685           pOBExpr = sqlite3ExprSkipCollate(pOrderBy->a[i].pExpr);
113686           testcase( wctrlFlags & WHERE_GROUPBY );
113687           testcase( wctrlFlags & WHERE_DISTINCTBY );
113688           if( (wctrlFlags & (WHERE_GROUPBY|WHERE_DISTINCTBY))==0 ) bOnce = 0;
113689           if( pOBExpr->op!=TK_COLUMN ) continue;
113690           if( pOBExpr->iTable!=iCur ) continue;
113691           if( pOBExpr->iColumn!=iColumn ) continue;
113692           if( iColumn>=0 ){
113693             pColl = sqlite3ExprCollSeq(pWInfo->pParse, pOrderBy->a[i].pExpr);
113694             if( !pColl ) pColl = db->pDfltColl;
113695             if( sqlite3StrICmp(pColl->zName, pIndex->azColl[j])!=0 ) continue;
113696           }
113697           isMatch = 1;
113698           break;
113699         }
113700         if( isMatch ){
113701           if( iColumn<0 ){
113702             testcase( distinctColumns==0 );
113703             distinctColumns = 1;
113704           }
113705           obSat |= MASKBIT(i);
113706           if( (pWInfo->wctrlFlags & WHERE_GROUPBY)==0 ){
113707             /* Make sure the sort order is compatible in an ORDER BY clause.
113708             ** Sort order is irrelevant for a GROUP BY clause. */
113709             if( revSet ){
113710               if( (rev ^ revIdx)!=pOrderBy->a[i].sortOrder ) return 0;
113711             }else{
113712               rev = revIdx ^ pOrderBy->a[i].sortOrder;
113713               if( rev ) *pRevMask |= MASKBIT(iLoop);
113714               revSet = 1;
113715             }
113716           }
113717         }else{
113718           /* No match found */
113719           if( j==0 || j<nKeyCol ){
113720             testcase( isOrderDistinct!=0 );
113721             isOrderDistinct = 0;
113722           }
113723           break;
113724         }
113725       } /* end Loop over all index columns */
113726       if( distinctColumns ){
113727         testcase( isOrderDistinct==0 );
113728         isOrderDistinct = 1;
113729       }
113730     } /* end-if not one-row */
113731 
113732     /* Mark off any other ORDER BY terms that reference pLoop */
113733     if( isOrderDistinct ){
113734       orderDistinctMask |= pLoop->maskSelf;
113735       for(i=0; i<nOrderBy; i++){
113736         Expr *p;
113737         if( MASKBIT(i) & obSat ) continue;
113738         p = pOrderBy->a[i].pExpr;
113739         if( (exprTableUsage(&pWInfo->sMaskSet, p)&~orderDistinctMask)==0 ){
113740           obSat |= MASKBIT(i);
113741         }
113742       }
113743     }
113744   } /* End the loop over all WhereLoops from outer-most down to inner-most */
113745   if( obSat==obDone ) return 1;
113746   if( !isOrderDistinct ) return 0;
113747   return -1;
113748 }
113749 
113750 #ifdef WHERETRACE_ENABLED
113751 /* For debugging use only: */
113752 static const char *wherePathName(WherePath *pPath, int nLoop, WhereLoop *pLast){
113753   static char zName[65];
113754   int i;
113755   for(i=0; i<nLoop; i++){ zName[i] = pPath->aLoop[i]->cId; }
113756   if( pLast ) zName[i++] = pLast->cId;
113757   zName[i] = 0;
113758   return zName;
113759 }
113760 #endif
113761 
113762 
113763 /*
113764 ** Given the list of WhereLoop objects at pWInfo->pLoops, this routine
113765 ** attempts to find the lowest cost path that visits each WhereLoop
113766 ** once.  This path is then loaded into the pWInfo->a[].pWLoop fields.
113767 **
113768 ** Assume that the total number of output rows that will need to be sorted
113769 ** will be nRowEst (in the 10*log2 representation).  Or, ignore sorting
113770 ** costs if nRowEst==0.
113771 **
113772 ** Return SQLITE_OK on success or SQLITE_NOMEM of a memory allocation
113773 ** error occurs.
113774 */
113775 static int wherePathSolver(WhereInfo *pWInfo, LogEst nRowEst){
113776   int mxChoice;             /* Maximum number of simultaneous paths tracked */
113777   int nLoop;                /* Number of terms in the join */
113778   Parse *pParse;            /* Parsing context */
113779   sqlite3 *db;              /* The database connection */
113780   int iLoop;                /* Loop counter over the terms of the join */
113781   int ii, jj;               /* Loop counters */
113782   int mxI = 0;              /* Index of next entry to replace */
113783   LogEst rCost;             /* Cost of a path */
113784   LogEst nOut;              /* Number of outputs */
113785   LogEst mxCost = 0;        /* Maximum cost of a set of paths */
113786   LogEst mxOut = 0;         /* Maximum nOut value on the set of paths */
113787   LogEst rSortCost;         /* Cost to do a sort */
113788   int nTo, nFrom;           /* Number of valid entries in aTo[] and aFrom[] */
113789   WherePath *aFrom;         /* All nFrom paths at the previous level */
113790   WherePath *aTo;           /* The nTo best paths at the current level */
113791   WherePath *pFrom;         /* An element of aFrom[] that we are working on */
113792   WherePath *pTo;           /* An element of aTo[] that we are working on */
113793   WhereLoop *pWLoop;        /* One of the WhereLoop objects */
113794   WhereLoop **pX;           /* Used to divy up the pSpace memory */
113795   char *pSpace;             /* Temporary memory used by this routine */
113796 
113797   pParse = pWInfo->pParse;
113798   db = pParse->db;
113799   nLoop = pWInfo->nLevel;
113800   /* TUNING: For simple queries, only the best path is tracked.
113801   ** For 2-way joins, the 5 best paths are followed.
113802   ** For joins of 3 or more tables, track the 10 best paths */
113803   mxChoice = (nLoop==1) ? 1 : (nLoop==2 ? 5 : 10);
113804   assert( nLoop<=pWInfo->pTabList->nSrc );
113805   WHERETRACE(0x002, ("---- begin solver\n"));
113806 
113807   /* Allocate and initialize space for aTo and aFrom */
113808   ii = (sizeof(WherePath)+sizeof(WhereLoop*)*nLoop)*mxChoice*2;
113809   pSpace = sqlite3DbMallocRaw(db, ii);
113810   if( pSpace==0 ) return SQLITE_NOMEM;
113811   aTo = (WherePath*)pSpace;
113812   aFrom = aTo+mxChoice;
113813   memset(aFrom, 0, sizeof(aFrom[0]));
113814   pX = (WhereLoop**)(aFrom+mxChoice);
113815   for(ii=mxChoice*2, pFrom=aTo; ii>0; ii--, pFrom++, pX += nLoop){
113816     pFrom->aLoop = pX;
113817   }
113818 
113819   /* Seed the search with a single WherePath containing zero WhereLoops.
113820   **
113821   ** TUNING: Do not let the number of iterations go above 25.  If the cost
113822   ** of computing an automatic index is not paid back within the first 25
113823   ** rows, then do not use the automatic index. */
113824   aFrom[0].nRow = MIN(pParse->nQueryLoop, 46);  assert( 46==sqlite3LogEst(25) );
113825   nFrom = 1;
113826 
113827   /* Precompute the cost of sorting the final result set, if the caller
113828   ** to sqlite3WhereBegin() was concerned about sorting */
113829   rSortCost = 0;
113830   if( pWInfo->pOrderBy==0 || nRowEst==0 ){
113831     aFrom[0].isOrderedValid = 1;
113832   }else{
113833     /* TUNING: Estimated cost of sorting is 48*N*log2(N) where N is the
113834     ** number of output rows. The 48 is the expected size of a row to sort.
113835     ** FIXME:  compute a better estimate of the 48 multiplier based on the
113836     ** result set expressions. */
113837     rSortCost = nRowEst + estLog(nRowEst);
113838     WHERETRACE(0x002,("---- sort cost=%-3d\n", rSortCost));
113839   }
113840 
113841   /* Compute successively longer WherePaths using the previous generation
113842   ** of WherePaths as the basis for the next.  Keep track of the mxChoice
113843   ** best paths at each generation */
113844   for(iLoop=0; iLoop<nLoop; iLoop++){
113845     nTo = 0;
113846     for(ii=0, pFrom=aFrom; ii<nFrom; ii++, pFrom++){
113847       for(pWLoop=pWInfo->pLoops; pWLoop; pWLoop=pWLoop->pNextLoop){
113848         Bitmask maskNew;
113849         Bitmask revMask = 0;
113850         u8 isOrderedValid = pFrom->isOrderedValid;
113851         u8 isOrdered = pFrom->isOrdered;
113852         if( (pWLoop->prereq & ~pFrom->maskLoop)!=0 ) continue;
113853         if( (pWLoop->maskSelf & pFrom->maskLoop)!=0 ) continue;
113854         /* At this point, pWLoop is a candidate to be the next loop.
113855         ** Compute its cost */
113856         rCost = sqlite3LogEstAdd(pWLoop->rSetup,pWLoop->rRun + pFrom->nRow);
113857         rCost = sqlite3LogEstAdd(rCost, pFrom->rCost);
113858         nOut = pFrom->nRow + pWLoop->nOut;
113859         maskNew = pFrom->maskLoop | pWLoop->maskSelf;
113860         if( !isOrderedValid ){
113861           switch( wherePathSatisfiesOrderBy(pWInfo,
113862                        pWInfo->pOrderBy, pFrom, pWInfo->wctrlFlags,
113863                        iLoop, pWLoop, &revMask) ){
113864             case 1:  /* Yes.  pFrom+pWLoop does satisfy the ORDER BY clause */
113865               isOrdered = 1;
113866               isOrderedValid = 1;
113867               break;
113868             case 0:  /* No.  pFrom+pWLoop will require a separate sort */
113869               isOrdered = 0;
113870               isOrderedValid = 1;
113871               rCost = sqlite3LogEstAdd(rCost, rSortCost);
113872               break;
113873             default: /* Cannot tell yet.  Try again on the next iteration */
113874               break;
113875           }
113876         }else{
113877           revMask = pFrom->revLoop;
113878         }
113879         /* Check to see if pWLoop should be added to the mxChoice best so far */
113880         for(jj=0, pTo=aTo; jj<nTo; jj++, pTo++){
113881           if( pTo->maskLoop==maskNew
113882            && pTo->isOrderedValid==isOrderedValid
113883            && ((pTo->rCost<=rCost && pTo->nRow<=nOut) ||
113884                 (pTo->rCost>=rCost && pTo->nRow>=nOut))
113885           ){
113886             testcase( jj==nTo-1 );
113887             break;
113888           }
113889         }
113890         if( jj>=nTo ){
113891           if( nTo>=mxChoice && rCost>=mxCost ){
113892 #ifdef WHERETRACE_ENABLED /* 0x4 */
113893             if( sqlite3WhereTrace&0x4 ){
113894               sqlite3DebugPrintf("Skip   %s cost=%-3d,%3d order=%c\n",
113895                   wherePathName(pFrom, iLoop, pWLoop), rCost, nOut,
113896                   isOrderedValid ? (isOrdered ? 'Y' : 'N') : '?');
113897             }
113898 #endif
113899             continue;
113900           }
113901           /* Add a new Path to the aTo[] set */
113902           if( nTo<mxChoice ){
113903             /* Increase the size of the aTo set by one */
113904             jj = nTo++;
113905           }else{
113906             /* New path replaces the prior worst to keep count below mxChoice */
113907             jj = mxI;
113908           }
113909           pTo = &aTo[jj];
113910 #ifdef WHERETRACE_ENABLED /* 0x4 */
113911           if( sqlite3WhereTrace&0x4 ){
113912             sqlite3DebugPrintf("New    %s cost=%-3d,%3d order=%c\n",
113913                 wherePathName(pFrom, iLoop, pWLoop), rCost, nOut,
113914                 isOrderedValid ? (isOrdered ? 'Y' : 'N') : '?');
113915           }
113916 #endif
113917         }else{
113918           if( pTo->rCost<=rCost && pTo->nRow<=nOut ){
113919 #ifdef WHERETRACE_ENABLED /* 0x4 */
113920             if( sqlite3WhereTrace&0x4 ){
113921               sqlite3DebugPrintf(
113922                   "Skip   %s cost=%-3d,%3d order=%c",
113923                   wherePathName(pFrom, iLoop, pWLoop), rCost, nOut,
113924                   isOrderedValid ? (isOrdered ? 'Y' : 'N') : '?');
113925               sqlite3DebugPrintf("   vs %s cost=%-3d,%d order=%c\n",
113926                   wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow,
113927                   pTo->isOrderedValid ? (pTo->isOrdered ? 'Y' : 'N') : '?');
113928             }
113929 #endif
113930             testcase( pTo->rCost==rCost );
113931             continue;
113932           }
113933           testcase( pTo->rCost==rCost+1 );
113934           /* A new and better score for a previously created equivalent path */
113935 #ifdef WHERETRACE_ENABLED /* 0x4 */
113936           if( sqlite3WhereTrace&0x4 ){
113937             sqlite3DebugPrintf(
113938                 "Update %s cost=%-3d,%3d order=%c",
113939                 wherePathName(pFrom, iLoop, pWLoop), rCost, nOut,
113940                 isOrderedValid ? (isOrdered ? 'Y' : 'N') : '?');
113941             sqlite3DebugPrintf("  was %s cost=%-3d,%3d order=%c\n",
113942                 wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow,
113943                 pTo->isOrderedValid ? (pTo->isOrdered ? 'Y' : 'N') : '?');
113944           }
113945 #endif
113946         }
113947         /* pWLoop is a winner.  Add it to the set of best so far */
113948         pTo->maskLoop = pFrom->maskLoop | pWLoop->maskSelf;
113949         pTo->revLoop = revMask;
113950         pTo->nRow = nOut;
113951         pTo->rCost = rCost;
113952         pTo->isOrderedValid = isOrderedValid;
113953         pTo->isOrdered = isOrdered;
113954         memcpy(pTo->aLoop, pFrom->aLoop, sizeof(WhereLoop*)*iLoop);
113955         pTo->aLoop[iLoop] = pWLoop;
113956         if( nTo>=mxChoice ){
113957           mxI = 0;
113958           mxCost = aTo[0].rCost;
113959           mxOut = aTo[0].nRow;
113960           for(jj=1, pTo=&aTo[1]; jj<mxChoice; jj++, pTo++){
113961             if( pTo->rCost>mxCost || (pTo->rCost==mxCost && pTo->nRow>mxOut) ){
113962               mxCost = pTo->rCost;
113963               mxOut = pTo->nRow;
113964               mxI = jj;
113965             }
113966           }
113967         }
113968       }
113969     }
113970 
113971 #ifdef WHERETRACE_ENABLED  /* >=2 */
113972     if( sqlite3WhereTrace>=2 ){
113973       sqlite3DebugPrintf("---- after round %d ----\n", iLoop);
113974       for(ii=0, pTo=aTo; ii<nTo; ii++, pTo++){
113975         sqlite3DebugPrintf(" %s cost=%-3d nrow=%-3d order=%c",
113976            wherePathName(pTo, iLoop+1, 0), pTo->rCost, pTo->nRow,
113977            pTo->isOrderedValid ? (pTo->isOrdered ? 'Y' : 'N') : '?');
113978         if( pTo->isOrderedValid && pTo->isOrdered ){
113979           sqlite3DebugPrintf(" rev=0x%llx\n", pTo->revLoop);
113980         }else{
113981           sqlite3DebugPrintf("\n");
113982         }
113983       }
113984     }
113985 #endif
113986 
113987     /* Swap the roles of aFrom and aTo for the next generation */
113988     pFrom = aTo;
113989     aTo = aFrom;
113990     aFrom = pFrom;
113991     nFrom = nTo;
113992   }
113993 
113994   if( nFrom==0 ){
113995     sqlite3ErrorMsg(pParse, "no query solution");
113996     sqlite3DbFree(db, pSpace);
113997     return SQLITE_ERROR;
113998   }
113999 
114000   /* Find the lowest cost path.  pFrom will be left pointing to that path */
114001   pFrom = aFrom;
114002   for(ii=1; ii<nFrom; ii++){
114003     if( pFrom->rCost>aFrom[ii].rCost ) pFrom = &aFrom[ii];
114004   }
114005   assert( pWInfo->nLevel==nLoop );
114006   /* Load the lowest cost path into pWInfo */
114007   for(iLoop=0; iLoop<nLoop; iLoop++){
114008     WhereLevel *pLevel = pWInfo->a + iLoop;
114009     pLevel->pWLoop = pWLoop = pFrom->aLoop[iLoop];
114010     pLevel->iFrom = pWLoop->iTab;
114011     pLevel->iTabCur = pWInfo->pTabList->a[pLevel->iFrom].iCursor;
114012   }
114013   if( (pWInfo->wctrlFlags & WHERE_WANT_DISTINCT)!=0
114014    && (pWInfo->wctrlFlags & WHERE_DISTINCTBY)==0
114015    && pWInfo->eDistinct==WHERE_DISTINCT_NOOP
114016    && nRowEst
114017   ){
114018     Bitmask notUsed;
114019     int rc = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pResultSet, pFrom,
114020                  WHERE_DISTINCTBY, nLoop-1, pFrom->aLoop[nLoop-1], &notUsed);
114021     if( rc==1 ) pWInfo->eDistinct = WHERE_DISTINCT_ORDERED;
114022   }
114023   if( pFrom->isOrdered ){
114024     if( pWInfo->wctrlFlags & WHERE_DISTINCTBY ){
114025       pWInfo->eDistinct = WHERE_DISTINCT_ORDERED;
114026     }else{
114027       pWInfo->bOBSat = 1;
114028       pWInfo->revMask = pFrom->revLoop;
114029     }
114030   }
114031   pWInfo->nRowOut = pFrom->nRow;
114032 
114033   /* Free temporary memory and return success */
114034   sqlite3DbFree(db, pSpace);
114035   return SQLITE_OK;
114036 }
114037 
114038 /*
114039 ** Most queries use only a single table (they are not joins) and have
114040 ** simple == constraints against indexed fields.  This routine attempts
114041 ** to plan those simple cases using much less ceremony than the
114042 ** general-purpose query planner, and thereby yield faster sqlite3_prepare()
114043 ** times for the common case.
114044 **
114045 ** Return non-zero on success, if this query can be handled by this
114046 ** no-frills query planner.  Return zero if this query needs the
114047 ** general-purpose query planner.
114048 */
114049 static int whereShortCut(WhereLoopBuilder *pBuilder){
114050   WhereInfo *pWInfo;
114051   struct SrcList_item *pItem;
114052   WhereClause *pWC;
114053   WhereTerm *pTerm;
114054   WhereLoop *pLoop;
114055   int iCur;
114056   int j;
114057   Table *pTab;
114058   Index *pIdx;
114059 
114060   pWInfo = pBuilder->pWInfo;
114061   if( pWInfo->wctrlFlags & WHERE_FORCE_TABLE ) return 0;
114062   assert( pWInfo->pTabList->nSrc>=1 );
114063   pItem = pWInfo->pTabList->a;
114064   pTab = pItem->pTab;
114065   if( IsVirtual(pTab) ) return 0;
114066   if( pItem->zIndex ) return 0;
114067   iCur = pItem->iCursor;
114068   pWC = &pWInfo->sWC;
114069   pLoop = pBuilder->pNew;
114070   pLoop->wsFlags = 0;
114071   pLoop->u.btree.nSkip = 0;
114072   pTerm = findTerm(pWC, iCur, -1, 0, WO_EQ, 0);
114073   if( pTerm ){
114074     pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_IPK|WHERE_ONEROW;
114075     pLoop->aLTerm[0] = pTerm;
114076     pLoop->nLTerm = 1;
114077     pLoop->u.btree.nEq = 1;
114078     /* TUNING: Cost of a rowid lookup is 10 */
114079     pLoop->rRun = 33;  /* 33==sqlite3LogEst(10) */
114080   }else{
114081     for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
114082       assert( pLoop->aLTermSpace==pLoop->aLTerm );
114083       assert( ArraySize(pLoop->aLTermSpace)==4 );
114084       if( pIdx->onError==OE_None
114085        || pIdx->pPartIdxWhere!=0
114086        || pIdx->nKeyCol>ArraySize(pLoop->aLTermSpace)
114087       ) continue;
114088       for(j=0; j<pIdx->nKeyCol; j++){
114089         pTerm = findTerm(pWC, iCur, pIdx->aiColumn[j], 0, WO_EQ, pIdx);
114090         if( pTerm==0 ) break;
114091         pLoop->aLTerm[j] = pTerm;
114092       }
114093       if( j!=pIdx->nKeyCol ) continue;
114094       pLoop->wsFlags = WHERE_COLUMN_EQ|WHERE_ONEROW|WHERE_INDEXED;
114095       if( pIdx->isCovering || (pItem->colUsed & ~columnsInIndex(pIdx))==0 ){
114096         pLoop->wsFlags |= WHERE_IDX_ONLY;
114097       }
114098       pLoop->nLTerm = j;
114099       pLoop->u.btree.nEq = j;
114100       pLoop->u.btree.pIndex = pIdx;
114101       /* TUNING: Cost of a unique index lookup is 15 */
114102       pLoop->rRun = 39;  /* 39==sqlite3LogEst(15) */
114103       break;
114104     }
114105   }
114106   if( pLoop->wsFlags ){
114107     pLoop->nOut = (LogEst)1;
114108     pWInfo->a[0].pWLoop = pLoop;
114109     pLoop->maskSelf = getMask(&pWInfo->sMaskSet, iCur);
114110     pWInfo->a[0].iTabCur = iCur;
114111     pWInfo->nRowOut = 1;
114112     if( pWInfo->pOrderBy ) pWInfo->bOBSat =  1;
114113     if( pWInfo->wctrlFlags & WHERE_WANT_DISTINCT ){
114114       pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE;
114115     }
114116 #ifdef SQLITE_DEBUG
114117     pLoop->cId = '0';
114118 #endif
114119     return 1;
114120   }
114121   return 0;
114122 }
114123 
114124 /*
114125 ** Generate the beginning of the loop used for WHERE clause processing.
114126 ** The return value is a pointer to an opaque structure that contains
114127 ** information needed to terminate the loop.  Later, the calling routine
114128 ** should invoke sqlite3WhereEnd() with the return value of this function
114129 ** in order to complete the WHERE clause processing.
114130 **
114131 ** If an error occurs, this routine returns NULL.
114132 **
114133 ** The basic idea is to do a nested loop, one loop for each table in
114134 ** the FROM clause of a select.  (INSERT and UPDATE statements are the
114135 ** same as a SELECT with only a single table in the FROM clause.)  For
114136 ** example, if the SQL is this:
114137 **
114138 **       SELECT * FROM t1, t2, t3 WHERE ...;
114139 **
114140 ** Then the code generated is conceptually like the following:
114141 **
114142 **      foreach row1 in t1 do       \    Code generated
114143 **        foreach row2 in t2 do      |-- by sqlite3WhereBegin()
114144 **          foreach row3 in t3 do   /
114145 **            ...
114146 **          end                     \    Code generated
114147 **        end                        |-- by sqlite3WhereEnd()
114148 **      end                         /
114149 **
114150 ** Note that the loops might not be nested in the order in which they
114151 ** appear in the FROM clause if a different order is better able to make
114152 ** use of indices.  Note also that when the IN operator appears in
114153 ** the WHERE clause, it might result in additional nested loops for
114154 ** scanning through all values on the right-hand side of the IN.
114155 **
114156 ** There are Btree cursors associated with each table.  t1 uses cursor
114157 ** number pTabList->a[0].iCursor.  t2 uses the cursor pTabList->a[1].iCursor.
114158 ** And so forth.  This routine generates code to open those VDBE cursors
114159 ** and sqlite3WhereEnd() generates the code to close them.
114160 **
114161 ** The code that sqlite3WhereBegin() generates leaves the cursors named
114162 ** in pTabList pointing at their appropriate entries.  The [...] code
114163 ** can use OP_Column and OP_Rowid opcodes on these cursors to extract
114164 ** data from the various tables of the loop.
114165 **
114166 ** If the WHERE clause is empty, the foreach loops must each scan their
114167 ** entire tables.  Thus a three-way join is an O(N^3) operation.  But if
114168 ** the tables have indices and there are terms in the WHERE clause that
114169 ** refer to those indices, a complete table scan can be avoided and the
114170 ** code will run much faster.  Most of the work of this routine is checking
114171 ** to see if there are indices that can be used to speed up the loop.
114172 **
114173 ** Terms of the WHERE clause are also used to limit which rows actually
114174 ** make it to the "..." in the middle of the loop.  After each "foreach",
114175 ** terms of the WHERE clause that use only terms in that loop and outer
114176 ** loops are evaluated and if false a jump is made around all subsequent
114177 ** inner loops (or around the "..." if the test occurs within the inner-
114178 ** most loop)
114179 **
114180 ** OUTER JOINS
114181 **
114182 ** An outer join of tables t1 and t2 is conceptally coded as follows:
114183 **
114184 **    foreach row1 in t1 do
114185 **      flag = 0
114186 **      foreach row2 in t2 do
114187 **        start:
114188 **          ...
114189 **          flag = 1
114190 **      end
114191 **      if flag==0 then
114192 **        move the row2 cursor to a null row
114193 **        goto start
114194 **      fi
114195 **    end
114196 **
114197 ** ORDER BY CLAUSE PROCESSING
114198 **
114199 ** pOrderBy is a pointer to the ORDER BY clause (or the GROUP BY clause
114200 ** if the WHERE_GROUPBY flag is set in wctrlFlags) of a SELECT statement
114201 ** if there is one.  If there is no ORDER BY clause or if this routine
114202 ** is called from an UPDATE or DELETE statement, then pOrderBy is NULL.
114203 **
114204 ** The iIdxCur parameter is the cursor number of an index.  If
114205 ** WHERE_ONETABLE_ONLY is set, iIdxCur is the cursor number of an index
114206 ** to use for OR clause processing.  The WHERE clause should use this
114207 ** specific cursor.  If WHERE_ONEPASS_DESIRED is set, then iIdxCur is
114208 ** the first cursor in an array of cursors for all indices.  iIdxCur should
114209 ** be used to compute the appropriate cursor depending on which index is
114210 ** used.
114211 */
114212 SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(
114213   Parse *pParse,        /* The parser context */
114214   SrcList *pTabList,    /* FROM clause: A list of all tables to be scanned */
114215   Expr *pWhere,         /* The WHERE clause */
114216   ExprList *pOrderBy,   /* An ORDER BY clause, or NULL */
114217   ExprList *pResultSet, /* Result set of the query */
114218   u16 wctrlFlags,       /* One of the WHERE_* flags defined in sqliteInt.h */
114219   int iIdxCur           /* If WHERE_ONETABLE_ONLY is set, index cursor number */
114220 ){
114221   int nByteWInfo;            /* Num. bytes allocated for WhereInfo struct */
114222   int nTabList;              /* Number of elements in pTabList */
114223   WhereInfo *pWInfo;         /* Will become the return value of this function */
114224   Vdbe *v = pParse->pVdbe;   /* The virtual database engine */
114225   Bitmask notReady;          /* Cursors that are not yet positioned */
114226   WhereLoopBuilder sWLB;     /* The WhereLoop builder */
114227   WhereMaskSet *pMaskSet;    /* The expression mask set */
114228   WhereLevel *pLevel;        /* A single level in pWInfo->a[] */
114229   WhereLoop *pLoop;          /* Pointer to a single WhereLoop object */
114230   int ii;                    /* Loop counter */
114231   sqlite3 *db;               /* Database connection */
114232   int rc;                    /* Return code */
114233 
114234 
114235   /* Variable initialization */
114236   db = pParse->db;
114237   memset(&sWLB, 0, sizeof(sWLB));
114238   sWLB.pOrderBy = pOrderBy;
114239 
114240   /* Disable the DISTINCT optimization if SQLITE_DistinctOpt is set via
114241   ** sqlite3_test_ctrl(SQLITE_TESTCTRL_OPTIMIZATIONS,...) */
114242   if( OptimizationDisabled(db, SQLITE_DistinctOpt) ){
114243     wctrlFlags &= ~WHERE_WANT_DISTINCT;
114244   }
114245 
114246   /* The number of tables in the FROM clause is limited by the number of
114247   ** bits in a Bitmask
114248   */
114249   testcase( pTabList->nSrc==BMS );
114250   if( pTabList->nSrc>BMS ){
114251     sqlite3ErrorMsg(pParse, "at most %d tables in a join", BMS);
114252     return 0;
114253   }
114254 
114255   /* This function normally generates a nested loop for all tables in
114256   ** pTabList.  But if the WHERE_ONETABLE_ONLY flag is set, then we should
114257   ** only generate code for the first table in pTabList and assume that
114258   ** any cursors associated with subsequent tables are uninitialized.
114259   */
114260   nTabList = (wctrlFlags & WHERE_ONETABLE_ONLY) ? 1 : pTabList->nSrc;
114261 
114262   /* Allocate and initialize the WhereInfo structure that will become the
114263   ** return value. A single allocation is used to store the WhereInfo
114264   ** struct, the contents of WhereInfo.a[], the WhereClause structure
114265   ** and the WhereMaskSet structure. Since WhereClause contains an 8-byte
114266   ** field (type Bitmask) it must be aligned on an 8-byte boundary on
114267   ** some architectures. Hence the ROUND8() below.
114268   */
114269   nByteWInfo = ROUND8(sizeof(WhereInfo)+(nTabList-1)*sizeof(WhereLevel));
114270   pWInfo = sqlite3DbMallocZero(db, nByteWInfo + sizeof(WhereLoop));
114271   if( db->mallocFailed ){
114272     sqlite3DbFree(db, pWInfo);
114273     pWInfo = 0;
114274     goto whereBeginError;
114275   }
114276   pWInfo->aiCurOnePass[0] = pWInfo->aiCurOnePass[1] = -1;
114277   pWInfo->nLevel = nTabList;
114278   pWInfo->pParse = pParse;
114279   pWInfo->pTabList = pTabList;
114280   pWInfo->pOrderBy = pOrderBy;
114281   pWInfo->pResultSet = pResultSet;
114282   pWInfo->iBreak = sqlite3VdbeMakeLabel(v);
114283   pWInfo->wctrlFlags = wctrlFlags;
114284   pWInfo->savedNQueryLoop = pParse->nQueryLoop;
114285   pMaskSet = &pWInfo->sMaskSet;
114286   sWLB.pWInfo = pWInfo;
114287   sWLB.pWC = &pWInfo->sWC;
114288   sWLB.pNew = (WhereLoop*)(((char*)pWInfo)+nByteWInfo);
114289   assert( EIGHT_BYTE_ALIGNMENT(sWLB.pNew) );
114290   whereLoopInit(sWLB.pNew);
114291 #ifdef SQLITE_DEBUG
114292   sWLB.pNew->cId = '*';
114293 #endif
114294 
114295   /* Split the WHERE clause into separate subexpressions where each
114296   ** subexpression is separated by an AND operator.
114297   */
114298   initMaskSet(pMaskSet);
114299   whereClauseInit(&pWInfo->sWC, pWInfo);
114300   whereSplit(&pWInfo->sWC, pWhere, TK_AND);
114301   sqlite3CodeVerifySchema(pParse, -1); /* Insert the cookie verifier Goto */
114302 
114303   /* Special case: a WHERE clause that is constant.  Evaluate the
114304   ** expression and either jump over all of the code or fall thru.
114305   */
114306   for(ii=0; ii<sWLB.pWC->nTerm; ii++){
114307     if( nTabList==0 || sqlite3ExprIsConstantNotJoin(sWLB.pWC->a[ii].pExpr) ){
114308       sqlite3ExprIfFalse(pParse, sWLB.pWC->a[ii].pExpr, pWInfo->iBreak,
114309                          SQLITE_JUMPIFNULL);
114310       sWLB.pWC->a[ii].wtFlags |= TERM_CODED;
114311     }
114312   }
114313 
114314   /* Special case: No FROM clause
114315   */
114316   if( nTabList==0 ){
114317     if( pOrderBy ) pWInfo->bOBSat = 1;
114318     if( wctrlFlags & WHERE_WANT_DISTINCT ){
114319       pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE;
114320     }
114321   }
114322 
114323   /* Assign a bit from the bitmask to every term in the FROM clause.
114324   **
114325   ** When assigning bitmask values to FROM clause cursors, it must be
114326   ** the case that if X is the bitmask for the N-th FROM clause term then
114327   ** the bitmask for all FROM clause terms to the left of the N-th term
114328   ** is (X-1).   An expression from the ON clause of a LEFT JOIN can use
114329   ** its Expr.iRightJoinTable value to find the bitmask of the right table
114330   ** of the join.  Subtracting one from the right table bitmask gives a
114331   ** bitmask for all tables to the left of the join.  Knowing the bitmask
114332   ** for all tables to the left of a left join is important.  Ticket #3015.
114333   **
114334   ** Note that bitmasks are created for all pTabList->nSrc tables in
114335   ** pTabList, not just the first nTabList tables.  nTabList is normally
114336   ** equal to pTabList->nSrc but might be shortened to 1 if the
114337   ** WHERE_ONETABLE_ONLY flag is set.
114338   */
114339   for(ii=0; ii<pTabList->nSrc; ii++){
114340     createMask(pMaskSet, pTabList->a[ii].iCursor);
114341   }
114342 #ifndef NDEBUG
114343   {
114344     Bitmask toTheLeft = 0;
114345     for(ii=0; ii<pTabList->nSrc; ii++){
114346       Bitmask m = getMask(pMaskSet, pTabList->a[ii].iCursor);
114347       assert( (m-1)==toTheLeft );
114348       toTheLeft |= m;
114349     }
114350   }
114351 #endif
114352 
114353   /* Analyze all of the subexpressions.  Note that exprAnalyze() might
114354   ** add new virtual terms onto the end of the WHERE clause.  We do not
114355   ** want to analyze these virtual terms, so start analyzing at the end
114356   ** and work forward so that the added virtual terms are never processed.
114357   */
114358   exprAnalyzeAll(pTabList, &pWInfo->sWC);
114359   if( db->mallocFailed ){
114360     goto whereBeginError;
114361   }
114362 
114363   /* If the ORDER BY (or GROUP BY) clause contains references to general
114364   ** expressions, then we won't be able to satisfy it using indices, so
114365   ** go ahead and disable it now.
114366   */
114367   if( pOrderBy && (wctrlFlags & WHERE_WANT_DISTINCT)!=0 ){
114368     for(ii=0; ii<pOrderBy->nExpr; ii++){
114369       Expr *pExpr = sqlite3ExprSkipCollate(pOrderBy->a[ii].pExpr);
114370       if( pExpr->op!=TK_COLUMN ){
114371         pWInfo->pOrderBy = pOrderBy = 0;
114372         break;
114373       }else if( pExpr->iColumn<0 ){
114374         break;
114375       }
114376     }
114377   }
114378 
114379   if( wctrlFlags & WHERE_WANT_DISTINCT ){
114380     if( isDistinctRedundant(pParse, pTabList, &pWInfo->sWC, pResultSet) ){
114381       /* The DISTINCT marking is pointless.  Ignore it. */
114382       pWInfo->eDistinct = WHERE_DISTINCT_UNIQUE;
114383     }else if( pOrderBy==0 ){
114384       /* Try to ORDER BY the result set to make distinct processing easier */
114385       pWInfo->wctrlFlags |= WHERE_DISTINCTBY;
114386       pWInfo->pOrderBy = pResultSet;
114387     }
114388   }
114389 
114390   /* Construct the WhereLoop objects */
114391   WHERETRACE(0xffff,("*** Optimizer Start ***\n"));
114392   /* Display all terms of the WHERE clause */
114393 #if defined(WHERETRACE_ENABLED) && defined(SQLITE_ENABLE_TREE_EXPLAIN)
114394   if( sqlite3WhereTrace & 0x100 ){
114395     int i;
114396     Vdbe *v = pParse->pVdbe;
114397     sqlite3ExplainBegin(v);
114398     for(i=0; i<sWLB.pWC->nTerm; i++){
114399       sqlite3ExplainPrintf(v, "#%-2d ", i);
114400       sqlite3ExplainPush(v);
114401       whereExplainTerm(v, &sWLB.pWC->a[i]);
114402       sqlite3ExplainPop(v);
114403       sqlite3ExplainNL(v);
114404     }
114405     sqlite3ExplainFinish(v);
114406     sqlite3DebugPrintf("%s", sqlite3VdbeExplanation(v));
114407   }
114408 #endif
114409   if( nTabList!=1 || whereShortCut(&sWLB)==0 ){
114410     rc = whereLoopAddAll(&sWLB);
114411     if( rc ) goto whereBeginError;
114412 
114413     /* Display all of the WhereLoop objects if wheretrace is enabled */
114414 #ifdef WHERETRACE_ENABLED /* !=0 */
114415     if( sqlite3WhereTrace ){
114416       WhereLoop *p;
114417       int i;
114418       static char zLabel[] = "0123456789abcdefghijklmnopqrstuvwyxz"
114419                                        "ABCDEFGHIJKLMNOPQRSTUVWYXZ";
114420       for(p=pWInfo->pLoops, i=0; p; p=p->pNextLoop, i++){
114421         p->cId = zLabel[i%sizeof(zLabel)];
114422         whereLoopPrint(p, sWLB.pWC);
114423       }
114424     }
114425 #endif
114426 
114427     wherePathSolver(pWInfo, 0);
114428     if( db->mallocFailed ) goto whereBeginError;
114429     if( pWInfo->pOrderBy ){
114430        wherePathSolver(pWInfo, pWInfo->nRowOut+1);
114431        if( db->mallocFailed ) goto whereBeginError;
114432     }
114433   }
114434   if( pWInfo->pOrderBy==0 && (db->flags & SQLITE_ReverseOrder)!=0 ){
114435      pWInfo->revMask = (Bitmask)(-1);
114436   }
114437   if( pParse->nErr || NEVER(db->mallocFailed) ){
114438     goto whereBeginError;
114439   }
114440 #ifdef WHERETRACE_ENABLED /* !=0 */
114441   if( sqlite3WhereTrace ){
114442     int ii;
114443     sqlite3DebugPrintf("---- Solution nRow=%d", pWInfo->nRowOut);
114444     if( pWInfo->bOBSat ){
114445       sqlite3DebugPrintf(" ORDERBY=0x%llx", pWInfo->revMask);
114446     }
114447     switch( pWInfo->eDistinct ){
114448       case WHERE_DISTINCT_UNIQUE: {
114449         sqlite3DebugPrintf("  DISTINCT=unique");
114450         break;
114451       }
114452       case WHERE_DISTINCT_ORDERED: {
114453         sqlite3DebugPrintf("  DISTINCT=ordered");
114454         break;
114455       }
114456       case WHERE_DISTINCT_UNORDERED: {
114457         sqlite3DebugPrintf("  DISTINCT=unordered");
114458         break;
114459       }
114460     }
114461     sqlite3DebugPrintf("\n");
114462     for(ii=0; ii<pWInfo->nLevel; ii++){
114463       whereLoopPrint(pWInfo->a[ii].pWLoop, sWLB.pWC);
114464     }
114465   }
114466 #endif
114467   /* Attempt to omit tables from the join that do not effect the result */
114468   if( pWInfo->nLevel>=2
114469    && pResultSet!=0
114470    && OptimizationEnabled(db, SQLITE_OmitNoopJoin)
114471   ){
114472     Bitmask tabUsed = exprListTableUsage(pMaskSet, pResultSet);
114473     if( sWLB.pOrderBy ) tabUsed |= exprListTableUsage(pMaskSet, sWLB.pOrderBy);
114474     while( pWInfo->nLevel>=2 ){
114475       WhereTerm *pTerm, *pEnd;
114476       pLoop = pWInfo->a[pWInfo->nLevel-1].pWLoop;
114477       if( (pWInfo->pTabList->a[pLoop->iTab].jointype & JT_LEFT)==0 ) break;
114478       if( (wctrlFlags & WHERE_WANT_DISTINCT)==0
114479        && (pLoop->wsFlags & WHERE_ONEROW)==0
114480       ){
114481         break;
114482       }
114483       if( (tabUsed & pLoop->maskSelf)!=0 ) break;
114484       pEnd = sWLB.pWC->a + sWLB.pWC->nTerm;
114485       for(pTerm=sWLB.pWC->a; pTerm<pEnd; pTerm++){
114486         if( (pTerm->prereqAll & pLoop->maskSelf)!=0
114487          && !ExprHasProperty(pTerm->pExpr, EP_FromJoin)
114488         ){
114489           break;
114490         }
114491       }
114492       if( pTerm<pEnd ) break;
114493       WHERETRACE(0xffff, ("-> drop loop %c not used\n", pLoop->cId));
114494       pWInfo->nLevel--;
114495       nTabList--;
114496     }
114497   }
114498   WHERETRACE(0xffff,("*** Optimizer Finished ***\n"));
114499   pWInfo->pParse->nQueryLoop += pWInfo->nRowOut;
114500 
114501   /* If the caller is an UPDATE or DELETE statement that is requesting
114502   ** to use a one-pass algorithm, determine if this is appropriate.
114503   ** The one-pass algorithm only works if the WHERE clause constrains
114504   ** the statement to update a single row.
114505   */
114506   assert( (wctrlFlags & WHERE_ONEPASS_DESIRED)==0 || pWInfo->nLevel==1 );
114507   if( (wctrlFlags & WHERE_ONEPASS_DESIRED)!=0
114508    && (pWInfo->a[0].pWLoop->wsFlags & WHERE_ONEROW)!=0 ){
114509     pWInfo->okOnePass = 1;
114510     if( HasRowid(pTabList->a[0].pTab) ){
114511       pWInfo->a[0].pWLoop->wsFlags &= ~WHERE_IDX_ONLY;
114512     }
114513   }
114514 
114515   /* Open all tables in the pTabList and any indices selected for
114516   ** searching those tables.
114517   */
114518   notReady = ~(Bitmask)0;
114519   for(ii=0, pLevel=pWInfo->a; ii<nTabList; ii++, pLevel++){
114520     Table *pTab;     /* Table to open */
114521     int iDb;         /* Index of database containing table/index */
114522     struct SrcList_item *pTabItem;
114523 
114524     pTabItem = &pTabList->a[pLevel->iFrom];
114525     pTab = pTabItem->pTab;
114526     iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
114527     pLoop = pLevel->pWLoop;
114528     if( (pTab->tabFlags & TF_Ephemeral)!=0 || pTab->pSelect ){
114529       /* Do nothing */
114530     }else
114531 #ifndef SQLITE_OMIT_VIRTUALTABLE
114532     if( (pLoop->wsFlags & WHERE_VIRTUALTABLE)!=0 ){
114533       const char *pVTab = (const char *)sqlite3GetVTable(db, pTab);
114534       int iCur = pTabItem->iCursor;
114535       sqlite3VdbeAddOp4(v, OP_VOpen, iCur, 0, 0, pVTab, P4_VTAB);
114536     }else if( IsVirtual(pTab) ){
114537       /* noop */
114538     }else
114539 #endif
114540     if( (pLoop->wsFlags & WHERE_IDX_ONLY)==0
114541          && (wctrlFlags & WHERE_OMIT_OPEN_CLOSE)==0 ){
114542       int op = OP_OpenRead;
114543       if( pWInfo->okOnePass ){
114544         op = OP_OpenWrite;
114545         pWInfo->aiCurOnePass[0] = pTabItem->iCursor;
114546       };
114547       sqlite3OpenTable(pParse, pTabItem->iCursor, iDb, pTab, op);
114548       assert( pTabItem->iCursor==pLevel->iTabCur );
114549       testcase( !pWInfo->okOnePass && pTab->nCol==BMS-1 );
114550       testcase( !pWInfo->okOnePass && pTab->nCol==BMS );
114551       if( !pWInfo->okOnePass && pTab->nCol<BMS && HasRowid(pTab) ){
114552         Bitmask b = pTabItem->colUsed;
114553         int n = 0;
114554         for(; b; b=b>>1, n++){}
114555         sqlite3VdbeChangeP4(v, sqlite3VdbeCurrentAddr(v)-1,
114556                             SQLITE_INT_TO_PTR(n), P4_INT32);
114557         assert( n<=pTab->nCol );
114558       }
114559     }else{
114560       sqlite3TableLock(pParse, iDb, pTab->tnum, 0, pTab->zName);
114561     }
114562     if( pLoop->wsFlags & WHERE_INDEXED ){
114563       Index *pIx = pLoop->u.btree.pIndex;
114564       int iIndexCur;
114565       int op = OP_OpenRead;
114566       /* iIdxCur is always set if to a positive value if ONEPASS is possible */
114567       assert( iIdxCur!=0 || (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 );
114568       if( pWInfo->okOnePass ){
114569         Index *pJ = pTabItem->pTab->pIndex;
114570         iIndexCur = iIdxCur;
114571         assert( wctrlFlags & WHERE_ONEPASS_DESIRED );
114572         while( ALWAYS(pJ) && pJ!=pIx ){
114573           iIndexCur++;
114574           pJ = pJ->pNext;
114575         }
114576         op = OP_OpenWrite;
114577         pWInfo->aiCurOnePass[1] = iIndexCur;
114578       }else if( iIdxCur && (wctrlFlags & WHERE_ONETABLE_ONLY)!=0 ){
114579         iIndexCur = iIdxCur;
114580       }else{
114581         iIndexCur = pParse->nTab++;
114582       }
114583       pLevel->iIdxCur = iIndexCur;
114584       assert( pIx->pSchema==pTab->pSchema );
114585       assert( iIndexCur>=0 );
114586       sqlite3VdbeAddOp3(v, op, iIndexCur, pIx->tnum, iDb);
114587       sqlite3VdbeSetP4KeyInfo(pParse, pIx);
114588       VdbeComment((v, "%s", pIx->zName));
114589     }
114590     sqlite3CodeVerifySchema(pParse, iDb);
114591     notReady &= ~getMask(&pWInfo->sMaskSet, pTabItem->iCursor);
114592   }
114593   pWInfo->iTop = sqlite3VdbeCurrentAddr(v);
114594   if( db->mallocFailed ) goto whereBeginError;
114595 
114596   /* Generate the code to do the search.  Each iteration of the for
114597   ** loop below generates code for a single nested loop of the VM
114598   ** program.
114599   */
114600   notReady = ~(Bitmask)0;
114601   for(ii=0; ii<nTabList; ii++){
114602     pLevel = &pWInfo->a[ii];
114603 #ifndef SQLITE_OMIT_AUTOMATIC_INDEX
114604     if( (pLevel->pWLoop->wsFlags & WHERE_AUTO_INDEX)!=0 ){
114605       constructAutomaticIndex(pParse, &pWInfo->sWC,
114606                 &pTabList->a[pLevel->iFrom], notReady, pLevel);
114607       if( db->mallocFailed ) goto whereBeginError;
114608     }
114609 #endif
114610     explainOneScan(pParse, pTabList, pLevel, ii, pLevel->iFrom, wctrlFlags);
114611     pLevel->addrBody = sqlite3VdbeCurrentAddr(v);
114612     notReady = codeOneLoopStart(pWInfo, ii, notReady);
114613     pWInfo->iContinue = pLevel->addrCont;
114614   }
114615 
114616   /* Done. */
114617   VdbeModuleComment((v, "Begin WHERE-core"));
114618   return pWInfo;
114619 
114620   /* Jump here if malloc fails */
114621 whereBeginError:
114622   if( pWInfo ){
114623     pParse->nQueryLoop = pWInfo->savedNQueryLoop;
114624     whereInfoFree(db, pWInfo);
114625   }
114626   return 0;
114627 }
114628 
114629 /*
114630 ** Generate the end of the WHERE loop.  See comments on
114631 ** sqlite3WhereBegin() for additional information.
114632 */
114633 SQLITE_PRIVATE void sqlite3WhereEnd(WhereInfo *pWInfo){
114634   Parse *pParse = pWInfo->pParse;
114635   Vdbe *v = pParse->pVdbe;
114636   int i;
114637   WhereLevel *pLevel;
114638   WhereLoop *pLoop;
114639   SrcList *pTabList = pWInfo->pTabList;
114640   sqlite3 *db = pParse->db;
114641 
114642   /* Generate loop termination code.
114643   */
114644   VdbeModuleComment((v, "End WHERE-core"));
114645   sqlite3ExprCacheClear(pParse);
114646   for(i=pWInfo->nLevel-1; i>=0; i--){
114647     int addr;
114648     pLevel = &pWInfo->a[i];
114649     pLoop = pLevel->pWLoop;
114650     sqlite3VdbeResolveLabel(v, pLevel->addrCont);
114651     if( pLevel->op!=OP_Noop ){
114652       sqlite3VdbeAddOp2(v, pLevel->op, pLevel->p1, pLevel->p2);
114653       sqlite3VdbeChangeP5(v, pLevel->p5);
114654     }
114655     if( pLoop->wsFlags & WHERE_IN_ABLE && pLevel->u.in.nIn>0 ){
114656       struct InLoop *pIn;
114657       int j;
114658       sqlite3VdbeResolveLabel(v, pLevel->addrNxt);
114659       for(j=pLevel->u.in.nIn, pIn=&pLevel->u.in.aInLoop[j-1]; j>0; j--, pIn--){
114660         sqlite3VdbeJumpHere(v, pIn->addrInTop+1);
114661         sqlite3VdbeAddOp2(v, pIn->eEndLoopOp, pIn->iCur, pIn->addrInTop);
114662         sqlite3VdbeJumpHere(v, pIn->addrInTop-1);
114663       }
114664       sqlite3DbFree(db, pLevel->u.in.aInLoop);
114665     }
114666     sqlite3VdbeResolveLabel(v, pLevel->addrBrk);
114667     if( pLevel->addrSkip ){
114668       sqlite3VdbeAddOp2(v, OP_Goto, 0, pLevel->addrSkip);
114669       VdbeComment((v, "next skip-scan on %s", pLoop->u.btree.pIndex->zName));
114670       sqlite3VdbeJumpHere(v, pLevel->addrSkip);
114671       sqlite3VdbeJumpHere(v, pLevel->addrSkip-2);
114672     }
114673     if( pLevel->iLeftJoin ){
114674       addr = sqlite3VdbeAddOp1(v, OP_IfPos, pLevel->iLeftJoin);
114675       assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0
114676            || (pLoop->wsFlags & WHERE_INDEXED)!=0 );
114677       if( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 ){
114678         sqlite3VdbeAddOp1(v, OP_NullRow, pTabList->a[i].iCursor);
114679       }
114680       if( pLoop->wsFlags & WHERE_INDEXED ){
114681         sqlite3VdbeAddOp1(v, OP_NullRow, pLevel->iIdxCur);
114682       }
114683       if( pLevel->op==OP_Return ){
114684         sqlite3VdbeAddOp2(v, OP_Gosub, pLevel->p1, pLevel->addrFirst);
114685       }else{
114686         sqlite3VdbeAddOp2(v, OP_Goto, 0, pLevel->addrFirst);
114687       }
114688       sqlite3VdbeJumpHere(v, addr);
114689     }
114690     VdbeModuleComment((v, "End WHERE-loop%d: %s", i,
114691                      pWInfo->pTabList->a[pLevel->iFrom].pTab->zName));
114692   }
114693 
114694   /* The "break" point is here, just past the end of the outer loop.
114695   ** Set it.
114696   */
114697   sqlite3VdbeResolveLabel(v, pWInfo->iBreak);
114698 
114699   assert( pWInfo->nLevel<=pTabList->nSrc );
114700   for(i=0, pLevel=pWInfo->a; i<pWInfo->nLevel; i++, pLevel++){
114701     Index *pIdx = 0;
114702     struct SrcList_item *pTabItem = &pTabList->a[pLevel->iFrom];
114703     Table *pTab = pTabItem->pTab;
114704     assert( pTab!=0 );
114705     pLoop = pLevel->pWLoop;
114706 
114707     /* Close all of the cursors that were opened by sqlite3WhereBegin.
114708     ** Except, do not close cursors that will be reused by the OR optimization
114709     ** (WHERE_OMIT_OPEN_CLOSE).  And do not close the OP_OpenWrite cursors
114710     ** created for the ONEPASS optimization.
114711     */
114712     if( (pTab->tabFlags & TF_Ephemeral)==0
114713      && pTab->pSelect==0
114714      && (pWInfo->wctrlFlags & WHERE_OMIT_OPEN_CLOSE)==0
114715     ){
114716       int ws = pLoop->wsFlags;
114717       if( !pWInfo->okOnePass && (ws & WHERE_IDX_ONLY)==0 ){
114718         sqlite3VdbeAddOp1(v, OP_Close, pTabItem->iCursor);
114719       }
114720       if( (ws & WHERE_INDEXED)!=0
114721        && (ws & (WHERE_IPK|WHERE_AUTO_INDEX))==0
114722        && pLevel->iIdxCur!=pWInfo->aiCurOnePass[1]
114723       ){
114724         sqlite3VdbeAddOp1(v, OP_Close, pLevel->iIdxCur);
114725       }
114726     }
114727 
114728     /* If this scan uses an index, make VDBE code substitutions to read data
114729     ** from the index instead of from the table where possible.  In some cases
114730     ** this optimization prevents the table from ever being read, which can
114731     ** yield a significant performance boost.
114732     **
114733     ** Calls to the code generator in between sqlite3WhereBegin and
114734     ** sqlite3WhereEnd will have created code that references the table
114735     ** directly.  This loop scans all that code looking for opcodes
114736     ** that reference the table and converts them into opcodes that
114737     ** reference the index.
114738     */
114739     if( pLoop->wsFlags & (WHERE_INDEXED|WHERE_IDX_ONLY) ){
114740       pIdx = pLoop->u.btree.pIndex;
114741     }else if( pLoop->wsFlags & WHERE_MULTI_OR ){
114742       pIdx = pLevel->u.pCovidx;
114743     }
114744     if( pIdx && !db->mallocFailed ){
114745       int k, last;
114746       VdbeOp *pOp;
114747 
114748       last = sqlite3VdbeCurrentAddr(v);
114749       k = pLevel->addrBody;
114750       pOp = sqlite3VdbeGetOp(v, k);
114751       for(; k<last; k++, pOp++){
114752         if( pOp->p1!=pLevel->iTabCur ) continue;
114753         if( pOp->opcode==OP_Column ){
114754           int x = pOp->p2;
114755           assert( pIdx->pTable==pTab );
114756           if( !HasRowid(pTab) ){
114757             Index *pPk = sqlite3PrimaryKeyIndex(pTab);
114758             x = pPk->aiColumn[x];
114759           }
114760           x = sqlite3ColumnOfIndex(pIdx, x);
114761           if( x>=0 ){
114762             pOp->p2 = x;
114763             pOp->p1 = pLevel->iIdxCur;
114764           }
114765           assert( (pLoop->wsFlags & WHERE_IDX_ONLY)==0 || x>=0 );
114766         }else if( pOp->opcode==OP_Rowid ){
114767           pOp->p1 = pLevel->iIdxCur;
114768           pOp->opcode = OP_IdxRowid;
114769         }
114770       }
114771     }
114772   }
114773 
114774   /* Final cleanup
114775   */
114776   pParse->nQueryLoop = pWInfo->savedNQueryLoop;
114777   whereInfoFree(db, pWInfo);
114778   return;
114779 }
114780 
114781 /************** End of where.c ***********************************************/
114782 /************** Begin file parse.c *******************************************/
114783 /* Driver template for the LEMON parser generator.
114784 ** The author disclaims copyright to this source code.
114785 **
114786 ** This version of "lempar.c" is modified, slightly, for use by SQLite.
114787 ** The only modifications are the addition of a couple of NEVER()
114788 ** macros to disable tests that are needed in the case of a general
114789 ** LALR(1) grammar but which are always false in the
114790 ** specific grammar used by SQLite.
114791 */
114792 /* First off, code is included that follows the "include" declaration
114793 ** in the input grammar file. */
114794 /* #include <stdio.h> */
114795 
114796 
114797 /*
114798 ** Disable all error recovery processing in the parser push-down
114799 ** automaton.
114800 */
114801 #define YYNOERRORRECOVERY 1
114802 
114803 /*
114804 ** Make yytestcase() the same as testcase()
114805 */
114806 #define yytestcase(X) testcase(X)
114807 
114808 /*
114809 ** An instance of this structure holds information about the
114810 ** LIMIT clause of a SELECT statement.
114811 */
114812 struct LimitVal {
114813   Expr *pLimit;    /* The LIMIT expression.  NULL if there is no limit */
114814   Expr *pOffset;   /* The OFFSET expression.  NULL if there is none */
114815 };
114816 
114817 /*
114818 ** An instance of this structure is used to store the LIKE,
114819 ** GLOB, NOT LIKE, and NOT GLOB operators.
114820 */
114821 struct LikeOp {
114822   Token eOperator;  /* "like" or "glob" or "regexp" */
114823   int bNot;         /* True if the NOT keyword is present */
114824 };
114825 
114826 /*
114827 ** An instance of the following structure describes the event of a
114828 ** TRIGGER.  "a" is the event type, one of TK_UPDATE, TK_INSERT,
114829 ** TK_DELETE, or TK_INSTEAD.  If the event is of the form
114830 **
114831 **      UPDATE ON (a,b,c)
114832 **
114833 ** Then the "b" IdList records the list "a,b,c".
114834 */
114835 struct TrigEvent { int a; IdList * b; };
114836 
114837 /*
114838 ** An instance of this structure holds the ATTACH key and the key type.
114839 */
114840 struct AttachKey { int type;  Token key; };
114841 
114842 
114843   /* This is a utility routine used to set the ExprSpan.zStart and
114844   ** ExprSpan.zEnd values of pOut so that the span covers the complete
114845   ** range of text beginning with pStart and going to the end of pEnd.
114846   */
114847   static void spanSet(ExprSpan *pOut, Token *pStart, Token *pEnd){
114848     pOut->zStart = pStart->z;
114849     pOut->zEnd = &pEnd->z[pEnd->n];
114850   }
114851 
114852   /* Construct a new Expr object from a single identifier.  Use the
114853   ** new Expr to populate pOut.  Set the span of pOut to be the identifier
114854   ** that created the expression.
114855   */
114856   static void spanExpr(ExprSpan *pOut, Parse *pParse, int op, Token *pValue){
114857     pOut->pExpr = sqlite3PExpr(pParse, op, 0, 0, pValue);
114858     pOut->zStart = pValue->z;
114859     pOut->zEnd = &pValue->z[pValue->n];
114860   }
114861 
114862   /* This routine constructs a binary expression node out of two ExprSpan
114863   ** objects and uses the result to populate a new ExprSpan object.
114864   */
114865   static void spanBinaryExpr(
114866     ExprSpan *pOut,     /* Write the result here */
114867     Parse *pParse,      /* The parsing context.  Errors accumulate here */
114868     int op,             /* The binary operation */
114869     ExprSpan *pLeft,    /* The left operand */
114870     ExprSpan *pRight    /* The right operand */
114871   ){
114872     pOut->pExpr = sqlite3PExpr(pParse, op, pLeft->pExpr, pRight->pExpr, 0);
114873     pOut->zStart = pLeft->zStart;
114874     pOut->zEnd = pRight->zEnd;
114875   }
114876 
114877   /* Construct an expression node for a unary postfix operator
114878   */
114879   static void spanUnaryPostfix(
114880     ExprSpan *pOut,        /* Write the new expression node here */
114881     Parse *pParse,         /* Parsing context to record errors */
114882     int op,                /* The operator */
114883     ExprSpan *pOperand,    /* The operand */
114884     Token *pPostOp         /* The operand token for setting the span */
114885   ){
114886     pOut->pExpr = sqlite3PExpr(pParse, op, pOperand->pExpr, 0, 0);
114887     pOut->zStart = pOperand->zStart;
114888     pOut->zEnd = &pPostOp->z[pPostOp->n];
114889   }
114890 
114891   /* A routine to convert a binary TK_IS or TK_ISNOT expression into a
114892   ** unary TK_ISNULL or TK_NOTNULL expression. */
114893   static void binaryToUnaryIfNull(Parse *pParse, Expr *pY, Expr *pA, int op){
114894     sqlite3 *db = pParse->db;
114895     if( db->mallocFailed==0 && pY->op==TK_NULL ){
114896       pA->op = (u8)op;
114897       sqlite3ExprDelete(db, pA->pRight);
114898       pA->pRight = 0;
114899     }
114900   }
114901 
114902   /* Construct an expression node for a unary prefix operator
114903   */
114904   static void spanUnaryPrefix(
114905     ExprSpan *pOut,        /* Write the new expression node here */
114906     Parse *pParse,         /* Parsing context to record errors */
114907     int op,                /* The operator */
114908     ExprSpan *pOperand,    /* The operand */
114909     Token *pPreOp         /* The operand token for setting the span */
114910   ){
114911     pOut->pExpr = sqlite3PExpr(pParse, op, pOperand->pExpr, 0, 0);
114912     pOut->zStart = pPreOp->z;
114913     pOut->zEnd = pOperand->zEnd;
114914   }
114915 /* Next is all token values, in a form suitable for use by makeheaders.
114916 ** This section will be null unless lemon is run with the -m switch.
114917 */
114918 /*
114919 ** These constants (all generated automatically by the parser generator)
114920 ** specify the various kinds of tokens (terminals) that the parser
114921 ** understands.
114922 **
114923 ** Each symbol here is a terminal symbol in the grammar.
114924 */
114925 /* Make sure the INTERFACE macro is defined.
114926 */
114927 #ifndef INTERFACE
114928 # define INTERFACE 1
114929 #endif
114930 /* The next thing included is series of defines which control
114931 ** various aspects of the generated parser.
114932 **    YYCODETYPE         is the data type used for storing terminal
114933 **                       and nonterminal numbers.  "unsigned char" is
114934 **                       used if there are fewer than 250 terminals
114935 **                       and nonterminals.  "int" is used otherwise.
114936 **    YYNOCODE           is a number of type YYCODETYPE which corresponds
114937 **                       to no legal terminal or nonterminal number.  This
114938 **                       number is used to fill in empty slots of the hash
114939 **                       table.
114940 **    YYFALLBACK         If defined, this indicates that one or more tokens
114941 **                       have fall-back values which should be used if the
114942 **                       original value of the token will not parse.
114943 **    YYACTIONTYPE       is the data type used for storing terminal
114944 **                       and nonterminal numbers.  "unsigned char" is
114945 **                       used if there are fewer than 250 rules and
114946 **                       states combined.  "int" is used otherwise.
114947 **    sqlite3ParserTOKENTYPE     is the data type used for minor tokens given
114948 **                       directly to the parser from the tokenizer.
114949 **    YYMINORTYPE        is the data type used for all minor tokens.
114950 **                       This is typically a union of many types, one of
114951 **                       which is sqlite3ParserTOKENTYPE.  The entry in the union
114952 **                       for base tokens is called "yy0".
114953 **    YYSTACKDEPTH       is the maximum depth of the parser's stack.  If
114954 **                       zero the stack is dynamically sized using realloc()
114955 **    sqlite3ParserARG_SDECL     A static variable declaration for the %extra_argument
114956 **    sqlite3ParserARG_PDECL     A parameter declaration for the %extra_argument
114957 **    sqlite3ParserARG_STORE     Code to store %extra_argument into yypParser
114958 **    sqlite3ParserARG_FETCH     Code to extract %extra_argument from yypParser
114959 **    YYNSTATE           the combined number of states.
114960 **    YYNRULE            the number of rules in the grammar
114961 **    YYERRORSYMBOL      is the code number of the error symbol.  If not
114962 **                       defined, then do no error processing.
114963 */
114964 #define YYCODETYPE unsigned char
114965 #define YYNOCODE 254
114966 #define YYACTIONTYPE unsigned short int
114967 #define YYWILDCARD 70
114968 #define sqlite3ParserTOKENTYPE Token
114969 typedef union {
114970   int yyinit;
114971   sqlite3ParserTOKENTYPE yy0;
114972   Select* yy3;
114973   ExprList* yy14;
114974   With* yy59;
114975   SrcList* yy65;
114976   struct LikeOp yy96;
114977   Expr* yy132;
114978   u8 yy186;
114979   int yy328;
114980   ExprSpan yy346;
114981   struct TrigEvent yy378;
114982   u16 yy381;
114983   IdList* yy408;
114984   struct {int value; int mask;} yy429;
114985   TriggerStep* yy473;
114986   struct LimitVal yy476;
114987 } YYMINORTYPE;
114988 #ifndef YYSTACKDEPTH
114989 #define YYSTACKDEPTH 100
114990 #endif
114991 #define sqlite3ParserARG_SDECL Parse *pParse;
114992 #define sqlite3ParserARG_PDECL ,Parse *pParse
114993 #define sqlite3ParserARG_FETCH Parse *pParse = yypParser->pParse
114994 #define sqlite3ParserARG_STORE yypParser->pParse = pParse
114995 #define YYNSTATE 642
114996 #define YYNRULE 327
114997 #define YYFALLBACK 1
114998 #define YY_NO_ACTION      (YYNSTATE+YYNRULE+2)
114999 #define YY_ACCEPT_ACTION  (YYNSTATE+YYNRULE+1)
115000 #define YY_ERROR_ACTION   (YYNSTATE+YYNRULE)
115001 
115002 /* The yyzerominor constant is used to initialize instances of
115003 ** YYMINORTYPE objects to zero. */
115004 static const YYMINORTYPE yyzerominor = { 0 };
115005 
115006 /* Define the yytestcase() macro to be a no-op if is not already defined
115007 ** otherwise.
115008 **
115009 ** Applications can choose to define yytestcase() in the %include section
115010 ** to a macro that can assist in verifying code coverage.  For production
115011 ** code the yytestcase() macro should be turned off.  But it is useful
115012 ** for testing.
115013 */
115014 #ifndef yytestcase
115015 # define yytestcase(X)
115016 #endif
115017 
115018 
115019 /* Next are the tables used to determine what action to take based on the
115020 ** current state and lookahead token.  These tables are used to implement
115021 ** functions that take a state number and lookahead value and return an
115022 ** action integer.
115023 **
115024 ** Suppose the action integer is N.  Then the action is determined as
115025 ** follows
115026 **
115027 **   0 <= N < YYNSTATE                  Shift N.  That is, push the lookahead
115028 **                                      token onto the stack and goto state N.
115029 **
115030 **   YYNSTATE <= N < YYNSTATE+YYNRULE   Reduce by rule N-YYNSTATE.
115031 **
115032 **   N == YYNSTATE+YYNRULE              A syntax error has occurred.
115033 **
115034 **   N == YYNSTATE+YYNRULE+1            The parser accepts its input.
115035 **
115036 **   N == YYNSTATE+YYNRULE+2            No such action.  Denotes unused
115037 **                                      slots in the yy_action[] table.
115038 **
115039 ** The action table is constructed as a single large table named yy_action[].
115040 ** Given state S and lookahead X, the action is computed as
115041 **
115042 **      yy_action[ yy_shift_ofst[S] + X ]
115043 **
115044 ** If the index value yy_shift_ofst[S]+X is out of range or if the value
115045 ** yy_lookahead[yy_shift_ofst[S]+X] is not equal to X or if yy_shift_ofst[S]
115046 ** is equal to YY_SHIFT_USE_DFLT, it means that the action is not in the table
115047 ** and that yy_default[S] should be used instead.
115048 **
115049 ** The formula above is for computing the action when the lookahead is
115050 ** a terminal symbol.  If the lookahead is a non-terminal (as occurs after
115051 ** a reduce action) then the yy_reduce_ofst[] array is used in place of
115052 ** the yy_shift_ofst[] array and YY_REDUCE_USE_DFLT is used in place of
115053 ** YY_SHIFT_USE_DFLT.
115054 **
115055 ** The following are the tables generated in this section:
115056 **
115057 **  yy_action[]        A single table containing all actions.
115058 **  yy_lookahead[]     A table containing the lookahead for each entry in
115059 **                     yy_action.  Used to detect hash collisions.
115060 **  yy_shift_ofst[]    For each state, the offset into yy_action for
115061 **                     shifting terminals.
115062 **  yy_reduce_ofst[]   For each state, the offset into yy_action for
115063 **                     shifting non-terminals after a reduce.
115064 **  yy_default[]       Default action for each state.
115065 */
115066 #define YY_ACTTAB_COUNT (1497)
115067 static const YYACTIONTYPE yy_action[] = {
115068  /*     0 */   306,  212,  432,  955,  639,  191,  955,  295,  559,   88,
115069  /*    10 */    88,   88,   88,   81,   86,   86,   86,   86,   85,   85,
115070  /*    20 */    84,   84,   84,   83,  330,  185,  184,  183,  635,  635,
115071  /*    30 */   292,  606,  606,   88,   88,   88,   88,  683,   86,   86,
115072  /*    40 */    86,   86,   85,   85,   84,   84,   84,   83,  330,   16,
115073  /*    50 */   436,  597,   89,   90,   80,  600,  599,  601,  601,   87,
115074  /*    60 */    87,   88,   88,   88,   88,  684,   86,   86,   86,   86,
115075  /*    70 */    85,   85,   84,   84,   84,   83,  330,  306,  559,   84,
115076  /*    80 */    84,   84,   83,  330,   65,   86,   86,   86,   86,   85,
115077  /*    90 */    85,   84,   84,   84,   83,  330,  635,  635,  634,  633,
115078  /*   100 */   182,  682,  550,  379,  376,  375,   17,  322,  606,  606,
115079  /*   110 */   371,  198,  479,   91,  374,   82,   79,  165,   85,   85,
115080  /*   120 */    84,   84,   84,   83,  330,  598,  635,  635,  107,   89,
115081  /*   130 */    90,   80,  600,  599,  601,  601,   87,   87,   88,   88,
115082  /*   140 */    88,   88,  186,   86,   86,   86,   86,   85,   85,   84,
115083  /*   150 */    84,   84,   83,  330,  306,  594,  594,  142,  328,  327,
115084  /*   160 */   484,  249,  344,  238,  635,  635,  634,  633,  585,  448,
115085  /*   170 */   526,  525,  229,  388,    1,  394,  450,  584,  449,  635,
115086  /*   180 */   635,  635,  635,  319,  395,  606,  606,  199,  157,  273,
115087  /*   190 */   382,  268,  381,  187,  635,  635,  634,  633,  311,  555,
115088  /*   200 */   266,  593,  593,  266,  347,  588,   89,   90,   80,  600,
115089  /*   210 */   599,  601,  601,   87,   87,   88,   88,   88,   88,  478,
115090  /*   220 */    86,   86,   86,   86,   85,   85,   84,   84,   84,   83,
115091  /*   230 */   330,  306,  272,  536,  634,  633,  146,  610,  197,  310,
115092  /*   240 */   575,  182,  482,  271,  379,  376,  375,  506,   21,  634,
115093  /*   250 */   633,  634,  633,  635,  635,  374,  611,  574,  548,  440,
115094  /*   260 */   111,  563,  606,  606,  634,  633,  324,  479,  608,  608,
115095  /*   270 */   608,  300,  435,  573,  119,  407,  210,  162,  562,  883,
115096  /*   280 */   592,  592,  306,   89,   90,   80,  600,  599,  601,  601,
115097  /*   290 */    87,   87,   88,   88,   88,   88,  506,   86,   86,   86,
115098  /*   300 */    86,   85,   85,   84,   84,   84,   83,  330,  620,  111,
115099  /*   310 */   635,  635,  361,  606,  606,  358,  249,  349,  248,  433,
115100  /*   320 */   243,  479,  586,  634,  633,  195,  611,   93,  119,  221,
115101  /*   330 */   575,  497,  534,  534,   89,   90,   80,  600,  599,  601,
115102  /*   340 */   601,   87,   87,   88,   88,   88,   88,  574,   86,   86,
115103  /*   350 */    86,   86,   85,   85,   84,   84,   84,   83,  330,  306,
115104  /*   360 */    77,  429,  638,  573,  589,  530,  240,  230,  242,  105,
115105  /*   370 */   249,  349,  248,  515,  588,  208,  460,  529,  564,  173,
115106  /*   380 */   634,  633,  970,  144,  430,    2,  424,  228,  380,  557,
115107  /*   390 */   606,  606,  190,  153,  159,  158,  514,   51,  632,  631,
115108  /*   400 */   630,   71,  536,  432,  954,  196,  610,  954,  614,   45,
115109  /*   410 */    18,   89,   90,   80,  600,  599,  601,  601,   87,   87,
115110  /*   420 */    88,   88,   88,   88,  261,   86,   86,   86,   86,   85,
115111  /*   430 */    85,   84,   84,   84,   83,  330,  306,  608,  608,  608,
115112  /*   440 */   542,  424,  402,  385,  241,  506,  451,  320,  211,  543,
115113  /*   450 */   164,  436,  386,  293,  451,  587,  108,  496,  111,  334,
115114  /*   460 */   391,  591,  424,  614,   27,  452,  453,  606,  606,   72,
115115  /*   470 */   257,   70,  259,  452,  339,  342,  564,  582,   68,  415,
115116  /*   480 */   469,  328,  327,   62,  614,   45,  110,  393,   89,   90,
115117  /*   490 */    80,  600,  599,  601,  601,   87,   87,   88,   88,   88,
115118  /*   500 */    88,  152,   86,   86,   86,   86,   85,   85,   84,   84,
115119  /*   510 */    84,   83,  330,  306,  110,  499,  520,  538,  402,  389,
115120  /*   520 */   424,  110,  566,  500,  593,  593,  454,   82,   79,  165,
115121  /*   530 */   424,  591,  384,  564,  340,  615,  188,  162,  424,  350,
115122  /*   540 */   616,  424,  614,   44,  606,  606,  445,  582,  300,  434,
115123  /*   550 */   151,   19,  614,    9,  568,  580,  348,  615,  469,  567,
115124  /*   560 */   614,   26,  616,  614,   45,   89,   90,   80,  600,  599,
115125  /*   570 */   601,  601,   87,   87,   88,   88,   88,   88,  411,   86,
115126  /*   580 */    86,   86,   86,   85,   85,   84,   84,   84,   83,  330,
115127  /*   590 */   306,  579,  110,  578,  521,  282,  433,  398,  400,  255,
115128  /*   600 */   486,   82,   79,  165,  487,  164,   82,   79,  165,  488,
115129  /*   610 */   488,  364,  387,  424,  544,  544,  509,  350,  362,  155,
115130  /*   620 */   191,  606,  606,  559,  642,  640,  333,   82,   79,  165,
115131  /*   630 */   305,  564,  507,  312,  357,  614,   45,  329,  596,  595,
115132  /*   640 */   194,  337,   89,   90,   80,  600,  599,  601,  601,   87,
115133  /*   650 */    87,   88,   88,   88,   88,  424,   86,   86,   86,   86,
115134  /*   660 */    85,   85,   84,   84,   84,   83,  330,  306,   20,  323,
115135  /*   670 */   150,  263,  211,  543,  421,  596,  595,  614,   22,  424,
115136  /*   680 */   193,  424,  284,  424,  391,  424,  509,  424,  577,  424,
115137  /*   690 */   186,  335,  424,  559,  424,  313,  120,  546,  606,  606,
115138  /*   700 */    67,  614,   47,  614,   50,  614,   48,  614,  100,  614,
115139  /*   710 */    99,  614,  101,  576,  614,  102,  614,  109,  326,   89,
115140  /*   720 */    90,   80,  600,  599,  601,  601,   87,   87,   88,   88,
115141  /*   730 */    88,   88,  424,   86,   86,   86,   86,   85,   85,   84,
115142  /*   740 */    84,   84,   83,  330,  306,  424,  311,  424,  585,   54,
115143  /*   750 */   424,  516,  517,  590,  614,  112,  424,  584,  424,  572,
115144  /*   760 */   424,  195,  424,  571,  424,   67,  424,  614,   94,  614,
115145  /*   770 */    98,  424,  614,   97,  264,  606,  606,  195,  614,   46,
115146  /*   780 */   614,   96,  614,   30,  614,   49,  614,  115,  614,  114,
115147  /*   790 */   418,  229,  388,  614,  113,  306,   89,   90,   80,  600,
115148  /*   800 */   599,  601,  601,   87,   87,   88,   88,   88,   88,  424,
115149  /*   810 */    86,   86,   86,   86,   85,   85,   84,   84,   84,   83,
115150  /*   820 */   330,  119,  424,  590,  110,  372,  606,  606,  195,   53,
115151  /*   830 */   250,  614,   29,  195,  472,  438,  729,  190,  302,  498,
115152  /*   840 */    14,  523,  641,    2,  614,   43,  306,   89,   90,   80,
115153  /*   850 */   600,  599,  601,  601,   87,   87,   88,   88,   88,   88,
115154  /*   860 */   424,   86,   86,   86,   86,   85,   85,   84,   84,   84,
115155  /*   870 */    83,  330,  424,  613,  964,  964,  354,  606,  606,  420,
115156  /*   880 */   312,   64,  614,   42,  391,  355,  283,  437,  301,  255,
115157  /*   890 */   414,  410,  495,  492,  614,   28,  471,  306,   89,   90,
115158  /*   900 */    80,  600,  599,  601,  601,   87,   87,   88,   88,   88,
115159  /*   910 */    88,  424,   86,   86,   86,   86,   85,   85,   84,   84,
115160  /*   920 */    84,   83,  330,  424,  110,  110,  110,  110,  606,  606,
115161  /*   930 */   110,  254,   13,  614,   41,  532,  531,  283,  481,  531,
115162  /*   940 */   457,  284,  119,  561,  356,  614,   40,  284,  306,   89,
115163  /*   950 */    78,   80,  600,  599,  601,  601,   87,   87,   88,   88,
115164  /*   960 */    88,   88,  424,   86,   86,   86,   86,   85,   85,   84,
115165  /*   970 */    84,   84,   83,  330,  110,  424,  341,  220,  555,  606,
115166  /*   980 */   606,  351,  555,  318,  614,   95,  413,  255,   83,  330,
115167  /*   990 */   284,  284,  255,  640,  333,  356,  255,  614,   39,  306,
115168  /*  1000 */   356,   90,   80,  600,  599,  601,  601,   87,   87,   88,
115169  /*  1010 */    88,   88,   88,  424,   86,   86,   86,   86,   85,   85,
115170  /*  1020 */    84,   84,   84,   83,  330,  424,  317,  316,  141,  465,
115171  /*  1030 */   606,  606,  219,  619,  463,  614,   10,  417,  462,  255,
115172  /*  1040 */   189,  510,  553,  351,  207,  363,  161,  614,   38,  315,
115173  /*  1050 */   218,  255,  255,   80,  600,  599,  601,  601,   87,   87,
115174  /*  1060 */    88,   88,   88,   88,  424,   86,   86,   86,   86,   85,
115175  /*  1070 */    85,   84,   84,   84,   83,  330,   76,  419,  255,    3,
115176  /*  1080 */   878,  461,  424,  247,  331,  331,  614,   37,  217,   76,
115177  /*  1090 */   419,  390,    3,  216,  215,  422,    4,  331,  331,  424,
115178  /*  1100 */   547,   12,  424,  545,  614,   36,  424,  541,  422,  424,
115179  /*  1110 */   540,  424,  214,  424,  408,  424,  539,  403,  605,  605,
115180  /*  1120 */   237,  614,   25,  119,  614,   24,  588,  408,  614,   45,
115181  /*  1130 */   118,  614,   35,  614,   34,  614,   33,  614,   23,  588,
115182  /*  1140 */    60,  223,  603,  602,  513,  378,   73,   74,  140,  139,
115183  /*  1150 */   424,  110,  265,   75,  426,  425,   59,  424,  610,   73,
115184  /*  1160 */    74,  549,  402,  404,  424,  373,   75,  426,  425,  604,
115185  /*  1170 */   138,  610,  614,   11,  392,   76,  419,  181,    3,  614,
115186  /*  1180 */    32,  271,  369,  331,  331,  493,  614,   31,  149,  608,
115187  /*  1190 */   608,  608,  607,   15,  422,  365,  614,    8,  137,  489,
115188  /*  1200 */   136,  190,  608,  608,  608,  607,   15,  485,  176,  135,
115189  /*  1210 */     7,  252,  477,  408,  174,  133,  175,  474,   57,   56,
115190  /*  1220 */   132,  130,  119,   76,  419,  588,    3,  468,  245,  464,
115191  /*  1230 */   171,  331,  331,  125,  123,  456,  447,  122,  446,  104,
115192  /*  1240 */   336,  231,  422,  166,  154,   73,   74,  332,  116,  431,
115193  /*  1250 */   121,  309,   75,  426,  425,  222,  106,  610,  308,  637,
115194  /*  1260 */   204,  408,  629,  627,  628,    6,  200,  428,  427,  290,
115195  /*  1270 */   203,  622,  201,  588,   62,   63,  289,   66,  419,  399,
115196  /*  1280 */     3,  401,  288,   92,  143,  331,  331,  287,  608,  608,
115197  /*  1290 */   608,  607,   15,   73,   74,  227,  422,  325,   69,  416,
115198  /*  1300 */    75,  426,  425,  612,  412,  610,  192,   61,  569,  209,
115199  /*  1310 */   396,  226,  278,  225,  383,  408,  527,  558,  276,  533,
115200  /*  1320 */   552,  528,  321,  523,  370,  508,  180,  588,  494,  179,
115201  /*  1330 */   366,  117,  253,  269,  522,  503,  608,  608,  608,  607,
115202  /*  1340 */    15,  551,  502,   58,  274,  524,  178,   73,   74,  304,
115203  /*  1350 */   501,  368,  303,  206,   75,  426,  425,  491,  360,  610,
115204  /*  1360 */   213,  177,  483,  131,  345,  298,  297,  296,  202,  294,
115205  /*  1370 */   480,  490,  466,  134,  172,  129,  444,  346,  470,  128,
115206  /*  1380 */   314,  459,  103,  127,  126,  148,  124,  167,  443,  235,
115207  /*  1390 */   608,  608,  608,  607,   15,  442,  439,  623,  234,  299,
115208  /*  1400 */   145,  583,  291,  377,  581,  160,  119,  156,  270,  636,
115209  /*  1410 */   971,  169,  279,  626,  520,  625,  473,  624,  170,  621,
115210  /*  1420 */   618,  119,  168,   55,  409,  423,  537,  609,  286,  285,
115211  /*  1430 */   405,  570,  560,  556,    5,   52,  458,  554,  147,  267,
115212  /*  1440 */   519,  504,  518,  406,  262,  239,  260,  512,  343,  511,
115213  /*  1450 */   258,  353,  565,  256,  224,  251,  359,  277,  275,  476,
115214  /*  1460 */   475,  246,  352,  244,  467,  455,  236,  233,  232,  307,
115215  /*  1470 */   441,  281,  205,  163,  397,  280,  535,  505,  330,  617,
115216  /*  1480 */   971,  971,  971,  971,  367,  971,  971,  971,  971,  971,
115217  /*  1490 */   971,  971,  971,  971,  971,  971,  338,
115218 };
115219 static const YYCODETYPE yy_lookahead[] = {
115220  /*     0 */    19,   22,   22,   23,    1,   24,   26,   15,   27,   80,
115221  /*    10 */    81,   82,   83,   84,   85,   86,   87,   88,   89,   90,
115222  /*    20 */    91,   92,   93,   94,   95,  108,  109,  110,   27,   28,
115223  /*    30 */    23,   50,   51,   80,   81,   82,   83,  122,   85,   86,
115224  /*    40 */    87,   88,   89,   90,   91,   92,   93,   94,   95,   22,
115225  /*    50 */    70,   23,   71,   72,   73,   74,   75,   76,   77,   78,
115226  /*    60 */    79,   80,   81,   82,   83,  122,   85,   86,   87,   88,
115227  /*    70 */    89,   90,   91,   92,   93,   94,   95,   19,   97,   91,
115228  /*    80 */    92,   93,   94,   95,   26,   85,   86,   87,   88,   89,
115229  /*    90 */    90,   91,   92,   93,   94,   95,   27,   28,   97,   98,
115230  /*   100 */    99,  122,  211,  102,  103,  104,   79,   19,   50,   51,
115231  /*   110 */    19,  122,   59,   55,  113,  224,  225,  226,   89,   90,
115232  /*   120 */    91,   92,   93,   94,   95,   23,   27,   28,   26,   71,
115233  /*   130 */    72,   73,   74,   75,   76,   77,   78,   79,   80,   81,
115234  /*   140 */    82,   83,   51,   85,   86,   87,   88,   89,   90,   91,
115235  /*   150 */    92,   93,   94,   95,   19,  132,  133,   58,   89,   90,
115236  /*   160 */    21,  108,  109,  110,   27,   28,   97,   98,   33,  100,
115237  /*   170 */     7,    8,  119,  120,   22,   19,  107,   42,  109,   27,
115238  /*   180 */    28,   27,   28,   95,   28,   50,   51,   99,  100,  101,
115239  /*   190 */   102,  103,  104,  105,   27,   28,   97,   98,  107,  152,
115240  /*   200 */   112,  132,  133,  112,   65,   69,   71,   72,   73,   74,
115241  /*   210 */    75,   76,   77,   78,   79,   80,   81,   82,   83,   11,
115242  /*   220 */    85,   86,   87,   88,   89,   90,   91,   92,   93,   94,
115243  /*   230 */    95,   19,  101,   97,   97,   98,   24,  101,  122,  157,
115244  /*   240 */    12,   99,  103,  112,  102,  103,  104,  152,   22,   97,
115245  /*   250 */    98,   97,   98,   27,   28,  113,   27,   29,   91,  164,
115246  /*   260 */   165,  124,   50,   51,   97,   98,  219,   59,  132,  133,
115247  /*   270 */   134,   22,   23,   45,   66,   47,  212,  213,  124,  140,
115248  /*   280 */   132,  133,   19,   71,   72,   73,   74,   75,   76,   77,
115249  /*   290 */    78,   79,   80,   81,   82,   83,  152,   85,   86,   87,
115250  /*   300 */    88,   89,   90,   91,   92,   93,   94,   95,  164,  165,
115251  /*   310 */    27,   28,  230,   50,   51,  233,  108,  109,  110,   70,
115252  /*   320 */    16,   59,   23,   97,   98,   26,   97,   22,   66,  185,
115253  /*   330 */    12,  187,   27,   28,   71,   72,   73,   74,   75,   76,
115254  /*   340 */    77,   78,   79,   80,   81,   82,   83,   29,   85,   86,
115255  /*   350 */    87,   88,   89,   90,   91,   92,   93,   94,   95,   19,
115256  /*   360 */    22,  148,  149,   45,   23,   47,   62,  154,   64,  156,
115257  /*   370 */   108,  109,  110,   37,   69,   23,  163,   59,   26,   26,
115258  /*   380 */    97,   98,  144,  145,  146,  147,  152,  200,   52,   23,
115259  /*   390 */    50,   51,   26,   22,   89,   90,   60,  210,    7,    8,
115260  /*   400 */     9,  138,   97,   22,   23,   26,  101,   26,  174,  175,
115261  /*   410 */   197,   71,   72,   73,   74,   75,   76,   77,   78,   79,
115262  /*   420 */    80,   81,   82,   83,   16,   85,   86,   87,   88,   89,
115263  /*   430 */    90,   91,   92,   93,   94,   95,   19,  132,  133,  134,
115264  /*   440 */    23,  152,  208,  209,  140,  152,  152,  111,  195,  196,
115265  /*   450 */    98,   70,  163,  160,  152,   23,   22,  164,  165,  246,
115266  /*   460 */   207,   27,  152,  174,  175,  171,  172,   50,   51,  137,
115267  /*   470 */    62,  139,   64,  171,  172,  222,  124,   27,  138,   24,
115268  /*   480 */   163,   89,   90,  130,  174,  175,  197,  163,   71,   72,
115269  /*   490 */    73,   74,   75,   76,   77,   78,   79,   80,   81,   82,
115270  /*   500 */    83,   22,   85,   86,   87,   88,   89,   90,   91,   92,
115271  /*   510 */    93,   94,   95,   19,  197,  181,  182,   23,  208,  209,
115272  /*   520 */   152,  197,   26,  189,  132,  133,  232,  224,  225,  226,
115273  /*   530 */   152,   97,   91,   26,  232,  116,  212,  213,  152,  222,
115274  /*   540 */   121,  152,  174,  175,   50,   51,  243,   97,   22,   23,
115275  /*   550 */    22,  234,  174,  175,  177,   23,  239,  116,  163,  177,
115276  /*   560 */   174,  175,  121,  174,  175,   71,   72,   73,   74,   75,
115277  /*   570 */    76,   77,   78,   79,   80,   81,   82,   83,   24,   85,
115278  /*   580 */    86,   87,   88,   89,   90,   91,   92,   93,   94,   95,
115279  /*   590 */    19,   23,  197,   11,   23,  227,   70,  208,  220,  152,
115280  /*   600 */    31,  224,  225,  226,   35,   98,  224,  225,  226,  108,
115281  /*   610 */   109,  110,  115,  152,  117,  118,   27,  222,   49,  123,
115282  /*   620 */    24,   50,   51,   27,    0,    1,    2,  224,  225,  226,
115283  /*   630 */   166,  124,  168,  169,  239,  174,  175,  170,  171,  172,
115284  /*   640 */    22,  194,   71,   72,   73,   74,   75,   76,   77,   78,
115285  /*   650 */    79,   80,   81,   82,   83,  152,   85,   86,   87,   88,
115286  /*   660 */    89,   90,   91,   92,   93,   94,   95,   19,   22,  208,
115287  /*   670 */    24,   23,  195,  196,  170,  171,  172,  174,  175,  152,
115288  /*   680 */    26,  152,  152,  152,  207,  152,   97,  152,   23,  152,
115289  /*   690 */    51,  244,  152,   97,  152,  247,  248,   23,   50,   51,
115290  /*   700 */    26,  174,  175,  174,  175,  174,  175,  174,  175,  174,
115291  /*   710 */   175,  174,  175,   23,  174,  175,  174,  175,  188,   71,
115292  /*   720 */    72,   73,   74,   75,   76,   77,   78,   79,   80,   81,
115293  /*   730 */    82,   83,  152,   85,   86,   87,   88,   89,   90,   91,
115294  /*   740 */    92,   93,   94,   95,   19,  152,  107,  152,   33,   24,
115295  /*   750 */   152,  100,  101,   27,  174,  175,  152,   42,  152,   23,
115296  /*   760 */   152,   26,  152,   23,  152,   26,  152,  174,  175,  174,
115297  /*   770 */   175,  152,  174,  175,   23,   50,   51,   26,  174,  175,
115298  /*   780 */   174,  175,  174,  175,  174,  175,  174,  175,  174,  175,
115299  /*   790 */   163,  119,  120,  174,  175,   19,   71,   72,   73,   74,
115300  /*   800 */    75,   76,   77,   78,   79,   80,   81,   82,   83,  152,
115301  /*   810 */    85,   86,   87,   88,   89,   90,   91,   92,   93,   94,
115302  /*   820 */    95,   66,  152,   97,  197,   23,   50,   51,   26,   53,
115303  /*   830 */    23,  174,  175,   26,   23,   23,   23,   26,   26,   26,
115304  /*   840 */    36,  106,  146,  147,  174,  175,   19,   71,   72,   73,
115305  /*   850 */    74,   75,   76,   77,   78,   79,   80,   81,   82,   83,
115306  /*   860 */   152,   85,   86,   87,   88,   89,   90,   91,   92,   93,
115307  /*   870 */    94,   95,  152,  196,  119,  120,   19,   50,   51,  168,
115308  /*   880 */   169,   26,  174,  175,  207,   28,  152,  249,  250,  152,
115309  /*   890 */   163,  163,  163,  163,  174,  175,  163,   19,   71,   72,
115310  /*   900 */    73,   74,   75,   76,   77,   78,   79,   80,   81,   82,
115311  /*   910 */    83,  152,   85,   86,   87,   88,   89,   90,   91,   92,
115312  /*   920 */    93,   94,   95,  152,  197,  197,  197,  197,   50,   51,
115313  /*   930 */   197,  194,   36,  174,  175,  191,  192,  152,  191,  192,
115314  /*   940 */   163,  152,   66,  124,  152,  174,  175,  152,   19,   71,
115315  /*   950 */    72,   73,   74,   75,   76,   77,   78,   79,   80,   81,
115316  /*   960 */    82,   83,  152,   85,   86,   87,   88,   89,   90,   91,
115317  /*   970 */    92,   93,   94,   95,  197,  152,  100,  188,  152,   50,
115318  /*   980 */    51,  152,  152,  188,  174,  175,  252,  152,   94,   95,
115319  /*   990 */   152,  152,  152,    1,    2,  152,  152,  174,  175,   19,
115320  /*  1000 */   152,   72,   73,   74,   75,   76,   77,   78,   79,   80,
115321  /*  1010 */    81,   82,   83,  152,   85,   86,   87,   88,   89,   90,
115322  /*  1020 */    91,   92,   93,   94,   95,  152,  188,  188,   22,  194,
115323  /*  1030 */    50,   51,  240,  173,  194,  174,  175,  252,  194,  152,
115324  /*  1040 */    36,  181,   28,  152,   23,  219,  122,  174,  175,  219,
115325  /*  1050 */   221,  152,  152,   73,   74,   75,   76,   77,   78,   79,
115326  /*  1060 */    80,   81,   82,   83,  152,   85,   86,   87,   88,   89,
115327  /*  1070 */    90,   91,   92,   93,   94,   95,   19,   20,  152,   22,
115328  /*  1080 */    23,  194,  152,  240,   27,   28,  174,  175,  240,   19,
115329  /*  1090 */    20,   26,   22,  194,  194,   38,   22,   27,   28,  152,
115330  /*  1100 */    23,   22,  152,  116,  174,  175,  152,   23,   38,  152,
115331  /*  1110 */    23,  152,  221,  152,   57,  152,   23,  163,   50,   51,
115332  /*  1120 */   194,  174,  175,   66,  174,  175,   69,   57,  174,  175,
115333  /*  1130 */    40,  174,  175,  174,  175,  174,  175,  174,  175,   69,
115334  /*  1140 */    22,   53,   74,   75,   30,   53,   89,   90,   22,   22,
115335  /*  1150 */   152,  197,   23,   96,   97,   98,   22,  152,  101,   89,
115336  /*  1160 */    90,   91,  208,  209,  152,   53,   96,   97,   98,  101,
115337  /*  1170 */    22,  101,  174,  175,  152,   19,   20,  105,   22,  174,
115338  /*  1180 */   175,  112,   19,   27,   28,   20,  174,  175,   24,  132,
115339  /*  1190 */   133,  134,  135,  136,   38,   44,  174,  175,  107,   61,
115340  /*  1200 */    54,   26,  132,  133,  134,  135,  136,   54,  107,   22,
115341  /*  1210 */     5,  140,    1,   57,   36,  111,  122,   28,   79,   79,
115342  /*  1220 */   131,  123,   66,   19,   20,   69,   22,    1,   16,   20,
115343  /*  1230 */   125,   27,   28,  123,  111,  120,   23,  131,   23,   16,
115344  /*  1240 */    68,  142,   38,   15,   22,   89,   90,    3,  167,    4,
115345  /*  1250 */   248,  251,   96,   97,   98,  180,  180,  101,  251,  151,
115346  /*  1260 */     6,   57,  151,   13,  151,   26,   25,  151,  161,  202,
115347  /*  1270 */   153,  162,  153,   69,  130,  128,  203,   19,   20,  127,
115348  /*  1280 */    22,  126,  204,  129,   22,   27,   28,  205,  132,  133,
115349  /*  1290 */   134,  135,  136,   89,   90,  231,   38,   95,  137,  179,
115350  /*  1300 */    96,   97,   98,  206,  179,  101,  122,  107,  159,  159,
115351  /*  1310 */   125,  231,  216,  228,  107,   57,  184,  217,  216,  176,
115352  /*  1320 */   217,  176,   48,  106,   18,  184,  158,   69,  159,  158,
115353  /*  1330 */    46,   71,  237,  176,  176,  176,  132,  133,  134,  135,
115354  /*  1340 */   136,  217,  176,  137,  216,  178,  158,   89,   90,  179,
115355  /*  1350 */   176,  159,  179,  159,   96,   97,   98,  159,  159,  101,
115356  /*  1360 */     5,  158,  202,   22,   18,   10,   11,   12,   13,   14,
115357  /*  1370 */   190,  238,   17,  190,  158,  193,   41,  159,  202,  193,
115358  /*  1380 */   159,  202,  245,  193,  193,  223,  190,   32,  159,   34,
115359  /*  1390 */   132,  133,  134,  135,  136,  159,   39,  155,   43,  150,
115360  /*  1400 */   223,  177,  201,  178,  177,  186,   66,  199,  177,  152,
115361  /*  1410 */   253,   56,  215,  152,  182,  152,  202,  152,   63,  152,
115362  /*  1420 */   152,   66,   67,  242,  229,  152,  174,  152,  152,  152,
115363  /*  1430 */   152,  152,  152,  152,  199,  242,  202,  152,  198,  152,
115364  /*  1440 */   152,  152,  183,  192,  152,  215,  152,  183,  215,  183,
115365  /*  1450 */   152,  241,  214,  152,  211,  152,  152,  211,  211,  152,
115366  /*  1460 */   152,  241,  152,  152,  152,  152,  152,  152,  152,  114,
115367  /*  1470 */   152,  152,  235,  152,  152,  152,  174,  187,   95,  174,
115368  /*  1480 */   253,  253,  253,  253,  236,  253,  253,  253,  253,  253,
115369  /*  1490 */   253,  253,  253,  253,  253,  253,  141,
115370 };
115371 #define YY_SHIFT_USE_DFLT (-86)
115372 #define YY_SHIFT_COUNT (429)
115373 #define YY_SHIFT_MIN   (-85)
115374 #define YY_SHIFT_MAX   (1383)
115375 static const short yy_shift_ofst[] = {
115376  /*     0 */   992, 1057, 1355, 1156, 1204, 1204,    1,  262,  -19,  135,
115377  /*    10 */   135,  776, 1204, 1204, 1204, 1204,   69,   69,   53,  208,
115378  /*    20 */   283,  755,   58,  725,  648,  571,  494,  417,  340,  263,
115379  /*    30 */   212,  827,  827,  827,  827,  827,  827,  827,  827,  827,
115380  /*    40 */   827,  827,  827,  827,  827,  827,  878,  827,  929,  980,
115381  /*    50 */   980, 1070, 1204, 1204, 1204, 1204, 1204, 1204, 1204, 1204,
115382  /*    60 */  1204, 1204, 1204, 1204, 1204, 1204, 1204, 1204, 1204, 1204,
115383  /*    70 */  1204, 1204, 1204, 1204, 1204, 1204, 1204, 1204, 1204, 1204,
115384  /*    80 */  1258, 1204, 1204, 1204, 1204, 1204, 1204, 1204, 1204, 1204,
115385  /*    90 */  1204, 1204, 1204, 1204,  -71,  -47,  -47,  -47,  -47,  -47,
115386  /*   100 */     0,   29,  -12,  283,  283,  139,   91,  392,  392,  894,
115387  /*   110 */   672,  726, 1383,  -86,  -86,  -86,   88,  318,  318,   99,
115388  /*   120 */   381,  -20,  283,  283,  283,  283,  283,  283,  283,  283,
115389  /*   130 */   283,  283,  283,  283,  283,  283,  283,  283,  283,  283,
115390  /*   140 */   283,  283,  283,  283,  624,  876,  726,  672, 1340, 1340,
115391  /*   150 */  1340, 1340, 1340, 1340,  -86,  -86,  -86,  305,  136,  136,
115392  /*   160 */   142,  167,  226,  154,  137,  152,  283,  283,  283,  283,
115393  /*   170 */   283,  283,  283,  283,  283,  283,  283,  283,  283,  283,
115394  /*   180 */   283,  283,  283,  336,  336,  336,  283,  283,  352,  283,
115395  /*   190 */   283,  283,  283,  283,  228,  283,  283,  283,  283,  283,
115396  /*   200 */   283,  283,  283,  283,  283,  501,  569,  596,  596,  596,
115397  /*   210 */   507,  497,  441,  391,  353,  156,  156,  857,  353,  857,
115398  /*   220 */   735,  813,  639,  715,  156,  332,  715,  715,  496,  419,
115399  /*   230 */   646, 1357, 1184, 1184, 1335, 1335, 1184, 1341, 1260, 1144,
115400  /*   240 */  1346, 1346, 1346, 1346, 1184, 1306, 1144, 1341, 1260, 1260,
115401  /*   250 */  1144, 1184, 1306, 1206, 1284, 1184, 1184, 1306, 1184, 1306,
115402  /*   260 */  1184, 1306, 1262, 1207, 1207, 1207, 1274, 1262, 1207, 1217,
115403  /*   270 */  1207, 1274, 1207, 1207, 1185, 1200, 1185, 1200, 1185, 1200,
115404  /*   280 */  1184, 1184, 1161, 1262, 1202, 1202, 1262, 1154, 1155, 1147,
115405  /*   290 */  1152, 1144, 1241, 1239, 1250, 1250, 1254, 1254, 1254, 1254,
115406  /*   300 */   -86,  -86,  -86,  -86,  -86,  -86, 1068,  304,  526,  249,
115407  /*   310 */   408,  -83,  434,  812,   27,  811,  807,  802,  751,  589,
115408  /*   320 */   651,  163,  131,  674,  366,  450,  299,  148,   23,  102,
115409  /*   330 */   229,  -21, 1245, 1244, 1222, 1099, 1228, 1172, 1223, 1215,
115410  /*   340 */  1213, 1115, 1106, 1123, 1110, 1209, 1105, 1212, 1226, 1098,
115411  /*   350 */  1089, 1140, 1139, 1104, 1189, 1178, 1094, 1211, 1205, 1187,
115412  /*   360 */  1101, 1071, 1153, 1175, 1146, 1138, 1151, 1091, 1164, 1165,
115413  /*   370 */  1163, 1069, 1072, 1148, 1112, 1134, 1127, 1129, 1126, 1092,
115414  /*   380 */  1114, 1118, 1088, 1090, 1093, 1087, 1084,  987, 1079, 1077,
115415  /*   390 */  1074, 1065,  924, 1021, 1014, 1004, 1006,  819,  739,  896,
115416  /*   400 */   855,  804,  739,  740,  736,  690,  654,  665,  618,  582,
115417  /*   410 */   568,  528,  554,  379,  532,  479,  455,  379,  432,  371,
115418  /*   420 */   341,   28,  338,  116,  -11,  -57,  -85,    7,   -8,    3,
115419 };
115420 #define YY_REDUCE_USE_DFLT (-110)
115421 #define YY_REDUCE_COUNT (305)
115422 #define YY_REDUCE_MIN   (-109)
115423 #define YY_REDUCE_MAX   (1323)
115424 static const short yy_reduce_ofst[] = {
115425  /*     0 */   238,  954,  213,  289,  310,  234,  144,  317, -109,  382,
115426  /*    10 */   377,  303,  461,  389,  378,  368,  302,  294,  253,  395,
115427  /*    20 */   293,  324,  403,  403,  403,  403,  403,  403,  403,  403,
115428  /*    30 */   403,  403,  403,  403,  403,  403,  403,  403,  403,  403,
115429  /*    40 */   403,  403,  403,  403,  403,  403,  403,  403,  403,  403,
115430  /*    50 */   403, 1022, 1012, 1005,  998,  963,  961,  959,  957,  950,
115431  /*    60 */   947,  930,  912,  873,  861,  823,  810,  771,  759,  720,
115432  /*    70 */   708,  670,  657,  619,  614,  612,  610,  608,  606,  604,
115433  /*    80 */   598,  595,  593,  580,  542,  540,  537,  535,  533,  531,
115434  /*    90 */   529,  527,  503,  386,  403,  403,  403,  403,  403,  403,
115435  /*   100 */   403,  403,  403,   95,  447,   82,  334,  504,  467,  403,
115436  /*   110 */   477,  464,  403,  403,  403,  403,  860,  747,  744,  785,
115437  /*   120 */   638,  638,  926,  891,  900,  899,  887,  844,  840,  835,
115438  /*   130 */   848,  830,  843,  829,  792,  839,  826,  737,  838,  795,
115439  /*   140 */   789,   47,  734,  530,  696,  777,  711,  677,  733,  730,
115440  /*   150 */   729,  728,  727,  627,  448,   64,  187, 1305, 1302, 1252,
115441  /*   160 */  1290, 1273, 1323, 1322, 1321, 1319, 1318, 1316, 1315, 1314,
115442  /*   170 */  1313, 1312, 1311, 1310, 1308, 1307, 1304, 1303, 1301, 1298,
115443  /*   180 */  1294, 1292, 1289, 1266, 1264, 1259, 1288, 1287, 1238, 1285,
115444  /*   190 */  1281, 1280, 1279, 1278, 1251, 1277, 1276, 1275, 1273, 1268,
115445  /*   200 */  1267, 1265, 1263, 1261, 1257, 1248, 1237, 1247, 1246, 1243,
115446  /*   210 */  1238, 1240, 1235, 1249, 1234, 1233, 1230, 1220, 1214, 1210,
115447  /*   220 */  1225, 1219, 1232, 1231, 1197, 1195, 1227, 1224, 1201, 1208,
115448  /*   230 */  1242, 1137, 1236, 1229, 1193, 1181, 1221, 1177, 1196, 1179,
115449  /*   240 */  1191, 1190, 1186, 1182, 1218, 1216, 1176, 1162, 1183, 1180,
115450  /*   250 */  1160, 1199, 1203, 1133, 1095, 1198, 1194, 1188, 1192, 1171,
115451  /*   260 */  1169, 1168, 1173, 1174, 1166, 1159, 1141, 1170, 1158, 1167,
115452  /*   270 */  1157, 1132, 1145, 1143, 1124, 1128, 1103, 1102, 1100, 1096,
115453  /*   280 */  1150, 1149, 1085, 1125, 1080, 1064, 1120, 1097, 1082, 1078,
115454  /*   290 */  1073, 1067, 1109, 1107, 1119, 1117, 1116, 1113, 1111, 1108,
115455  /*   300 */  1007, 1000, 1002, 1076, 1075, 1081,
115456 };
115457 static const YYACTIONTYPE yy_default[] = {
115458  /*     0 */   647,  964,  964,  964,  878,  878,  969,  964,  774,  802,
115459  /*    10 */   802,  938,  969,  969,  969,  876,  969,  969,  969,  964,
115460  /*    20 */   969,  778,  808,  969,  969,  969,  969,  969,  969,  969,
115461  /*    30 */   969,  937,  939,  816,  815,  918,  789,  813,  806,  810,
115462  /*    40 */   879,  872,  873,  871,  875,  880,  969,  809,  841,  856,
115463  /*    50 */   840,  969,  969,  969,  969,  969,  969,  969,  969,  969,
115464  /*    60 */   969,  969,  969,  969,  969,  969,  969,  969,  969,  969,
115465  /*    70 */   969,  969,  969,  969,  969,  969,  969,  969,  969,  969,
115466  /*    80 */   969,  969,  969,  969,  969,  969,  969,  969,  969,  969,
115467  /*    90 */   969,  969,  969,  969,  850,  855,  862,  854,  851,  843,
115468  /*   100 */   842,  844,  845,  969,  969,  673,  739,  969,  969,  846,
115469  /*   110 */   969,  685,  847,  859,  858,  857,  680,  969,  969,  969,
115470  /*   120 */   969,  969,  969,  969,  969,  969,  969,  969,  969,  969,
115471  /*   130 */   969,  969,  969,  969,  969,  969,  969,  969,  969,  969,
115472  /*   140 */   969,  969,  969,  969,  647,  964,  969,  969,  964,  964,
115473  /*   150 */   964,  964,  964,  964,  956,  778,  768,  969,  969,  969,
115474  /*   160 */   969,  969,  969,  969,  969,  969,  969,  944,  942,  969,
115475  /*   170 */   891,  969,  969,  969,  969,  969,  969,  969,  969,  969,
115476  /*   180 */   969,  969,  969,  969,  969,  969,  969,  969,  969,  969,
115477  /*   190 */   969,  969,  969,  969,  969,  969,  969,  969,  969,  969,
115478  /*   200 */   969,  969,  969,  969,  653,  969,  911,  774,  774,  774,
115479  /*   210 */   776,  754,  766,  655,  812,  791,  791,  923,  812,  923,
115480  /*   220 */   710,  733,  707,  802,  791,  874,  802,  802,  775,  766,
115481  /*   230 */   969,  949,  782,  782,  941,  941,  782,  821,  743,  812,
115482  /*   240 */   750,  750,  750,  750,  782,  670,  812,  821,  743,  743,
115483  /*   250 */   812,  782,  670,  917,  915,  782,  782,  670,  782,  670,
115484  /*   260 */   782,  670,  884,  741,  741,  741,  725,  884,  741,  710,
115485  /*   270 */   741,  725,  741,  741,  795,  790,  795,  790,  795,  790,
115486  /*   280 */   782,  782,  969,  884,  888,  888,  884,  807,  796,  805,
115487  /*   290 */   803,  812,  676,  728,  663,  663,  652,  652,  652,  652,
115488  /*   300 */   961,  961,  956,  712,  712,  695,  969,  969,  969,  969,
115489  /*   310 */   969,  969,  687,  969,  893,  969,  969,  969,  969,  969,
115490  /*   320 */   969,  969,  969,  969,  969,  969,  969,  969,  969,  969,
115491  /*   330 */   969,  828,  969,  648,  951,  969,  969,  948,  969,  969,
115492  /*   340 */   969,  969,  969,  969,  969,  969,  969,  969,  969,  969,
115493  /*   350 */   969,  969,  969,  969,  969,  969,  921,  969,  969,  969,
115494  /*   360 */   969,  969,  969,  914,  913,  969,  969,  969,  969,  969,
115495  /*   370 */   969,  969,  969,  969,  969,  969,  969,  969,  969,  969,
115496  /*   380 */   969,  969,  969,  969,  969,  969,  969,  757,  969,  969,
115497  /*   390 */   969,  761,  969,  969,  969,  969,  969,  969,  804,  969,
115498  /*   400 */   797,  969,  877,  969,  969,  969,  969,  969,  969,  969,
115499  /*   410 */   969,  969,  969,  966,  969,  969,  969,  965,  969,  969,
115500  /*   420 */   969,  969,  969,  830,  969,  829,  833,  969,  661,  969,
115501  /*   430 */   644,  649,  960,  963,  962,  959,  958,  957,  952,  950,
115502  /*   440 */   947,  946,  945,  943,  940,  936,  897,  895,  902,  901,
115503  /*   450 */   900,  899,  898,  896,  894,  892,  818,  817,  814,  811,
115504  /*   460 */   753,  935,  890,  752,  749,  748,  669,  953,  920,  929,
115505  /*   470 */   928,  927,  822,  926,  925,  924,  922,  919,  906,  820,
115506  /*   480 */   819,  744,  882,  881,  672,  910,  909,  908,  912,  916,
115507  /*   490 */   907,  784,  751,  671,  668,  675,  679,  731,  732,  740,
115508  /*   500 */   738,  737,  736,  735,  734,  730,  681,  686,  724,  709,
115509  /*   510 */   708,  717,  716,  722,  721,  720,  719,  718,  715,  714,
115510  /*   520 */   713,  706,  705,  711,  704,  727,  726,  723,  703,  747,
115511  /*   530 */   746,  745,  742,  702,  701,  700,  833,  699,  698,  838,
115512  /*   540 */   837,  866,  826,  755,  759,  758,  762,  763,  771,  770,
115513  /*   550 */   769,  780,  781,  793,  792,  824,  823,  794,  779,  773,
115514  /*   560 */   772,  788,  787,  786,  785,  777,  767,  799,  798,  868,
115515  /*   570 */   783,  867,  865,  934,  933,  932,  931,  930,  870,  967,
115516  /*   580 */   968,  887,  889,  886,  801,  800,  885,  869,  839,  836,
115517  /*   590 */   690,  691,  905,  904,  903,  693,  692,  689,  688,  863,
115518  /*   600 */   860,  852,  864,  861,  853,  849,  848,  834,  832,  831,
115519  /*   610 */   827,  835,  760,  756,  825,  765,  764,  697,  696,  694,
115520  /*   620 */   678,  677,  674,  667,  665,  664,  666,  662,  660,  659,
115521  /*   630 */   658,  657,  656,  684,  683,  682,  654,  651,  650,  646,
115522  /*   640 */   645,  643,
115523 };
115524 
115525 /* The next table maps tokens into fallback tokens.  If a construct
115526 ** like the following:
115527 **
115528 **      %fallback ID X Y Z.
115529 **
115530 ** appears in the grammar, then ID becomes a fallback token for X, Y,
115531 ** and Z.  Whenever one of the tokens X, Y, or Z is input to the parser
115532 ** but it does not parse, the type of the token is changed to ID and
115533 ** the parse is retried before an error is thrown.
115534 */
115535 #ifdef YYFALLBACK
115536 static const YYCODETYPE yyFallback[] = {
115537     0,  /*          $ => nothing */
115538     0,  /*       SEMI => nothing */
115539    27,  /*    EXPLAIN => ID */
115540    27,  /*      QUERY => ID */
115541    27,  /*       PLAN => ID */
115542    27,  /*      BEGIN => ID */
115543     0,  /* TRANSACTION => nothing */
115544    27,  /*   DEFERRED => ID */
115545    27,  /*  IMMEDIATE => ID */
115546    27,  /*  EXCLUSIVE => ID */
115547     0,  /*     COMMIT => nothing */
115548    27,  /*        END => ID */
115549    27,  /*   ROLLBACK => ID */
115550    27,  /*  SAVEPOINT => ID */
115551    27,  /*    RELEASE => ID */
115552     0,  /*         TO => nothing */
115553     0,  /*      TABLE => nothing */
115554     0,  /*     CREATE => nothing */
115555    27,  /*         IF => ID */
115556     0,  /*        NOT => nothing */
115557     0,  /*     EXISTS => nothing */
115558    27,  /*       TEMP => ID */
115559     0,  /*         LP => nothing */
115560     0,  /*         RP => nothing */
115561     0,  /*         AS => nothing */
115562    27,  /*    WITHOUT => ID */
115563     0,  /*      COMMA => nothing */
115564     0,  /*         ID => nothing */
115565     0,  /*    INDEXED => nothing */
115566    27,  /*      ABORT => ID */
115567    27,  /*     ACTION => ID */
115568    27,  /*      AFTER => ID */
115569    27,  /*    ANALYZE => ID */
115570    27,  /*        ASC => ID */
115571    27,  /*     ATTACH => ID */
115572    27,  /*     BEFORE => ID */
115573    27,  /*         BY => ID */
115574    27,  /*    CASCADE => ID */
115575    27,  /*       CAST => ID */
115576    27,  /*   COLUMNKW => ID */
115577    27,  /*   CONFLICT => ID */
115578    27,  /*   DATABASE => ID */
115579    27,  /*       DESC => ID */
115580    27,  /*     DETACH => ID */
115581    27,  /*       EACH => ID */
115582    27,  /*       FAIL => ID */
115583    27,  /*        FOR => ID */
115584    27,  /*     IGNORE => ID */
115585    27,  /*  INITIALLY => ID */
115586    27,  /*    INSTEAD => ID */
115587    27,  /*    LIKE_KW => ID */
115588    27,  /*      MATCH => ID */
115589    27,  /*         NO => ID */
115590    27,  /*        KEY => ID */
115591    27,  /*         OF => ID */
115592    27,  /*     OFFSET => ID */
115593    27,  /*     PRAGMA => ID */
115594    27,  /*      RAISE => ID */
115595    27,  /*  RECURSIVE => ID */
115596    27,  /*    REPLACE => ID */
115597    27,  /*   RESTRICT => ID */
115598    27,  /*        ROW => ID */
115599    27,  /*    TRIGGER => ID */
115600    27,  /*     VACUUM => ID */
115601    27,  /*       VIEW => ID */
115602    27,  /*    VIRTUAL => ID */
115603    27,  /*       WITH => ID */
115604    27,  /*    REINDEX => ID */
115605    27,  /*     RENAME => ID */
115606    27,  /*   CTIME_KW => ID */
115607 };
115608 #endif /* YYFALLBACK */
115609 
115610 /* The following structure represents a single element of the
115611 ** parser's stack.  Information stored includes:
115612 **
115613 **   +  The state number for the parser at this level of the stack.
115614 **
115615 **   +  The value of the token stored at this level of the stack.
115616 **      (In other words, the "major" token.)
115617 **
115618 **   +  The semantic value stored at this level of the stack.  This is
115619 **      the information used by the action routines in the grammar.
115620 **      It is sometimes called the "minor" token.
115621 */
115622 struct yyStackEntry {
115623   YYACTIONTYPE stateno;  /* The state-number */
115624   YYCODETYPE major;      /* The major token value.  This is the code
115625                          ** number for the token at this stack level */
115626   YYMINORTYPE minor;     /* The user-supplied minor token value.  This
115627                          ** is the value of the token  */
115628 };
115629 typedef struct yyStackEntry yyStackEntry;
115630 
115631 /* The state of the parser is completely contained in an instance of
115632 ** the following structure */
115633 struct yyParser {
115634   int yyidx;                    /* Index of top element in stack */
115635 #ifdef YYTRACKMAXSTACKDEPTH
115636   int yyidxMax;                 /* Maximum value of yyidx */
115637 #endif
115638   int yyerrcnt;                 /* Shifts left before out of the error */
115639   sqlite3ParserARG_SDECL                /* A place to hold %extra_argument */
115640 #if YYSTACKDEPTH<=0
115641   int yystksz;                  /* Current side of the stack */
115642   yyStackEntry *yystack;        /* The parser's stack */
115643 #else
115644   yyStackEntry yystack[YYSTACKDEPTH];  /* The parser's stack */
115645 #endif
115646 };
115647 typedef struct yyParser yyParser;
115648 
115649 #ifndef NDEBUG
115650 /* #include <stdio.h> */
115651 static FILE *yyTraceFILE = 0;
115652 static char *yyTracePrompt = 0;
115653 #endif /* NDEBUG */
115654 
115655 #ifndef NDEBUG
115656 /*
115657 ** Turn parser tracing on by giving a stream to which to write the trace
115658 ** and a prompt to preface each trace message.  Tracing is turned off
115659 ** by making either argument NULL
115660 **
115661 ** Inputs:
115662 ** <ul>
115663 ** <li> A FILE* to which trace output should be written.
115664 **      If NULL, then tracing is turned off.
115665 ** <li> A prefix string written at the beginning of every
115666 **      line of trace output.  If NULL, then tracing is
115667 **      turned off.
115668 ** </ul>
115669 **
115670 ** Outputs:
115671 ** None.
115672 */
115673 SQLITE_PRIVATE void sqlite3ParserTrace(FILE *TraceFILE, char *zTracePrompt){
115674   yyTraceFILE = TraceFILE;
115675   yyTracePrompt = zTracePrompt;
115676   if( yyTraceFILE==0 ) yyTracePrompt = 0;
115677   else if( yyTracePrompt==0 ) yyTraceFILE = 0;
115678 }
115679 #endif /* NDEBUG */
115680 
115681 #ifndef NDEBUG
115682 /* For tracing shifts, the names of all terminals and nonterminals
115683 ** are required.  The following table supplies these names */
115684 static const char *const yyTokenName[] = {
115685   "$",             "SEMI",          "EXPLAIN",       "QUERY",
115686   "PLAN",          "BEGIN",         "TRANSACTION",   "DEFERRED",
115687   "IMMEDIATE",     "EXCLUSIVE",     "COMMIT",        "END",
115688   "ROLLBACK",      "SAVEPOINT",     "RELEASE",       "TO",
115689   "TABLE",         "CREATE",        "IF",            "NOT",
115690   "EXISTS",        "TEMP",          "LP",            "RP",
115691   "AS",            "WITHOUT",       "COMMA",         "ID",
115692   "INDEXED",       "ABORT",         "ACTION",        "AFTER",
115693   "ANALYZE",       "ASC",           "ATTACH",        "BEFORE",
115694   "BY",            "CASCADE",       "CAST",          "COLUMNKW",
115695   "CONFLICT",      "DATABASE",      "DESC",          "DETACH",
115696   "EACH",          "FAIL",          "FOR",           "IGNORE",
115697   "INITIALLY",     "INSTEAD",       "LIKE_KW",       "MATCH",
115698   "NO",            "KEY",           "OF",            "OFFSET",
115699   "PRAGMA",        "RAISE",         "RECURSIVE",     "REPLACE",
115700   "RESTRICT",      "ROW",           "TRIGGER",       "VACUUM",
115701   "VIEW",          "VIRTUAL",       "WITH",          "REINDEX",
115702   "RENAME",        "CTIME_KW",      "ANY",           "OR",
115703   "AND",           "IS",            "BETWEEN",       "IN",
115704   "ISNULL",        "NOTNULL",       "NE",            "EQ",
115705   "GT",            "LE",            "LT",            "GE",
115706   "ESCAPE",        "BITAND",        "BITOR",         "LSHIFT",
115707   "RSHIFT",        "PLUS",          "MINUS",         "STAR",
115708   "SLASH",         "REM",           "CONCAT",        "COLLATE",
115709   "BITNOT",        "STRING",        "JOIN_KW",       "CONSTRAINT",
115710   "DEFAULT",       "NULL",          "PRIMARY",       "UNIQUE",
115711   "CHECK",         "REFERENCES",    "AUTOINCR",      "ON",
115712   "INSERT",        "DELETE",        "UPDATE",        "SET",
115713   "DEFERRABLE",    "FOREIGN",       "DROP",          "UNION",
115714   "ALL",           "EXCEPT",        "INTERSECT",     "SELECT",
115715   "VALUES",        "DISTINCT",      "DOT",           "FROM",
115716   "JOIN",          "USING",         "ORDER",         "GROUP",
115717   "HAVING",        "LIMIT",         "WHERE",         "INTO",
115718   "INTEGER",       "FLOAT",         "BLOB",          "VARIABLE",
115719   "CASE",          "WHEN",          "THEN",          "ELSE",
115720   "INDEX",         "ALTER",         "ADD",           "error",
115721   "input",         "cmdlist",       "ecmd",          "explain",
115722   "cmdx",          "cmd",           "transtype",     "trans_opt",
115723   "nm",            "savepoint_opt",  "create_table",  "create_table_args",
115724   "createkw",      "temp",          "ifnotexists",   "dbnm",
115725   "columnlist",    "conslist_opt",  "table_options",  "select",
115726   "column",        "columnid",      "type",          "carglist",
115727   "typetoken",     "typename",      "signed",        "plus_num",
115728   "minus_num",     "ccons",         "term",          "expr",
115729   "onconf",        "sortorder",     "autoinc",       "idxlist_opt",
115730   "refargs",       "defer_subclause",  "refarg",        "refact",
115731   "init_deferred_pred_opt",  "conslist",      "tconscomma",    "tcons",
115732   "idxlist",       "defer_subclause_opt",  "orconf",        "resolvetype",
115733   "raisetype",     "ifexists",      "fullname",      "selectnowith",
115734   "oneselect",     "with",          "multiselect_op",  "distinct",
115735   "selcollist",    "from",          "where_opt",     "groupby_opt",
115736   "having_opt",    "orderby_opt",   "limit_opt",     "values",
115737   "nexprlist",     "exprlist",      "sclp",          "as",
115738   "seltablist",    "stl_prefix",    "joinop",        "indexed_opt",
115739   "on_opt",        "using_opt",     "joinop2",       "idlist",
115740   "sortlist",      "setlist",       "insert_cmd",    "inscollist_opt",
115741   "likeop",        "between_op",    "in_op",         "case_operand",
115742   "case_exprlist",  "case_else",     "uniqueflag",    "collate",
115743   "nmnum",         "trigger_decl",  "trigger_cmd_list",  "trigger_time",
115744   "trigger_event",  "foreach_clause",  "when_clause",   "trigger_cmd",
115745   "trnm",          "tridxby",       "database_kw_opt",  "key_opt",
115746   "add_column_fullname",  "kwcolumn_opt",  "create_vtab",   "vtabarglist",
115747   "vtabarg",       "vtabargtoken",  "lp",            "anylist",
115748   "wqlist",
115749 };
115750 #endif /* NDEBUG */
115751 
115752 #ifndef NDEBUG
115753 /* For tracing reduce actions, the names of all rules are required.
115754 */
115755 static const char *const yyRuleName[] = {
115756  /*   0 */ "input ::= cmdlist",
115757  /*   1 */ "cmdlist ::= cmdlist ecmd",
115758  /*   2 */ "cmdlist ::= ecmd",
115759  /*   3 */ "ecmd ::= SEMI",
115760  /*   4 */ "ecmd ::= explain cmdx SEMI",
115761  /*   5 */ "explain ::=",
115762  /*   6 */ "explain ::= EXPLAIN",
115763  /*   7 */ "explain ::= EXPLAIN QUERY PLAN",
115764  /*   8 */ "cmdx ::= cmd",
115765  /*   9 */ "cmd ::= BEGIN transtype trans_opt",
115766  /*  10 */ "trans_opt ::=",
115767  /*  11 */ "trans_opt ::= TRANSACTION",
115768  /*  12 */ "trans_opt ::= TRANSACTION nm",
115769  /*  13 */ "transtype ::=",
115770  /*  14 */ "transtype ::= DEFERRED",
115771  /*  15 */ "transtype ::= IMMEDIATE",
115772  /*  16 */ "transtype ::= EXCLUSIVE",
115773  /*  17 */ "cmd ::= COMMIT trans_opt",
115774  /*  18 */ "cmd ::= END trans_opt",
115775  /*  19 */ "cmd ::= ROLLBACK trans_opt",
115776  /*  20 */ "savepoint_opt ::= SAVEPOINT",
115777  /*  21 */ "savepoint_opt ::=",
115778  /*  22 */ "cmd ::= SAVEPOINT nm",
115779  /*  23 */ "cmd ::= RELEASE savepoint_opt nm",
115780  /*  24 */ "cmd ::= ROLLBACK trans_opt TO savepoint_opt nm",
115781  /*  25 */ "cmd ::= create_table create_table_args",
115782  /*  26 */ "create_table ::= createkw temp TABLE ifnotexists nm dbnm",
115783  /*  27 */ "createkw ::= CREATE",
115784  /*  28 */ "ifnotexists ::=",
115785  /*  29 */ "ifnotexists ::= IF NOT EXISTS",
115786  /*  30 */ "temp ::= TEMP",
115787  /*  31 */ "temp ::=",
115788  /*  32 */ "create_table_args ::= LP columnlist conslist_opt RP table_options",
115789  /*  33 */ "create_table_args ::= AS select",
115790  /*  34 */ "table_options ::=",
115791  /*  35 */ "table_options ::= WITHOUT nm",
115792  /*  36 */ "columnlist ::= columnlist COMMA column",
115793  /*  37 */ "columnlist ::= column",
115794  /*  38 */ "column ::= columnid type carglist",
115795  /*  39 */ "columnid ::= nm",
115796  /*  40 */ "nm ::= ID|INDEXED",
115797  /*  41 */ "nm ::= STRING",
115798  /*  42 */ "nm ::= JOIN_KW",
115799  /*  43 */ "type ::=",
115800  /*  44 */ "type ::= typetoken",
115801  /*  45 */ "typetoken ::= typename",
115802  /*  46 */ "typetoken ::= typename LP signed RP",
115803  /*  47 */ "typetoken ::= typename LP signed COMMA signed RP",
115804  /*  48 */ "typename ::= ID|STRING",
115805  /*  49 */ "typename ::= typename ID|STRING",
115806  /*  50 */ "signed ::= plus_num",
115807  /*  51 */ "signed ::= minus_num",
115808  /*  52 */ "carglist ::= carglist ccons",
115809  /*  53 */ "carglist ::=",
115810  /*  54 */ "ccons ::= CONSTRAINT nm",
115811  /*  55 */ "ccons ::= DEFAULT term",
115812  /*  56 */ "ccons ::= DEFAULT LP expr RP",
115813  /*  57 */ "ccons ::= DEFAULT PLUS term",
115814  /*  58 */ "ccons ::= DEFAULT MINUS term",
115815  /*  59 */ "ccons ::= DEFAULT ID|INDEXED",
115816  /*  60 */ "ccons ::= NULL onconf",
115817  /*  61 */ "ccons ::= NOT NULL onconf",
115818  /*  62 */ "ccons ::= PRIMARY KEY sortorder onconf autoinc",
115819  /*  63 */ "ccons ::= UNIQUE onconf",
115820  /*  64 */ "ccons ::= CHECK LP expr RP",
115821  /*  65 */ "ccons ::= REFERENCES nm idxlist_opt refargs",
115822  /*  66 */ "ccons ::= defer_subclause",
115823  /*  67 */ "ccons ::= COLLATE ID|STRING",
115824  /*  68 */ "autoinc ::=",
115825  /*  69 */ "autoinc ::= AUTOINCR",
115826  /*  70 */ "refargs ::=",
115827  /*  71 */ "refargs ::= refargs refarg",
115828  /*  72 */ "refarg ::= MATCH nm",
115829  /*  73 */ "refarg ::= ON INSERT refact",
115830  /*  74 */ "refarg ::= ON DELETE refact",
115831  /*  75 */ "refarg ::= ON UPDATE refact",
115832  /*  76 */ "refact ::= SET NULL",
115833  /*  77 */ "refact ::= SET DEFAULT",
115834  /*  78 */ "refact ::= CASCADE",
115835  /*  79 */ "refact ::= RESTRICT",
115836  /*  80 */ "refact ::= NO ACTION",
115837  /*  81 */ "defer_subclause ::= NOT DEFERRABLE init_deferred_pred_opt",
115838  /*  82 */ "defer_subclause ::= DEFERRABLE init_deferred_pred_opt",
115839  /*  83 */ "init_deferred_pred_opt ::=",
115840  /*  84 */ "init_deferred_pred_opt ::= INITIALLY DEFERRED",
115841  /*  85 */ "init_deferred_pred_opt ::= INITIALLY IMMEDIATE",
115842  /*  86 */ "conslist_opt ::=",
115843  /*  87 */ "conslist_opt ::= COMMA conslist",
115844  /*  88 */ "conslist ::= conslist tconscomma tcons",
115845  /*  89 */ "conslist ::= tcons",
115846  /*  90 */ "tconscomma ::= COMMA",
115847  /*  91 */ "tconscomma ::=",
115848  /*  92 */ "tcons ::= CONSTRAINT nm",
115849  /*  93 */ "tcons ::= PRIMARY KEY LP idxlist autoinc RP onconf",
115850  /*  94 */ "tcons ::= UNIQUE LP idxlist RP onconf",
115851  /*  95 */ "tcons ::= CHECK LP expr RP onconf",
115852  /*  96 */ "tcons ::= FOREIGN KEY LP idxlist RP REFERENCES nm idxlist_opt refargs defer_subclause_opt",
115853  /*  97 */ "defer_subclause_opt ::=",
115854  /*  98 */ "defer_subclause_opt ::= defer_subclause",
115855  /*  99 */ "onconf ::=",
115856  /* 100 */ "onconf ::= ON CONFLICT resolvetype",
115857  /* 101 */ "orconf ::=",
115858  /* 102 */ "orconf ::= OR resolvetype",
115859  /* 103 */ "resolvetype ::= raisetype",
115860  /* 104 */ "resolvetype ::= IGNORE",
115861  /* 105 */ "resolvetype ::= REPLACE",
115862  /* 106 */ "cmd ::= DROP TABLE ifexists fullname",
115863  /* 107 */ "ifexists ::= IF EXISTS",
115864  /* 108 */ "ifexists ::=",
115865  /* 109 */ "cmd ::= createkw temp VIEW ifnotexists nm dbnm AS select",
115866  /* 110 */ "cmd ::= DROP VIEW ifexists fullname",
115867  /* 111 */ "cmd ::= select",
115868  /* 112 */ "select ::= with selectnowith",
115869  /* 113 */ "selectnowith ::= oneselect",
115870  /* 114 */ "selectnowith ::= selectnowith multiselect_op oneselect",
115871  /* 115 */ "multiselect_op ::= UNION",
115872  /* 116 */ "multiselect_op ::= UNION ALL",
115873  /* 117 */ "multiselect_op ::= EXCEPT|INTERSECT",
115874  /* 118 */ "oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt orderby_opt limit_opt",
115875  /* 119 */ "oneselect ::= values",
115876  /* 120 */ "values ::= VALUES LP nexprlist RP",
115877  /* 121 */ "values ::= values COMMA LP exprlist RP",
115878  /* 122 */ "distinct ::= DISTINCT",
115879  /* 123 */ "distinct ::= ALL",
115880  /* 124 */ "distinct ::=",
115881  /* 125 */ "sclp ::= selcollist COMMA",
115882  /* 126 */ "sclp ::=",
115883  /* 127 */ "selcollist ::= sclp expr as",
115884  /* 128 */ "selcollist ::= sclp STAR",
115885  /* 129 */ "selcollist ::= sclp nm DOT STAR",
115886  /* 130 */ "as ::= AS nm",
115887  /* 131 */ "as ::= ID|STRING",
115888  /* 132 */ "as ::=",
115889  /* 133 */ "from ::=",
115890  /* 134 */ "from ::= FROM seltablist",
115891  /* 135 */ "stl_prefix ::= seltablist joinop",
115892  /* 136 */ "stl_prefix ::=",
115893  /* 137 */ "seltablist ::= stl_prefix nm dbnm as indexed_opt on_opt using_opt",
115894  /* 138 */ "seltablist ::= stl_prefix LP select RP as on_opt using_opt",
115895  /* 139 */ "seltablist ::= stl_prefix LP seltablist RP as on_opt using_opt",
115896  /* 140 */ "dbnm ::=",
115897  /* 141 */ "dbnm ::= DOT nm",
115898  /* 142 */ "fullname ::= nm dbnm",
115899  /* 143 */ "joinop ::= COMMA|JOIN",
115900  /* 144 */ "joinop ::= JOIN_KW JOIN",
115901  /* 145 */ "joinop ::= JOIN_KW nm JOIN",
115902  /* 146 */ "joinop ::= JOIN_KW nm nm JOIN",
115903  /* 147 */ "on_opt ::= ON expr",
115904  /* 148 */ "on_opt ::=",
115905  /* 149 */ "indexed_opt ::=",
115906  /* 150 */ "indexed_opt ::= INDEXED BY nm",
115907  /* 151 */ "indexed_opt ::= NOT INDEXED",
115908  /* 152 */ "using_opt ::= USING LP idlist RP",
115909  /* 153 */ "using_opt ::=",
115910  /* 154 */ "orderby_opt ::=",
115911  /* 155 */ "orderby_opt ::= ORDER BY sortlist",
115912  /* 156 */ "sortlist ::= sortlist COMMA expr sortorder",
115913  /* 157 */ "sortlist ::= expr sortorder",
115914  /* 158 */ "sortorder ::= ASC",
115915  /* 159 */ "sortorder ::= DESC",
115916  /* 160 */ "sortorder ::=",
115917  /* 161 */ "groupby_opt ::=",
115918  /* 162 */ "groupby_opt ::= GROUP BY nexprlist",
115919  /* 163 */ "having_opt ::=",
115920  /* 164 */ "having_opt ::= HAVING expr",
115921  /* 165 */ "limit_opt ::=",
115922  /* 166 */ "limit_opt ::= LIMIT expr",
115923  /* 167 */ "limit_opt ::= LIMIT expr OFFSET expr",
115924  /* 168 */ "limit_opt ::= LIMIT expr COMMA expr",
115925  /* 169 */ "cmd ::= with DELETE FROM fullname indexed_opt where_opt",
115926  /* 170 */ "where_opt ::=",
115927  /* 171 */ "where_opt ::= WHERE expr",
115928  /* 172 */ "cmd ::= with UPDATE orconf fullname indexed_opt SET setlist where_opt",
115929  /* 173 */ "setlist ::= setlist COMMA nm EQ expr",
115930  /* 174 */ "setlist ::= nm EQ expr",
115931  /* 175 */ "cmd ::= with insert_cmd INTO fullname inscollist_opt select",
115932  /* 176 */ "cmd ::= with insert_cmd INTO fullname inscollist_opt DEFAULT VALUES",
115933  /* 177 */ "insert_cmd ::= INSERT orconf",
115934  /* 178 */ "insert_cmd ::= REPLACE",
115935  /* 179 */ "inscollist_opt ::=",
115936  /* 180 */ "inscollist_opt ::= LP idlist RP",
115937  /* 181 */ "idlist ::= idlist COMMA nm",
115938  /* 182 */ "idlist ::= nm",
115939  /* 183 */ "expr ::= term",
115940  /* 184 */ "expr ::= LP expr RP",
115941  /* 185 */ "term ::= NULL",
115942  /* 186 */ "expr ::= ID|INDEXED",
115943  /* 187 */ "expr ::= JOIN_KW",
115944  /* 188 */ "expr ::= nm DOT nm",
115945  /* 189 */ "expr ::= nm DOT nm DOT nm",
115946  /* 190 */ "term ::= INTEGER|FLOAT|BLOB",
115947  /* 191 */ "term ::= STRING",
115948  /* 192 */ "expr ::= VARIABLE",
115949  /* 193 */ "expr ::= expr COLLATE ID|STRING",
115950  /* 194 */ "expr ::= CAST LP expr AS typetoken RP",
115951  /* 195 */ "expr ::= ID|INDEXED LP distinct exprlist RP",
115952  /* 196 */ "expr ::= ID|INDEXED LP STAR RP",
115953  /* 197 */ "term ::= CTIME_KW",
115954  /* 198 */ "expr ::= expr AND expr",
115955  /* 199 */ "expr ::= expr OR expr",
115956  /* 200 */ "expr ::= expr LT|GT|GE|LE expr",
115957  /* 201 */ "expr ::= expr EQ|NE expr",
115958  /* 202 */ "expr ::= expr BITAND|BITOR|LSHIFT|RSHIFT expr",
115959  /* 203 */ "expr ::= expr PLUS|MINUS expr",
115960  /* 204 */ "expr ::= expr STAR|SLASH|REM expr",
115961  /* 205 */ "expr ::= expr CONCAT expr",
115962  /* 206 */ "likeop ::= LIKE_KW|MATCH",
115963  /* 207 */ "likeop ::= NOT LIKE_KW|MATCH",
115964  /* 208 */ "expr ::= expr likeop expr",
115965  /* 209 */ "expr ::= expr likeop expr ESCAPE expr",
115966  /* 210 */ "expr ::= expr ISNULL|NOTNULL",
115967  /* 211 */ "expr ::= expr NOT NULL",
115968  /* 212 */ "expr ::= expr IS expr",
115969  /* 213 */ "expr ::= expr IS NOT expr",
115970  /* 214 */ "expr ::= NOT expr",
115971  /* 215 */ "expr ::= BITNOT expr",
115972  /* 216 */ "expr ::= MINUS expr",
115973  /* 217 */ "expr ::= PLUS expr",
115974  /* 218 */ "between_op ::= BETWEEN",
115975  /* 219 */ "between_op ::= NOT BETWEEN",
115976  /* 220 */ "expr ::= expr between_op expr AND expr",
115977  /* 221 */ "in_op ::= IN",
115978  /* 222 */ "in_op ::= NOT IN",
115979  /* 223 */ "expr ::= expr in_op LP exprlist RP",
115980  /* 224 */ "expr ::= LP select RP",
115981  /* 225 */ "expr ::= expr in_op LP select RP",
115982  /* 226 */ "expr ::= expr in_op nm dbnm",
115983  /* 227 */ "expr ::= EXISTS LP select RP",
115984  /* 228 */ "expr ::= CASE case_operand case_exprlist case_else END",
115985  /* 229 */ "case_exprlist ::= case_exprlist WHEN expr THEN expr",
115986  /* 230 */ "case_exprlist ::= WHEN expr THEN expr",
115987  /* 231 */ "case_else ::= ELSE expr",
115988  /* 232 */ "case_else ::=",
115989  /* 233 */ "case_operand ::= expr",
115990  /* 234 */ "case_operand ::=",
115991  /* 235 */ "exprlist ::= nexprlist",
115992  /* 236 */ "exprlist ::=",
115993  /* 237 */ "nexprlist ::= nexprlist COMMA expr",
115994  /* 238 */ "nexprlist ::= expr",
115995  /* 239 */ "cmd ::= createkw uniqueflag INDEX ifnotexists nm dbnm ON nm LP idxlist RP where_opt",
115996  /* 240 */ "uniqueflag ::= UNIQUE",
115997  /* 241 */ "uniqueflag ::=",
115998  /* 242 */ "idxlist_opt ::=",
115999  /* 243 */ "idxlist_opt ::= LP idxlist RP",
116000  /* 244 */ "idxlist ::= idxlist COMMA nm collate sortorder",
116001  /* 245 */ "idxlist ::= nm collate sortorder",
116002  /* 246 */ "collate ::=",
116003  /* 247 */ "collate ::= COLLATE ID|STRING",
116004  /* 248 */ "cmd ::= DROP INDEX ifexists fullname",
116005  /* 249 */ "cmd ::= VACUUM",
116006  /* 250 */ "cmd ::= VACUUM nm",
116007  /* 251 */ "cmd ::= PRAGMA nm dbnm",
116008  /* 252 */ "cmd ::= PRAGMA nm dbnm EQ nmnum",
116009  /* 253 */ "cmd ::= PRAGMA nm dbnm LP nmnum RP",
116010  /* 254 */ "cmd ::= PRAGMA nm dbnm EQ minus_num",
116011  /* 255 */ "cmd ::= PRAGMA nm dbnm LP minus_num RP",
116012  /* 256 */ "nmnum ::= plus_num",
116013  /* 257 */ "nmnum ::= nm",
116014  /* 258 */ "nmnum ::= ON",
116015  /* 259 */ "nmnum ::= DELETE",
116016  /* 260 */ "nmnum ::= DEFAULT",
116017  /* 261 */ "plus_num ::= PLUS INTEGER|FLOAT",
116018  /* 262 */ "plus_num ::= INTEGER|FLOAT",
116019  /* 263 */ "minus_num ::= MINUS INTEGER|FLOAT",
116020  /* 264 */ "cmd ::= createkw trigger_decl BEGIN trigger_cmd_list END",
116021  /* 265 */ "trigger_decl ::= temp TRIGGER ifnotexists nm dbnm trigger_time trigger_event ON fullname foreach_clause when_clause",
116022  /* 266 */ "trigger_time ::= BEFORE",
116023  /* 267 */ "trigger_time ::= AFTER",
116024  /* 268 */ "trigger_time ::= INSTEAD OF",
116025  /* 269 */ "trigger_time ::=",
116026  /* 270 */ "trigger_event ::= DELETE|INSERT",
116027  /* 271 */ "trigger_event ::= UPDATE",
116028  /* 272 */ "trigger_event ::= UPDATE OF idlist",
116029  /* 273 */ "foreach_clause ::=",
116030  /* 274 */ "foreach_clause ::= FOR EACH ROW",
116031  /* 275 */ "when_clause ::=",
116032  /* 276 */ "when_clause ::= WHEN expr",
116033  /* 277 */ "trigger_cmd_list ::= trigger_cmd_list trigger_cmd SEMI",
116034  /* 278 */ "trigger_cmd_list ::= trigger_cmd SEMI",
116035  /* 279 */ "trnm ::= nm",
116036  /* 280 */ "trnm ::= nm DOT nm",
116037  /* 281 */ "tridxby ::=",
116038  /* 282 */ "tridxby ::= INDEXED BY nm",
116039  /* 283 */ "tridxby ::= NOT INDEXED",
116040  /* 284 */ "trigger_cmd ::= UPDATE orconf trnm tridxby SET setlist where_opt",
116041  /* 285 */ "trigger_cmd ::= insert_cmd INTO trnm inscollist_opt select",
116042  /* 286 */ "trigger_cmd ::= DELETE FROM trnm tridxby where_opt",
116043  /* 287 */ "trigger_cmd ::= select",
116044  /* 288 */ "expr ::= RAISE LP IGNORE RP",
116045  /* 289 */ "expr ::= RAISE LP raisetype COMMA nm RP",
116046  /* 290 */ "raisetype ::= ROLLBACK",
116047  /* 291 */ "raisetype ::= ABORT",
116048  /* 292 */ "raisetype ::= FAIL",
116049  /* 293 */ "cmd ::= DROP TRIGGER ifexists fullname",
116050  /* 294 */ "cmd ::= ATTACH database_kw_opt expr AS expr key_opt",
116051  /* 295 */ "cmd ::= DETACH database_kw_opt expr",
116052  /* 296 */ "key_opt ::=",
116053  /* 297 */ "key_opt ::= KEY expr",
116054  /* 298 */ "database_kw_opt ::= DATABASE",
116055  /* 299 */ "database_kw_opt ::=",
116056  /* 300 */ "cmd ::= REINDEX",
116057  /* 301 */ "cmd ::= REINDEX nm dbnm",
116058  /* 302 */ "cmd ::= ANALYZE",
116059  /* 303 */ "cmd ::= ANALYZE nm dbnm",
116060  /* 304 */ "cmd ::= ALTER TABLE fullname RENAME TO nm",
116061  /* 305 */ "cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt column",
116062  /* 306 */ "add_column_fullname ::= fullname",
116063  /* 307 */ "kwcolumn_opt ::=",
116064  /* 308 */ "kwcolumn_opt ::= COLUMNKW",
116065  /* 309 */ "cmd ::= create_vtab",
116066  /* 310 */ "cmd ::= create_vtab LP vtabarglist RP",
116067  /* 311 */ "create_vtab ::= createkw VIRTUAL TABLE ifnotexists nm dbnm USING nm",
116068  /* 312 */ "vtabarglist ::= vtabarg",
116069  /* 313 */ "vtabarglist ::= vtabarglist COMMA vtabarg",
116070  /* 314 */ "vtabarg ::=",
116071  /* 315 */ "vtabarg ::= vtabarg vtabargtoken",
116072  /* 316 */ "vtabargtoken ::= ANY",
116073  /* 317 */ "vtabargtoken ::= lp anylist RP",
116074  /* 318 */ "lp ::= LP",
116075  /* 319 */ "anylist ::=",
116076  /* 320 */ "anylist ::= anylist LP anylist RP",
116077  /* 321 */ "anylist ::= anylist ANY",
116078  /* 322 */ "with ::=",
116079  /* 323 */ "with ::= WITH wqlist",
116080  /* 324 */ "with ::= WITH RECURSIVE wqlist",
116081  /* 325 */ "wqlist ::= nm idxlist_opt AS LP select RP",
116082  /* 326 */ "wqlist ::= wqlist COMMA nm idxlist_opt AS LP select RP",
116083 };
116084 #endif /* NDEBUG */
116085 
116086 
116087 #if YYSTACKDEPTH<=0
116088 /*
116089 ** Try to increase the size of the parser stack.
116090 */
116091 static void yyGrowStack(yyParser *p){
116092   int newSize;
116093   yyStackEntry *pNew;
116094 
116095   newSize = p->yystksz*2 + 100;
116096   pNew = realloc(p->yystack, newSize*sizeof(pNew[0]));
116097   if( pNew ){
116098     p->yystack = pNew;
116099     p->yystksz = newSize;
116100 #ifndef NDEBUG
116101     if( yyTraceFILE ){
116102       fprintf(yyTraceFILE,"%sStack grows to %d entries!\n",
116103               yyTracePrompt, p->yystksz);
116104     }
116105 #endif
116106   }
116107 }
116108 #endif
116109 
116110 /*
116111 ** This function allocates a new parser.
116112 ** The only argument is a pointer to a function which works like
116113 ** malloc.
116114 **
116115 ** Inputs:
116116 ** A pointer to the function used to allocate memory.
116117 **
116118 ** Outputs:
116119 ** A pointer to a parser.  This pointer is used in subsequent calls
116120 ** to sqlite3Parser and sqlite3ParserFree.
116121 */
116122 SQLITE_PRIVATE void *sqlite3ParserAlloc(void *(*mallocProc)(size_t)){
116123   yyParser *pParser;
116124   pParser = (yyParser*)(*mallocProc)( (size_t)sizeof(yyParser) );
116125   if( pParser ){
116126     pParser->yyidx = -1;
116127 #ifdef YYTRACKMAXSTACKDEPTH
116128     pParser->yyidxMax = 0;
116129 #endif
116130 #if YYSTACKDEPTH<=0
116131     pParser->yystack = NULL;
116132     pParser->yystksz = 0;
116133     yyGrowStack(pParser);
116134 #endif
116135   }
116136   return pParser;
116137 }
116138 
116139 /* The following function deletes the value associated with a
116140 ** symbol.  The symbol can be either a terminal or nonterminal.
116141 ** "yymajor" is the symbol code, and "yypminor" is a pointer to
116142 ** the value.
116143 */
116144 static void yy_destructor(
116145   yyParser *yypParser,    /* The parser */
116146   YYCODETYPE yymajor,     /* Type code for object to destroy */
116147   YYMINORTYPE *yypminor   /* The object to be destroyed */
116148 ){
116149   sqlite3ParserARG_FETCH;
116150   switch( yymajor ){
116151     /* Here is inserted the actions which take place when a
116152     ** terminal or non-terminal is destroyed.  This can happen
116153     ** when the symbol is popped from the stack during a
116154     ** reduce or during error processing or when a parser is
116155     ** being destroyed before it is finished parsing.
116156     **
116157     ** Note: during a reduce, the only symbols destroyed are those
116158     ** which appear on the RHS of the rule, but which are not used
116159     ** inside the C code.
116160     */
116161     case 163: /* select */
116162     case 195: /* selectnowith */
116163     case 196: /* oneselect */
116164     case 207: /* values */
116165 {
116166 sqlite3SelectDelete(pParse->db, (yypminor->yy3));
116167 }
116168       break;
116169     case 174: /* term */
116170     case 175: /* expr */
116171 {
116172 sqlite3ExprDelete(pParse->db, (yypminor->yy346).pExpr);
116173 }
116174       break;
116175     case 179: /* idxlist_opt */
116176     case 188: /* idxlist */
116177     case 200: /* selcollist */
116178     case 203: /* groupby_opt */
116179     case 205: /* orderby_opt */
116180     case 208: /* nexprlist */
116181     case 209: /* exprlist */
116182     case 210: /* sclp */
116183     case 220: /* sortlist */
116184     case 221: /* setlist */
116185     case 228: /* case_exprlist */
116186 {
116187 sqlite3ExprListDelete(pParse->db, (yypminor->yy14));
116188 }
116189       break;
116190     case 194: /* fullname */
116191     case 201: /* from */
116192     case 212: /* seltablist */
116193     case 213: /* stl_prefix */
116194 {
116195 sqlite3SrcListDelete(pParse->db, (yypminor->yy65));
116196 }
116197       break;
116198     case 197: /* with */
116199     case 252: /* wqlist */
116200 {
116201 sqlite3WithDelete(pParse->db, (yypminor->yy59));
116202 }
116203       break;
116204     case 202: /* where_opt */
116205     case 204: /* having_opt */
116206     case 216: /* on_opt */
116207     case 227: /* case_operand */
116208     case 229: /* case_else */
116209     case 238: /* when_clause */
116210     case 243: /* key_opt */
116211 {
116212 sqlite3ExprDelete(pParse->db, (yypminor->yy132));
116213 }
116214       break;
116215     case 217: /* using_opt */
116216     case 219: /* idlist */
116217     case 223: /* inscollist_opt */
116218 {
116219 sqlite3IdListDelete(pParse->db, (yypminor->yy408));
116220 }
116221       break;
116222     case 234: /* trigger_cmd_list */
116223     case 239: /* trigger_cmd */
116224 {
116225 sqlite3DeleteTriggerStep(pParse->db, (yypminor->yy473));
116226 }
116227       break;
116228     case 236: /* trigger_event */
116229 {
116230 sqlite3IdListDelete(pParse->db, (yypminor->yy378).b);
116231 }
116232       break;
116233     default:  break;   /* If no destructor action specified: do nothing */
116234   }
116235 }
116236 
116237 /*
116238 ** Pop the parser's stack once.
116239 **
116240 ** If there is a destructor routine associated with the token which
116241 ** is popped from the stack, then call it.
116242 **
116243 ** Return the major token number for the symbol popped.
116244 */
116245 static int yy_pop_parser_stack(yyParser *pParser){
116246   YYCODETYPE yymajor;
116247   yyStackEntry *yytos = &pParser->yystack[pParser->yyidx];
116248 
116249   /* There is no mechanism by which the parser stack can be popped below
116250   ** empty in SQLite.  */
116251   if( NEVER(pParser->yyidx<0) ) return 0;
116252 #ifndef NDEBUG
116253   if( yyTraceFILE && pParser->yyidx>=0 ){
116254     fprintf(yyTraceFILE,"%sPopping %s\n",
116255       yyTracePrompt,
116256       yyTokenName[yytos->major]);
116257   }
116258 #endif
116259   yymajor = yytos->major;
116260   yy_destructor(pParser, yymajor, &yytos->minor);
116261   pParser->yyidx--;
116262   return yymajor;
116263 }
116264 
116265 /*
116266 ** Deallocate and destroy a parser.  Destructors are all called for
116267 ** all stack elements before shutting the parser down.
116268 **
116269 ** Inputs:
116270 ** <ul>
116271 ** <li>  A pointer to the parser.  This should be a pointer
116272 **       obtained from sqlite3ParserAlloc.
116273 ** <li>  A pointer to a function used to reclaim memory obtained
116274 **       from malloc.
116275 ** </ul>
116276 */
116277 SQLITE_PRIVATE void sqlite3ParserFree(
116278   void *p,                    /* The parser to be deleted */
116279   void (*freeProc)(void*)     /* Function used to reclaim memory */
116280 ){
116281   yyParser *pParser = (yyParser*)p;
116282   /* In SQLite, we never try to destroy a parser that was not successfully
116283   ** created in the first place. */
116284   if( NEVER(pParser==0) ) return;
116285   while( pParser->yyidx>=0 ) yy_pop_parser_stack(pParser);
116286 #if YYSTACKDEPTH<=0
116287   free(pParser->yystack);
116288 #endif
116289   (*freeProc)((void*)pParser);
116290 }
116291 
116292 /*
116293 ** Return the peak depth of the stack for a parser.
116294 */
116295 #ifdef YYTRACKMAXSTACKDEPTH
116296 SQLITE_PRIVATE int sqlite3ParserStackPeak(void *p){
116297   yyParser *pParser = (yyParser*)p;
116298   return pParser->yyidxMax;
116299 }
116300 #endif
116301 
116302 /*
116303 ** Find the appropriate action for a parser given the terminal
116304 ** look-ahead token iLookAhead.
116305 **
116306 ** If the look-ahead token is YYNOCODE, then check to see if the action is
116307 ** independent of the look-ahead.  If it is, return the action, otherwise
116308 ** return YY_NO_ACTION.
116309 */
116310 static int yy_find_shift_action(
116311   yyParser *pParser,        /* The parser */
116312   YYCODETYPE iLookAhead     /* The look-ahead token */
116313 ){
116314   int i;
116315   int stateno = pParser->yystack[pParser->yyidx].stateno;
116316 
116317   if( stateno>YY_SHIFT_COUNT
116318    || (i = yy_shift_ofst[stateno])==YY_SHIFT_USE_DFLT ){
116319     return yy_default[stateno];
116320   }
116321   assert( iLookAhead!=YYNOCODE );
116322   i += iLookAhead;
116323   if( i<0 || i>=YY_ACTTAB_COUNT || yy_lookahead[i]!=iLookAhead ){
116324     if( iLookAhead>0 ){
116325 #ifdef YYFALLBACK
116326       YYCODETYPE iFallback;            /* Fallback token */
116327       if( iLookAhead<sizeof(yyFallback)/sizeof(yyFallback[0])
116328              && (iFallback = yyFallback[iLookAhead])!=0 ){
116329 #ifndef NDEBUG
116330         if( yyTraceFILE ){
116331           fprintf(yyTraceFILE, "%sFALLBACK %s => %s\n",
116332              yyTracePrompt, yyTokenName[iLookAhead], yyTokenName[iFallback]);
116333         }
116334 #endif
116335         return yy_find_shift_action(pParser, iFallback);
116336       }
116337 #endif
116338 #ifdef YYWILDCARD
116339       {
116340         int j = i - iLookAhead + YYWILDCARD;
116341         if(
116342 #if YY_SHIFT_MIN+YYWILDCARD<0
116343           j>=0 &&
116344 #endif
116345 #if YY_SHIFT_MAX+YYWILDCARD>=YY_ACTTAB_COUNT
116346           j<YY_ACTTAB_COUNT &&
116347 #endif
116348           yy_lookahead[j]==YYWILDCARD
116349         ){
116350 #ifndef NDEBUG
116351           if( yyTraceFILE ){
116352             fprintf(yyTraceFILE, "%sWILDCARD %s => %s\n",
116353                yyTracePrompt, yyTokenName[iLookAhead], yyTokenName[YYWILDCARD]);
116354           }
116355 #endif /* NDEBUG */
116356           return yy_action[j];
116357         }
116358       }
116359 #endif /* YYWILDCARD */
116360     }
116361     return yy_default[stateno];
116362   }else{
116363     return yy_action[i];
116364   }
116365 }
116366 
116367 /*
116368 ** Find the appropriate action for a parser given the non-terminal
116369 ** look-ahead token iLookAhead.
116370 **
116371 ** If the look-ahead token is YYNOCODE, then check to see if the action is
116372 ** independent of the look-ahead.  If it is, return the action, otherwise
116373 ** return YY_NO_ACTION.
116374 */
116375 static int yy_find_reduce_action(
116376   int stateno,              /* Current state number */
116377   YYCODETYPE iLookAhead     /* The look-ahead token */
116378 ){
116379   int i;
116380 #ifdef YYERRORSYMBOL
116381   if( stateno>YY_REDUCE_COUNT ){
116382     return yy_default[stateno];
116383   }
116384 #else
116385   assert( stateno<=YY_REDUCE_COUNT );
116386 #endif
116387   i = yy_reduce_ofst[stateno];
116388   assert( i!=YY_REDUCE_USE_DFLT );
116389   assert( iLookAhead!=YYNOCODE );
116390   i += iLookAhead;
116391 #ifdef YYERRORSYMBOL
116392   if( i<0 || i>=YY_ACTTAB_COUNT || yy_lookahead[i]!=iLookAhead ){
116393     return yy_default[stateno];
116394   }
116395 #else
116396   assert( i>=0 && i<YY_ACTTAB_COUNT );
116397   assert( yy_lookahead[i]==iLookAhead );
116398 #endif
116399   return yy_action[i];
116400 }
116401 
116402 /*
116403 ** The following routine is called if the stack overflows.
116404 */
116405 static void yyStackOverflow(yyParser *yypParser, YYMINORTYPE *yypMinor){
116406    sqlite3ParserARG_FETCH;
116407    yypParser->yyidx--;
116408 #ifndef NDEBUG
116409    if( yyTraceFILE ){
116410      fprintf(yyTraceFILE,"%sStack Overflow!\n",yyTracePrompt);
116411    }
116412 #endif
116413    while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser);
116414    /* Here code is inserted which will execute if the parser
116415    ** stack every overflows */
116416 
116417   UNUSED_PARAMETER(yypMinor); /* Silence some compiler warnings */
116418   sqlite3ErrorMsg(pParse, "parser stack overflow");
116419    sqlite3ParserARG_STORE; /* Suppress warning about unused %extra_argument var */
116420 }
116421 
116422 /*
116423 ** Perform a shift action.
116424 */
116425 static void yy_shift(
116426   yyParser *yypParser,          /* The parser to be shifted */
116427   int yyNewState,               /* The new state to shift in */
116428   int yyMajor,                  /* The major token to shift in */
116429   YYMINORTYPE *yypMinor         /* Pointer to the minor token to shift in */
116430 ){
116431   yyStackEntry *yytos;
116432   yypParser->yyidx++;
116433 #ifdef YYTRACKMAXSTACKDEPTH
116434   if( yypParser->yyidx>yypParser->yyidxMax ){
116435     yypParser->yyidxMax = yypParser->yyidx;
116436   }
116437 #endif
116438 #if YYSTACKDEPTH>0
116439   if( yypParser->yyidx>=YYSTACKDEPTH ){
116440     yyStackOverflow(yypParser, yypMinor);
116441     return;
116442   }
116443 #else
116444   if( yypParser->yyidx>=yypParser->yystksz ){
116445     yyGrowStack(yypParser);
116446     if( yypParser->yyidx>=yypParser->yystksz ){
116447       yyStackOverflow(yypParser, yypMinor);
116448       return;
116449     }
116450   }
116451 #endif
116452   yytos = &yypParser->yystack[yypParser->yyidx];
116453   yytos->stateno = (YYACTIONTYPE)yyNewState;
116454   yytos->major = (YYCODETYPE)yyMajor;
116455   yytos->minor = *yypMinor;
116456 #ifndef NDEBUG
116457   if( yyTraceFILE && yypParser->yyidx>0 ){
116458     int i;
116459     fprintf(yyTraceFILE,"%sShift %d\n",yyTracePrompt,yyNewState);
116460     fprintf(yyTraceFILE,"%sStack:",yyTracePrompt);
116461     for(i=1; i<=yypParser->yyidx; i++)
116462       fprintf(yyTraceFILE," %s",yyTokenName[yypParser->yystack[i].major]);
116463     fprintf(yyTraceFILE,"\n");
116464   }
116465 #endif
116466 }
116467 
116468 /* The following table contains information about every rule that
116469 ** is used during the reduce.
116470 */
116471 static const struct {
116472   YYCODETYPE lhs;         /* Symbol on the left-hand side of the rule */
116473   unsigned char nrhs;     /* Number of right-hand side symbols in the rule */
116474 } yyRuleInfo[] = {
116475   { 144, 1 },
116476   { 145, 2 },
116477   { 145, 1 },
116478   { 146, 1 },
116479   { 146, 3 },
116480   { 147, 0 },
116481   { 147, 1 },
116482   { 147, 3 },
116483   { 148, 1 },
116484   { 149, 3 },
116485   { 151, 0 },
116486   { 151, 1 },
116487   { 151, 2 },
116488   { 150, 0 },
116489   { 150, 1 },
116490   { 150, 1 },
116491   { 150, 1 },
116492   { 149, 2 },
116493   { 149, 2 },
116494   { 149, 2 },
116495   { 153, 1 },
116496   { 153, 0 },
116497   { 149, 2 },
116498   { 149, 3 },
116499   { 149, 5 },
116500   { 149, 2 },
116501   { 154, 6 },
116502   { 156, 1 },
116503   { 158, 0 },
116504   { 158, 3 },
116505   { 157, 1 },
116506   { 157, 0 },
116507   { 155, 5 },
116508   { 155, 2 },
116509   { 162, 0 },
116510   { 162, 2 },
116511   { 160, 3 },
116512   { 160, 1 },
116513   { 164, 3 },
116514   { 165, 1 },
116515   { 152, 1 },
116516   { 152, 1 },
116517   { 152, 1 },
116518   { 166, 0 },
116519   { 166, 1 },
116520   { 168, 1 },
116521   { 168, 4 },
116522   { 168, 6 },
116523   { 169, 1 },
116524   { 169, 2 },
116525   { 170, 1 },
116526   { 170, 1 },
116527   { 167, 2 },
116528   { 167, 0 },
116529   { 173, 2 },
116530   { 173, 2 },
116531   { 173, 4 },
116532   { 173, 3 },
116533   { 173, 3 },
116534   { 173, 2 },
116535   { 173, 2 },
116536   { 173, 3 },
116537   { 173, 5 },
116538   { 173, 2 },
116539   { 173, 4 },
116540   { 173, 4 },
116541   { 173, 1 },
116542   { 173, 2 },
116543   { 178, 0 },
116544   { 178, 1 },
116545   { 180, 0 },
116546   { 180, 2 },
116547   { 182, 2 },
116548   { 182, 3 },
116549   { 182, 3 },
116550   { 182, 3 },
116551   { 183, 2 },
116552   { 183, 2 },
116553   { 183, 1 },
116554   { 183, 1 },
116555   { 183, 2 },
116556   { 181, 3 },
116557   { 181, 2 },
116558   { 184, 0 },
116559   { 184, 2 },
116560   { 184, 2 },
116561   { 161, 0 },
116562   { 161, 2 },
116563   { 185, 3 },
116564   { 185, 1 },
116565   { 186, 1 },
116566   { 186, 0 },
116567   { 187, 2 },
116568   { 187, 7 },
116569   { 187, 5 },
116570   { 187, 5 },
116571   { 187, 10 },
116572   { 189, 0 },
116573   { 189, 1 },
116574   { 176, 0 },
116575   { 176, 3 },
116576   { 190, 0 },
116577   { 190, 2 },
116578   { 191, 1 },
116579   { 191, 1 },
116580   { 191, 1 },
116581   { 149, 4 },
116582   { 193, 2 },
116583   { 193, 0 },
116584   { 149, 8 },
116585   { 149, 4 },
116586   { 149, 1 },
116587   { 163, 2 },
116588   { 195, 1 },
116589   { 195, 3 },
116590   { 198, 1 },
116591   { 198, 2 },
116592   { 198, 1 },
116593   { 196, 9 },
116594   { 196, 1 },
116595   { 207, 4 },
116596   { 207, 5 },
116597   { 199, 1 },
116598   { 199, 1 },
116599   { 199, 0 },
116600   { 210, 2 },
116601   { 210, 0 },
116602   { 200, 3 },
116603   { 200, 2 },
116604   { 200, 4 },
116605   { 211, 2 },
116606   { 211, 1 },
116607   { 211, 0 },
116608   { 201, 0 },
116609   { 201, 2 },
116610   { 213, 2 },
116611   { 213, 0 },
116612   { 212, 7 },
116613   { 212, 7 },
116614   { 212, 7 },
116615   { 159, 0 },
116616   { 159, 2 },
116617   { 194, 2 },
116618   { 214, 1 },
116619   { 214, 2 },
116620   { 214, 3 },
116621   { 214, 4 },
116622   { 216, 2 },
116623   { 216, 0 },
116624   { 215, 0 },
116625   { 215, 3 },
116626   { 215, 2 },
116627   { 217, 4 },
116628   { 217, 0 },
116629   { 205, 0 },
116630   { 205, 3 },
116631   { 220, 4 },
116632   { 220, 2 },
116633   { 177, 1 },
116634   { 177, 1 },
116635   { 177, 0 },
116636   { 203, 0 },
116637   { 203, 3 },
116638   { 204, 0 },
116639   { 204, 2 },
116640   { 206, 0 },
116641   { 206, 2 },
116642   { 206, 4 },
116643   { 206, 4 },
116644   { 149, 6 },
116645   { 202, 0 },
116646   { 202, 2 },
116647   { 149, 8 },
116648   { 221, 5 },
116649   { 221, 3 },
116650   { 149, 6 },
116651   { 149, 7 },
116652   { 222, 2 },
116653   { 222, 1 },
116654   { 223, 0 },
116655   { 223, 3 },
116656   { 219, 3 },
116657   { 219, 1 },
116658   { 175, 1 },
116659   { 175, 3 },
116660   { 174, 1 },
116661   { 175, 1 },
116662   { 175, 1 },
116663   { 175, 3 },
116664   { 175, 5 },
116665   { 174, 1 },
116666   { 174, 1 },
116667   { 175, 1 },
116668   { 175, 3 },
116669   { 175, 6 },
116670   { 175, 5 },
116671   { 175, 4 },
116672   { 174, 1 },
116673   { 175, 3 },
116674   { 175, 3 },
116675   { 175, 3 },
116676   { 175, 3 },
116677   { 175, 3 },
116678   { 175, 3 },
116679   { 175, 3 },
116680   { 175, 3 },
116681   { 224, 1 },
116682   { 224, 2 },
116683   { 175, 3 },
116684   { 175, 5 },
116685   { 175, 2 },
116686   { 175, 3 },
116687   { 175, 3 },
116688   { 175, 4 },
116689   { 175, 2 },
116690   { 175, 2 },
116691   { 175, 2 },
116692   { 175, 2 },
116693   { 225, 1 },
116694   { 225, 2 },
116695   { 175, 5 },
116696   { 226, 1 },
116697   { 226, 2 },
116698   { 175, 5 },
116699   { 175, 3 },
116700   { 175, 5 },
116701   { 175, 4 },
116702   { 175, 4 },
116703   { 175, 5 },
116704   { 228, 5 },
116705   { 228, 4 },
116706   { 229, 2 },
116707   { 229, 0 },
116708   { 227, 1 },
116709   { 227, 0 },
116710   { 209, 1 },
116711   { 209, 0 },
116712   { 208, 3 },
116713   { 208, 1 },
116714   { 149, 12 },
116715   { 230, 1 },
116716   { 230, 0 },
116717   { 179, 0 },
116718   { 179, 3 },
116719   { 188, 5 },
116720   { 188, 3 },
116721   { 231, 0 },
116722   { 231, 2 },
116723   { 149, 4 },
116724   { 149, 1 },
116725   { 149, 2 },
116726   { 149, 3 },
116727   { 149, 5 },
116728   { 149, 6 },
116729   { 149, 5 },
116730   { 149, 6 },
116731   { 232, 1 },
116732   { 232, 1 },
116733   { 232, 1 },
116734   { 232, 1 },
116735   { 232, 1 },
116736   { 171, 2 },
116737   { 171, 1 },
116738   { 172, 2 },
116739   { 149, 5 },
116740   { 233, 11 },
116741   { 235, 1 },
116742   { 235, 1 },
116743   { 235, 2 },
116744   { 235, 0 },
116745   { 236, 1 },
116746   { 236, 1 },
116747   { 236, 3 },
116748   { 237, 0 },
116749   { 237, 3 },
116750   { 238, 0 },
116751   { 238, 2 },
116752   { 234, 3 },
116753   { 234, 2 },
116754   { 240, 1 },
116755   { 240, 3 },
116756   { 241, 0 },
116757   { 241, 3 },
116758   { 241, 2 },
116759   { 239, 7 },
116760   { 239, 5 },
116761   { 239, 5 },
116762   { 239, 1 },
116763   { 175, 4 },
116764   { 175, 6 },
116765   { 192, 1 },
116766   { 192, 1 },
116767   { 192, 1 },
116768   { 149, 4 },
116769   { 149, 6 },
116770   { 149, 3 },
116771   { 243, 0 },
116772   { 243, 2 },
116773   { 242, 1 },
116774   { 242, 0 },
116775   { 149, 1 },
116776   { 149, 3 },
116777   { 149, 1 },
116778   { 149, 3 },
116779   { 149, 6 },
116780   { 149, 6 },
116781   { 244, 1 },
116782   { 245, 0 },
116783   { 245, 1 },
116784   { 149, 1 },
116785   { 149, 4 },
116786   { 246, 8 },
116787   { 247, 1 },
116788   { 247, 3 },
116789   { 248, 0 },
116790   { 248, 2 },
116791   { 249, 1 },
116792   { 249, 3 },
116793   { 250, 1 },
116794   { 251, 0 },
116795   { 251, 4 },
116796   { 251, 2 },
116797   { 197, 0 },
116798   { 197, 2 },
116799   { 197, 3 },
116800   { 252, 6 },
116801   { 252, 8 },
116802 };
116803 
116804 static void yy_accept(yyParser*);  /* Forward Declaration */
116805 
116806 /*
116807 ** Perform a reduce action and the shift that must immediately
116808 ** follow the reduce.
116809 */
116810 static void yy_reduce(
116811   yyParser *yypParser,         /* The parser */
116812   int yyruleno                 /* Number of the rule by which to reduce */
116813 ){
116814   int yygoto;                     /* The next state */
116815   int yyact;                      /* The next action */
116816   YYMINORTYPE yygotominor;        /* The LHS of the rule reduced */
116817   yyStackEntry *yymsp;            /* The top of the parser's stack */
116818   int yysize;                     /* Amount to pop the stack */
116819   sqlite3ParserARG_FETCH;
116820   yymsp = &yypParser->yystack[yypParser->yyidx];
116821 #ifndef NDEBUG
116822   if( yyTraceFILE && yyruleno>=0
116823         && yyruleno<(int)(sizeof(yyRuleName)/sizeof(yyRuleName[0])) ){
116824     fprintf(yyTraceFILE, "%sReduce [%s].\n", yyTracePrompt,
116825       yyRuleName[yyruleno]);
116826   }
116827 #endif /* NDEBUG */
116828 
116829   /* Silence complaints from purify about yygotominor being uninitialized
116830   ** in some cases when it is copied into the stack after the following
116831   ** switch.  yygotominor is uninitialized when a rule reduces that does
116832   ** not set the value of its left-hand side nonterminal.  Leaving the
116833   ** value of the nonterminal uninitialized is utterly harmless as long
116834   ** as the value is never used.  So really the only thing this code
116835   ** accomplishes is to quieten purify.
116836   **
116837   ** 2007-01-16:  The wireshark project (www.wireshark.org) reports that
116838   ** without this code, their parser segfaults.  I'm not sure what there
116839   ** parser is doing to make this happen.  This is the second bug report
116840   ** from wireshark this week.  Clearly they are stressing Lemon in ways
116841   ** that it has not been previously stressed...  (SQLite ticket #2172)
116842   */
116843   /*memset(&yygotominor, 0, sizeof(yygotominor));*/
116844   yygotominor = yyzerominor;
116845 
116846 
116847   switch( yyruleno ){
116848   /* Beginning here are the reduction cases.  A typical example
116849   ** follows:
116850   **   case 0:
116851   **  #line <lineno> <grammarfile>
116852   **     { ... }           // User supplied code
116853   **  #line <lineno> <thisfile>
116854   **     break;
116855   */
116856       case 5: /* explain ::= */
116857 { sqlite3BeginParse(pParse, 0); }
116858         break;
116859       case 6: /* explain ::= EXPLAIN */
116860 { sqlite3BeginParse(pParse, 1); }
116861         break;
116862       case 7: /* explain ::= EXPLAIN QUERY PLAN */
116863 { sqlite3BeginParse(pParse, 2); }
116864         break;
116865       case 8: /* cmdx ::= cmd */
116866 { sqlite3FinishCoding(pParse); }
116867         break;
116868       case 9: /* cmd ::= BEGIN transtype trans_opt */
116869 {sqlite3BeginTransaction(pParse, yymsp[-1].minor.yy328);}
116870         break;
116871       case 13: /* transtype ::= */
116872 {yygotominor.yy328 = TK_DEFERRED;}
116873         break;
116874       case 14: /* transtype ::= DEFERRED */
116875       case 15: /* transtype ::= IMMEDIATE */ yytestcase(yyruleno==15);
116876       case 16: /* transtype ::= EXCLUSIVE */ yytestcase(yyruleno==16);
116877       case 115: /* multiselect_op ::= UNION */ yytestcase(yyruleno==115);
116878       case 117: /* multiselect_op ::= EXCEPT|INTERSECT */ yytestcase(yyruleno==117);
116879 {yygotominor.yy328 = yymsp[0].major;}
116880         break;
116881       case 17: /* cmd ::= COMMIT trans_opt */
116882       case 18: /* cmd ::= END trans_opt */ yytestcase(yyruleno==18);
116883 {sqlite3CommitTransaction(pParse);}
116884         break;
116885       case 19: /* cmd ::= ROLLBACK trans_opt */
116886 {sqlite3RollbackTransaction(pParse);}
116887         break;
116888       case 22: /* cmd ::= SAVEPOINT nm */
116889 {
116890   sqlite3Savepoint(pParse, SAVEPOINT_BEGIN, &yymsp[0].minor.yy0);
116891 }
116892         break;
116893       case 23: /* cmd ::= RELEASE savepoint_opt nm */
116894 {
116895   sqlite3Savepoint(pParse, SAVEPOINT_RELEASE, &yymsp[0].minor.yy0);
116896 }
116897         break;
116898       case 24: /* cmd ::= ROLLBACK trans_opt TO savepoint_opt nm */
116899 {
116900   sqlite3Savepoint(pParse, SAVEPOINT_ROLLBACK, &yymsp[0].minor.yy0);
116901 }
116902         break;
116903       case 26: /* create_table ::= createkw temp TABLE ifnotexists nm dbnm */
116904 {
116905    sqlite3StartTable(pParse,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0,yymsp[-4].minor.yy328,0,0,yymsp[-2].minor.yy328);
116906 }
116907         break;
116908       case 27: /* createkw ::= CREATE */
116909 {
116910   pParse->db->lookaside.bEnabled = 0;
116911   yygotominor.yy0 = yymsp[0].minor.yy0;
116912 }
116913         break;
116914       case 28: /* ifnotexists ::= */
116915       case 31: /* temp ::= */ yytestcase(yyruleno==31);
116916       case 68: /* autoinc ::= */ yytestcase(yyruleno==68);
116917       case 81: /* defer_subclause ::= NOT DEFERRABLE init_deferred_pred_opt */ yytestcase(yyruleno==81);
116918       case 83: /* init_deferred_pred_opt ::= */ yytestcase(yyruleno==83);
116919       case 85: /* init_deferred_pred_opt ::= INITIALLY IMMEDIATE */ yytestcase(yyruleno==85);
116920       case 97: /* defer_subclause_opt ::= */ yytestcase(yyruleno==97);
116921       case 108: /* ifexists ::= */ yytestcase(yyruleno==108);
116922       case 218: /* between_op ::= BETWEEN */ yytestcase(yyruleno==218);
116923       case 221: /* in_op ::= IN */ yytestcase(yyruleno==221);
116924 {yygotominor.yy328 = 0;}
116925         break;
116926       case 29: /* ifnotexists ::= IF NOT EXISTS */
116927       case 30: /* temp ::= TEMP */ yytestcase(yyruleno==30);
116928       case 69: /* autoinc ::= AUTOINCR */ yytestcase(yyruleno==69);
116929       case 84: /* init_deferred_pred_opt ::= INITIALLY DEFERRED */ yytestcase(yyruleno==84);
116930       case 107: /* ifexists ::= IF EXISTS */ yytestcase(yyruleno==107);
116931       case 219: /* between_op ::= NOT BETWEEN */ yytestcase(yyruleno==219);
116932       case 222: /* in_op ::= NOT IN */ yytestcase(yyruleno==222);
116933 {yygotominor.yy328 = 1;}
116934         break;
116935       case 32: /* create_table_args ::= LP columnlist conslist_opt RP table_options */
116936 {
116937   sqlite3EndTable(pParse,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy0,yymsp[0].minor.yy186,0);
116938 }
116939         break;
116940       case 33: /* create_table_args ::= AS select */
116941 {
116942   sqlite3EndTable(pParse,0,0,0,yymsp[0].minor.yy3);
116943   sqlite3SelectDelete(pParse->db, yymsp[0].minor.yy3);
116944 }
116945         break;
116946       case 34: /* table_options ::= */
116947 {yygotominor.yy186 = 0;}
116948         break;
116949       case 35: /* table_options ::= WITHOUT nm */
116950 {
116951   if( yymsp[0].minor.yy0.n==5 && sqlite3_strnicmp(yymsp[0].minor.yy0.z,"rowid",5)==0 ){
116952     yygotominor.yy186 = TF_WithoutRowid;
116953   }else{
116954     yygotominor.yy186 = 0;
116955     sqlite3ErrorMsg(pParse, "unknown table option: %.*s", yymsp[0].minor.yy0.n, yymsp[0].minor.yy0.z);
116956   }
116957 }
116958         break;
116959       case 38: /* column ::= columnid type carglist */
116960 {
116961   yygotominor.yy0.z = yymsp[-2].minor.yy0.z;
116962   yygotominor.yy0.n = (int)(pParse->sLastToken.z-yymsp[-2].minor.yy0.z) + pParse->sLastToken.n;
116963 }
116964         break;
116965       case 39: /* columnid ::= nm */
116966 {
116967   sqlite3AddColumn(pParse,&yymsp[0].minor.yy0);
116968   yygotominor.yy0 = yymsp[0].minor.yy0;
116969   pParse->constraintName.n = 0;
116970 }
116971         break;
116972       case 40: /* nm ::= ID|INDEXED */
116973       case 41: /* nm ::= STRING */ yytestcase(yyruleno==41);
116974       case 42: /* nm ::= JOIN_KW */ yytestcase(yyruleno==42);
116975       case 45: /* typetoken ::= typename */ yytestcase(yyruleno==45);
116976       case 48: /* typename ::= ID|STRING */ yytestcase(yyruleno==48);
116977       case 130: /* as ::= AS nm */ yytestcase(yyruleno==130);
116978       case 131: /* as ::= ID|STRING */ yytestcase(yyruleno==131);
116979       case 141: /* dbnm ::= DOT nm */ yytestcase(yyruleno==141);
116980       case 150: /* indexed_opt ::= INDEXED BY nm */ yytestcase(yyruleno==150);
116981       case 247: /* collate ::= COLLATE ID|STRING */ yytestcase(yyruleno==247);
116982       case 256: /* nmnum ::= plus_num */ yytestcase(yyruleno==256);
116983       case 257: /* nmnum ::= nm */ yytestcase(yyruleno==257);
116984       case 258: /* nmnum ::= ON */ yytestcase(yyruleno==258);
116985       case 259: /* nmnum ::= DELETE */ yytestcase(yyruleno==259);
116986       case 260: /* nmnum ::= DEFAULT */ yytestcase(yyruleno==260);
116987       case 261: /* plus_num ::= PLUS INTEGER|FLOAT */ yytestcase(yyruleno==261);
116988       case 262: /* plus_num ::= INTEGER|FLOAT */ yytestcase(yyruleno==262);
116989       case 263: /* minus_num ::= MINUS INTEGER|FLOAT */ yytestcase(yyruleno==263);
116990       case 279: /* trnm ::= nm */ yytestcase(yyruleno==279);
116991 {yygotominor.yy0 = yymsp[0].minor.yy0;}
116992         break;
116993       case 44: /* type ::= typetoken */
116994 {sqlite3AddColumnType(pParse,&yymsp[0].minor.yy0);}
116995         break;
116996       case 46: /* typetoken ::= typename LP signed RP */
116997 {
116998   yygotominor.yy0.z = yymsp[-3].minor.yy0.z;
116999   yygotominor.yy0.n = (int)(&yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n] - yymsp[-3].minor.yy0.z);
117000 }
117001         break;
117002       case 47: /* typetoken ::= typename LP signed COMMA signed RP */
117003 {
117004   yygotominor.yy0.z = yymsp[-5].minor.yy0.z;
117005   yygotominor.yy0.n = (int)(&yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n] - yymsp[-5].minor.yy0.z);
117006 }
117007         break;
117008       case 49: /* typename ::= typename ID|STRING */
117009 {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);}
117010         break;
117011       case 54: /* ccons ::= CONSTRAINT nm */
117012       case 92: /* tcons ::= CONSTRAINT nm */ yytestcase(yyruleno==92);
117013 {pParse->constraintName = yymsp[0].minor.yy0;}
117014         break;
117015       case 55: /* ccons ::= DEFAULT term */
117016       case 57: /* ccons ::= DEFAULT PLUS term */ yytestcase(yyruleno==57);
117017 {sqlite3AddDefaultValue(pParse,&yymsp[0].minor.yy346);}
117018         break;
117019       case 56: /* ccons ::= DEFAULT LP expr RP */
117020 {sqlite3AddDefaultValue(pParse,&yymsp[-1].minor.yy346);}
117021         break;
117022       case 58: /* ccons ::= DEFAULT MINUS term */
117023 {
117024   ExprSpan v;
117025   v.pExpr = sqlite3PExpr(pParse, TK_UMINUS, yymsp[0].minor.yy346.pExpr, 0, 0);
117026   v.zStart = yymsp[-1].minor.yy0.z;
117027   v.zEnd = yymsp[0].minor.yy346.zEnd;
117028   sqlite3AddDefaultValue(pParse,&v);
117029 }
117030         break;
117031       case 59: /* ccons ::= DEFAULT ID|INDEXED */
117032 {
117033   ExprSpan v;
117034   spanExpr(&v, pParse, TK_STRING, &yymsp[0].minor.yy0);
117035   sqlite3AddDefaultValue(pParse,&v);
117036 }
117037         break;
117038       case 61: /* ccons ::= NOT NULL onconf */
117039 {sqlite3AddNotNull(pParse, yymsp[0].minor.yy328);}
117040         break;
117041       case 62: /* ccons ::= PRIMARY KEY sortorder onconf autoinc */
117042 {sqlite3AddPrimaryKey(pParse,0,yymsp[-1].minor.yy328,yymsp[0].minor.yy328,yymsp[-2].minor.yy328);}
117043         break;
117044       case 63: /* ccons ::= UNIQUE onconf */
117045 {sqlite3CreateIndex(pParse,0,0,0,0,yymsp[0].minor.yy328,0,0,0,0);}
117046         break;
117047       case 64: /* ccons ::= CHECK LP expr RP */
117048 {sqlite3AddCheckConstraint(pParse,yymsp[-1].minor.yy346.pExpr);}
117049         break;
117050       case 65: /* ccons ::= REFERENCES nm idxlist_opt refargs */
117051 {sqlite3CreateForeignKey(pParse,0,&yymsp[-2].minor.yy0,yymsp[-1].minor.yy14,yymsp[0].minor.yy328);}
117052         break;
117053       case 66: /* ccons ::= defer_subclause */
117054 {sqlite3DeferForeignKey(pParse,yymsp[0].minor.yy328);}
117055         break;
117056       case 67: /* ccons ::= COLLATE ID|STRING */
117057 {sqlite3AddCollateType(pParse, &yymsp[0].minor.yy0);}
117058         break;
117059       case 70: /* refargs ::= */
117060 { yygotominor.yy328 = OE_None*0x0101; /* EV: R-19803-45884 */}
117061         break;
117062       case 71: /* refargs ::= refargs refarg */
117063 { yygotominor.yy328 = (yymsp[-1].minor.yy328 & ~yymsp[0].minor.yy429.mask) | yymsp[0].minor.yy429.value; }
117064         break;
117065       case 72: /* refarg ::= MATCH nm */
117066       case 73: /* refarg ::= ON INSERT refact */ yytestcase(yyruleno==73);
117067 { yygotominor.yy429.value = 0;     yygotominor.yy429.mask = 0x000000; }
117068         break;
117069       case 74: /* refarg ::= ON DELETE refact */
117070 { yygotominor.yy429.value = yymsp[0].minor.yy328;     yygotominor.yy429.mask = 0x0000ff; }
117071         break;
117072       case 75: /* refarg ::= ON UPDATE refact */
117073 { yygotominor.yy429.value = yymsp[0].minor.yy328<<8;  yygotominor.yy429.mask = 0x00ff00; }
117074         break;
117075       case 76: /* refact ::= SET NULL */
117076 { yygotominor.yy328 = OE_SetNull;  /* EV: R-33326-45252 */}
117077         break;
117078       case 77: /* refact ::= SET DEFAULT */
117079 { yygotominor.yy328 = OE_SetDflt;  /* EV: R-33326-45252 */}
117080         break;
117081       case 78: /* refact ::= CASCADE */
117082 { yygotominor.yy328 = OE_Cascade;  /* EV: R-33326-45252 */}
117083         break;
117084       case 79: /* refact ::= RESTRICT */
117085 { yygotominor.yy328 = OE_Restrict; /* EV: R-33326-45252 */}
117086         break;
117087       case 80: /* refact ::= NO ACTION */
117088 { yygotominor.yy328 = OE_None;     /* EV: R-33326-45252 */}
117089         break;
117090       case 82: /* defer_subclause ::= DEFERRABLE init_deferred_pred_opt */
117091       case 98: /* defer_subclause_opt ::= defer_subclause */ yytestcase(yyruleno==98);
117092       case 100: /* onconf ::= ON CONFLICT resolvetype */ yytestcase(yyruleno==100);
117093       case 103: /* resolvetype ::= raisetype */ yytestcase(yyruleno==103);
117094 {yygotominor.yy328 = yymsp[0].minor.yy328;}
117095         break;
117096       case 86: /* conslist_opt ::= */
117097 {yygotominor.yy0.n = 0; yygotominor.yy0.z = 0;}
117098         break;
117099       case 87: /* conslist_opt ::= COMMA conslist */
117100 {yygotominor.yy0 = yymsp[-1].minor.yy0;}
117101         break;
117102       case 90: /* tconscomma ::= COMMA */
117103 {pParse->constraintName.n = 0;}
117104         break;
117105       case 93: /* tcons ::= PRIMARY KEY LP idxlist autoinc RP onconf */
117106 {sqlite3AddPrimaryKey(pParse,yymsp[-3].minor.yy14,yymsp[0].minor.yy328,yymsp[-2].minor.yy328,0);}
117107         break;
117108       case 94: /* tcons ::= UNIQUE LP idxlist RP onconf */
117109 {sqlite3CreateIndex(pParse,0,0,0,yymsp[-2].minor.yy14,yymsp[0].minor.yy328,0,0,0,0);}
117110         break;
117111       case 95: /* tcons ::= CHECK LP expr RP onconf */
117112 {sqlite3AddCheckConstraint(pParse,yymsp[-2].minor.yy346.pExpr);}
117113         break;
117114       case 96: /* tcons ::= FOREIGN KEY LP idxlist RP REFERENCES nm idxlist_opt refargs defer_subclause_opt */
117115 {
117116     sqlite3CreateForeignKey(pParse, yymsp[-6].minor.yy14, &yymsp[-3].minor.yy0, yymsp[-2].minor.yy14, yymsp[-1].minor.yy328);
117117     sqlite3DeferForeignKey(pParse, yymsp[0].minor.yy328);
117118 }
117119         break;
117120       case 99: /* onconf ::= */
117121 {yygotominor.yy328 = OE_Default;}
117122         break;
117123       case 101: /* orconf ::= */
117124 {yygotominor.yy186 = OE_Default;}
117125         break;
117126       case 102: /* orconf ::= OR resolvetype */
117127 {yygotominor.yy186 = (u8)yymsp[0].minor.yy328;}
117128         break;
117129       case 104: /* resolvetype ::= IGNORE */
117130 {yygotominor.yy328 = OE_Ignore;}
117131         break;
117132       case 105: /* resolvetype ::= REPLACE */
117133 {yygotominor.yy328 = OE_Replace;}
117134         break;
117135       case 106: /* cmd ::= DROP TABLE ifexists fullname */
117136 {
117137   sqlite3DropTable(pParse, yymsp[0].minor.yy65, 0, yymsp[-1].minor.yy328);
117138 }
117139         break;
117140       case 109: /* cmd ::= createkw temp VIEW ifnotexists nm dbnm AS select */
117141 {
117142   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);
117143 }
117144         break;
117145       case 110: /* cmd ::= DROP VIEW ifexists fullname */
117146 {
117147   sqlite3DropTable(pParse, yymsp[0].minor.yy65, 1, yymsp[-1].minor.yy328);
117148 }
117149         break;
117150       case 111: /* cmd ::= select */
117151 {
117152   SelectDest dest = {SRT_Output, 0, 0, 0, 0, 0};
117153   sqlite3Select(pParse, yymsp[0].minor.yy3, &dest);
117154   sqlite3ExplainBegin(pParse->pVdbe);
117155   sqlite3ExplainSelect(pParse->pVdbe, yymsp[0].minor.yy3);
117156   sqlite3ExplainFinish(pParse->pVdbe);
117157   sqlite3SelectDelete(pParse->db, yymsp[0].minor.yy3);
117158 }
117159         break;
117160       case 112: /* select ::= with selectnowith */
117161 {
117162   if( yymsp[0].minor.yy3 ){
117163     yymsp[0].minor.yy3->pWith = yymsp[-1].minor.yy59;
117164   }else{
117165     sqlite3WithDelete(pParse->db, yymsp[-1].minor.yy59);
117166   }
117167   yygotominor.yy3 = yymsp[0].minor.yy3;
117168 }
117169         break;
117170       case 113: /* selectnowith ::= oneselect */
117171       case 119: /* oneselect ::= values */ yytestcase(yyruleno==119);
117172 {yygotominor.yy3 = yymsp[0].minor.yy3;}
117173         break;
117174       case 114: /* selectnowith ::= selectnowith multiselect_op oneselect */
117175 {
117176   if( yymsp[0].minor.yy3 ){
117177     yymsp[0].minor.yy3->op = (u8)yymsp[-1].minor.yy328;
117178     yymsp[0].minor.yy3->pPrior = yymsp[-2].minor.yy3;
117179     if( yymsp[-1].minor.yy328!=TK_ALL ) pParse->hasCompound = 1;
117180   }else{
117181     sqlite3SelectDelete(pParse->db, yymsp[-2].minor.yy3);
117182   }
117183   yygotominor.yy3 = yymsp[0].minor.yy3;
117184 }
117185         break;
117186       case 116: /* multiselect_op ::= UNION ALL */
117187 {yygotominor.yy328 = TK_ALL;}
117188         break;
117189       case 118: /* oneselect ::= SELECT distinct selcollist from where_opt groupby_opt having_opt orderby_opt limit_opt */
117190 {
117191   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);
117192 }
117193         break;
117194       case 120: /* values ::= VALUES LP nexprlist RP */
117195 {
117196   yygotominor.yy3 = sqlite3SelectNew(pParse,yymsp[-1].minor.yy14,0,0,0,0,0,SF_Values,0,0);
117197 }
117198         break;
117199       case 121: /* values ::= values COMMA LP exprlist RP */
117200 {
117201   Select *pRight = sqlite3SelectNew(pParse,yymsp[-1].minor.yy14,0,0,0,0,0,SF_Values,0,0);
117202   if( pRight ){
117203     pRight->op = TK_ALL;
117204     pRight->pPrior = yymsp[-4].minor.yy3;
117205     yygotominor.yy3 = pRight;
117206   }else{
117207     yygotominor.yy3 = yymsp[-4].minor.yy3;
117208   }
117209 }
117210         break;
117211       case 122: /* distinct ::= DISTINCT */
117212 {yygotominor.yy381 = SF_Distinct;}
117213         break;
117214       case 123: /* distinct ::= ALL */
117215       case 124: /* distinct ::= */ yytestcase(yyruleno==124);
117216 {yygotominor.yy381 = 0;}
117217         break;
117218       case 125: /* sclp ::= selcollist COMMA */
117219       case 243: /* idxlist_opt ::= LP idxlist RP */ yytestcase(yyruleno==243);
117220 {yygotominor.yy14 = yymsp[-1].minor.yy14;}
117221         break;
117222       case 126: /* sclp ::= */
117223       case 154: /* orderby_opt ::= */ yytestcase(yyruleno==154);
117224       case 161: /* groupby_opt ::= */ yytestcase(yyruleno==161);
117225       case 236: /* exprlist ::= */ yytestcase(yyruleno==236);
117226       case 242: /* idxlist_opt ::= */ yytestcase(yyruleno==242);
117227 {yygotominor.yy14 = 0;}
117228         break;
117229       case 127: /* selcollist ::= sclp expr as */
117230 {
117231    yygotominor.yy14 = sqlite3ExprListAppend(pParse, yymsp[-2].minor.yy14, yymsp[-1].minor.yy346.pExpr);
117232    if( yymsp[0].minor.yy0.n>0 ) sqlite3ExprListSetName(pParse, yygotominor.yy14, &yymsp[0].minor.yy0, 1);
117233    sqlite3ExprListSetSpan(pParse,yygotominor.yy14,&yymsp[-1].minor.yy346);
117234 }
117235         break;
117236       case 128: /* selcollist ::= sclp STAR */
117237 {
117238   Expr *p = sqlite3Expr(pParse->db, TK_ALL, 0);
117239   yygotominor.yy14 = sqlite3ExprListAppend(pParse, yymsp[-1].minor.yy14, p);
117240 }
117241         break;
117242       case 129: /* selcollist ::= sclp nm DOT STAR */
117243 {
117244   Expr *pRight = sqlite3PExpr(pParse, TK_ALL, 0, 0, &yymsp[0].minor.yy0);
117245   Expr *pLeft = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[-2].minor.yy0);
117246   Expr *pDot = sqlite3PExpr(pParse, TK_DOT, pLeft, pRight, 0);
117247   yygotominor.yy14 = sqlite3ExprListAppend(pParse,yymsp[-3].minor.yy14, pDot);
117248 }
117249         break;
117250       case 132: /* as ::= */
117251 {yygotominor.yy0.n = 0;}
117252         break;
117253       case 133: /* from ::= */
117254 {yygotominor.yy65 = sqlite3DbMallocZero(pParse->db, sizeof(*yygotominor.yy65));}
117255         break;
117256       case 134: /* from ::= FROM seltablist */
117257 {
117258   yygotominor.yy65 = yymsp[0].minor.yy65;
117259   sqlite3SrcListShiftJoinType(yygotominor.yy65);
117260 }
117261         break;
117262       case 135: /* stl_prefix ::= seltablist joinop */
117263 {
117264    yygotominor.yy65 = yymsp[-1].minor.yy65;
117265    if( ALWAYS(yygotominor.yy65 && yygotominor.yy65->nSrc>0) ) yygotominor.yy65->a[yygotominor.yy65->nSrc-1].jointype = (u8)yymsp[0].minor.yy328;
117266 }
117267         break;
117268       case 136: /* stl_prefix ::= */
117269 {yygotominor.yy65 = 0;}
117270         break;
117271       case 137: /* seltablist ::= stl_prefix nm dbnm as indexed_opt on_opt using_opt */
117272 {
117273   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);
117274   sqlite3SrcListIndexedBy(pParse, yygotominor.yy65, &yymsp[-2].minor.yy0);
117275 }
117276         break;
117277       case 138: /* seltablist ::= stl_prefix LP select RP as on_opt using_opt */
117278 {
117279     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);
117280   }
117281         break;
117282       case 139: /* seltablist ::= stl_prefix LP seltablist RP as on_opt using_opt */
117283 {
117284     if( yymsp[-6].minor.yy65==0 && yymsp[-2].minor.yy0.n==0 && yymsp[-1].minor.yy132==0 && yymsp[0].minor.yy408==0 ){
117285       yygotominor.yy65 = yymsp[-4].minor.yy65;
117286     }else if( yymsp[-4].minor.yy65->nSrc==1 ){
117287       yygotominor.yy65 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-6].minor.yy65,0,0,&yymsp[-2].minor.yy0,0,yymsp[-1].minor.yy132,yymsp[0].minor.yy408);
117288       if( yygotominor.yy65 ){
117289         struct SrcList_item *pNew = &yygotominor.yy65->a[yygotominor.yy65->nSrc-1];
117290         struct SrcList_item *pOld = yymsp[-4].minor.yy65->a;
117291         pNew->zName = pOld->zName;
117292         pNew->zDatabase = pOld->zDatabase;
117293         pNew->pSelect = pOld->pSelect;
117294         pOld->zName = pOld->zDatabase = 0;
117295         pOld->pSelect = 0;
117296       }
117297       sqlite3SrcListDelete(pParse->db, yymsp[-4].minor.yy65);
117298     }else{
117299       Select *pSubquery;
117300       sqlite3SrcListShiftJoinType(yymsp[-4].minor.yy65);
117301       pSubquery = sqlite3SelectNew(pParse,0,yymsp[-4].minor.yy65,0,0,0,0,SF_NestedFrom,0,0);
117302       yygotominor.yy65 = sqlite3SrcListAppendFromTerm(pParse,yymsp[-6].minor.yy65,0,0,&yymsp[-2].minor.yy0,pSubquery,yymsp[-1].minor.yy132,yymsp[0].minor.yy408);
117303     }
117304   }
117305         break;
117306       case 140: /* dbnm ::= */
117307       case 149: /* indexed_opt ::= */ yytestcase(yyruleno==149);
117308 {yygotominor.yy0.z=0; yygotominor.yy0.n=0;}
117309         break;
117310       case 142: /* fullname ::= nm dbnm */
117311 {yygotominor.yy65 = sqlite3SrcListAppend(pParse->db,0,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0);}
117312         break;
117313       case 143: /* joinop ::= COMMA|JOIN */
117314 { yygotominor.yy328 = JT_INNER; }
117315         break;
117316       case 144: /* joinop ::= JOIN_KW JOIN */
117317 { yygotominor.yy328 = sqlite3JoinType(pParse,&yymsp[-1].minor.yy0,0,0); }
117318         break;
117319       case 145: /* joinop ::= JOIN_KW nm JOIN */
117320 { yygotominor.yy328 = sqlite3JoinType(pParse,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy0,0); }
117321         break;
117322       case 146: /* joinop ::= JOIN_KW nm nm JOIN */
117323 { yygotominor.yy328 = sqlite3JoinType(pParse,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy0,&yymsp[-1].minor.yy0); }
117324         break;
117325       case 147: /* on_opt ::= ON expr */
117326       case 164: /* having_opt ::= HAVING expr */ yytestcase(yyruleno==164);
117327       case 171: /* where_opt ::= WHERE expr */ yytestcase(yyruleno==171);
117328       case 231: /* case_else ::= ELSE expr */ yytestcase(yyruleno==231);
117329       case 233: /* case_operand ::= expr */ yytestcase(yyruleno==233);
117330 {yygotominor.yy132 = yymsp[0].minor.yy346.pExpr;}
117331         break;
117332       case 148: /* on_opt ::= */
117333       case 163: /* having_opt ::= */ yytestcase(yyruleno==163);
117334       case 170: /* where_opt ::= */ yytestcase(yyruleno==170);
117335       case 232: /* case_else ::= */ yytestcase(yyruleno==232);
117336       case 234: /* case_operand ::= */ yytestcase(yyruleno==234);
117337 {yygotominor.yy132 = 0;}
117338         break;
117339       case 151: /* indexed_opt ::= NOT INDEXED */
117340 {yygotominor.yy0.z=0; yygotominor.yy0.n=1;}
117341         break;
117342       case 152: /* using_opt ::= USING LP idlist RP */
117343       case 180: /* inscollist_opt ::= LP idlist RP */ yytestcase(yyruleno==180);
117344 {yygotominor.yy408 = yymsp[-1].minor.yy408;}
117345         break;
117346       case 153: /* using_opt ::= */
117347       case 179: /* inscollist_opt ::= */ yytestcase(yyruleno==179);
117348 {yygotominor.yy408 = 0;}
117349         break;
117350       case 155: /* orderby_opt ::= ORDER BY sortlist */
117351       case 162: /* groupby_opt ::= GROUP BY nexprlist */ yytestcase(yyruleno==162);
117352       case 235: /* exprlist ::= nexprlist */ yytestcase(yyruleno==235);
117353 {yygotominor.yy14 = yymsp[0].minor.yy14;}
117354         break;
117355       case 156: /* sortlist ::= sortlist COMMA expr sortorder */
117356 {
117357   yygotominor.yy14 = sqlite3ExprListAppend(pParse,yymsp[-3].minor.yy14,yymsp[-1].minor.yy346.pExpr);
117358   if( yygotominor.yy14 ) yygotominor.yy14->a[yygotominor.yy14->nExpr-1].sortOrder = (u8)yymsp[0].minor.yy328;
117359 }
117360         break;
117361       case 157: /* sortlist ::= expr sortorder */
117362 {
117363   yygotominor.yy14 = sqlite3ExprListAppend(pParse,0,yymsp[-1].minor.yy346.pExpr);
117364   if( yygotominor.yy14 && ALWAYS(yygotominor.yy14->a) ) yygotominor.yy14->a[0].sortOrder = (u8)yymsp[0].minor.yy328;
117365 }
117366         break;
117367       case 158: /* sortorder ::= ASC */
117368       case 160: /* sortorder ::= */ yytestcase(yyruleno==160);
117369 {yygotominor.yy328 = SQLITE_SO_ASC;}
117370         break;
117371       case 159: /* sortorder ::= DESC */
117372 {yygotominor.yy328 = SQLITE_SO_DESC;}
117373         break;
117374       case 165: /* limit_opt ::= */
117375 {yygotominor.yy476.pLimit = 0; yygotominor.yy476.pOffset = 0;}
117376         break;
117377       case 166: /* limit_opt ::= LIMIT expr */
117378 {yygotominor.yy476.pLimit = yymsp[0].minor.yy346.pExpr; yygotominor.yy476.pOffset = 0;}
117379         break;
117380       case 167: /* limit_opt ::= LIMIT expr OFFSET expr */
117381 {yygotominor.yy476.pLimit = yymsp[-2].minor.yy346.pExpr; yygotominor.yy476.pOffset = yymsp[0].minor.yy346.pExpr;}
117382         break;
117383       case 168: /* limit_opt ::= LIMIT expr COMMA expr */
117384 {yygotominor.yy476.pOffset = yymsp[-2].minor.yy346.pExpr; yygotominor.yy476.pLimit = yymsp[0].minor.yy346.pExpr;}
117385         break;
117386       case 169: /* cmd ::= with DELETE FROM fullname indexed_opt where_opt */
117387 {
117388   sqlite3WithPush(pParse, yymsp[-5].minor.yy59, 1);
117389   sqlite3SrcListIndexedBy(pParse, yymsp[-2].minor.yy65, &yymsp[-1].minor.yy0);
117390   sqlite3DeleteFrom(pParse,yymsp[-2].minor.yy65,yymsp[0].minor.yy132);
117391 }
117392         break;
117393       case 172: /* cmd ::= with UPDATE orconf fullname indexed_opt SET setlist where_opt */
117394 {
117395   sqlite3WithPush(pParse, yymsp[-7].minor.yy59, 1);
117396   sqlite3SrcListIndexedBy(pParse, yymsp[-4].minor.yy65, &yymsp[-3].minor.yy0);
117397   sqlite3ExprListCheckLength(pParse,yymsp[-1].minor.yy14,"set list");
117398   sqlite3Update(pParse,yymsp[-4].minor.yy65,yymsp[-1].minor.yy14,yymsp[0].minor.yy132,yymsp[-5].minor.yy186);
117399 }
117400         break;
117401       case 173: /* setlist ::= setlist COMMA nm EQ expr */
117402 {
117403   yygotominor.yy14 = sqlite3ExprListAppend(pParse, yymsp[-4].minor.yy14, yymsp[0].minor.yy346.pExpr);
117404   sqlite3ExprListSetName(pParse, yygotominor.yy14, &yymsp[-2].minor.yy0, 1);
117405 }
117406         break;
117407       case 174: /* setlist ::= nm EQ expr */
117408 {
117409   yygotominor.yy14 = sqlite3ExprListAppend(pParse, 0, yymsp[0].minor.yy346.pExpr);
117410   sqlite3ExprListSetName(pParse, yygotominor.yy14, &yymsp[-2].minor.yy0, 1);
117411 }
117412         break;
117413       case 175: /* cmd ::= with insert_cmd INTO fullname inscollist_opt select */
117414 {
117415   sqlite3WithPush(pParse, yymsp[-5].minor.yy59, 1);
117416   sqlite3Insert(pParse, yymsp[-2].minor.yy65, yymsp[0].minor.yy3, yymsp[-1].minor.yy408, yymsp[-4].minor.yy186);
117417 }
117418         break;
117419       case 176: /* cmd ::= with insert_cmd INTO fullname inscollist_opt DEFAULT VALUES */
117420 {
117421   sqlite3WithPush(pParse, yymsp[-6].minor.yy59, 1);
117422   sqlite3Insert(pParse, yymsp[-3].minor.yy65, 0, yymsp[-2].minor.yy408, yymsp[-5].minor.yy186);
117423 }
117424         break;
117425       case 177: /* insert_cmd ::= INSERT orconf */
117426 {yygotominor.yy186 = yymsp[0].minor.yy186;}
117427         break;
117428       case 178: /* insert_cmd ::= REPLACE */
117429 {yygotominor.yy186 = OE_Replace;}
117430         break;
117431       case 181: /* idlist ::= idlist COMMA nm */
117432 {yygotominor.yy408 = sqlite3IdListAppend(pParse->db,yymsp[-2].minor.yy408,&yymsp[0].minor.yy0);}
117433         break;
117434       case 182: /* idlist ::= nm */
117435 {yygotominor.yy408 = sqlite3IdListAppend(pParse->db,0,&yymsp[0].minor.yy0);}
117436         break;
117437       case 183: /* expr ::= term */
117438 {yygotominor.yy346 = yymsp[0].minor.yy346;}
117439         break;
117440       case 184: /* expr ::= LP expr RP */
117441 {yygotominor.yy346.pExpr = yymsp[-1].minor.yy346.pExpr; spanSet(&yygotominor.yy346,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0);}
117442         break;
117443       case 185: /* term ::= NULL */
117444       case 190: /* term ::= INTEGER|FLOAT|BLOB */ yytestcase(yyruleno==190);
117445       case 191: /* term ::= STRING */ yytestcase(yyruleno==191);
117446 {spanExpr(&yygotominor.yy346, pParse, yymsp[0].major, &yymsp[0].minor.yy0);}
117447         break;
117448       case 186: /* expr ::= ID|INDEXED */
117449       case 187: /* expr ::= JOIN_KW */ yytestcase(yyruleno==187);
117450 {spanExpr(&yygotominor.yy346, pParse, TK_ID, &yymsp[0].minor.yy0);}
117451         break;
117452       case 188: /* expr ::= nm DOT nm */
117453 {
117454   Expr *temp1 = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[-2].minor.yy0);
117455   Expr *temp2 = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[0].minor.yy0);
117456   yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_DOT, temp1, temp2, 0);
117457   spanSet(&yygotominor.yy346,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0);
117458 }
117459         break;
117460       case 189: /* expr ::= nm DOT nm DOT nm */
117461 {
117462   Expr *temp1 = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[-4].minor.yy0);
117463   Expr *temp2 = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[-2].minor.yy0);
117464   Expr *temp3 = sqlite3PExpr(pParse, TK_ID, 0, 0, &yymsp[0].minor.yy0);
117465   Expr *temp4 = sqlite3PExpr(pParse, TK_DOT, temp2, temp3, 0);
117466   yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_DOT, temp1, temp4, 0);
117467   spanSet(&yygotominor.yy346,&yymsp[-4].minor.yy0,&yymsp[0].minor.yy0);
117468 }
117469         break;
117470       case 192: /* expr ::= VARIABLE */
117471 {
117472   if( yymsp[0].minor.yy0.n>=2 && yymsp[0].minor.yy0.z[0]=='#' && sqlite3Isdigit(yymsp[0].minor.yy0.z[1]) ){
117473     /* When doing a nested parse, one can include terms in an expression
117474     ** that look like this:   #1 #2 ...  These terms refer to registers
117475     ** in the virtual machine.  #N is the N-th register. */
117476     if( pParse->nested==0 ){
117477       sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &yymsp[0].minor.yy0);
117478       yygotominor.yy346.pExpr = 0;
117479     }else{
117480       yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_REGISTER, 0, 0, &yymsp[0].minor.yy0);
117481       if( yygotominor.yy346.pExpr ) sqlite3GetInt32(&yymsp[0].minor.yy0.z[1], &yygotominor.yy346.pExpr->iTable);
117482     }
117483   }else{
117484     spanExpr(&yygotominor.yy346, pParse, TK_VARIABLE, &yymsp[0].minor.yy0);
117485     sqlite3ExprAssignVarNumber(pParse, yygotominor.yy346.pExpr);
117486   }
117487   spanSet(&yygotominor.yy346, &yymsp[0].minor.yy0, &yymsp[0].minor.yy0);
117488 }
117489         break;
117490       case 193: /* expr ::= expr COLLATE ID|STRING */
117491 {
117492   yygotominor.yy346.pExpr = sqlite3ExprAddCollateToken(pParse, yymsp[-2].minor.yy346.pExpr, &yymsp[0].minor.yy0);
117493   yygotominor.yy346.zStart = yymsp[-2].minor.yy346.zStart;
117494   yygotominor.yy346.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
117495 }
117496         break;
117497       case 194: /* expr ::= CAST LP expr AS typetoken RP */
117498 {
117499   yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_CAST, yymsp[-3].minor.yy346.pExpr, 0, &yymsp[-1].minor.yy0);
117500   spanSet(&yygotominor.yy346,&yymsp[-5].minor.yy0,&yymsp[0].minor.yy0);
117501 }
117502         break;
117503       case 195: /* expr ::= ID|INDEXED LP distinct exprlist RP */
117504 {
117505   if( yymsp[-1].minor.yy14 && yymsp[-1].minor.yy14->nExpr>pParse->db->aLimit[SQLITE_LIMIT_FUNCTION_ARG] ){
117506     sqlite3ErrorMsg(pParse, "too many arguments on function %T", &yymsp[-4].minor.yy0);
117507   }
117508   yygotominor.yy346.pExpr = sqlite3ExprFunction(pParse, yymsp[-1].minor.yy14, &yymsp[-4].minor.yy0);
117509   spanSet(&yygotominor.yy346,&yymsp[-4].minor.yy0,&yymsp[0].minor.yy0);
117510   if( yymsp[-2].minor.yy381 && yygotominor.yy346.pExpr ){
117511     yygotominor.yy346.pExpr->flags |= EP_Distinct;
117512   }
117513 }
117514         break;
117515       case 196: /* expr ::= ID|INDEXED LP STAR RP */
117516 {
117517   yygotominor.yy346.pExpr = sqlite3ExprFunction(pParse, 0, &yymsp[-3].minor.yy0);
117518   spanSet(&yygotominor.yy346,&yymsp[-3].minor.yy0,&yymsp[0].minor.yy0);
117519 }
117520         break;
117521       case 197: /* term ::= CTIME_KW */
117522 {
117523   yygotominor.yy346.pExpr = sqlite3ExprFunction(pParse, 0, &yymsp[0].minor.yy0);
117524   spanSet(&yygotominor.yy346, &yymsp[0].minor.yy0, &yymsp[0].minor.yy0);
117525 }
117526         break;
117527       case 198: /* expr ::= expr AND expr */
117528       case 199: /* expr ::= expr OR expr */ yytestcase(yyruleno==199);
117529       case 200: /* expr ::= expr LT|GT|GE|LE expr */ yytestcase(yyruleno==200);
117530       case 201: /* expr ::= expr EQ|NE expr */ yytestcase(yyruleno==201);
117531       case 202: /* expr ::= expr BITAND|BITOR|LSHIFT|RSHIFT expr */ yytestcase(yyruleno==202);
117532       case 203: /* expr ::= expr PLUS|MINUS expr */ yytestcase(yyruleno==203);
117533       case 204: /* expr ::= expr STAR|SLASH|REM expr */ yytestcase(yyruleno==204);
117534       case 205: /* expr ::= expr CONCAT expr */ yytestcase(yyruleno==205);
117535 {spanBinaryExpr(&yygotominor.yy346,pParse,yymsp[-1].major,&yymsp[-2].minor.yy346,&yymsp[0].minor.yy346);}
117536         break;
117537       case 206: /* likeop ::= LIKE_KW|MATCH */
117538 {yygotominor.yy96.eOperator = yymsp[0].minor.yy0; yygotominor.yy96.bNot = 0;}
117539         break;
117540       case 207: /* likeop ::= NOT LIKE_KW|MATCH */
117541 {yygotominor.yy96.eOperator = yymsp[0].minor.yy0; yygotominor.yy96.bNot = 1;}
117542         break;
117543       case 208: /* expr ::= expr likeop expr */
117544 {
117545   ExprList *pList;
117546   pList = sqlite3ExprListAppend(pParse,0, yymsp[0].minor.yy346.pExpr);
117547   pList = sqlite3ExprListAppend(pParse,pList, yymsp[-2].minor.yy346.pExpr);
117548   yygotominor.yy346.pExpr = sqlite3ExprFunction(pParse, pList, &yymsp[-1].minor.yy96.eOperator);
117549   if( yymsp[-1].minor.yy96.bNot ) yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_NOT, yygotominor.yy346.pExpr, 0, 0);
117550   yygotominor.yy346.zStart = yymsp[-2].minor.yy346.zStart;
117551   yygotominor.yy346.zEnd = yymsp[0].minor.yy346.zEnd;
117552   if( yygotominor.yy346.pExpr ) yygotominor.yy346.pExpr->flags |= EP_InfixFunc;
117553 }
117554         break;
117555       case 209: /* expr ::= expr likeop expr ESCAPE expr */
117556 {
117557   ExprList *pList;
117558   pList = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy346.pExpr);
117559   pList = sqlite3ExprListAppend(pParse,pList, yymsp[-4].minor.yy346.pExpr);
117560   pList = sqlite3ExprListAppend(pParse,pList, yymsp[0].minor.yy346.pExpr);
117561   yygotominor.yy346.pExpr = sqlite3ExprFunction(pParse, pList, &yymsp[-3].minor.yy96.eOperator);
117562   if( yymsp[-3].minor.yy96.bNot ) yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_NOT, yygotominor.yy346.pExpr, 0, 0);
117563   yygotominor.yy346.zStart = yymsp[-4].minor.yy346.zStart;
117564   yygotominor.yy346.zEnd = yymsp[0].minor.yy346.zEnd;
117565   if( yygotominor.yy346.pExpr ) yygotominor.yy346.pExpr->flags |= EP_InfixFunc;
117566 }
117567         break;
117568       case 210: /* expr ::= expr ISNULL|NOTNULL */
117569 {spanUnaryPostfix(&yygotominor.yy346,pParse,yymsp[0].major,&yymsp[-1].minor.yy346,&yymsp[0].minor.yy0);}
117570         break;
117571       case 211: /* expr ::= expr NOT NULL */
117572 {spanUnaryPostfix(&yygotominor.yy346,pParse,TK_NOTNULL,&yymsp[-2].minor.yy346,&yymsp[0].minor.yy0);}
117573         break;
117574       case 212: /* expr ::= expr IS expr */
117575 {
117576   spanBinaryExpr(&yygotominor.yy346,pParse,TK_IS,&yymsp[-2].minor.yy346,&yymsp[0].minor.yy346);
117577   binaryToUnaryIfNull(pParse, yymsp[0].minor.yy346.pExpr, yygotominor.yy346.pExpr, TK_ISNULL);
117578 }
117579         break;
117580       case 213: /* expr ::= expr IS NOT expr */
117581 {
117582   spanBinaryExpr(&yygotominor.yy346,pParse,TK_ISNOT,&yymsp[-3].minor.yy346,&yymsp[0].minor.yy346);
117583   binaryToUnaryIfNull(pParse, yymsp[0].minor.yy346.pExpr, yygotominor.yy346.pExpr, TK_NOTNULL);
117584 }
117585         break;
117586       case 214: /* expr ::= NOT expr */
117587       case 215: /* expr ::= BITNOT expr */ yytestcase(yyruleno==215);
117588 {spanUnaryPrefix(&yygotominor.yy346,pParse,yymsp[-1].major,&yymsp[0].minor.yy346,&yymsp[-1].minor.yy0);}
117589         break;
117590       case 216: /* expr ::= MINUS expr */
117591 {spanUnaryPrefix(&yygotominor.yy346,pParse,TK_UMINUS,&yymsp[0].minor.yy346,&yymsp[-1].minor.yy0);}
117592         break;
117593       case 217: /* expr ::= PLUS expr */
117594 {spanUnaryPrefix(&yygotominor.yy346,pParse,TK_UPLUS,&yymsp[0].minor.yy346,&yymsp[-1].minor.yy0);}
117595         break;
117596       case 220: /* expr ::= expr between_op expr AND expr */
117597 {
117598   ExprList *pList = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy346.pExpr);
117599   pList = sqlite3ExprListAppend(pParse,pList, yymsp[0].minor.yy346.pExpr);
117600   yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_BETWEEN, yymsp[-4].minor.yy346.pExpr, 0, 0);
117601   if( yygotominor.yy346.pExpr ){
117602     yygotominor.yy346.pExpr->x.pList = pList;
117603   }else{
117604     sqlite3ExprListDelete(pParse->db, pList);
117605   }
117606   if( yymsp[-3].minor.yy328 ) yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_NOT, yygotominor.yy346.pExpr, 0, 0);
117607   yygotominor.yy346.zStart = yymsp[-4].minor.yy346.zStart;
117608   yygotominor.yy346.zEnd = yymsp[0].minor.yy346.zEnd;
117609 }
117610         break;
117611       case 223: /* expr ::= expr in_op LP exprlist RP */
117612 {
117613     if( yymsp[-1].minor.yy14==0 ){
117614       /* Expressions of the form
117615       **
117616       **      expr1 IN ()
117617       **      expr1 NOT IN ()
117618       **
117619       ** simplify to constants 0 (false) and 1 (true), respectively,
117620       ** regardless of the value of expr1.
117621       */
117622       yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_INTEGER, 0, 0, &sqlite3IntTokens[yymsp[-3].minor.yy328]);
117623       sqlite3ExprDelete(pParse->db, yymsp[-4].minor.yy346.pExpr);
117624     }else{
117625       yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_IN, yymsp[-4].minor.yy346.pExpr, 0, 0);
117626       if( yygotominor.yy346.pExpr ){
117627         yygotominor.yy346.pExpr->x.pList = yymsp[-1].minor.yy14;
117628         sqlite3ExprSetHeight(pParse, yygotominor.yy346.pExpr);
117629       }else{
117630         sqlite3ExprListDelete(pParse->db, yymsp[-1].minor.yy14);
117631       }
117632       if( yymsp[-3].minor.yy328 ) yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_NOT, yygotominor.yy346.pExpr, 0, 0);
117633     }
117634     yygotominor.yy346.zStart = yymsp[-4].minor.yy346.zStart;
117635     yygotominor.yy346.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
117636   }
117637         break;
117638       case 224: /* expr ::= LP select RP */
117639 {
117640     yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_SELECT, 0, 0, 0);
117641     if( yygotominor.yy346.pExpr ){
117642       yygotominor.yy346.pExpr->x.pSelect = yymsp[-1].minor.yy3;
117643       ExprSetProperty(yygotominor.yy346.pExpr, EP_xIsSelect);
117644       sqlite3ExprSetHeight(pParse, yygotominor.yy346.pExpr);
117645     }else{
117646       sqlite3SelectDelete(pParse->db, yymsp[-1].minor.yy3);
117647     }
117648     yygotominor.yy346.zStart = yymsp[-2].minor.yy0.z;
117649     yygotominor.yy346.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
117650   }
117651         break;
117652       case 225: /* expr ::= expr in_op LP select RP */
117653 {
117654     yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_IN, yymsp[-4].minor.yy346.pExpr, 0, 0);
117655     if( yygotominor.yy346.pExpr ){
117656       yygotominor.yy346.pExpr->x.pSelect = yymsp[-1].minor.yy3;
117657       ExprSetProperty(yygotominor.yy346.pExpr, EP_xIsSelect);
117658       sqlite3ExprSetHeight(pParse, yygotominor.yy346.pExpr);
117659     }else{
117660       sqlite3SelectDelete(pParse->db, yymsp[-1].minor.yy3);
117661     }
117662     if( yymsp[-3].minor.yy328 ) yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_NOT, yygotominor.yy346.pExpr, 0, 0);
117663     yygotominor.yy346.zStart = yymsp[-4].minor.yy346.zStart;
117664     yygotominor.yy346.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
117665   }
117666         break;
117667       case 226: /* expr ::= expr in_op nm dbnm */
117668 {
117669     SrcList *pSrc = sqlite3SrcListAppend(pParse->db, 0,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0);
117670     yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_IN, yymsp[-3].minor.yy346.pExpr, 0, 0);
117671     if( yygotominor.yy346.pExpr ){
117672       yygotominor.yy346.pExpr->x.pSelect = sqlite3SelectNew(pParse, 0,pSrc,0,0,0,0,0,0,0);
117673       ExprSetProperty(yygotominor.yy346.pExpr, EP_xIsSelect);
117674       sqlite3ExprSetHeight(pParse, yygotominor.yy346.pExpr);
117675     }else{
117676       sqlite3SrcListDelete(pParse->db, pSrc);
117677     }
117678     if( yymsp[-2].minor.yy328 ) yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_NOT, yygotominor.yy346.pExpr, 0, 0);
117679     yygotominor.yy346.zStart = yymsp[-3].minor.yy346.zStart;
117680     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];
117681   }
117682         break;
117683       case 227: /* expr ::= EXISTS LP select RP */
117684 {
117685     Expr *p = yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_EXISTS, 0, 0, 0);
117686     if( p ){
117687       p->x.pSelect = yymsp[-1].minor.yy3;
117688       ExprSetProperty(p, EP_xIsSelect);
117689       sqlite3ExprSetHeight(pParse, p);
117690     }else{
117691       sqlite3SelectDelete(pParse->db, yymsp[-1].minor.yy3);
117692     }
117693     yygotominor.yy346.zStart = yymsp[-3].minor.yy0.z;
117694     yygotominor.yy346.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
117695   }
117696         break;
117697       case 228: /* expr ::= CASE case_operand case_exprlist case_else END */
117698 {
117699   yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_CASE, yymsp[-3].minor.yy132, 0, 0);
117700   if( yygotominor.yy346.pExpr ){
117701     yygotominor.yy346.pExpr->x.pList = yymsp[-1].minor.yy132 ? sqlite3ExprListAppend(pParse,yymsp[-2].minor.yy14,yymsp[-1].minor.yy132) : yymsp[-2].minor.yy14;
117702     sqlite3ExprSetHeight(pParse, yygotominor.yy346.pExpr);
117703   }else{
117704     sqlite3ExprListDelete(pParse->db, yymsp[-2].minor.yy14);
117705     sqlite3ExprDelete(pParse->db, yymsp[-1].minor.yy132);
117706   }
117707   yygotominor.yy346.zStart = yymsp[-4].minor.yy0.z;
117708   yygotominor.yy346.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
117709 }
117710         break;
117711       case 229: /* case_exprlist ::= case_exprlist WHEN expr THEN expr */
117712 {
117713   yygotominor.yy14 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy14, yymsp[-2].minor.yy346.pExpr);
117714   yygotominor.yy14 = sqlite3ExprListAppend(pParse,yygotominor.yy14, yymsp[0].minor.yy346.pExpr);
117715 }
117716         break;
117717       case 230: /* case_exprlist ::= WHEN expr THEN expr */
117718 {
117719   yygotominor.yy14 = sqlite3ExprListAppend(pParse,0, yymsp[-2].minor.yy346.pExpr);
117720   yygotominor.yy14 = sqlite3ExprListAppend(pParse,yygotominor.yy14, yymsp[0].minor.yy346.pExpr);
117721 }
117722         break;
117723       case 237: /* nexprlist ::= nexprlist COMMA expr */
117724 {yygotominor.yy14 = sqlite3ExprListAppend(pParse,yymsp[-2].minor.yy14,yymsp[0].minor.yy346.pExpr);}
117725         break;
117726       case 238: /* nexprlist ::= expr */
117727 {yygotominor.yy14 = sqlite3ExprListAppend(pParse,0,yymsp[0].minor.yy346.pExpr);}
117728         break;
117729       case 239: /* cmd ::= createkw uniqueflag INDEX ifnotexists nm dbnm ON nm LP idxlist RP where_opt */
117730 {
117731   sqlite3CreateIndex(pParse, &yymsp[-7].minor.yy0, &yymsp[-6].minor.yy0,
117732                      sqlite3SrcListAppend(pParse->db,0,&yymsp[-4].minor.yy0,0), yymsp[-2].minor.yy14, yymsp[-10].minor.yy328,
117733                       &yymsp[-11].minor.yy0, yymsp[0].minor.yy132, SQLITE_SO_ASC, yymsp[-8].minor.yy328);
117734 }
117735         break;
117736       case 240: /* uniqueflag ::= UNIQUE */
117737       case 291: /* raisetype ::= ABORT */ yytestcase(yyruleno==291);
117738 {yygotominor.yy328 = OE_Abort;}
117739         break;
117740       case 241: /* uniqueflag ::= */
117741 {yygotominor.yy328 = OE_None;}
117742         break;
117743       case 244: /* idxlist ::= idxlist COMMA nm collate sortorder */
117744 {
117745   Expr *p = sqlite3ExprAddCollateToken(pParse, 0, &yymsp[-1].minor.yy0);
117746   yygotominor.yy14 = sqlite3ExprListAppend(pParse,yymsp[-4].minor.yy14, p);
117747   sqlite3ExprListSetName(pParse,yygotominor.yy14,&yymsp[-2].minor.yy0,1);
117748   sqlite3ExprListCheckLength(pParse, yygotominor.yy14, "index");
117749   if( yygotominor.yy14 ) yygotominor.yy14->a[yygotominor.yy14->nExpr-1].sortOrder = (u8)yymsp[0].minor.yy328;
117750 }
117751         break;
117752       case 245: /* idxlist ::= nm collate sortorder */
117753 {
117754   Expr *p = sqlite3ExprAddCollateToken(pParse, 0, &yymsp[-1].minor.yy0);
117755   yygotominor.yy14 = sqlite3ExprListAppend(pParse,0, p);
117756   sqlite3ExprListSetName(pParse, yygotominor.yy14, &yymsp[-2].minor.yy0, 1);
117757   sqlite3ExprListCheckLength(pParse, yygotominor.yy14, "index");
117758   if( yygotominor.yy14 ) yygotominor.yy14->a[yygotominor.yy14->nExpr-1].sortOrder = (u8)yymsp[0].minor.yy328;
117759 }
117760         break;
117761       case 246: /* collate ::= */
117762 {yygotominor.yy0.z = 0; yygotominor.yy0.n = 0;}
117763         break;
117764       case 248: /* cmd ::= DROP INDEX ifexists fullname */
117765 {sqlite3DropIndex(pParse, yymsp[0].minor.yy65, yymsp[-1].minor.yy328);}
117766         break;
117767       case 249: /* cmd ::= VACUUM */
117768       case 250: /* cmd ::= VACUUM nm */ yytestcase(yyruleno==250);
117769 {sqlite3Vacuum(pParse);}
117770         break;
117771       case 251: /* cmd ::= PRAGMA nm dbnm */
117772 {sqlite3Pragma(pParse,&yymsp[-1].minor.yy0,&yymsp[0].minor.yy0,0,0);}
117773         break;
117774       case 252: /* cmd ::= PRAGMA nm dbnm EQ nmnum */
117775 {sqlite3Pragma(pParse,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0,0);}
117776         break;
117777       case 253: /* cmd ::= PRAGMA nm dbnm LP nmnum RP */
117778 {sqlite3Pragma(pParse,&yymsp[-4].minor.yy0,&yymsp[-3].minor.yy0,&yymsp[-1].minor.yy0,0);}
117779         break;
117780       case 254: /* cmd ::= PRAGMA nm dbnm EQ minus_num */
117781 {sqlite3Pragma(pParse,&yymsp[-3].minor.yy0,&yymsp[-2].minor.yy0,&yymsp[0].minor.yy0,1);}
117782         break;
117783       case 255: /* cmd ::= PRAGMA nm dbnm LP minus_num RP */
117784 {sqlite3Pragma(pParse,&yymsp[-4].minor.yy0,&yymsp[-3].minor.yy0,&yymsp[-1].minor.yy0,1);}
117785         break;
117786       case 264: /* cmd ::= createkw trigger_decl BEGIN trigger_cmd_list END */
117787 {
117788   Token all;
117789   all.z = yymsp[-3].minor.yy0.z;
117790   all.n = (int)(yymsp[0].minor.yy0.z - yymsp[-3].minor.yy0.z) + yymsp[0].minor.yy0.n;
117791   sqlite3FinishTrigger(pParse, yymsp[-1].minor.yy473, &all);
117792 }
117793         break;
117794       case 265: /* trigger_decl ::= temp TRIGGER ifnotexists nm dbnm trigger_time trigger_event ON fullname foreach_clause when_clause */
117795 {
117796   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);
117797   yygotominor.yy0 = (yymsp[-6].minor.yy0.n==0?yymsp[-7].minor.yy0:yymsp[-6].minor.yy0);
117798 }
117799         break;
117800       case 266: /* trigger_time ::= BEFORE */
117801       case 269: /* trigger_time ::= */ yytestcase(yyruleno==269);
117802 { yygotominor.yy328 = TK_BEFORE; }
117803         break;
117804       case 267: /* trigger_time ::= AFTER */
117805 { yygotominor.yy328 = TK_AFTER;  }
117806         break;
117807       case 268: /* trigger_time ::= INSTEAD OF */
117808 { yygotominor.yy328 = TK_INSTEAD;}
117809         break;
117810       case 270: /* trigger_event ::= DELETE|INSERT */
117811       case 271: /* trigger_event ::= UPDATE */ yytestcase(yyruleno==271);
117812 {yygotominor.yy378.a = yymsp[0].major; yygotominor.yy378.b = 0;}
117813         break;
117814       case 272: /* trigger_event ::= UPDATE OF idlist */
117815 {yygotominor.yy378.a = TK_UPDATE; yygotominor.yy378.b = yymsp[0].minor.yy408;}
117816         break;
117817       case 275: /* when_clause ::= */
117818       case 296: /* key_opt ::= */ yytestcase(yyruleno==296);
117819 { yygotominor.yy132 = 0; }
117820         break;
117821       case 276: /* when_clause ::= WHEN expr */
117822       case 297: /* key_opt ::= KEY expr */ yytestcase(yyruleno==297);
117823 { yygotominor.yy132 = yymsp[0].minor.yy346.pExpr; }
117824         break;
117825       case 277: /* trigger_cmd_list ::= trigger_cmd_list trigger_cmd SEMI */
117826 {
117827   assert( yymsp[-2].minor.yy473!=0 );
117828   yymsp[-2].minor.yy473->pLast->pNext = yymsp[-1].minor.yy473;
117829   yymsp[-2].minor.yy473->pLast = yymsp[-1].minor.yy473;
117830   yygotominor.yy473 = yymsp[-2].minor.yy473;
117831 }
117832         break;
117833       case 278: /* trigger_cmd_list ::= trigger_cmd SEMI */
117834 {
117835   assert( yymsp[-1].minor.yy473!=0 );
117836   yymsp[-1].minor.yy473->pLast = yymsp[-1].minor.yy473;
117837   yygotominor.yy473 = yymsp[-1].minor.yy473;
117838 }
117839         break;
117840       case 280: /* trnm ::= nm DOT nm */
117841 {
117842   yygotominor.yy0 = yymsp[0].minor.yy0;
117843   sqlite3ErrorMsg(pParse,
117844         "qualified table names are not allowed on INSERT, UPDATE, and DELETE "
117845         "statements within triggers");
117846 }
117847         break;
117848       case 282: /* tridxby ::= INDEXED BY nm */
117849 {
117850   sqlite3ErrorMsg(pParse,
117851         "the INDEXED BY clause is not allowed on UPDATE or DELETE statements "
117852         "within triggers");
117853 }
117854         break;
117855       case 283: /* tridxby ::= NOT INDEXED */
117856 {
117857   sqlite3ErrorMsg(pParse,
117858         "the NOT INDEXED clause is not allowed on UPDATE or DELETE statements "
117859         "within triggers");
117860 }
117861         break;
117862       case 284: /* trigger_cmd ::= UPDATE orconf trnm tridxby SET setlist where_opt */
117863 { yygotominor.yy473 = sqlite3TriggerUpdateStep(pParse->db, &yymsp[-4].minor.yy0, yymsp[-1].minor.yy14, yymsp[0].minor.yy132, yymsp[-5].minor.yy186); }
117864         break;
117865       case 285: /* trigger_cmd ::= insert_cmd INTO trnm inscollist_opt select */
117866 {yygotominor.yy473 = sqlite3TriggerInsertStep(pParse->db, &yymsp[-2].minor.yy0, yymsp[-1].minor.yy408, yymsp[0].minor.yy3, yymsp[-4].minor.yy186);}
117867         break;
117868       case 286: /* trigger_cmd ::= DELETE FROM trnm tridxby where_opt */
117869 {yygotominor.yy473 = sqlite3TriggerDeleteStep(pParse->db, &yymsp[-2].minor.yy0, yymsp[0].minor.yy132);}
117870         break;
117871       case 287: /* trigger_cmd ::= select */
117872 {yygotominor.yy473 = sqlite3TriggerSelectStep(pParse->db, yymsp[0].minor.yy3); }
117873         break;
117874       case 288: /* expr ::= RAISE LP IGNORE RP */
117875 {
117876   yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_RAISE, 0, 0, 0);
117877   if( yygotominor.yy346.pExpr ){
117878     yygotominor.yy346.pExpr->affinity = OE_Ignore;
117879   }
117880   yygotominor.yy346.zStart = yymsp[-3].minor.yy0.z;
117881   yygotominor.yy346.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
117882 }
117883         break;
117884       case 289: /* expr ::= RAISE LP raisetype COMMA nm RP */
117885 {
117886   yygotominor.yy346.pExpr = sqlite3PExpr(pParse, TK_RAISE, 0, 0, &yymsp[-1].minor.yy0);
117887   if( yygotominor.yy346.pExpr ) {
117888     yygotominor.yy346.pExpr->affinity = (char)yymsp[-3].minor.yy328;
117889   }
117890   yygotominor.yy346.zStart = yymsp[-5].minor.yy0.z;
117891   yygotominor.yy346.zEnd = &yymsp[0].minor.yy0.z[yymsp[0].minor.yy0.n];
117892 }
117893         break;
117894       case 290: /* raisetype ::= ROLLBACK */
117895 {yygotominor.yy328 = OE_Rollback;}
117896         break;
117897       case 292: /* raisetype ::= FAIL */
117898 {yygotominor.yy328 = OE_Fail;}
117899         break;
117900       case 293: /* cmd ::= DROP TRIGGER ifexists fullname */
117901 {
117902   sqlite3DropTrigger(pParse,yymsp[0].minor.yy65,yymsp[-1].minor.yy328);
117903 }
117904         break;
117905       case 294: /* cmd ::= ATTACH database_kw_opt expr AS expr key_opt */
117906 {
117907   sqlite3Attach(pParse, yymsp[-3].minor.yy346.pExpr, yymsp[-1].minor.yy346.pExpr, yymsp[0].minor.yy132);
117908 }
117909         break;
117910       case 295: /* cmd ::= DETACH database_kw_opt expr */
117911 {
117912   sqlite3Detach(pParse, yymsp[0].minor.yy346.pExpr);
117913 }
117914         break;
117915       case 300: /* cmd ::= REINDEX */
117916 {sqlite3Reindex(pParse, 0, 0);}
117917         break;
117918       case 301: /* cmd ::= REINDEX nm dbnm */
117919 {sqlite3Reindex(pParse, &yymsp[-1].minor.yy0, &yymsp[0].minor.yy0);}
117920         break;
117921       case 302: /* cmd ::= ANALYZE */
117922 {sqlite3Analyze(pParse, 0, 0);}
117923         break;
117924       case 303: /* cmd ::= ANALYZE nm dbnm */
117925 {sqlite3Analyze(pParse, &yymsp[-1].minor.yy0, &yymsp[0].minor.yy0);}
117926         break;
117927       case 304: /* cmd ::= ALTER TABLE fullname RENAME TO nm */
117928 {
117929   sqlite3AlterRenameTable(pParse,yymsp[-3].minor.yy65,&yymsp[0].minor.yy0);
117930 }
117931         break;
117932       case 305: /* cmd ::= ALTER TABLE add_column_fullname ADD kwcolumn_opt column */
117933 {
117934   sqlite3AlterFinishAddColumn(pParse, &yymsp[0].minor.yy0);
117935 }
117936         break;
117937       case 306: /* add_column_fullname ::= fullname */
117938 {
117939   pParse->db->lookaside.bEnabled = 0;
117940   sqlite3AlterBeginAddColumn(pParse, yymsp[0].minor.yy65);
117941 }
117942         break;
117943       case 309: /* cmd ::= create_vtab */
117944 {sqlite3VtabFinishParse(pParse,0);}
117945         break;
117946       case 310: /* cmd ::= create_vtab LP vtabarglist RP */
117947 {sqlite3VtabFinishParse(pParse,&yymsp[0].minor.yy0);}
117948         break;
117949       case 311: /* create_vtab ::= createkw VIRTUAL TABLE ifnotexists nm dbnm USING nm */
117950 {
117951     sqlite3VtabBeginParse(pParse, &yymsp[-3].minor.yy0, &yymsp[-2].minor.yy0, &yymsp[0].minor.yy0, yymsp[-4].minor.yy328);
117952 }
117953         break;
117954       case 314: /* vtabarg ::= */
117955 {sqlite3VtabArgInit(pParse);}
117956         break;
117957       case 316: /* vtabargtoken ::= ANY */
117958       case 317: /* vtabargtoken ::= lp anylist RP */ yytestcase(yyruleno==317);
117959       case 318: /* lp ::= LP */ yytestcase(yyruleno==318);
117960 {sqlite3VtabArgExtend(pParse,&yymsp[0].minor.yy0);}
117961         break;
117962       case 322: /* with ::= */
117963 {yygotominor.yy59 = 0;}
117964         break;
117965       case 323: /* with ::= WITH wqlist */
117966       case 324: /* with ::= WITH RECURSIVE wqlist */ yytestcase(yyruleno==324);
117967 { yygotominor.yy59 = yymsp[0].minor.yy59; }
117968         break;
117969       case 325: /* wqlist ::= nm idxlist_opt AS LP select RP */
117970 {
117971   yygotominor.yy59 = sqlite3WithAdd(pParse, 0, &yymsp[-5].minor.yy0, yymsp[-4].minor.yy14, yymsp[-1].minor.yy3);
117972 }
117973         break;
117974       case 326: /* wqlist ::= wqlist COMMA nm idxlist_opt AS LP select RP */
117975 {
117976   yygotominor.yy59 = sqlite3WithAdd(pParse, yymsp[-7].minor.yy59, &yymsp[-5].minor.yy0, yymsp[-4].minor.yy14, yymsp[-1].minor.yy3);
117977 }
117978         break;
117979       default:
117980       /* (0) input ::= cmdlist */ yytestcase(yyruleno==0);
117981       /* (1) cmdlist ::= cmdlist ecmd */ yytestcase(yyruleno==1);
117982       /* (2) cmdlist ::= ecmd */ yytestcase(yyruleno==2);
117983       /* (3) ecmd ::= SEMI */ yytestcase(yyruleno==3);
117984       /* (4) ecmd ::= explain cmdx SEMI */ yytestcase(yyruleno==4);
117985       /* (10) trans_opt ::= */ yytestcase(yyruleno==10);
117986       /* (11) trans_opt ::= TRANSACTION */ yytestcase(yyruleno==11);
117987       /* (12) trans_opt ::= TRANSACTION nm */ yytestcase(yyruleno==12);
117988       /* (20) savepoint_opt ::= SAVEPOINT */ yytestcase(yyruleno==20);
117989       /* (21) savepoint_opt ::= */ yytestcase(yyruleno==21);
117990       /* (25) cmd ::= create_table create_table_args */ yytestcase(yyruleno==25);
117991       /* (36) columnlist ::= columnlist COMMA column */ yytestcase(yyruleno==36);
117992       /* (37) columnlist ::= column */ yytestcase(yyruleno==37);
117993       /* (43) type ::= */ yytestcase(yyruleno==43);
117994       /* (50) signed ::= plus_num */ yytestcase(yyruleno==50);
117995       /* (51) signed ::= minus_num */ yytestcase(yyruleno==51);
117996       /* (52) carglist ::= carglist ccons */ yytestcase(yyruleno==52);
117997       /* (53) carglist ::= */ yytestcase(yyruleno==53);
117998       /* (60) ccons ::= NULL onconf */ yytestcase(yyruleno==60);
117999       /* (88) conslist ::= conslist tconscomma tcons */ yytestcase(yyruleno==88);
118000       /* (89) conslist ::= tcons */ yytestcase(yyruleno==89);
118001       /* (91) tconscomma ::= */ yytestcase(yyruleno==91);
118002       /* (273) foreach_clause ::= */ yytestcase(yyruleno==273);
118003       /* (274) foreach_clause ::= FOR EACH ROW */ yytestcase(yyruleno==274);
118004       /* (281) tridxby ::= */ yytestcase(yyruleno==281);
118005       /* (298) database_kw_opt ::= DATABASE */ yytestcase(yyruleno==298);
118006       /* (299) database_kw_opt ::= */ yytestcase(yyruleno==299);
118007       /* (307) kwcolumn_opt ::= */ yytestcase(yyruleno==307);
118008       /* (308) kwcolumn_opt ::= COLUMNKW */ yytestcase(yyruleno==308);
118009       /* (312) vtabarglist ::= vtabarg */ yytestcase(yyruleno==312);
118010       /* (313) vtabarglist ::= vtabarglist COMMA vtabarg */ yytestcase(yyruleno==313);
118011       /* (315) vtabarg ::= vtabarg vtabargtoken */ yytestcase(yyruleno==315);
118012       /* (319) anylist ::= */ yytestcase(yyruleno==319);
118013       /* (320) anylist ::= anylist LP anylist RP */ yytestcase(yyruleno==320);
118014       /* (321) anylist ::= anylist ANY */ yytestcase(yyruleno==321);
118015         break;
118016   };
118017   assert( yyruleno>=0 && yyruleno<sizeof(yyRuleInfo)/sizeof(yyRuleInfo[0]) );
118018   yygoto = yyRuleInfo[yyruleno].lhs;
118019   yysize = yyRuleInfo[yyruleno].nrhs;
118020   yypParser->yyidx -= yysize;
118021   yyact = yy_find_reduce_action(yymsp[-yysize].stateno,(YYCODETYPE)yygoto);
118022   if( yyact < YYNSTATE ){
118023 #ifdef NDEBUG
118024     /* If we are not debugging and the reduce action popped at least
118025     ** one element off the stack, then we can push the new element back
118026     ** onto the stack here, and skip the stack overflow test in yy_shift().
118027     ** That gives a significant speed improvement. */
118028     if( yysize ){
118029       yypParser->yyidx++;
118030       yymsp -= yysize-1;
118031       yymsp->stateno = (YYACTIONTYPE)yyact;
118032       yymsp->major = (YYCODETYPE)yygoto;
118033       yymsp->minor = yygotominor;
118034     }else
118035 #endif
118036     {
118037       yy_shift(yypParser,yyact,yygoto,&yygotominor);
118038     }
118039   }else{
118040     assert( yyact == YYNSTATE + YYNRULE + 1 );
118041     yy_accept(yypParser);
118042   }
118043 }
118044 
118045 /*
118046 ** The following code executes when the parse fails
118047 */
118048 #ifndef YYNOERRORRECOVERY
118049 static void yy_parse_failed(
118050   yyParser *yypParser           /* The parser */
118051 ){
118052   sqlite3ParserARG_FETCH;
118053 #ifndef NDEBUG
118054   if( yyTraceFILE ){
118055     fprintf(yyTraceFILE,"%sFail!\n",yyTracePrompt);
118056   }
118057 #endif
118058   while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser);
118059   /* Here code is inserted which will be executed whenever the
118060   ** parser fails */
118061   sqlite3ParserARG_STORE; /* Suppress warning about unused %extra_argument variable */
118062 }
118063 #endif /* YYNOERRORRECOVERY */
118064 
118065 /*
118066 ** The following code executes when a syntax error first occurs.
118067 */
118068 static void yy_syntax_error(
118069   yyParser *yypParser,           /* The parser */
118070   int yymajor,                   /* The major type of the error token */
118071   YYMINORTYPE yyminor            /* The minor type of the error token */
118072 ){
118073   sqlite3ParserARG_FETCH;
118074 #define TOKEN (yyminor.yy0)
118075 
118076   UNUSED_PARAMETER(yymajor);  /* Silence some compiler warnings */
118077   assert( TOKEN.z[0] );  /* The tokenizer always gives us a token */
118078   sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &TOKEN);
118079   sqlite3ParserARG_STORE; /* Suppress warning about unused %extra_argument variable */
118080 }
118081 
118082 /*
118083 ** The following is executed when the parser accepts
118084 */
118085 static void yy_accept(
118086   yyParser *yypParser           /* The parser */
118087 ){
118088   sqlite3ParserARG_FETCH;
118089 #ifndef NDEBUG
118090   if( yyTraceFILE ){
118091     fprintf(yyTraceFILE,"%sAccept!\n",yyTracePrompt);
118092   }
118093 #endif
118094   while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser);
118095   /* Here code is inserted which will be executed whenever the
118096   ** parser accepts */
118097   sqlite3ParserARG_STORE; /* Suppress warning about unused %extra_argument variable */
118098 }
118099 
118100 /* The main parser program.
118101 ** The first argument is a pointer to a structure obtained from
118102 ** "sqlite3ParserAlloc" which describes the current state of the parser.
118103 ** The second argument is the major token number.  The third is
118104 ** the minor token.  The fourth optional argument is whatever the
118105 ** user wants (and specified in the grammar) and is available for
118106 ** use by the action routines.
118107 **
118108 ** Inputs:
118109 ** <ul>
118110 ** <li> A pointer to the parser (an opaque structure.)
118111 ** <li> The major token number.
118112 ** <li> The minor token number.
118113 ** <li> An option argument of a grammar-specified type.
118114 ** </ul>
118115 **
118116 ** Outputs:
118117 ** None.
118118 */
118119 SQLITE_PRIVATE void sqlite3Parser(
118120   void *yyp,                   /* The parser */
118121   int yymajor,                 /* The major token code number */
118122   sqlite3ParserTOKENTYPE yyminor       /* The value for the token */
118123   sqlite3ParserARG_PDECL               /* Optional %extra_argument parameter */
118124 ){
118125   YYMINORTYPE yyminorunion;
118126   int yyact;            /* The parser action. */
118127 #if !defined(YYERRORSYMBOL) && !defined(YYNOERRORRECOVERY)
118128   int yyendofinput;     /* True if we are at the end of input */
118129 #endif
118130 #ifdef YYERRORSYMBOL
118131   int yyerrorhit = 0;   /* True if yymajor has invoked an error */
118132 #endif
118133   yyParser *yypParser;  /* The parser */
118134 
118135   /* (re)initialize the parser, if necessary */
118136   yypParser = (yyParser*)yyp;
118137   if( yypParser->yyidx<0 ){
118138 #if YYSTACKDEPTH<=0
118139     if( yypParser->yystksz <=0 ){
118140       /*memset(&yyminorunion, 0, sizeof(yyminorunion));*/
118141       yyminorunion = yyzerominor;
118142       yyStackOverflow(yypParser, &yyminorunion);
118143       return;
118144     }
118145 #endif
118146     yypParser->yyidx = 0;
118147     yypParser->yyerrcnt = -1;
118148     yypParser->yystack[0].stateno = 0;
118149     yypParser->yystack[0].major = 0;
118150   }
118151   yyminorunion.yy0 = yyminor;
118152 #if !defined(YYERRORSYMBOL) && !defined(YYNOERRORRECOVERY)
118153   yyendofinput = (yymajor==0);
118154 #endif
118155   sqlite3ParserARG_STORE;
118156 
118157 #ifndef NDEBUG
118158   if( yyTraceFILE ){
118159     fprintf(yyTraceFILE,"%sInput %s\n",yyTracePrompt,yyTokenName[yymajor]);
118160   }
118161 #endif
118162 
118163   do{
118164     yyact = yy_find_shift_action(yypParser,(YYCODETYPE)yymajor);
118165     if( yyact<YYNSTATE ){
118166       yy_shift(yypParser,yyact,yymajor,&yyminorunion);
118167       yypParser->yyerrcnt--;
118168       yymajor = YYNOCODE;
118169     }else if( yyact < YYNSTATE + YYNRULE ){
118170       yy_reduce(yypParser,yyact-YYNSTATE);
118171     }else{
118172       assert( yyact == YY_ERROR_ACTION );
118173 #ifdef YYERRORSYMBOL
118174       int yymx;
118175 #endif
118176 #ifndef NDEBUG
118177       if( yyTraceFILE ){
118178         fprintf(yyTraceFILE,"%sSyntax Error!\n",yyTracePrompt);
118179       }
118180 #endif
118181 #ifdef YYERRORSYMBOL
118182       /* A syntax error has occurred.
118183       ** The response to an error depends upon whether or not the
118184       ** grammar defines an error token "ERROR".
118185       **
118186       ** This is what we do if the grammar does define ERROR:
118187       **
118188       **  * Call the %syntax_error function.
118189       **
118190       **  * Begin popping the stack until we enter a state where
118191       **    it is legal to shift the error symbol, then shift
118192       **    the error symbol.
118193       **
118194       **  * Set the error count to three.
118195       **
118196       **  * Begin accepting and shifting new tokens.  No new error
118197       **    processing will occur until three tokens have been
118198       **    shifted successfully.
118199       **
118200       */
118201       if( yypParser->yyerrcnt<0 ){
118202         yy_syntax_error(yypParser,yymajor,yyminorunion);
118203       }
118204       yymx = yypParser->yystack[yypParser->yyidx].major;
118205       if( yymx==YYERRORSYMBOL || yyerrorhit ){
118206 #ifndef NDEBUG
118207         if( yyTraceFILE ){
118208           fprintf(yyTraceFILE,"%sDiscard input token %s\n",
118209              yyTracePrompt,yyTokenName[yymajor]);
118210         }
118211 #endif
118212         yy_destructor(yypParser, (YYCODETYPE)yymajor,&yyminorunion);
118213         yymajor = YYNOCODE;
118214       }else{
118215          while(
118216           yypParser->yyidx >= 0 &&
118217           yymx != YYERRORSYMBOL &&
118218           (yyact = yy_find_reduce_action(
118219                         yypParser->yystack[yypParser->yyidx].stateno,
118220                         YYERRORSYMBOL)) >= YYNSTATE
118221         ){
118222           yy_pop_parser_stack(yypParser);
118223         }
118224         if( yypParser->yyidx < 0 || yymajor==0 ){
118225           yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion);
118226           yy_parse_failed(yypParser);
118227           yymajor = YYNOCODE;
118228         }else if( yymx!=YYERRORSYMBOL ){
118229           YYMINORTYPE u2;
118230           u2.YYERRSYMDT = 0;
118231           yy_shift(yypParser,yyact,YYERRORSYMBOL,&u2);
118232         }
118233       }
118234       yypParser->yyerrcnt = 3;
118235       yyerrorhit = 1;
118236 #elif defined(YYNOERRORRECOVERY)
118237       /* If the YYNOERRORRECOVERY macro is defined, then do not attempt to
118238       ** do any kind of error recovery.  Instead, simply invoke the syntax
118239       ** error routine and continue going as if nothing had happened.
118240       **
118241       ** Applications can set this macro (for example inside %include) if
118242       ** they intend to abandon the parse upon the first syntax error seen.
118243       */
118244       yy_syntax_error(yypParser,yymajor,yyminorunion);
118245       yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion);
118246       yymajor = YYNOCODE;
118247 
118248 #else  /* YYERRORSYMBOL is not defined */
118249       /* This is what we do if the grammar does not define ERROR:
118250       **
118251       **  * Report an error message, and throw away the input token.
118252       **
118253       **  * If the input token is $, then fail the parse.
118254       **
118255       ** As before, subsequent error messages are suppressed until
118256       ** three input tokens have been successfully shifted.
118257       */
118258       if( yypParser->yyerrcnt<=0 ){
118259         yy_syntax_error(yypParser,yymajor,yyminorunion);
118260       }
118261       yypParser->yyerrcnt = 3;
118262       yy_destructor(yypParser,(YYCODETYPE)yymajor,&yyminorunion);
118263       if( yyendofinput ){
118264         yy_parse_failed(yypParser);
118265       }
118266       yymajor = YYNOCODE;
118267 #endif
118268     }
118269   }while( yymajor!=YYNOCODE && yypParser->yyidx>=0 );
118270   return;
118271 }
118272 
118273 /************** End of parse.c ***********************************************/
118274 /************** Begin file tokenize.c ****************************************/
118275 /*
118276 ** 2001 September 15
118277 **
118278 ** The author disclaims copyright to this source code.  In place of
118279 ** a legal notice, here is a blessing:
118280 **
118281 **    May you do good and not evil.
118282 **    May you find forgiveness for yourself and forgive others.
118283 **    May you share freely, never taking more than you give.
118284 **
118285 *************************************************************************
118286 ** An tokenizer for SQL
118287 **
118288 ** This file contains C code that splits an SQL input string up into
118289 ** individual tokens and sends those tokens one-by-one over to the
118290 ** parser for analysis.
118291 */
118292 /* #include <stdlib.h> */
118293 
118294 /*
118295 ** The charMap() macro maps alphabetic characters into their
118296 ** lower-case ASCII equivalent.  On ASCII machines, this is just
118297 ** an upper-to-lower case map.  On EBCDIC machines we also need
118298 ** to adjust the encoding.  Only alphabetic characters and underscores
118299 ** need to be translated.
118300 */
118301 #ifdef SQLITE_ASCII
118302 # define charMap(X) sqlite3UpperToLower[(unsigned char)X]
118303 #endif
118304 #ifdef SQLITE_EBCDIC
118305 # define charMap(X) ebcdicToAscii[(unsigned char)X]
118306 const unsigned char ebcdicToAscii[] = {
118307 /* 0   1   2   3   4   5   6   7   8   9   A   B   C   D   E   F */
118308    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 0x */
118309    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 1x */
118310    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 2x */
118311    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 3x */
118312    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 4x */
118313    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 5x */
118314    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, 95,  0,  0,  /* 6x */
118315    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* 7x */
118316    0, 97, 98, 99,100,101,102,103,104,105,  0,  0,  0,  0,  0,  0,  /* 8x */
118317    0,106,107,108,109,110,111,112,113,114,  0,  0,  0,  0,  0,  0,  /* 9x */
118318    0,  0,115,116,117,118,119,120,121,122,  0,  0,  0,  0,  0,  0,  /* Ax */
118319    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* Bx */
118320    0, 97, 98, 99,100,101,102,103,104,105,  0,  0,  0,  0,  0,  0,  /* Cx */
118321    0,106,107,108,109,110,111,112,113,114,  0,  0,  0,  0,  0,  0,  /* Dx */
118322    0,  0,115,116,117,118,119,120,121,122,  0,  0,  0,  0,  0,  0,  /* Ex */
118323    0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  /* Fx */
118324 };
118325 #endif
118326 
118327 /*
118328 ** The sqlite3KeywordCode function looks up an identifier to determine if
118329 ** it is a keyword.  If it is a keyword, the token code of that keyword is
118330 ** returned.  If the input is not a keyword, TK_ID is returned.
118331 **
118332 ** The implementation of this routine was generated by a program,
118333 ** mkkeywordhash.h, located in the tool subdirectory of the distribution.
118334 ** The output of the mkkeywordhash.c program is written into a file
118335 ** named keywordhash.h and then included into this source file by
118336 ** the #include below.
118337 */
118338 /************** Include keywordhash.h in the middle of tokenize.c ************/
118339 /************** Begin file keywordhash.h *************************************/
118340 /***** This file contains automatically generated code ******
118341 **
118342 ** The code in this file has been automatically generated by
118343 **
118344 **   sqlite/tool/mkkeywordhash.c
118345 **
118346 ** The code in this file implements a function that determines whether
118347 ** or not a given identifier is really an SQL keyword.  The same thing
118348 ** might be implemented more directly using a hand-written hash table.
118349 ** But by using this automatically generated code, the size of the code
118350 ** is substantially reduced.  This is important for embedded applications
118351 ** on platforms with limited memory.
118352 */
118353 /* Hash score: 182 */
118354 static int keywordCode(const char *z, int n){
118355   /* zText[] encodes 834 bytes of keywords in 554 bytes */
118356   /*   REINDEXEDESCAPEACHECKEYBEFOREIGNOREGEXPLAINSTEADDATABASELECT       */
118357   /*   ABLEFTHENDEFERRABLELSEXCEPTRANSACTIONATURALTERAISEXCLUSIVE         */
118358   /*   XISTSAVEPOINTERSECTRIGGEREFERENCESCONSTRAINTOFFSETEMPORARY         */
118359   /*   UNIQUERYWITHOUTERELEASEATTACHAVINGROUPDATEBEGINNERECURSIVE         */
118360   /*   BETWEENOTNULLIKECASCADELETECASECOLLATECREATECURRENT_DATEDETACH     */
118361   /*   IMMEDIATEJOINSERTMATCHPLANALYZEPRAGMABORTVALUESVIRTUALIMITWHEN     */
118362   /*   WHERENAMEAFTEREPLACEANDEFAULTAUTOINCREMENTCASTCOLUMNCOMMIT         */
118363   /*   CONFLICTCROSSCURRENT_TIMESTAMPRIMARYDEFERREDISTINCTDROPFAIL        */
118364   /*   FROMFULLGLOBYIFISNULLORDERESTRICTRIGHTROLLBACKROWUNIONUSING        */
118365   /*   VACUUMVIEWINITIALLY                                                */
118366   static const char zText[553] = {
118367     'R','E','I','N','D','E','X','E','D','E','S','C','A','P','E','A','C','H',
118368     'E','C','K','E','Y','B','E','F','O','R','E','I','G','N','O','R','E','G',
118369     'E','X','P','L','A','I','N','S','T','E','A','D','D','A','T','A','B','A',
118370     'S','E','L','E','C','T','A','B','L','E','F','T','H','E','N','D','E','F',
118371     'E','R','R','A','B','L','E','L','S','E','X','C','E','P','T','R','A','N',
118372     'S','A','C','T','I','O','N','A','T','U','R','A','L','T','E','R','A','I',
118373     'S','E','X','C','L','U','S','I','V','E','X','I','S','T','S','A','V','E',
118374     'P','O','I','N','T','E','R','S','E','C','T','R','I','G','G','E','R','E',
118375     'F','E','R','E','N','C','E','S','C','O','N','S','T','R','A','I','N','T',
118376     'O','F','F','S','E','T','E','M','P','O','R','A','R','Y','U','N','I','Q',
118377     'U','E','R','Y','W','I','T','H','O','U','T','E','R','E','L','E','A','S',
118378     'E','A','T','T','A','C','H','A','V','I','N','G','R','O','U','P','D','A',
118379     'T','E','B','E','G','I','N','N','E','R','E','C','U','R','S','I','V','E',
118380     'B','E','T','W','E','E','N','O','T','N','U','L','L','I','K','E','C','A',
118381     'S','C','A','D','E','L','E','T','E','C','A','S','E','C','O','L','L','A',
118382     'T','E','C','R','E','A','T','E','C','U','R','R','E','N','T','_','D','A',
118383     'T','E','D','E','T','A','C','H','I','M','M','E','D','I','A','T','E','J',
118384     'O','I','N','S','E','R','T','M','A','T','C','H','P','L','A','N','A','L',
118385     'Y','Z','E','P','R','A','G','M','A','B','O','R','T','V','A','L','U','E',
118386     'S','V','I','R','T','U','A','L','I','M','I','T','W','H','E','N','W','H',
118387     'E','R','E','N','A','M','E','A','F','T','E','R','E','P','L','A','C','E',
118388     'A','N','D','E','F','A','U','L','T','A','U','T','O','I','N','C','R','E',
118389     'M','E','N','T','C','A','S','T','C','O','L','U','M','N','C','O','M','M',
118390     'I','T','C','O','N','F','L','I','C','T','C','R','O','S','S','C','U','R',
118391     'R','E','N','T','_','T','I','M','E','S','T','A','M','P','R','I','M','A',
118392     'R','Y','D','E','F','E','R','R','E','D','I','S','T','I','N','C','T','D',
118393     'R','O','P','F','A','I','L','F','R','O','M','F','U','L','L','G','L','O',
118394     'B','Y','I','F','I','S','N','U','L','L','O','R','D','E','R','E','S','T',
118395     'R','I','C','T','R','I','G','H','T','R','O','L','L','B','A','C','K','R',
118396     'O','W','U','N','I','O','N','U','S','I','N','G','V','A','C','U','U','M',
118397     'V','I','E','W','I','N','I','T','I','A','L','L','Y',
118398   };
118399   static const unsigned char aHash[127] = {
118400       76, 105, 117,  74,   0,  45,   0,   0,  82,   0,  77,   0,   0,
118401       42,  12,  78,  15,   0, 116,  85,  54, 112,   0,  19,   0,   0,
118402      121,   0, 119, 115,   0,  22,  93,   0,   9,   0,   0,  70,  71,
118403        0,  69,   6,   0,  48,  90, 102,   0, 118, 101,   0,   0,  44,
118404        0, 103,  24,   0,  17,   0, 122,  53,  23,   0,   5, 110,  25,
118405       96,   0,   0, 124, 106,  60, 123,  57,  28,  55,   0,  91,   0,
118406      100,  26,   0,  99,   0,   0,   0,  95,  92,  97,  88, 109,  14,
118407       39, 108,   0,  81,   0,  18,  89, 111,  32,   0, 120,  80, 113,
118408       62,  46,  84,   0,   0,  94,  40,  59, 114,   0,  36,   0,   0,
118409       29,   0,  86,  63,  64,   0,  20,  61,   0,  56,
118410   };
118411   static const unsigned char aNext[124] = {
118412        0,   0,   0,   0,   4,   0,   0,   0,   0,   0,   0,   0,   0,
118413        0,   2,   0,   0,   0,   0,   0,   0,  13,   0,   0,   0,   0,
118414        0,   7,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
118415        0,   0,   0,   0,  33,   0,  21,   0,   0,   0,   0,   0,  50,
118416        0,  43,   3,  47,   0,   0,   0,   0,  30,   0,  58,   0,  38,
118417        0,   0,   0,   1,  66,   0,   0,  67,   0,  41,   0,   0,   0,
118418        0,   0,   0,  49,  65,   0,   0,   0,   0,  31,  52,  16,  34,
118419       10,   0,   0,   0,   0,   0,   0,   0,  11,  72,  79,   0,   8,
118420        0, 104,  98,   0, 107,   0,  87,   0,  75,  51,   0,  27,  37,
118421       73,  83,   0,  35,  68,   0,   0,
118422   };
118423   static const unsigned char aLen[124] = {
118424        7,   7,   5,   4,   6,   4,   5,   3,   6,   7,   3,   6,   6,
118425        7,   7,   3,   8,   2,   6,   5,   4,   4,   3,  10,   4,   6,
118426       11,   6,   2,   7,   5,   5,   9,   6,   9,   9,   7,  10,  10,
118427        4,   6,   2,   3,   9,   4,   2,   6,   5,   7,   4,   5,   7,
118428        6,   6,   5,   6,   5,   5,   9,   7,   7,   3,   2,   4,   4,
118429        7,   3,   6,   4,   7,   6,  12,   6,   9,   4,   6,   5,   4,
118430        7,   6,   5,   6,   7,   5,   4,   5,   6,   5,   7,   3,   7,
118431       13,   2,   2,   4,   6,   6,   8,   5,  17,  12,   7,   8,   8,
118432        2,   4,   4,   4,   4,   4,   2,   2,   6,   5,   8,   5,   8,
118433        3,   5,   5,   6,   4,   9,   3,
118434   };
118435   static const unsigned short int aOffset[124] = {
118436        0,   2,   2,   8,   9,  14,  16,  20,  23,  25,  25,  29,  33,
118437       36,  41,  46,  48,  53,  54,  59,  62,  65,  67,  69,  78,  81,
118438       86,  91,  95,  96, 101, 105, 109, 117, 122, 128, 136, 142, 152,
118439      159, 162, 162, 165, 167, 167, 171, 176, 179, 184, 184, 188, 192,
118440      199, 204, 209, 212, 218, 221, 225, 234, 240, 240, 240, 243, 246,
118441      250, 251, 255, 261, 265, 272, 278, 290, 296, 305, 307, 313, 318,
118442      320, 327, 332, 337, 343, 349, 354, 358, 361, 367, 371, 378, 380,
118443      387, 389, 391, 400, 404, 410, 416, 424, 429, 429, 445, 452, 459,
118444      460, 467, 471, 475, 479, 483, 486, 488, 490, 496, 500, 508, 513,
118445      521, 524, 529, 534, 540, 544, 549,
118446   };
118447   static const unsigned char aCode[124] = {
118448     TK_REINDEX,    TK_INDEXED,    TK_INDEX,      TK_DESC,       TK_ESCAPE,
118449     TK_EACH,       TK_CHECK,      TK_KEY,        TK_BEFORE,     TK_FOREIGN,
118450     TK_FOR,        TK_IGNORE,     TK_LIKE_KW,    TK_EXPLAIN,    TK_INSTEAD,
118451     TK_ADD,        TK_DATABASE,   TK_AS,         TK_SELECT,     TK_TABLE,
118452     TK_JOIN_KW,    TK_THEN,       TK_END,        TK_DEFERRABLE, TK_ELSE,
118453     TK_EXCEPT,     TK_TRANSACTION,TK_ACTION,     TK_ON,         TK_JOIN_KW,
118454     TK_ALTER,      TK_RAISE,      TK_EXCLUSIVE,  TK_EXISTS,     TK_SAVEPOINT,
118455     TK_INTERSECT,  TK_TRIGGER,    TK_REFERENCES, TK_CONSTRAINT, TK_INTO,
118456     TK_OFFSET,     TK_OF,         TK_SET,        TK_TEMP,       TK_TEMP,
118457     TK_OR,         TK_UNIQUE,     TK_QUERY,      TK_WITHOUT,    TK_WITH,
118458     TK_JOIN_KW,    TK_RELEASE,    TK_ATTACH,     TK_HAVING,     TK_GROUP,
118459     TK_UPDATE,     TK_BEGIN,      TK_JOIN_KW,    TK_RECURSIVE,  TK_BETWEEN,
118460     TK_NOTNULL,    TK_NOT,        TK_NO,         TK_NULL,       TK_LIKE_KW,
118461     TK_CASCADE,    TK_ASC,        TK_DELETE,     TK_CASE,       TK_COLLATE,
118462     TK_CREATE,     TK_CTIME_KW,   TK_DETACH,     TK_IMMEDIATE,  TK_JOIN,
118463     TK_INSERT,     TK_MATCH,      TK_PLAN,       TK_ANALYZE,    TK_PRAGMA,
118464     TK_ABORT,      TK_VALUES,     TK_VIRTUAL,    TK_LIMIT,      TK_WHEN,
118465     TK_WHERE,      TK_RENAME,     TK_AFTER,      TK_REPLACE,    TK_AND,
118466     TK_DEFAULT,    TK_AUTOINCR,   TK_TO,         TK_IN,         TK_CAST,
118467     TK_COLUMNKW,   TK_COMMIT,     TK_CONFLICT,   TK_JOIN_KW,    TK_CTIME_KW,
118468     TK_CTIME_KW,   TK_PRIMARY,    TK_DEFERRED,   TK_DISTINCT,   TK_IS,
118469     TK_DROP,       TK_FAIL,       TK_FROM,       TK_JOIN_KW,    TK_LIKE_KW,
118470     TK_BY,         TK_IF,         TK_ISNULL,     TK_ORDER,      TK_RESTRICT,
118471     TK_JOIN_KW,    TK_ROLLBACK,   TK_ROW,        TK_UNION,      TK_USING,
118472     TK_VACUUM,     TK_VIEW,       TK_INITIALLY,  TK_ALL,
118473   };
118474   int h, i;
118475   if( n<2 ) return TK_ID;
118476   h = ((charMap(z[0])*4) ^
118477       (charMap(z[n-1])*3) ^
118478       n) % 127;
118479   for(i=((int)aHash[h])-1; i>=0; i=((int)aNext[i])-1){
118480     if( aLen[i]==n && sqlite3StrNICmp(&zText[aOffset[i]],z,n)==0 ){
118481       testcase( i==0 ); /* REINDEX */
118482       testcase( i==1 ); /* INDEXED */
118483       testcase( i==2 ); /* INDEX */
118484       testcase( i==3 ); /* DESC */
118485       testcase( i==4 ); /* ESCAPE */
118486       testcase( i==5 ); /* EACH */
118487       testcase( i==6 ); /* CHECK */
118488       testcase( i==7 ); /* KEY */
118489       testcase( i==8 ); /* BEFORE */
118490       testcase( i==9 ); /* FOREIGN */
118491       testcase( i==10 ); /* FOR */
118492       testcase( i==11 ); /* IGNORE */
118493       testcase( i==12 ); /* REGEXP */
118494       testcase( i==13 ); /* EXPLAIN */
118495       testcase( i==14 ); /* INSTEAD */
118496       testcase( i==15 ); /* ADD */
118497       testcase( i==16 ); /* DATABASE */
118498       testcase( i==17 ); /* AS */
118499       testcase( i==18 ); /* SELECT */
118500       testcase( i==19 ); /* TABLE */
118501       testcase( i==20 ); /* LEFT */
118502       testcase( i==21 ); /* THEN */
118503       testcase( i==22 ); /* END */
118504       testcase( i==23 ); /* DEFERRABLE */
118505       testcase( i==24 ); /* ELSE */
118506       testcase( i==25 ); /* EXCEPT */
118507       testcase( i==26 ); /* TRANSACTION */
118508       testcase( i==27 ); /* ACTION */
118509       testcase( i==28 ); /* ON */
118510       testcase( i==29 ); /* NATURAL */
118511       testcase( i==30 ); /* ALTER */
118512       testcase( i==31 ); /* RAISE */
118513       testcase( i==32 ); /* EXCLUSIVE */
118514       testcase( i==33 ); /* EXISTS */
118515       testcase( i==34 ); /* SAVEPOINT */
118516       testcase( i==35 ); /* INTERSECT */
118517       testcase( i==36 ); /* TRIGGER */
118518       testcase( i==37 ); /* REFERENCES */
118519       testcase( i==38 ); /* CONSTRAINT */
118520       testcase( i==39 ); /* INTO */
118521       testcase( i==40 ); /* OFFSET */
118522       testcase( i==41 ); /* OF */
118523       testcase( i==42 ); /* SET */
118524       testcase( i==43 ); /* TEMPORARY */
118525       testcase( i==44 ); /* TEMP */
118526       testcase( i==45 ); /* OR */
118527       testcase( i==46 ); /* UNIQUE */
118528       testcase( i==47 ); /* QUERY */
118529       testcase( i==48 ); /* WITHOUT */
118530       testcase( i==49 ); /* WITH */
118531       testcase( i==50 ); /* OUTER */
118532       testcase( i==51 ); /* RELEASE */
118533       testcase( i==52 ); /* ATTACH */
118534       testcase( i==53 ); /* HAVING */
118535       testcase( i==54 ); /* GROUP */
118536       testcase( i==55 ); /* UPDATE */
118537       testcase( i==56 ); /* BEGIN */
118538       testcase( i==57 ); /* INNER */
118539       testcase( i==58 ); /* RECURSIVE */
118540       testcase( i==59 ); /* BETWEEN */
118541       testcase( i==60 ); /* NOTNULL */
118542       testcase( i==61 ); /* NOT */
118543       testcase( i==62 ); /* NO */
118544       testcase( i==63 ); /* NULL */
118545       testcase( i==64 ); /* LIKE */
118546       testcase( i==65 ); /* CASCADE */
118547       testcase( i==66 ); /* ASC */
118548       testcase( i==67 ); /* DELETE */
118549       testcase( i==68 ); /* CASE */
118550       testcase( i==69 ); /* COLLATE */
118551       testcase( i==70 ); /* CREATE */
118552       testcase( i==71 ); /* CURRENT_DATE */
118553       testcase( i==72 ); /* DETACH */
118554       testcase( i==73 ); /* IMMEDIATE */
118555       testcase( i==74 ); /* JOIN */
118556       testcase( i==75 ); /* INSERT */
118557       testcase( i==76 ); /* MATCH */
118558       testcase( i==77 ); /* PLAN */
118559       testcase( i==78 ); /* ANALYZE */
118560       testcase( i==79 ); /* PRAGMA */
118561       testcase( i==80 ); /* ABORT */
118562       testcase( i==81 ); /* VALUES */
118563       testcase( i==82 ); /* VIRTUAL */
118564       testcase( i==83 ); /* LIMIT */
118565       testcase( i==84 ); /* WHEN */
118566       testcase( i==85 ); /* WHERE */
118567       testcase( i==86 ); /* RENAME */
118568       testcase( i==87 ); /* AFTER */
118569       testcase( i==88 ); /* REPLACE */
118570       testcase( i==89 ); /* AND */
118571       testcase( i==90 ); /* DEFAULT */
118572       testcase( i==91 ); /* AUTOINCREMENT */
118573       testcase( i==92 ); /* TO */
118574       testcase( i==93 ); /* IN */
118575       testcase( i==94 ); /* CAST */
118576       testcase( i==95 ); /* COLUMN */
118577       testcase( i==96 ); /* COMMIT */
118578       testcase( i==97 ); /* CONFLICT */
118579       testcase( i==98 ); /* CROSS */
118580       testcase( i==99 ); /* CURRENT_TIMESTAMP */
118581       testcase( i==100 ); /* CURRENT_TIME */
118582       testcase( i==101 ); /* PRIMARY */
118583       testcase( i==102 ); /* DEFERRED */
118584       testcase( i==103 ); /* DISTINCT */
118585       testcase( i==104 ); /* IS */
118586       testcase( i==105 ); /* DROP */
118587       testcase( i==106 ); /* FAIL */
118588       testcase( i==107 ); /* FROM */
118589       testcase( i==108 ); /* FULL */
118590       testcase( i==109 ); /* GLOB */
118591       testcase( i==110 ); /* BY */
118592       testcase( i==111 ); /* IF */
118593       testcase( i==112 ); /* ISNULL */
118594       testcase( i==113 ); /* ORDER */
118595       testcase( i==114 ); /* RESTRICT */
118596       testcase( i==115 ); /* RIGHT */
118597       testcase( i==116 ); /* ROLLBACK */
118598       testcase( i==117 ); /* ROW */
118599       testcase( i==118 ); /* UNION */
118600       testcase( i==119 ); /* USING */
118601       testcase( i==120 ); /* VACUUM */
118602       testcase( i==121 ); /* VIEW */
118603       testcase( i==122 ); /* INITIALLY */
118604       testcase( i==123 ); /* ALL */
118605       return aCode[i];
118606     }
118607   }
118608   return TK_ID;
118609 }
118610 SQLITE_PRIVATE int sqlite3KeywordCode(const unsigned char *z, int n){
118611   return keywordCode((char*)z, n);
118612 }
118613 #define SQLITE_N_KEYWORD 124
118614 
118615 /************** End of keywordhash.h *****************************************/
118616 /************** Continuing where we left off in tokenize.c *******************/
118617 
118618 
118619 /*
118620 ** If X is a character that can be used in an identifier then
118621 ** IdChar(X) will be true.  Otherwise it is false.
118622 **
118623 ** For ASCII, any character with the high-order bit set is
118624 ** allowed in an identifier.  For 7-bit characters,
118625 ** sqlite3IsIdChar[X] must be 1.
118626 **
118627 ** For EBCDIC, the rules are more complex but have the same
118628 ** end result.
118629 **
118630 ** Ticket #1066.  the SQL standard does not allow '$' in the
118631 ** middle of identfiers.  But many SQL implementations do.
118632 ** SQLite will allow '$' in identifiers for compatibility.
118633 ** But the feature is undocumented.
118634 */
118635 #ifdef SQLITE_ASCII
118636 #define IdChar(C)  ((sqlite3CtypeMap[(unsigned char)C]&0x46)!=0)
118637 #endif
118638 #ifdef SQLITE_EBCDIC
118639 SQLITE_PRIVATE const char sqlite3IsEbcdicIdChar[] = {
118640 /* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
118641     0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,  /* 4x */
118642     0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0,  /* 5x */
118643     0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0,  /* 6x */
118644     0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0,  /* 7x */
118645     0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0,  /* 8x */
118646     0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0,  /* 9x */
118647     1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0,  /* Ax */
118648     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  /* Bx */
118649     0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1,  /* Cx */
118650     0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1,  /* Dx */
118651     0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1,  /* Ex */
118652     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0,  /* Fx */
118653 };
118654 #define IdChar(C)  (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40]))
118655 #endif
118656 
118657 
118658 /*
118659 ** Return the length of the token that begins at z[0].
118660 ** Store the token type in *tokenType before returning.
118661 */
118662 SQLITE_PRIVATE int sqlite3GetToken(const unsigned char *z, int *tokenType){
118663   int i, c;
118664   switch( *z ){
118665     case ' ': case '\t': case '\n': case '\f': case '\r': {
118666       testcase( z[0]==' ' );
118667       testcase( z[0]=='\t' );
118668       testcase( z[0]=='\n' );
118669       testcase( z[0]=='\f' );
118670       testcase( z[0]=='\r' );
118671       for(i=1; sqlite3Isspace(z[i]); i++){}
118672       *tokenType = TK_SPACE;
118673       return i;
118674     }
118675     case '-': {
118676       if( z[1]=='-' ){
118677         for(i=2; (c=z[i])!=0 && c!='\n'; i++){}
118678         *tokenType = TK_SPACE;   /* IMP: R-22934-25134 */
118679         return i;
118680       }
118681       *tokenType = TK_MINUS;
118682       return 1;
118683     }
118684     case '(': {
118685       *tokenType = TK_LP;
118686       return 1;
118687     }
118688     case ')': {
118689       *tokenType = TK_RP;
118690       return 1;
118691     }
118692     case ';': {
118693       *tokenType = TK_SEMI;
118694       return 1;
118695     }
118696     case '+': {
118697       *tokenType = TK_PLUS;
118698       return 1;
118699     }
118700     case '*': {
118701       *tokenType = TK_STAR;
118702       return 1;
118703     }
118704     case '/': {
118705       if( z[1]!='*' || z[2]==0 ){
118706         *tokenType = TK_SLASH;
118707         return 1;
118708       }
118709       for(i=3, c=z[2]; (c!='*' || z[i]!='/') && (c=z[i])!=0; i++){}
118710       if( c ) i++;
118711       *tokenType = TK_SPACE;   /* IMP: R-22934-25134 */
118712       return i;
118713     }
118714     case '%': {
118715       *tokenType = TK_REM;
118716       return 1;
118717     }
118718     case '=': {
118719       *tokenType = TK_EQ;
118720       return 1 + (z[1]=='=');
118721     }
118722     case '<': {
118723       if( (c=z[1])=='=' ){
118724         *tokenType = TK_LE;
118725         return 2;
118726       }else if( c=='>' ){
118727         *tokenType = TK_NE;
118728         return 2;
118729       }else if( c=='<' ){
118730         *tokenType = TK_LSHIFT;
118731         return 2;
118732       }else{
118733         *tokenType = TK_LT;
118734         return 1;
118735       }
118736     }
118737     case '>': {
118738       if( (c=z[1])=='=' ){
118739         *tokenType = TK_GE;
118740         return 2;
118741       }else if( c=='>' ){
118742         *tokenType = TK_RSHIFT;
118743         return 2;
118744       }else{
118745         *tokenType = TK_GT;
118746         return 1;
118747       }
118748     }
118749     case '!': {
118750       if( z[1]!='=' ){
118751         *tokenType = TK_ILLEGAL;
118752         return 2;
118753       }else{
118754         *tokenType = TK_NE;
118755         return 2;
118756       }
118757     }
118758     case '|': {
118759       if( z[1]!='|' ){
118760         *tokenType = TK_BITOR;
118761         return 1;
118762       }else{
118763         *tokenType = TK_CONCAT;
118764         return 2;
118765       }
118766     }
118767     case ',': {
118768       *tokenType = TK_COMMA;
118769       return 1;
118770     }
118771     case '&': {
118772       *tokenType = TK_BITAND;
118773       return 1;
118774     }
118775     case '~': {
118776       *tokenType = TK_BITNOT;
118777       return 1;
118778     }
118779     case '`':
118780     case '\'':
118781     case '"': {
118782       int delim = z[0];
118783       testcase( delim=='`' );
118784       testcase( delim=='\'' );
118785       testcase( delim=='"' );
118786       for(i=1; (c=z[i])!=0; i++){
118787         if( c==delim ){
118788           if( z[i+1]==delim ){
118789             i++;
118790           }else{
118791             break;
118792           }
118793         }
118794       }
118795       if( c=='\'' ){
118796         *tokenType = TK_STRING;
118797         return i+1;
118798       }else if( c!=0 ){
118799         *tokenType = TK_ID;
118800         return i+1;
118801       }else{
118802         *tokenType = TK_ILLEGAL;
118803         return i;
118804       }
118805     }
118806     case '.': {
118807 #ifndef SQLITE_OMIT_FLOATING_POINT
118808       if( !sqlite3Isdigit(z[1]) )
118809 #endif
118810       {
118811         *tokenType = TK_DOT;
118812         return 1;
118813       }
118814       /* If the next character is a digit, this is a floating point
118815       ** number that begins with ".".  Fall thru into the next case */
118816     }
118817     case '0': case '1': case '2': case '3': case '4':
118818     case '5': case '6': case '7': case '8': case '9': {
118819       testcase( z[0]=='0' );  testcase( z[0]=='1' );  testcase( z[0]=='2' );
118820       testcase( z[0]=='3' );  testcase( z[0]=='4' );  testcase( z[0]=='5' );
118821       testcase( z[0]=='6' );  testcase( z[0]=='7' );  testcase( z[0]=='8' );
118822       testcase( z[0]=='9' );
118823       *tokenType = TK_INTEGER;
118824       for(i=0; sqlite3Isdigit(z[i]); i++){}
118825 #ifndef SQLITE_OMIT_FLOATING_POINT
118826       if( z[i]=='.' ){
118827         i++;
118828         while( sqlite3Isdigit(z[i]) ){ i++; }
118829         *tokenType = TK_FLOAT;
118830       }
118831       if( (z[i]=='e' || z[i]=='E') &&
118832            ( sqlite3Isdigit(z[i+1])
118833             || ((z[i+1]=='+' || z[i+1]=='-') && sqlite3Isdigit(z[i+2]))
118834            )
118835       ){
118836         i += 2;
118837         while( sqlite3Isdigit(z[i]) ){ i++; }
118838         *tokenType = TK_FLOAT;
118839       }
118840 #endif
118841       while( IdChar(z[i]) ){
118842         *tokenType = TK_ILLEGAL;
118843         i++;
118844       }
118845       return i;
118846     }
118847     case '[': {
118848       for(i=1, c=z[0]; c!=']' && (c=z[i])!=0; i++){}
118849       *tokenType = c==']' ? TK_ID : TK_ILLEGAL;
118850       return i;
118851     }
118852     case '?': {
118853       *tokenType = TK_VARIABLE;
118854       for(i=1; sqlite3Isdigit(z[i]); i++){}
118855       return i;
118856     }
118857 #ifndef SQLITE_OMIT_TCL_VARIABLE
118858     case '$':
118859 #endif
118860     case '@':  /* For compatibility with MS SQL Server */
118861     case '#':
118862     case ':': {
118863       int n = 0;
118864       testcase( z[0]=='$' );  testcase( z[0]=='@' );
118865       testcase( z[0]==':' );  testcase( z[0]=='#' );
118866       *tokenType = TK_VARIABLE;
118867       for(i=1; (c=z[i])!=0; i++){
118868         if( IdChar(c) ){
118869           n++;
118870 #ifndef SQLITE_OMIT_TCL_VARIABLE
118871         }else if( c=='(' && n>0 ){
118872           do{
118873             i++;
118874           }while( (c=z[i])!=0 && !sqlite3Isspace(c) && c!=')' );
118875           if( c==')' ){
118876             i++;
118877           }else{
118878             *tokenType = TK_ILLEGAL;
118879           }
118880           break;
118881         }else if( c==':' && z[i+1]==':' ){
118882           i++;
118883 #endif
118884         }else{
118885           break;
118886         }
118887       }
118888       if( n==0 ) *tokenType = TK_ILLEGAL;
118889       return i;
118890     }
118891 #ifndef SQLITE_OMIT_BLOB_LITERAL
118892     case 'x': case 'X': {
118893       testcase( z[0]=='x' ); testcase( z[0]=='X' );
118894       if( z[1]=='\'' ){
118895         *tokenType = TK_BLOB;
118896         for(i=2; sqlite3Isxdigit(z[i]); i++){}
118897         if( z[i]!='\'' || i%2 ){
118898           *tokenType = TK_ILLEGAL;
118899           while( z[i] && z[i]!='\'' ){ i++; }
118900         }
118901         if( z[i] ) i++;
118902         return i;
118903       }
118904       /* Otherwise fall through to the next case */
118905     }
118906 #endif
118907     default: {
118908       if( !IdChar(*z) ){
118909         break;
118910       }
118911       for(i=1; IdChar(z[i]); i++){}
118912       *tokenType = keywordCode((char*)z, i);
118913       return i;
118914     }
118915   }
118916   *tokenType = TK_ILLEGAL;
118917   return 1;
118918 }
118919 
118920 /*
118921 ** Run the parser on the given SQL string.  The parser structure is
118922 ** passed in.  An SQLITE_ status code is returned.  If an error occurs
118923 ** then an and attempt is made to write an error message into
118924 ** memory obtained from sqlite3_malloc() and to make *pzErrMsg point to that
118925 ** error message.
118926 */
118927 SQLITE_PRIVATE int sqlite3RunParser(Parse *pParse, const char *zSql, char **pzErrMsg){
118928   int nErr = 0;                   /* Number of errors encountered */
118929   int i;                          /* Loop counter */
118930   void *pEngine;                  /* The LEMON-generated LALR(1) parser */
118931   int tokenType;                  /* type of the next token */
118932   int lastTokenParsed = -1;       /* type of the previous token */
118933   u8 enableLookaside;             /* Saved value of db->lookaside.bEnabled */
118934   sqlite3 *db = pParse->db;       /* The database connection */
118935   int mxSqlLen;                   /* Max length of an SQL string */
118936 
118937 
118938   mxSqlLen = db->aLimit[SQLITE_LIMIT_SQL_LENGTH];
118939   if( db->nVdbeActive==0 ){
118940     db->u1.isInterrupted = 0;
118941   }
118942   pParse->rc = SQLITE_OK;
118943   pParse->zTail = zSql;
118944   i = 0;
118945   assert( pzErrMsg!=0 );
118946   pEngine = sqlite3ParserAlloc((void*(*)(size_t))sqlite3Malloc);
118947   if( pEngine==0 ){
118948     db->mallocFailed = 1;
118949     return SQLITE_NOMEM;
118950   }
118951   assert( pParse->pNewTable==0 );
118952   assert( pParse->pNewTrigger==0 );
118953   assert( pParse->nVar==0 );
118954   assert( pParse->nzVar==0 );
118955   assert( pParse->azVar==0 );
118956   enableLookaside = db->lookaside.bEnabled;
118957   if( db->lookaside.pStart ) db->lookaside.bEnabled = 1;
118958   while( !db->mallocFailed && zSql[i]!=0 ){
118959     assert( i>=0 );
118960     pParse->sLastToken.z = &zSql[i];
118961     pParse->sLastToken.n = sqlite3GetToken((unsigned char*)&zSql[i],&tokenType);
118962     i += pParse->sLastToken.n;
118963     if( i>mxSqlLen ){
118964       pParse->rc = SQLITE_TOOBIG;
118965       break;
118966     }
118967     switch( tokenType ){
118968       case TK_SPACE: {
118969         if( db->u1.isInterrupted ){
118970           sqlite3ErrorMsg(pParse, "interrupt");
118971           pParse->rc = SQLITE_INTERRUPT;
118972           goto abort_parse;
118973         }
118974         break;
118975       }
118976       case TK_ILLEGAL: {
118977         sqlite3DbFree(db, *pzErrMsg);
118978         *pzErrMsg = sqlite3MPrintf(db, "unrecognized token: \"%T\"",
118979                         &pParse->sLastToken);
118980         nErr++;
118981         goto abort_parse;
118982       }
118983       case TK_SEMI: {
118984         pParse->zTail = &zSql[i];
118985         /* Fall thru into the default case */
118986       }
118987       default: {
118988         sqlite3Parser(pEngine, tokenType, pParse->sLastToken, pParse);
118989         lastTokenParsed = tokenType;
118990         if( pParse->rc!=SQLITE_OK ){
118991           goto abort_parse;
118992         }
118993         break;
118994       }
118995     }
118996   }
118997 abort_parse:
118998   if( zSql[i]==0 && nErr==0 && pParse->rc==SQLITE_OK ){
118999     if( lastTokenParsed!=TK_SEMI ){
119000       sqlite3Parser(pEngine, TK_SEMI, pParse->sLastToken, pParse);
119001       pParse->zTail = &zSql[i];
119002     }
119003     sqlite3Parser(pEngine, 0, pParse->sLastToken, pParse);
119004   }
119005 #ifdef YYTRACKMAXSTACKDEPTH
119006   sqlite3StatusSet(SQLITE_STATUS_PARSER_STACK,
119007       sqlite3ParserStackPeak(pEngine)
119008   );
119009 #endif /* YYDEBUG */
119010   sqlite3ParserFree(pEngine, sqlite3_free);
119011   db->lookaside.bEnabled = enableLookaside;
119012   if( db->mallocFailed ){
119013     pParse->rc = SQLITE_NOMEM;
119014   }
119015   if( pParse->rc!=SQLITE_OK && pParse->rc!=SQLITE_DONE && pParse->zErrMsg==0 ){
119016     sqlite3SetString(&pParse->zErrMsg, db, "%s", sqlite3ErrStr(pParse->rc));
119017   }
119018   assert( pzErrMsg!=0 );
119019   if( pParse->zErrMsg ){
119020     *pzErrMsg = pParse->zErrMsg;
119021     sqlite3_log(pParse->rc, "%s", *pzErrMsg);
119022     pParse->zErrMsg = 0;
119023     nErr++;
119024   }
119025   if( pParse->pVdbe && pParse->nErr>0 && pParse->nested==0 ){
119026     sqlite3VdbeDelete(pParse->pVdbe);
119027     pParse->pVdbe = 0;
119028   }
119029 #ifndef SQLITE_OMIT_SHARED_CACHE
119030   if( pParse->nested==0 ){
119031     sqlite3DbFree(db, pParse->aTableLock);
119032     pParse->aTableLock = 0;
119033     pParse->nTableLock = 0;
119034   }
119035 #endif
119036 #ifndef SQLITE_OMIT_VIRTUALTABLE
119037   sqlite3_free(pParse->apVtabLock);
119038 #endif
119039 
119040   if( !IN_DECLARE_VTAB ){
119041     /* If the pParse->declareVtab flag is set, do not delete any table
119042     ** structure built up in pParse->pNewTable. The calling code (see vtab.c)
119043     ** will take responsibility for freeing the Table structure.
119044     */
119045     sqlite3DeleteTable(db, pParse->pNewTable);
119046   }
119047 
119048   if( pParse->bFreeWith ) sqlite3WithDelete(db, pParse->pWith);
119049   sqlite3DeleteTrigger(db, pParse->pNewTrigger);
119050   for(i=pParse->nzVar-1; i>=0; i--) sqlite3DbFree(db, pParse->azVar[i]);
119051   sqlite3DbFree(db, pParse->azVar);
119052   while( pParse->pAinc ){
119053     AutoincInfo *p = pParse->pAinc;
119054     pParse->pAinc = p->pNext;
119055     sqlite3DbFree(db, p);
119056   }
119057   while( pParse->pZombieTab ){
119058     Table *p = pParse->pZombieTab;
119059     pParse->pZombieTab = p->pNextZombie;
119060     sqlite3DeleteTable(db, p);
119061   }
119062   if( nErr>0 && pParse->rc==SQLITE_OK ){
119063     pParse->rc = SQLITE_ERROR;
119064   }
119065   return nErr;
119066 }
119067 
119068 /************** End of tokenize.c ********************************************/
119069 /************** Begin file complete.c ****************************************/
119070 /*
119071 ** 2001 September 15
119072 **
119073 ** The author disclaims copyright to this source code.  In place of
119074 ** a legal notice, here is a blessing:
119075 **
119076 **    May you do good and not evil.
119077 **    May you find forgiveness for yourself and forgive others.
119078 **    May you share freely, never taking more than you give.
119079 **
119080 *************************************************************************
119081 ** An tokenizer for SQL
119082 **
119083 ** This file contains C code that implements the sqlite3_complete() API.
119084 ** This code used to be part of the tokenizer.c source file.  But by
119085 ** separating it out, the code will be automatically omitted from
119086 ** static links that do not use it.
119087 */
119088 #ifndef SQLITE_OMIT_COMPLETE
119089 
119090 /*
119091 ** This is defined in tokenize.c.  We just have to import the definition.
119092 */
119093 #ifndef SQLITE_AMALGAMATION
119094 #ifdef SQLITE_ASCII
119095 #define IdChar(C)  ((sqlite3CtypeMap[(unsigned char)C]&0x46)!=0)
119096 #endif
119097 #ifdef SQLITE_EBCDIC
119098 SQLITE_PRIVATE const char sqlite3IsEbcdicIdChar[];
119099 #define IdChar(C)  (((c=C)>=0x42 && sqlite3IsEbcdicIdChar[c-0x40]))
119100 #endif
119101 #endif /* SQLITE_AMALGAMATION */
119102 
119103 
119104 /*
119105 ** Token types used by the sqlite3_complete() routine.  See the header
119106 ** comments on that procedure for additional information.
119107 */
119108 #define tkSEMI    0
119109 #define tkWS      1
119110 #define tkOTHER   2
119111 #ifndef SQLITE_OMIT_TRIGGER
119112 #define tkEXPLAIN 3
119113 #define tkCREATE  4
119114 #define tkTEMP    5
119115 #define tkTRIGGER 6
119116 #define tkEND     7
119117 #endif
119118 
119119 /*
119120 ** Return TRUE if the given SQL string ends in a semicolon.
119121 **
119122 ** Special handling is require for CREATE TRIGGER statements.
119123 ** Whenever the CREATE TRIGGER keywords are seen, the statement
119124 ** must end with ";END;".
119125 **
119126 ** This implementation uses a state machine with 8 states:
119127 **
119128 **   (0) INVALID   We have not yet seen a non-whitespace character.
119129 **
119130 **   (1) START     At the beginning or end of an SQL statement.  This routine
119131 **                 returns 1 if it ends in the START state and 0 if it ends
119132 **                 in any other state.
119133 **
119134 **   (2) NORMAL    We are in the middle of statement which ends with a single
119135 **                 semicolon.
119136 **
119137 **   (3) EXPLAIN   The keyword EXPLAIN has been seen at the beginning of
119138 **                 a statement.
119139 **
119140 **   (4) CREATE    The keyword CREATE has been seen at the beginning of a
119141 **                 statement, possibly preceeded by EXPLAIN and/or followed by
119142 **                 TEMP or TEMPORARY
119143 **
119144 **   (5) TRIGGER   We are in the middle of a trigger definition that must be
119145 **                 ended by a semicolon, the keyword END, and another semicolon.
119146 **
119147 **   (6) SEMI      We've seen the first semicolon in the ";END;" that occurs at
119148 **                 the end of a trigger definition.
119149 **
119150 **   (7) END       We've seen the ";END" of the ";END;" that occurs at the end
119151 **                 of a trigger difinition.
119152 **
119153 ** Transitions between states above are determined by tokens extracted
119154 ** from the input.  The following tokens are significant:
119155 **
119156 **   (0) tkSEMI      A semicolon.
119157 **   (1) tkWS        Whitespace.
119158 **   (2) tkOTHER     Any other SQL token.
119159 **   (3) tkEXPLAIN   The "explain" keyword.
119160 **   (4) tkCREATE    The "create" keyword.
119161 **   (5) tkTEMP      The "temp" or "temporary" keyword.
119162 **   (6) tkTRIGGER   The "trigger" keyword.
119163 **   (7) tkEND       The "end" keyword.
119164 **
119165 ** Whitespace never causes a state transition and is always ignored.
119166 ** This means that a SQL string of all whitespace is invalid.
119167 **
119168 ** If we compile with SQLITE_OMIT_TRIGGER, all of the computation needed
119169 ** to recognize the end of a trigger can be omitted.  All we have to do
119170 ** is look for a semicolon that is not part of an string or comment.
119171 */
119172 SQLITE_API int sqlite3_complete(const char *zSql){
119173   u8 state = 0;   /* Current state, using numbers defined in header comment */
119174   u8 token;       /* Value of the next token */
119175 
119176 #ifndef SQLITE_OMIT_TRIGGER
119177   /* A complex statement machine used to detect the end of a CREATE TRIGGER
119178   ** statement.  This is the normal case.
119179   */
119180   static const u8 trans[8][8] = {
119181                      /* Token:                                                */
119182      /* State:       **  SEMI  WS  OTHER  EXPLAIN  CREATE  TEMP  TRIGGER  END */
119183      /* 0 INVALID: */ {    1,  0,     2,       3,      4,    2,       2,   2, },
119184      /* 1   START: */ {    1,  1,     2,       3,      4,    2,       2,   2, },
119185      /* 2  NORMAL: */ {    1,  2,     2,       2,      2,    2,       2,   2, },
119186      /* 3 EXPLAIN: */ {    1,  3,     3,       2,      4,    2,       2,   2, },
119187      /* 4  CREATE: */ {    1,  4,     2,       2,      2,    4,       5,   2, },
119188      /* 5 TRIGGER: */ {    6,  5,     5,       5,      5,    5,       5,   5, },
119189      /* 6    SEMI: */ {    6,  6,     5,       5,      5,    5,       5,   7, },
119190      /* 7     END: */ {    1,  7,     5,       5,      5,    5,       5,   5, },
119191   };
119192 #else
119193   /* If triggers are not supported by this compile then the statement machine
119194   ** used to detect the end of a statement is much simplier
119195   */
119196   static const u8 trans[3][3] = {
119197                      /* Token:           */
119198      /* State:       **  SEMI  WS  OTHER */
119199      /* 0 INVALID: */ {    1,  0,     2, },
119200      /* 1   START: */ {    1,  1,     2, },
119201      /* 2  NORMAL: */ {    1,  2,     2, },
119202   };
119203 #endif /* SQLITE_OMIT_TRIGGER */
119204 
119205   while( *zSql ){
119206     switch( *zSql ){
119207       case ';': {  /* A semicolon */
119208         token = tkSEMI;
119209         break;
119210       }
119211       case ' ':
119212       case '\r':
119213       case '\t':
119214       case '\n':
119215       case '\f': {  /* White space is ignored */
119216         token = tkWS;
119217         break;
119218       }
119219       case '/': {   /* C-style comments */
119220         if( zSql[1]!='*' ){
119221           token = tkOTHER;
119222           break;
119223         }
119224         zSql += 2;
119225         while( zSql[0] && (zSql[0]!='*' || zSql[1]!='/') ){ zSql++; }
119226         if( zSql[0]==0 ) return 0;
119227         zSql++;
119228         token = tkWS;
119229         break;
119230       }
119231       case '-': {   /* SQL-style comments from "--" to end of line */
119232         if( zSql[1]!='-' ){
119233           token = tkOTHER;
119234           break;
119235         }
119236         while( *zSql && *zSql!='\n' ){ zSql++; }
119237         if( *zSql==0 ) return state==1;
119238         token = tkWS;
119239         break;
119240       }
119241       case '[': {   /* Microsoft-style identifiers in [...] */
119242         zSql++;
119243         while( *zSql && *zSql!=']' ){ zSql++; }
119244         if( *zSql==0 ) return 0;
119245         token = tkOTHER;
119246         break;
119247       }
119248       case '`':     /* Grave-accent quoted symbols used by MySQL */
119249       case '"':     /* single- and double-quoted strings */
119250       case '\'': {
119251         int c = *zSql;
119252         zSql++;
119253         while( *zSql && *zSql!=c ){ zSql++; }
119254         if( *zSql==0 ) return 0;
119255         token = tkOTHER;
119256         break;
119257       }
119258       default: {
119259 #ifdef SQLITE_EBCDIC
119260         unsigned char c;
119261 #endif
119262         if( IdChar((u8)*zSql) ){
119263           /* Keywords and unquoted identifiers */
119264           int nId;
119265           for(nId=1; IdChar(zSql[nId]); nId++){}
119266 #ifdef SQLITE_OMIT_TRIGGER
119267           token = tkOTHER;
119268 #else
119269           switch( *zSql ){
119270             case 'c': case 'C': {
119271               if( nId==6 && sqlite3StrNICmp(zSql, "create", 6)==0 ){
119272                 token = tkCREATE;
119273               }else{
119274                 token = tkOTHER;
119275               }
119276               break;
119277             }
119278             case 't': case 'T': {
119279               if( nId==7 && sqlite3StrNICmp(zSql, "trigger", 7)==0 ){
119280                 token = tkTRIGGER;
119281               }else if( nId==4 && sqlite3StrNICmp(zSql, "temp", 4)==0 ){
119282                 token = tkTEMP;
119283               }else if( nId==9 && sqlite3StrNICmp(zSql, "temporary", 9)==0 ){
119284                 token = tkTEMP;
119285               }else{
119286                 token = tkOTHER;
119287               }
119288               break;
119289             }
119290             case 'e':  case 'E': {
119291               if( nId==3 && sqlite3StrNICmp(zSql, "end", 3)==0 ){
119292                 token = tkEND;
119293               }else
119294 #ifndef SQLITE_OMIT_EXPLAIN
119295               if( nId==7 && sqlite3StrNICmp(zSql, "explain", 7)==0 ){
119296                 token = tkEXPLAIN;
119297               }else
119298 #endif
119299               {
119300                 token = tkOTHER;
119301               }
119302               break;
119303             }
119304             default: {
119305               token = tkOTHER;
119306               break;
119307             }
119308           }
119309 #endif /* SQLITE_OMIT_TRIGGER */
119310           zSql += nId-1;
119311         }else{
119312           /* Operators and special symbols */
119313           token = tkOTHER;
119314         }
119315         break;
119316       }
119317     }
119318     state = trans[state][token];
119319     zSql++;
119320   }
119321   return state==1;
119322 }
119323 
119324 #ifndef SQLITE_OMIT_UTF16
119325 /*
119326 ** This routine is the same as the sqlite3_complete() routine described
119327 ** above, except that the parameter is required to be UTF-16 encoded, not
119328 ** UTF-8.
119329 */
119330 SQLITE_API int sqlite3_complete16(const void *zSql){
119331   sqlite3_value *pVal;
119332   char const *zSql8;
119333   int rc = SQLITE_NOMEM;
119334 
119335 #ifndef SQLITE_OMIT_AUTOINIT
119336   rc = sqlite3_initialize();
119337   if( rc ) return rc;
119338 #endif
119339   pVal = sqlite3ValueNew(0);
119340   sqlite3ValueSetStr(pVal, -1, zSql, SQLITE_UTF16NATIVE, SQLITE_STATIC);
119341   zSql8 = sqlite3ValueText(pVal, SQLITE_UTF8);
119342   if( zSql8 ){
119343     rc = sqlite3_complete(zSql8);
119344   }else{
119345     rc = SQLITE_NOMEM;
119346   }
119347   sqlite3ValueFree(pVal);
119348   return sqlite3ApiExit(0, rc);
119349 }
119350 #endif /* SQLITE_OMIT_UTF16 */
119351 #endif /* SQLITE_OMIT_COMPLETE */
119352 
119353 /************** End of complete.c ********************************************/
119354 /************** Begin file main.c ********************************************/
119355 /*
119356 ** 2001 September 15
119357 **
119358 ** The author disclaims copyright to this source code.  In place of
119359 ** a legal notice, here is a blessing:
119360 **
119361 **    May you do good and not evil.
119362 **    May you find forgiveness for yourself and forgive others.
119363 **    May you share freely, never taking more than you give.
119364 **
119365 *************************************************************************
119366 ** Main file for the SQLite library.  The routines in this file
119367 ** implement the programmer interface to the library.  Routines in
119368 ** other files are for internal use by SQLite and should not be
119369 ** accessed by users of the library.
119370 */
119371 
119372 #ifdef SQLITE_ENABLE_FTS3
119373 /************** Include fts3.h in the middle of main.c ***********************/
119374 /************** Begin file fts3.h ********************************************/
119375 /*
119376 ** 2006 Oct 10
119377 **
119378 ** The author disclaims copyright to this source code.  In place of
119379 ** a legal notice, here is a blessing:
119380 **
119381 **    May you do good and not evil.
119382 **    May you find forgiveness for yourself and forgive others.
119383 **    May you share freely, never taking more than you give.
119384 **
119385 ******************************************************************************
119386 **
119387 ** This header file is used by programs that want to link against the
119388 ** FTS3 library.  All it does is declare the sqlite3Fts3Init() interface.
119389 */
119390 
119391 #if 0
119392 extern "C" {
119393 #endif  /* __cplusplus */
119394 
119395 SQLITE_PRIVATE int sqlite3Fts3Init(sqlite3 *db);
119396 
119397 #if 0
119398 }  /* extern "C" */
119399 #endif  /* __cplusplus */
119400 
119401 /************** End of fts3.h ************************************************/
119402 /************** Continuing where we left off in main.c ***********************/
119403 #endif
119404 #ifdef SQLITE_ENABLE_RTREE
119405 /************** Include rtree.h in the middle of main.c **********************/
119406 /************** Begin file rtree.h *******************************************/
119407 /*
119408 ** 2008 May 26
119409 **
119410 ** The author disclaims copyright to this source code.  In place of
119411 ** a legal notice, here is a blessing:
119412 **
119413 **    May you do good and not evil.
119414 **    May you find forgiveness for yourself and forgive others.
119415 **    May you share freely, never taking more than you give.
119416 **
119417 ******************************************************************************
119418 **
119419 ** This header file is used by programs that want to link against the
119420 ** RTREE library.  All it does is declare the sqlite3RtreeInit() interface.
119421 */
119422 
119423 #if 0
119424 extern "C" {
119425 #endif  /* __cplusplus */
119426 
119427 SQLITE_PRIVATE int sqlite3RtreeInit(sqlite3 *db);
119428 
119429 #if 0
119430 }  /* extern "C" */
119431 #endif  /* __cplusplus */
119432 
119433 /************** End of rtree.h ***********************************************/
119434 /************** Continuing where we left off in main.c ***********************/
119435 #endif
119436 #ifdef SQLITE_ENABLE_ICU
119437 /************** Include sqliteicu.h in the middle of main.c ******************/
119438 /************** Begin file sqliteicu.h ***************************************/
119439 /*
119440 ** 2008 May 26
119441 **
119442 ** The author disclaims copyright to this source code.  In place of
119443 ** a legal notice, here is a blessing:
119444 **
119445 **    May you do good and not evil.
119446 **    May you find forgiveness for yourself and forgive others.
119447 **    May you share freely, never taking more than you give.
119448 **
119449 ******************************************************************************
119450 **
119451 ** This header file is used by programs that want to link against the
119452 ** ICU extension.  All it does is declare the sqlite3IcuInit() interface.
119453 */
119454 
119455 #if 0
119456 extern "C" {
119457 #endif  /* __cplusplus */
119458 
119459 SQLITE_PRIVATE int sqlite3IcuInit(sqlite3 *db);
119460 
119461 #if 0
119462 }  /* extern "C" */
119463 #endif  /* __cplusplus */
119464 
119465 
119466 /************** End of sqliteicu.h *******************************************/
119467 /************** Continuing where we left off in main.c ***********************/
119468 #endif
119469 
119470 #ifndef SQLITE_AMALGAMATION
119471 /* IMPLEMENTATION-OF: R-46656-45156 The sqlite3_version[] string constant
119472 ** contains the text of SQLITE_VERSION macro.
119473 */
119474 SQLITE_API const char sqlite3_version[] = SQLITE_VERSION;
119475 #endif
119476 
119477 /* IMPLEMENTATION-OF: R-53536-42575 The sqlite3_libversion() function returns
119478 ** a pointer to the to the sqlite3_version[] string constant.
119479 */
119480 SQLITE_API const char *sqlite3_libversion(void){ return sqlite3_version; }
119481 
119482 /* IMPLEMENTATION-OF: R-63124-39300 The sqlite3_sourceid() function returns a
119483 ** pointer to a string constant whose value is the same as the
119484 ** SQLITE_SOURCE_ID C preprocessor macro.
119485 */
119486 SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; }
119487 
119488 /* IMPLEMENTATION-OF: R-35210-63508 The sqlite3_libversion_number() function
119489 ** returns an integer equal to SQLITE_VERSION_NUMBER.
119490 */
119491 SQLITE_API int sqlite3_libversion_number(void){ return SQLITE_VERSION_NUMBER; }
119492 
119493 /* IMPLEMENTATION-OF: R-20790-14025 The sqlite3_threadsafe() function returns
119494 ** zero if and only if SQLite was compiled with mutexing code omitted due to
119495 ** the SQLITE_THREADSAFE compile-time option being set to 0.
119496 */
119497 SQLITE_API int sqlite3_threadsafe(void){ return SQLITE_THREADSAFE; }
119498 
119499 #if !defined(SQLITE_OMIT_TRACE) && defined(SQLITE_ENABLE_IOTRACE)
119500 /*
119501 ** If the following function pointer is not NULL and if
119502 ** SQLITE_ENABLE_IOTRACE is enabled, then messages describing
119503 ** I/O active are written using this function.  These messages
119504 ** are intended for debugging activity only.
119505 */
119506 SQLITE_PRIVATE void (*sqlite3IoTrace)(const char*, ...) = 0;
119507 #endif
119508 
119509 /*
119510 ** If the following global variable points to a string which is the
119511 ** name of a directory, then that directory will be used to store
119512 ** temporary files.
119513 **
119514 ** See also the "PRAGMA temp_store_directory" SQL command.
119515 */
119516 SQLITE_API char *sqlite3_temp_directory = 0;
119517 
119518 /*
119519 ** If the following global variable points to a string which is the
119520 ** name of a directory, then that directory will be used to store
119521 ** all database files specified with a relative pathname.
119522 **
119523 ** See also the "PRAGMA data_store_directory" SQL command.
119524 */
119525 SQLITE_API char *sqlite3_data_directory = 0;
119526 
119527 /*
119528 ** Initialize SQLite.
119529 **
119530 ** This routine must be called to initialize the memory allocation,
119531 ** VFS, and mutex subsystems prior to doing any serious work with
119532 ** SQLite.  But as long as you do not compile with SQLITE_OMIT_AUTOINIT
119533 ** this routine will be called automatically by key routines such as
119534 ** sqlite3_open().
119535 **
119536 ** This routine is a no-op except on its very first call for the process,
119537 ** or for the first call after a call to sqlite3_shutdown.
119538 **
119539 ** The first thread to call this routine runs the initialization to
119540 ** completion.  If subsequent threads call this routine before the first
119541 ** thread has finished the initialization process, then the subsequent
119542 ** threads must block until the first thread finishes with the initialization.
119543 **
119544 ** The first thread might call this routine recursively.  Recursive
119545 ** calls to this routine should not block, of course.  Otherwise the
119546 ** initialization process would never complete.
119547 **
119548 ** Let X be the first thread to enter this routine.  Let Y be some other
119549 ** thread.  Then while the initial invocation of this routine by X is
119550 ** incomplete, it is required that:
119551 **
119552 **    *  Calls to this routine from Y must block until the outer-most
119553 **       call by X completes.
119554 **
119555 **    *  Recursive calls to this routine from thread X return immediately
119556 **       without blocking.
119557 */
119558 SQLITE_API int sqlite3_initialize(void){
119559   MUTEX_LOGIC( sqlite3_mutex *pMaster; )       /* The main static mutex */
119560   int rc;                                      /* Result code */
119561 #ifdef SQLITE_EXTRA_INIT
119562   int bRunExtraInit = 0;                       /* Extra initialization needed */
119563 #endif
119564 
119565 #ifdef SQLITE_OMIT_WSD
119566   rc = sqlite3_wsd_init(4096, 24);
119567   if( rc!=SQLITE_OK ){
119568     return rc;
119569   }
119570 #endif
119571 
119572   /* If SQLite is already completely initialized, then this call
119573   ** to sqlite3_initialize() should be a no-op.  But the initialization
119574   ** must be complete.  So isInit must not be set until the very end
119575   ** of this routine.
119576   */
119577   if( sqlite3GlobalConfig.isInit ) return SQLITE_OK;
119578 
119579   /* Make sure the mutex subsystem is initialized.  If unable to
119580   ** initialize the mutex subsystem, return early with the error.
119581   ** If the system is so sick that we are unable to allocate a mutex,
119582   ** there is not much SQLite is going to be able to do.
119583   **
119584   ** The mutex subsystem must take care of serializing its own
119585   ** initialization.
119586   */
119587   rc = sqlite3MutexInit();
119588   if( rc ) return rc;
119589 
119590   /* Initialize the malloc() system and the recursive pInitMutex mutex.
119591   ** This operation is protected by the STATIC_MASTER mutex.  Note that
119592   ** MutexAlloc() is called for a static mutex prior to initializing the
119593   ** malloc subsystem - this implies that the allocation of a static
119594   ** mutex must not require support from the malloc subsystem.
119595   */
119596   MUTEX_LOGIC( pMaster = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); )
119597   sqlite3_mutex_enter(pMaster);
119598   sqlite3GlobalConfig.isMutexInit = 1;
119599   if( !sqlite3GlobalConfig.isMallocInit ){
119600     rc = sqlite3MallocInit();
119601   }
119602   if( rc==SQLITE_OK ){
119603     sqlite3GlobalConfig.isMallocInit = 1;
119604     if( !sqlite3GlobalConfig.pInitMutex ){
119605       sqlite3GlobalConfig.pInitMutex =
119606            sqlite3MutexAlloc(SQLITE_MUTEX_RECURSIVE);
119607       if( sqlite3GlobalConfig.bCoreMutex && !sqlite3GlobalConfig.pInitMutex ){
119608         rc = SQLITE_NOMEM;
119609       }
119610     }
119611   }
119612   if( rc==SQLITE_OK ){
119613     sqlite3GlobalConfig.nRefInitMutex++;
119614   }
119615   sqlite3_mutex_leave(pMaster);
119616 
119617   /* If rc is not SQLITE_OK at this point, then either the malloc
119618   ** subsystem could not be initialized or the system failed to allocate
119619   ** the pInitMutex mutex. Return an error in either case.  */
119620   if( rc!=SQLITE_OK ){
119621     return rc;
119622   }
119623 
119624   /* Do the rest of the initialization under the recursive mutex so
119625   ** that we will be able to handle recursive calls into
119626   ** sqlite3_initialize().  The recursive calls normally come through
119627   ** sqlite3_os_init() when it invokes sqlite3_vfs_register(), but other
119628   ** recursive calls might also be possible.
119629   **
119630   ** IMPLEMENTATION-OF: R-00140-37445 SQLite automatically serializes calls
119631   ** to the xInit method, so the xInit method need not be threadsafe.
119632   **
119633   ** The following mutex is what serializes access to the appdef pcache xInit
119634   ** methods.  The sqlite3_pcache_methods.xInit() all is embedded in the
119635   ** call to sqlite3PcacheInitialize().
119636   */
119637   sqlite3_mutex_enter(sqlite3GlobalConfig.pInitMutex);
119638   if( sqlite3GlobalConfig.isInit==0 && sqlite3GlobalConfig.inProgress==0 ){
119639     FuncDefHash *pHash = &GLOBAL(FuncDefHash, sqlite3GlobalFunctions);
119640     sqlite3GlobalConfig.inProgress = 1;
119641     memset(pHash, 0, sizeof(sqlite3GlobalFunctions));
119642     sqlite3RegisterGlobalFunctions();
119643     if( sqlite3GlobalConfig.isPCacheInit==0 ){
119644       rc = sqlite3PcacheInitialize();
119645     }
119646     if( rc==SQLITE_OK ){
119647       sqlite3GlobalConfig.isPCacheInit = 1;
119648       rc = sqlite3OsInit();
119649     }
119650     if( rc==SQLITE_OK ){
119651       sqlite3PCacheBufferSetup( sqlite3GlobalConfig.pPage,
119652           sqlite3GlobalConfig.szPage, sqlite3GlobalConfig.nPage);
119653       sqlite3GlobalConfig.isInit = 1;
119654 #ifdef SQLITE_EXTRA_INIT
119655       bRunExtraInit = 1;
119656 #endif
119657     }
119658     sqlite3GlobalConfig.inProgress = 0;
119659   }
119660   sqlite3_mutex_leave(sqlite3GlobalConfig.pInitMutex);
119661 
119662   /* Go back under the static mutex and clean up the recursive
119663   ** mutex to prevent a resource leak.
119664   */
119665   sqlite3_mutex_enter(pMaster);
119666   sqlite3GlobalConfig.nRefInitMutex--;
119667   if( sqlite3GlobalConfig.nRefInitMutex<=0 ){
119668     assert( sqlite3GlobalConfig.nRefInitMutex==0 );
119669     sqlite3_mutex_free(sqlite3GlobalConfig.pInitMutex);
119670     sqlite3GlobalConfig.pInitMutex = 0;
119671   }
119672   sqlite3_mutex_leave(pMaster);
119673 
119674   /* The following is just a sanity check to make sure SQLite has
119675   ** been compiled correctly.  It is important to run this code, but
119676   ** we don't want to run it too often and soak up CPU cycles for no
119677   ** reason.  So we run it once during initialization.
119678   */
119679 #ifndef NDEBUG
119680 #ifndef SQLITE_OMIT_FLOATING_POINT
119681   /* This section of code's only "output" is via assert() statements. */
119682   if ( rc==SQLITE_OK ){
119683     u64 x = (((u64)1)<<63)-1;
119684     double y;
119685     assert(sizeof(x)==8);
119686     assert(sizeof(x)==sizeof(y));
119687     memcpy(&y, &x, 8);
119688     assert( sqlite3IsNaN(y) );
119689   }
119690 #endif
119691 #endif
119692 
119693   /* Do extra initialization steps requested by the SQLITE_EXTRA_INIT
119694   ** compile-time option.
119695   */
119696 #ifdef SQLITE_EXTRA_INIT
119697   if( bRunExtraInit ){
119698     int SQLITE_EXTRA_INIT(const char*);
119699     rc = SQLITE_EXTRA_INIT(0);
119700   }
119701 #endif
119702 
119703   return rc;
119704 }
119705 
119706 /*
119707 ** Undo the effects of sqlite3_initialize().  Must not be called while
119708 ** there are outstanding database connections or memory allocations or
119709 ** while any part of SQLite is otherwise in use in any thread.  This
119710 ** routine is not threadsafe.  But it is safe to invoke this routine
119711 ** on when SQLite is already shut down.  If SQLite is already shut down
119712 ** when this routine is invoked, then this routine is a harmless no-op.
119713 */
119714 SQLITE_API int sqlite3_shutdown(void){
119715   if( sqlite3GlobalConfig.isInit ){
119716 #ifdef SQLITE_EXTRA_SHUTDOWN
119717     void SQLITE_EXTRA_SHUTDOWN(void);
119718     SQLITE_EXTRA_SHUTDOWN();
119719 #endif
119720     sqlite3_os_end();
119721     sqlite3_reset_auto_extension();
119722     sqlite3GlobalConfig.isInit = 0;
119723   }
119724   if( sqlite3GlobalConfig.isPCacheInit ){
119725     sqlite3PcacheShutdown();
119726     sqlite3GlobalConfig.isPCacheInit = 0;
119727   }
119728   if( sqlite3GlobalConfig.isMallocInit ){
119729     sqlite3MallocEnd();
119730     sqlite3GlobalConfig.isMallocInit = 0;
119731 
119732 #ifndef SQLITE_OMIT_SHUTDOWN_DIRECTORIES
119733     /* The heap subsystem has now been shutdown and these values are supposed
119734     ** to be NULL or point to memory that was obtained from sqlite3_malloc(),
119735     ** which would rely on that heap subsystem; therefore, make sure these
119736     ** values cannot refer to heap memory that was just invalidated when the
119737     ** heap subsystem was shutdown.  This is only done if the current call to
119738     ** this function resulted in the heap subsystem actually being shutdown.
119739     */
119740     sqlite3_data_directory = 0;
119741     sqlite3_temp_directory = 0;
119742 #endif
119743   }
119744   if( sqlite3GlobalConfig.isMutexInit ){
119745     sqlite3MutexEnd();
119746     sqlite3GlobalConfig.isMutexInit = 0;
119747   }
119748 
119749   return SQLITE_OK;
119750 }
119751 
119752 /*
119753 ** This API allows applications to modify the global configuration of
119754 ** the SQLite library at run-time.
119755 **
119756 ** This routine should only be called when there are no outstanding
119757 ** database connections or memory allocations.  This routine is not
119758 ** threadsafe.  Failure to heed these warnings can lead to unpredictable
119759 ** behavior.
119760 */
119761 SQLITE_API int sqlite3_config(int op, ...){
119762   va_list ap;
119763   int rc = SQLITE_OK;
119764 
119765   /* sqlite3_config() shall return SQLITE_MISUSE if it is invoked while
119766   ** the SQLite library is in use. */
119767   if( sqlite3GlobalConfig.isInit ) return SQLITE_MISUSE_BKPT;
119768 
119769   va_start(ap, op);
119770   switch( op ){
119771 
119772     /* Mutex configuration options are only available in a threadsafe
119773     ** compile.
119774     */
119775 #if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE>0
119776     case SQLITE_CONFIG_SINGLETHREAD: {
119777       /* Disable all mutexing */
119778       sqlite3GlobalConfig.bCoreMutex = 0;
119779       sqlite3GlobalConfig.bFullMutex = 0;
119780       break;
119781     }
119782     case SQLITE_CONFIG_MULTITHREAD: {
119783       /* Disable mutexing of database connections */
119784       /* Enable mutexing of core data structures */
119785       sqlite3GlobalConfig.bCoreMutex = 1;
119786       sqlite3GlobalConfig.bFullMutex = 0;
119787       break;
119788     }
119789     case SQLITE_CONFIG_SERIALIZED: {
119790       /* Enable all mutexing */
119791       sqlite3GlobalConfig.bCoreMutex = 1;
119792       sqlite3GlobalConfig.bFullMutex = 1;
119793       break;
119794     }
119795     case SQLITE_CONFIG_MUTEX: {
119796       /* Specify an alternative mutex implementation */
119797       sqlite3GlobalConfig.mutex = *va_arg(ap, sqlite3_mutex_methods*);
119798       break;
119799     }
119800     case SQLITE_CONFIG_GETMUTEX: {
119801       /* Retrieve the current mutex implementation */
119802       *va_arg(ap, sqlite3_mutex_methods*) = sqlite3GlobalConfig.mutex;
119803       break;
119804     }
119805 #endif
119806 
119807 
119808     case SQLITE_CONFIG_MALLOC: {
119809       /* Specify an alternative malloc implementation */
119810       sqlite3GlobalConfig.m = *va_arg(ap, sqlite3_mem_methods*);
119811       break;
119812     }
119813     case SQLITE_CONFIG_GETMALLOC: {
119814       /* Retrieve the current malloc() implementation */
119815       if( sqlite3GlobalConfig.m.xMalloc==0 ) sqlite3MemSetDefault();
119816       *va_arg(ap, sqlite3_mem_methods*) = sqlite3GlobalConfig.m;
119817       break;
119818     }
119819     case SQLITE_CONFIG_MEMSTATUS: {
119820       /* Enable or disable the malloc status collection */
119821       sqlite3GlobalConfig.bMemstat = va_arg(ap, int);
119822       break;
119823     }
119824     case SQLITE_CONFIG_SCRATCH: {
119825       /* Designate a buffer for scratch memory space */
119826       sqlite3GlobalConfig.pScratch = va_arg(ap, void*);
119827       sqlite3GlobalConfig.szScratch = va_arg(ap, int);
119828       sqlite3GlobalConfig.nScratch = va_arg(ap, int);
119829       break;
119830     }
119831     case SQLITE_CONFIG_PAGECACHE: {
119832       /* Designate a buffer for page cache memory space */
119833       sqlite3GlobalConfig.pPage = va_arg(ap, void*);
119834       sqlite3GlobalConfig.szPage = va_arg(ap, int);
119835       sqlite3GlobalConfig.nPage = va_arg(ap, int);
119836       break;
119837     }
119838 
119839     case SQLITE_CONFIG_PCACHE: {
119840       /* no-op */
119841       break;
119842     }
119843     case SQLITE_CONFIG_GETPCACHE: {
119844       /* now an error */
119845       rc = SQLITE_ERROR;
119846       break;
119847     }
119848 
119849     case SQLITE_CONFIG_PCACHE2: {
119850       /* Specify an alternative page cache implementation */
119851       sqlite3GlobalConfig.pcache2 = *va_arg(ap, sqlite3_pcache_methods2*);
119852       break;
119853     }
119854     case SQLITE_CONFIG_GETPCACHE2: {
119855       if( sqlite3GlobalConfig.pcache2.xInit==0 ){
119856         sqlite3PCacheSetDefault();
119857       }
119858       *va_arg(ap, sqlite3_pcache_methods2*) = sqlite3GlobalConfig.pcache2;
119859       break;
119860     }
119861 
119862 #if defined(SQLITE_ENABLE_MEMSYS3) || defined(SQLITE_ENABLE_MEMSYS5)
119863     case SQLITE_CONFIG_HEAP: {
119864       /* Designate a buffer for heap memory space */
119865       sqlite3GlobalConfig.pHeap = va_arg(ap, void*);
119866       sqlite3GlobalConfig.nHeap = va_arg(ap, int);
119867       sqlite3GlobalConfig.mnReq = va_arg(ap, int);
119868 
119869       if( sqlite3GlobalConfig.mnReq<1 ){
119870         sqlite3GlobalConfig.mnReq = 1;
119871       }else if( sqlite3GlobalConfig.mnReq>(1<<12) ){
119872         /* cap min request size at 2^12 */
119873         sqlite3GlobalConfig.mnReq = (1<<12);
119874       }
119875 
119876       if( sqlite3GlobalConfig.pHeap==0 ){
119877         /* If the heap pointer is NULL, then restore the malloc implementation
119878         ** back to NULL pointers too.  This will cause the malloc to go
119879         ** back to its default implementation when sqlite3_initialize() is
119880         ** run.
119881         */
119882         memset(&sqlite3GlobalConfig.m, 0, sizeof(sqlite3GlobalConfig.m));
119883       }else{
119884         /* The heap pointer is not NULL, then install one of the
119885         ** mem5.c/mem3.c methods.  The enclosing #if guarantees at
119886         ** least one of these methods is currently enabled.
119887         */
119888 #ifdef SQLITE_ENABLE_MEMSYS3
119889         sqlite3GlobalConfig.m = *sqlite3MemGetMemsys3();
119890 #endif
119891 #ifdef SQLITE_ENABLE_MEMSYS5
119892         sqlite3GlobalConfig.m = *sqlite3MemGetMemsys5();
119893 #endif
119894       }
119895       break;
119896     }
119897 #endif
119898 
119899     case SQLITE_CONFIG_LOOKASIDE: {
119900       sqlite3GlobalConfig.szLookaside = va_arg(ap, int);
119901       sqlite3GlobalConfig.nLookaside = va_arg(ap, int);
119902       break;
119903     }
119904 
119905     /* Record a pointer to the logger function and its first argument.
119906     ** The default is NULL.  Logging is disabled if the function pointer is
119907     ** NULL.
119908     */
119909     case SQLITE_CONFIG_LOG: {
119910       /* MSVC is picky about pulling func ptrs from va lists.
119911       ** http://support.microsoft.com/kb/47961
119912       ** sqlite3GlobalConfig.xLog = va_arg(ap, void(*)(void*,int,const char*));
119913       */
119914       typedef void(*LOGFUNC_t)(void*,int,const char*);
119915       sqlite3GlobalConfig.xLog = va_arg(ap, LOGFUNC_t);
119916       sqlite3GlobalConfig.pLogArg = va_arg(ap, void*);
119917       break;
119918     }
119919 
119920     case SQLITE_CONFIG_URI: {
119921       sqlite3GlobalConfig.bOpenUri = va_arg(ap, int);
119922       break;
119923     }
119924 
119925     case SQLITE_CONFIG_COVERING_INDEX_SCAN: {
119926       sqlite3GlobalConfig.bUseCis = va_arg(ap, int);
119927       break;
119928     }
119929 
119930 #ifdef SQLITE_ENABLE_SQLLOG
119931     case SQLITE_CONFIG_SQLLOG: {
119932       typedef void(*SQLLOGFUNC_t)(void*, sqlite3*, const char*, int);
119933       sqlite3GlobalConfig.xSqllog = va_arg(ap, SQLLOGFUNC_t);
119934       sqlite3GlobalConfig.pSqllogArg = va_arg(ap, void *);
119935       break;
119936     }
119937 #endif
119938 
119939     case SQLITE_CONFIG_MMAP_SIZE: {
119940       sqlite3_int64 szMmap = va_arg(ap, sqlite3_int64);
119941       sqlite3_int64 mxMmap = va_arg(ap, sqlite3_int64);
119942       if( mxMmap<0 || mxMmap>SQLITE_MAX_MMAP_SIZE ){
119943         mxMmap = SQLITE_MAX_MMAP_SIZE;
119944       }
119945       sqlite3GlobalConfig.mxMmap = mxMmap;
119946       if( szMmap<0 ) szMmap = SQLITE_DEFAULT_MMAP_SIZE;
119947       if( szMmap>mxMmap) szMmap = mxMmap;
119948       sqlite3GlobalConfig.szMmap = szMmap;
119949       break;
119950     }
119951 
119952 #if SQLITE_OS_WIN && defined(SQLITE_WIN32_MALLOC)
119953     case SQLITE_CONFIG_WIN32_HEAPSIZE: {
119954       sqlite3GlobalConfig.nHeap = va_arg(ap, int);
119955       break;
119956     }
119957 #endif
119958 
119959     default: {
119960       rc = SQLITE_ERROR;
119961       break;
119962     }
119963   }
119964   va_end(ap);
119965   return rc;
119966 }
119967 
119968 /*
119969 ** Set up the lookaside buffers for a database connection.
119970 ** Return SQLITE_OK on success.
119971 ** If lookaside is already active, return SQLITE_BUSY.
119972 **
119973 ** The sz parameter is the number of bytes in each lookaside slot.
119974 ** The cnt parameter is the number of slots.  If pStart is NULL the
119975 ** space for the lookaside memory is obtained from sqlite3_malloc().
119976 ** If pStart is not NULL then it is sz*cnt bytes of memory to use for
119977 ** the lookaside memory.
119978 */
119979 static int setupLookaside(sqlite3 *db, void *pBuf, int sz, int cnt){
119980   void *pStart;
119981   if( db->lookaside.nOut ){
119982     return SQLITE_BUSY;
119983   }
119984   /* Free any existing lookaside buffer for this handle before
119985   ** allocating a new one so we don't have to have space for
119986   ** both at the same time.
119987   */
119988   if( db->lookaside.bMalloced ){
119989     sqlite3_free(db->lookaside.pStart);
119990   }
119991   /* The size of a lookaside slot after ROUNDDOWN8 needs to be larger
119992   ** than a pointer to be useful.
119993   */
119994   sz = ROUNDDOWN8(sz);  /* IMP: R-33038-09382 */
119995   if( sz<=(int)sizeof(LookasideSlot*) ) sz = 0;
119996   if( cnt<0 ) cnt = 0;
119997   if( sz==0 || cnt==0 ){
119998     sz = 0;
119999     pStart = 0;
120000   }else if( pBuf==0 ){
120001     sqlite3BeginBenignMalloc();
120002     pStart = sqlite3Malloc( sz*cnt );  /* IMP: R-61949-35727 */
120003     sqlite3EndBenignMalloc();
120004     if( pStart ) cnt = sqlite3MallocSize(pStart)/sz;
120005   }else{
120006     pStart = pBuf;
120007   }
120008   db->lookaside.pStart = pStart;
120009   db->lookaside.pFree = 0;
120010   db->lookaside.sz = (u16)sz;
120011   if( pStart ){
120012     int i;
120013     LookasideSlot *p;
120014     assert( sz > (int)sizeof(LookasideSlot*) );
120015     p = (LookasideSlot*)pStart;
120016     for(i=cnt-1; i>=0; i--){
120017       p->pNext = db->lookaside.pFree;
120018       db->lookaside.pFree = p;
120019       p = (LookasideSlot*)&((u8*)p)[sz];
120020     }
120021     db->lookaside.pEnd = p;
120022     db->lookaside.bEnabled = 1;
120023     db->lookaside.bMalloced = pBuf==0 ?1:0;
120024   }else{
120025     db->lookaside.pStart = db;
120026     db->lookaside.pEnd = db;
120027     db->lookaside.bEnabled = 0;
120028     db->lookaside.bMalloced = 0;
120029   }
120030   return SQLITE_OK;
120031 }
120032 
120033 /*
120034 ** Return the mutex associated with a database connection.
120035 */
120036 SQLITE_API sqlite3_mutex *sqlite3_db_mutex(sqlite3 *db){
120037   return db->mutex;
120038 }
120039 
120040 /*
120041 ** Free up as much memory as we can from the given database
120042 ** connection.
120043 */
120044 SQLITE_API int sqlite3_db_release_memory(sqlite3 *db){
120045   int i;
120046   sqlite3_mutex_enter(db->mutex);
120047   sqlite3BtreeEnterAll(db);
120048   for(i=0; i<db->nDb; i++){
120049     Btree *pBt = db->aDb[i].pBt;
120050     if( pBt ){
120051       Pager *pPager = sqlite3BtreePager(pBt);
120052       sqlite3PagerShrink(pPager);
120053     }
120054   }
120055   sqlite3BtreeLeaveAll(db);
120056   sqlite3_mutex_leave(db->mutex);
120057   return SQLITE_OK;
120058 }
120059 
120060 /*
120061 ** Configuration settings for an individual database connection
120062 */
120063 SQLITE_API int sqlite3_db_config(sqlite3 *db, int op, ...){
120064   va_list ap;
120065   int rc;
120066   va_start(ap, op);
120067   switch( op ){
120068     case SQLITE_DBCONFIG_LOOKASIDE: {
120069       void *pBuf = va_arg(ap, void*); /* IMP: R-26835-10964 */
120070       int sz = va_arg(ap, int);       /* IMP: R-47871-25994 */
120071       int cnt = va_arg(ap, int);      /* IMP: R-04460-53386 */
120072       rc = setupLookaside(db, pBuf, sz, cnt);
120073       break;
120074     }
120075     default: {
120076       static const struct {
120077         int op;      /* The opcode */
120078         u32 mask;    /* Mask of the bit in sqlite3.flags to set/clear */
120079       } aFlagOp[] = {
120080         { SQLITE_DBCONFIG_ENABLE_FKEY,    SQLITE_ForeignKeys    },
120081         { SQLITE_DBCONFIG_ENABLE_TRIGGER, SQLITE_EnableTrigger  },
120082       };
120083       unsigned int i;
120084       rc = SQLITE_ERROR; /* IMP: R-42790-23372 */
120085       for(i=0; i<ArraySize(aFlagOp); i++){
120086         if( aFlagOp[i].op==op ){
120087           int onoff = va_arg(ap, int);
120088           int *pRes = va_arg(ap, int*);
120089           int oldFlags = db->flags;
120090           if( onoff>0 ){
120091             db->flags |= aFlagOp[i].mask;
120092           }else if( onoff==0 ){
120093             db->flags &= ~aFlagOp[i].mask;
120094           }
120095           if( oldFlags!=db->flags ){
120096             sqlite3ExpirePreparedStatements(db);
120097           }
120098           if( pRes ){
120099             *pRes = (db->flags & aFlagOp[i].mask)!=0;
120100           }
120101           rc = SQLITE_OK;
120102           break;
120103         }
120104       }
120105       break;
120106     }
120107   }
120108   va_end(ap);
120109   return rc;
120110 }
120111 
120112 
120113 /*
120114 ** Return true if the buffer z[0..n-1] contains all spaces.
120115 */
120116 static int allSpaces(const char *z, int n){
120117   while( n>0 && z[n-1]==' ' ){ n--; }
120118   return n==0;
120119 }
120120 
120121 /*
120122 ** This is the default collating function named "BINARY" which is always
120123 ** available.
120124 **
120125 ** If the padFlag argument is not NULL then space padding at the end
120126 ** of strings is ignored.  This implements the RTRIM collation.
120127 */
120128 static int binCollFunc(
120129   void *padFlag,
120130   int nKey1, const void *pKey1,
120131   int nKey2, const void *pKey2
120132 ){
120133   int rc, n;
120134   n = nKey1<nKey2 ? nKey1 : nKey2;
120135   rc = memcmp(pKey1, pKey2, n);
120136   if( rc==0 ){
120137     if( padFlag
120138      && allSpaces(((char*)pKey1)+n, nKey1-n)
120139      && allSpaces(((char*)pKey2)+n, nKey2-n)
120140     ){
120141       /* Leave rc unchanged at 0 */
120142     }else{
120143       rc = nKey1 - nKey2;
120144     }
120145   }
120146   return rc;
120147 }
120148 
120149 /*
120150 ** Another built-in collating sequence: NOCASE.
120151 **
120152 ** This collating sequence is intended to be used for "case independent
120153 ** comparison". SQLite's knowledge of upper and lower case equivalents
120154 ** extends only to the 26 characters used in the English language.
120155 **
120156 ** At the moment there is only a UTF-8 implementation.
120157 */
120158 static int nocaseCollatingFunc(
120159   void *NotUsed,
120160   int nKey1, const void *pKey1,
120161   int nKey2, const void *pKey2
120162 ){
120163   int r = sqlite3StrNICmp(
120164       (const char *)pKey1, (const char *)pKey2, (nKey1<nKey2)?nKey1:nKey2);
120165   UNUSED_PARAMETER(NotUsed);
120166   if( 0==r ){
120167     r = nKey1-nKey2;
120168   }
120169   return r;
120170 }
120171 
120172 /*
120173 ** Return the ROWID of the most recent insert
120174 */
120175 SQLITE_API sqlite_int64 sqlite3_last_insert_rowid(sqlite3 *db){
120176   return db->lastRowid;
120177 }
120178 
120179 /*
120180 ** Return the number of changes in the most recent call to sqlite3_exec().
120181 */
120182 SQLITE_API int sqlite3_changes(sqlite3 *db){
120183   return db->nChange;
120184 }
120185 
120186 /*
120187 ** Return the number of changes since the database handle was opened.
120188 */
120189 SQLITE_API int sqlite3_total_changes(sqlite3 *db){
120190   return db->nTotalChange;
120191 }
120192 
120193 /*
120194 ** Close all open savepoints. This function only manipulates fields of the
120195 ** database handle object, it does not close any savepoints that may be open
120196 ** at the b-tree/pager level.
120197 */
120198 SQLITE_PRIVATE void sqlite3CloseSavepoints(sqlite3 *db){
120199   while( db->pSavepoint ){
120200     Savepoint *pTmp = db->pSavepoint;
120201     db->pSavepoint = pTmp->pNext;
120202     sqlite3DbFree(db, pTmp);
120203   }
120204   db->nSavepoint = 0;
120205   db->nStatement = 0;
120206   db->isTransactionSavepoint = 0;
120207 }
120208 
120209 /*
120210 ** Invoke the destructor function associated with FuncDef p, if any. Except,
120211 ** if this is not the last copy of the function, do not invoke it. Multiple
120212 ** copies of a single function are created when create_function() is called
120213 ** with SQLITE_ANY as the encoding.
120214 */
120215 static void functionDestroy(sqlite3 *db, FuncDef *p){
120216   FuncDestructor *pDestructor = p->pDestructor;
120217   if( pDestructor ){
120218     pDestructor->nRef--;
120219     if( pDestructor->nRef==0 ){
120220       pDestructor->xDestroy(pDestructor->pUserData);
120221       sqlite3DbFree(db, pDestructor);
120222     }
120223   }
120224 }
120225 
120226 /*
120227 ** Disconnect all sqlite3_vtab objects that belong to database connection
120228 ** db. This is called when db is being closed.
120229 */
120230 static void disconnectAllVtab(sqlite3 *db){
120231 #ifndef SQLITE_OMIT_VIRTUALTABLE
120232   int i;
120233   sqlite3BtreeEnterAll(db);
120234   for(i=0; i<db->nDb; i++){
120235     Schema *pSchema = db->aDb[i].pSchema;
120236     if( db->aDb[i].pSchema ){
120237       HashElem *p;
120238       for(p=sqliteHashFirst(&pSchema->tblHash); p; p=sqliteHashNext(p)){
120239         Table *pTab = (Table *)sqliteHashData(p);
120240         if( IsVirtual(pTab) ) sqlite3VtabDisconnect(db, pTab);
120241       }
120242     }
120243   }
120244   sqlite3BtreeLeaveAll(db);
120245 #else
120246   UNUSED_PARAMETER(db);
120247 #endif
120248 }
120249 
120250 /*
120251 ** Return TRUE if database connection db has unfinalized prepared
120252 ** statements or unfinished sqlite3_backup objects.
120253 */
120254 static int connectionIsBusy(sqlite3 *db){
120255   int j;
120256   assert( sqlite3_mutex_held(db->mutex) );
120257   if( db->pVdbe ) return 1;
120258   for(j=0; j<db->nDb; j++){
120259     Btree *pBt = db->aDb[j].pBt;
120260     if( pBt && sqlite3BtreeIsInBackup(pBt) ) return 1;
120261   }
120262   return 0;
120263 }
120264 
120265 /*
120266 ** Close an existing SQLite database
120267 */
120268 static int sqlite3Close(sqlite3 *db, int forceZombie){
120269   if( !db ){
120270     return SQLITE_OK;
120271   }
120272   if( !sqlite3SafetyCheckSickOrOk(db) ){
120273     return SQLITE_MISUSE_BKPT;
120274   }
120275   sqlite3_mutex_enter(db->mutex);
120276 
120277   /* Force xDisconnect calls on all virtual tables */
120278   disconnectAllVtab(db);
120279 
120280   /* If a transaction is open, the disconnectAllVtab() call above
120281   ** will not have called the xDisconnect() method on any virtual
120282   ** tables in the db->aVTrans[] array. The following sqlite3VtabRollback()
120283   ** call will do so. We need to do this before the check for active
120284   ** SQL statements below, as the v-table implementation may be storing
120285   ** some prepared statements internally.
120286   */
120287   sqlite3VtabRollback(db);
120288 
120289   /* Legacy behavior (sqlite3_close() behavior) is to return
120290   ** SQLITE_BUSY if the connection can not be closed immediately.
120291   */
120292   if( !forceZombie && connectionIsBusy(db) ){
120293     sqlite3Error(db, SQLITE_BUSY, "unable to close due to unfinalized "
120294        "statements or unfinished backups");
120295     sqlite3_mutex_leave(db->mutex);
120296     return SQLITE_BUSY;
120297   }
120298 
120299 #ifdef SQLITE_ENABLE_SQLLOG
120300   if( sqlite3GlobalConfig.xSqllog ){
120301     /* Closing the handle. Fourth parameter is passed the value 2. */
120302     sqlite3GlobalConfig.xSqllog(sqlite3GlobalConfig.pSqllogArg, db, 0, 2);
120303   }
120304 #endif
120305 
120306   /* Convert the connection into a zombie and then close it.
120307   */
120308   db->magic = SQLITE_MAGIC_ZOMBIE;
120309   sqlite3LeaveMutexAndCloseZombie(db);
120310   return SQLITE_OK;
120311 }
120312 
120313 /*
120314 ** Two variations on the public interface for closing a database
120315 ** connection. The sqlite3_close() version returns SQLITE_BUSY and
120316 ** leaves the connection option if there are unfinalized prepared
120317 ** statements or unfinished sqlite3_backups.  The sqlite3_close_v2()
120318 ** version forces the connection to become a zombie if there are
120319 ** unclosed resources, and arranges for deallocation when the last
120320 ** prepare statement or sqlite3_backup closes.
120321 */
120322 SQLITE_API int sqlite3_close(sqlite3 *db){ return sqlite3Close(db,0); }
120323 SQLITE_API int sqlite3_close_v2(sqlite3 *db){ return sqlite3Close(db,1); }
120324 
120325 
120326 /*
120327 ** Close the mutex on database connection db.
120328 **
120329 ** Furthermore, if database connection db is a zombie (meaning that there
120330 ** has been a prior call to sqlite3_close(db) or sqlite3_close_v2(db)) and
120331 ** every sqlite3_stmt has now been finalized and every sqlite3_backup has
120332 ** finished, then free all resources.
120333 */
120334 SQLITE_PRIVATE void sqlite3LeaveMutexAndCloseZombie(sqlite3 *db){
120335   HashElem *i;                    /* Hash table iterator */
120336   int j;
120337 
120338   /* If there are outstanding sqlite3_stmt or sqlite3_backup objects
120339   ** or if the connection has not yet been closed by sqlite3_close_v2(),
120340   ** then just leave the mutex and return.
120341   */
120342   if( db->magic!=SQLITE_MAGIC_ZOMBIE || connectionIsBusy(db) ){
120343     sqlite3_mutex_leave(db->mutex);
120344     return;
120345   }
120346 
120347   /* If we reach this point, it means that the database connection has
120348   ** closed all sqlite3_stmt and sqlite3_backup objects and has been
120349   ** passed to sqlite3_close (meaning that it is a zombie).  Therefore,
120350   ** go ahead and free all resources.
120351   */
120352 
120353   /* If a transaction is open, roll it back. This also ensures that if
120354   ** any database schemas have been modified by an uncommitted transaction
120355   ** they are reset. And that the required b-tree mutex is held to make
120356   ** the pager rollback and schema reset an atomic operation. */
120357   sqlite3RollbackAll(db, SQLITE_OK);
120358 
120359   /* Free any outstanding Savepoint structures. */
120360   sqlite3CloseSavepoints(db);
120361 
120362   /* Close all database connections */
120363   for(j=0; j<db->nDb; j++){
120364     struct Db *pDb = &db->aDb[j];
120365     if( pDb->pBt ){
120366       sqlite3BtreeClose(pDb->pBt);
120367       pDb->pBt = 0;
120368       if( j!=1 ){
120369         pDb->pSchema = 0;
120370       }
120371     }
120372   }
120373   /* Clear the TEMP schema separately and last */
120374   if( db->aDb[1].pSchema ){
120375     sqlite3SchemaClear(db->aDb[1].pSchema);
120376   }
120377   sqlite3VtabUnlockList(db);
120378 
120379   /* Free up the array of auxiliary databases */
120380   sqlite3CollapseDatabaseArray(db);
120381   assert( db->nDb<=2 );
120382   assert( db->aDb==db->aDbStatic );
120383 
120384   /* Tell the code in notify.c that the connection no longer holds any
120385   ** locks and does not require any further unlock-notify callbacks.
120386   */
120387   sqlite3ConnectionClosed(db);
120388 
120389   for(j=0; j<ArraySize(db->aFunc.a); j++){
120390     FuncDef *pNext, *pHash, *p;
120391     for(p=db->aFunc.a[j]; p; p=pHash){
120392       pHash = p->pHash;
120393       while( p ){
120394         functionDestroy(db, p);
120395         pNext = p->pNext;
120396         sqlite3DbFree(db, p);
120397         p = pNext;
120398       }
120399     }
120400   }
120401   for(i=sqliteHashFirst(&db->aCollSeq); i; i=sqliteHashNext(i)){
120402     CollSeq *pColl = (CollSeq *)sqliteHashData(i);
120403     /* Invoke any destructors registered for collation sequence user data. */
120404     for(j=0; j<3; j++){
120405       if( pColl[j].xDel ){
120406         pColl[j].xDel(pColl[j].pUser);
120407       }
120408     }
120409     sqlite3DbFree(db, pColl);
120410   }
120411   sqlite3HashClear(&db->aCollSeq);
120412 #ifndef SQLITE_OMIT_VIRTUALTABLE
120413   for(i=sqliteHashFirst(&db->aModule); i; i=sqliteHashNext(i)){
120414     Module *pMod = (Module *)sqliteHashData(i);
120415     if( pMod->xDestroy ){
120416       pMod->xDestroy(pMod->pAux);
120417     }
120418     sqlite3DbFree(db, pMod);
120419   }
120420   sqlite3HashClear(&db->aModule);
120421 #endif
120422 
120423   sqlite3Error(db, SQLITE_OK, 0); /* Deallocates any cached error strings. */
120424   sqlite3ValueFree(db->pErr);
120425   sqlite3CloseExtensions(db);
120426 
120427   db->magic = SQLITE_MAGIC_ERROR;
120428 
120429   /* The temp-database schema is allocated differently from the other schema
120430   ** objects (using sqliteMalloc() directly, instead of sqlite3BtreeSchema()).
120431   ** So it needs to be freed here. Todo: Why not roll the temp schema into
120432   ** the same sqliteMalloc() as the one that allocates the database
120433   ** structure?
120434   */
120435   sqlite3DbFree(db, db->aDb[1].pSchema);
120436   sqlite3_mutex_leave(db->mutex);
120437   db->magic = SQLITE_MAGIC_CLOSED;
120438   sqlite3_mutex_free(db->mutex);
120439   assert( db->lookaside.nOut==0 );  /* Fails on a lookaside memory leak */
120440   if( db->lookaside.bMalloced ){
120441     sqlite3_free(db->lookaside.pStart);
120442   }
120443   sqlite3_free(db);
120444 }
120445 
120446 /*
120447 ** Rollback all database files.  If tripCode is not SQLITE_OK, then
120448 ** any open cursors are invalidated ("tripped" - as in "tripping a circuit
120449 ** breaker") and made to return tripCode if there are any further
120450 ** attempts to use that cursor.
120451 */
120452 SQLITE_PRIVATE void sqlite3RollbackAll(sqlite3 *db, int tripCode){
120453   int i;
120454   int inTrans = 0;
120455   assert( sqlite3_mutex_held(db->mutex) );
120456   sqlite3BeginBenignMalloc();
120457 
120458   /* Obtain all b-tree mutexes before making any calls to BtreeRollback().
120459   ** This is important in case the transaction being rolled back has
120460   ** modified the database schema. If the b-tree mutexes are not taken
120461   ** here, then another shared-cache connection might sneak in between
120462   ** the database rollback and schema reset, which can cause false
120463   ** corruption reports in some cases.  */
120464   sqlite3BtreeEnterAll(db);
120465 
120466   for(i=0; i<db->nDb; i++){
120467     Btree *p = db->aDb[i].pBt;
120468     if( p ){
120469       if( sqlite3BtreeIsInTrans(p) ){
120470         inTrans = 1;
120471       }
120472       sqlite3BtreeRollback(p, tripCode);
120473     }
120474   }
120475   sqlite3VtabRollback(db);
120476   sqlite3EndBenignMalloc();
120477 
120478   if( (db->flags&SQLITE_InternChanges)!=0 && db->init.busy==0 ){
120479     sqlite3ExpirePreparedStatements(db);
120480     sqlite3ResetAllSchemasOfConnection(db);
120481   }
120482   sqlite3BtreeLeaveAll(db);
120483 
120484   /* Any deferred constraint violations have now been resolved. */
120485   db->nDeferredCons = 0;
120486   db->nDeferredImmCons = 0;
120487   db->flags &= ~SQLITE_DeferFKs;
120488 
120489   /* If one has been configured, invoke the rollback-hook callback */
120490   if( db->xRollbackCallback && (inTrans || !db->autoCommit) ){
120491     db->xRollbackCallback(db->pRollbackArg);
120492   }
120493 }
120494 
120495 /*
120496 ** Return a static string containing the name corresponding to the error code
120497 ** specified in the argument.
120498 */
120499 #if defined(SQLITE_TEST)
120500 SQLITE_PRIVATE const char *sqlite3ErrName(int rc){
120501   const char *zName = 0;
120502   int i, origRc = rc;
120503   for(i=0; i<2 && zName==0; i++, rc &= 0xff){
120504     switch( rc ){
120505       case SQLITE_OK:                 zName = "SQLITE_OK";                break;
120506       case SQLITE_ERROR:              zName = "SQLITE_ERROR";             break;
120507       case SQLITE_INTERNAL:           zName = "SQLITE_INTERNAL";          break;
120508       case SQLITE_PERM:               zName = "SQLITE_PERM";              break;
120509       case SQLITE_ABORT:              zName = "SQLITE_ABORT";             break;
120510       case SQLITE_ABORT_ROLLBACK:     zName = "SQLITE_ABORT_ROLLBACK";    break;
120511       case SQLITE_BUSY:               zName = "SQLITE_BUSY";              break;
120512       case SQLITE_BUSY_RECOVERY:      zName = "SQLITE_BUSY_RECOVERY";     break;
120513       case SQLITE_BUSY_SNAPSHOT:      zName = "SQLITE_BUSY_SNAPSHOT";     break;
120514       case SQLITE_LOCKED:             zName = "SQLITE_LOCKED";            break;
120515       case SQLITE_LOCKED_SHAREDCACHE: zName = "SQLITE_LOCKED_SHAREDCACHE";break;
120516       case SQLITE_NOMEM:              zName = "SQLITE_NOMEM";             break;
120517       case SQLITE_READONLY:           zName = "SQLITE_READONLY";          break;
120518       case SQLITE_READONLY_RECOVERY:  zName = "SQLITE_READONLY_RECOVERY"; break;
120519       case SQLITE_READONLY_CANTLOCK:  zName = "SQLITE_READONLY_CANTLOCK"; break;
120520       case SQLITE_READONLY_ROLLBACK:  zName = "SQLITE_READONLY_ROLLBACK"; break;
120521       case SQLITE_READONLY_DBMOVED:   zName = "SQLITE_READONLY_DBMOVED";  break;
120522       case SQLITE_INTERRUPT:          zName = "SQLITE_INTERRUPT";         break;
120523       case SQLITE_IOERR:              zName = "SQLITE_IOERR";             break;
120524       case SQLITE_IOERR_READ:         zName = "SQLITE_IOERR_READ";        break;
120525       case SQLITE_IOERR_SHORT_READ:   zName = "SQLITE_IOERR_SHORT_READ";  break;
120526       case SQLITE_IOERR_WRITE:        zName = "SQLITE_IOERR_WRITE";       break;
120527       case SQLITE_IOERR_FSYNC:        zName = "SQLITE_IOERR_FSYNC";       break;
120528       case SQLITE_IOERR_DIR_FSYNC:    zName = "SQLITE_IOERR_DIR_FSYNC";   break;
120529       case SQLITE_IOERR_TRUNCATE:     zName = "SQLITE_IOERR_TRUNCATE";    break;
120530       case SQLITE_IOERR_FSTAT:        zName = "SQLITE_IOERR_FSTAT";       break;
120531       case SQLITE_IOERR_UNLOCK:       zName = "SQLITE_IOERR_UNLOCK";      break;
120532       case SQLITE_IOERR_RDLOCK:       zName = "SQLITE_IOERR_RDLOCK";      break;
120533       case SQLITE_IOERR_DELETE:       zName = "SQLITE_IOERR_DELETE";      break;
120534       case SQLITE_IOERR_BLOCKED:      zName = "SQLITE_IOERR_BLOCKED";     break;
120535       case SQLITE_IOERR_NOMEM:        zName = "SQLITE_IOERR_NOMEM";       break;
120536       case SQLITE_IOERR_ACCESS:       zName = "SQLITE_IOERR_ACCESS";      break;
120537       case SQLITE_IOERR_CHECKRESERVEDLOCK:
120538                                 zName = "SQLITE_IOERR_CHECKRESERVEDLOCK"; break;
120539       case SQLITE_IOERR_LOCK:         zName = "SQLITE_IOERR_LOCK";        break;
120540       case SQLITE_IOERR_CLOSE:        zName = "SQLITE_IOERR_CLOSE";       break;
120541       case SQLITE_IOERR_DIR_CLOSE:    zName = "SQLITE_IOERR_DIR_CLOSE";   break;
120542       case SQLITE_IOERR_SHMOPEN:      zName = "SQLITE_IOERR_SHMOPEN";     break;
120543       case SQLITE_IOERR_SHMSIZE:      zName = "SQLITE_IOERR_SHMSIZE";     break;
120544       case SQLITE_IOERR_SHMLOCK:      zName = "SQLITE_IOERR_SHMLOCK";     break;
120545       case SQLITE_IOERR_SHMMAP:       zName = "SQLITE_IOERR_SHMMAP";      break;
120546       case SQLITE_IOERR_SEEK:         zName = "SQLITE_IOERR_SEEK";        break;
120547       case SQLITE_IOERR_DELETE_NOENT: zName = "SQLITE_IOERR_DELETE_NOENT";break;
120548       case SQLITE_IOERR_MMAP:         zName = "SQLITE_IOERR_MMAP";        break;
120549       case SQLITE_IOERR_GETTEMPPATH:  zName = "SQLITE_IOERR_GETTEMPPATH"; break;
120550       case SQLITE_IOERR_CONVPATH:     zName = "SQLITE_IOERR_CONVPATH";    break;
120551       case SQLITE_CORRUPT:            zName = "SQLITE_CORRUPT";           break;
120552       case SQLITE_CORRUPT_VTAB:       zName = "SQLITE_CORRUPT_VTAB";      break;
120553       case SQLITE_NOTFOUND:           zName = "SQLITE_NOTFOUND";          break;
120554       case SQLITE_FULL:               zName = "SQLITE_FULL";              break;
120555       case SQLITE_CANTOPEN:           zName = "SQLITE_CANTOPEN";          break;
120556       case SQLITE_CANTOPEN_NOTEMPDIR: zName = "SQLITE_CANTOPEN_NOTEMPDIR";break;
120557       case SQLITE_CANTOPEN_ISDIR:     zName = "SQLITE_CANTOPEN_ISDIR";    break;
120558       case SQLITE_CANTOPEN_FULLPATH:  zName = "SQLITE_CANTOPEN_FULLPATH"; break;
120559       case SQLITE_CANTOPEN_CONVPATH:  zName = "SQLITE_CANTOPEN_CONVPATH"; break;
120560       case SQLITE_PROTOCOL:           zName = "SQLITE_PROTOCOL";          break;
120561       case SQLITE_EMPTY:              zName = "SQLITE_EMPTY";             break;
120562       case SQLITE_SCHEMA:             zName = "SQLITE_SCHEMA";            break;
120563       case SQLITE_TOOBIG:             zName = "SQLITE_TOOBIG";            break;
120564       case SQLITE_CONSTRAINT:         zName = "SQLITE_CONSTRAINT";        break;
120565       case SQLITE_CONSTRAINT_UNIQUE:  zName = "SQLITE_CONSTRAINT_UNIQUE"; break;
120566       case SQLITE_CONSTRAINT_TRIGGER: zName = "SQLITE_CONSTRAINT_TRIGGER";break;
120567       case SQLITE_CONSTRAINT_FOREIGNKEY:
120568                                 zName = "SQLITE_CONSTRAINT_FOREIGNKEY";   break;
120569       case SQLITE_CONSTRAINT_CHECK:   zName = "SQLITE_CONSTRAINT_CHECK";  break;
120570       case SQLITE_CONSTRAINT_PRIMARYKEY:
120571                                 zName = "SQLITE_CONSTRAINT_PRIMARYKEY";   break;
120572       case SQLITE_CONSTRAINT_NOTNULL: zName = "SQLITE_CONSTRAINT_NOTNULL";break;
120573       case SQLITE_CONSTRAINT_COMMITHOOK:
120574                                 zName = "SQLITE_CONSTRAINT_COMMITHOOK";   break;
120575       case SQLITE_CONSTRAINT_VTAB:    zName = "SQLITE_CONSTRAINT_VTAB";   break;
120576       case SQLITE_CONSTRAINT_FUNCTION:
120577                                 zName = "SQLITE_CONSTRAINT_FUNCTION";     break;
120578       case SQLITE_CONSTRAINT_ROWID:   zName = "SQLITE_CONSTRAINT_ROWID";  break;
120579       case SQLITE_MISMATCH:           zName = "SQLITE_MISMATCH";          break;
120580       case SQLITE_MISUSE:             zName = "SQLITE_MISUSE";            break;
120581       case SQLITE_NOLFS:              zName = "SQLITE_NOLFS";             break;
120582       case SQLITE_AUTH:               zName = "SQLITE_AUTH";              break;
120583       case SQLITE_FORMAT:             zName = "SQLITE_FORMAT";            break;
120584       case SQLITE_RANGE:              zName = "SQLITE_RANGE";             break;
120585       case SQLITE_NOTADB:             zName = "SQLITE_NOTADB";            break;
120586       case SQLITE_ROW:                zName = "SQLITE_ROW";               break;
120587       case SQLITE_NOTICE:             zName = "SQLITE_NOTICE";            break;
120588       case SQLITE_NOTICE_RECOVER_WAL: zName = "SQLITE_NOTICE_RECOVER_WAL";break;
120589       case SQLITE_NOTICE_RECOVER_ROLLBACK:
120590                                 zName = "SQLITE_NOTICE_RECOVER_ROLLBACK"; break;
120591       case SQLITE_WARNING:            zName = "SQLITE_WARNING";           break;
120592       case SQLITE_WARNING_AUTOINDEX:  zName = "SQLITE_WARNING_AUTOINDEX"; break;
120593       case SQLITE_DONE:               zName = "SQLITE_DONE";              break;
120594     }
120595   }
120596   if( zName==0 ){
120597     static char zBuf[50];
120598     sqlite3_snprintf(sizeof(zBuf), zBuf, "SQLITE_UNKNOWN(%d)", origRc);
120599     zName = zBuf;
120600   }
120601   return zName;
120602 }
120603 #endif
120604 
120605 /*
120606 ** Return a static string that describes the kind of error specified in the
120607 ** argument.
120608 */
120609 SQLITE_PRIVATE const char *sqlite3ErrStr(int rc){
120610   static const char* const aMsg[] = {
120611     /* SQLITE_OK          */ "not an error",
120612     /* SQLITE_ERROR       */ "SQL logic error or missing database",
120613     /* SQLITE_INTERNAL    */ 0,
120614     /* SQLITE_PERM        */ "access permission denied",
120615     /* SQLITE_ABORT       */ "callback requested query abort",
120616     /* SQLITE_BUSY        */ "database is locked",
120617     /* SQLITE_LOCKED      */ "database table is locked",
120618     /* SQLITE_NOMEM       */ "out of memory",
120619     /* SQLITE_READONLY    */ "attempt to write a readonly database",
120620     /* SQLITE_INTERRUPT   */ "interrupted",
120621     /* SQLITE_IOERR       */ "disk I/O error",
120622     /* SQLITE_CORRUPT     */ "database disk image is malformed",
120623     /* SQLITE_NOTFOUND    */ "unknown operation",
120624     /* SQLITE_FULL        */ "database or disk is full",
120625     /* SQLITE_CANTOPEN    */ "unable to open database file",
120626     /* SQLITE_PROTOCOL    */ "locking protocol",
120627     /* SQLITE_EMPTY       */ "table contains no data",
120628     /* SQLITE_SCHEMA      */ "database schema has changed",
120629     /* SQLITE_TOOBIG      */ "string or blob too big",
120630     /* SQLITE_CONSTRAINT  */ "constraint failed",
120631     /* SQLITE_MISMATCH    */ "datatype mismatch",
120632     /* SQLITE_MISUSE      */ "library routine called out of sequence",
120633     /* SQLITE_NOLFS       */ "large file support is disabled",
120634     /* SQLITE_AUTH        */ "authorization denied",
120635     /* SQLITE_FORMAT      */ "auxiliary database format error",
120636     /* SQLITE_RANGE       */ "bind or column index out of range",
120637     /* SQLITE_NOTADB      */ "file is encrypted or is not a database",
120638   };
120639   const char *zErr = "unknown error";
120640   switch( rc ){
120641     case SQLITE_ABORT_ROLLBACK: {
120642       zErr = "abort due to ROLLBACK";
120643       break;
120644     }
120645     default: {
120646       rc &= 0xff;
120647       if( ALWAYS(rc>=0) && rc<ArraySize(aMsg) && aMsg[rc]!=0 ){
120648         zErr = aMsg[rc];
120649       }
120650       break;
120651     }
120652   }
120653   return zErr;
120654 }
120655 
120656 /*
120657 ** This routine implements a busy callback that sleeps and tries
120658 ** again until a timeout value is reached.  The timeout value is
120659 ** an integer number of milliseconds passed in as the first
120660 ** argument.
120661 */
120662 static int sqliteDefaultBusyCallback(
120663  void *ptr,               /* Database connection */
120664  int count                /* Number of times table has been busy */
120665 ){
120666 #if SQLITE_OS_WIN || (defined(HAVE_USLEEP) && HAVE_USLEEP)
120667   static const u8 delays[] =
120668      { 1, 2, 5, 10, 15, 20, 25, 25,  25,  50,  50, 100 };
120669   static const u8 totals[] =
120670      { 0, 1, 3,  8, 18, 33, 53, 78, 103, 128, 178, 228 };
120671 # define NDELAY ArraySize(delays)
120672   sqlite3 *db = (sqlite3 *)ptr;
120673   int timeout = db->busyTimeout;
120674   int delay, prior;
120675 
120676   assert( count>=0 );
120677   if( count < NDELAY ){
120678     delay = delays[count];
120679     prior = totals[count];
120680   }else{
120681     delay = delays[NDELAY-1];
120682     prior = totals[NDELAY-1] + delay*(count-(NDELAY-1));
120683   }
120684   if( prior + delay > timeout ){
120685     delay = timeout - prior;
120686     if( delay<=0 ) return 0;
120687   }
120688   sqlite3OsSleep(db->pVfs, delay*1000);
120689   return 1;
120690 #else
120691   sqlite3 *db = (sqlite3 *)ptr;
120692   int timeout = ((sqlite3 *)ptr)->busyTimeout;
120693   if( (count+1)*1000 > timeout ){
120694     return 0;
120695   }
120696   sqlite3OsSleep(db->pVfs, 1000000);
120697   return 1;
120698 #endif
120699 }
120700 
120701 /*
120702 ** Invoke the given busy handler.
120703 **
120704 ** This routine is called when an operation failed with a lock.
120705 ** If this routine returns non-zero, the lock is retried.  If it
120706 ** returns 0, the operation aborts with an SQLITE_BUSY error.
120707 */
120708 SQLITE_PRIVATE int sqlite3InvokeBusyHandler(BusyHandler *p){
120709   int rc;
120710   if( NEVER(p==0) || p->xFunc==0 || p->nBusy<0 ) return 0;
120711   rc = p->xFunc(p->pArg, p->nBusy);
120712   if( rc==0 ){
120713     p->nBusy = -1;
120714   }else{
120715     p->nBusy++;
120716   }
120717   return rc;
120718 }
120719 
120720 /*
120721 ** This routine sets the busy callback for an Sqlite database to the
120722 ** given callback function with the given argument.
120723 */
120724 SQLITE_API int sqlite3_busy_handler(
120725   sqlite3 *db,
120726   int (*xBusy)(void*,int),
120727   void *pArg
120728 ){
120729   sqlite3_mutex_enter(db->mutex);
120730   db->busyHandler.xFunc = xBusy;
120731   db->busyHandler.pArg = pArg;
120732   db->busyHandler.nBusy = 0;
120733   db->busyTimeout = 0;
120734   sqlite3_mutex_leave(db->mutex);
120735   return SQLITE_OK;
120736 }
120737 
120738 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK
120739 /*
120740 ** This routine sets the progress callback for an Sqlite database to the
120741 ** given callback function with the given argument. The progress callback will
120742 ** be invoked every nOps opcodes.
120743 */
120744 SQLITE_API void sqlite3_progress_handler(
120745   sqlite3 *db,
120746   int nOps,
120747   int (*xProgress)(void*),
120748   void *pArg
120749 ){
120750   sqlite3_mutex_enter(db->mutex);
120751   if( nOps>0 ){
120752     db->xProgress = xProgress;
120753     db->nProgressOps = (unsigned)nOps;
120754     db->pProgressArg = pArg;
120755   }else{
120756     db->xProgress = 0;
120757     db->nProgressOps = 0;
120758     db->pProgressArg = 0;
120759   }
120760   sqlite3_mutex_leave(db->mutex);
120761 }
120762 #endif
120763 
120764 
120765 /*
120766 ** This routine installs a default busy handler that waits for the
120767 ** specified number of milliseconds before returning 0.
120768 */
120769 SQLITE_API int sqlite3_busy_timeout(sqlite3 *db, int ms){
120770   if( ms>0 ){
120771     sqlite3_busy_handler(db, sqliteDefaultBusyCallback, (void*)db);
120772     db->busyTimeout = ms;
120773   }else{
120774     sqlite3_busy_handler(db, 0, 0);
120775   }
120776   return SQLITE_OK;
120777 }
120778 
120779 /*
120780 ** Cause any pending operation to stop at its earliest opportunity.
120781 */
120782 SQLITE_API void sqlite3_interrupt(sqlite3 *db){
120783   db->u1.isInterrupted = 1;
120784 }
120785 
120786 
120787 /*
120788 ** This function is exactly the same as sqlite3_create_function(), except
120789 ** that it is designed to be called by internal code. The difference is
120790 ** that if a malloc() fails in sqlite3_create_function(), an error code
120791 ** is returned and the mallocFailed flag cleared.
120792 */
120793 SQLITE_PRIVATE int sqlite3CreateFunc(
120794   sqlite3 *db,
120795   const char *zFunctionName,
120796   int nArg,
120797   int enc,
120798   void *pUserData,
120799   void (*xFunc)(sqlite3_context*,int,sqlite3_value **),
120800   void (*xStep)(sqlite3_context*,int,sqlite3_value **),
120801   void (*xFinal)(sqlite3_context*),
120802   FuncDestructor *pDestructor
120803 ){
120804   FuncDef *p;
120805   int nName;
120806   int extraFlags;
120807 
120808   assert( sqlite3_mutex_held(db->mutex) );
120809   if( zFunctionName==0 ||
120810       (xFunc && (xFinal || xStep)) ||
120811       (!xFunc && (xFinal && !xStep)) ||
120812       (!xFunc && (!xFinal && xStep)) ||
120813       (nArg<-1 || nArg>SQLITE_MAX_FUNCTION_ARG) ||
120814       (255<(nName = sqlite3Strlen30( zFunctionName))) ){
120815     return SQLITE_MISUSE_BKPT;
120816   }
120817 
120818   assert( SQLITE_FUNC_CONSTANT==SQLITE_DETERMINISTIC );
120819   extraFlags = enc &  SQLITE_DETERMINISTIC;
120820   enc &= (SQLITE_FUNC_ENCMASK|SQLITE_ANY);
120821 
120822 #ifndef SQLITE_OMIT_UTF16
120823   /* If SQLITE_UTF16 is specified as the encoding type, transform this
120824   ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the
120825   ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally.
120826   **
120827   ** If SQLITE_ANY is specified, add three versions of the function
120828   ** to the hash table.
120829   */
120830   if( enc==SQLITE_UTF16 ){
120831     enc = SQLITE_UTF16NATIVE;
120832   }else if( enc==SQLITE_ANY ){
120833     int rc;
120834     rc = sqlite3CreateFunc(db, zFunctionName, nArg, SQLITE_UTF8|extraFlags,
120835          pUserData, xFunc, xStep, xFinal, pDestructor);
120836     if( rc==SQLITE_OK ){
120837       rc = sqlite3CreateFunc(db, zFunctionName, nArg, SQLITE_UTF16LE|extraFlags,
120838           pUserData, xFunc, xStep, xFinal, pDestructor);
120839     }
120840     if( rc!=SQLITE_OK ){
120841       return rc;
120842     }
120843     enc = SQLITE_UTF16BE;
120844   }
120845 #else
120846   enc = SQLITE_UTF8;
120847 #endif
120848 
120849   /* Check if an existing function is being overridden or deleted. If so,
120850   ** and there are active VMs, then return SQLITE_BUSY. If a function
120851   ** is being overridden/deleted but there are no active VMs, allow the
120852   ** operation to continue but invalidate all precompiled statements.
120853   */
120854   p = sqlite3FindFunction(db, zFunctionName, nName, nArg, (u8)enc, 0);
120855   if( p && (p->funcFlags & SQLITE_FUNC_ENCMASK)==enc && p->nArg==nArg ){
120856     if( db->nVdbeActive ){
120857       sqlite3Error(db, SQLITE_BUSY,
120858         "unable to delete/modify user-function due to active statements");
120859       assert( !db->mallocFailed );
120860       return SQLITE_BUSY;
120861     }else{
120862       sqlite3ExpirePreparedStatements(db);
120863     }
120864   }
120865 
120866   p = sqlite3FindFunction(db, zFunctionName, nName, nArg, (u8)enc, 1);
120867   assert(p || db->mallocFailed);
120868   if( !p ){
120869     return SQLITE_NOMEM;
120870   }
120871 
120872   /* If an older version of the function with a configured destructor is
120873   ** being replaced invoke the destructor function here. */
120874   functionDestroy(db, p);
120875 
120876   if( pDestructor ){
120877     pDestructor->nRef++;
120878   }
120879   p->pDestructor = pDestructor;
120880   p->funcFlags = (p->funcFlags & SQLITE_FUNC_ENCMASK) | extraFlags;
120881   testcase( p->funcFlags & SQLITE_DETERMINISTIC );
120882   p->xFunc = xFunc;
120883   p->xStep = xStep;
120884   p->xFinalize = xFinal;
120885   p->pUserData = pUserData;
120886   p->nArg = (u16)nArg;
120887   return SQLITE_OK;
120888 }
120889 
120890 /*
120891 ** Create new user functions.
120892 */
120893 SQLITE_API int sqlite3_create_function(
120894   sqlite3 *db,
120895   const char *zFunc,
120896   int nArg,
120897   int enc,
120898   void *p,
120899   void (*xFunc)(sqlite3_context*,int,sqlite3_value **),
120900   void (*xStep)(sqlite3_context*,int,sqlite3_value **),
120901   void (*xFinal)(sqlite3_context*)
120902 ){
120903   return sqlite3_create_function_v2(db, zFunc, nArg, enc, p, xFunc, xStep,
120904                                     xFinal, 0);
120905 }
120906 
120907 SQLITE_API int sqlite3_create_function_v2(
120908   sqlite3 *db,
120909   const char *zFunc,
120910   int nArg,
120911   int enc,
120912   void *p,
120913   void (*xFunc)(sqlite3_context*,int,sqlite3_value **),
120914   void (*xStep)(sqlite3_context*,int,sqlite3_value **),
120915   void (*xFinal)(sqlite3_context*),
120916   void (*xDestroy)(void *)
120917 ){
120918   int rc = SQLITE_ERROR;
120919   FuncDestructor *pArg = 0;
120920   sqlite3_mutex_enter(db->mutex);
120921   if( xDestroy ){
120922     pArg = (FuncDestructor *)sqlite3DbMallocZero(db, sizeof(FuncDestructor));
120923     if( !pArg ){
120924       xDestroy(p);
120925       goto out;
120926     }
120927     pArg->xDestroy = xDestroy;
120928     pArg->pUserData = p;
120929   }
120930   rc = sqlite3CreateFunc(db, zFunc, nArg, enc, p, xFunc, xStep, xFinal, pArg);
120931   if( pArg && pArg->nRef==0 ){
120932     assert( rc!=SQLITE_OK );
120933     xDestroy(p);
120934     sqlite3DbFree(db, pArg);
120935   }
120936 
120937  out:
120938   rc = sqlite3ApiExit(db, rc);
120939   sqlite3_mutex_leave(db->mutex);
120940   return rc;
120941 }
120942 
120943 #ifndef SQLITE_OMIT_UTF16
120944 SQLITE_API int sqlite3_create_function16(
120945   sqlite3 *db,
120946   const void *zFunctionName,
120947   int nArg,
120948   int eTextRep,
120949   void *p,
120950   void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
120951   void (*xStep)(sqlite3_context*,int,sqlite3_value**),
120952   void (*xFinal)(sqlite3_context*)
120953 ){
120954   int rc;
120955   char *zFunc8;
120956   sqlite3_mutex_enter(db->mutex);
120957   assert( !db->mallocFailed );
120958   zFunc8 = sqlite3Utf16to8(db, zFunctionName, -1, SQLITE_UTF16NATIVE);
120959   rc = sqlite3CreateFunc(db, zFunc8, nArg, eTextRep, p, xFunc, xStep, xFinal,0);
120960   sqlite3DbFree(db, zFunc8);
120961   rc = sqlite3ApiExit(db, rc);
120962   sqlite3_mutex_leave(db->mutex);
120963   return rc;
120964 }
120965 #endif
120966 
120967 
120968 /*
120969 ** Declare that a function has been overloaded by a virtual table.
120970 **
120971 ** If the function already exists as a regular global function, then
120972 ** this routine is a no-op.  If the function does not exist, then create
120973 ** a new one that always throws a run-time error.
120974 **
120975 ** When virtual tables intend to provide an overloaded function, they
120976 ** should call this routine to make sure the global function exists.
120977 ** A global function must exist in order for name resolution to work
120978 ** properly.
120979 */
120980 SQLITE_API int sqlite3_overload_function(
120981   sqlite3 *db,
120982   const char *zName,
120983   int nArg
120984 ){
120985   int nName = sqlite3Strlen30(zName);
120986   int rc = SQLITE_OK;
120987   sqlite3_mutex_enter(db->mutex);
120988   if( sqlite3FindFunction(db, zName, nName, nArg, SQLITE_UTF8, 0)==0 ){
120989     rc = sqlite3CreateFunc(db, zName, nArg, SQLITE_UTF8,
120990                            0, sqlite3InvalidFunction, 0, 0, 0);
120991   }
120992   rc = sqlite3ApiExit(db, rc);
120993   sqlite3_mutex_leave(db->mutex);
120994   return rc;
120995 }
120996 
120997 #ifndef SQLITE_OMIT_TRACE
120998 /*
120999 ** Register a trace function.  The pArg from the previously registered trace
121000 ** is returned.
121001 **
121002 ** A NULL trace function means that no tracing is executes.  A non-NULL
121003 ** trace is a pointer to a function that is invoked at the start of each
121004 ** SQL statement.
121005 */
121006 SQLITE_API void *sqlite3_trace(sqlite3 *db, void (*xTrace)(void*,const char*), void *pArg){
121007   void *pOld;
121008   sqlite3_mutex_enter(db->mutex);
121009   pOld = db->pTraceArg;
121010   db->xTrace = xTrace;
121011   db->pTraceArg = pArg;
121012   sqlite3_mutex_leave(db->mutex);
121013   return pOld;
121014 }
121015 /*
121016 ** Register a profile function.  The pArg from the previously registered
121017 ** profile function is returned.
121018 **
121019 ** A NULL profile function means that no profiling is executes.  A non-NULL
121020 ** profile is a pointer to a function that is invoked at the conclusion of
121021 ** each SQL statement that is run.
121022 */
121023 SQLITE_API void *sqlite3_profile(
121024   sqlite3 *db,
121025   void (*xProfile)(void*,const char*,sqlite_uint64),
121026   void *pArg
121027 ){
121028   void *pOld;
121029   sqlite3_mutex_enter(db->mutex);
121030   pOld = db->pProfileArg;
121031   db->xProfile = xProfile;
121032   db->pProfileArg = pArg;
121033   sqlite3_mutex_leave(db->mutex);
121034   return pOld;
121035 }
121036 #endif /* SQLITE_OMIT_TRACE */
121037 
121038 /*
121039 ** Register a function to be invoked when a transaction commits.
121040 ** If the invoked function returns non-zero, then the commit becomes a
121041 ** rollback.
121042 */
121043 SQLITE_API void *sqlite3_commit_hook(
121044   sqlite3 *db,              /* Attach the hook to this database */
121045   int (*xCallback)(void*),  /* Function to invoke on each commit */
121046   void *pArg                /* Argument to the function */
121047 ){
121048   void *pOld;
121049   sqlite3_mutex_enter(db->mutex);
121050   pOld = db->pCommitArg;
121051   db->xCommitCallback = xCallback;
121052   db->pCommitArg = pArg;
121053   sqlite3_mutex_leave(db->mutex);
121054   return pOld;
121055 }
121056 
121057 /*
121058 ** Register a callback to be invoked each time a row is updated,
121059 ** inserted or deleted using this database connection.
121060 */
121061 SQLITE_API void *sqlite3_update_hook(
121062   sqlite3 *db,              /* Attach the hook to this database */
121063   void (*xCallback)(void*,int,char const *,char const *,sqlite_int64),
121064   void *pArg                /* Argument to the function */
121065 ){
121066   void *pRet;
121067   sqlite3_mutex_enter(db->mutex);
121068   pRet = db->pUpdateArg;
121069   db->xUpdateCallback = xCallback;
121070   db->pUpdateArg = pArg;
121071   sqlite3_mutex_leave(db->mutex);
121072   return pRet;
121073 }
121074 
121075 /*
121076 ** Register a callback to be invoked each time a transaction is rolled
121077 ** back by this database connection.
121078 */
121079 SQLITE_API void *sqlite3_rollback_hook(
121080   sqlite3 *db,              /* Attach the hook to this database */
121081   void (*xCallback)(void*), /* Callback function */
121082   void *pArg                /* Argument to the function */
121083 ){
121084   void *pRet;
121085   sqlite3_mutex_enter(db->mutex);
121086   pRet = db->pRollbackArg;
121087   db->xRollbackCallback = xCallback;
121088   db->pRollbackArg = pArg;
121089   sqlite3_mutex_leave(db->mutex);
121090   return pRet;
121091 }
121092 
121093 #ifndef SQLITE_OMIT_WAL
121094 /*
121095 ** The sqlite3_wal_hook() callback registered by sqlite3_wal_autocheckpoint().
121096 ** Invoke sqlite3_wal_checkpoint if the number of frames in the log file
121097 ** is greater than sqlite3.pWalArg cast to an integer (the value configured by
121098 ** wal_autocheckpoint()).
121099 */
121100 SQLITE_PRIVATE int sqlite3WalDefaultHook(
121101   void *pClientData,     /* Argument */
121102   sqlite3 *db,           /* Connection */
121103   const char *zDb,       /* Database */
121104   int nFrame             /* Size of WAL */
121105 ){
121106   if( nFrame>=SQLITE_PTR_TO_INT(pClientData) ){
121107     sqlite3BeginBenignMalloc();
121108     sqlite3_wal_checkpoint(db, zDb);
121109     sqlite3EndBenignMalloc();
121110   }
121111   return SQLITE_OK;
121112 }
121113 #endif /* SQLITE_OMIT_WAL */
121114 
121115 /*
121116 ** Configure an sqlite3_wal_hook() callback to automatically checkpoint
121117 ** a database after committing a transaction if there are nFrame or
121118 ** more frames in the log file. Passing zero or a negative value as the
121119 ** nFrame parameter disables automatic checkpoints entirely.
121120 **
121121 ** The callback registered by this function replaces any existing callback
121122 ** registered using sqlite3_wal_hook(). Likewise, registering a callback
121123 ** using sqlite3_wal_hook() disables the automatic checkpoint mechanism
121124 ** configured by this function.
121125 */
121126 SQLITE_API int sqlite3_wal_autocheckpoint(sqlite3 *db, int nFrame){
121127 #ifdef SQLITE_OMIT_WAL
121128   UNUSED_PARAMETER(db);
121129   UNUSED_PARAMETER(nFrame);
121130 #else
121131   if( nFrame>0 ){
121132     sqlite3_wal_hook(db, sqlite3WalDefaultHook, SQLITE_INT_TO_PTR(nFrame));
121133   }else{
121134     sqlite3_wal_hook(db, 0, 0);
121135   }
121136 #endif
121137   return SQLITE_OK;
121138 }
121139 
121140 /*
121141 ** Register a callback to be invoked each time a transaction is written
121142 ** into the write-ahead-log by this database connection.
121143 */
121144 SQLITE_API void *sqlite3_wal_hook(
121145   sqlite3 *db,                    /* Attach the hook to this db handle */
121146   int(*xCallback)(void *, sqlite3*, const char*, int),
121147   void *pArg                      /* First argument passed to xCallback() */
121148 ){
121149 #ifndef SQLITE_OMIT_WAL
121150   void *pRet;
121151   sqlite3_mutex_enter(db->mutex);
121152   pRet = db->pWalArg;
121153   db->xWalCallback = xCallback;
121154   db->pWalArg = pArg;
121155   sqlite3_mutex_leave(db->mutex);
121156   return pRet;
121157 #else
121158   return 0;
121159 #endif
121160 }
121161 
121162 /*
121163 ** Checkpoint database zDb.
121164 */
121165 SQLITE_API int sqlite3_wal_checkpoint_v2(
121166   sqlite3 *db,                    /* Database handle */
121167   const char *zDb,                /* Name of attached database (or NULL) */
121168   int eMode,                      /* SQLITE_CHECKPOINT_* value */
121169   int *pnLog,                     /* OUT: Size of WAL log in frames */
121170   int *pnCkpt                     /* OUT: Total number of frames checkpointed */
121171 ){
121172 #ifdef SQLITE_OMIT_WAL
121173   return SQLITE_OK;
121174 #else
121175   int rc;                         /* Return code */
121176   int iDb = SQLITE_MAX_ATTACHED;  /* sqlite3.aDb[] index of db to checkpoint */
121177 
121178   /* Initialize the output variables to -1 in case an error occurs. */
121179   if( pnLog ) *pnLog = -1;
121180   if( pnCkpt ) *pnCkpt = -1;
121181 
121182   assert( SQLITE_CHECKPOINT_FULL>SQLITE_CHECKPOINT_PASSIVE );
121183   assert( SQLITE_CHECKPOINT_FULL<SQLITE_CHECKPOINT_RESTART );
121184   assert( SQLITE_CHECKPOINT_PASSIVE+2==SQLITE_CHECKPOINT_RESTART );
121185   if( eMode<SQLITE_CHECKPOINT_PASSIVE || eMode>SQLITE_CHECKPOINT_RESTART ){
121186     return SQLITE_MISUSE;
121187   }
121188 
121189   sqlite3_mutex_enter(db->mutex);
121190   if( zDb && zDb[0] ){
121191     iDb = sqlite3FindDbName(db, zDb);
121192   }
121193   if( iDb<0 ){
121194     rc = SQLITE_ERROR;
121195     sqlite3Error(db, SQLITE_ERROR, "unknown database: %s", zDb);
121196   }else{
121197     rc = sqlite3Checkpoint(db, iDb, eMode, pnLog, pnCkpt);
121198     sqlite3Error(db, rc, 0);
121199   }
121200   rc = sqlite3ApiExit(db, rc);
121201   sqlite3_mutex_leave(db->mutex);
121202   return rc;
121203 #endif
121204 }
121205 
121206 
121207 /*
121208 ** Checkpoint database zDb. If zDb is NULL, or if the buffer zDb points
121209 ** to contains a zero-length string, all attached databases are
121210 ** checkpointed.
121211 */
121212 SQLITE_API int sqlite3_wal_checkpoint(sqlite3 *db, const char *zDb){
121213   return sqlite3_wal_checkpoint_v2(db, zDb, SQLITE_CHECKPOINT_PASSIVE, 0, 0);
121214 }
121215 
121216 #ifndef SQLITE_OMIT_WAL
121217 /*
121218 ** Run a checkpoint on database iDb. This is a no-op if database iDb is
121219 ** not currently open in WAL mode.
121220 **
121221 ** If a transaction is open on the database being checkpointed, this
121222 ** function returns SQLITE_LOCKED and a checkpoint is not attempted. If
121223 ** an error occurs while running the checkpoint, an SQLite error code is
121224 ** returned (i.e. SQLITE_IOERR). Otherwise, SQLITE_OK.
121225 **
121226 ** The mutex on database handle db should be held by the caller. The mutex
121227 ** associated with the specific b-tree being checkpointed is taken by
121228 ** this function while the checkpoint is running.
121229 **
121230 ** If iDb is passed SQLITE_MAX_ATTACHED, then all attached databases are
121231 ** checkpointed. If an error is encountered it is returned immediately -
121232 ** no attempt is made to checkpoint any remaining databases.
121233 **
121234 ** Parameter eMode is one of SQLITE_CHECKPOINT_PASSIVE, FULL or RESTART.
121235 */
121236 SQLITE_PRIVATE int sqlite3Checkpoint(sqlite3 *db, int iDb, int eMode, int *pnLog, int *pnCkpt){
121237   int rc = SQLITE_OK;             /* Return code */
121238   int i;                          /* Used to iterate through attached dbs */
121239   int bBusy = 0;                  /* True if SQLITE_BUSY has been encountered */
121240 
121241   assert( sqlite3_mutex_held(db->mutex) );
121242   assert( !pnLog || *pnLog==-1 );
121243   assert( !pnCkpt || *pnCkpt==-1 );
121244 
121245   for(i=0; i<db->nDb && rc==SQLITE_OK; i++){
121246     if( i==iDb || iDb==SQLITE_MAX_ATTACHED ){
121247       rc = sqlite3BtreeCheckpoint(db->aDb[i].pBt, eMode, pnLog, pnCkpt);
121248       pnLog = 0;
121249       pnCkpt = 0;
121250       if( rc==SQLITE_BUSY ){
121251         bBusy = 1;
121252         rc = SQLITE_OK;
121253       }
121254     }
121255   }
121256 
121257   return (rc==SQLITE_OK && bBusy) ? SQLITE_BUSY : rc;
121258 }
121259 #endif /* SQLITE_OMIT_WAL */
121260 
121261 /*
121262 ** This function returns true if main-memory should be used instead of
121263 ** a temporary file for transient pager files and statement journals.
121264 ** The value returned depends on the value of db->temp_store (runtime
121265 ** parameter) and the compile time value of SQLITE_TEMP_STORE. The
121266 ** following table describes the relationship between these two values
121267 ** and this functions return value.
121268 **
121269 **   SQLITE_TEMP_STORE     db->temp_store     Location of temporary database
121270 **   -----------------     --------------     ------------------------------
121271 **   0                     any                file      (return 0)
121272 **   1                     1                  file      (return 0)
121273 **   1                     2                  memory    (return 1)
121274 **   1                     0                  file      (return 0)
121275 **   2                     1                  file      (return 0)
121276 **   2                     2                  memory    (return 1)
121277 **   2                     0                  memory    (return 1)
121278 **   3                     any                memory    (return 1)
121279 */
121280 SQLITE_PRIVATE int sqlite3TempInMemory(const sqlite3 *db){
121281 #if SQLITE_TEMP_STORE==1
121282   return ( db->temp_store==2 );
121283 #endif
121284 #if SQLITE_TEMP_STORE==2
121285   return ( db->temp_store!=1 );
121286 #endif
121287 #if SQLITE_TEMP_STORE==3
121288   return 1;
121289 #endif
121290 #if SQLITE_TEMP_STORE<1 || SQLITE_TEMP_STORE>3
121291   return 0;
121292 #endif
121293 }
121294 
121295 /*
121296 ** Return UTF-8 encoded English language explanation of the most recent
121297 ** error.
121298 */
121299 SQLITE_API const char *sqlite3_errmsg(sqlite3 *db){
121300   const char *z;
121301   if( !db ){
121302     return sqlite3ErrStr(SQLITE_NOMEM);
121303   }
121304   if( !sqlite3SafetyCheckSickOrOk(db) ){
121305     return sqlite3ErrStr(SQLITE_MISUSE_BKPT);
121306   }
121307   sqlite3_mutex_enter(db->mutex);
121308   if( db->mallocFailed ){
121309     z = sqlite3ErrStr(SQLITE_NOMEM);
121310   }else{
121311     testcase( db->pErr==0 );
121312     z = (char*)sqlite3_value_text(db->pErr);
121313     assert( !db->mallocFailed );
121314     if( z==0 ){
121315       z = sqlite3ErrStr(db->errCode);
121316     }
121317   }
121318   sqlite3_mutex_leave(db->mutex);
121319   return z;
121320 }
121321 
121322 #ifndef SQLITE_OMIT_UTF16
121323 /*
121324 ** Return UTF-16 encoded English language explanation of the most recent
121325 ** error.
121326 */
121327 SQLITE_API const void *sqlite3_errmsg16(sqlite3 *db){
121328   static const u16 outOfMem[] = {
121329     'o', 'u', 't', ' ', 'o', 'f', ' ', 'm', 'e', 'm', 'o', 'r', 'y', 0
121330   };
121331   static const u16 misuse[] = {
121332     'l', 'i', 'b', 'r', 'a', 'r', 'y', ' ',
121333     'r', 'o', 'u', 't', 'i', 'n', 'e', ' ',
121334     'c', 'a', 'l', 'l', 'e', 'd', ' ',
121335     'o', 'u', 't', ' ',
121336     'o', 'f', ' ',
121337     's', 'e', 'q', 'u', 'e', 'n', 'c', 'e', 0
121338   };
121339 
121340   const void *z;
121341   if( !db ){
121342     return (void *)outOfMem;
121343   }
121344   if( !sqlite3SafetyCheckSickOrOk(db) ){
121345     return (void *)misuse;
121346   }
121347   sqlite3_mutex_enter(db->mutex);
121348   if( db->mallocFailed ){
121349     z = (void *)outOfMem;
121350   }else{
121351     z = sqlite3_value_text16(db->pErr);
121352     if( z==0 ){
121353       sqlite3Error(db, db->errCode, sqlite3ErrStr(db->errCode));
121354       z = sqlite3_value_text16(db->pErr);
121355     }
121356     /* A malloc() may have failed within the call to sqlite3_value_text16()
121357     ** above. If this is the case, then the db->mallocFailed flag needs to
121358     ** be cleared before returning. Do this directly, instead of via
121359     ** sqlite3ApiExit(), to avoid setting the database handle error message.
121360     */
121361     db->mallocFailed = 0;
121362   }
121363   sqlite3_mutex_leave(db->mutex);
121364   return z;
121365 }
121366 #endif /* SQLITE_OMIT_UTF16 */
121367 
121368 /*
121369 ** Return the most recent error code generated by an SQLite routine. If NULL is
121370 ** passed to this function, we assume a malloc() failed during sqlite3_open().
121371 */
121372 SQLITE_API int sqlite3_errcode(sqlite3 *db){
121373   if( db && !sqlite3SafetyCheckSickOrOk(db) ){
121374     return SQLITE_MISUSE_BKPT;
121375   }
121376   if( !db || db->mallocFailed ){
121377     return SQLITE_NOMEM;
121378   }
121379   return db->errCode & db->errMask;
121380 }
121381 SQLITE_API int sqlite3_extended_errcode(sqlite3 *db){
121382   if( db && !sqlite3SafetyCheckSickOrOk(db) ){
121383     return SQLITE_MISUSE_BKPT;
121384   }
121385   if( !db || db->mallocFailed ){
121386     return SQLITE_NOMEM;
121387   }
121388   return db->errCode;
121389 }
121390 
121391 /*
121392 ** Return a string that describes the kind of error specified in the
121393 ** argument.  For now, this simply calls the internal sqlite3ErrStr()
121394 ** function.
121395 */
121396 SQLITE_API const char *sqlite3_errstr(int rc){
121397   return sqlite3ErrStr(rc);
121398 }
121399 
121400 /*
121401 ** Invalidate all cached KeyInfo objects for database connection "db"
121402 */
121403 static void invalidateCachedKeyInfo(sqlite3 *db){
121404   Db *pDb;                    /* A single database */
121405   int iDb;                    /* The database index number */
121406   HashElem *k;                /* For looping over tables in pDb */
121407   Table *pTab;                /* A table in the database */
121408   Index *pIdx;                /* Each index */
121409 
121410   for(iDb=0, pDb=db->aDb; iDb<db->nDb; iDb++, pDb++){
121411     if( pDb->pBt==0 ) continue;
121412     sqlite3BtreeEnter(pDb->pBt);
121413     for(k=sqliteHashFirst(&pDb->pSchema->tblHash);  k; k=sqliteHashNext(k)){
121414       pTab = (Table*)sqliteHashData(k);
121415       for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
121416         if( pIdx->pKeyInfo && pIdx->pKeyInfo->db==db ){
121417           sqlite3KeyInfoUnref(pIdx->pKeyInfo);
121418           pIdx->pKeyInfo = 0;
121419         }
121420       }
121421     }
121422     sqlite3BtreeLeave(pDb->pBt);
121423   }
121424 }
121425 
121426 /*
121427 ** Create a new collating function for database "db".  The name is zName
121428 ** and the encoding is enc.
121429 */
121430 static int createCollation(
121431   sqlite3* db,
121432   const char *zName,
121433   u8 enc,
121434   void* pCtx,
121435   int(*xCompare)(void*,int,const void*,int,const void*),
121436   void(*xDel)(void*)
121437 ){
121438   CollSeq *pColl;
121439   int enc2;
121440   int nName = sqlite3Strlen30(zName);
121441 
121442   assert( sqlite3_mutex_held(db->mutex) );
121443 
121444   /* If SQLITE_UTF16 is specified as the encoding type, transform this
121445   ** to one of SQLITE_UTF16LE or SQLITE_UTF16BE using the
121446   ** SQLITE_UTF16NATIVE macro. SQLITE_UTF16 is not used internally.
121447   */
121448   enc2 = enc;
121449   testcase( enc2==SQLITE_UTF16 );
121450   testcase( enc2==SQLITE_UTF16_ALIGNED );
121451   if( enc2==SQLITE_UTF16 || enc2==SQLITE_UTF16_ALIGNED ){
121452     enc2 = SQLITE_UTF16NATIVE;
121453   }
121454   if( enc2<SQLITE_UTF8 || enc2>SQLITE_UTF16BE ){
121455     return SQLITE_MISUSE_BKPT;
121456   }
121457 
121458   /* Check if this call is removing or replacing an existing collation
121459   ** sequence. If so, and there are active VMs, return busy. If there
121460   ** are no active VMs, invalidate any pre-compiled statements.
121461   */
121462   pColl = sqlite3FindCollSeq(db, (u8)enc2, zName, 0);
121463   if( pColl && pColl->xCmp ){
121464     if( db->nVdbeActive ){
121465       sqlite3Error(db, SQLITE_BUSY,
121466         "unable to delete/modify collation sequence due to active statements");
121467       return SQLITE_BUSY;
121468     }
121469     sqlite3ExpirePreparedStatements(db);
121470     invalidateCachedKeyInfo(db);
121471 
121472     /* If collation sequence pColl was created directly by a call to
121473     ** sqlite3_create_collation, and not generated by synthCollSeq(),
121474     ** then any copies made by synthCollSeq() need to be invalidated.
121475     ** Also, collation destructor - CollSeq.xDel() - function may need
121476     ** to be called.
121477     */
121478     if( (pColl->enc & ~SQLITE_UTF16_ALIGNED)==enc2 ){
121479       CollSeq *aColl = sqlite3HashFind(&db->aCollSeq, zName, nName);
121480       int j;
121481       for(j=0; j<3; j++){
121482         CollSeq *p = &aColl[j];
121483         if( p->enc==pColl->enc ){
121484           if( p->xDel ){
121485             p->xDel(p->pUser);
121486           }
121487           p->xCmp = 0;
121488         }
121489       }
121490     }
121491   }
121492 
121493   pColl = sqlite3FindCollSeq(db, (u8)enc2, zName, 1);
121494   if( pColl==0 ) return SQLITE_NOMEM;
121495   pColl->xCmp = xCompare;
121496   pColl->pUser = pCtx;
121497   pColl->xDel = xDel;
121498   pColl->enc = (u8)(enc2 | (enc & SQLITE_UTF16_ALIGNED));
121499   sqlite3Error(db, SQLITE_OK, 0);
121500   return SQLITE_OK;
121501 }
121502 
121503 
121504 /*
121505 ** This array defines hard upper bounds on limit values.  The
121506 ** initializer must be kept in sync with the SQLITE_LIMIT_*
121507 ** #defines in sqlite3.h.
121508 */
121509 static const int aHardLimit[] = {
121510   SQLITE_MAX_LENGTH,
121511   SQLITE_MAX_SQL_LENGTH,
121512   SQLITE_MAX_COLUMN,
121513   SQLITE_MAX_EXPR_DEPTH,
121514   SQLITE_MAX_COMPOUND_SELECT,
121515   SQLITE_MAX_VDBE_OP,
121516   SQLITE_MAX_FUNCTION_ARG,
121517   SQLITE_MAX_ATTACHED,
121518   SQLITE_MAX_LIKE_PATTERN_LENGTH,
121519   SQLITE_MAX_VARIABLE_NUMBER,
121520   SQLITE_MAX_TRIGGER_DEPTH,
121521 };
121522 
121523 /*
121524 ** Make sure the hard limits are set to reasonable values
121525 */
121526 #if SQLITE_MAX_LENGTH<100
121527 # error SQLITE_MAX_LENGTH must be at least 100
121528 #endif
121529 #if SQLITE_MAX_SQL_LENGTH<100
121530 # error SQLITE_MAX_SQL_LENGTH must be at least 100
121531 #endif
121532 #if SQLITE_MAX_SQL_LENGTH>SQLITE_MAX_LENGTH
121533 # error SQLITE_MAX_SQL_LENGTH must not be greater than SQLITE_MAX_LENGTH
121534 #endif
121535 #if SQLITE_MAX_COMPOUND_SELECT<2
121536 # error SQLITE_MAX_COMPOUND_SELECT must be at least 2
121537 #endif
121538 #if SQLITE_MAX_VDBE_OP<40
121539 # error SQLITE_MAX_VDBE_OP must be at least 40
121540 #endif
121541 #if SQLITE_MAX_FUNCTION_ARG<0 || SQLITE_MAX_FUNCTION_ARG>1000
121542 # error SQLITE_MAX_FUNCTION_ARG must be between 0 and 1000
121543 #endif
121544 #if SQLITE_MAX_ATTACHED<0 || SQLITE_MAX_ATTACHED>62
121545 # error SQLITE_MAX_ATTACHED must be between 0 and 62
121546 #endif
121547 #if SQLITE_MAX_LIKE_PATTERN_LENGTH<1
121548 # error SQLITE_MAX_LIKE_PATTERN_LENGTH must be at least 1
121549 #endif
121550 #if SQLITE_MAX_COLUMN>32767
121551 # error SQLITE_MAX_COLUMN must not exceed 32767
121552 #endif
121553 #if SQLITE_MAX_TRIGGER_DEPTH<1
121554 # error SQLITE_MAX_TRIGGER_DEPTH must be at least 1
121555 #endif
121556 
121557 
121558 /*
121559 ** Change the value of a limit.  Report the old value.
121560 ** If an invalid limit index is supplied, report -1.
121561 ** Make no changes but still report the old value if the
121562 ** new limit is negative.
121563 **
121564 ** A new lower limit does not shrink existing constructs.
121565 ** It merely prevents new constructs that exceed the limit
121566 ** from forming.
121567 */
121568 SQLITE_API int sqlite3_limit(sqlite3 *db, int limitId, int newLimit){
121569   int oldLimit;
121570 
121571 
121572   /* EVIDENCE-OF: R-30189-54097 For each limit category SQLITE_LIMIT_NAME
121573   ** there is a hard upper bound set at compile-time by a C preprocessor
121574   ** macro called SQLITE_MAX_NAME. (The "_LIMIT_" in the name is changed to
121575   ** "_MAX_".)
121576   */
121577   assert( aHardLimit[SQLITE_LIMIT_LENGTH]==SQLITE_MAX_LENGTH );
121578   assert( aHardLimit[SQLITE_LIMIT_SQL_LENGTH]==SQLITE_MAX_SQL_LENGTH );
121579   assert( aHardLimit[SQLITE_LIMIT_COLUMN]==SQLITE_MAX_COLUMN );
121580   assert( aHardLimit[SQLITE_LIMIT_EXPR_DEPTH]==SQLITE_MAX_EXPR_DEPTH );
121581   assert( aHardLimit[SQLITE_LIMIT_COMPOUND_SELECT]==SQLITE_MAX_COMPOUND_SELECT);
121582   assert( aHardLimit[SQLITE_LIMIT_VDBE_OP]==SQLITE_MAX_VDBE_OP );
121583   assert( aHardLimit[SQLITE_LIMIT_FUNCTION_ARG]==SQLITE_MAX_FUNCTION_ARG );
121584   assert( aHardLimit[SQLITE_LIMIT_ATTACHED]==SQLITE_MAX_ATTACHED );
121585   assert( aHardLimit[SQLITE_LIMIT_LIKE_PATTERN_LENGTH]==
121586                                                SQLITE_MAX_LIKE_PATTERN_LENGTH );
121587   assert( aHardLimit[SQLITE_LIMIT_VARIABLE_NUMBER]==SQLITE_MAX_VARIABLE_NUMBER);
121588   assert( aHardLimit[SQLITE_LIMIT_TRIGGER_DEPTH]==SQLITE_MAX_TRIGGER_DEPTH );
121589   assert( SQLITE_LIMIT_TRIGGER_DEPTH==(SQLITE_N_LIMIT-1) );
121590 
121591 
121592   if( limitId<0 || limitId>=SQLITE_N_LIMIT ){
121593     return -1;
121594   }
121595   oldLimit = db->aLimit[limitId];
121596   if( newLimit>=0 ){                   /* IMP: R-52476-28732 */
121597     if( newLimit>aHardLimit[limitId] ){
121598       newLimit = aHardLimit[limitId];  /* IMP: R-51463-25634 */
121599     }
121600     db->aLimit[limitId] = newLimit;
121601   }
121602   return oldLimit;                     /* IMP: R-53341-35419 */
121603 }
121604 
121605 /*
121606 ** This function is used to parse both URIs and non-URI filenames passed by the
121607 ** user to API functions sqlite3_open() or sqlite3_open_v2(), and for database
121608 ** URIs specified as part of ATTACH statements.
121609 **
121610 ** The first argument to this function is the name of the VFS to use (or
121611 ** a NULL to signify the default VFS) if the URI does not contain a "vfs=xxx"
121612 ** query parameter. The second argument contains the URI (or non-URI filename)
121613 ** itself. When this function is called the *pFlags variable should contain
121614 ** the default flags to open the database handle with. The value stored in
121615 ** *pFlags may be updated before returning if the URI filename contains
121616 ** "cache=xxx" or "mode=xxx" query parameters.
121617 **
121618 ** If successful, SQLITE_OK is returned. In this case *ppVfs is set to point to
121619 ** the VFS that should be used to open the database file. *pzFile is set to
121620 ** point to a buffer containing the name of the file to open. It is the
121621 ** responsibility of the caller to eventually call sqlite3_free() to release
121622 ** this buffer.
121623 **
121624 ** If an error occurs, then an SQLite error code is returned and *pzErrMsg
121625 ** may be set to point to a buffer containing an English language error
121626 ** message. It is the responsibility of the caller to eventually release
121627 ** this buffer by calling sqlite3_free().
121628 */
121629 SQLITE_PRIVATE int sqlite3ParseUri(
121630   const char *zDefaultVfs,        /* VFS to use if no "vfs=xxx" query option */
121631   const char *zUri,               /* Nul-terminated URI to parse */
121632   unsigned int *pFlags,           /* IN/OUT: SQLITE_OPEN_XXX flags */
121633   sqlite3_vfs **ppVfs,            /* OUT: VFS to use */
121634   char **pzFile,                  /* OUT: Filename component of URI */
121635   char **pzErrMsg                 /* OUT: Error message (if rc!=SQLITE_OK) */
121636 ){
121637   int rc = SQLITE_OK;
121638   unsigned int flags = *pFlags;
121639   const char *zVfs = zDefaultVfs;
121640   char *zFile;
121641   char c;
121642   int nUri = sqlite3Strlen30(zUri);
121643 
121644   assert( *pzErrMsg==0 );
121645 
121646   if( ((flags & SQLITE_OPEN_URI) || sqlite3GlobalConfig.bOpenUri)
121647    && nUri>=5 && memcmp(zUri, "file:", 5)==0
121648   ){
121649     char *zOpt;
121650     int eState;                   /* Parser state when parsing URI */
121651     int iIn;                      /* Input character index */
121652     int iOut = 0;                 /* Output character index */
121653     int nByte = nUri+2;           /* Bytes of space to allocate */
121654 
121655     /* Make sure the SQLITE_OPEN_URI flag is set to indicate to the VFS xOpen
121656     ** method that there may be extra parameters following the file-name.  */
121657     flags |= SQLITE_OPEN_URI;
121658 
121659     for(iIn=0; iIn<nUri; iIn++) nByte += (zUri[iIn]=='&');
121660     zFile = sqlite3_malloc(nByte);
121661     if( !zFile ) return SQLITE_NOMEM;
121662 
121663     iIn = 5;
121664 #ifndef SQLITE_ALLOW_URI_AUTHORITY
121665     /* Discard the scheme and authority segments of the URI. */
121666     if( zUri[5]=='/' && zUri[6]=='/' ){
121667       iIn = 7;
121668       while( zUri[iIn] && zUri[iIn]!='/' ) iIn++;
121669       if( iIn!=7 && (iIn!=16 || memcmp("localhost", &zUri[7], 9)) ){
121670         *pzErrMsg = sqlite3_mprintf("invalid uri authority: %.*s",
121671             iIn-7, &zUri[7]);
121672         rc = SQLITE_ERROR;
121673         goto parse_uri_out;
121674       }
121675     }
121676 #endif
121677 
121678     /* Copy the filename and any query parameters into the zFile buffer.
121679     ** Decode %HH escape codes along the way.
121680     **
121681     ** Within this loop, variable eState may be set to 0, 1 or 2, depending
121682     ** on the parsing context. As follows:
121683     **
121684     **   0: Parsing file-name.
121685     **   1: Parsing name section of a name=value query parameter.
121686     **   2: Parsing value section of a name=value query parameter.
121687     */
121688     eState = 0;
121689     while( (c = zUri[iIn])!=0 && c!='#' ){
121690       iIn++;
121691       if( c=='%'
121692        && sqlite3Isxdigit(zUri[iIn])
121693        && sqlite3Isxdigit(zUri[iIn+1])
121694       ){
121695         int octet = (sqlite3HexToInt(zUri[iIn++]) << 4);
121696         octet += sqlite3HexToInt(zUri[iIn++]);
121697 
121698         assert( octet>=0 && octet<256 );
121699         if( octet==0 ){
121700           /* This branch is taken when "%00" appears within the URI. In this
121701           ** case we ignore all text in the remainder of the path, name or
121702           ** value currently being parsed. So ignore the current character
121703           ** and skip to the next "?", "=" or "&", as appropriate. */
121704           while( (c = zUri[iIn])!=0 && c!='#'
121705               && (eState!=0 || c!='?')
121706               && (eState!=1 || (c!='=' && c!='&'))
121707               && (eState!=2 || c!='&')
121708           ){
121709             iIn++;
121710           }
121711           continue;
121712         }
121713         c = octet;
121714       }else if( eState==1 && (c=='&' || c=='=') ){
121715         if( zFile[iOut-1]==0 ){
121716           /* An empty option name. Ignore this option altogether. */
121717           while( zUri[iIn] && zUri[iIn]!='#' && zUri[iIn-1]!='&' ) iIn++;
121718           continue;
121719         }
121720         if( c=='&' ){
121721           zFile[iOut++] = '\0';
121722         }else{
121723           eState = 2;
121724         }
121725         c = 0;
121726       }else if( (eState==0 && c=='?') || (eState==2 && c=='&') ){
121727         c = 0;
121728         eState = 1;
121729       }
121730       zFile[iOut++] = c;
121731     }
121732     if( eState==1 ) zFile[iOut++] = '\0';
121733     zFile[iOut++] = '\0';
121734     zFile[iOut++] = '\0';
121735 
121736     /* Check if there were any options specified that should be interpreted
121737     ** here. Options that are interpreted here include "vfs" and those that
121738     ** correspond to flags that may be passed to the sqlite3_open_v2()
121739     ** method. */
121740     zOpt = &zFile[sqlite3Strlen30(zFile)+1];
121741     while( zOpt[0] ){
121742       int nOpt = sqlite3Strlen30(zOpt);
121743       char *zVal = &zOpt[nOpt+1];
121744       int nVal = sqlite3Strlen30(zVal);
121745 
121746       if( nOpt==3 && memcmp("vfs", zOpt, 3)==0 ){
121747         zVfs = zVal;
121748       }else{
121749         struct OpenMode {
121750           const char *z;
121751           int mode;
121752         } *aMode = 0;
121753         char *zModeType = 0;
121754         int mask = 0;
121755         int limit = 0;
121756 
121757         if( nOpt==5 && memcmp("cache", zOpt, 5)==0 ){
121758           static struct OpenMode aCacheMode[] = {
121759             { "shared",  SQLITE_OPEN_SHAREDCACHE },
121760             { "private", SQLITE_OPEN_PRIVATECACHE },
121761             { 0, 0 }
121762           };
121763 
121764           mask = SQLITE_OPEN_SHAREDCACHE|SQLITE_OPEN_PRIVATECACHE;
121765           aMode = aCacheMode;
121766           limit = mask;
121767           zModeType = "cache";
121768         }
121769         if( nOpt==4 && memcmp("mode", zOpt, 4)==0 ){
121770           static struct OpenMode aOpenMode[] = {
121771             { "ro",  SQLITE_OPEN_READONLY },
121772             { "rw",  SQLITE_OPEN_READWRITE },
121773             { "rwc", SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE },
121774             { "memory", SQLITE_OPEN_MEMORY },
121775             { 0, 0 }
121776           };
121777 
121778           mask = SQLITE_OPEN_READONLY | SQLITE_OPEN_READWRITE
121779                    | SQLITE_OPEN_CREATE | SQLITE_OPEN_MEMORY;
121780           aMode = aOpenMode;
121781           limit = mask & flags;
121782           zModeType = "access";
121783         }
121784 
121785         if( aMode ){
121786           int i;
121787           int mode = 0;
121788           for(i=0; aMode[i].z; i++){
121789             const char *z = aMode[i].z;
121790             if( nVal==sqlite3Strlen30(z) && 0==memcmp(zVal, z, nVal) ){
121791               mode = aMode[i].mode;
121792               break;
121793             }
121794           }
121795           if( mode==0 ){
121796             *pzErrMsg = sqlite3_mprintf("no such %s mode: %s", zModeType, zVal);
121797             rc = SQLITE_ERROR;
121798             goto parse_uri_out;
121799           }
121800           if( (mode & ~SQLITE_OPEN_MEMORY)>limit ){
121801             *pzErrMsg = sqlite3_mprintf("%s mode not allowed: %s",
121802                                         zModeType, zVal);
121803             rc = SQLITE_PERM;
121804             goto parse_uri_out;
121805           }
121806           flags = (flags & ~mask) | mode;
121807         }
121808       }
121809 
121810       zOpt = &zVal[nVal+1];
121811     }
121812 
121813   }else{
121814     zFile = sqlite3_malloc(nUri+2);
121815     if( !zFile ) return SQLITE_NOMEM;
121816     memcpy(zFile, zUri, nUri);
121817     zFile[nUri] = '\0';
121818     zFile[nUri+1] = '\0';
121819     flags &= ~SQLITE_OPEN_URI;
121820   }
121821 
121822   *ppVfs = sqlite3_vfs_find(zVfs);
121823   if( *ppVfs==0 ){
121824     *pzErrMsg = sqlite3_mprintf("no such vfs: %s", zVfs);
121825     rc = SQLITE_ERROR;
121826   }
121827  parse_uri_out:
121828   if( rc!=SQLITE_OK ){
121829     sqlite3_free(zFile);
121830     zFile = 0;
121831   }
121832   *pFlags = flags;
121833   *pzFile = zFile;
121834   return rc;
121835 }
121836 
121837 
121838 /*
121839 ** This routine does the work of opening a database on behalf of
121840 ** sqlite3_open() and sqlite3_open16(). The database filename "zFilename"
121841 ** is UTF-8 encoded.
121842 */
121843 static int openDatabase(
121844   const char *zFilename, /* Database filename UTF-8 encoded */
121845   sqlite3 **ppDb,        /* OUT: Returned database handle */
121846   unsigned int flags,    /* Operational flags */
121847   const char *zVfs       /* Name of the VFS to use */
121848 ){
121849   sqlite3 *db;                    /* Store allocated handle here */
121850   int rc;                         /* Return code */
121851   int isThreadsafe;               /* True for threadsafe connections */
121852   char *zOpen = 0;                /* Filename argument to pass to BtreeOpen() */
121853   char *zErrMsg = 0;              /* Error message from sqlite3ParseUri() */
121854 
121855   *ppDb = 0;
121856 #ifndef SQLITE_OMIT_AUTOINIT
121857   rc = sqlite3_initialize();
121858   if( rc ) return rc;
121859 #endif
121860 
121861   /* Only allow sensible combinations of bits in the flags argument.
121862   ** Throw an error if any non-sense combination is used.  If we
121863   ** do not block illegal combinations here, it could trigger
121864   ** assert() statements in deeper layers.  Sensible combinations
121865   ** are:
121866   **
121867   **  1:  SQLITE_OPEN_READONLY
121868   **  2:  SQLITE_OPEN_READWRITE
121869   **  6:  SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE
121870   */
121871   assert( SQLITE_OPEN_READONLY  == 0x01 );
121872   assert( SQLITE_OPEN_READWRITE == 0x02 );
121873   assert( SQLITE_OPEN_CREATE    == 0x04 );
121874   testcase( (1<<(flags&7))==0x02 ); /* READONLY */
121875   testcase( (1<<(flags&7))==0x04 ); /* READWRITE */
121876   testcase( (1<<(flags&7))==0x40 ); /* READWRITE | CREATE */
121877   if( ((1<<(flags&7)) & 0x46)==0 ) return SQLITE_MISUSE_BKPT;
121878 
121879   if( sqlite3GlobalConfig.bCoreMutex==0 ){
121880     isThreadsafe = 0;
121881   }else if( flags & SQLITE_OPEN_NOMUTEX ){
121882     isThreadsafe = 0;
121883   }else if( flags & SQLITE_OPEN_FULLMUTEX ){
121884     isThreadsafe = 1;
121885   }else{
121886     isThreadsafe = sqlite3GlobalConfig.bFullMutex;
121887   }
121888   if( flags & SQLITE_OPEN_PRIVATECACHE ){
121889     flags &= ~SQLITE_OPEN_SHAREDCACHE;
121890   }else if( sqlite3GlobalConfig.sharedCacheEnabled ){
121891     flags |= SQLITE_OPEN_SHAREDCACHE;
121892   }
121893 
121894   /* Remove harmful bits from the flags parameter
121895   **
121896   ** The SQLITE_OPEN_NOMUTEX and SQLITE_OPEN_FULLMUTEX flags were
121897   ** dealt with in the previous code block.  Besides these, the only
121898   ** valid input flags for sqlite3_open_v2() are SQLITE_OPEN_READONLY,
121899   ** SQLITE_OPEN_READWRITE, SQLITE_OPEN_CREATE, SQLITE_OPEN_SHAREDCACHE,
121900   ** SQLITE_OPEN_PRIVATECACHE, and some reserved bits.  Silently mask
121901   ** off all other flags.
121902   */
121903   flags &=  ~( SQLITE_OPEN_DELETEONCLOSE |
121904                SQLITE_OPEN_EXCLUSIVE |
121905                SQLITE_OPEN_MAIN_DB |
121906                SQLITE_OPEN_TEMP_DB |
121907                SQLITE_OPEN_TRANSIENT_DB |
121908                SQLITE_OPEN_MAIN_JOURNAL |
121909                SQLITE_OPEN_TEMP_JOURNAL |
121910                SQLITE_OPEN_SUBJOURNAL |
121911                SQLITE_OPEN_MASTER_JOURNAL |
121912                SQLITE_OPEN_NOMUTEX |
121913                SQLITE_OPEN_FULLMUTEX |
121914                SQLITE_OPEN_WAL
121915              );
121916 
121917   /* Allocate the sqlite data structure */
121918   db = sqlite3MallocZero( sizeof(sqlite3) );
121919   if( db==0 ) goto opendb_out;
121920   if( isThreadsafe ){
121921     db->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_RECURSIVE);
121922     if( db->mutex==0 ){
121923       sqlite3_free(db);
121924       db = 0;
121925       goto opendb_out;
121926     }
121927   }
121928   sqlite3_mutex_enter(db->mutex);
121929   db->errMask = 0xff;
121930   db->nDb = 2;
121931   db->magic = SQLITE_MAGIC_BUSY;
121932   db->aDb = db->aDbStatic;
121933 
121934   assert( sizeof(db->aLimit)==sizeof(aHardLimit) );
121935   memcpy(db->aLimit, aHardLimit, sizeof(db->aLimit));
121936   db->autoCommit = 1;
121937   db->nextAutovac = -1;
121938   db->szMmap = sqlite3GlobalConfig.szMmap;
121939   db->nextPagesize = 0;
121940   db->flags |= SQLITE_ShortColNames | SQLITE_EnableTrigger | SQLITE_CacheSpill
121941 #if !defined(SQLITE_DEFAULT_AUTOMATIC_INDEX) || SQLITE_DEFAULT_AUTOMATIC_INDEX
121942                  | SQLITE_AutoIndex
121943 #endif
121944 #if SQLITE_DEFAULT_FILE_FORMAT<4
121945                  | SQLITE_LegacyFileFmt
121946 #endif
121947 #ifdef SQLITE_ENABLE_LOAD_EXTENSION
121948                  | SQLITE_LoadExtension
121949 #endif
121950 #if SQLITE_DEFAULT_RECURSIVE_TRIGGERS
121951                  | SQLITE_RecTriggers
121952 #endif
121953 #if defined(SQLITE_DEFAULT_FOREIGN_KEYS) && SQLITE_DEFAULT_FOREIGN_KEYS
121954                  | SQLITE_ForeignKeys
121955 #endif
121956       ;
121957   sqlite3HashInit(&db->aCollSeq);
121958 #ifndef SQLITE_OMIT_VIRTUALTABLE
121959   sqlite3HashInit(&db->aModule);
121960 #endif
121961 
121962   /* Add the default collation sequence BINARY. BINARY works for both UTF-8
121963   ** and UTF-16, so add a version for each to avoid any unnecessary
121964   ** conversions. The only error that can occur here is a malloc() failure.
121965   */
121966   createCollation(db, "BINARY", SQLITE_UTF8, 0, binCollFunc, 0);
121967   createCollation(db, "BINARY", SQLITE_UTF16BE, 0, binCollFunc, 0);
121968   createCollation(db, "BINARY", SQLITE_UTF16LE, 0, binCollFunc, 0);
121969   createCollation(db, "RTRIM", SQLITE_UTF8, (void*)1, binCollFunc, 0);
121970   if( db->mallocFailed ){
121971     goto opendb_out;
121972   }
121973   db->pDfltColl = sqlite3FindCollSeq(db, SQLITE_UTF8, "BINARY", 0);
121974   assert( db->pDfltColl!=0 );
121975 
121976   /* Also add a UTF-8 case-insensitive collation sequence. */
121977   createCollation(db, "NOCASE", SQLITE_UTF8, 0, nocaseCollatingFunc, 0);
121978 
121979   /* Parse the filename/URI argument. */
121980   db->openFlags = flags;
121981   rc = sqlite3ParseUri(zVfs, zFilename, &flags, &db->pVfs, &zOpen, &zErrMsg);
121982   if( rc!=SQLITE_OK ){
121983     if( rc==SQLITE_NOMEM ) db->mallocFailed = 1;
121984     sqlite3Error(db, rc, zErrMsg ? "%s" : 0, zErrMsg);
121985     sqlite3_free(zErrMsg);
121986     goto opendb_out;
121987   }
121988 
121989   /* Open the backend database driver */
121990   rc = sqlite3BtreeOpen(db->pVfs, zOpen, db, &db->aDb[0].pBt, 0,
121991                         flags | SQLITE_OPEN_MAIN_DB);
121992   if( rc!=SQLITE_OK ){
121993     if( rc==SQLITE_IOERR_NOMEM ){
121994       rc = SQLITE_NOMEM;
121995     }
121996     sqlite3Error(db, rc, 0);
121997     goto opendb_out;
121998   }
121999   db->aDb[0].pSchema = sqlite3SchemaGet(db, db->aDb[0].pBt);
122000   db->aDb[1].pSchema = sqlite3SchemaGet(db, 0);
122001 
122002 
122003   /* The default safety_level for the main database is 'full'; for the temp
122004   ** database it is 'NONE'. This matches the pager layer defaults.
122005   */
122006   db->aDb[0].zName = "main";
122007   db->aDb[0].safety_level = 3;
122008   db->aDb[1].zName = "temp";
122009   db->aDb[1].safety_level = 1;
122010 
122011   db->magic = SQLITE_MAGIC_OPEN;
122012   if( db->mallocFailed ){
122013     goto opendb_out;
122014   }
122015 
122016   /* Register all built-in functions, but do not attempt to read the
122017   ** database schema yet. This is delayed until the first time the database
122018   ** is accessed.
122019   */
122020   sqlite3Error(db, SQLITE_OK, 0);
122021   sqlite3RegisterBuiltinFunctions(db);
122022 
122023   /* Load automatic extensions - extensions that have been registered
122024   ** using the sqlite3_automatic_extension() API.
122025   */
122026   rc = sqlite3_errcode(db);
122027   if( rc==SQLITE_OK ){
122028     sqlite3AutoLoadExtensions(db);
122029     rc = sqlite3_errcode(db);
122030     if( rc!=SQLITE_OK ){
122031       goto opendb_out;
122032     }
122033   }
122034 
122035 #ifdef SQLITE_ENABLE_FTS1
122036   if( !db->mallocFailed ){
122037     extern int sqlite3Fts1Init(sqlite3*);
122038     rc = sqlite3Fts1Init(db);
122039   }
122040 #endif
122041 
122042 #ifdef SQLITE_ENABLE_FTS2
122043   if( !db->mallocFailed && rc==SQLITE_OK ){
122044     extern int sqlite3Fts2Init(sqlite3*);
122045     rc = sqlite3Fts2Init(db);
122046   }
122047 #endif
122048 
122049 #ifdef SQLITE_ENABLE_FTS3
122050   if( !db->mallocFailed && rc==SQLITE_OK ){
122051     rc = sqlite3Fts3Init(db);
122052   }
122053 #endif
122054 
122055 #ifdef SQLITE_ENABLE_ICU
122056   if( !db->mallocFailed && rc==SQLITE_OK ){
122057     rc = sqlite3IcuInit(db);
122058   }
122059 #endif
122060 
122061 #ifdef SQLITE_ENABLE_RTREE
122062   if( !db->mallocFailed && rc==SQLITE_OK){
122063     rc = sqlite3RtreeInit(db);
122064   }
122065 #endif
122066 
122067   /* -DSQLITE_DEFAULT_LOCKING_MODE=1 makes EXCLUSIVE the default locking
122068   ** mode.  -DSQLITE_DEFAULT_LOCKING_MODE=0 make NORMAL the default locking
122069   ** mode.  Doing nothing at all also makes NORMAL the default.
122070   */
122071 #ifdef SQLITE_DEFAULT_LOCKING_MODE
122072   db->dfltLockMode = SQLITE_DEFAULT_LOCKING_MODE;
122073   sqlite3PagerLockingMode(sqlite3BtreePager(db->aDb[0].pBt),
122074                           SQLITE_DEFAULT_LOCKING_MODE);
122075 #endif
122076 
122077   if( rc ) sqlite3Error(db, rc, 0);
122078 
122079   /* Enable the lookaside-malloc subsystem */
122080   setupLookaside(db, 0, sqlite3GlobalConfig.szLookaside,
122081                         sqlite3GlobalConfig.nLookaside);
122082 
122083   sqlite3_wal_autocheckpoint(db, SQLITE_DEFAULT_WAL_AUTOCHECKPOINT);
122084 
122085 opendb_out:
122086   sqlite3_free(zOpen);
122087   if( db ){
122088     assert( db->mutex!=0 || isThreadsafe==0 || sqlite3GlobalConfig.bFullMutex==0 );
122089     sqlite3_mutex_leave(db->mutex);
122090   }
122091   rc = sqlite3_errcode(db);
122092   assert( db!=0 || rc==SQLITE_NOMEM );
122093   if( rc==SQLITE_NOMEM ){
122094     sqlite3_close(db);
122095     db = 0;
122096   }else if( rc!=SQLITE_OK ){
122097     db->magic = SQLITE_MAGIC_SICK;
122098   }
122099   *ppDb = db;
122100 #ifdef SQLITE_ENABLE_SQLLOG
122101   if( sqlite3GlobalConfig.xSqllog ){
122102     /* Opening a db handle. Fourth parameter is passed 0. */
122103     void *pArg = sqlite3GlobalConfig.pSqllogArg;
122104     sqlite3GlobalConfig.xSqllog(pArg, db, zFilename, 0);
122105   }
122106 #endif
122107   return sqlite3ApiExit(0, rc);
122108 }
122109 
122110 /*
122111 ** Open a new database handle.
122112 */
122113 SQLITE_API int sqlite3_open(
122114   const char *zFilename,
122115   sqlite3 **ppDb
122116 ){
122117   return openDatabase(zFilename, ppDb,
122118                       SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, 0);
122119 }
122120 SQLITE_API int sqlite3_open_v2(
122121   const char *filename,   /* Database filename (UTF-8) */
122122   sqlite3 **ppDb,         /* OUT: SQLite db handle */
122123   int flags,              /* Flags */
122124   const char *zVfs        /* Name of VFS module to use */
122125 ){
122126   return openDatabase(filename, ppDb, (unsigned int)flags, zVfs);
122127 }
122128 
122129 #ifndef SQLITE_OMIT_UTF16
122130 /*
122131 ** Open a new database handle.
122132 */
122133 SQLITE_API int sqlite3_open16(
122134   const void *zFilename,
122135   sqlite3 **ppDb
122136 ){
122137   char const *zFilename8;   /* zFilename encoded in UTF-8 instead of UTF-16 */
122138   sqlite3_value *pVal;
122139   int rc;
122140 
122141   assert( zFilename );
122142   assert( ppDb );
122143   *ppDb = 0;
122144 #ifndef SQLITE_OMIT_AUTOINIT
122145   rc = sqlite3_initialize();
122146   if( rc ) return rc;
122147 #endif
122148   pVal = sqlite3ValueNew(0);
122149   sqlite3ValueSetStr(pVal, -1, zFilename, SQLITE_UTF16NATIVE, SQLITE_STATIC);
122150   zFilename8 = sqlite3ValueText(pVal, SQLITE_UTF8);
122151   if( zFilename8 ){
122152     rc = openDatabase(zFilename8, ppDb,
122153                       SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, 0);
122154     assert( *ppDb || rc==SQLITE_NOMEM );
122155     if( rc==SQLITE_OK && !DbHasProperty(*ppDb, 0, DB_SchemaLoaded) ){
122156       ENC(*ppDb) = SQLITE_UTF16NATIVE;
122157     }
122158   }else{
122159     rc = SQLITE_NOMEM;
122160   }
122161   sqlite3ValueFree(pVal);
122162 
122163   return sqlite3ApiExit(0, rc);
122164 }
122165 #endif /* SQLITE_OMIT_UTF16 */
122166 
122167 /*
122168 ** Register a new collation sequence with the database handle db.
122169 */
122170 SQLITE_API int sqlite3_create_collation(
122171   sqlite3* db,
122172   const char *zName,
122173   int enc,
122174   void* pCtx,
122175   int(*xCompare)(void*,int,const void*,int,const void*)
122176 ){
122177   int rc;
122178   sqlite3_mutex_enter(db->mutex);
122179   assert( !db->mallocFailed );
122180   rc = createCollation(db, zName, (u8)enc, pCtx, xCompare, 0);
122181   rc = sqlite3ApiExit(db, rc);
122182   sqlite3_mutex_leave(db->mutex);
122183   return rc;
122184 }
122185 
122186 /*
122187 ** Register a new collation sequence with the database handle db.
122188 */
122189 SQLITE_API int sqlite3_create_collation_v2(
122190   sqlite3* db,
122191   const char *zName,
122192   int enc,
122193   void* pCtx,
122194   int(*xCompare)(void*,int,const void*,int,const void*),
122195   void(*xDel)(void*)
122196 ){
122197   int rc;
122198   sqlite3_mutex_enter(db->mutex);
122199   assert( !db->mallocFailed );
122200   rc = createCollation(db, zName, (u8)enc, pCtx, xCompare, xDel);
122201   rc = sqlite3ApiExit(db, rc);
122202   sqlite3_mutex_leave(db->mutex);
122203   return rc;
122204 }
122205 
122206 #ifndef SQLITE_OMIT_UTF16
122207 /*
122208 ** Register a new collation sequence with the database handle db.
122209 */
122210 SQLITE_API int sqlite3_create_collation16(
122211   sqlite3* db,
122212   const void *zName,
122213   int enc,
122214   void* pCtx,
122215   int(*xCompare)(void*,int,const void*,int,const void*)
122216 ){
122217   int rc = SQLITE_OK;
122218   char *zName8;
122219   sqlite3_mutex_enter(db->mutex);
122220   assert( !db->mallocFailed );
122221   zName8 = sqlite3Utf16to8(db, zName, -1, SQLITE_UTF16NATIVE);
122222   if( zName8 ){
122223     rc = createCollation(db, zName8, (u8)enc, pCtx, xCompare, 0);
122224     sqlite3DbFree(db, zName8);
122225   }
122226   rc = sqlite3ApiExit(db, rc);
122227   sqlite3_mutex_leave(db->mutex);
122228   return rc;
122229 }
122230 #endif /* SQLITE_OMIT_UTF16 */
122231 
122232 /*
122233 ** Register a collation sequence factory callback with the database handle
122234 ** db. Replace any previously installed collation sequence factory.
122235 */
122236 SQLITE_API int sqlite3_collation_needed(
122237   sqlite3 *db,
122238   void *pCollNeededArg,
122239   void(*xCollNeeded)(void*,sqlite3*,int eTextRep,const char*)
122240 ){
122241   sqlite3_mutex_enter(db->mutex);
122242   db->xCollNeeded = xCollNeeded;
122243   db->xCollNeeded16 = 0;
122244   db->pCollNeededArg = pCollNeededArg;
122245   sqlite3_mutex_leave(db->mutex);
122246   return SQLITE_OK;
122247 }
122248 
122249 #ifndef SQLITE_OMIT_UTF16
122250 /*
122251 ** Register a collation sequence factory callback with the database handle
122252 ** db. Replace any previously installed collation sequence factory.
122253 */
122254 SQLITE_API int sqlite3_collation_needed16(
122255   sqlite3 *db,
122256   void *pCollNeededArg,
122257   void(*xCollNeeded16)(void*,sqlite3*,int eTextRep,const void*)
122258 ){
122259   sqlite3_mutex_enter(db->mutex);
122260   db->xCollNeeded = 0;
122261   db->xCollNeeded16 = xCollNeeded16;
122262   db->pCollNeededArg = pCollNeededArg;
122263   sqlite3_mutex_leave(db->mutex);
122264   return SQLITE_OK;
122265 }
122266 #endif /* SQLITE_OMIT_UTF16 */
122267 
122268 #ifndef SQLITE_OMIT_DEPRECATED
122269 /*
122270 ** This function is now an anachronism. It used to be used to recover from a
122271 ** malloc() failure, but SQLite now does this automatically.
122272 */
122273 SQLITE_API int sqlite3_global_recover(void){
122274   return SQLITE_OK;
122275 }
122276 #endif
122277 
122278 /*
122279 ** Test to see whether or not the database connection is in autocommit
122280 ** mode.  Return TRUE if it is and FALSE if not.  Autocommit mode is on
122281 ** by default.  Autocommit is disabled by a BEGIN statement and reenabled
122282 ** by the next COMMIT or ROLLBACK.
122283 */
122284 SQLITE_API int sqlite3_get_autocommit(sqlite3 *db){
122285   return db->autoCommit;
122286 }
122287 
122288 /*
122289 ** The following routines are subtitutes for constants SQLITE_CORRUPT,
122290 ** SQLITE_MISUSE, SQLITE_CANTOPEN, SQLITE_IOERR and possibly other error
122291 ** constants.  They server two purposes:
122292 **
122293 **   1.  Serve as a convenient place to set a breakpoint in a debugger
122294 **       to detect when version error conditions occurs.
122295 **
122296 **   2.  Invoke sqlite3_log() to provide the source code location where
122297 **       a low-level error is first detected.
122298 */
122299 SQLITE_PRIVATE int sqlite3CorruptError(int lineno){
122300   testcase( sqlite3GlobalConfig.xLog!=0 );
122301   sqlite3_log(SQLITE_CORRUPT,
122302               "database corruption at line %d of [%.10s]",
122303               lineno, 20+sqlite3_sourceid());
122304   return SQLITE_CORRUPT;
122305 }
122306 SQLITE_PRIVATE int sqlite3MisuseError(int lineno){
122307   testcase( sqlite3GlobalConfig.xLog!=0 );
122308   sqlite3_log(SQLITE_MISUSE,
122309               "misuse at line %d of [%.10s]",
122310               lineno, 20+sqlite3_sourceid());
122311   return SQLITE_MISUSE;
122312 }
122313 SQLITE_PRIVATE int sqlite3CantopenError(int lineno){
122314   testcase( sqlite3GlobalConfig.xLog!=0 );
122315   sqlite3_log(SQLITE_CANTOPEN,
122316               "cannot open file at line %d of [%.10s]",
122317               lineno, 20+sqlite3_sourceid());
122318   return SQLITE_CANTOPEN;
122319 }
122320 
122321 
122322 #ifndef SQLITE_OMIT_DEPRECATED
122323 /*
122324 ** This is a convenience routine that makes sure that all thread-specific
122325 ** data for this thread has been deallocated.
122326 **
122327 ** SQLite no longer uses thread-specific data so this routine is now a
122328 ** no-op.  It is retained for historical compatibility.
122329 */
122330 SQLITE_API void sqlite3_thread_cleanup(void){
122331 }
122332 #endif
122333 
122334 /*
122335 ** Return meta information about a specific column of a database table.
122336 ** See comment in sqlite3.h (sqlite.h.in) for details.
122337 */
122338 #ifdef SQLITE_ENABLE_COLUMN_METADATA
122339 SQLITE_API int sqlite3_table_column_metadata(
122340   sqlite3 *db,                /* Connection handle */
122341   const char *zDbName,        /* Database name or NULL */
122342   const char *zTableName,     /* Table name */
122343   const char *zColumnName,    /* Column name */
122344   char const **pzDataType,    /* OUTPUT: Declared data type */
122345   char const **pzCollSeq,     /* OUTPUT: Collation sequence name */
122346   int *pNotNull,              /* OUTPUT: True if NOT NULL constraint exists */
122347   int *pPrimaryKey,           /* OUTPUT: True if column part of PK */
122348   int *pAutoinc               /* OUTPUT: True if column is auto-increment */
122349 ){
122350   int rc;
122351   char *zErrMsg = 0;
122352   Table *pTab = 0;
122353   Column *pCol = 0;
122354   int iCol;
122355 
122356   char const *zDataType = 0;
122357   char const *zCollSeq = 0;
122358   int notnull = 0;
122359   int primarykey = 0;
122360   int autoinc = 0;
122361 
122362   /* Ensure the database schema has been loaded */
122363   sqlite3_mutex_enter(db->mutex);
122364   sqlite3BtreeEnterAll(db);
122365   rc = sqlite3Init(db, &zErrMsg);
122366   if( SQLITE_OK!=rc ){
122367     goto error_out;
122368   }
122369 
122370   /* Locate the table in question */
122371   pTab = sqlite3FindTable(db, zTableName, zDbName);
122372   if( !pTab || pTab->pSelect ){
122373     pTab = 0;
122374     goto error_out;
122375   }
122376 
122377   /* Find the column for which info is requested */
122378   if( sqlite3IsRowid(zColumnName) ){
122379     iCol = pTab->iPKey;
122380     if( iCol>=0 ){
122381       pCol = &pTab->aCol[iCol];
122382     }
122383   }else{
122384     for(iCol=0; iCol<pTab->nCol; iCol++){
122385       pCol = &pTab->aCol[iCol];
122386       if( 0==sqlite3StrICmp(pCol->zName, zColumnName) ){
122387         break;
122388       }
122389     }
122390     if( iCol==pTab->nCol ){
122391       pTab = 0;
122392       goto error_out;
122393     }
122394   }
122395 
122396   /* The following block stores the meta information that will be returned
122397   ** to the caller in local variables zDataType, zCollSeq, notnull, primarykey
122398   ** and autoinc. At this point there are two possibilities:
122399   **
122400   **     1. The specified column name was rowid", "oid" or "_rowid_"
122401   **        and there is no explicitly declared IPK column.
122402   **
122403   **     2. The table is not a view and the column name identified an
122404   **        explicitly declared column. Copy meta information from *pCol.
122405   */
122406   if( pCol ){
122407     zDataType = pCol->zType;
122408     zCollSeq = pCol->zColl;
122409     notnull = pCol->notNull!=0;
122410     primarykey  = (pCol->colFlags & COLFLAG_PRIMKEY)!=0;
122411     autoinc = pTab->iPKey==iCol && (pTab->tabFlags & TF_Autoincrement)!=0;
122412   }else{
122413     zDataType = "INTEGER";
122414     primarykey = 1;
122415   }
122416   if( !zCollSeq ){
122417     zCollSeq = "BINARY";
122418   }
122419 
122420 error_out:
122421   sqlite3BtreeLeaveAll(db);
122422 
122423   /* Whether the function call succeeded or failed, set the output parameters
122424   ** to whatever their local counterparts contain. If an error did occur,
122425   ** this has the effect of zeroing all output parameters.
122426   */
122427   if( pzDataType ) *pzDataType = zDataType;
122428   if( pzCollSeq ) *pzCollSeq = zCollSeq;
122429   if( pNotNull ) *pNotNull = notnull;
122430   if( pPrimaryKey ) *pPrimaryKey = primarykey;
122431   if( pAutoinc ) *pAutoinc = autoinc;
122432 
122433   if( SQLITE_OK==rc && !pTab ){
122434     sqlite3DbFree(db, zErrMsg);
122435     zErrMsg = sqlite3MPrintf(db, "no such table column: %s.%s", zTableName,
122436         zColumnName);
122437     rc = SQLITE_ERROR;
122438   }
122439   sqlite3Error(db, rc, (zErrMsg?"%s":0), zErrMsg);
122440   sqlite3DbFree(db, zErrMsg);
122441   rc = sqlite3ApiExit(db, rc);
122442   sqlite3_mutex_leave(db->mutex);
122443   return rc;
122444 }
122445 #endif
122446 
122447 /*
122448 ** Sleep for a little while.  Return the amount of time slept.
122449 */
122450 SQLITE_API int sqlite3_sleep(int ms){
122451   sqlite3_vfs *pVfs;
122452   int rc;
122453   pVfs = sqlite3_vfs_find(0);
122454   if( pVfs==0 ) return 0;
122455 
122456   /* This function works in milliseconds, but the underlying OsSleep()
122457   ** API uses microseconds. Hence the 1000's.
122458   */
122459   rc = (sqlite3OsSleep(pVfs, 1000*ms)/1000);
122460   return rc;
122461 }
122462 
122463 /*
122464 ** Enable or disable the extended result codes.
122465 */
122466 SQLITE_API int sqlite3_extended_result_codes(sqlite3 *db, int onoff){
122467   sqlite3_mutex_enter(db->mutex);
122468   db->errMask = onoff ? 0xffffffff : 0xff;
122469   sqlite3_mutex_leave(db->mutex);
122470   return SQLITE_OK;
122471 }
122472 
122473 /*
122474 ** Invoke the xFileControl method on a particular database.
122475 */
122476 SQLITE_API int sqlite3_file_control(sqlite3 *db, const char *zDbName, int op, void *pArg){
122477   int rc = SQLITE_ERROR;
122478   Btree *pBtree;
122479 
122480   sqlite3_mutex_enter(db->mutex);
122481   pBtree = sqlite3DbNameToBtree(db, zDbName);
122482   if( pBtree ){
122483     Pager *pPager;
122484     sqlite3_file *fd;
122485     sqlite3BtreeEnter(pBtree);
122486     pPager = sqlite3BtreePager(pBtree);
122487     assert( pPager!=0 );
122488     fd = sqlite3PagerFile(pPager);
122489     assert( fd!=0 );
122490     if( op==SQLITE_FCNTL_FILE_POINTER ){
122491       *(sqlite3_file**)pArg = fd;
122492       rc = SQLITE_OK;
122493     }else if( fd->pMethods ){
122494       rc = sqlite3OsFileControl(fd, op, pArg);
122495     }else{
122496       rc = SQLITE_NOTFOUND;
122497     }
122498     sqlite3BtreeLeave(pBtree);
122499   }
122500   sqlite3_mutex_leave(db->mutex);
122501   return rc;
122502 }
122503 
122504 /*
122505 ** Interface to the testing logic.
122506 */
122507 SQLITE_API int sqlite3_test_control(int op, ...){
122508   int rc = 0;
122509 #ifndef SQLITE_OMIT_BUILTIN_TEST
122510   va_list ap;
122511   va_start(ap, op);
122512   switch( op ){
122513 
122514     /*
122515     ** Save the current state of the PRNG.
122516     */
122517     case SQLITE_TESTCTRL_PRNG_SAVE: {
122518       sqlite3PrngSaveState();
122519       break;
122520     }
122521 
122522     /*
122523     ** Restore the state of the PRNG to the last state saved using
122524     ** PRNG_SAVE.  If PRNG_SAVE has never before been called, then
122525     ** this verb acts like PRNG_RESET.
122526     */
122527     case SQLITE_TESTCTRL_PRNG_RESTORE: {
122528       sqlite3PrngRestoreState();
122529       break;
122530     }
122531 
122532     /*
122533     ** Reset the PRNG back to its uninitialized state.  The next call
122534     ** to sqlite3_randomness() will reseed the PRNG using a single call
122535     ** to the xRandomness method of the default VFS.
122536     */
122537     case SQLITE_TESTCTRL_PRNG_RESET: {
122538       sqlite3_randomness(0,0);
122539       break;
122540     }
122541 
122542     /*
122543     **  sqlite3_test_control(BITVEC_TEST, size, program)
122544     **
122545     ** Run a test against a Bitvec object of size.  The program argument
122546     ** is an array of integers that defines the test.  Return -1 on a
122547     ** memory allocation error, 0 on success, or non-zero for an error.
122548     ** See the sqlite3BitvecBuiltinTest() for additional information.
122549     */
122550     case SQLITE_TESTCTRL_BITVEC_TEST: {
122551       int sz = va_arg(ap, int);
122552       int *aProg = va_arg(ap, int*);
122553       rc = sqlite3BitvecBuiltinTest(sz, aProg);
122554       break;
122555     }
122556 
122557     /*
122558     **  sqlite3_test_control(BENIGN_MALLOC_HOOKS, xBegin, xEnd)
122559     **
122560     ** Register hooks to call to indicate which malloc() failures
122561     ** are benign.
122562     */
122563     case SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS: {
122564       typedef void (*void_function)(void);
122565       void_function xBenignBegin;
122566       void_function xBenignEnd;
122567       xBenignBegin = va_arg(ap, void_function);
122568       xBenignEnd = va_arg(ap, void_function);
122569       sqlite3BenignMallocHooks(xBenignBegin, xBenignEnd);
122570       break;
122571     }
122572 
122573     /*
122574     **  sqlite3_test_control(SQLITE_TESTCTRL_PENDING_BYTE, unsigned int X)
122575     **
122576     ** Set the PENDING byte to the value in the argument, if X>0.
122577     ** Make no changes if X==0.  Return the value of the pending byte
122578     ** as it existing before this routine was called.
122579     **
122580     ** IMPORTANT:  Changing the PENDING byte from 0x40000000 results in
122581     ** an incompatible database file format.  Changing the PENDING byte
122582     ** while any database connection is open results in undefined and
122583     ** dileterious behavior.
122584     */
122585     case SQLITE_TESTCTRL_PENDING_BYTE: {
122586       rc = PENDING_BYTE;
122587 #ifndef SQLITE_OMIT_WSD
122588       {
122589         unsigned int newVal = va_arg(ap, unsigned int);
122590         if( newVal ) sqlite3PendingByte = newVal;
122591       }
122592 #endif
122593       break;
122594     }
122595 
122596     /*
122597     **  sqlite3_test_control(SQLITE_TESTCTRL_ASSERT, int X)
122598     **
122599     ** This action provides a run-time test to see whether or not
122600     ** assert() was enabled at compile-time.  If X is true and assert()
122601     ** is enabled, then the return value is true.  If X is true and
122602     ** assert() is disabled, then the return value is zero.  If X is
122603     ** false and assert() is enabled, then the assertion fires and the
122604     ** process aborts.  If X is false and assert() is disabled, then the
122605     ** return value is zero.
122606     */
122607     case SQLITE_TESTCTRL_ASSERT: {
122608       volatile int x = 0;
122609       assert( (x = va_arg(ap,int))!=0 );
122610       rc = x;
122611       break;
122612     }
122613 
122614 
122615     /*
122616     **  sqlite3_test_control(SQLITE_TESTCTRL_ALWAYS, int X)
122617     **
122618     ** This action provides a run-time test to see how the ALWAYS and
122619     ** NEVER macros were defined at compile-time.
122620     **
122621     ** The return value is ALWAYS(X).
122622     **
122623     ** The recommended test is X==2.  If the return value is 2, that means
122624     ** ALWAYS() and NEVER() are both no-op pass-through macros, which is the
122625     ** default setting.  If the return value is 1, then ALWAYS() is either
122626     ** hard-coded to true or else it asserts if its argument is false.
122627     ** The first behavior (hard-coded to true) is the case if
122628     ** SQLITE_TESTCTRL_ASSERT shows that assert() is disabled and the second
122629     ** behavior (assert if the argument to ALWAYS() is false) is the case if
122630     ** SQLITE_TESTCTRL_ASSERT shows that assert() is enabled.
122631     **
122632     ** The run-time test procedure might look something like this:
122633     **
122634     **    if( sqlite3_test_control(SQLITE_TESTCTRL_ALWAYS, 2)==2 ){
122635     **      // ALWAYS() and NEVER() are no-op pass-through macros
122636     **    }else if( sqlite3_test_control(SQLITE_TESTCTRL_ASSERT, 1) ){
122637     **      // ALWAYS(x) asserts that x is true. NEVER(x) asserts x is false.
122638     **    }else{
122639     **      // ALWAYS(x) is a constant 1.  NEVER(x) is a constant 0.
122640     **    }
122641     */
122642     case SQLITE_TESTCTRL_ALWAYS: {
122643       int x = va_arg(ap,int);
122644       rc = ALWAYS(x);
122645       break;
122646     }
122647 
122648     /*   sqlite3_test_control(SQLITE_TESTCTRL_RESERVE, sqlite3 *db, int N)
122649     **
122650     ** Set the nReserve size to N for the main database on the database
122651     ** connection db.
122652     */
122653     case SQLITE_TESTCTRL_RESERVE: {
122654       sqlite3 *db = va_arg(ap, sqlite3*);
122655       int x = va_arg(ap,int);
122656       sqlite3_mutex_enter(db->mutex);
122657       sqlite3BtreeSetPageSize(db->aDb[0].pBt, 0, x, 0);
122658       sqlite3_mutex_leave(db->mutex);
122659       break;
122660     }
122661 
122662     /*  sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS, sqlite3 *db, int N)
122663     **
122664     ** Enable or disable various optimizations for testing purposes.  The
122665     ** argument N is a bitmask of optimizations to be disabled.  For normal
122666     ** operation N should be 0.  The idea is that a test program (like the
122667     ** SQL Logic Test or SLT test module) can run the same SQL multiple times
122668     ** with various optimizations disabled to verify that the same answer
122669     ** is obtained in every case.
122670     */
122671     case SQLITE_TESTCTRL_OPTIMIZATIONS: {
122672       sqlite3 *db = va_arg(ap, sqlite3*);
122673       db->dbOptFlags = (u16)(va_arg(ap, int) & 0xffff);
122674       break;
122675     }
122676 
122677 #ifdef SQLITE_N_KEYWORD
122678     /* sqlite3_test_control(SQLITE_TESTCTRL_ISKEYWORD, const char *zWord)
122679     **
122680     ** If zWord is a keyword recognized by the parser, then return the
122681     ** number of keywords.  Or if zWord is not a keyword, return 0.
122682     **
122683     ** This test feature is only available in the amalgamation since
122684     ** the SQLITE_N_KEYWORD macro is not defined in this file if SQLite
122685     ** is built using separate source files.
122686     */
122687     case SQLITE_TESTCTRL_ISKEYWORD: {
122688       const char *zWord = va_arg(ap, const char*);
122689       int n = sqlite3Strlen30(zWord);
122690       rc = (sqlite3KeywordCode((u8*)zWord, n)!=TK_ID) ? SQLITE_N_KEYWORD : 0;
122691       break;
122692     }
122693 #endif
122694 
122695     /* sqlite3_test_control(SQLITE_TESTCTRL_SCRATCHMALLOC, sz, &pNew, pFree);
122696     **
122697     ** Pass pFree into sqlite3ScratchFree().
122698     ** If sz>0 then allocate a scratch buffer into pNew.
122699     */
122700     case SQLITE_TESTCTRL_SCRATCHMALLOC: {
122701       void *pFree, **ppNew;
122702       int sz;
122703       sz = va_arg(ap, int);
122704       ppNew = va_arg(ap, void**);
122705       pFree = va_arg(ap, void*);
122706       if( sz ) *ppNew = sqlite3ScratchMalloc(sz);
122707       sqlite3ScratchFree(pFree);
122708       break;
122709     }
122710 
122711     /*   sqlite3_test_control(SQLITE_TESTCTRL_LOCALTIME_FAULT, int onoff);
122712     **
122713     ** If parameter onoff is non-zero, configure the wrappers so that all
122714     ** subsequent calls to localtime() and variants fail. If onoff is zero,
122715     ** undo this setting.
122716     */
122717     case SQLITE_TESTCTRL_LOCALTIME_FAULT: {
122718       sqlite3GlobalConfig.bLocaltimeFault = va_arg(ap, int);
122719       break;
122720     }
122721 
122722 #if defined(SQLITE_ENABLE_TREE_EXPLAIN)
122723     /*   sqlite3_test_control(SQLITE_TESTCTRL_EXPLAIN_STMT,
122724     **                        sqlite3_stmt*,const char**);
122725     **
122726     ** If compiled with SQLITE_ENABLE_TREE_EXPLAIN, each sqlite3_stmt holds
122727     ** a string that describes the optimized parse tree.  This test-control
122728     ** returns a pointer to that string.
122729     */
122730     case SQLITE_TESTCTRL_EXPLAIN_STMT: {
122731       sqlite3_stmt *pStmt = va_arg(ap, sqlite3_stmt*);
122732       const char **pzRet = va_arg(ap, const char**);
122733       *pzRet = sqlite3VdbeExplanation((Vdbe*)pStmt);
122734       break;
122735     }
122736 #endif
122737 
122738     /*   sqlite3_test_control(SQLITE_TESTCTRL_NEVER_CORRUPT, int);
122739     **
122740     ** Set or clear a flag that indicates that the database file is always well-
122741     ** formed and never corrupt.  This flag is clear by default, indicating that
122742     ** database files might have arbitrary corruption.  Setting the flag during
122743     ** testing causes certain assert() statements in the code to be activated
122744     ** that demonstrat invariants on well-formed database files.
122745     */
122746     case SQLITE_TESTCTRL_NEVER_CORRUPT: {
122747       sqlite3GlobalConfig.neverCorrupt = va_arg(ap, int);
122748       break;
122749     }
122750 
122751   }
122752   va_end(ap);
122753 #endif /* SQLITE_OMIT_BUILTIN_TEST */
122754   return rc;
122755 }
122756 
122757 /*
122758 ** This is a utility routine, useful to VFS implementations, that checks
122759 ** to see if a database file was a URI that contained a specific query
122760 ** parameter, and if so obtains the value of the query parameter.
122761 **
122762 ** The zFilename argument is the filename pointer passed into the xOpen()
122763 ** method of a VFS implementation.  The zParam argument is the name of the
122764 ** query parameter we seek.  This routine returns the value of the zParam
122765 ** parameter if it exists.  If the parameter does not exist, this routine
122766 ** returns a NULL pointer.
122767 */
122768 SQLITE_API const char *sqlite3_uri_parameter(const char *zFilename, const char *zParam){
122769   if( zFilename==0 ) return 0;
122770   zFilename += sqlite3Strlen30(zFilename) + 1;
122771   while( zFilename[0] ){
122772     int x = strcmp(zFilename, zParam);
122773     zFilename += sqlite3Strlen30(zFilename) + 1;
122774     if( x==0 ) return zFilename;
122775     zFilename += sqlite3Strlen30(zFilename) + 1;
122776   }
122777   return 0;
122778 }
122779 
122780 /*
122781 ** Return a boolean value for a query parameter.
122782 */
122783 SQLITE_API int sqlite3_uri_boolean(const char *zFilename, const char *zParam, int bDflt){
122784   const char *z = sqlite3_uri_parameter(zFilename, zParam);
122785   bDflt = bDflt!=0;
122786   return z ? sqlite3GetBoolean(z, bDflt) : bDflt;
122787 }
122788 
122789 /*
122790 ** Return a 64-bit integer value for a query parameter.
122791 */
122792 SQLITE_API sqlite3_int64 sqlite3_uri_int64(
122793   const char *zFilename,    /* Filename as passed to xOpen */
122794   const char *zParam,       /* URI parameter sought */
122795   sqlite3_int64 bDflt       /* return if parameter is missing */
122796 ){
122797   const char *z = sqlite3_uri_parameter(zFilename, zParam);
122798   sqlite3_int64 v;
122799   if( z && sqlite3Atoi64(z, &v, sqlite3Strlen30(z), SQLITE_UTF8)==SQLITE_OK ){
122800     bDflt = v;
122801   }
122802   return bDflt;
122803 }
122804 
122805 /*
122806 ** Return the Btree pointer identified by zDbName.  Return NULL if not found.
122807 */
122808 SQLITE_PRIVATE Btree *sqlite3DbNameToBtree(sqlite3 *db, const char *zDbName){
122809   int i;
122810   for(i=0; i<db->nDb; i++){
122811     if( db->aDb[i].pBt
122812      && (zDbName==0 || sqlite3StrICmp(zDbName, db->aDb[i].zName)==0)
122813     ){
122814       return db->aDb[i].pBt;
122815     }
122816   }
122817   return 0;
122818 }
122819 
122820 /*
122821 ** Return the filename of the database associated with a database
122822 ** connection.
122823 */
122824 SQLITE_API const char *sqlite3_db_filename(sqlite3 *db, const char *zDbName){
122825   Btree *pBt = sqlite3DbNameToBtree(db, zDbName);
122826   return pBt ? sqlite3BtreeGetFilename(pBt) : 0;
122827 }
122828 
122829 /*
122830 ** Return 1 if database is read-only or 0 if read/write.  Return -1 if
122831 ** no such database exists.
122832 */
122833 SQLITE_API int sqlite3_db_readonly(sqlite3 *db, const char *zDbName){
122834   Btree *pBt = sqlite3DbNameToBtree(db, zDbName);
122835   return pBt ? sqlite3PagerIsreadonly(sqlite3BtreePager(pBt)) : -1;
122836 }
122837 
122838 /************** End of main.c ************************************************/
122839 /************** Begin file notify.c ******************************************/
122840 /*
122841 ** 2009 March 3
122842 **
122843 ** The author disclaims copyright to this source code.  In place of
122844 ** a legal notice, here is a blessing:
122845 **
122846 **    May you do good and not evil.
122847 **    May you find forgiveness for yourself and forgive others.
122848 **    May you share freely, never taking more than you give.
122849 **
122850 *************************************************************************
122851 **
122852 ** This file contains the implementation of the sqlite3_unlock_notify()
122853 ** API method and its associated functionality.
122854 */
122855 
122856 /* Omit this entire file if SQLITE_ENABLE_UNLOCK_NOTIFY is not defined. */
122857 #ifdef SQLITE_ENABLE_UNLOCK_NOTIFY
122858 
122859 /*
122860 ** Public interfaces:
122861 **
122862 **   sqlite3ConnectionBlocked()
122863 **   sqlite3ConnectionUnlocked()
122864 **   sqlite3ConnectionClosed()
122865 **   sqlite3_unlock_notify()
122866 */
122867 
122868 #define assertMutexHeld() \
122869   assert( sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)) )
122870 
122871 /*
122872 ** Head of a linked list of all sqlite3 objects created by this process
122873 ** for which either sqlite3.pBlockingConnection or sqlite3.pUnlockConnection
122874 ** is not NULL. This variable may only accessed while the STATIC_MASTER
122875 ** mutex is held.
122876 */
122877 static sqlite3 *SQLITE_WSD sqlite3BlockedList = 0;
122878 
122879 #ifndef NDEBUG
122880 /*
122881 ** This function is a complex assert() that verifies the following
122882 ** properties of the blocked connections list:
122883 **
122884 **   1) Each entry in the list has a non-NULL value for either
122885 **      pUnlockConnection or pBlockingConnection, or both.
122886 **
122887 **   2) All entries in the list that share a common value for
122888 **      xUnlockNotify are grouped together.
122889 **
122890 **   3) If the argument db is not NULL, then none of the entries in the
122891 **      blocked connections list have pUnlockConnection or pBlockingConnection
122892 **      set to db. This is used when closing connection db.
122893 */
122894 static void checkListProperties(sqlite3 *db){
122895   sqlite3 *p;
122896   for(p=sqlite3BlockedList; p; p=p->pNextBlocked){
122897     int seen = 0;
122898     sqlite3 *p2;
122899 
122900     /* Verify property (1) */
122901     assert( p->pUnlockConnection || p->pBlockingConnection );
122902 
122903     /* Verify property (2) */
122904     for(p2=sqlite3BlockedList; p2!=p; p2=p2->pNextBlocked){
122905       if( p2->xUnlockNotify==p->xUnlockNotify ) seen = 1;
122906       assert( p2->xUnlockNotify==p->xUnlockNotify || !seen );
122907       assert( db==0 || p->pUnlockConnection!=db );
122908       assert( db==0 || p->pBlockingConnection!=db );
122909     }
122910   }
122911 }
122912 #else
122913 # define checkListProperties(x)
122914 #endif
122915 
122916 /*
122917 ** Remove connection db from the blocked connections list. If connection
122918 ** db is not currently a part of the list, this function is a no-op.
122919 */
122920 static void removeFromBlockedList(sqlite3 *db){
122921   sqlite3 **pp;
122922   assertMutexHeld();
122923   for(pp=&sqlite3BlockedList; *pp; pp = &(*pp)->pNextBlocked){
122924     if( *pp==db ){
122925       *pp = (*pp)->pNextBlocked;
122926       break;
122927     }
122928   }
122929 }
122930 
122931 /*
122932 ** Add connection db to the blocked connections list. It is assumed
122933 ** that it is not already a part of the list.
122934 */
122935 static void addToBlockedList(sqlite3 *db){
122936   sqlite3 **pp;
122937   assertMutexHeld();
122938   for(
122939     pp=&sqlite3BlockedList;
122940     *pp && (*pp)->xUnlockNotify!=db->xUnlockNotify;
122941     pp=&(*pp)->pNextBlocked
122942   );
122943   db->pNextBlocked = *pp;
122944   *pp = db;
122945 }
122946 
122947 /*
122948 ** Obtain the STATIC_MASTER mutex.
122949 */
122950 static void enterMutex(void){
122951   sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
122952   checkListProperties(0);
122953 }
122954 
122955 /*
122956 ** Release the STATIC_MASTER mutex.
122957 */
122958 static void leaveMutex(void){
122959   assertMutexHeld();
122960   checkListProperties(0);
122961   sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
122962 }
122963 
122964 /*
122965 ** Register an unlock-notify callback.
122966 **
122967 ** This is called after connection "db" has attempted some operation
122968 ** but has received an SQLITE_LOCKED error because another connection
122969 ** (call it pOther) in the same process was busy using the same shared
122970 ** cache.  pOther is found by looking at db->pBlockingConnection.
122971 **
122972 ** If there is no blocking connection, the callback is invoked immediately,
122973 ** before this routine returns.
122974 **
122975 ** If pOther is already blocked on db, then report SQLITE_LOCKED, to indicate
122976 ** a deadlock.
122977 **
122978 ** Otherwise, make arrangements to invoke xNotify when pOther drops
122979 ** its locks.
122980 **
122981 ** Each call to this routine overrides any prior callbacks registered
122982 ** on the same "db".  If xNotify==0 then any prior callbacks are immediately
122983 ** cancelled.
122984 */
122985 SQLITE_API int sqlite3_unlock_notify(
122986   sqlite3 *db,
122987   void (*xNotify)(void **, int),
122988   void *pArg
122989 ){
122990   int rc = SQLITE_OK;
122991 
122992   sqlite3_mutex_enter(db->mutex);
122993   enterMutex();
122994 
122995   if( xNotify==0 ){
122996     removeFromBlockedList(db);
122997     db->pBlockingConnection = 0;
122998     db->pUnlockConnection = 0;
122999     db->xUnlockNotify = 0;
123000     db->pUnlockArg = 0;
123001   }else if( 0==db->pBlockingConnection ){
123002     /* The blocking transaction has been concluded. Or there never was a
123003     ** blocking transaction. In either case, invoke the notify callback
123004     ** immediately.
123005     */
123006     xNotify(&pArg, 1);
123007   }else{
123008     sqlite3 *p;
123009 
123010     for(p=db->pBlockingConnection; p && p!=db; p=p->pUnlockConnection){}
123011     if( p ){
123012       rc = SQLITE_LOCKED;              /* Deadlock detected. */
123013     }else{
123014       db->pUnlockConnection = db->pBlockingConnection;
123015       db->xUnlockNotify = xNotify;
123016       db->pUnlockArg = pArg;
123017       removeFromBlockedList(db);
123018       addToBlockedList(db);
123019     }
123020   }
123021 
123022   leaveMutex();
123023   assert( !db->mallocFailed );
123024   sqlite3Error(db, rc, (rc?"database is deadlocked":0));
123025   sqlite3_mutex_leave(db->mutex);
123026   return rc;
123027 }
123028 
123029 /*
123030 ** This function is called while stepping or preparing a statement
123031 ** associated with connection db. The operation will return SQLITE_LOCKED
123032 ** to the user because it requires a lock that will not be available
123033 ** until connection pBlocker concludes its current transaction.
123034 */
123035 SQLITE_PRIVATE void sqlite3ConnectionBlocked(sqlite3 *db, sqlite3 *pBlocker){
123036   enterMutex();
123037   if( db->pBlockingConnection==0 && db->pUnlockConnection==0 ){
123038     addToBlockedList(db);
123039   }
123040   db->pBlockingConnection = pBlocker;
123041   leaveMutex();
123042 }
123043 
123044 /*
123045 ** This function is called when
123046 ** the transaction opened by database db has just finished. Locks held
123047 ** by database connection db have been released.
123048 **
123049 ** This function loops through each entry in the blocked connections
123050 ** list and does the following:
123051 **
123052 **   1) If the sqlite3.pBlockingConnection member of a list entry is
123053 **      set to db, then set pBlockingConnection=0.
123054 **
123055 **   2) If the sqlite3.pUnlockConnection member of a list entry is
123056 **      set to db, then invoke the configured unlock-notify callback and
123057 **      set pUnlockConnection=0.
123058 **
123059 **   3) If the two steps above mean that pBlockingConnection==0 and
123060 **      pUnlockConnection==0, remove the entry from the blocked connections
123061 **      list.
123062 */
123063 SQLITE_PRIVATE void sqlite3ConnectionUnlocked(sqlite3 *db){
123064   void (*xUnlockNotify)(void **, int) = 0; /* Unlock-notify cb to invoke */
123065   int nArg = 0;                            /* Number of entries in aArg[] */
123066   sqlite3 **pp;                            /* Iterator variable */
123067   void **aArg;               /* Arguments to the unlock callback */
123068   void **aDyn = 0;           /* Dynamically allocated space for aArg[] */
123069   void *aStatic[16];         /* Starter space for aArg[].  No malloc required */
123070 
123071   aArg = aStatic;
123072   enterMutex();         /* Enter STATIC_MASTER mutex */
123073 
123074   /* This loop runs once for each entry in the blocked-connections list. */
123075   for(pp=&sqlite3BlockedList; *pp; /* no-op */ ){
123076     sqlite3 *p = *pp;
123077 
123078     /* Step 1. */
123079     if( p->pBlockingConnection==db ){
123080       p->pBlockingConnection = 0;
123081     }
123082 
123083     /* Step 2. */
123084     if( p->pUnlockConnection==db ){
123085       assert( p->xUnlockNotify );
123086       if( p->xUnlockNotify!=xUnlockNotify && nArg!=0 ){
123087         xUnlockNotify(aArg, nArg);
123088         nArg = 0;
123089       }
123090 
123091       sqlite3BeginBenignMalloc();
123092       assert( aArg==aDyn || (aDyn==0 && aArg==aStatic) );
123093       assert( nArg<=(int)ArraySize(aStatic) || aArg==aDyn );
123094       if( (!aDyn && nArg==(int)ArraySize(aStatic))
123095        || (aDyn && nArg==(int)(sqlite3MallocSize(aDyn)/sizeof(void*)))
123096       ){
123097         /* The aArg[] array needs to grow. */
123098         void **pNew = (void **)sqlite3Malloc(nArg*sizeof(void *)*2);
123099         if( pNew ){
123100           memcpy(pNew, aArg, nArg*sizeof(void *));
123101           sqlite3_free(aDyn);
123102           aDyn = aArg = pNew;
123103         }else{
123104           /* This occurs when the array of context pointers that need to
123105           ** be passed to the unlock-notify callback is larger than the
123106           ** aStatic[] array allocated on the stack and the attempt to
123107           ** allocate a larger array from the heap has failed.
123108           **
123109           ** This is a difficult situation to handle. Returning an error
123110           ** code to the caller is insufficient, as even if an error code
123111           ** is returned the transaction on connection db will still be
123112           ** closed and the unlock-notify callbacks on blocked connections
123113           ** will go unissued. This might cause the application to wait
123114           ** indefinitely for an unlock-notify callback that will never
123115           ** arrive.
123116           **
123117           ** Instead, invoke the unlock-notify callback with the context
123118           ** array already accumulated. We can then clear the array and
123119           ** begin accumulating any further context pointers without
123120           ** requiring any dynamic allocation. This is sub-optimal because
123121           ** it means that instead of one callback with a large array of
123122           ** context pointers the application will receive two or more
123123           ** callbacks with smaller arrays of context pointers, which will
123124           ** reduce the applications ability to prioritize multiple
123125           ** connections. But it is the best that can be done under the
123126           ** circumstances.
123127           */
123128           xUnlockNotify(aArg, nArg);
123129           nArg = 0;
123130         }
123131       }
123132       sqlite3EndBenignMalloc();
123133 
123134       aArg[nArg++] = p->pUnlockArg;
123135       xUnlockNotify = p->xUnlockNotify;
123136       p->pUnlockConnection = 0;
123137       p->xUnlockNotify = 0;
123138       p->pUnlockArg = 0;
123139     }
123140 
123141     /* Step 3. */
123142     if( p->pBlockingConnection==0 && p->pUnlockConnection==0 ){
123143       /* Remove connection p from the blocked connections list. */
123144       *pp = p->pNextBlocked;
123145       p->pNextBlocked = 0;
123146     }else{
123147       pp = &p->pNextBlocked;
123148     }
123149   }
123150 
123151   if( nArg!=0 ){
123152     xUnlockNotify(aArg, nArg);
123153   }
123154   sqlite3_free(aDyn);
123155   leaveMutex();         /* Leave STATIC_MASTER mutex */
123156 }
123157 
123158 /*
123159 ** This is called when the database connection passed as an argument is
123160 ** being closed. The connection is removed from the blocked list.
123161 */
123162 SQLITE_PRIVATE void sqlite3ConnectionClosed(sqlite3 *db){
123163   sqlite3ConnectionUnlocked(db);
123164   enterMutex();
123165   removeFromBlockedList(db);
123166   checkListProperties(db);
123167   leaveMutex();
123168 }
123169 #endif
123170 
123171 /************** End of notify.c **********************************************/
123172 /************** Begin file fts3.c ********************************************/
123173 /*
123174 ** 2006 Oct 10
123175 **
123176 ** The author disclaims copyright to this source code.  In place of
123177 ** a legal notice, here is a blessing:
123178 **
123179 **    May you do good and not evil.
123180 **    May you find forgiveness for yourself and forgive others.
123181 **    May you share freely, never taking more than you give.
123182 **
123183 ******************************************************************************
123184 **
123185 ** This is an SQLite module implementing full-text search.
123186 */
123187 
123188 /*
123189 ** The code in this file is only compiled if:
123190 **
123191 **     * The FTS3 module is being built as an extension
123192 **       (in which case SQLITE_CORE is not defined), or
123193 **
123194 **     * The FTS3 module is being built into the core of
123195 **       SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
123196 */
123197 
123198 /* The full-text index is stored in a series of b+tree (-like)
123199 ** structures called segments which map terms to doclists.  The
123200 ** structures are like b+trees in layout, but are constructed from the
123201 ** bottom up in optimal fashion and are not updatable.  Since trees
123202 ** are built from the bottom up, things will be described from the
123203 ** bottom up.
123204 **
123205 **
123206 **** Varints ****
123207 ** The basic unit of encoding is a variable-length integer called a
123208 ** varint.  We encode variable-length integers in little-endian order
123209 ** using seven bits * per byte as follows:
123210 **
123211 ** KEY:
123212 **         A = 0xxxxxxx    7 bits of data and one flag bit
123213 **         B = 1xxxxxxx    7 bits of data and one flag bit
123214 **
123215 **  7 bits - A
123216 ** 14 bits - BA
123217 ** 21 bits - BBA
123218 ** and so on.
123219 **
123220 ** This is similar in concept to how sqlite encodes "varints" but
123221 ** the encoding is not the same.  SQLite varints are big-endian
123222 ** are are limited to 9 bytes in length whereas FTS3 varints are
123223 ** little-endian and can be up to 10 bytes in length (in theory).
123224 **
123225 ** Example encodings:
123226 **
123227 **     1:    0x01
123228 **   127:    0x7f
123229 **   128:    0x81 0x00
123230 **
123231 **
123232 **** Document lists ****
123233 ** A doclist (document list) holds a docid-sorted list of hits for a
123234 ** given term.  Doclists hold docids and associated token positions.
123235 ** A docid is the unique integer identifier for a single document.
123236 ** A position is the index of a word within the document.  The first
123237 ** word of the document has a position of 0.
123238 **
123239 ** FTS3 used to optionally store character offsets using a compile-time
123240 ** option.  But that functionality is no longer supported.
123241 **
123242 ** A doclist is stored like this:
123243 **
123244 ** array {
123245 **   varint docid;          (delta from previous doclist)
123246 **   array {                (position list for column 0)
123247 **     varint position;     (2 more than the delta from previous position)
123248 **   }
123249 **   array {
123250 **     varint POS_COLUMN;   (marks start of position list for new column)
123251 **     varint column;       (index of new column)
123252 **     array {
123253 **       varint position;   (2 more than the delta from previous position)
123254 **     }
123255 **   }
123256 **   varint POS_END;        (marks end of positions for this document.
123257 ** }
123258 **
123259 ** Here, array { X } means zero or more occurrences of X, adjacent in
123260 ** memory.  A "position" is an index of a token in the token stream
123261 ** generated by the tokenizer. Note that POS_END and POS_COLUMN occur
123262 ** in the same logical place as the position element, and act as sentinals
123263 ** ending a position list array.  POS_END is 0.  POS_COLUMN is 1.
123264 ** The positions numbers are not stored literally but rather as two more
123265 ** than the difference from the prior position, or the just the position plus
123266 ** 2 for the first position.  Example:
123267 **
123268 **   label:       A B C D E  F  G H   I  J K
123269 **   value:     123 5 9 1 1 14 35 0 234 72 0
123270 **
123271 ** The 123 value is the first docid.  For column zero in this document
123272 ** there are two matches at positions 3 and 10 (5-2 and 9-2+3).  The 1
123273 ** at D signals the start of a new column; the 1 at E indicates that the
123274 ** new column is column number 1.  There are two positions at 12 and 45
123275 ** (14-2 and 35-2+12).  The 0 at H indicate the end-of-document.  The
123276 ** 234 at I is the delta to next docid (357).  It has one position 70
123277 ** (72-2) and then terminates with the 0 at K.
123278 **
123279 ** A "position-list" is the list of positions for multiple columns for
123280 ** a single docid.  A "column-list" is the set of positions for a single
123281 ** column.  Hence, a position-list consists of one or more column-lists,
123282 ** a document record consists of a docid followed by a position-list and
123283 ** a doclist consists of one or more document records.
123284 **
123285 ** A bare doclist omits the position information, becoming an
123286 ** array of varint-encoded docids.
123287 **
123288 **** Segment leaf nodes ****
123289 ** Segment leaf nodes store terms and doclists, ordered by term.  Leaf
123290 ** nodes are written using LeafWriter, and read using LeafReader (to
123291 ** iterate through a single leaf node's data) and LeavesReader (to
123292 ** iterate through a segment's entire leaf layer).  Leaf nodes have
123293 ** the format:
123294 **
123295 ** varint iHeight;             (height from leaf level, always 0)
123296 ** varint nTerm;               (length of first term)
123297 ** char pTerm[nTerm];          (content of first term)
123298 ** varint nDoclist;            (length of term's associated doclist)
123299 ** char pDoclist[nDoclist];    (content of doclist)
123300 ** array {
123301 **                             (further terms are delta-encoded)
123302 **   varint nPrefix;           (length of prefix shared with previous term)
123303 **   varint nSuffix;           (length of unshared suffix)
123304 **   char pTermSuffix[nSuffix];(unshared suffix of next term)
123305 **   varint nDoclist;          (length of term's associated doclist)
123306 **   char pDoclist[nDoclist];  (content of doclist)
123307 ** }
123308 **
123309 ** Here, array { X } means zero or more occurrences of X, adjacent in
123310 ** memory.
123311 **
123312 ** Leaf nodes are broken into blocks which are stored contiguously in
123313 ** the %_segments table in sorted order.  This means that when the end
123314 ** of a node is reached, the next term is in the node with the next
123315 ** greater node id.
123316 **
123317 ** New data is spilled to a new leaf node when the current node
123318 ** exceeds LEAF_MAX bytes (default 2048).  New data which itself is
123319 ** larger than STANDALONE_MIN (default 1024) is placed in a standalone
123320 ** node (a leaf node with a single term and doclist).  The goal of
123321 ** these settings is to pack together groups of small doclists while
123322 ** making it efficient to directly access large doclists.  The
123323 ** assumption is that large doclists represent terms which are more
123324 ** likely to be query targets.
123325 **
123326 ** TODO(shess) It may be useful for blocking decisions to be more
123327 ** dynamic.  For instance, it may make more sense to have a 2.5k leaf
123328 ** node rather than splitting into 2k and .5k nodes.  My intuition is
123329 ** that this might extend through 2x or 4x the pagesize.
123330 **
123331 **
123332 **** Segment interior nodes ****
123333 ** Segment interior nodes store blockids for subtree nodes and terms
123334 ** to describe what data is stored by the each subtree.  Interior
123335 ** nodes are written using InteriorWriter, and read using
123336 ** InteriorReader.  InteriorWriters are created as needed when
123337 ** SegmentWriter creates new leaf nodes, or when an interior node
123338 ** itself grows too big and must be split.  The format of interior
123339 ** nodes:
123340 **
123341 ** varint iHeight;           (height from leaf level, always >0)
123342 ** varint iBlockid;          (block id of node's leftmost subtree)
123343 ** optional {
123344 **   varint nTerm;           (length of first term)
123345 **   char pTerm[nTerm];      (content of first term)
123346 **   array {
123347 **                                (further terms are delta-encoded)
123348 **     varint nPrefix;            (length of shared prefix with previous term)
123349 **     varint nSuffix;            (length of unshared suffix)
123350 **     char pTermSuffix[nSuffix]; (unshared suffix of next term)
123351 **   }
123352 ** }
123353 **
123354 ** Here, optional { X } means an optional element, while array { X }
123355 ** means zero or more occurrences of X, adjacent in memory.
123356 **
123357 ** An interior node encodes n terms separating n+1 subtrees.  The
123358 ** subtree blocks are contiguous, so only the first subtree's blockid
123359 ** is encoded.  The subtree at iBlockid will contain all terms less
123360 ** than the first term encoded (or all terms if no term is encoded).
123361 ** Otherwise, for terms greater than or equal to pTerm[i] but less
123362 ** than pTerm[i+1], the subtree for that term will be rooted at
123363 ** iBlockid+i.  Interior nodes only store enough term data to
123364 ** distinguish adjacent children (if the rightmost term of the left
123365 ** child is "something", and the leftmost term of the right child is
123366 ** "wicked", only "w" is stored).
123367 **
123368 ** New data is spilled to a new interior node at the same height when
123369 ** the current node exceeds INTERIOR_MAX bytes (default 2048).
123370 ** INTERIOR_MIN_TERMS (default 7) keeps large terms from monopolizing
123371 ** interior nodes and making the tree too skinny.  The interior nodes
123372 ** at a given height are naturally tracked by interior nodes at
123373 ** height+1, and so on.
123374 **
123375 **
123376 **** Segment directory ****
123377 ** The segment directory in table %_segdir stores meta-information for
123378 ** merging and deleting segments, and also the root node of the
123379 ** segment's tree.
123380 **
123381 ** The root node is the top node of the segment's tree after encoding
123382 ** the entire segment, restricted to ROOT_MAX bytes (default 1024).
123383 ** This could be either a leaf node or an interior node.  If the top
123384 ** node requires more than ROOT_MAX bytes, it is flushed to %_segments
123385 ** and a new root interior node is generated (which should always fit
123386 ** within ROOT_MAX because it only needs space for 2 varints, the
123387 ** height and the blockid of the previous root).
123388 **
123389 ** The meta-information in the segment directory is:
123390 **   level               - segment level (see below)
123391 **   idx                 - index within level
123392 **                       - (level,idx uniquely identify a segment)
123393 **   start_block         - first leaf node
123394 **   leaves_end_block    - last leaf node
123395 **   end_block           - last block (including interior nodes)
123396 **   root                - contents of root node
123397 **
123398 ** If the root node is a leaf node, then start_block,
123399 ** leaves_end_block, and end_block are all 0.
123400 **
123401 **
123402 **** Segment merging ****
123403 ** To amortize update costs, segments are grouped into levels and
123404 ** merged in batches.  Each increase in level represents exponentially
123405 ** more documents.
123406 **
123407 ** New documents (actually, document updates) are tokenized and
123408 ** written individually (using LeafWriter) to a level 0 segment, with
123409 ** incrementing idx.  When idx reaches MERGE_COUNT (default 16), all
123410 ** level 0 segments are merged into a single level 1 segment.  Level 1
123411 ** is populated like level 0, and eventually MERGE_COUNT level 1
123412 ** segments are merged to a single level 2 segment (representing
123413 ** MERGE_COUNT^2 updates), and so on.
123414 **
123415 ** A segment merge traverses all segments at a given level in
123416 ** parallel, performing a straightforward sorted merge.  Since segment
123417 ** leaf nodes are written in to the %_segments table in order, this
123418 ** merge traverses the underlying sqlite disk structures efficiently.
123419 ** After the merge, all segment blocks from the merged level are
123420 ** deleted.
123421 **
123422 ** MERGE_COUNT controls how often we merge segments.  16 seems to be
123423 ** somewhat of a sweet spot for insertion performance.  32 and 64 show
123424 ** very similar performance numbers to 16 on insertion, though they're
123425 ** a tiny bit slower (perhaps due to more overhead in merge-time
123426 ** sorting).  8 is about 20% slower than 16, 4 about 50% slower than
123427 ** 16, 2 about 66% slower than 16.
123428 **
123429 ** At query time, high MERGE_COUNT increases the number of segments
123430 ** which need to be scanned and merged.  For instance, with 100k docs
123431 ** inserted:
123432 **
123433 **    MERGE_COUNT   segments
123434 **       16           25
123435 **        8           12
123436 **        4           10
123437 **        2            6
123438 **
123439 ** This appears to have only a moderate impact on queries for very
123440 ** frequent terms (which are somewhat dominated by segment merge
123441 ** costs), and infrequent and non-existent terms still seem to be fast
123442 ** even with many segments.
123443 **
123444 ** TODO(shess) That said, it would be nice to have a better query-side
123445 ** argument for MERGE_COUNT of 16.  Also, it is possible/likely that
123446 ** optimizations to things like doclist merging will swing the sweet
123447 ** spot around.
123448 **
123449 **
123450 **
123451 **** Handling of deletions and updates ****
123452 ** Since we're using a segmented structure, with no docid-oriented
123453 ** index into the term index, we clearly cannot simply update the term
123454 ** index when a document is deleted or updated.  For deletions, we
123455 ** write an empty doclist (varint(docid) varint(POS_END)), for updates
123456 ** we simply write the new doclist.  Segment merges overwrite older
123457 ** data for a particular docid with newer data, so deletes or updates
123458 ** will eventually overtake the earlier data and knock it out.  The
123459 ** query logic likewise merges doclists so that newer data knocks out
123460 ** older data.
123461 */
123462 
123463 /************** Include fts3Int.h in the middle of fts3.c ********************/
123464 /************** Begin file fts3Int.h *****************************************/
123465 /*
123466 ** 2009 Nov 12
123467 **
123468 ** The author disclaims copyright to this source code.  In place of
123469 ** a legal notice, here is a blessing:
123470 **
123471 **    May you do good and not evil.
123472 **    May you find forgiveness for yourself and forgive others.
123473 **    May you share freely, never taking more than you give.
123474 **
123475 ******************************************************************************
123476 **
123477 */
123478 #ifndef _FTSINT_H
123479 #define _FTSINT_H
123480 
123481 #if !defined(NDEBUG) && !defined(SQLITE_DEBUG)
123482 # define NDEBUG 1
123483 #endif
123484 
123485 /*
123486 ** FTS4 is really an extension for FTS3.  It is enabled using the
123487 ** SQLITE_ENABLE_FTS3 macro.  But to avoid confusion we also all
123488 ** the SQLITE_ENABLE_FTS4 macro to serve as an alisse for SQLITE_ENABLE_FTS3.
123489 */
123490 #if defined(SQLITE_ENABLE_FTS4) && !defined(SQLITE_ENABLE_FTS3)
123491 # define SQLITE_ENABLE_FTS3
123492 #endif
123493 
123494 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
123495 
123496 /* If not building as part of the core, include sqlite3ext.h. */
123497 #ifndef SQLITE_CORE
123498 SQLITE_EXTENSION_INIT3
123499 #endif
123500 
123501 /************** Include fts3_tokenizer.h in the middle of fts3Int.h **********/
123502 /************** Begin file fts3_tokenizer.h **********************************/
123503 /*
123504 ** 2006 July 10
123505 **
123506 ** The author disclaims copyright to this source code.
123507 **
123508 *************************************************************************
123509 ** Defines the interface to tokenizers used by fulltext-search.  There
123510 ** are three basic components:
123511 **
123512 ** sqlite3_tokenizer_module is a singleton defining the tokenizer
123513 ** interface functions.  This is essentially the class structure for
123514 ** tokenizers.
123515 **
123516 ** sqlite3_tokenizer is used to define a particular tokenizer, perhaps
123517 ** including customization information defined at creation time.
123518 **
123519 ** sqlite3_tokenizer_cursor is generated by a tokenizer to generate
123520 ** tokens from a particular input.
123521 */
123522 #ifndef _FTS3_TOKENIZER_H_
123523 #define _FTS3_TOKENIZER_H_
123524 
123525 /* TODO(shess) Only used for SQLITE_OK and SQLITE_DONE at this time.
123526 ** If tokenizers are to be allowed to call sqlite3_*() functions, then
123527 ** we will need a way to register the API consistently.
123528 */
123529 
123530 /*
123531 ** Structures used by the tokenizer interface. When a new tokenizer
123532 ** implementation is registered, the caller provides a pointer to
123533 ** an sqlite3_tokenizer_module containing pointers to the callback
123534 ** functions that make up an implementation.
123535 **
123536 ** When an fts3 table is created, it passes any arguments passed to
123537 ** the tokenizer clause of the CREATE VIRTUAL TABLE statement to the
123538 ** sqlite3_tokenizer_module.xCreate() function of the requested tokenizer
123539 ** implementation. The xCreate() function in turn returns an
123540 ** sqlite3_tokenizer structure representing the specific tokenizer to
123541 ** be used for the fts3 table (customized by the tokenizer clause arguments).
123542 **
123543 ** To tokenize an input buffer, the sqlite3_tokenizer_module.xOpen()
123544 ** method is called. It returns an sqlite3_tokenizer_cursor object
123545 ** that may be used to tokenize a specific input buffer based on
123546 ** the tokenization rules supplied by a specific sqlite3_tokenizer
123547 ** object.
123548 */
123549 typedef struct sqlite3_tokenizer_module sqlite3_tokenizer_module;
123550 typedef struct sqlite3_tokenizer sqlite3_tokenizer;
123551 typedef struct sqlite3_tokenizer_cursor sqlite3_tokenizer_cursor;
123552 
123553 struct sqlite3_tokenizer_module {
123554 
123555   /*
123556   ** Structure version. Should always be set to 0 or 1.
123557   */
123558   int iVersion;
123559 
123560   /*
123561   ** Create a new tokenizer. The values in the argv[] array are the
123562   ** arguments passed to the "tokenizer" clause of the CREATE VIRTUAL
123563   ** TABLE statement that created the fts3 table. For example, if
123564   ** the following SQL is executed:
123565   **
123566   **   CREATE .. USING fts3( ... , tokenizer <tokenizer-name> arg1 arg2)
123567   **
123568   ** then argc is set to 2, and the argv[] array contains pointers
123569   ** to the strings "arg1" and "arg2".
123570   **
123571   ** This method should return either SQLITE_OK (0), or an SQLite error
123572   ** code. If SQLITE_OK is returned, then *ppTokenizer should be set
123573   ** to point at the newly created tokenizer structure. The generic
123574   ** sqlite3_tokenizer.pModule variable should not be initialized by
123575   ** this callback. The caller will do so.
123576   */
123577   int (*xCreate)(
123578     int argc,                           /* Size of argv array */
123579     const char *const*argv,             /* Tokenizer argument strings */
123580     sqlite3_tokenizer **ppTokenizer     /* OUT: Created tokenizer */
123581   );
123582 
123583   /*
123584   ** Destroy an existing tokenizer. The fts3 module calls this method
123585   ** exactly once for each successful call to xCreate().
123586   */
123587   int (*xDestroy)(sqlite3_tokenizer *pTokenizer);
123588 
123589   /*
123590   ** Create a tokenizer cursor to tokenize an input buffer. The caller
123591   ** is responsible for ensuring that the input buffer remains valid
123592   ** until the cursor is closed (using the xClose() method).
123593   */
123594   int (*xOpen)(
123595     sqlite3_tokenizer *pTokenizer,       /* Tokenizer object */
123596     const char *pInput, int nBytes,      /* Input buffer */
123597     sqlite3_tokenizer_cursor **ppCursor  /* OUT: Created tokenizer cursor */
123598   );
123599 
123600   /*
123601   ** Destroy an existing tokenizer cursor. The fts3 module calls this
123602   ** method exactly once for each successful call to xOpen().
123603   */
123604   int (*xClose)(sqlite3_tokenizer_cursor *pCursor);
123605 
123606   /*
123607   ** Retrieve the next token from the tokenizer cursor pCursor. This
123608   ** method should either return SQLITE_OK and set the values of the
123609   ** "OUT" variables identified below, or SQLITE_DONE to indicate that
123610   ** the end of the buffer has been reached, or an SQLite error code.
123611   **
123612   ** *ppToken should be set to point at a buffer containing the
123613   ** normalized version of the token (i.e. after any case-folding and/or
123614   ** stemming has been performed). *pnBytes should be set to the length
123615   ** of this buffer in bytes. The input text that generated the token is
123616   ** identified by the byte offsets returned in *piStartOffset and
123617   ** *piEndOffset. *piStartOffset should be set to the index of the first
123618   ** byte of the token in the input buffer. *piEndOffset should be set
123619   ** to the index of the first byte just past the end of the token in
123620   ** the input buffer.
123621   **
123622   ** The buffer *ppToken is set to point at is managed by the tokenizer
123623   ** implementation. It is only required to be valid until the next call
123624   ** to xNext() or xClose().
123625   */
123626   /* TODO(shess) current implementation requires pInput to be
123627   ** nul-terminated.  This should either be fixed, or pInput/nBytes
123628   ** should be converted to zInput.
123629   */
123630   int (*xNext)(
123631     sqlite3_tokenizer_cursor *pCursor,   /* Tokenizer cursor */
123632     const char **ppToken, int *pnBytes,  /* OUT: Normalized text for token */
123633     int *piStartOffset,  /* OUT: Byte offset of token in input buffer */
123634     int *piEndOffset,    /* OUT: Byte offset of end of token in input buffer */
123635     int *piPosition      /* OUT: Number of tokens returned before this one */
123636   );
123637 
123638   /***********************************************************************
123639   ** Methods below this point are only available if iVersion>=1.
123640   */
123641 
123642   /*
123643   ** Configure the language id of a tokenizer cursor.
123644   */
123645   int (*xLanguageid)(sqlite3_tokenizer_cursor *pCsr, int iLangid);
123646 };
123647 
123648 struct sqlite3_tokenizer {
123649   const sqlite3_tokenizer_module *pModule;  /* The module for this tokenizer */
123650   /* Tokenizer implementations will typically add additional fields */
123651 };
123652 
123653 struct sqlite3_tokenizer_cursor {
123654   sqlite3_tokenizer *pTokenizer;       /* Tokenizer for this cursor. */
123655   /* Tokenizer implementations will typically add additional fields */
123656 };
123657 
123658 int fts3_global_term_cnt(int iTerm, int iCol);
123659 int fts3_term_cnt(int iTerm, int iCol);
123660 
123661 
123662 #endif /* _FTS3_TOKENIZER_H_ */
123663 
123664 /************** End of fts3_tokenizer.h **************************************/
123665 /************** Continuing where we left off in fts3Int.h ********************/
123666 /************** Include fts3_hash.h in the middle of fts3Int.h ***************/
123667 /************** Begin file fts3_hash.h ***************************************/
123668 /*
123669 ** 2001 September 22
123670 **
123671 ** The author disclaims copyright to this source code.  In place of
123672 ** a legal notice, here is a blessing:
123673 **
123674 **    May you do good and not evil.
123675 **    May you find forgiveness for yourself and forgive others.
123676 **    May you share freely, never taking more than you give.
123677 **
123678 *************************************************************************
123679 ** This is the header file for the generic hash-table implementation
123680 ** used in SQLite.  We've modified it slightly to serve as a standalone
123681 ** hash table implementation for the full-text indexing module.
123682 **
123683 */
123684 #ifndef _FTS3_HASH_H_
123685 #define _FTS3_HASH_H_
123686 
123687 /* Forward declarations of structures. */
123688 typedef struct Fts3Hash Fts3Hash;
123689 typedef struct Fts3HashElem Fts3HashElem;
123690 
123691 /* A complete hash table is an instance of the following structure.
123692 ** The internals of this structure are intended to be opaque -- client
123693 ** code should not attempt to access or modify the fields of this structure
123694 ** directly.  Change this structure only by using the routines below.
123695 ** However, many of the "procedures" and "functions" for modifying and
123696 ** accessing this structure are really macros, so we can't really make
123697 ** this structure opaque.
123698 */
123699 struct Fts3Hash {
123700   char keyClass;          /* HASH_INT, _POINTER, _STRING, _BINARY */
123701   char copyKey;           /* True if copy of key made on insert */
123702   int count;              /* Number of entries in this table */
123703   Fts3HashElem *first;    /* The first element of the array */
123704   int htsize;             /* Number of buckets in the hash table */
123705   struct _fts3ht {        /* the hash table */
123706     int count;               /* Number of entries with this hash */
123707     Fts3HashElem *chain;     /* Pointer to first entry with this hash */
123708   } *ht;
123709 };
123710 
123711 /* Each element in the hash table is an instance of the following
123712 ** structure.  All elements are stored on a single doubly-linked list.
123713 **
123714 ** Again, this structure is intended to be opaque, but it can't really
123715 ** be opaque because it is used by macros.
123716 */
123717 struct Fts3HashElem {
123718   Fts3HashElem *next, *prev; /* Next and previous elements in the table */
123719   void *data;                /* Data associated with this element */
123720   void *pKey; int nKey;      /* Key associated with this element */
123721 };
123722 
123723 /*
123724 ** There are 2 different modes of operation for a hash table:
123725 **
123726 **   FTS3_HASH_STRING        pKey points to a string that is nKey bytes long
123727 **                           (including the null-terminator, if any).  Case
123728 **                           is respected in comparisons.
123729 **
123730 **   FTS3_HASH_BINARY        pKey points to binary data nKey bytes long.
123731 **                           memcmp() is used to compare keys.
123732 **
123733 ** A copy of the key is made if the copyKey parameter to fts3HashInit is 1.
123734 */
123735 #define FTS3_HASH_STRING    1
123736 #define FTS3_HASH_BINARY    2
123737 
123738 /*
123739 ** Access routines.  To delete, insert a NULL pointer.
123740 */
123741 SQLITE_PRIVATE void sqlite3Fts3HashInit(Fts3Hash *pNew, char keyClass, char copyKey);
123742 SQLITE_PRIVATE void *sqlite3Fts3HashInsert(Fts3Hash*, const void *pKey, int nKey, void *pData);
123743 SQLITE_PRIVATE void *sqlite3Fts3HashFind(const Fts3Hash*, const void *pKey, int nKey);
123744 SQLITE_PRIVATE void sqlite3Fts3HashClear(Fts3Hash*);
123745 SQLITE_PRIVATE Fts3HashElem *sqlite3Fts3HashFindElem(const Fts3Hash *, const void *, int);
123746 
123747 /*
123748 ** Shorthand for the functions above
123749 */
123750 #define fts3HashInit     sqlite3Fts3HashInit
123751 #define fts3HashInsert   sqlite3Fts3HashInsert
123752 #define fts3HashFind     sqlite3Fts3HashFind
123753 #define fts3HashClear    sqlite3Fts3HashClear
123754 #define fts3HashFindElem sqlite3Fts3HashFindElem
123755 
123756 /*
123757 ** Macros for looping over all elements of a hash table.  The idiom is
123758 ** like this:
123759 **
123760 **   Fts3Hash h;
123761 **   Fts3HashElem *p;
123762 **   ...
123763 **   for(p=fts3HashFirst(&h); p; p=fts3HashNext(p)){
123764 **     SomeStructure *pData = fts3HashData(p);
123765 **     // do something with pData
123766 **   }
123767 */
123768 #define fts3HashFirst(H)  ((H)->first)
123769 #define fts3HashNext(E)   ((E)->next)
123770 #define fts3HashData(E)   ((E)->data)
123771 #define fts3HashKey(E)    ((E)->pKey)
123772 #define fts3HashKeysize(E) ((E)->nKey)
123773 
123774 /*
123775 ** Number of entries in a hash table
123776 */
123777 #define fts3HashCount(H)  ((H)->count)
123778 
123779 #endif /* _FTS3_HASH_H_ */
123780 
123781 /************** End of fts3_hash.h *******************************************/
123782 /************** Continuing where we left off in fts3Int.h ********************/
123783 
123784 /*
123785 ** This constant determines the maximum depth of an FTS expression tree
123786 ** that the library will create and use. FTS uses recursion to perform
123787 ** various operations on the query tree, so the disadvantage of a large
123788 ** limit is that it may allow very large queries to use large amounts
123789 ** of stack space (perhaps causing a stack overflow).
123790 */
123791 #ifndef SQLITE_FTS3_MAX_EXPR_DEPTH
123792 # define SQLITE_FTS3_MAX_EXPR_DEPTH 12
123793 #endif
123794 
123795 
123796 /*
123797 ** This constant controls how often segments are merged. Once there are
123798 ** FTS3_MERGE_COUNT segments of level N, they are merged into a single
123799 ** segment of level N+1.
123800 */
123801 #define FTS3_MERGE_COUNT 16
123802 
123803 /*
123804 ** This is the maximum amount of data (in bytes) to store in the
123805 ** Fts3Table.pendingTerms hash table. Normally, the hash table is
123806 ** populated as documents are inserted/updated/deleted in a transaction
123807 ** and used to create a new segment when the transaction is committed.
123808 ** However if this limit is reached midway through a transaction, a new
123809 ** segment is created and the hash table cleared immediately.
123810 */
123811 #define FTS3_MAX_PENDING_DATA (1*1024*1024)
123812 
123813 /*
123814 ** Macro to return the number of elements in an array. SQLite has a
123815 ** similar macro called ArraySize(). Use a different name to avoid
123816 ** a collision when building an amalgamation with built-in FTS3.
123817 */
123818 #define SizeofArray(X) ((int)(sizeof(X)/sizeof(X[0])))
123819 
123820 
123821 #ifndef MIN
123822 # define MIN(x,y) ((x)<(y)?(x):(y))
123823 #endif
123824 #ifndef MAX
123825 # define MAX(x,y) ((x)>(y)?(x):(y))
123826 #endif
123827 
123828 /*
123829 ** Maximum length of a varint encoded integer. The varint format is different
123830 ** from that used by SQLite, so the maximum length is 10, not 9.
123831 */
123832 #define FTS3_VARINT_MAX 10
123833 
123834 /*
123835 ** FTS4 virtual tables may maintain multiple indexes - one index of all terms
123836 ** in the document set and zero or more prefix indexes. All indexes are stored
123837 ** as one or more b+-trees in the %_segments and %_segdir tables.
123838 **
123839 ** It is possible to determine which index a b+-tree belongs to based on the
123840 ** value stored in the "%_segdir.level" column. Given this value L, the index
123841 ** that the b+-tree belongs to is (L<<10). In other words, all b+-trees with
123842 ** level values between 0 and 1023 (inclusive) belong to index 0, all levels
123843 ** between 1024 and 2047 to index 1, and so on.
123844 **
123845 ** It is considered impossible for an index to use more than 1024 levels. In
123846 ** theory though this may happen, but only after at least
123847 ** (FTS3_MERGE_COUNT^1024) separate flushes of the pending-terms tables.
123848 */
123849 #define FTS3_SEGDIR_MAXLEVEL      1024
123850 #define FTS3_SEGDIR_MAXLEVEL_STR "1024"
123851 
123852 /*
123853 ** The testcase() macro is only used by the amalgamation.  If undefined,
123854 ** make it a no-op.
123855 */
123856 #ifndef testcase
123857 # define testcase(X)
123858 #endif
123859 
123860 /*
123861 ** Terminator values for position-lists and column-lists.
123862 */
123863 #define POS_COLUMN  (1)     /* Column-list terminator */
123864 #define POS_END     (0)     /* Position-list terminator */
123865 
123866 /*
123867 ** This section provides definitions to allow the
123868 ** FTS3 extension to be compiled outside of the
123869 ** amalgamation.
123870 */
123871 #ifndef SQLITE_AMALGAMATION
123872 /*
123873 ** Macros indicating that conditional expressions are always true or
123874 ** false.
123875 */
123876 #ifdef SQLITE_COVERAGE_TEST
123877 # define ALWAYS(x) (1)
123878 # define NEVER(X)  (0)
123879 #else
123880 # define ALWAYS(x) (x)
123881 # define NEVER(x)  (x)
123882 #endif
123883 
123884 /*
123885 ** Internal types used by SQLite.
123886 */
123887 typedef unsigned char u8;         /* 1-byte (or larger) unsigned integer */
123888 typedef short int i16;            /* 2-byte (or larger) signed integer */
123889 typedef unsigned int u32;         /* 4-byte unsigned integer */
123890 typedef sqlite3_uint64 u64;       /* 8-byte unsigned integer */
123891 typedef sqlite3_int64 i64;        /* 8-byte signed integer */
123892 
123893 /*
123894 ** Macro used to suppress compiler warnings for unused parameters.
123895 */
123896 #define UNUSED_PARAMETER(x) (void)(x)
123897 
123898 /*
123899 ** Activate assert() only if SQLITE_TEST is enabled.
123900 */
123901 #if !defined(NDEBUG) && !defined(SQLITE_DEBUG)
123902 # define NDEBUG 1
123903 #endif
123904 
123905 /*
123906 ** The TESTONLY macro is used to enclose variable declarations or
123907 ** other bits of code that are needed to support the arguments
123908 ** within testcase() and assert() macros.
123909 */
123910 #if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST)
123911 # define TESTONLY(X)  X
123912 #else
123913 # define TESTONLY(X)
123914 #endif
123915 
123916 #endif /* SQLITE_AMALGAMATION */
123917 
123918 #ifdef SQLITE_DEBUG
123919 SQLITE_PRIVATE int sqlite3Fts3Corrupt(void);
123920 # define FTS_CORRUPT_VTAB sqlite3Fts3Corrupt()
123921 #else
123922 # define FTS_CORRUPT_VTAB SQLITE_CORRUPT_VTAB
123923 #endif
123924 
123925 typedef struct Fts3Table Fts3Table;
123926 typedef struct Fts3Cursor Fts3Cursor;
123927 typedef struct Fts3Expr Fts3Expr;
123928 typedef struct Fts3Phrase Fts3Phrase;
123929 typedef struct Fts3PhraseToken Fts3PhraseToken;
123930 
123931 typedef struct Fts3Doclist Fts3Doclist;
123932 typedef struct Fts3SegFilter Fts3SegFilter;
123933 typedef struct Fts3DeferredToken Fts3DeferredToken;
123934 typedef struct Fts3SegReader Fts3SegReader;
123935 typedef struct Fts3MultiSegReader Fts3MultiSegReader;
123936 
123937 /*
123938 ** A connection to a fulltext index is an instance of the following
123939 ** structure. The xCreate and xConnect methods create an instance
123940 ** of this structure and xDestroy and xDisconnect free that instance.
123941 ** All other methods receive a pointer to the structure as one of their
123942 ** arguments.
123943 */
123944 struct Fts3Table {
123945   sqlite3_vtab base;              /* Base class used by SQLite core */
123946   sqlite3 *db;                    /* The database connection */
123947   const char *zDb;                /* logical database name */
123948   const char *zName;              /* virtual table name */
123949   int nColumn;                    /* number of named columns in virtual table */
123950   char **azColumn;                /* column names.  malloced */
123951   u8 *abNotindexed;               /* True for 'notindexed' columns */
123952   sqlite3_tokenizer *pTokenizer;  /* tokenizer for inserts and queries */
123953   char *zContentTbl;              /* content=xxx option, or NULL */
123954   char *zLanguageid;              /* languageid=xxx option, or NULL */
123955   u8 bAutoincrmerge;              /* True if automerge=1 */
123956   u32 nLeafAdd;                   /* Number of leaf blocks added this trans */
123957 
123958   /* Precompiled statements used by the implementation. Each of these
123959   ** statements is run and reset within a single virtual table API call.
123960   */
123961   sqlite3_stmt *aStmt[37];
123962 
123963   char *zReadExprlist;
123964   char *zWriteExprlist;
123965 
123966   int nNodeSize;                  /* Soft limit for node size */
123967   u8 bFts4;                       /* True for FTS4, false for FTS3 */
123968   u8 bHasStat;                    /* True if %_stat table exists */
123969   u8 bHasDocsize;                 /* True if %_docsize table exists */
123970   u8 bDescIdx;                    /* True if doclists are in reverse order */
123971   u8 bIgnoreSavepoint;            /* True to ignore xSavepoint invocations */
123972   int nPgsz;                      /* Page size for host database */
123973   char *zSegmentsTbl;             /* Name of %_segments table */
123974   sqlite3_blob *pSegments;        /* Blob handle open on %_segments table */
123975 
123976   /*
123977   ** The following array of hash tables is used to buffer pending index
123978   ** updates during transactions. All pending updates buffered at any one
123979   ** time must share a common language-id (see the FTS4 langid= feature).
123980   ** The current language id is stored in variable iPrevLangid.
123981   **
123982   ** A single FTS4 table may have multiple full-text indexes. For each index
123983   ** there is an entry in the aIndex[] array. Index 0 is an index of all the
123984   ** terms that appear in the document set. Each subsequent index in aIndex[]
123985   ** is an index of prefixes of a specific length.
123986   **
123987   ** Variable nPendingData contains an estimate the memory consumed by the
123988   ** pending data structures, including hash table overhead, but not including
123989   ** malloc overhead.  When nPendingData exceeds nMaxPendingData, all hash
123990   ** tables are flushed to disk. Variable iPrevDocid is the docid of the most
123991   ** recently inserted record.
123992   */
123993   int nIndex;                     /* Size of aIndex[] */
123994   struct Fts3Index {
123995     int nPrefix;                  /* Prefix length (0 for main terms index) */
123996     Fts3Hash hPending;            /* Pending terms table for this index */
123997   } *aIndex;
123998   int nMaxPendingData;            /* Max pending data before flush to disk */
123999   int nPendingData;               /* Current bytes of pending data */
124000   sqlite_int64 iPrevDocid;        /* Docid of most recently inserted document */
124001   int iPrevLangid;                /* Langid of recently inserted document */
124002 
124003 #if defined(SQLITE_DEBUG) || defined(SQLITE_COVERAGE_TEST)
124004   /* State variables used for validating that the transaction control
124005   ** methods of the virtual table are called at appropriate times.  These
124006   ** values do not contribute to FTS functionality; they are used for
124007   ** verifying the operation of the SQLite core.
124008   */
124009   int inTransaction;     /* True after xBegin but before xCommit/xRollback */
124010   int mxSavepoint;       /* Largest valid xSavepoint integer */
124011 #endif
124012 
124013 #ifdef SQLITE_TEST
124014   /* True to disable the incremental doclist optimization. This is controled
124015   ** by special insert command 'test-no-incr-doclist'.  */
124016   int bNoIncrDoclist;
124017 #endif
124018 };
124019 
124020 /*
124021 ** When the core wants to read from the virtual table, it creates a
124022 ** virtual table cursor (an instance of the following structure) using
124023 ** the xOpen method. Cursors are destroyed using the xClose method.
124024 */
124025 struct Fts3Cursor {
124026   sqlite3_vtab_cursor base;       /* Base class used by SQLite core */
124027   i16 eSearch;                    /* Search strategy (see below) */
124028   u8 isEof;                       /* True if at End Of Results */
124029   u8 isRequireSeek;               /* True if must seek pStmt to %_content row */
124030   sqlite3_stmt *pStmt;            /* Prepared statement in use by the cursor */
124031   Fts3Expr *pExpr;                /* Parsed MATCH query string */
124032   int iLangid;                    /* Language being queried for */
124033   int nPhrase;                    /* Number of matchable phrases in query */
124034   Fts3DeferredToken *pDeferred;   /* Deferred search tokens, if any */
124035   sqlite3_int64 iPrevId;          /* Previous id read from aDoclist */
124036   char *pNextId;                  /* Pointer into the body of aDoclist */
124037   char *aDoclist;                 /* List of docids for full-text queries */
124038   int nDoclist;                   /* Size of buffer at aDoclist */
124039   u8 bDesc;                       /* True to sort in descending order */
124040   int eEvalmode;                  /* An FTS3_EVAL_XX constant */
124041   int nRowAvg;                    /* Average size of database rows, in pages */
124042   sqlite3_int64 nDoc;             /* Documents in table */
124043   i64 iMinDocid;                  /* Minimum docid to return */
124044   i64 iMaxDocid;                  /* Maximum docid to return */
124045   int isMatchinfoNeeded;          /* True when aMatchinfo[] needs filling in */
124046   u32 *aMatchinfo;                /* Information about most recent match */
124047   int nMatchinfo;                 /* Number of elements in aMatchinfo[] */
124048   char *zMatchinfo;               /* Matchinfo specification */
124049 };
124050 
124051 #define FTS3_EVAL_FILTER    0
124052 #define FTS3_EVAL_NEXT      1
124053 #define FTS3_EVAL_MATCHINFO 2
124054 
124055 /*
124056 ** The Fts3Cursor.eSearch member is always set to one of the following.
124057 ** Actualy, Fts3Cursor.eSearch can be greater than or equal to
124058 ** FTS3_FULLTEXT_SEARCH.  If so, then Fts3Cursor.eSearch - 2 is the index
124059 ** of the column to be searched.  For example, in
124060 **
124061 **     CREATE VIRTUAL TABLE ex1 USING fts3(a,b,c,d);
124062 **     SELECT docid FROM ex1 WHERE b MATCH 'one two three';
124063 **
124064 ** Because the LHS of the MATCH operator is 2nd column "b",
124065 ** Fts3Cursor.eSearch will be set to FTS3_FULLTEXT_SEARCH+1.  (+0 for a,
124066 ** +1 for b, +2 for c, +3 for d.)  If the LHS of MATCH were "ex1"
124067 ** indicating that all columns should be searched,
124068 ** then eSearch would be set to FTS3_FULLTEXT_SEARCH+4.
124069 */
124070 #define FTS3_FULLSCAN_SEARCH 0    /* Linear scan of %_content table */
124071 #define FTS3_DOCID_SEARCH    1    /* Lookup by rowid on %_content table */
124072 #define FTS3_FULLTEXT_SEARCH 2    /* Full-text index search */
124073 
124074 /*
124075 ** The lower 16-bits of the sqlite3_index_info.idxNum value set by
124076 ** the xBestIndex() method contains the Fts3Cursor.eSearch value described
124077 ** above. The upper 16-bits contain a combination of the following
124078 ** bits, used to describe extra constraints on full-text searches.
124079 */
124080 #define FTS3_HAVE_LANGID    0x00010000      /* languageid=? */
124081 #define FTS3_HAVE_DOCID_GE  0x00020000      /* docid>=? */
124082 #define FTS3_HAVE_DOCID_LE  0x00040000      /* docid<=? */
124083 
124084 struct Fts3Doclist {
124085   char *aAll;                    /* Array containing doclist (or NULL) */
124086   int nAll;                      /* Size of a[] in bytes */
124087   char *pNextDocid;              /* Pointer to next docid */
124088 
124089   sqlite3_int64 iDocid;          /* Current docid (if pList!=0) */
124090   int bFreeList;                 /* True if pList should be sqlite3_free()d */
124091   char *pList;                   /* Pointer to position list following iDocid */
124092   int nList;                     /* Length of position list */
124093 };
124094 
124095 /*
124096 ** A "phrase" is a sequence of one or more tokens that must match in
124097 ** sequence.  A single token is the base case and the most common case.
124098 ** For a sequence of tokens contained in double-quotes (i.e. "one two three")
124099 ** nToken will be the number of tokens in the string.
124100 */
124101 struct Fts3PhraseToken {
124102   char *z;                        /* Text of the token */
124103   int n;                          /* Number of bytes in buffer z */
124104   int isPrefix;                   /* True if token ends with a "*" character */
124105   int bFirst;                     /* True if token must appear at position 0 */
124106 
124107   /* Variables above this point are populated when the expression is
124108   ** parsed (by code in fts3_expr.c). Below this point the variables are
124109   ** used when evaluating the expression. */
124110   Fts3DeferredToken *pDeferred;   /* Deferred token object for this token */
124111   Fts3MultiSegReader *pSegcsr;    /* Segment-reader for this token */
124112 };
124113 
124114 struct Fts3Phrase {
124115   /* Cache of doclist for this phrase. */
124116   Fts3Doclist doclist;
124117   int bIncr;                 /* True if doclist is loaded incrementally */
124118   int iDoclistToken;
124119 
124120   /* Variables below this point are populated by fts3_expr.c when parsing
124121   ** a MATCH expression. Everything above is part of the evaluation phase.
124122   */
124123   int nToken;                /* Number of tokens in the phrase */
124124   int iColumn;               /* Index of column this phrase must match */
124125   Fts3PhraseToken aToken[1]; /* One entry for each token in the phrase */
124126 };
124127 
124128 /*
124129 ** A tree of these objects forms the RHS of a MATCH operator.
124130 **
124131 ** If Fts3Expr.eType is FTSQUERY_PHRASE and isLoaded is true, then aDoclist
124132 ** points to a malloced buffer, size nDoclist bytes, containing the results
124133 ** of this phrase query in FTS3 doclist format. As usual, the initial
124134 ** "Length" field found in doclists stored on disk is omitted from this
124135 ** buffer.
124136 **
124137 ** Variable aMI is used only for FTSQUERY_NEAR nodes to store the global
124138 ** matchinfo data. If it is not NULL, it points to an array of size nCol*3,
124139 ** where nCol is the number of columns in the queried FTS table. The array
124140 ** is populated as follows:
124141 **
124142 **   aMI[iCol*3 + 0] = Undefined
124143 **   aMI[iCol*3 + 1] = Number of occurrences
124144 **   aMI[iCol*3 + 2] = Number of rows containing at least one instance
124145 **
124146 ** The aMI array is allocated using sqlite3_malloc(). It should be freed
124147 ** when the expression node is.
124148 */
124149 struct Fts3Expr {
124150   int eType;                 /* One of the FTSQUERY_XXX values defined below */
124151   int nNear;                 /* Valid if eType==FTSQUERY_NEAR */
124152   Fts3Expr *pParent;         /* pParent->pLeft==this or pParent->pRight==this */
124153   Fts3Expr *pLeft;           /* Left operand */
124154   Fts3Expr *pRight;          /* Right operand */
124155   Fts3Phrase *pPhrase;       /* Valid if eType==FTSQUERY_PHRASE */
124156 
124157   /* The following are used by the fts3_eval.c module. */
124158   sqlite3_int64 iDocid;      /* Current docid */
124159   u8 bEof;                   /* True this expression is at EOF already */
124160   u8 bStart;                 /* True if iDocid is valid */
124161   u8 bDeferred;              /* True if this expression is entirely deferred */
124162 
124163   u32 *aMI;
124164 };
124165 
124166 /*
124167 ** Candidate values for Fts3Query.eType. Note that the order of the first
124168 ** four values is in order of precedence when parsing expressions. For
124169 ** example, the following:
124170 **
124171 **   "a OR b AND c NOT d NEAR e"
124172 **
124173 ** is equivalent to:
124174 **
124175 **   "a OR (b AND (c NOT (d NEAR e)))"
124176 */
124177 #define FTSQUERY_NEAR   1
124178 #define FTSQUERY_NOT    2
124179 #define FTSQUERY_AND    3
124180 #define FTSQUERY_OR     4
124181 #define FTSQUERY_PHRASE 5
124182 
124183 
124184 /* fts3_write.c */
124185 SQLITE_PRIVATE int sqlite3Fts3UpdateMethod(sqlite3_vtab*,int,sqlite3_value**,sqlite3_int64*);
124186 SQLITE_PRIVATE int sqlite3Fts3PendingTermsFlush(Fts3Table *);
124187 SQLITE_PRIVATE void sqlite3Fts3PendingTermsClear(Fts3Table *);
124188 SQLITE_PRIVATE int sqlite3Fts3Optimize(Fts3Table *);
124189 SQLITE_PRIVATE int sqlite3Fts3SegReaderNew(int, int, sqlite3_int64,
124190   sqlite3_int64, sqlite3_int64, const char *, int, Fts3SegReader**);
124191 SQLITE_PRIVATE int sqlite3Fts3SegReaderPending(
124192   Fts3Table*,int,const char*,int,int,Fts3SegReader**);
124193 SQLITE_PRIVATE void sqlite3Fts3SegReaderFree(Fts3SegReader *);
124194 SQLITE_PRIVATE int sqlite3Fts3AllSegdirs(Fts3Table*, int, int, int, sqlite3_stmt **);
124195 SQLITE_PRIVATE int sqlite3Fts3ReadBlock(Fts3Table*, sqlite3_int64, char **, int*, int*);
124196 
124197 SQLITE_PRIVATE int sqlite3Fts3SelectDoctotal(Fts3Table *, sqlite3_stmt **);
124198 SQLITE_PRIVATE int sqlite3Fts3SelectDocsize(Fts3Table *, sqlite3_int64, sqlite3_stmt **);
124199 
124200 #ifndef SQLITE_DISABLE_FTS4_DEFERRED
124201 SQLITE_PRIVATE void sqlite3Fts3FreeDeferredTokens(Fts3Cursor *);
124202 SQLITE_PRIVATE int sqlite3Fts3DeferToken(Fts3Cursor *, Fts3PhraseToken *, int);
124203 SQLITE_PRIVATE int sqlite3Fts3CacheDeferredDoclists(Fts3Cursor *);
124204 SQLITE_PRIVATE void sqlite3Fts3FreeDeferredDoclists(Fts3Cursor *);
124205 SQLITE_PRIVATE int sqlite3Fts3DeferredTokenList(Fts3DeferredToken *, char **, int *);
124206 #else
124207 # define sqlite3Fts3FreeDeferredTokens(x)
124208 # define sqlite3Fts3DeferToken(x,y,z) SQLITE_OK
124209 # define sqlite3Fts3CacheDeferredDoclists(x) SQLITE_OK
124210 # define sqlite3Fts3FreeDeferredDoclists(x)
124211 # define sqlite3Fts3DeferredTokenList(x,y,z) SQLITE_OK
124212 #endif
124213 
124214 SQLITE_PRIVATE void sqlite3Fts3SegmentsClose(Fts3Table *);
124215 SQLITE_PRIVATE int sqlite3Fts3MaxLevel(Fts3Table *, int *);
124216 
124217 /* Special values interpreted by sqlite3SegReaderCursor() */
124218 #define FTS3_SEGCURSOR_PENDING        -1
124219 #define FTS3_SEGCURSOR_ALL            -2
124220 
124221 SQLITE_PRIVATE int sqlite3Fts3SegReaderStart(Fts3Table*, Fts3MultiSegReader*, Fts3SegFilter*);
124222 SQLITE_PRIVATE int sqlite3Fts3SegReaderStep(Fts3Table *, Fts3MultiSegReader *);
124223 SQLITE_PRIVATE void sqlite3Fts3SegReaderFinish(Fts3MultiSegReader *);
124224 
124225 SQLITE_PRIVATE int sqlite3Fts3SegReaderCursor(Fts3Table *,
124226     int, int, int, const char *, int, int, int, Fts3MultiSegReader *);
124227 
124228 /* Flags allowed as part of the 4th argument to SegmentReaderIterate() */
124229 #define FTS3_SEGMENT_REQUIRE_POS   0x00000001
124230 #define FTS3_SEGMENT_IGNORE_EMPTY  0x00000002
124231 #define FTS3_SEGMENT_COLUMN_FILTER 0x00000004
124232 #define FTS3_SEGMENT_PREFIX        0x00000008
124233 #define FTS3_SEGMENT_SCAN          0x00000010
124234 #define FTS3_SEGMENT_FIRST         0x00000020
124235 
124236 /* Type passed as 4th argument to SegmentReaderIterate() */
124237 struct Fts3SegFilter {
124238   const char *zTerm;
124239   int nTerm;
124240   int iCol;
124241   int flags;
124242 };
124243 
124244 struct Fts3MultiSegReader {
124245   /* Used internally by sqlite3Fts3SegReaderXXX() calls */
124246   Fts3SegReader **apSegment;      /* Array of Fts3SegReader objects */
124247   int nSegment;                   /* Size of apSegment array */
124248   int nAdvance;                   /* How many seg-readers to advance */
124249   Fts3SegFilter *pFilter;         /* Pointer to filter object */
124250   char *aBuffer;                  /* Buffer to merge doclists in */
124251   int nBuffer;                    /* Allocated size of aBuffer[] in bytes */
124252 
124253   int iColFilter;                 /* If >=0, filter for this column */
124254   int bRestart;
124255 
124256   /* Used by fts3.c only. */
124257   int nCost;                      /* Cost of running iterator */
124258   int bLookup;                    /* True if a lookup of a single entry. */
124259 
124260   /* Output values. Valid only after Fts3SegReaderStep() returns SQLITE_ROW. */
124261   char *zTerm;                    /* Pointer to term buffer */
124262   int nTerm;                      /* Size of zTerm in bytes */
124263   char *aDoclist;                 /* Pointer to doclist buffer */
124264   int nDoclist;                   /* Size of aDoclist[] in bytes */
124265 };
124266 
124267 SQLITE_PRIVATE int sqlite3Fts3Incrmerge(Fts3Table*,int,int);
124268 
124269 #define fts3GetVarint32(p, piVal) (                                           \
124270   (*(u8*)(p)&0x80) ? sqlite3Fts3GetVarint32(p, piVal) : (*piVal=*(u8*)(p), 1) \
124271 )
124272 
124273 /* fts3.c */
124274 SQLITE_PRIVATE int sqlite3Fts3PutVarint(char *, sqlite3_int64);
124275 SQLITE_PRIVATE int sqlite3Fts3GetVarint(const char *, sqlite_int64 *);
124276 SQLITE_PRIVATE int sqlite3Fts3GetVarint32(const char *, int *);
124277 SQLITE_PRIVATE int sqlite3Fts3VarintLen(sqlite3_uint64);
124278 SQLITE_PRIVATE void sqlite3Fts3Dequote(char *);
124279 SQLITE_PRIVATE void sqlite3Fts3DoclistPrev(int,char*,int,char**,sqlite3_int64*,int*,u8*);
124280 SQLITE_PRIVATE int sqlite3Fts3EvalPhraseStats(Fts3Cursor *, Fts3Expr *, u32 *);
124281 SQLITE_PRIVATE int sqlite3Fts3FirstFilter(sqlite3_int64, char *, int, char *);
124282 SQLITE_PRIVATE void sqlite3Fts3CreateStatTable(int*, Fts3Table*);
124283 
124284 /* fts3_tokenizer.c */
124285 SQLITE_PRIVATE const char *sqlite3Fts3NextToken(const char *, int *);
124286 SQLITE_PRIVATE int sqlite3Fts3InitHashTable(sqlite3 *, Fts3Hash *, const char *);
124287 SQLITE_PRIVATE int sqlite3Fts3InitTokenizer(Fts3Hash *pHash, const char *,
124288     sqlite3_tokenizer **, char **
124289 );
124290 SQLITE_PRIVATE int sqlite3Fts3IsIdChar(char);
124291 
124292 /* fts3_snippet.c */
124293 SQLITE_PRIVATE void sqlite3Fts3Offsets(sqlite3_context*, Fts3Cursor*);
124294 SQLITE_PRIVATE void sqlite3Fts3Snippet(sqlite3_context *, Fts3Cursor *, const char *,
124295   const char *, const char *, int, int
124296 );
124297 SQLITE_PRIVATE void sqlite3Fts3Matchinfo(sqlite3_context *, Fts3Cursor *, const char *);
124298 
124299 /* fts3_expr.c */
124300 SQLITE_PRIVATE int sqlite3Fts3ExprParse(sqlite3_tokenizer *, int,
124301   char **, int, int, int, const char *, int, Fts3Expr **, char **
124302 );
124303 SQLITE_PRIVATE void sqlite3Fts3ExprFree(Fts3Expr *);
124304 #ifdef SQLITE_TEST
124305 SQLITE_PRIVATE int sqlite3Fts3ExprInitTestInterface(sqlite3 *db);
124306 SQLITE_PRIVATE int sqlite3Fts3InitTerm(sqlite3 *db);
124307 #endif
124308 
124309 SQLITE_PRIVATE int sqlite3Fts3OpenTokenizer(sqlite3_tokenizer *, int, const char *, int,
124310   sqlite3_tokenizer_cursor **
124311 );
124312 
124313 /* fts3_aux.c */
124314 SQLITE_PRIVATE int sqlite3Fts3InitAux(sqlite3 *db);
124315 
124316 SQLITE_PRIVATE void sqlite3Fts3EvalPhraseCleanup(Fts3Phrase *);
124317 
124318 SQLITE_PRIVATE int sqlite3Fts3MsrIncrStart(
124319     Fts3Table*, Fts3MultiSegReader*, int, const char*, int);
124320 SQLITE_PRIVATE int sqlite3Fts3MsrIncrNext(
124321     Fts3Table *, Fts3MultiSegReader *, sqlite3_int64 *, char **, int *);
124322 SQLITE_PRIVATE int sqlite3Fts3EvalPhrasePoslist(Fts3Cursor *, Fts3Expr *, int iCol, char **);
124323 SQLITE_PRIVATE int sqlite3Fts3MsrOvfl(Fts3Cursor *, Fts3MultiSegReader *, int *);
124324 SQLITE_PRIVATE int sqlite3Fts3MsrIncrRestart(Fts3MultiSegReader *pCsr);
124325 
124326 /* fts3_tokenize_vtab.c */
124327 SQLITE_PRIVATE int sqlite3Fts3InitTok(sqlite3*, Fts3Hash *);
124328 
124329 /* fts3_unicode2.c (functions generated by parsing unicode text files) */
124330 #ifdef SQLITE_ENABLE_FTS4_UNICODE61
124331 SQLITE_PRIVATE int sqlite3FtsUnicodeFold(int, int);
124332 SQLITE_PRIVATE int sqlite3FtsUnicodeIsalnum(int);
124333 SQLITE_PRIVATE int sqlite3FtsUnicodeIsdiacritic(int);
124334 #endif
124335 
124336 #endif /* !SQLITE_CORE || SQLITE_ENABLE_FTS3 */
124337 #endif /* _FTSINT_H */
124338 
124339 /************** End of fts3Int.h *********************************************/
124340 /************** Continuing where we left off in fts3.c ***********************/
124341 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
124342 
124343 #if defined(SQLITE_ENABLE_FTS3) && !defined(SQLITE_CORE)
124344 # define SQLITE_CORE 1
124345 #endif
124346 
124347 /* #include <assert.h> */
124348 /* #include <stdlib.h> */
124349 /* #include <stddef.h> */
124350 /* #include <stdio.h> */
124351 /* #include <string.h> */
124352 /* #include <stdarg.h> */
124353 
124354 #ifndef SQLITE_CORE
124355   SQLITE_EXTENSION_INIT1
124356 #endif
124357 
124358 static int fts3EvalNext(Fts3Cursor *pCsr);
124359 static int fts3EvalStart(Fts3Cursor *pCsr);
124360 static int fts3TermSegReaderCursor(
124361     Fts3Cursor *, const char *, int, int, Fts3MultiSegReader **);
124362 
124363 /*
124364 ** Write a 64-bit variable-length integer to memory starting at p[0].
124365 ** The length of data written will be between 1 and FTS3_VARINT_MAX bytes.
124366 ** The number of bytes written is returned.
124367 */
124368 SQLITE_PRIVATE int sqlite3Fts3PutVarint(char *p, sqlite_int64 v){
124369   unsigned char *q = (unsigned char *) p;
124370   sqlite_uint64 vu = v;
124371   do{
124372     *q++ = (unsigned char) ((vu & 0x7f) | 0x80);
124373     vu >>= 7;
124374   }while( vu!=0 );
124375   q[-1] &= 0x7f;  /* turn off high bit in final byte */
124376   assert( q - (unsigned char *)p <= FTS3_VARINT_MAX );
124377   return (int) (q - (unsigned char *)p);
124378 }
124379 
124380 #define GETVARINT_STEP(v, ptr, shift, mask1, mask2, var, ret) \
124381   v = (v & mask1) | ( (*ptr++) << shift );                    \
124382   if( (v & mask2)==0 ){ var = v; return ret; }
124383 #define GETVARINT_INIT(v, ptr, shift, mask1, mask2, var, ret) \
124384   v = (*ptr++);                                               \
124385   if( (v & mask2)==0 ){ var = v; return ret; }
124386 
124387 /*
124388 ** Read a 64-bit variable-length integer from memory starting at p[0].
124389 ** Return the number of bytes read, or 0 on error.
124390 ** The value is stored in *v.
124391 */
124392 SQLITE_PRIVATE int sqlite3Fts3GetVarint(const char *p, sqlite_int64 *v){
124393   const char *pStart = p;
124394   u32 a;
124395   u64 b;
124396   int shift;
124397 
124398   GETVARINT_INIT(a, p, 0,  0x00,     0x80, *v, 1);
124399   GETVARINT_STEP(a, p, 7,  0x7F,     0x4000, *v, 2);
124400   GETVARINT_STEP(a, p, 14, 0x3FFF,   0x200000, *v, 3);
124401   GETVARINT_STEP(a, p, 21, 0x1FFFFF, 0x10000000, *v, 4);
124402   b = (a & 0x0FFFFFFF );
124403 
124404   for(shift=28; shift<=63; shift+=7){
124405     u64 c = *p++;
124406     b += (c&0x7F) << shift;
124407     if( (c & 0x80)==0 ) break;
124408   }
124409   *v = b;
124410   return (int)(p - pStart);
124411 }
124412 
124413 /*
124414 ** Similar to sqlite3Fts3GetVarint(), except that the output is truncated to a
124415 ** 32-bit integer before it is returned.
124416 */
124417 SQLITE_PRIVATE int sqlite3Fts3GetVarint32(const char *p, int *pi){
124418   u32 a;
124419 
124420 #ifndef fts3GetVarint32
124421   GETVARINT_INIT(a, p, 0,  0x00,     0x80, *pi, 1);
124422 #else
124423   a = (*p++);
124424   assert( a & 0x80 );
124425 #endif
124426 
124427   GETVARINT_STEP(a, p, 7,  0x7F,     0x4000, *pi, 2);
124428   GETVARINT_STEP(a, p, 14, 0x3FFF,   0x200000, *pi, 3);
124429   GETVARINT_STEP(a, p, 21, 0x1FFFFF, 0x10000000, *pi, 4);
124430   a = (a & 0x0FFFFFFF );
124431   *pi = (int)(a | ((u32)(*p & 0x0F) << 28));
124432   return 5;
124433 }
124434 
124435 /*
124436 ** Return the number of bytes required to encode v as a varint
124437 */
124438 SQLITE_PRIVATE int sqlite3Fts3VarintLen(sqlite3_uint64 v){
124439   int i = 0;
124440   do{
124441     i++;
124442     v >>= 7;
124443   }while( v!=0 );
124444   return i;
124445 }
124446 
124447 /*
124448 ** Convert an SQL-style quoted string into a normal string by removing
124449 ** the quote characters.  The conversion is done in-place.  If the
124450 ** input does not begin with a quote character, then this routine
124451 ** is a no-op.
124452 **
124453 ** Examples:
124454 **
124455 **     "abc"   becomes   abc
124456 **     'xyz'   becomes   xyz
124457 **     [pqr]   becomes   pqr
124458 **     `mno`   becomes   mno
124459 **
124460 */
124461 SQLITE_PRIVATE void sqlite3Fts3Dequote(char *z){
124462   char quote;                     /* Quote character (if any ) */
124463 
124464   quote = z[0];
124465   if( quote=='[' || quote=='\'' || quote=='"' || quote=='`' ){
124466     int iIn = 1;                  /* Index of next byte to read from input */
124467     int iOut = 0;                 /* Index of next byte to write to output */
124468 
124469     /* If the first byte was a '[', then the close-quote character is a ']' */
124470     if( quote=='[' ) quote = ']';
124471 
124472     while( ALWAYS(z[iIn]) ){
124473       if( z[iIn]==quote ){
124474         if( z[iIn+1]!=quote ) break;
124475         z[iOut++] = quote;
124476         iIn += 2;
124477       }else{
124478         z[iOut++] = z[iIn++];
124479       }
124480     }
124481     z[iOut] = '\0';
124482   }
124483 }
124484 
124485 /*
124486 ** Read a single varint from the doclist at *pp and advance *pp to point
124487 ** to the first byte past the end of the varint.  Add the value of the varint
124488 ** to *pVal.
124489 */
124490 static void fts3GetDeltaVarint(char **pp, sqlite3_int64 *pVal){
124491   sqlite3_int64 iVal;
124492   *pp += sqlite3Fts3GetVarint(*pp, &iVal);
124493   *pVal += iVal;
124494 }
124495 
124496 /*
124497 ** When this function is called, *pp points to the first byte following a
124498 ** varint that is part of a doclist (or position-list, or any other list
124499 ** of varints). This function moves *pp to point to the start of that varint,
124500 ** and sets *pVal by the varint value.
124501 **
124502 ** Argument pStart points to the first byte of the doclist that the
124503 ** varint is part of.
124504 */
124505 static void fts3GetReverseVarint(
124506   char **pp,
124507   char *pStart,
124508   sqlite3_int64 *pVal
124509 ){
124510   sqlite3_int64 iVal;
124511   char *p;
124512 
124513   /* Pointer p now points at the first byte past the varint we are
124514   ** interested in. So, unless the doclist is corrupt, the 0x80 bit is
124515   ** clear on character p[-1]. */
124516   for(p = (*pp)-2; p>=pStart && *p&0x80; p--);
124517   p++;
124518   *pp = p;
124519 
124520   sqlite3Fts3GetVarint(p, &iVal);
124521   *pVal = iVal;
124522 }
124523 
124524 /*
124525 ** The xDisconnect() virtual table method.
124526 */
124527 static int fts3DisconnectMethod(sqlite3_vtab *pVtab){
124528   Fts3Table *p = (Fts3Table *)pVtab;
124529   int i;
124530 
124531   assert( p->nPendingData==0 );
124532   assert( p->pSegments==0 );
124533 
124534   /* Free any prepared statements held */
124535   for(i=0; i<SizeofArray(p->aStmt); i++){
124536     sqlite3_finalize(p->aStmt[i]);
124537   }
124538   sqlite3_free(p->zSegmentsTbl);
124539   sqlite3_free(p->zReadExprlist);
124540   sqlite3_free(p->zWriteExprlist);
124541   sqlite3_free(p->zContentTbl);
124542   sqlite3_free(p->zLanguageid);
124543 
124544   /* Invoke the tokenizer destructor to free the tokenizer. */
124545   p->pTokenizer->pModule->xDestroy(p->pTokenizer);
124546 
124547   sqlite3_free(p);
124548   return SQLITE_OK;
124549 }
124550 
124551 /*
124552 ** Construct one or more SQL statements from the format string given
124553 ** and then evaluate those statements. The success code is written
124554 ** into *pRc.
124555 **
124556 ** If *pRc is initially non-zero then this routine is a no-op.
124557 */
124558 static void fts3DbExec(
124559   int *pRc,              /* Success code */
124560   sqlite3 *db,           /* Database in which to run SQL */
124561   const char *zFormat,   /* Format string for SQL */
124562   ...                    /* Arguments to the format string */
124563 ){
124564   va_list ap;
124565   char *zSql;
124566   if( *pRc ) return;
124567   va_start(ap, zFormat);
124568   zSql = sqlite3_vmprintf(zFormat, ap);
124569   va_end(ap);
124570   if( zSql==0 ){
124571     *pRc = SQLITE_NOMEM;
124572   }else{
124573     *pRc = sqlite3_exec(db, zSql, 0, 0, 0);
124574     sqlite3_free(zSql);
124575   }
124576 }
124577 
124578 /*
124579 ** The xDestroy() virtual table method.
124580 */
124581 static int fts3DestroyMethod(sqlite3_vtab *pVtab){
124582   Fts3Table *p = (Fts3Table *)pVtab;
124583   int rc = SQLITE_OK;              /* Return code */
124584   const char *zDb = p->zDb;        /* Name of database (e.g. "main", "temp") */
124585   sqlite3 *db = p->db;             /* Database handle */
124586 
124587   /* Drop the shadow tables */
124588   if( p->zContentTbl==0 ){
124589     fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_content'", zDb, p->zName);
124590   }
124591   fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_segments'", zDb,p->zName);
124592   fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_segdir'", zDb, p->zName);
124593   fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_docsize'", zDb, p->zName);
124594   fts3DbExec(&rc, db, "DROP TABLE IF EXISTS %Q.'%q_stat'", zDb, p->zName);
124595 
124596   /* If everything has worked, invoke fts3DisconnectMethod() to free the
124597   ** memory associated with the Fts3Table structure and return SQLITE_OK.
124598   ** Otherwise, return an SQLite error code.
124599   */
124600   return (rc==SQLITE_OK ? fts3DisconnectMethod(pVtab) : rc);
124601 }
124602 
124603 
124604 /*
124605 ** Invoke sqlite3_declare_vtab() to declare the schema for the FTS3 table
124606 ** passed as the first argument. This is done as part of the xConnect()
124607 ** and xCreate() methods.
124608 **
124609 ** If *pRc is non-zero when this function is called, it is a no-op.
124610 ** Otherwise, if an error occurs, an SQLite error code is stored in *pRc
124611 ** before returning.
124612 */
124613 static void fts3DeclareVtab(int *pRc, Fts3Table *p){
124614   if( *pRc==SQLITE_OK ){
124615     int i;                        /* Iterator variable */
124616     int rc;                       /* Return code */
124617     char *zSql;                   /* SQL statement passed to declare_vtab() */
124618     char *zCols;                  /* List of user defined columns */
124619     const char *zLanguageid;
124620 
124621     zLanguageid = (p->zLanguageid ? p->zLanguageid : "__langid");
124622     sqlite3_vtab_config(p->db, SQLITE_VTAB_CONSTRAINT_SUPPORT, 1);
124623 
124624     /* Create a list of user columns for the virtual table */
124625     zCols = sqlite3_mprintf("%Q, ", p->azColumn[0]);
124626     for(i=1; zCols && i<p->nColumn; i++){
124627       zCols = sqlite3_mprintf("%z%Q, ", zCols, p->azColumn[i]);
124628     }
124629 
124630     /* Create the whole "CREATE TABLE" statement to pass to SQLite */
124631     zSql = sqlite3_mprintf(
124632         "CREATE TABLE x(%s %Q HIDDEN, docid HIDDEN, %Q HIDDEN)",
124633         zCols, p->zName, zLanguageid
124634     );
124635     if( !zCols || !zSql ){
124636       rc = SQLITE_NOMEM;
124637     }else{
124638       rc = sqlite3_declare_vtab(p->db, zSql);
124639     }
124640 
124641     sqlite3_free(zSql);
124642     sqlite3_free(zCols);
124643     *pRc = rc;
124644   }
124645 }
124646 
124647 /*
124648 ** Create the %_stat table if it does not already exist.
124649 */
124650 SQLITE_PRIVATE void sqlite3Fts3CreateStatTable(int *pRc, Fts3Table *p){
124651   fts3DbExec(pRc, p->db,
124652       "CREATE TABLE IF NOT EXISTS %Q.'%q_stat'"
124653           "(id INTEGER PRIMARY KEY, value BLOB);",
124654       p->zDb, p->zName
124655   );
124656   if( (*pRc)==SQLITE_OK ) p->bHasStat = 1;
124657 }
124658 
124659 /*
124660 ** Create the backing store tables (%_content, %_segments and %_segdir)
124661 ** required by the FTS3 table passed as the only argument. This is done
124662 ** as part of the vtab xCreate() method.
124663 **
124664 ** If the p->bHasDocsize boolean is true (indicating that this is an
124665 ** FTS4 table, not an FTS3 table) then also create the %_docsize and
124666 ** %_stat tables required by FTS4.
124667 */
124668 static int fts3CreateTables(Fts3Table *p){
124669   int rc = SQLITE_OK;             /* Return code */
124670   int i;                          /* Iterator variable */
124671   sqlite3 *db = p->db;            /* The database connection */
124672 
124673   if( p->zContentTbl==0 ){
124674     const char *zLanguageid = p->zLanguageid;
124675     char *zContentCols;           /* Columns of %_content table */
124676 
124677     /* Create a list of user columns for the content table */
124678     zContentCols = sqlite3_mprintf("docid INTEGER PRIMARY KEY");
124679     for(i=0; zContentCols && i<p->nColumn; i++){
124680       char *z = p->azColumn[i];
124681       zContentCols = sqlite3_mprintf("%z, 'c%d%q'", zContentCols, i, z);
124682     }
124683     if( zLanguageid && zContentCols ){
124684       zContentCols = sqlite3_mprintf("%z, langid", zContentCols, zLanguageid);
124685     }
124686     if( zContentCols==0 ) rc = SQLITE_NOMEM;
124687 
124688     /* Create the content table */
124689     fts3DbExec(&rc, db,
124690        "CREATE TABLE %Q.'%q_content'(%s)",
124691        p->zDb, p->zName, zContentCols
124692     );
124693     sqlite3_free(zContentCols);
124694   }
124695 
124696   /* Create other tables */
124697   fts3DbExec(&rc, db,
124698       "CREATE TABLE %Q.'%q_segments'(blockid INTEGER PRIMARY KEY, block BLOB);",
124699       p->zDb, p->zName
124700   );
124701   fts3DbExec(&rc, db,
124702       "CREATE TABLE %Q.'%q_segdir'("
124703         "level INTEGER,"
124704         "idx INTEGER,"
124705         "start_block INTEGER,"
124706         "leaves_end_block INTEGER,"
124707         "end_block INTEGER,"
124708         "root BLOB,"
124709         "PRIMARY KEY(level, idx)"
124710       ");",
124711       p->zDb, p->zName
124712   );
124713   if( p->bHasDocsize ){
124714     fts3DbExec(&rc, db,
124715         "CREATE TABLE %Q.'%q_docsize'(docid INTEGER PRIMARY KEY, size BLOB);",
124716         p->zDb, p->zName
124717     );
124718   }
124719   assert( p->bHasStat==p->bFts4 );
124720   if( p->bHasStat ){
124721     sqlite3Fts3CreateStatTable(&rc, p);
124722   }
124723   return rc;
124724 }
124725 
124726 /*
124727 ** Store the current database page-size in bytes in p->nPgsz.
124728 **
124729 ** If *pRc is non-zero when this function is called, it is a no-op.
124730 ** Otherwise, if an error occurs, an SQLite error code is stored in *pRc
124731 ** before returning.
124732 */
124733 static void fts3DatabasePageSize(int *pRc, Fts3Table *p){
124734   if( *pRc==SQLITE_OK ){
124735     int rc;                       /* Return code */
124736     char *zSql;                   /* SQL text "PRAGMA %Q.page_size" */
124737     sqlite3_stmt *pStmt;          /* Compiled "PRAGMA %Q.page_size" statement */
124738 
124739     zSql = sqlite3_mprintf("PRAGMA %Q.page_size", p->zDb);
124740     if( !zSql ){
124741       rc = SQLITE_NOMEM;
124742     }else{
124743       rc = sqlite3_prepare(p->db, zSql, -1, &pStmt, 0);
124744       if( rc==SQLITE_OK ){
124745         sqlite3_step(pStmt);
124746         p->nPgsz = sqlite3_column_int(pStmt, 0);
124747         rc = sqlite3_finalize(pStmt);
124748       }else if( rc==SQLITE_AUTH ){
124749         p->nPgsz = 1024;
124750         rc = SQLITE_OK;
124751       }
124752     }
124753     assert( p->nPgsz>0 || rc!=SQLITE_OK );
124754     sqlite3_free(zSql);
124755     *pRc = rc;
124756   }
124757 }
124758 
124759 /*
124760 ** "Special" FTS4 arguments are column specifications of the following form:
124761 **
124762 **   <key> = <value>
124763 **
124764 ** There may not be whitespace surrounding the "=" character. The <value>
124765 ** term may be quoted, but the <key> may not.
124766 */
124767 static int fts3IsSpecialColumn(
124768   const char *z,
124769   int *pnKey,
124770   char **pzValue
124771 ){
124772   char *zValue;
124773   const char *zCsr = z;
124774 
124775   while( *zCsr!='=' ){
124776     if( *zCsr=='\0' ) return 0;
124777     zCsr++;
124778   }
124779 
124780   *pnKey = (int)(zCsr-z);
124781   zValue = sqlite3_mprintf("%s", &zCsr[1]);
124782   if( zValue ){
124783     sqlite3Fts3Dequote(zValue);
124784   }
124785   *pzValue = zValue;
124786   return 1;
124787 }
124788 
124789 /*
124790 ** Append the output of a printf() style formatting to an existing string.
124791 */
124792 static void fts3Appendf(
124793   int *pRc,                       /* IN/OUT: Error code */
124794   char **pz,                      /* IN/OUT: Pointer to string buffer */
124795   const char *zFormat,            /* Printf format string to append */
124796   ...                             /* Arguments for printf format string */
124797 ){
124798   if( *pRc==SQLITE_OK ){
124799     va_list ap;
124800     char *z;
124801     va_start(ap, zFormat);
124802     z = sqlite3_vmprintf(zFormat, ap);
124803     va_end(ap);
124804     if( z && *pz ){
124805       char *z2 = sqlite3_mprintf("%s%s", *pz, z);
124806       sqlite3_free(z);
124807       z = z2;
124808     }
124809     if( z==0 ) *pRc = SQLITE_NOMEM;
124810     sqlite3_free(*pz);
124811     *pz = z;
124812   }
124813 }
124814 
124815 /*
124816 ** Return a copy of input string zInput enclosed in double-quotes (") and
124817 ** with all double quote characters escaped. For example:
124818 **
124819 **     fts3QuoteId("un \"zip\"")   ->    "un \"\"zip\"\""
124820 **
124821 ** The pointer returned points to memory obtained from sqlite3_malloc(). It
124822 ** is the callers responsibility to call sqlite3_free() to release this
124823 ** memory.
124824 */
124825 static char *fts3QuoteId(char const *zInput){
124826   int nRet;
124827   char *zRet;
124828   nRet = 2 + (int)strlen(zInput)*2 + 1;
124829   zRet = sqlite3_malloc(nRet);
124830   if( zRet ){
124831     int i;
124832     char *z = zRet;
124833     *(z++) = '"';
124834     for(i=0; zInput[i]; i++){
124835       if( zInput[i]=='"' ) *(z++) = '"';
124836       *(z++) = zInput[i];
124837     }
124838     *(z++) = '"';
124839     *(z++) = '\0';
124840   }
124841   return zRet;
124842 }
124843 
124844 /*
124845 ** Return a list of comma separated SQL expressions and a FROM clause that
124846 ** could be used in a SELECT statement such as the following:
124847 **
124848 **     SELECT <list of expressions> FROM %_content AS x ...
124849 **
124850 ** to return the docid, followed by each column of text data in order
124851 ** from left to write. If parameter zFunc is not NULL, then instead of
124852 ** being returned directly each column of text data is passed to an SQL
124853 ** function named zFunc first. For example, if zFunc is "unzip" and the
124854 ** table has the three user-defined columns "a", "b", and "c", the following
124855 ** string is returned:
124856 **
124857 **     "docid, unzip(x.'a'), unzip(x.'b'), unzip(x.'c') FROM %_content AS x"
124858 **
124859 ** The pointer returned points to a buffer allocated by sqlite3_malloc(). It
124860 ** is the responsibility of the caller to eventually free it.
124861 **
124862 ** If *pRc is not SQLITE_OK when this function is called, it is a no-op (and
124863 ** a NULL pointer is returned). Otherwise, if an OOM error is encountered
124864 ** by this function, NULL is returned and *pRc is set to SQLITE_NOMEM. If
124865 ** no error occurs, *pRc is left unmodified.
124866 */
124867 static char *fts3ReadExprList(Fts3Table *p, const char *zFunc, int *pRc){
124868   char *zRet = 0;
124869   char *zFree = 0;
124870   char *zFunction;
124871   int i;
124872 
124873   if( p->zContentTbl==0 ){
124874     if( !zFunc ){
124875       zFunction = "";
124876     }else{
124877       zFree = zFunction = fts3QuoteId(zFunc);
124878     }
124879     fts3Appendf(pRc, &zRet, "docid");
124880     for(i=0; i<p->nColumn; i++){
124881       fts3Appendf(pRc, &zRet, ",%s(x.'c%d%q')", zFunction, i, p->azColumn[i]);
124882     }
124883     if( p->zLanguageid ){
124884       fts3Appendf(pRc, &zRet, ", x.%Q", "langid");
124885     }
124886     sqlite3_free(zFree);
124887   }else{
124888     fts3Appendf(pRc, &zRet, "rowid");
124889     for(i=0; i<p->nColumn; i++){
124890       fts3Appendf(pRc, &zRet, ", x.'%q'", p->azColumn[i]);
124891     }
124892     if( p->zLanguageid ){
124893       fts3Appendf(pRc, &zRet, ", x.%Q", p->zLanguageid);
124894     }
124895   }
124896   fts3Appendf(pRc, &zRet, " FROM '%q'.'%q%s' AS x",
124897       p->zDb,
124898       (p->zContentTbl ? p->zContentTbl : p->zName),
124899       (p->zContentTbl ? "" : "_content")
124900   );
124901   return zRet;
124902 }
124903 
124904 /*
124905 ** Return a list of N comma separated question marks, where N is the number
124906 ** of columns in the %_content table (one for the docid plus one for each
124907 ** user-defined text column).
124908 **
124909 ** If argument zFunc is not NULL, then all but the first question mark
124910 ** is preceded by zFunc and an open bracket, and followed by a closed
124911 ** bracket. For example, if zFunc is "zip" and the FTS3 table has three
124912 ** user-defined text columns, the following string is returned:
124913 **
124914 **     "?, zip(?), zip(?), zip(?)"
124915 **
124916 ** The pointer returned points to a buffer allocated by sqlite3_malloc(). It
124917 ** is the responsibility of the caller to eventually free it.
124918 **
124919 ** If *pRc is not SQLITE_OK when this function is called, it is a no-op (and
124920 ** a NULL pointer is returned). Otherwise, if an OOM error is encountered
124921 ** by this function, NULL is returned and *pRc is set to SQLITE_NOMEM. If
124922 ** no error occurs, *pRc is left unmodified.
124923 */
124924 static char *fts3WriteExprList(Fts3Table *p, const char *zFunc, int *pRc){
124925   char *zRet = 0;
124926   char *zFree = 0;
124927   char *zFunction;
124928   int i;
124929 
124930   if( !zFunc ){
124931     zFunction = "";
124932   }else{
124933     zFree = zFunction = fts3QuoteId(zFunc);
124934   }
124935   fts3Appendf(pRc, &zRet, "?");
124936   for(i=0; i<p->nColumn; i++){
124937     fts3Appendf(pRc, &zRet, ",%s(?)", zFunction);
124938   }
124939   if( p->zLanguageid ){
124940     fts3Appendf(pRc, &zRet, ", ?");
124941   }
124942   sqlite3_free(zFree);
124943   return zRet;
124944 }
124945 
124946 /*
124947 ** This function interprets the string at (*pp) as a non-negative integer
124948 ** value. It reads the integer and sets *pnOut to the value read, then
124949 ** sets *pp to point to the byte immediately following the last byte of
124950 ** the integer value.
124951 **
124952 ** Only decimal digits ('0'..'9') may be part of an integer value.
124953 **
124954 ** If *pp does not being with a decimal digit SQLITE_ERROR is returned and
124955 ** the output value undefined. Otherwise SQLITE_OK is returned.
124956 **
124957 ** This function is used when parsing the "prefix=" FTS4 parameter.
124958 */
124959 static int fts3GobbleInt(const char **pp, int *pnOut){
124960   const char *p;                  /* Iterator pointer */
124961   int nInt = 0;                   /* Output value */
124962 
124963   for(p=*pp; p[0]>='0' && p[0]<='9'; p++){
124964     nInt = nInt * 10 + (p[0] - '0');
124965   }
124966   if( p==*pp ) return SQLITE_ERROR;
124967   *pnOut = nInt;
124968   *pp = p;
124969   return SQLITE_OK;
124970 }
124971 
124972 /*
124973 ** This function is called to allocate an array of Fts3Index structures
124974 ** representing the indexes maintained by the current FTS table. FTS tables
124975 ** always maintain the main "terms" index, but may also maintain one or
124976 ** more "prefix" indexes, depending on the value of the "prefix=" parameter
124977 ** (if any) specified as part of the CREATE VIRTUAL TABLE statement.
124978 **
124979 ** Argument zParam is passed the value of the "prefix=" option if one was
124980 ** specified, or NULL otherwise.
124981 **
124982 ** If no error occurs, SQLITE_OK is returned and *apIndex set to point to
124983 ** the allocated array. *pnIndex is set to the number of elements in the
124984 ** array. If an error does occur, an SQLite error code is returned.
124985 **
124986 ** Regardless of whether or not an error is returned, it is the responsibility
124987 ** of the caller to call sqlite3_free() on the output array to free it.
124988 */
124989 static int fts3PrefixParameter(
124990   const char *zParam,             /* ABC in prefix=ABC parameter to parse */
124991   int *pnIndex,                   /* OUT: size of *apIndex[] array */
124992   struct Fts3Index **apIndex      /* OUT: Array of indexes for this table */
124993 ){
124994   struct Fts3Index *aIndex;       /* Allocated array */
124995   int nIndex = 1;                 /* Number of entries in array */
124996 
124997   if( zParam && zParam[0] ){
124998     const char *p;
124999     nIndex++;
125000     for(p=zParam; *p; p++){
125001       if( *p==',' ) nIndex++;
125002     }
125003   }
125004 
125005   aIndex = sqlite3_malloc(sizeof(struct Fts3Index) * nIndex);
125006   *apIndex = aIndex;
125007   *pnIndex = nIndex;
125008   if( !aIndex ){
125009     return SQLITE_NOMEM;
125010   }
125011 
125012   memset(aIndex, 0, sizeof(struct Fts3Index) * nIndex);
125013   if( zParam ){
125014     const char *p = zParam;
125015     int i;
125016     for(i=1; i<nIndex; i++){
125017       int nPrefix;
125018       if( fts3GobbleInt(&p, &nPrefix) ) return SQLITE_ERROR;
125019       aIndex[i].nPrefix = nPrefix;
125020       p++;
125021     }
125022   }
125023 
125024   return SQLITE_OK;
125025 }
125026 
125027 /*
125028 ** This function is called when initializing an FTS4 table that uses the
125029 ** content=xxx option. It determines the number of and names of the columns
125030 ** of the new FTS4 table.
125031 **
125032 ** The third argument passed to this function is the value passed to the
125033 ** config=xxx option (i.e. "xxx"). This function queries the database for
125034 ** a table of that name. If found, the output variables are populated
125035 ** as follows:
125036 **
125037 **   *pnCol:   Set to the number of columns table xxx has,
125038 **
125039 **   *pnStr:   Set to the total amount of space required to store a copy
125040 **             of each columns name, including the nul-terminator.
125041 **
125042 **   *pazCol:  Set to point to an array of *pnCol strings. Each string is
125043 **             the name of the corresponding column in table xxx. The array
125044 **             and its contents are allocated using a single allocation. It
125045 **             is the responsibility of the caller to free this allocation
125046 **             by eventually passing the *pazCol value to sqlite3_free().
125047 **
125048 ** If the table cannot be found, an error code is returned and the output
125049 ** variables are undefined. Or, if an OOM is encountered, SQLITE_NOMEM is
125050 ** returned (and the output variables are undefined).
125051 */
125052 static int fts3ContentColumns(
125053   sqlite3 *db,                    /* Database handle */
125054   const char *zDb,                /* Name of db (i.e. "main", "temp" etc.) */
125055   const char *zTbl,               /* Name of content table */
125056   const char ***pazCol,           /* OUT: Malloc'd array of column names */
125057   int *pnCol,                     /* OUT: Size of array *pazCol */
125058   int *pnStr                      /* OUT: Bytes of string content */
125059 ){
125060   int rc = SQLITE_OK;             /* Return code */
125061   char *zSql;                     /* "SELECT *" statement on zTbl */
125062   sqlite3_stmt *pStmt = 0;        /* Compiled version of zSql */
125063 
125064   zSql = sqlite3_mprintf("SELECT * FROM %Q.%Q", zDb, zTbl);
125065   if( !zSql ){
125066     rc = SQLITE_NOMEM;
125067   }else{
125068     rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
125069   }
125070   sqlite3_free(zSql);
125071 
125072   if( rc==SQLITE_OK ){
125073     const char **azCol;           /* Output array */
125074     int nStr = 0;                 /* Size of all column names (incl. 0x00) */
125075     int nCol;                     /* Number of table columns */
125076     int i;                        /* Used to iterate through columns */
125077 
125078     /* Loop through the returned columns. Set nStr to the number of bytes of
125079     ** space required to store a copy of each column name, including the
125080     ** nul-terminator byte.  */
125081     nCol = sqlite3_column_count(pStmt);
125082     for(i=0; i<nCol; i++){
125083       const char *zCol = sqlite3_column_name(pStmt, i);
125084       nStr += (int)strlen(zCol) + 1;
125085     }
125086 
125087     /* Allocate and populate the array to return. */
125088     azCol = (const char **)sqlite3_malloc(sizeof(char *) * nCol + nStr);
125089     if( azCol==0 ){
125090       rc = SQLITE_NOMEM;
125091     }else{
125092       char *p = (char *)&azCol[nCol];
125093       for(i=0; i<nCol; i++){
125094         const char *zCol = sqlite3_column_name(pStmt, i);
125095         int n = (int)strlen(zCol)+1;
125096         memcpy(p, zCol, n);
125097         azCol[i] = p;
125098         p += n;
125099       }
125100     }
125101     sqlite3_finalize(pStmt);
125102 
125103     /* Set the output variables. */
125104     *pnCol = nCol;
125105     *pnStr = nStr;
125106     *pazCol = azCol;
125107   }
125108 
125109   return rc;
125110 }
125111 
125112 /*
125113 ** This function is the implementation of both the xConnect and xCreate
125114 ** methods of the FTS3 virtual table.
125115 **
125116 ** The argv[] array contains the following:
125117 **
125118 **   argv[0]   -> module name  ("fts3" or "fts4")
125119 **   argv[1]   -> database name
125120 **   argv[2]   -> table name
125121 **   argv[...] -> "column name" and other module argument fields.
125122 */
125123 static int fts3InitVtab(
125124   int isCreate,                   /* True for xCreate, false for xConnect */
125125   sqlite3 *db,                    /* The SQLite database connection */
125126   void *pAux,                     /* Hash table containing tokenizers */
125127   int argc,                       /* Number of elements in argv array */
125128   const char * const *argv,       /* xCreate/xConnect argument array */
125129   sqlite3_vtab **ppVTab,          /* Write the resulting vtab structure here */
125130   char **pzErr                    /* Write any error message here */
125131 ){
125132   Fts3Hash *pHash = (Fts3Hash *)pAux;
125133   Fts3Table *p = 0;               /* Pointer to allocated vtab */
125134   int rc = SQLITE_OK;             /* Return code */
125135   int i;                          /* Iterator variable */
125136   int nByte;                      /* Size of allocation used for *p */
125137   int iCol;                       /* Column index */
125138   int nString = 0;                /* Bytes required to hold all column names */
125139   int nCol = 0;                   /* Number of columns in the FTS table */
125140   char *zCsr;                     /* Space for holding column names */
125141   int nDb;                        /* Bytes required to hold database name */
125142   int nName;                      /* Bytes required to hold table name */
125143   int isFts4 = (argv[0][3]=='4'); /* True for FTS4, false for FTS3 */
125144   const char **aCol;              /* Array of column names */
125145   sqlite3_tokenizer *pTokenizer = 0;        /* Tokenizer for this table */
125146 
125147   int nIndex;                     /* Size of aIndex[] array */
125148   struct Fts3Index *aIndex = 0;   /* Array of indexes for this table */
125149 
125150   /* The results of parsing supported FTS4 key=value options: */
125151   int bNoDocsize = 0;             /* True to omit %_docsize table */
125152   int bDescIdx = 0;               /* True to store descending indexes */
125153   char *zPrefix = 0;              /* Prefix parameter value (or NULL) */
125154   char *zCompress = 0;            /* compress=? parameter (or NULL) */
125155   char *zUncompress = 0;          /* uncompress=? parameter (or NULL) */
125156   char *zContent = 0;             /* content=? parameter (or NULL) */
125157   char *zLanguageid = 0;          /* languageid=? parameter (or NULL) */
125158   char **azNotindexed = 0;        /* The set of notindexed= columns */
125159   int nNotindexed = 0;            /* Size of azNotindexed[] array */
125160 
125161   assert( strlen(argv[0])==4 );
125162   assert( (sqlite3_strnicmp(argv[0], "fts4", 4)==0 && isFts4)
125163        || (sqlite3_strnicmp(argv[0], "fts3", 4)==0 && !isFts4)
125164   );
125165 
125166   nDb = (int)strlen(argv[1]) + 1;
125167   nName = (int)strlen(argv[2]) + 1;
125168 
125169   nByte = sizeof(const char *) * (argc-2);
125170   aCol = (const char **)sqlite3_malloc(nByte);
125171   if( aCol ){
125172     memset((void*)aCol, 0, nByte);
125173     azNotindexed = (char **)sqlite3_malloc(nByte);
125174   }
125175   if( azNotindexed ){
125176     memset(azNotindexed, 0, nByte);
125177   }
125178   if( !aCol || !azNotindexed ){
125179     rc = SQLITE_NOMEM;
125180     goto fts3_init_out;
125181   }
125182 
125183   /* Loop through all of the arguments passed by the user to the FTS3/4
125184   ** module (i.e. all the column names and special arguments). This loop
125185   ** does the following:
125186   **
125187   **   + Figures out the number of columns the FTSX table will have, and
125188   **     the number of bytes of space that must be allocated to store copies
125189   **     of the column names.
125190   **
125191   **   + If there is a tokenizer specification included in the arguments,
125192   **     initializes the tokenizer pTokenizer.
125193   */
125194   for(i=3; rc==SQLITE_OK && i<argc; i++){
125195     char const *z = argv[i];
125196     int nKey;
125197     char *zVal;
125198 
125199     /* Check if this is a tokenizer specification */
125200     if( !pTokenizer
125201      && strlen(z)>8
125202      && 0==sqlite3_strnicmp(z, "tokenize", 8)
125203      && 0==sqlite3Fts3IsIdChar(z[8])
125204     ){
125205       rc = sqlite3Fts3InitTokenizer(pHash, &z[9], &pTokenizer, pzErr);
125206     }
125207 
125208     /* Check if it is an FTS4 special argument. */
125209     else if( isFts4 && fts3IsSpecialColumn(z, &nKey, &zVal) ){
125210       struct Fts4Option {
125211         const char *zOpt;
125212         int nOpt;
125213       } aFts4Opt[] = {
125214         { "matchinfo",   9 },     /* 0 -> MATCHINFO */
125215         { "prefix",      6 },     /* 1 -> PREFIX */
125216         { "compress",    8 },     /* 2 -> COMPRESS */
125217         { "uncompress", 10 },     /* 3 -> UNCOMPRESS */
125218         { "order",       5 },     /* 4 -> ORDER */
125219         { "content",     7 },     /* 5 -> CONTENT */
125220         { "languageid", 10 },     /* 6 -> LANGUAGEID */
125221         { "notindexed", 10 }      /* 7 -> NOTINDEXED */
125222       };
125223 
125224       int iOpt;
125225       if( !zVal ){
125226         rc = SQLITE_NOMEM;
125227       }else{
125228         for(iOpt=0; iOpt<SizeofArray(aFts4Opt); iOpt++){
125229           struct Fts4Option *pOp = &aFts4Opt[iOpt];
125230           if( nKey==pOp->nOpt && !sqlite3_strnicmp(z, pOp->zOpt, pOp->nOpt) ){
125231             break;
125232           }
125233         }
125234         if( iOpt==SizeofArray(aFts4Opt) ){
125235           *pzErr = sqlite3_mprintf("unrecognized parameter: %s", z);
125236           rc = SQLITE_ERROR;
125237         }else{
125238           switch( iOpt ){
125239             case 0:               /* MATCHINFO */
125240               if( strlen(zVal)!=4 || sqlite3_strnicmp(zVal, "fts3", 4) ){
125241                 *pzErr = sqlite3_mprintf("unrecognized matchinfo: %s", zVal);
125242                 rc = SQLITE_ERROR;
125243               }
125244               bNoDocsize = 1;
125245               break;
125246 
125247             case 1:               /* PREFIX */
125248               sqlite3_free(zPrefix);
125249               zPrefix = zVal;
125250               zVal = 0;
125251               break;
125252 
125253             case 2:               /* COMPRESS */
125254               sqlite3_free(zCompress);
125255               zCompress = zVal;
125256               zVal = 0;
125257               break;
125258 
125259             case 3:               /* UNCOMPRESS */
125260               sqlite3_free(zUncompress);
125261               zUncompress = zVal;
125262               zVal = 0;
125263               break;
125264 
125265             case 4:               /* ORDER */
125266               if( (strlen(zVal)!=3 || sqlite3_strnicmp(zVal, "asc", 3))
125267                && (strlen(zVal)!=4 || sqlite3_strnicmp(zVal, "desc", 4))
125268               ){
125269                 *pzErr = sqlite3_mprintf("unrecognized order: %s", zVal);
125270                 rc = SQLITE_ERROR;
125271               }
125272               bDescIdx = (zVal[0]=='d' || zVal[0]=='D');
125273               break;
125274 
125275             case 5:              /* CONTENT */
125276               sqlite3_free(zContent);
125277               zContent = zVal;
125278               zVal = 0;
125279               break;
125280 
125281             case 6:              /* LANGUAGEID */
125282               assert( iOpt==6 );
125283               sqlite3_free(zLanguageid);
125284               zLanguageid = zVal;
125285               zVal = 0;
125286               break;
125287 
125288             case 7:              /* NOTINDEXED */
125289               azNotindexed[nNotindexed++] = zVal;
125290               zVal = 0;
125291               break;
125292           }
125293         }
125294         sqlite3_free(zVal);
125295       }
125296     }
125297 
125298     /* Otherwise, the argument is a column name. */
125299     else {
125300       nString += (int)(strlen(z) + 1);
125301       aCol[nCol++] = z;
125302     }
125303   }
125304 
125305   /* If a content=xxx option was specified, the following:
125306   **
125307   **   1. Ignore any compress= and uncompress= options.
125308   **
125309   **   2. If no column names were specified as part of the CREATE VIRTUAL
125310   **      TABLE statement, use all columns from the content table.
125311   */
125312   if( rc==SQLITE_OK && zContent ){
125313     sqlite3_free(zCompress);
125314     sqlite3_free(zUncompress);
125315     zCompress = 0;
125316     zUncompress = 0;
125317     if( nCol==0 ){
125318       sqlite3_free((void*)aCol);
125319       aCol = 0;
125320       rc = fts3ContentColumns(db, argv[1], zContent, &aCol, &nCol, &nString);
125321 
125322       /* If a languageid= option was specified, remove the language id
125323       ** column from the aCol[] array. */
125324       if( rc==SQLITE_OK && zLanguageid ){
125325         int j;
125326         for(j=0; j<nCol; j++){
125327           if( sqlite3_stricmp(zLanguageid, aCol[j])==0 ){
125328             int k;
125329             for(k=j; k<nCol; k++) aCol[k] = aCol[k+1];
125330             nCol--;
125331             break;
125332           }
125333         }
125334       }
125335     }
125336   }
125337   if( rc!=SQLITE_OK ) goto fts3_init_out;
125338 
125339   if( nCol==0 ){
125340     assert( nString==0 );
125341     aCol[0] = "content";
125342     nString = 8;
125343     nCol = 1;
125344   }
125345 
125346   if( pTokenizer==0 ){
125347     rc = sqlite3Fts3InitTokenizer(pHash, "simple", &pTokenizer, pzErr);
125348     if( rc!=SQLITE_OK ) goto fts3_init_out;
125349   }
125350   assert( pTokenizer );
125351 
125352   rc = fts3PrefixParameter(zPrefix, &nIndex, &aIndex);
125353   if( rc==SQLITE_ERROR ){
125354     assert( zPrefix );
125355     *pzErr = sqlite3_mprintf("error parsing prefix parameter: %s", zPrefix);
125356   }
125357   if( rc!=SQLITE_OK ) goto fts3_init_out;
125358 
125359   /* Allocate and populate the Fts3Table structure. */
125360   nByte = sizeof(Fts3Table) +                  /* Fts3Table */
125361           nCol * sizeof(char *) +              /* azColumn */
125362           nIndex * sizeof(struct Fts3Index) +  /* aIndex */
125363           nCol * sizeof(u8) +                  /* abNotindexed */
125364           nName +                              /* zName */
125365           nDb +                                /* zDb */
125366           nString;                             /* Space for azColumn strings */
125367   p = (Fts3Table*)sqlite3_malloc(nByte);
125368   if( p==0 ){
125369     rc = SQLITE_NOMEM;
125370     goto fts3_init_out;
125371   }
125372   memset(p, 0, nByte);
125373   p->db = db;
125374   p->nColumn = nCol;
125375   p->nPendingData = 0;
125376   p->azColumn = (char **)&p[1];
125377   p->pTokenizer = pTokenizer;
125378   p->nMaxPendingData = FTS3_MAX_PENDING_DATA;
125379   p->bHasDocsize = (isFts4 && bNoDocsize==0);
125380   p->bHasStat = isFts4;
125381   p->bFts4 = isFts4;
125382   p->bDescIdx = bDescIdx;
125383   p->bAutoincrmerge = 0xff;   /* 0xff means setting unknown */
125384   p->zContentTbl = zContent;
125385   p->zLanguageid = zLanguageid;
125386   zContent = 0;
125387   zLanguageid = 0;
125388   TESTONLY( p->inTransaction = -1 );
125389   TESTONLY( p->mxSavepoint = -1 );
125390 
125391   p->aIndex = (struct Fts3Index *)&p->azColumn[nCol];
125392   memcpy(p->aIndex, aIndex, sizeof(struct Fts3Index) * nIndex);
125393   p->nIndex = nIndex;
125394   for(i=0; i<nIndex; i++){
125395     fts3HashInit(&p->aIndex[i].hPending, FTS3_HASH_STRING, 1);
125396   }
125397   p->abNotindexed = (u8 *)&p->aIndex[nIndex];
125398 
125399   /* Fill in the zName and zDb fields of the vtab structure. */
125400   zCsr = (char *)&p->abNotindexed[nCol];
125401   p->zName = zCsr;
125402   memcpy(zCsr, argv[2], nName);
125403   zCsr += nName;
125404   p->zDb = zCsr;
125405   memcpy(zCsr, argv[1], nDb);
125406   zCsr += nDb;
125407 
125408   /* Fill in the azColumn array */
125409   for(iCol=0; iCol<nCol; iCol++){
125410     char *z;
125411     int n = 0;
125412     z = (char *)sqlite3Fts3NextToken(aCol[iCol], &n);
125413     memcpy(zCsr, z, n);
125414     zCsr[n] = '\0';
125415     sqlite3Fts3Dequote(zCsr);
125416     p->azColumn[iCol] = zCsr;
125417     zCsr += n+1;
125418     assert( zCsr <= &((char *)p)[nByte] );
125419   }
125420 
125421   /* Fill in the abNotindexed array */
125422   for(iCol=0; iCol<nCol; iCol++){
125423     int n = (int)strlen(p->azColumn[iCol]);
125424     for(i=0; i<nNotindexed; i++){
125425       char *zNot = azNotindexed[i];
125426       if( zNot && 0==sqlite3_strnicmp(p->azColumn[iCol], zNot, n) ){
125427         p->abNotindexed[iCol] = 1;
125428         sqlite3_free(zNot);
125429         azNotindexed[i] = 0;
125430       }
125431     }
125432   }
125433   for(i=0; i<nNotindexed; i++){
125434     if( azNotindexed[i] ){
125435       *pzErr = sqlite3_mprintf("no such column: %s", azNotindexed[i]);
125436       rc = SQLITE_ERROR;
125437     }
125438   }
125439 
125440   if( rc==SQLITE_OK && (zCompress==0)!=(zUncompress==0) ){
125441     char const *zMiss = (zCompress==0 ? "compress" : "uncompress");
125442     rc = SQLITE_ERROR;
125443     *pzErr = sqlite3_mprintf("missing %s parameter in fts4 constructor", zMiss);
125444   }
125445   p->zReadExprlist = fts3ReadExprList(p, zUncompress, &rc);
125446   p->zWriteExprlist = fts3WriteExprList(p, zCompress, &rc);
125447   if( rc!=SQLITE_OK ) goto fts3_init_out;
125448 
125449   /* If this is an xCreate call, create the underlying tables in the
125450   ** database. TODO: For xConnect(), it could verify that said tables exist.
125451   */
125452   if( isCreate ){
125453     rc = fts3CreateTables(p);
125454   }
125455 
125456   /* Check to see if a legacy fts3 table has been "upgraded" by the
125457   ** addition of a %_stat table so that it can use incremental merge.
125458   */
125459   if( !isFts4 && !isCreate ){
125460     int rc2 = SQLITE_OK;
125461     fts3DbExec(&rc2, db, "SELECT 1 FROM %Q.'%q_stat' WHERE id=2",
125462                p->zDb, p->zName);
125463     if( rc2==SQLITE_OK ) p->bHasStat = 1;
125464   }
125465 
125466   /* Figure out the page-size for the database. This is required in order to
125467   ** estimate the cost of loading large doclists from the database.  */
125468   fts3DatabasePageSize(&rc, p);
125469   p->nNodeSize = p->nPgsz-35;
125470 
125471   /* Declare the table schema to SQLite. */
125472   fts3DeclareVtab(&rc, p);
125473 
125474 fts3_init_out:
125475   sqlite3_free(zPrefix);
125476   sqlite3_free(aIndex);
125477   sqlite3_free(zCompress);
125478   sqlite3_free(zUncompress);
125479   sqlite3_free(zContent);
125480   sqlite3_free(zLanguageid);
125481   for(i=0; i<nNotindexed; i++) sqlite3_free(azNotindexed[i]);
125482   sqlite3_free((void *)aCol);
125483   sqlite3_free((void *)azNotindexed);
125484   if( rc!=SQLITE_OK ){
125485     if( p ){
125486       fts3DisconnectMethod((sqlite3_vtab *)p);
125487     }else if( pTokenizer ){
125488       pTokenizer->pModule->xDestroy(pTokenizer);
125489     }
125490   }else{
125491     assert( p->pSegments==0 );
125492     *ppVTab = &p->base;
125493   }
125494   return rc;
125495 }
125496 
125497 /*
125498 ** The xConnect() and xCreate() methods for the virtual table. All the
125499 ** work is done in function fts3InitVtab().
125500 */
125501 static int fts3ConnectMethod(
125502   sqlite3 *db,                    /* Database connection */
125503   void *pAux,                     /* Pointer to tokenizer hash table */
125504   int argc,                       /* Number of elements in argv array */
125505   const char * const *argv,       /* xCreate/xConnect argument array */
125506   sqlite3_vtab **ppVtab,          /* OUT: New sqlite3_vtab object */
125507   char **pzErr                    /* OUT: sqlite3_malloc'd error message */
125508 ){
125509   return fts3InitVtab(0, db, pAux, argc, argv, ppVtab, pzErr);
125510 }
125511 static int fts3CreateMethod(
125512   sqlite3 *db,                    /* Database connection */
125513   void *pAux,                     /* Pointer to tokenizer hash table */
125514   int argc,                       /* Number of elements in argv array */
125515   const char * const *argv,       /* xCreate/xConnect argument array */
125516   sqlite3_vtab **ppVtab,          /* OUT: New sqlite3_vtab object */
125517   char **pzErr                    /* OUT: sqlite3_malloc'd error message */
125518 ){
125519   return fts3InitVtab(1, db, pAux, argc, argv, ppVtab, pzErr);
125520 }
125521 
125522 /*
125523 ** Set the pIdxInfo->estimatedRows variable to nRow. Unless this
125524 ** extension is currently being used by a version of SQLite too old to
125525 ** support estimatedRows. In that case this function is a no-op.
125526 */
125527 static void fts3SetEstimatedRows(sqlite3_index_info *pIdxInfo, i64 nRow){
125528 #if SQLITE_VERSION_NUMBER>=3008002
125529   if( sqlite3_libversion_number()>=3008002 ){
125530     pIdxInfo->estimatedRows = nRow;
125531   }
125532 #endif
125533 }
125534 
125535 /*
125536 ** Implementation of the xBestIndex method for FTS3 tables. There
125537 ** are three possible strategies, in order of preference:
125538 **
125539 **   1. Direct lookup by rowid or docid.
125540 **   2. Full-text search using a MATCH operator on a non-docid column.
125541 **   3. Linear scan of %_content table.
125542 */
125543 static int fts3BestIndexMethod(sqlite3_vtab *pVTab, sqlite3_index_info *pInfo){
125544   Fts3Table *p = (Fts3Table *)pVTab;
125545   int i;                          /* Iterator variable */
125546   int iCons = -1;                 /* Index of constraint to use */
125547 
125548   int iLangidCons = -1;           /* Index of langid=x constraint, if present */
125549   int iDocidGe = -1;              /* Index of docid>=x constraint, if present */
125550   int iDocidLe = -1;              /* Index of docid<=x constraint, if present */
125551   int iIdx;
125552 
125553   /* By default use a full table scan. This is an expensive option,
125554   ** so search through the constraints to see if a more efficient
125555   ** strategy is possible.
125556   */
125557   pInfo->idxNum = FTS3_FULLSCAN_SEARCH;
125558   pInfo->estimatedCost = 5000000;
125559   for(i=0; i<pInfo->nConstraint; i++){
125560     int bDocid;                 /* True if this constraint is on docid */
125561     struct sqlite3_index_constraint *pCons = &pInfo->aConstraint[i];
125562     if( pCons->usable==0 ){
125563       if( pCons->op==SQLITE_INDEX_CONSTRAINT_MATCH ){
125564         /* There exists an unusable MATCH constraint. This means that if
125565         ** the planner does elect to use the results of this call as part
125566         ** of the overall query plan the user will see an "unable to use
125567         ** function MATCH in the requested context" error. To discourage
125568         ** this, return a very high cost here.  */
125569         pInfo->idxNum = FTS3_FULLSCAN_SEARCH;
125570         pInfo->estimatedCost = SQLITE_HUGE_COST;
125571         fts3SetEstimatedRows(pInfo, ((sqlite3_int64)1) << 50);
125572         return SQLITE_OK;
125573       }
125574       continue;
125575     }
125576 
125577     bDocid = (pCons->iColumn<0 || pCons->iColumn==p->nColumn+1);
125578 
125579     /* A direct lookup on the rowid or docid column. Assign a cost of 1.0. */
125580     if( iCons<0 && pCons->op==SQLITE_INDEX_CONSTRAINT_EQ && bDocid ){
125581       pInfo->idxNum = FTS3_DOCID_SEARCH;
125582       pInfo->estimatedCost = 1.0;
125583       iCons = i;
125584     }
125585 
125586     /* A MATCH constraint. Use a full-text search.
125587     **
125588     ** If there is more than one MATCH constraint available, use the first
125589     ** one encountered. If there is both a MATCH constraint and a direct
125590     ** rowid/docid lookup, prefer the MATCH strategy. This is done even
125591     ** though the rowid/docid lookup is faster than a MATCH query, selecting
125592     ** it would lead to an "unable to use function MATCH in the requested
125593     ** context" error.
125594     */
125595     if( pCons->op==SQLITE_INDEX_CONSTRAINT_MATCH
125596      && pCons->iColumn>=0 && pCons->iColumn<=p->nColumn
125597     ){
125598       pInfo->idxNum = FTS3_FULLTEXT_SEARCH + pCons->iColumn;
125599       pInfo->estimatedCost = 2.0;
125600       iCons = i;
125601     }
125602 
125603     /* Equality constraint on the langid column */
125604     if( pCons->op==SQLITE_INDEX_CONSTRAINT_EQ
125605      && pCons->iColumn==p->nColumn + 2
125606     ){
125607       iLangidCons = i;
125608     }
125609 
125610     if( bDocid ){
125611       switch( pCons->op ){
125612         case SQLITE_INDEX_CONSTRAINT_GE:
125613         case SQLITE_INDEX_CONSTRAINT_GT:
125614           iDocidGe = i;
125615           break;
125616 
125617         case SQLITE_INDEX_CONSTRAINT_LE:
125618         case SQLITE_INDEX_CONSTRAINT_LT:
125619           iDocidLe = i;
125620           break;
125621       }
125622     }
125623   }
125624 
125625   iIdx = 1;
125626   if( iCons>=0 ){
125627     pInfo->aConstraintUsage[iCons].argvIndex = iIdx++;
125628     pInfo->aConstraintUsage[iCons].omit = 1;
125629   }
125630   if( iLangidCons>=0 ){
125631     pInfo->idxNum |= FTS3_HAVE_LANGID;
125632     pInfo->aConstraintUsage[iLangidCons].argvIndex = iIdx++;
125633   }
125634   if( iDocidGe>=0 ){
125635     pInfo->idxNum |= FTS3_HAVE_DOCID_GE;
125636     pInfo->aConstraintUsage[iDocidGe].argvIndex = iIdx++;
125637   }
125638   if( iDocidLe>=0 ){
125639     pInfo->idxNum |= FTS3_HAVE_DOCID_LE;
125640     pInfo->aConstraintUsage[iDocidLe].argvIndex = iIdx++;
125641   }
125642 
125643   /* Regardless of the strategy selected, FTS can deliver rows in rowid (or
125644   ** docid) order. Both ascending and descending are possible.
125645   */
125646   if( pInfo->nOrderBy==1 ){
125647     struct sqlite3_index_orderby *pOrder = &pInfo->aOrderBy[0];
125648     if( pOrder->iColumn<0 || pOrder->iColumn==p->nColumn+1 ){
125649       if( pOrder->desc ){
125650         pInfo->idxStr = "DESC";
125651       }else{
125652         pInfo->idxStr = "ASC";
125653       }
125654       pInfo->orderByConsumed = 1;
125655     }
125656   }
125657 
125658   assert( p->pSegments==0 );
125659   return SQLITE_OK;
125660 }
125661 
125662 /*
125663 ** Implementation of xOpen method.
125664 */
125665 static int fts3OpenMethod(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCsr){
125666   sqlite3_vtab_cursor *pCsr;               /* Allocated cursor */
125667 
125668   UNUSED_PARAMETER(pVTab);
125669 
125670   /* Allocate a buffer large enough for an Fts3Cursor structure. If the
125671   ** allocation succeeds, zero it and return SQLITE_OK. Otherwise,
125672   ** if the allocation fails, return SQLITE_NOMEM.
125673   */
125674   *ppCsr = pCsr = (sqlite3_vtab_cursor *)sqlite3_malloc(sizeof(Fts3Cursor));
125675   if( !pCsr ){
125676     return SQLITE_NOMEM;
125677   }
125678   memset(pCsr, 0, sizeof(Fts3Cursor));
125679   return SQLITE_OK;
125680 }
125681 
125682 /*
125683 ** Close the cursor.  For additional information see the documentation
125684 ** on the xClose method of the virtual table interface.
125685 */
125686 static int fts3CloseMethod(sqlite3_vtab_cursor *pCursor){
125687   Fts3Cursor *pCsr = (Fts3Cursor *)pCursor;
125688   assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
125689   sqlite3_finalize(pCsr->pStmt);
125690   sqlite3Fts3ExprFree(pCsr->pExpr);
125691   sqlite3Fts3FreeDeferredTokens(pCsr);
125692   sqlite3_free(pCsr->aDoclist);
125693   sqlite3_free(pCsr->aMatchinfo);
125694   assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
125695   sqlite3_free(pCsr);
125696   return SQLITE_OK;
125697 }
125698 
125699 /*
125700 ** If pCsr->pStmt has not been prepared (i.e. if pCsr->pStmt==0), then
125701 ** compose and prepare an SQL statement of the form:
125702 **
125703 **    "SELECT <columns> FROM %_content WHERE rowid = ?"
125704 **
125705 ** (or the equivalent for a content=xxx table) and set pCsr->pStmt to
125706 ** it. If an error occurs, return an SQLite error code.
125707 **
125708 ** Otherwise, set *ppStmt to point to pCsr->pStmt and return SQLITE_OK.
125709 */
125710 static int fts3CursorSeekStmt(Fts3Cursor *pCsr, sqlite3_stmt **ppStmt){
125711   int rc = SQLITE_OK;
125712   if( pCsr->pStmt==0 ){
125713     Fts3Table *p = (Fts3Table *)pCsr->base.pVtab;
125714     char *zSql;
125715     zSql = sqlite3_mprintf("SELECT %s WHERE rowid = ?", p->zReadExprlist);
125716     if( !zSql ) return SQLITE_NOMEM;
125717     rc = sqlite3_prepare_v2(p->db, zSql, -1, &pCsr->pStmt, 0);
125718     sqlite3_free(zSql);
125719   }
125720   *ppStmt = pCsr->pStmt;
125721   return rc;
125722 }
125723 
125724 /*
125725 ** Position the pCsr->pStmt statement so that it is on the row
125726 ** of the %_content table that contains the last match.  Return
125727 ** SQLITE_OK on success.
125728 */
125729 static int fts3CursorSeek(sqlite3_context *pContext, Fts3Cursor *pCsr){
125730   int rc = SQLITE_OK;
125731   if( pCsr->isRequireSeek ){
125732     sqlite3_stmt *pStmt = 0;
125733 
125734     rc = fts3CursorSeekStmt(pCsr, &pStmt);
125735     if( rc==SQLITE_OK ){
125736       sqlite3_bind_int64(pCsr->pStmt, 1, pCsr->iPrevId);
125737       pCsr->isRequireSeek = 0;
125738       if( SQLITE_ROW==sqlite3_step(pCsr->pStmt) ){
125739         return SQLITE_OK;
125740       }else{
125741         rc = sqlite3_reset(pCsr->pStmt);
125742         if( rc==SQLITE_OK && ((Fts3Table *)pCsr->base.pVtab)->zContentTbl==0 ){
125743           /* If no row was found and no error has occurred, then the %_content
125744           ** table is missing a row that is present in the full-text index.
125745           ** The data structures are corrupt.  */
125746           rc = FTS_CORRUPT_VTAB;
125747           pCsr->isEof = 1;
125748         }
125749       }
125750     }
125751   }
125752 
125753   if( rc!=SQLITE_OK && pContext ){
125754     sqlite3_result_error_code(pContext, rc);
125755   }
125756   return rc;
125757 }
125758 
125759 /*
125760 ** This function is used to process a single interior node when searching
125761 ** a b-tree for a term or term prefix. The node data is passed to this
125762 ** function via the zNode/nNode parameters. The term to search for is
125763 ** passed in zTerm/nTerm.
125764 **
125765 ** If piFirst is not NULL, then this function sets *piFirst to the blockid
125766 ** of the child node that heads the sub-tree that may contain the term.
125767 **
125768 ** If piLast is not NULL, then *piLast is set to the right-most child node
125769 ** that heads a sub-tree that may contain a term for which zTerm/nTerm is
125770 ** a prefix.
125771 **
125772 ** If an OOM error occurs, SQLITE_NOMEM is returned. Otherwise, SQLITE_OK.
125773 */
125774 static int fts3ScanInteriorNode(
125775   const char *zTerm,              /* Term to select leaves for */
125776   int nTerm,                      /* Size of term zTerm in bytes */
125777   const char *zNode,              /* Buffer containing segment interior node */
125778   int nNode,                      /* Size of buffer at zNode */
125779   sqlite3_int64 *piFirst,         /* OUT: Selected child node */
125780   sqlite3_int64 *piLast           /* OUT: Selected child node */
125781 ){
125782   int rc = SQLITE_OK;             /* Return code */
125783   const char *zCsr = zNode;       /* Cursor to iterate through node */
125784   const char *zEnd = &zCsr[nNode];/* End of interior node buffer */
125785   char *zBuffer = 0;              /* Buffer to load terms into */
125786   int nAlloc = 0;                 /* Size of allocated buffer */
125787   int isFirstTerm = 1;            /* True when processing first term on page */
125788   sqlite3_int64 iChild;           /* Block id of child node to descend to */
125789 
125790   /* Skip over the 'height' varint that occurs at the start of every
125791   ** interior node. Then load the blockid of the left-child of the b-tree
125792   ** node into variable iChild.
125793   **
125794   ** Even if the data structure on disk is corrupted, this (reading two
125795   ** varints from the buffer) does not risk an overread. If zNode is a
125796   ** root node, then the buffer comes from a SELECT statement. SQLite does
125797   ** not make this guarantee explicitly, but in practice there are always
125798   ** either more than 20 bytes of allocated space following the nNode bytes of
125799   ** contents, or two zero bytes. Or, if the node is read from the %_segments
125800   ** table, then there are always 20 bytes of zeroed padding following the
125801   ** nNode bytes of content (see sqlite3Fts3ReadBlock() for details).
125802   */
125803   zCsr += sqlite3Fts3GetVarint(zCsr, &iChild);
125804   zCsr += sqlite3Fts3GetVarint(zCsr, &iChild);
125805   if( zCsr>zEnd ){
125806     return FTS_CORRUPT_VTAB;
125807   }
125808 
125809   while( zCsr<zEnd && (piFirst || piLast) ){
125810     int cmp;                      /* memcmp() result */
125811     int nSuffix;                  /* Size of term suffix */
125812     int nPrefix = 0;              /* Size of term prefix */
125813     int nBuffer;                  /* Total term size */
125814 
125815     /* Load the next term on the node into zBuffer. Use realloc() to expand
125816     ** the size of zBuffer if required.  */
125817     if( !isFirstTerm ){
125818       zCsr += fts3GetVarint32(zCsr, &nPrefix);
125819     }
125820     isFirstTerm = 0;
125821     zCsr += fts3GetVarint32(zCsr, &nSuffix);
125822 
125823     if( nPrefix<0 || nSuffix<0 || &zCsr[nSuffix]>zEnd ){
125824       rc = FTS_CORRUPT_VTAB;
125825       goto finish_scan;
125826     }
125827     if( nPrefix+nSuffix>nAlloc ){
125828       char *zNew;
125829       nAlloc = (nPrefix+nSuffix) * 2;
125830       zNew = (char *)sqlite3_realloc(zBuffer, nAlloc);
125831       if( !zNew ){
125832         rc = SQLITE_NOMEM;
125833         goto finish_scan;
125834       }
125835       zBuffer = zNew;
125836     }
125837     assert( zBuffer );
125838     memcpy(&zBuffer[nPrefix], zCsr, nSuffix);
125839     nBuffer = nPrefix + nSuffix;
125840     zCsr += nSuffix;
125841 
125842     /* Compare the term we are searching for with the term just loaded from
125843     ** the interior node. If the specified term is greater than or equal
125844     ** to the term from the interior node, then all terms on the sub-tree
125845     ** headed by node iChild are smaller than zTerm. No need to search
125846     ** iChild.
125847     **
125848     ** If the interior node term is larger than the specified term, then
125849     ** the tree headed by iChild may contain the specified term.
125850     */
125851     cmp = memcmp(zTerm, zBuffer, (nBuffer>nTerm ? nTerm : nBuffer));
125852     if( piFirst && (cmp<0 || (cmp==0 && nBuffer>nTerm)) ){
125853       *piFirst = iChild;
125854       piFirst = 0;
125855     }
125856 
125857     if( piLast && cmp<0 ){
125858       *piLast = iChild;
125859       piLast = 0;
125860     }
125861 
125862     iChild++;
125863   };
125864 
125865   if( piFirst ) *piFirst = iChild;
125866   if( piLast ) *piLast = iChild;
125867 
125868  finish_scan:
125869   sqlite3_free(zBuffer);
125870   return rc;
125871 }
125872 
125873 
125874 /*
125875 ** The buffer pointed to by argument zNode (size nNode bytes) contains an
125876 ** interior node of a b-tree segment. The zTerm buffer (size nTerm bytes)
125877 ** contains a term. This function searches the sub-tree headed by the zNode
125878 ** node for the range of leaf nodes that may contain the specified term
125879 ** or terms for which the specified term is a prefix.
125880 **
125881 ** If piLeaf is not NULL, then *piLeaf is set to the blockid of the
125882 ** left-most leaf node in the tree that may contain the specified term.
125883 ** If piLeaf2 is not NULL, then *piLeaf2 is set to the blockid of the
125884 ** right-most leaf node that may contain a term for which the specified
125885 ** term is a prefix.
125886 **
125887 ** It is possible that the range of returned leaf nodes does not contain
125888 ** the specified term or any terms for which it is a prefix. However, if the
125889 ** segment does contain any such terms, they are stored within the identified
125890 ** range. Because this function only inspects interior segment nodes (and
125891 ** never loads leaf nodes into memory), it is not possible to be sure.
125892 **
125893 ** If an error occurs, an error code other than SQLITE_OK is returned.
125894 */
125895 static int fts3SelectLeaf(
125896   Fts3Table *p,                   /* Virtual table handle */
125897   const char *zTerm,              /* Term to select leaves for */
125898   int nTerm,                      /* Size of term zTerm in bytes */
125899   const char *zNode,              /* Buffer containing segment interior node */
125900   int nNode,                      /* Size of buffer at zNode */
125901   sqlite3_int64 *piLeaf,          /* Selected leaf node */
125902   sqlite3_int64 *piLeaf2          /* Selected leaf node */
125903 ){
125904   int rc;                         /* Return code */
125905   int iHeight;                    /* Height of this node in tree */
125906 
125907   assert( piLeaf || piLeaf2 );
125908 
125909   fts3GetVarint32(zNode, &iHeight);
125910   rc = fts3ScanInteriorNode(zTerm, nTerm, zNode, nNode, piLeaf, piLeaf2);
125911   assert( !piLeaf2 || !piLeaf || rc!=SQLITE_OK || (*piLeaf<=*piLeaf2) );
125912 
125913   if( rc==SQLITE_OK && iHeight>1 ){
125914     char *zBlob = 0;              /* Blob read from %_segments table */
125915     int nBlob;                    /* Size of zBlob in bytes */
125916 
125917     if( piLeaf && piLeaf2 && (*piLeaf!=*piLeaf2) ){
125918       rc = sqlite3Fts3ReadBlock(p, *piLeaf, &zBlob, &nBlob, 0);
125919       if( rc==SQLITE_OK ){
125920         rc = fts3SelectLeaf(p, zTerm, nTerm, zBlob, nBlob, piLeaf, 0);
125921       }
125922       sqlite3_free(zBlob);
125923       piLeaf = 0;
125924       zBlob = 0;
125925     }
125926 
125927     if( rc==SQLITE_OK ){
125928       rc = sqlite3Fts3ReadBlock(p, piLeaf?*piLeaf:*piLeaf2, &zBlob, &nBlob, 0);
125929     }
125930     if( rc==SQLITE_OK ){
125931       rc = fts3SelectLeaf(p, zTerm, nTerm, zBlob, nBlob, piLeaf, piLeaf2);
125932     }
125933     sqlite3_free(zBlob);
125934   }
125935 
125936   return rc;
125937 }
125938 
125939 /*
125940 ** This function is used to create delta-encoded serialized lists of FTS3
125941 ** varints. Each call to this function appends a single varint to a list.
125942 */
125943 static void fts3PutDeltaVarint(
125944   char **pp,                      /* IN/OUT: Output pointer */
125945   sqlite3_int64 *piPrev,          /* IN/OUT: Previous value written to list */
125946   sqlite3_int64 iVal              /* Write this value to the list */
125947 ){
125948   assert( iVal-*piPrev > 0 || (*piPrev==0 && iVal==0) );
125949   *pp += sqlite3Fts3PutVarint(*pp, iVal-*piPrev);
125950   *piPrev = iVal;
125951 }
125952 
125953 /*
125954 ** When this function is called, *ppPoslist is assumed to point to the
125955 ** start of a position-list. After it returns, *ppPoslist points to the
125956 ** first byte after the position-list.
125957 **
125958 ** A position list is list of positions (delta encoded) and columns for
125959 ** a single document record of a doclist.  So, in other words, this
125960 ** routine advances *ppPoslist so that it points to the next docid in
125961 ** the doclist, or to the first byte past the end of the doclist.
125962 **
125963 ** If pp is not NULL, then the contents of the position list are copied
125964 ** to *pp. *pp is set to point to the first byte past the last byte copied
125965 ** before this function returns.
125966 */
125967 static void fts3PoslistCopy(char **pp, char **ppPoslist){
125968   char *pEnd = *ppPoslist;
125969   char c = 0;
125970 
125971   /* The end of a position list is marked by a zero encoded as an FTS3
125972   ** varint. A single POS_END (0) byte. Except, if the 0 byte is preceded by
125973   ** a byte with the 0x80 bit set, then it is not a varint 0, but the tail
125974   ** of some other, multi-byte, value.
125975   **
125976   ** The following while-loop moves pEnd to point to the first byte that is not
125977   ** immediately preceded by a byte with the 0x80 bit set. Then increments
125978   ** pEnd once more so that it points to the byte immediately following the
125979   ** last byte in the position-list.
125980   */
125981   while( *pEnd | c ){
125982     c = *pEnd++ & 0x80;
125983     testcase( c!=0 && (*pEnd)==0 );
125984   }
125985   pEnd++;  /* Advance past the POS_END terminator byte */
125986 
125987   if( pp ){
125988     int n = (int)(pEnd - *ppPoslist);
125989     char *p = *pp;
125990     memcpy(p, *ppPoslist, n);
125991     p += n;
125992     *pp = p;
125993   }
125994   *ppPoslist = pEnd;
125995 }
125996 
125997 /*
125998 ** When this function is called, *ppPoslist is assumed to point to the
125999 ** start of a column-list. After it returns, *ppPoslist points to the
126000 ** to the terminator (POS_COLUMN or POS_END) byte of the column-list.
126001 **
126002 ** A column-list is list of delta-encoded positions for a single column
126003 ** within a single document within a doclist.
126004 **
126005 ** The column-list is terminated either by a POS_COLUMN varint (1) or
126006 ** a POS_END varint (0).  This routine leaves *ppPoslist pointing to
126007 ** the POS_COLUMN or POS_END that terminates the column-list.
126008 **
126009 ** If pp is not NULL, then the contents of the column-list are copied
126010 ** to *pp. *pp is set to point to the first byte past the last byte copied
126011 ** before this function returns.  The POS_COLUMN or POS_END terminator
126012 ** is not copied into *pp.
126013 */
126014 static void fts3ColumnlistCopy(char **pp, char **ppPoslist){
126015   char *pEnd = *ppPoslist;
126016   char c = 0;
126017 
126018   /* A column-list is terminated by either a 0x01 or 0x00 byte that is
126019   ** not part of a multi-byte varint.
126020   */
126021   while( 0xFE & (*pEnd | c) ){
126022     c = *pEnd++ & 0x80;
126023     testcase( c!=0 && ((*pEnd)&0xfe)==0 );
126024   }
126025   if( pp ){
126026     int n = (int)(pEnd - *ppPoslist);
126027     char *p = *pp;
126028     memcpy(p, *ppPoslist, n);
126029     p += n;
126030     *pp = p;
126031   }
126032   *ppPoslist = pEnd;
126033 }
126034 
126035 /*
126036 ** Value used to signify the end of an position-list. This is safe because
126037 ** it is not possible to have a document with 2^31 terms.
126038 */
126039 #define POSITION_LIST_END 0x7fffffff
126040 
126041 /*
126042 ** This function is used to help parse position-lists. When this function is
126043 ** called, *pp may point to the start of the next varint in the position-list
126044 ** being parsed, or it may point to 1 byte past the end of the position-list
126045 ** (in which case **pp will be a terminator bytes POS_END (0) or
126046 ** (1)).
126047 **
126048 ** If *pp points past the end of the current position-list, set *pi to
126049 ** POSITION_LIST_END and return. Otherwise, read the next varint from *pp,
126050 ** increment the current value of *pi by the value read, and set *pp to
126051 ** point to the next value before returning.
126052 **
126053 ** Before calling this routine *pi must be initialized to the value of
126054 ** the previous position, or zero if we are reading the first position
126055 ** in the position-list.  Because positions are delta-encoded, the value
126056 ** of the previous position is needed in order to compute the value of
126057 ** the next position.
126058 */
126059 static void fts3ReadNextPos(
126060   char **pp,                    /* IN/OUT: Pointer into position-list buffer */
126061   sqlite3_int64 *pi             /* IN/OUT: Value read from position-list */
126062 ){
126063   if( (**pp)&0xFE ){
126064     fts3GetDeltaVarint(pp, pi);
126065     *pi -= 2;
126066   }else{
126067     *pi = POSITION_LIST_END;
126068   }
126069 }
126070 
126071 /*
126072 ** If parameter iCol is not 0, write an POS_COLUMN (1) byte followed by
126073 ** the value of iCol encoded as a varint to *pp.   This will start a new
126074 ** column list.
126075 **
126076 ** Set *pp to point to the byte just after the last byte written before
126077 ** returning (do not modify it if iCol==0). Return the total number of bytes
126078 ** written (0 if iCol==0).
126079 */
126080 static int fts3PutColNumber(char **pp, int iCol){
126081   int n = 0;                      /* Number of bytes written */
126082   if( iCol ){
126083     char *p = *pp;                /* Output pointer */
126084     n = 1 + sqlite3Fts3PutVarint(&p[1], iCol);
126085     *p = 0x01;
126086     *pp = &p[n];
126087   }
126088   return n;
126089 }
126090 
126091 /*
126092 ** Compute the union of two position lists.  The output written
126093 ** into *pp contains all positions of both *pp1 and *pp2 in sorted
126094 ** order and with any duplicates removed.  All pointers are
126095 ** updated appropriately.   The caller is responsible for insuring
126096 ** that there is enough space in *pp to hold the complete output.
126097 */
126098 static void fts3PoslistMerge(
126099   char **pp,                      /* Output buffer */
126100   char **pp1,                     /* Left input list */
126101   char **pp2                      /* Right input list */
126102 ){
126103   char *p = *pp;
126104   char *p1 = *pp1;
126105   char *p2 = *pp2;
126106 
126107   while( *p1 || *p2 ){
126108     int iCol1;         /* The current column index in pp1 */
126109     int iCol2;         /* The current column index in pp2 */
126110 
126111     if( *p1==POS_COLUMN ) fts3GetVarint32(&p1[1], &iCol1);
126112     else if( *p1==POS_END ) iCol1 = POSITION_LIST_END;
126113     else iCol1 = 0;
126114 
126115     if( *p2==POS_COLUMN ) fts3GetVarint32(&p2[1], &iCol2);
126116     else if( *p2==POS_END ) iCol2 = POSITION_LIST_END;
126117     else iCol2 = 0;
126118 
126119     if( iCol1==iCol2 ){
126120       sqlite3_int64 i1 = 0;       /* Last position from pp1 */
126121       sqlite3_int64 i2 = 0;       /* Last position from pp2 */
126122       sqlite3_int64 iPrev = 0;
126123       int n = fts3PutColNumber(&p, iCol1);
126124       p1 += n;
126125       p2 += n;
126126 
126127       /* At this point, both p1 and p2 point to the start of column-lists
126128       ** for the same column (the column with index iCol1 and iCol2).
126129       ** A column-list is a list of non-negative delta-encoded varints, each
126130       ** incremented by 2 before being stored. Each list is terminated by a
126131       ** POS_END (0) or POS_COLUMN (1). The following block merges the two lists
126132       ** and writes the results to buffer p. p is left pointing to the byte
126133       ** after the list written. No terminator (POS_END or POS_COLUMN) is
126134       ** written to the output.
126135       */
126136       fts3GetDeltaVarint(&p1, &i1);
126137       fts3GetDeltaVarint(&p2, &i2);
126138       do {
126139         fts3PutDeltaVarint(&p, &iPrev, (i1<i2) ? i1 : i2);
126140         iPrev -= 2;
126141         if( i1==i2 ){
126142           fts3ReadNextPos(&p1, &i1);
126143           fts3ReadNextPos(&p2, &i2);
126144         }else if( i1<i2 ){
126145           fts3ReadNextPos(&p1, &i1);
126146         }else{
126147           fts3ReadNextPos(&p2, &i2);
126148         }
126149       }while( i1!=POSITION_LIST_END || i2!=POSITION_LIST_END );
126150     }else if( iCol1<iCol2 ){
126151       p1 += fts3PutColNumber(&p, iCol1);
126152       fts3ColumnlistCopy(&p, &p1);
126153     }else{
126154       p2 += fts3PutColNumber(&p, iCol2);
126155       fts3ColumnlistCopy(&p, &p2);
126156     }
126157   }
126158 
126159   *p++ = POS_END;
126160   *pp = p;
126161   *pp1 = p1 + 1;
126162   *pp2 = p2 + 1;
126163 }
126164 
126165 /*
126166 ** This function is used to merge two position lists into one. When it is
126167 ** called, *pp1 and *pp2 must both point to position lists. A position-list is
126168 ** the part of a doclist that follows each document id. For example, if a row
126169 ** contains:
126170 **
126171 **     'a b c'|'x y z'|'a b b a'
126172 **
126173 ** Then the position list for this row for token 'b' would consist of:
126174 **
126175 **     0x02 0x01 0x02 0x03 0x03 0x00
126176 **
126177 ** When this function returns, both *pp1 and *pp2 are left pointing to the
126178 ** byte following the 0x00 terminator of their respective position lists.
126179 **
126180 ** If isSaveLeft is 0, an entry is added to the output position list for
126181 ** each position in *pp2 for which there exists one or more positions in
126182 ** *pp1 so that (pos(*pp2)>pos(*pp1) && pos(*pp2)-pos(*pp1)<=nToken). i.e.
126183 ** when the *pp1 token appears before the *pp2 token, but not more than nToken
126184 ** slots before it.
126185 **
126186 ** e.g. nToken==1 searches for adjacent positions.
126187 */
126188 static int fts3PoslistPhraseMerge(
126189   char **pp,                      /* IN/OUT: Preallocated output buffer */
126190   int nToken,                     /* Maximum difference in token positions */
126191   int isSaveLeft,                 /* Save the left position */
126192   int isExact,                    /* If *pp1 is exactly nTokens before *pp2 */
126193   char **pp1,                     /* IN/OUT: Left input list */
126194   char **pp2                      /* IN/OUT: Right input list */
126195 ){
126196   char *p = *pp;
126197   char *p1 = *pp1;
126198   char *p2 = *pp2;
126199   int iCol1 = 0;
126200   int iCol2 = 0;
126201 
126202   /* Never set both isSaveLeft and isExact for the same invocation. */
126203   assert( isSaveLeft==0 || isExact==0 );
126204 
126205   assert( p!=0 && *p1!=0 && *p2!=0 );
126206   if( *p1==POS_COLUMN ){
126207     p1++;
126208     p1 += fts3GetVarint32(p1, &iCol1);
126209   }
126210   if( *p2==POS_COLUMN ){
126211     p2++;
126212     p2 += fts3GetVarint32(p2, &iCol2);
126213   }
126214 
126215   while( 1 ){
126216     if( iCol1==iCol2 ){
126217       char *pSave = p;
126218       sqlite3_int64 iPrev = 0;
126219       sqlite3_int64 iPos1 = 0;
126220       sqlite3_int64 iPos2 = 0;
126221 
126222       if( iCol1 ){
126223         *p++ = POS_COLUMN;
126224         p += sqlite3Fts3PutVarint(p, iCol1);
126225       }
126226 
126227       assert( *p1!=POS_END && *p1!=POS_COLUMN );
126228       assert( *p2!=POS_END && *p2!=POS_COLUMN );
126229       fts3GetDeltaVarint(&p1, &iPos1); iPos1 -= 2;
126230       fts3GetDeltaVarint(&p2, &iPos2); iPos2 -= 2;
126231 
126232       while( 1 ){
126233         if( iPos2==iPos1+nToken
126234          || (isExact==0 && iPos2>iPos1 && iPos2<=iPos1+nToken)
126235         ){
126236           sqlite3_int64 iSave;
126237           iSave = isSaveLeft ? iPos1 : iPos2;
126238           fts3PutDeltaVarint(&p, &iPrev, iSave+2); iPrev -= 2;
126239           pSave = 0;
126240           assert( p );
126241         }
126242         if( (!isSaveLeft && iPos2<=(iPos1+nToken)) || iPos2<=iPos1 ){
126243           if( (*p2&0xFE)==0 ) break;
126244           fts3GetDeltaVarint(&p2, &iPos2); iPos2 -= 2;
126245         }else{
126246           if( (*p1&0xFE)==0 ) break;
126247           fts3GetDeltaVarint(&p1, &iPos1); iPos1 -= 2;
126248         }
126249       }
126250 
126251       if( pSave ){
126252         assert( pp && p );
126253         p = pSave;
126254       }
126255 
126256       fts3ColumnlistCopy(0, &p1);
126257       fts3ColumnlistCopy(0, &p2);
126258       assert( (*p1&0xFE)==0 && (*p2&0xFE)==0 );
126259       if( 0==*p1 || 0==*p2 ) break;
126260 
126261       p1++;
126262       p1 += fts3GetVarint32(p1, &iCol1);
126263       p2++;
126264       p2 += fts3GetVarint32(p2, &iCol2);
126265     }
126266 
126267     /* Advance pointer p1 or p2 (whichever corresponds to the smaller of
126268     ** iCol1 and iCol2) so that it points to either the 0x00 that marks the
126269     ** end of the position list, or the 0x01 that precedes the next
126270     ** column-number in the position list.
126271     */
126272     else if( iCol1<iCol2 ){
126273       fts3ColumnlistCopy(0, &p1);
126274       if( 0==*p1 ) break;
126275       p1++;
126276       p1 += fts3GetVarint32(p1, &iCol1);
126277     }else{
126278       fts3ColumnlistCopy(0, &p2);
126279       if( 0==*p2 ) break;
126280       p2++;
126281       p2 += fts3GetVarint32(p2, &iCol2);
126282     }
126283   }
126284 
126285   fts3PoslistCopy(0, &p2);
126286   fts3PoslistCopy(0, &p1);
126287   *pp1 = p1;
126288   *pp2 = p2;
126289   if( *pp==p ){
126290     return 0;
126291   }
126292   *p++ = 0x00;
126293   *pp = p;
126294   return 1;
126295 }
126296 
126297 /*
126298 ** Merge two position-lists as required by the NEAR operator. The argument
126299 ** position lists correspond to the left and right phrases of an expression
126300 ** like:
126301 **
126302 **     "phrase 1" NEAR "phrase number 2"
126303 **
126304 ** Position list *pp1 corresponds to the left-hand side of the NEAR
126305 ** expression and *pp2 to the right. As usual, the indexes in the position
126306 ** lists are the offsets of the last token in each phrase (tokens "1" and "2"
126307 ** in the example above).
126308 **
126309 ** The output position list - written to *pp - is a copy of *pp2 with those
126310 ** entries that are not sufficiently NEAR entries in *pp1 removed.
126311 */
126312 static int fts3PoslistNearMerge(
126313   char **pp,                      /* Output buffer */
126314   char *aTmp,                     /* Temporary buffer space */
126315   int nRight,                     /* Maximum difference in token positions */
126316   int nLeft,                      /* Maximum difference in token positions */
126317   char **pp1,                     /* IN/OUT: Left input list */
126318   char **pp2                      /* IN/OUT: Right input list */
126319 ){
126320   char *p1 = *pp1;
126321   char *p2 = *pp2;
126322 
126323   char *pTmp1 = aTmp;
126324   char *pTmp2;
126325   char *aTmp2;
126326   int res = 1;
126327 
126328   fts3PoslistPhraseMerge(&pTmp1, nRight, 0, 0, pp1, pp2);
126329   aTmp2 = pTmp2 = pTmp1;
126330   *pp1 = p1;
126331   *pp2 = p2;
126332   fts3PoslistPhraseMerge(&pTmp2, nLeft, 1, 0, pp2, pp1);
126333   if( pTmp1!=aTmp && pTmp2!=aTmp2 ){
126334     fts3PoslistMerge(pp, &aTmp, &aTmp2);
126335   }else if( pTmp1!=aTmp ){
126336     fts3PoslistCopy(pp, &aTmp);
126337   }else if( pTmp2!=aTmp2 ){
126338     fts3PoslistCopy(pp, &aTmp2);
126339   }else{
126340     res = 0;
126341   }
126342 
126343   return res;
126344 }
126345 
126346 /*
126347 ** An instance of this function is used to merge together the (potentially
126348 ** large number of) doclists for each term that matches a prefix query.
126349 ** See function fts3TermSelectMerge() for details.
126350 */
126351 typedef struct TermSelect TermSelect;
126352 struct TermSelect {
126353   char *aaOutput[16];             /* Malloc'd output buffers */
126354   int anOutput[16];               /* Size each output buffer in bytes */
126355 };
126356 
126357 /*
126358 ** This function is used to read a single varint from a buffer. Parameter
126359 ** pEnd points 1 byte past the end of the buffer. When this function is
126360 ** called, if *pp points to pEnd or greater, then the end of the buffer
126361 ** has been reached. In this case *pp is set to 0 and the function returns.
126362 **
126363 ** If *pp does not point to or past pEnd, then a single varint is read
126364 ** from *pp. *pp is then set to point 1 byte past the end of the read varint.
126365 **
126366 ** If bDescIdx is false, the value read is added to *pVal before returning.
126367 ** If it is true, the value read is subtracted from *pVal before this
126368 ** function returns.
126369 */
126370 static void fts3GetDeltaVarint3(
126371   char **pp,                      /* IN/OUT: Point to read varint from */
126372   char *pEnd,                     /* End of buffer */
126373   int bDescIdx,                   /* True if docids are descending */
126374   sqlite3_int64 *pVal             /* IN/OUT: Integer value */
126375 ){
126376   if( *pp>=pEnd ){
126377     *pp = 0;
126378   }else{
126379     sqlite3_int64 iVal;
126380     *pp += sqlite3Fts3GetVarint(*pp, &iVal);
126381     if( bDescIdx ){
126382       *pVal -= iVal;
126383     }else{
126384       *pVal += iVal;
126385     }
126386   }
126387 }
126388 
126389 /*
126390 ** This function is used to write a single varint to a buffer. The varint
126391 ** is written to *pp. Before returning, *pp is set to point 1 byte past the
126392 ** end of the value written.
126393 **
126394 ** If *pbFirst is zero when this function is called, the value written to
126395 ** the buffer is that of parameter iVal.
126396 **
126397 ** If *pbFirst is non-zero when this function is called, then the value
126398 ** written is either (iVal-*piPrev) (if bDescIdx is zero) or (*piPrev-iVal)
126399 ** (if bDescIdx is non-zero).
126400 **
126401 ** Before returning, this function always sets *pbFirst to 1 and *piPrev
126402 ** to the value of parameter iVal.
126403 */
126404 static void fts3PutDeltaVarint3(
126405   char **pp,                      /* IN/OUT: Output pointer */
126406   int bDescIdx,                   /* True for descending docids */
126407   sqlite3_int64 *piPrev,          /* IN/OUT: Previous value written to list */
126408   int *pbFirst,                   /* IN/OUT: True after first int written */
126409   sqlite3_int64 iVal              /* Write this value to the list */
126410 ){
126411   sqlite3_int64 iWrite;
126412   if( bDescIdx==0 || *pbFirst==0 ){
126413     iWrite = iVal - *piPrev;
126414   }else{
126415     iWrite = *piPrev - iVal;
126416   }
126417   assert( *pbFirst || *piPrev==0 );
126418   assert( *pbFirst==0 || iWrite>0 );
126419   *pp += sqlite3Fts3PutVarint(*pp, iWrite);
126420   *piPrev = iVal;
126421   *pbFirst = 1;
126422 }
126423 
126424 
126425 /*
126426 ** This macro is used by various functions that merge doclists. The two
126427 ** arguments are 64-bit docid values. If the value of the stack variable
126428 ** bDescDoclist is 0 when this macro is invoked, then it returns (i1-i2).
126429 ** Otherwise, (i2-i1).
126430 **
126431 ** Using this makes it easier to write code that can merge doclists that are
126432 ** sorted in either ascending or descending order.
126433 */
126434 #define DOCID_CMP(i1, i2) ((bDescDoclist?-1:1) * (i1-i2))
126435 
126436 /*
126437 ** This function does an "OR" merge of two doclists (output contains all
126438 ** positions contained in either argument doclist). If the docids in the
126439 ** input doclists are sorted in ascending order, parameter bDescDoclist
126440 ** should be false. If they are sorted in ascending order, it should be
126441 ** passed a non-zero value.
126442 **
126443 ** If no error occurs, *paOut is set to point at an sqlite3_malloc'd buffer
126444 ** containing the output doclist and SQLITE_OK is returned. In this case
126445 ** *pnOut is set to the number of bytes in the output doclist.
126446 **
126447 ** If an error occurs, an SQLite error code is returned. The output values
126448 ** are undefined in this case.
126449 */
126450 static int fts3DoclistOrMerge(
126451   int bDescDoclist,               /* True if arguments are desc */
126452   char *a1, int n1,               /* First doclist */
126453   char *a2, int n2,               /* Second doclist */
126454   char **paOut, int *pnOut        /* OUT: Malloc'd doclist */
126455 ){
126456   sqlite3_int64 i1 = 0;
126457   sqlite3_int64 i2 = 0;
126458   sqlite3_int64 iPrev = 0;
126459   char *pEnd1 = &a1[n1];
126460   char *pEnd2 = &a2[n2];
126461   char *p1 = a1;
126462   char *p2 = a2;
126463   char *p;
126464   char *aOut;
126465   int bFirstOut = 0;
126466 
126467   *paOut = 0;
126468   *pnOut = 0;
126469 
126470   /* Allocate space for the output. Both the input and output doclists
126471   ** are delta encoded. If they are in ascending order (bDescDoclist==0),
126472   ** then the first docid in each list is simply encoded as a varint. For
126473   ** each subsequent docid, the varint stored is the difference between the
126474   ** current and previous docid (a positive number - since the list is in
126475   ** ascending order).
126476   **
126477   ** The first docid written to the output is therefore encoded using the
126478   ** same number of bytes as it is in whichever of the input lists it is
126479   ** read from. And each subsequent docid read from the same input list
126480   ** consumes either the same or less bytes as it did in the input (since
126481   ** the difference between it and the previous value in the output must
126482   ** be a positive value less than or equal to the delta value read from
126483   ** the input list). The same argument applies to all but the first docid
126484   ** read from the 'other' list. And to the contents of all position lists
126485   ** that will be copied and merged from the input to the output.
126486   **
126487   ** However, if the first docid copied to the output is a negative number,
126488   ** then the encoding of the first docid from the 'other' input list may
126489   ** be larger in the output than it was in the input (since the delta value
126490   ** may be a larger positive integer than the actual docid).
126491   **
126492   ** The space required to store the output is therefore the sum of the
126493   ** sizes of the two inputs, plus enough space for exactly one of the input
126494   ** docids to grow.
126495   **
126496   ** A symetric argument may be made if the doclists are in descending
126497   ** order.
126498   */
126499   aOut = sqlite3_malloc(n1+n2+FTS3_VARINT_MAX-1);
126500   if( !aOut ) return SQLITE_NOMEM;
126501 
126502   p = aOut;
126503   fts3GetDeltaVarint3(&p1, pEnd1, 0, &i1);
126504   fts3GetDeltaVarint3(&p2, pEnd2, 0, &i2);
126505   while( p1 || p2 ){
126506     sqlite3_int64 iDiff = DOCID_CMP(i1, i2);
126507 
126508     if( p2 && p1 && iDiff==0 ){
126509       fts3PutDeltaVarint3(&p, bDescDoclist, &iPrev, &bFirstOut, i1);
126510       fts3PoslistMerge(&p, &p1, &p2);
126511       fts3GetDeltaVarint3(&p1, pEnd1, bDescDoclist, &i1);
126512       fts3GetDeltaVarint3(&p2, pEnd2, bDescDoclist, &i2);
126513     }else if( !p2 || (p1 && iDiff<0) ){
126514       fts3PutDeltaVarint3(&p, bDescDoclist, &iPrev, &bFirstOut, i1);
126515       fts3PoslistCopy(&p, &p1);
126516       fts3GetDeltaVarint3(&p1, pEnd1, bDescDoclist, &i1);
126517     }else{
126518       fts3PutDeltaVarint3(&p, bDescDoclist, &iPrev, &bFirstOut, i2);
126519       fts3PoslistCopy(&p, &p2);
126520       fts3GetDeltaVarint3(&p2, pEnd2, bDescDoclist, &i2);
126521     }
126522   }
126523 
126524   *paOut = aOut;
126525   *pnOut = (int)(p-aOut);
126526   assert( *pnOut<=n1+n2+FTS3_VARINT_MAX-1 );
126527   return SQLITE_OK;
126528 }
126529 
126530 /*
126531 ** This function does a "phrase" merge of two doclists. In a phrase merge,
126532 ** the output contains a copy of each position from the right-hand input
126533 ** doclist for which there is a position in the left-hand input doclist
126534 ** exactly nDist tokens before it.
126535 **
126536 ** If the docids in the input doclists are sorted in ascending order,
126537 ** parameter bDescDoclist should be false. If they are sorted in ascending
126538 ** order, it should be passed a non-zero value.
126539 **
126540 ** The right-hand input doclist is overwritten by this function.
126541 */
126542 static void fts3DoclistPhraseMerge(
126543   int bDescDoclist,               /* True if arguments are desc */
126544   int nDist,                      /* Distance from left to right (1=adjacent) */
126545   char *aLeft, int nLeft,         /* Left doclist */
126546   char *aRight, int *pnRight      /* IN/OUT: Right/output doclist */
126547 ){
126548   sqlite3_int64 i1 = 0;
126549   sqlite3_int64 i2 = 0;
126550   sqlite3_int64 iPrev = 0;
126551   char *pEnd1 = &aLeft[nLeft];
126552   char *pEnd2 = &aRight[*pnRight];
126553   char *p1 = aLeft;
126554   char *p2 = aRight;
126555   char *p;
126556   int bFirstOut = 0;
126557   char *aOut = aRight;
126558 
126559   assert( nDist>0 );
126560 
126561   p = aOut;
126562   fts3GetDeltaVarint3(&p1, pEnd1, 0, &i1);
126563   fts3GetDeltaVarint3(&p2, pEnd2, 0, &i2);
126564 
126565   while( p1 && p2 ){
126566     sqlite3_int64 iDiff = DOCID_CMP(i1, i2);
126567     if( iDiff==0 ){
126568       char *pSave = p;
126569       sqlite3_int64 iPrevSave = iPrev;
126570       int bFirstOutSave = bFirstOut;
126571 
126572       fts3PutDeltaVarint3(&p, bDescDoclist, &iPrev, &bFirstOut, i1);
126573       if( 0==fts3PoslistPhraseMerge(&p, nDist, 0, 1, &p1, &p2) ){
126574         p = pSave;
126575         iPrev = iPrevSave;
126576         bFirstOut = bFirstOutSave;
126577       }
126578       fts3GetDeltaVarint3(&p1, pEnd1, bDescDoclist, &i1);
126579       fts3GetDeltaVarint3(&p2, pEnd2, bDescDoclist, &i2);
126580     }else if( iDiff<0 ){
126581       fts3PoslistCopy(0, &p1);
126582       fts3GetDeltaVarint3(&p1, pEnd1, bDescDoclist, &i1);
126583     }else{
126584       fts3PoslistCopy(0, &p2);
126585       fts3GetDeltaVarint3(&p2, pEnd2, bDescDoclist, &i2);
126586     }
126587   }
126588 
126589   *pnRight = (int)(p - aOut);
126590 }
126591 
126592 /*
126593 ** Argument pList points to a position list nList bytes in size. This
126594 ** function checks to see if the position list contains any entries for
126595 ** a token in position 0 (of any column). If so, it writes argument iDelta
126596 ** to the output buffer pOut, followed by a position list consisting only
126597 ** of the entries from pList at position 0, and terminated by an 0x00 byte.
126598 ** The value returned is the number of bytes written to pOut (if any).
126599 */
126600 SQLITE_PRIVATE int sqlite3Fts3FirstFilter(
126601   sqlite3_int64 iDelta,           /* Varint that may be written to pOut */
126602   char *pList,                    /* Position list (no 0x00 term) */
126603   int nList,                      /* Size of pList in bytes */
126604   char *pOut                      /* Write output here */
126605 ){
126606   int nOut = 0;
126607   int bWritten = 0;               /* True once iDelta has been written */
126608   char *p = pList;
126609   char *pEnd = &pList[nList];
126610 
126611   if( *p!=0x01 ){
126612     if( *p==0x02 ){
126613       nOut += sqlite3Fts3PutVarint(&pOut[nOut], iDelta);
126614       pOut[nOut++] = 0x02;
126615       bWritten = 1;
126616     }
126617     fts3ColumnlistCopy(0, &p);
126618   }
126619 
126620   while( p<pEnd && *p==0x01 ){
126621     sqlite3_int64 iCol;
126622     p++;
126623     p += sqlite3Fts3GetVarint(p, &iCol);
126624     if( *p==0x02 ){
126625       if( bWritten==0 ){
126626         nOut += sqlite3Fts3PutVarint(&pOut[nOut], iDelta);
126627         bWritten = 1;
126628       }
126629       pOut[nOut++] = 0x01;
126630       nOut += sqlite3Fts3PutVarint(&pOut[nOut], iCol);
126631       pOut[nOut++] = 0x02;
126632     }
126633     fts3ColumnlistCopy(0, &p);
126634   }
126635   if( bWritten ){
126636     pOut[nOut++] = 0x00;
126637   }
126638 
126639   return nOut;
126640 }
126641 
126642 
126643 /*
126644 ** Merge all doclists in the TermSelect.aaOutput[] array into a single
126645 ** doclist stored in TermSelect.aaOutput[0]. If successful, delete all
126646 ** other doclists (except the aaOutput[0] one) and return SQLITE_OK.
126647 **
126648 ** If an OOM error occurs, return SQLITE_NOMEM. In this case it is
126649 ** the responsibility of the caller to free any doclists left in the
126650 ** TermSelect.aaOutput[] array.
126651 */
126652 static int fts3TermSelectFinishMerge(Fts3Table *p, TermSelect *pTS){
126653   char *aOut = 0;
126654   int nOut = 0;
126655   int i;
126656 
126657   /* Loop through the doclists in the aaOutput[] array. Merge them all
126658   ** into a single doclist.
126659   */
126660   for(i=0; i<SizeofArray(pTS->aaOutput); i++){
126661     if( pTS->aaOutput[i] ){
126662       if( !aOut ){
126663         aOut = pTS->aaOutput[i];
126664         nOut = pTS->anOutput[i];
126665         pTS->aaOutput[i] = 0;
126666       }else{
126667         int nNew;
126668         char *aNew;
126669 
126670         int rc = fts3DoclistOrMerge(p->bDescIdx,
126671             pTS->aaOutput[i], pTS->anOutput[i], aOut, nOut, &aNew, &nNew
126672         );
126673         if( rc!=SQLITE_OK ){
126674           sqlite3_free(aOut);
126675           return rc;
126676         }
126677 
126678         sqlite3_free(pTS->aaOutput[i]);
126679         sqlite3_free(aOut);
126680         pTS->aaOutput[i] = 0;
126681         aOut = aNew;
126682         nOut = nNew;
126683       }
126684     }
126685   }
126686 
126687   pTS->aaOutput[0] = aOut;
126688   pTS->anOutput[0] = nOut;
126689   return SQLITE_OK;
126690 }
126691 
126692 /*
126693 ** Merge the doclist aDoclist/nDoclist into the TermSelect object passed
126694 ** as the first argument. The merge is an "OR" merge (see function
126695 ** fts3DoclistOrMerge() for details).
126696 **
126697 ** This function is called with the doclist for each term that matches
126698 ** a queried prefix. It merges all these doclists into one, the doclist
126699 ** for the specified prefix. Since there can be a very large number of
126700 ** doclists to merge, the merging is done pair-wise using the TermSelect
126701 ** object.
126702 **
126703 ** This function returns SQLITE_OK if the merge is successful, or an
126704 ** SQLite error code (SQLITE_NOMEM) if an error occurs.
126705 */
126706 static int fts3TermSelectMerge(
126707   Fts3Table *p,                   /* FTS table handle */
126708   TermSelect *pTS,                /* TermSelect object to merge into */
126709   char *aDoclist,                 /* Pointer to doclist */
126710   int nDoclist                    /* Size of aDoclist in bytes */
126711 ){
126712   if( pTS->aaOutput[0]==0 ){
126713     /* If this is the first term selected, copy the doclist to the output
126714     ** buffer using memcpy(). */
126715     pTS->aaOutput[0] = sqlite3_malloc(nDoclist);
126716     pTS->anOutput[0] = nDoclist;
126717     if( pTS->aaOutput[0] ){
126718       memcpy(pTS->aaOutput[0], aDoclist, nDoclist);
126719     }else{
126720       return SQLITE_NOMEM;
126721     }
126722   }else{
126723     char *aMerge = aDoclist;
126724     int nMerge = nDoclist;
126725     int iOut;
126726 
126727     for(iOut=0; iOut<SizeofArray(pTS->aaOutput); iOut++){
126728       if( pTS->aaOutput[iOut]==0 ){
126729         assert( iOut>0 );
126730         pTS->aaOutput[iOut] = aMerge;
126731         pTS->anOutput[iOut] = nMerge;
126732         break;
126733       }else{
126734         char *aNew;
126735         int nNew;
126736 
126737         int rc = fts3DoclistOrMerge(p->bDescIdx, aMerge, nMerge,
126738             pTS->aaOutput[iOut], pTS->anOutput[iOut], &aNew, &nNew
126739         );
126740         if( rc!=SQLITE_OK ){
126741           if( aMerge!=aDoclist ) sqlite3_free(aMerge);
126742           return rc;
126743         }
126744 
126745         if( aMerge!=aDoclist ) sqlite3_free(aMerge);
126746         sqlite3_free(pTS->aaOutput[iOut]);
126747         pTS->aaOutput[iOut] = 0;
126748 
126749         aMerge = aNew;
126750         nMerge = nNew;
126751         if( (iOut+1)==SizeofArray(pTS->aaOutput) ){
126752           pTS->aaOutput[iOut] = aMerge;
126753           pTS->anOutput[iOut] = nMerge;
126754         }
126755       }
126756     }
126757   }
126758   return SQLITE_OK;
126759 }
126760 
126761 /*
126762 ** Append SegReader object pNew to the end of the pCsr->apSegment[] array.
126763 */
126764 static int fts3SegReaderCursorAppend(
126765   Fts3MultiSegReader *pCsr,
126766   Fts3SegReader *pNew
126767 ){
126768   if( (pCsr->nSegment%16)==0 ){
126769     Fts3SegReader **apNew;
126770     int nByte = (pCsr->nSegment + 16)*sizeof(Fts3SegReader*);
126771     apNew = (Fts3SegReader **)sqlite3_realloc(pCsr->apSegment, nByte);
126772     if( !apNew ){
126773       sqlite3Fts3SegReaderFree(pNew);
126774       return SQLITE_NOMEM;
126775     }
126776     pCsr->apSegment = apNew;
126777   }
126778   pCsr->apSegment[pCsr->nSegment++] = pNew;
126779   return SQLITE_OK;
126780 }
126781 
126782 /*
126783 ** Add seg-reader objects to the Fts3MultiSegReader object passed as the
126784 ** 8th argument.
126785 **
126786 ** This function returns SQLITE_OK if successful, or an SQLite error code
126787 ** otherwise.
126788 */
126789 static int fts3SegReaderCursor(
126790   Fts3Table *p,                   /* FTS3 table handle */
126791   int iLangid,                    /* Language id */
126792   int iIndex,                     /* Index to search (from 0 to p->nIndex-1) */
126793   int iLevel,                     /* Level of segments to scan */
126794   const char *zTerm,              /* Term to query for */
126795   int nTerm,                      /* Size of zTerm in bytes */
126796   int isPrefix,                   /* True for a prefix search */
126797   int isScan,                     /* True to scan from zTerm to EOF */
126798   Fts3MultiSegReader *pCsr        /* Cursor object to populate */
126799 ){
126800   int rc = SQLITE_OK;             /* Error code */
126801   sqlite3_stmt *pStmt = 0;        /* Statement to iterate through segments */
126802   int rc2;                        /* Result of sqlite3_reset() */
126803 
126804   /* If iLevel is less than 0 and this is not a scan, include a seg-reader
126805   ** for the pending-terms. If this is a scan, then this call must be being
126806   ** made by an fts4aux module, not an FTS table. In this case calling
126807   ** Fts3SegReaderPending might segfault, as the data structures used by
126808   ** fts4aux are not completely populated. So it's easiest to filter these
126809   ** calls out here.  */
126810   if( iLevel<0 && p->aIndex ){
126811     Fts3SegReader *pSeg = 0;
126812     rc = sqlite3Fts3SegReaderPending(p, iIndex, zTerm, nTerm, isPrefix, &pSeg);
126813     if( rc==SQLITE_OK && pSeg ){
126814       rc = fts3SegReaderCursorAppend(pCsr, pSeg);
126815     }
126816   }
126817 
126818   if( iLevel!=FTS3_SEGCURSOR_PENDING ){
126819     if( rc==SQLITE_OK ){
126820       rc = sqlite3Fts3AllSegdirs(p, iLangid, iIndex, iLevel, &pStmt);
126821     }
126822 
126823     while( rc==SQLITE_OK && SQLITE_ROW==(rc = sqlite3_step(pStmt)) ){
126824       Fts3SegReader *pSeg = 0;
126825 
126826       /* Read the values returned by the SELECT into local variables. */
126827       sqlite3_int64 iStartBlock = sqlite3_column_int64(pStmt, 1);
126828       sqlite3_int64 iLeavesEndBlock = sqlite3_column_int64(pStmt, 2);
126829       sqlite3_int64 iEndBlock = sqlite3_column_int64(pStmt, 3);
126830       int nRoot = sqlite3_column_bytes(pStmt, 4);
126831       char const *zRoot = sqlite3_column_blob(pStmt, 4);
126832 
126833       /* If zTerm is not NULL, and this segment is not stored entirely on its
126834       ** root node, the range of leaves scanned can be reduced. Do this. */
126835       if( iStartBlock && zTerm ){
126836         sqlite3_int64 *pi = (isPrefix ? &iLeavesEndBlock : 0);
126837         rc = fts3SelectLeaf(p, zTerm, nTerm, zRoot, nRoot, &iStartBlock, pi);
126838         if( rc!=SQLITE_OK ) goto finished;
126839         if( isPrefix==0 && isScan==0 ) iLeavesEndBlock = iStartBlock;
126840       }
126841 
126842       rc = sqlite3Fts3SegReaderNew(pCsr->nSegment+1,
126843           (isPrefix==0 && isScan==0),
126844           iStartBlock, iLeavesEndBlock,
126845           iEndBlock, zRoot, nRoot, &pSeg
126846       );
126847       if( rc!=SQLITE_OK ) goto finished;
126848       rc = fts3SegReaderCursorAppend(pCsr, pSeg);
126849     }
126850   }
126851 
126852  finished:
126853   rc2 = sqlite3_reset(pStmt);
126854   if( rc==SQLITE_DONE ) rc = rc2;
126855 
126856   return rc;
126857 }
126858 
126859 /*
126860 ** Set up a cursor object for iterating through a full-text index or a
126861 ** single level therein.
126862 */
126863 SQLITE_PRIVATE int sqlite3Fts3SegReaderCursor(
126864   Fts3Table *p,                   /* FTS3 table handle */
126865   int iLangid,                    /* Language-id to search */
126866   int iIndex,                     /* Index to search (from 0 to p->nIndex-1) */
126867   int iLevel,                     /* Level of segments to scan */
126868   const char *zTerm,              /* Term to query for */
126869   int nTerm,                      /* Size of zTerm in bytes */
126870   int isPrefix,                   /* True for a prefix search */
126871   int isScan,                     /* True to scan from zTerm to EOF */
126872   Fts3MultiSegReader *pCsr       /* Cursor object to populate */
126873 ){
126874   assert( iIndex>=0 && iIndex<p->nIndex );
126875   assert( iLevel==FTS3_SEGCURSOR_ALL
126876       ||  iLevel==FTS3_SEGCURSOR_PENDING
126877       ||  iLevel>=0
126878   );
126879   assert( iLevel<FTS3_SEGDIR_MAXLEVEL );
126880   assert( FTS3_SEGCURSOR_ALL<0 && FTS3_SEGCURSOR_PENDING<0 );
126881   assert( isPrefix==0 || isScan==0 );
126882 
126883   memset(pCsr, 0, sizeof(Fts3MultiSegReader));
126884   return fts3SegReaderCursor(
126885       p, iLangid, iIndex, iLevel, zTerm, nTerm, isPrefix, isScan, pCsr
126886   );
126887 }
126888 
126889 /*
126890 ** In addition to its current configuration, have the Fts3MultiSegReader
126891 ** passed as the 4th argument also scan the doclist for term zTerm/nTerm.
126892 **
126893 ** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code.
126894 */
126895 static int fts3SegReaderCursorAddZero(
126896   Fts3Table *p,                   /* FTS virtual table handle */
126897   int iLangid,
126898   const char *zTerm,              /* Term to scan doclist of */
126899   int nTerm,                      /* Number of bytes in zTerm */
126900   Fts3MultiSegReader *pCsr        /* Fts3MultiSegReader to modify */
126901 ){
126902   return fts3SegReaderCursor(p,
126903       iLangid, 0, FTS3_SEGCURSOR_ALL, zTerm, nTerm, 0, 0,pCsr
126904   );
126905 }
126906 
126907 /*
126908 ** Open an Fts3MultiSegReader to scan the doclist for term zTerm/nTerm. Or,
126909 ** if isPrefix is true, to scan the doclist for all terms for which
126910 ** zTerm/nTerm is a prefix. If successful, return SQLITE_OK and write
126911 ** a pointer to the new Fts3MultiSegReader to *ppSegcsr. Otherwise, return
126912 ** an SQLite error code.
126913 **
126914 ** It is the responsibility of the caller to free this object by eventually
126915 ** passing it to fts3SegReaderCursorFree()
126916 **
126917 ** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code.
126918 ** Output parameter *ppSegcsr is set to 0 if an error occurs.
126919 */
126920 static int fts3TermSegReaderCursor(
126921   Fts3Cursor *pCsr,               /* Virtual table cursor handle */
126922   const char *zTerm,              /* Term to query for */
126923   int nTerm,                      /* Size of zTerm in bytes */
126924   int isPrefix,                   /* True for a prefix search */
126925   Fts3MultiSegReader **ppSegcsr   /* OUT: Allocated seg-reader cursor */
126926 ){
126927   Fts3MultiSegReader *pSegcsr;    /* Object to allocate and return */
126928   int rc = SQLITE_NOMEM;          /* Return code */
126929 
126930   pSegcsr = sqlite3_malloc(sizeof(Fts3MultiSegReader));
126931   if( pSegcsr ){
126932     int i;
126933     int bFound = 0;               /* True once an index has been found */
126934     Fts3Table *p = (Fts3Table *)pCsr->base.pVtab;
126935 
126936     if( isPrefix ){
126937       for(i=1; bFound==0 && i<p->nIndex; i++){
126938         if( p->aIndex[i].nPrefix==nTerm ){
126939           bFound = 1;
126940           rc = sqlite3Fts3SegReaderCursor(p, pCsr->iLangid,
126941               i, FTS3_SEGCURSOR_ALL, zTerm, nTerm, 0, 0, pSegcsr
126942           );
126943           pSegcsr->bLookup = 1;
126944         }
126945       }
126946 
126947       for(i=1; bFound==0 && i<p->nIndex; i++){
126948         if( p->aIndex[i].nPrefix==nTerm+1 ){
126949           bFound = 1;
126950           rc = sqlite3Fts3SegReaderCursor(p, pCsr->iLangid,
126951               i, FTS3_SEGCURSOR_ALL, zTerm, nTerm, 1, 0, pSegcsr
126952           );
126953           if( rc==SQLITE_OK ){
126954             rc = fts3SegReaderCursorAddZero(
126955                 p, pCsr->iLangid, zTerm, nTerm, pSegcsr
126956             );
126957           }
126958         }
126959       }
126960     }
126961 
126962     if( bFound==0 ){
126963       rc = sqlite3Fts3SegReaderCursor(p, pCsr->iLangid,
126964           0, FTS3_SEGCURSOR_ALL, zTerm, nTerm, isPrefix, 0, pSegcsr
126965       );
126966       pSegcsr->bLookup = !isPrefix;
126967     }
126968   }
126969 
126970   *ppSegcsr = pSegcsr;
126971   return rc;
126972 }
126973 
126974 /*
126975 ** Free an Fts3MultiSegReader allocated by fts3TermSegReaderCursor().
126976 */
126977 static void fts3SegReaderCursorFree(Fts3MultiSegReader *pSegcsr){
126978   sqlite3Fts3SegReaderFinish(pSegcsr);
126979   sqlite3_free(pSegcsr);
126980 }
126981 
126982 /*
126983 ** This function retrieves the doclist for the specified term (or term
126984 ** prefix) from the database.
126985 */
126986 static int fts3TermSelect(
126987   Fts3Table *p,                   /* Virtual table handle */
126988   Fts3PhraseToken *pTok,          /* Token to query for */
126989   int iColumn,                    /* Column to query (or -ve for all columns) */
126990   int *pnOut,                     /* OUT: Size of buffer at *ppOut */
126991   char **ppOut                    /* OUT: Malloced result buffer */
126992 ){
126993   int rc;                         /* Return code */
126994   Fts3MultiSegReader *pSegcsr;    /* Seg-reader cursor for this term */
126995   TermSelect tsc;                 /* Object for pair-wise doclist merging */
126996   Fts3SegFilter filter;           /* Segment term filter configuration */
126997 
126998   pSegcsr = pTok->pSegcsr;
126999   memset(&tsc, 0, sizeof(TermSelect));
127000 
127001   filter.flags = FTS3_SEGMENT_IGNORE_EMPTY | FTS3_SEGMENT_REQUIRE_POS
127002         | (pTok->isPrefix ? FTS3_SEGMENT_PREFIX : 0)
127003         | (pTok->bFirst ? FTS3_SEGMENT_FIRST : 0)
127004         | (iColumn<p->nColumn ? FTS3_SEGMENT_COLUMN_FILTER : 0);
127005   filter.iCol = iColumn;
127006   filter.zTerm = pTok->z;
127007   filter.nTerm = pTok->n;
127008 
127009   rc = sqlite3Fts3SegReaderStart(p, pSegcsr, &filter);
127010   while( SQLITE_OK==rc
127011       && SQLITE_ROW==(rc = sqlite3Fts3SegReaderStep(p, pSegcsr))
127012   ){
127013     rc = fts3TermSelectMerge(p, &tsc, pSegcsr->aDoclist, pSegcsr->nDoclist);
127014   }
127015 
127016   if( rc==SQLITE_OK ){
127017     rc = fts3TermSelectFinishMerge(p, &tsc);
127018   }
127019   if( rc==SQLITE_OK ){
127020     *ppOut = tsc.aaOutput[0];
127021     *pnOut = tsc.anOutput[0];
127022   }else{
127023     int i;
127024     for(i=0; i<SizeofArray(tsc.aaOutput); i++){
127025       sqlite3_free(tsc.aaOutput[i]);
127026     }
127027   }
127028 
127029   fts3SegReaderCursorFree(pSegcsr);
127030   pTok->pSegcsr = 0;
127031   return rc;
127032 }
127033 
127034 /*
127035 ** This function counts the total number of docids in the doclist stored
127036 ** in buffer aList[], size nList bytes.
127037 **
127038 ** If the isPoslist argument is true, then it is assumed that the doclist
127039 ** contains a position-list following each docid. Otherwise, it is assumed
127040 ** that the doclist is simply a list of docids stored as delta encoded
127041 ** varints.
127042 */
127043 static int fts3DoclistCountDocids(char *aList, int nList){
127044   int nDoc = 0;                   /* Return value */
127045   if( aList ){
127046     char *aEnd = &aList[nList];   /* Pointer to one byte after EOF */
127047     char *p = aList;              /* Cursor */
127048     while( p<aEnd ){
127049       nDoc++;
127050       while( (*p++)&0x80 );     /* Skip docid varint */
127051       fts3PoslistCopy(0, &p);   /* Skip over position list */
127052     }
127053   }
127054 
127055   return nDoc;
127056 }
127057 
127058 /*
127059 ** Advance the cursor to the next row in the %_content table that
127060 ** matches the search criteria.  For a MATCH search, this will be
127061 ** the next row that matches. For a full-table scan, this will be
127062 ** simply the next row in the %_content table.  For a docid lookup,
127063 ** this routine simply sets the EOF flag.
127064 **
127065 ** Return SQLITE_OK if nothing goes wrong.  SQLITE_OK is returned
127066 ** even if we reach end-of-file.  The fts3EofMethod() will be called
127067 ** subsequently to determine whether or not an EOF was hit.
127068 */
127069 static int fts3NextMethod(sqlite3_vtab_cursor *pCursor){
127070   int rc;
127071   Fts3Cursor *pCsr = (Fts3Cursor *)pCursor;
127072   if( pCsr->eSearch==FTS3_DOCID_SEARCH || pCsr->eSearch==FTS3_FULLSCAN_SEARCH ){
127073     if( SQLITE_ROW!=sqlite3_step(pCsr->pStmt) ){
127074       pCsr->isEof = 1;
127075       rc = sqlite3_reset(pCsr->pStmt);
127076     }else{
127077       pCsr->iPrevId = sqlite3_column_int64(pCsr->pStmt, 0);
127078       rc = SQLITE_OK;
127079     }
127080   }else{
127081     rc = fts3EvalNext((Fts3Cursor *)pCursor);
127082   }
127083   assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
127084   return rc;
127085 }
127086 
127087 /*
127088 ** The following are copied from sqliteInt.h.
127089 **
127090 ** Constants for the largest and smallest possible 64-bit signed integers.
127091 ** These macros are designed to work correctly on both 32-bit and 64-bit
127092 ** compilers.
127093 */
127094 #ifndef SQLITE_AMALGAMATION
127095 # define LARGEST_INT64  (0xffffffff|(((sqlite3_int64)0x7fffffff)<<32))
127096 # define SMALLEST_INT64 (((sqlite3_int64)-1) - LARGEST_INT64)
127097 #endif
127098 
127099 /*
127100 ** If the numeric type of argument pVal is "integer", then return it
127101 ** converted to a 64-bit signed integer. Otherwise, return a copy of
127102 ** the second parameter, iDefault.
127103 */
127104 static sqlite3_int64 fts3DocidRange(sqlite3_value *pVal, i64 iDefault){
127105   if( pVal ){
127106     int eType = sqlite3_value_numeric_type(pVal);
127107     if( eType==SQLITE_INTEGER ){
127108       return sqlite3_value_int64(pVal);
127109     }
127110   }
127111   return iDefault;
127112 }
127113 
127114 /*
127115 ** This is the xFilter interface for the virtual table.  See
127116 ** the virtual table xFilter method documentation for additional
127117 ** information.
127118 **
127119 ** If idxNum==FTS3_FULLSCAN_SEARCH then do a full table scan against
127120 ** the %_content table.
127121 **
127122 ** If idxNum==FTS3_DOCID_SEARCH then do a docid lookup for a single entry
127123 ** in the %_content table.
127124 **
127125 ** If idxNum>=FTS3_FULLTEXT_SEARCH then use the full text index.  The
127126 ** column on the left-hand side of the MATCH operator is column
127127 ** number idxNum-FTS3_FULLTEXT_SEARCH, 0 indexed.  argv[0] is the right-hand
127128 ** side of the MATCH operator.
127129 */
127130 static int fts3FilterMethod(
127131   sqlite3_vtab_cursor *pCursor,   /* The cursor used for this query */
127132   int idxNum,                     /* Strategy index */
127133   const char *idxStr,             /* Unused */
127134   int nVal,                       /* Number of elements in apVal */
127135   sqlite3_value **apVal           /* Arguments for the indexing scheme */
127136 ){
127137   int rc;
127138   char *zSql;                     /* SQL statement used to access %_content */
127139   int eSearch;
127140   Fts3Table *p = (Fts3Table *)pCursor->pVtab;
127141   Fts3Cursor *pCsr = (Fts3Cursor *)pCursor;
127142 
127143   sqlite3_value *pCons = 0;       /* The MATCH or rowid constraint, if any */
127144   sqlite3_value *pLangid = 0;     /* The "langid = ?" constraint, if any */
127145   sqlite3_value *pDocidGe = 0;    /* The "docid >= ?" constraint, if any */
127146   sqlite3_value *pDocidLe = 0;    /* The "docid <= ?" constraint, if any */
127147   int iIdx;
127148 
127149   UNUSED_PARAMETER(idxStr);
127150   UNUSED_PARAMETER(nVal);
127151 
127152   eSearch = (idxNum & 0x0000FFFF);
127153   assert( eSearch>=0 && eSearch<=(FTS3_FULLTEXT_SEARCH+p->nColumn) );
127154   assert( p->pSegments==0 );
127155 
127156   /* Collect arguments into local variables */
127157   iIdx = 0;
127158   if( eSearch!=FTS3_FULLSCAN_SEARCH ) pCons = apVal[iIdx++];
127159   if( idxNum & FTS3_HAVE_LANGID ) pLangid = apVal[iIdx++];
127160   if( idxNum & FTS3_HAVE_DOCID_GE ) pDocidGe = apVal[iIdx++];
127161   if( idxNum & FTS3_HAVE_DOCID_LE ) pDocidLe = apVal[iIdx++];
127162   assert( iIdx==nVal );
127163 
127164   /* In case the cursor has been used before, clear it now. */
127165   sqlite3_finalize(pCsr->pStmt);
127166   sqlite3_free(pCsr->aDoclist);
127167   sqlite3Fts3ExprFree(pCsr->pExpr);
127168   memset(&pCursor[1], 0, sizeof(Fts3Cursor)-sizeof(sqlite3_vtab_cursor));
127169 
127170   /* Set the lower and upper bounds on docids to return */
127171   pCsr->iMinDocid = fts3DocidRange(pDocidGe, SMALLEST_INT64);
127172   pCsr->iMaxDocid = fts3DocidRange(pDocidLe, LARGEST_INT64);
127173 
127174   if( idxStr ){
127175     pCsr->bDesc = (idxStr[0]=='D');
127176   }else{
127177     pCsr->bDesc = p->bDescIdx;
127178   }
127179   pCsr->eSearch = (i16)eSearch;
127180 
127181   if( eSearch!=FTS3_DOCID_SEARCH && eSearch!=FTS3_FULLSCAN_SEARCH ){
127182     int iCol = eSearch-FTS3_FULLTEXT_SEARCH;
127183     const char *zQuery = (const char *)sqlite3_value_text(pCons);
127184 
127185     if( zQuery==0 && sqlite3_value_type(pCons)!=SQLITE_NULL ){
127186       return SQLITE_NOMEM;
127187     }
127188 
127189     pCsr->iLangid = 0;
127190     if( pLangid ) pCsr->iLangid = sqlite3_value_int(pLangid);
127191 
127192     assert( p->base.zErrMsg==0 );
127193     rc = sqlite3Fts3ExprParse(p->pTokenizer, pCsr->iLangid,
127194         p->azColumn, p->bFts4, p->nColumn, iCol, zQuery, -1, &pCsr->pExpr,
127195         &p->base.zErrMsg
127196     );
127197     if( rc!=SQLITE_OK ){
127198       return rc;
127199     }
127200 
127201     rc = fts3EvalStart(pCsr);
127202     sqlite3Fts3SegmentsClose(p);
127203     if( rc!=SQLITE_OK ) return rc;
127204     pCsr->pNextId = pCsr->aDoclist;
127205     pCsr->iPrevId = 0;
127206   }
127207 
127208   /* Compile a SELECT statement for this cursor. For a full-table-scan, the
127209   ** statement loops through all rows of the %_content table. For a
127210   ** full-text query or docid lookup, the statement retrieves a single
127211   ** row by docid.
127212   */
127213   if( eSearch==FTS3_FULLSCAN_SEARCH ){
127214     zSql = sqlite3_mprintf(
127215         "SELECT %s ORDER BY rowid %s",
127216         p->zReadExprlist, (pCsr->bDesc ? "DESC" : "ASC")
127217     );
127218     if( zSql ){
127219       rc = sqlite3_prepare_v2(p->db, zSql, -1, &pCsr->pStmt, 0);
127220       sqlite3_free(zSql);
127221     }else{
127222       rc = SQLITE_NOMEM;
127223     }
127224   }else if( eSearch==FTS3_DOCID_SEARCH ){
127225     rc = fts3CursorSeekStmt(pCsr, &pCsr->pStmt);
127226     if( rc==SQLITE_OK ){
127227       rc = sqlite3_bind_value(pCsr->pStmt, 1, pCons);
127228     }
127229   }
127230   if( rc!=SQLITE_OK ) return rc;
127231 
127232   return fts3NextMethod(pCursor);
127233 }
127234 
127235 /*
127236 ** This is the xEof method of the virtual table. SQLite calls this
127237 ** routine to find out if it has reached the end of a result set.
127238 */
127239 static int fts3EofMethod(sqlite3_vtab_cursor *pCursor){
127240   return ((Fts3Cursor *)pCursor)->isEof;
127241 }
127242 
127243 /*
127244 ** This is the xRowid method. The SQLite core calls this routine to
127245 ** retrieve the rowid for the current row of the result set. fts3
127246 ** exposes %_content.docid as the rowid for the virtual table. The
127247 ** rowid should be written to *pRowid.
127248 */
127249 static int fts3RowidMethod(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){
127250   Fts3Cursor *pCsr = (Fts3Cursor *) pCursor;
127251   *pRowid = pCsr->iPrevId;
127252   return SQLITE_OK;
127253 }
127254 
127255 /*
127256 ** This is the xColumn method, called by SQLite to request a value from
127257 ** the row that the supplied cursor currently points to.
127258 **
127259 ** If:
127260 **
127261 **   (iCol <  p->nColumn)   -> The value of the iCol'th user column.
127262 **   (iCol == p->nColumn)   -> Magic column with the same name as the table.
127263 **   (iCol == p->nColumn+1) -> Docid column
127264 **   (iCol == p->nColumn+2) -> Langid column
127265 */
127266 static int fts3ColumnMethod(
127267   sqlite3_vtab_cursor *pCursor,   /* Cursor to retrieve value from */
127268   sqlite3_context *pCtx,          /* Context for sqlite3_result_xxx() calls */
127269   int iCol                        /* Index of column to read value from */
127270 ){
127271   int rc = SQLITE_OK;             /* Return Code */
127272   Fts3Cursor *pCsr = (Fts3Cursor *) pCursor;
127273   Fts3Table *p = (Fts3Table *)pCursor->pVtab;
127274 
127275   /* The column value supplied by SQLite must be in range. */
127276   assert( iCol>=0 && iCol<=p->nColumn+2 );
127277 
127278   if( iCol==p->nColumn+1 ){
127279     /* This call is a request for the "docid" column. Since "docid" is an
127280     ** alias for "rowid", use the xRowid() method to obtain the value.
127281     */
127282     sqlite3_result_int64(pCtx, pCsr->iPrevId);
127283   }else if( iCol==p->nColumn ){
127284     /* The extra column whose name is the same as the table.
127285     ** Return a blob which is a pointer to the cursor.  */
127286     sqlite3_result_blob(pCtx, &pCsr, sizeof(pCsr), SQLITE_TRANSIENT);
127287   }else if( iCol==p->nColumn+2 && pCsr->pExpr ){
127288     sqlite3_result_int64(pCtx, pCsr->iLangid);
127289   }else{
127290     /* The requested column is either a user column (one that contains
127291     ** indexed data), or the language-id column.  */
127292     rc = fts3CursorSeek(0, pCsr);
127293 
127294     if( rc==SQLITE_OK ){
127295       if( iCol==p->nColumn+2 ){
127296         int iLangid = 0;
127297         if( p->zLanguageid ){
127298           iLangid = sqlite3_column_int(pCsr->pStmt, p->nColumn+1);
127299         }
127300         sqlite3_result_int(pCtx, iLangid);
127301       }else if( sqlite3_data_count(pCsr->pStmt)>(iCol+1) ){
127302         sqlite3_result_value(pCtx, sqlite3_column_value(pCsr->pStmt, iCol+1));
127303       }
127304     }
127305   }
127306 
127307   assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
127308   return rc;
127309 }
127310 
127311 /*
127312 ** This function is the implementation of the xUpdate callback used by
127313 ** FTS3 virtual tables. It is invoked by SQLite each time a row is to be
127314 ** inserted, updated or deleted.
127315 */
127316 static int fts3UpdateMethod(
127317   sqlite3_vtab *pVtab,            /* Virtual table handle */
127318   int nArg,                       /* Size of argument array */
127319   sqlite3_value **apVal,          /* Array of arguments */
127320   sqlite_int64 *pRowid            /* OUT: The affected (or effected) rowid */
127321 ){
127322   return sqlite3Fts3UpdateMethod(pVtab, nArg, apVal, pRowid);
127323 }
127324 
127325 /*
127326 ** Implementation of xSync() method. Flush the contents of the pending-terms
127327 ** hash-table to the database.
127328 */
127329 static int fts3SyncMethod(sqlite3_vtab *pVtab){
127330 
127331   /* Following an incremental-merge operation, assuming that the input
127332   ** segments are not completely consumed (the usual case), they are updated
127333   ** in place to remove the entries that have already been merged. This
127334   ** involves updating the leaf block that contains the smallest unmerged
127335   ** entry and each block (if any) between the leaf and the root node. So
127336   ** if the height of the input segment b-trees is N, and input segments
127337   ** are merged eight at a time, updating the input segments at the end
127338   ** of an incremental-merge requires writing (8*(1+N)) blocks. N is usually
127339   ** small - often between 0 and 2. So the overhead of the incremental
127340   ** merge is somewhere between 8 and 24 blocks. To avoid this overhead
127341   ** dwarfing the actual productive work accomplished, the incremental merge
127342   ** is only attempted if it will write at least 64 leaf blocks. Hence
127343   ** nMinMerge.
127344   **
127345   ** Of course, updating the input segments also involves deleting a bunch
127346   ** of blocks from the segments table. But this is not considered overhead
127347   ** as it would also be required by a crisis-merge that used the same input
127348   ** segments.
127349   */
127350   const u32 nMinMerge = 64;       /* Minimum amount of incr-merge work to do */
127351 
127352   Fts3Table *p = (Fts3Table*)pVtab;
127353   int rc = sqlite3Fts3PendingTermsFlush(p);
127354 
127355   if( rc==SQLITE_OK && p->bAutoincrmerge==1 && p->nLeafAdd>(nMinMerge/16) ){
127356     int mxLevel = 0;              /* Maximum relative level value in db */
127357     int A;                        /* Incr-merge parameter A */
127358 
127359     rc = sqlite3Fts3MaxLevel(p, &mxLevel);
127360     assert( rc==SQLITE_OK || mxLevel==0 );
127361     A = p->nLeafAdd * mxLevel;
127362     A += (A/2);
127363     if( A>(int)nMinMerge ) rc = sqlite3Fts3Incrmerge(p, A, 8);
127364   }
127365   sqlite3Fts3SegmentsClose(p);
127366   return rc;
127367 }
127368 
127369 /*
127370 ** Implementation of xBegin() method. This is a no-op.
127371 */
127372 static int fts3BeginMethod(sqlite3_vtab *pVtab){
127373   Fts3Table *p = (Fts3Table*)pVtab;
127374   UNUSED_PARAMETER(pVtab);
127375   assert( p->pSegments==0 );
127376   assert( p->nPendingData==0 );
127377   assert( p->inTransaction!=1 );
127378   TESTONLY( p->inTransaction = 1 );
127379   TESTONLY( p->mxSavepoint = -1; );
127380   p->nLeafAdd = 0;
127381   return SQLITE_OK;
127382 }
127383 
127384 /*
127385 ** Implementation of xCommit() method. This is a no-op. The contents of
127386 ** the pending-terms hash-table have already been flushed into the database
127387 ** by fts3SyncMethod().
127388 */
127389 static int fts3CommitMethod(sqlite3_vtab *pVtab){
127390   TESTONLY( Fts3Table *p = (Fts3Table*)pVtab );
127391   UNUSED_PARAMETER(pVtab);
127392   assert( p->nPendingData==0 );
127393   assert( p->inTransaction!=0 );
127394   assert( p->pSegments==0 );
127395   TESTONLY( p->inTransaction = 0 );
127396   TESTONLY( p->mxSavepoint = -1; );
127397   return SQLITE_OK;
127398 }
127399 
127400 /*
127401 ** Implementation of xRollback(). Discard the contents of the pending-terms
127402 ** hash-table. Any changes made to the database are reverted by SQLite.
127403 */
127404 static int fts3RollbackMethod(sqlite3_vtab *pVtab){
127405   Fts3Table *p = (Fts3Table*)pVtab;
127406   sqlite3Fts3PendingTermsClear(p);
127407   assert( p->inTransaction!=0 );
127408   TESTONLY( p->inTransaction = 0 );
127409   TESTONLY( p->mxSavepoint = -1; );
127410   return SQLITE_OK;
127411 }
127412 
127413 /*
127414 ** When called, *ppPoslist must point to the byte immediately following the
127415 ** end of a position-list. i.e. ( (*ppPoslist)[-1]==POS_END ). This function
127416 ** moves *ppPoslist so that it instead points to the first byte of the
127417 ** same position list.
127418 */
127419 static void fts3ReversePoslist(char *pStart, char **ppPoslist){
127420   char *p = &(*ppPoslist)[-2];
127421   char c = 0;
127422 
127423   while( p>pStart && (c=*p--)==0 );
127424   while( p>pStart && (*p & 0x80) | c ){
127425     c = *p--;
127426   }
127427   if( p>pStart ){ p = &p[2]; }
127428   while( *p++&0x80 );
127429   *ppPoslist = p;
127430 }
127431 
127432 /*
127433 ** Helper function used by the implementation of the overloaded snippet(),
127434 ** offsets() and optimize() SQL functions.
127435 **
127436 ** If the value passed as the third argument is a blob of size
127437 ** sizeof(Fts3Cursor*), then the blob contents are copied to the
127438 ** output variable *ppCsr and SQLITE_OK is returned. Otherwise, an error
127439 ** message is written to context pContext and SQLITE_ERROR returned. The
127440 ** string passed via zFunc is used as part of the error message.
127441 */
127442 static int fts3FunctionArg(
127443   sqlite3_context *pContext,      /* SQL function call context */
127444   const char *zFunc,              /* Function name */
127445   sqlite3_value *pVal,            /* argv[0] passed to function */
127446   Fts3Cursor **ppCsr              /* OUT: Store cursor handle here */
127447 ){
127448   Fts3Cursor *pRet;
127449   if( sqlite3_value_type(pVal)!=SQLITE_BLOB
127450    || sqlite3_value_bytes(pVal)!=sizeof(Fts3Cursor *)
127451   ){
127452     char *zErr = sqlite3_mprintf("illegal first argument to %s", zFunc);
127453     sqlite3_result_error(pContext, zErr, -1);
127454     sqlite3_free(zErr);
127455     return SQLITE_ERROR;
127456   }
127457   memcpy(&pRet, sqlite3_value_blob(pVal), sizeof(Fts3Cursor *));
127458   *ppCsr = pRet;
127459   return SQLITE_OK;
127460 }
127461 
127462 /*
127463 ** Implementation of the snippet() function for FTS3
127464 */
127465 static void fts3SnippetFunc(
127466   sqlite3_context *pContext,      /* SQLite function call context */
127467   int nVal,                       /* Size of apVal[] array */
127468   sqlite3_value **apVal           /* Array of arguments */
127469 ){
127470   Fts3Cursor *pCsr;               /* Cursor handle passed through apVal[0] */
127471   const char *zStart = "<b>";
127472   const char *zEnd = "</b>";
127473   const char *zEllipsis = "<b>...</b>";
127474   int iCol = -1;
127475   int nToken = 15;                /* Default number of tokens in snippet */
127476 
127477   /* There must be at least one argument passed to this function (otherwise
127478   ** the non-overloaded version would have been called instead of this one).
127479   */
127480   assert( nVal>=1 );
127481 
127482   if( nVal>6 ){
127483     sqlite3_result_error(pContext,
127484         "wrong number of arguments to function snippet()", -1);
127485     return;
127486   }
127487   if( fts3FunctionArg(pContext, "snippet", apVal[0], &pCsr) ) return;
127488 
127489   switch( nVal ){
127490     case 6: nToken = sqlite3_value_int(apVal[5]);
127491     case 5: iCol = sqlite3_value_int(apVal[4]);
127492     case 4: zEllipsis = (const char*)sqlite3_value_text(apVal[3]);
127493     case 3: zEnd = (const char*)sqlite3_value_text(apVal[2]);
127494     case 2: zStart = (const char*)sqlite3_value_text(apVal[1]);
127495   }
127496   if( !zEllipsis || !zEnd || !zStart ){
127497     sqlite3_result_error_nomem(pContext);
127498   }else if( SQLITE_OK==fts3CursorSeek(pContext, pCsr) ){
127499     sqlite3Fts3Snippet(pContext, pCsr, zStart, zEnd, zEllipsis, iCol, nToken);
127500   }
127501 }
127502 
127503 /*
127504 ** Implementation of the offsets() function for FTS3
127505 */
127506 static void fts3OffsetsFunc(
127507   sqlite3_context *pContext,      /* SQLite function call context */
127508   int nVal,                       /* Size of argument array */
127509   sqlite3_value **apVal           /* Array of arguments */
127510 ){
127511   Fts3Cursor *pCsr;               /* Cursor handle passed through apVal[0] */
127512 
127513   UNUSED_PARAMETER(nVal);
127514 
127515   assert( nVal==1 );
127516   if( fts3FunctionArg(pContext, "offsets", apVal[0], &pCsr) ) return;
127517   assert( pCsr );
127518   if( SQLITE_OK==fts3CursorSeek(pContext, pCsr) ){
127519     sqlite3Fts3Offsets(pContext, pCsr);
127520   }
127521 }
127522 
127523 /*
127524 ** Implementation of the special optimize() function for FTS3. This
127525 ** function merges all segments in the database to a single segment.
127526 ** Example usage is:
127527 **
127528 **   SELECT optimize(t) FROM t LIMIT 1;
127529 **
127530 ** where 't' is the name of an FTS3 table.
127531 */
127532 static void fts3OptimizeFunc(
127533   sqlite3_context *pContext,      /* SQLite function call context */
127534   int nVal,                       /* Size of argument array */
127535   sqlite3_value **apVal           /* Array of arguments */
127536 ){
127537   int rc;                         /* Return code */
127538   Fts3Table *p;                   /* Virtual table handle */
127539   Fts3Cursor *pCursor;            /* Cursor handle passed through apVal[0] */
127540 
127541   UNUSED_PARAMETER(nVal);
127542 
127543   assert( nVal==1 );
127544   if( fts3FunctionArg(pContext, "optimize", apVal[0], &pCursor) ) return;
127545   p = (Fts3Table *)pCursor->base.pVtab;
127546   assert( p );
127547 
127548   rc = sqlite3Fts3Optimize(p);
127549 
127550   switch( rc ){
127551     case SQLITE_OK:
127552       sqlite3_result_text(pContext, "Index optimized", -1, SQLITE_STATIC);
127553       break;
127554     case SQLITE_DONE:
127555       sqlite3_result_text(pContext, "Index already optimal", -1, SQLITE_STATIC);
127556       break;
127557     default:
127558       sqlite3_result_error_code(pContext, rc);
127559       break;
127560   }
127561 }
127562 
127563 /*
127564 ** Implementation of the matchinfo() function for FTS3
127565 */
127566 static void fts3MatchinfoFunc(
127567   sqlite3_context *pContext,      /* SQLite function call context */
127568   int nVal,                       /* Size of argument array */
127569   sqlite3_value **apVal           /* Array of arguments */
127570 ){
127571   Fts3Cursor *pCsr;               /* Cursor handle passed through apVal[0] */
127572   assert( nVal==1 || nVal==2 );
127573   if( SQLITE_OK==fts3FunctionArg(pContext, "matchinfo", apVal[0], &pCsr) ){
127574     const char *zArg = 0;
127575     if( nVal>1 ){
127576       zArg = (const char *)sqlite3_value_text(apVal[1]);
127577     }
127578     sqlite3Fts3Matchinfo(pContext, pCsr, zArg);
127579   }
127580 }
127581 
127582 /*
127583 ** This routine implements the xFindFunction method for the FTS3
127584 ** virtual table.
127585 */
127586 static int fts3FindFunctionMethod(
127587   sqlite3_vtab *pVtab,            /* Virtual table handle */
127588   int nArg,                       /* Number of SQL function arguments */
127589   const char *zName,              /* Name of SQL function */
127590   void (**pxFunc)(sqlite3_context*,int,sqlite3_value**), /* OUT: Result */
127591   void **ppArg                    /* Unused */
127592 ){
127593   struct Overloaded {
127594     const char *zName;
127595     void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
127596   } aOverload[] = {
127597     { "snippet", fts3SnippetFunc },
127598     { "offsets", fts3OffsetsFunc },
127599     { "optimize", fts3OptimizeFunc },
127600     { "matchinfo", fts3MatchinfoFunc },
127601   };
127602   int i;                          /* Iterator variable */
127603 
127604   UNUSED_PARAMETER(pVtab);
127605   UNUSED_PARAMETER(nArg);
127606   UNUSED_PARAMETER(ppArg);
127607 
127608   for(i=0; i<SizeofArray(aOverload); i++){
127609     if( strcmp(zName, aOverload[i].zName)==0 ){
127610       *pxFunc = aOverload[i].xFunc;
127611       return 1;
127612     }
127613   }
127614 
127615   /* No function of the specified name was found. Return 0. */
127616   return 0;
127617 }
127618 
127619 /*
127620 ** Implementation of FTS3 xRename method. Rename an fts3 table.
127621 */
127622 static int fts3RenameMethod(
127623   sqlite3_vtab *pVtab,            /* Virtual table handle */
127624   const char *zName               /* New name of table */
127625 ){
127626   Fts3Table *p = (Fts3Table *)pVtab;
127627   sqlite3 *db = p->db;            /* Database connection */
127628   int rc;                         /* Return Code */
127629 
127630   /* As it happens, the pending terms table is always empty here. This is
127631   ** because an "ALTER TABLE RENAME TABLE" statement inside a transaction
127632   ** always opens a savepoint transaction. And the xSavepoint() method
127633   ** flushes the pending terms table. But leave the (no-op) call to
127634   ** PendingTermsFlush() in in case that changes.
127635   */
127636   assert( p->nPendingData==0 );
127637   rc = sqlite3Fts3PendingTermsFlush(p);
127638 
127639   if( p->zContentTbl==0 ){
127640     fts3DbExec(&rc, db,
127641       "ALTER TABLE %Q.'%q_content'  RENAME TO '%q_content';",
127642       p->zDb, p->zName, zName
127643     );
127644   }
127645 
127646   if( p->bHasDocsize ){
127647     fts3DbExec(&rc, db,
127648       "ALTER TABLE %Q.'%q_docsize'  RENAME TO '%q_docsize';",
127649       p->zDb, p->zName, zName
127650     );
127651   }
127652   if( p->bHasStat ){
127653     fts3DbExec(&rc, db,
127654       "ALTER TABLE %Q.'%q_stat'  RENAME TO '%q_stat';",
127655       p->zDb, p->zName, zName
127656     );
127657   }
127658   fts3DbExec(&rc, db,
127659     "ALTER TABLE %Q.'%q_segments' RENAME TO '%q_segments';",
127660     p->zDb, p->zName, zName
127661   );
127662   fts3DbExec(&rc, db,
127663     "ALTER TABLE %Q.'%q_segdir'   RENAME TO '%q_segdir';",
127664     p->zDb, p->zName, zName
127665   );
127666   return rc;
127667 }
127668 
127669 /*
127670 ** The xSavepoint() method.
127671 **
127672 ** Flush the contents of the pending-terms table to disk.
127673 */
127674 static int fts3SavepointMethod(sqlite3_vtab *pVtab, int iSavepoint){
127675   int rc = SQLITE_OK;
127676   UNUSED_PARAMETER(iSavepoint);
127677   assert( ((Fts3Table *)pVtab)->inTransaction );
127678   assert( ((Fts3Table *)pVtab)->mxSavepoint < iSavepoint );
127679   TESTONLY( ((Fts3Table *)pVtab)->mxSavepoint = iSavepoint );
127680   if( ((Fts3Table *)pVtab)->bIgnoreSavepoint==0 ){
127681     rc = fts3SyncMethod(pVtab);
127682   }
127683   return rc;
127684 }
127685 
127686 /*
127687 ** The xRelease() method.
127688 **
127689 ** This is a no-op.
127690 */
127691 static int fts3ReleaseMethod(sqlite3_vtab *pVtab, int iSavepoint){
127692   TESTONLY( Fts3Table *p = (Fts3Table*)pVtab );
127693   UNUSED_PARAMETER(iSavepoint);
127694   UNUSED_PARAMETER(pVtab);
127695   assert( p->inTransaction );
127696   assert( p->mxSavepoint >= iSavepoint );
127697   TESTONLY( p->mxSavepoint = iSavepoint-1 );
127698   return SQLITE_OK;
127699 }
127700 
127701 /*
127702 ** The xRollbackTo() method.
127703 **
127704 ** Discard the contents of the pending terms table.
127705 */
127706 static int fts3RollbackToMethod(sqlite3_vtab *pVtab, int iSavepoint){
127707   Fts3Table *p = (Fts3Table*)pVtab;
127708   UNUSED_PARAMETER(iSavepoint);
127709   assert( p->inTransaction );
127710   assert( p->mxSavepoint >= iSavepoint );
127711   TESTONLY( p->mxSavepoint = iSavepoint );
127712   sqlite3Fts3PendingTermsClear(p);
127713   return SQLITE_OK;
127714 }
127715 
127716 static const sqlite3_module fts3Module = {
127717   /* iVersion      */ 2,
127718   /* xCreate       */ fts3CreateMethod,
127719   /* xConnect      */ fts3ConnectMethod,
127720   /* xBestIndex    */ fts3BestIndexMethod,
127721   /* xDisconnect   */ fts3DisconnectMethod,
127722   /* xDestroy      */ fts3DestroyMethod,
127723   /* xOpen         */ fts3OpenMethod,
127724   /* xClose        */ fts3CloseMethod,
127725   /* xFilter       */ fts3FilterMethod,
127726   /* xNext         */ fts3NextMethod,
127727   /* xEof          */ fts3EofMethod,
127728   /* xColumn       */ fts3ColumnMethod,
127729   /* xRowid        */ fts3RowidMethod,
127730   /* xUpdate       */ fts3UpdateMethod,
127731   /* xBegin        */ fts3BeginMethod,
127732   /* xSync         */ fts3SyncMethod,
127733   /* xCommit       */ fts3CommitMethod,
127734   /* xRollback     */ fts3RollbackMethod,
127735   /* xFindFunction */ fts3FindFunctionMethod,
127736   /* xRename */       fts3RenameMethod,
127737   /* xSavepoint    */ fts3SavepointMethod,
127738   /* xRelease      */ fts3ReleaseMethod,
127739   /* xRollbackTo   */ fts3RollbackToMethod,
127740 };
127741 
127742 /*
127743 ** This function is registered as the module destructor (called when an
127744 ** FTS3 enabled database connection is closed). It frees the memory
127745 ** allocated for the tokenizer hash table.
127746 */
127747 static void hashDestroy(void *p){
127748   Fts3Hash *pHash = (Fts3Hash *)p;
127749   sqlite3Fts3HashClear(pHash);
127750   sqlite3_free(pHash);
127751 }
127752 
127753 /*
127754 ** The fts3 built-in tokenizers - "simple", "porter" and "icu"- are
127755 ** implemented in files fts3_tokenizer1.c, fts3_porter.c and fts3_icu.c
127756 ** respectively. The following three forward declarations are for functions
127757 ** declared in these files used to retrieve the respective implementations.
127758 **
127759 ** Calling sqlite3Fts3SimpleTokenizerModule() sets the value pointed
127760 ** to by the argument to point to the "simple" tokenizer implementation.
127761 ** And so on.
127762 */
127763 SQLITE_PRIVATE void sqlite3Fts3SimpleTokenizerModule(sqlite3_tokenizer_module const**ppModule);
127764 SQLITE_PRIVATE void sqlite3Fts3PorterTokenizerModule(sqlite3_tokenizer_module const**ppModule);
127765 #ifdef SQLITE_ENABLE_FTS4_UNICODE61
127766 SQLITE_PRIVATE void sqlite3Fts3UnicodeTokenizer(sqlite3_tokenizer_module const**ppModule);
127767 #endif
127768 #ifdef SQLITE_ENABLE_ICU
127769 SQLITE_PRIVATE void sqlite3Fts3IcuTokenizerModule(sqlite3_tokenizer_module const**ppModule);
127770 #endif
127771 
127772 /*
127773 ** Initialize the fts3 extension. If this extension is built as part
127774 ** of the sqlite library, then this function is called directly by
127775 ** SQLite. If fts3 is built as a dynamically loadable extension, this
127776 ** function is called by the sqlite3_extension_init() entry point.
127777 */
127778 SQLITE_PRIVATE int sqlite3Fts3Init(sqlite3 *db){
127779   int rc = SQLITE_OK;
127780   Fts3Hash *pHash = 0;
127781   const sqlite3_tokenizer_module *pSimple = 0;
127782   const sqlite3_tokenizer_module *pPorter = 0;
127783 #ifdef SQLITE_ENABLE_FTS4_UNICODE61
127784   const sqlite3_tokenizer_module *pUnicode = 0;
127785 #endif
127786 
127787 #ifdef SQLITE_ENABLE_ICU
127788   const sqlite3_tokenizer_module *pIcu = 0;
127789   sqlite3Fts3IcuTokenizerModule(&pIcu);
127790 #endif
127791 
127792 #ifdef SQLITE_ENABLE_FTS4_UNICODE61
127793   sqlite3Fts3UnicodeTokenizer(&pUnicode);
127794 #endif
127795 
127796 #ifdef SQLITE_TEST
127797   rc = sqlite3Fts3InitTerm(db);
127798   if( rc!=SQLITE_OK ) return rc;
127799 #endif
127800 
127801   rc = sqlite3Fts3InitAux(db);
127802   if( rc!=SQLITE_OK ) return rc;
127803 
127804   sqlite3Fts3SimpleTokenizerModule(&pSimple);
127805   sqlite3Fts3PorterTokenizerModule(&pPorter);
127806 
127807   /* Allocate and initialize the hash-table used to store tokenizers. */
127808   pHash = sqlite3_malloc(sizeof(Fts3Hash));
127809   if( !pHash ){
127810     rc = SQLITE_NOMEM;
127811   }else{
127812     sqlite3Fts3HashInit(pHash, FTS3_HASH_STRING, 1);
127813   }
127814 
127815   /* Load the built-in tokenizers into the hash table */
127816   if( rc==SQLITE_OK ){
127817     if( sqlite3Fts3HashInsert(pHash, "simple", 7, (void *)pSimple)
127818      || sqlite3Fts3HashInsert(pHash, "porter", 7, (void *)pPorter)
127819 
127820 #ifdef SQLITE_ENABLE_FTS4_UNICODE61
127821      || sqlite3Fts3HashInsert(pHash, "unicode61", 10, (void *)pUnicode)
127822 #endif
127823 #ifdef SQLITE_ENABLE_ICU
127824      || (pIcu && sqlite3Fts3HashInsert(pHash, "icu", 4, (void *)pIcu))
127825 #endif
127826     ){
127827       rc = SQLITE_NOMEM;
127828     }
127829   }
127830 
127831 #ifdef SQLITE_TEST
127832   if( rc==SQLITE_OK ){
127833     rc = sqlite3Fts3ExprInitTestInterface(db);
127834   }
127835 #endif
127836 
127837   /* Create the virtual table wrapper around the hash-table and overload
127838   ** the two scalar functions. If this is successful, register the
127839   ** module with sqlite.
127840   */
127841   if( SQLITE_OK==rc
127842    && SQLITE_OK==(rc = sqlite3Fts3InitHashTable(db, pHash, "fts3_tokenizer"))
127843    && SQLITE_OK==(rc = sqlite3_overload_function(db, "snippet", -1))
127844    && SQLITE_OK==(rc = sqlite3_overload_function(db, "offsets", 1))
127845    && SQLITE_OK==(rc = sqlite3_overload_function(db, "matchinfo", 1))
127846    && SQLITE_OK==(rc = sqlite3_overload_function(db, "matchinfo", 2))
127847    && SQLITE_OK==(rc = sqlite3_overload_function(db, "optimize", 1))
127848   ){
127849     rc = sqlite3_create_module_v2(
127850         db, "fts3", &fts3Module, (void *)pHash, hashDestroy
127851     );
127852     if( rc==SQLITE_OK ){
127853       rc = sqlite3_create_module_v2(
127854           db, "fts4", &fts3Module, (void *)pHash, 0
127855       );
127856     }
127857     if( rc==SQLITE_OK ){
127858       rc = sqlite3Fts3InitTok(db, (void *)pHash);
127859     }
127860     return rc;
127861   }
127862 
127863 
127864   /* An error has occurred. Delete the hash table and return the error code. */
127865   assert( rc!=SQLITE_OK );
127866   if( pHash ){
127867     sqlite3Fts3HashClear(pHash);
127868     sqlite3_free(pHash);
127869   }
127870   return rc;
127871 }
127872 
127873 /*
127874 ** Allocate an Fts3MultiSegReader for each token in the expression headed
127875 ** by pExpr.
127876 **
127877 ** An Fts3SegReader object is a cursor that can seek or scan a range of
127878 ** entries within a single segment b-tree. An Fts3MultiSegReader uses multiple
127879 ** Fts3SegReader objects internally to provide an interface to seek or scan
127880 ** within the union of all segments of a b-tree. Hence the name.
127881 **
127882 ** If the allocated Fts3MultiSegReader just seeks to a single entry in a
127883 ** segment b-tree (if the term is not a prefix or it is a prefix for which
127884 ** there exists prefix b-tree of the right length) then it may be traversed
127885 ** and merged incrementally. Otherwise, it has to be merged into an in-memory
127886 ** doclist and then traversed.
127887 */
127888 static void fts3EvalAllocateReaders(
127889   Fts3Cursor *pCsr,               /* FTS cursor handle */
127890   Fts3Expr *pExpr,                /* Allocate readers for this expression */
127891   int *pnToken,                   /* OUT: Total number of tokens in phrase. */
127892   int *pnOr,                      /* OUT: Total number of OR nodes in expr. */
127893   int *pRc                        /* IN/OUT: Error code */
127894 ){
127895   if( pExpr && SQLITE_OK==*pRc ){
127896     if( pExpr->eType==FTSQUERY_PHRASE ){
127897       int i;
127898       int nToken = pExpr->pPhrase->nToken;
127899       *pnToken += nToken;
127900       for(i=0; i<nToken; i++){
127901         Fts3PhraseToken *pToken = &pExpr->pPhrase->aToken[i];
127902         int rc = fts3TermSegReaderCursor(pCsr,
127903             pToken->z, pToken->n, pToken->isPrefix, &pToken->pSegcsr
127904         );
127905         if( rc!=SQLITE_OK ){
127906           *pRc = rc;
127907           return;
127908         }
127909       }
127910       assert( pExpr->pPhrase->iDoclistToken==0 );
127911       pExpr->pPhrase->iDoclistToken = -1;
127912     }else{
127913       *pnOr += (pExpr->eType==FTSQUERY_OR);
127914       fts3EvalAllocateReaders(pCsr, pExpr->pLeft, pnToken, pnOr, pRc);
127915       fts3EvalAllocateReaders(pCsr, pExpr->pRight, pnToken, pnOr, pRc);
127916     }
127917   }
127918 }
127919 
127920 /*
127921 ** Arguments pList/nList contain the doclist for token iToken of phrase p.
127922 ** It is merged into the main doclist stored in p->doclist.aAll/nAll.
127923 **
127924 ** This function assumes that pList points to a buffer allocated using
127925 ** sqlite3_malloc(). This function takes responsibility for eventually
127926 ** freeing the buffer.
127927 */
127928 static void fts3EvalPhraseMergeToken(
127929   Fts3Table *pTab,                /* FTS Table pointer */
127930   Fts3Phrase *p,                  /* Phrase to merge pList/nList into */
127931   int iToken,                     /* Token pList/nList corresponds to */
127932   char *pList,                    /* Pointer to doclist */
127933   int nList                       /* Number of bytes in pList */
127934 ){
127935   assert( iToken!=p->iDoclistToken );
127936 
127937   if( pList==0 ){
127938     sqlite3_free(p->doclist.aAll);
127939     p->doclist.aAll = 0;
127940     p->doclist.nAll = 0;
127941   }
127942 
127943   else if( p->iDoclistToken<0 ){
127944     p->doclist.aAll = pList;
127945     p->doclist.nAll = nList;
127946   }
127947 
127948   else if( p->doclist.aAll==0 ){
127949     sqlite3_free(pList);
127950   }
127951 
127952   else {
127953     char *pLeft;
127954     char *pRight;
127955     int nLeft;
127956     int nRight;
127957     int nDiff;
127958 
127959     if( p->iDoclistToken<iToken ){
127960       pLeft = p->doclist.aAll;
127961       nLeft = p->doclist.nAll;
127962       pRight = pList;
127963       nRight = nList;
127964       nDiff = iToken - p->iDoclistToken;
127965     }else{
127966       pRight = p->doclist.aAll;
127967       nRight = p->doclist.nAll;
127968       pLeft = pList;
127969       nLeft = nList;
127970       nDiff = p->iDoclistToken - iToken;
127971     }
127972 
127973     fts3DoclistPhraseMerge(pTab->bDescIdx, nDiff, pLeft, nLeft, pRight,&nRight);
127974     sqlite3_free(pLeft);
127975     p->doclist.aAll = pRight;
127976     p->doclist.nAll = nRight;
127977   }
127978 
127979   if( iToken>p->iDoclistToken ) p->iDoclistToken = iToken;
127980 }
127981 
127982 /*
127983 ** Load the doclist for phrase p into p->doclist.aAll/nAll. The loaded doclist
127984 ** does not take deferred tokens into account.
127985 **
127986 ** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code.
127987 */
127988 static int fts3EvalPhraseLoad(
127989   Fts3Cursor *pCsr,               /* FTS Cursor handle */
127990   Fts3Phrase *p                   /* Phrase object */
127991 ){
127992   Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
127993   int iToken;
127994   int rc = SQLITE_OK;
127995 
127996   for(iToken=0; rc==SQLITE_OK && iToken<p->nToken; iToken++){
127997     Fts3PhraseToken *pToken = &p->aToken[iToken];
127998     assert( pToken->pDeferred==0 || pToken->pSegcsr==0 );
127999 
128000     if( pToken->pSegcsr ){
128001       int nThis = 0;
128002       char *pThis = 0;
128003       rc = fts3TermSelect(pTab, pToken, p->iColumn, &nThis, &pThis);
128004       if( rc==SQLITE_OK ){
128005         fts3EvalPhraseMergeToken(pTab, p, iToken, pThis, nThis);
128006       }
128007     }
128008     assert( pToken->pSegcsr==0 );
128009   }
128010 
128011   return rc;
128012 }
128013 
128014 /*
128015 ** This function is called on each phrase after the position lists for
128016 ** any deferred tokens have been loaded into memory. It updates the phrases
128017 ** current position list to include only those positions that are really
128018 ** instances of the phrase (after considering deferred tokens). If this
128019 ** means that the phrase does not appear in the current row, doclist.pList
128020 ** and doclist.nList are both zeroed.
128021 **
128022 ** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code.
128023 */
128024 static int fts3EvalDeferredPhrase(Fts3Cursor *pCsr, Fts3Phrase *pPhrase){
128025   int iToken;                     /* Used to iterate through phrase tokens */
128026   char *aPoslist = 0;             /* Position list for deferred tokens */
128027   int nPoslist = 0;               /* Number of bytes in aPoslist */
128028   int iPrev = -1;                 /* Token number of previous deferred token */
128029 
128030   assert( pPhrase->doclist.bFreeList==0 );
128031 
128032   for(iToken=0; iToken<pPhrase->nToken; iToken++){
128033     Fts3PhraseToken *pToken = &pPhrase->aToken[iToken];
128034     Fts3DeferredToken *pDeferred = pToken->pDeferred;
128035 
128036     if( pDeferred ){
128037       char *pList;
128038       int nList;
128039       int rc = sqlite3Fts3DeferredTokenList(pDeferred, &pList, &nList);
128040       if( rc!=SQLITE_OK ) return rc;
128041 
128042       if( pList==0 ){
128043         sqlite3_free(aPoslist);
128044         pPhrase->doclist.pList = 0;
128045         pPhrase->doclist.nList = 0;
128046         return SQLITE_OK;
128047 
128048       }else if( aPoslist==0 ){
128049         aPoslist = pList;
128050         nPoslist = nList;
128051 
128052       }else{
128053         char *aOut = pList;
128054         char *p1 = aPoslist;
128055         char *p2 = aOut;
128056 
128057         assert( iPrev>=0 );
128058         fts3PoslistPhraseMerge(&aOut, iToken-iPrev, 0, 1, &p1, &p2);
128059         sqlite3_free(aPoslist);
128060         aPoslist = pList;
128061         nPoslist = (int)(aOut - aPoslist);
128062         if( nPoslist==0 ){
128063           sqlite3_free(aPoslist);
128064           pPhrase->doclist.pList = 0;
128065           pPhrase->doclist.nList = 0;
128066           return SQLITE_OK;
128067         }
128068       }
128069       iPrev = iToken;
128070     }
128071   }
128072 
128073   if( iPrev>=0 ){
128074     int nMaxUndeferred = pPhrase->iDoclistToken;
128075     if( nMaxUndeferred<0 ){
128076       pPhrase->doclist.pList = aPoslist;
128077       pPhrase->doclist.nList = nPoslist;
128078       pPhrase->doclist.iDocid = pCsr->iPrevId;
128079       pPhrase->doclist.bFreeList = 1;
128080     }else{
128081       int nDistance;
128082       char *p1;
128083       char *p2;
128084       char *aOut;
128085 
128086       if( nMaxUndeferred>iPrev ){
128087         p1 = aPoslist;
128088         p2 = pPhrase->doclist.pList;
128089         nDistance = nMaxUndeferred - iPrev;
128090       }else{
128091         p1 = pPhrase->doclist.pList;
128092         p2 = aPoslist;
128093         nDistance = iPrev - nMaxUndeferred;
128094       }
128095 
128096       aOut = (char *)sqlite3_malloc(nPoslist+8);
128097       if( !aOut ){
128098         sqlite3_free(aPoslist);
128099         return SQLITE_NOMEM;
128100       }
128101 
128102       pPhrase->doclist.pList = aOut;
128103       if( fts3PoslistPhraseMerge(&aOut, nDistance, 0, 1, &p1, &p2) ){
128104         pPhrase->doclist.bFreeList = 1;
128105         pPhrase->doclist.nList = (int)(aOut - pPhrase->doclist.pList);
128106       }else{
128107         sqlite3_free(aOut);
128108         pPhrase->doclist.pList = 0;
128109         pPhrase->doclist.nList = 0;
128110       }
128111       sqlite3_free(aPoslist);
128112     }
128113   }
128114 
128115   return SQLITE_OK;
128116 }
128117 
128118 /*
128119 ** Maximum number of tokens a phrase may have to be considered for the
128120 ** incremental doclists strategy.
128121 */
128122 #define MAX_INCR_PHRASE_TOKENS 4
128123 
128124 /*
128125 ** This function is called for each Fts3Phrase in a full-text query
128126 ** expression to initialize the mechanism for returning rows. Once this
128127 ** function has been called successfully on an Fts3Phrase, it may be
128128 ** used with fts3EvalPhraseNext() to iterate through the matching docids.
128129 **
128130 ** If parameter bOptOk is true, then the phrase may (or may not) use the
128131 ** incremental loading strategy. Otherwise, the entire doclist is loaded into
128132 ** memory within this call.
128133 **
128134 ** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code.
128135 */
128136 static int fts3EvalPhraseStart(Fts3Cursor *pCsr, int bOptOk, Fts3Phrase *p){
128137   Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
128138   int rc = SQLITE_OK;             /* Error code */
128139   int i;
128140 
128141   /* Determine if doclists may be loaded from disk incrementally. This is
128142   ** possible if the bOptOk argument is true, the FTS doclists will be
128143   ** scanned in forward order, and the phrase consists of
128144   ** MAX_INCR_PHRASE_TOKENS or fewer tokens, none of which are are "^first"
128145   ** tokens or prefix tokens that cannot use a prefix-index.  */
128146   int bHaveIncr = 0;
128147   int bIncrOk = (bOptOk
128148    && pCsr->bDesc==pTab->bDescIdx
128149    && p->nToken<=MAX_INCR_PHRASE_TOKENS && p->nToken>0
128150    && p->nToken<=MAX_INCR_PHRASE_TOKENS && p->nToken>0
128151 #ifdef SQLITE_TEST
128152    && pTab->bNoIncrDoclist==0
128153 #endif
128154   );
128155   for(i=0; bIncrOk==1 && i<p->nToken; i++){
128156     Fts3PhraseToken *pToken = &p->aToken[i];
128157     if( pToken->bFirst || (pToken->pSegcsr!=0 && !pToken->pSegcsr->bLookup) ){
128158       bIncrOk = 0;
128159     }
128160     if( pToken->pSegcsr ) bHaveIncr = 1;
128161   }
128162 
128163   if( bIncrOk && bHaveIncr ){
128164     /* Use the incremental approach. */
128165     int iCol = (p->iColumn >= pTab->nColumn ? -1 : p->iColumn);
128166     for(i=0; rc==SQLITE_OK && i<p->nToken; i++){
128167       Fts3PhraseToken *pToken = &p->aToken[i];
128168       Fts3MultiSegReader *pSegcsr = pToken->pSegcsr;
128169       if( pSegcsr ){
128170         rc = sqlite3Fts3MsrIncrStart(pTab, pSegcsr, iCol, pToken->z, pToken->n);
128171       }
128172     }
128173     p->bIncr = 1;
128174   }else{
128175     /* Load the full doclist for the phrase into memory. */
128176     rc = fts3EvalPhraseLoad(pCsr, p);
128177     p->bIncr = 0;
128178   }
128179 
128180   assert( rc!=SQLITE_OK || p->nToken<1 || p->aToken[0].pSegcsr==0 || p->bIncr );
128181   return rc;
128182 }
128183 
128184 /*
128185 ** This function is used to iterate backwards (from the end to start)
128186 ** through doclists. It is used by this module to iterate through phrase
128187 ** doclists in reverse and by the fts3_write.c module to iterate through
128188 ** pending-terms lists when writing to databases with "order=desc".
128189 **
128190 ** The doclist may be sorted in ascending (parameter bDescIdx==0) or
128191 ** descending (parameter bDescIdx==1) order of docid. Regardless, this
128192 ** function iterates from the end of the doclist to the beginning.
128193 */
128194 SQLITE_PRIVATE void sqlite3Fts3DoclistPrev(
128195   int bDescIdx,                   /* True if the doclist is desc */
128196   char *aDoclist,                 /* Pointer to entire doclist */
128197   int nDoclist,                   /* Length of aDoclist in bytes */
128198   char **ppIter,                  /* IN/OUT: Iterator pointer */
128199   sqlite3_int64 *piDocid,         /* IN/OUT: Docid pointer */
128200   int *pnList,                    /* OUT: List length pointer */
128201   u8 *pbEof                       /* OUT: End-of-file flag */
128202 ){
128203   char *p = *ppIter;
128204 
128205   assert( nDoclist>0 );
128206   assert( *pbEof==0 );
128207   assert( p || *piDocid==0 );
128208   assert( !p || (p>aDoclist && p<&aDoclist[nDoclist]) );
128209 
128210   if( p==0 ){
128211     sqlite3_int64 iDocid = 0;
128212     char *pNext = 0;
128213     char *pDocid = aDoclist;
128214     char *pEnd = &aDoclist[nDoclist];
128215     int iMul = 1;
128216 
128217     while( pDocid<pEnd ){
128218       sqlite3_int64 iDelta;
128219       pDocid += sqlite3Fts3GetVarint(pDocid, &iDelta);
128220       iDocid += (iMul * iDelta);
128221       pNext = pDocid;
128222       fts3PoslistCopy(0, &pDocid);
128223       while( pDocid<pEnd && *pDocid==0 ) pDocid++;
128224       iMul = (bDescIdx ? -1 : 1);
128225     }
128226 
128227     *pnList = (int)(pEnd - pNext);
128228     *ppIter = pNext;
128229     *piDocid = iDocid;
128230   }else{
128231     int iMul = (bDescIdx ? -1 : 1);
128232     sqlite3_int64 iDelta;
128233     fts3GetReverseVarint(&p, aDoclist, &iDelta);
128234     *piDocid -= (iMul * iDelta);
128235 
128236     if( p==aDoclist ){
128237       *pbEof = 1;
128238     }else{
128239       char *pSave = p;
128240       fts3ReversePoslist(aDoclist, &p);
128241       *pnList = (int)(pSave - p);
128242     }
128243     *ppIter = p;
128244   }
128245 }
128246 
128247 /*
128248 ** Iterate forwards through a doclist.
128249 */
128250 SQLITE_PRIVATE void sqlite3Fts3DoclistNext(
128251   int bDescIdx,                   /* True if the doclist is desc */
128252   char *aDoclist,                 /* Pointer to entire doclist */
128253   int nDoclist,                   /* Length of aDoclist in bytes */
128254   char **ppIter,                  /* IN/OUT: Iterator pointer */
128255   sqlite3_int64 *piDocid,         /* IN/OUT: Docid pointer */
128256   u8 *pbEof                       /* OUT: End-of-file flag */
128257 ){
128258   char *p = *ppIter;
128259 
128260   assert( nDoclist>0 );
128261   assert( *pbEof==0 );
128262   assert( p || *piDocid==0 );
128263   assert( !p || (p>=aDoclist && p<=&aDoclist[nDoclist]) );
128264 
128265   if( p==0 ){
128266     p = aDoclist;
128267     p += sqlite3Fts3GetVarint(p, piDocid);
128268   }else{
128269     fts3PoslistCopy(0, &p);
128270     if( p>=&aDoclist[nDoclist] ){
128271       *pbEof = 1;
128272     }else{
128273       sqlite3_int64 iVar;
128274       p += sqlite3Fts3GetVarint(p, &iVar);
128275       *piDocid += ((bDescIdx ? -1 : 1) * iVar);
128276     }
128277   }
128278 
128279   *ppIter = p;
128280 }
128281 
128282 /*
128283 ** Advance the iterator pDL to the next entry in pDL->aAll/nAll. Set *pbEof
128284 ** to true if EOF is reached.
128285 */
128286 static void fts3EvalDlPhraseNext(
128287   Fts3Table *pTab,
128288   Fts3Doclist *pDL,
128289   u8 *pbEof
128290 ){
128291   char *pIter;                            /* Used to iterate through aAll */
128292   char *pEnd = &pDL->aAll[pDL->nAll];     /* 1 byte past end of aAll */
128293 
128294   if( pDL->pNextDocid ){
128295     pIter = pDL->pNextDocid;
128296   }else{
128297     pIter = pDL->aAll;
128298   }
128299 
128300   if( pIter>=pEnd ){
128301     /* We have already reached the end of this doclist. EOF. */
128302     *pbEof = 1;
128303   }else{
128304     sqlite3_int64 iDelta;
128305     pIter += sqlite3Fts3GetVarint(pIter, &iDelta);
128306     if( pTab->bDescIdx==0 || pDL->pNextDocid==0 ){
128307       pDL->iDocid += iDelta;
128308     }else{
128309       pDL->iDocid -= iDelta;
128310     }
128311     pDL->pList = pIter;
128312     fts3PoslistCopy(0, &pIter);
128313     pDL->nList = (int)(pIter - pDL->pList);
128314 
128315     /* pIter now points just past the 0x00 that terminates the position-
128316     ** list for document pDL->iDocid. However, if this position-list was
128317     ** edited in place by fts3EvalNearTrim(), then pIter may not actually
128318     ** point to the start of the next docid value. The following line deals
128319     ** with this case by advancing pIter past the zero-padding added by
128320     ** fts3EvalNearTrim().  */
128321     while( pIter<pEnd && *pIter==0 ) pIter++;
128322 
128323     pDL->pNextDocid = pIter;
128324     assert( pIter>=&pDL->aAll[pDL->nAll] || *pIter );
128325     *pbEof = 0;
128326   }
128327 }
128328 
128329 /*
128330 ** Helper type used by fts3EvalIncrPhraseNext() and incrPhraseTokenNext().
128331 */
128332 typedef struct TokenDoclist TokenDoclist;
128333 struct TokenDoclist {
128334   int bIgnore;
128335   sqlite3_int64 iDocid;
128336   char *pList;
128337   int nList;
128338 };
128339 
128340 /*
128341 ** Token pToken is an incrementally loaded token that is part of a
128342 ** multi-token phrase. Advance it to the next matching document in the
128343 ** database and populate output variable *p with the details of the new
128344 ** entry. Or, if the iterator has reached EOF, set *pbEof to true.
128345 **
128346 ** If an error occurs, return an SQLite error code. Otherwise, return
128347 ** SQLITE_OK.
128348 */
128349 static int incrPhraseTokenNext(
128350   Fts3Table *pTab,                /* Virtual table handle */
128351   Fts3Phrase *pPhrase,            /* Phrase to advance token of */
128352   int iToken,                     /* Specific token to advance */
128353   TokenDoclist *p,                /* OUT: Docid and doclist for new entry */
128354   u8 *pbEof                       /* OUT: True if iterator is at EOF */
128355 ){
128356   int rc = SQLITE_OK;
128357 
128358   if( pPhrase->iDoclistToken==iToken ){
128359     assert( p->bIgnore==0 );
128360     assert( pPhrase->aToken[iToken].pSegcsr==0 );
128361     fts3EvalDlPhraseNext(pTab, &pPhrase->doclist, pbEof);
128362     p->pList = pPhrase->doclist.pList;
128363     p->nList = pPhrase->doclist.nList;
128364     p->iDocid = pPhrase->doclist.iDocid;
128365   }else{
128366     Fts3PhraseToken *pToken = &pPhrase->aToken[iToken];
128367     assert( pToken->pDeferred==0 );
128368     assert( pToken->pSegcsr || pPhrase->iDoclistToken>=0 );
128369     if( pToken->pSegcsr ){
128370       assert( p->bIgnore==0 );
128371       rc = sqlite3Fts3MsrIncrNext(
128372           pTab, pToken->pSegcsr, &p->iDocid, &p->pList, &p->nList
128373       );
128374       if( p->pList==0 ) *pbEof = 1;
128375     }else{
128376       p->bIgnore = 1;
128377     }
128378   }
128379 
128380   return rc;
128381 }
128382 
128383 
128384 /*
128385 ** The phrase iterator passed as the second argument:
128386 **
128387 **   * features at least one token that uses an incremental doclist, and
128388 **
128389 **   * does not contain any deferred tokens.
128390 **
128391 ** Advance it to the next matching documnent in the database and populate
128392 ** the Fts3Doclist.pList and nList fields.
128393 **
128394 ** If there is no "next" entry and no error occurs, then *pbEof is set to
128395 ** 1 before returning. Otherwise, if no error occurs and the iterator is
128396 ** successfully advanced, *pbEof is set to 0.
128397 **
128398 ** If an error occurs, return an SQLite error code. Otherwise, return
128399 ** SQLITE_OK.
128400 */
128401 static int fts3EvalIncrPhraseNext(
128402   Fts3Cursor *pCsr,               /* FTS Cursor handle */
128403   Fts3Phrase *p,                  /* Phrase object to advance to next docid */
128404   u8 *pbEof                       /* OUT: Set to 1 if EOF */
128405 ){
128406   int rc = SQLITE_OK;
128407   Fts3Doclist *pDL = &p->doclist;
128408   Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
128409   u8 bEof = 0;
128410 
128411   /* This is only called if it is guaranteed that the phrase has at least
128412   ** one incremental token. In which case the bIncr flag is set. */
128413   assert( p->bIncr==1 );
128414 
128415   if( p->nToken==1 && p->bIncr ){
128416     rc = sqlite3Fts3MsrIncrNext(pTab, p->aToken[0].pSegcsr,
128417         &pDL->iDocid, &pDL->pList, &pDL->nList
128418     );
128419     if( pDL->pList==0 ) bEof = 1;
128420   }else{
128421     int bDescDoclist = pCsr->bDesc;
128422     struct TokenDoclist a[MAX_INCR_PHRASE_TOKENS];
128423 
128424     memset(a, 0, sizeof(a));
128425     assert( p->nToken<=MAX_INCR_PHRASE_TOKENS );
128426     assert( p->iDoclistToken<MAX_INCR_PHRASE_TOKENS );
128427 
128428     while( bEof==0 ){
128429       int bMaxSet = 0;
128430       sqlite3_int64 iMax = 0;     /* Largest docid for all iterators */
128431       int i;                      /* Used to iterate through tokens */
128432 
128433       /* Advance the iterator for each token in the phrase once. */
128434       for(i=0; rc==SQLITE_OK && i<p->nToken && bEof==0; i++){
128435         rc = incrPhraseTokenNext(pTab, p, i, &a[i], &bEof);
128436         if( a[i].bIgnore==0 && (bMaxSet==0 || DOCID_CMP(iMax, a[i].iDocid)<0) ){
128437           iMax = a[i].iDocid;
128438           bMaxSet = 1;
128439         }
128440       }
128441       assert( rc!=SQLITE_OK || a[p->nToken-1].bIgnore==0 );
128442       assert( rc!=SQLITE_OK || bMaxSet );
128443 
128444       /* Keep advancing iterators until they all point to the same document */
128445       for(i=0; i<p->nToken; i++){
128446         while( rc==SQLITE_OK && bEof==0
128447             && a[i].bIgnore==0 && DOCID_CMP(a[i].iDocid, iMax)<0
128448         ){
128449           rc = incrPhraseTokenNext(pTab, p, i, &a[i], &bEof);
128450           if( DOCID_CMP(a[i].iDocid, iMax)>0 ){
128451             iMax = a[i].iDocid;
128452             i = 0;
128453           }
128454         }
128455       }
128456 
128457       /* Check if the current entries really are a phrase match */
128458       if( bEof==0 ){
128459         int nList = 0;
128460         int nByte = a[p->nToken-1].nList;
128461         char *aDoclist = sqlite3_malloc(nByte+1);
128462         if( !aDoclist ) return SQLITE_NOMEM;
128463         memcpy(aDoclist, a[p->nToken-1].pList, nByte+1);
128464 
128465         for(i=0; i<(p->nToken-1); i++){
128466           if( a[i].bIgnore==0 ){
128467             char *pL = a[i].pList;
128468             char *pR = aDoclist;
128469             char *pOut = aDoclist;
128470             int nDist = p->nToken-1-i;
128471             int res = fts3PoslistPhraseMerge(&pOut, nDist, 0, 1, &pL, &pR);
128472             if( res==0 ) break;
128473             nList = (int)(pOut - aDoclist);
128474           }
128475         }
128476         if( i==(p->nToken-1) ){
128477           pDL->iDocid = iMax;
128478           pDL->pList = aDoclist;
128479           pDL->nList = nList;
128480           pDL->bFreeList = 1;
128481           break;
128482         }
128483         sqlite3_free(aDoclist);
128484       }
128485     }
128486   }
128487 
128488   *pbEof = bEof;
128489   return rc;
128490 }
128491 
128492 /*
128493 ** Attempt to move the phrase iterator to point to the next matching docid.
128494 ** If an error occurs, return an SQLite error code. Otherwise, return
128495 ** SQLITE_OK.
128496 **
128497 ** If there is no "next" entry and no error occurs, then *pbEof is set to
128498 ** 1 before returning. Otherwise, if no error occurs and the iterator is
128499 ** successfully advanced, *pbEof is set to 0.
128500 */
128501 static int fts3EvalPhraseNext(
128502   Fts3Cursor *pCsr,               /* FTS Cursor handle */
128503   Fts3Phrase *p,                  /* Phrase object to advance to next docid */
128504   u8 *pbEof                       /* OUT: Set to 1 if EOF */
128505 ){
128506   int rc = SQLITE_OK;
128507   Fts3Doclist *pDL = &p->doclist;
128508   Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
128509 
128510   if( p->bIncr ){
128511     rc = fts3EvalIncrPhraseNext(pCsr, p, pbEof);
128512   }else if( pCsr->bDesc!=pTab->bDescIdx && pDL->nAll ){
128513     sqlite3Fts3DoclistPrev(pTab->bDescIdx, pDL->aAll, pDL->nAll,
128514         &pDL->pNextDocid, &pDL->iDocid, &pDL->nList, pbEof
128515     );
128516     pDL->pList = pDL->pNextDocid;
128517   }else{
128518     fts3EvalDlPhraseNext(pTab, pDL, pbEof);
128519   }
128520 
128521   return rc;
128522 }
128523 
128524 /*
128525 **
128526 ** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
128527 ** Otherwise, fts3EvalPhraseStart() is called on all phrases within the
128528 ** expression. Also the Fts3Expr.bDeferred variable is set to true for any
128529 ** expressions for which all descendent tokens are deferred.
128530 **
128531 ** If parameter bOptOk is zero, then it is guaranteed that the
128532 ** Fts3Phrase.doclist.aAll/nAll variables contain the entire doclist for
128533 ** each phrase in the expression (subject to deferred token processing).
128534 ** Or, if bOptOk is non-zero, then one or more tokens within the expression
128535 ** may be loaded incrementally, meaning doclist.aAll/nAll is not available.
128536 **
128537 ** If an error occurs within this function, *pRc is set to an SQLite error
128538 ** code before returning.
128539 */
128540 static void fts3EvalStartReaders(
128541   Fts3Cursor *pCsr,               /* FTS Cursor handle */
128542   Fts3Expr *pExpr,                /* Expression to initialize phrases in */
128543   int *pRc                        /* IN/OUT: Error code */
128544 ){
128545   if( pExpr && SQLITE_OK==*pRc ){
128546     if( pExpr->eType==FTSQUERY_PHRASE ){
128547       int i;
128548       int nToken = pExpr->pPhrase->nToken;
128549       for(i=0; i<nToken; i++){
128550         if( pExpr->pPhrase->aToken[i].pDeferred==0 ) break;
128551       }
128552       pExpr->bDeferred = (i==nToken);
128553       *pRc = fts3EvalPhraseStart(pCsr, 1, pExpr->pPhrase);
128554     }else{
128555       fts3EvalStartReaders(pCsr, pExpr->pLeft, pRc);
128556       fts3EvalStartReaders(pCsr, pExpr->pRight, pRc);
128557       pExpr->bDeferred = (pExpr->pLeft->bDeferred && pExpr->pRight->bDeferred);
128558     }
128559   }
128560 }
128561 
128562 /*
128563 ** An array of the following structures is assembled as part of the process
128564 ** of selecting tokens to defer before the query starts executing (as part
128565 ** of the xFilter() method). There is one element in the array for each
128566 ** token in the FTS expression.
128567 **
128568 ** Tokens are divided into AND/NEAR clusters. All tokens in a cluster belong
128569 ** to phrases that are connected only by AND and NEAR operators (not OR or
128570 ** NOT). When determining tokens to defer, each AND/NEAR cluster is considered
128571 ** separately. The root of a tokens AND/NEAR cluster is stored in
128572 ** Fts3TokenAndCost.pRoot.
128573 */
128574 typedef struct Fts3TokenAndCost Fts3TokenAndCost;
128575 struct Fts3TokenAndCost {
128576   Fts3Phrase *pPhrase;            /* The phrase the token belongs to */
128577   int iToken;                     /* Position of token in phrase */
128578   Fts3PhraseToken *pToken;        /* The token itself */
128579   Fts3Expr *pRoot;                /* Root of NEAR/AND cluster */
128580   int nOvfl;                      /* Number of overflow pages to load doclist */
128581   int iCol;                       /* The column the token must match */
128582 };
128583 
128584 /*
128585 ** This function is used to populate an allocated Fts3TokenAndCost array.
128586 **
128587 ** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
128588 ** Otherwise, if an error occurs during execution, *pRc is set to an
128589 ** SQLite error code.
128590 */
128591 static void fts3EvalTokenCosts(
128592   Fts3Cursor *pCsr,               /* FTS Cursor handle */
128593   Fts3Expr *pRoot,                /* Root of current AND/NEAR cluster */
128594   Fts3Expr *pExpr,                /* Expression to consider */
128595   Fts3TokenAndCost **ppTC,        /* Write new entries to *(*ppTC)++ */
128596   Fts3Expr ***ppOr,               /* Write new OR root to *(*ppOr)++ */
128597   int *pRc                        /* IN/OUT: Error code */
128598 ){
128599   if( *pRc==SQLITE_OK ){
128600     if( pExpr->eType==FTSQUERY_PHRASE ){
128601       Fts3Phrase *pPhrase = pExpr->pPhrase;
128602       int i;
128603       for(i=0; *pRc==SQLITE_OK && i<pPhrase->nToken; i++){
128604         Fts3TokenAndCost *pTC = (*ppTC)++;
128605         pTC->pPhrase = pPhrase;
128606         pTC->iToken = i;
128607         pTC->pRoot = pRoot;
128608         pTC->pToken = &pPhrase->aToken[i];
128609         pTC->iCol = pPhrase->iColumn;
128610         *pRc = sqlite3Fts3MsrOvfl(pCsr, pTC->pToken->pSegcsr, &pTC->nOvfl);
128611       }
128612     }else if( pExpr->eType!=FTSQUERY_NOT ){
128613       assert( pExpr->eType==FTSQUERY_OR
128614            || pExpr->eType==FTSQUERY_AND
128615            || pExpr->eType==FTSQUERY_NEAR
128616       );
128617       assert( pExpr->pLeft && pExpr->pRight );
128618       if( pExpr->eType==FTSQUERY_OR ){
128619         pRoot = pExpr->pLeft;
128620         **ppOr = pRoot;
128621         (*ppOr)++;
128622       }
128623       fts3EvalTokenCosts(pCsr, pRoot, pExpr->pLeft, ppTC, ppOr, pRc);
128624       if( pExpr->eType==FTSQUERY_OR ){
128625         pRoot = pExpr->pRight;
128626         **ppOr = pRoot;
128627         (*ppOr)++;
128628       }
128629       fts3EvalTokenCosts(pCsr, pRoot, pExpr->pRight, ppTC, ppOr, pRc);
128630     }
128631   }
128632 }
128633 
128634 /*
128635 ** Determine the average document (row) size in pages. If successful,
128636 ** write this value to *pnPage and return SQLITE_OK. Otherwise, return
128637 ** an SQLite error code.
128638 **
128639 ** The average document size in pages is calculated by first calculating
128640 ** determining the average size in bytes, B. If B is less than the amount
128641 ** of data that will fit on a single leaf page of an intkey table in
128642 ** this database, then the average docsize is 1. Otherwise, it is 1 plus
128643 ** the number of overflow pages consumed by a record B bytes in size.
128644 */
128645 static int fts3EvalAverageDocsize(Fts3Cursor *pCsr, int *pnPage){
128646   if( pCsr->nRowAvg==0 ){
128647     /* The average document size, which is required to calculate the cost
128648     ** of each doclist, has not yet been determined. Read the required
128649     ** data from the %_stat table to calculate it.
128650     **
128651     ** Entry 0 of the %_stat table is a blob containing (nCol+1) FTS3
128652     ** varints, where nCol is the number of columns in the FTS3 table.
128653     ** The first varint is the number of documents currently stored in
128654     ** the table. The following nCol varints contain the total amount of
128655     ** data stored in all rows of each column of the table, from left
128656     ** to right.
128657     */
128658     int rc;
128659     Fts3Table *p = (Fts3Table*)pCsr->base.pVtab;
128660     sqlite3_stmt *pStmt;
128661     sqlite3_int64 nDoc = 0;
128662     sqlite3_int64 nByte = 0;
128663     const char *pEnd;
128664     const char *a;
128665 
128666     rc = sqlite3Fts3SelectDoctotal(p, &pStmt);
128667     if( rc!=SQLITE_OK ) return rc;
128668     a = sqlite3_column_blob(pStmt, 0);
128669     assert( a );
128670 
128671     pEnd = &a[sqlite3_column_bytes(pStmt, 0)];
128672     a += sqlite3Fts3GetVarint(a, &nDoc);
128673     while( a<pEnd ){
128674       a += sqlite3Fts3GetVarint(a, &nByte);
128675     }
128676     if( nDoc==0 || nByte==0 ){
128677       sqlite3_reset(pStmt);
128678       return FTS_CORRUPT_VTAB;
128679     }
128680 
128681     pCsr->nDoc = nDoc;
128682     pCsr->nRowAvg = (int)(((nByte / nDoc) + p->nPgsz) / p->nPgsz);
128683     assert( pCsr->nRowAvg>0 );
128684     rc = sqlite3_reset(pStmt);
128685     if( rc!=SQLITE_OK ) return rc;
128686   }
128687 
128688   *pnPage = pCsr->nRowAvg;
128689   return SQLITE_OK;
128690 }
128691 
128692 /*
128693 ** This function is called to select the tokens (if any) that will be
128694 ** deferred. The array aTC[] has already been populated when this is
128695 ** called.
128696 **
128697 ** This function is called once for each AND/NEAR cluster in the
128698 ** expression. Each invocation determines which tokens to defer within
128699 ** the cluster with root node pRoot. See comments above the definition
128700 ** of struct Fts3TokenAndCost for more details.
128701 **
128702 ** If no error occurs, SQLITE_OK is returned and sqlite3Fts3DeferToken()
128703 ** called on each token to defer. Otherwise, an SQLite error code is
128704 ** returned.
128705 */
128706 static int fts3EvalSelectDeferred(
128707   Fts3Cursor *pCsr,               /* FTS Cursor handle */
128708   Fts3Expr *pRoot,                /* Consider tokens with this root node */
128709   Fts3TokenAndCost *aTC,          /* Array of expression tokens and costs */
128710   int nTC                         /* Number of entries in aTC[] */
128711 ){
128712   Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
128713   int nDocSize = 0;               /* Number of pages per doc loaded */
128714   int rc = SQLITE_OK;             /* Return code */
128715   int ii;                         /* Iterator variable for various purposes */
128716   int nOvfl = 0;                  /* Total overflow pages used by doclists */
128717   int nToken = 0;                 /* Total number of tokens in cluster */
128718 
128719   int nMinEst = 0;                /* The minimum count for any phrase so far. */
128720   int nLoad4 = 1;                 /* (Phrases that will be loaded)^4. */
128721 
128722   /* Tokens are never deferred for FTS tables created using the content=xxx
128723   ** option. The reason being that it is not guaranteed that the content
128724   ** table actually contains the same data as the index. To prevent this from
128725   ** causing any problems, the deferred token optimization is completely
128726   ** disabled for content=xxx tables. */
128727   if( pTab->zContentTbl ){
128728     return SQLITE_OK;
128729   }
128730 
128731   /* Count the tokens in this AND/NEAR cluster. If none of the doclists
128732   ** associated with the tokens spill onto overflow pages, or if there is
128733   ** only 1 token, exit early. No tokens to defer in this case. */
128734   for(ii=0; ii<nTC; ii++){
128735     if( aTC[ii].pRoot==pRoot ){
128736       nOvfl += aTC[ii].nOvfl;
128737       nToken++;
128738     }
128739   }
128740   if( nOvfl==0 || nToken<2 ) return SQLITE_OK;
128741 
128742   /* Obtain the average docsize (in pages). */
128743   rc = fts3EvalAverageDocsize(pCsr, &nDocSize);
128744   assert( rc!=SQLITE_OK || nDocSize>0 );
128745 
128746 
128747   /* Iterate through all tokens in this AND/NEAR cluster, in ascending order
128748   ** of the number of overflow pages that will be loaded by the pager layer
128749   ** to retrieve the entire doclist for the token from the full-text index.
128750   ** Load the doclists for tokens that are either:
128751   **
128752   **   a. The cheapest token in the entire query (i.e. the one visited by the
128753   **      first iteration of this loop), or
128754   **
128755   **   b. Part of a multi-token phrase.
128756   **
128757   ** After each token doclist is loaded, merge it with the others from the
128758   ** same phrase and count the number of documents that the merged doclist
128759   ** contains. Set variable "nMinEst" to the smallest number of documents in
128760   ** any phrase doclist for which 1 or more token doclists have been loaded.
128761   ** Let nOther be the number of other phrases for which it is certain that
128762   ** one or more tokens will not be deferred.
128763   **
128764   ** Then, for each token, defer it if loading the doclist would result in
128765   ** loading N or more overflow pages into memory, where N is computed as:
128766   **
128767   **    (nMinEst + 4^nOther - 1) / (4^nOther)
128768   */
128769   for(ii=0; ii<nToken && rc==SQLITE_OK; ii++){
128770     int iTC;                      /* Used to iterate through aTC[] array. */
128771     Fts3TokenAndCost *pTC = 0;    /* Set to cheapest remaining token. */
128772 
128773     /* Set pTC to point to the cheapest remaining token. */
128774     for(iTC=0; iTC<nTC; iTC++){
128775       if( aTC[iTC].pToken && aTC[iTC].pRoot==pRoot
128776        && (!pTC || aTC[iTC].nOvfl<pTC->nOvfl)
128777       ){
128778         pTC = &aTC[iTC];
128779       }
128780     }
128781     assert( pTC );
128782 
128783     if( ii && pTC->nOvfl>=((nMinEst+(nLoad4/4)-1)/(nLoad4/4))*nDocSize ){
128784       /* The number of overflow pages to load for this (and therefore all
128785       ** subsequent) tokens is greater than the estimated number of pages
128786       ** that will be loaded if all subsequent tokens are deferred.
128787       */
128788       Fts3PhraseToken *pToken = pTC->pToken;
128789       rc = sqlite3Fts3DeferToken(pCsr, pToken, pTC->iCol);
128790       fts3SegReaderCursorFree(pToken->pSegcsr);
128791       pToken->pSegcsr = 0;
128792     }else{
128793       /* Set nLoad4 to the value of (4^nOther) for the next iteration of the
128794       ** for-loop. Except, limit the value to 2^24 to prevent it from
128795       ** overflowing the 32-bit integer it is stored in. */
128796       if( ii<12 ) nLoad4 = nLoad4*4;
128797 
128798       if( ii==0 || (pTC->pPhrase->nToken>1 && ii!=nToken-1) ){
128799         /* Either this is the cheapest token in the entire query, or it is
128800         ** part of a multi-token phrase. Either way, the entire doclist will
128801         ** (eventually) be loaded into memory. It may as well be now. */
128802         Fts3PhraseToken *pToken = pTC->pToken;
128803         int nList = 0;
128804         char *pList = 0;
128805         rc = fts3TermSelect(pTab, pToken, pTC->iCol, &nList, &pList);
128806         assert( rc==SQLITE_OK || pList==0 );
128807         if( rc==SQLITE_OK ){
128808           int nCount;
128809           fts3EvalPhraseMergeToken(pTab, pTC->pPhrase, pTC->iToken,pList,nList);
128810           nCount = fts3DoclistCountDocids(
128811               pTC->pPhrase->doclist.aAll, pTC->pPhrase->doclist.nAll
128812           );
128813           if( ii==0 || nCount<nMinEst ) nMinEst = nCount;
128814         }
128815       }
128816     }
128817     pTC->pToken = 0;
128818   }
128819 
128820   return rc;
128821 }
128822 
128823 /*
128824 ** This function is called from within the xFilter method. It initializes
128825 ** the full-text query currently stored in pCsr->pExpr. To iterate through
128826 ** the results of a query, the caller does:
128827 **
128828 **    fts3EvalStart(pCsr);
128829 **    while( 1 ){
128830 **      fts3EvalNext(pCsr);
128831 **      if( pCsr->bEof ) break;
128832 **      ... return row pCsr->iPrevId to the caller ...
128833 **    }
128834 */
128835 static int fts3EvalStart(Fts3Cursor *pCsr){
128836   Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
128837   int rc = SQLITE_OK;
128838   int nToken = 0;
128839   int nOr = 0;
128840 
128841   /* Allocate a MultiSegReader for each token in the expression. */
128842   fts3EvalAllocateReaders(pCsr, pCsr->pExpr, &nToken, &nOr, &rc);
128843 
128844   /* Determine which, if any, tokens in the expression should be deferred. */
128845 #ifndef SQLITE_DISABLE_FTS4_DEFERRED
128846   if( rc==SQLITE_OK && nToken>1 && pTab->bFts4 ){
128847     Fts3TokenAndCost *aTC;
128848     Fts3Expr **apOr;
128849     aTC = (Fts3TokenAndCost *)sqlite3_malloc(
128850         sizeof(Fts3TokenAndCost) * nToken
128851       + sizeof(Fts3Expr *) * nOr * 2
128852     );
128853     apOr = (Fts3Expr **)&aTC[nToken];
128854 
128855     if( !aTC ){
128856       rc = SQLITE_NOMEM;
128857     }else{
128858       int ii;
128859       Fts3TokenAndCost *pTC = aTC;
128860       Fts3Expr **ppOr = apOr;
128861 
128862       fts3EvalTokenCosts(pCsr, 0, pCsr->pExpr, &pTC, &ppOr, &rc);
128863       nToken = (int)(pTC-aTC);
128864       nOr = (int)(ppOr-apOr);
128865 
128866       if( rc==SQLITE_OK ){
128867         rc = fts3EvalSelectDeferred(pCsr, 0, aTC, nToken);
128868         for(ii=0; rc==SQLITE_OK && ii<nOr; ii++){
128869           rc = fts3EvalSelectDeferred(pCsr, apOr[ii], aTC, nToken);
128870         }
128871       }
128872 
128873       sqlite3_free(aTC);
128874     }
128875   }
128876 #endif
128877 
128878   fts3EvalStartReaders(pCsr, pCsr->pExpr, &rc);
128879   return rc;
128880 }
128881 
128882 /*
128883 ** Invalidate the current position list for phrase pPhrase.
128884 */
128885 static void fts3EvalInvalidatePoslist(Fts3Phrase *pPhrase){
128886   if( pPhrase->doclist.bFreeList ){
128887     sqlite3_free(pPhrase->doclist.pList);
128888   }
128889   pPhrase->doclist.pList = 0;
128890   pPhrase->doclist.nList = 0;
128891   pPhrase->doclist.bFreeList = 0;
128892 }
128893 
128894 /*
128895 ** This function is called to edit the position list associated with
128896 ** the phrase object passed as the fifth argument according to a NEAR
128897 ** condition. For example:
128898 **
128899 **     abc NEAR/5 "def ghi"
128900 **
128901 ** Parameter nNear is passed the NEAR distance of the expression (5 in
128902 ** the example above). When this function is called, *paPoslist points to
128903 ** the position list, and *pnToken is the number of phrase tokens in, the
128904 ** phrase on the other side of the NEAR operator to pPhrase. For example,
128905 ** if pPhrase refers to the "def ghi" phrase, then *paPoslist points to
128906 ** the position list associated with phrase "abc".
128907 **
128908 ** All positions in the pPhrase position list that are not sufficiently
128909 ** close to a position in the *paPoslist position list are removed. If this
128910 ** leaves 0 positions, zero is returned. Otherwise, non-zero.
128911 **
128912 ** Before returning, *paPoslist is set to point to the position lsit
128913 ** associated with pPhrase. And *pnToken is set to the number of tokens in
128914 ** pPhrase.
128915 */
128916 static int fts3EvalNearTrim(
128917   int nNear,                      /* NEAR distance. As in "NEAR/nNear". */
128918   char *aTmp,                     /* Temporary space to use */
128919   char **paPoslist,               /* IN/OUT: Position list */
128920   int *pnToken,                   /* IN/OUT: Tokens in phrase of *paPoslist */
128921   Fts3Phrase *pPhrase             /* The phrase object to trim the doclist of */
128922 ){
128923   int nParam1 = nNear + pPhrase->nToken;
128924   int nParam2 = nNear + *pnToken;
128925   int nNew;
128926   char *p2;
128927   char *pOut;
128928   int res;
128929 
128930   assert( pPhrase->doclist.pList );
128931 
128932   p2 = pOut = pPhrase->doclist.pList;
128933   res = fts3PoslistNearMerge(
128934     &pOut, aTmp, nParam1, nParam2, paPoslist, &p2
128935   );
128936   if( res ){
128937     nNew = (int)(pOut - pPhrase->doclist.pList) - 1;
128938     assert( pPhrase->doclist.pList[nNew]=='\0' );
128939     assert( nNew<=pPhrase->doclist.nList && nNew>0 );
128940     memset(&pPhrase->doclist.pList[nNew], 0, pPhrase->doclist.nList - nNew);
128941     pPhrase->doclist.nList = nNew;
128942     *paPoslist = pPhrase->doclist.pList;
128943     *pnToken = pPhrase->nToken;
128944   }
128945 
128946   return res;
128947 }
128948 
128949 /*
128950 ** This function is a no-op if *pRc is other than SQLITE_OK when it is called.
128951 ** Otherwise, it advances the expression passed as the second argument to
128952 ** point to the next matching row in the database. Expressions iterate through
128953 ** matching rows in docid order. Ascending order if Fts3Cursor.bDesc is zero,
128954 ** or descending if it is non-zero.
128955 **
128956 ** If an error occurs, *pRc is set to an SQLite error code. Otherwise, if
128957 ** successful, the following variables in pExpr are set:
128958 **
128959 **   Fts3Expr.bEof                (non-zero if EOF - there is no next row)
128960 **   Fts3Expr.iDocid              (valid if bEof==0. The docid of the next row)
128961 **
128962 ** If the expression is of type FTSQUERY_PHRASE, and the expression is not
128963 ** at EOF, then the following variables are populated with the position list
128964 ** for the phrase for the visited row:
128965 **
128966 **   FTs3Expr.pPhrase->doclist.nList        (length of pList in bytes)
128967 **   FTs3Expr.pPhrase->doclist.pList        (pointer to position list)
128968 **
128969 ** It says above that this function advances the expression to the next
128970 ** matching row. This is usually true, but there are the following exceptions:
128971 **
128972 **   1. Deferred tokens are not taken into account. If a phrase consists
128973 **      entirely of deferred tokens, it is assumed to match every row in
128974 **      the db. In this case the position-list is not populated at all.
128975 **
128976 **      Or, if a phrase contains one or more deferred tokens and one or
128977 **      more non-deferred tokens, then the expression is advanced to the
128978 **      next possible match, considering only non-deferred tokens. In other
128979 **      words, if the phrase is "A B C", and "B" is deferred, the expression
128980 **      is advanced to the next row that contains an instance of "A * C",
128981 **      where "*" may match any single token. The position list in this case
128982 **      is populated as for "A * C" before returning.
128983 **
128984 **   2. NEAR is treated as AND. If the expression is "x NEAR y", it is
128985 **      advanced to point to the next row that matches "x AND y".
128986 **
128987 ** See fts3EvalTestDeferredAndNear() for details on testing if a row is
128988 ** really a match, taking into account deferred tokens and NEAR operators.
128989 */
128990 static void fts3EvalNextRow(
128991   Fts3Cursor *pCsr,               /* FTS Cursor handle */
128992   Fts3Expr *pExpr,                /* Expr. to advance to next matching row */
128993   int *pRc                        /* IN/OUT: Error code */
128994 ){
128995   if( *pRc==SQLITE_OK ){
128996     int bDescDoclist = pCsr->bDesc;         /* Used by DOCID_CMP() macro */
128997     assert( pExpr->bEof==0 );
128998     pExpr->bStart = 1;
128999 
129000     switch( pExpr->eType ){
129001       case FTSQUERY_NEAR:
129002       case FTSQUERY_AND: {
129003         Fts3Expr *pLeft = pExpr->pLeft;
129004         Fts3Expr *pRight = pExpr->pRight;
129005         assert( !pLeft->bDeferred || !pRight->bDeferred );
129006 
129007         if( pLeft->bDeferred ){
129008           /* LHS is entirely deferred. So we assume it matches every row.
129009           ** Advance the RHS iterator to find the next row visited. */
129010           fts3EvalNextRow(pCsr, pRight, pRc);
129011           pExpr->iDocid = pRight->iDocid;
129012           pExpr->bEof = pRight->bEof;
129013         }else if( pRight->bDeferred ){
129014           /* RHS is entirely deferred. So we assume it matches every row.
129015           ** Advance the LHS iterator to find the next row visited. */
129016           fts3EvalNextRow(pCsr, pLeft, pRc);
129017           pExpr->iDocid = pLeft->iDocid;
129018           pExpr->bEof = pLeft->bEof;
129019         }else{
129020           /* Neither the RHS or LHS are deferred. */
129021           fts3EvalNextRow(pCsr, pLeft, pRc);
129022           fts3EvalNextRow(pCsr, pRight, pRc);
129023           while( !pLeft->bEof && !pRight->bEof && *pRc==SQLITE_OK ){
129024             sqlite3_int64 iDiff = DOCID_CMP(pLeft->iDocid, pRight->iDocid);
129025             if( iDiff==0 ) break;
129026             if( iDiff<0 ){
129027               fts3EvalNextRow(pCsr, pLeft, pRc);
129028             }else{
129029               fts3EvalNextRow(pCsr, pRight, pRc);
129030             }
129031           }
129032           pExpr->iDocid = pLeft->iDocid;
129033           pExpr->bEof = (pLeft->bEof || pRight->bEof);
129034         }
129035         break;
129036       }
129037 
129038       case FTSQUERY_OR: {
129039         Fts3Expr *pLeft = pExpr->pLeft;
129040         Fts3Expr *pRight = pExpr->pRight;
129041         sqlite3_int64 iCmp = DOCID_CMP(pLeft->iDocid, pRight->iDocid);
129042 
129043         assert( pLeft->bStart || pLeft->iDocid==pRight->iDocid );
129044         assert( pRight->bStart || pLeft->iDocid==pRight->iDocid );
129045 
129046         if( pRight->bEof || (pLeft->bEof==0 && iCmp<0) ){
129047           fts3EvalNextRow(pCsr, pLeft, pRc);
129048         }else if( pLeft->bEof || (pRight->bEof==0 && iCmp>0) ){
129049           fts3EvalNextRow(pCsr, pRight, pRc);
129050         }else{
129051           fts3EvalNextRow(pCsr, pLeft, pRc);
129052           fts3EvalNextRow(pCsr, pRight, pRc);
129053         }
129054 
129055         pExpr->bEof = (pLeft->bEof && pRight->bEof);
129056         iCmp = DOCID_CMP(pLeft->iDocid, pRight->iDocid);
129057         if( pRight->bEof || (pLeft->bEof==0 &&  iCmp<0) ){
129058           pExpr->iDocid = pLeft->iDocid;
129059         }else{
129060           pExpr->iDocid = pRight->iDocid;
129061         }
129062 
129063         break;
129064       }
129065 
129066       case FTSQUERY_NOT: {
129067         Fts3Expr *pLeft = pExpr->pLeft;
129068         Fts3Expr *pRight = pExpr->pRight;
129069 
129070         if( pRight->bStart==0 ){
129071           fts3EvalNextRow(pCsr, pRight, pRc);
129072           assert( *pRc!=SQLITE_OK || pRight->bStart );
129073         }
129074 
129075         fts3EvalNextRow(pCsr, pLeft, pRc);
129076         if( pLeft->bEof==0 ){
129077           while( !*pRc
129078               && !pRight->bEof
129079               && DOCID_CMP(pLeft->iDocid, pRight->iDocid)>0
129080           ){
129081             fts3EvalNextRow(pCsr, pRight, pRc);
129082           }
129083         }
129084         pExpr->iDocid = pLeft->iDocid;
129085         pExpr->bEof = pLeft->bEof;
129086         break;
129087       }
129088 
129089       default: {
129090         Fts3Phrase *pPhrase = pExpr->pPhrase;
129091         fts3EvalInvalidatePoslist(pPhrase);
129092         *pRc = fts3EvalPhraseNext(pCsr, pPhrase, &pExpr->bEof);
129093         pExpr->iDocid = pPhrase->doclist.iDocid;
129094         break;
129095       }
129096     }
129097   }
129098 }
129099 
129100 /*
129101 ** If *pRc is not SQLITE_OK, or if pExpr is not the root node of a NEAR
129102 ** cluster, then this function returns 1 immediately.
129103 **
129104 ** Otherwise, it checks if the current row really does match the NEAR
129105 ** expression, using the data currently stored in the position lists
129106 ** (Fts3Expr->pPhrase.doclist.pList/nList) for each phrase in the expression.
129107 **
129108 ** If the current row is a match, the position list associated with each
129109 ** phrase in the NEAR expression is edited in place to contain only those
129110 ** phrase instances sufficiently close to their peers to satisfy all NEAR
129111 ** constraints. In this case it returns 1. If the NEAR expression does not
129112 ** match the current row, 0 is returned. The position lists may or may not
129113 ** be edited if 0 is returned.
129114 */
129115 static int fts3EvalNearTest(Fts3Expr *pExpr, int *pRc){
129116   int res = 1;
129117 
129118   /* The following block runs if pExpr is the root of a NEAR query.
129119   ** For example, the query:
129120   **
129121   **         "w" NEAR "x" NEAR "y" NEAR "z"
129122   **
129123   ** which is represented in tree form as:
129124   **
129125   **                               |
129126   **                          +--NEAR--+      <-- root of NEAR query
129127   **                          |        |
129128   **                     +--NEAR--+   "z"
129129   **                     |        |
129130   **                +--NEAR--+   "y"
129131   **                |        |
129132   **               "w"      "x"
129133   **
129134   ** The right-hand child of a NEAR node is always a phrase. The
129135   ** left-hand child may be either a phrase or a NEAR node. There are
129136   ** no exceptions to this - it's the way the parser in fts3_expr.c works.
129137   */
129138   if( *pRc==SQLITE_OK
129139    && pExpr->eType==FTSQUERY_NEAR
129140    && pExpr->bEof==0
129141    && (pExpr->pParent==0 || pExpr->pParent->eType!=FTSQUERY_NEAR)
129142   ){
129143     Fts3Expr *p;
129144     int nTmp = 0;                 /* Bytes of temp space */
129145     char *aTmp;                   /* Temp space for PoslistNearMerge() */
129146 
129147     /* Allocate temporary working space. */
129148     for(p=pExpr; p->pLeft; p=p->pLeft){
129149       nTmp += p->pRight->pPhrase->doclist.nList;
129150     }
129151     nTmp += p->pPhrase->doclist.nList;
129152     if( nTmp==0 ){
129153       res = 0;
129154     }else{
129155       aTmp = sqlite3_malloc(nTmp*2);
129156       if( !aTmp ){
129157         *pRc = SQLITE_NOMEM;
129158         res = 0;
129159       }else{
129160         char *aPoslist = p->pPhrase->doclist.pList;
129161         int nToken = p->pPhrase->nToken;
129162 
129163         for(p=p->pParent;res && p && p->eType==FTSQUERY_NEAR; p=p->pParent){
129164           Fts3Phrase *pPhrase = p->pRight->pPhrase;
129165           int nNear = p->nNear;
129166           res = fts3EvalNearTrim(nNear, aTmp, &aPoslist, &nToken, pPhrase);
129167         }
129168 
129169         aPoslist = pExpr->pRight->pPhrase->doclist.pList;
129170         nToken = pExpr->pRight->pPhrase->nToken;
129171         for(p=pExpr->pLeft; p && res; p=p->pLeft){
129172           int nNear;
129173           Fts3Phrase *pPhrase;
129174           assert( p->pParent && p->pParent->pLeft==p );
129175           nNear = p->pParent->nNear;
129176           pPhrase = (
129177               p->eType==FTSQUERY_NEAR ? p->pRight->pPhrase : p->pPhrase
129178               );
129179           res = fts3EvalNearTrim(nNear, aTmp, &aPoslist, &nToken, pPhrase);
129180         }
129181       }
129182 
129183       sqlite3_free(aTmp);
129184     }
129185   }
129186 
129187   return res;
129188 }
129189 
129190 /*
129191 ** This function is a helper function for fts3EvalTestDeferredAndNear().
129192 ** Assuming no error occurs or has occurred, It returns non-zero if the
129193 ** expression passed as the second argument matches the row that pCsr
129194 ** currently points to, or zero if it does not.
129195 **
129196 ** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
129197 ** If an error occurs during execution of this function, *pRc is set to
129198 ** the appropriate SQLite error code. In this case the returned value is
129199 ** undefined.
129200 */
129201 static int fts3EvalTestExpr(
129202   Fts3Cursor *pCsr,               /* FTS cursor handle */
129203   Fts3Expr *pExpr,                /* Expr to test. May or may not be root. */
129204   int *pRc                        /* IN/OUT: Error code */
129205 ){
129206   int bHit = 1;                   /* Return value */
129207   if( *pRc==SQLITE_OK ){
129208     switch( pExpr->eType ){
129209       case FTSQUERY_NEAR:
129210       case FTSQUERY_AND:
129211         bHit = (
129212             fts3EvalTestExpr(pCsr, pExpr->pLeft, pRc)
129213          && fts3EvalTestExpr(pCsr, pExpr->pRight, pRc)
129214          && fts3EvalNearTest(pExpr, pRc)
129215         );
129216 
129217         /* If the NEAR expression does not match any rows, zero the doclist for
129218         ** all phrases involved in the NEAR. This is because the snippet(),
129219         ** offsets() and matchinfo() functions are not supposed to recognize
129220         ** any instances of phrases that are part of unmatched NEAR queries.
129221         ** For example if this expression:
129222         **
129223         **    ... MATCH 'a OR (b NEAR c)'
129224         **
129225         ** is matched against a row containing:
129226         **
129227         **        'a b d e'
129228         **
129229         ** then any snippet() should ony highlight the "a" term, not the "b"
129230         ** (as "b" is part of a non-matching NEAR clause).
129231         */
129232         if( bHit==0
129233          && pExpr->eType==FTSQUERY_NEAR
129234          && (pExpr->pParent==0 || pExpr->pParent->eType!=FTSQUERY_NEAR)
129235         ){
129236           Fts3Expr *p;
129237           for(p=pExpr; p->pPhrase==0; p=p->pLeft){
129238             if( p->pRight->iDocid==pCsr->iPrevId ){
129239               fts3EvalInvalidatePoslist(p->pRight->pPhrase);
129240             }
129241           }
129242           if( p->iDocid==pCsr->iPrevId ){
129243             fts3EvalInvalidatePoslist(p->pPhrase);
129244           }
129245         }
129246 
129247         break;
129248 
129249       case FTSQUERY_OR: {
129250         int bHit1 = fts3EvalTestExpr(pCsr, pExpr->pLeft, pRc);
129251         int bHit2 = fts3EvalTestExpr(pCsr, pExpr->pRight, pRc);
129252         bHit = bHit1 || bHit2;
129253         break;
129254       }
129255 
129256       case FTSQUERY_NOT:
129257         bHit = (
129258             fts3EvalTestExpr(pCsr, pExpr->pLeft, pRc)
129259          && !fts3EvalTestExpr(pCsr, pExpr->pRight, pRc)
129260         );
129261         break;
129262 
129263       default: {
129264 #ifndef SQLITE_DISABLE_FTS4_DEFERRED
129265         if( pCsr->pDeferred
129266          && (pExpr->iDocid==pCsr->iPrevId || pExpr->bDeferred)
129267         ){
129268           Fts3Phrase *pPhrase = pExpr->pPhrase;
129269           assert( pExpr->bDeferred || pPhrase->doclist.bFreeList==0 );
129270           if( pExpr->bDeferred ){
129271             fts3EvalInvalidatePoslist(pPhrase);
129272           }
129273           *pRc = fts3EvalDeferredPhrase(pCsr, pPhrase);
129274           bHit = (pPhrase->doclist.pList!=0);
129275           pExpr->iDocid = pCsr->iPrevId;
129276         }else
129277 #endif
129278         {
129279           bHit = (pExpr->bEof==0 && pExpr->iDocid==pCsr->iPrevId);
129280         }
129281         break;
129282       }
129283     }
129284   }
129285   return bHit;
129286 }
129287 
129288 /*
129289 ** This function is called as the second part of each xNext operation when
129290 ** iterating through the results of a full-text query. At this point the
129291 ** cursor points to a row that matches the query expression, with the
129292 ** following caveats:
129293 **
129294 **   * Up until this point, "NEAR" operators in the expression have been
129295 **     treated as "AND".
129296 **
129297 **   * Deferred tokens have not yet been considered.
129298 **
129299 ** If *pRc is not SQLITE_OK when this function is called, it immediately
129300 ** returns 0. Otherwise, it tests whether or not after considering NEAR
129301 ** operators and deferred tokens the current row is still a match for the
129302 ** expression. It returns 1 if both of the following are true:
129303 **
129304 **   1. *pRc is SQLITE_OK when this function returns, and
129305 **
129306 **   2. After scanning the current FTS table row for the deferred tokens,
129307 **      it is determined that the row does *not* match the query.
129308 **
129309 ** Or, if no error occurs and it seems the current row does match the FTS
129310 ** query, return 0.
129311 */
129312 static int fts3EvalTestDeferredAndNear(Fts3Cursor *pCsr, int *pRc){
129313   int rc = *pRc;
129314   int bMiss = 0;
129315   if( rc==SQLITE_OK ){
129316 
129317     /* If there are one or more deferred tokens, load the current row into
129318     ** memory and scan it to determine the position list for each deferred
129319     ** token. Then, see if this row is really a match, considering deferred
129320     ** tokens and NEAR operators (neither of which were taken into account
129321     ** earlier, by fts3EvalNextRow()).
129322     */
129323     if( pCsr->pDeferred ){
129324       rc = fts3CursorSeek(0, pCsr);
129325       if( rc==SQLITE_OK ){
129326         rc = sqlite3Fts3CacheDeferredDoclists(pCsr);
129327       }
129328     }
129329     bMiss = (0==fts3EvalTestExpr(pCsr, pCsr->pExpr, &rc));
129330 
129331     /* Free the position-lists accumulated for each deferred token above. */
129332     sqlite3Fts3FreeDeferredDoclists(pCsr);
129333     *pRc = rc;
129334   }
129335   return (rc==SQLITE_OK && bMiss);
129336 }
129337 
129338 /*
129339 ** Advance to the next document that matches the FTS expression in
129340 ** Fts3Cursor.pExpr.
129341 */
129342 static int fts3EvalNext(Fts3Cursor *pCsr){
129343   int rc = SQLITE_OK;             /* Return Code */
129344   Fts3Expr *pExpr = pCsr->pExpr;
129345   assert( pCsr->isEof==0 );
129346   if( pExpr==0 ){
129347     pCsr->isEof = 1;
129348   }else{
129349     do {
129350       if( pCsr->isRequireSeek==0 ){
129351         sqlite3_reset(pCsr->pStmt);
129352       }
129353       assert( sqlite3_data_count(pCsr->pStmt)==0 );
129354       fts3EvalNextRow(pCsr, pExpr, &rc);
129355       pCsr->isEof = pExpr->bEof;
129356       pCsr->isRequireSeek = 1;
129357       pCsr->isMatchinfoNeeded = 1;
129358       pCsr->iPrevId = pExpr->iDocid;
129359     }while( pCsr->isEof==0 && fts3EvalTestDeferredAndNear(pCsr, &rc) );
129360   }
129361 
129362   /* Check if the cursor is past the end of the docid range specified
129363   ** by Fts3Cursor.iMinDocid/iMaxDocid. If so, set the EOF flag.  */
129364   if( rc==SQLITE_OK && (
129365         (pCsr->bDesc==0 && pCsr->iPrevId>pCsr->iMaxDocid)
129366      || (pCsr->bDesc!=0 && pCsr->iPrevId<pCsr->iMinDocid)
129367   )){
129368     pCsr->isEof = 1;
129369   }
129370 
129371   return rc;
129372 }
129373 
129374 /*
129375 ** Restart interation for expression pExpr so that the next call to
129376 ** fts3EvalNext() visits the first row. Do not allow incremental
129377 ** loading or merging of phrase doclists for this iteration.
129378 **
129379 ** If *pRc is other than SQLITE_OK when this function is called, it is
129380 ** a no-op. If an error occurs within this function, *pRc is set to an
129381 ** SQLite error code before returning.
129382 */
129383 static void fts3EvalRestart(
129384   Fts3Cursor *pCsr,
129385   Fts3Expr *pExpr,
129386   int *pRc
129387 ){
129388   if( pExpr && *pRc==SQLITE_OK ){
129389     Fts3Phrase *pPhrase = pExpr->pPhrase;
129390 
129391     if( pPhrase ){
129392       fts3EvalInvalidatePoslist(pPhrase);
129393       if( pPhrase->bIncr ){
129394         int i;
129395         for(i=0; i<pPhrase->nToken; i++){
129396           Fts3PhraseToken *pToken = &pPhrase->aToken[i];
129397           assert( pToken->pDeferred==0 );
129398           if( pToken->pSegcsr ){
129399             sqlite3Fts3MsrIncrRestart(pToken->pSegcsr);
129400           }
129401         }
129402         *pRc = fts3EvalPhraseStart(pCsr, 0, pPhrase);
129403       }
129404       pPhrase->doclist.pNextDocid = 0;
129405       pPhrase->doclist.iDocid = 0;
129406     }
129407 
129408     pExpr->iDocid = 0;
129409     pExpr->bEof = 0;
129410     pExpr->bStart = 0;
129411 
129412     fts3EvalRestart(pCsr, pExpr->pLeft, pRc);
129413     fts3EvalRestart(pCsr, pExpr->pRight, pRc);
129414   }
129415 }
129416 
129417 /*
129418 ** After allocating the Fts3Expr.aMI[] array for each phrase in the
129419 ** expression rooted at pExpr, the cursor iterates through all rows matched
129420 ** by pExpr, calling this function for each row. This function increments
129421 ** the values in Fts3Expr.aMI[] according to the position-list currently
129422 ** found in Fts3Expr.pPhrase->doclist.pList for each of the phrase
129423 ** expression nodes.
129424 */
129425 static void fts3EvalUpdateCounts(Fts3Expr *pExpr){
129426   if( pExpr ){
129427     Fts3Phrase *pPhrase = pExpr->pPhrase;
129428     if( pPhrase && pPhrase->doclist.pList ){
129429       int iCol = 0;
129430       char *p = pPhrase->doclist.pList;
129431 
129432       assert( *p );
129433       while( 1 ){
129434         u8 c = 0;
129435         int iCnt = 0;
129436         while( 0xFE & (*p | c) ){
129437           if( (c&0x80)==0 ) iCnt++;
129438           c = *p++ & 0x80;
129439         }
129440 
129441         /* aMI[iCol*3 + 1] = Number of occurrences
129442         ** aMI[iCol*3 + 2] = Number of rows containing at least one instance
129443         */
129444         pExpr->aMI[iCol*3 + 1] += iCnt;
129445         pExpr->aMI[iCol*3 + 2] += (iCnt>0);
129446         if( *p==0x00 ) break;
129447         p++;
129448         p += fts3GetVarint32(p, &iCol);
129449       }
129450     }
129451 
129452     fts3EvalUpdateCounts(pExpr->pLeft);
129453     fts3EvalUpdateCounts(pExpr->pRight);
129454   }
129455 }
129456 
129457 /*
129458 ** Expression pExpr must be of type FTSQUERY_PHRASE.
129459 **
129460 ** If it is not already allocated and populated, this function allocates and
129461 ** populates the Fts3Expr.aMI[] array for expression pExpr. If pExpr is part
129462 ** of a NEAR expression, then it also allocates and populates the same array
129463 ** for all other phrases that are part of the NEAR expression.
129464 **
129465 ** SQLITE_OK is returned if the aMI[] array is successfully allocated and
129466 ** populated. Otherwise, if an error occurs, an SQLite error code is returned.
129467 */
129468 static int fts3EvalGatherStats(
129469   Fts3Cursor *pCsr,               /* Cursor object */
129470   Fts3Expr *pExpr                 /* FTSQUERY_PHRASE expression */
129471 ){
129472   int rc = SQLITE_OK;             /* Return code */
129473 
129474   assert( pExpr->eType==FTSQUERY_PHRASE );
129475   if( pExpr->aMI==0 ){
129476     Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
129477     Fts3Expr *pRoot;                /* Root of NEAR expression */
129478     Fts3Expr *p;                    /* Iterator used for several purposes */
129479 
129480     sqlite3_int64 iPrevId = pCsr->iPrevId;
129481     sqlite3_int64 iDocid;
129482     u8 bEof;
129483 
129484     /* Find the root of the NEAR expression */
129485     pRoot = pExpr;
129486     while( pRoot->pParent && pRoot->pParent->eType==FTSQUERY_NEAR ){
129487       pRoot = pRoot->pParent;
129488     }
129489     iDocid = pRoot->iDocid;
129490     bEof = pRoot->bEof;
129491     assert( pRoot->bStart );
129492 
129493     /* Allocate space for the aMSI[] array of each FTSQUERY_PHRASE node */
129494     for(p=pRoot; p; p=p->pLeft){
129495       Fts3Expr *pE = (p->eType==FTSQUERY_PHRASE?p:p->pRight);
129496       assert( pE->aMI==0 );
129497       pE->aMI = (u32 *)sqlite3_malloc(pTab->nColumn * 3 * sizeof(u32));
129498       if( !pE->aMI ) return SQLITE_NOMEM;
129499       memset(pE->aMI, 0, pTab->nColumn * 3 * sizeof(u32));
129500     }
129501 
129502     fts3EvalRestart(pCsr, pRoot, &rc);
129503 
129504     while( pCsr->isEof==0 && rc==SQLITE_OK ){
129505 
129506       do {
129507         /* Ensure the %_content statement is reset. */
129508         if( pCsr->isRequireSeek==0 ) sqlite3_reset(pCsr->pStmt);
129509         assert( sqlite3_data_count(pCsr->pStmt)==0 );
129510 
129511         /* Advance to the next document */
129512         fts3EvalNextRow(pCsr, pRoot, &rc);
129513         pCsr->isEof = pRoot->bEof;
129514         pCsr->isRequireSeek = 1;
129515         pCsr->isMatchinfoNeeded = 1;
129516         pCsr->iPrevId = pRoot->iDocid;
129517       }while( pCsr->isEof==0
129518            && pRoot->eType==FTSQUERY_NEAR
129519            && fts3EvalTestDeferredAndNear(pCsr, &rc)
129520       );
129521 
129522       if( rc==SQLITE_OK && pCsr->isEof==0 ){
129523         fts3EvalUpdateCounts(pRoot);
129524       }
129525     }
129526 
129527     pCsr->isEof = 0;
129528     pCsr->iPrevId = iPrevId;
129529 
129530     if( bEof ){
129531       pRoot->bEof = bEof;
129532     }else{
129533       /* Caution: pRoot may iterate through docids in ascending or descending
129534       ** order. For this reason, even though it seems more defensive, the
129535       ** do loop can not be written:
129536       **
129537       **   do {...} while( pRoot->iDocid<iDocid && rc==SQLITE_OK );
129538       */
129539       fts3EvalRestart(pCsr, pRoot, &rc);
129540       do {
129541         fts3EvalNextRow(pCsr, pRoot, &rc);
129542         assert( pRoot->bEof==0 );
129543       }while( pRoot->iDocid!=iDocid && rc==SQLITE_OK );
129544       fts3EvalTestDeferredAndNear(pCsr, &rc);
129545     }
129546   }
129547   return rc;
129548 }
129549 
129550 /*
129551 ** This function is used by the matchinfo() module to query a phrase
129552 ** expression node for the following information:
129553 **
129554 **   1. The total number of occurrences of the phrase in each column of
129555 **      the FTS table (considering all rows), and
129556 **
129557 **   2. For each column, the number of rows in the table for which the
129558 **      column contains at least one instance of the phrase.
129559 **
129560 ** If no error occurs, SQLITE_OK is returned and the values for each column
129561 ** written into the array aiOut as follows:
129562 **
129563 **   aiOut[iCol*3 + 1] = Number of occurrences
129564 **   aiOut[iCol*3 + 2] = Number of rows containing at least one instance
129565 **
129566 ** Caveats:
129567 **
129568 **   * If a phrase consists entirely of deferred tokens, then all output
129569 **     values are set to the number of documents in the table. In other
129570 **     words we assume that very common tokens occur exactly once in each
129571 **     column of each row of the table.
129572 **
129573 **   * If a phrase contains some deferred tokens (and some non-deferred
129574 **     tokens), count the potential occurrence identified by considering
129575 **     the non-deferred tokens instead of actual phrase occurrences.
129576 **
129577 **   * If the phrase is part of a NEAR expression, then only phrase instances
129578 **     that meet the NEAR constraint are included in the counts.
129579 */
129580 SQLITE_PRIVATE int sqlite3Fts3EvalPhraseStats(
129581   Fts3Cursor *pCsr,               /* FTS cursor handle */
129582   Fts3Expr *pExpr,                /* Phrase expression */
129583   u32 *aiOut                      /* Array to write results into (see above) */
129584 ){
129585   Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
129586   int rc = SQLITE_OK;
129587   int iCol;
129588 
129589   if( pExpr->bDeferred && pExpr->pParent->eType!=FTSQUERY_NEAR ){
129590     assert( pCsr->nDoc>0 );
129591     for(iCol=0; iCol<pTab->nColumn; iCol++){
129592       aiOut[iCol*3 + 1] = (u32)pCsr->nDoc;
129593       aiOut[iCol*3 + 2] = (u32)pCsr->nDoc;
129594     }
129595   }else{
129596     rc = fts3EvalGatherStats(pCsr, pExpr);
129597     if( rc==SQLITE_OK ){
129598       assert( pExpr->aMI );
129599       for(iCol=0; iCol<pTab->nColumn; iCol++){
129600         aiOut[iCol*3 + 1] = pExpr->aMI[iCol*3 + 1];
129601         aiOut[iCol*3 + 2] = pExpr->aMI[iCol*3 + 2];
129602       }
129603     }
129604   }
129605 
129606   return rc;
129607 }
129608 
129609 /*
129610 ** The expression pExpr passed as the second argument to this function
129611 ** must be of type FTSQUERY_PHRASE.
129612 **
129613 ** The returned value is either NULL or a pointer to a buffer containing
129614 ** a position-list indicating the occurrences of the phrase in column iCol
129615 ** of the current row.
129616 **
129617 ** More specifically, the returned buffer contains 1 varint for each
129618 ** occurrence of the phrase in the column, stored using the normal (delta+2)
129619 ** compression and is terminated by either an 0x01 or 0x00 byte. For example,
129620 ** if the requested column contains "a b X c d X X" and the position-list
129621 ** for 'X' is requested, the buffer returned may contain:
129622 **
129623 **     0x04 0x05 0x03 0x01   or   0x04 0x05 0x03 0x00
129624 **
129625 ** This function works regardless of whether or not the phrase is deferred,
129626 ** incremental, or neither.
129627 */
129628 SQLITE_PRIVATE int sqlite3Fts3EvalPhrasePoslist(
129629   Fts3Cursor *pCsr,               /* FTS3 cursor object */
129630   Fts3Expr *pExpr,                /* Phrase to return doclist for */
129631   int iCol,                       /* Column to return position list for */
129632   char **ppOut                    /* OUT: Pointer to position list */
129633 ){
129634   Fts3Phrase *pPhrase = pExpr->pPhrase;
129635   Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
129636   char *pIter;
129637   int iThis;
129638   sqlite3_int64 iDocid;
129639 
129640   /* If this phrase is applies specifically to some column other than
129641   ** column iCol, return a NULL pointer.  */
129642   *ppOut = 0;
129643   assert( iCol>=0 && iCol<pTab->nColumn );
129644   if( (pPhrase->iColumn<pTab->nColumn && pPhrase->iColumn!=iCol) ){
129645     return SQLITE_OK;
129646   }
129647 
129648   iDocid = pExpr->iDocid;
129649   pIter = pPhrase->doclist.pList;
129650   if( iDocid!=pCsr->iPrevId || pExpr->bEof ){
129651     int bDescDoclist = pTab->bDescIdx;      /* For DOCID_CMP macro */
129652     int iMul;                     /* +1 if csr dir matches index dir, else -1 */
129653     int bOr = 0;
129654     u8 bEof = 0;
129655     u8 bTreeEof = 0;
129656     Fts3Expr *p;                  /* Used to iterate from pExpr to root */
129657     Fts3Expr *pNear;              /* Most senior NEAR ancestor (or pExpr) */
129658 
129659     /* Check if this phrase descends from an OR expression node. If not,
129660     ** return NULL. Otherwise, the entry that corresponds to docid
129661     ** pCsr->iPrevId may lie earlier in the doclist buffer. Or, if the
129662     ** tree that the node is part of has been marked as EOF, but the node
129663     ** itself is not EOF, then it may point to an earlier entry. */
129664     pNear = pExpr;
129665     for(p=pExpr->pParent; p; p=p->pParent){
129666       if( p->eType==FTSQUERY_OR ) bOr = 1;
129667       if( p->eType==FTSQUERY_NEAR ) pNear = p;
129668       if( p->bEof ) bTreeEof = 1;
129669     }
129670     if( bOr==0 ) return SQLITE_OK;
129671 
129672     /* This is the descendent of an OR node. In this case we cannot use
129673     ** an incremental phrase. Load the entire doclist for the phrase
129674     ** into memory in this case.  */
129675     if( pPhrase->bIncr ){
129676       int rc = SQLITE_OK;
129677       int bEofSave = pExpr->bEof;
129678       fts3EvalRestart(pCsr, pExpr, &rc);
129679       while( rc==SQLITE_OK && !pExpr->bEof ){
129680         fts3EvalNextRow(pCsr, pExpr, &rc);
129681         if( bEofSave==0 && pExpr->iDocid==iDocid ) break;
129682       }
129683       pIter = pPhrase->doclist.pList;
129684       assert( rc!=SQLITE_OK || pPhrase->bIncr==0 );
129685       if( rc!=SQLITE_OK ) return rc;
129686     }
129687 
129688     iMul = ((pCsr->bDesc==bDescDoclist) ? 1 : -1);
129689     while( bTreeEof==1
129690         && pNear->bEof==0
129691         && (DOCID_CMP(pNear->iDocid, pCsr->iPrevId) * iMul)<0
129692     ){
129693       int rc = SQLITE_OK;
129694       fts3EvalNextRow(pCsr, pExpr, &rc);
129695       if( rc!=SQLITE_OK ) return rc;
129696       iDocid = pExpr->iDocid;
129697       pIter = pPhrase->doclist.pList;
129698     }
129699 
129700     bEof = (pPhrase->doclist.nAll==0);
129701     assert( bDescDoclist==0 || bDescDoclist==1 );
129702     assert( pCsr->bDesc==0 || pCsr->bDesc==1 );
129703 
129704     if( bEof==0 ){
129705       if( pCsr->bDesc==bDescDoclist ){
129706         int dummy;
129707         if( pNear->bEof ){
129708           /* This expression is already at EOF. So position it to point to the
129709           ** last entry in the doclist at pPhrase->doclist.aAll[]. Variable
129710           ** iDocid is already set for this entry, so all that is required is
129711           ** to set pIter to point to the first byte of the last position-list
129712           ** in the doclist.
129713           **
129714           ** It would also be correct to set pIter and iDocid to zero. In
129715           ** this case, the first call to sqltie3Fts4DoclistPrev() below
129716           ** would also move the iterator to point to the last entry in the
129717           ** doclist. However, this is expensive, as to do so it has to
129718           ** iterate through the entire doclist from start to finish (since
129719           ** it does not know the docid for the last entry).  */
129720           pIter = &pPhrase->doclist.aAll[pPhrase->doclist.nAll-1];
129721           fts3ReversePoslist(pPhrase->doclist.aAll, &pIter);
129722         }
129723         while( (pIter==0 || DOCID_CMP(iDocid, pCsr->iPrevId)>0 ) && bEof==0 ){
129724           sqlite3Fts3DoclistPrev(
129725               bDescDoclist, pPhrase->doclist.aAll, pPhrase->doclist.nAll,
129726               &pIter, &iDocid, &dummy, &bEof
129727           );
129728         }
129729       }else{
129730         if( pNear->bEof ){
129731           pIter = 0;
129732           iDocid = 0;
129733         }
129734         while( (pIter==0 || DOCID_CMP(iDocid, pCsr->iPrevId)<0 ) && bEof==0 ){
129735           sqlite3Fts3DoclistNext(
129736               bDescDoclist, pPhrase->doclist.aAll, pPhrase->doclist.nAll,
129737               &pIter, &iDocid, &bEof
129738           );
129739         }
129740       }
129741     }
129742 
129743     if( bEof || iDocid!=pCsr->iPrevId ) pIter = 0;
129744   }
129745   if( pIter==0 ) return SQLITE_OK;
129746 
129747   if( *pIter==0x01 ){
129748     pIter++;
129749     pIter += fts3GetVarint32(pIter, &iThis);
129750   }else{
129751     iThis = 0;
129752   }
129753   while( iThis<iCol ){
129754     fts3ColumnlistCopy(0, &pIter);
129755     if( *pIter==0x00 ) return 0;
129756     pIter++;
129757     pIter += fts3GetVarint32(pIter, &iThis);
129758   }
129759 
129760   *ppOut = ((iCol==iThis)?pIter:0);
129761   return SQLITE_OK;
129762 }
129763 
129764 /*
129765 ** Free all components of the Fts3Phrase structure that were allocated by
129766 ** the eval module. Specifically, this means to free:
129767 **
129768 **   * the contents of pPhrase->doclist, and
129769 **   * any Fts3MultiSegReader objects held by phrase tokens.
129770 */
129771 SQLITE_PRIVATE void sqlite3Fts3EvalPhraseCleanup(Fts3Phrase *pPhrase){
129772   if( pPhrase ){
129773     int i;
129774     sqlite3_free(pPhrase->doclist.aAll);
129775     fts3EvalInvalidatePoslist(pPhrase);
129776     memset(&pPhrase->doclist, 0, sizeof(Fts3Doclist));
129777     for(i=0; i<pPhrase->nToken; i++){
129778       fts3SegReaderCursorFree(pPhrase->aToken[i].pSegcsr);
129779       pPhrase->aToken[i].pSegcsr = 0;
129780     }
129781   }
129782 }
129783 
129784 
129785 /*
129786 ** Return SQLITE_CORRUPT_VTAB.
129787 */
129788 #ifdef SQLITE_DEBUG
129789 SQLITE_PRIVATE int sqlite3Fts3Corrupt(){
129790   return SQLITE_CORRUPT_VTAB;
129791 }
129792 #endif
129793 
129794 #if !SQLITE_CORE
129795 /*
129796 ** Initialize API pointer table, if required.
129797 */
129798 #ifdef _WIN32
129799 __declspec(dllexport)
129800 #endif
129801 SQLITE_API int sqlite3_fts3_init(
129802   sqlite3 *db,
129803   char **pzErrMsg,
129804   const sqlite3_api_routines *pApi
129805 ){
129806   SQLITE_EXTENSION_INIT2(pApi)
129807   return sqlite3Fts3Init(db);
129808 }
129809 #endif
129810 
129811 #endif
129812 
129813 /************** End of fts3.c ************************************************/
129814 /************** Begin file fts3_aux.c ****************************************/
129815 /*
129816 ** 2011 Jan 27
129817 **
129818 ** The author disclaims copyright to this source code.  In place of
129819 ** a legal notice, here is a blessing:
129820 **
129821 **    May you do good and not evil.
129822 **    May you find forgiveness for yourself and forgive others.
129823 **    May you share freely, never taking more than you give.
129824 **
129825 ******************************************************************************
129826 **
129827 */
129828 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
129829 
129830 /* #include <string.h> */
129831 /* #include <assert.h> */
129832 
129833 typedef struct Fts3auxTable Fts3auxTable;
129834 typedef struct Fts3auxCursor Fts3auxCursor;
129835 
129836 struct Fts3auxTable {
129837   sqlite3_vtab base;              /* Base class used by SQLite core */
129838   Fts3Table *pFts3Tab;
129839 };
129840 
129841 struct Fts3auxCursor {
129842   sqlite3_vtab_cursor base;       /* Base class used by SQLite core */
129843   Fts3MultiSegReader csr;        /* Must be right after "base" */
129844   Fts3SegFilter filter;
129845   char *zStop;
129846   int nStop;                      /* Byte-length of string zStop */
129847   int iLangid;                    /* Language id to query */
129848   int isEof;                      /* True if cursor is at EOF */
129849   sqlite3_int64 iRowid;           /* Current rowid */
129850 
129851   int iCol;                       /* Current value of 'col' column */
129852   int nStat;                      /* Size of aStat[] array */
129853   struct Fts3auxColstats {
129854     sqlite3_int64 nDoc;           /* 'documents' values for current csr row */
129855     sqlite3_int64 nOcc;           /* 'occurrences' values for current csr row */
129856   } *aStat;
129857 };
129858 
129859 /*
129860 ** Schema of the terms table.
129861 */
129862 #define FTS3_AUX_SCHEMA \
129863   "CREATE TABLE x(term, col, documents, occurrences, languageid HIDDEN)"
129864 
129865 /*
129866 ** This function does all the work for both the xConnect and xCreate methods.
129867 ** These tables have no persistent representation of their own, so xConnect
129868 ** and xCreate are identical operations.
129869 */
129870 static int fts3auxConnectMethod(
129871   sqlite3 *db,                    /* Database connection */
129872   void *pUnused,                  /* Unused */
129873   int argc,                       /* Number of elements in argv array */
129874   const char * const *argv,       /* xCreate/xConnect argument array */
129875   sqlite3_vtab **ppVtab,          /* OUT: New sqlite3_vtab object */
129876   char **pzErr                    /* OUT: sqlite3_malloc'd error message */
129877 ){
129878   char const *zDb;                /* Name of database (e.g. "main") */
129879   char const *zFts3;              /* Name of fts3 table */
129880   int nDb;                        /* Result of strlen(zDb) */
129881   int nFts3;                      /* Result of strlen(zFts3) */
129882   int nByte;                      /* Bytes of space to allocate here */
129883   int rc;                         /* value returned by declare_vtab() */
129884   Fts3auxTable *p;                /* Virtual table object to return */
129885 
129886   UNUSED_PARAMETER(pUnused);
129887 
129888   /* The user should invoke this in one of two forms:
129889   **
129890   **     CREATE VIRTUAL TABLE xxx USING fts4aux(fts4-table);
129891   **     CREATE VIRTUAL TABLE xxx USING fts4aux(fts4-table-db, fts4-table);
129892   */
129893   if( argc!=4 && argc!=5 ) goto bad_args;
129894 
129895   zDb = argv[1];
129896   nDb = (int)strlen(zDb);
129897   if( argc==5 ){
129898     if( nDb==4 && 0==sqlite3_strnicmp("temp", zDb, 4) ){
129899       zDb = argv[3];
129900       nDb = (int)strlen(zDb);
129901       zFts3 = argv[4];
129902     }else{
129903       goto bad_args;
129904     }
129905   }else{
129906     zFts3 = argv[3];
129907   }
129908   nFts3 = (int)strlen(zFts3);
129909 
129910   rc = sqlite3_declare_vtab(db, FTS3_AUX_SCHEMA);
129911   if( rc!=SQLITE_OK ) return rc;
129912 
129913   nByte = sizeof(Fts3auxTable) + sizeof(Fts3Table) + nDb + nFts3 + 2;
129914   p = (Fts3auxTable *)sqlite3_malloc(nByte);
129915   if( !p ) return SQLITE_NOMEM;
129916   memset(p, 0, nByte);
129917 
129918   p->pFts3Tab = (Fts3Table *)&p[1];
129919   p->pFts3Tab->zDb = (char *)&p->pFts3Tab[1];
129920   p->pFts3Tab->zName = &p->pFts3Tab->zDb[nDb+1];
129921   p->pFts3Tab->db = db;
129922   p->pFts3Tab->nIndex = 1;
129923 
129924   memcpy((char *)p->pFts3Tab->zDb, zDb, nDb);
129925   memcpy((char *)p->pFts3Tab->zName, zFts3, nFts3);
129926   sqlite3Fts3Dequote((char *)p->pFts3Tab->zName);
129927 
129928   *ppVtab = (sqlite3_vtab *)p;
129929   return SQLITE_OK;
129930 
129931  bad_args:
129932   *pzErr = sqlite3_mprintf("invalid arguments to fts4aux constructor");
129933   return SQLITE_ERROR;
129934 }
129935 
129936 /*
129937 ** This function does the work for both the xDisconnect and xDestroy methods.
129938 ** These tables have no persistent representation of their own, so xDisconnect
129939 ** and xDestroy are identical operations.
129940 */
129941 static int fts3auxDisconnectMethod(sqlite3_vtab *pVtab){
129942   Fts3auxTable *p = (Fts3auxTable *)pVtab;
129943   Fts3Table *pFts3 = p->pFts3Tab;
129944   int i;
129945 
129946   /* Free any prepared statements held */
129947   for(i=0; i<SizeofArray(pFts3->aStmt); i++){
129948     sqlite3_finalize(pFts3->aStmt[i]);
129949   }
129950   sqlite3_free(pFts3->zSegmentsTbl);
129951   sqlite3_free(p);
129952   return SQLITE_OK;
129953 }
129954 
129955 #define FTS4AUX_EQ_CONSTRAINT 1
129956 #define FTS4AUX_GE_CONSTRAINT 2
129957 #define FTS4AUX_LE_CONSTRAINT 4
129958 
129959 /*
129960 ** xBestIndex - Analyze a WHERE and ORDER BY clause.
129961 */
129962 static int fts3auxBestIndexMethod(
129963   sqlite3_vtab *pVTab,
129964   sqlite3_index_info *pInfo
129965 ){
129966   int i;
129967   int iEq = -1;
129968   int iGe = -1;
129969   int iLe = -1;
129970   int iLangid = -1;
129971   int iNext = 1;                  /* Next free argvIndex value */
129972 
129973   UNUSED_PARAMETER(pVTab);
129974 
129975   /* This vtab delivers always results in "ORDER BY term ASC" order. */
129976   if( pInfo->nOrderBy==1
129977    && pInfo->aOrderBy[0].iColumn==0
129978    && pInfo->aOrderBy[0].desc==0
129979   ){
129980     pInfo->orderByConsumed = 1;
129981   }
129982 
129983   /* Search for equality and range constraints on the "term" column.
129984   ** And equality constraints on the hidden "languageid" column. */
129985   for(i=0; i<pInfo->nConstraint; i++){
129986     if( pInfo->aConstraint[i].usable ){
129987       int op = pInfo->aConstraint[i].op;
129988       int iCol = pInfo->aConstraint[i].iColumn;
129989 
129990       if( iCol==0 ){
129991         if( op==SQLITE_INDEX_CONSTRAINT_EQ ) iEq = i;
129992         if( op==SQLITE_INDEX_CONSTRAINT_LT ) iLe = i;
129993         if( op==SQLITE_INDEX_CONSTRAINT_LE ) iLe = i;
129994         if( op==SQLITE_INDEX_CONSTRAINT_GT ) iGe = i;
129995         if( op==SQLITE_INDEX_CONSTRAINT_GE ) iGe = i;
129996       }
129997       if( iCol==4 ){
129998         if( op==SQLITE_INDEX_CONSTRAINT_EQ ) iLangid = i;
129999       }
130000     }
130001   }
130002 
130003   if( iEq>=0 ){
130004     pInfo->idxNum = FTS4AUX_EQ_CONSTRAINT;
130005     pInfo->aConstraintUsage[iEq].argvIndex = iNext++;
130006     pInfo->estimatedCost = 5;
130007   }else{
130008     pInfo->idxNum = 0;
130009     pInfo->estimatedCost = 20000;
130010     if( iGe>=0 ){
130011       pInfo->idxNum += FTS4AUX_GE_CONSTRAINT;
130012       pInfo->aConstraintUsage[iGe].argvIndex = iNext++;
130013       pInfo->estimatedCost /= 2;
130014     }
130015     if( iLe>=0 ){
130016       pInfo->idxNum += FTS4AUX_LE_CONSTRAINT;
130017       pInfo->aConstraintUsage[iLe].argvIndex = iNext++;
130018       pInfo->estimatedCost /= 2;
130019     }
130020   }
130021   if( iLangid>=0 ){
130022     pInfo->aConstraintUsage[iLangid].argvIndex = iNext++;
130023     pInfo->estimatedCost--;
130024   }
130025 
130026   return SQLITE_OK;
130027 }
130028 
130029 /*
130030 ** xOpen - Open a cursor.
130031 */
130032 static int fts3auxOpenMethod(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCsr){
130033   Fts3auxCursor *pCsr;            /* Pointer to cursor object to return */
130034 
130035   UNUSED_PARAMETER(pVTab);
130036 
130037   pCsr = (Fts3auxCursor *)sqlite3_malloc(sizeof(Fts3auxCursor));
130038   if( !pCsr ) return SQLITE_NOMEM;
130039   memset(pCsr, 0, sizeof(Fts3auxCursor));
130040 
130041   *ppCsr = (sqlite3_vtab_cursor *)pCsr;
130042   return SQLITE_OK;
130043 }
130044 
130045 /*
130046 ** xClose - Close a cursor.
130047 */
130048 static int fts3auxCloseMethod(sqlite3_vtab_cursor *pCursor){
130049   Fts3Table *pFts3 = ((Fts3auxTable *)pCursor->pVtab)->pFts3Tab;
130050   Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
130051 
130052   sqlite3Fts3SegmentsClose(pFts3);
130053   sqlite3Fts3SegReaderFinish(&pCsr->csr);
130054   sqlite3_free((void *)pCsr->filter.zTerm);
130055   sqlite3_free(pCsr->zStop);
130056   sqlite3_free(pCsr->aStat);
130057   sqlite3_free(pCsr);
130058   return SQLITE_OK;
130059 }
130060 
130061 static int fts3auxGrowStatArray(Fts3auxCursor *pCsr, int nSize){
130062   if( nSize>pCsr->nStat ){
130063     struct Fts3auxColstats *aNew;
130064     aNew = (struct Fts3auxColstats *)sqlite3_realloc(pCsr->aStat,
130065         sizeof(struct Fts3auxColstats) * nSize
130066     );
130067     if( aNew==0 ) return SQLITE_NOMEM;
130068     memset(&aNew[pCsr->nStat], 0,
130069         sizeof(struct Fts3auxColstats) * (nSize - pCsr->nStat)
130070     );
130071     pCsr->aStat = aNew;
130072     pCsr->nStat = nSize;
130073   }
130074   return SQLITE_OK;
130075 }
130076 
130077 /*
130078 ** xNext - Advance the cursor to the next row, if any.
130079 */
130080 static int fts3auxNextMethod(sqlite3_vtab_cursor *pCursor){
130081   Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
130082   Fts3Table *pFts3 = ((Fts3auxTable *)pCursor->pVtab)->pFts3Tab;
130083   int rc;
130084 
130085   /* Increment our pretend rowid value. */
130086   pCsr->iRowid++;
130087 
130088   for(pCsr->iCol++; pCsr->iCol<pCsr->nStat; pCsr->iCol++){
130089     if( pCsr->aStat[pCsr->iCol].nDoc>0 ) return SQLITE_OK;
130090   }
130091 
130092   rc = sqlite3Fts3SegReaderStep(pFts3, &pCsr->csr);
130093   if( rc==SQLITE_ROW ){
130094     int i = 0;
130095     int nDoclist = pCsr->csr.nDoclist;
130096     char *aDoclist = pCsr->csr.aDoclist;
130097     int iCol;
130098 
130099     int eState = 0;
130100 
130101     if( pCsr->zStop ){
130102       int n = (pCsr->nStop<pCsr->csr.nTerm) ? pCsr->nStop : pCsr->csr.nTerm;
130103       int mc = memcmp(pCsr->zStop, pCsr->csr.zTerm, n);
130104       if( mc<0 || (mc==0 && pCsr->csr.nTerm>pCsr->nStop) ){
130105         pCsr->isEof = 1;
130106         return SQLITE_OK;
130107       }
130108     }
130109 
130110     if( fts3auxGrowStatArray(pCsr, 2) ) return SQLITE_NOMEM;
130111     memset(pCsr->aStat, 0, sizeof(struct Fts3auxColstats) * pCsr->nStat);
130112     iCol = 0;
130113 
130114     while( i<nDoclist ){
130115       sqlite3_int64 v = 0;
130116 
130117       i += sqlite3Fts3GetVarint(&aDoclist[i], &v);
130118       switch( eState ){
130119         /* State 0. In this state the integer just read was a docid. */
130120         case 0:
130121           pCsr->aStat[0].nDoc++;
130122           eState = 1;
130123           iCol = 0;
130124           break;
130125 
130126         /* State 1. In this state we are expecting either a 1, indicating
130127         ** that the following integer will be a column number, or the
130128         ** start of a position list for column 0.
130129         **
130130         ** The only difference between state 1 and state 2 is that if the
130131         ** integer encountered in state 1 is not 0 or 1, then we need to
130132         ** increment the column 0 "nDoc" count for this term.
130133         */
130134         case 1:
130135           assert( iCol==0 );
130136           if( v>1 ){
130137             pCsr->aStat[1].nDoc++;
130138           }
130139           eState = 2;
130140           /* fall through */
130141 
130142         case 2:
130143           if( v==0 ){       /* 0x00. Next integer will be a docid. */
130144             eState = 0;
130145           }else if( v==1 ){ /* 0x01. Next integer will be a column number. */
130146             eState = 3;
130147           }else{            /* 2 or greater. A position. */
130148             pCsr->aStat[iCol+1].nOcc++;
130149             pCsr->aStat[0].nOcc++;
130150           }
130151           break;
130152 
130153         /* State 3. The integer just read is a column number. */
130154         default: assert( eState==3 );
130155           iCol = (int)v;
130156           if( fts3auxGrowStatArray(pCsr, iCol+2) ) return SQLITE_NOMEM;
130157           pCsr->aStat[iCol+1].nDoc++;
130158           eState = 2;
130159           break;
130160       }
130161     }
130162 
130163     pCsr->iCol = 0;
130164     rc = SQLITE_OK;
130165   }else{
130166     pCsr->isEof = 1;
130167   }
130168   return rc;
130169 }
130170 
130171 /*
130172 ** xFilter - Initialize a cursor to point at the start of its data.
130173 */
130174 static int fts3auxFilterMethod(
130175   sqlite3_vtab_cursor *pCursor,   /* The cursor used for this query */
130176   int idxNum,                     /* Strategy index */
130177   const char *idxStr,             /* Unused */
130178   int nVal,                       /* Number of elements in apVal */
130179   sqlite3_value **apVal           /* Arguments for the indexing scheme */
130180 ){
130181   Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
130182   Fts3Table *pFts3 = ((Fts3auxTable *)pCursor->pVtab)->pFts3Tab;
130183   int rc;
130184   int isScan = 0;
130185   int iLangVal = 0;               /* Language id to query */
130186 
130187   int iEq = -1;                   /* Index of term=? value in apVal */
130188   int iGe = -1;                   /* Index of term>=? value in apVal */
130189   int iLe = -1;                   /* Index of term<=? value in apVal */
130190   int iLangid = -1;               /* Index of languageid=? value in apVal */
130191   int iNext = 0;
130192 
130193   UNUSED_PARAMETER(nVal);
130194   UNUSED_PARAMETER(idxStr);
130195 
130196   assert( idxStr==0 );
130197   assert( idxNum==FTS4AUX_EQ_CONSTRAINT || idxNum==0
130198        || idxNum==FTS4AUX_LE_CONSTRAINT || idxNum==FTS4AUX_GE_CONSTRAINT
130199        || idxNum==(FTS4AUX_LE_CONSTRAINT|FTS4AUX_GE_CONSTRAINT)
130200   );
130201 
130202   if( idxNum==FTS4AUX_EQ_CONSTRAINT ){
130203     iEq = iNext++;
130204   }else{
130205     isScan = 1;
130206     if( idxNum & FTS4AUX_GE_CONSTRAINT ){
130207       iGe = iNext++;
130208     }
130209     if( idxNum & FTS4AUX_LE_CONSTRAINT ){
130210       iLe = iNext++;
130211     }
130212   }
130213   if( iNext<nVal ){
130214     iLangid = iNext++;
130215   }
130216 
130217   /* In case this cursor is being reused, close and zero it. */
130218   testcase(pCsr->filter.zTerm);
130219   sqlite3Fts3SegReaderFinish(&pCsr->csr);
130220   sqlite3_free((void *)pCsr->filter.zTerm);
130221   sqlite3_free(pCsr->aStat);
130222   memset(&pCsr->csr, 0, ((u8*)&pCsr[1]) - (u8*)&pCsr->csr);
130223 
130224   pCsr->filter.flags = FTS3_SEGMENT_REQUIRE_POS|FTS3_SEGMENT_IGNORE_EMPTY;
130225   if( isScan ) pCsr->filter.flags |= FTS3_SEGMENT_SCAN;
130226 
130227   if( iEq>=0 || iGe>=0 ){
130228     const unsigned char *zStr = sqlite3_value_text(apVal[0]);
130229     assert( (iEq==0 && iGe==-1) || (iEq==-1 && iGe==0) );
130230     if( zStr ){
130231       pCsr->filter.zTerm = sqlite3_mprintf("%s", zStr);
130232       pCsr->filter.nTerm = sqlite3_value_bytes(apVal[0]);
130233       if( pCsr->filter.zTerm==0 ) return SQLITE_NOMEM;
130234     }
130235   }
130236 
130237   if( iLe>=0 ){
130238     pCsr->zStop = sqlite3_mprintf("%s", sqlite3_value_text(apVal[iLe]));
130239     pCsr->nStop = sqlite3_value_bytes(apVal[iLe]);
130240     if( pCsr->zStop==0 ) return SQLITE_NOMEM;
130241   }
130242 
130243   if( iLangid>=0 ){
130244     iLangVal = sqlite3_value_int(apVal[iLangid]);
130245 
130246     /* If the user specified a negative value for the languageid, use zero
130247     ** instead. This works, as the "languageid=?" constraint will also
130248     ** be tested by the VDBE layer. The test will always be false (since
130249     ** this module will not return a row with a negative languageid), and
130250     ** so the overall query will return zero rows.  */
130251     if( iLangVal<0 ) iLangVal = 0;
130252   }
130253   pCsr->iLangid = iLangVal;
130254 
130255   rc = sqlite3Fts3SegReaderCursor(pFts3, iLangVal, 0, FTS3_SEGCURSOR_ALL,
130256       pCsr->filter.zTerm, pCsr->filter.nTerm, 0, isScan, &pCsr->csr
130257   );
130258   if( rc==SQLITE_OK ){
130259     rc = sqlite3Fts3SegReaderStart(pFts3, &pCsr->csr, &pCsr->filter);
130260   }
130261 
130262   if( rc==SQLITE_OK ) rc = fts3auxNextMethod(pCursor);
130263   return rc;
130264 }
130265 
130266 /*
130267 ** xEof - Return true if the cursor is at EOF, or false otherwise.
130268 */
130269 static int fts3auxEofMethod(sqlite3_vtab_cursor *pCursor){
130270   Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
130271   return pCsr->isEof;
130272 }
130273 
130274 /*
130275 ** xColumn - Return a column value.
130276 */
130277 static int fts3auxColumnMethod(
130278   sqlite3_vtab_cursor *pCursor,   /* Cursor to retrieve value from */
130279   sqlite3_context *pCtx,          /* Context for sqlite3_result_xxx() calls */
130280   int iCol                        /* Index of column to read value from */
130281 ){
130282   Fts3auxCursor *p = (Fts3auxCursor *)pCursor;
130283 
130284   assert( p->isEof==0 );
130285   switch( iCol ){
130286     case 0: /* term */
130287       sqlite3_result_text(pCtx, p->csr.zTerm, p->csr.nTerm, SQLITE_TRANSIENT);
130288       break;
130289 
130290     case 1: /* col */
130291       if( p->iCol ){
130292         sqlite3_result_int(pCtx, p->iCol-1);
130293       }else{
130294         sqlite3_result_text(pCtx, "*", -1, SQLITE_STATIC);
130295       }
130296       break;
130297 
130298     case 2: /* documents */
130299       sqlite3_result_int64(pCtx, p->aStat[p->iCol].nDoc);
130300       break;
130301 
130302     case 3: /* occurrences */
130303       sqlite3_result_int64(pCtx, p->aStat[p->iCol].nOcc);
130304       break;
130305 
130306     default: /* languageid */
130307       assert( iCol==4 );
130308       sqlite3_result_int(pCtx, p->iLangid);
130309       break;
130310   }
130311 
130312   return SQLITE_OK;
130313 }
130314 
130315 /*
130316 ** xRowid - Return the current rowid for the cursor.
130317 */
130318 static int fts3auxRowidMethod(
130319   sqlite3_vtab_cursor *pCursor,   /* Cursor to retrieve value from */
130320   sqlite_int64 *pRowid            /* OUT: Rowid value */
130321 ){
130322   Fts3auxCursor *pCsr = (Fts3auxCursor *)pCursor;
130323   *pRowid = pCsr->iRowid;
130324   return SQLITE_OK;
130325 }
130326 
130327 /*
130328 ** Register the fts3aux module with database connection db. Return SQLITE_OK
130329 ** if successful or an error code if sqlite3_create_module() fails.
130330 */
130331 SQLITE_PRIVATE int sqlite3Fts3InitAux(sqlite3 *db){
130332   static const sqlite3_module fts3aux_module = {
130333      0,                           /* iVersion      */
130334      fts3auxConnectMethod,        /* xCreate       */
130335      fts3auxConnectMethod,        /* xConnect      */
130336      fts3auxBestIndexMethod,      /* xBestIndex    */
130337      fts3auxDisconnectMethod,     /* xDisconnect   */
130338      fts3auxDisconnectMethod,     /* xDestroy      */
130339      fts3auxOpenMethod,           /* xOpen         */
130340      fts3auxCloseMethod,          /* xClose        */
130341      fts3auxFilterMethod,         /* xFilter       */
130342      fts3auxNextMethod,           /* xNext         */
130343      fts3auxEofMethod,            /* xEof          */
130344      fts3auxColumnMethod,         /* xColumn       */
130345      fts3auxRowidMethod,          /* xRowid        */
130346      0,                           /* xUpdate       */
130347      0,                           /* xBegin        */
130348      0,                           /* xSync         */
130349      0,                           /* xCommit       */
130350      0,                           /* xRollback     */
130351      0,                           /* xFindFunction */
130352      0,                           /* xRename       */
130353      0,                           /* xSavepoint    */
130354      0,                           /* xRelease      */
130355      0                            /* xRollbackTo   */
130356   };
130357   int rc;                         /* Return code */
130358 
130359   rc = sqlite3_create_module(db, "fts4aux", &fts3aux_module, 0);
130360   return rc;
130361 }
130362 
130363 #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
130364 
130365 /************** End of fts3_aux.c ********************************************/
130366 /************** Begin file fts3_expr.c ***************************************/
130367 /*
130368 ** 2008 Nov 28
130369 **
130370 ** The author disclaims copyright to this source code.  In place of
130371 ** a legal notice, here is a blessing:
130372 **
130373 **    May you do good and not evil.
130374 **    May you find forgiveness for yourself and forgive others.
130375 **    May you share freely, never taking more than you give.
130376 **
130377 ******************************************************************************
130378 **
130379 ** This module contains code that implements a parser for fts3 query strings
130380 ** (the right-hand argument to the MATCH operator). Because the supported
130381 ** syntax is relatively simple, the whole tokenizer/parser system is
130382 ** hand-coded.
130383 */
130384 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
130385 
130386 /*
130387 ** By default, this module parses the legacy syntax that has been
130388 ** traditionally used by fts3. Or, if SQLITE_ENABLE_FTS3_PARENTHESIS
130389 ** is defined, then it uses the new syntax. The differences between
130390 ** the new and the old syntaxes are:
130391 **
130392 **  a) The new syntax supports parenthesis. The old does not.
130393 **
130394 **  b) The new syntax supports the AND and NOT operators. The old does not.
130395 **
130396 **  c) The old syntax supports the "-" token qualifier. This is not
130397 **     supported by the new syntax (it is replaced by the NOT operator).
130398 **
130399 **  d) When using the old syntax, the OR operator has a greater precedence
130400 **     than an implicit AND. When using the new, both implicity and explicit
130401 **     AND operators have a higher precedence than OR.
130402 **
130403 ** If compiled with SQLITE_TEST defined, then this module exports the
130404 ** symbol "int sqlite3_fts3_enable_parentheses". Setting this variable
130405 ** to zero causes the module to use the old syntax. If it is set to
130406 ** non-zero the new syntax is activated. This is so both syntaxes can
130407 ** be tested using a single build of testfixture.
130408 **
130409 ** The following describes the syntax supported by the fts3 MATCH
130410 ** operator in a similar format to that used by the lemon parser
130411 ** generator. This module does not use actually lemon, it uses a
130412 ** custom parser.
130413 **
130414 **   query ::= andexpr (OR andexpr)*.
130415 **
130416 **   andexpr ::= notexpr (AND? notexpr)*.
130417 **
130418 **   notexpr ::= nearexpr (NOT nearexpr|-TOKEN)*.
130419 **   notexpr ::= LP query RP.
130420 **
130421 **   nearexpr ::= phrase (NEAR distance_opt nearexpr)*.
130422 **
130423 **   distance_opt ::= .
130424 **   distance_opt ::= / INTEGER.
130425 **
130426 **   phrase ::= TOKEN.
130427 **   phrase ::= COLUMN:TOKEN.
130428 **   phrase ::= "TOKEN TOKEN TOKEN...".
130429 */
130430 
130431 #ifdef SQLITE_TEST
130432 SQLITE_API int sqlite3_fts3_enable_parentheses = 0;
130433 #else
130434 # ifdef SQLITE_ENABLE_FTS3_PARENTHESIS
130435 #  define sqlite3_fts3_enable_parentheses 1
130436 # else
130437 #  define sqlite3_fts3_enable_parentheses 0
130438 # endif
130439 #endif
130440 
130441 /*
130442 ** Default span for NEAR operators.
130443 */
130444 #define SQLITE_FTS3_DEFAULT_NEAR_PARAM 10
130445 
130446 /* #include <string.h> */
130447 /* #include <assert.h> */
130448 
130449 /*
130450 ** isNot:
130451 **   This variable is used by function getNextNode(). When getNextNode() is
130452 **   called, it sets ParseContext.isNot to true if the 'next node' is a
130453 **   FTSQUERY_PHRASE with a unary "-" attached to it. i.e. "mysql" in the
130454 **   FTS3 query "sqlite -mysql". Otherwise, ParseContext.isNot is set to
130455 **   zero.
130456 */
130457 typedef struct ParseContext ParseContext;
130458 struct ParseContext {
130459   sqlite3_tokenizer *pTokenizer;      /* Tokenizer module */
130460   int iLangid;                        /* Language id used with tokenizer */
130461   const char **azCol;                 /* Array of column names for fts3 table */
130462   int bFts4;                          /* True to allow FTS4-only syntax */
130463   int nCol;                           /* Number of entries in azCol[] */
130464   int iDefaultCol;                    /* Default column to query */
130465   int isNot;                          /* True if getNextNode() sees a unary - */
130466   sqlite3_context *pCtx;              /* Write error message here */
130467   int nNest;                          /* Number of nested brackets */
130468 };
130469 
130470 /*
130471 ** This function is equivalent to the standard isspace() function.
130472 **
130473 ** The standard isspace() can be awkward to use safely, because although it
130474 ** is defined to accept an argument of type int, its behavior when passed
130475 ** an integer that falls outside of the range of the unsigned char type
130476 ** is undefined (and sometimes, "undefined" means segfault). This wrapper
130477 ** is defined to accept an argument of type char, and always returns 0 for
130478 ** any values that fall outside of the range of the unsigned char type (i.e.
130479 ** negative values).
130480 */
130481 static int fts3isspace(char c){
130482   return c==' ' || c=='\t' || c=='\n' || c=='\r' || c=='\v' || c=='\f';
130483 }
130484 
130485 /*
130486 ** Allocate nByte bytes of memory using sqlite3_malloc(). If successful,
130487 ** zero the memory before returning a pointer to it. If unsuccessful,
130488 ** return NULL.
130489 */
130490 static void *fts3MallocZero(int nByte){
130491   void *pRet = sqlite3_malloc(nByte);
130492   if( pRet ) memset(pRet, 0, nByte);
130493   return pRet;
130494 }
130495 
130496 SQLITE_PRIVATE int sqlite3Fts3OpenTokenizer(
130497   sqlite3_tokenizer *pTokenizer,
130498   int iLangid,
130499   const char *z,
130500   int n,
130501   sqlite3_tokenizer_cursor **ppCsr
130502 ){
130503   sqlite3_tokenizer_module const *pModule = pTokenizer->pModule;
130504   sqlite3_tokenizer_cursor *pCsr = 0;
130505   int rc;
130506 
130507   rc = pModule->xOpen(pTokenizer, z, n, &pCsr);
130508   assert( rc==SQLITE_OK || pCsr==0 );
130509   if( rc==SQLITE_OK ){
130510     pCsr->pTokenizer = pTokenizer;
130511     if( pModule->iVersion>=1 ){
130512       rc = pModule->xLanguageid(pCsr, iLangid);
130513       if( rc!=SQLITE_OK ){
130514         pModule->xClose(pCsr);
130515         pCsr = 0;
130516       }
130517     }
130518   }
130519   *ppCsr = pCsr;
130520   return rc;
130521 }
130522 
130523 /*
130524 ** Function getNextNode(), which is called by fts3ExprParse(), may itself
130525 ** call fts3ExprParse(). So this forward declaration is required.
130526 */
130527 static int fts3ExprParse(ParseContext *, const char *, int, Fts3Expr **, int *);
130528 
130529 /*
130530 ** Extract the next token from buffer z (length n) using the tokenizer
130531 ** and other information (column names etc.) in pParse. Create an Fts3Expr
130532 ** structure of type FTSQUERY_PHRASE containing a phrase consisting of this
130533 ** single token and set *ppExpr to point to it. If the end of the buffer is
130534 ** reached before a token is found, set *ppExpr to zero. It is the
130535 ** responsibility of the caller to eventually deallocate the allocated
130536 ** Fts3Expr structure (if any) by passing it to sqlite3_free().
130537 **
130538 ** Return SQLITE_OK if successful, or SQLITE_NOMEM if a memory allocation
130539 ** fails.
130540 */
130541 static int getNextToken(
130542   ParseContext *pParse,                   /* fts3 query parse context */
130543   int iCol,                               /* Value for Fts3Phrase.iColumn */
130544   const char *z, int n,                   /* Input string */
130545   Fts3Expr **ppExpr,                      /* OUT: expression */
130546   int *pnConsumed                         /* OUT: Number of bytes consumed */
130547 ){
130548   sqlite3_tokenizer *pTokenizer = pParse->pTokenizer;
130549   sqlite3_tokenizer_module const *pModule = pTokenizer->pModule;
130550   int rc;
130551   sqlite3_tokenizer_cursor *pCursor;
130552   Fts3Expr *pRet = 0;
130553   int nConsumed = 0;
130554 
130555   rc = sqlite3Fts3OpenTokenizer(pTokenizer, pParse->iLangid, z, n, &pCursor);
130556   if( rc==SQLITE_OK ){
130557     const char *zToken;
130558     int nToken = 0, iStart = 0, iEnd = 0, iPosition = 0;
130559     int nByte;                               /* total space to allocate */
130560 
130561     rc = pModule->xNext(pCursor, &zToken, &nToken, &iStart, &iEnd, &iPosition);
130562 
130563     if( (rc==SQLITE_OK || rc==SQLITE_DONE) && sqlite3_fts3_enable_parentheses ){
130564       int i;
130565       if( rc==SQLITE_DONE ) iStart = n;
130566       for(i=0; i<iStart; i++){
130567         if( z[i]=='(' ){
130568           pParse->nNest++;
130569           rc = fts3ExprParse(pParse, &z[i+1], n-i-1, &pRet, &nConsumed);
130570           if( rc==SQLITE_OK && !pRet ){
130571             rc = SQLITE_DONE;
130572           }
130573           nConsumed = (int)(i + 1 + nConsumed);
130574           break;
130575         }
130576 
130577         if( z[i]==')' ){
130578           rc = SQLITE_DONE;
130579           pParse->nNest--;
130580           nConsumed = i+1;
130581           break;
130582         }
130583       }
130584     }
130585 
130586     if( nConsumed==0 && rc==SQLITE_OK ){
130587       nByte = sizeof(Fts3Expr) + sizeof(Fts3Phrase) + nToken;
130588       pRet = (Fts3Expr *)fts3MallocZero(nByte);
130589       if( !pRet ){
130590         rc = SQLITE_NOMEM;
130591       }else{
130592         pRet->eType = FTSQUERY_PHRASE;
130593         pRet->pPhrase = (Fts3Phrase *)&pRet[1];
130594         pRet->pPhrase->nToken = 1;
130595         pRet->pPhrase->iColumn = iCol;
130596         pRet->pPhrase->aToken[0].n = nToken;
130597         pRet->pPhrase->aToken[0].z = (char *)&pRet->pPhrase[1];
130598         memcpy(pRet->pPhrase->aToken[0].z, zToken, nToken);
130599 
130600         if( iEnd<n && z[iEnd]=='*' ){
130601           pRet->pPhrase->aToken[0].isPrefix = 1;
130602           iEnd++;
130603         }
130604 
130605         while( 1 ){
130606           if( !sqlite3_fts3_enable_parentheses
130607            && iStart>0 && z[iStart-1]=='-'
130608           ){
130609             pParse->isNot = 1;
130610             iStart--;
130611           }else if( pParse->bFts4 && iStart>0 && z[iStart-1]=='^' ){
130612             pRet->pPhrase->aToken[0].bFirst = 1;
130613             iStart--;
130614           }else{
130615             break;
130616           }
130617         }
130618 
130619       }
130620       nConsumed = iEnd;
130621     }
130622 
130623     pModule->xClose(pCursor);
130624   }
130625 
130626   *pnConsumed = nConsumed;
130627   *ppExpr = pRet;
130628   return rc;
130629 }
130630 
130631 
130632 /*
130633 ** Enlarge a memory allocation.  If an out-of-memory allocation occurs,
130634 ** then free the old allocation.
130635 */
130636 static void *fts3ReallocOrFree(void *pOrig, int nNew){
130637   void *pRet = sqlite3_realloc(pOrig, nNew);
130638   if( !pRet ){
130639     sqlite3_free(pOrig);
130640   }
130641   return pRet;
130642 }
130643 
130644 /*
130645 ** Buffer zInput, length nInput, contains the contents of a quoted string
130646 ** that appeared as part of an fts3 query expression. Neither quote character
130647 ** is included in the buffer. This function attempts to tokenize the entire
130648 ** input buffer and create an Fts3Expr structure of type FTSQUERY_PHRASE
130649 ** containing the results.
130650 **
130651 ** If successful, SQLITE_OK is returned and *ppExpr set to point at the
130652 ** allocated Fts3Expr structure. Otherwise, either SQLITE_NOMEM (out of memory
130653 ** error) or SQLITE_ERROR (tokenization error) is returned and *ppExpr set
130654 ** to 0.
130655 */
130656 static int getNextString(
130657   ParseContext *pParse,                   /* fts3 query parse context */
130658   const char *zInput, int nInput,         /* Input string */
130659   Fts3Expr **ppExpr                       /* OUT: expression */
130660 ){
130661   sqlite3_tokenizer *pTokenizer = pParse->pTokenizer;
130662   sqlite3_tokenizer_module const *pModule = pTokenizer->pModule;
130663   int rc;
130664   Fts3Expr *p = 0;
130665   sqlite3_tokenizer_cursor *pCursor = 0;
130666   char *zTemp = 0;
130667   int nTemp = 0;
130668 
130669   const int nSpace = sizeof(Fts3Expr) + sizeof(Fts3Phrase);
130670   int nToken = 0;
130671 
130672   /* The final Fts3Expr data structure, including the Fts3Phrase,
130673   ** Fts3PhraseToken structures token buffers are all stored as a single
130674   ** allocation so that the expression can be freed with a single call to
130675   ** sqlite3_free(). Setting this up requires a two pass approach.
130676   **
130677   ** The first pass, in the block below, uses a tokenizer cursor to iterate
130678   ** through the tokens in the expression. This pass uses fts3ReallocOrFree()
130679   ** to assemble data in two dynamic buffers:
130680   **
130681   **   Buffer p: Points to the Fts3Expr structure, followed by the Fts3Phrase
130682   **             structure, followed by the array of Fts3PhraseToken
130683   **             structures. This pass only populates the Fts3PhraseToken array.
130684   **
130685   **   Buffer zTemp: Contains copies of all tokens.
130686   **
130687   ** The second pass, in the block that begins "if( rc==SQLITE_DONE )" below,
130688   ** appends buffer zTemp to buffer p, and fills in the Fts3Expr and Fts3Phrase
130689   ** structures.
130690   */
130691   rc = sqlite3Fts3OpenTokenizer(
130692       pTokenizer, pParse->iLangid, zInput, nInput, &pCursor);
130693   if( rc==SQLITE_OK ){
130694     int ii;
130695     for(ii=0; rc==SQLITE_OK; ii++){
130696       const char *zByte;
130697       int nByte = 0, iBegin = 0, iEnd = 0, iPos = 0;
130698       rc = pModule->xNext(pCursor, &zByte, &nByte, &iBegin, &iEnd, &iPos);
130699       if( rc==SQLITE_OK ){
130700         Fts3PhraseToken *pToken;
130701 
130702         p = fts3ReallocOrFree(p, nSpace + ii*sizeof(Fts3PhraseToken));
130703         if( !p ) goto no_mem;
130704 
130705         zTemp = fts3ReallocOrFree(zTemp, nTemp + nByte);
130706         if( !zTemp ) goto no_mem;
130707 
130708         assert( nToken==ii );
130709         pToken = &((Fts3Phrase *)(&p[1]))->aToken[ii];
130710         memset(pToken, 0, sizeof(Fts3PhraseToken));
130711 
130712         memcpy(&zTemp[nTemp], zByte, nByte);
130713         nTemp += nByte;
130714 
130715         pToken->n = nByte;
130716         pToken->isPrefix = (iEnd<nInput && zInput[iEnd]=='*');
130717         pToken->bFirst = (iBegin>0 && zInput[iBegin-1]=='^');
130718         nToken = ii+1;
130719       }
130720     }
130721 
130722     pModule->xClose(pCursor);
130723     pCursor = 0;
130724   }
130725 
130726   if( rc==SQLITE_DONE ){
130727     int jj;
130728     char *zBuf = 0;
130729 
130730     p = fts3ReallocOrFree(p, nSpace + nToken*sizeof(Fts3PhraseToken) + nTemp);
130731     if( !p ) goto no_mem;
130732     memset(p, 0, (char *)&(((Fts3Phrase *)&p[1])->aToken[0])-(char *)p);
130733     p->eType = FTSQUERY_PHRASE;
130734     p->pPhrase = (Fts3Phrase *)&p[1];
130735     p->pPhrase->iColumn = pParse->iDefaultCol;
130736     p->pPhrase->nToken = nToken;
130737 
130738     zBuf = (char *)&p->pPhrase->aToken[nToken];
130739     if( zTemp ){
130740       memcpy(zBuf, zTemp, nTemp);
130741       sqlite3_free(zTemp);
130742     }else{
130743       assert( nTemp==0 );
130744     }
130745 
130746     for(jj=0; jj<p->pPhrase->nToken; jj++){
130747       p->pPhrase->aToken[jj].z = zBuf;
130748       zBuf += p->pPhrase->aToken[jj].n;
130749     }
130750     rc = SQLITE_OK;
130751   }
130752 
130753   *ppExpr = p;
130754   return rc;
130755 no_mem:
130756 
130757   if( pCursor ){
130758     pModule->xClose(pCursor);
130759   }
130760   sqlite3_free(zTemp);
130761   sqlite3_free(p);
130762   *ppExpr = 0;
130763   return SQLITE_NOMEM;
130764 }
130765 
130766 /*
130767 ** The output variable *ppExpr is populated with an allocated Fts3Expr
130768 ** structure, or set to 0 if the end of the input buffer is reached.
130769 **
130770 ** Returns an SQLite error code. SQLITE_OK if everything works, SQLITE_NOMEM
130771 ** if a malloc failure occurs, or SQLITE_ERROR if a parse error is encountered.
130772 ** If SQLITE_ERROR is returned, pContext is populated with an error message.
130773 */
130774 static int getNextNode(
130775   ParseContext *pParse,                   /* fts3 query parse context */
130776   const char *z, int n,                   /* Input string */
130777   Fts3Expr **ppExpr,                      /* OUT: expression */
130778   int *pnConsumed                         /* OUT: Number of bytes consumed */
130779 ){
130780   static const struct Fts3Keyword {
130781     char *z;                              /* Keyword text */
130782     unsigned char n;                      /* Length of the keyword */
130783     unsigned char parenOnly;              /* Only valid in paren mode */
130784     unsigned char eType;                  /* Keyword code */
130785   } aKeyword[] = {
130786     { "OR" ,  2, 0, FTSQUERY_OR   },
130787     { "AND",  3, 1, FTSQUERY_AND  },
130788     { "NOT",  3, 1, FTSQUERY_NOT  },
130789     { "NEAR", 4, 0, FTSQUERY_NEAR }
130790   };
130791   int ii;
130792   int iCol;
130793   int iColLen;
130794   int rc;
130795   Fts3Expr *pRet = 0;
130796 
130797   const char *zInput = z;
130798   int nInput = n;
130799 
130800   pParse->isNot = 0;
130801 
130802   /* Skip over any whitespace before checking for a keyword, an open or
130803   ** close bracket, or a quoted string.
130804   */
130805   while( nInput>0 && fts3isspace(*zInput) ){
130806     nInput--;
130807     zInput++;
130808   }
130809   if( nInput==0 ){
130810     return SQLITE_DONE;
130811   }
130812 
130813   /* See if we are dealing with a keyword. */
130814   for(ii=0; ii<(int)(sizeof(aKeyword)/sizeof(struct Fts3Keyword)); ii++){
130815     const struct Fts3Keyword *pKey = &aKeyword[ii];
130816 
130817     if( (pKey->parenOnly & ~sqlite3_fts3_enable_parentheses)!=0 ){
130818       continue;
130819     }
130820 
130821     if( nInput>=pKey->n && 0==memcmp(zInput, pKey->z, pKey->n) ){
130822       int nNear = SQLITE_FTS3_DEFAULT_NEAR_PARAM;
130823       int nKey = pKey->n;
130824       char cNext;
130825 
130826       /* If this is a "NEAR" keyword, check for an explicit nearness. */
130827       if( pKey->eType==FTSQUERY_NEAR ){
130828         assert( nKey==4 );
130829         if( zInput[4]=='/' && zInput[5]>='0' && zInput[5]<='9' ){
130830           nNear = 0;
130831           for(nKey=5; zInput[nKey]>='0' && zInput[nKey]<='9'; nKey++){
130832             nNear = nNear * 10 + (zInput[nKey] - '0');
130833           }
130834         }
130835       }
130836 
130837       /* At this point this is probably a keyword. But for that to be true,
130838       ** the next byte must contain either whitespace, an open or close
130839       ** parenthesis, a quote character, or EOF.
130840       */
130841       cNext = zInput[nKey];
130842       if( fts3isspace(cNext)
130843        || cNext=='"' || cNext=='(' || cNext==')' || cNext==0
130844       ){
130845         pRet = (Fts3Expr *)fts3MallocZero(sizeof(Fts3Expr));
130846         if( !pRet ){
130847           return SQLITE_NOMEM;
130848         }
130849         pRet->eType = pKey->eType;
130850         pRet->nNear = nNear;
130851         *ppExpr = pRet;
130852         *pnConsumed = (int)((zInput - z) + nKey);
130853         return SQLITE_OK;
130854       }
130855 
130856       /* Turns out that wasn't a keyword after all. This happens if the
130857       ** user has supplied a token such as "ORacle". Continue.
130858       */
130859     }
130860   }
130861 
130862   /* See if we are dealing with a quoted phrase. If this is the case, then
130863   ** search for the closing quote and pass the whole string to getNextString()
130864   ** for processing. This is easy to do, as fts3 has no syntax for escaping
130865   ** a quote character embedded in a string.
130866   */
130867   if( *zInput=='"' ){
130868     for(ii=1; ii<nInput && zInput[ii]!='"'; ii++);
130869     *pnConsumed = (int)((zInput - z) + ii + 1);
130870     if( ii==nInput ){
130871       return SQLITE_ERROR;
130872     }
130873     return getNextString(pParse, &zInput[1], ii-1, ppExpr);
130874   }
130875 
130876 
130877   /* If control flows to this point, this must be a regular token, or
130878   ** the end of the input. Read a regular token using the sqlite3_tokenizer
130879   ** interface. Before doing so, figure out if there is an explicit
130880   ** column specifier for the token.
130881   **
130882   ** TODO: Strangely, it is not possible to associate a column specifier
130883   ** with a quoted phrase, only with a single token. Not sure if this was
130884   ** an implementation artifact or an intentional decision when fts3 was
130885   ** first implemented. Whichever it was, this module duplicates the
130886   ** limitation.
130887   */
130888   iCol = pParse->iDefaultCol;
130889   iColLen = 0;
130890   for(ii=0; ii<pParse->nCol; ii++){
130891     const char *zStr = pParse->azCol[ii];
130892     int nStr = (int)strlen(zStr);
130893     if( nInput>nStr && zInput[nStr]==':'
130894      && sqlite3_strnicmp(zStr, zInput, nStr)==0
130895     ){
130896       iCol = ii;
130897       iColLen = (int)((zInput - z) + nStr + 1);
130898       break;
130899     }
130900   }
130901   rc = getNextToken(pParse, iCol, &z[iColLen], n-iColLen, ppExpr, pnConsumed);
130902   *pnConsumed += iColLen;
130903   return rc;
130904 }
130905 
130906 /*
130907 ** The argument is an Fts3Expr structure for a binary operator (any type
130908 ** except an FTSQUERY_PHRASE). Return an integer value representing the
130909 ** precedence of the operator. Lower values have a higher precedence (i.e.
130910 ** group more tightly). For example, in the C language, the == operator
130911 ** groups more tightly than ||, and would therefore have a higher precedence.
130912 **
130913 ** When using the new fts3 query syntax (when SQLITE_ENABLE_FTS3_PARENTHESIS
130914 ** is defined), the order of the operators in precedence from highest to
130915 ** lowest is:
130916 **
130917 **   NEAR
130918 **   NOT
130919 **   AND (including implicit ANDs)
130920 **   OR
130921 **
130922 ** Note that when using the old query syntax, the OR operator has a higher
130923 ** precedence than the AND operator.
130924 */
130925 static int opPrecedence(Fts3Expr *p){
130926   assert( p->eType!=FTSQUERY_PHRASE );
130927   if( sqlite3_fts3_enable_parentheses ){
130928     return p->eType;
130929   }else if( p->eType==FTSQUERY_NEAR ){
130930     return 1;
130931   }else if( p->eType==FTSQUERY_OR ){
130932     return 2;
130933   }
130934   assert( p->eType==FTSQUERY_AND );
130935   return 3;
130936 }
130937 
130938 /*
130939 ** Argument ppHead contains a pointer to the current head of a query
130940 ** expression tree being parsed. pPrev is the expression node most recently
130941 ** inserted into the tree. This function adds pNew, which is always a binary
130942 ** operator node, into the expression tree based on the relative precedence
130943 ** of pNew and the existing nodes of the tree. This may result in the head
130944 ** of the tree changing, in which case *ppHead is set to the new root node.
130945 */
130946 static void insertBinaryOperator(
130947   Fts3Expr **ppHead,       /* Pointer to the root node of a tree */
130948   Fts3Expr *pPrev,         /* Node most recently inserted into the tree */
130949   Fts3Expr *pNew           /* New binary node to insert into expression tree */
130950 ){
130951   Fts3Expr *pSplit = pPrev;
130952   while( pSplit->pParent && opPrecedence(pSplit->pParent)<=opPrecedence(pNew) ){
130953     pSplit = pSplit->pParent;
130954   }
130955 
130956   if( pSplit->pParent ){
130957     assert( pSplit->pParent->pRight==pSplit );
130958     pSplit->pParent->pRight = pNew;
130959     pNew->pParent = pSplit->pParent;
130960   }else{
130961     *ppHead = pNew;
130962   }
130963   pNew->pLeft = pSplit;
130964   pSplit->pParent = pNew;
130965 }
130966 
130967 /*
130968 ** Parse the fts3 query expression found in buffer z, length n. This function
130969 ** returns either when the end of the buffer is reached or an unmatched
130970 ** closing bracket - ')' - is encountered.
130971 **
130972 ** If successful, SQLITE_OK is returned, *ppExpr is set to point to the
130973 ** parsed form of the expression and *pnConsumed is set to the number of
130974 ** bytes read from buffer z. Otherwise, *ppExpr is set to 0 and SQLITE_NOMEM
130975 ** (out of memory error) or SQLITE_ERROR (parse error) is returned.
130976 */
130977 static int fts3ExprParse(
130978   ParseContext *pParse,                   /* fts3 query parse context */
130979   const char *z, int n,                   /* Text of MATCH query */
130980   Fts3Expr **ppExpr,                      /* OUT: Parsed query structure */
130981   int *pnConsumed                         /* OUT: Number of bytes consumed */
130982 ){
130983   Fts3Expr *pRet = 0;
130984   Fts3Expr *pPrev = 0;
130985   Fts3Expr *pNotBranch = 0;               /* Only used in legacy parse mode */
130986   int nIn = n;
130987   const char *zIn = z;
130988   int rc = SQLITE_OK;
130989   int isRequirePhrase = 1;
130990 
130991   while( rc==SQLITE_OK ){
130992     Fts3Expr *p = 0;
130993     int nByte = 0;
130994     rc = getNextNode(pParse, zIn, nIn, &p, &nByte);
130995     if( rc==SQLITE_OK ){
130996       int isPhrase;
130997 
130998       if( !sqlite3_fts3_enable_parentheses
130999        && p->eType==FTSQUERY_PHRASE && pParse->isNot
131000       ){
131001         /* Create an implicit NOT operator. */
131002         Fts3Expr *pNot = fts3MallocZero(sizeof(Fts3Expr));
131003         if( !pNot ){
131004           sqlite3Fts3ExprFree(p);
131005           rc = SQLITE_NOMEM;
131006           goto exprparse_out;
131007         }
131008         pNot->eType = FTSQUERY_NOT;
131009         pNot->pRight = p;
131010         p->pParent = pNot;
131011         if( pNotBranch ){
131012           pNot->pLeft = pNotBranch;
131013           pNotBranch->pParent = pNot;
131014         }
131015         pNotBranch = pNot;
131016         p = pPrev;
131017       }else{
131018         int eType = p->eType;
131019         isPhrase = (eType==FTSQUERY_PHRASE || p->pLeft);
131020 
131021         /* The isRequirePhrase variable is set to true if a phrase or
131022         ** an expression contained in parenthesis is required. If a
131023         ** binary operator (AND, OR, NOT or NEAR) is encounted when
131024         ** isRequirePhrase is set, this is a syntax error.
131025         */
131026         if( !isPhrase && isRequirePhrase ){
131027           sqlite3Fts3ExprFree(p);
131028           rc = SQLITE_ERROR;
131029           goto exprparse_out;
131030         }
131031 
131032         if( isPhrase && !isRequirePhrase ){
131033           /* Insert an implicit AND operator. */
131034           Fts3Expr *pAnd;
131035           assert( pRet && pPrev );
131036           pAnd = fts3MallocZero(sizeof(Fts3Expr));
131037           if( !pAnd ){
131038             sqlite3Fts3ExprFree(p);
131039             rc = SQLITE_NOMEM;
131040             goto exprparse_out;
131041           }
131042           pAnd->eType = FTSQUERY_AND;
131043           insertBinaryOperator(&pRet, pPrev, pAnd);
131044           pPrev = pAnd;
131045         }
131046 
131047         /* This test catches attempts to make either operand of a NEAR
131048         ** operator something other than a phrase. For example, either of
131049         ** the following:
131050         **
131051         **    (bracketed expression) NEAR phrase
131052         **    phrase NEAR (bracketed expression)
131053         **
131054         ** Return an error in either case.
131055         */
131056         if( pPrev && (
131057             (eType==FTSQUERY_NEAR && !isPhrase && pPrev->eType!=FTSQUERY_PHRASE)
131058          || (eType!=FTSQUERY_PHRASE && isPhrase && pPrev->eType==FTSQUERY_NEAR)
131059         )){
131060           sqlite3Fts3ExprFree(p);
131061           rc = SQLITE_ERROR;
131062           goto exprparse_out;
131063         }
131064 
131065         if( isPhrase ){
131066           if( pRet ){
131067             assert( pPrev && pPrev->pLeft && pPrev->pRight==0 );
131068             pPrev->pRight = p;
131069             p->pParent = pPrev;
131070           }else{
131071             pRet = p;
131072           }
131073         }else{
131074           insertBinaryOperator(&pRet, pPrev, p);
131075         }
131076         isRequirePhrase = !isPhrase;
131077       }
131078       assert( nByte>0 );
131079     }
131080     assert( rc!=SQLITE_OK || (nByte>0 && nByte<=nIn) );
131081     nIn -= nByte;
131082     zIn += nByte;
131083     pPrev = p;
131084   }
131085 
131086   if( rc==SQLITE_DONE && pRet && isRequirePhrase ){
131087     rc = SQLITE_ERROR;
131088   }
131089 
131090   if( rc==SQLITE_DONE ){
131091     rc = SQLITE_OK;
131092     if( !sqlite3_fts3_enable_parentheses && pNotBranch ){
131093       if( !pRet ){
131094         rc = SQLITE_ERROR;
131095       }else{
131096         Fts3Expr *pIter = pNotBranch;
131097         while( pIter->pLeft ){
131098           pIter = pIter->pLeft;
131099         }
131100         pIter->pLeft = pRet;
131101         pRet->pParent = pIter;
131102         pRet = pNotBranch;
131103       }
131104     }
131105   }
131106   *pnConsumed = n - nIn;
131107 
131108 exprparse_out:
131109   if( rc!=SQLITE_OK ){
131110     sqlite3Fts3ExprFree(pRet);
131111     sqlite3Fts3ExprFree(pNotBranch);
131112     pRet = 0;
131113   }
131114   *ppExpr = pRet;
131115   return rc;
131116 }
131117 
131118 /*
131119 ** Return SQLITE_ERROR if the maximum depth of the expression tree passed
131120 ** as the only argument is more than nMaxDepth.
131121 */
131122 static int fts3ExprCheckDepth(Fts3Expr *p, int nMaxDepth){
131123   int rc = SQLITE_OK;
131124   if( p ){
131125     if( nMaxDepth<0 ){
131126       rc = SQLITE_TOOBIG;
131127     }else{
131128       rc = fts3ExprCheckDepth(p->pLeft, nMaxDepth-1);
131129       if( rc==SQLITE_OK ){
131130         rc = fts3ExprCheckDepth(p->pRight, nMaxDepth-1);
131131       }
131132     }
131133   }
131134   return rc;
131135 }
131136 
131137 /*
131138 ** This function attempts to transform the expression tree at (*pp) to
131139 ** an equivalent but more balanced form. The tree is modified in place.
131140 ** If successful, SQLITE_OK is returned and (*pp) set to point to the
131141 ** new root expression node.
131142 **
131143 ** nMaxDepth is the maximum allowable depth of the balanced sub-tree.
131144 **
131145 ** Otherwise, if an error occurs, an SQLite error code is returned and
131146 ** expression (*pp) freed.
131147 */
131148 static int fts3ExprBalance(Fts3Expr **pp, int nMaxDepth){
131149   int rc = SQLITE_OK;             /* Return code */
131150   Fts3Expr *pRoot = *pp;          /* Initial root node */
131151   Fts3Expr *pFree = 0;            /* List of free nodes. Linked by pParent. */
131152   int eType = pRoot->eType;       /* Type of node in this tree */
131153 
131154   if( nMaxDepth==0 ){
131155     rc = SQLITE_ERROR;
131156   }
131157 
131158   if( rc==SQLITE_OK && (eType==FTSQUERY_AND || eType==FTSQUERY_OR) ){
131159     Fts3Expr **apLeaf;
131160     apLeaf = (Fts3Expr **)sqlite3_malloc(sizeof(Fts3Expr *) * nMaxDepth);
131161     if( 0==apLeaf ){
131162       rc = SQLITE_NOMEM;
131163     }else{
131164       memset(apLeaf, 0, sizeof(Fts3Expr *) * nMaxDepth);
131165     }
131166 
131167     if( rc==SQLITE_OK ){
131168       int i;
131169       Fts3Expr *p;
131170 
131171       /* Set $p to point to the left-most leaf in the tree of eType nodes. */
131172       for(p=pRoot; p->eType==eType; p=p->pLeft){
131173         assert( p->pParent==0 || p->pParent->pLeft==p );
131174         assert( p->pLeft && p->pRight );
131175       }
131176 
131177       /* This loop runs once for each leaf in the tree of eType nodes. */
131178       while( 1 ){
131179         int iLvl;
131180         Fts3Expr *pParent = p->pParent;     /* Current parent of p */
131181 
131182         assert( pParent==0 || pParent->pLeft==p );
131183         p->pParent = 0;
131184         if( pParent ){
131185           pParent->pLeft = 0;
131186         }else{
131187           pRoot = 0;
131188         }
131189         rc = fts3ExprBalance(&p, nMaxDepth-1);
131190         if( rc!=SQLITE_OK ) break;
131191 
131192         for(iLvl=0; p && iLvl<nMaxDepth; iLvl++){
131193           if( apLeaf[iLvl]==0 ){
131194             apLeaf[iLvl] = p;
131195             p = 0;
131196           }else{
131197             assert( pFree );
131198             pFree->pLeft = apLeaf[iLvl];
131199             pFree->pRight = p;
131200             pFree->pLeft->pParent = pFree;
131201             pFree->pRight->pParent = pFree;
131202 
131203             p = pFree;
131204             pFree = pFree->pParent;
131205             p->pParent = 0;
131206             apLeaf[iLvl] = 0;
131207           }
131208         }
131209         if( p ){
131210           sqlite3Fts3ExprFree(p);
131211           rc = SQLITE_TOOBIG;
131212           break;
131213         }
131214 
131215         /* If that was the last leaf node, break out of the loop */
131216         if( pParent==0 ) break;
131217 
131218         /* Set $p to point to the next leaf in the tree of eType nodes */
131219         for(p=pParent->pRight; p->eType==eType; p=p->pLeft);
131220 
131221         /* Remove pParent from the original tree. */
131222         assert( pParent->pParent==0 || pParent->pParent->pLeft==pParent );
131223         pParent->pRight->pParent = pParent->pParent;
131224         if( pParent->pParent ){
131225           pParent->pParent->pLeft = pParent->pRight;
131226         }else{
131227           assert( pParent==pRoot );
131228           pRoot = pParent->pRight;
131229         }
131230 
131231         /* Link pParent into the free node list. It will be used as an
131232         ** internal node of the new tree.  */
131233         pParent->pParent = pFree;
131234         pFree = pParent;
131235       }
131236 
131237       if( rc==SQLITE_OK ){
131238         p = 0;
131239         for(i=0; i<nMaxDepth; i++){
131240           if( apLeaf[i] ){
131241             if( p==0 ){
131242               p = apLeaf[i];
131243               p->pParent = 0;
131244             }else{
131245               assert( pFree!=0 );
131246               pFree->pRight = p;
131247               pFree->pLeft = apLeaf[i];
131248               pFree->pLeft->pParent = pFree;
131249               pFree->pRight->pParent = pFree;
131250 
131251               p = pFree;
131252               pFree = pFree->pParent;
131253               p->pParent = 0;
131254             }
131255           }
131256         }
131257         pRoot = p;
131258       }else{
131259         /* An error occurred. Delete the contents of the apLeaf[] array
131260         ** and pFree list. Everything else is cleaned up by the call to
131261         ** sqlite3Fts3ExprFree(pRoot) below.  */
131262         Fts3Expr *pDel;
131263         for(i=0; i<nMaxDepth; i++){
131264           sqlite3Fts3ExprFree(apLeaf[i]);
131265         }
131266         while( (pDel=pFree)!=0 ){
131267           pFree = pDel->pParent;
131268           sqlite3_free(pDel);
131269         }
131270       }
131271 
131272       assert( pFree==0 );
131273       sqlite3_free( apLeaf );
131274     }
131275   }
131276 
131277   if( rc!=SQLITE_OK ){
131278     sqlite3Fts3ExprFree(pRoot);
131279     pRoot = 0;
131280   }
131281   *pp = pRoot;
131282   return rc;
131283 }
131284 
131285 /*
131286 ** This function is similar to sqlite3Fts3ExprParse(), with the following
131287 ** differences:
131288 **
131289 **   1. It does not do expression rebalancing.
131290 **   2. It does not check that the expression does not exceed the
131291 **      maximum allowable depth.
131292 **   3. Even if it fails, *ppExpr may still be set to point to an
131293 **      expression tree. It should be deleted using sqlite3Fts3ExprFree()
131294 **      in this case.
131295 */
131296 static int fts3ExprParseUnbalanced(
131297   sqlite3_tokenizer *pTokenizer,      /* Tokenizer module */
131298   int iLangid,                        /* Language id for tokenizer */
131299   char **azCol,                       /* Array of column names for fts3 table */
131300   int bFts4,                          /* True to allow FTS4-only syntax */
131301   int nCol,                           /* Number of entries in azCol[] */
131302   int iDefaultCol,                    /* Default column to query */
131303   const char *z, int n,               /* Text of MATCH query */
131304   Fts3Expr **ppExpr                   /* OUT: Parsed query structure */
131305 ){
131306   int nParsed;
131307   int rc;
131308   ParseContext sParse;
131309 
131310   memset(&sParse, 0, sizeof(ParseContext));
131311   sParse.pTokenizer = pTokenizer;
131312   sParse.iLangid = iLangid;
131313   sParse.azCol = (const char **)azCol;
131314   sParse.nCol = nCol;
131315   sParse.iDefaultCol = iDefaultCol;
131316   sParse.bFts4 = bFts4;
131317   if( z==0 ){
131318     *ppExpr = 0;
131319     return SQLITE_OK;
131320   }
131321   if( n<0 ){
131322     n = (int)strlen(z);
131323   }
131324   rc = fts3ExprParse(&sParse, z, n, ppExpr, &nParsed);
131325   assert( rc==SQLITE_OK || *ppExpr==0 );
131326 
131327   /* Check for mismatched parenthesis */
131328   if( rc==SQLITE_OK && sParse.nNest ){
131329     rc = SQLITE_ERROR;
131330   }
131331 
131332   return rc;
131333 }
131334 
131335 /*
131336 ** Parameters z and n contain a pointer to and length of a buffer containing
131337 ** an fts3 query expression, respectively. This function attempts to parse the
131338 ** query expression and create a tree of Fts3Expr structures representing the
131339 ** parsed expression. If successful, *ppExpr is set to point to the head
131340 ** of the parsed expression tree and SQLITE_OK is returned. If an error
131341 ** occurs, either SQLITE_NOMEM (out-of-memory error) or SQLITE_ERROR (parse
131342 ** error) is returned and *ppExpr is set to 0.
131343 **
131344 ** If parameter n is a negative number, then z is assumed to point to a
131345 ** nul-terminated string and the length is determined using strlen().
131346 **
131347 ** The first parameter, pTokenizer, is passed the fts3 tokenizer module to
131348 ** use to normalize query tokens while parsing the expression. The azCol[]
131349 ** array, which is assumed to contain nCol entries, should contain the names
131350 ** of each column in the target fts3 table, in order from left to right.
131351 ** Column names must be nul-terminated strings.
131352 **
131353 ** The iDefaultCol parameter should be passed the index of the table column
131354 ** that appears on the left-hand-side of the MATCH operator (the default
131355 ** column to match against for tokens for which a column name is not explicitly
131356 ** specified as part of the query string), or -1 if tokens may by default
131357 ** match any table column.
131358 */
131359 SQLITE_PRIVATE int sqlite3Fts3ExprParse(
131360   sqlite3_tokenizer *pTokenizer,      /* Tokenizer module */
131361   int iLangid,                        /* Language id for tokenizer */
131362   char **azCol,                       /* Array of column names for fts3 table */
131363   int bFts4,                          /* True to allow FTS4-only syntax */
131364   int nCol,                           /* Number of entries in azCol[] */
131365   int iDefaultCol,                    /* Default column to query */
131366   const char *z, int n,               /* Text of MATCH query */
131367   Fts3Expr **ppExpr,                  /* OUT: Parsed query structure */
131368   char **pzErr                        /* OUT: Error message (sqlite3_malloc) */
131369 ){
131370   int rc = fts3ExprParseUnbalanced(
131371       pTokenizer, iLangid, azCol, bFts4, nCol, iDefaultCol, z, n, ppExpr
131372   );
131373 
131374   /* Rebalance the expression. And check that its depth does not exceed
131375   ** SQLITE_FTS3_MAX_EXPR_DEPTH.  */
131376   if( rc==SQLITE_OK && *ppExpr ){
131377     rc = fts3ExprBalance(ppExpr, SQLITE_FTS3_MAX_EXPR_DEPTH);
131378     if( rc==SQLITE_OK ){
131379       rc = fts3ExprCheckDepth(*ppExpr, SQLITE_FTS3_MAX_EXPR_DEPTH);
131380     }
131381   }
131382 
131383   if( rc!=SQLITE_OK ){
131384     sqlite3Fts3ExprFree(*ppExpr);
131385     *ppExpr = 0;
131386     if( rc==SQLITE_TOOBIG ){
131387       *pzErr = sqlite3_mprintf(
131388           "FTS expression tree is too large (maximum depth %d)",
131389           SQLITE_FTS3_MAX_EXPR_DEPTH
131390       );
131391       rc = SQLITE_ERROR;
131392     }else if( rc==SQLITE_ERROR ){
131393       *pzErr = sqlite3_mprintf("malformed MATCH expression: [%s]", z);
131394     }
131395   }
131396 
131397   return rc;
131398 }
131399 
131400 /*
131401 ** Free a single node of an expression tree.
131402 */
131403 static void fts3FreeExprNode(Fts3Expr *p){
131404   assert( p->eType==FTSQUERY_PHRASE || p->pPhrase==0 );
131405   sqlite3Fts3EvalPhraseCleanup(p->pPhrase);
131406   sqlite3_free(p->aMI);
131407   sqlite3_free(p);
131408 }
131409 
131410 /*
131411 ** Free a parsed fts3 query expression allocated by sqlite3Fts3ExprParse().
131412 **
131413 ** This function would be simpler if it recursively called itself. But
131414 ** that would mean passing a sufficiently large expression to ExprParse()
131415 ** could cause a stack overflow.
131416 */
131417 SQLITE_PRIVATE void sqlite3Fts3ExprFree(Fts3Expr *pDel){
131418   Fts3Expr *p;
131419   assert( pDel==0 || pDel->pParent==0 );
131420   for(p=pDel; p && (p->pLeft||p->pRight); p=(p->pLeft ? p->pLeft : p->pRight)){
131421     assert( p->pParent==0 || p==p->pParent->pRight || p==p->pParent->pLeft );
131422   }
131423   while( p ){
131424     Fts3Expr *pParent = p->pParent;
131425     fts3FreeExprNode(p);
131426     if( pParent && p==pParent->pLeft && pParent->pRight ){
131427       p = pParent->pRight;
131428       while( p && (p->pLeft || p->pRight) ){
131429         assert( p==p->pParent->pRight || p==p->pParent->pLeft );
131430         p = (p->pLeft ? p->pLeft : p->pRight);
131431       }
131432     }else{
131433       p = pParent;
131434     }
131435   }
131436 }
131437 
131438 /****************************************************************************
131439 *****************************************************************************
131440 ** Everything after this point is just test code.
131441 */
131442 
131443 #ifdef SQLITE_TEST
131444 
131445 /* #include <stdio.h> */
131446 
131447 /*
131448 ** Function to query the hash-table of tokenizers (see README.tokenizers).
131449 */
131450 static int queryTestTokenizer(
131451   sqlite3 *db,
131452   const char *zName,
131453   const sqlite3_tokenizer_module **pp
131454 ){
131455   int rc;
131456   sqlite3_stmt *pStmt;
131457   const char zSql[] = "SELECT fts3_tokenizer(?)";
131458 
131459   *pp = 0;
131460   rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
131461   if( rc!=SQLITE_OK ){
131462     return rc;
131463   }
131464 
131465   sqlite3_bind_text(pStmt, 1, zName, -1, SQLITE_STATIC);
131466   if( SQLITE_ROW==sqlite3_step(pStmt) ){
131467     if( sqlite3_column_type(pStmt, 0)==SQLITE_BLOB ){
131468       memcpy((void *)pp, sqlite3_column_blob(pStmt, 0), sizeof(*pp));
131469     }
131470   }
131471 
131472   return sqlite3_finalize(pStmt);
131473 }
131474 
131475 /*
131476 ** Return a pointer to a buffer containing a text representation of the
131477 ** expression passed as the first argument. The buffer is obtained from
131478 ** sqlite3_malloc(). It is the responsibility of the caller to use
131479 ** sqlite3_free() to release the memory. If an OOM condition is encountered,
131480 ** NULL is returned.
131481 **
131482 ** If the second argument is not NULL, then its contents are prepended to
131483 ** the returned expression text and then freed using sqlite3_free().
131484 */
131485 static char *exprToString(Fts3Expr *pExpr, char *zBuf){
131486   if( pExpr==0 ){
131487     return sqlite3_mprintf("");
131488   }
131489   switch( pExpr->eType ){
131490     case FTSQUERY_PHRASE: {
131491       Fts3Phrase *pPhrase = pExpr->pPhrase;
131492       int i;
131493       zBuf = sqlite3_mprintf(
131494           "%zPHRASE %d 0", zBuf, pPhrase->iColumn);
131495       for(i=0; zBuf && i<pPhrase->nToken; i++){
131496         zBuf = sqlite3_mprintf("%z %.*s%s", zBuf,
131497             pPhrase->aToken[i].n, pPhrase->aToken[i].z,
131498             (pPhrase->aToken[i].isPrefix?"+":"")
131499         );
131500       }
131501       return zBuf;
131502     }
131503 
131504     case FTSQUERY_NEAR:
131505       zBuf = sqlite3_mprintf("%zNEAR/%d ", zBuf, pExpr->nNear);
131506       break;
131507     case FTSQUERY_NOT:
131508       zBuf = sqlite3_mprintf("%zNOT ", zBuf);
131509       break;
131510     case FTSQUERY_AND:
131511       zBuf = sqlite3_mprintf("%zAND ", zBuf);
131512       break;
131513     case FTSQUERY_OR:
131514       zBuf = sqlite3_mprintf("%zOR ", zBuf);
131515       break;
131516   }
131517 
131518   if( zBuf ) zBuf = sqlite3_mprintf("%z{", zBuf);
131519   if( zBuf ) zBuf = exprToString(pExpr->pLeft, zBuf);
131520   if( zBuf ) zBuf = sqlite3_mprintf("%z} {", zBuf);
131521 
131522   if( zBuf ) zBuf = exprToString(pExpr->pRight, zBuf);
131523   if( zBuf ) zBuf = sqlite3_mprintf("%z}", zBuf);
131524 
131525   return zBuf;
131526 }
131527 
131528 /*
131529 ** This is the implementation of a scalar SQL function used to test the
131530 ** expression parser. It should be called as follows:
131531 **
131532 **   fts3_exprtest(<tokenizer>, <expr>, <column 1>, ...);
131533 **
131534 ** The first argument, <tokenizer>, is the name of the fts3 tokenizer used
131535 ** to parse the query expression (see README.tokenizers). The second argument
131536 ** is the query expression to parse. Each subsequent argument is the name
131537 ** of a column of the fts3 table that the query expression may refer to.
131538 ** For example:
131539 **
131540 **   SELECT fts3_exprtest('simple', 'Bill col2:Bloggs', 'col1', 'col2');
131541 */
131542 static void fts3ExprTest(
131543   sqlite3_context *context,
131544   int argc,
131545   sqlite3_value **argv
131546 ){
131547   sqlite3_tokenizer_module const *pModule = 0;
131548   sqlite3_tokenizer *pTokenizer = 0;
131549   int rc;
131550   char **azCol = 0;
131551   const char *zExpr;
131552   int nExpr;
131553   int nCol;
131554   int ii;
131555   Fts3Expr *pExpr;
131556   char *zBuf = 0;
131557   sqlite3 *db = sqlite3_context_db_handle(context);
131558 
131559   if( argc<3 ){
131560     sqlite3_result_error(context,
131561         "Usage: fts3_exprtest(tokenizer, expr, col1, ...", -1
131562     );
131563     return;
131564   }
131565 
131566   rc = queryTestTokenizer(db,
131567                           (const char *)sqlite3_value_text(argv[0]), &pModule);
131568   if( rc==SQLITE_NOMEM ){
131569     sqlite3_result_error_nomem(context);
131570     goto exprtest_out;
131571   }else if( !pModule ){
131572     sqlite3_result_error(context, "No such tokenizer module", -1);
131573     goto exprtest_out;
131574   }
131575 
131576   rc = pModule->xCreate(0, 0, &pTokenizer);
131577   assert( rc==SQLITE_NOMEM || rc==SQLITE_OK );
131578   if( rc==SQLITE_NOMEM ){
131579     sqlite3_result_error_nomem(context);
131580     goto exprtest_out;
131581   }
131582   pTokenizer->pModule = pModule;
131583 
131584   zExpr = (const char *)sqlite3_value_text(argv[1]);
131585   nExpr = sqlite3_value_bytes(argv[1]);
131586   nCol = argc-2;
131587   azCol = (char **)sqlite3_malloc(nCol*sizeof(char *));
131588   if( !azCol ){
131589     sqlite3_result_error_nomem(context);
131590     goto exprtest_out;
131591   }
131592   for(ii=0; ii<nCol; ii++){
131593     azCol[ii] = (char *)sqlite3_value_text(argv[ii+2]);
131594   }
131595 
131596   if( sqlite3_user_data(context) ){
131597     char *zDummy = 0;
131598     rc = sqlite3Fts3ExprParse(
131599         pTokenizer, 0, azCol, 0, nCol, nCol, zExpr, nExpr, &pExpr, &zDummy
131600     );
131601     assert( rc==SQLITE_OK || pExpr==0 );
131602     sqlite3_free(zDummy);
131603   }else{
131604     rc = fts3ExprParseUnbalanced(
131605         pTokenizer, 0, azCol, 0, nCol, nCol, zExpr, nExpr, &pExpr
131606     );
131607   }
131608 
131609   if( rc!=SQLITE_OK && rc!=SQLITE_NOMEM ){
131610     sqlite3Fts3ExprFree(pExpr);
131611     sqlite3_result_error(context, "Error parsing expression", -1);
131612   }else if( rc==SQLITE_NOMEM || !(zBuf = exprToString(pExpr, 0)) ){
131613     sqlite3_result_error_nomem(context);
131614   }else{
131615     sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
131616     sqlite3_free(zBuf);
131617   }
131618 
131619   sqlite3Fts3ExprFree(pExpr);
131620 
131621 exprtest_out:
131622   if( pModule && pTokenizer ){
131623     rc = pModule->xDestroy(pTokenizer);
131624   }
131625   sqlite3_free(azCol);
131626 }
131627 
131628 /*
131629 ** Register the query expression parser test function fts3_exprtest()
131630 ** with database connection db.
131631 */
131632 SQLITE_PRIVATE int sqlite3Fts3ExprInitTestInterface(sqlite3* db){
131633   int rc = sqlite3_create_function(
131634       db, "fts3_exprtest", -1, SQLITE_UTF8, 0, fts3ExprTest, 0, 0
131635   );
131636   if( rc==SQLITE_OK ){
131637     rc = sqlite3_create_function(db, "fts3_exprtest_rebalance",
131638         -1, SQLITE_UTF8, (void *)1, fts3ExprTest, 0, 0
131639     );
131640   }
131641   return rc;
131642 }
131643 
131644 #endif
131645 #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
131646 
131647 /************** End of fts3_expr.c *******************************************/
131648 /************** Begin file fts3_hash.c ***************************************/
131649 /*
131650 ** 2001 September 22
131651 **
131652 ** The author disclaims copyright to this source code.  In place of
131653 ** a legal notice, here is a blessing:
131654 **
131655 **    May you do good and not evil.
131656 **    May you find forgiveness for yourself and forgive others.
131657 **    May you share freely, never taking more than you give.
131658 **
131659 *************************************************************************
131660 ** This is the implementation of generic hash-tables used in SQLite.
131661 ** We've modified it slightly to serve as a standalone hash table
131662 ** implementation for the full-text indexing module.
131663 */
131664 
131665 /*
131666 ** The code in this file is only compiled if:
131667 **
131668 **     * The FTS3 module is being built as an extension
131669 **       (in which case SQLITE_CORE is not defined), or
131670 **
131671 **     * The FTS3 module is being built into the core of
131672 **       SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
131673 */
131674 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
131675 
131676 /* #include <assert.h> */
131677 /* #include <stdlib.h> */
131678 /* #include <string.h> */
131679 
131680 
131681 /*
131682 ** Malloc and Free functions
131683 */
131684 static void *fts3HashMalloc(int n){
131685   void *p = sqlite3_malloc(n);
131686   if( p ){
131687     memset(p, 0, n);
131688   }
131689   return p;
131690 }
131691 static void fts3HashFree(void *p){
131692   sqlite3_free(p);
131693 }
131694 
131695 /* Turn bulk memory into a hash table object by initializing the
131696 ** fields of the Hash structure.
131697 **
131698 ** "pNew" is a pointer to the hash table that is to be initialized.
131699 ** keyClass is one of the constants
131700 ** FTS3_HASH_BINARY or FTS3_HASH_STRING.  The value of keyClass
131701 ** determines what kind of key the hash table will use.  "copyKey" is
131702 ** true if the hash table should make its own private copy of keys and
131703 ** false if it should just use the supplied pointer.
131704 */
131705 SQLITE_PRIVATE void sqlite3Fts3HashInit(Fts3Hash *pNew, char keyClass, char copyKey){
131706   assert( pNew!=0 );
131707   assert( keyClass>=FTS3_HASH_STRING && keyClass<=FTS3_HASH_BINARY );
131708   pNew->keyClass = keyClass;
131709   pNew->copyKey = copyKey;
131710   pNew->first = 0;
131711   pNew->count = 0;
131712   pNew->htsize = 0;
131713   pNew->ht = 0;
131714 }
131715 
131716 /* Remove all entries from a hash table.  Reclaim all memory.
131717 ** Call this routine to delete a hash table or to reset a hash table
131718 ** to the empty state.
131719 */
131720 SQLITE_PRIVATE void sqlite3Fts3HashClear(Fts3Hash *pH){
131721   Fts3HashElem *elem;         /* For looping over all elements of the table */
131722 
131723   assert( pH!=0 );
131724   elem = pH->first;
131725   pH->first = 0;
131726   fts3HashFree(pH->ht);
131727   pH->ht = 0;
131728   pH->htsize = 0;
131729   while( elem ){
131730     Fts3HashElem *next_elem = elem->next;
131731     if( pH->copyKey && elem->pKey ){
131732       fts3HashFree(elem->pKey);
131733     }
131734     fts3HashFree(elem);
131735     elem = next_elem;
131736   }
131737   pH->count = 0;
131738 }
131739 
131740 /*
131741 ** Hash and comparison functions when the mode is FTS3_HASH_STRING
131742 */
131743 static int fts3StrHash(const void *pKey, int nKey){
131744   const char *z = (const char *)pKey;
131745   unsigned h = 0;
131746   if( nKey<=0 ) nKey = (int) strlen(z);
131747   while( nKey > 0  ){
131748     h = (h<<3) ^ h ^ *z++;
131749     nKey--;
131750   }
131751   return (int)(h & 0x7fffffff);
131752 }
131753 static int fts3StrCompare(const void *pKey1, int n1, const void *pKey2, int n2){
131754   if( n1!=n2 ) return 1;
131755   return strncmp((const char*)pKey1,(const char*)pKey2,n1);
131756 }
131757 
131758 /*
131759 ** Hash and comparison functions when the mode is FTS3_HASH_BINARY
131760 */
131761 static int fts3BinHash(const void *pKey, int nKey){
131762   int h = 0;
131763   const char *z = (const char *)pKey;
131764   while( nKey-- > 0 ){
131765     h = (h<<3) ^ h ^ *(z++);
131766   }
131767   return h & 0x7fffffff;
131768 }
131769 static int fts3BinCompare(const void *pKey1, int n1, const void *pKey2, int n2){
131770   if( n1!=n2 ) return 1;
131771   return memcmp(pKey1,pKey2,n1);
131772 }
131773 
131774 /*
131775 ** Return a pointer to the appropriate hash function given the key class.
131776 **
131777 ** The C syntax in this function definition may be unfamilar to some
131778 ** programmers, so we provide the following additional explanation:
131779 **
131780 ** The name of the function is "ftsHashFunction".  The function takes a
131781 ** single parameter "keyClass".  The return value of ftsHashFunction()
131782 ** is a pointer to another function.  Specifically, the return value
131783 ** of ftsHashFunction() is a pointer to a function that takes two parameters
131784 ** with types "const void*" and "int" and returns an "int".
131785 */
131786 static int (*ftsHashFunction(int keyClass))(const void*,int){
131787   if( keyClass==FTS3_HASH_STRING ){
131788     return &fts3StrHash;
131789   }else{
131790     assert( keyClass==FTS3_HASH_BINARY );
131791     return &fts3BinHash;
131792   }
131793 }
131794 
131795 /*
131796 ** Return a pointer to the appropriate hash function given the key class.
131797 **
131798 ** For help in interpreted the obscure C code in the function definition,
131799 ** see the header comment on the previous function.
131800 */
131801 static int (*ftsCompareFunction(int keyClass))(const void*,int,const void*,int){
131802   if( keyClass==FTS3_HASH_STRING ){
131803     return &fts3StrCompare;
131804   }else{
131805     assert( keyClass==FTS3_HASH_BINARY );
131806     return &fts3BinCompare;
131807   }
131808 }
131809 
131810 /* Link an element into the hash table
131811 */
131812 static void fts3HashInsertElement(
131813   Fts3Hash *pH,            /* The complete hash table */
131814   struct _fts3ht *pEntry,  /* The entry into which pNew is inserted */
131815   Fts3HashElem *pNew       /* The element to be inserted */
131816 ){
131817   Fts3HashElem *pHead;     /* First element already in pEntry */
131818   pHead = pEntry->chain;
131819   if( pHead ){
131820     pNew->next = pHead;
131821     pNew->prev = pHead->prev;
131822     if( pHead->prev ){ pHead->prev->next = pNew; }
131823     else             { pH->first = pNew; }
131824     pHead->prev = pNew;
131825   }else{
131826     pNew->next = pH->first;
131827     if( pH->first ){ pH->first->prev = pNew; }
131828     pNew->prev = 0;
131829     pH->first = pNew;
131830   }
131831   pEntry->count++;
131832   pEntry->chain = pNew;
131833 }
131834 
131835 
131836 /* Resize the hash table so that it cantains "new_size" buckets.
131837 ** "new_size" must be a power of 2.  The hash table might fail
131838 ** to resize if sqliteMalloc() fails.
131839 **
131840 ** Return non-zero if a memory allocation error occurs.
131841 */
131842 static int fts3Rehash(Fts3Hash *pH, int new_size){
131843   struct _fts3ht *new_ht;          /* The new hash table */
131844   Fts3HashElem *elem, *next_elem;  /* For looping over existing elements */
131845   int (*xHash)(const void*,int);   /* The hash function */
131846 
131847   assert( (new_size & (new_size-1))==0 );
131848   new_ht = (struct _fts3ht *)fts3HashMalloc( new_size*sizeof(struct _fts3ht) );
131849   if( new_ht==0 ) return 1;
131850   fts3HashFree(pH->ht);
131851   pH->ht = new_ht;
131852   pH->htsize = new_size;
131853   xHash = ftsHashFunction(pH->keyClass);
131854   for(elem=pH->first, pH->first=0; elem; elem = next_elem){
131855     int h = (*xHash)(elem->pKey, elem->nKey) & (new_size-1);
131856     next_elem = elem->next;
131857     fts3HashInsertElement(pH, &new_ht[h], elem);
131858   }
131859   return 0;
131860 }
131861 
131862 /* This function (for internal use only) locates an element in an
131863 ** hash table that matches the given key.  The hash for this key has
131864 ** already been computed and is passed as the 4th parameter.
131865 */
131866 static Fts3HashElem *fts3FindElementByHash(
131867   const Fts3Hash *pH, /* The pH to be searched */
131868   const void *pKey,   /* The key we are searching for */
131869   int nKey,
131870   int h               /* The hash for this key. */
131871 ){
131872   Fts3HashElem *elem;            /* Used to loop thru the element list */
131873   int count;                     /* Number of elements left to test */
131874   int (*xCompare)(const void*,int,const void*,int);  /* comparison function */
131875 
131876   if( pH->ht ){
131877     struct _fts3ht *pEntry = &pH->ht[h];
131878     elem = pEntry->chain;
131879     count = pEntry->count;
131880     xCompare = ftsCompareFunction(pH->keyClass);
131881     while( count-- && elem ){
131882       if( (*xCompare)(elem->pKey,elem->nKey,pKey,nKey)==0 ){
131883         return elem;
131884       }
131885       elem = elem->next;
131886     }
131887   }
131888   return 0;
131889 }
131890 
131891 /* Remove a single entry from the hash table given a pointer to that
131892 ** element and a hash on the element's key.
131893 */
131894 static void fts3RemoveElementByHash(
131895   Fts3Hash *pH,         /* The pH containing "elem" */
131896   Fts3HashElem* elem,   /* The element to be removed from the pH */
131897   int h                 /* Hash value for the element */
131898 ){
131899   struct _fts3ht *pEntry;
131900   if( elem->prev ){
131901     elem->prev->next = elem->next;
131902   }else{
131903     pH->first = elem->next;
131904   }
131905   if( elem->next ){
131906     elem->next->prev = elem->prev;
131907   }
131908   pEntry = &pH->ht[h];
131909   if( pEntry->chain==elem ){
131910     pEntry->chain = elem->next;
131911   }
131912   pEntry->count--;
131913   if( pEntry->count<=0 ){
131914     pEntry->chain = 0;
131915   }
131916   if( pH->copyKey && elem->pKey ){
131917     fts3HashFree(elem->pKey);
131918   }
131919   fts3HashFree( elem );
131920   pH->count--;
131921   if( pH->count<=0 ){
131922     assert( pH->first==0 );
131923     assert( pH->count==0 );
131924     fts3HashClear(pH);
131925   }
131926 }
131927 
131928 SQLITE_PRIVATE Fts3HashElem *sqlite3Fts3HashFindElem(
131929   const Fts3Hash *pH,
131930   const void *pKey,
131931   int nKey
131932 ){
131933   int h;                          /* A hash on key */
131934   int (*xHash)(const void*,int);  /* The hash function */
131935 
131936   if( pH==0 || pH->ht==0 ) return 0;
131937   xHash = ftsHashFunction(pH->keyClass);
131938   assert( xHash!=0 );
131939   h = (*xHash)(pKey,nKey);
131940   assert( (pH->htsize & (pH->htsize-1))==0 );
131941   return fts3FindElementByHash(pH,pKey,nKey, h & (pH->htsize-1));
131942 }
131943 
131944 /*
131945 ** Attempt to locate an element of the hash table pH with a key
131946 ** that matches pKey,nKey.  Return the data for this element if it is
131947 ** found, or NULL if there is no match.
131948 */
131949 SQLITE_PRIVATE void *sqlite3Fts3HashFind(const Fts3Hash *pH, const void *pKey, int nKey){
131950   Fts3HashElem *pElem;            /* The element that matches key (if any) */
131951 
131952   pElem = sqlite3Fts3HashFindElem(pH, pKey, nKey);
131953   return pElem ? pElem->data : 0;
131954 }
131955 
131956 /* Insert an element into the hash table pH.  The key is pKey,nKey
131957 ** and the data is "data".
131958 **
131959 ** If no element exists with a matching key, then a new
131960 ** element is created.  A copy of the key is made if the copyKey
131961 ** flag is set.  NULL is returned.
131962 **
131963 ** If another element already exists with the same key, then the
131964 ** new data replaces the old data and the old data is returned.
131965 ** The key is not copied in this instance.  If a malloc fails, then
131966 ** the new data is returned and the hash table is unchanged.
131967 **
131968 ** If the "data" parameter to this function is NULL, then the
131969 ** element corresponding to "key" is removed from the hash table.
131970 */
131971 SQLITE_PRIVATE void *sqlite3Fts3HashInsert(
131972   Fts3Hash *pH,        /* The hash table to insert into */
131973   const void *pKey,    /* The key */
131974   int nKey,            /* Number of bytes in the key */
131975   void *data           /* The data */
131976 ){
131977   int hraw;                 /* Raw hash value of the key */
131978   int h;                    /* the hash of the key modulo hash table size */
131979   Fts3HashElem *elem;       /* Used to loop thru the element list */
131980   Fts3HashElem *new_elem;   /* New element added to the pH */
131981   int (*xHash)(const void*,int);  /* The hash function */
131982 
131983   assert( pH!=0 );
131984   xHash = ftsHashFunction(pH->keyClass);
131985   assert( xHash!=0 );
131986   hraw = (*xHash)(pKey, nKey);
131987   assert( (pH->htsize & (pH->htsize-1))==0 );
131988   h = hraw & (pH->htsize-1);
131989   elem = fts3FindElementByHash(pH,pKey,nKey,h);
131990   if( elem ){
131991     void *old_data = elem->data;
131992     if( data==0 ){
131993       fts3RemoveElementByHash(pH,elem,h);
131994     }else{
131995       elem->data = data;
131996     }
131997     return old_data;
131998   }
131999   if( data==0 ) return 0;
132000   if( (pH->htsize==0 && fts3Rehash(pH,8))
132001    || (pH->count>=pH->htsize && fts3Rehash(pH, pH->htsize*2))
132002   ){
132003     pH->count = 0;
132004     return data;
132005   }
132006   assert( pH->htsize>0 );
132007   new_elem = (Fts3HashElem*)fts3HashMalloc( sizeof(Fts3HashElem) );
132008   if( new_elem==0 ) return data;
132009   if( pH->copyKey && pKey!=0 ){
132010     new_elem->pKey = fts3HashMalloc( nKey );
132011     if( new_elem->pKey==0 ){
132012       fts3HashFree(new_elem);
132013       return data;
132014     }
132015     memcpy((void*)new_elem->pKey, pKey, nKey);
132016   }else{
132017     new_elem->pKey = (void*)pKey;
132018   }
132019   new_elem->nKey = nKey;
132020   pH->count++;
132021   assert( pH->htsize>0 );
132022   assert( (pH->htsize & (pH->htsize-1))==0 );
132023   h = hraw & (pH->htsize-1);
132024   fts3HashInsertElement(pH, &pH->ht[h], new_elem);
132025   new_elem->data = data;
132026   return 0;
132027 }
132028 
132029 #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
132030 
132031 /************** End of fts3_hash.c *******************************************/
132032 /************** Begin file fts3_porter.c *************************************/
132033 /*
132034 ** 2006 September 30
132035 **
132036 ** The author disclaims copyright to this source code.  In place of
132037 ** a legal notice, here is a blessing:
132038 **
132039 **    May you do good and not evil.
132040 **    May you find forgiveness for yourself and forgive others.
132041 **    May you share freely, never taking more than you give.
132042 **
132043 *************************************************************************
132044 ** Implementation of the full-text-search tokenizer that implements
132045 ** a Porter stemmer.
132046 */
132047 
132048 /*
132049 ** The code in this file is only compiled if:
132050 **
132051 **     * The FTS3 module is being built as an extension
132052 **       (in which case SQLITE_CORE is not defined), or
132053 **
132054 **     * The FTS3 module is being built into the core of
132055 **       SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
132056 */
132057 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
132058 
132059 /* #include <assert.h> */
132060 /* #include <stdlib.h> */
132061 /* #include <stdio.h> */
132062 /* #include <string.h> */
132063 
132064 
132065 /*
132066 ** Class derived from sqlite3_tokenizer
132067 */
132068 typedef struct porter_tokenizer {
132069   sqlite3_tokenizer base;      /* Base class */
132070 } porter_tokenizer;
132071 
132072 /*
132073 ** Class derived from sqlite3_tokenizer_cursor
132074 */
132075 typedef struct porter_tokenizer_cursor {
132076   sqlite3_tokenizer_cursor base;
132077   const char *zInput;          /* input we are tokenizing */
132078   int nInput;                  /* size of the input */
132079   int iOffset;                 /* current position in zInput */
132080   int iToken;                  /* index of next token to be returned */
132081   char *zToken;                /* storage for current token */
132082   int nAllocated;              /* space allocated to zToken buffer */
132083 } porter_tokenizer_cursor;
132084 
132085 
132086 /*
132087 ** Create a new tokenizer instance.
132088 */
132089 static int porterCreate(
132090   int argc, const char * const *argv,
132091   sqlite3_tokenizer **ppTokenizer
132092 ){
132093   porter_tokenizer *t;
132094 
132095   UNUSED_PARAMETER(argc);
132096   UNUSED_PARAMETER(argv);
132097 
132098   t = (porter_tokenizer *) sqlite3_malloc(sizeof(*t));
132099   if( t==NULL ) return SQLITE_NOMEM;
132100   memset(t, 0, sizeof(*t));
132101   *ppTokenizer = &t->base;
132102   return SQLITE_OK;
132103 }
132104 
132105 /*
132106 ** Destroy a tokenizer
132107 */
132108 static int porterDestroy(sqlite3_tokenizer *pTokenizer){
132109   sqlite3_free(pTokenizer);
132110   return SQLITE_OK;
132111 }
132112 
132113 /*
132114 ** Prepare to begin tokenizing a particular string.  The input
132115 ** string to be tokenized is zInput[0..nInput-1].  A cursor
132116 ** used to incrementally tokenize this string is returned in
132117 ** *ppCursor.
132118 */
132119 static int porterOpen(
132120   sqlite3_tokenizer *pTokenizer,         /* The tokenizer */
132121   const char *zInput, int nInput,        /* String to be tokenized */
132122   sqlite3_tokenizer_cursor **ppCursor    /* OUT: Tokenization cursor */
132123 ){
132124   porter_tokenizer_cursor *c;
132125 
132126   UNUSED_PARAMETER(pTokenizer);
132127 
132128   c = (porter_tokenizer_cursor *) sqlite3_malloc(sizeof(*c));
132129   if( c==NULL ) return SQLITE_NOMEM;
132130 
132131   c->zInput = zInput;
132132   if( zInput==0 ){
132133     c->nInput = 0;
132134   }else if( nInput<0 ){
132135     c->nInput = (int)strlen(zInput);
132136   }else{
132137     c->nInput = nInput;
132138   }
132139   c->iOffset = 0;                 /* start tokenizing at the beginning */
132140   c->iToken = 0;
132141   c->zToken = NULL;               /* no space allocated, yet. */
132142   c->nAllocated = 0;
132143 
132144   *ppCursor = &c->base;
132145   return SQLITE_OK;
132146 }
132147 
132148 /*
132149 ** Close a tokenization cursor previously opened by a call to
132150 ** porterOpen() above.
132151 */
132152 static int porterClose(sqlite3_tokenizer_cursor *pCursor){
132153   porter_tokenizer_cursor *c = (porter_tokenizer_cursor *) pCursor;
132154   sqlite3_free(c->zToken);
132155   sqlite3_free(c);
132156   return SQLITE_OK;
132157 }
132158 /*
132159 ** Vowel or consonant
132160 */
132161 static const char cType[] = {
132162    0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0,
132163    1, 1, 1, 2, 1
132164 };
132165 
132166 /*
132167 ** isConsonant() and isVowel() determine if their first character in
132168 ** the string they point to is a consonant or a vowel, according
132169 ** to Porter ruls.
132170 **
132171 ** A consonate is any letter other than 'a', 'e', 'i', 'o', or 'u'.
132172 ** 'Y' is a consonant unless it follows another consonant,
132173 ** in which case it is a vowel.
132174 **
132175 ** In these routine, the letters are in reverse order.  So the 'y' rule
132176 ** is that 'y' is a consonant unless it is followed by another
132177 ** consonent.
132178 */
132179 static int isVowel(const char*);
132180 static int isConsonant(const char *z){
132181   int j;
132182   char x = *z;
132183   if( x==0 ) return 0;
132184   assert( x>='a' && x<='z' );
132185   j = cType[x-'a'];
132186   if( j<2 ) return j;
132187   return z[1]==0 || isVowel(z + 1);
132188 }
132189 static int isVowel(const char *z){
132190   int j;
132191   char x = *z;
132192   if( x==0 ) return 0;
132193   assert( x>='a' && x<='z' );
132194   j = cType[x-'a'];
132195   if( j<2 ) return 1-j;
132196   return isConsonant(z + 1);
132197 }
132198 
132199 /*
132200 ** Let any sequence of one or more vowels be represented by V and let
132201 ** C be sequence of one or more consonants.  Then every word can be
132202 ** represented as:
132203 **
132204 **           [C] (VC){m} [V]
132205 **
132206 ** In prose:  A word is an optional consonant followed by zero or
132207 ** vowel-consonant pairs followed by an optional vowel.  "m" is the
132208 ** number of vowel consonant pairs.  This routine computes the value
132209 ** of m for the first i bytes of a word.
132210 **
132211 ** Return true if the m-value for z is 1 or more.  In other words,
132212 ** return true if z contains at least one vowel that is followed
132213 ** by a consonant.
132214 **
132215 ** In this routine z[] is in reverse order.  So we are really looking
132216 ** for an instance of of a consonant followed by a vowel.
132217 */
132218 static int m_gt_0(const char *z){
132219   while( isVowel(z) ){ z++; }
132220   if( *z==0 ) return 0;
132221   while( isConsonant(z) ){ z++; }
132222   return *z!=0;
132223 }
132224 
132225 /* Like mgt0 above except we are looking for a value of m which is
132226 ** exactly 1
132227 */
132228 static int m_eq_1(const char *z){
132229   while( isVowel(z) ){ z++; }
132230   if( *z==0 ) return 0;
132231   while( isConsonant(z) ){ z++; }
132232   if( *z==0 ) return 0;
132233   while( isVowel(z) ){ z++; }
132234   if( *z==0 ) return 1;
132235   while( isConsonant(z) ){ z++; }
132236   return *z==0;
132237 }
132238 
132239 /* Like mgt0 above except we are looking for a value of m>1 instead
132240 ** or m>0
132241 */
132242 static int m_gt_1(const char *z){
132243   while( isVowel(z) ){ z++; }
132244   if( *z==0 ) return 0;
132245   while( isConsonant(z) ){ z++; }
132246   if( *z==0 ) return 0;
132247   while( isVowel(z) ){ z++; }
132248   if( *z==0 ) return 0;
132249   while( isConsonant(z) ){ z++; }
132250   return *z!=0;
132251 }
132252 
132253 /*
132254 ** Return TRUE if there is a vowel anywhere within z[0..n-1]
132255 */
132256 static int hasVowel(const char *z){
132257   while( isConsonant(z) ){ z++; }
132258   return *z!=0;
132259 }
132260 
132261 /*
132262 ** Return TRUE if the word ends in a double consonant.
132263 **
132264 ** The text is reversed here. So we are really looking at
132265 ** the first two characters of z[].
132266 */
132267 static int doubleConsonant(const char *z){
132268   return isConsonant(z) && z[0]==z[1];
132269 }
132270 
132271 /*
132272 ** Return TRUE if the word ends with three letters which
132273 ** are consonant-vowel-consonent and where the final consonant
132274 ** is not 'w', 'x', or 'y'.
132275 **
132276 ** The word is reversed here.  So we are really checking the
132277 ** first three letters and the first one cannot be in [wxy].
132278 */
132279 static int star_oh(const char *z){
132280   return
132281     isConsonant(z) &&
132282     z[0]!='w' && z[0]!='x' && z[0]!='y' &&
132283     isVowel(z+1) &&
132284     isConsonant(z+2);
132285 }
132286 
132287 /*
132288 ** If the word ends with zFrom and xCond() is true for the stem
132289 ** of the word that preceeds the zFrom ending, then change the
132290 ** ending to zTo.
132291 **
132292 ** The input word *pz and zFrom are both in reverse order.  zTo
132293 ** is in normal order.
132294 **
132295 ** Return TRUE if zFrom matches.  Return FALSE if zFrom does not
132296 ** match.  Not that TRUE is returned even if xCond() fails and
132297 ** no substitution occurs.
132298 */
132299 static int stem(
132300   char **pz,             /* The word being stemmed (Reversed) */
132301   const char *zFrom,     /* If the ending matches this... (Reversed) */
132302   const char *zTo,       /* ... change the ending to this (not reversed) */
132303   int (*xCond)(const char*)   /* Condition that must be true */
132304 ){
132305   char *z = *pz;
132306   while( *zFrom && *zFrom==*z ){ z++; zFrom++; }
132307   if( *zFrom!=0 ) return 0;
132308   if( xCond && !xCond(z) ) return 1;
132309   while( *zTo ){
132310     *(--z) = *(zTo++);
132311   }
132312   *pz = z;
132313   return 1;
132314 }
132315 
132316 /*
132317 ** This is the fallback stemmer used when the porter stemmer is
132318 ** inappropriate.  The input word is copied into the output with
132319 ** US-ASCII case folding.  If the input word is too long (more
132320 ** than 20 bytes if it contains no digits or more than 6 bytes if
132321 ** it contains digits) then word is truncated to 20 or 6 bytes
132322 ** by taking 10 or 3 bytes from the beginning and end.
132323 */
132324 static void copy_stemmer(const char *zIn, int nIn, char *zOut, int *pnOut){
132325   int i, mx, j;
132326   int hasDigit = 0;
132327   for(i=0; i<nIn; i++){
132328     char c = zIn[i];
132329     if( c>='A' && c<='Z' ){
132330       zOut[i] = c - 'A' + 'a';
132331     }else{
132332       if( c>='0' && c<='9' ) hasDigit = 1;
132333       zOut[i] = c;
132334     }
132335   }
132336   mx = hasDigit ? 3 : 10;
132337   if( nIn>mx*2 ){
132338     for(j=mx, i=nIn-mx; i<nIn; i++, j++){
132339       zOut[j] = zOut[i];
132340     }
132341     i = j;
132342   }
132343   zOut[i] = 0;
132344   *pnOut = i;
132345 }
132346 
132347 
132348 /*
132349 ** Stem the input word zIn[0..nIn-1].  Store the output in zOut.
132350 ** zOut is at least big enough to hold nIn bytes.  Write the actual
132351 ** size of the output word (exclusive of the '\0' terminator) into *pnOut.
132352 **
132353 ** Any upper-case characters in the US-ASCII character set ([A-Z])
132354 ** are converted to lower case.  Upper-case UTF characters are
132355 ** unchanged.
132356 **
132357 ** Words that are longer than about 20 bytes are stemmed by retaining
132358 ** a few bytes from the beginning and the end of the word.  If the
132359 ** word contains digits, 3 bytes are taken from the beginning and
132360 ** 3 bytes from the end.  For long words without digits, 10 bytes
132361 ** are taken from each end.  US-ASCII case folding still applies.
132362 **
132363 ** If the input word contains not digits but does characters not
132364 ** in [a-zA-Z] then no stemming is attempted and this routine just
132365 ** copies the input into the input into the output with US-ASCII
132366 ** case folding.
132367 **
132368 ** Stemming never increases the length of the word.  So there is
132369 ** no chance of overflowing the zOut buffer.
132370 */
132371 static void porter_stemmer(const char *zIn, int nIn, char *zOut, int *pnOut){
132372   int i, j;
132373   char zReverse[28];
132374   char *z, *z2;
132375   if( nIn<3 || nIn>=(int)sizeof(zReverse)-7 ){
132376     /* The word is too big or too small for the porter stemmer.
132377     ** Fallback to the copy stemmer */
132378     copy_stemmer(zIn, nIn, zOut, pnOut);
132379     return;
132380   }
132381   for(i=0, j=sizeof(zReverse)-6; i<nIn; i++, j--){
132382     char c = zIn[i];
132383     if( c>='A' && c<='Z' ){
132384       zReverse[j] = c + 'a' - 'A';
132385     }else if( c>='a' && c<='z' ){
132386       zReverse[j] = c;
132387     }else{
132388       /* The use of a character not in [a-zA-Z] means that we fallback
132389       ** to the copy stemmer */
132390       copy_stemmer(zIn, nIn, zOut, pnOut);
132391       return;
132392     }
132393   }
132394   memset(&zReverse[sizeof(zReverse)-5], 0, 5);
132395   z = &zReverse[j+1];
132396 
132397 
132398   /* Step 1a */
132399   if( z[0]=='s' ){
132400     if(
132401      !stem(&z, "sess", "ss", 0) &&
132402      !stem(&z, "sei", "i", 0)  &&
132403      !stem(&z, "ss", "ss", 0)
132404     ){
132405       z++;
132406     }
132407   }
132408 
132409   /* Step 1b */
132410   z2 = z;
132411   if( stem(&z, "dee", "ee", m_gt_0) ){
132412     /* Do nothing.  The work was all in the test */
132413   }else if(
132414      (stem(&z, "gni", "", hasVowel) || stem(&z, "de", "", hasVowel))
132415       && z!=z2
132416   ){
132417      if( stem(&z, "ta", "ate", 0) ||
132418          stem(&z, "lb", "ble", 0) ||
132419          stem(&z, "zi", "ize", 0) ){
132420        /* Do nothing.  The work was all in the test */
132421      }else if( doubleConsonant(z) && (*z!='l' && *z!='s' && *z!='z') ){
132422        z++;
132423      }else if( m_eq_1(z) && star_oh(z) ){
132424        *(--z) = 'e';
132425      }
132426   }
132427 
132428   /* Step 1c */
132429   if( z[0]=='y' && hasVowel(z+1) ){
132430     z[0] = 'i';
132431   }
132432 
132433   /* Step 2 */
132434   switch( z[1] ){
132435    case 'a':
132436      if( !stem(&z, "lanoita", "ate", m_gt_0) ){
132437        stem(&z, "lanoit", "tion", m_gt_0);
132438      }
132439      break;
132440    case 'c':
132441      if( !stem(&z, "icne", "ence", m_gt_0) ){
132442        stem(&z, "icna", "ance", m_gt_0);
132443      }
132444      break;
132445    case 'e':
132446      stem(&z, "rezi", "ize", m_gt_0);
132447      break;
132448    case 'g':
132449      stem(&z, "igol", "log", m_gt_0);
132450      break;
132451    case 'l':
132452      if( !stem(&z, "ilb", "ble", m_gt_0)
132453       && !stem(&z, "illa", "al", m_gt_0)
132454       && !stem(&z, "iltne", "ent", m_gt_0)
132455       && !stem(&z, "ile", "e", m_gt_0)
132456      ){
132457        stem(&z, "ilsuo", "ous", m_gt_0);
132458      }
132459      break;
132460    case 'o':
132461      if( !stem(&z, "noitazi", "ize", m_gt_0)
132462       && !stem(&z, "noita", "ate", m_gt_0)
132463      ){
132464        stem(&z, "rota", "ate", m_gt_0);
132465      }
132466      break;
132467    case 's':
132468      if( !stem(&z, "msila", "al", m_gt_0)
132469       && !stem(&z, "ssenevi", "ive", m_gt_0)
132470       && !stem(&z, "ssenluf", "ful", m_gt_0)
132471      ){
132472        stem(&z, "ssensuo", "ous", m_gt_0);
132473      }
132474      break;
132475    case 't':
132476      if( !stem(&z, "itila", "al", m_gt_0)
132477       && !stem(&z, "itivi", "ive", m_gt_0)
132478      ){
132479        stem(&z, "itilib", "ble", m_gt_0);
132480      }
132481      break;
132482   }
132483 
132484   /* Step 3 */
132485   switch( z[0] ){
132486    case 'e':
132487      if( !stem(&z, "etaci", "ic", m_gt_0)
132488       && !stem(&z, "evita", "", m_gt_0)
132489      ){
132490        stem(&z, "ezila", "al", m_gt_0);
132491      }
132492      break;
132493    case 'i':
132494      stem(&z, "itici", "ic", m_gt_0);
132495      break;
132496    case 'l':
132497      if( !stem(&z, "laci", "ic", m_gt_0) ){
132498        stem(&z, "luf", "", m_gt_0);
132499      }
132500      break;
132501    case 's':
132502      stem(&z, "ssen", "", m_gt_0);
132503      break;
132504   }
132505 
132506   /* Step 4 */
132507   switch( z[1] ){
132508    case 'a':
132509      if( z[0]=='l' && m_gt_1(z+2) ){
132510        z += 2;
132511      }
132512      break;
132513    case 'c':
132514      if( z[0]=='e' && z[2]=='n' && (z[3]=='a' || z[3]=='e')  && m_gt_1(z+4)  ){
132515        z += 4;
132516      }
132517      break;
132518    case 'e':
132519      if( z[0]=='r' && m_gt_1(z+2) ){
132520        z += 2;
132521      }
132522      break;
132523    case 'i':
132524      if( z[0]=='c' && m_gt_1(z+2) ){
132525        z += 2;
132526      }
132527      break;
132528    case 'l':
132529      if( z[0]=='e' && z[2]=='b' && (z[3]=='a' || z[3]=='i') && m_gt_1(z+4) ){
132530        z += 4;
132531      }
132532      break;
132533    case 'n':
132534      if( z[0]=='t' ){
132535        if( z[2]=='a' ){
132536          if( m_gt_1(z+3) ){
132537            z += 3;
132538          }
132539        }else if( z[2]=='e' ){
132540          if( !stem(&z, "tneme", "", m_gt_1)
132541           && !stem(&z, "tnem", "", m_gt_1)
132542          ){
132543            stem(&z, "tne", "", m_gt_1);
132544          }
132545        }
132546      }
132547      break;
132548    case 'o':
132549      if( z[0]=='u' ){
132550        if( m_gt_1(z+2) ){
132551          z += 2;
132552        }
132553      }else if( z[3]=='s' || z[3]=='t' ){
132554        stem(&z, "noi", "", m_gt_1);
132555      }
132556      break;
132557    case 's':
132558      if( z[0]=='m' && z[2]=='i' && m_gt_1(z+3) ){
132559        z += 3;
132560      }
132561      break;
132562    case 't':
132563      if( !stem(&z, "eta", "", m_gt_1) ){
132564        stem(&z, "iti", "", m_gt_1);
132565      }
132566      break;
132567    case 'u':
132568      if( z[0]=='s' && z[2]=='o' && m_gt_1(z+3) ){
132569        z += 3;
132570      }
132571      break;
132572    case 'v':
132573    case 'z':
132574      if( z[0]=='e' && z[2]=='i' && m_gt_1(z+3) ){
132575        z += 3;
132576      }
132577      break;
132578   }
132579 
132580   /* Step 5a */
132581   if( z[0]=='e' ){
132582     if( m_gt_1(z+1) ){
132583       z++;
132584     }else if( m_eq_1(z+1) && !star_oh(z+1) ){
132585       z++;
132586     }
132587   }
132588 
132589   /* Step 5b */
132590   if( m_gt_1(z) && z[0]=='l' && z[1]=='l' ){
132591     z++;
132592   }
132593 
132594   /* z[] is now the stemmed word in reverse order.  Flip it back
132595   ** around into forward order and return.
132596   */
132597   *pnOut = i = (int)strlen(z);
132598   zOut[i] = 0;
132599   while( *z ){
132600     zOut[--i] = *(z++);
132601   }
132602 }
132603 
132604 /*
132605 ** Characters that can be part of a token.  We assume any character
132606 ** whose value is greater than 0x80 (any UTF character) can be
132607 ** part of a token.  In other words, delimiters all must have
132608 ** values of 0x7f or lower.
132609 */
132610 static const char porterIdChar[] = {
132611 /* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xA xB xC xD xE xF */
132612     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,  /* 3x */
132613     0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /* 4x */
132614     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1,  /* 5x */
132615     0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /* 6x */
132616     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,  /* 7x */
132617 };
132618 #define isDelim(C) (((ch=C)&0x80)==0 && (ch<0x30 || !porterIdChar[ch-0x30]))
132619 
132620 /*
132621 ** Extract the next token from a tokenization cursor.  The cursor must
132622 ** have been opened by a prior call to porterOpen().
132623 */
132624 static int porterNext(
132625   sqlite3_tokenizer_cursor *pCursor,  /* Cursor returned by porterOpen */
132626   const char **pzToken,               /* OUT: *pzToken is the token text */
132627   int *pnBytes,                       /* OUT: Number of bytes in token */
132628   int *piStartOffset,                 /* OUT: Starting offset of token */
132629   int *piEndOffset,                   /* OUT: Ending offset of token */
132630   int *piPosition                     /* OUT: Position integer of token */
132631 ){
132632   porter_tokenizer_cursor *c = (porter_tokenizer_cursor *) pCursor;
132633   const char *z = c->zInput;
132634 
132635   while( c->iOffset<c->nInput ){
132636     int iStartOffset, ch;
132637 
132638     /* Scan past delimiter characters */
132639     while( c->iOffset<c->nInput && isDelim(z[c->iOffset]) ){
132640       c->iOffset++;
132641     }
132642 
132643     /* Count non-delimiter characters. */
132644     iStartOffset = c->iOffset;
132645     while( c->iOffset<c->nInput && !isDelim(z[c->iOffset]) ){
132646       c->iOffset++;
132647     }
132648 
132649     if( c->iOffset>iStartOffset ){
132650       int n = c->iOffset-iStartOffset;
132651       if( n>c->nAllocated ){
132652         char *pNew;
132653         c->nAllocated = n+20;
132654         pNew = sqlite3_realloc(c->zToken, c->nAllocated);
132655         if( !pNew ) return SQLITE_NOMEM;
132656         c->zToken = pNew;
132657       }
132658       porter_stemmer(&z[iStartOffset], n, c->zToken, pnBytes);
132659       *pzToken = c->zToken;
132660       *piStartOffset = iStartOffset;
132661       *piEndOffset = c->iOffset;
132662       *piPosition = c->iToken++;
132663       return SQLITE_OK;
132664     }
132665   }
132666   return SQLITE_DONE;
132667 }
132668 
132669 /*
132670 ** The set of routines that implement the porter-stemmer tokenizer
132671 */
132672 static const sqlite3_tokenizer_module porterTokenizerModule = {
132673   0,
132674   porterCreate,
132675   porterDestroy,
132676   porterOpen,
132677   porterClose,
132678   porterNext,
132679   0
132680 };
132681 
132682 /*
132683 ** Allocate a new porter tokenizer.  Return a pointer to the new
132684 ** tokenizer in *ppModule
132685 */
132686 SQLITE_PRIVATE void sqlite3Fts3PorterTokenizerModule(
132687   sqlite3_tokenizer_module const**ppModule
132688 ){
132689   *ppModule = &porterTokenizerModule;
132690 }
132691 
132692 #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
132693 
132694 /************** End of fts3_porter.c *****************************************/
132695 /************** Begin file fts3_tokenizer.c **********************************/
132696 /*
132697 ** 2007 June 22
132698 **
132699 ** The author disclaims copyright to this source code.  In place of
132700 ** a legal notice, here is a blessing:
132701 **
132702 **    May you do good and not evil.
132703 **    May you find forgiveness for yourself and forgive others.
132704 **    May you share freely, never taking more than you give.
132705 **
132706 ******************************************************************************
132707 **
132708 ** This is part of an SQLite module implementing full-text search.
132709 ** This particular file implements the generic tokenizer interface.
132710 */
132711 
132712 /*
132713 ** The code in this file is only compiled if:
132714 **
132715 **     * The FTS3 module is being built as an extension
132716 **       (in which case SQLITE_CORE is not defined), or
132717 **
132718 **     * The FTS3 module is being built into the core of
132719 **       SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
132720 */
132721 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
132722 
132723 /* #include <assert.h> */
132724 /* #include <string.h> */
132725 
132726 /*
132727 ** Implementation of the SQL scalar function for accessing the underlying
132728 ** hash table. This function may be called as follows:
132729 **
132730 **   SELECT <function-name>(<key-name>);
132731 **   SELECT <function-name>(<key-name>, <pointer>);
132732 **
132733 ** where <function-name> is the name passed as the second argument
132734 ** to the sqlite3Fts3InitHashTable() function (e.g. 'fts3_tokenizer').
132735 **
132736 ** If the <pointer> argument is specified, it must be a blob value
132737 ** containing a pointer to be stored as the hash data corresponding
132738 ** to the string <key-name>. If <pointer> is not specified, then
132739 ** the string <key-name> must already exist in the has table. Otherwise,
132740 ** an error is returned.
132741 **
132742 ** Whether or not the <pointer> argument is specified, the value returned
132743 ** is a blob containing the pointer stored as the hash data corresponding
132744 ** to string <key-name> (after the hash-table is updated, if applicable).
132745 */
132746 static void scalarFunc(
132747   sqlite3_context *context,
132748   int argc,
132749   sqlite3_value **argv
132750 ){
132751   Fts3Hash *pHash;
132752   void *pPtr = 0;
132753   const unsigned char *zName;
132754   int nName;
132755 
132756   assert( argc==1 || argc==2 );
132757 
132758   pHash = (Fts3Hash *)sqlite3_user_data(context);
132759 
132760   zName = sqlite3_value_text(argv[0]);
132761   nName = sqlite3_value_bytes(argv[0])+1;
132762 
132763   if( argc==2 ){
132764     void *pOld;
132765     int n = sqlite3_value_bytes(argv[1]);
132766     if( n!=sizeof(pPtr) ){
132767       sqlite3_result_error(context, "argument type mismatch", -1);
132768       return;
132769     }
132770     pPtr = *(void **)sqlite3_value_blob(argv[1]);
132771     pOld = sqlite3Fts3HashInsert(pHash, (void *)zName, nName, pPtr);
132772     if( pOld==pPtr ){
132773       sqlite3_result_error(context, "out of memory", -1);
132774       return;
132775     }
132776   }else{
132777     pPtr = sqlite3Fts3HashFind(pHash, zName, nName);
132778     if( !pPtr ){
132779       char *zErr = sqlite3_mprintf("unknown tokenizer: %s", zName);
132780       sqlite3_result_error(context, zErr, -1);
132781       sqlite3_free(zErr);
132782       return;
132783     }
132784   }
132785 
132786   sqlite3_result_blob(context, (void *)&pPtr, sizeof(pPtr), SQLITE_TRANSIENT);
132787 }
132788 
132789 SQLITE_PRIVATE int sqlite3Fts3IsIdChar(char c){
132790   static const char isFtsIdChar[] = {
132791       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  /* 0x */
132792       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  /* 1x */
132793       0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  /* 2x */
132794       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,  /* 3x */
132795       0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /* 4x */
132796       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1,  /* 5x */
132797       0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,  /* 6x */
132798       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,  /* 7x */
132799   };
132800   return (c&0x80 || isFtsIdChar[(int)(c)]);
132801 }
132802 
132803 SQLITE_PRIVATE const char *sqlite3Fts3NextToken(const char *zStr, int *pn){
132804   const char *z1;
132805   const char *z2 = 0;
132806 
132807   /* Find the start of the next token. */
132808   z1 = zStr;
132809   while( z2==0 ){
132810     char c = *z1;
132811     switch( c ){
132812       case '\0': return 0;        /* No more tokens here */
132813       case '\'':
132814       case '"':
132815       case '`': {
132816         z2 = z1;
132817         while( *++z2 && (*z2!=c || *++z2==c) );
132818         break;
132819       }
132820       case '[':
132821         z2 = &z1[1];
132822         while( *z2 && z2[0]!=']' ) z2++;
132823         if( *z2 ) z2++;
132824         break;
132825 
132826       default:
132827         if( sqlite3Fts3IsIdChar(*z1) ){
132828           z2 = &z1[1];
132829           while( sqlite3Fts3IsIdChar(*z2) ) z2++;
132830         }else{
132831           z1++;
132832         }
132833     }
132834   }
132835 
132836   *pn = (int)(z2-z1);
132837   return z1;
132838 }
132839 
132840 SQLITE_PRIVATE int sqlite3Fts3InitTokenizer(
132841   Fts3Hash *pHash,                /* Tokenizer hash table */
132842   const char *zArg,               /* Tokenizer name */
132843   sqlite3_tokenizer **ppTok,      /* OUT: Tokenizer (if applicable) */
132844   char **pzErr                    /* OUT: Set to malloced error message */
132845 ){
132846   int rc;
132847   char *z = (char *)zArg;
132848   int n = 0;
132849   char *zCopy;
132850   char *zEnd;                     /* Pointer to nul-term of zCopy */
132851   sqlite3_tokenizer_module *m;
132852 
132853   zCopy = sqlite3_mprintf("%s", zArg);
132854   if( !zCopy ) return SQLITE_NOMEM;
132855   zEnd = &zCopy[strlen(zCopy)];
132856 
132857   z = (char *)sqlite3Fts3NextToken(zCopy, &n);
132858   z[n] = '\0';
132859   sqlite3Fts3Dequote(z);
132860 
132861   m = (sqlite3_tokenizer_module *)sqlite3Fts3HashFind(pHash,z,(int)strlen(z)+1);
132862   if( !m ){
132863     *pzErr = sqlite3_mprintf("unknown tokenizer: %s", z);
132864     rc = SQLITE_ERROR;
132865   }else{
132866     char const **aArg = 0;
132867     int iArg = 0;
132868     z = &z[n+1];
132869     while( z<zEnd && (NULL!=(z = (char *)sqlite3Fts3NextToken(z, &n))) ){
132870       int nNew = sizeof(char *)*(iArg+1);
132871       char const **aNew = (const char **)sqlite3_realloc((void *)aArg, nNew);
132872       if( !aNew ){
132873         sqlite3_free(zCopy);
132874         sqlite3_free((void *)aArg);
132875         return SQLITE_NOMEM;
132876       }
132877       aArg = aNew;
132878       aArg[iArg++] = z;
132879       z[n] = '\0';
132880       sqlite3Fts3Dequote(z);
132881       z = &z[n+1];
132882     }
132883     rc = m->xCreate(iArg, aArg, ppTok);
132884     assert( rc!=SQLITE_OK || *ppTok );
132885     if( rc!=SQLITE_OK ){
132886       *pzErr = sqlite3_mprintf("unknown tokenizer");
132887     }else{
132888       (*ppTok)->pModule = m;
132889     }
132890     sqlite3_free((void *)aArg);
132891   }
132892 
132893   sqlite3_free(zCopy);
132894   return rc;
132895 }
132896 
132897 
132898 #ifdef SQLITE_TEST
132899 
132900 #include <tcl.h>
132901 /* #include <string.h> */
132902 
132903 /*
132904 ** Implementation of a special SQL scalar function for testing tokenizers
132905 ** designed to be used in concert with the Tcl testing framework. This
132906 ** function must be called with two or more arguments:
132907 **
132908 **   SELECT <function-name>(<key-name>, ..., <input-string>);
132909 **
132910 ** where <function-name> is the name passed as the second argument
132911 ** to the sqlite3Fts3InitHashTable() function (e.g. 'fts3_tokenizer')
132912 ** concatenated with the string '_test' (e.g. 'fts3_tokenizer_test').
132913 **
132914 ** The return value is a string that may be interpreted as a Tcl
132915 ** list. For each token in the <input-string>, three elements are
132916 ** added to the returned list. The first is the token position, the
132917 ** second is the token text (folded, stemmed, etc.) and the third is the
132918 ** substring of <input-string> associated with the token. For example,
132919 ** using the built-in "simple" tokenizer:
132920 **
132921 **   SELECT fts_tokenizer_test('simple', 'I don't see how');
132922 **
132923 ** will return the string:
132924 **
132925 **   "{0 i I 1 dont don't 2 see see 3 how how}"
132926 **
132927 */
132928 static void testFunc(
132929   sqlite3_context *context,
132930   int argc,
132931   sqlite3_value **argv
132932 ){
132933   Fts3Hash *pHash;
132934   sqlite3_tokenizer_module *p;
132935   sqlite3_tokenizer *pTokenizer = 0;
132936   sqlite3_tokenizer_cursor *pCsr = 0;
132937 
132938   const char *zErr = 0;
132939 
132940   const char *zName;
132941   int nName;
132942   const char *zInput;
132943   int nInput;
132944 
132945   const char *azArg[64];
132946 
132947   const char *zToken;
132948   int nToken = 0;
132949   int iStart = 0;
132950   int iEnd = 0;
132951   int iPos = 0;
132952   int i;
132953 
132954   Tcl_Obj *pRet;
132955 
132956   if( argc<2 ){
132957     sqlite3_result_error(context, "insufficient arguments", -1);
132958     return;
132959   }
132960 
132961   nName = sqlite3_value_bytes(argv[0]);
132962   zName = (const char *)sqlite3_value_text(argv[0]);
132963   nInput = sqlite3_value_bytes(argv[argc-1]);
132964   zInput = (const char *)sqlite3_value_text(argv[argc-1]);
132965 
132966   pHash = (Fts3Hash *)sqlite3_user_data(context);
132967   p = (sqlite3_tokenizer_module *)sqlite3Fts3HashFind(pHash, zName, nName+1);
132968 
132969   if( !p ){
132970     char *zErr = sqlite3_mprintf("unknown tokenizer: %s", zName);
132971     sqlite3_result_error(context, zErr, -1);
132972     sqlite3_free(zErr);
132973     return;
132974   }
132975 
132976   pRet = Tcl_NewObj();
132977   Tcl_IncrRefCount(pRet);
132978 
132979   for(i=1; i<argc-1; i++){
132980     azArg[i-1] = (const char *)sqlite3_value_text(argv[i]);
132981   }
132982 
132983   if( SQLITE_OK!=p->xCreate(argc-2, azArg, &pTokenizer) ){
132984     zErr = "error in xCreate()";
132985     goto finish;
132986   }
132987   pTokenizer->pModule = p;
132988   if( sqlite3Fts3OpenTokenizer(pTokenizer, 0, zInput, nInput, &pCsr) ){
132989     zErr = "error in xOpen()";
132990     goto finish;
132991   }
132992 
132993   while( SQLITE_OK==p->xNext(pCsr, &zToken, &nToken, &iStart, &iEnd, &iPos) ){
132994     Tcl_ListObjAppendElement(0, pRet, Tcl_NewIntObj(iPos));
132995     Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zToken, nToken));
132996     zToken = &zInput[iStart];
132997     nToken = iEnd-iStart;
132998     Tcl_ListObjAppendElement(0, pRet, Tcl_NewStringObj(zToken, nToken));
132999   }
133000 
133001   if( SQLITE_OK!=p->xClose(pCsr) ){
133002     zErr = "error in xClose()";
133003     goto finish;
133004   }
133005   if( SQLITE_OK!=p->xDestroy(pTokenizer) ){
133006     zErr = "error in xDestroy()";
133007     goto finish;
133008   }
133009 
133010 finish:
133011   if( zErr ){
133012     sqlite3_result_error(context, zErr, -1);
133013   }else{
133014     sqlite3_result_text(context, Tcl_GetString(pRet), -1, SQLITE_TRANSIENT);
133015   }
133016   Tcl_DecrRefCount(pRet);
133017 }
133018 
133019 static
133020 int registerTokenizer(
133021   sqlite3 *db,
133022   char *zName,
133023   const sqlite3_tokenizer_module *p
133024 ){
133025   int rc;
133026   sqlite3_stmt *pStmt;
133027   const char zSql[] = "SELECT fts3_tokenizer(?, ?)";
133028 
133029   rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
133030   if( rc!=SQLITE_OK ){
133031     return rc;
133032   }
133033 
133034   sqlite3_bind_text(pStmt, 1, zName, -1, SQLITE_STATIC);
133035   sqlite3_bind_blob(pStmt, 2, &p, sizeof(p), SQLITE_STATIC);
133036   sqlite3_step(pStmt);
133037 
133038   return sqlite3_finalize(pStmt);
133039 }
133040 
133041 static
133042 int queryTokenizer(
133043   sqlite3 *db,
133044   char *zName,
133045   const sqlite3_tokenizer_module **pp
133046 ){
133047   int rc;
133048   sqlite3_stmt *pStmt;
133049   const char zSql[] = "SELECT fts3_tokenizer(?)";
133050 
133051   *pp = 0;
133052   rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
133053   if( rc!=SQLITE_OK ){
133054     return rc;
133055   }
133056 
133057   sqlite3_bind_text(pStmt, 1, zName, -1, SQLITE_STATIC);
133058   if( SQLITE_ROW==sqlite3_step(pStmt) ){
133059     if( sqlite3_column_type(pStmt, 0)==SQLITE_BLOB ){
133060       memcpy((void *)pp, sqlite3_column_blob(pStmt, 0), sizeof(*pp));
133061     }
133062   }
133063 
133064   return sqlite3_finalize(pStmt);
133065 }
133066 
133067 SQLITE_PRIVATE void sqlite3Fts3SimpleTokenizerModule(sqlite3_tokenizer_module const**ppModule);
133068 
133069 /*
133070 ** Implementation of the scalar function fts3_tokenizer_internal_test().
133071 ** This function is used for testing only, it is not included in the
133072 ** build unless SQLITE_TEST is defined.
133073 **
133074 ** The purpose of this is to test that the fts3_tokenizer() function
133075 ** can be used as designed by the C-code in the queryTokenizer and
133076 ** registerTokenizer() functions above. These two functions are repeated
133077 ** in the README.tokenizer file as an example, so it is important to
133078 ** test them.
133079 **
133080 ** To run the tests, evaluate the fts3_tokenizer_internal_test() scalar
133081 ** function with no arguments. An assert() will fail if a problem is
133082 ** detected. i.e.:
133083 **
133084 **     SELECT fts3_tokenizer_internal_test();
133085 **
133086 */
133087 static void intTestFunc(
133088   sqlite3_context *context,
133089   int argc,
133090   sqlite3_value **argv
133091 ){
133092   int rc;
133093   const sqlite3_tokenizer_module *p1;
133094   const sqlite3_tokenizer_module *p2;
133095   sqlite3 *db = (sqlite3 *)sqlite3_user_data(context);
133096 
133097   UNUSED_PARAMETER(argc);
133098   UNUSED_PARAMETER(argv);
133099 
133100   /* Test the query function */
133101   sqlite3Fts3SimpleTokenizerModule(&p1);
133102   rc = queryTokenizer(db, "simple", &p2);
133103   assert( rc==SQLITE_OK );
133104   assert( p1==p2 );
133105   rc = queryTokenizer(db, "nosuchtokenizer", &p2);
133106   assert( rc==SQLITE_ERROR );
133107   assert( p2==0 );
133108   assert( 0==strcmp(sqlite3_errmsg(db), "unknown tokenizer: nosuchtokenizer") );
133109 
133110   /* Test the storage function */
133111   rc = registerTokenizer(db, "nosuchtokenizer", p1);
133112   assert( rc==SQLITE_OK );
133113   rc = queryTokenizer(db, "nosuchtokenizer", &p2);
133114   assert( rc==SQLITE_OK );
133115   assert( p2==p1 );
133116 
133117   sqlite3_result_text(context, "ok", -1, SQLITE_STATIC);
133118 }
133119 
133120 #endif
133121 
133122 /*
133123 ** Set up SQL objects in database db used to access the contents of
133124 ** the hash table pointed to by argument pHash. The hash table must
133125 ** been initialized to use string keys, and to take a private copy
133126 ** of the key when a value is inserted. i.e. by a call similar to:
133127 **
133128 **    sqlite3Fts3HashInit(pHash, FTS3_HASH_STRING, 1);
133129 **
133130 ** This function adds a scalar function (see header comment above
133131 ** scalarFunc() in this file for details) and, if ENABLE_TABLE is
133132 ** defined at compilation time, a temporary virtual table (see header
133133 ** comment above struct HashTableVtab) to the database schema. Both
133134 ** provide read/write access to the contents of *pHash.
133135 **
133136 ** The third argument to this function, zName, is used as the name
133137 ** of both the scalar and, if created, the virtual table.
133138 */
133139 SQLITE_PRIVATE int sqlite3Fts3InitHashTable(
133140   sqlite3 *db,
133141   Fts3Hash *pHash,
133142   const char *zName
133143 ){
133144   int rc = SQLITE_OK;
133145   void *p = (void *)pHash;
133146   const int any = SQLITE_ANY;
133147 
133148 #ifdef SQLITE_TEST
133149   char *zTest = 0;
133150   char *zTest2 = 0;
133151   void *pdb = (void *)db;
133152   zTest = sqlite3_mprintf("%s_test", zName);
133153   zTest2 = sqlite3_mprintf("%s_internal_test", zName);
133154   if( !zTest || !zTest2 ){
133155     rc = SQLITE_NOMEM;
133156   }
133157 #endif
133158 
133159   if( SQLITE_OK==rc ){
133160     rc = sqlite3_create_function(db, zName, 1, any, p, scalarFunc, 0, 0);
133161   }
133162   if( SQLITE_OK==rc ){
133163     rc = sqlite3_create_function(db, zName, 2, any, p, scalarFunc, 0, 0);
133164   }
133165 #ifdef SQLITE_TEST
133166   if( SQLITE_OK==rc ){
133167     rc = sqlite3_create_function(db, zTest, -1, any, p, testFunc, 0, 0);
133168   }
133169   if( SQLITE_OK==rc ){
133170     rc = sqlite3_create_function(db, zTest2, 0, any, pdb, intTestFunc, 0, 0);
133171   }
133172 #endif
133173 
133174 #ifdef SQLITE_TEST
133175   sqlite3_free(zTest);
133176   sqlite3_free(zTest2);
133177 #endif
133178 
133179   return rc;
133180 }
133181 
133182 #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
133183 
133184 /************** End of fts3_tokenizer.c **************************************/
133185 /************** Begin file fts3_tokenizer1.c *********************************/
133186 /*
133187 ** 2006 Oct 10
133188 **
133189 ** The author disclaims copyright to this source code.  In place of
133190 ** a legal notice, here is a blessing:
133191 **
133192 **    May you do good and not evil.
133193 **    May you find forgiveness for yourself and forgive others.
133194 **    May you share freely, never taking more than you give.
133195 **
133196 ******************************************************************************
133197 **
133198 ** Implementation of the "simple" full-text-search tokenizer.
133199 */
133200 
133201 /*
133202 ** The code in this file is only compiled if:
133203 **
133204 **     * The FTS3 module is being built as an extension
133205 **       (in which case SQLITE_CORE is not defined), or
133206 **
133207 **     * The FTS3 module is being built into the core of
133208 **       SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
133209 */
133210 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
133211 
133212 /* #include <assert.h> */
133213 /* #include <stdlib.h> */
133214 /* #include <stdio.h> */
133215 /* #include <string.h> */
133216 
133217 
133218 typedef struct simple_tokenizer {
133219   sqlite3_tokenizer base;
133220   char delim[128];             /* flag ASCII delimiters */
133221 } simple_tokenizer;
133222 
133223 typedef struct simple_tokenizer_cursor {
133224   sqlite3_tokenizer_cursor base;
133225   const char *pInput;          /* input we are tokenizing */
133226   int nBytes;                  /* size of the input */
133227   int iOffset;                 /* current position in pInput */
133228   int iToken;                  /* index of next token to be returned */
133229   char *pToken;                /* storage for current token */
133230   int nTokenAllocated;         /* space allocated to zToken buffer */
133231 } simple_tokenizer_cursor;
133232 
133233 
133234 static int simpleDelim(simple_tokenizer *t, unsigned char c){
133235   return c<0x80 && t->delim[c];
133236 }
133237 static int fts3_isalnum(int x){
133238   return (x>='0' && x<='9') || (x>='A' && x<='Z') || (x>='a' && x<='z');
133239 }
133240 
133241 /*
133242 ** Create a new tokenizer instance.
133243 */
133244 static int simpleCreate(
133245   int argc, const char * const *argv,
133246   sqlite3_tokenizer **ppTokenizer
133247 ){
133248   simple_tokenizer *t;
133249 
133250   t = (simple_tokenizer *) sqlite3_malloc(sizeof(*t));
133251   if( t==NULL ) return SQLITE_NOMEM;
133252   memset(t, 0, sizeof(*t));
133253 
133254   /* TODO(shess) Delimiters need to remain the same from run to run,
133255   ** else we need to reindex.  One solution would be a meta-table to
133256   ** track such information in the database, then we'd only want this
133257   ** information on the initial create.
133258   */
133259   if( argc>1 ){
133260     int i, n = (int)strlen(argv[1]);
133261     for(i=0; i<n; i++){
133262       unsigned char ch = argv[1][i];
133263       /* We explicitly don't support UTF-8 delimiters for now. */
133264       if( ch>=0x80 ){
133265         sqlite3_free(t);
133266         return SQLITE_ERROR;
133267       }
133268       t->delim[ch] = 1;
133269     }
133270   } else {
133271     /* Mark non-alphanumeric ASCII characters as delimiters */
133272     int i;
133273     for(i=1; i<0x80; i++){
133274       t->delim[i] = !fts3_isalnum(i) ? -1 : 0;
133275     }
133276   }
133277 
133278   *ppTokenizer = &t->base;
133279   return SQLITE_OK;
133280 }
133281 
133282 /*
133283 ** Destroy a tokenizer
133284 */
133285 static int simpleDestroy(sqlite3_tokenizer *pTokenizer){
133286   sqlite3_free(pTokenizer);
133287   return SQLITE_OK;
133288 }
133289 
133290 /*
133291 ** Prepare to begin tokenizing a particular string.  The input
133292 ** string to be tokenized is pInput[0..nBytes-1].  A cursor
133293 ** used to incrementally tokenize this string is returned in
133294 ** *ppCursor.
133295 */
133296 static int simpleOpen(
133297   sqlite3_tokenizer *pTokenizer,         /* The tokenizer */
133298   const char *pInput, int nBytes,        /* String to be tokenized */
133299   sqlite3_tokenizer_cursor **ppCursor    /* OUT: Tokenization cursor */
133300 ){
133301   simple_tokenizer_cursor *c;
133302 
133303   UNUSED_PARAMETER(pTokenizer);
133304 
133305   c = (simple_tokenizer_cursor *) sqlite3_malloc(sizeof(*c));
133306   if( c==NULL ) return SQLITE_NOMEM;
133307 
133308   c->pInput = pInput;
133309   if( pInput==0 ){
133310     c->nBytes = 0;
133311   }else if( nBytes<0 ){
133312     c->nBytes = (int)strlen(pInput);
133313   }else{
133314     c->nBytes = nBytes;
133315   }
133316   c->iOffset = 0;                 /* start tokenizing at the beginning */
133317   c->iToken = 0;
133318   c->pToken = NULL;               /* no space allocated, yet. */
133319   c->nTokenAllocated = 0;
133320 
133321   *ppCursor = &c->base;
133322   return SQLITE_OK;
133323 }
133324 
133325 /*
133326 ** Close a tokenization cursor previously opened by a call to
133327 ** simpleOpen() above.
133328 */
133329 static int simpleClose(sqlite3_tokenizer_cursor *pCursor){
133330   simple_tokenizer_cursor *c = (simple_tokenizer_cursor *) pCursor;
133331   sqlite3_free(c->pToken);
133332   sqlite3_free(c);
133333   return SQLITE_OK;
133334 }
133335 
133336 /*
133337 ** Extract the next token from a tokenization cursor.  The cursor must
133338 ** have been opened by a prior call to simpleOpen().
133339 */
133340 static int simpleNext(
133341   sqlite3_tokenizer_cursor *pCursor,  /* Cursor returned by simpleOpen */
133342   const char **ppToken,               /* OUT: *ppToken is the token text */
133343   int *pnBytes,                       /* OUT: Number of bytes in token */
133344   int *piStartOffset,                 /* OUT: Starting offset of token */
133345   int *piEndOffset,                   /* OUT: Ending offset of token */
133346   int *piPosition                     /* OUT: Position integer of token */
133347 ){
133348   simple_tokenizer_cursor *c = (simple_tokenizer_cursor *) pCursor;
133349   simple_tokenizer *t = (simple_tokenizer *) pCursor->pTokenizer;
133350   unsigned char *p = (unsigned char *)c->pInput;
133351 
133352   while( c->iOffset<c->nBytes ){
133353     int iStartOffset;
133354 
133355     /* Scan past delimiter characters */
133356     while( c->iOffset<c->nBytes && simpleDelim(t, p[c->iOffset]) ){
133357       c->iOffset++;
133358     }
133359 
133360     /* Count non-delimiter characters. */
133361     iStartOffset = c->iOffset;
133362     while( c->iOffset<c->nBytes && !simpleDelim(t, p[c->iOffset]) ){
133363       c->iOffset++;
133364     }
133365 
133366     if( c->iOffset>iStartOffset ){
133367       int i, n = c->iOffset-iStartOffset;
133368       if( n>c->nTokenAllocated ){
133369         char *pNew;
133370         c->nTokenAllocated = n+20;
133371         pNew = sqlite3_realloc(c->pToken, c->nTokenAllocated);
133372         if( !pNew ) return SQLITE_NOMEM;
133373         c->pToken = pNew;
133374       }
133375       for(i=0; i<n; i++){
133376         /* TODO(shess) This needs expansion to handle UTF-8
133377         ** case-insensitivity.
133378         */
133379         unsigned char ch = p[iStartOffset+i];
133380         c->pToken[i] = (char)((ch>='A' && ch<='Z') ? ch-'A'+'a' : ch);
133381       }
133382       *ppToken = c->pToken;
133383       *pnBytes = n;
133384       *piStartOffset = iStartOffset;
133385       *piEndOffset = c->iOffset;
133386       *piPosition = c->iToken++;
133387 
133388       return SQLITE_OK;
133389     }
133390   }
133391   return SQLITE_DONE;
133392 }
133393 
133394 /*
133395 ** The set of routines that implement the simple tokenizer
133396 */
133397 static const sqlite3_tokenizer_module simpleTokenizerModule = {
133398   0,
133399   simpleCreate,
133400   simpleDestroy,
133401   simpleOpen,
133402   simpleClose,
133403   simpleNext,
133404   0,
133405 };
133406 
133407 /*
133408 ** Allocate a new simple tokenizer.  Return a pointer to the new
133409 ** tokenizer in *ppModule
133410 */
133411 SQLITE_PRIVATE void sqlite3Fts3SimpleTokenizerModule(
133412   sqlite3_tokenizer_module const**ppModule
133413 ){
133414   *ppModule = &simpleTokenizerModule;
133415 }
133416 
133417 #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
133418 
133419 /************** End of fts3_tokenizer1.c *************************************/
133420 /************** Begin file fts3_tokenize_vtab.c ******************************/
133421 /*
133422 ** 2013 Apr 22
133423 **
133424 ** The author disclaims copyright to this source code.  In place of
133425 ** a legal notice, here is a blessing:
133426 **
133427 **    May you do good and not evil.
133428 **    May you find forgiveness for yourself and forgive others.
133429 **    May you share freely, never taking more than you give.
133430 **
133431 ******************************************************************************
133432 **
133433 ** This file contains code for the "fts3tokenize" virtual table module.
133434 ** An fts3tokenize virtual table is created as follows:
133435 **
133436 **   CREATE VIRTUAL TABLE <tbl> USING fts3tokenize(
133437 **       <tokenizer-name>, <arg-1>, ...
133438 **   );
133439 **
133440 ** The table created has the following schema:
133441 **
133442 **   CREATE TABLE <tbl>(input, token, start, end, position)
133443 **
133444 ** When queried, the query must include a WHERE clause of type:
133445 **
133446 **   input = <string>
133447 **
133448 ** The virtual table module tokenizes this <string>, using the FTS3
133449 ** tokenizer specified by the arguments to the CREATE VIRTUAL TABLE
133450 ** statement and returns one row for each token in the result. With
133451 ** fields set as follows:
133452 **
133453 **   input:   Always set to a copy of <string>
133454 **   token:   A token from the input.
133455 **   start:   Byte offset of the token within the input <string>.
133456 **   end:     Byte offset of the byte immediately following the end of the
133457 **            token within the input string.
133458 **   pos:     Token offset of token within input.
133459 **
133460 */
133461 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
133462 
133463 /* #include <string.h> */
133464 /* #include <assert.h> */
133465 
133466 typedef struct Fts3tokTable Fts3tokTable;
133467 typedef struct Fts3tokCursor Fts3tokCursor;
133468 
133469 /*
133470 ** Virtual table structure.
133471 */
133472 struct Fts3tokTable {
133473   sqlite3_vtab base;              /* Base class used by SQLite core */
133474   const sqlite3_tokenizer_module *pMod;
133475   sqlite3_tokenizer *pTok;
133476 };
133477 
133478 /*
133479 ** Virtual table cursor structure.
133480 */
133481 struct Fts3tokCursor {
133482   sqlite3_vtab_cursor base;       /* Base class used by SQLite core */
133483   char *zInput;                   /* Input string */
133484   sqlite3_tokenizer_cursor *pCsr; /* Cursor to iterate through zInput */
133485   int iRowid;                     /* Current 'rowid' value */
133486   const char *zToken;             /* Current 'token' value */
133487   int nToken;                     /* Size of zToken in bytes */
133488   int iStart;                     /* Current 'start' value */
133489   int iEnd;                       /* Current 'end' value */
133490   int iPos;                       /* Current 'pos' value */
133491 };
133492 
133493 /*
133494 ** Query FTS for the tokenizer implementation named zName.
133495 */
133496 static int fts3tokQueryTokenizer(
133497   Fts3Hash *pHash,
133498   const char *zName,
133499   const sqlite3_tokenizer_module **pp,
133500   char **pzErr
133501 ){
133502   sqlite3_tokenizer_module *p;
133503   int nName = (int)strlen(zName);
133504 
133505   p = (sqlite3_tokenizer_module *)sqlite3Fts3HashFind(pHash, zName, nName+1);
133506   if( !p ){
133507     *pzErr = sqlite3_mprintf("unknown tokenizer: %s", zName);
133508     return SQLITE_ERROR;
133509   }
133510 
133511   *pp = p;
133512   return SQLITE_OK;
133513 }
133514 
133515 /*
133516 ** The second argument, argv[], is an array of pointers to nul-terminated
133517 ** strings. This function makes a copy of the array and strings into a
133518 ** single block of memory. It then dequotes any of the strings that appear
133519 ** to be quoted.
133520 **
133521 ** If successful, output parameter *pazDequote is set to point at the
133522 ** array of dequoted strings and SQLITE_OK is returned. The caller is
133523 ** responsible for eventually calling sqlite3_free() to free the array
133524 ** in this case. Or, if an error occurs, an SQLite error code is returned.
133525 ** The final value of *pazDequote is undefined in this case.
133526 */
133527 static int fts3tokDequoteArray(
133528   int argc,                       /* Number of elements in argv[] */
133529   const char * const *argv,       /* Input array */
133530   char ***pazDequote              /* Output array */
133531 ){
133532   int rc = SQLITE_OK;             /* Return code */
133533   if( argc==0 ){
133534     *pazDequote = 0;
133535   }else{
133536     int i;
133537     int nByte = 0;
133538     char **azDequote;
133539 
133540     for(i=0; i<argc; i++){
133541       nByte += (int)(strlen(argv[i]) + 1);
133542     }
133543 
133544     *pazDequote = azDequote = sqlite3_malloc(sizeof(char *)*argc + nByte);
133545     if( azDequote==0 ){
133546       rc = SQLITE_NOMEM;
133547     }else{
133548       char *pSpace = (char *)&azDequote[argc];
133549       for(i=0; i<argc; i++){
133550         int n = (int)strlen(argv[i]);
133551         azDequote[i] = pSpace;
133552         memcpy(pSpace, argv[i], n+1);
133553         sqlite3Fts3Dequote(pSpace);
133554         pSpace += (n+1);
133555       }
133556     }
133557   }
133558 
133559   return rc;
133560 }
133561 
133562 /*
133563 ** Schema of the tokenizer table.
133564 */
133565 #define FTS3_TOK_SCHEMA "CREATE TABLE x(input, token, start, end, position)"
133566 
133567 /*
133568 ** This function does all the work for both the xConnect and xCreate methods.
133569 ** These tables have no persistent representation of their own, so xConnect
133570 ** and xCreate are identical operations.
133571 **
133572 **   argv[0]: module name
133573 **   argv[1]: database name
133574 **   argv[2]: table name
133575 **   argv[3]: first argument (tokenizer name)
133576 */
133577 static int fts3tokConnectMethod(
133578   sqlite3 *db,                    /* Database connection */
133579   void *pHash,                    /* Hash table of tokenizers */
133580   int argc,                       /* Number of elements in argv array */
133581   const char * const *argv,       /* xCreate/xConnect argument array */
133582   sqlite3_vtab **ppVtab,          /* OUT: New sqlite3_vtab object */
133583   char **pzErr                    /* OUT: sqlite3_malloc'd error message */
133584 ){
133585   Fts3tokTable *pTab;
133586   const sqlite3_tokenizer_module *pMod = 0;
133587   sqlite3_tokenizer *pTok = 0;
133588   int rc;
133589   char **azDequote = 0;
133590   int nDequote;
133591 
133592   rc = sqlite3_declare_vtab(db, FTS3_TOK_SCHEMA);
133593   if( rc!=SQLITE_OK ) return rc;
133594 
133595   nDequote = argc-3;
133596   rc = fts3tokDequoteArray(nDequote, &argv[3], &azDequote);
133597 
133598   if( rc==SQLITE_OK ){
133599     const char *zModule;
133600     if( nDequote<1 ){
133601       zModule = "simple";
133602     }else{
133603       zModule = azDequote[0];
133604     }
133605     rc = fts3tokQueryTokenizer((Fts3Hash*)pHash, zModule, &pMod, pzErr);
133606   }
133607 
133608   assert( (rc==SQLITE_OK)==(pMod!=0) );
133609   if( rc==SQLITE_OK ){
133610     const char * const *azArg = (const char * const *)&azDequote[1];
133611     rc = pMod->xCreate((nDequote>1 ? nDequote-1 : 0), azArg, &pTok);
133612   }
133613 
133614   if( rc==SQLITE_OK ){
133615     pTab = (Fts3tokTable *)sqlite3_malloc(sizeof(Fts3tokTable));
133616     if( pTab==0 ){
133617       rc = SQLITE_NOMEM;
133618     }
133619   }
133620 
133621   if( rc==SQLITE_OK ){
133622     memset(pTab, 0, sizeof(Fts3tokTable));
133623     pTab->pMod = pMod;
133624     pTab->pTok = pTok;
133625     *ppVtab = &pTab->base;
133626   }else{
133627     if( pTok ){
133628       pMod->xDestroy(pTok);
133629     }
133630   }
133631 
133632   sqlite3_free(azDequote);
133633   return rc;
133634 }
133635 
133636 /*
133637 ** This function does the work for both the xDisconnect and xDestroy methods.
133638 ** These tables have no persistent representation of their own, so xDisconnect
133639 ** and xDestroy are identical operations.
133640 */
133641 static int fts3tokDisconnectMethod(sqlite3_vtab *pVtab){
133642   Fts3tokTable *pTab = (Fts3tokTable *)pVtab;
133643 
133644   pTab->pMod->xDestroy(pTab->pTok);
133645   sqlite3_free(pTab);
133646   return SQLITE_OK;
133647 }
133648 
133649 /*
133650 ** xBestIndex - Analyze a WHERE and ORDER BY clause.
133651 */
133652 static int fts3tokBestIndexMethod(
133653   sqlite3_vtab *pVTab,
133654   sqlite3_index_info *pInfo
133655 ){
133656   int i;
133657   UNUSED_PARAMETER(pVTab);
133658 
133659   for(i=0; i<pInfo->nConstraint; i++){
133660     if( pInfo->aConstraint[i].usable
133661      && pInfo->aConstraint[i].iColumn==0
133662      && pInfo->aConstraint[i].op==SQLITE_INDEX_CONSTRAINT_EQ
133663     ){
133664       pInfo->idxNum = 1;
133665       pInfo->aConstraintUsage[i].argvIndex = 1;
133666       pInfo->aConstraintUsage[i].omit = 1;
133667       pInfo->estimatedCost = 1;
133668       return SQLITE_OK;
133669     }
133670   }
133671 
133672   pInfo->idxNum = 0;
133673   assert( pInfo->estimatedCost>1000000.0 );
133674 
133675   return SQLITE_OK;
133676 }
133677 
133678 /*
133679 ** xOpen - Open a cursor.
133680 */
133681 static int fts3tokOpenMethod(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCsr){
133682   Fts3tokCursor *pCsr;
133683   UNUSED_PARAMETER(pVTab);
133684 
133685   pCsr = (Fts3tokCursor *)sqlite3_malloc(sizeof(Fts3tokCursor));
133686   if( pCsr==0 ){
133687     return SQLITE_NOMEM;
133688   }
133689   memset(pCsr, 0, sizeof(Fts3tokCursor));
133690 
133691   *ppCsr = (sqlite3_vtab_cursor *)pCsr;
133692   return SQLITE_OK;
133693 }
133694 
133695 /*
133696 ** Reset the tokenizer cursor passed as the only argument. As if it had
133697 ** just been returned by fts3tokOpenMethod().
133698 */
133699 static void fts3tokResetCursor(Fts3tokCursor *pCsr){
133700   if( pCsr->pCsr ){
133701     Fts3tokTable *pTab = (Fts3tokTable *)(pCsr->base.pVtab);
133702     pTab->pMod->xClose(pCsr->pCsr);
133703     pCsr->pCsr = 0;
133704   }
133705   sqlite3_free(pCsr->zInput);
133706   pCsr->zInput = 0;
133707   pCsr->zToken = 0;
133708   pCsr->nToken = 0;
133709   pCsr->iStart = 0;
133710   pCsr->iEnd = 0;
133711   pCsr->iPos = 0;
133712   pCsr->iRowid = 0;
133713 }
133714 
133715 /*
133716 ** xClose - Close a cursor.
133717 */
133718 static int fts3tokCloseMethod(sqlite3_vtab_cursor *pCursor){
133719   Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor;
133720 
133721   fts3tokResetCursor(pCsr);
133722   sqlite3_free(pCsr);
133723   return SQLITE_OK;
133724 }
133725 
133726 /*
133727 ** xNext - Advance the cursor to the next row, if any.
133728 */
133729 static int fts3tokNextMethod(sqlite3_vtab_cursor *pCursor){
133730   Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor;
133731   Fts3tokTable *pTab = (Fts3tokTable *)(pCursor->pVtab);
133732   int rc;                         /* Return code */
133733 
133734   pCsr->iRowid++;
133735   rc = pTab->pMod->xNext(pCsr->pCsr,
133736       &pCsr->zToken, &pCsr->nToken,
133737       &pCsr->iStart, &pCsr->iEnd, &pCsr->iPos
133738   );
133739 
133740   if( rc!=SQLITE_OK ){
133741     fts3tokResetCursor(pCsr);
133742     if( rc==SQLITE_DONE ) rc = SQLITE_OK;
133743   }
133744 
133745   return rc;
133746 }
133747 
133748 /*
133749 ** xFilter - Initialize a cursor to point at the start of its data.
133750 */
133751 static int fts3tokFilterMethod(
133752   sqlite3_vtab_cursor *pCursor,   /* The cursor used for this query */
133753   int idxNum,                     /* Strategy index */
133754   const char *idxStr,             /* Unused */
133755   int nVal,                       /* Number of elements in apVal */
133756   sqlite3_value **apVal           /* Arguments for the indexing scheme */
133757 ){
133758   int rc = SQLITE_ERROR;
133759   Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor;
133760   Fts3tokTable *pTab = (Fts3tokTable *)(pCursor->pVtab);
133761   UNUSED_PARAMETER(idxStr);
133762   UNUSED_PARAMETER(nVal);
133763 
133764   fts3tokResetCursor(pCsr);
133765   if( idxNum==1 ){
133766     const char *zByte = (const char *)sqlite3_value_text(apVal[0]);
133767     int nByte = sqlite3_value_bytes(apVal[0]);
133768     pCsr->zInput = sqlite3_malloc(nByte+1);
133769     if( pCsr->zInput==0 ){
133770       rc = SQLITE_NOMEM;
133771     }else{
133772       memcpy(pCsr->zInput, zByte, nByte);
133773       pCsr->zInput[nByte] = 0;
133774       rc = pTab->pMod->xOpen(pTab->pTok, pCsr->zInput, nByte, &pCsr->pCsr);
133775       if( rc==SQLITE_OK ){
133776         pCsr->pCsr->pTokenizer = pTab->pTok;
133777       }
133778     }
133779   }
133780 
133781   if( rc!=SQLITE_OK ) return rc;
133782   return fts3tokNextMethod(pCursor);
133783 }
133784 
133785 /*
133786 ** xEof - Return true if the cursor is at EOF, or false otherwise.
133787 */
133788 static int fts3tokEofMethod(sqlite3_vtab_cursor *pCursor){
133789   Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor;
133790   return (pCsr->zToken==0);
133791 }
133792 
133793 /*
133794 ** xColumn - Return a column value.
133795 */
133796 static int fts3tokColumnMethod(
133797   sqlite3_vtab_cursor *pCursor,   /* Cursor to retrieve value from */
133798   sqlite3_context *pCtx,          /* Context for sqlite3_result_xxx() calls */
133799   int iCol                        /* Index of column to read value from */
133800 ){
133801   Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor;
133802 
133803   /* CREATE TABLE x(input, token, start, end, position) */
133804   switch( iCol ){
133805     case 0:
133806       sqlite3_result_text(pCtx, pCsr->zInput, -1, SQLITE_TRANSIENT);
133807       break;
133808     case 1:
133809       sqlite3_result_text(pCtx, pCsr->zToken, pCsr->nToken, SQLITE_TRANSIENT);
133810       break;
133811     case 2:
133812       sqlite3_result_int(pCtx, pCsr->iStart);
133813       break;
133814     case 3:
133815       sqlite3_result_int(pCtx, pCsr->iEnd);
133816       break;
133817     default:
133818       assert( iCol==4 );
133819       sqlite3_result_int(pCtx, pCsr->iPos);
133820       break;
133821   }
133822   return SQLITE_OK;
133823 }
133824 
133825 /*
133826 ** xRowid - Return the current rowid for the cursor.
133827 */
133828 static int fts3tokRowidMethod(
133829   sqlite3_vtab_cursor *pCursor,   /* Cursor to retrieve value from */
133830   sqlite_int64 *pRowid            /* OUT: Rowid value */
133831 ){
133832   Fts3tokCursor *pCsr = (Fts3tokCursor *)pCursor;
133833   *pRowid = (sqlite3_int64)pCsr->iRowid;
133834   return SQLITE_OK;
133835 }
133836 
133837 /*
133838 ** Register the fts3tok module with database connection db. Return SQLITE_OK
133839 ** if successful or an error code if sqlite3_create_module() fails.
133840 */
133841 SQLITE_PRIVATE int sqlite3Fts3InitTok(sqlite3 *db, Fts3Hash *pHash){
133842   static const sqlite3_module fts3tok_module = {
133843      0,                           /* iVersion      */
133844      fts3tokConnectMethod,        /* xCreate       */
133845      fts3tokConnectMethod,        /* xConnect      */
133846      fts3tokBestIndexMethod,      /* xBestIndex    */
133847      fts3tokDisconnectMethod,     /* xDisconnect   */
133848      fts3tokDisconnectMethod,     /* xDestroy      */
133849      fts3tokOpenMethod,           /* xOpen         */
133850      fts3tokCloseMethod,          /* xClose        */
133851      fts3tokFilterMethod,         /* xFilter       */
133852      fts3tokNextMethod,           /* xNext         */
133853      fts3tokEofMethod,            /* xEof          */
133854      fts3tokColumnMethod,         /* xColumn       */
133855      fts3tokRowidMethod,          /* xRowid        */
133856      0,                           /* xUpdate       */
133857      0,                           /* xBegin        */
133858      0,                           /* xSync         */
133859      0,                           /* xCommit       */
133860      0,                           /* xRollback     */
133861      0,                           /* xFindFunction */
133862      0,                           /* xRename       */
133863      0,                           /* xSavepoint    */
133864      0,                           /* xRelease      */
133865      0                            /* xRollbackTo   */
133866   };
133867   int rc;                         /* Return code */
133868 
133869   rc = sqlite3_create_module(db, "fts3tokenize", &fts3tok_module, (void*)pHash);
133870   return rc;
133871 }
133872 
133873 #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
133874 
133875 /************** End of fts3_tokenize_vtab.c **********************************/
133876 /************** Begin file fts3_write.c **************************************/
133877 /*
133878 ** 2009 Oct 23
133879 **
133880 ** The author disclaims copyright to this source code.  In place of
133881 ** a legal notice, here is a blessing:
133882 **
133883 **    May you do good and not evil.
133884 **    May you find forgiveness for yourself and forgive others.
133885 **    May you share freely, never taking more than you give.
133886 **
133887 ******************************************************************************
133888 **
133889 ** This file is part of the SQLite FTS3 extension module. Specifically,
133890 ** this file contains code to insert, update and delete rows from FTS3
133891 ** tables. It also contains code to merge FTS3 b-tree segments. Some
133892 ** of the sub-routines used to merge segments are also used by the query
133893 ** code in fts3.c.
133894 */
133895 
133896 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
133897 
133898 /* #include <string.h> */
133899 /* #include <assert.h> */
133900 /* #include <stdlib.h> */
133901 
133902 
133903 #define FTS_MAX_APPENDABLE_HEIGHT 16
133904 
133905 /*
133906 ** When full-text index nodes are loaded from disk, the buffer that they
133907 ** are loaded into has the following number of bytes of padding at the end
133908 ** of it. i.e. if a full-text index node is 900 bytes in size, then a buffer
133909 ** of 920 bytes is allocated for it.
133910 **
133911 ** This means that if we have a pointer into a buffer containing node data,
133912 ** it is always safe to read up to two varints from it without risking an
133913 ** overread, even if the node data is corrupted.
133914 */
133915 #define FTS3_NODE_PADDING (FTS3_VARINT_MAX*2)
133916 
133917 /*
133918 ** Under certain circumstances, b-tree nodes (doclists) can be loaded into
133919 ** memory incrementally instead of all at once. This can be a big performance
133920 ** win (reduced IO and CPU) if SQLite stops calling the virtual table xNext()
133921 ** method before retrieving all query results (as may happen, for example,
133922 ** if a query has a LIMIT clause).
133923 **
133924 ** Incremental loading is used for b-tree nodes FTS3_NODE_CHUNK_THRESHOLD
133925 ** bytes and larger. Nodes are loaded in chunks of FTS3_NODE_CHUNKSIZE bytes.
133926 ** The code is written so that the hard lower-limit for each of these values
133927 ** is 1. Clearly such small values would be inefficient, but can be useful
133928 ** for testing purposes.
133929 **
133930 ** If this module is built with SQLITE_TEST defined, these constants may
133931 ** be overridden at runtime for testing purposes. File fts3_test.c contains
133932 ** a Tcl interface to read and write the values.
133933 */
133934 #ifdef SQLITE_TEST
133935 int test_fts3_node_chunksize = (4*1024);
133936 int test_fts3_node_chunk_threshold = (4*1024)*4;
133937 # define FTS3_NODE_CHUNKSIZE       test_fts3_node_chunksize
133938 # define FTS3_NODE_CHUNK_THRESHOLD test_fts3_node_chunk_threshold
133939 #else
133940 # define FTS3_NODE_CHUNKSIZE (4*1024)
133941 # define FTS3_NODE_CHUNK_THRESHOLD (FTS3_NODE_CHUNKSIZE*4)
133942 #endif
133943 
133944 /*
133945 ** The two values that may be meaningfully bound to the :1 parameter in
133946 ** statements SQL_REPLACE_STAT and SQL_SELECT_STAT.
133947 */
133948 #define FTS_STAT_DOCTOTAL      0
133949 #define FTS_STAT_INCRMERGEHINT 1
133950 #define FTS_STAT_AUTOINCRMERGE 2
133951 
133952 /*
133953 ** If FTS_LOG_MERGES is defined, call sqlite3_log() to report each automatic
133954 ** and incremental merge operation that takes place. This is used for
133955 ** debugging FTS only, it should not usually be turned on in production
133956 ** systems.
133957 */
133958 #ifdef FTS3_LOG_MERGES
133959 static void fts3LogMerge(int nMerge, sqlite3_int64 iAbsLevel){
133960   sqlite3_log(SQLITE_OK, "%d-way merge from level %d", nMerge, (int)iAbsLevel);
133961 }
133962 #else
133963 #define fts3LogMerge(x, y)
133964 #endif
133965 
133966 
133967 typedef struct PendingList PendingList;
133968 typedef struct SegmentNode SegmentNode;
133969 typedef struct SegmentWriter SegmentWriter;
133970 
133971 /*
133972 ** An instance of the following data structure is used to build doclists
133973 ** incrementally. See function fts3PendingListAppend() for details.
133974 */
133975 struct PendingList {
133976   int nData;
133977   char *aData;
133978   int nSpace;
133979   sqlite3_int64 iLastDocid;
133980   sqlite3_int64 iLastCol;
133981   sqlite3_int64 iLastPos;
133982 };
133983 
133984 
133985 /*
133986 ** Each cursor has a (possibly empty) linked list of the following objects.
133987 */
133988 struct Fts3DeferredToken {
133989   Fts3PhraseToken *pToken;        /* Pointer to corresponding expr token */
133990   int iCol;                       /* Column token must occur in */
133991   Fts3DeferredToken *pNext;       /* Next in list of deferred tokens */
133992   PendingList *pList;             /* Doclist is assembled here */
133993 };
133994 
133995 /*
133996 ** An instance of this structure is used to iterate through the terms on
133997 ** a contiguous set of segment b-tree leaf nodes. Although the details of
133998 ** this structure are only manipulated by code in this file, opaque handles
133999 ** of type Fts3SegReader* are also used by code in fts3.c to iterate through
134000 ** terms when querying the full-text index. See functions:
134001 **
134002 **   sqlite3Fts3SegReaderNew()
134003 **   sqlite3Fts3SegReaderFree()
134004 **   sqlite3Fts3SegReaderIterate()
134005 **
134006 ** Methods used to manipulate Fts3SegReader structures:
134007 **
134008 **   fts3SegReaderNext()
134009 **   fts3SegReaderFirstDocid()
134010 **   fts3SegReaderNextDocid()
134011 */
134012 struct Fts3SegReader {
134013   int iIdx;                       /* Index within level, or 0x7FFFFFFF for PT */
134014   u8 bLookup;                     /* True for a lookup only */
134015   u8 rootOnly;                    /* True for a root-only reader */
134016 
134017   sqlite3_int64 iStartBlock;      /* Rowid of first leaf block to traverse */
134018   sqlite3_int64 iLeafEndBlock;    /* Rowid of final leaf block to traverse */
134019   sqlite3_int64 iEndBlock;        /* Rowid of final block in segment (or 0) */
134020   sqlite3_int64 iCurrentBlock;    /* Current leaf block (or 0) */
134021 
134022   char *aNode;                    /* Pointer to node data (or NULL) */
134023   int nNode;                      /* Size of buffer at aNode (or 0) */
134024   int nPopulate;                  /* If >0, bytes of buffer aNode[] loaded */
134025   sqlite3_blob *pBlob;            /* If not NULL, blob handle to read node */
134026 
134027   Fts3HashElem **ppNextElem;
134028 
134029   /* Variables set by fts3SegReaderNext(). These may be read directly
134030   ** by the caller. They are valid from the time SegmentReaderNew() returns
134031   ** until SegmentReaderNext() returns something other than SQLITE_OK
134032   ** (i.e. SQLITE_DONE).
134033   */
134034   int nTerm;                      /* Number of bytes in current term */
134035   char *zTerm;                    /* Pointer to current term */
134036   int nTermAlloc;                 /* Allocated size of zTerm buffer */
134037   char *aDoclist;                 /* Pointer to doclist of current entry */
134038   int nDoclist;                   /* Size of doclist in current entry */
134039 
134040   /* The following variables are used by fts3SegReaderNextDocid() to iterate
134041   ** through the current doclist (aDoclist/nDoclist).
134042   */
134043   char *pOffsetList;
134044   int nOffsetList;                /* For descending pending seg-readers only */
134045   sqlite3_int64 iDocid;
134046 };
134047 
134048 #define fts3SegReaderIsPending(p) ((p)->ppNextElem!=0)
134049 #define fts3SegReaderIsRootOnly(p) ((p)->rootOnly!=0)
134050 
134051 /*
134052 ** An instance of this structure is used to create a segment b-tree in the
134053 ** database. The internal details of this type are only accessed by the
134054 ** following functions:
134055 **
134056 **   fts3SegWriterAdd()
134057 **   fts3SegWriterFlush()
134058 **   fts3SegWriterFree()
134059 */
134060 struct SegmentWriter {
134061   SegmentNode *pTree;             /* Pointer to interior tree structure */
134062   sqlite3_int64 iFirst;           /* First slot in %_segments written */
134063   sqlite3_int64 iFree;            /* Next free slot in %_segments */
134064   char *zTerm;                    /* Pointer to previous term buffer */
134065   int nTerm;                      /* Number of bytes in zTerm */
134066   int nMalloc;                    /* Size of malloc'd buffer at zMalloc */
134067   char *zMalloc;                  /* Malloc'd space (possibly) used for zTerm */
134068   int nSize;                      /* Size of allocation at aData */
134069   int nData;                      /* Bytes of data in aData */
134070   char *aData;                    /* Pointer to block from malloc() */
134071 };
134072 
134073 /*
134074 ** Type SegmentNode is used by the following three functions to create
134075 ** the interior part of the segment b+-tree structures (everything except
134076 ** the leaf nodes). These functions and type are only ever used by code
134077 ** within the fts3SegWriterXXX() family of functions described above.
134078 **
134079 **   fts3NodeAddTerm()
134080 **   fts3NodeWrite()
134081 **   fts3NodeFree()
134082 **
134083 ** When a b+tree is written to the database (either as a result of a merge
134084 ** or the pending-terms table being flushed), leaves are written into the
134085 ** database file as soon as they are completely populated. The interior of
134086 ** the tree is assembled in memory and written out only once all leaves have
134087 ** been populated and stored. This is Ok, as the b+-tree fanout is usually
134088 ** very large, meaning that the interior of the tree consumes relatively
134089 ** little memory.
134090 */
134091 struct SegmentNode {
134092   SegmentNode *pParent;           /* Parent node (or NULL for root node) */
134093   SegmentNode *pRight;            /* Pointer to right-sibling */
134094   SegmentNode *pLeftmost;         /* Pointer to left-most node of this depth */
134095   int nEntry;                     /* Number of terms written to node so far */
134096   char *zTerm;                    /* Pointer to previous term buffer */
134097   int nTerm;                      /* Number of bytes in zTerm */
134098   int nMalloc;                    /* Size of malloc'd buffer at zMalloc */
134099   char *zMalloc;                  /* Malloc'd space (possibly) used for zTerm */
134100   int nData;                      /* Bytes of valid data so far */
134101   char *aData;                    /* Node data */
134102 };
134103 
134104 /*
134105 ** Valid values for the second argument to fts3SqlStmt().
134106 */
134107 #define SQL_DELETE_CONTENT             0
134108 #define SQL_IS_EMPTY                   1
134109 #define SQL_DELETE_ALL_CONTENT         2
134110 #define SQL_DELETE_ALL_SEGMENTS        3
134111 #define SQL_DELETE_ALL_SEGDIR          4
134112 #define SQL_DELETE_ALL_DOCSIZE         5
134113 #define SQL_DELETE_ALL_STAT            6
134114 #define SQL_SELECT_CONTENT_BY_ROWID    7
134115 #define SQL_NEXT_SEGMENT_INDEX         8
134116 #define SQL_INSERT_SEGMENTS            9
134117 #define SQL_NEXT_SEGMENTS_ID          10
134118 #define SQL_INSERT_SEGDIR             11
134119 #define SQL_SELECT_LEVEL              12
134120 #define SQL_SELECT_LEVEL_RANGE        13
134121 #define SQL_SELECT_LEVEL_COUNT        14
134122 #define SQL_SELECT_SEGDIR_MAX_LEVEL   15
134123 #define SQL_DELETE_SEGDIR_LEVEL       16
134124 #define SQL_DELETE_SEGMENTS_RANGE     17
134125 #define SQL_CONTENT_INSERT            18
134126 #define SQL_DELETE_DOCSIZE            19
134127 #define SQL_REPLACE_DOCSIZE           20
134128 #define SQL_SELECT_DOCSIZE            21
134129 #define SQL_SELECT_STAT               22
134130 #define SQL_REPLACE_STAT              23
134131 
134132 #define SQL_SELECT_ALL_PREFIX_LEVEL   24
134133 #define SQL_DELETE_ALL_TERMS_SEGDIR   25
134134 #define SQL_DELETE_SEGDIR_RANGE       26
134135 #define SQL_SELECT_ALL_LANGID         27
134136 #define SQL_FIND_MERGE_LEVEL          28
134137 #define SQL_MAX_LEAF_NODE_ESTIMATE    29
134138 #define SQL_DELETE_SEGDIR_ENTRY       30
134139 #define SQL_SHIFT_SEGDIR_ENTRY        31
134140 #define SQL_SELECT_SEGDIR             32
134141 #define SQL_CHOMP_SEGDIR              33
134142 #define SQL_SEGMENT_IS_APPENDABLE     34
134143 #define SQL_SELECT_INDEXES            35
134144 #define SQL_SELECT_MXLEVEL            36
134145 
134146 /*
134147 ** This function is used to obtain an SQLite prepared statement handle
134148 ** for the statement identified by the second argument. If successful,
134149 ** *pp is set to the requested statement handle and SQLITE_OK returned.
134150 ** Otherwise, an SQLite error code is returned and *pp is set to 0.
134151 **
134152 ** If argument apVal is not NULL, then it must point to an array with
134153 ** at least as many entries as the requested statement has bound
134154 ** parameters. The values are bound to the statements parameters before
134155 ** returning.
134156 */
134157 static int fts3SqlStmt(
134158   Fts3Table *p,                   /* Virtual table handle */
134159   int eStmt,                      /* One of the SQL_XXX constants above */
134160   sqlite3_stmt **pp,              /* OUT: Statement handle */
134161   sqlite3_value **apVal           /* Values to bind to statement */
134162 ){
134163   const char *azSql[] = {
134164 /* 0  */  "DELETE FROM %Q.'%q_content' WHERE rowid = ?",
134165 /* 1  */  "SELECT NOT EXISTS(SELECT docid FROM %Q.'%q_content' WHERE rowid!=?)",
134166 /* 2  */  "DELETE FROM %Q.'%q_content'",
134167 /* 3  */  "DELETE FROM %Q.'%q_segments'",
134168 /* 4  */  "DELETE FROM %Q.'%q_segdir'",
134169 /* 5  */  "DELETE FROM %Q.'%q_docsize'",
134170 /* 6  */  "DELETE FROM %Q.'%q_stat'",
134171 /* 7  */  "SELECT %s WHERE rowid=?",
134172 /* 8  */  "SELECT (SELECT max(idx) FROM %Q.'%q_segdir' WHERE level = ?) + 1",
134173 /* 9  */  "REPLACE INTO %Q.'%q_segments'(blockid, block) VALUES(?, ?)",
134174 /* 10 */  "SELECT coalesce((SELECT max(blockid) FROM %Q.'%q_segments') + 1, 1)",
134175 /* 11 */  "REPLACE INTO %Q.'%q_segdir' VALUES(?,?,?,?,?,?)",
134176 
134177           /* Return segments in order from oldest to newest.*/
134178 /* 12 */  "SELECT idx, start_block, leaves_end_block, end_block, root "
134179             "FROM %Q.'%q_segdir' WHERE level = ? ORDER BY idx ASC",
134180 /* 13 */  "SELECT idx, start_block, leaves_end_block, end_block, root "
134181             "FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?"
134182             "ORDER BY level DESC, idx ASC",
134183 
134184 /* 14 */  "SELECT count(*) FROM %Q.'%q_segdir' WHERE level = ?",
134185 /* 15 */  "SELECT max(level) FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?",
134186 
134187 /* 16 */  "DELETE FROM %Q.'%q_segdir' WHERE level = ?",
134188 /* 17 */  "DELETE FROM %Q.'%q_segments' WHERE blockid BETWEEN ? AND ?",
134189 /* 18 */  "INSERT INTO %Q.'%q_content' VALUES(%s)",
134190 /* 19 */  "DELETE FROM %Q.'%q_docsize' WHERE docid = ?",
134191 /* 20 */  "REPLACE INTO %Q.'%q_docsize' VALUES(?,?)",
134192 /* 21 */  "SELECT size FROM %Q.'%q_docsize' WHERE docid=?",
134193 /* 22 */  "SELECT value FROM %Q.'%q_stat' WHERE id=?",
134194 /* 23 */  "REPLACE INTO %Q.'%q_stat' VALUES(?,?)",
134195 /* 24 */  "",
134196 /* 25 */  "",
134197 
134198 /* 26 */ "DELETE FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?",
134199 /* 27 */ "SELECT DISTINCT level / (1024 * ?) FROM %Q.'%q_segdir'",
134200 
134201 /* This statement is used to determine which level to read the input from
134202 ** when performing an incremental merge. It returns the absolute level number
134203 ** of the oldest level in the db that contains at least ? segments. Or,
134204 ** if no level in the FTS index contains more than ? segments, the statement
134205 ** returns zero rows.  */
134206 /* 28 */ "SELECT level FROM %Q.'%q_segdir' GROUP BY level HAVING count(*)>=?"
134207          "  ORDER BY (level %% 1024) ASC LIMIT 1",
134208 
134209 /* Estimate the upper limit on the number of leaf nodes in a new segment
134210 ** created by merging the oldest :2 segments from absolute level :1. See
134211 ** function sqlite3Fts3Incrmerge() for details.  */
134212 /* 29 */ "SELECT 2 * total(1 + leaves_end_block - start_block) "
134213          "  FROM %Q.'%q_segdir' WHERE level = ? AND idx < ?",
134214 
134215 /* SQL_DELETE_SEGDIR_ENTRY
134216 **   Delete the %_segdir entry on absolute level :1 with index :2.  */
134217 /* 30 */ "DELETE FROM %Q.'%q_segdir' WHERE level = ? AND idx = ?",
134218 
134219 /* SQL_SHIFT_SEGDIR_ENTRY
134220 **   Modify the idx value for the segment with idx=:3 on absolute level :2
134221 **   to :1.  */
134222 /* 31 */ "UPDATE %Q.'%q_segdir' SET idx = ? WHERE level=? AND idx=?",
134223 
134224 /* SQL_SELECT_SEGDIR
134225 **   Read a single entry from the %_segdir table. The entry from absolute
134226 **   level :1 with index value :2.  */
134227 /* 32 */  "SELECT idx, start_block, leaves_end_block, end_block, root "
134228             "FROM %Q.'%q_segdir' WHERE level = ? AND idx = ?",
134229 
134230 /* SQL_CHOMP_SEGDIR
134231 **   Update the start_block (:1) and root (:2) fields of the %_segdir
134232 **   entry located on absolute level :3 with index :4.  */
134233 /* 33 */  "UPDATE %Q.'%q_segdir' SET start_block = ?, root = ?"
134234             "WHERE level = ? AND idx = ?",
134235 
134236 /* SQL_SEGMENT_IS_APPENDABLE
134237 **   Return a single row if the segment with end_block=? is appendable. Or
134238 **   no rows otherwise.  */
134239 /* 34 */  "SELECT 1 FROM %Q.'%q_segments' WHERE blockid=? AND block IS NULL",
134240 
134241 /* SQL_SELECT_INDEXES
134242 **   Return the list of valid segment indexes for absolute level ?  */
134243 /* 35 */  "SELECT idx FROM %Q.'%q_segdir' WHERE level=? ORDER BY 1 ASC",
134244 
134245 /* SQL_SELECT_MXLEVEL
134246 **   Return the largest relative level in the FTS index or indexes.  */
134247 /* 36 */  "SELECT max( level %% 1024 ) FROM %Q.'%q_segdir'"
134248   };
134249   int rc = SQLITE_OK;
134250   sqlite3_stmt *pStmt;
134251 
134252   assert( SizeofArray(azSql)==SizeofArray(p->aStmt) );
134253   assert( eStmt<SizeofArray(azSql) && eStmt>=0 );
134254 
134255   pStmt = p->aStmt[eStmt];
134256   if( !pStmt ){
134257     char *zSql;
134258     if( eStmt==SQL_CONTENT_INSERT ){
134259       zSql = sqlite3_mprintf(azSql[eStmt], p->zDb, p->zName, p->zWriteExprlist);
134260     }else if( eStmt==SQL_SELECT_CONTENT_BY_ROWID ){
134261       zSql = sqlite3_mprintf(azSql[eStmt], p->zReadExprlist);
134262     }else{
134263       zSql = sqlite3_mprintf(azSql[eStmt], p->zDb, p->zName);
134264     }
134265     if( !zSql ){
134266       rc = SQLITE_NOMEM;
134267     }else{
134268       rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, NULL);
134269       sqlite3_free(zSql);
134270       assert( rc==SQLITE_OK || pStmt==0 );
134271       p->aStmt[eStmt] = pStmt;
134272     }
134273   }
134274   if( apVal ){
134275     int i;
134276     int nParam = sqlite3_bind_parameter_count(pStmt);
134277     for(i=0; rc==SQLITE_OK && i<nParam; i++){
134278       rc = sqlite3_bind_value(pStmt, i+1, apVal[i]);
134279     }
134280   }
134281   *pp = pStmt;
134282   return rc;
134283 }
134284 
134285 
134286 static int fts3SelectDocsize(
134287   Fts3Table *pTab,                /* FTS3 table handle */
134288   sqlite3_int64 iDocid,           /* Docid to bind for SQL_SELECT_DOCSIZE */
134289   sqlite3_stmt **ppStmt           /* OUT: Statement handle */
134290 ){
134291   sqlite3_stmt *pStmt = 0;        /* Statement requested from fts3SqlStmt() */
134292   int rc;                         /* Return code */
134293 
134294   rc = fts3SqlStmt(pTab, SQL_SELECT_DOCSIZE, &pStmt, 0);
134295   if( rc==SQLITE_OK ){
134296     sqlite3_bind_int64(pStmt, 1, iDocid);
134297     rc = sqlite3_step(pStmt);
134298     if( rc!=SQLITE_ROW || sqlite3_column_type(pStmt, 0)!=SQLITE_BLOB ){
134299       rc = sqlite3_reset(pStmt);
134300       if( rc==SQLITE_OK ) rc = FTS_CORRUPT_VTAB;
134301       pStmt = 0;
134302     }else{
134303       rc = SQLITE_OK;
134304     }
134305   }
134306 
134307   *ppStmt = pStmt;
134308   return rc;
134309 }
134310 
134311 SQLITE_PRIVATE int sqlite3Fts3SelectDoctotal(
134312   Fts3Table *pTab,                /* Fts3 table handle */
134313   sqlite3_stmt **ppStmt           /* OUT: Statement handle */
134314 ){
134315   sqlite3_stmt *pStmt = 0;
134316   int rc;
134317   rc = fts3SqlStmt(pTab, SQL_SELECT_STAT, &pStmt, 0);
134318   if( rc==SQLITE_OK ){
134319     sqlite3_bind_int(pStmt, 1, FTS_STAT_DOCTOTAL);
134320     if( sqlite3_step(pStmt)!=SQLITE_ROW
134321      || sqlite3_column_type(pStmt, 0)!=SQLITE_BLOB
134322     ){
134323       rc = sqlite3_reset(pStmt);
134324       if( rc==SQLITE_OK ) rc = FTS_CORRUPT_VTAB;
134325       pStmt = 0;
134326     }
134327   }
134328   *ppStmt = pStmt;
134329   return rc;
134330 }
134331 
134332 SQLITE_PRIVATE int sqlite3Fts3SelectDocsize(
134333   Fts3Table *pTab,                /* Fts3 table handle */
134334   sqlite3_int64 iDocid,           /* Docid to read size data for */
134335   sqlite3_stmt **ppStmt           /* OUT: Statement handle */
134336 ){
134337   return fts3SelectDocsize(pTab, iDocid, ppStmt);
134338 }
134339 
134340 /*
134341 ** Similar to fts3SqlStmt(). Except, after binding the parameters in
134342 ** array apVal[] to the SQL statement identified by eStmt, the statement
134343 ** is executed.
134344 **
134345 ** Returns SQLITE_OK if the statement is successfully executed, or an
134346 ** SQLite error code otherwise.
134347 */
134348 static void fts3SqlExec(
134349   int *pRC,                /* Result code */
134350   Fts3Table *p,            /* The FTS3 table */
134351   int eStmt,               /* Index of statement to evaluate */
134352   sqlite3_value **apVal    /* Parameters to bind */
134353 ){
134354   sqlite3_stmt *pStmt;
134355   int rc;
134356   if( *pRC ) return;
134357   rc = fts3SqlStmt(p, eStmt, &pStmt, apVal);
134358   if( rc==SQLITE_OK ){
134359     sqlite3_step(pStmt);
134360     rc = sqlite3_reset(pStmt);
134361   }
134362   *pRC = rc;
134363 }
134364 
134365 
134366 /*
134367 ** This function ensures that the caller has obtained an exclusive
134368 ** shared-cache table-lock on the %_segdir table. This is required before
134369 ** writing data to the fts3 table. If this lock is not acquired first, then
134370 ** the caller may end up attempting to take this lock as part of committing
134371 ** a transaction, causing SQLite to return SQLITE_LOCKED or
134372 ** LOCKED_SHAREDCACHEto a COMMIT command.
134373 **
134374 ** It is best to avoid this because if FTS3 returns any error when
134375 ** committing a transaction, the whole transaction will be rolled back.
134376 ** And this is not what users expect when they get SQLITE_LOCKED_SHAREDCACHE.
134377 ** It can still happen if the user locks the underlying tables directly
134378 ** instead of accessing them via FTS.
134379 */
134380 static int fts3Writelock(Fts3Table *p){
134381   int rc = SQLITE_OK;
134382 
134383   if( p->nPendingData==0 ){
134384     sqlite3_stmt *pStmt;
134385     rc = fts3SqlStmt(p, SQL_DELETE_SEGDIR_LEVEL, &pStmt, 0);
134386     if( rc==SQLITE_OK ){
134387       sqlite3_bind_null(pStmt, 1);
134388       sqlite3_step(pStmt);
134389       rc = sqlite3_reset(pStmt);
134390     }
134391   }
134392 
134393   return rc;
134394 }
134395 
134396 /*
134397 ** FTS maintains a separate indexes for each language-id (a 32-bit integer).
134398 ** Within each language id, a separate index is maintained to store the
134399 ** document terms, and each configured prefix size (configured the FTS
134400 ** "prefix=" option). And each index consists of multiple levels ("relative
134401 ** levels").
134402 **
134403 ** All three of these values (the language id, the specific index and the
134404 ** level within the index) are encoded in 64-bit integer values stored
134405 ** in the %_segdir table on disk. This function is used to convert three
134406 ** separate component values into the single 64-bit integer value that
134407 ** can be used to query the %_segdir table.
134408 **
134409 ** Specifically, each language-id/index combination is allocated 1024
134410 ** 64-bit integer level values ("absolute levels"). The main terms index
134411 ** for language-id 0 is allocate values 0-1023. The first prefix index
134412 ** (if any) for language-id 0 is allocated values 1024-2047. And so on.
134413 ** Language 1 indexes are allocated immediately following language 0.
134414 **
134415 ** So, for a system with nPrefix prefix indexes configured, the block of
134416 ** absolute levels that corresponds to language-id iLangid and index
134417 ** iIndex starts at absolute level ((iLangid * (nPrefix+1) + iIndex) * 1024).
134418 */
134419 static sqlite3_int64 getAbsoluteLevel(
134420   Fts3Table *p,                   /* FTS3 table handle */
134421   int iLangid,                    /* Language id */
134422   int iIndex,                     /* Index in p->aIndex[] */
134423   int iLevel                      /* Level of segments */
134424 ){
134425   sqlite3_int64 iBase;            /* First absolute level for iLangid/iIndex */
134426   assert( iLangid>=0 );
134427   assert( p->nIndex>0 );
134428   assert( iIndex>=0 && iIndex<p->nIndex );
134429 
134430   iBase = ((sqlite3_int64)iLangid * p->nIndex + iIndex) * FTS3_SEGDIR_MAXLEVEL;
134431   return iBase + iLevel;
134432 }
134433 
134434 /*
134435 ** Set *ppStmt to a statement handle that may be used to iterate through
134436 ** all rows in the %_segdir table, from oldest to newest. If successful,
134437 ** return SQLITE_OK. If an error occurs while preparing the statement,
134438 ** return an SQLite error code.
134439 **
134440 ** There is only ever one instance of this SQL statement compiled for
134441 ** each FTS3 table.
134442 **
134443 ** The statement returns the following columns from the %_segdir table:
134444 **
134445 **   0: idx
134446 **   1: start_block
134447 **   2: leaves_end_block
134448 **   3: end_block
134449 **   4: root
134450 */
134451 SQLITE_PRIVATE int sqlite3Fts3AllSegdirs(
134452   Fts3Table *p,                   /* FTS3 table */
134453   int iLangid,                    /* Language being queried */
134454   int iIndex,                     /* Index for p->aIndex[] */
134455   int iLevel,                     /* Level to select (relative level) */
134456   sqlite3_stmt **ppStmt           /* OUT: Compiled statement */
134457 ){
134458   int rc;
134459   sqlite3_stmt *pStmt = 0;
134460 
134461   assert( iLevel==FTS3_SEGCURSOR_ALL || iLevel>=0 );
134462   assert( iLevel<FTS3_SEGDIR_MAXLEVEL );
134463   assert( iIndex>=0 && iIndex<p->nIndex );
134464 
134465   if( iLevel<0 ){
134466     /* "SELECT * FROM %_segdir WHERE level BETWEEN ? AND ? ORDER BY ..." */
134467     rc = fts3SqlStmt(p, SQL_SELECT_LEVEL_RANGE, &pStmt, 0);
134468     if( rc==SQLITE_OK ){
134469       sqlite3_bind_int64(pStmt, 1, getAbsoluteLevel(p, iLangid, iIndex, 0));
134470       sqlite3_bind_int64(pStmt, 2,
134471           getAbsoluteLevel(p, iLangid, iIndex, FTS3_SEGDIR_MAXLEVEL-1)
134472       );
134473     }
134474   }else{
134475     /* "SELECT * FROM %_segdir WHERE level = ? ORDER BY ..." */
134476     rc = fts3SqlStmt(p, SQL_SELECT_LEVEL, &pStmt, 0);
134477     if( rc==SQLITE_OK ){
134478       sqlite3_bind_int64(pStmt, 1, getAbsoluteLevel(p, iLangid, iIndex,iLevel));
134479     }
134480   }
134481   *ppStmt = pStmt;
134482   return rc;
134483 }
134484 
134485 
134486 /*
134487 ** Append a single varint to a PendingList buffer. SQLITE_OK is returned
134488 ** if successful, or an SQLite error code otherwise.
134489 **
134490 ** This function also serves to allocate the PendingList structure itself.
134491 ** For example, to create a new PendingList structure containing two
134492 ** varints:
134493 **
134494 **   PendingList *p = 0;
134495 **   fts3PendingListAppendVarint(&p, 1);
134496 **   fts3PendingListAppendVarint(&p, 2);
134497 */
134498 static int fts3PendingListAppendVarint(
134499   PendingList **pp,               /* IN/OUT: Pointer to PendingList struct */
134500   sqlite3_int64 i                 /* Value to append to data */
134501 ){
134502   PendingList *p = *pp;
134503 
134504   /* Allocate or grow the PendingList as required. */
134505   if( !p ){
134506     p = sqlite3_malloc(sizeof(*p) + 100);
134507     if( !p ){
134508       return SQLITE_NOMEM;
134509     }
134510     p->nSpace = 100;
134511     p->aData = (char *)&p[1];
134512     p->nData = 0;
134513   }
134514   else if( p->nData+FTS3_VARINT_MAX+1>p->nSpace ){
134515     int nNew = p->nSpace * 2;
134516     p = sqlite3_realloc(p, sizeof(*p) + nNew);
134517     if( !p ){
134518       sqlite3_free(*pp);
134519       *pp = 0;
134520       return SQLITE_NOMEM;
134521     }
134522     p->nSpace = nNew;
134523     p->aData = (char *)&p[1];
134524   }
134525 
134526   /* Append the new serialized varint to the end of the list. */
134527   p->nData += sqlite3Fts3PutVarint(&p->aData[p->nData], i);
134528   p->aData[p->nData] = '\0';
134529   *pp = p;
134530   return SQLITE_OK;
134531 }
134532 
134533 /*
134534 ** Add a docid/column/position entry to a PendingList structure. Non-zero
134535 ** is returned if the structure is sqlite3_realloced as part of adding
134536 ** the entry. Otherwise, zero.
134537 **
134538 ** If an OOM error occurs, *pRc is set to SQLITE_NOMEM before returning.
134539 ** Zero is always returned in this case. Otherwise, if no OOM error occurs,
134540 ** it is set to SQLITE_OK.
134541 */
134542 static int fts3PendingListAppend(
134543   PendingList **pp,               /* IN/OUT: PendingList structure */
134544   sqlite3_int64 iDocid,           /* Docid for entry to add */
134545   sqlite3_int64 iCol,             /* Column for entry to add */
134546   sqlite3_int64 iPos,             /* Position of term for entry to add */
134547   int *pRc                        /* OUT: Return code */
134548 ){
134549   PendingList *p = *pp;
134550   int rc = SQLITE_OK;
134551 
134552   assert( !p || p->iLastDocid<=iDocid );
134553 
134554   if( !p || p->iLastDocid!=iDocid ){
134555     sqlite3_int64 iDelta = iDocid - (p ? p->iLastDocid : 0);
134556     if( p ){
134557       assert( p->nData<p->nSpace );
134558       assert( p->aData[p->nData]==0 );
134559       p->nData++;
134560     }
134561     if( SQLITE_OK!=(rc = fts3PendingListAppendVarint(&p, iDelta)) ){
134562       goto pendinglistappend_out;
134563     }
134564     p->iLastCol = -1;
134565     p->iLastPos = 0;
134566     p->iLastDocid = iDocid;
134567   }
134568   if( iCol>0 && p->iLastCol!=iCol ){
134569     if( SQLITE_OK!=(rc = fts3PendingListAppendVarint(&p, 1))
134570      || SQLITE_OK!=(rc = fts3PendingListAppendVarint(&p, iCol))
134571     ){
134572       goto pendinglistappend_out;
134573     }
134574     p->iLastCol = iCol;
134575     p->iLastPos = 0;
134576   }
134577   if( iCol>=0 ){
134578     assert( iPos>p->iLastPos || (iPos==0 && p->iLastPos==0) );
134579     rc = fts3PendingListAppendVarint(&p, 2+iPos-p->iLastPos);
134580     if( rc==SQLITE_OK ){
134581       p->iLastPos = iPos;
134582     }
134583   }
134584 
134585  pendinglistappend_out:
134586   *pRc = rc;
134587   if( p!=*pp ){
134588     *pp = p;
134589     return 1;
134590   }
134591   return 0;
134592 }
134593 
134594 /*
134595 ** Free a PendingList object allocated by fts3PendingListAppend().
134596 */
134597 static void fts3PendingListDelete(PendingList *pList){
134598   sqlite3_free(pList);
134599 }
134600 
134601 /*
134602 ** Add an entry to one of the pending-terms hash tables.
134603 */
134604 static int fts3PendingTermsAddOne(
134605   Fts3Table *p,
134606   int iCol,
134607   int iPos,
134608   Fts3Hash *pHash,                /* Pending terms hash table to add entry to */
134609   const char *zToken,
134610   int nToken
134611 ){
134612   PendingList *pList;
134613   int rc = SQLITE_OK;
134614 
134615   pList = (PendingList *)fts3HashFind(pHash, zToken, nToken);
134616   if( pList ){
134617     p->nPendingData -= (pList->nData + nToken + sizeof(Fts3HashElem));
134618   }
134619   if( fts3PendingListAppend(&pList, p->iPrevDocid, iCol, iPos, &rc) ){
134620     if( pList==fts3HashInsert(pHash, zToken, nToken, pList) ){
134621       /* Malloc failed while inserting the new entry. This can only
134622       ** happen if there was no previous entry for this token.
134623       */
134624       assert( 0==fts3HashFind(pHash, zToken, nToken) );
134625       sqlite3_free(pList);
134626       rc = SQLITE_NOMEM;
134627     }
134628   }
134629   if( rc==SQLITE_OK ){
134630     p->nPendingData += (pList->nData + nToken + sizeof(Fts3HashElem));
134631   }
134632   return rc;
134633 }
134634 
134635 /*
134636 ** Tokenize the nul-terminated string zText and add all tokens to the
134637 ** pending-terms hash-table. The docid used is that currently stored in
134638 ** p->iPrevDocid, and the column is specified by argument iCol.
134639 **
134640 ** If successful, SQLITE_OK is returned. Otherwise, an SQLite error code.
134641 */
134642 static int fts3PendingTermsAdd(
134643   Fts3Table *p,                   /* Table into which text will be inserted */
134644   int iLangid,                    /* Language id to use */
134645   const char *zText,              /* Text of document to be inserted */
134646   int iCol,                       /* Column into which text is being inserted */
134647   u32 *pnWord                     /* IN/OUT: Incr. by number tokens inserted */
134648 ){
134649   int rc;
134650   int iStart = 0;
134651   int iEnd = 0;
134652   int iPos = 0;
134653   int nWord = 0;
134654 
134655   char const *zToken;
134656   int nToken = 0;
134657 
134658   sqlite3_tokenizer *pTokenizer = p->pTokenizer;
134659   sqlite3_tokenizer_module const *pModule = pTokenizer->pModule;
134660   sqlite3_tokenizer_cursor *pCsr;
134661   int (*xNext)(sqlite3_tokenizer_cursor *pCursor,
134662       const char**,int*,int*,int*,int*);
134663 
134664   assert( pTokenizer && pModule );
134665 
134666   /* If the user has inserted a NULL value, this function may be called with
134667   ** zText==0. In this case, add zero token entries to the hash table and
134668   ** return early. */
134669   if( zText==0 ){
134670     *pnWord = 0;
134671     return SQLITE_OK;
134672   }
134673 
134674   rc = sqlite3Fts3OpenTokenizer(pTokenizer, iLangid, zText, -1, &pCsr);
134675   if( rc!=SQLITE_OK ){
134676     return rc;
134677   }
134678 
134679   xNext = pModule->xNext;
134680   while( SQLITE_OK==rc
134681       && SQLITE_OK==(rc = xNext(pCsr, &zToken, &nToken, &iStart, &iEnd, &iPos))
134682   ){
134683     int i;
134684     if( iPos>=nWord ) nWord = iPos+1;
134685 
134686     /* Positions cannot be negative; we use -1 as a terminator internally.
134687     ** Tokens must have a non-zero length.
134688     */
134689     if( iPos<0 || !zToken || nToken<=0 ){
134690       rc = SQLITE_ERROR;
134691       break;
134692     }
134693 
134694     /* Add the term to the terms index */
134695     rc = fts3PendingTermsAddOne(
134696         p, iCol, iPos, &p->aIndex[0].hPending, zToken, nToken
134697     );
134698 
134699     /* Add the term to each of the prefix indexes that it is not too
134700     ** short for. */
134701     for(i=1; rc==SQLITE_OK && i<p->nIndex; i++){
134702       struct Fts3Index *pIndex = &p->aIndex[i];
134703       if( nToken<pIndex->nPrefix ) continue;
134704       rc = fts3PendingTermsAddOne(
134705           p, iCol, iPos, &pIndex->hPending, zToken, pIndex->nPrefix
134706       );
134707     }
134708   }
134709 
134710   pModule->xClose(pCsr);
134711   *pnWord += nWord;
134712   return (rc==SQLITE_DONE ? SQLITE_OK : rc);
134713 }
134714 
134715 /*
134716 ** Calling this function indicates that subsequent calls to
134717 ** fts3PendingTermsAdd() are to add term/position-list pairs for the
134718 ** contents of the document with docid iDocid.
134719 */
134720 static int fts3PendingTermsDocid(
134721   Fts3Table *p,                   /* Full-text table handle */
134722   int iLangid,                    /* Language id of row being written */
134723   sqlite_int64 iDocid             /* Docid of row being written */
134724 ){
134725   assert( iLangid>=0 );
134726 
134727   /* TODO(shess) Explore whether partially flushing the buffer on
134728   ** forced-flush would provide better performance.  I suspect that if
134729   ** we ordered the doclists by size and flushed the largest until the
134730   ** buffer was half empty, that would let the less frequent terms
134731   ** generate longer doclists.
134732   */
134733   if( iDocid<=p->iPrevDocid
134734    || p->iPrevLangid!=iLangid
134735    || p->nPendingData>p->nMaxPendingData
134736   ){
134737     int rc = sqlite3Fts3PendingTermsFlush(p);
134738     if( rc!=SQLITE_OK ) return rc;
134739   }
134740   p->iPrevDocid = iDocid;
134741   p->iPrevLangid = iLangid;
134742   return SQLITE_OK;
134743 }
134744 
134745 /*
134746 ** Discard the contents of the pending-terms hash tables.
134747 */
134748 SQLITE_PRIVATE void sqlite3Fts3PendingTermsClear(Fts3Table *p){
134749   int i;
134750   for(i=0; i<p->nIndex; i++){
134751     Fts3HashElem *pElem;
134752     Fts3Hash *pHash = &p->aIndex[i].hPending;
134753     for(pElem=fts3HashFirst(pHash); pElem; pElem=fts3HashNext(pElem)){
134754       PendingList *pList = (PendingList *)fts3HashData(pElem);
134755       fts3PendingListDelete(pList);
134756     }
134757     fts3HashClear(pHash);
134758   }
134759   p->nPendingData = 0;
134760 }
134761 
134762 /*
134763 ** This function is called by the xUpdate() method as part of an INSERT
134764 ** operation. It adds entries for each term in the new record to the
134765 ** pendingTerms hash table.
134766 **
134767 ** Argument apVal is the same as the similarly named argument passed to
134768 ** fts3InsertData(). Parameter iDocid is the docid of the new row.
134769 */
134770 static int fts3InsertTerms(
134771   Fts3Table *p,
134772   int iLangid,
134773   sqlite3_value **apVal,
134774   u32 *aSz
134775 ){
134776   int i;                          /* Iterator variable */
134777   for(i=2; i<p->nColumn+2; i++){
134778     int iCol = i-2;
134779     if( p->abNotindexed[iCol]==0 ){
134780       const char *zText = (const char *)sqlite3_value_text(apVal[i]);
134781       int rc = fts3PendingTermsAdd(p, iLangid, zText, iCol, &aSz[iCol]);
134782       if( rc!=SQLITE_OK ){
134783         return rc;
134784       }
134785       aSz[p->nColumn] += sqlite3_value_bytes(apVal[i]);
134786     }
134787   }
134788   return SQLITE_OK;
134789 }
134790 
134791 /*
134792 ** This function is called by the xUpdate() method for an INSERT operation.
134793 ** The apVal parameter is passed a copy of the apVal argument passed by
134794 ** SQLite to the xUpdate() method. i.e:
134795 **
134796 **   apVal[0]                Not used for INSERT.
134797 **   apVal[1]                rowid
134798 **   apVal[2]                Left-most user-defined column
134799 **   ...
134800 **   apVal[p->nColumn+1]     Right-most user-defined column
134801 **   apVal[p->nColumn+2]     Hidden column with same name as table
134802 **   apVal[p->nColumn+3]     Hidden "docid" column (alias for rowid)
134803 **   apVal[p->nColumn+4]     Hidden languageid column
134804 */
134805 static int fts3InsertData(
134806   Fts3Table *p,                   /* Full-text table */
134807   sqlite3_value **apVal,          /* Array of values to insert */
134808   sqlite3_int64 *piDocid          /* OUT: Docid for row just inserted */
134809 ){
134810   int rc;                         /* Return code */
134811   sqlite3_stmt *pContentInsert;   /* INSERT INTO %_content VALUES(...) */
134812 
134813   if( p->zContentTbl ){
134814     sqlite3_value *pRowid = apVal[p->nColumn+3];
134815     if( sqlite3_value_type(pRowid)==SQLITE_NULL ){
134816       pRowid = apVal[1];
134817     }
134818     if( sqlite3_value_type(pRowid)!=SQLITE_INTEGER ){
134819       return SQLITE_CONSTRAINT;
134820     }
134821     *piDocid = sqlite3_value_int64(pRowid);
134822     return SQLITE_OK;
134823   }
134824 
134825   /* Locate the statement handle used to insert data into the %_content
134826   ** table. The SQL for this statement is:
134827   **
134828   **   INSERT INTO %_content VALUES(?, ?, ?, ...)
134829   **
134830   ** The statement features N '?' variables, where N is the number of user
134831   ** defined columns in the FTS3 table, plus one for the docid field.
134832   */
134833   rc = fts3SqlStmt(p, SQL_CONTENT_INSERT, &pContentInsert, &apVal[1]);
134834   if( rc==SQLITE_OK && p->zLanguageid ){
134835     rc = sqlite3_bind_int(
134836         pContentInsert, p->nColumn+2,
134837         sqlite3_value_int(apVal[p->nColumn+4])
134838     );
134839   }
134840   if( rc!=SQLITE_OK ) return rc;
134841 
134842   /* There is a quirk here. The users INSERT statement may have specified
134843   ** a value for the "rowid" field, for the "docid" field, or for both.
134844   ** Which is a problem, since "rowid" and "docid" are aliases for the
134845   ** same value. For example:
134846   **
134847   **   INSERT INTO fts3tbl(rowid, docid) VALUES(1, 2);
134848   **
134849   ** In FTS3, this is an error. It is an error to specify non-NULL values
134850   ** for both docid and some other rowid alias.
134851   */
134852   if( SQLITE_NULL!=sqlite3_value_type(apVal[3+p->nColumn]) ){
134853     if( SQLITE_NULL==sqlite3_value_type(apVal[0])
134854      && SQLITE_NULL!=sqlite3_value_type(apVal[1])
134855     ){
134856       /* A rowid/docid conflict. */
134857       return SQLITE_ERROR;
134858     }
134859     rc = sqlite3_bind_value(pContentInsert, 1, apVal[3+p->nColumn]);
134860     if( rc!=SQLITE_OK ) return rc;
134861   }
134862 
134863   /* Execute the statement to insert the record. Set *piDocid to the
134864   ** new docid value.
134865   */
134866   sqlite3_step(pContentInsert);
134867   rc = sqlite3_reset(pContentInsert);
134868 
134869   *piDocid = sqlite3_last_insert_rowid(p->db);
134870   return rc;
134871 }
134872 
134873 
134874 
134875 /*
134876 ** Remove all data from the FTS3 table. Clear the hash table containing
134877 ** pending terms.
134878 */
134879 static int fts3DeleteAll(Fts3Table *p, int bContent){
134880   int rc = SQLITE_OK;             /* Return code */
134881 
134882   /* Discard the contents of the pending-terms hash table. */
134883   sqlite3Fts3PendingTermsClear(p);
134884 
134885   /* Delete everything from the shadow tables. Except, leave %_content as
134886   ** is if bContent is false.  */
134887   assert( p->zContentTbl==0 || bContent==0 );
134888   if( bContent ) fts3SqlExec(&rc, p, SQL_DELETE_ALL_CONTENT, 0);
134889   fts3SqlExec(&rc, p, SQL_DELETE_ALL_SEGMENTS, 0);
134890   fts3SqlExec(&rc, p, SQL_DELETE_ALL_SEGDIR, 0);
134891   if( p->bHasDocsize ){
134892     fts3SqlExec(&rc, p, SQL_DELETE_ALL_DOCSIZE, 0);
134893   }
134894   if( p->bHasStat ){
134895     fts3SqlExec(&rc, p, SQL_DELETE_ALL_STAT, 0);
134896   }
134897   return rc;
134898 }
134899 
134900 /*
134901 **
134902 */
134903 static int langidFromSelect(Fts3Table *p, sqlite3_stmt *pSelect){
134904   int iLangid = 0;
134905   if( p->zLanguageid ) iLangid = sqlite3_column_int(pSelect, p->nColumn+1);
134906   return iLangid;
134907 }
134908 
134909 /*
134910 ** The first element in the apVal[] array is assumed to contain the docid
134911 ** (an integer) of a row about to be deleted. Remove all terms from the
134912 ** full-text index.
134913 */
134914 static void fts3DeleteTerms(
134915   int *pRC,               /* Result code */
134916   Fts3Table *p,           /* The FTS table to delete from */
134917   sqlite3_value *pRowid,  /* The docid to be deleted */
134918   u32 *aSz,               /* Sizes of deleted document written here */
134919   int *pbFound            /* OUT: Set to true if row really does exist */
134920 ){
134921   int rc;
134922   sqlite3_stmt *pSelect;
134923 
134924   assert( *pbFound==0 );
134925   if( *pRC ) return;
134926   rc = fts3SqlStmt(p, SQL_SELECT_CONTENT_BY_ROWID, &pSelect, &pRowid);
134927   if( rc==SQLITE_OK ){
134928     if( SQLITE_ROW==sqlite3_step(pSelect) ){
134929       int i;
134930       int iLangid = langidFromSelect(p, pSelect);
134931       rc = fts3PendingTermsDocid(p, iLangid, sqlite3_column_int64(pSelect, 0));
134932       for(i=1; rc==SQLITE_OK && i<=p->nColumn; i++){
134933         int iCol = i-1;
134934         if( p->abNotindexed[iCol]==0 ){
134935           const char *zText = (const char *)sqlite3_column_text(pSelect, i);
134936           rc = fts3PendingTermsAdd(p, iLangid, zText, -1, &aSz[iCol]);
134937           aSz[p->nColumn] += sqlite3_column_bytes(pSelect, i);
134938         }
134939       }
134940       if( rc!=SQLITE_OK ){
134941         sqlite3_reset(pSelect);
134942         *pRC = rc;
134943         return;
134944       }
134945       *pbFound = 1;
134946     }
134947     rc = sqlite3_reset(pSelect);
134948   }else{
134949     sqlite3_reset(pSelect);
134950   }
134951   *pRC = rc;
134952 }
134953 
134954 /*
134955 ** Forward declaration to account for the circular dependency between
134956 ** functions fts3SegmentMerge() and fts3AllocateSegdirIdx().
134957 */
134958 static int fts3SegmentMerge(Fts3Table *, int, int, int);
134959 
134960 /*
134961 ** This function allocates a new level iLevel index in the segdir table.
134962 ** Usually, indexes are allocated within a level sequentially starting
134963 ** with 0, so the allocated index is one greater than the value returned
134964 ** by:
134965 **
134966 **   SELECT max(idx) FROM %_segdir WHERE level = :iLevel
134967 **
134968 ** However, if there are already FTS3_MERGE_COUNT indexes at the requested
134969 ** level, they are merged into a single level (iLevel+1) segment and the
134970 ** allocated index is 0.
134971 **
134972 ** If successful, *piIdx is set to the allocated index slot and SQLITE_OK
134973 ** returned. Otherwise, an SQLite error code is returned.
134974 */
134975 static int fts3AllocateSegdirIdx(
134976   Fts3Table *p,
134977   int iLangid,                    /* Language id */
134978   int iIndex,                     /* Index for p->aIndex */
134979   int iLevel,
134980   int *piIdx
134981 ){
134982   int rc;                         /* Return Code */
134983   sqlite3_stmt *pNextIdx;         /* Query for next idx at level iLevel */
134984   int iNext = 0;                  /* Result of query pNextIdx */
134985 
134986   assert( iLangid>=0 );
134987   assert( p->nIndex>=1 );
134988 
134989   /* Set variable iNext to the next available segdir index at level iLevel. */
134990   rc = fts3SqlStmt(p, SQL_NEXT_SEGMENT_INDEX, &pNextIdx, 0);
134991   if( rc==SQLITE_OK ){
134992     sqlite3_bind_int64(
134993         pNextIdx, 1, getAbsoluteLevel(p, iLangid, iIndex, iLevel)
134994     );
134995     if( SQLITE_ROW==sqlite3_step(pNextIdx) ){
134996       iNext = sqlite3_column_int(pNextIdx, 0);
134997     }
134998     rc = sqlite3_reset(pNextIdx);
134999   }
135000 
135001   if( rc==SQLITE_OK ){
135002     /* If iNext is FTS3_MERGE_COUNT, indicating that level iLevel is already
135003     ** full, merge all segments in level iLevel into a single iLevel+1
135004     ** segment and allocate (newly freed) index 0 at level iLevel. Otherwise,
135005     ** if iNext is less than FTS3_MERGE_COUNT, allocate index iNext.
135006     */
135007     if( iNext>=FTS3_MERGE_COUNT ){
135008       fts3LogMerge(16, getAbsoluteLevel(p, iLangid, iIndex, iLevel));
135009       rc = fts3SegmentMerge(p, iLangid, iIndex, iLevel);
135010       *piIdx = 0;
135011     }else{
135012       *piIdx = iNext;
135013     }
135014   }
135015 
135016   return rc;
135017 }
135018 
135019 /*
135020 ** The %_segments table is declared as follows:
135021 **
135022 **   CREATE TABLE %_segments(blockid INTEGER PRIMARY KEY, block BLOB)
135023 **
135024 ** This function reads data from a single row of the %_segments table. The
135025 ** specific row is identified by the iBlockid parameter. If paBlob is not
135026 ** NULL, then a buffer is allocated using sqlite3_malloc() and populated
135027 ** with the contents of the blob stored in the "block" column of the
135028 ** identified table row is. Whether or not paBlob is NULL, *pnBlob is set
135029 ** to the size of the blob in bytes before returning.
135030 **
135031 ** If an error occurs, or the table does not contain the specified row,
135032 ** an SQLite error code is returned. Otherwise, SQLITE_OK is returned. If
135033 ** paBlob is non-NULL, then it is the responsibility of the caller to
135034 ** eventually free the returned buffer.
135035 **
135036 ** This function may leave an open sqlite3_blob* handle in the
135037 ** Fts3Table.pSegments variable. This handle is reused by subsequent calls
135038 ** to this function. The handle may be closed by calling the
135039 ** sqlite3Fts3SegmentsClose() function. Reusing a blob handle is a handy
135040 ** performance improvement, but the blob handle should always be closed
135041 ** before control is returned to the user (to prevent a lock being held
135042 ** on the database file for longer than necessary). Thus, any virtual table
135043 ** method (xFilter etc.) that may directly or indirectly call this function
135044 ** must call sqlite3Fts3SegmentsClose() before returning.
135045 */
135046 SQLITE_PRIVATE int sqlite3Fts3ReadBlock(
135047   Fts3Table *p,                   /* FTS3 table handle */
135048   sqlite3_int64 iBlockid,         /* Access the row with blockid=$iBlockid */
135049   char **paBlob,                  /* OUT: Blob data in malloc'd buffer */
135050   int *pnBlob,                    /* OUT: Size of blob data */
135051   int *pnLoad                     /* OUT: Bytes actually loaded */
135052 ){
135053   int rc;                         /* Return code */
135054 
135055   /* pnBlob must be non-NULL. paBlob may be NULL or non-NULL. */
135056   assert( pnBlob );
135057 
135058   if( p->pSegments ){
135059     rc = sqlite3_blob_reopen(p->pSegments, iBlockid);
135060   }else{
135061     if( 0==p->zSegmentsTbl ){
135062       p->zSegmentsTbl = sqlite3_mprintf("%s_segments", p->zName);
135063       if( 0==p->zSegmentsTbl ) return SQLITE_NOMEM;
135064     }
135065     rc = sqlite3_blob_open(
135066        p->db, p->zDb, p->zSegmentsTbl, "block", iBlockid, 0, &p->pSegments
135067     );
135068   }
135069 
135070   if( rc==SQLITE_OK ){
135071     int nByte = sqlite3_blob_bytes(p->pSegments);
135072     *pnBlob = nByte;
135073     if( paBlob ){
135074       char *aByte = sqlite3_malloc(nByte + FTS3_NODE_PADDING);
135075       if( !aByte ){
135076         rc = SQLITE_NOMEM;
135077       }else{
135078         if( pnLoad && nByte>(FTS3_NODE_CHUNK_THRESHOLD) ){
135079           nByte = FTS3_NODE_CHUNKSIZE;
135080           *pnLoad = nByte;
135081         }
135082         rc = sqlite3_blob_read(p->pSegments, aByte, nByte, 0);
135083         memset(&aByte[nByte], 0, FTS3_NODE_PADDING);
135084         if( rc!=SQLITE_OK ){
135085           sqlite3_free(aByte);
135086           aByte = 0;
135087         }
135088       }
135089       *paBlob = aByte;
135090     }
135091   }
135092 
135093   return rc;
135094 }
135095 
135096 /*
135097 ** Close the blob handle at p->pSegments, if it is open. See comments above
135098 ** the sqlite3Fts3ReadBlock() function for details.
135099 */
135100 SQLITE_PRIVATE void sqlite3Fts3SegmentsClose(Fts3Table *p){
135101   sqlite3_blob_close(p->pSegments);
135102   p->pSegments = 0;
135103 }
135104 
135105 static int fts3SegReaderIncrRead(Fts3SegReader *pReader){
135106   int nRead;                      /* Number of bytes to read */
135107   int rc;                         /* Return code */
135108 
135109   nRead = MIN(pReader->nNode - pReader->nPopulate, FTS3_NODE_CHUNKSIZE);
135110   rc = sqlite3_blob_read(
135111       pReader->pBlob,
135112       &pReader->aNode[pReader->nPopulate],
135113       nRead,
135114       pReader->nPopulate
135115   );
135116 
135117   if( rc==SQLITE_OK ){
135118     pReader->nPopulate += nRead;
135119     memset(&pReader->aNode[pReader->nPopulate], 0, FTS3_NODE_PADDING);
135120     if( pReader->nPopulate==pReader->nNode ){
135121       sqlite3_blob_close(pReader->pBlob);
135122       pReader->pBlob = 0;
135123       pReader->nPopulate = 0;
135124     }
135125   }
135126   return rc;
135127 }
135128 
135129 static int fts3SegReaderRequire(Fts3SegReader *pReader, char *pFrom, int nByte){
135130   int rc = SQLITE_OK;
135131   assert( !pReader->pBlob
135132        || (pFrom>=pReader->aNode && pFrom<&pReader->aNode[pReader->nNode])
135133   );
135134   while( pReader->pBlob && rc==SQLITE_OK
135135      &&  (pFrom - pReader->aNode + nByte)>pReader->nPopulate
135136   ){
135137     rc = fts3SegReaderIncrRead(pReader);
135138   }
135139   return rc;
135140 }
135141 
135142 /*
135143 ** Set an Fts3SegReader cursor to point at EOF.
135144 */
135145 static void fts3SegReaderSetEof(Fts3SegReader *pSeg){
135146   if( !fts3SegReaderIsRootOnly(pSeg) ){
135147     sqlite3_free(pSeg->aNode);
135148     sqlite3_blob_close(pSeg->pBlob);
135149     pSeg->pBlob = 0;
135150   }
135151   pSeg->aNode = 0;
135152 }
135153 
135154 /*
135155 ** Move the iterator passed as the first argument to the next term in the
135156 ** segment. If successful, SQLITE_OK is returned. If there is no next term,
135157 ** SQLITE_DONE. Otherwise, an SQLite error code.
135158 */
135159 static int fts3SegReaderNext(
135160   Fts3Table *p,
135161   Fts3SegReader *pReader,
135162   int bIncr
135163 ){
135164   int rc;                         /* Return code of various sub-routines */
135165   char *pNext;                    /* Cursor variable */
135166   int nPrefix;                    /* Number of bytes in term prefix */
135167   int nSuffix;                    /* Number of bytes in term suffix */
135168 
135169   if( !pReader->aDoclist ){
135170     pNext = pReader->aNode;
135171   }else{
135172     pNext = &pReader->aDoclist[pReader->nDoclist];
135173   }
135174 
135175   if( !pNext || pNext>=&pReader->aNode[pReader->nNode] ){
135176 
135177     if( fts3SegReaderIsPending(pReader) ){
135178       Fts3HashElem *pElem = *(pReader->ppNextElem);
135179       if( pElem==0 ){
135180         pReader->aNode = 0;
135181       }else{
135182         PendingList *pList = (PendingList *)fts3HashData(pElem);
135183         pReader->zTerm = (char *)fts3HashKey(pElem);
135184         pReader->nTerm = fts3HashKeysize(pElem);
135185         pReader->nNode = pReader->nDoclist = pList->nData + 1;
135186         pReader->aNode = pReader->aDoclist = pList->aData;
135187         pReader->ppNextElem++;
135188         assert( pReader->aNode );
135189       }
135190       return SQLITE_OK;
135191     }
135192 
135193     fts3SegReaderSetEof(pReader);
135194 
135195     /* If iCurrentBlock>=iLeafEndBlock, this is an EOF condition. All leaf
135196     ** blocks have already been traversed.  */
135197     assert( pReader->iCurrentBlock<=pReader->iLeafEndBlock );
135198     if( pReader->iCurrentBlock>=pReader->iLeafEndBlock ){
135199       return SQLITE_OK;
135200     }
135201 
135202     rc = sqlite3Fts3ReadBlock(
135203         p, ++pReader->iCurrentBlock, &pReader->aNode, &pReader->nNode,
135204         (bIncr ? &pReader->nPopulate : 0)
135205     );
135206     if( rc!=SQLITE_OK ) return rc;
135207     assert( pReader->pBlob==0 );
135208     if( bIncr && pReader->nPopulate<pReader->nNode ){
135209       pReader->pBlob = p->pSegments;
135210       p->pSegments = 0;
135211     }
135212     pNext = pReader->aNode;
135213   }
135214 
135215   assert( !fts3SegReaderIsPending(pReader) );
135216 
135217   rc = fts3SegReaderRequire(pReader, pNext, FTS3_VARINT_MAX*2);
135218   if( rc!=SQLITE_OK ) return rc;
135219 
135220   /* Because of the FTS3_NODE_PADDING bytes of padding, the following is
135221   ** safe (no risk of overread) even if the node data is corrupted. */
135222   pNext += fts3GetVarint32(pNext, &nPrefix);
135223   pNext += fts3GetVarint32(pNext, &nSuffix);
135224   if( nPrefix<0 || nSuffix<=0
135225    || &pNext[nSuffix]>&pReader->aNode[pReader->nNode]
135226   ){
135227     return FTS_CORRUPT_VTAB;
135228   }
135229 
135230   if( nPrefix+nSuffix>pReader->nTermAlloc ){
135231     int nNew = (nPrefix+nSuffix)*2;
135232     char *zNew = sqlite3_realloc(pReader->zTerm, nNew);
135233     if( !zNew ){
135234       return SQLITE_NOMEM;
135235     }
135236     pReader->zTerm = zNew;
135237     pReader->nTermAlloc = nNew;
135238   }
135239 
135240   rc = fts3SegReaderRequire(pReader, pNext, nSuffix+FTS3_VARINT_MAX);
135241   if( rc!=SQLITE_OK ) return rc;
135242 
135243   memcpy(&pReader->zTerm[nPrefix], pNext, nSuffix);
135244   pReader->nTerm = nPrefix+nSuffix;
135245   pNext += nSuffix;
135246   pNext += fts3GetVarint32(pNext, &pReader->nDoclist);
135247   pReader->aDoclist = pNext;
135248   pReader->pOffsetList = 0;
135249 
135250   /* Check that the doclist does not appear to extend past the end of the
135251   ** b-tree node. And that the final byte of the doclist is 0x00. If either
135252   ** of these statements is untrue, then the data structure is corrupt.
135253   */
135254   if( &pReader->aDoclist[pReader->nDoclist]>&pReader->aNode[pReader->nNode]
135255    || (pReader->nPopulate==0 && pReader->aDoclist[pReader->nDoclist-1])
135256   ){
135257     return FTS_CORRUPT_VTAB;
135258   }
135259   return SQLITE_OK;
135260 }
135261 
135262 /*
135263 ** Set the SegReader to point to the first docid in the doclist associated
135264 ** with the current term.
135265 */
135266 static int fts3SegReaderFirstDocid(Fts3Table *pTab, Fts3SegReader *pReader){
135267   int rc = SQLITE_OK;
135268   assert( pReader->aDoclist );
135269   assert( !pReader->pOffsetList );
135270   if( pTab->bDescIdx && fts3SegReaderIsPending(pReader) ){
135271     u8 bEof = 0;
135272     pReader->iDocid = 0;
135273     pReader->nOffsetList = 0;
135274     sqlite3Fts3DoclistPrev(0,
135275         pReader->aDoclist, pReader->nDoclist, &pReader->pOffsetList,
135276         &pReader->iDocid, &pReader->nOffsetList, &bEof
135277     );
135278   }else{
135279     rc = fts3SegReaderRequire(pReader, pReader->aDoclist, FTS3_VARINT_MAX);
135280     if( rc==SQLITE_OK ){
135281       int n = sqlite3Fts3GetVarint(pReader->aDoclist, &pReader->iDocid);
135282       pReader->pOffsetList = &pReader->aDoclist[n];
135283     }
135284   }
135285   return rc;
135286 }
135287 
135288 /*
135289 ** Advance the SegReader to point to the next docid in the doclist
135290 ** associated with the current term.
135291 **
135292 ** If arguments ppOffsetList and pnOffsetList are not NULL, then
135293 ** *ppOffsetList is set to point to the first column-offset list
135294 ** in the doclist entry (i.e. immediately past the docid varint).
135295 ** *pnOffsetList is set to the length of the set of column-offset
135296 ** lists, not including the nul-terminator byte. For example:
135297 */
135298 static int fts3SegReaderNextDocid(
135299   Fts3Table *pTab,
135300   Fts3SegReader *pReader,         /* Reader to advance to next docid */
135301   char **ppOffsetList,            /* OUT: Pointer to current position-list */
135302   int *pnOffsetList               /* OUT: Length of *ppOffsetList in bytes */
135303 ){
135304   int rc = SQLITE_OK;
135305   char *p = pReader->pOffsetList;
135306   char c = 0;
135307 
135308   assert( p );
135309 
135310   if( pTab->bDescIdx && fts3SegReaderIsPending(pReader) ){
135311     /* A pending-terms seg-reader for an FTS4 table that uses order=desc.
135312     ** Pending-terms doclists are always built up in ascending order, so
135313     ** we have to iterate through them backwards here. */
135314     u8 bEof = 0;
135315     if( ppOffsetList ){
135316       *ppOffsetList = pReader->pOffsetList;
135317       *pnOffsetList = pReader->nOffsetList - 1;
135318     }
135319     sqlite3Fts3DoclistPrev(0,
135320         pReader->aDoclist, pReader->nDoclist, &p, &pReader->iDocid,
135321         &pReader->nOffsetList, &bEof
135322     );
135323     if( bEof ){
135324       pReader->pOffsetList = 0;
135325     }else{
135326       pReader->pOffsetList = p;
135327     }
135328   }else{
135329     char *pEnd = &pReader->aDoclist[pReader->nDoclist];
135330 
135331     /* Pointer p currently points at the first byte of an offset list. The
135332     ** following block advances it to point one byte past the end of
135333     ** the same offset list. */
135334     while( 1 ){
135335 
135336       /* The following line of code (and the "p++" below the while() loop) is
135337       ** normally all that is required to move pointer p to the desired
135338       ** position. The exception is if this node is being loaded from disk
135339       ** incrementally and pointer "p" now points to the first byte past
135340       ** the populated part of pReader->aNode[].
135341       */
135342       while( *p | c ) c = *p++ & 0x80;
135343       assert( *p==0 );
135344 
135345       if( pReader->pBlob==0 || p<&pReader->aNode[pReader->nPopulate] ) break;
135346       rc = fts3SegReaderIncrRead(pReader);
135347       if( rc!=SQLITE_OK ) return rc;
135348     }
135349     p++;
135350 
135351     /* If required, populate the output variables with a pointer to and the
135352     ** size of the previous offset-list.
135353     */
135354     if( ppOffsetList ){
135355       *ppOffsetList = pReader->pOffsetList;
135356       *pnOffsetList = (int)(p - pReader->pOffsetList - 1);
135357     }
135358 
135359     /* List may have been edited in place by fts3EvalNearTrim() */
135360     while( p<pEnd && *p==0 ) p++;
135361 
135362     /* If there are no more entries in the doclist, set pOffsetList to
135363     ** NULL. Otherwise, set Fts3SegReader.iDocid to the next docid and
135364     ** Fts3SegReader.pOffsetList to point to the next offset list before
135365     ** returning.
135366     */
135367     if( p>=pEnd ){
135368       pReader->pOffsetList = 0;
135369     }else{
135370       rc = fts3SegReaderRequire(pReader, p, FTS3_VARINT_MAX);
135371       if( rc==SQLITE_OK ){
135372         sqlite3_int64 iDelta;
135373         pReader->pOffsetList = p + sqlite3Fts3GetVarint(p, &iDelta);
135374         if( pTab->bDescIdx ){
135375           pReader->iDocid -= iDelta;
135376         }else{
135377           pReader->iDocid += iDelta;
135378         }
135379       }
135380     }
135381   }
135382 
135383   return SQLITE_OK;
135384 }
135385 
135386 
135387 SQLITE_PRIVATE int sqlite3Fts3MsrOvfl(
135388   Fts3Cursor *pCsr,
135389   Fts3MultiSegReader *pMsr,
135390   int *pnOvfl
135391 ){
135392   Fts3Table *p = (Fts3Table*)pCsr->base.pVtab;
135393   int nOvfl = 0;
135394   int ii;
135395   int rc = SQLITE_OK;
135396   int pgsz = p->nPgsz;
135397 
135398   assert( p->bFts4 );
135399   assert( pgsz>0 );
135400 
135401   for(ii=0; rc==SQLITE_OK && ii<pMsr->nSegment; ii++){
135402     Fts3SegReader *pReader = pMsr->apSegment[ii];
135403     if( !fts3SegReaderIsPending(pReader)
135404      && !fts3SegReaderIsRootOnly(pReader)
135405     ){
135406       sqlite3_int64 jj;
135407       for(jj=pReader->iStartBlock; jj<=pReader->iLeafEndBlock; jj++){
135408         int nBlob;
135409         rc = sqlite3Fts3ReadBlock(p, jj, 0, &nBlob, 0);
135410         if( rc!=SQLITE_OK ) break;
135411         if( (nBlob+35)>pgsz ){
135412           nOvfl += (nBlob + 34)/pgsz;
135413         }
135414       }
135415     }
135416   }
135417   *pnOvfl = nOvfl;
135418   return rc;
135419 }
135420 
135421 /*
135422 ** Free all allocations associated with the iterator passed as the
135423 ** second argument.
135424 */
135425 SQLITE_PRIVATE void sqlite3Fts3SegReaderFree(Fts3SegReader *pReader){
135426   if( pReader && !fts3SegReaderIsPending(pReader) ){
135427     sqlite3_free(pReader->zTerm);
135428     if( !fts3SegReaderIsRootOnly(pReader) ){
135429       sqlite3_free(pReader->aNode);
135430       sqlite3_blob_close(pReader->pBlob);
135431     }
135432   }
135433   sqlite3_free(pReader);
135434 }
135435 
135436 /*
135437 ** Allocate a new SegReader object.
135438 */
135439 SQLITE_PRIVATE int sqlite3Fts3SegReaderNew(
135440   int iAge,                       /* Segment "age". */
135441   int bLookup,                    /* True for a lookup only */
135442   sqlite3_int64 iStartLeaf,       /* First leaf to traverse */
135443   sqlite3_int64 iEndLeaf,         /* Final leaf to traverse */
135444   sqlite3_int64 iEndBlock,        /* Final block of segment */
135445   const char *zRoot,              /* Buffer containing root node */
135446   int nRoot,                      /* Size of buffer containing root node */
135447   Fts3SegReader **ppReader        /* OUT: Allocated Fts3SegReader */
135448 ){
135449   Fts3SegReader *pReader;         /* Newly allocated SegReader object */
135450   int nExtra = 0;                 /* Bytes to allocate segment root node */
135451 
135452   assert( iStartLeaf<=iEndLeaf );
135453   if( iStartLeaf==0 ){
135454     nExtra = nRoot + FTS3_NODE_PADDING;
135455   }
135456 
135457   pReader = (Fts3SegReader *)sqlite3_malloc(sizeof(Fts3SegReader) + nExtra);
135458   if( !pReader ){
135459     return SQLITE_NOMEM;
135460   }
135461   memset(pReader, 0, sizeof(Fts3SegReader));
135462   pReader->iIdx = iAge;
135463   pReader->bLookup = bLookup!=0;
135464   pReader->iStartBlock = iStartLeaf;
135465   pReader->iLeafEndBlock = iEndLeaf;
135466   pReader->iEndBlock = iEndBlock;
135467 
135468   if( nExtra ){
135469     /* The entire segment is stored in the root node. */
135470     pReader->aNode = (char *)&pReader[1];
135471     pReader->rootOnly = 1;
135472     pReader->nNode = nRoot;
135473     memcpy(pReader->aNode, zRoot, nRoot);
135474     memset(&pReader->aNode[nRoot], 0, FTS3_NODE_PADDING);
135475   }else{
135476     pReader->iCurrentBlock = iStartLeaf-1;
135477   }
135478   *ppReader = pReader;
135479   return SQLITE_OK;
135480 }
135481 
135482 /*
135483 ** This is a comparison function used as a qsort() callback when sorting
135484 ** an array of pending terms by term. This occurs as part of flushing
135485 ** the contents of the pending-terms hash table to the database.
135486 */
135487 static int fts3CompareElemByTerm(const void *lhs, const void *rhs){
135488   char *z1 = fts3HashKey(*(Fts3HashElem **)lhs);
135489   char *z2 = fts3HashKey(*(Fts3HashElem **)rhs);
135490   int n1 = fts3HashKeysize(*(Fts3HashElem **)lhs);
135491   int n2 = fts3HashKeysize(*(Fts3HashElem **)rhs);
135492 
135493   int n = (n1<n2 ? n1 : n2);
135494   int c = memcmp(z1, z2, n);
135495   if( c==0 ){
135496     c = n1 - n2;
135497   }
135498   return c;
135499 }
135500 
135501 /*
135502 ** This function is used to allocate an Fts3SegReader that iterates through
135503 ** a subset of the terms stored in the Fts3Table.pendingTerms array.
135504 **
135505 ** If the isPrefixIter parameter is zero, then the returned SegReader iterates
135506 ** through each term in the pending-terms table. Or, if isPrefixIter is
135507 ** non-zero, it iterates through each term and its prefixes. For example, if
135508 ** the pending terms hash table contains the terms "sqlite", "mysql" and
135509 ** "firebird", then the iterator visits the following 'terms' (in the order
135510 ** shown):
135511 **
135512 **   f fi fir fire fireb firebi firebir firebird
135513 **   m my mys mysq mysql
135514 **   s sq sql sqli sqlit sqlite
135515 **
135516 ** Whereas if isPrefixIter is zero, the terms visited are:
135517 **
135518 **   firebird mysql sqlite
135519 */
135520 SQLITE_PRIVATE int sqlite3Fts3SegReaderPending(
135521   Fts3Table *p,                   /* Virtual table handle */
135522   int iIndex,                     /* Index for p->aIndex */
135523   const char *zTerm,              /* Term to search for */
135524   int nTerm,                      /* Size of buffer zTerm */
135525   int bPrefix,                    /* True for a prefix iterator */
135526   Fts3SegReader **ppReader        /* OUT: SegReader for pending-terms */
135527 ){
135528   Fts3SegReader *pReader = 0;     /* Fts3SegReader object to return */
135529   Fts3HashElem *pE;               /* Iterator variable */
135530   Fts3HashElem **aElem = 0;       /* Array of term hash entries to scan */
135531   int nElem = 0;                  /* Size of array at aElem */
135532   int rc = SQLITE_OK;             /* Return Code */
135533   Fts3Hash *pHash;
135534 
135535   pHash = &p->aIndex[iIndex].hPending;
135536   if( bPrefix ){
135537     int nAlloc = 0;               /* Size of allocated array at aElem */
135538 
135539     for(pE=fts3HashFirst(pHash); pE; pE=fts3HashNext(pE)){
135540       char *zKey = (char *)fts3HashKey(pE);
135541       int nKey = fts3HashKeysize(pE);
135542       if( nTerm==0 || (nKey>=nTerm && 0==memcmp(zKey, zTerm, nTerm)) ){
135543         if( nElem==nAlloc ){
135544           Fts3HashElem **aElem2;
135545           nAlloc += 16;
135546           aElem2 = (Fts3HashElem **)sqlite3_realloc(
135547               aElem, nAlloc*sizeof(Fts3HashElem *)
135548           );
135549           if( !aElem2 ){
135550             rc = SQLITE_NOMEM;
135551             nElem = 0;
135552             break;
135553           }
135554           aElem = aElem2;
135555         }
135556 
135557         aElem[nElem++] = pE;
135558       }
135559     }
135560 
135561     /* If more than one term matches the prefix, sort the Fts3HashElem
135562     ** objects in term order using qsort(). This uses the same comparison
135563     ** callback as is used when flushing terms to disk.
135564     */
135565     if( nElem>1 ){
135566       qsort(aElem, nElem, sizeof(Fts3HashElem *), fts3CompareElemByTerm);
135567     }
135568 
135569   }else{
135570     /* The query is a simple term lookup that matches at most one term in
135571     ** the index. All that is required is a straight hash-lookup.
135572     **
135573     ** Because the stack address of pE may be accessed via the aElem pointer
135574     ** below, the "Fts3HashElem *pE" must be declared so that it is valid
135575     ** within this entire function, not just this "else{...}" block.
135576     */
135577     pE = fts3HashFindElem(pHash, zTerm, nTerm);
135578     if( pE ){
135579       aElem = &pE;
135580       nElem = 1;
135581     }
135582   }
135583 
135584   if( nElem>0 ){
135585     int nByte = sizeof(Fts3SegReader) + (nElem+1)*sizeof(Fts3HashElem *);
135586     pReader = (Fts3SegReader *)sqlite3_malloc(nByte);
135587     if( !pReader ){
135588       rc = SQLITE_NOMEM;
135589     }else{
135590       memset(pReader, 0, nByte);
135591       pReader->iIdx = 0x7FFFFFFF;
135592       pReader->ppNextElem = (Fts3HashElem **)&pReader[1];
135593       memcpy(pReader->ppNextElem, aElem, nElem*sizeof(Fts3HashElem *));
135594     }
135595   }
135596 
135597   if( bPrefix ){
135598     sqlite3_free(aElem);
135599   }
135600   *ppReader = pReader;
135601   return rc;
135602 }
135603 
135604 /*
135605 ** Compare the entries pointed to by two Fts3SegReader structures.
135606 ** Comparison is as follows:
135607 **
135608 **   1) EOF is greater than not EOF.
135609 **
135610 **   2) The current terms (if any) are compared using memcmp(). If one
135611 **      term is a prefix of another, the longer term is considered the
135612 **      larger.
135613 **
135614 **   3) By segment age. An older segment is considered larger.
135615 */
135616 static int fts3SegReaderCmp(Fts3SegReader *pLhs, Fts3SegReader *pRhs){
135617   int rc;
135618   if( pLhs->aNode && pRhs->aNode ){
135619     int rc2 = pLhs->nTerm - pRhs->nTerm;
135620     if( rc2<0 ){
135621       rc = memcmp(pLhs->zTerm, pRhs->zTerm, pLhs->nTerm);
135622     }else{
135623       rc = memcmp(pLhs->zTerm, pRhs->zTerm, pRhs->nTerm);
135624     }
135625     if( rc==0 ){
135626       rc = rc2;
135627     }
135628   }else{
135629     rc = (pLhs->aNode==0) - (pRhs->aNode==0);
135630   }
135631   if( rc==0 ){
135632     rc = pRhs->iIdx - pLhs->iIdx;
135633   }
135634   assert( rc!=0 );
135635   return rc;
135636 }
135637 
135638 /*
135639 ** A different comparison function for SegReader structures. In this
135640 ** version, it is assumed that each SegReader points to an entry in
135641 ** a doclist for identical terms. Comparison is made as follows:
135642 **
135643 **   1) EOF (end of doclist in this case) is greater than not EOF.
135644 **
135645 **   2) By current docid.
135646 **
135647 **   3) By segment age. An older segment is considered larger.
135648 */
135649 static int fts3SegReaderDoclistCmp(Fts3SegReader *pLhs, Fts3SegReader *pRhs){
135650   int rc = (pLhs->pOffsetList==0)-(pRhs->pOffsetList==0);
135651   if( rc==0 ){
135652     if( pLhs->iDocid==pRhs->iDocid ){
135653       rc = pRhs->iIdx - pLhs->iIdx;
135654     }else{
135655       rc = (pLhs->iDocid > pRhs->iDocid) ? 1 : -1;
135656     }
135657   }
135658   assert( pLhs->aNode && pRhs->aNode );
135659   return rc;
135660 }
135661 static int fts3SegReaderDoclistCmpRev(Fts3SegReader *pLhs, Fts3SegReader *pRhs){
135662   int rc = (pLhs->pOffsetList==0)-(pRhs->pOffsetList==0);
135663   if( rc==0 ){
135664     if( pLhs->iDocid==pRhs->iDocid ){
135665       rc = pRhs->iIdx - pLhs->iIdx;
135666     }else{
135667       rc = (pLhs->iDocid < pRhs->iDocid) ? 1 : -1;
135668     }
135669   }
135670   assert( pLhs->aNode && pRhs->aNode );
135671   return rc;
135672 }
135673 
135674 /*
135675 ** Compare the term that the Fts3SegReader object passed as the first argument
135676 ** points to with the term specified by arguments zTerm and nTerm.
135677 **
135678 ** If the pSeg iterator is already at EOF, return 0. Otherwise, return
135679 ** -ve if the pSeg term is less than zTerm/nTerm, 0 if the two terms are
135680 ** equal, or +ve if the pSeg term is greater than zTerm/nTerm.
135681 */
135682 static int fts3SegReaderTermCmp(
135683   Fts3SegReader *pSeg,            /* Segment reader object */
135684   const char *zTerm,              /* Term to compare to */
135685   int nTerm                       /* Size of term zTerm in bytes */
135686 ){
135687   int res = 0;
135688   if( pSeg->aNode ){
135689     if( pSeg->nTerm>nTerm ){
135690       res = memcmp(pSeg->zTerm, zTerm, nTerm);
135691     }else{
135692       res = memcmp(pSeg->zTerm, zTerm, pSeg->nTerm);
135693     }
135694     if( res==0 ){
135695       res = pSeg->nTerm-nTerm;
135696     }
135697   }
135698   return res;
135699 }
135700 
135701 /*
135702 ** Argument apSegment is an array of nSegment elements. It is known that
135703 ** the final (nSegment-nSuspect) members are already in sorted order
135704 ** (according to the comparison function provided). This function shuffles
135705 ** the array around until all entries are in sorted order.
135706 */
135707 static void fts3SegReaderSort(
135708   Fts3SegReader **apSegment,                     /* Array to sort entries of */
135709   int nSegment,                                  /* Size of apSegment array */
135710   int nSuspect,                                  /* Unsorted entry count */
135711   int (*xCmp)(Fts3SegReader *, Fts3SegReader *)  /* Comparison function */
135712 ){
135713   int i;                          /* Iterator variable */
135714 
135715   assert( nSuspect<=nSegment );
135716 
135717   if( nSuspect==nSegment ) nSuspect--;
135718   for(i=nSuspect-1; i>=0; i--){
135719     int j;
135720     for(j=i; j<(nSegment-1); j++){
135721       Fts3SegReader *pTmp;
135722       if( xCmp(apSegment[j], apSegment[j+1])<0 ) break;
135723       pTmp = apSegment[j+1];
135724       apSegment[j+1] = apSegment[j];
135725       apSegment[j] = pTmp;
135726     }
135727   }
135728 
135729 #ifndef NDEBUG
135730   /* Check that the list really is sorted now. */
135731   for(i=0; i<(nSuspect-1); i++){
135732     assert( xCmp(apSegment[i], apSegment[i+1])<0 );
135733   }
135734 #endif
135735 }
135736 
135737 /*
135738 ** Insert a record into the %_segments table.
135739 */
135740 static int fts3WriteSegment(
135741   Fts3Table *p,                   /* Virtual table handle */
135742   sqlite3_int64 iBlock,           /* Block id for new block */
135743   char *z,                        /* Pointer to buffer containing block data */
135744   int n                           /* Size of buffer z in bytes */
135745 ){
135746   sqlite3_stmt *pStmt;
135747   int rc = fts3SqlStmt(p, SQL_INSERT_SEGMENTS, &pStmt, 0);
135748   if( rc==SQLITE_OK ){
135749     sqlite3_bind_int64(pStmt, 1, iBlock);
135750     sqlite3_bind_blob(pStmt, 2, z, n, SQLITE_STATIC);
135751     sqlite3_step(pStmt);
135752     rc = sqlite3_reset(pStmt);
135753   }
135754   return rc;
135755 }
135756 
135757 /*
135758 ** Find the largest relative level number in the table. If successful, set
135759 ** *pnMax to this value and return SQLITE_OK. Otherwise, if an error occurs,
135760 ** set *pnMax to zero and return an SQLite error code.
135761 */
135762 SQLITE_PRIVATE int sqlite3Fts3MaxLevel(Fts3Table *p, int *pnMax){
135763   int rc;
135764   int mxLevel = 0;
135765   sqlite3_stmt *pStmt = 0;
135766 
135767   rc = fts3SqlStmt(p, SQL_SELECT_MXLEVEL, &pStmt, 0);
135768   if( rc==SQLITE_OK ){
135769     if( SQLITE_ROW==sqlite3_step(pStmt) ){
135770       mxLevel = sqlite3_column_int(pStmt, 0);
135771     }
135772     rc = sqlite3_reset(pStmt);
135773   }
135774   *pnMax = mxLevel;
135775   return rc;
135776 }
135777 
135778 /*
135779 ** Insert a record into the %_segdir table.
135780 */
135781 static int fts3WriteSegdir(
135782   Fts3Table *p,                   /* Virtual table handle */
135783   sqlite3_int64 iLevel,           /* Value for "level" field (absolute level) */
135784   int iIdx,                       /* Value for "idx" field */
135785   sqlite3_int64 iStartBlock,      /* Value for "start_block" field */
135786   sqlite3_int64 iLeafEndBlock,    /* Value for "leaves_end_block" field */
135787   sqlite3_int64 iEndBlock,        /* Value for "end_block" field */
135788   char *zRoot,                    /* Blob value for "root" field */
135789   int nRoot                       /* Number of bytes in buffer zRoot */
135790 ){
135791   sqlite3_stmt *pStmt;
135792   int rc = fts3SqlStmt(p, SQL_INSERT_SEGDIR, &pStmt, 0);
135793   if( rc==SQLITE_OK ){
135794     sqlite3_bind_int64(pStmt, 1, iLevel);
135795     sqlite3_bind_int(pStmt, 2, iIdx);
135796     sqlite3_bind_int64(pStmt, 3, iStartBlock);
135797     sqlite3_bind_int64(pStmt, 4, iLeafEndBlock);
135798     sqlite3_bind_int64(pStmt, 5, iEndBlock);
135799     sqlite3_bind_blob(pStmt, 6, zRoot, nRoot, SQLITE_STATIC);
135800     sqlite3_step(pStmt);
135801     rc = sqlite3_reset(pStmt);
135802   }
135803   return rc;
135804 }
135805 
135806 /*
135807 ** Return the size of the common prefix (if any) shared by zPrev and
135808 ** zNext, in bytes. For example,
135809 **
135810 **   fts3PrefixCompress("abc", 3, "abcdef", 6)   // returns 3
135811 **   fts3PrefixCompress("abX", 3, "abcdef", 6)   // returns 2
135812 **   fts3PrefixCompress("abX", 3, "Xbcdef", 6)   // returns 0
135813 */
135814 static int fts3PrefixCompress(
135815   const char *zPrev,              /* Buffer containing previous term */
135816   int nPrev,                      /* Size of buffer zPrev in bytes */
135817   const char *zNext,              /* Buffer containing next term */
135818   int nNext                       /* Size of buffer zNext in bytes */
135819 ){
135820   int n;
135821   UNUSED_PARAMETER(nNext);
135822   for(n=0; n<nPrev && zPrev[n]==zNext[n]; n++);
135823   return n;
135824 }
135825 
135826 /*
135827 ** Add term zTerm to the SegmentNode. It is guaranteed that zTerm is larger
135828 ** (according to memcmp) than the previous term.
135829 */
135830 static int fts3NodeAddTerm(
135831   Fts3Table *p,                   /* Virtual table handle */
135832   SegmentNode **ppTree,           /* IN/OUT: SegmentNode handle */
135833   int isCopyTerm,                 /* True if zTerm/nTerm is transient */
135834   const char *zTerm,              /* Pointer to buffer containing term */
135835   int nTerm                       /* Size of term in bytes */
135836 ){
135837   SegmentNode *pTree = *ppTree;
135838   int rc;
135839   SegmentNode *pNew;
135840 
135841   /* First try to append the term to the current node. Return early if
135842   ** this is possible.
135843   */
135844   if( pTree ){
135845     int nData = pTree->nData;     /* Current size of node in bytes */
135846     int nReq = nData;             /* Required space after adding zTerm */
135847     int nPrefix;                  /* Number of bytes of prefix compression */
135848     int nSuffix;                  /* Suffix length */
135849 
135850     nPrefix = fts3PrefixCompress(pTree->zTerm, pTree->nTerm, zTerm, nTerm);
135851     nSuffix = nTerm-nPrefix;
135852 
135853     nReq += sqlite3Fts3VarintLen(nPrefix)+sqlite3Fts3VarintLen(nSuffix)+nSuffix;
135854     if( nReq<=p->nNodeSize || !pTree->zTerm ){
135855 
135856       if( nReq>p->nNodeSize ){
135857         /* An unusual case: this is the first term to be added to the node
135858         ** and the static node buffer (p->nNodeSize bytes) is not large
135859         ** enough. Use a separately malloced buffer instead This wastes
135860         ** p->nNodeSize bytes, but since this scenario only comes about when
135861         ** the database contain two terms that share a prefix of almost 2KB,
135862         ** this is not expected to be a serious problem.
135863         */
135864         assert( pTree->aData==(char *)&pTree[1] );
135865         pTree->aData = (char *)sqlite3_malloc(nReq);
135866         if( !pTree->aData ){
135867           return SQLITE_NOMEM;
135868         }
135869       }
135870 
135871       if( pTree->zTerm ){
135872         /* There is no prefix-length field for first term in a node */
135873         nData += sqlite3Fts3PutVarint(&pTree->aData[nData], nPrefix);
135874       }
135875 
135876       nData += sqlite3Fts3PutVarint(&pTree->aData[nData], nSuffix);
135877       memcpy(&pTree->aData[nData], &zTerm[nPrefix], nSuffix);
135878       pTree->nData = nData + nSuffix;
135879       pTree->nEntry++;
135880 
135881       if( isCopyTerm ){
135882         if( pTree->nMalloc<nTerm ){
135883           char *zNew = sqlite3_realloc(pTree->zMalloc, nTerm*2);
135884           if( !zNew ){
135885             return SQLITE_NOMEM;
135886           }
135887           pTree->nMalloc = nTerm*2;
135888           pTree->zMalloc = zNew;
135889         }
135890         pTree->zTerm = pTree->zMalloc;
135891         memcpy(pTree->zTerm, zTerm, nTerm);
135892         pTree->nTerm = nTerm;
135893       }else{
135894         pTree->zTerm = (char *)zTerm;
135895         pTree->nTerm = nTerm;
135896       }
135897       return SQLITE_OK;
135898     }
135899   }
135900 
135901   /* If control flows to here, it was not possible to append zTerm to the
135902   ** current node. Create a new node (a right-sibling of the current node).
135903   ** If this is the first node in the tree, the term is added to it.
135904   **
135905   ** Otherwise, the term is not added to the new node, it is left empty for
135906   ** now. Instead, the term is inserted into the parent of pTree. If pTree
135907   ** has no parent, one is created here.
135908   */
135909   pNew = (SegmentNode *)sqlite3_malloc(sizeof(SegmentNode) + p->nNodeSize);
135910   if( !pNew ){
135911     return SQLITE_NOMEM;
135912   }
135913   memset(pNew, 0, sizeof(SegmentNode));
135914   pNew->nData = 1 + FTS3_VARINT_MAX;
135915   pNew->aData = (char *)&pNew[1];
135916 
135917   if( pTree ){
135918     SegmentNode *pParent = pTree->pParent;
135919     rc = fts3NodeAddTerm(p, &pParent, isCopyTerm, zTerm, nTerm);
135920     if( pTree->pParent==0 ){
135921       pTree->pParent = pParent;
135922     }
135923     pTree->pRight = pNew;
135924     pNew->pLeftmost = pTree->pLeftmost;
135925     pNew->pParent = pParent;
135926     pNew->zMalloc = pTree->zMalloc;
135927     pNew->nMalloc = pTree->nMalloc;
135928     pTree->zMalloc = 0;
135929   }else{
135930     pNew->pLeftmost = pNew;
135931     rc = fts3NodeAddTerm(p, &pNew, isCopyTerm, zTerm, nTerm);
135932   }
135933 
135934   *ppTree = pNew;
135935   return rc;
135936 }
135937 
135938 /*
135939 ** Helper function for fts3NodeWrite().
135940 */
135941 static int fts3TreeFinishNode(
135942   SegmentNode *pTree,
135943   int iHeight,
135944   sqlite3_int64 iLeftChild
135945 ){
135946   int nStart;
135947   assert( iHeight>=1 && iHeight<128 );
135948   nStart = FTS3_VARINT_MAX - sqlite3Fts3VarintLen(iLeftChild);
135949   pTree->aData[nStart] = (char)iHeight;
135950   sqlite3Fts3PutVarint(&pTree->aData[nStart+1], iLeftChild);
135951   return nStart;
135952 }
135953 
135954 /*
135955 ** Write the buffer for the segment node pTree and all of its peers to the
135956 ** database. Then call this function recursively to write the parent of
135957 ** pTree and its peers to the database.
135958 **
135959 ** Except, if pTree is a root node, do not write it to the database. Instead,
135960 ** set output variables *paRoot and *pnRoot to contain the root node.
135961 **
135962 ** If successful, SQLITE_OK is returned and output variable *piLast is
135963 ** set to the largest blockid written to the database (or zero if no
135964 ** blocks were written to the db). Otherwise, an SQLite error code is
135965 ** returned.
135966 */
135967 static int fts3NodeWrite(
135968   Fts3Table *p,                   /* Virtual table handle */
135969   SegmentNode *pTree,             /* SegmentNode handle */
135970   int iHeight,                    /* Height of this node in tree */
135971   sqlite3_int64 iLeaf,            /* Block id of first leaf node */
135972   sqlite3_int64 iFree,            /* Block id of next free slot in %_segments */
135973   sqlite3_int64 *piLast,          /* OUT: Block id of last entry written */
135974   char **paRoot,                  /* OUT: Data for root node */
135975   int *pnRoot                     /* OUT: Size of root node in bytes */
135976 ){
135977   int rc = SQLITE_OK;
135978 
135979   if( !pTree->pParent ){
135980     /* Root node of the tree. */
135981     int nStart = fts3TreeFinishNode(pTree, iHeight, iLeaf);
135982     *piLast = iFree-1;
135983     *pnRoot = pTree->nData - nStart;
135984     *paRoot = &pTree->aData[nStart];
135985   }else{
135986     SegmentNode *pIter;
135987     sqlite3_int64 iNextFree = iFree;
135988     sqlite3_int64 iNextLeaf = iLeaf;
135989     for(pIter=pTree->pLeftmost; pIter && rc==SQLITE_OK; pIter=pIter->pRight){
135990       int nStart = fts3TreeFinishNode(pIter, iHeight, iNextLeaf);
135991       int nWrite = pIter->nData - nStart;
135992 
135993       rc = fts3WriteSegment(p, iNextFree, &pIter->aData[nStart], nWrite);
135994       iNextFree++;
135995       iNextLeaf += (pIter->nEntry+1);
135996     }
135997     if( rc==SQLITE_OK ){
135998       assert( iNextLeaf==iFree );
135999       rc = fts3NodeWrite(
136000           p, pTree->pParent, iHeight+1, iFree, iNextFree, piLast, paRoot, pnRoot
136001       );
136002     }
136003   }
136004 
136005   return rc;
136006 }
136007 
136008 /*
136009 ** Free all memory allocations associated with the tree pTree.
136010 */
136011 static void fts3NodeFree(SegmentNode *pTree){
136012   if( pTree ){
136013     SegmentNode *p = pTree->pLeftmost;
136014     fts3NodeFree(p->pParent);
136015     while( p ){
136016       SegmentNode *pRight = p->pRight;
136017       if( p->aData!=(char *)&p[1] ){
136018         sqlite3_free(p->aData);
136019       }
136020       assert( pRight==0 || p->zMalloc==0 );
136021       sqlite3_free(p->zMalloc);
136022       sqlite3_free(p);
136023       p = pRight;
136024     }
136025   }
136026 }
136027 
136028 /*
136029 ** Add a term to the segment being constructed by the SegmentWriter object
136030 ** *ppWriter. When adding the first term to a segment, *ppWriter should
136031 ** be passed NULL. This function will allocate a new SegmentWriter object
136032 ** and return it via the input/output variable *ppWriter in this case.
136033 **
136034 ** If successful, SQLITE_OK is returned. Otherwise, an SQLite error code.
136035 */
136036 static int fts3SegWriterAdd(
136037   Fts3Table *p,                   /* Virtual table handle */
136038   SegmentWriter **ppWriter,       /* IN/OUT: SegmentWriter handle */
136039   int isCopyTerm,                 /* True if buffer zTerm must be copied */
136040   const char *zTerm,              /* Pointer to buffer containing term */
136041   int nTerm,                      /* Size of term in bytes */
136042   const char *aDoclist,           /* Pointer to buffer containing doclist */
136043   int nDoclist                    /* Size of doclist in bytes */
136044 ){
136045   int nPrefix;                    /* Size of term prefix in bytes */
136046   int nSuffix;                    /* Size of term suffix in bytes */
136047   int nReq;                       /* Number of bytes required on leaf page */
136048   int nData;
136049   SegmentWriter *pWriter = *ppWriter;
136050 
136051   if( !pWriter ){
136052     int rc;
136053     sqlite3_stmt *pStmt;
136054 
136055     /* Allocate the SegmentWriter structure */
136056     pWriter = (SegmentWriter *)sqlite3_malloc(sizeof(SegmentWriter));
136057     if( !pWriter ) return SQLITE_NOMEM;
136058     memset(pWriter, 0, sizeof(SegmentWriter));
136059     *ppWriter = pWriter;
136060 
136061     /* Allocate a buffer in which to accumulate data */
136062     pWriter->aData = (char *)sqlite3_malloc(p->nNodeSize);
136063     if( !pWriter->aData ) return SQLITE_NOMEM;
136064     pWriter->nSize = p->nNodeSize;
136065 
136066     /* Find the next free blockid in the %_segments table */
136067     rc = fts3SqlStmt(p, SQL_NEXT_SEGMENTS_ID, &pStmt, 0);
136068     if( rc!=SQLITE_OK ) return rc;
136069     if( SQLITE_ROW==sqlite3_step(pStmt) ){
136070       pWriter->iFree = sqlite3_column_int64(pStmt, 0);
136071       pWriter->iFirst = pWriter->iFree;
136072     }
136073     rc = sqlite3_reset(pStmt);
136074     if( rc!=SQLITE_OK ) return rc;
136075   }
136076   nData = pWriter->nData;
136077 
136078   nPrefix = fts3PrefixCompress(pWriter->zTerm, pWriter->nTerm, zTerm, nTerm);
136079   nSuffix = nTerm-nPrefix;
136080 
136081   /* Figure out how many bytes are required by this new entry */
136082   nReq = sqlite3Fts3VarintLen(nPrefix) +    /* varint containing prefix size */
136083     sqlite3Fts3VarintLen(nSuffix) +         /* varint containing suffix size */
136084     nSuffix +                               /* Term suffix */
136085     sqlite3Fts3VarintLen(nDoclist) +        /* Size of doclist */
136086     nDoclist;                               /* Doclist data */
136087 
136088   if( nData>0 && nData+nReq>p->nNodeSize ){
136089     int rc;
136090 
136091     /* The current leaf node is full. Write it out to the database. */
136092     rc = fts3WriteSegment(p, pWriter->iFree++, pWriter->aData, nData);
136093     if( rc!=SQLITE_OK ) return rc;
136094     p->nLeafAdd++;
136095 
136096     /* Add the current term to the interior node tree. The term added to
136097     ** the interior tree must:
136098     **
136099     **   a) be greater than the largest term on the leaf node just written
136100     **      to the database (still available in pWriter->zTerm), and
136101     **
136102     **   b) be less than or equal to the term about to be added to the new
136103     **      leaf node (zTerm/nTerm).
136104     **
136105     ** In other words, it must be the prefix of zTerm 1 byte longer than
136106     ** the common prefix (if any) of zTerm and pWriter->zTerm.
136107     */
136108     assert( nPrefix<nTerm );
136109     rc = fts3NodeAddTerm(p, &pWriter->pTree, isCopyTerm, zTerm, nPrefix+1);
136110     if( rc!=SQLITE_OK ) return rc;
136111 
136112     nData = 0;
136113     pWriter->nTerm = 0;
136114 
136115     nPrefix = 0;
136116     nSuffix = nTerm;
136117     nReq = 1 +                              /* varint containing prefix size */
136118       sqlite3Fts3VarintLen(nTerm) +         /* varint containing suffix size */
136119       nTerm +                               /* Term suffix */
136120       sqlite3Fts3VarintLen(nDoclist) +      /* Size of doclist */
136121       nDoclist;                             /* Doclist data */
136122   }
136123 
136124   /* If the buffer currently allocated is too small for this entry, realloc
136125   ** the buffer to make it large enough.
136126   */
136127   if( nReq>pWriter->nSize ){
136128     char *aNew = sqlite3_realloc(pWriter->aData, nReq);
136129     if( !aNew ) return SQLITE_NOMEM;
136130     pWriter->aData = aNew;
136131     pWriter->nSize = nReq;
136132   }
136133   assert( nData+nReq<=pWriter->nSize );
136134 
136135   /* Append the prefix-compressed term and doclist to the buffer. */
136136   nData += sqlite3Fts3PutVarint(&pWriter->aData[nData], nPrefix);
136137   nData += sqlite3Fts3PutVarint(&pWriter->aData[nData], nSuffix);
136138   memcpy(&pWriter->aData[nData], &zTerm[nPrefix], nSuffix);
136139   nData += nSuffix;
136140   nData += sqlite3Fts3PutVarint(&pWriter->aData[nData], nDoclist);
136141   memcpy(&pWriter->aData[nData], aDoclist, nDoclist);
136142   pWriter->nData = nData + nDoclist;
136143 
136144   /* Save the current term so that it can be used to prefix-compress the next.
136145   ** If the isCopyTerm parameter is true, then the buffer pointed to by
136146   ** zTerm is transient, so take a copy of the term data. Otherwise, just
136147   ** store a copy of the pointer.
136148   */
136149   if( isCopyTerm ){
136150     if( nTerm>pWriter->nMalloc ){
136151       char *zNew = sqlite3_realloc(pWriter->zMalloc, nTerm*2);
136152       if( !zNew ){
136153         return SQLITE_NOMEM;
136154       }
136155       pWriter->nMalloc = nTerm*2;
136156       pWriter->zMalloc = zNew;
136157       pWriter->zTerm = zNew;
136158     }
136159     assert( pWriter->zTerm==pWriter->zMalloc );
136160     memcpy(pWriter->zTerm, zTerm, nTerm);
136161   }else{
136162     pWriter->zTerm = (char *)zTerm;
136163   }
136164   pWriter->nTerm = nTerm;
136165 
136166   return SQLITE_OK;
136167 }
136168 
136169 /*
136170 ** Flush all data associated with the SegmentWriter object pWriter to the
136171 ** database. This function must be called after all terms have been added
136172 ** to the segment using fts3SegWriterAdd(). If successful, SQLITE_OK is
136173 ** returned. Otherwise, an SQLite error code.
136174 */
136175 static int fts3SegWriterFlush(
136176   Fts3Table *p,                   /* Virtual table handle */
136177   SegmentWriter *pWriter,         /* SegmentWriter to flush to the db */
136178   sqlite3_int64 iLevel,           /* Value for 'level' column of %_segdir */
136179   int iIdx                        /* Value for 'idx' column of %_segdir */
136180 ){
136181   int rc;                         /* Return code */
136182   if( pWriter->pTree ){
136183     sqlite3_int64 iLast = 0;      /* Largest block id written to database */
136184     sqlite3_int64 iLastLeaf;      /* Largest leaf block id written to db */
136185     char *zRoot = NULL;           /* Pointer to buffer containing root node */
136186     int nRoot = 0;                /* Size of buffer zRoot */
136187 
136188     iLastLeaf = pWriter->iFree;
136189     rc = fts3WriteSegment(p, pWriter->iFree++, pWriter->aData, pWriter->nData);
136190     if( rc==SQLITE_OK ){
136191       rc = fts3NodeWrite(p, pWriter->pTree, 1,
136192           pWriter->iFirst, pWriter->iFree, &iLast, &zRoot, &nRoot);
136193     }
136194     if( rc==SQLITE_OK ){
136195       rc = fts3WriteSegdir(
136196           p, iLevel, iIdx, pWriter->iFirst, iLastLeaf, iLast, zRoot, nRoot);
136197     }
136198   }else{
136199     /* The entire tree fits on the root node. Write it to the segdir table. */
136200     rc = fts3WriteSegdir(
136201         p, iLevel, iIdx, 0, 0, 0, pWriter->aData, pWriter->nData);
136202   }
136203   p->nLeafAdd++;
136204   return rc;
136205 }
136206 
136207 /*
136208 ** Release all memory held by the SegmentWriter object passed as the
136209 ** first argument.
136210 */
136211 static void fts3SegWriterFree(SegmentWriter *pWriter){
136212   if( pWriter ){
136213     sqlite3_free(pWriter->aData);
136214     sqlite3_free(pWriter->zMalloc);
136215     fts3NodeFree(pWriter->pTree);
136216     sqlite3_free(pWriter);
136217   }
136218 }
136219 
136220 /*
136221 ** The first value in the apVal[] array is assumed to contain an integer.
136222 ** This function tests if there exist any documents with docid values that
136223 ** are different from that integer. i.e. if deleting the document with docid
136224 ** pRowid would mean the FTS3 table were empty.
136225 **
136226 ** If successful, *pisEmpty is set to true if the table is empty except for
136227 ** document pRowid, or false otherwise, and SQLITE_OK is returned. If an
136228 ** error occurs, an SQLite error code is returned.
136229 */
136230 static int fts3IsEmpty(Fts3Table *p, sqlite3_value *pRowid, int *pisEmpty){
136231   sqlite3_stmt *pStmt;
136232   int rc;
136233   if( p->zContentTbl ){
136234     /* If using the content=xxx option, assume the table is never empty */
136235     *pisEmpty = 0;
136236     rc = SQLITE_OK;
136237   }else{
136238     rc = fts3SqlStmt(p, SQL_IS_EMPTY, &pStmt, &pRowid);
136239     if( rc==SQLITE_OK ){
136240       if( SQLITE_ROW==sqlite3_step(pStmt) ){
136241         *pisEmpty = sqlite3_column_int(pStmt, 0);
136242       }
136243       rc = sqlite3_reset(pStmt);
136244     }
136245   }
136246   return rc;
136247 }
136248 
136249 /*
136250 ** Set *pnMax to the largest segment level in the database for the index
136251 ** iIndex.
136252 **
136253 ** Segment levels are stored in the 'level' column of the %_segdir table.
136254 **
136255 ** Return SQLITE_OK if successful, or an SQLite error code if not.
136256 */
136257 static int fts3SegmentMaxLevel(
136258   Fts3Table *p,
136259   int iLangid,
136260   int iIndex,
136261   sqlite3_int64 *pnMax
136262 ){
136263   sqlite3_stmt *pStmt;
136264   int rc;
136265   assert( iIndex>=0 && iIndex<p->nIndex );
136266 
136267   /* Set pStmt to the compiled version of:
136268   **
136269   **   SELECT max(level) FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?
136270   **
136271   ** (1024 is actually the value of macro FTS3_SEGDIR_PREFIXLEVEL_STR).
136272   */
136273   rc = fts3SqlStmt(p, SQL_SELECT_SEGDIR_MAX_LEVEL, &pStmt, 0);
136274   if( rc!=SQLITE_OK ) return rc;
136275   sqlite3_bind_int64(pStmt, 1, getAbsoluteLevel(p, iLangid, iIndex, 0));
136276   sqlite3_bind_int64(pStmt, 2,
136277       getAbsoluteLevel(p, iLangid, iIndex, FTS3_SEGDIR_MAXLEVEL-1)
136278   );
136279   if( SQLITE_ROW==sqlite3_step(pStmt) ){
136280     *pnMax = sqlite3_column_int64(pStmt, 0);
136281   }
136282   return sqlite3_reset(pStmt);
136283 }
136284 
136285 /*
136286 ** Delete all entries in the %_segments table associated with the segment
136287 ** opened with seg-reader pSeg. This function does not affect the contents
136288 ** of the %_segdir table.
136289 */
136290 static int fts3DeleteSegment(
136291   Fts3Table *p,                   /* FTS table handle */
136292   Fts3SegReader *pSeg             /* Segment to delete */
136293 ){
136294   int rc = SQLITE_OK;             /* Return code */
136295   if( pSeg->iStartBlock ){
136296     sqlite3_stmt *pDelete;        /* SQL statement to delete rows */
136297     rc = fts3SqlStmt(p, SQL_DELETE_SEGMENTS_RANGE, &pDelete, 0);
136298     if( rc==SQLITE_OK ){
136299       sqlite3_bind_int64(pDelete, 1, pSeg->iStartBlock);
136300       sqlite3_bind_int64(pDelete, 2, pSeg->iEndBlock);
136301       sqlite3_step(pDelete);
136302       rc = sqlite3_reset(pDelete);
136303     }
136304   }
136305   return rc;
136306 }
136307 
136308 /*
136309 ** This function is used after merging multiple segments into a single large
136310 ** segment to delete the old, now redundant, segment b-trees. Specifically,
136311 ** it:
136312 **
136313 **   1) Deletes all %_segments entries for the segments associated with
136314 **      each of the SegReader objects in the array passed as the third
136315 **      argument, and
136316 **
136317 **   2) deletes all %_segdir entries with level iLevel, or all %_segdir
136318 **      entries regardless of level if (iLevel<0).
136319 **
136320 ** SQLITE_OK is returned if successful, otherwise an SQLite error code.
136321 */
136322 static int fts3DeleteSegdir(
136323   Fts3Table *p,                   /* Virtual table handle */
136324   int iLangid,                    /* Language id */
136325   int iIndex,                     /* Index for p->aIndex */
136326   int iLevel,                     /* Level of %_segdir entries to delete */
136327   Fts3SegReader **apSegment,      /* Array of SegReader objects */
136328   int nReader                     /* Size of array apSegment */
136329 ){
136330   int rc = SQLITE_OK;             /* Return Code */
136331   int i;                          /* Iterator variable */
136332   sqlite3_stmt *pDelete = 0;      /* SQL statement to delete rows */
136333 
136334   for(i=0; rc==SQLITE_OK && i<nReader; i++){
136335     rc = fts3DeleteSegment(p, apSegment[i]);
136336   }
136337   if( rc!=SQLITE_OK ){
136338     return rc;
136339   }
136340 
136341   assert( iLevel>=0 || iLevel==FTS3_SEGCURSOR_ALL );
136342   if( iLevel==FTS3_SEGCURSOR_ALL ){
136343     rc = fts3SqlStmt(p, SQL_DELETE_SEGDIR_RANGE, &pDelete, 0);
136344     if( rc==SQLITE_OK ){
136345       sqlite3_bind_int64(pDelete, 1, getAbsoluteLevel(p, iLangid, iIndex, 0));
136346       sqlite3_bind_int64(pDelete, 2,
136347           getAbsoluteLevel(p, iLangid, iIndex, FTS3_SEGDIR_MAXLEVEL-1)
136348       );
136349     }
136350   }else{
136351     rc = fts3SqlStmt(p, SQL_DELETE_SEGDIR_LEVEL, &pDelete, 0);
136352     if( rc==SQLITE_OK ){
136353       sqlite3_bind_int64(
136354           pDelete, 1, getAbsoluteLevel(p, iLangid, iIndex, iLevel)
136355       );
136356     }
136357   }
136358 
136359   if( rc==SQLITE_OK ){
136360     sqlite3_step(pDelete);
136361     rc = sqlite3_reset(pDelete);
136362   }
136363 
136364   return rc;
136365 }
136366 
136367 /*
136368 ** When this function is called, buffer *ppList (size *pnList bytes) contains
136369 ** a position list that may (or may not) feature multiple columns. This
136370 ** function adjusts the pointer *ppList and the length *pnList so that they
136371 ** identify the subset of the position list that corresponds to column iCol.
136372 **
136373 ** If there are no entries in the input position list for column iCol, then
136374 ** *pnList is set to zero before returning.
136375 **
136376 ** If parameter bZero is non-zero, then any part of the input list following
136377 ** the end of the output list is zeroed before returning.
136378 */
136379 static void fts3ColumnFilter(
136380   int iCol,                       /* Column to filter on */
136381   int bZero,                      /* Zero out anything following *ppList */
136382   char **ppList,                  /* IN/OUT: Pointer to position list */
136383   int *pnList                     /* IN/OUT: Size of buffer *ppList in bytes */
136384 ){
136385   char *pList = *ppList;
136386   int nList = *pnList;
136387   char *pEnd = &pList[nList];
136388   int iCurrent = 0;
136389   char *p = pList;
136390 
136391   assert( iCol>=0 );
136392   while( 1 ){
136393     char c = 0;
136394     while( p<pEnd && (c | *p)&0xFE ) c = *p++ & 0x80;
136395 
136396     if( iCol==iCurrent ){
136397       nList = (int)(p - pList);
136398       break;
136399     }
136400 
136401     nList -= (int)(p - pList);
136402     pList = p;
136403     if( nList==0 ){
136404       break;
136405     }
136406     p = &pList[1];
136407     p += fts3GetVarint32(p, &iCurrent);
136408   }
136409 
136410   if( bZero && &pList[nList]!=pEnd ){
136411     memset(&pList[nList], 0, pEnd - &pList[nList]);
136412   }
136413   *ppList = pList;
136414   *pnList = nList;
136415 }
136416 
136417 /*
136418 ** Cache data in the Fts3MultiSegReader.aBuffer[] buffer (overwriting any
136419 ** existing data). Grow the buffer if required.
136420 **
136421 ** If successful, return SQLITE_OK. Otherwise, if an OOM error is encountered
136422 ** trying to resize the buffer, return SQLITE_NOMEM.
136423 */
136424 static int fts3MsrBufferData(
136425   Fts3MultiSegReader *pMsr,       /* Multi-segment-reader handle */
136426   char *pList,
136427   int nList
136428 ){
136429   if( nList>pMsr->nBuffer ){
136430     char *pNew;
136431     pMsr->nBuffer = nList*2;
136432     pNew = (char *)sqlite3_realloc(pMsr->aBuffer, pMsr->nBuffer);
136433     if( !pNew ) return SQLITE_NOMEM;
136434     pMsr->aBuffer = pNew;
136435   }
136436 
136437   memcpy(pMsr->aBuffer, pList, nList);
136438   return SQLITE_OK;
136439 }
136440 
136441 SQLITE_PRIVATE int sqlite3Fts3MsrIncrNext(
136442   Fts3Table *p,                   /* Virtual table handle */
136443   Fts3MultiSegReader *pMsr,       /* Multi-segment-reader handle */
136444   sqlite3_int64 *piDocid,         /* OUT: Docid value */
136445   char **paPoslist,               /* OUT: Pointer to position list */
136446   int *pnPoslist                  /* OUT: Size of position list in bytes */
136447 ){
136448   int nMerge = pMsr->nAdvance;
136449   Fts3SegReader **apSegment = pMsr->apSegment;
136450   int (*xCmp)(Fts3SegReader *, Fts3SegReader *) = (
136451     p->bDescIdx ? fts3SegReaderDoclistCmpRev : fts3SegReaderDoclistCmp
136452   );
136453 
136454   if( nMerge==0 ){
136455     *paPoslist = 0;
136456     return SQLITE_OK;
136457   }
136458 
136459   while( 1 ){
136460     Fts3SegReader *pSeg;
136461     pSeg = pMsr->apSegment[0];
136462 
136463     if( pSeg->pOffsetList==0 ){
136464       *paPoslist = 0;
136465       break;
136466     }else{
136467       int rc;
136468       char *pList;
136469       int nList;
136470       int j;
136471       sqlite3_int64 iDocid = apSegment[0]->iDocid;
136472 
136473       rc = fts3SegReaderNextDocid(p, apSegment[0], &pList, &nList);
136474       j = 1;
136475       while( rc==SQLITE_OK
136476         && j<nMerge
136477         && apSegment[j]->pOffsetList
136478         && apSegment[j]->iDocid==iDocid
136479       ){
136480         rc = fts3SegReaderNextDocid(p, apSegment[j], 0, 0);
136481         j++;
136482       }
136483       if( rc!=SQLITE_OK ) return rc;
136484       fts3SegReaderSort(pMsr->apSegment, nMerge, j, xCmp);
136485 
136486       if( nList>0 && fts3SegReaderIsPending(apSegment[0]) ){
136487         rc = fts3MsrBufferData(pMsr, pList, nList+1);
136488         if( rc!=SQLITE_OK ) return rc;
136489         assert( (pMsr->aBuffer[nList] & 0xFE)==0x00 );
136490         pList = pMsr->aBuffer;
136491       }
136492 
136493       if( pMsr->iColFilter>=0 ){
136494         fts3ColumnFilter(pMsr->iColFilter, 1, &pList, &nList);
136495       }
136496 
136497       if( nList>0 ){
136498         *paPoslist = pList;
136499         *piDocid = iDocid;
136500         *pnPoslist = nList;
136501         break;
136502       }
136503     }
136504   }
136505 
136506   return SQLITE_OK;
136507 }
136508 
136509 static int fts3SegReaderStart(
136510   Fts3Table *p,                   /* Virtual table handle */
136511   Fts3MultiSegReader *pCsr,       /* Cursor object */
136512   const char *zTerm,              /* Term searched for (or NULL) */
136513   int nTerm                       /* Length of zTerm in bytes */
136514 ){
136515   int i;
136516   int nSeg = pCsr->nSegment;
136517 
136518   /* If the Fts3SegFilter defines a specific term (or term prefix) to search
136519   ** for, then advance each segment iterator until it points to a term of
136520   ** equal or greater value than the specified term. This prevents many
136521   ** unnecessary merge/sort operations for the case where single segment
136522   ** b-tree leaf nodes contain more than one term.
136523   */
136524   for(i=0; pCsr->bRestart==0 && i<pCsr->nSegment; i++){
136525     int res = 0;
136526     Fts3SegReader *pSeg = pCsr->apSegment[i];
136527     do {
136528       int rc = fts3SegReaderNext(p, pSeg, 0);
136529       if( rc!=SQLITE_OK ) return rc;
136530     }while( zTerm && (res = fts3SegReaderTermCmp(pSeg, zTerm, nTerm))<0 );
136531 
136532     if( pSeg->bLookup && res!=0 ){
136533       fts3SegReaderSetEof(pSeg);
136534     }
136535   }
136536   fts3SegReaderSort(pCsr->apSegment, nSeg, nSeg, fts3SegReaderCmp);
136537 
136538   return SQLITE_OK;
136539 }
136540 
136541 SQLITE_PRIVATE int sqlite3Fts3SegReaderStart(
136542   Fts3Table *p,                   /* Virtual table handle */
136543   Fts3MultiSegReader *pCsr,       /* Cursor object */
136544   Fts3SegFilter *pFilter          /* Restrictions on range of iteration */
136545 ){
136546   pCsr->pFilter = pFilter;
136547   return fts3SegReaderStart(p, pCsr, pFilter->zTerm, pFilter->nTerm);
136548 }
136549 
136550 SQLITE_PRIVATE int sqlite3Fts3MsrIncrStart(
136551   Fts3Table *p,                   /* Virtual table handle */
136552   Fts3MultiSegReader *pCsr,       /* Cursor object */
136553   int iCol,                       /* Column to match on. */
136554   const char *zTerm,              /* Term to iterate through a doclist for */
136555   int nTerm                       /* Number of bytes in zTerm */
136556 ){
136557   int i;
136558   int rc;
136559   int nSegment = pCsr->nSegment;
136560   int (*xCmp)(Fts3SegReader *, Fts3SegReader *) = (
136561     p->bDescIdx ? fts3SegReaderDoclistCmpRev : fts3SegReaderDoclistCmp
136562   );
136563 
136564   assert( pCsr->pFilter==0 );
136565   assert( zTerm && nTerm>0 );
136566 
136567   /* Advance each segment iterator until it points to the term zTerm/nTerm. */
136568   rc = fts3SegReaderStart(p, pCsr, zTerm, nTerm);
136569   if( rc!=SQLITE_OK ) return rc;
136570 
136571   /* Determine how many of the segments actually point to zTerm/nTerm. */
136572   for(i=0; i<nSegment; i++){
136573     Fts3SegReader *pSeg = pCsr->apSegment[i];
136574     if( !pSeg->aNode || fts3SegReaderTermCmp(pSeg, zTerm, nTerm) ){
136575       break;
136576     }
136577   }
136578   pCsr->nAdvance = i;
136579 
136580   /* Advance each of the segments to point to the first docid. */
136581   for(i=0; i<pCsr->nAdvance; i++){
136582     rc = fts3SegReaderFirstDocid(p, pCsr->apSegment[i]);
136583     if( rc!=SQLITE_OK ) return rc;
136584   }
136585   fts3SegReaderSort(pCsr->apSegment, i, i, xCmp);
136586 
136587   assert( iCol<0 || iCol<p->nColumn );
136588   pCsr->iColFilter = iCol;
136589 
136590   return SQLITE_OK;
136591 }
136592 
136593 /*
136594 ** This function is called on a MultiSegReader that has been started using
136595 ** sqlite3Fts3MsrIncrStart(). One or more calls to MsrIncrNext() may also
136596 ** have been made. Calling this function puts the MultiSegReader in such
136597 ** a state that if the next two calls are:
136598 **
136599 **   sqlite3Fts3SegReaderStart()
136600 **   sqlite3Fts3SegReaderStep()
136601 **
136602 ** then the entire doclist for the term is available in
136603 ** MultiSegReader.aDoclist/nDoclist.
136604 */
136605 SQLITE_PRIVATE int sqlite3Fts3MsrIncrRestart(Fts3MultiSegReader *pCsr){
136606   int i;                          /* Used to iterate through segment-readers */
136607 
136608   assert( pCsr->zTerm==0 );
136609   assert( pCsr->nTerm==0 );
136610   assert( pCsr->aDoclist==0 );
136611   assert( pCsr->nDoclist==0 );
136612 
136613   pCsr->nAdvance = 0;
136614   pCsr->bRestart = 1;
136615   for(i=0; i<pCsr->nSegment; i++){
136616     pCsr->apSegment[i]->pOffsetList = 0;
136617     pCsr->apSegment[i]->nOffsetList = 0;
136618     pCsr->apSegment[i]->iDocid = 0;
136619   }
136620 
136621   return SQLITE_OK;
136622 }
136623 
136624 
136625 SQLITE_PRIVATE int sqlite3Fts3SegReaderStep(
136626   Fts3Table *p,                   /* Virtual table handle */
136627   Fts3MultiSegReader *pCsr        /* Cursor object */
136628 ){
136629   int rc = SQLITE_OK;
136630 
136631   int isIgnoreEmpty =  (pCsr->pFilter->flags & FTS3_SEGMENT_IGNORE_EMPTY);
136632   int isRequirePos =   (pCsr->pFilter->flags & FTS3_SEGMENT_REQUIRE_POS);
136633   int isColFilter =    (pCsr->pFilter->flags & FTS3_SEGMENT_COLUMN_FILTER);
136634   int isPrefix =       (pCsr->pFilter->flags & FTS3_SEGMENT_PREFIX);
136635   int isScan =         (pCsr->pFilter->flags & FTS3_SEGMENT_SCAN);
136636   int isFirst =        (pCsr->pFilter->flags & FTS3_SEGMENT_FIRST);
136637 
136638   Fts3SegReader **apSegment = pCsr->apSegment;
136639   int nSegment = pCsr->nSegment;
136640   Fts3SegFilter *pFilter = pCsr->pFilter;
136641   int (*xCmp)(Fts3SegReader *, Fts3SegReader *) = (
136642     p->bDescIdx ? fts3SegReaderDoclistCmpRev : fts3SegReaderDoclistCmp
136643   );
136644 
136645   if( pCsr->nSegment==0 ) return SQLITE_OK;
136646 
136647   do {
136648     int nMerge;
136649     int i;
136650 
136651     /* Advance the first pCsr->nAdvance entries in the apSegment[] array
136652     ** forward. Then sort the list in order of current term again.
136653     */
136654     for(i=0; i<pCsr->nAdvance; i++){
136655       Fts3SegReader *pSeg = apSegment[i];
136656       if( pSeg->bLookup ){
136657         fts3SegReaderSetEof(pSeg);
136658       }else{
136659         rc = fts3SegReaderNext(p, pSeg, 0);
136660       }
136661       if( rc!=SQLITE_OK ) return rc;
136662     }
136663     fts3SegReaderSort(apSegment, nSegment, pCsr->nAdvance, fts3SegReaderCmp);
136664     pCsr->nAdvance = 0;
136665 
136666     /* If all the seg-readers are at EOF, we're finished. return SQLITE_OK. */
136667     assert( rc==SQLITE_OK );
136668     if( apSegment[0]->aNode==0 ) break;
136669 
136670     pCsr->nTerm = apSegment[0]->nTerm;
136671     pCsr->zTerm = apSegment[0]->zTerm;
136672 
136673     /* If this is a prefix-search, and if the term that apSegment[0] points
136674     ** to does not share a suffix with pFilter->zTerm/nTerm, then all
136675     ** required callbacks have been made. In this case exit early.
136676     **
136677     ** Similarly, if this is a search for an exact match, and the first term
136678     ** of segment apSegment[0] is not a match, exit early.
136679     */
136680     if( pFilter->zTerm && !isScan ){
136681       if( pCsr->nTerm<pFilter->nTerm
136682        || (!isPrefix && pCsr->nTerm>pFilter->nTerm)
136683        || memcmp(pCsr->zTerm, pFilter->zTerm, pFilter->nTerm)
136684       ){
136685         break;
136686       }
136687     }
136688 
136689     nMerge = 1;
136690     while( nMerge<nSegment
136691         && apSegment[nMerge]->aNode
136692         && apSegment[nMerge]->nTerm==pCsr->nTerm
136693         && 0==memcmp(pCsr->zTerm, apSegment[nMerge]->zTerm, pCsr->nTerm)
136694     ){
136695       nMerge++;
136696     }
136697 
136698     assert( isIgnoreEmpty || (isRequirePos && !isColFilter) );
136699     if( nMerge==1
136700      && !isIgnoreEmpty
136701      && !isFirst
136702      && (p->bDescIdx==0 || fts3SegReaderIsPending(apSegment[0])==0)
136703     ){
136704       pCsr->nDoclist = apSegment[0]->nDoclist;
136705       if( fts3SegReaderIsPending(apSegment[0]) ){
136706         rc = fts3MsrBufferData(pCsr, apSegment[0]->aDoclist, pCsr->nDoclist);
136707         pCsr->aDoclist = pCsr->aBuffer;
136708       }else{
136709         pCsr->aDoclist = apSegment[0]->aDoclist;
136710       }
136711       if( rc==SQLITE_OK ) rc = SQLITE_ROW;
136712     }else{
136713       int nDoclist = 0;           /* Size of doclist */
136714       sqlite3_int64 iPrev = 0;    /* Previous docid stored in doclist */
136715 
136716       /* The current term of the first nMerge entries in the array
136717       ** of Fts3SegReader objects is the same. The doclists must be merged
136718       ** and a single term returned with the merged doclist.
136719       */
136720       for(i=0; i<nMerge; i++){
136721         fts3SegReaderFirstDocid(p, apSegment[i]);
136722       }
136723       fts3SegReaderSort(apSegment, nMerge, nMerge, xCmp);
136724       while( apSegment[0]->pOffsetList ){
136725         int j;                    /* Number of segments that share a docid */
136726         char *pList = 0;
136727         int nList = 0;
136728         int nByte;
136729         sqlite3_int64 iDocid = apSegment[0]->iDocid;
136730         fts3SegReaderNextDocid(p, apSegment[0], &pList, &nList);
136731         j = 1;
136732         while( j<nMerge
136733             && apSegment[j]->pOffsetList
136734             && apSegment[j]->iDocid==iDocid
136735         ){
136736           fts3SegReaderNextDocid(p, apSegment[j], 0, 0);
136737           j++;
136738         }
136739 
136740         if( isColFilter ){
136741           fts3ColumnFilter(pFilter->iCol, 0, &pList, &nList);
136742         }
136743 
136744         if( !isIgnoreEmpty || nList>0 ){
136745 
136746           /* Calculate the 'docid' delta value to write into the merged
136747           ** doclist. */
136748           sqlite3_int64 iDelta;
136749           if( p->bDescIdx && nDoclist>0 ){
136750             iDelta = iPrev - iDocid;
136751           }else{
136752             iDelta = iDocid - iPrev;
136753           }
136754           assert( iDelta>0 || (nDoclist==0 && iDelta==iDocid) );
136755           assert( nDoclist>0 || iDelta==iDocid );
136756 
136757           nByte = sqlite3Fts3VarintLen(iDelta) + (isRequirePos?nList+1:0);
136758           if( nDoclist+nByte>pCsr->nBuffer ){
136759             char *aNew;
136760             pCsr->nBuffer = (nDoclist+nByte)*2;
136761             aNew = sqlite3_realloc(pCsr->aBuffer, pCsr->nBuffer);
136762             if( !aNew ){
136763               return SQLITE_NOMEM;
136764             }
136765             pCsr->aBuffer = aNew;
136766           }
136767 
136768           if( isFirst ){
136769             char *a = &pCsr->aBuffer[nDoclist];
136770             int nWrite;
136771 
136772             nWrite = sqlite3Fts3FirstFilter(iDelta, pList, nList, a);
136773             if( nWrite ){
136774               iPrev = iDocid;
136775               nDoclist += nWrite;
136776             }
136777           }else{
136778             nDoclist += sqlite3Fts3PutVarint(&pCsr->aBuffer[nDoclist], iDelta);
136779             iPrev = iDocid;
136780             if( isRequirePos ){
136781               memcpy(&pCsr->aBuffer[nDoclist], pList, nList);
136782               nDoclist += nList;
136783               pCsr->aBuffer[nDoclist++] = '\0';
136784             }
136785           }
136786         }
136787 
136788         fts3SegReaderSort(apSegment, nMerge, j, xCmp);
136789       }
136790       if( nDoclist>0 ){
136791         pCsr->aDoclist = pCsr->aBuffer;
136792         pCsr->nDoclist = nDoclist;
136793         rc = SQLITE_ROW;
136794       }
136795     }
136796     pCsr->nAdvance = nMerge;
136797   }while( rc==SQLITE_OK );
136798 
136799   return rc;
136800 }
136801 
136802 
136803 SQLITE_PRIVATE void sqlite3Fts3SegReaderFinish(
136804   Fts3MultiSegReader *pCsr       /* Cursor object */
136805 ){
136806   if( pCsr ){
136807     int i;
136808     for(i=0; i<pCsr->nSegment; i++){
136809       sqlite3Fts3SegReaderFree(pCsr->apSegment[i]);
136810     }
136811     sqlite3_free(pCsr->apSegment);
136812     sqlite3_free(pCsr->aBuffer);
136813 
136814     pCsr->nSegment = 0;
136815     pCsr->apSegment = 0;
136816     pCsr->aBuffer = 0;
136817   }
136818 }
136819 
136820 /*
136821 ** Merge all level iLevel segments in the database into a single
136822 ** iLevel+1 segment. Or, if iLevel<0, merge all segments into a
136823 ** single segment with a level equal to the numerically largest level
136824 ** currently present in the database.
136825 **
136826 ** If this function is called with iLevel<0, but there is only one
136827 ** segment in the database, SQLITE_DONE is returned immediately.
136828 ** Otherwise, if successful, SQLITE_OK is returned. If an error occurs,
136829 ** an SQLite error code is returned.
136830 */
136831 static int fts3SegmentMerge(
136832   Fts3Table *p,
136833   int iLangid,                    /* Language id to merge */
136834   int iIndex,                     /* Index in p->aIndex[] to merge */
136835   int iLevel                      /* Level to merge */
136836 ){
136837   int rc;                         /* Return code */
136838   int iIdx = 0;                   /* Index of new segment */
136839   sqlite3_int64 iNewLevel = 0;    /* Level/index to create new segment at */
136840   SegmentWriter *pWriter = 0;     /* Used to write the new, merged, segment */
136841   Fts3SegFilter filter;           /* Segment term filter condition */
136842   Fts3MultiSegReader csr;         /* Cursor to iterate through level(s) */
136843   int bIgnoreEmpty = 0;           /* True to ignore empty segments */
136844 
136845   assert( iLevel==FTS3_SEGCURSOR_ALL
136846        || iLevel==FTS3_SEGCURSOR_PENDING
136847        || iLevel>=0
136848   );
136849   assert( iLevel<FTS3_SEGDIR_MAXLEVEL );
136850   assert( iIndex>=0 && iIndex<p->nIndex );
136851 
136852   rc = sqlite3Fts3SegReaderCursor(p, iLangid, iIndex, iLevel, 0, 0, 1, 0, &csr);
136853   if( rc!=SQLITE_OK || csr.nSegment==0 ) goto finished;
136854 
136855   if( iLevel==FTS3_SEGCURSOR_ALL ){
136856     /* This call is to merge all segments in the database to a single
136857     ** segment. The level of the new segment is equal to the numerically
136858     ** greatest segment level currently present in the database for this
136859     ** index. The idx of the new segment is always 0.  */
136860     if( csr.nSegment==1 ){
136861       rc = SQLITE_DONE;
136862       goto finished;
136863     }
136864     rc = fts3SegmentMaxLevel(p, iLangid, iIndex, &iNewLevel);
136865     bIgnoreEmpty = 1;
136866 
136867   }else if( iLevel==FTS3_SEGCURSOR_PENDING ){
136868     iNewLevel = getAbsoluteLevel(p, iLangid, iIndex, 0);
136869     rc = fts3AllocateSegdirIdx(p, iLangid, iIndex, 0, &iIdx);
136870   }else{
136871     /* This call is to merge all segments at level iLevel. find the next
136872     ** available segment index at level iLevel+1. The call to
136873     ** fts3AllocateSegdirIdx() will merge the segments at level iLevel+1 to
136874     ** a single iLevel+2 segment if necessary.  */
136875     rc = fts3AllocateSegdirIdx(p, iLangid, iIndex, iLevel+1, &iIdx);
136876     iNewLevel = getAbsoluteLevel(p, iLangid, iIndex, iLevel+1);
136877   }
136878   if( rc!=SQLITE_OK ) goto finished;
136879   assert( csr.nSegment>0 );
136880   assert( iNewLevel>=getAbsoluteLevel(p, iLangid, iIndex, 0) );
136881   assert( iNewLevel<getAbsoluteLevel(p, iLangid, iIndex,FTS3_SEGDIR_MAXLEVEL) );
136882 
136883   memset(&filter, 0, sizeof(Fts3SegFilter));
136884   filter.flags = FTS3_SEGMENT_REQUIRE_POS;
136885   filter.flags |= (bIgnoreEmpty ? FTS3_SEGMENT_IGNORE_EMPTY : 0);
136886 
136887   rc = sqlite3Fts3SegReaderStart(p, &csr, &filter);
136888   while( SQLITE_OK==rc ){
136889     rc = sqlite3Fts3SegReaderStep(p, &csr);
136890     if( rc!=SQLITE_ROW ) break;
136891     rc = fts3SegWriterAdd(p, &pWriter, 1,
136892         csr.zTerm, csr.nTerm, csr.aDoclist, csr.nDoclist);
136893   }
136894   if( rc!=SQLITE_OK ) goto finished;
136895   assert( pWriter );
136896 
136897   if( iLevel!=FTS3_SEGCURSOR_PENDING ){
136898     rc = fts3DeleteSegdir(
136899         p, iLangid, iIndex, iLevel, csr.apSegment, csr.nSegment
136900     );
136901     if( rc!=SQLITE_OK ) goto finished;
136902   }
136903   rc = fts3SegWriterFlush(p, pWriter, iNewLevel, iIdx);
136904 
136905  finished:
136906   fts3SegWriterFree(pWriter);
136907   sqlite3Fts3SegReaderFinish(&csr);
136908   return rc;
136909 }
136910 
136911 
136912 /*
136913 ** Flush the contents of pendingTerms to level 0 segments.
136914 */
136915 SQLITE_PRIVATE int sqlite3Fts3PendingTermsFlush(Fts3Table *p){
136916   int rc = SQLITE_OK;
136917   int i;
136918 
136919   for(i=0; rc==SQLITE_OK && i<p->nIndex; i++){
136920     rc = fts3SegmentMerge(p, p->iPrevLangid, i, FTS3_SEGCURSOR_PENDING);
136921     if( rc==SQLITE_DONE ) rc = SQLITE_OK;
136922   }
136923   sqlite3Fts3PendingTermsClear(p);
136924 
136925   /* Determine the auto-incr-merge setting if unknown.  If enabled,
136926   ** estimate the number of leaf blocks of content to be written
136927   */
136928   if( rc==SQLITE_OK && p->bHasStat
136929    && p->bAutoincrmerge==0xff && p->nLeafAdd>0
136930   ){
136931     sqlite3_stmt *pStmt = 0;
136932     rc = fts3SqlStmt(p, SQL_SELECT_STAT, &pStmt, 0);
136933     if( rc==SQLITE_OK ){
136934       sqlite3_bind_int(pStmt, 1, FTS_STAT_AUTOINCRMERGE);
136935       rc = sqlite3_step(pStmt);
136936       p->bAutoincrmerge = (rc==SQLITE_ROW && sqlite3_column_int(pStmt, 0));
136937       rc = sqlite3_reset(pStmt);
136938     }
136939   }
136940   return rc;
136941 }
136942 
136943 /*
136944 ** Encode N integers as varints into a blob.
136945 */
136946 static void fts3EncodeIntArray(
136947   int N,             /* The number of integers to encode */
136948   u32 *a,            /* The integer values */
136949   char *zBuf,        /* Write the BLOB here */
136950   int *pNBuf         /* Write number of bytes if zBuf[] used here */
136951 ){
136952   int i, j;
136953   for(i=j=0; i<N; i++){
136954     j += sqlite3Fts3PutVarint(&zBuf[j], (sqlite3_int64)a[i]);
136955   }
136956   *pNBuf = j;
136957 }
136958 
136959 /*
136960 ** Decode a blob of varints into N integers
136961 */
136962 static void fts3DecodeIntArray(
136963   int N,             /* The number of integers to decode */
136964   u32 *a,            /* Write the integer values */
136965   const char *zBuf,  /* The BLOB containing the varints */
136966   int nBuf           /* size of the BLOB */
136967 ){
136968   int i, j;
136969   UNUSED_PARAMETER(nBuf);
136970   for(i=j=0; i<N; i++){
136971     sqlite3_int64 x;
136972     j += sqlite3Fts3GetVarint(&zBuf[j], &x);
136973     assert(j<=nBuf);
136974     a[i] = (u32)(x & 0xffffffff);
136975   }
136976 }
136977 
136978 /*
136979 ** Insert the sizes (in tokens) for each column of the document
136980 ** with docid equal to p->iPrevDocid.  The sizes are encoded as
136981 ** a blob of varints.
136982 */
136983 static void fts3InsertDocsize(
136984   int *pRC,                       /* Result code */
136985   Fts3Table *p,                   /* Table into which to insert */
136986   u32 *aSz                        /* Sizes of each column, in tokens */
136987 ){
136988   char *pBlob;             /* The BLOB encoding of the document size */
136989   int nBlob;               /* Number of bytes in the BLOB */
136990   sqlite3_stmt *pStmt;     /* Statement used to insert the encoding */
136991   int rc;                  /* Result code from subfunctions */
136992 
136993   if( *pRC ) return;
136994   pBlob = sqlite3_malloc( 10*p->nColumn );
136995   if( pBlob==0 ){
136996     *pRC = SQLITE_NOMEM;
136997     return;
136998   }
136999   fts3EncodeIntArray(p->nColumn, aSz, pBlob, &nBlob);
137000   rc = fts3SqlStmt(p, SQL_REPLACE_DOCSIZE, &pStmt, 0);
137001   if( rc ){
137002     sqlite3_free(pBlob);
137003     *pRC = rc;
137004     return;
137005   }
137006   sqlite3_bind_int64(pStmt, 1, p->iPrevDocid);
137007   sqlite3_bind_blob(pStmt, 2, pBlob, nBlob, sqlite3_free);
137008   sqlite3_step(pStmt);
137009   *pRC = sqlite3_reset(pStmt);
137010 }
137011 
137012 /*
137013 ** Record 0 of the %_stat table contains a blob consisting of N varints,
137014 ** where N is the number of user defined columns in the fts3 table plus
137015 ** two. If nCol is the number of user defined columns, then values of the
137016 ** varints are set as follows:
137017 **
137018 **   Varint 0:       Total number of rows in the table.
137019 **
137020 **   Varint 1..nCol: For each column, the total number of tokens stored in
137021 **                   the column for all rows of the table.
137022 **
137023 **   Varint 1+nCol:  The total size, in bytes, of all text values in all
137024 **                   columns of all rows of the table.
137025 **
137026 */
137027 static void fts3UpdateDocTotals(
137028   int *pRC,                       /* The result code */
137029   Fts3Table *p,                   /* Table being updated */
137030   u32 *aSzIns,                    /* Size increases */
137031   u32 *aSzDel,                    /* Size decreases */
137032   int nChng                       /* Change in the number of documents */
137033 ){
137034   char *pBlob;             /* Storage for BLOB written into %_stat */
137035   int nBlob;               /* Size of BLOB written into %_stat */
137036   u32 *a;                  /* Array of integers that becomes the BLOB */
137037   sqlite3_stmt *pStmt;     /* Statement for reading and writing */
137038   int i;                   /* Loop counter */
137039   int rc;                  /* Result code from subfunctions */
137040 
137041   const int nStat = p->nColumn+2;
137042 
137043   if( *pRC ) return;
137044   a = sqlite3_malloc( (sizeof(u32)+10)*nStat );
137045   if( a==0 ){
137046     *pRC = SQLITE_NOMEM;
137047     return;
137048   }
137049   pBlob = (char*)&a[nStat];
137050   rc = fts3SqlStmt(p, SQL_SELECT_STAT, &pStmt, 0);
137051   if( rc ){
137052     sqlite3_free(a);
137053     *pRC = rc;
137054     return;
137055   }
137056   sqlite3_bind_int(pStmt, 1, FTS_STAT_DOCTOTAL);
137057   if( sqlite3_step(pStmt)==SQLITE_ROW ){
137058     fts3DecodeIntArray(nStat, a,
137059          sqlite3_column_blob(pStmt, 0),
137060          sqlite3_column_bytes(pStmt, 0));
137061   }else{
137062     memset(a, 0, sizeof(u32)*(nStat) );
137063   }
137064   rc = sqlite3_reset(pStmt);
137065   if( rc!=SQLITE_OK ){
137066     sqlite3_free(a);
137067     *pRC = rc;
137068     return;
137069   }
137070   if( nChng<0 && a[0]<(u32)(-nChng) ){
137071     a[0] = 0;
137072   }else{
137073     a[0] += nChng;
137074   }
137075   for(i=0; i<p->nColumn+1; i++){
137076     u32 x = a[i+1];
137077     if( x+aSzIns[i] < aSzDel[i] ){
137078       x = 0;
137079     }else{
137080       x = x + aSzIns[i] - aSzDel[i];
137081     }
137082     a[i+1] = x;
137083   }
137084   fts3EncodeIntArray(nStat, a, pBlob, &nBlob);
137085   rc = fts3SqlStmt(p, SQL_REPLACE_STAT, &pStmt, 0);
137086   if( rc ){
137087     sqlite3_free(a);
137088     *pRC = rc;
137089     return;
137090   }
137091   sqlite3_bind_int(pStmt, 1, FTS_STAT_DOCTOTAL);
137092   sqlite3_bind_blob(pStmt, 2, pBlob, nBlob, SQLITE_STATIC);
137093   sqlite3_step(pStmt);
137094   *pRC = sqlite3_reset(pStmt);
137095   sqlite3_free(a);
137096 }
137097 
137098 /*
137099 ** Merge the entire database so that there is one segment for each
137100 ** iIndex/iLangid combination.
137101 */
137102 static int fts3DoOptimize(Fts3Table *p, int bReturnDone){
137103   int bSeenDone = 0;
137104   int rc;
137105   sqlite3_stmt *pAllLangid = 0;
137106 
137107   rc = fts3SqlStmt(p, SQL_SELECT_ALL_LANGID, &pAllLangid, 0);
137108   if( rc==SQLITE_OK ){
137109     int rc2;
137110     sqlite3_bind_int(pAllLangid, 1, p->nIndex);
137111     while( sqlite3_step(pAllLangid)==SQLITE_ROW ){
137112       int i;
137113       int iLangid = sqlite3_column_int(pAllLangid, 0);
137114       for(i=0; rc==SQLITE_OK && i<p->nIndex; i++){
137115         rc = fts3SegmentMerge(p, iLangid, i, FTS3_SEGCURSOR_ALL);
137116         if( rc==SQLITE_DONE ){
137117           bSeenDone = 1;
137118           rc = SQLITE_OK;
137119         }
137120       }
137121     }
137122     rc2 = sqlite3_reset(pAllLangid);
137123     if( rc==SQLITE_OK ) rc = rc2;
137124   }
137125 
137126   sqlite3Fts3SegmentsClose(p);
137127   sqlite3Fts3PendingTermsClear(p);
137128 
137129   return (rc==SQLITE_OK && bReturnDone && bSeenDone) ? SQLITE_DONE : rc;
137130 }
137131 
137132 /*
137133 ** This function is called when the user executes the following statement:
137134 **
137135 **     INSERT INTO <tbl>(<tbl>) VALUES('rebuild');
137136 **
137137 ** The entire FTS index is discarded and rebuilt. If the table is one
137138 ** created using the content=xxx option, then the new index is based on
137139 ** the current contents of the xxx table. Otherwise, it is rebuilt based
137140 ** on the contents of the %_content table.
137141 */
137142 static int fts3DoRebuild(Fts3Table *p){
137143   int rc;                         /* Return Code */
137144 
137145   rc = fts3DeleteAll(p, 0);
137146   if( rc==SQLITE_OK ){
137147     u32 *aSz = 0;
137148     u32 *aSzIns = 0;
137149     u32 *aSzDel = 0;
137150     sqlite3_stmt *pStmt = 0;
137151     int nEntry = 0;
137152 
137153     /* Compose and prepare an SQL statement to loop through the content table */
137154     char *zSql = sqlite3_mprintf("SELECT %s" , p->zReadExprlist);
137155     if( !zSql ){
137156       rc = SQLITE_NOMEM;
137157     }else{
137158       rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
137159       sqlite3_free(zSql);
137160     }
137161 
137162     if( rc==SQLITE_OK ){
137163       int nByte = sizeof(u32) * (p->nColumn+1)*3;
137164       aSz = (u32 *)sqlite3_malloc(nByte);
137165       if( aSz==0 ){
137166         rc = SQLITE_NOMEM;
137167       }else{
137168         memset(aSz, 0, nByte);
137169         aSzIns = &aSz[p->nColumn+1];
137170         aSzDel = &aSzIns[p->nColumn+1];
137171       }
137172     }
137173 
137174     while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
137175       int iCol;
137176       int iLangid = langidFromSelect(p, pStmt);
137177       rc = fts3PendingTermsDocid(p, iLangid, sqlite3_column_int64(pStmt, 0));
137178       memset(aSz, 0, sizeof(aSz[0]) * (p->nColumn+1));
137179       for(iCol=0; rc==SQLITE_OK && iCol<p->nColumn; iCol++){
137180         if( p->abNotindexed[iCol]==0 ){
137181           const char *z = (const char *) sqlite3_column_text(pStmt, iCol+1);
137182           rc = fts3PendingTermsAdd(p, iLangid, z, iCol, &aSz[iCol]);
137183           aSz[p->nColumn] += sqlite3_column_bytes(pStmt, iCol+1);
137184         }
137185       }
137186       if( p->bHasDocsize ){
137187         fts3InsertDocsize(&rc, p, aSz);
137188       }
137189       if( rc!=SQLITE_OK ){
137190         sqlite3_finalize(pStmt);
137191         pStmt = 0;
137192       }else{
137193         nEntry++;
137194         for(iCol=0; iCol<=p->nColumn; iCol++){
137195           aSzIns[iCol] += aSz[iCol];
137196         }
137197       }
137198     }
137199     if( p->bFts4 ){
137200       fts3UpdateDocTotals(&rc, p, aSzIns, aSzDel, nEntry);
137201     }
137202     sqlite3_free(aSz);
137203 
137204     if( pStmt ){
137205       int rc2 = sqlite3_finalize(pStmt);
137206       if( rc==SQLITE_OK ){
137207         rc = rc2;
137208       }
137209     }
137210   }
137211 
137212   return rc;
137213 }
137214 
137215 
137216 /*
137217 ** This function opens a cursor used to read the input data for an
137218 ** incremental merge operation. Specifically, it opens a cursor to scan
137219 ** the oldest nSeg segments (idx=0 through idx=(nSeg-1)) in absolute
137220 ** level iAbsLevel.
137221 */
137222 static int fts3IncrmergeCsr(
137223   Fts3Table *p,                   /* FTS3 table handle */
137224   sqlite3_int64 iAbsLevel,        /* Absolute level to open */
137225   int nSeg,                       /* Number of segments to merge */
137226   Fts3MultiSegReader *pCsr        /* Cursor object to populate */
137227 ){
137228   int rc;                         /* Return Code */
137229   sqlite3_stmt *pStmt = 0;        /* Statement used to read %_segdir entry */
137230   int nByte;                      /* Bytes allocated at pCsr->apSegment[] */
137231 
137232   /* Allocate space for the Fts3MultiSegReader.aCsr[] array */
137233   memset(pCsr, 0, sizeof(*pCsr));
137234   nByte = sizeof(Fts3SegReader *) * nSeg;
137235   pCsr->apSegment = (Fts3SegReader **)sqlite3_malloc(nByte);
137236 
137237   if( pCsr->apSegment==0 ){
137238     rc = SQLITE_NOMEM;
137239   }else{
137240     memset(pCsr->apSegment, 0, nByte);
137241     rc = fts3SqlStmt(p, SQL_SELECT_LEVEL, &pStmt, 0);
137242   }
137243   if( rc==SQLITE_OK ){
137244     int i;
137245     int rc2;
137246     sqlite3_bind_int64(pStmt, 1, iAbsLevel);
137247     assert( pCsr->nSegment==0 );
137248     for(i=0; rc==SQLITE_OK && sqlite3_step(pStmt)==SQLITE_ROW && i<nSeg; i++){
137249       rc = sqlite3Fts3SegReaderNew(i, 0,
137250           sqlite3_column_int64(pStmt, 1),        /* segdir.start_block */
137251           sqlite3_column_int64(pStmt, 2),        /* segdir.leaves_end_block */
137252           sqlite3_column_int64(pStmt, 3),        /* segdir.end_block */
137253           sqlite3_column_blob(pStmt, 4),         /* segdir.root */
137254           sqlite3_column_bytes(pStmt, 4),        /* segdir.root */
137255           &pCsr->apSegment[i]
137256       );
137257       pCsr->nSegment++;
137258     }
137259     rc2 = sqlite3_reset(pStmt);
137260     if( rc==SQLITE_OK ) rc = rc2;
137261   }
137262 
137263   return rc;
137264 }
137265 
137266 typedef struct IncrmergeWriter IncrmergeWriter;
137267 typedef struct NodeWriter NodeWriter;
137268 typedef struct Blob Blob;
137269 typedef struct NodeReader NodeReader;
137270 
137271 /*
137272 ** An instance of the following structure is used as a dynamic buffer
137273 ** to build up nodes or other blobs of data in.
137274 **
137275 ** The function blobGrowBuffer() is used to extend the allocation.
137276 */
137277 struct Blob {
137278   char *a;                        /* Pointer to allocation */
137279   int n;                          /* Number of valid bytes of data in a[] */
137280   int nAlloc;                     /* Allocated size of a[] (nAlloc>=n) */
137281 };
137282 
137283 /*
137284 ** This structure is used to build up buffers containing segment b-tree
137285 ** nodes (blocks).
137286 */
137287 struct NodeWriter {
137288   sqlite3_int64 iBlock;           /* Current block id */
137289   Blob key;                       /* Last key written to the current block */
137290   Blob block;                     /* Current block image */
137291 };
137292 
137293 /*
137294 ** An object of this type contains the state required to create or append
137295 ** to an appendable b-tree segment.
137296 */
137297 struct IncrmergeWriter {
137298   int nLeafEst;                   /* Space allocated for leaf blocks */
137299   int nWork;                      /* Number of leaf pages flushed */
137300   sqlite3_int64 iAbsLevel;        /* Absolute level of input segments */
137301   int iIdx;                       /* Index of *output* segment in iAbsLevel+1 */
137302   sqlite3_int64 iStart;           /* Block number of first allocated block */
137303   sqlite3_int64 iEnd;             /* Block number of last allocated block */
137304   NodeWriter aNodeWriter[FTS_MAX_APPENDABLE_HEIGHT];
137305 };
137306 
137307 /*
137308 ** An object of the following type is used to read data from a single
137309 ** FTS segment node. See the following functions:
137310 **
137311 **     nodeReaderInit()
137312 **     nodeReaderNext()
137313 **     nodeReaderRelease()
137314 */
137315 struct NodeReader {
137316   const char *aNode;
137317   int nNode;
137318   int iOff;                       /* Current offset within aNode[] */
137319 
137320   /* Output variables. Containing the current node entry. */
137321   sqlite3_int64 iChild;           /* Pointer to child node */
137322   Blob term;                      /* Current term */
137323   const char *aDoclist;           /* Pointer to doclist */
137324   int nDoclist;                   /* Size of doclist in bytes */
137325 };
137326 
137327 /*
137328 ** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
137329 ** Otherwise, if the allocation at pBlob->a is not already at least nMin
137330 ** bytes in size, extend (realloc) it to be so.
137331 **
137332 ** If an OOM error occurs, set *pRc to SQLITE_NOMEM and leave pBlob->a
137333 ** unmodified. Otherwise, if the allocation succeeds, update pBlob->nAlloc
137334 ** to reflect the new size of the pBlob->a[] buffer.
137335 */
137336 static void blobGrowBuffer(Blob *pBlob, int nMin, int *pRc){
137337   if( *pRc==SQLITE_OK && nMin>pBlob->nAlloc ){
137338     int nAlloc = nMin;
137339     char *a = (char *)sqlite3_realloc(pBlob->a, nAlloc);
137340     if( a ){
137341       pBlob->nAlloc = nAlloc;
137342       pBlob->a = a;
137343     }else{
137344       *pRc = SQLITE_NOMEM;
137345     }
137346   }
137347 }
137348 
137349 /*
137350 ** Attempt to advance the node-reader object passed as the first argument to
137351 ** the next entry on the node.
137352 **
137353 ** Return an error code if an error occurs (SQLITE_NOMEM is possible).
137354 ** Otherwise return SQLITE_OK. If there is no next entry on the node
137355 ** (e.g. because the current entry is the last) set NodeReader->aNode to
137356 ** NULL to indicate EOF. Otherwise, populate the NodeReader structure output
137357 ** variables for the new entry.
137358 */
137359 static int nodeReaderNext(NodeReader *p){
137360   int bFirst = (p->term.n==0);    /* True for first term on the node */
137361   int nPrefix = 0;                /* Bytes to copy from previous term */
137362   int nSuffix = 0;                /* Bytes to append to the prefix */
137363   int rc = SQLITE_OK;             /* Return code */
137364 
137365   assert( p->aNode );
137366   if( p->iChild && bFirst==0 ) p->iChild++;
137367   if( p->iOff>=p->nNode ){
137368     /* EOF */
137369     p->aNode = 0;
137370   }else{
137371     if( bFirst==0 ){
137372       p->iOff += fts3GetVarint32(&p->aNode[p->iOff], &nPrefix);
137373     }
137374     p->iOff += fts3GetVarint32(&p->aNode[p->iOff], &nSuffix);
137375 
137376     blobGrowBuffer(&p->term, nPrefix+nSuffix, &rc);
137377     if( rc==SQLITE_OK ){
137378       memcpy(&p->term.a[nPrefix], &p->aNode[p->iOff], nSuffix);
137379       p->term.n = nPrefix+nSuffix;
137380       p->iOff += nSuffix;
137381       if( p->iChild==0 ){
137382         p->iOff += fts3GetVarint32(&p->aNode[p->iOff], &p->nDoclist);
137383         p->aDoclist = &p->aNode[p->iOff];
137384         p->iOff += p->nDoclist;
137385       }
137386     }
137387   }
137388 
137389   assert( p->iOff<=p->nNode );
137390 
137391   return rc;
137392 }
137393 
137394 /*
137395 ** Release all dynamic resources held by node-reader object *p.
137396 */
137397 static void nodeReaderRelease(NodeReader *p){
137398   sqlite3_free(p->term.a);
137399 }
137400 
137401 /*
137402 ** Initialize a node-reader object to read the node in buffer aNode/nNode.
137403 **
137404 ** If successful, SQLITE_OK is returned and the NodeReader object set to
137405 ** point to the first entry on the node (if any). Otherwise, an SQLite
137406 ** error code is returned.
137407 */
137408 static int nodeReaderInit(NodeReader *p, const char *aNode, int nNode){
137409   memset(p, 0, sizeof(NodeReader));
137410   p->aNode = aNode;
137411   p->nNode = nNode;
137412 
137413   /* Figure out if this is a leaf or an internal node. */
137414   if( p->aNode[0] ){
137415     /* An internal node. */
137416     p->iOff = 1 + sqlite3Fts3GetVarint(&p->aNode[1], &p->iChild);
137417   }else{
137418     p->iOff = 1;
137419   }
137420 
137421   return nodeReaderNext(p);
137422 }
137423 
137424 /*
137425 ** This function is called while writing an FTS segment each time a leaf o
137426 ** node is finished and written to disk. The key (zTerm/nTerm) is guaranteed
137427 ** to be greater than the largest key on the node just written, but smaller
137428 ** than or equal to the first key that will be written to the next leaf
137429 ** node.
137430 **
137431 ** The block id of the leaf node just written to disk may be found in
137432 ** (pWriter->aNodeWriter[0].iBlock) when this function is called.
137433 */
137434 static int fts3IncrmergePush(
137435   Fts3Table *p,                   /* Fts3 table handle */
137436   IncrmergeWriter *pWriter,       /* Writer object */
137437   const char *zTerm,              /* Term to write to internal node */
137438   int nTerm                       /* Bytes at zTerm */
137439 ){
137440   sqlite3_int64 iPtr = pWriter->aNodeWriter[0].iBlock;
137441   int iLayer;
137442 
137443   assert( nTerm>0 );
137444   for(iLayer=1; ALWAYS(iLayer<FTS_MAX_APPENDABLE_HEIGHT); iLayer++){
137445     sqlite3_int64 iNextPtr = 0;
137446     NodeWriter *pNode = &pWriter->aNodeWriter[iLayer];
137447     int rc = SQLITE_OK;
137448     int nPrefix;
137449     int nSuffix;
137450     int nSpace;
137451 
137452     /* Figure out how much space the key will consume if it is written to
137453     ** the current node of layer iLayer. Due to the prefix compression,
137454     ** the space required changes depending on which node the key is to
137455     ** be added to.  */
137456     nPrefix = fts3PrefixCompress(pNode->key.a, pNode->key.n, zTerm, nTerm);
137457     nSuffix = nTerm - nPrefix;
137458     nSpace  = sqlite3Fts3VarintLen(nPrefix);
137459     nSpace += sqlite3Fts3VarintLen(nSuffix) + nSuffix;
137460 
137461     if( pNode->key.n==0 || (pNode->block.n + nSpace)<=p->nNodeSize ){
137462       /* If the current node of layer iLayer contains zero keys, or if adding
137463       ** the key to it will not cause it to grow to larger than nNodeSize
137464       ** bytes in size, write the key here.  */
137465 
137466       Blob *pBlk = &pNode->block;
137467       if( pBlk->n==0 ){
137468         blobGrowBuffer(pBlk, p->nNodeSize, &rc);
137469         if( rc==SQLITE_OK ){
137470           pBlk->a[0] = (char)iLayer;
137471           pBlk->n = 1 + sqlite3Fts3PutVarint(&pBlk->a[1], iPtr);
137472         }
137473       }
137474       blobGrowBuffer(pBlk, pBlk->n + nSpace, &rc);
137475       blobGrowBuffer(&pNode->key, nTerm, &rc);
137476 
137477       if( rc==SQLITE_OK ){
137478         if( pNode->key.n ){
137479           pBlk->n += sqlite3Fts3PutVarint(&pBlk->a[pBlk->n], nPrefix);
137480         }
137481         pBlk->n += sqlite3Fts3PutVarint(&pBlk->a[pBlk->n], nSuffix);
137482         memcpy(&pBlk->a[pBlk->n], &zTerm[nPrefix], nSuffix);
137483         pBlk->n += nSuffix;
137484 
137485         memcpy(pNode->key.a, zTerm, nTerm);
137486         pNode->key.n = nTerm;
137487       }
137488     }else{
137489       /* Otherwise, flush the current node of layer iLayer to disk.
137490       ** Then allocate a new, empty sibling node. The key will be written
137491       ** into the parent of this node. */
137492       rc = fts3WriteSegment(p, pNode->iBlock, pNode->block.a, pNode->block.n);
137493 
137494       assert( pNode->block.nAlloc>=p->nNodeSize );
137495       pNode->block.a[0] = (char)iLayer;
137496       pNode->block.n = 1 + sqlite3Fts3PutVarint(&pNode->block.a[1], iPtr+1);
137497 
137498       iNextPtr = pNode->iBlock;
137499       pNode->iBlock++;
137500       pNode->key.n = 0;
137501     }
137502 
137503     if( rc!=SQLITE_OK || iNextPtr==0 ) return rc;
137504     iPtr = iNextPtr;
137505   }
137506 
137507   assert( 0 );
137508   return 0;
137509 }
137510 
137511 /*
137512 ** Append a term and (optionally) doclist to the FTS segment node currently
137513 ** stored in blob *pNode. The node need not contain any terms, but the
137514 ** header must be written before this function is called.
137515 **
137516 ** A node header is a single 0x00 byte for a leaf node, or a height varint
137517 ** followed by the left-hand-child varint for an internal node.
137518 **
137519 ** The term to be appended is passed via arguments zTerm/nTerm. For a
137520 ** leaf node, the doclist is passed as aDoclist/nDoclist. For an internal
137521 ** node, both aDoclist and nDoclist must be passed 0.
137522 **
137523 ** If the size of the value in blob pPrev is zero, then this is the first
137524 ** term written to the node. Otherwise, pPrev contains a copy of the
137525 ** previous term. Before this function returns, it is updated to contain a
137526 ** copy of zTerm/nTerm.
137527 **
137528 ** It is assumed that the buffer associated with pNode is already large
137529 ** enough to accommodate the new entry. The buffer associated with pPrev
137530 ** is extended by this function if requrired.
137531 **
137532 ** If an error (i.e. OOM condition) occurs, an SQLite error code is
137533 ** returned. Otherwise, SQLITE_OK.
137534 */
137535 static int fts3AppendToNode(
137536   Blob *pNode,                    /* Current node image to append to */
137537   Blob *pPrev,                    /* Buffer containing previous term written */
137538   const char *zTerm,              /* New term to write */
137539   int nTerm,                      /* Size of zTerm in bytes */
137540   const char *aDoclist,           /* Doclist (or NULL) to write */
137541   int nDoclist                    /* Size of aDoclist in bytes */
137542 ){
137543   int rc = SQLITE_OK;             /* Return code */
137544   int bFirst = (pPrev->n==0);     /* True if this is the first term written */
137545   int nPrefix;                    /* Size of term prefix in bytes */
137546   int nSuffix;                    /* Size of term suffix in bytes */
137547 
137548   /* Node must have already been started. There must be a doclist for a
137549   ** leaf node, and there must not be a doclist for an internal node.  */
137550   assert( pNode->n>0 );
137551   assert( (pNode->a[0]=='\0')==(aDoclist!=0) );
137552 
137553   blobGrowBuffer(pPrev, nTerm, &rc);
137554   if( rc!=SQLITE_OK ) return rc;
137555 
137556   nPrefix = fts3PrefixCompress(pPrev->a, pPrev->n, zTerm, nTerm);
137557   nSuffix = nTerm - nPrefix;
137558   memcpy(pPrev->a, zTerm, nTerm);
137559   pPrev->n = nTerm;
137560 
137561   if( bFirst==0 ){
137562     pNode->n += sqlite3Fts3PutVarint(&pNode->a[pNode->n], nPrefix);
137563   }
137564   pNode->n += sqlite3Fts3PutVarint(&pNode->a[pNode->n], nSuffix);
137565   memcpy(&pNode->a[pNode->n], &zTerm[nPrefix], nSuffix);
137566   pNode->n += nSuffix;
137567 
137568   if( aDoclist ){
137569     pNode->n += sqlite3Fts3PutVarint(&pNode->a[pNode->n], nDoclist);
137570     memcpy(&pNode->a[pNode->n], aDoclist, nDoclist);
137571     pNode->n += nDoclist;
137572   }
137573 
137574   assert( pNode->n<=pNode->nAlloc );
137575 
137576   return SQLITE_OK;
137577 }
137578 
137579 /*
137580 ** Append the current term and doclist pointed to by cursor pCsr to the
137581 ** appendable b-tree segment opened for writing by pWriter.
137582 **
137583 ** Return SQLITE_OK if successful, or an SQLite error code otherwise.
137584 */
137585 static int fts3IncrmergeAppend(
137586   Fts3Table *p,                   /* Fts3 table handle */
137587   IncrmergeWriter *pWriter,       /* Writer object */
137588   Fts3MultiSegReader *pCsr        /* Cursor containing term and doclist */
137589 ){
137590   const char *zTerm = pCsr->zTerm;
137591   int nTerm = pCsr->nTerm;
137592   const char *aDoclist = pCsr->aDoclist;
137593   int nDoclist = pCsr->nDoclist;
137594   int rc = SQLITE_OK;           /* Return code */
137595   int nSpace;                   /* Total space in bytes required on leaf */
137596   int nPrefix;                  /* Size of prefix shared with previous term */
137597   int nSuffix;                  /* Size of suffix (nTerm - nPrefix) */
137598   NodeWriter *pLeaf;            /* Object used to write leaf nodes */
137599 
137600   pLeaf = &pWriter->aNodeWriter[0];
137601   nPrefix = fts3PrefixCompress(pLeaf->key.a, pLeaf->key.n, zTerm, nTerm);
137602   nSuffix = nTerm - nPrefix;
137603 
137604   nSpace  = sqlite3Fts3VarintLen(nPrefix);
137605   nSpace += sqlite3Fts3VarintLen(nSuffix) + nSuffix;
137606   nSpace += sqlite3Fts3VarintLen(nDoclist) + nDoclist;
137607 
137608   /* If the current block is not empty, and if adding this term/doclist
137609   ** to the current block would make it larger than Fts3Table.nNodeSize
137610   ** bytes, write this block out to the database. */
137611   if( pLeaf->block.n>0 && (pLeaf->block.n + nSpace)>p->nNodeSize ){
137612     rc = fts3WriteSegment(p, pLeaf->iBlock, pLeaf->block.a, pLeaf->block.n);
137613     pWriter->nWork++;
137614 
137615     /* Add the current term to the parent node. The term added to the
137616     ** parent must:
137617     **
137618     **   a) be greater than the largest term on the leaf node just written
137619     **      to the database (still available in pLeaf->key), and
137620     **
137621     **   b) be less than or equal to the term about to be added to the new
137622     **      leaf node (zTerm/nTerm).
137623     **
137624     ** In other words, it must be the prefix of zTerm 1 byte longer than
137625     ** the common prefix (if any) of zTerm and pWriter->zTerm.
137626     */
137627     if( rc==SQLITE_OK ){
137628       rc = fts3IncrmergePush(p, pWriter, zTerm, nPrefix+1);
137629     }
137630 
137631     /* Advance to the next output block */
137632     pLeaf->iBlock++;
137633     pLeaf->key.n = 0;
137634     pLeaf->block.n = 0;
137635 
137636     nSuffix = nTerm;
137637     nSpace  = 1;
137638     nSpace += sqlite3Fts3VarintLen(nSuffix) + nSuffix;
137639     nSpace += sqlite3Fts3VarintLen(nDoclist) + nDoclist;
137640   }
137641 
137642   blobGrowBuffer(&pLeaf->block, pLeaf->block.n + nSpace, &rc);
137643 
137644   if( rc==SQLITE_OK ){
137645     if( pLeaf->block.n==0 ){
137646       pLeaf->block.n = 1;
137647       pLeaf->block.a[0] = '\0';
137648     }
137649     rc = fts3AppendToNode(
137650         &pLeaf->block, &pLeaf->key, zTerm, nTerm, aDoclist, nDoclist
137651     );
137652   }
137653 
137654   return rc;
137655 }
137656 
137657 /*
137658 ** This function is called to release all dynamic resources held by the
137659 ** merge-writer object pWriter, and if no error has occurred, to flush
137660 ** all outstanding node buffers held by pWriter to disk.
137661 **
137662 ** If *pRc is not SQLITE_OK when this function is called, then no attempt
137663 ** is made to write any data to disk. Instead, this function serves only
137664 ** to release outstanding resources.
137665 **
137666 ** Otherwise, if *pRc is initially SQLITE_OK and an error occurs while
137667 ** flushing buffers to disk, *pRc is set to an SQLite error code before
137668 ** returning.
137669 */
137670 static void fts3IncrmergeRelease(
137671   Fts3Table *p,                   /* FTS3 table handle */
137672   IncrmergeWriter *pWriter,       /* Merge-writer object */
137673   int *pRc                        /* IN/OUT: Error code */
137674 ){
137675   int i;                          /* Used to iterate through non-root layers */
137676   int iRoot;                      /* Index of root in pWriter->aNodeWriter */
137677   NodeWriter *pRoot;              /* NodeWriter for root node */
137678   int rc = *pRc;                  /* Error code */
137679 
137680   /* Set iRoot to the index in pWriter->aNodeWriter[] of the output segment
137681   ** root node. If the segment fits entirely on a single leaf node, iRoot
137682   ** will be set to 0. If the root node is the parent of the leaves, iRoot
137683   ** will be 1. And so on.  */
137684   for(iRoot=FTS_MAX_APPENDABLE_HEIGHT-1; iRoot>=0; iRoot--){
137685     NodeWriter *pNode = &pWriter->aNodeWriter[iRoot];
137686     if( pNode->block.n>0 ) break;
137687     assert( *pRc || pNode->block.nAlloc==0 );
137688     assert( *pRc || pNode->key.nAlloc==0 );
137689     sqlite3_free(pNode->block.a);
137690     sqlite3_free(pNode->key.a);
137691   }
137692 
137693   /* Empty output segment. This is a no-op. */
137694   if( iRoot<0 ) return;
137695 
137696   /* The entire output segment fits on a single node. Normally, this means
137697   ** the node would be stored as a blob in the "root" column of the %_segdir
137698   ** table. However, this is not permitted in this case. The problem is that
137699   ** space has already been reserved in the %_segments table, and so the
137700   ** start_block and end_block fields of the %_segdir table must be populated.
137701   ** And, by design or by accident, released versions of FTS cannot handle
137702   ** segments that fit entirely on the root node with start_block!=0.
137703   **
137704   ** Instead, create a synthetic root node that contains nothing but a
137705   ** pointer to the single content node. So that the segment consists of a
137706   ** single leaf and a single interior (root) node.
137707   **
137708   ** Todo: Better might be to defer allocating space in the %_segments
137709   ** table until we are sure it is needed.
137710   */
137711   if( iRoot==0 ){
137712     Blob *pBlock = &pWriter->aNodeWriter[1].block;
137713     blobGrowBuffer(pBlock, 1 + FTS3_VARINT_MAX, &rc);
137714     if( rc==SQLITE_OK ){
137715       pBlock->a[0] = 0x01;
137716       pBlock->n = 1 + sqlite3Fts3PutVarint(
137717           &pBlock->a[1], pWriter->aNodeWriter[0].iBlock
137718       );
137719     }
137720     iRoot = 1;
137721   }
137722   pRoot = &pWriter->aNodeWriter[iRoot];
137723 
137724   /* Flush all currently outstanding nodes to disk. */
137725   for(i=0; i<iRoot; i++){
137726     NodeWriter *pNode = &pWriter->aNodeWriter[i];
137727     if( pNode->block.n>0 && rc==SQLITE_OK ){
137728       rc = fts3WriteSegment(p, pNode->iBlock, pNode->block.a, pNode->block.n);
137729     }
137730     sqlite3_free(pNode->block.a);
137731     sqlite3_free(pNode->key.a);
137732   }
137733 
137734   /* Write the %_segdir record. */
137735   if( rc==SQLITE_OK ){
137736     rc = fts3WriteSegdir(p,
137737         pWriter->iAbsLevel+1,               /* level */
137738         pWriter->iIdx,                      /* idx */
137739         pWriter->iStart,                    /* start_block */
137740         pWriter->aNodeWriter[0].iBlock,     /* leaves_end_block */
137741         pWriter->iEnd,                      /* end_block */
137742         pRoot->block.a, pRoot->block.n      /* root */
137743     );
137744   }
137745   sqlite3_free(pRoot->block.a);
137746   sqlite3_free(pRoot->key.a);
137747 
137748   *pRc = rc;
137749 }
137750 
137751 /*
137752 ** Compare the term in buffer zLhs (size in bytes nLhs) with that in
137753 ** zRhs (size in bytes nRhs) using memcmp. If one term is a prefix of
137754 ** the other, it is considered to be smaller than the other.
137755 **
137756 ** Return -ve if zLhs is smaller than zRhs, 0 if it is equal, or +ve
137757 ** if it is greater.
137758 */
137759 static int fts3TermCmp(
137760   const char *zLhs, int nLhs,     /* LHS of comparison */
137761   const char *zRhs, int nRhs      /* RHS of comparison */
137762 ){
137763   int nCmp = MIN(nLhs, nRhs);
137764   int res;
137765 
137766   res = memcmp(zLhs, zRhs, nCmp);
137767   if( res==0 ) res = nLhs - nRhs;
137768 
137769   return res;
137770 }
137771 
137772 
137773 /*
137774 ** Query to see if the entry in the %_segments table with blockid iEnd is
137775 ** NULL. If no error occurs and the entry is NULL, set *pbRes 1 before
137776 ** returning. Otherwise, set *pbRes to 0.
137777 **
137778 ** Or, if an error occurs while querying the database, return an SQLite
137779 ** error code. The final value of *pbRes is undefined in this case.
137780 **
137781 ** This is used to test if a segment is an "appendable" segment. If it
137782 ** is, then a NULL entry has been inserted into the %_segments table
137783 ** with blockid %_segdir.end_block.
137784 */
137785 static int fts3IsAppendable(Fts3Table *p, sqlite3_int64 iEnd, int *pbRes){
137786   int bRes = 0;                   /* Result to set *pbRes to */
137787   sqlite3_stmt *pCheck = 0;       /* Statement to query database with */
137788   int rc;                         /* Return code */
137789 
137790   rc = fts3SqlStmt(p, SQL_SEGMENT_IS_APPENDABLE, &pCheck, 0);
137791   if( rc==SQLITE_OK ){
137792     sqlite3_bind_int64(pCheck, 1, iEnd);
137793     if( SQLITE_ROW==sqlite3_step(pCheck) ) bRes = 1;
137794     rc = sqlite3_reset(pCheck);
137795   }
137796 
137797   *pbRes = bRes;
137798   return rc;
137799 }
137800 
137801 /*
137802 ** This function is called when initializing an incremental-merge operation.
137803 ** It checks if the existing segment with index value iIdx at absolute level
137804 ** (iAbsLevel+1) can be appended to by the incremental merge. If it can, the
137805 ** merge-writer object *pWriter is initialized to write to it.
137806 **
137807 ** An existing segment can be appended to by an incremental merge if:
137808 **
137809 **   * It was initially created as an appendable segment (with all required
137810 **     space pre-allocated), and
137811 **
137812 **   * The first key read from the input (arguments zKey and nKey) is
137813 **     greater than the largest key currently stored in the potential
137814 **     output segment.
137815 */
137816 static int fts3IncrmergeLoad(
137817   Fts3Table *p,                   /* Fts3 table handle */
137818   sqlite3_int64 iAbsLevel,        /* Absolute level of input segments */
137819   int iIdx,                       /* Index of candidate output segment */
137820   const char *zKey,               /* First key to write */
137821   int nKey,                       /* Number of bytes in nKey */
137822   IncrmergeWriter *pWriter        /* Populate this object */
137823 ){
137824   int rc;                         /* Return code */
137825   sqlite3_stmt *pSelect = 0;      /* SELECT to read %_segdir entry */
137826 
137827   rc = fts3SqlStmt(p, SQL_SELECT_SEGDIR, &pSelect, 0);
137828   if( rc==SQLITE_OK ){
137829     sqlite3_int64 iStart = 0;     /* Value of %_segdir.start_block */
137830     sqlite3_int64 iLeafEnd = 0;   /* Value of %_segdir.leaves_end_block */
137831     sqlite3_int64 iEnd = 0;       /* Value of %_segdir.end_block */
137832     const char *aRoot = 0;        /* Pointer to %_segdir.root buffer */
137833     int nRoot = 0;                /* Size of aRoot[] in bytes */
137834     int rc2;                      /* Return code from sqlite3_reset() */
137835     int bAppendable = 0;          /* Set to true if segment is appendable */
137836 
137837     /* Read the %_segdir entry for index iIdx absolute level (iAbsLevel+1) */
137838     sqlite3_bind_int64(pSelect, 1, iAbsLevel+1);
137839     sqlite3_bind_int(pSelect, 2, iIdx);
137840     if( sqlite3_step(pSelect)==SQLITE_ROW ){
137841       iStart = sqlite3_column_int64(pSelect, 1);
137842       iLeafEnd = sqlite3_column_int64(pSelect, 2);
137843       iEnd = sqlite3_column_int64(pSelect, 3);
137844       nRoot = sqlite3_column_bytes(pSelect, 4);
137845       aRoot = sqlite3_column_blob(pSelect, 4);
137846     }else{
137847       return sqlite3_reset(pSelect);
137848     }
137849 
137850     /* Check for the zero-length marker in the %_segments table */
137851     rc = fts3IsAppendable(p, iEnd, &bAppendable);
137852 
137853     /* Check that zKey/nKey is larger than the largest key the candidate */
137854     if( rc==SQLITE_OK && bAppendable ){
137855       char *aLeaf = 0;
137856       int nLeaf = 0;
137857 
137858       rc = sqlite3Fts3ReadBlock(p, iLeafEnd, &aLeaf, &nLeaf, 0);
137859       if( rc==SQLITE_OK ){
137860         NodeReader reader;
137861         for(rc = nodeReaderInit(&reader, aLeaf, nLeaf);
137862             rc==SQLITE_OK && reader.aNode;
137863             rc = nodeReaderNext(&reader)
137864         ){
137865           assert( reader.aNode );
137866         }
137867         if( fts3TermCmp(zKey, nKey, reader.term.a, reader.term.n)<=0 ){
137868           bAppendable = 0;
137869         }
137870         nodeReaderRelease(&reader);
137871       }
137872       sqlite3_free(aLeaf);
137873     }
137874 
137875     if( rc==SQLITE_OK && bAppendable ){
137876       /* It is possible to append to this segment. Set up the IncrmergeWriter
137877       ** object to do so.  */
137878       int i;
137879       int nHeight = (int)aRoot[0];
137880       NodeWriter *pNode;
137881 
137882       pWriter->nLeafEst = (int)((iEnd - iStart) + 1)/FTS_MAX_APPENDABLE_HEIGHT;
137883       pWriter->iStart = iStart;
137884       pWriter->iEnd = iEnd;
137885       pWriter->iAbsLevel = iAbsLevel;
137886       pWriter->iIdx = iIdx;
137887 
137888       for(i=nHeight+1; i<FTS_MAX_APPENDABLE_HEIGHT; i++){
137889         pWriter->aNodeWriter[i].iBlock = pWriter->iStart + i*pWriter->nLeafEst;
137890       }
137891 
137892       pNode = &pWriter->aNodeWriter[nHeight];
137893       pNode->iBlock = pWriter->iStart + pWriter->nLeafEst*nHeight;
137894       blobGrowBuffer(&pNode->block, MAX(nRoot, p->nNodeSize), &rc);
137895       if( rc==SQLITE_OK ){
137896         memcpy(pNode->block.a, aRoot, nRoot);
137897         pNode->block.n = nRoot;
137898       }
137899 
137900       for(i=nHeight; i>=0 && rc==SQLITE_OK; i--){
137901         NodeReader reader;
137902         pNode = &pWriter->aNodeWriter[i];
137903 
137904         rc = nodeReaderInit(&reader, pNode->block.a, pNode->block.n);
137905         while( reader.aNode && rc==SQLITE_OK ) rc = nodeReaderNext(&reader);
137906         blobGrowBuffer(&pNode->key, reader.term.n, &rc);
137907         if( rc==SQLITE_OK ){
137908           memcpy(pNode->key.a, reader.term.a, reader.term.n);
137909           pNode->key.n = reader.term.n;
137910           if( i>0 ){
137911             char *aBlock = 0;
137912             int nBlock = 0;
137913             pNode = &pWriter->aNodeWriter[i-1];
137914             pNode->iBlock = reader.iChild;
137915             rc = sqlite3Fts3ReadBlock(p, reader.iChild, &aBlock, &nBlock, 0);
137916             blobGrowBuffer(&pNode->block, MAX(nBlock, p->nNodeSize), &rc);
137917             if( rc==SQLITE_OK ){
137918               memcpy(pNode->block.a, aBlock, nBlock);
137919               pNode->block.n = nBlock;
137920             }
137921             sqlite3_free(aBlock);
137922           }
137923         }
137924         nodeReaderRelease(&reader);
137925       }
137926     }
137927 
137928     rc2 = sqlite3_reset(pSelect);
137929     if( rc==SQLITE_OK ) rc = rc2;
137930   }
137931 
137932   return rc;
137933 }
137934 
137935 /*
137936 ** Determine the largest segment index value that exists within absolute
137937 ** level iAbsLevel+1. If no error occurs, set *piIdx to this value plus
137938 ** one before returning SQLITE_OK. Or, if there are no segments at all
137939 ** within level iAbsLevel, set *piIdx to zero.
137940 **
137941 ** If an error occurs, return an SQLite error code. The final value of
137942 ** *piIdx is undefined in this case.
137943 */
137944 static int fts3IncrmergeOutputIdx(
137945   Fts3Table *p,                   /* FTS Table handle */
137946   sqlite3_int64 iAbsLevel,        /* Absolute index of input segments */
137947   int *piIdx                      /* OUT: Next free index at iAbsLevel+1 */
137948 ){
137949   int rc;
137950   sqlite3_stmt *pOutputIdx = 0;   /* SQL used to find output index */
137951 
137952   rc = fts3SqlStmt(p, SQL_NEXT_SEGMENT_INDEX, &pOutputIdx, 0);
137953   if( rc==SQLITE_OK ){
137954     sqlite3_bind_int64(pOutputIdx, 1, iAbsLevel+1);
137955     sqlite3_step(pOutputIdx);
137956     *piIdx = sqlite3_column_int(pOutputIdx, 0);
137957     rc = sqlite3_reset(pOutputIdx);
137958   }
137959 
137960   return rc;
137961 }
137962 
137963 /*
137964 ** Allocate an appendable output segment on absolute level iAbsLevel+1
137965 ** with idx value iIdx.
137966 **
137967 ** In the %_segdir table, a segment is defined by the values in three
137968 ** columns:
137969 **
137970 **     start_block
137971 **     leaves_end_block
137972 **     end_block
137973 **
137974 ** When an appendable segment is allocated, it is estimated that the
137975 ** maximum number of leaf blocks that may be required is the sum of the
137976 ** number of leaf blocks consumed by the input segments, plus the number
137977 ** of input segments, multiplied by two. This value is stored in stack
137978 ** variable nLeafEst.
137979 **
137980 ** A total of 16*nLeafEst blocks are allocated when an appendable segment
137981 ** is created ((1 + end_block - start_block)==16*nLeafEst). The contiguous
137982 ** array of leaf nodes starts at the first block allocated. The array
137983 ** of interior nodes that are parents of the leaf nodes start at block
137984 ** (start_block + (1 + end_block - start_block) / 16). And so on.
137985 **
137986 ** In the actual code below, the value "16" is replaced with the
137987 ** pre-processor macro FTS_MAX_APPENDABLE_HEIGHT.
137988 */
137989 static int fts3IncrmergeWriter(
137990   Fts3Table *p,                   /* Fts3 table handle */
137991   sqlite3_int64 iAbsLevel,        /* Absolute level of input segments */
137992   int iIdx,                       /* Index of new output segment */
137993   Fts3MultiSegReader *pCsr,       /* Cursor that data will be read from */
137994   IncrmergeWriter *pWriter        /* Populate this object */
137995 ){
137996   int rc;                         /* Return Code */
137997   int i;                          /* Iterator variable */
137998   int nLeafEst = 0;               /* Blocks allocated for leaf nodes */
137999   sqlite3_stmt *pLeafEst = 0;     /* SQL used to determine nLeafEst */
138000   sqlite3_stmt *pFirstBlock = 0;  /* SQL used to determine first block */
138001 
138002   /* Calculate nLeafEst. */
138003   rc = fts3SqlStmt(p, SQL_MAX_LEAF_NODE_ESTIMATE, &pLeafEst, 0);
138004   if( rc==SQLITE_OK ){
138005     sqlite3_bind_int64(pLeafEst, 1, iAbsLevel);
138006     sqlite3_bind_int64(pLeafEst, 2, pCsr->nSegment);
138007     if( SQLITE_ROW==sqlite3_step(pLeafEst) ){
138008       nLeafEst = sqlite3_column_int(pLeafEst, 0);
138009     }
138010     rc = sqlite3_reset(pLeafEst);
138011   }
138012   if( rc!=SQLITE_OK ) return rc;
138013 
138014   /* Calculate the first block to use in the output segment */
138015   rc = fts3SqlStmt(p, SQL_NEXT_SEGMENTS_ID, &pFirstBlock, 0);
138016   if( rc==SQLITE_OK ){
138017     if( SQLITE_ROW==sqlite3_step(pFirstBlock) ){
138018       pWriter->iStart = sqlite3_column_int64(pFirstBlock, 0);
138019       pWriter->iEnd = pWriter->iStart - 1;
138020       pWriter->iEnd += nLeafEst * FTS_MAX_APPENDABLE_HEIGHT;
138021     }
138022     rc = sqlite3_reset(pFirstBlock);
138023   }
138024   if( rc!=SQLITE_OK ) return rc;
138025 
138026   /* Insert the marker in the %_segments table to make sure nobody tries
138027   ** to steal the space just allocated. This is also used to identify
138028   ** appendable segments.  */
138029   rc = fts3WriteSegment(p, pWriter->iEnd, 0, 0);
138030   if( rc!=SQLITE_OK ) return rc;
138031 
138032   pWriter->iAbsLevel = iAbsLevel;
138033   pWriter->nLeafEst = nLeafEst;
138034   pWriter->iIdx = iIdx;
138035 
138036   /* Set up the array of NodeWriter objects */
138037   for(i=0; i<FTS_MAX_APPENDABLE_HEIGHT; i++){
138038     pWriter->aNodeWriter[i].iBlock = pWriter->iStart + i*pWriter->nLeafEst;
138039   }
138040   return SQLITE_OK;
138041 }
138042 
138043 /*
138044 ** Remove an entry from the %_segdir table. This involves running the
138045 ** following two statements:
138046 **
138047 **   DELETE FROM %_segdir WHERE level = :iAbsLevel AND idx = :iIdx
138048 **   UPDATE %_segdir SET idx = idx - 1 WHERE level = :iAbsLevel AND idx > :iIdx
138049 **
138050 ** The DELETE statement removes the specific %_segdir level. The UPDATE
138051 ** statement ensures that the remaining segments have contiguously allocated
138052 ** idx values.
138053 */
138054 static int fts3RemoveSegdirEntry(
138055   Fts3Table *p,                   /* FTS3 table handle */
138056   sqlite3_int64 iAbsLevel,        /* Absolute level to delete from */
138057   int iIdx                        /* Index of %_segdir entry to delete */
138058 ){
138059   int rc;                         /* Return code */
138060   sqlite3_stmt *pDelete = 0;      /* DELETE statement */
138061 
138062   rc = fts3SqlStmt(p, SQL_DELETE_SEGDIR_ENTRY, &pDelete, 0);
138063   if( rc==SQLITE_OK ){
138064     sqlite3_bind_int64(pDelete, 1, iAbsLevel);
138065     sqlite3_bind_int(pDelete, 2, iIdx);
138066     sqlite3_step(pDelete);
138067     rc = sqlite3_reset(pDelete);
138068   }
138069 
138070   return rc;
138071 }
138072 
138073 /*
138074 ** One or more segments have just been removed from absolute level iAbsLevel.
138075 ** Update the 'idx' values of the remaining segments in the level so that
138076 ** the idx values are a contiguous sequence starting from 0.
138077 */
138078 static int fts3RepackSegdirLevel(
138079   Fts3Table *p,                   /* FTS3 table handle */
138080   sqlite3_int64 iAbsLevel         /* Absolute level to repack */
138081 ){
138082   int rc;                         /* Return code */
138083   int *aIdx = 0;                  /* Array of remaining idx values */
138084   int nIdx = 0;                   /* Valid entries in aIdx[] */
138085   int nAlloc = 0;                 /* Allocated size of aIdx[] */
138086   int i;                          /* Iterator variable */
138087   sqlite3_stmt *pSelect = 0;      /* Select statement to read idx values */
138088   sqlite3_stmt *pUpdate = 0;      /* Update statement to modify idx values */
138089 
138090   rc = fts3SqlStmt(p, SQL_SELECT_INDEXES, &pSelect, 0);
138091   if( rc==SQLITE_OK ){
138092     int rc2;
138093     sqlite3_bind_int64(pSelect, 1, iAbsLevel);
138094     while( SQLITE_ROW==sqlite3_step(pSelect) ){
138095       if( nIdx>=nAlloc ){
138096         int *aNew;
138097         nAlloc += 16;
138098         aNew = sqlite3_realloc(aIdx, nAlloc*sizeof(int));
138099         if( !aNew ){
138100           rc = SQLITE_NOMEM;
138101           break;
138102         }
138103         aIdx = aNew;
138104       }
138105       aIdx[nIdx++] = sqlite3_column_int(pSelect, 0);
138106     }
138107     rc2 = sqlite3_reset(pSelect);
138108     if( rc==SQLITE_OK ) rc = rc2;
138109   }
138110 
138111   if( rc==SQLITE_OK ){
138112     rc = fts3SqlStmt(p, SQL_SHIFT_SEGDIR_ENTRY, &pUpdate, 0);
138113   }
138114   if( rc==SQLITE_OK ){
138115     sqlite3_bind_int64(pUpdate, 2, iAbsLevel);
138116   }
138117 
138118   assert( p->bIgnoreSavepoint==0 );
138119   p->bIgnoreSavepoint = 1;
138120   for(i=0; rc==SQLITE_OK && i<nIdx; i++){
138121     if( aIdx[i]!=i ){
138122       sqlite3_bind_int(pUpdate, 3, aIdx[i]);
138123       sqlite3_bind_int(pUpdate, 1, i);
138124       sqlite3_step(pUpdate);
138125       rc = sqlite3_reset(pUpdate);
138126     }
138127   }
138128   p->bIgnoreSavepoint = 0;
138129 
138130   sqlite3_free(aIdx);
138131   return rc;
138132 }
138133 
138134 static void fts3StartNode(Blob *pNode, int iHeight, sqlite3_int64 iChild){
138135   pNode->a[0] = (char)iHeight;
138136   if( iChild ){
138137     assert( pNode->nAlloc>=1+sqlite3Fts3VarintLen(iChild) );
138138     pNode->n = 1 + sqlite3Fts3PutVarint(&pNode->a[1], iChild);
138139   }else{
138140     assert( pNode->nAlloc>=1 );
138141     pNode->n = 1;
138142   }
138143 }
138144 
138145 /*
138146 ** The first two arguments are a pointer to and the size of a segment b-tree
138147 ** node. The node may be a leaf or an internal node.
138148 **
138149 ** This function creates a new node image in blob object *pNew by copying
138150 ** all terms that are greater than or equal to zTerm/nTerm (for leaf nodes)
138151 ** or greater than zTerm/nTerm (for internal nodes) from aNode/nNode.
138152 */
138153 static int fts3TruncateNode(
138154   const char *aNode,              /* Current node image */
138155   int nNode,                      /* Size of aNode in bytes */
138156   Blob *pNew,                     /* OUT: Write new node image here */
138157   const char *zTerm,              /* Omit all terms smaller than this */
138158   int nTerm,                      /* Size of zTerm in bytes */
138159   sqlite3_int64 *piBlock          /* OUT: Block number in next layer down */
138160 ){
138161   NodeReader reader;              /* Reader object */
138162   Blob prev = {0, 0, 0};          /* Previous term written to new node */
138163   int rc = SQLITE_OK;             /* Return code */
138164   int bLeaf = aNode[0]=='\0';     /* True for a leaf node */
138165 
138166   /* Allocate required output space */
138167   blobGrowBuffer(pNew, nNode, &rc);
138168   if( rc!=SQLITE_OK ) return rc;
138169   pNew->n = 0;
138170 
138171   /* Populate new node buffer */
138172   for(rc = nodeReaderInit(&reader, aNode, nNode);
138173       rc==SQLITE_OK && reader.aNode;
138174       rc = nodeReaderNext(&reader)
138175   ){
138176     if( pNew->n==0 ){
138177       int res = fts3TermCmp(reader.term.a, reader.term.n, zTerm, nTerm);
138178       if( res<0 || (bLeaf==0 && res==0) ) continue;
138179       fts3StartNode(pNew, (int)aNode[0], reader.iChild);
138180       *piBlock = reader.iChild;
138181     }
138182     rc = fts3AppendToNode(
138183         pNew, &prev, reader.term.a, reader.term.n,
138184         reader.aDoclist, reader.nDoclist
138185     );
138186     if( rc!=SQLITE_OK ) break;
138187   }
138188   if( pNew->n==0 ){
138189     fts3StartNode(pNew, (int)aNode[0], reader.iChild);
138190     *piBlock = reader.iChild;
138191   }
138192   assert( pNew->n<=pNew->nAlloc );
138193 
138194   nodeReaderRelease(&reader);
138195   sqlite3_free(prev.a);
138196   return rc;
138197 }
138198 
138199 /*
138200 ** Remove all terms smaller than zTerm/nTerm from segment iIdx in absolute
138201 ** level iAbsLevel. This may involve deleting entries from the %_segments
138202 ** table, and modifying existing entries in both the %_segments and %_segdir
138203 ** tables.
138204 **
138205 ** SQLITE_OK is returned if the segment is updated successfully. Or an
138206 ** SQLite error code otherwise.
138207 */
138208 static int fts3TruncateSegment(
138209   Fts3Table *p,                   /* FTS3 table handle */
138210   sqlite3_int64 iAbsLevel,        /* Absolute level of segment to modify */
138211   int iIdx,                       /* Index within level of segment to modify */
138212   const char *zTerm,              /* Remove terms smaller than this */
138213   int nTerm                      /* Number of bytes in buffer zTerm */
138214 ){
138215   int rc = SQLITE_OK;             /* Return code */
138216   Blob root = {0,0,0};            /* New root page image */
138217   Blob block = {0,0,0};           /* Buffer used for any other block */
138218   sqlite3_int64 iBlock = 0;       /* Block id */
138219   sqlite3_int64 iNewStart = 0;    /* New value for iStartBlock */
138220   sqlite3_int64 iOldStart = 0;    /* Old value for iStartBlock */
138221   sqlite3_stmt *pFetch = 0;       /* Statement used to fetch segdir */
138222 
138223   rc = fts3SqlStmt(p, SQL_SELECT_SEGDIR, &pFetch, 0);
138224   if( rc==SQLITE_OK ){
138225     int rc2;                      /* sqlite3_reset() return code */
138226     sqlite3_bind_int64(pFetch, 1, iAbsLevel);
138227     sqlite3_bind_int(pFetch, 2, iIdx);
138228     if( SQLITE_ROW==sqlite3_step(pFetch) ){
138229       const char *aRoot = sqlite3_column_blob(pFetch, 4);
138230       int nRoot = sqlite3_column_bytes(pFetch, 4);
138231       iOldStart = sqlite3_column_int64(pFetch, 1);
138232       rc = fts3TruncateNode(aRoot, nRoot, &root, zTerm, nTerm, &iBlock);
138233     }
138234     rc2 = sqlite3_reset(pFetch);
138235     if( rc==SQLITE_OK ) rc = rc2;
138236   }
138237 
138238   while( rc==SQLITE_OK && iBlock ){
138239     char *aBlock = 0;
138240     int nBlock = 0;
138241     iNewStart = iBlock;
138242 
138243     rc = sqlite3Fts3ReadBlock(p, iBlock, &aBlock, &nBlock, 0);
138244     if( rc==SQLITE_OK ){
138245       rc = fts3TruncateNode(aBlock, nBlock, &block, zTerm, nTerm, &iBlock);
138246     }
138247     if( rc==SQLITE_OK ){
138248       rc = fts3WriteSegment(p, iNewStart, block.a, block.n);
138249     }
138250     sqlite3_free(aBlock);
138251   }
138252 
138253   /* Variable iNewStart now contains the first valid leaf node. */
138254   if( rc==SQLITE_OK && iNewStart ){
138255     sqlite3_stmt *pDel = 0;
138256     rc = fts3SqlStmt(p, SQL_DELETE_SEGMENTS_RANGE, &pDel, 0);
138257     if( rc==SQLITE_OK ){
138258       sqlite3_bind_int64(pDel, 1, iOldStart);
138259       sqlite3_bind_int64(pDel, 2, iNewStart-1);
138260       sqlite3_step(pDel);
138261       rc = sqlite3_reset(pDel);
138262     }
138263   }
138264 
138265   if( rc==SQLITE_OK ){
138266     sqlite3_stmt *pChomp = 0;
138267     rc = fts3SqlStmt(p, SQL_CHOMP_SEGDIR, &pChomp, 0);
138268     if( rc==SQLITE_OK ){
138269       sqlite3_bind_int64(pChomp, 1, iNewStart);
138270       sqlite3_bind_blob(pChomp, 2, root.a, root.n, SQLITE_STATIC);
138271       sqlite3_bind_int64(pChomp, 3, iAbsLevel);
138272       sqlite3_bind_int(pChomp, 4, iIdx);
138273       sqlite3_step(pChomp);
138274       rc = sqlite3_reset(pChomp);
138275     }
138276   }
138277 
138278   sqlite3_free(root.a);
138279   sqlite3_free(block.a);
138280   return rc;
138281 }
138282 
138283 /*
138284 ** This function is called after an incrmental-merge operation has run to
138285 ** merge (or partially merge) two or more segments from absolute level
138286 ** iAbsLevel.
138287 **
138288 ** Each input segment is either removed from the db completely (if all of
138289 ** its data was copied to the output segment by the incrmerge operation)
138290 ** or modified in place so that it no longer contains those entries that
138291 ** have been duplicated in the output segment.
138292 */
138293 static int fts3IncrmergeChomp(
138294   Fts3Table *p,                   /* FTS table handle */
138295   sqlite3_int64 iAbsLevel,        /* Absolute level containing segments */
138296   Fts3MultiSegReader *pCsr,       /* Chomp all segments opened by this cursor */
138297   int *pnRem                      /* Number of segments not deleted */
138298 ){
138299   int i;
138300   int nRem = 0;
138301   int rc = SQLITE_OK;
138302 
138303   for(i=pCsr->nSegment-1; i>=0 && rc==SQLITE_OK; i--){
138304     Fts3SegReader *pSeg = 0;
138305     int j;
138306 
138307     /* Find the Fts3SegReader object with Fts3SegReader.iIdx==i. It is hiding
138308     ** somewhere in the pCsr->apSegment[] array.  */
138309     for(j=0; ALWAYS(j<pCsr->nSegment); j++){
138310       pSeg = pCsr->apSegment[j];
138311       if( pSeg->iIdx==i ) break;
138312     }
138313     assert( j<pCsr->nSegment && pSeg->iIdx==i );
138314 
138315     if( pSeg->aNode==0 ){
138316       /* Seg-reader is at EOF. Remove the entire input segment. */
138317       rc = fts3DeleteSegment(p, pSeg);
138318       if( rc==SQLITE_OK ){
138319         rc = fts3RemoveSegdirEntry(p, iAbsLevel, pSeg->iIdx);
138320       }
138321       *pnRem = 0;
138322     }else{
138323       /* The incremental merge did not copy all the data from this
138324       ** segment to the upper level. The segment is modified in place
138325       ** so that it contains no keys smaller than zTerm/nTerm. */
138326       const char *zTerm = pSeg->zTerm;
138327       int nTerm = pSeg->nTerm;
138328       rc = fts3TruncateSegment(p, iAbsLevel, pSeg->iIdx, zTerm, nTerm);
138329       nRem++;
138330     }
138331   }
138332 
138333   if( rc==SQLITE_OK && nRem!=pCsr->nSegment ){
138334     rc = fts3RepackSegdirLevel(p, iAbsLevel);
138335   }
138336 
138337   *pnRem = nRem;
138338   return rc;
138339 }
138340 
138341 /*
138342 ** Store an incr-merge hint in the database.
138343 */
138344 static int fts3IncrmergeHintStore(Fts3Table *p, Blob *pHint){
138345   sqlite3_stmt *pReplace = 0;
138346   int rc;                         /* Return code */
138347 
138348   rc = fts3SqlStmt(p, SQL_REPLACE_STAT, &pReplace, 0);
138349   if( rc==SQLITE_OK ){
138350     sqlite3_bind_int(pReplace, 1, FTS_STAT_INCRMERGEHINT);
138351     sqlite3_bind_blob(pReplace, 2, pHint->a, pHint->n, SQLITE_STATIC);
138352     sqlite3_step(pReplace);
138353     rc = sqlite3_reset(pReplace);
138354   }
138355 
138356   return rc;
138357 }
138358 
138359 /*
138360 ** Load an incr-merge hint from the database. The incr-merge hint, if one
138361 ** exists, is stored in the rowid==1 row of the %_stat table.
138362 **
138363 ** If successful, populate blob *pHint with the value read from the %_stat
138364 ** table and return SQLITE_OK. Otherwise, if an error occurs, return an
138365 ** SQLite error code.
138366 */
138367 static int fts3IncrmergeHintLoad(Fts3Table *p, Blob *pHint){
138368   sqlite3_stmt *pSelect = 0;
138369   int rc;
138370 
138371   pHint->n = 0;
138372   rc = fts3SqlStmt(p, SQL_SELECT_STAT, &pSelect, 0);
138373   if( rc==SQLITE_OK ){
138374     int rc2;
138375     sqlite3_bind_int(pSelect, 1, FTS_STAT_INCRMERGEHINT);
138376     if( SQLITE_ROW==sqlite3_step(pSelect) ){
138377       const char *aHint = sqlite3_column_blob(pSelect, 0);
138378       int nHint = sqlite3_column_bytes(pSelect, 0);
138379       if( aHint ){
138380         blobGrowBuffer(pHint, nHint, &rc);
138381         if( rc==SQLITE_OK ){
138382           memcpy(pHint->a, aHint, nHint);
138383           pHint->n = nHint;
138384         }
138385       }
138386     }
138387     rc2 = sqlite3_reset(pSelect);
138388     if( rc==SQLITE_OK ) rc = rc2;
138389   }
138390 
138391   return rc;
138392 }
138393 
138394 /*
138395 ** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
138396 ** Otherwise, append an entry to the hint stored in blob *pHint. Each entry
138397 ** consists of two varints, the absolute level number of the input segments
138398 ** and the number of input segments.
138399 **
138400 ** If successful, leave *pRc set to SQLITE_OK and return. If an error occurs,
138401 ** set *pRc to an SQLite error code before returning.
138402 */
138403 static void fts3IncrmergeHintPush(
138404   Blob *pHint,                    /* Hint blob to append to */
138405   i64 iAbsLevel,                  /* First varint to store in hint */
138406   int nInput,                     /* Second varint to store in hint */
138407   int *pRc                        /* IN/OUT: Error code */
138408 ){
138409   blobGrowBuffer(pHint, pHint->n + 2*FTS3_VARINT_MAX, pRc);
138410   if( *pRc==SQLITE_OK ){
138411     pHint->n += sqlite3Fts3PutVarint(&pHint->a[pHint->n], iAbsLevel);
138412     pHint->n += sqlite3Fts3PutVarint(&pHint->a[pHint->n], (i64)nInput);
138413   }
138414 }
138415 
138416 /*
138417 ** Read the last entry (most recently pushed) from the hint blob *pHint
138418 ** and then remove the entry. Write the two values read to *piAbsLevel and
138419 ** *pnInput before returning.
138420 **
138421 ** If no error occurs, return SQLITE_OK. If the hint blob in *pHint does
138422 ** not contain at least two valid varints, return SQLITE_CORRUPT_VTAB.
138423 */
138424 static int fts3IncrmergeHintPop(Blob *pHint, i64 *piAbsLevel, int *pnInput){
138425   const int nHint = pHint->n;
138426   int i;
138427 
138428   i = pHint->n-2;
138429   while( i>0 && (pHint->a[i-1] & 0x80) ) i--;
138430   while( i>0 && (pHint->a[i-1] & 0x80) ) i--;
138431 
138432   pHint->n = i;
138433   i += sqlite3Fts3GetVarint(&pHint->a[i], piAbsLevel);
138434   i += fts3GetVarint32(&pHint->a[i], pnInput);
138435   if( i!=nHint ) return SQLITE_CORRUPT_VTAB;
138436 
138437   return SQLITE_OK;
138438 }
138439 
138440 
138441 /*
138442 ** Attempt an incremental merge that writes nMerge leaf blocks.
138443 **
138444 ** Incremental merges happen nMin segments at a time. The two
138445 ** segments to be merged are the nMin oldest segments (the ones with
138446 ** the smallest indexes) in the highest level that contains at least
138447 ** nMin segments. Multiple merges might occur in an attempt to write the
138448 ** quota of nMerge leaf blocks.
138449 */
138450 SQLITE_PRIVATE int sqlite3Fts3Incrmerge(Fts3Table *p, int nMerge, int nMin){
138451   int rc;                         /* Return code */
138452   int nRem = nMerge;              /* Number of leaf pages yet to  be written */
138453   Fts3MultiSegReader *pCsr;       /* Cursor used to read input data */
138454   Fts3SegFilter *pFilter;         /* Filter used with cursor pCsr */
138455   IncrmergeWriter *pWriter;       /* Writer object */
138456   int nSeg = 0;                   /* Number of input segments */
138457   sqlite3_int64 iAbsLevel = 0;    /* Absolute level number to work on */
138458   Blob hint = {0, 0, 0};          /* Hint read from %_stat table */
138459   int bDirtyHint = 0;             /* True if blob 'hint' has been modified */
138460 
138461   /* Allocate space for the cursor, filter and writer objects */
138462   const int nAlloc = sizeof(*pCsr) + sizeof(*pFilter) + sizeof(*pWriter);
138463   pWriter = (IncrmergeWriter *)sqlite3_malloc(nAlloc);
138464   if( !pWriter ) return SQLITE_NOMEM;
138465   pFilter = (Fts3SegFilter *)&pWriter[1];
138466   pCsr = (Fts3MultiSegReader *)&pFilter[1];
138467 
138468   rc = fts3IncrmergeHintLoad(p, &hint);
138469   while( rc==SQLITE_OK && nRem>0 ){
138470     const i64 nMod = FTS3_SEGDIR_MAXLEVEL * p->nIndex;
138471     sqlite3_stmt *pFindLevel = 0; /* SQL used to determine iAbsLevel */
138472     int bUseHint = 0;             /* True if attempting to append */
138473 
138474     /* Search the %_segdir table for the absolute level with the smallest
138475     ** relative level number that contains at least nMin segments, if any.
138476     ** If one is found, set iAbsLevel to the absolute level number and
138477     ** nSeg to nMin. If no level with at least nMin segments can be found,
138478     ** set nSeg to -1.
138479     */
138480     rc = fts3SqlStmt(p, SQL_FIND_MERGE_LEVEL, &pFindLevel, 0);
138481     sqlite3_bind_int(pFindLevel, 1, nMin);
138482     if( sqlite3_step(pFindLevel)==SQLITE_ROW ){
138483       iAbsLevel = sqlite3_column_int64(pFindLevel, 0);
138484       nSeg = nMin;
138485     }else{
138486       nSeg = -1;
138487     }
138488     rc = sqlite3_reset(pFindLevel);
138489 
138490     /* If the hint read from the %_stat table is not empty, check if the
138491     ** last entry in it specifies a relative level smaller than or equal
138492     ** to the level identified by the block above (if any). If so, this
138493     ** iteration of the loop will work on merging at the hinted level.
138494     */
138495     if( rc==SQLITE_OK && hint.n ){
138496       int nHint = hint.n;
138497       sqlite3_int64 iHintAbsLevel = 0;      /* Hint level */
138498       int nHintSeg = 0;                     /* Hint number of segments */
138499 
138500       rc = fts3IncrmergeHintPop(&hint, &iHintAbsLevel, &nHintSeg);
138501       if( nSeg<0 || (iAbsLevel % nMod) >= (iHintAbsLevel % nMod) ){
138502         iAbsLevel = iHintAbsLevel;
138503         nSeg = nHintSeg;
138504         bUseHint = 1;
138505         bDirtyHint = 1;
138506       }else{
138507         /* This undoes the effect of the HintPop() above - so that no entry
138508         ** is removed from the hint blob.  */
138509         hint.n = nHint;
138510       }
138511     }
138512 
138513     /* If nSeg is less that zero, then there is no level with at least
138514     ** nMin segments and no hint in the %_stat table. No work to do.
138515     ** Exit early in this case.  */
138516     if( nSeg<0 ) break;
138517 
138518     /* Open a cursor to iterate through the contents of the oldest nSeg
138519     ** indexes of absolute level iAbsLevel. If this cursor is opened using
138520     ** the 'hint' parameters, it is possible that there are less than nSeg
138521     ** segments available in level iAbsLevel. In this case, no work is
138522     ** done on iAbsLevel - fall through to the next iteration of the loop
138523     ** to start work on some other level.  */
138524     memset(pWriter, 0, nAlloc);
138525     pFilter->flags = FTS3_SEGMENT_REQUIRE_POS;
138526     if( rc==SQLITE_OK ){
138527       rc = fts3IncrmergeCsr(p, iAbsLevel, nSeg, pCsr);
138528     }
138529     if( SQLITE_OK==rc && pCsr->nSegment==nSeg
138530      && SQLITE_OK==(rc = sqlite3Fts3SegReaderStart(p, pCsr, pFilter))
138531      && SQLITE_ROW==(rc = sqlite3Fts3SegReaderStep(p, pCsr))
138532     ){
138533       int iIdx = 0;               /* Largest idx in level (iAbsLevel+1) */
138534       rc = fts3IncrmergeOutputIdx(p, iAbsLevel, &iIdx);
138535       if( rc==SQLITE_OK ){
138536         if( bUseHint && iIdx>0 ){
138537           const char *zKey = pCsr->zTerm;
138538           int nKey = pCsr->nTerm;
138539           rc = fts3IncrmergeLoad(p, iAbsLevel, iIdx-1, zKey, nKey, pWriter);
138540         }else{
138541           rc = fts3IncrmergeWriter(p, iAbsLevel, iIdx, pCsr, pWriter);
138542         }
138543       }
138544 
138545       if( rc==SQLITE_OK && pWriter->nLeafEst ){
138546         fts3LogMerge(nSeg, iAbsLevel);
138547         do {
138548           rc = fts3IncrmergeAppend(p, pWriter, pCsr);
138549           if( rc==SQLITE_OK ) rc = sqlite3Fts3SegReaderStep(p, pCsr);
138550           if( pWriter->nWork>=nRem && rc==SQLITE_ROW ) rc = SQLITE_OK;
138551         }while( rc==SQLITE_ROW );
138552 
138553         /* Update or delete the input segments */
138554         if( rc==SQLITE_OK ){
138555           nRem -= (1 + pWriter->nWork);
138556           rc = fts3IncrmergeChomp(p, iAbsLevel, pCsr, &nSeg);
138557           if( nSeg!=0 ){
138558             bDirtyHint = 1;
138559             fts3IncrmergeHintPush(&hint, iAbsLevel, nSeg, &rc);
138560           }
138561         }
138562       }
138563 
138564       fts3IncrmergeRelease(p, pWriter, &rc);
138565     }
138566 
138567     sqlite3Fts3SegReaderFinish(pCsr);
138568   }
138569 
138570   /* Write the hint values into the %_stat table for the next incr-merger */
138571   if( bDirtyHint && rc==SQLITE_OK ){
138572     rc = fts3IncrmergeHintStore(p, &hint);
138573   }
138574 
138575   sqlite3_free(pWriter);
138576   sqlite3_free(hint.a);
138577   return rc;
138578 }
138579 
138580 /*
138581 ** Convert the text beginning at *pz into an integer and return
138582 ** its value.  Advance *pz to point to the first character past
138583 ** the integer.
138584 */
138585 static int fts3Getint(const char **pz){
138586   const char *z = *pz;
138587   int i = 0;
138588   while( (*z)>='0' && (*z)<='9' ) i = 10*i + *(z++) - '0';
138589   *pz = z;
138590   return i;
138591 }
138592 
138593 /*
138594 ** Process statements of the form:
138595 **
138596 **    INSERT INTO table(table) VALUES('merge=A,B');
138597 **
138598 ** A and B are integers that decode to be the number of leaf pages
138599 ** written for the merge, and the minimum number of segments on a level
138600 ** before it will be selected for a merge, respectively.
138601 */
138602 static int fts3DoIncrmerge(
138603   Fts3Table *p,                   /* FTS3 table handle */
138604   const char *zParam              /* Nul-terminated string containing "A,B" */
138605 ){
138606   int rc;
138607   int nMin = (FTS3_MERGE_COUNT / 2);
138608   int nMerge = 0;
138609   const char *z = zParam;
138610 
138611   /* Read the first integer value */
138612   nMerge = fts3Getint(&z);
138613 
138614   /* If the first integer value is followed by a ',',  read the second
138615   ** integer value. */
138616   if( z[0]==',' && z[1]!='\0' ){
138617     z++;
138618     nMin = fts3Getint(&z);
138619   }
138620 
138621   if( z[0]!='\0' || nMin<2 ){
138622     rc = SQLITE_ERROR;
138623   }else{
138624     rc = SQLITE_OK;
138625     if( !p->bHasStat ){
138626       assert( p->bFts4==0 );
138627       sqlite3Fts3CreateStatTable(&rc, p);
138628     }
138629     if( rc==SQLITE_OK ){
138630       rc = sqlite3Fts3Incrmerge(p, nMerge, nMin);
138631     }
138632     sqlite3Fts3SegmentsClose(p);
138633   }
138634   return rc;
138635 }
138636 
138637 /*
138638 ** Process statements of the form:
138639 **
138640 **    INSERT INTO table(table) VALUES('automerge=X');
138641 **
138642 ** where X is an integer.  X==0 means to turn automerge off.  X!=0 means
138643 ** turn it on.  The setting is persistent.
138644 */
138645 static int fts3DoAutoincrmerge(
138646   Fts3Table *p,                   /* FTS3 table handle */
138647   const char *zParam              /* Nul-terminated string containing boolean */
138648 ){
138649   int rc = SQLITE_OK;
138650   sqlite3_stmt *pStmt = 0;
138651   p->bAutoincrmerge = fts3Getint(&zParam)!=0;
138652   if( !p->bHasStat ){
138653     assert( p->bFts4==0 );
138654     sqlite3Fts3CreateStatTable(&rc, p);
138655     if( rc ) return rc;
138656   }
138657   rc = fts3SqlStmt(p, SQL_REPLACE_STAT, &pStmt, 0);
138658   if( rc ) return rc;
138659   sqlite3_bind_int(pStmt, 1, FTS_STAT_AUTOINCRMERGE);
138660   sqlite3_bind_int(pStmt, 2, p->bAutoincrmerge);
138661   sqlite3_step(pStmt);
138662   rc = sqlite3_reset(pStmt);
138663   return rc;
138664 }
138665 
138666 /*
138667 ** Return a 64-bit checksum for the FTS index entry specified by the
138668 ** arguments to this function.
138669 */
138670 static u64 fts3ChecksumEntry(
138671   const char *zTerm,              /* Pointer to buffer containing term */
138672   int nTerm,                      /* Size of zTerm in bytes */
138673   int iLangid,                    /* Language id for current row */
138674   int iIndex,                     /* Index (0..Fts3Table.nIndex-1) */
138675   i64 iDocid,                     /* Docid for current row. */
138676   int iCol,                       /* Column number */
138677   int iPos                        /* Position */
138678 ){
138679   int i;
138680   u64 ret = (u64)iDocid;
138681 
138682   ret += (ret<<3) + iLangid;
138683   ret += (ret<<3) + iIndex;
138684   ret += (ret<<3) + iCol;
138685   ret += (ret<<3) + iPos;
138686   for(i=0; i<nTerm; i++) ret += (ret<<3) + zTerm[i];
138687 
138688   return ret;
138689 }
138690 
138691 /*
138692 ** Return a checksum of all entries in the FTS index that correspond to
138693 ** language id iLangid. The checksum is calculated by XORing the checksums
138694 ** of each individual entry (see fts3ChecksumEntry()) together.
138695 **
138696 ** If successful, the checksum value is returned and *pRc set to SQLITE_OK.
138697 ** Otherwise, if an error occurs, *pRc is set to an SQLite error code. The
138698 ** return value is undefined in this case.
138699 */
138700 static u64 fts3ChecksumIndex(
138701   Fts3Table *p,                   /* FTS3 table handle */
138702   int iLangid,                    /* Language id to return cksum for */
138703   int iIndex,                     /* Index to cksum (0..p->nIndex-1) */
138704   int *pRc                        /* OUT: Return code */
138705 ){
138706   Fts3SegFilter filter;
138707   Fts3MultiSegReader csr;
138708   int rc;
138709   u64 cksum = 0;
138710 
138711   assert( *pRc==SQLITE_OK );
138712 
138713   memset(&filter, 0, sizeof(filter));
138714   memset(&csr, 0, sizeof(csr));
138715   filter.flags =  FTS3_SEGMENT_REQUIRE_POS|FTS3_SEGMENT_IGNORE_EMPTY;
138716   filter.flags |= FTS3_SEGMENT_SCAN;
138717 
138718   rc = sqlite3Fts3SegReaderCursor(
138719       p, iLangid, iIndex, FTS3_SEGCURSOR_ALL, 0, 0, 0, 1,&csr
138720   );
138721   if( rc==SQLITE_OK ){
138722     rc = sqlite3Fts3SegReaderStart(p, &csr, &filter);
138723   }
138724 
138725   if( rc==SQLITE_OK ){
138726     while( SQLITE_ROW==(rc = sqlite3Fts3SegReaderStep(p, &csr)) ){
138727       char *pCsr = csr.aDoclist;
138728       char *pEnd = &pCsr[csr.nDoclist];
138729 
138730       i64 iDocid = 0;
138731       i64 iCol = 0;
138732       i64 iPos = 0;
138733 
138734       pCsr += sqlite3Fts3GetVarint(pCsr, &iDocid);
138735       while( pCsr<pEnd ){
138736         i64 iVal = 0;
138737         pCsr += sqlite3Fts3GetVarint(pCsr, &iVal);
138738         if( pCsr<pEnd ){
138739           if( iVal==0 || iVal==1 ){
138740             iCol = 0;
138741             iPos = 0;
138742             if( iVal ){
138743               pCsr += sqlite3Fts3GetVarint(pCsr, &iCol);
138744             }else{
138745               pCsr += sqlite3Fts3GetVarint(pCsr, &iVal);
138746               iDocid += iVal;
138747             }
138748           }else{
138749             iPos += (iVal - 2);
138750             cksum = cksum ^ fts3ChecksumEntry(
138751                 csr.zTerm, csr.nTerm, iLangid, iIndex, iDocid,
138752                 (int)iCol, (int)iPos
138753             );
138754           }
138755         }
138756       }
138757     }
138758   }
138759   sqlite3Fts3SegReaderFinish(&csr);
138760 
138761   *pRc = rc;
138762   return cksum;
138763 }
138764 
138765 /*
138766 ** Check if the contents of the FTS index match the current contents of the
138767 ** content table. If no error occurs and the contents do match, set *pbOk
138768 ** to true and return SQLITE_OK. Or if the contents do not match, set *pbOk
138769 ** to false before returning.
138770 **
138771 ** If an error occurs (e.g. an OOM or IO error), return an SQLite error
138772 ** code. The final value of *pbOk is undefined in this case.
138773 */
138774 static int fts3IntegrityCheck(Fts3Table *p, int *pbOk){
138775   int rc = SQLITE_OK;             /* Return code */
138776   u64 cksum1 = 0;                 /* Checksum based on FTS index contents */
138777   u64 cksum2 = 0;                 /* Checksum based on %_content contents */
138778   sqlite3_stmt *pAllLangid = 0;   /* Statement to return all language-ids */
138779 
138780   /* This block calculates the checksum according to the FTS index. */
138781   rc = fts3SqlStmt(p, SQL_SELECT_ALL_LANGID, &pAllLangid, 0);
138782   if( rc==SQLITE_OK ){
138783     int rc2;
138784     sqlite3_bind_int(pAllLangid, 1, p->nIndex);
138785     while( rc==SQLITE_OK && sqlite3_step(pAllLangid)==SQLITE_ROW ){
138786       int iLangid = sqlite3_column_int(pAllLangid, 0);
138787       int i;
138788       for(i=0; i<p->nIndex; i++){
138789         cksum1 = cksum1 ^ fts3ChecksumIndex(p, iLangid, i, &rc);
138790       }
138791     }
138792     rc2 = sqlite3_reset(pAllLangid);
138793     if( rc==SQLITE_OK ) rc = rc2;
138794   }
138795 
138796   /* This block calculates the checksum according to the %_content table */
138797   rc = fts3SqlStmt(p, SQL_SELECT_ALL_LANGID, &pAllLangid, 0);
138798   if( rc==SQLITE_OK ){
138799     sqlite3_tokenizer_module const *pModule = p->pTokenizer->pModule;
138800     sqlite3_stmt *pStmt = 0;
138801     char *zSql;
138802 
138803     zSql = sqlite3_mprintf("SELECT %s" , p->zReadExprlist);
138804     if( !zSql ){
138805       rc = SQLITE_NOMEM;
138806     }else{
138807       rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
138808       sqlite3_free(zSql);
138809     }
138810 
138811     while( rc==SQLITE_OK && SQLITE_ROW==sqlite3_step(pStmt) ){
138812       i64 iDocid = sqlite3_column_int64(pStmt, 0);
138813       int iLang = langidFromSelect(p, pStmt);
138814       int iCol;
138815 
138816       for(iCol=0; rc==SQLITE_OK && iCol<p->nColumn; iCol++){
138817         const char *zText = (const char *)sqlite3_column_text(pStmt, iCol+1);
138818         int nText = sqlite3_column_bytes(pStmt, iCol+1);
138819         sqlite3_tokenizer_cursor *pT = 0;
138820 
138821         rc = sqlite3Fts3OpenTokenizer(p->pTokenizer, iLang, zText, nText, &pT);
138822         while( rc==SQLITE_OK ){
138823           char const *zToken;       /* Buffer containing token */
138824           int nToken = 0;           /* Number of bytes in token */
138825           int iDum1 = 0, iDum2 = 0; /* Dummy variables */
138826           int iPos = 0;             /* Position of token in zText */
138827 
138828           rc = pModule->xNext(pT, &zToken, &nToken, &iDum1, &iDum2, &iPos);
138829           if( rc==SQLITE_OK ){
138830             int i;
138831             cksum2 = cksum2 ^ fts3ChecksumEntry(
138832                 zToken, nToken, iLang, 0, iDocid, iCol, iPos
138833             );
138834             for(i=1; i<p->nIndex; i++){
138835               if( p->aIndex[i].nPrefix<=nToken ){
138836                 cksum2 = cksum2 ^ fts3ChecksumEntry(
138837                   zToken, p->aIndex[i].nPrefix, iLang, i, iDocid, iCol, iPos
138838                 );
138839               }
138840             }
138841           }
138842         }
138843         if( pT ) pModule->xClose(pT);
138844         if( rc==SQLITE_DONE ) rc = SQLITE_OK;
138845       }
138846     }
138847 
138848     sqlite3_finalize(pStmt);
138849   }
138850 
138851   *pbOk = (cksum1==cksum2);
138852   return rc;
138853 }
138854 
138855 /*
138856 ** Run the integrity-check. If no error occurs and the current contents of
138857 ** the FTS index are correct, return SQLITE_OK. Or, if the contents of the
138858 ** FTS index are incorrect, return SQLITE_CORRUPT_VTAB.
138859 **
138860 ** Or, if an error (e.g. an OOM or IO error) occurs, return an SQLite
138861 ** error code.
138862 **
138863 ** The integrity-check works as follows. For each token and indexed token
138864 ** prefix in the document set, a 64-bit checksum is calculated (by code
138865 ** in fts3ChecksumEntry()) based on the following:
138866 **
138867 **     + The index number (0 for the main index, 1 for the first prefix
138868 **       index etc.),
138869 **     + The token (or token prefix) text itself,
138870 **     + The language-id of the row it appears in,
138871 **     + The docid of the row it appears in,
138872 **     + The column it appears in, and
138873 **     + The tokens position within that column.
138874 **
138875 ** The checksums for all entries in the index are XORed together to create
138876 ** a single checksum for the entire index.
138877 **
138878 ** The integrity-check code calculates the same checksum in two ways:
138879 **
138880 **     1. By scanning the contents of the FTS index, and
138881 **     2. By scanning and tokenizing the content table.
138882 **
138883 ** If the two checksums are identical, the integrity-check is deemed to have
138884 ** passed.
138885 */
138886 static int fts3DoIntegrityCheck(
138887   Fts3Table *p                    /* FTS3 table handle */
138888 ){
138889   int rc;
138890   int bOk = 0;
138891   rc = fts3IntegrityCheck(p, &bOk);
138892   if( rc==SQLITE_OK && bOk==0 ) rc = SQLITE_CORRUPT_VTAB;
138893   return rc;
138894 }
138895 
138896 /*
138897 ** Handle a 'special' INSERT of the form:
138898 **
138899 **   "INSERT INTO tbl(tbl) VALUES(<expr>)"
138900 **
138901 ** Argument pVal contains the result of <expr>. Currently the only
138902 ** meaningful value to insert is the text 'optimize'.
138903 */
138904 static int fts3SpecialInsert(Fts3Table *p, sqlite3_value *pVal){
138905   int rc;                         /* Return Code */
138906   const char *zVal = (const char *)sqlite3_value_text(pVal);
138907   int nVal = sqlite3_value_bytes(pVal);
138908 
138909   if( !zVal ){
138910     return SQLITE_NOMEM;
138911   }else if( nVal==8 && 0==sqlite3_strnicmp(zVal, "optimize", 8) ){
138912     rc = fts3DoOptimize(p, 0);
138913   }else if( nVal==7 && 0==sqlite3_strnicmp(zVal, "rebuild", 7) ){
138914     rc = fts3DoRebuild(p);
138915   }else if( nVal==15 && 0==sqlite3_strnicmp(zVal, "integrity-check", 15) ){
138916     rc = fts3DoIntegrityCheck(p);
138917   }else if( nVal>6 && 0==sqlite3_strnicmp(zVal, "merge=", 6) ){
138918     rc = fts3DoIncrmerge(p, &zVal[6]);
138919   }else if( nVal>10 && 0==sqlite3_strnicmp(zVal, "automerge=", 10) ){
138920     rc = fts3DoAutoincrmerge(p, &zVal[10]);
138921 #ifdef SQLITE_TEST
138922   }else if( nVal>9 && 0==sqlite3_strnicmp(zVal, "nodesize=", 9) ){
138923     p->nNodeSize = atoi(&zVal[9]);
138924     rc = SQLITE_OK;
138925   }else if( nVal>11 && 0==sqlite3_strnicmp(zVal, "maxpending=", 9) ){
138926     p->nMaxPendingData = atoi(&zVal[11]);
138927     rc = SQLITE_OK;
138928   }else if( nVal>21 && 0==sqlite3_strnicmp(zVal, "test-no-incr-doclist=", 21) ){
138929     p->bNoIncrDoclist = atoi(&zVal[21]);
138930     rc = SQLITE_OK;
138931 #endif
138932   }else{
138933     rc = SQLITE_ERROR;
138934   }
138935 
138936   return rc;
138937 }
138938 
138939 #ifndef SQLITE_DISABLE_FTS4_DEFERRED
138940 /*
138941 ** Delete all cached deferred doclists. Deferred doclists are cached
138942 ** (allocated) by the sqlite3Fts3CacheDeferredDoclists() function.
138943 */
138944 SQLITE_PRIVATE void sqlite3Fts3FreeDeferredDoclists(Fts3Cursor *pCsr){
138945   Fts3DeferredToken *pDef;
138946   for(pDef=pCsr->pDeferred; pDef; pDef=pDef->pNext){
138947     fts3PendingListDelete(pDef->pList);
138948     pDef->pList = 0;
138949   }
138950 }
138951 
138952 /*
138953 ** Free all entries in the pCsr->pDeffered list. Entries are added to
138954 ** this list using sqlite3Fts3DeferToken().
138955 */
138956 SQLITE_PRIVATE void sqlite3Fts3FreeDeferredTokens(Fts3Cursor *pCsr){
138957   Fts3DeferredToken *pDef;
138958   Fts3DeferredToken *pNext;
138959   for(pDef=pCsr->pDeferred; pDef; pDef=pNext){
138960     pNext = pDef->pNext;
138961     fts3PendingListDelete(pDef->pList);
138962     sqlite3_free(pDef);
138963   }
138964   pCsr->pDeferred = 0;
138965 }
138966 
138967 /*
138968 ** Generate deferred-doclists for all tokens in the pCsr->pDeferred list
138969 ** based on the row that pCsr currently points to.
138970 **
138971 ** A deferred-doclist is like any other doclist with position information
138972 ** included, except that it only contains entries for a single row of the
138973 ** table, not for all rows.
138974 */
138975 SQLITE_PRIVATE int sqlite3Fts3CacheDeferredDoclists(Fts3Cursor *pCsr){
138976   int rc = SQLITE_OK;             /* Return code */
138977   if( pCsr->pDeferred ){
138978     int i;                        /* Used to iterate through table columns */
138979     sqlite3_int64 iDocid;         /* Docid of the row pCsr points to */
138980     Fts3DeferredToken *pDef;      /* Used to iterate through deferred tokens */
138981 
138982     Fts3Table *p = (Fts3Table *)pCsr->base.pVtab;
138983     sqlite3_tokenizer *pT = p->pTokenizer;
138984     sqlite3_tokenizer_module const *pModule = pT->pModule;
138985 
138986     assert( pCsr->isRequireSeek==0 );
138987     iDocid = sqlite3_column_int64(pCsr->pStmt, 0);
138988 
138989     for(i=0; i<p->nColumn && rc==SQLITE_OK; i++){
138990       if( p->abNotindexed[i]==0 ){
138991         const char *zText = (const char *)sqlite3_column_text(pCsr->pStmt, i+1);
138992         sqlite3_tokenizer_cursor *pTC = 0;
138993 
138994         rc = sqlite3Fts3OpenTokenizer(pT, pCsr->iLangid, zText, -1, &pTC);
138995         while( rc==SQLITE_OK ){
138996           char const *zToken;       /* Buffer containing token */
138997           int nToken = 0;           /* Number of bytes in token */
138998           int iDum1 = 0, iDum2 = 0; /* Dummy variables */
138999           int iPos = 0;             /* Position of token in zText */
139000 
139001           rc = pModule->xNext(pTC, &zToken, &nToken, &iDum1, &iDum2, &iPos);
139002           for(pDef=pCsr->pDeferred; pDef && rc==SQLITE_OK; pDef=pDef->pNext){
139003             Fts3PhraseToken *pPT = pDef->pToken;
139004             if( (pDef->iCol>=p->nColumn || pDef->iCol==i)
139005                 && (pPT->bFirst==0 || iPos==0)
139006                 && (pPT->n==nToken || (pPT->isPrefix && pPT->n<nToken))
139007                 && (0==memcmp(zToken, pPT->z, pPT->n))
139008               ){
139009               fts3PendingListAppend(&pDef->pList, iDocid, i, iPos, &rc);
139010             }
139011           }
139012         }
139013         if( pTC ) pModule->xClose(pTC);
139014         if( rc==SQLITE_DONE ) rc = SQLITE_OK;
139015       }
139016     }
139017 
139018     for(pDef=pCsr->pDeferred; pDef && rc==SQLITE_OK; pDef=pDef->pNext){
139019       if( pDef->pList ){
139020         rc = fts3PendingListAppendVarint(&pDef->pList, 0);
139021       }
139022     }
139023   }
139024 
139025   return rc;
139026 }
139027 
139028 SQLITE_PRIVATE int sqlite3Fts3DeferredTokenList(
139029   Fts3DeferredToken *p,
139030   char **ppData,
139031   int *pnData
139032 ){
139033   char *pRet;
139034   int nSkip;
139035   sqlite3_int64 dummy;
139036 
139037   *ppData = 0;
139038   *pnData = 0;
139039 
139040   if( p->pList==0 ){
139041     return SQLITE_OK;
139042   }
139043 
139044   pRet = (char *)sqlite3_malloc(p->pList->nData);
139045   if( !pRet ) return SQLITE_NOMEM;
139046 
139047   nSkip = sqlite3Fts3GetVarint(p->pList->aData, &dummy);
139048   *pnData = p->pList->nData - nSkip;
139049   *ppData = pRet;
139050 
139051   memcpy(pRet, &p->pList->aData[nSkip], *pnData);
139052   return SQLITE_OK;
139053 }
139054 
139055 /*
139056 ** Add an entry for token pToken to the pCsr->pDeferred list.
139057 */
139058 SQLITE_PRIVATE int sqlite3Fts3DeferToken(
139059   Fts3Cursor *pCsr,               /* Fts3 table cursor */
139060   Fts3PhraseToken *pToken,        /* Token to defer */
139061   int iCol                        /* Column that token must appear in (or -1) */
139062 ){
139063   Fts3DeferredToken *pDeferred;
139064   pDeferred = sqlite3_malloc(sizeof(*pDeferred));
139065   if( !pDeferred ){
139066     return SQLITE_NOMEM;
139067   }
139068   memset(pDeferred, 0, sizeof(*pDeferred));
139069   pDeferred->pToken = pToken;
139070   pDeferred->pNext = pCsr->pDeferred;
139071   pDeferred->iCol = iCol;
139072   pCsr->pDeferred = pDeferred;
139073 
139074   assert( pToken->pDeferred==0 );
139075   pToken->pDeferred = pDeferred;
139076 
139077   return SQLITE_OK;
139078 }
139079 #endif
139080 
139081 /*
139082 ** SQLite value pRowid contains the rowid of a row that may or may not be
139083 ** present in the FTS3 table. If it is, delete it and adjust the contents
139084 ** of subsiduary data structures accordingly.
139085 */
139086 static int fts3DeleteByRowid(
139087   Fts3Table *p,
139088   sqlite3_value *pRowid,
139089   int *pnChng,                    /* IN/OUT: Decrement if row is deleted */
139090   u32 *aSzDel
139091 ){
139092   int rc = SQLITE_OK;             /* Return code */
139093   int bFound = 0;                 /* True if *pRowid really is in the table */
139094 
139095   fts3DeleteTerms(&rc, p, pRowid, aSzDel, &bFound);
139096   if( bFound && rc==SQLITE_OK ){
139097     int isEmpty = 0;              /* Deleting *pRowid leaves the table empty */
139098     rc = fts3IsEmpty(p, pRowid, &isEmpty);
139099     if( rc==SQLITE_OK ){
139100       if( isEmpty ){
139101         /* Deleting this row means the whole table is empty. In this case
139102         ** delete the contents of all three tables and throw away any
139103         ** data in the pendingTerms hash table.  */
139104         rc = fts3DeleteAll(p, 1);
139105         *pnChng = 0;
139106         memset(aSzDel, 0, sizeof(u32) * (p->nColumn+1) * 2);
139107       }else{
139108         *pnChng = *pnChng - 1;
139109         if( p->zContentTbl==0 ){
139110           fts3SqlExec(&rc, p, SQL_DELETE_CONTENT, &pRowid);
139111         }
139112         if( p->bHasDocsize ){
139113           fts3SqlExec(&rc, p, SQL_DELETE_DOCSIZE, &pRowid);
139114         }
139115       }
139116     }
139117   }
139118 
139119   return rc;
139120 }
139121 
139122 /*
139123 ** This function does the work for the xUpdate method of FTS3 virtual
139124 ** tables. The schema of the virtual table being:
139125 **
139126 **     CREATE TABLE <table name>(
139127 **       <user columns>,
139128 **       <table name> HIDDEN,
139129 **       docid HIDDEN,
139130 **       <langid> HIDDEN
139131 **     );
139132 **
139133 **
139134 */
139135 SQLITE_PRIVATE int sqlite3Fts3UpdateMethod(
139136   sqlite3_vtab *pVtab,            /* FTS3 vtab object */
139137   int nArg,                       /* Size of argument array */
139138   sqlite3_value **apVal,          /* Array of arguments */
139139   sqlite_int64 *pRowid            /* OUT: The affected (or effected) rowid */
139140 ){
139141   Fts3Table *p = (Fts3Table *)pVtab;
139142   int rc = SQLITE_OK;             /* Return Code */
139143   int isRemove = 0;               /* True for an UPDATE or DELETE */
139144   u32 *aSzIns = 0;                /* Sizes of inserted documents */
139145   u32 *aSzDel = 0;                /* Sizes of deleted documents */
139146   int nChng = 0;                  /* Net change in number of documents */
139147   int bInsertDone = 0;
139148 
139149   assert( p->pSegments==0 );
139150   assert(
139151       nArg==1                     /* DELETE operations */
139152    || nArg==(2 + p->nColumn + 3)  /* INSERT or UPDATE operations */
139153   );
139154 
139155   /* Check for a "special" INSERT operation. One of the form:
139156   **
139157   **   INSERT INTO xyz(xyz) VALUES('command');
139158   */
139159   if( nArg>1
139160    && sqlite3_value_type(apVal[0])==SQLITE_NULL
139161    && sqlite3_value_type(apVal[p->nColumn+2])!=SQLITE_NULL
139162   ){
139163     rc = fts3SpecialInsert(p, apVal[p->nColumn+2]);
139164     goto update_out;
139165   }
139166 
139167   if( nArg>1 && sqlite3_value_int(apVal[2 + p->nColumn + 2])<0 ){
139168     rc = SQLITE_CONSTRAINT;
139169     goto update_out;
139170   }
139171 
139172   /* Allocate space to hold the change in document sizes */
139173   aSzDel = sqlite3_malloc( sizeof(aSzDel[0])*(p->nColumn+1)*2 );
139174   if( aSzDel==0 ){
139175     rc = SQLITE_NOMEM;
139176     goto update_out;
139177   }
139178   aSzIns = &aSzDel[p->nColumn+1];
139179   memset(aSzDel, 0, sizeof(aSzDel[0])*(p->nColumn+1)*2);
139180 
139181   rc = fts3Writelock(p);
139182   if( rc!=SQLITE_OK ) goto update_out;
139183 
139184   /* If this is an INSERT operation, or an UPDATE that modifies the rowid
139185   ** value, then this operation requires constraint handling.
139186   **
139187   ** If the on-conflict mode is REPLACE, this means that the existing row
139188   ** should be deleted from the database before inserting the new row. Or,
139189   ** if the on-conflict mode is other than REPLACE, then this method must
139190   ** detect the conflict and return SQLITE_CONSTRAINT before beginning to
139191   ** modify the database file.
139192   */
139193   if( nArg>1 && p->zContentTbl==0 ){
139194     /* Find the value object that holds the new rowid value. */
139195     sqlite3_value *pNewRowid = apVal[3+p->nColumn];
139196     if( sqlite3_value_type(pNewRowid)==SQLITE_NULL ){
139197       pNewRowid = apVal[1];
139198     }
139199 
139200     if( sqlite3_value_type(pNewRowid)!=SQLITE_NULL && (
139201         sqlite3_value_type(apVal[0])==SQLITE_NULL
139202      || sqlite3_value_int64(apVal[0])!=sqlite3_value_int64(pNewRowid)
139203     )){
139204       /* The new rowid is not NULL (in this case the rowid will be
139205       ** automatically assigned and there is no chance of a conflict), and
139206       ** the statement is either an INSERT or an UPDATE that modifies the
139207       ** rowid column. So if the conflict mode is REPLACE, then delete any
139208       ** existing row with rowid=pNewRowid.
139209       **
139210       ** Or, if the conflict mode is not REPLACE, insert the new record into
139211       ** the %_content table. If we hit the duplicate rowid constraint (or any
139212       ** other error) while doing so, return immediately.
139213       **
139214       ** This branch may also run if pNewRowid contains a value that cannot
139215       ** be losslessly converted to an integer. In this case, the eventual
139216       ** call to fts3InsertData() (either just below or further on in this
139217       ** function) will return SQLITE_MISMATCH. If fts3DeleteByRowid is
139218       ** invoked, it will delete zero rows (since no row will have
139219       ** docid=$pNewRowid if $pNewRowid is not an integer value).
139220       */
139221       if( sqlite3_vtab_on_conflict(p->db)==SQLITE_REPLACE ){
139222         rc = fts3DeleteByRowid(p, pNewRowid, &nChng, aSzDel);
139223       }else{
139224         rc = fts3InsertData(p, apVal, pRowid);
139225         bInsertDone = 1;
139226       }
139227     }
139228   }
139229   if( rc!=SQLITE_OK ){
139230     goto update_out;
139231   }
139232 
139233   /* If this is a DELETE or UPDATE operation, remove the old record. */
139234   if( sqlite3_value_type(apVal[0])!=SQLITE_NULL ){
139235     assert( sqlite3_value_type(apVal[0])==SQLITE_INTEGER );
139236     rc = fts3DeleteByRowid(p, apVal[0], &nChng, aSzDel);
139237     isRemove = 1;
139238   }
139239 
139240   /* If this is an INSERT or UPDATE operation, insert the new record. */
139241   if( nArg>1 && rc==SQLITE_OK ){
139242     int iLangid = sqlite3_value_int(apVal[2 + p->nColumn + 2]);
139243     if( bInsertDone==0 ){
139244       rc = fts3InsertData(p, apVal, pRowid);
139245       if( rc==SQLITE_CONSTRAINT && p->zContentTbl==0 ){
139246         rc = FTS_CORRUPT_VTAB;
139247       }
139248     }
139249     if( rc==SQLITE_OK && (!isRemove || *pRowid!=p->iPrevDocid ) ){
139250       rc = fts3PendingTermsDocid(p, iLangid, *pRowid);
139251     }
139252     if( rc==SQLITE_OK ){
139253       assert( p->iPrevDocid==*pRowid );
139254       rc = fts3InsertTerms(p, iLangid, apVal, aSzIns);
139255     }
139256     if( p->bHasDocsize ){
139257       fts3InsertDocsize(&rc, p, aSzIns);
139258     }
139259     nChng++;
139260   }
139261 
139262   if( p->bFts4 ){
139263     fts3UpdateDocTotals(&rc, p, aSzIns, aSzDel, nChng);
139264   }
139265 
139266  update_out:
139267   sqlite3_free(aSzDel);
139268   sqlite3Fts3SegmentsClose(p);
139269   return rc;
139270 }
139271 
139272 /*
139273 ** Flush any data in the pending-terms hash table to disk. If successful,
139274 ** merge all segments in the database (including the new segment, if
139275 ** there was any data to flush) into a single segment.
139276 */
139277 SQLITE_PRIVATE int sqlite3Fts3Optimize(Fts3Table *p){
139278   int rc;
139279   rc = sqlite3_exec(p->db, "SAVEPOINT fts3", 0, 0, 0);
139280   if( rc==SQLITE_OK ){
139281     rc = fts3DoOptimize(p, 1);
139282     if( rc==SQLITE_OK || rc==SQLITE_DONE ){
139283       int rc2 = sqlite3_exec(p->db, "RELEASE fts3", 0, 0, 0);
139284       if( rc2!=SQLITE_OK ) rc = rc2;
139285     }else{
139286       sqlite3_exec(p->db, "ROLLBACK TO fts3", 0, 0, 0);
139287       sqlite3_exec(p->db, "RELEASE fts3", 0, 0, 0);
139288     }
139289   }
139290   sqlite3Fts3SegmentsClose(p);
139291   return rc;
139292 }
139293 
139294 #endif
139295 
139296 /************** End of fts3_write.c ******************************************/
139297 /************** Begin file fts3_snippet.c ************************************/
139298 /*
139299 ** 2009 Oct 23
139300 **
139301 ** The author disclaims copyright to this source code.  In place of
139302 ** a legal notice, here is a blessing:
139303 **
139304 **    May you do good and not evil.
139305 **    May you find forgiveness for yourself and forgive others.
139306 **    May you share freely, never taking more than you give.
139307 **
139308 ******************************************************************************
139309 */
139310 
139311 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
139312 
139313 /* #include <string.h> */
139314 /* #include <assert.h> */
139315 
139316 /*
139317 ** Characters that may appear in the second argument to matchinfo().
139318 */
139319 #define FTS3_MATCHINFO_NPHRASE   'p'        /* 1 value */
139320 #define FTS3_MATCHINFO_NCOL      'c'        /* 1 value */
139321 #define FTS3_MATCHINFO_NDOC      'n'        /* 1 value */
139322 #define FTS3_MATCHINFO_AVGLENGTH 'a'        /* nCol values */
139323 #define FTS3_MATCHINFO_LENGTH    'l'        /* nCol values */
139324 #define FTS3_MATCHINFO_LCS       's'        /* nCol values */
139325 #define FTS3_MATCHINFO_HITS      'x'        /* 3*nCol*nPhrase values */
139326 
139327 /*
139328 ** The default value for the second argument to matchinfo().
139329 */
139330 #define FTS3_MATCHINFO_DEFAULT   "pcx"
139331 
139332 
139333 /*
139334 ** Used as an fts3ExprIterate() context when loading phrase doclists to
139335 ** Fts3Expr.aDoclist[]/nDoclist.
139336 */
139337 typedef struct LoadDoclistCtx LoadDoclistCtx;
139338 struct LoadDoclistCtx {
139339   Fts3Cursor *pCsr;               /* FTS3 Cursor */
139340   int nPhrase;                    /* Number of phrases seen so far */
139341   int nToken;                     /* Number of tokens seen so far */
139342 };
139343 
139344 /*
139345 ** The following types are used as part of the implementation of the
139346 ** fts3BestSnippet() routine.
139347 */
139348 typedef struct SnippetIter SnippetIter;
139349 typedef struct SnippetPhrase SnippetPhrase;
139350 typedef struct SnippetFragment SnippetFragment;
139351 
139352 struct SnippetIter {
139353   Fts3Cursor *pCsr;               /* Cursor snippet is being generated from */
139354   int iCol;                       /* Extract snippet from this column */
139355   int nSnippet;                   /* Requested snippet length (in tokens) */
139356   int nPhrase;                    /* Number of phrases in query */
139357   SnippetPhrase *aPhrase;         /* Array of size nPhrase */
139358   int iCurrent;                   /* First token of current snippet */
139359 };
139360 
139361 struct SnippetPhrase {
139362   int nToken;                     /* Number of tokens in phrase */
139363   char *pList;                    /* Pointer to start of phrase position list */
139364   int iHead;                      /* Next value in position list */
139365   char *pHead;                    /* Position list data following iHead */
139366   int iTail;                      /* Next value in trailing position list */
139367   char *pTail;                    /* Position list data following iTail */
139368 };
139369 
139370 struct SnippetFragment {
139371   int iCol;                       /* Column snippet is extracted from */
139372   int iPos;                       /* Index of first token in snippet */
139373   u64 covered;                    /* Mask of query phrases covered */
139374   u64 hlmask;                     /* Mask of snippet terms to highlight */
139375 };
139376 
139377 /*
139378 ** This type is used as an fts3ExprIterate() context object while
139379 ** accumulating the data returned by the matchinfo() function.
139380 */
139381 typedef struct MatchInfo MatchInfo;
139382 struct MatchInfo {
139383   Fts3Cursor *pCursor;            /* FTS3 Cursor */
139384   int nCol;                       /* Number of columns in table */
139385   int nPhrase;                    /* Number of matchable phrases in query */
139386   sqlite3_int64 nDoc;             /* Number of docs in database */
139387   u32 *aMatchinfo;                /* Pre-allocated buffer */
139388 };
139389 
139390 
139391 
139392 /*
139393 ** The snippet() and offsets() functions both return text values. An instance
139394 ** of the following structure is used to accumulate those values while the
139395 ** functions are running. See fts3StringAppend() for details.
139396 */
139397 typedef struct StrBuffer StrBuffer;
139398 struct StrBuffer {
139399   char *z;                        /* Pointer to buffer containing string */
139400   int n;                          /* Length of z in bytes (excl. nul-term) */
139401   int nAlloc;                     /* Allocated size of buffer z in bytes */
139402 };
139403 
139404 
139405 /*
139406 ** This function is used to help iterate through a position-list. A position
139407 ** list is a list of unique integers, sorted from smallest to largest. Each
139408 ** element of the list is represented by an FTS3 varint that takes the value
139409 ** of the difference between the current element and the previous one plus
139410 ** two. For example, to store the position-list:
139411 **
139412 **     4 9 113
139413 **
139414 ** the three varints:
139415 **
139416 **     6 7 106
139417 **
139418 ** are encoded.
139419 **
139420 ** When this function is called, *pp points to the start of an element of
139421 ** the list. *piPos contains the value of the previous entry in the list.
139422 ** After it returns, *piPos contains the value of the next element of the
139423 ** list and *pp is advanced to the following varint.
139424 */
139425 static void fts3GetDeltaPosition(char **pp, int *piPos){
139426   int iVal;
139427   *pp += fts3GetVarint32(*pp, &iVal);
139428   *piPos += (iVal-2);
139429 }
139430 
139431 /*
139432 ** Helper function for fts3ExprIterate() (see below).
139433 */
139434 static int fts3ExprIterate2(
139435   Fts3Expr *pExpr,                /* Expression to iterate phrases of */
139436   int *piPhrase,                  /* Pointer to phrase counter */
139437   int (*x)(Fts3Expr*,int,void*),  /* Callback function to invoke for phrases */
139438   void *pCtx                      /* Second argument to pass to callback */
139439 ){
139440   int rc;                         /* Return code */
139441   int eType = pExpr->eType;       /* Type of expression node pExpr */
139442 
139443   if( eType!=FTSQUERY_PHRASE ){
139444     assert( pExpr->pLeft && pExpr->pRight );
139445     rc = fts3ExprIterate2(pExpr->pLeft, piPhrase, x, pCtx);
139446     if( rc==SQLITE_OK && eType!=FTSQUERY_NOT ){
139447       rc = fts3ExprIterate2(pExpr->pRight, piPhrase, x, pCtx);
139448     }
139449   }else{
139450     rc = x(pExpr, *piPhrase, pCtx);
139451     (*piPhrase)++;
139452   }
139453   return rc;
139454 }
139455 
139456 /*
139457 ** Iterate through all phrase nodes in an FTS3 query, except those that
139458 ** are part of a sub-tree that is the right-hand-side of a NOT operator.
139459 ** For each phrase node found, the supplied callback function is invoked.
139460 **
139461 ** If the callback function returns anything other than SQLITE_OK,
139462 ** the iteration is abandoned and the error code returned immediately.
139463 ** Otherwise, SQLITE_OK is returned after a callback has been made for
139464 ** all eligible phrase nodes.
139465 */
139466 static int fts3ExprIterate(
139467   Fts3Expr *pExpr,                /* Expression to iterate phrases of */
139468   int (*x)(Fts3Expr*,int,void*),  /* Callback function to invoke for phrases */
139469   void *pCtx                      /* Second argument to pass to callback */
139470 ){
139471   int iPhrase = 0;                /* Variable used as the phrase counter */
139472   return fts3ExprIterate2(pExpr, &iPhrase, x, pCtx);
139473 }
139474 
139475 /*
139476 ** This is an fts3ExprIterate() callback used while loading the doclists
139477 ** for each phrase into Fts3Expr.aDoclist[]/nDoclist. See also
139478 ** fts3ExprLoadDoclists().
139479 */
139480 static int fts3ExprLoadDoclistsCb(Fts3Expr *pExpr, int iPhrase, void *ctx){
139481   int rc = SQLITE_OK;
139482   Fts3Phrase *pPhrase = pExpr->pPhrase;
139483   LoadDoclistCtx *p = (LoadDoclistCtx *)ctx;
139484 
139485   UNUSED_PARAMETER(iPhrase);
139486 
139487   p->nPhrase++;
139488   p->nToken += pPhrase->nToken;
139489 
139490   return rc;
139491 }
139492 
139493 /*
139494 ** Load the doclists for each phrase in the query associated with FTS3 cursor
139495 ** pCsr.
139496 **
139497 ** If pnPhrase is not NULL, then *pnPhrase is set to the number of matchable
139498 ** phrases in the expression (all phrases except those directly or
139499 ** indirectly descended from the right-hand-side of a NOT operator). If
139500 ** pnToken is not NULL, then it is set to the number of tokens in all
139501 ** matchable phrases of the expression.
139502 */
139503 static int fts3ExprLoadDoclists(
139504   Fts3Cursor *pCsr,               /* Fts3 cursor for current query */
139505   int *pnPhrase,                  /* OUT: Number of phrases in query */
139506   int *pnToken                    /* OUT: Number of tokens in query */
139507 ){
139508   int rc;                         /* Return Code */
139509   LoadDoclistCtx sCtx = {0,0,0};  /* Context for fts3ExprIterate() */
139510   sCtx.pCsr = pCsr;
139511   rc = fts3ExprIterate(pCsr->pExpr, fts3ExprLoadDoclistsCb, (void *)&sCtx);
139512   if( pnPhrase ) *pnPhrase = sCtx.nPhrase;
139513   if( pnToken ) *pnToken = sCtx.nToken;
139514   return rc;
139515 }
139516 
139517 static int fts3ExprPhraseCountCb(Fts3Expr *pExpr, int iPhrase, void *ctx){
139518   (*(int *)ctx)++;
139519   UNUSED_PARAMETER(pExpr);
139520   UNUSED_PARAMETER(iPhrase);
139521   return SQLITE_OK;
139522 }
139523 static int fts3ExprPhraseCount(Fts3Expr *pExpr){
139524   int nPhrase = 0;
139525   (void)fts3ExprIterate(pExpr, fts3ExprPhraseCountCb, (void *)&nPhrase);
139526   return nPhrase;
139527 }
139528 
139529 /*
139530 ** Advance the position list iterator specified by the first two
139531 ** arguments so that it points to the first element with a value greater
139532 ** than or equal to parameter iNext.
139533 */
139534 static void fts3SnippetAdvance(char **ppIter, int *piIter, int iNext){
139535   char *pIter = *ppIter;
139536   if( pIter ){
139537     int iIter = *piIter;
139538 
139539     while( iIter<iNext ){
139540       if( 0==(*pIter & 0xFE) ){
139541         iIter = -1;
139542         pIter = 0;
139543         break;
139544       }
139545       fts3GetDeltaPosition(&pIter, &iIter);
139546     }
139547 
139548     *piIter = iIter;
139549     *ppIter = pIter;
139550   }
139551 }
139552 
139553 /*
139554 ** Advance the snippet iterator to the next candidate snippet.
139555 */
139556 static int fts3SnippetNextCandidate(SnippetIter *pIter){
139557   int i;                          /* Loop counter */
139558 
139559   if( pIter->iCurrent<0 ){
139560     /* The SnippetIter object has just been initialized. The first snippet
139561     ** candidate always starts at offset 0 (even if this candidate has a
139562     ** score of 0.0).
139563     */
139564     pIter->iCurrent = 0;
139565 
139566     /* Advance the 'head' iterator of each phrase to the first offset that
139567     ** is greater than or equal to (iNext+nSnippet).
139568     */
139569     for(i=0; i<pIter->nPhrase; i++){
139570       SnippetPhrase *pPhrase = &pIter->aPhrase[i];
139571       fts3SnippetAdvance(&pPhrase->pHead, &pPhrase->iHead, pIter->nSnippet);
139572     }
139573   }else{
139574     int iStart;
139575     int iEnd = 0x7FFFFFFF;
139576 
139577     for(i=0; i<pIter->nPhrase; i++){
139578       SnippetPhrase *pPhrase = &pIter->aPhrase[i];
139579       if( pPhrase->pHead && pPhrase->iHead<iEnd ){
139580         iEnd = pPhrase->iHead;
139581       }
139582     }
139583     if( iEnd==0x7FFFFFFF ){
139584       return 1;
139585     }
139586 
139587     pIter->iCurrent = iStart = iEnd - pIter->nSnippet + 1;
139588     for(i=0; i<pIter->nPhrase; i++){
139589       SnippetPhrase *pPhrase = &pIter->aPhrase[i];
139590       fts3SnippetAdvance(&pPhrase->pHead, &pPhrase->iHead, iEnd+1);
139591       fts3SnippetAdvance(&pPhrase->pTail, &pPhrase->iTail, iStart);
139592     }
139593   }
139594 
139595   return 0;
139596 }
139597 
139598 /*
139599 ** Retrieve information about the current candidate snippet of snippet
139600 ** iterator pIter.
139601 */
139602 static void fts3SnippetDetails(
139603   SnippetIter *pIter,             /* Snippet iterator */
139604   u64 mCovered,                   /* Bitmask of phrases already covered */
139605   int *piToken,                   /* OUT: First token of proposed snippet */
139606   int *piScore,                   /* OUT: "Score" for this snippet */
139607   u64 *pmCover,                   /* OUT: Bitmask of phrases covered */
139608   u64 *pmHighlight                /* OUT: Bitmask of terms to highlight */
139609 ){
139610   int iStart = pIter->iCurrent;   /* First token of snippet */
139611   int iScore = 0;                 /* Score of this snippet */
139612   int i;                          /* Loop counter */
139613   u64 mCover = 0;                 /* Mask of phrases covered by this snippet */
139614   u64 mHighlight = 0;             /* Mask of tokens to highlight in snippet */
139615 
139616   for(i=0; i<pIter->nPhrase; i++){
139617     SnippetPhrase *pPhrase = &pIter->aPhrase[i];
139618     if( pPhrase->pTail ){
139619       char *pCsr = pPhrase->pTail;
139620       int iCsr = pPhrase->iTail;
139621 
139622       while( iCsr<(iStart+pIter->nSnippet) ){
139623         int j;
139624         u64 mPhrase = (u64)1 << i;
139625         u64 mPos = (u64)1 << (iCsr - iStart);
139626         assert( iCsr>=iStart );
139627         if( (mCover|mCovered)&mPhrase ){
139628           iScore++;
139629         }else{
139630           iScore += 1000;
139631         }
139632         mCover |= mPhrase;
139633 
139634         for(j=0; j<pPhrase->nToken; j++){
139635           mHighlight |= (mPos>>j);
139636         }
139637 
139638         if( 0==(*pCsr & 0x0FE) ) break;
139639         fts3GetDeltaPosition(&pCsr, &iCsr);
139640       }
139641     }
139642   }
139643 
139644   /* Set the output variables before returning. */
139645   *piToken = iStart;
139646   *piScore = iScore;
139647   *pmCover = mCover;
139648   *pmHighlight = mHighlight;
139649 }
139650 
139651 /*
139652 ** This function is an fts3ExprIterate() callback used by fts3BestSnippet().
139653 ** Each invocation populates an element of the SnippetIter.aPhrase[] array.
139654 */
139655 static int fts3SnippetFindPositions(Fts3Expr *pExpr, int iPhrase, void *ctx){
139656   SnippetIter *p = (SnippetIter *)ctx;
139657   SnippetPhrase *pPhrase = &p->aPhrase[iPhrase];
139658   char *pCsr;
139659   int rc;
139660 
139661   pPhrase->nToken = pExpr->pPhrase->nToken;
139662   rc = sqlite3Fts3EvalPhrasePoslist(p->pCsr, pExpr, p->iCol, &pCsr);
139663   assert( rc==SQLITE_OK || pCsr==0 );
139664   if( pCsr ){
139665     int iFirst = 0;
139666     pPhrase->pList = pCsr;
139667     fts3GetDeltaPosition(&pCsr, &iFirst);
139668     assert( iFirst>=0 );
139669     pPhrase->pHead = pCsr;
139670     pPhrase->pTail = pCsr;
139671     pPhrase->iHead = iFirst;
139672     pPhrase->iTail = iFirst;
139673   }else{
139674     assert( rc!=SQLITE_OK || (
139675        pPhrase->pList==0 && pPhrase->pHead==0 && pPhrase->pTail==0
139676     ));
139677   }
139678 
139679   return rc;
139680 }
139681 
139682 /*
139683 ** Select the fragment of text consisting of nFragment contiguous tokens
139684 ** from column iCol that represent the "best" snippet. The best snippet
139685 ** is the snippet with the highest score, where scores are calculated
139686 ** by adding:
139687 **
139688 **   (a) +1 point for each occurrence of a matchable phrase in the snippet.
139689 **
139690 **   (b) +1000 points for the first occurrence of each matchable phrase in
139691 **       the snippet for which the corresponding mCovered bit is not set.
139692 **
139693 ** The selected snippet parameters are stored in structure *pFragment before
139694 ** returning. The score of the selected snippet is stored in *piScore
139695 ** before returning.
139696 */
139697 static int fts3BestSnippet(
139698   int nSnippet,                   /* Desired snippet length */
139699   Fts3Cursor *pCsr,               /* Cursor to create snippet for */
139700   int iCol,                       /* Index of column to create snippet from */
139701   u64 mCovered,                   /* Mask of phrases already covered */
139702   u64 *pmSeen,                    /* IN/OUT: Mask of phrases seen */
139703   SnippetFragment *pFragment,     /* OUT: Best snippet found */
139704   int *piScore                    /* OUT: Score of snippet pFragment */
139705 ){
139706   int rc;                         /* Return Code */
139707   int nList;                      /* Number of phrases in expression */
139708   SnippetIter sIter;              /* Iterates through snippet candidates */
139709   int nByte;                      /* Number of bytes of space to allocate */
139710   int iBestScore = -1;            /* Best snippet score found so far */
139711   int i;                          /* Loop counter */
139712 
139713   memset(&sIter, 0, sizeof(sIter));
139714 
139715   /* Iterate through the phrases in the expression to count them. The same
139716   ** callback makes sure the doclists are loaded for each phrase.
139717   */
139718   rc = fts3ExprLoadDoclists(pCsr, &nList, 0);
139719   if( rc!=SQLITE_OK ){
139720     return rc;
139721   }
139722 
139723   /* Now that it is known how many phrases there are, allocate and zero
139724   ** the required space using malloc().
139725   */
139726   nByte = sizeof(SnippetPhrase) * nList;
139727   sIter.aPhrase = (SnippetPhrase *)sqlite3_malloc(nByte);
139728   if( !sIter.aPhrase ){
139729     return SQLITE_NOMEM;
139730   }
139731   memset(sIter.aPhrase, 0, nByte);
139732 
139733   /* Initialize the contents of the SnippetIter object. Then iterate through
139734   ** the set of phrases in the expression to populate the aPhrase[] array.
139735   */
139736   sIter.pCsr = pCsr;
139737   sIter.iCol = iCol;
139738   sIter.nSnippet = nSnippet;
139739   sIter.nPhrase = nList;
139740   sIter.iCurrent = -1;
139741   (void)fts3ExprIterate(pCsr->pExpr, fts3SnippetFindPositions, (void *)&sIter);
139742 
139743   /* Set the *pmSeen output variable. */
139744   for(i=0; i<nList; i++){
139745     if( sIter.aPhrase[i].pHead ){
139746       *pmSeen |= (u64)1 << i;
139747     }
139748   }
139749 
139750   /* Loop through all candidate snippets. Store the best snippet in
139751   ** *pFragment. Store its associated 'score' in iBestScore.
139752   */
139753   pFragment->iCol = iCol;
139754   while( !fts3SnippetNextCandidate(&sIter) ){
139755     int iPos;
139756     int iScore;
139757     u64 mCover;
139758     u64 mHighlight;
139759     fts3SnippetDetails(&sIter, mCovered, &iPos, &iScore, &mCover, &mHighlight);
139760     assert( iScore>=0 );
139761     if( iScore>iBestScore ){
139762       pFragment->iPos = iPos;
139763       pFragment->hlmask = mHighlight;
139764       pFragment->covered = mCover;
139765       iBestScore = iScore;
139766     }
139767   }
139768 
139769   sqlite3_free(sIter.aPhrase);
139770   *piScore = iBestScore;
139771   return SQLITE_OK;
139772 }
139773 
139774 
139775 /*
139776 ** Append a string to the string-buffer passed as the first argument.
139777 **
139778 ** If nAppend is negative, then the length of the string zAppend is
139779 ** determined using strlen().
139780 */
139781 static int fts3StringAppend(
139782   StrBuffer *pStr,                /* Buffer to append to */
139783   const char *zAppend,            /* Pointer to data to append to buffer */
139784   int nAppend                     /* Size of zAppend in bytes (or -1) */
139785 ){
139786   if( nAppend<0 ){
139787     nAppend = (int)strlen(zAppend);
139788   }
139789 
139790   /* If there is insufficient space allocated at StrBuffer.z, use realloc()
139791   ** to grow the buffer until so that it is big enough to accomadate the
139792   ** appended data.
139793   */
139794   if( pStr->n+nAppend+1>=pStr->nAlloc ){
139795     int nAlloc = pStr->nAlloc+nAppend+100;
139796     char *zNew = sqlite3_realloc(pStr->z, nAlloc);
139797     if( !zNew ){
139798       return SQLITE_NOMEM;
139799     }
139800     pStr->z = zNew;
139801     pStr->nAlloc = nAlloc;
139802   }
139803   assert( pStr->z!=0 && (pStr->nAlloc >= pStr->n+nAppend+1) );
139804 
139805   /* Append the data to the string buffer. */
139806   memcpy(&pStr->z[pStr->n], zAppend, nAppend);
139807   pStr->n += nAppend;
139808   pStr->z[pStr->n] = '\0';
139809 
139810   return SQLITE_OK;
139811 }
139812 
139813 /*
139814 ** The fts3BestSnippet() function often selects snippets that end with a
139815 ** query term. That is, the final term of the snippet is always a term
139816 ** that requires highlighting. For example, if 'X' is a highlighted term
139817 ** and '.' is a non-highlighted term, BestSnippet() may select:
139818 **
139819 **     ........X.....X
139820 **
139821 ** This function "shifts" the beginning of the snippet forward in the
139822 ** document so that there are approximately the same number of
139823 ** non-highlighted terms to the right of the final highlighted term as there
139824 ** are to the left of the first highlighted term. For example, to this:
139825 **
139826 **     ....X.....X....
139827 **
139828 ** This is done as part of extracting the snippet text, not when selecting
139829 ** the snippet. Snippet selection is done based on doclists only, so there
139830 ** is no way for fts3BestSnippet() to know whether or not the document
139831 ** actually contains terms that follow the final highlighted term.
139832 */
139833 static int fts3SnippetShift(
139834   Fts3Table *pTab,                /* FTS3 table snippet comes from */
139835   int iLangid,                    /* Language id to use in tokenizing */
139836   int nSnippet,                   /* Number of tokens desired for snippet */
139837   const char *zDoc,               /* Document text to extract snippet from */
139838   int nDoc,                       /* Size of buffer zDoc in bytes */
139839   int *piPos,                     /* IN/OUT: First token of snippet */
139840   u64 *pHlmask                    /* IN/OUT: Mask of tokens to highlight */
139841 ){
139842   u64 hlmask = *pHlmask;          /* Local copy of initial highlight-mask */
139843 
139844   if( hlmask ){
139845     int nLeft;                    /* Tokens to the left of first highlight */
139846     int nRight;                   /* Tokens to the right of last highlight */
139847     int nDesired;                 /* Ideal number of tokens to shift forward */
139848 
139849     for(nLeft=0; !(hlmask & ((u64)1 << nLeft)); nLeft++);
139850     for(nRight=0; !(hlmask & ((u64)1 << (nSnippet-1-nRight))); nRight++);
139851     nDesired = (nLeft-nRight)/2;
139852 
139853     /* Ideally, the start of the snippet should be pushed forward in the
139854     ** document nDesired tokens. This block checks if there are actually
139855     ** nDesired tokens to the right of the snippet. If so, *piPos and
139856     ** *pHlMask are updated to shift the snippet nDesired tokens to the
139857     ** right. Otherwise, the snippet is shifted by the number of tokens
139858     ** available.
139859     */
139860     if( nDesired>0 ){
139861       int nShift;                 /* Number of tokens to shift snippet by */
139862       int iCurrent = 0;           /* Token counter */
139863       int rc;                     /* Return Code */
139864       sqlite3_tokenizer_module *pMod;
139865       sqlite3_tokenizer_cursor *pC;
139866       pMod = (sqlite3_tokenizer_module *)pTab->pTokenizer->pModule;
139867 
139868       /* Open a cursor on zDoc/nDoc. Check if there are (nSnippet+nDesired)
139869       ** or more tokens in zDoc/nDoc.
139870       */
139871       rc = sqlite3Fts3OpenTokenizer(pTab->pTokenizer, iLangid, zDoc, nDoc, &pC);
139872       if( rc!=SQLITE_OK ){
139873         return rc;
139874       }
139875       while( rc==SQLITE_OK && iCurrent<(nSnippet+nDesired) ){
139876         const char *ZDUMMY; int DUMMY1 = 0, DUMMY2 = 0, DUMMY3 = 0;
139877         rc = pMod->xNext(pC, &ZDUMMY, &DUMMY1, &DUMMY2, &DUMMY3, &iCurrent);
139878       }
139879       pMod->xClose(pC);
139880       if( rc!=SQLITE_OK && rc!=SQLITE_DONE ){ return rc; }
139881 
139882       nShift = (rc==SQLITE_DONE)+iCurrent-nSnippet;
139883       assert( nShift<=nDesired );
139884       if( nShift>0 ){
139885         *piPos += nShift;
139886         *pHlmask = hlmask >> nShift;
139887       }
139888     }
139889   }
139890   return SQLITE_OK;
139891 }
139892 
139893 /*
139894 ** Extract the snippet text for fragment pFragment from cursor pCsr and
139895 ** append it to string buffer pOut.
139896 */
139897 static int fts3SnippetText(
139898   Fts3Cursor *pCsr,               /* FTS3 Cursor */
139899   SnippetFragment *pFragment,     /* Snippet to extract */
139900   int iFragment,                  /* Fragment number */
139901   int isLast,                     /* True for final fragment in snippet */
139902   int nSnippet,                   /* Number of tokens in extracted snippet */
139903   const char *zOpen,              /* String inserted before highlighted term */
139904   const char *zClose,             /* String inserted after highlighted term */
139905   const char *zEllipsis,          /* String inserted between snippets */
139906   StrBuffer *pOut                 /* Write output here */
139907 ){
139908   Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
139909   int rc;                         /* Return code */
139910   const char *zDoc;               /* Document text to extract snippet from */
139911   int nDoc;                       /* Size of zDoc in bytes */
139912   int iCurrent = 0;               /* Current token number of document */
139913   int iEnd = 0;                   /* Byte offset of end of current token */
139914   int isShiftDone = 0;            /* True after snippet is shifted */
139915   int iPos = pFragment->iPos;     /* First token of snippet */
139916   u64 hlmask = pFragment->hlmask; /* Highlight-mask for snippet */
139917   int iCol = pFragment->iCol+1;   /* Query column to extract text from */
139918   sqlite3_tokenizer_module *pMod; /* Tokenizer module methods object */
139919   sqlite3_tokenizer_cursor *pC;   /* Tokenizer cursor open on zDoc/nDoc */
139920 
139921   zDoc = (const char *)sqlite3_column_text(pCsr->pStmt, iCol);
139922   if( zDoc==0 ){
139923     if( sqlite3_column_type(pCsr->pStmt, iCol)!=SQLITE_NULL ){
139924       return SQLITE_NOMEM;
139925     }
139926     return SQLITE_OK;
139927   }
139928   nDoc = sqlite3_column_bytes(pCsr->pStmt, iCol);
139929 
139930   /* Open a token cursor on the document. */
139931   pMod = (sqlite3_tokenizer_module *)pTab->pTokenizer->pModule;
139932   rc = sqlite3Fts3OpenTokenizer(pTab->pTokenizer, pCsr->iLangid, zDoc,nDoc,&pC);
139933   if( rc!=SQLITE_OK ){
139934     return rc;
139935   }
139936 
139937   while( rc==SQLITE_OK ){
139938     const char *ZDUMMY;           /* Dummy argument used with tokenizer */
139939     int DUMMY1 = -1;              /* Dummy argument used with tokenizer */
139940     int iBegin = 0;               /* Offset in zDoc of start of token */
139941     int iFin = 0;                 /* Offset in zDoc of end of token */
139942     int isHighlight = 0;          /* True for highlighted terms */
139943 
139944     /* Variable DUMMY1 is initialized to a negative value above. Elsewhere
139945     ** in the FTS code the variable that the third argument to xNext points to
139946     ** is initialized to zero before the first (*but not necessarily
139947     ** subsequent*) call to xNext(). This is done for a particular application
139948     ** that needs to know whether or not the tokenizer is being used for
139949     ** snippet generation or for some other purpose.
139950     **
139951     ** Extreme care is required when writing code to depend on this
139952     ** initialization. It is not a documented part of the tokenizer interface.
139953     ** If a tokenizer is used directly by any code outside of FTS, this
139954     ** convention might not be respected.  */
139955     rc = pMod->xNext(pC, &ZDUMMY, &DUMMY1, &iBegin, &iFin, &iCurrent);
139956     if( rc!=SQLITE_OK ){
139957       if( rc==SQLITE_DONE ){
139958         /* Special case - the last token of the snippet is also the last token
139959         ** of the column. Append any punctuation that occurred between the end
139960         ** of the previous token and the end of the document to the output.
139961         ** Then break out of the loop. */
139962         rc = fts3StringAppend(pOut, &zDoc[iEnd], -1);
139963       }
139964       break;
139965     }
139966     if( iCurrent<iPos ){ continue; }
139967 
139968     if( !isShiftDone ){
139969       int n = nDoc - iBegin;
139970       rc = fts3SnippetShift(
139971           pTab, pCsr->iLangid, nSnippet, &zDoc[iBegin], n, &iPos, &hlmask
139972       );
139973       isShiftDone = 1;
139974 
139975       /* Now that the shift has been done, check if the initial "..." are
139976       ** required. They are required if (a) this is not the first fragment,
139977       ** or (b) this fragment does not begin at position 0 of its column.
139978       */
139979       if( rc==SQLITE_OK && (iPos>0 || iFragment>0) ){
139980         rc = fts3StringAppend(pOut, zEllipsis, -1);
139981       }
139982       if( rc!=SQLITE_OK || iCurrent<iPos ) continue;
139983     }
139984 
139985     if( iCurrent>=(iPos+nSnippet) ){
139986       if( isLast ){
139987         rc = fts3StringAppend(pOut, zEllipsis, -1);
139988       }
139989       break;
139990     }
139991 
139992     /* Set isHighlight to true if this term should be highlighted. */
139993     isHighlight = (hlmask & ((u64)1 << (iCurrent-iPos)))!=0;
139994 
139995     if( iCurrent>iPos ) rc = fts3StringAppend(pOut, &zDoc[iEnd], iBegin-iEnd);
139996     if( rc==SQLITE_OK && isHighlight ) rc = fts3StringAppend(pOut, zOpen, -1);
139997     if( rc==SQLITE_OK ) rc = fts3StringAppend(pOut, &zDoc[iBegin], iFin-iBegin);
139998     if( rc==SQLITE_OK && isHighlight ) rc = fts3StringAppend(pOut, zClose, -1);
139999 
140000     iEnd = iFin;
140001   }
140002 
140003   pMod->xClose(pC);
140004   return rc;
140005 }
140006 
140007 
140008 /*
140009 ** This function is used to count the entries in a column-list (a
140010 ** delta-encoded list of term offsets within a single column of a single
140011 ** row). When this function is called, *ppCollist should point to the
140012 ** beginning of the first varint in the column-list (the varint that
140013 ** contains the position of the first matching term in the column data).
140014 ** Before returning, *ppCollist is set to point to the first byte after
140015 ** the last varint in the column-list (either the 0x00 signifying the end
140016 ** of the position-list, or the 0x01 that precedes the column number of
140017 ** the next column in the position-list).
140018 **
140019 ** The number of elements in the column-list is returned.
140020 */
140021 static int fts3ColumnlistCount(char **ppCollist){
140022   char *pEnd = *ppCollist;
140023   char c = 0;
140024   int nEntry = 0;
140025 
140026   /* A column-list is terminated by either a 0x01 or 0x00. */
140027   while( 0xFE & (*pEnd | c) ){
140028     c = *pEnd++ & 0x80;
140029     if( !c ) nEntry++;
140030   }
140031 
140032   *ppCollist = pEnd;
140033   return nEntry;
140034 }
140035 
140036 /*
140037 ** fts3ExprIterate() callback used to collect the "global" matchinfo stats
140038 ** for a single query.
140039 **
140040 ** fts3ExprIterate() callback to load the 'global' elements of a
140041 ** FTS3_MATCHINFO_HITS matchinfo array. The global stats are those elements
140042 ** of the matchinfo array that are constant for all rows returned by the
140043 ** current query.
140044 **
140045 ** Argument pCtx is actually a pointer to a struct of type MatchInfo. This
140046 ** function populates Matchinfo.aMatchinfo[] as follows:
140047 **
140048 **   for(iCol=0; iCol<nCol; iCol++){
140049 **     aMatchinfo[3*iPhrase*nCol + 3*iCol + 1] = X;
140050 **     aMatchinfo[3*iPhrase*nCol + 3*iCol + 2] = Y;
140051 **   }
140052 **
140053 ** where X is the number of matches for phrase iPhrase is column iCol of all
140054 ** rows of the table. Y is the number of rows for which column iCol contains
140055 ** at least one instance of phrase iPhrase.
140056 **
140057 ** If the phrase pExpr consists entirely of deferred tokens, then all X and
140058 ** Y values are set to nDoc, where nDoc is the number of documents in the
140059 ** file system. This is done because the full-text index doclist is required
140060 ** to calculate these values properly, and the full-text index doclist is
140061 ** not available for deferred tokens.
140062 */
140063 static int fts3ExprGlobalHitsCb(
140064   Fts3Expr *pExpr,                /* Phrase expression node */
140065   int iPhrase,                    /* Phrase number (numbered from zero) */
140066   void *pCtx                      /* Pointer to MatchInfo structure */
140067 ){
140068   MatchInfo *p = (MatchInfo *)pCtx;
140069   return sqlite3Fts3EvalPhraseStats(
140070       p->pCursor, pExpr, &p->aMatchinfo[3*iPhrase*p->nCol]
140071   );
140072 }
140073 
140074 /*
140075 ** fts3ExprIterate() callback used to collect the "local" part of the
140076 ** FTS3_MATCHINFO_HITS array. The local stats are those elements of the
140077 ** array that are different for each row returned by the query.
140078 */
140079 static int fts3ExprLocalHitsCb(
140080   Fts3Expr *pExpr,                /* Phrase expression node */
140081   int iPhrase,                    /* Phrase number */
140082   void *pCtx                      /* Pointer to MatchInfo structure */
140083 ){
140084   int rc = SQLITE_OK;
140085   MatchInfo *p = (MatchInfo *)pCtx;
140086   int iStart = iPhrase * p->nCol * 3;
140087   int i;
140088 
140089   for(i=0; i<p->nCol && rc==SQLITE_OK; i++){
140090     char *pCsr;
140091     rc = sqlite3Fts3EvalPhrasePoslist(p->pCursor, pExpr, i, &pCsr);
140092     if( pCsr ){
140093       p->aMatchinfo[iStart+i*3] = fts3ColumnlistCount(&pCsr);
140094     }else{
140095       p->aMatchinfo[iStart+i*3] = 0;
140096     }
140097   }
140098 
140099   return rc;
140100 }
140101 
140102 static int fts3MatchinfoCheck(
140103   Fts3Table *pTab,
140104   char cArg,
140105   char **pzErr
140106 ){
140107   if( (cArg==FTS3_MATCHINFO_NPHRASE)
140108    || (cArg==FTS3_MATCHINFO_NCOL)
140109    || (cArg==FTS3_MATCHINFO_NDOC && pTab->bFts4)
140110    || (cArg==FTS3_MATCHINFO_AVGLENGTH && pTab->bFts4)
140111    || (cArg==FTS3_MATCHINFO_LENGTH && pTab->bHasDocsize)
140112    || (cArg==FTS3_MATCHINFO_LCS)
140113    || (cArg==FTS3_MATCHINFO_HITS)
140114   ){
140115     return SQLITE_OK;
140116   }
140117   *pzErr = sqlite3_mprintf("unrecognized matchinfo request: %c", cArg);
140118   return SQLITE_ERROR;
140119 }
140120 
140121 static int fts3MatchinfoSize(MatchInfo *pInfo, char cArg){
140122   int nVal;                       /* Number of integers output by cArg */
140123 
140124   switch( cArg ){
140125     case FTS3_MATCHINFO_NDOC:
140126     case FTS3_MATCHINFO_NPHRASE:
140127     case FTS3_MATCHINFO_NCOL:
140128       nVal = 1;
140129       break;
140130 
140131     case FTS3_MATCHINFO_AVGLENGTH:
140132     case FTS3_MATCHINFO_LENGTH:
140133     case FTS3_MATCHINFO_LCS:
140134       nVal = pInfo->nCol;
140135       break;
140136 
140137     default:
140138       assert( cArg==FTS3_MATCHINFO_HITS );
140139       nVal = pInfo->nCol * pInfo->nPhrase * 3;
140140       break;
140141   }
140142 
140143   return nVal;
140144 }
140145 
140146 static int fts3MatchinfoSelectDoctotal(
140147   Fts3Table *pTab,
140148   sqlite3_stmt **ppStmt,
140149   sqlite3_int64 *pnDoc,
140150   const char **paLen
140151 ){
140152   sqlite3_stmt *pStmt;
140153   const char *a;
140154   sqlite3_int64 nDoc;
140155 
140156   if( !*ppStmt ){
140157     int rc = sqlite3Fts3SelectDoctotal(pTab, ppStmt);
140158     if( rc!=SQLITE_OK ) return rc;
140159   }
140160   pStmt = *ppStmt;
140161   assert( sqlite3_data_count(pStmt)==1 );
140162 
140163   a = sqlite3_column_blob(pStmt, 0);
140164   a += sqlite3Fts3GetVarint(a, &nDoc);
140165   if( nDoc==0 ) return FTS_CORRUPT_VTAB;
140166   *pnDoc = (u32)nDoc;
140167 
140168   if( paLen ) *paLen = a;
140169   return SQLITE_OK;
140170 }
140171 
140172 /*
140173 ** An instance of the following structure is used to store state while
140174 ** iterating through a multi-column position-list corresponding to the
140175 ** hits for a single phrase on a single row in order to calculate the
140176 ** values for a matchinfo() FTS3_MATCHINFO_LCS request.
140177 */
140178 typedef struct LcsIterator LcsIterator;
140179 struct LcsIterator {
140180   Fts3Expr *pExpr;                /* Pointer to phrase expression */
140181   int iPosOffset;                 /* Tokens count up to end of this phrase */
140182   char *pRead;                    /* Cursor used to iterate through aDoclist */
140183   int iPos;                       /* Current position */
140184 };
140185 
140186 /*
140187 ** If LcsIterator.iCol is set to the following value, the iterator has
140188 ** finished iterating through all offsets for all columns.
140189 */
140190 #define LCS_ITERATOR_FINISHED 0x7FFFFFFF;
140191 
140192 static int fts3MatchinfoLcsCb(
140193   Fts3Expr *pExpr,                /* Phrase expression node */
140194   int iPhrase,                    /* Phrase number (numbered from zero) */
140195   void *pCtx                      /* Pointer to MatchInfo structure */
140196 ){
140197   LcsIterator *aIter = (LcsIterator *)pCtx;
140198   aIter[iPhrase].pExpr = pExpr;
140199   return SQLITE_OK;
140200 }
140201 
140202 /*
140203 ** Advance the iterator passed as an argument to the next position. Return
140204 ** 1 if the iterator is at EOF or if it now points to the start of the
140205 ** position list for the next column.
140206 */
140207 static int fts3LcsIteratorAdvance(LcsIterator *pIter){
140208   char *pRead = pIter->pRead;
140209   sqlite3_int64 iRead;
140210   int rc = 0;
140211 
140212   pRead += sqlite3Fts3GetVarint(pRead, &iRead);
140213   if( iRead==0 || iRead==1 ){
140214     pRead = 0;
140215     rc = 1;
140216   }else{
140217     pIter->iPos += (int)(iRead-2);
140218   }
140219 
140220   pIter->pRead = pRead;
140221   return rc;
140222 }
140223 
140224 /*
140225 ** This function implements the FTS3_MATCHINFO_LCS matchinfo() flag.
140226 **
140227 ** If the call is successful, the longest-common-substring lengths for each
140228 ** column are written into the first nCol elements of the pInfo->aMatchinfo[]
140229 ** array before returning. SQLITE_OK is returned in this case.
140230 **
140231 ** Otherwise, if an error occurs, an SQLite error code is returned and the
140232 ** data written to the first nCol elements of pInfo->aMatchinfo[] is
140233 ** undefined.
140234 */
140235 static int fts3MatchinfoLcs(Fts3Cursor *pCsr, MatchInfo *pInfo){
140236   LcsIterator *aIter;
140237   int i;
140238   int iCol;
140239   int nToken = 0;
140240 
140241   /* Allocate and populate the array of LcsIterator objects. The array
140242   ** contains one element for each matchable phrase in the query.
140243   **/
140244   aIter = sqlite3_malloc(sizeof(LcsIterator) * pCsr->nPhrase);
140245   if( !aIter ) return SQLITE_NOMEM;
140246   memset(aIter, 0, sizeof(LcsIterator) * pCsr->nPhrase);
140247   (void)fts3ExprIterate(pCsr->pExpr, fts3MatchinfoLcsCb, (void*)aIter);
140248 
140249   for(i=0; i<pInfo->nPhrase; i++){
140250     LcsIterator *pIter = &aIter[i];
140251     nToken -= pIter->pExpr->pPhrase->nToken;
140252     pIter->iPosOffset = nToken;
140253   }
140254 
140255   for(iCol=0; iCol<pInfo->nCol; iCol++){
140256     int nLcs = 0;                 /* LCS value for this column */
140257     int nLive = 0;                /* Number of iterators in aIter not at EOF */
140258 
140259     for(i=0; i<pInfo->nPhrase; i++){
140260       int rc;
140261       LcsIterator *pIt = &aIter[i];
140262       rc = sqlite3Fts3EvalPhrasePoslist(pCsr, pIt->pExpr, iCol, &pIt->pRead);
140263       if( rc!=SQLITE_OK ) return rc;
140264       if( pIt->pRead ){
140265         pIt->iPos = pIt->iPosOffset;
140266         fts3LcsIteratorAdvance(&aIter[i]);
140267         nLive++;
140268       }
140269     }
140270 
140271     while( nLive>0 ){
140272       LcsIterator *pAdv = 0;      /* The iterator to advance by one position */
140273       int nThisLcs = 0;           /* LCS for the current iterator positions */
140274 
140275       for(i=0; i<pInfo->nPhrase; i++){
140276         LcsIterator *pIter = &aIter[i];
140277         if( pIter->pRead==0 ){
140278           /* This iterator is already at EOF for this column. */
140279           nThisLcs = 0;
140280         }else{
140281           if( pAdv==0 || pIter->iPos<pAdv->iPos ){
140282             pAdv = pIter;
140283           }
140284           if( nThisLcs==0 || pIter->iPos==pIter[-1].iPos ){
140285             nThisLcs++;
140286           }else{
140287             nThisLcs = 1;
140288           }
140289           if( nThisLcs>nLcs ) nLcs = nThisLcs;
140290         }
140291       }
140292       if( fts3LcsIteratorAdvance(pAdv) ) nLive--;
140293     }
140294 
140295     pInfo->aMatchinfo[iCol] = nLcs;
140296   }
140297 
140298   sqlite3_free(aIter);
140299   return SQLITE_OK;
140300 }
140301 
140302 /*
140303 ** Populate the buffer pInfo->aMatchinfo[] with an array of integers to
140304 ** be returned by the matchinfo() function. Argument zArg contains the
140305 ** format string passed as the second argument to matchinfo (or the
140306 ** default value "pcx" if no second argument was specified). The format
140307 ** string has already been validated and the pInfo->aMatchinfo[] array
140308 ** is guaranteed to be large enough for the output.
140309 **
140310 ** If bGlobal is true, then populate all fields of the matchinfo() output.
140311 ** If it is false, then assume that those fields that do not change between
140312 ** rows (i.e. FTS3_MATCHINFO_NPHRASE, NCOL, NDOC, AVGLENGTH and part of HITS)
140313 ** have already been populated.
140314 **
140315 ** Return SQLITE_OK if successful, or an SQLite error code if an error
140316 ** occurs. If a value other than SQLITE_OK is returned, the state the
140317 ** pInfo->aMatchinfo[] buffer is left in is undefined.
140318 */
140319 static int fts3MatchinfoValues(
140320   Fts3Cursor *pCsr,               /* FTS3 cursor object */
140321   int bGlobal,                    /* True to grab the global stats */
140322   MatchInfo *pInfo,               /* Matchinfo context object */
140323   const char *zArg                /* Matchinfo format string */
140324 ){
140325   int rc = SQLITE_OK;
140326   int i;
140327   Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
140328   sqlite3_stmt *pSelect = 0;
140329 
140330   for(i=0; rc==SQLITE_OK && zArg[i]; i++){
140331 
140332     switch( zArg[i] ){
140333       case FTS3_MATCHINFO_NPHRASE:
140334         if( bGlobal ) pInfo->aMatchinfo[0] = pInfo->nPhrase;
140335         break;
140336 
140337       case FTS3_MATCHINFO_NCOL:
140338         if( bGlobal ) pInfo->aMatchinfo[0] = pInfo->nCol;
140339         break;
140340 
140341       case FTS3_MATCHINFO_NDOC:
140342         if( bGlobal ){
140343           sqlite3_int64 nDoc = 0;
140344           rc = fts3MatchinfoSelectDoctotal(pTab, &pSelect, &nDoc, 0);
140345           pInfo->aMatchinfo[0] = (u32)nDoc;
140346         }
140347         break;
140348 
140349       case FTS3_MATCHINFO_AVGLENGTH:
140350         if( bGlobal ){
140351           sqlite3_int64 nDoc;     /* Number of rows in table */
140352           const char *a;          /* Aggregate column length array */
140353 
140354           rc = fts3MatchinfoSelectDoctotal(pTab, &pSelect, &nDoc, &a);
140355           if( rc==SQLITE_OK ){
140356             int iCol;
140357             for(iCol=0; iCol<pInfo->nCol; iCol++){
140358               u32 iVal;
140359               sqlite3_int64 nToken;
140360               a += sqlite3Fts3GetVarint(a, &nToken);
140361               iVal = (u32)(((u32)(nToken&0xffffffff)+nDoc/2)/nDoc);
140362               pInfo->aMatchinfo[iCol] = iVal;
140363             }
140364           }
140365         }
140366         break;
140367 
140368       case FTS3_MATCHINFO_LENGTH: {
140369         sqlite3_stmt *pSelectDocsize = 0;
140370         rc = sqlite3Fts3SelectDocsize(pTab, pCsr->iPrevId, &pSelectDocsize);
140371         if( rc==SQLITE_OK ){
140372           int iCol;
140373           const char *a = sqlite3_column_blob(pSelectDocsize, 0);
140374           for(iCol=0; iCol<pInfo->nCol; iCol++){
140375             sqlite3_int64 nToken;
140376             a += sqlite3Fts3GetVarint(a, &nToken);
140377             pInfo->aMatchinfo[iCol] = (u32)nToken;
140378           }
140379         }
140380         sqlite3_reset(pSelectDocsize);
140381         break;
140382       }
140383 
140384       case FTS3_MATCHINFO_LCS:
140385         rc = fts3ExprLoadDoclists(pCsr, 0, 0);
140386         if( rc==SQLITE_OK ){
140387           rc = fts3MatchinfoLcs(pCsr, pInfo);
140388         }
140389         break;
140390 
140391       default: {
140392         Fts3Expr *pExpr;
140393         assert( zArg[i]==FTS3_MATCHINFO_HITS );
140394         pExpr = pCsr->pExpr;
140395         rc = fts3ExprLoadDoclists(pCsr, 0, 0);
140396         if( rc!=SQLITE_OK ) break;
140397         if( bGlobal ){
140398           if( pCsr->pDeferred ){
140399             rc = fts3MatchinfoSelectDoctotal(pTab, &pSelect, &pInfo->nDoc, 0);
140400             if( rc!=SQLITE_OK ) break;
140401           }
140402           rc = fts3ExprIterate(pExpr, fts3ExprGlobalHitsCb,(void*)pInfo);
140403           if( rc!=SQLITE_OK ) break;
140404         }
140405         (void)fts3ExprIterate(pExpr, fts3ExprLocalHitsCb,(void*)pInfo);
140406         break;
140407       }
140408     }
140409 
140410     pInfo->aMatchinfo += fts3MatchinfoSize(pInfo, zArg[i]);
140411   }
140412 
140413   sqlite3_reset(pSelect);
140414   return rc;
140415 }
140416 
140417 
140418 /*
140419 ** Populate pCsr->aMatchinfo[] with data for the current row. The
140420 ** 'matchinfo' data is an array of 32-bit unsigned integers (C type u32).
140421 */
140422 static int fts3GetMatchinfo(
140423   Fts3Cursor *pCsr,               /* FTS3 Cursor object */
140424   const char *zArg                /* Second argument to matchinfo() function */
140425 ){
140426   MatchInfo sInfo;
140427   Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
140428   int rc = SQLITE_OK;
140429   int bGlobal = 0;                /* Collect 'global' stats as well as local */
140430 
140431   memset(&sInfo, 0, sizeof(MatchInfo));
140432   sInfo.pCursor = pCsr;
140433   sInfo.nCol = pTab->nColumn;
140434 
140435   /* If there is cached matchinfo() data, but the format string for the
140436   ** cache does not match the format string for this request, discard
140437   ** the cached data. */
140438   if( pCsr->zMatchinfo && strcmp(pCsr->zMatchinfo, zArg) ){
140439     assert( pCsr->aMatchinfo );
140440     sqlite3_free(pCsr->aMatchinfo);
140441     pCsr->zMatchinfo = 0;
140442     pCsr->aMatchinfo = 0;
140443   }
140444 
140445   /* If Fts3Cursor.aMatchinfo[] is NULL, then this is the first time the
140446   ** matchinfo function has been called for this query. In this case
140447   ** allocate the array used to accumulate the matchinfo data and
140448   ** initialize those elements that are constant for every row.
140449   */
140450   if( pCsr->aMatchinfo==0 ){
140451     int nMatchinfo = 0;           /* Number of u32 elements in match-info */
140452     int nArg;                     /* Bytes in zArg */
140453     int i;                        /* Used to iterate through zArg */
140454 
140455     /* Determine the number of phrases in the query */
140456     pCsr->nPhrase = fts3ExprPhraseCount(pCsr->pExpr);
140457     sInfo.nPhrase = pCsr->nPhrase;
140458 
140459     /* Determine the number of integers in the buffer returned by this call. */
140460     for(i=0; zArg[i]; i++){
140461       nMatchinfo += fts3MatchinfoSize(&sInfo, zArg[i]);
140462     }
140463 
140464     /* Allocate space for Fts3Cursor.aMatchinfo[] and Fts3Cursor.zMatchinfo. */
140465     nArg = (int)strlen(zArg);
140466     pCsr->aMatchinfo = (u32 *)sqlite3_malloc(sizeof(u32)*nMatchinfo + nArg + 1);
140467     if( !pCsr->aMatchinfo ) return SQLITE_NOMEM;
140468 
140469     pCsr->zMatchinfo = (char *)&pCsr->aMatchinfo[nMatchinfo];
140470     pCsr->nMatchinfo = nMatchinfo;
140471     memcpy(pCsr->zMatchinfo, zArg, nArg+1);
140472     memset(pCsr->aMatchinfo, 0, sizeof(u32)*nMatchinfo);
140473     pCsr->isMatchinfoNeeded = 1;
140474     bGlobal = 1;
140475   }
140476 
140477   sInfo.aMatchinfo = pCsr->aMatchinfo;
140478   sInfo.nPhrase = pCsr->nPhrase;
140479   if( pCsr->isMatchinfoNeeded ){
140480     rc = fts3MatchinfoValues(pCsr, bGlobal, &sInfo, zArg);
140481     pCsr->isMatchinfoNeeded = 0;
140482   }
140483 
140484   return rc;
140485 }
140486 
140487 /*
140488 ** Implementation of snippet() function.
140489 */
140490 SQLITE_PRIVATE void sqlite3Fts3Snippet(
140491   sqlite3_context *pCtx,          /* SQLite function call context */
140492   Fts3Cursor *pCsr,               /* Cursor object */
140493   const char *zStart,             /* Snippet start text - "<b>" */
140494   const char *zEnd,               /* Snippet end text - "</b>" */
140495   const char *zEllipsis,          /* Snippet ellipsis text - "<b>...</b>" */
140496   int iCol,                       /* Extract snippet from this column */
140497   int nToken                      /* Approximate number of tokens in snippet */
140498 ){
140499   Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
140500   int rc = SQLITE_OK;
140501   int i;
140502   StrBuffer res = {0, 0, 0};
140503 
140504   /* The returned text includes up to four fragments of text extracted from
140505   ** the data in the current row. The first iteration of the for(...) loop
140506   ** below attempts to locate a single fragment of text nToken tokens in
140507   ** size that contains at least one instance of all phrases in the query
140508   ** expression that appear in the current row. If such a fragment of text
140509   ** cannot be found, the second iteration of the loop attempts to locate
140510   ** a pair of fragments, and so on.
140511   */
140512   int nSnippet = 0;               /* Number of fragments in this snippet */
140513   SnippetFragment aSnippet[4];    /* Maximum of 4 fragments per snippet */
140514   int nFToken = -1;               /* Number of tokens in each fragment */
140515 
140516   if( !pCsr->pExpr ){
140517     sqlite3_result_text(pCtx, "", 0, SQLITE_STATIC);
140518     return;
140519   }
140520 
140521   for(nSnippet=1; 1; nSnippet++){
140522 
140523     int iSnip;                    /* Loop counter 0..nSnippet-1 */
140524     u64 mCovered = 0;             /* Bitmask of phrases covered by snippet */
140525     u64 mSeen = 0;                /* Bitmask of phrases seen by BestSnippet() */
140526 
140527     if( nToken>=0 ){
140528       nFToken = (nToken+nSnippet-1) / nSnippet;
140529     }else{
140530       nFToken = -1 * nToken;
140531     }
140532 
140533     for(iSnip=0; iSnip<nSnippet; iSnip++){
140534       int iBestScore = -1;        /* Best score of columns checked so far */
140535       int iRead;                  /* Used to iterate through columns */
140536       SnippetFragment *pFragment = &aSnippet[iSnip];
140537 
140538       memset(pFragment, 0, sizeof(*pFragment));
140539 
140540       /* Loop through all columns of the table being considered for snippets.
140541       ** If the iCol argument to this function was negative, this means all
140542       ** columns of the FTS3 table. Otherwise, only column iCol is considered.
140543       */
140544       for(iRead=0; iRead<pTab->nColumn; iRead++){
140545         SnippetFragment sF = {0, 0, 0, 0};
140546         int iS;
140547         if( iCol>=0 && iRead!=iCol ) continue;
140548 
140549         /* Find the best snippet of nFToken tokens in column iRead. */
140550         rc = fts3BestSnippet(nFToken, pCsr, iRead, mCovered, &mSeen, &sF, &iS);
140551         if( rc!=SQLITE_OK ){
140552           goto snippet_out;
140553         }
140554         if( iS>iBestScore ){
140555           *pFragment = sF;
140556           iBestScore = iS;
140557         }
140558       }
140559 
140560       mCovered |= pFragment->covered;
140561     }
140562 
140563     /* If all query phrases seen by fts3BestSnippet() are present in at least
140564     ** one of the nSnippet snippet fragments, break out of the loop.
140565     */
140566     assert( (mCovered&mSeen)==mCovered );
140567     if( mSeen==mCovered || nSnippet==SizeofArray(aSnippet) ) break;
140568   }
140569 
140570   assert( nFToken>0 );
140571 
140572   for(i=0; i<nSnippet && rc==SQLITE_OK; i++){
140573     rc = fts3SnippetText(pCsr, &aSnippet[i],
140574         i, (i==nSnippet-1), nFToken, zStart, zEnd, zEllipsis, &res
140575     );
140576   }
140577 
140578  snippet_out:
140579   sqlite3Fts3SegmentsClose(pTab);
140580   if( rc!=SQLITE_OK ){
140581     sqlite3_result_error_code(pCtx, rc);
140582     sqlite3_free(res.z);
140583   }else{
140584     sqlite3_result_text(pCtx, res.z, -1, sqlite3_free);
140585   }
140586 }
140587 
140588 
140589 typedef struct TermOffset TermOffset;
140590 typedef struct TermOffsetCtx TermOffsetCtx;
140591 
140592 struct TermOffset {
140593   char *pList;                    /* Position-list */
140594   int iPos;                       /* Position just read from pList */
140595   int iOff;                       /* Offset of this term from read positions */
140596 };
140597 
140598 struct TermOffsetCtx {
140599   Fts3Cursor *pCsr;
140600   int iCol;                       /* Column of table to populate aTerm for */
140601   int iTerm;
140602   sqlite3_int64 iDocid;
140603   TermOffset *aTerm;
140604 };
140605 
140606 /*
140607 ** This function is an fts3ExprIterate() callback used by sqlite3Fts3Offsets().
140608 */
140609 static int fts3ExprTermOffsetInit(Fts3Expr *pExpr, int iPhrase, void *ctx){
140610   TermOffsetCtx *p = (TermOffsetCtx *)ctx;
140611   int nTerm;                      /* Number of tokens in phrase */
140612   int iTerm;                      /* For looping through nTerm phrase terms */
140613   char *pList;                    /* Pointer to position list for phrase */
140614   int iPos = 0;                   /* First position in position-list */
140615   int rc;
140616 
140617   UNUSED_PARAMETER(iPhrase);
140618   rc = sqlite3Fts3EvalPhrasePoslist(p->pCsr, pExpr, p->iCol, &pList);
140619   nTerm = pExpr->pPhrase->nToken;
140620   if( pList ){
140621     fts3GetDeltaPosition(&pList, &iPos);
140622     assert( iPos>=0 );
140623   }
140624 
140625   for(iTerm=0; iTerm<nTerm; iTerm++){
140626     TermOffset *pT = &p->aTerm[p->iTerm++];
140627     pT->iOff = nTerm-iTerm-1;
140628     pT->pList = pList;
140629     pT->iPos = iPos;
140630   }
140631 
140632   return rc;
140633 }
140634 
140635 /*
140636 ** Implementation of offsets() function.
140637 */
140638 SQLITE_PRIVATE void sqlite3Fts3Offsets(
140639   sqlite3_context *pCtx,          /* SQLite function call context */
140640   Fts3Cursor *pCsr                /* Cursor object */
140641 ){
140642   Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
140643   sqlite3_tokenizer_module const *pMod = pTab->pTokenizer->pModule;
140644   int rc;                         /* Return Code */
140645   int nToken;                     /* Number of tokens in query */
140646   int iCol;                       /* Column currently being processed */
140647   StrBuffer res = {0, 0, 0};      /* Result string */
140648   TermOffsetCtx sCtx;             /* Context for fts3ExprTermOffsetInit() */
140649 
140650   if( !pCsr->pExpr ){
140651     sqlite3_result_text(pCtx, "", 0, SQLITE_STATIC);
140652     return;
140653   }
140654 
140655   memset(&sCtx, 0, sizeof(sCtx));
140656   assert( pCsr->isRequireSeek==0 );
140657 
140658   /* Count the number of terms in the query */
140659   rc = fts3ExprLoadDoclists(pCsr, 0, &nToken);
140660   if( rc!=SQLITE_OK ) goto offsets_out;
140661 
140662   /* Allocate the array of TermOffset iterators. */
140663   sCtx.aTerm = (TermOffset *)sqlite3_malloc(sizeof(TermOffset)*nToken);
140664   if( 0==sCtx.aTerm ){
140665     rc = SQLITE_NOMEM;
140666     goto offsets_out;
140667   }
140668   sCtx.iDocid = pCsr->iPrevId;
140669   sCtx.pCsr = pCsr;
140670 
140671   /* Loop through the table columns, appending offset information to
140672   ** string-buffer res for each column.
140673   */
140674   for(iCol=0; iCol<pTab->nColumn; iCol++){
140675     sqlite3_tokenizer_cursor *pC; /* Tokenizer cursor */
140676     const char *ZDUMMY;           /* Dummy argument used with xNext() */
140677     int NDUMMY = 0;               /* Dummy argument used with xNext() */
140678     int iStart = 0;
140679     int iEnd = 0;
140680     int iCurrent = 0;
140681     const char *zDoc;
140682     int nDoc;
140683 
140684     /* Initialize the contents of sCtx.aTerm[] for column iCol. There is
140685     ** no way that this operation can fail, so the return code from
140686     ** fts3ExprIterate() can be discarded.
140687     */
140688     sCtx.iCol = iCol;
140689     sCtx.iTerm = 0;
140690     (void)fts3ExprIterate(pCsr->pExpr, fts3ExprTermOffsetInit, (void *)&sCtx);
140691 
140692     /* Retreive the text stored in column iCol. If an SQL NULL is stored
140693     ** in column iCol, jump immediately to the next iteration of the loop.
140694     ** If an OOM occurs while retrieving the data (this can happen if SQLite
140695     ** needs to transform the data from utf-16 to utf-8), return SQLITE_NOMEM
140696     ** to the caller.
140697     */
140698     zDoc = (const char *)sqlite3_column_text(pCsr->pStmt, iCol+1);
140699     nDoc = sqlite3_column_bytes(pCsr->pStmt, iCol+1);
140700     if( zDoc==0 ){
140701       if( sqlite3_column_type(pCsr->pStmt, iCol+1)==SQLITE_NULL ){
140702         continue;
140703       }
140704       rc = SQLITE_NOMEM;
140705       goto offsets_out;
140706     }
140707 
140708     /* Initialize a tokenizer iterator to iterate through column iCol. */
140709     rc = sqlite3Fts3OpenTokenizer(pTab->pTokenizer, pCsr->iLangid,
140710         zDoc, nDoc, &pC
140711     );
140712     if( rc!=SQLITE_OK ) goto offsets_out;
140713 
140714     rc = pMod->xNext(pC, &ZDUMMY, &NDUMMY, &iStart, &iEnd, &iCurrent);
140715     while( rc==SQLITE_OK ){
140716       int i;                      /* Used to loop through terms */
140717       int iMinPos = 0x7FFFFFFF;   /* Position of next token */
140718       TermOffset *pTerm = 0;      /* TermOffset associated with next token */
140719 
140720       for(i=0; i<nToken; i++){
140721         TermOffset *pT = &sCtx.aTerm[i];
140722         if( pT->pList && (pT->iPos-pT->iOff)<iMinPos ){
140723           iMinPos = pT->iPos-pT->iOff;
140724           pTerm = pT;
140725         }
140726       }
140727 
140728       if( !pTerm ){
140729         /* All offsets for this column have been gathered. */
140730         rc = SQLITE_DONE;
140731       }else{
140732         assert( iCurrent<=iMinPos );
140733         if( 0==(0xFE&*pTerm->pList) ){
140734           pTerm->pList = 0;
140735         }else{
140736           fts3GetDeltaPosition(&pTerm->pList, &pTerm->iPos);
140737         }
140738         while( rc==SQLITE_OK && iCurrent<iMinPos ){
140739           rc = pMod->xNext(pC, &ZDUMMY, &NDUMMY, &iStart, &iEnd, &iCurrent);
140740         }
140741         if( rc==SQLITE_OK ){
140742           char aBuffer[64];
140743           sqlite3_snprintf(sizeof(aBuffer), aBuffer,
140744               "%d %d %d %d ", iCol, pTerm-sCtx.aTerm, iStart, iEnd-iStart
140745           );
140746           rc = fts3StringAppend(&res, aBuffer, -1);
140747         }else if( rc==SQLITE_DONE && pTab->zContentTbl==0 ){
140748           rc = FTS_CORRUPT_VTAB;
140749         }
140750       }
140751     }
140752     if( rc==SQLITE_DONE ){
140753       rc = SQLITE_OK;
140754     }
140755 
140756     pMod->xClose(pC);
140757     if( rc!=SQLITE_OK ) goto offsets_out;
140758   }
140759 
140760  offsets_out:
140761   sqlite3_free(sCtx.aTerm);
140762   assert( rc!=SQLITE_DONE );
140763   sqlite3Fts3SegmentsClose(pTab);
140764   if( rc!=SQLITE_OK ){
140765     sqlite3_result_error_code(pCtx,  rc);
140766     sqlite3_free(res.z);
140767   }else{
140768     sqlite3_result_text(pCtx, res.z, res.n-1, sqlite3_free);
140769   }
140770   return;
140771 }
140772 
140773 /*
140774 ** Implementation of matchinfo() function.
140775 */
140776 SQLITE_PRIVATE void sqlite3Fts3Matchinfo(
140777   sqlite3_context *pContext,      /* Function call context */
140778   Fts3Cursor *pCsr,               /* FTS3 table cursor */
140779   const char *zArg                /* Second arg to matchinfo() function */
140780 ){
140781   Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
140782   int rc;
140783   int i;
140784   const char *zFormat;
140785 
140786   if( zArg ){
140787     for(i=0; zArg[i]; i++){
140788       char *zErr = 0;
140789       if( fts3MatchinfoCheck(pTab, zArg[i], &zErr) ){
140790         sqlite3_result_error(pContext, zErr, -1);
140791         sqlite3_free(zErr);
140792         return;
140793       }
140794     }
140795     zFormat = zArg;
140796   }else{
140797     zFormat = FTS3_MATCHINFO_DEFAULT;
140798   }
140799 
140800   if( !pCsr->pExpr ){
140801     sqlite3_result_blob(pContext, "", 0, SQLITE_STATIC);
140802     return;
140803   }
140804 
140805   /* Retrieve matchinfo() data. */
140806   rc = fts3GetMatchinfo(pCsr, zFormat);
140807   sqlite3Fts3SegmentsClose(pTab);
140808 
140809   if( rc!=SQLITE_OK ){
140810     sqlite3_result_error_code(pContext, rc);
140811   }else{
140812     int n = pCsr->nMatchinfo * sizeof(u32);
140813     sqlite3_result_blob(pContext, pCsr->aMatchinfo, n, SQLITE_TRANSIENT);
140814   }
140815 }
140816 
140817 #endif
140818 
140819 /************** End of fts3_snippet.c ****************************************/
140820 /************** Begin file fts3_unicode.c ************************************/
140821 /*
140822 ** 2012 May 24
140823 **
140824 ** The author disclaims copyright to this source code.  In place of
140825 ** a legal notice, here is a blessing:
140826 **
140827 **    May you do good and not evil.
140828 **    May you find forgiveness for yourself and forgive others.
140829 **    May you share freely, never taking more than you give.
140830 **
140831 ******************************************************************************
140832 **
140833 ** Implementation of the "unicode" full-text-search tokenizer.
140834 */
140835 
140836 #ifdef SQLITE_ENABLE_FTS4_UNICODE61
140837 
140838 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
140839 
140840 /* #include <assert.h> */
140841 /* #include <stdlib.h> */
140842 /* #include <stdio.h> */
140843 /* #include <string.h> */
140844 
140845 
140846 /*
140847 ** The following two macros - READ_UTF8 and WRITE_UTF8 - have been copied
140848 ** from the sqlite3 source file utf.c. If this file is compiled as part
140849 ** of the amalgamation, they are not required.
140850 */
140851 #ifndef SQLITE_AMALGAMATION
140852 
140853 static const unsigned char sqlite3Utf8Trans1[] = {
140854   0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
140855   0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
140856   0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
140857   0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
140858   0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
140859   0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
140860   0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
140861   0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x00, 0x00,
140862 };
140863 
140864 #define READ_UTF8(zIn, zTerm, c)                           \
140865   c = *(zIn++);                                            \
140866   if( c>=0xc0 ){                                           \
140867     c = sqlite3Utf8Trans1[c-0xc0];                         \
140868     while( zIn!=zTerm && (*zIn & 0xc0)==0x80 ){            \
140869       c = (c<<6) + (0x3f & *(zIn++));                      \
140870     }                                                      \
140871     if( c<0x80                                             \
140872         || (c&0xFFFFF800)==0xD800                          \
140873         || (c&0xFFFFFFFE)==0xFFFE ){  c = 0xFFFD; }        \
140874   }
140875 
140876 #define WRITE_UTF8(zOut, c) {                          \
140877   if( c<0x00080 ){                                     \
140878     *zOut++ = (u8)(c&0xFF);                            \
140879   }                                                    \
140880   else if( c<0x00800 ){                                \
140881     *zOut++ = 0xC0 + (u8)((c>>6)&0x1F);                \
140882     *zOut++ = 0x80 + (u8)(c & 0x3F);                   \
140883   }                                                    \
140884   else if( c<0x10000 ){                                \
140885     *zOut++ = 0xE0 + (u8)((c>>12)&0x0F);               \
140886     *zOut++ = 0x80 + (u8)((c>>6) & 0x3F);              \
140887     *zOut++ = 0x80 + (u8)(c & 0x3F);                   \
140888   }else{                                               \
140889     *zOut++ = 0xF0 + (u8)((c>>18) & 0x07);             \
140890     *zOut++ = 0x80 + (u8)((c>>12) & 0x3F);             \
140891     *zOut++ = 0x80 + (u8)((c>>6) & 0x3F);              \
140892     *zOut++ = 0x80 + (u8)(c & 0x3F);                   \
140893   }                                                    \
140894 }
140895 
140896 #endif /* ifndef SQLITE_AMALGAMATION */
140897 
140898 typedef struct unicode_tokenizer unicode_tokenizer;
140899 typedef struct unicode_cursor unicode_cursor;
140900 
140901 struct unicode_tokenizer {
140902   sqlite3_tokenizer base;
140903   int bRemoveDiacritic;
140904   int nException;
140905   int *aiException;
140906 };
140907 
140908 struct unicode_cursor {
140909   sqlite3_tokenizer_cursor base;
140910   const unsigned char *aInput;    /* Input text being tokenized */
140911   int nInput;                     /* Size of aInput[] in bytes */
140912   int iOff;                       /* Current offset within aInput[] */
140913   int iToken;                     /* Index of next token to be returned */
140914   char *zToken;                   /* storage for current token */
140915   int nAlloc;                     /* space allocated at zToken */
140916 };
140917 
140918 
140919 /*
140920 ** Destroy a tokenizer allocated by unicodeCreate().
140921 */
140922 static int unicodeDestroy(sqlite3_tokenizer *pTokenizer){
140923   if( pTokenizer ){
140924     unicode_tokenizer *p = (unicode_tokenizer *)pTokenizer;
140925     sqlite3_free(p->aiException);
140926     sqlite3_free(p);
140927   }
140928   return SQLITE_OK;
140929 }
140930 
140931 /*
140932 ** As part of a tokenchars= or separators= option, the CREATE VIRTUAL TABLE
140933 ** statement has specified that the tokenizer for this table shall consider
140934 ** all characters in string zIn/nIn to be separators (if bAlnum==0) or
140935 ** token characters (if bAlnum==1).
140936 **
140937 ** For each codepoint in the zIn/nIn string, this function checks if the
140938 ** sqlite3FtsUnicodeIsalnum() function already returns the desired result.
140939 ** If so, no action is taken. Otherwise, the codepoint is added to the
140940 ** unicode_tokenizer.aiException[] array. For the purposes of tokenization,
140941 ** the return value of sqlite3FtsUnicodeIsalnum() is inverted for all
140942 ** codepoints in the aiException[] array.
140943 **
140944 ** If a standalone diacritic mark (one that sqlite3FtsUnicodeIsdiacritic()
140945 ** identifies as a diacritic) occurs in the zIn/nIn string it is ignored.
140946 ** It is not possible to change the behavior of the tokenizer with respect
140947 ** to these codepoints.
140948 */
140949 static int unicodeAddExceptions(
140950   unicode_tokenizer *p,           /* Tokenizer to add exceptions to */
140951   int bAlnum,                     /* Replace Isalnum() return value with this */
140952   const char *zIn,                /* Array of characters to make exceptions */
140953   int nIn                         /* Length of z in bytes */
140954 ){
140955   const unsigned char *z = (const unsigned char *)zIn;
140956   const unsigned char *zTerm = &z[nIn];
140957   int iCode;
140958   int nEntry = 0;
140959 
140960   assert( bAlnum==0 || bAlnum==1 );
140961 
140962   while( z<zTerm ){
140963     READ_UTF8(z, zTerm, iCode);
140964     assert( (sqlite3FtsUnicodeIsalnum(iCode) & 0xFFFFFFFE)==0 );
140965     if( sqlite3FtsUnicodeIsalnum(iCode)!=bAlnum
140966      && sqlite3FtsUnicodeIsdiacritic(iCode)==0
140967     ){
140968       nEntry++;
140969     }
140970   }
140971 
140972   if( nEntry ){
140973     int *aNew;                    /* New aiException[] array */
140974     int nNew;                     /* Number of valid entries in array aNew[] */
140975 
140976     aNew = sqlite3_realloc(p->aiException, (p->nException+nEntry)*sizeof(int));
140977     if( aNew==0 ) return SQLITE_NOMEM;
140978     nNew = p->nException;
140979 
140980     z = (const unsigned char *)zIn;
140981     while( z<zTerm ){
140982       READ_UTF8(z, zTerm, iCode);
140983       if( sqlite3FtsUnicodeIsalnum(iCode)!=bAlnum
140984        && sqlite3FtsUnicodeIsdiacritic(iCode)==0
140985       ){
140986         int i, j;
140987         for(i=0; i<nNew && aNew[i]<iCode; i++);
140988         for(j=nNew; j>i; j--) aNew[j] = aNew[j-1];
140989         aNew[i] = iCode;
140990         nNew++;
140991       }
140992     }
140993     p->aiException = aNew;
140994     p->nException = nNew;
140995   }
140996 
140997   return SQLITE_OK;
140998 }
140999 
141000 /*
141001 ** Return true if the p->aiException[] array contains the value iCode.
141002 */
141003 static int unicodeIsException(unicode_tokenizer *p, int iCode){
141004   if( p->nException>0 ){
141005     int *a = p->aiException;
141006     int iLo = 0;
141007     int iHi = p->nException-1;
141008 
141009     while( iHi>=iLo ){
141010       int iTest = (iHi + iLo) / 2;
141011       if( iCode==a[iTest] ){
141012         return 1;
141013       }else if( iCode>a[iTest] ){
141014         iLo = iTest+1;
141015       }else{
141016         iHi = iTest-1;
141017       }
141018     }
141019   }
141020 
141021   return 0;
141022 }
141023 
141024 /*
141025 ** Return true if, for the purposes of tokenization, codepoint iCode is
141026 ** considered a token character (not a separator).
141027 */
141028 static int unicodeIsAlnum(unicode_tokenizer *p, int iCode){
141029   assert( (sqlite3FtsUnicodeIsalnum(iCode) & 0xFFFFFFFE)==0 );
141030   return sqlite3FtsUnicodeIsalnum(iCode) ^ unicodeIsException(p, iCode);
141031 }
141032 
141033 /*
141034 ** Create a new tokenizer instance.
141035 */
141036 static int unicodeCreate(
141037   int nArg,                       /* Size of array argv[] */
141038   const char * const *azArg,      /* Tokenizer creation arguments */
141039   sqlite3_tokenizer **pp          /* OUT: New tokenizer handle */
141040 ){
141041   unicode_tokenizer *pNew;        /* New tokenizer object */
141042   int i;
141043   int rc = SQLITE_OK;
141044 
141045   pNew = (unicode_tokenizer *) sqlite3_malloc(sizeof(unicode_tokenizer));
141046   if( pNew==NULL ) return SQLITE_NOMEM;
141047   memset(pNew, 0, sizeof(unicode_tokenizer));
141048   pNew->bRemoveDiacritic = 1;
141049 
141050   for(i=0; rc==SQLITE_OK && i<nArg; i++){
141051     const char *z = azArg[i];
141052     int n = strlen(z);
141053 
141054     if( n==19 && memcmp("remove_diacritics=1", z, 19)==0 ){
141055       pNew->bRemoveDiacritic = 1;
141056     }
141057     else if( n==19 && memcmp("remove_diacritics=0", z, 19)==0 ){
141058       pNew->bRemoveDiacritic = 0;
141059     }
141060     else if( n>=11 && memcmp("tokenchars=", z, 11)==0 ){
141061       rc = unicodeAddExceptions(pNew, 1, &z[11], n-11);
141062     }
141063     else if( n>=11 && memcmp("separators=", z, 11)==0 ){
141064       rc = unicodeAddExceptions(pNew, 0, &z[11], n-11);
141065     }
141066     else{
141067       /* Unrecognized argument */
141068       rc  = SQLITE_ERROR;
141069     }
141070   }
141071 
141072   if( rc!=SQLITE_OK ){
141073     unicodeDestroy((sqlite3_tokenizer *)pNew);
141074     pNew = 0;
141075   }
141076   *pp = (sqlite3_tokenizer *)pNew;
141077   return rc;
141078 }
141079 
141080 /*
141081 ** Prepare to begin tokenizing a particular string.  The input
141082 ** string to be tokenized is pInput[0..nBytes-1].  A cursor
141083 ** used to incrementally tokenize this string is returned in
141084 ** *ppCursor.
141085 */
141086 static int unicodeOpen(
141087   sqlite3_tokenizer *p,           /* The tokenizer */
141088   const char *aInput,             /* Input string */
141089   int nInput,                     /* Size of string aInput in bytes */
141090   sqlite3_tokenizer_cursor **pp   /* OUT: New cursor object */
141091 ){
141092   unicode_cursor *pCsr;
141093 
141094   pCsr = (unicode_cursor *)sqlite3_malloc(sizeof(unicode_cursor));
141095   if( pCsr==0 ){
141096     return SQLITE_NOMEM;
141097   }
141098   memset(pCsr, 0, sizeof(unicode_cursor));
141099 
141100   pCsr->aInput = (const unsigned char *)aInput;
141101   if( aInput==0 ){
141102     pCsr->nInput = 0;
141103   }else if( nInput<0 ){
141104     pCsr->nInput = (int)strlen(aInput);
141105   }else{
141106     pCsr->nInput = nInput;
141107   }
141108 
141109   *pp = &pCsr->base;
141110   UNUSED_PARAMETER(p);
141111   return SQLITE_OK;
141112 }
141113 
141114 /*
141115 ** Close a tokenization cursor previously opened by a call to
141116 ** simpleOpen() above.
141117 */
141118 static int unicodeClose(sqlite3_tokenizer_cursor *pCursor){
141119   unicode_cursor *pCsr = (unicode_cursor *) pCursor;
141120   sqlite3_free(pCsr->zToken);
141121   sqlite3_free(pCsr);
141122   return SQLITE_OK;
141123 }
141124 
141125 /*
141126 ** Extract the next token from a tokenization cursor.  The cursor must
141127 ** have been opened by a prior call to simpleOpen().
141128 */
141129 static int unicodeNext(
141130   sqlite3_tokenizer_cursor *pC,   /* Cursor returned by simpleOpen */
141131   const char **paToken,           /* OUT: Token text */
141132   int *pnToken,                   /* OUT: Number of bytes at *paToken */
141133   int *piStart,                   /* OUT: Starting offset of token */
141134   int *piEnd,                     /* OUT: Ending offset of token */
141135   int *piPos                      /* OUT: Position integer of token */
141136 ){
141137   unicode_cursor *pCsr = (unicode_cursor *)pC;
141138   unicode_tokenizer *p = ((unicode_tokenizer *)pCsr->base.pTokenizer);
141139   int iCode;
141140   char *zOut;
141141   const unsigned char *z = &pCsr->aInput[pCsr->iOff];
141142   const unsigned char *zStart = z;
141143   const unsigned char *zEnd;
141144   const unsigned char *zTerm = &pCsr->aInput[pCsr->nInput];
141145 
141146   /* Scan past any delimiter characters before the start of the next token.
141147   ** Return SQLITE_DONE early if this takes us all the way to the end of
141148   ** the input.  */
141149   while( z<zTerm ){
141150     READ_UTF8(z, zTerm, iCode);
141151     if( unicodeIsAlnum(p, iCode) ) break;
141152     zStart = z;
141153   }
141154   if( zStart>=zTerm ) return SQLITE_DONE;
141155 
141156   zOut = pCsr->zToken;
141157   do {
141158     int iOut;
141159 
141160     /* Grow the output buffer if required. */
141161     if( (zOut-pCsr->zToken)>=(pCsr->nAlloc-4) ){
141162       char *zNew = sqlite3_realloc(pCsr->zToken, pCsr->nAlloc+64);
141163       if( !zNew ) return SQLITE_NOMEM;
141164       zOut = &zNew[zOut - pCsr->zToken];
141165       pCsr->zToken = zNew;
141166       pCsr->nAlloc += 64;
141167     }
141168 
141169     /* Write the folded case of the last character read to the output */
141170     zEnd = z;
141171     iOut = sqlite3FtsUnicodeFold(iCode, p->bRemoveDiacritic);
141172     if( iOut ){
141173       WRITE_UTF8(zOut, iOut);
141174     }
141175 
141176     /* If the cursor is not at EOF, read the next character */
141177     if( z>=zTerm ) break;
141178     READ_UTF8(z, zTerm, iCode);
141179   }while( unicodeIsAlnum(p, iCode)
141180        || sqlite3FtsUnicodeIsdiacritic(iCode)
141181   );
141182 
141183   /* Set the output variables and return. */
141184   pCsr->iOff = (z - pCsr->aInput);
141185   *paToken = pCsr->zToken;
141186   *pnToken = zOut - pCsr->zToken;
141187   *piStart = (zStart - pCsr->aInput);
141188   *piEnd = (zEnd - pCsr->aInput);
141189   *piPos = pCsr->iToken++;
141190   return SQLITE_OK;
141191 }
141192 
141193 /*
141194 ** Set *ppModule to a pointer to the sqlite3_tokenizer_module
141195 ** structure for the unicode tokenizer.
141196 */
141197 SQLITE_PRIVATE void sqlite3Fts3UnicodeTokenizer(sqlite3_tokenizer_module const **ppModule){
141198   static const sqlite3_tokenizer_module module = {
141199     0,
141200     unicodeCreate,
141201     unicodeDestroy,
141202     unicodeOpen,
141203     unicodeClose,
141204     unicodeNext,
141205     0,
141206   };
141207   *ppModule = &module;
141208 }
141209 
141210 #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
141211 #endif /* ifndef SQLITE_ENABLE_FTS4_UNICODE61 */
141212 
141213 /************** End of fts3_unicode.c ****************************************/
141214 /************** Begin file fts3_unicode2.c ***********************************/
141215 /*
141216 ** 2012 May 25
141217 **
141218 ** The author disclaims copyright to this source code.  In place of
141219 ** a legal notice, here is a blessing:
141220 **
141221 **    May you do good and not evil.
141222 **    May you find forgiveness for yourself and forgive others.
141223 **    May you share freely, never taking more than you give.
141224 **
141225 ******************************************************************************
141226 */
141227 
141228 /*
141229 ** DO NOT EDIT THIS MACHINE GENERATED FILE.
141230 */
141231 
141232 #if defined(SQLITE_ENABLE_FTS4_UNICODE61)
141233 #if defined(SQLITE_ENABLE_FTS3) || defined(SQLITE_ENABLE_FTS4)
141234 
141235 /* #include <assert.h> */
141236 
141237 /*
141238 ** Return true if the argument corresponds to a unicode codepoint
141239 ** classified as either a letter or a number. Otherwise false.
141240 **
141241 ** The results are undefined if the value passed to this function
141242 ** is less than zero.
141243 */
141244 SQLITE_PRIVATE int sqlite3FtsUnicodeIsalnum(int c){
141245   /* Each unsigned integer in the following array corresponds to a contiguous
141246   ** range of unicode codepoints that are not either letters or numbers (i.e.
141247   ** codepoints for which this function should return 0).
141248   **
141249   ** The most significant 22 bits in each 32-bit value contain the first
141250   ** codepoint in the range. The least significant 10 bits are used to store
141251   ** the size of the range (always at least 1). In other words, the value
141252   ** ((C<<22) + N) represents a range of N codepoints starting with codepoint
141253   ** C. It is not possible to represent a range larger than 1023 codepoints
141254   ** using this format.
141255   */
141256   const static unsigned int aEntry[] = {
141257     0x00000030, 0x0000E807, 0x00016C06, 0x0001EC2F, 0x0002AC07,
141258     0x0002D001, 0x0002D803, 0x0002EC01, 0x0002FC01, 0x00035C01,
141259     0x0003DC01, 0x000B0804, 0x000B480E, 0x000B9407, 0x000BB401,
141260     0x000BBC81, 0x000DD401, 0x000DF801, 0x000E1002, 0x000E1C01,
141261     0x000FD801, 0x00120808, 0x00156806, 0x00162402, 0x00163C01,
141262     0x00164437, 0x0017CC02, 0x00180005, 0x00181816, 0x00187802,
141263     0x00192C15, 0x0019A804, 0x0019C001, 0x001B5001, 0x001B580F,
141264     0x001B9C07, 0x001BF402, 0x001C000E, 0x001C3C01, 0x001C4401,
141265     0x001CC01B, 0x001E980B, 0x001FAC09, 0x001FD804, 0x00205804,
141266     0x00206C09, 0x00209403, 0x0020A405, 0x0020C00F, 0x00216403,
141267     0x00217801, 0x0023901B, 0x00240004, 0x0024E803, 0x0024F812,
141268     0x00254407, 0x00258804, 0x0025C001, 0x00260403, 0x0026F001,
141269     0x0026F807, 0x00271C02, 0x00272C03, 0x00275C01, 0x00278802,
141270     0x0027C802, 0x0027E802, 0x00280403, 0x0028F001, 0x0028F805,
141271     0x00291C02, 0x00292C03, 0x00294401, 0x0029C002, 0x0029D401,
141272     0x002A0403, 0x002AF001, 0x002AF808, 0x002B1C03, 0x002B2C03,
141273     0x002B8802, 0x002BC002, 0x002C0403, 0x002CF001, 0x002CF807,
141274     0x002D1C02, 0x002D2C03, 0x002D5802, 0x002D8802, 0x002DC001,
141275     0x002E0801, 0x002EF805, 0x002F1803, 0x002F2804, 0x002F5C01,
141276     0x002FCC08, 0x00300403, 0x0030F807, 0x00311803, 0x00312804,
141277     0x00315402, 0x00318802, 0x0031FC01, 0x00320802, 0x0032F001,
141278     0x0032F807, 0x00331803, 0x00332804, 0x00335402, 0x00338802,
141279     0x00340802, 0x0034F807, 0x00351803, 0x00352804, 0x00355C01,
141280     0x00358802, 0x0035E401, 0x00360802, 0x00372801, 0x00373C06,
141281     0x00375801, 0x00376008, 0x0037C803, 0x0038C401, 0x0038D007,
141282     0x0038FC01, 0x00391C09, 0x00396802, 0x003AC401, 0x003AD006,
141283     0x003AEC02, 0x003B2006, 0x003C041F, 0x003CD00C, 0x003DC417,
141284     0x003E340B, 0x003E6424, 0x003EF80F, 0x003F380D, 0x0040AC14,
141285     0x00412806, 0x00415804, 0x00417803, 0x00418803, 0x00419C07,
141286     0x0041C404, 0x0042080C, 0x00423C01, 0x00426806, 0x0043EC01,
141287     0x004D740C, 0x004E400A, 0x00500001, 0x0059B402, 0x005A0001,
141288     0x005A6C02, 0x005BAC03, 0x005C4803, 0x005CC805, 0x005D4802,
141289     0x005DC802, 0x005ED023, 0x005F6004, 0x005F7401, 0x0060000F,
141290     0x0062A401, 0x0064800C, 0x0064C00C, 0x00650001, 0x00651002,
141291     0x0066C011, 0x00672002, 0x00677822, 0x00685C05, 0x00687802,
141292     0x0069540A, 0x0069801D, 0x0069FC01, 0x006A8007, 0x006AA006,
141293     0x006C0005, 0x006CD011, 0x006D6823, 0x006E0003, 0x006E840D,
141294     0x006F980E, 0x006FF004, 0x00709014, 0x0070EC05, 0x0071F802,
141295     0x00730008, 0x00734019, 0x0073B401, 0x0073C803, 0x00770027,
141296     0x0077F004, 0x007EF401, 0x007EFC03, 0x007F3403, 0x007F7403,
141297     0x007FB403, 0x007FF402, 0x00800065, 0x0081A806, 0x0081E805,
141298     0x00822805, 0x0082801A, 0x00834021, 0x00840002, 0x00840C04,
141299     0x00842002, 0x00845001, 0x00845803, 0x00847806, 0x00849401,
141300     0x00849C01, 0x0084A401, 0x0084B801, 0x0084E802, 0x00850005,
141301     0x00852804, 0x00853C01, 0x00864264, 0x00900027, 0x0091000B,
141302     0x0092704E, 0x00940200, 0x009C0475, 0x009E53B9, 0x00AD400A,
141303     0x00B39406, 0x00B3BC03, 0x00B3E404, 0x00B3F802, 0x00B5C001,
141304     0x00B5FC01, 0x00B7804F, 0x00B8C00C, 0x00BA001A, 0x00BA6C59,
141305     0x00BC00D6, 0x00BFC00C, 0x00C00005, 0x00C02019, 0x00C0A807,
141306     0x00C0D802, 0x00C0F403, 0x00C26404, 0x00C28001, 0x00C3EC01,
141307     0x00C64002, 0x00C6580A, 0x00C70024, 0x00C8001F, 0x00C8A81E,
141308     0x00C94001, 0x00C98020, 0x00CA2827, 0x00CB003F, 0x00CC0100,
141309     0x01370040, 0x02924037, 0x0293F802, 0x02983403, 0x0299BC10,
141310     0x029A7C01, 0x029BC008, 0x029C0017, 0x029C8002, 0x029E2402,
141311     0x02A00801, 0x02A01801, 0x02A02C01, 0x02A08C09, 0x02A0D804,
141312     0x02A1D004, 0x02A20002, 0x02A2D011, 0x02A33802, 0x02A38012,
141313     0x02A3E003, 0x02A4980A, 0x02A51C0D, 0x02A57C01, 0x02A60004,
141314     0x02A6CC1B, 0x02A77802, 0x02A8A40E, 0x02A90C01, 0x02A93002,
141315     0x02A97004, 0x02A9DC03, 0x02A9EC01, 0x02AAC001, 0x02AAC803,
141316     0x02AADC02, 0x02AAF802, 0x02AB0401, 0x02AB7802, 0x02ABAC07,
141317     0x02ABD402, 0x02AF8C0B, 0x03600001, 0x036DFC02, 0x036FFC02,
141318     0x037FFC01, 0x03EC7801, 0x03ECA401, 0x03EEC810, 0x03F4F802,
141319     0x03F7F002, 0x03F8001A, 0x03F88007, 0x03F8C023, 0x03F95013,
141320     0x03F9A004, 0x03FBFC01, 0x03FC040F, 0x03FC6807, 0x03FCEC06,
141321     0x03FD6C0B, 0x03FF8007, 0x03FFA007, 0x03FFE405, 0x04040003,
141322     0x0404DC09, 0x0405E411, 0x0406400C, 0x0407402E, 0x040E7C01,
141323     0x040F4001, 0x04215C01, 0x04247C01, 0x0424FC01, 0x04280403,
141324     0x04281402, 0x04283004, 0x0428E003, 0x0428FC01, 0x04294009,
141325     0x0429FC01, 0x042CE407, 0x04400003, 0x0440E016, 0x04420003,
141326     0x0442C012, 0x04440003, 0x04449C0E, 0x04450004, 0x04460003,
141327     0x0446CC0E, 0x04471404, 0x045AAC0D, 0x0491C004, 0x05BD442E,
141328     0x05BE3C04, 0x074000F6, 0x07440027, 0x0744A4B5, 0x07480046,
141329     0x074C0057, 0x075B0401, 0x075B6C01, 0x075BEC01, 0x075C5401,
141330     0x075CD401, 0x075D3C01, 0x075DBC01, 0x075E2401, 0x075EA401,
141331     0x075F0C01, 0x07BBC002, 0x07C0002C, 0x07C0C064, 0x07C2800F,
141332     0x07C2C40E, 0x07C3040F, 0x07C3440F, 0x07C4401F, 0x07C4C03C,
141333     0x07C5C02B, 0x07C7981D, 0x07C8402B, 0x07C90009, 0x07C94002,
141334     0x07CC0021, 0x07CCC006, 0x07CCDC46, 0x07CE0014, 0x07CE8025,
141335     0x07CF1805, 0x07CF8011, 0x07D0003F, 0x07D10001, 0x07D108B6,
141336     0x07D3E404, 0x07D4003E, 0x07D50004, 0x07D54018, 0x07D7EC46,
141337     0x07D9140B, 0x07DA0046, 0x07DC0074, 0x38000401, 0x38008060,
141338     0x380400F0,
141339   };
141340   static const unsigned int aAscii[4] = {
141341     0xFFFFFFFF, 0xFC00FFFF, 0xF8000001, 0xF8000001,
141342   };
141343 
141344   if( c<128 ){
141345     return ( (aAscii[c >> 5] & (1 << (c & 0x001F)))==0 );
141346   }else if( c<(1<<22) ){
141347     unsigned int key = (((unsigned int)c)<<10) | 0x000003FF;
141348     int iRes;
141349     int iHi = sizeof(aEntry)/sizeof(aEntry[0]) - 1;
141350     int iLo = 0;
141351     while( iHi>=iLo ){
141352       int iTest = (iHi + iLo) / 2;
141353       if( key >= aEntry[iTest] ){
141354         iRes = iTest;
141355         iLo = iTest+1;
141356       }else{
141357         iHi = iTest-1;
141358       }
141359     }
141360     assert( aEntry[0]<key );
141361     assert( key>=aEntry[iRes] );
141362     return (((unsigned int)c) >= ((aEntry[iRes]>>10) + (aEntry[iRes]&0x3FF)));
141363   }
141364   return 1;
141365 }
141366 
141367 
141368 /*
141369 ** If the argument is a codepoint corresponding to a lowercase letter
141370 ** in the ASCII range with a diacritic added, return the codepoint
141371 ** of the ASCII letter only. For example, if passed 235 - "LATIN
141372 ** SMALL LETTER E WITH DIAERESIS" - return 65 ("LATIN SMALL LETTER
141373 ** E"). The resuls of passing a codepoint that corresponds to an
141374 ** uppercase letter are undefined.
141375 */
141376 static int remove_diacritic(int c){
141377   unsigned short aDia[] = {
141378         0,  1797,  1848,  1859,  1891,  1928,  1940,  1995,
141379      2024,  2040,  2060,  2110,  2168,  2206,  2264,  2286,
141380      2344,  2383,  2472,  2488,  2516,  2596,  2668,  2732,
141381      2782,  2842,  2894,  2954,  2984,  3000,  3028,  3336,
141382      3456,  3696,  3712,  3728,  3744,  3896,  3912,  3928,
141383      3968,  4008,  4040,  4106,  4138,  4170,  4202,  4234,
141384      4266,  4296,  4312,  4344,  4408,  4424,  4472,  4504,
141385      6148,  6198,  6264,  6280,  6360,  6429,  6505,  6529,
141386     61448, 61468, 61534, 61592, 61642, 61688, 61704, 61726,
141387     61784, 61800, 61836, 61880, 61914, 61948, 61998, 62122,
141388     62154, 62200, 62218, 62302, 62364, 62442, 62478, 62536,
141389     62554, 62584, 62604, 62640, 62648, 62656, 62664, 62730,
141390     62924, 63050, 63082, 63274, 63390,
141391   };
141392   char aChar[] = {
141393     '\0', 'a',  'c',  'e',  'i',  'n',  'o',  'u',  'y',  'y',  'a',  'c',
141394     'd',  'e',  'e',  'g',  'h',  'i',  'j',  'k',  'l',  'n',  'o',  'r',
141395     's',  't',  'u',  'u',  'w',  'y',  'z',  'o',  'u',  'a',  'i',  'o',
141396     'u',  'g',  'k',  'o',  'j',  'g',  'n',  'a',  'e',  'i',  'o',  'r',
141397     'u',  's',  't',  'h',  'a',  'e',  'o',  'y',  '\0', '\0', '\0', '\0',
141398     '\0', '\0', '\0', '\0', 'a',  'b',  'd',  'd',  'e',  'f',  'g',  'h',
141399     'h',  'i',  'k',  'l',  'l',  'm',  'n',  'p',  'r',  'r',  's',  't',
141400     'u',  'v',  'w',  'w',  'x',  'y',  'z',  'h',  't',  'w',  'y',  'a',
141401     'e',  'i',  'o',  'u',  'y',
141402   };
141403 
141404   unsigned int key = (((unsigned int)c)<<3) | 0x00000007;
141405   int iRes = 0;
141406   int iHi = sizeof(aDia)/sizeof(aDia[0]) - 1;
141407   int iLo = 0;
141408   while( iHi>=iLo ){
141409     int iTest = (iHi + iLo) / 2;
141410     if( key >= aDia[iTest] ){
141411       iRes = iTest;
141412       iLo = iTest+1;
141413     }else{
141414       iHi = iTest-1;
141415     }
141416   }
141417   assert( key>=aDia[iRes] );
141418   return ((c > (aDia[iRes]>>3) + (aDia[iRes]&0x07)) ? c : (int)aChar[iRes]);
141419 };
141420 
141421 
141422 /*
141423 ** Return true if the argument interpreted as a unicode codepoint
141424 ** is a diacritical modifier character.
141425 */
141426 SQLITE_PRIVATE int sqlite3FtsUnicodeIsdiacritic(int c){
141427   unsigned int mask0 = 0x08029FDF;
141428   unsigned int mask1 = 0x000361F8;
141429   if( c<768 || c>817 ) return 0;
141430   return (c < 768+32) ?
141431       (mask0 & (1 << (c-768))) :
141432       (mask1 & (1 << (c-768-32)));
141433 }
141434 
141435 
141436 /*
141437 ** Interpret the argument as a unicode codepoint. If the codepoint
141438 ** is an upper case character that has a lower case equivalent,
141439 ** return the codepoint corresponding to the lower case version.
141440 ** Otherwise, return a copy of the argument.
141441 **
141442 ** The results are undefined if the value passed to this function
141443 ** is less than zero.
141444 */
141445 SQLITE_PRIVATE int sqlite3FtsUnicodeFold(int c, int bRemoveDiacritic){
141446   /* Each entry in the following array defines a rule for folding a range
141447   ** of codepoints to lower case. The rule applies to a range of nRange
141448   ** codepoints starting at codepoint iCode.
141449   **
141450   ** If the least significant bit in flags is clear, then the rule applies
141451   ** to all nRange codepoints (i.e. all nRange codepoints are upper case and
141452   ** need to be folded). Or, if it is set, then the rule only applies to
141453   ** every second codepoint in the range, starting with codepoint C.
141454   **
141455   ** The 7 most significant bits in flags are an index into the aiOff[]
141456   ** array. If a specific codepoint C does require folding, then its lower
141457   ** case equivalent is ((C + aiOff[flags>>1]) & 0xFFFF).
141458   **
141459   ** The contents of this array are generated by parsing the CaseFolding.txt
141460   ** file distributed as part of the "Unicode Character Database". See
141461   ** http://www.unicode.org for details.
141462   */
141463   static const struct TableEntry {
141464     unsigned short iCode;
141465     unsigned char flags;
141466     unsigned char nRange;
141467   } aEntry[] = {
141468     {65, 14, 26},          {181, 64, 1},          {192, 14, 23},
141469     {216, 14, 7},          {256, 1, 48},          {306, 1, 6},
141470     {313, 1, 16},          {330, 1, 46},          {376, 116, 1},
141471     {377, 1, 6},           {383, 104, 1},         {385, 50, 1},
141472     {386, 1, 4},           {390, 44, 1},          {391, 0, 1},
141473     {393, 42, 2},          {395, 0, 1},           {398, 32, 1},
141474     {399, 38, 1},          {400, 40, 1},          {401, 0, 1},
141475     {403, 42, 1},          {404, 46, 1},          {406, 52, 1},
141476     {407, 48, 1},          {408, 0, 1},           {412, 52, 1},
141477     {413, 54, 1},          {415, 56, 1},          {416, 1, 6},
141478     {422, 60, 1},          {423, 0, 1},           {425, 60, 1},
141479     {428, 0, 1},           {430, 60, 1},          {431, 0, 1},
141480     {433, 58, 2},          {435, 1, 4},           {439, 62, 1},
141481     {440, 0, 1},           {444, 0, 1},           {452, 2, 1},
141482     {453, 0, 1},           {455, 2, 1},           {456, 0, 1},
141483     {458, 2, 1},           {459, 1, 18},          {478, 1, 18},
141484     {497, 2, 1},           {498, 1, 4},           {502, 122, 1},
141485     {503, 134, 1},         {504, 1, 40},          {544, 110, 1},
141486     {546, 1, 18},          {570, 70, 1},          {571, 0, 1},
141487     {573, 108, 1},         {574, 68, 1},          {577, 0, 1},
141488     {579, 106, 1},         {580, 28, 1},          {581, 30, 1},
141489     {582, 1, 10},          {837, 36, 1},          {880, 1, 4},
141490     {886, 0, 1},           {902, 18, 1},          {904, 16, 3},
141491     {908, 26, 1},          {910, 24, 2},          {913, 14, 17},
141492     {931, 14, 9},          {962, 0, 1},           {975, 4, 1},
141493     {976, 140, 1},         {977, 142, 1},         {981, 146, 1},
141494     {982, 144, 1},         {984, 1, 24},          {1008, 136, 1},
141495     {1009, 138, 1},        {1012, 130, 1},        {1013, 128, 1},
141496     {1015, 0, 1},          {1017, 152, 1},        {1018, 0, 1},
141497     {1021, 110, 3},        {1024, 34, 16},        {1040, 14, 32},
141498     {1120, 1, 34},         {1162, 1, 54},         {1216, 6, 1},
141499     {1217, 1, 14},         {1232, 1, 88},         {1329, 22, 38},
141500     {4256, 66, 38},        {4295, 66, 1},         {4301, 66, 1},
141501     {7680, 1, 150},        {7835, 132, 1},        {7838, 96, 1},
141502     {7840, 1, 96},         {7944, 150, 8},        {7960, 150, 6},
141503     {7976, 150, 8},        {7992, 150, 8},        {8008, 150, 6},
141504     {8025, 151, 8},        {8040, 150, 8},        {8072, 150, 8},
141505     {8088, 150, 8},        {8104, 150, 8},        {8120, 150, 2},
141506     {8122, 126, 2},        {8124, 148, 1},        {8126, 100, 1},
141507     {8136, 124, 4},        {8140, 148, 1},        {8152, 150, 2},
141508     {8154, 120, 2},        {8168, 150, 2},        {8170, 118, 2},
141509     {8172, 152, 1},        {8184, 112, 2},        {8186, 114, 2},
141510     {8188, 148, 1},        {8486, 98, 1},         {8490, 92, 1},
141511     {8491, 94, 1},         {8498, 12, 1},         {8544, 8, 16},
141512     {8579, 0, 1},          {9398, 10, 26},        {11264, 22, 47},
141513     {11360, 0, 1},         {11362, 88, 1},        {11363, 102, 1},
141514     {11364, 90, 1},        {11367, 1, 6},         {11373, 84, 1},
141515     {11374, 86, 1},        {11375, 80, 1},        {11376, 82, 1},
141516     {11378, 0, 1},         {11381, 0, 1},         {11390, 78, 2},
141517     {11392, 1, 100},       {11499, 1, 4},         {11506, 0, 1},
141518     {42560, 1, 46},        {42624, 1, 24},        {42786, 1, 14},
141519     {42802, 1, 62},        {42873, 1, 4},         {42877, 76, 1},
141520     {42878, 1, 10},        {42891, 0, 1},         {42893, 74, 1},
141521     {42896, 1, 4},         {42912, 1, 10},        {42922, 72, 1},
141522     {65313, 14, 26},
141523   };
141524   static const unsigned short aiOff[] = {
141525    1,     2,     8,     15,    16,    26,    28,    32,
141526    37,    38,    40,    48,    63,    64,    69,    71,
141527    79,    80,    116,   202,   203,   205,   206,   207,
141528    209,   210,   211,   213,   214,   217,   218,   219,
141529    775,   7264,  10792, 10795, 23228, 23256, 30204, 54721,
141530    54753, 54754, 54756, 54787, 54793, 54809, 57153, 57274,
141531    57921, 58019, 58363, 61722, 65268, 65341, 65373, 65406,
141532    65408, 65410, 65415, 65424, 65436, 65439, 65450, 65462,
141533    65472, 65476, 65478, 65480, 65482, 65488, 65506, 65511,
141534    65514, 65521, 65527, 65528, 65529,
141535   };
141536 
141537   int ret = c;
141538 
141539   assert( c>=0 );
141540   assert( sizeof(unsigned short)==2 && sizeof(unsigned char)==1 );
141541 
141542   if( c<128 ){
141543     if( c>='A' && c<='Z' ) ret = c + ('a' - 'A');
141544   }else if( c<65536 ){
141545     int iHi = sizeof(aEntry)/sizeof(aEntry[0]) - 1;
141546     int iLo = 0;
141547     int iRes = -1;
141548 
141549     while( iHi>=iLo ){
141550       int iTest = (iHi + iLo) / 2;
141551       int cmp = (c - aEntry[iTest].iCode);
141552       if( cmp>=0 ){
141553         iRes = iTest;
141554         iLo = iTest+1;
141555       }else{
141556         iHi = iTest-1;
141557       }
141558     }
141559     assert( iRes<0 || c>=aEntry[iRes].iCode );
141560 
141561     if( iRes>=0 ){
141562       const struct TableEntry *p = &aEntry[iRes];
141563       if( c<(p->iCode + p->nRange) && 0==(0x01 & p->flags & (p->iCode ^ c)) ){
141564         ret = (c + (aiOff[p->flags>>1])) & 0x0000FFFF;
141565         assert( ret>0 );
141566       }
141567     }
141568 
141569     if( bRemoveDiacritic ) ret = remove_diacritic(ret);
141570   }
141571 
141572   else if( c>=66560 && c<66600 ){
141573     ret = c + 40;
141574   }
141575 
141576   return ret;
141577 }
141578 #endif /* defined(SQLITE_ENABLE_FTS3) || defined(SQLITE_ENABLE_FTS4) */
141579 #endif /* !defined(SQLITE_ENABLE_FTS4_UNICODE61) */
141580 
141581 /************** End of fts3_unicode2.c ***************************************/
141582 /************** Begin file rtree.c *******************************************/
141583 /*
141584 ** 2001 September 15
141585 **
141586 ** The author disclaims copyright to this source code.  In place of
141587 ** a legal notice, here is a blessing:
141588 **
141589 **    May you do good and not evil.
141590 **    May you find forgiveness for yourself and forgive others.
141591 **    May you share freely, never taking more than you give.
141592 **
141593 *************************************************************************
141594 ** This file contains code for implementations of the r-tree and r*-tree
141595 ** algorithms packaged as an SQLite virtual table module.
141596 */
141597 
141598 /*
141599 ** Database Format of R-Tree Tables
141600 ** --------------------------------
141601 **
141602 ** The data structure for a single virtual r-tree table is stored in three
141603 ** native SQLite tables declared as follows. In each case, the '%' character
141604 ** in the table name is replaced with the user-supplied name of the r-tree
141605 ** table.
141606 **
141607 **   CREATE TABLE %_node(nodeno INTEGER PRIMARY KEY, data BLOB)
141608 **   CREATE TABLE %_parent(nodeno INTEGER PRIMARY KEY, parentnode INTEGER)
141609 **   CREATE TABLE %_rowid(rowid INTEGER PRIMARY KEY, nodeno INTEGER)
141610 **
141611 ** The data for each node of the r-tree structure is stored in the %_node
141612 ** table. For each node that is not the root node of the r-tree, there is
141613 ** an entry in the %_parent table associating the node with its parent.
141614 ** And for each row of data in the table, there is an entry in the %_rowid
141615 ** table that maps from the entries rowid to the id of the node that it
141616 ** is stored on.
141617 **
141618 ** The root node of an r-tree always exists, even if the r-tree table is
141619 ** empty. The nodeno of the root node is always 1. All other nodes in the
141620 ** table must be the same size as the root node. The content of each node
141621 ** is formatted as follows:
141622 **
141623 **   1. If the node is the root node (node 1), then the first 2 bytes
141624 **      of the node contain the tree depth as a big-endian integer.
141625 **      For non-root nodes, the first 2 bytes are left unused.
141626 **
141627 **   2. The next 2 bytes contain the number of entries currently
141628 **      stored in the node.
141629 **
141630 **   3. The remainder of the node contains the node entries. Each entry
141631 **      consists of a single 8-byte integer followed by an even number
141632 **      of 4-byte coordinates. For leaf nodes the integer is the rowid
141633 **      of a record. For internal nodes it is the node number of a
141634 **      child page.
141635 */
141636 
141637 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_RTREE)
141638 
141639 /*
141640 ** This file contains an implementation of a couple of different variants
141641 ** of the r-tree algorithm. See the README file for further details. The
141642 ** same data-structure is used for all, but the algorithms for insert and
141643 ** delete operations vary. The variants used are selected at compile time
141644 ** by defining the following symbols:
141645 */
141646 
141647 /* Either, both or none of the following may be set to activate
141648 ** r*tree variant algorithms.
141649 */
141650 #define VARIANT_RSTARTREE_CHOOSESUBTREE 0
141651 #define VARIANT_RSTARTREE_REINSERT      1
141652 
141653 /*
141654 ** Exactly one of the following must be set to 1.
141655 */
141656 #define VARIANT_GUTTMAN_QUADRATIC_SPLIT 0
141657 #define VARIANT_GUTTMAN_LINEAR_SPLIT    0
141658 #define VARIANT_RSTARTREE_SPLIT         1
141659 
141660 #define VARIANT_GUTTMAN_SPLIT \
141661         (VARIANT_GUTTMAN_LINEAR_SPLIT||VARIANT_GUTTMAN_QUADRATIC_SPLIT)
141662 
141663 #if VARIANT_GUTTMAN_QUADRATIC_SPLIT
141664   #define PickNext QuadraticPickNext
141665   #define PickSeeds QuadraticPickSeeds
141666   #define AssignCells splitNodeGuttman
141667 #endif
141668 #if VARIANT_GUTTMAN_LINEAR_SPLIT
141669   #define PickNext LinearPickNext
141670   #define PickSeeds LinearPickSeeds
141671   #define AssignCells splitNodeGuttman
141672 #endif
141673 #if VARIANT_RSTARTREE_SPLIT
141674   #define AssignCells splitNodeStartree
141675 #endif
141676 
141677 #if !defined(NDEBUG) && !defined(SQLITE_DEBUG)
141678 # define NDEBUG 1
141679 #endif
141680 
141681 #ifndef SQLITE_CORE
141682   SQLITE_EXTENSION_INIT1
141683 #else
141684 #endif
141685 
141686 /* #include <string.h> */
141687 /* #include <assert.h> */
141688 
141689 #ifndef SQLITE_AMALGAMATION
141690 #include "sqlite3rtree.h"
141691 typedef sqlite3_int64 i64;
141692 typedef unsigned char u8;
141693 typedef unsigned int u32;
141694 #endif
141695 
141696 /*  The following macro is used to suppress compiler warnings.
141697 */
141698 #ifndef UNUSED_PARAMETER
141699 # define UNUSED_PARAMETER(x) (void)(x)
141700 #endif
141701 
141702 typedef struct Rtree Rtree;
141703 typedef struct RtreeCursor RtreeCursor;
141704 typedef struct RtreeNode RtreeNode;
141705 typedef struct RtreeCell RtreeCell;
141706 typedef struct RtreeConstraint RtreeConstraint;
141707 typedef struct RtreeMatchArg RtreeMatchArg;
141708 typedef struct RtreeGeomCallback RtreeGeomCallback;
141709 typedef union RtreeCoord RtreeCoord;
141710 
141711 /* The rtree may have between 1 and RTREE_MAX_DIMENSIONS dimensions. */
141712 #define RTREE_MAX_DIMENSIONS 5
141713 
141714 /* Size of hash table Rtree.aHash. This hash table is not expected to
141715 ** ever contain very many entries, so a fixed number of buckets is
141716 ** used.
141717 */
141718 #define HASHSIZE 128
141719 
141720 /* The xBestIndex method of this virtual table requires an estimate of
141721 ** the number of rows in the virtual table to calculate the costs of
141722 ** various strategies. If possible, this estimate is loaded from the
141723 ** sqlite_stat1 table (with RTREE_MIN_ROWEST as a hard-coded minimum).
141724 ** Otherwise, if no sqlite_stat1 entry is available, use
141725 ** RTREE_DEFAULT_ROWEST.
141726 */
141727 #define RTREE_DEFAULT_ROWEST 1048576
141728 #define RTREE_MIN_ROWEST         100
141729 
141730 /*
141731 ** An rtree virtual-table object.
141732 */
141733 struct Rtree {
141734   sqlite3_vtab base;
141735   sqlite3 *db;                /* Host database connection */
141736   int iNodeSize;              /* Size in bytes of each node in the node table */
141737   int nDim;                   /* Number of dimensions */
141738   int nBytesPerCell;          /* Bytes consumed per cell */
141739   int iDepth;                 /* Current depth of the r-tree structure */
141740   char *zDb;                  /* Name of database containing r-tree table */
141741   char *zName;                /* Name of r-tree table */
141742   RtreeNode *aHash[HASHSIZE]; /* Hash table of in-memory nodes. */
141743   int nBusy;                  /* Current number of users of this structure */
141744   i64 nRowEst;                /* Estimated number of rows in this table */
141745 
141746   /* List of nodes removed during a CondenseTree operation. List is
141747   ** linked together via the pointer normally used for hash chains -
141748   ** RtreeNode.pNext. RtreeNode.iNode stores the depth of the sub-tree
141749   ** headed by the node (leaf nodes have RtreeNode.iNode==0).
141750   */
141751   RtreeNode *pDeleted;
141752   int iReinsertHeight;        /* Height of sub-trees Reinsert() has run on */
141753 
141754   /* Statements to read/write/delete a record from xxx_node */
141755   sqlite3_stmt *pReadNode;
141756   sqlite3_stmt *pWriteNode;
141757   sqlite3_stmt *pDeleteNode;
141758 
141759   /* Statements to read/write/delete a record from xxx_rowid */
141760   sqlite3_stmt *pReadRowid;
141761   sqlite3_stmt *pWriteRowid;
141762   sqlite3_stmt *pDeleteRowid;
141763 
141764   /* Statements to read/write/delete a record from xxx_parent */
141765   sqlite3_stmt *pReadParent;
141766   sqlite3_stmt *pWriteParent;
141767   sqlite3_stmt *pDeleteParent;
141768 
141769   int eCoordType;
141770 };
141771 
141772 /* Possible values for eCoordType: */
141773 #define RTREE_COORD_REAL32 0
141774 #define RTREE_COORD_INT32  1
141775 
141776 /*
141777 ** If SQLITE_RTREE_INT_ONLY is defined, then this virtual table will
141778 ** only deal with integer coordinates.  No floating point operations
141779 ** will be done.
141780 */
141781 #ifdef SQLITE_RTREE_INT_ONLY
141782   typedef sqlite3_int64 RtreeDValue;       /* High accuracy coordinate */
141783   typedef int RtreeValue;                  /* Low accuracy coordinate */
141784 #else
141785   typedef double RtreeDValue;              /* High accuracy coordinate */
141786   typedef float RtreeValue;                /* Low accuracy coordinate */
141787 #endif
141788 
141789 /*
141790 ** The minimum number of cells allowed for a node is a third of the
141791 ** maximum. In Gutman's notation:
141792 **
141793 **     m = M/3
141794 **
141795 ** If an R*-tree "Reinsert" operation is required, the same number of
141796 ** cells are removed from the overfull node and reinserted into the tree.
141797 */
141798 #define RTREE_MINCELLS(p) ((((p)->iNodeSize-4)/(p)->nBytesPerCell)/3)
141799 #define RTREE_REINSERT(p) RTREE_MINCELLS(p)
141800 #define RTREE_MAXCELLS 51
141801 
141802 /*
141803 ** The smallest possible node-size is (512-64)==448 bytes. And the largest
141804 ** supported cell size is 48 bytes (8 byte rowid + ten 4 byte coordinates).
141805 ** Therefore all non-root nodes must contain at least 3 entries. Since
141806 ** 2^40 is greater than 2^64, an r-tree structure always has a depth of
141807 ** 40 or less.
141808 */
141809 #define RTREE_MAX_DEPTH 40
141810 
141811 /*
141812 ** An rtree cursor object.
141813 */
141814 struct RtreeCursor {
141815   sqlite3_vtab_cursor base;
141816   RtreeNode *pNode;                 /* Node cursor is currently pointing at */
141817   int iCell;                        /* Index of current cell in pNode */
141818   int iStrategy;                    /* Copy of idxNum search parameter */
141819   int nConstraint;                  /* Number of entries in aConstraint */
141820   RtreeConstraint *aConstraint;     /* Search constraints. */
141821 };
141822 
141823 union RtreeCoord {
141824   RtreeValue f;
141825   int i;
141826 };
141827 
141828 /*
141829 ** The argument is an RtreeCoord. Return the value stored within the RtreeCoord
141830 ** formatted as a RtreeDValue (double or int64). This macro assumes that local
141831 ** variable pRtree points to the Rtree structure associated with the
141832 ** RtreeCoord.
141833 */
141834 #ifdef SQLITE_RTREE_INT_ONLY
141835 # define DCOORD(coord) ((RtreeDValue)coord.i)
141836 #else
141837 # define DCOORD(coord) (                           \
141838     (pRtree->eCoordType==RTREE_COORD_REAL32) ?      \
141839       ((double)coord.f) :                           \
141840       ((double)coord.i)                             \
141841   )
141842 #endif
141843 
141844 /*
141845 ** A search constraint.
141846 */
141847 struct RtreeConstraint {
141848   int iCoord;                     /* Index of constrained coordinate */
141849   int op;                         /* Constraining operation */
141850   RtreeDValue rValue;             /* Constraint value. */
141851   int (*xGeom)(sqlite3_rtree_geometry*, int, RtreeDValue*, int*);
141852   sqlite3_rtree_geometry *pGeom;  /* Constraint callback argument for a MATCH */
141853 };
141854 
141855 /* Possible values for RtreeConstraint.op */
141856 #define RTREE_EQ    0x41
141857 #define RTREE_LE    0x42
141858 #define RTREE_LT    0x43
141859 #define RTREE_GE    0x44
141860 #define RTREE_GT    0x45
141861 #define RTREE_MATCH 0x46
141862 
141863 /*
141864 ** An rtree structure node.
141865 */
141866 struct RtreeNode {
141867   RtreeNode *pParent;               /* Parent node */
141868   i64 iNode;
141869   int nRef;
141870   int isDirty;
141871   u8 *zData;
141872   RtreeNode *pNext;                 /* Next node in this hash chain */
141873 };
141874 #define NCELL(pNode) readInt16(&(pNode)->zData[2])
141875 
141876 /*
141877 ** Structure to store a deserialized rtree record.
141878 */
141879 struct RtreeCell {
141880   i64 iRowid;
141881   RtreeCoord aCoord[RTREE_MAX_DIMENSIONS*2];
141882 };
141883 
141884 
141885 /*
141886 ** Value for the first field of every RtreeMatchArg object. The MATCH
141887 ** operator tests that the first field of a blob operand matches this
141888 ** value to avoid operating on invalid blobs (which could cause a segfault).
141889 */
141890 #define RTREE_GEOMETRY_MAGIC 0x891245AB
141891 
141892 /*
141893 ** An instance of this structure must be supplied as a blob argument to
141894 ** the right-hand-side of an SQL MATCH operator used to constrain an
141895 ** r-tree query.
141896 */
141897 struct RtreeMatchArg {
141898   u32 magic;                      /* Always RTREE_GEOMETRY_MAGIC */
141899   int (*xGeom)(sqlite3_rtree_geometry *, int, RtreeDValue*, int *);
141900   void *pContext;
141901   int nParam;
141902   RtreeDValue aParam[1];
141903 };
141904 
141905 /*
141906 ** When a geometry callback is created (see sqlite3_rtree_geometry_callback),
141907 ** a single instance of the following structure is allocated. It is used
141908 ** as the context for the user-function created by by s_r_g_c(). The object
141909 ** is eventually deleted by the destructor mechanism provided by
141910 ** sqlite3_create_function_v2() (which is called by s_r_g_c() to create
141911 ** the geometry callback function).
141912 */
141913 struct RtreeGeomCallback {
141914   int (*xGeom)(sqlite3_rtree_geometry*, int, RtreeDValue*, int*);
141915   void *pContext;
141916 };
141917 
141918 #ifndef MAX
141919 # define MAX(x,y) ((x) < (y) ? (y) : (x))
141920 #endif
141921 #ifndef MIN
141922 # define MIN(x,y) ((x) > (y) ? (y) : (x))
141923 #endif
141924 
141925 /*
141926 ** Functions to deserialize a 16 bit integer, 32 bit real number and
141927 ** 64 bit integer. The deserialized value is returned.
141928 */
141929 static int readInt16(u8 *p){
141930   return (p[0]<<8) + p[1];
141931 }
141932 static void readCoord(u8 *p, RtreeCoord *pCoord){
141933   u32 i = (
141934     (((u32)p[0]) << 24) +
141935     (((u32)p[1]) << 16) +
141936     (((u32)p[2]) <<  8) +
141937     (((u32)p[3]) <<  0)
141938   );
141939   *(u32 *)pCoord = i;
141940 }
141941 static i64 readInt64(u8 *p){
141942   return (
141943     (((i64)p[0]) << 56) +
141944     (((i64)p[1]) << 48) +
141945     (((i64)p[2]) << 40) +
141946     (((i64)p[3]) << 32) +
141947     (((i64)p[4]) << 24) +
141948     (((i64)p[5]) << 16) +
141949     (((i64)p[6]) <<  8) +
141950     (((i64)p[7]) <<  0)
141951   );
141952 }
141953 
141954 /*
141955 ** Functions to serialize a 16 bit integer, 32 bit real number and
141956 ** 64 bit integer. The value returned is the number of bytes written
141957 ** to the argument buffer (always 2, 4 and 8 respectively).
141958 */
141959 static int writeInt16(u8 *p, int i){
141960   p[0] = (i>> 8)&0xFF;
141961   p[1] = (i>> 0)&0xFF;
141962   return 2;
141963 }
141964 static int writeCoord(u8 *p, RtreeCoord *pCoord){
141965   u32 i;
141966   assert( sizeof(RtreeCoord)==4 );
141967   assert( sizeof(u32)==4 );
141968   i = *(u32 *)pCoord;
141969   p[0] = (i>>24)&0xFF;
141970   p[1] = (i>>16)&0xFF;
141971   p[2] = (i>> 8)&0xFF;
141972   p[3] = (i>> 0)&0xFF;
141973   return 4;
141974 }
141975 static int writeInt64(u8 *p, i64 i){
141976   p[0] = (i>>56)&0xFF;
141977   p[1] = (i>>48)&0xFF;
141978   p[2] = (i>>40)&0xFF;
141979   p[3] = (i>>32)&0xFF;
141980   p[4] = (i>>24)&0xFF;
141981   p[5] = (i>>16)&0xFF;
141982   p[6] = (i>> 8)&0xFF;
141983   p[7] = (i>> 0)&0xFF;
141984   return 8;
141985 }
141986 
141987 /*
141988 ** Increment the reference count of node p.
141989 */
141990 static void nodeReference(RtreeNode *p){
141991   if( p ){
141992     p->nRef++;
141993   }
141994 }
141995 
141996 /*
141997 ** Clear the content of node p (set all bytes to 0x00).
141998 */
141999 static void nodeZero(Rtree *pRtree, RtreeNode *p){
142000   memset(&p->zData[2], 0, pRtree->iNodeSize-2);
142001   p->isDirty = 1;
142002 }
142003 
142004 /*
142005 ** Given a node number iNode, return the corresponding key to use
142006 ** in the Rtree.aHash table.
142007 */
142008 static int nodeHash(i64 iNode){
142009   return (
142010     (iNode>>56) ^ (iNode>>48) ^ (iNode>>40) ^ (iNode>>32) ^
142011     (iNode>>24) ^ (iNode>>16) ^ (iNode>> 8) ^ (iNode>> 0)
142012   ) % HASHSIZE;
142013 }
142014 
142015 /*
142016 ** Search the node hash table for node iNode. If found, return a pointer
142017 ** to it. Otherwise, return 0.
142018 */
142019 static RtreeNode *nodeHashLookup(Rtree *pRtree, i64 iNode){
142020   RtreeNode *p;
142021   for(p=pRtree->aHash[nodeHash(iNode)]; p && p->iNode!=iNode; p=p->pNext);
142022   return p;
142023 }
142024 
142025 /*
142026 ** Add node pNode to the node hash table.
142027 */
142028 static void nodeHashInsert(Rtree *pRtree, RtreeNode *pNode){
142029   int iHash;
142030   assert( pNode->pNext==0 );
142031   iHash = nodeHash(pNode->iNode);
142032   pNode->pNext = pRtree->aHash[iHash];
142033   pRtree->aHash[iHash] = pNode;
142034 }
142035 
142036 /*
142037 ** Remove node pNode from the node hash table.
142038 */
142039 static void nodeHashDelete(Rtree *pRtree, RtreeNode *pNode){
142040   RtreeNode **pp;
142041   if( pNode->iNode!=0 ){
142042     pp = &pRtree->aHash[nodeHash(pNode->iNode)];
142043     for( ; (*pp)!=pNode; pp = &(*pp)->pNext){ assert(*pp); }
142044     *pp = pNode->pNext;
142045     pNode->pNext = 0;
142046   }
142047 }
142048 
142049 /*
142050 ** Allocate and return new r-tree node. Initially, (RtreeNode.iNode==0),
142051 ** indicating that node has not yet been assigned a node number. It is
142052 ** assigned a node number when nodeWrite() is called to write the
142053 ** node contents out to the database.
142054 */
142055 static RtreeNode *nodeNew(Rtree *pRtree, RtreeNode *pParent){
142056   RtreeNode *pNode;
142057   pNode = (RtreeNode *)sqlite3_malloc(sizeof(RtreeNode) + pRtree->iNodeSize);
142058   if( pNode ){
142059     memset(pNode, 0, sizeof(RtreeNode) + pRtree->iNodeSize);
142060     pNode->zData = (u8 *)&pNode[1];
142061     pNode->nRef = 1;
142062     pNode->pParent = pParent;
142063     pNode->isDirty = 1;
142064     nodeReference(pParent);
142065   }
142066   return pNode;
142067 }
142068 
142069 /*
142070 ** Obtain a reference to an r-tree node.
142071 */
142072 static int
142073 nodeAcquire(
142074   Rtree *pRtree,             /* R-tree structure */
142075   i64 iNode,                 /* Node number to load */
142076   RtreeNode *pParent,        /* Either the parent node or NULL */
142077   RtreeNode **ppNode         /* OUT: Acquired node */
142078 ){
142079   int rc;
142080   int rc2 = SQLITE_OK;
142081   RtreeNode *pNode;
142082 
142083   /* Check if the requested node is already in the hash table. If so,
142084   ** increase its reference count and return it.
142085   */
142086   if( (pNode = nodeHashLookup(pRtree, iNode)) ){
142087     assert( !pParent || !pNode->pParent || pNode->pParent==pParent );
142088     if( pParent && !pNode->pParent ){
142089       nodeReference(pParent);
142090       pNode->pParent = pParent;
142091     }
142092     pNode->nRef++;
142093     *ppNode = pNode;
142094     return SQLITE_OK;
142095   }
142096 
142097   sqlite3_bind_int64(pRtree->pReadNode, 1, iNode);
142098   rc = sqlite3_step(pRtree->pReadNode);
142099   if( rc==SQLITE_ROW ){
142100     const u8 *zBlob = sqlite3_column_blob(pRtree->pReadNode, 0);
142101     if( pRtree->iNodeSize==sqlite3_column_bytes(pRtree->pReadNode, 0) ){
142102       pNode = (RtreeNode *)sqlite3_malloc(sizeof(RtreeNode)+pRtree->iNodeSize);
142103       if( !pNode ){
142104         rc2 = SQLITE_NOMEM;
142105       }else{
142106         pNode->pParent = pParent;
142107         pNode->zData = (u8 *)&pNode[1];
142108         pNode->nRef = 1;
142109         pNode->iNode = iNode;
142110         pNode->isDirty = 0;
142111         pNode->pNext = 0;
142112         memcpy(pNode->zData, zBlob, pRtree->iNodeSize);
142113         nodeReference(pParent);
142114       }
142115     }
142116   }
142117   rc = sqlite3_reset(pRtree->pReadNode);
142118   if( rc==SQLITE_OK ) rc = rc2;
142119 
142120   /* If the root node was just loaded, set pRtree->iDepth to the height
142121   ** of the r-tree structure. A height of zero means all data is stored on
142122   ** the root node. A height of one means the children of the root node
142123   ** are the leaves, and so on. If the depth as specified on the root node
142124   ** is greater than RTREE_MAX_DEPTH, the r-tree structure must be corrupt.
142125   */
142126   if( pNode && iNode==1 ){
142127     pRtree->iDepth = readInt16(pNode->zData);
142128     if( pRtree->iDepth>RTREE_MAX_DEPTH ){
142129       rc = SQLITE_CORRUPT_VTAB;
142130     }
142131   }
142132 
142133   /* If no error has occurred so far, check if the "number of entries"
142134   ** field on the node is too large. If so, set the return code to
142135   ** SQLITE_CORRUPT_VTAB.
142136   */
142137   if( pNode && rc==SQLITE_OK ){
142138     if( NCELL(pNode)>((pRtree->iNodeSize-4)/pRtree->nBytesPerCell) ){
142139       rc = SQLITE_CORRUPT_VTAB;
142140     }
142141   }
142142 
142143   if( rc==SQLITE_OK ){
142144     if( pNode!=0 ){
142145       nodeHashInsert(pRtree, pNode);
142146     }else{
142147       rc = SQLITE_CORRUPT_VTAB;
142148     }
142149     *ppNode = pNode;
142150   }else{
142151     sqlite3_free(pNode);
142152     *ppNode = 0;
142153   }
142154 
142155   return rc;
142156 }
142157 
142158 /*
142159 ** Overwrite cell iCell of node pNode with the contents of pCell.
142160 */
142161 static void nodeOverwriteCell(
142162   Rtree *pRtree,
142163   RtreeNode *pNode,
142164   RtreeCell *pCell,
142165   int iCell
142166 ){
142167   int ii;
142168   u8 *p = &pNode->zData[4 + pRtree->nBytesPerCell*iCell];
142169   p += writeInt64(p, pCell->iRowid);
142170   for(ii=0; ii<(pRtree->nDim*2); ii++){
142171     p += writeCoord(p, &pCell->aCoord[ii]);
142172   }
142173   pNode->isDirty = 1;
142174 }
142175 
142176 /*
142177 ** Remove cell the cell with index iCell from node pNode.
142178 */
142179 static void nodeDeleteCell(Rtree *pRtree, RtreeNode *pNode, int iCell){
142180   u8 *pDst = &pNode->zData[4 + pRtree->nBytesPerCell*iCell];
142181   u8 *pSrc = &pDst[pRtree->nBytesPerCell];
142182   int nByte = (NCELL(pNode) - iCell - 1) * pRtree->nBytesPerCell;
142183   memmove(pDst, pSrc, nByte);
142184   writeInt16(&pNode->zData[2], NCELL(pNode)-1);
142185   pNode->isDirty = 1;
142186 }
142187 
142188 /*
142189 ** Insert the contents of cell pCell into node pNode. If the insert
142190 ** is successful, return SQLITE_OK.
142191 **
142192 ** If there is not enough free space in pNode, return SQLITE_FULL.
142193 */
142194 static int
142195 nodeInsertCell(
142196   Rtree *pRtree,
142197   RtreeNode *pNode,
142198   RtreeCell *pCell
142199 ){
142200   int nCell;                    /* Current number of cells in pNode */
142201   int nMaxCell;                 /* Maximum number of cells for pNode */
142202 
142203   nMaxCell = (pRtree->iNodeSize-4)/pRtree->nBytesPerCell;
142204   nCell = NCELL(pNode);
142205 
142206   assert( nCell<=nMaxCell );
142207   if( nCell<nMaxCell ){
142208     nodeOverwriteCell(pRtree, pNode, pCell, nCell);
142209     writeInt16(&pNode->zData[2], nCell+1);
142210     pNode->isDirty = 1;
142211   }
142212 
142213   return (nCell==nMaxCell);
142214 }
142215 
142216 /*
142217 ** If the node is dirty, write it out to the database.
142218 */
142219 static int
142220 nodeWrite(Rtree *pRtree, RtreeNode *pNode){
142221   int rc = SQLITE_OK;
142222   if( pNode->isDirty ){
142223     sqlite3_stmt *p = pRtree->pWriteNode;
142224     if( pNode->iNode ){
142225       sqlite3_bind_int64(p, 1, pNode->iNode);
142226     }else{
142227       sqlite3_bind_null(p, 1);
142228     }
142229     sqlite3_bind_blob(p, 2, pNode->zData, pRtree->iNodeSize, SQLITE_STATIC);
142230     sqlite3_step(p);
142231     pNode->isDirty = 0;
142232     rc = sqlite3_reset(p);
142233     if( pNode->iNode==0 && rc==SQLITE_OK ){
142234       pNode->iNode = sqlite3_last_insert_rowid(pRtree->db);
142235       nodeHashInsert(pRtree, pNode);
142236     }
142237   }
142238   return rc;
142239 }
142240 
142241 /*
142242 ** Release a reference to a node. If the node is dirty and the reference
142243 ** count drops to zero, the node data is written to the database.
142244 */
142245 static int
142246 nodeRelease(Rtree *pRtree, RtreeNode *pNode){
142247   int rc = SQLITE_OK;
142248   if( pNode ){
142249     assert( pNode->nRef>0 );
142250     pNode->nRef--;
142251     if( pNode->nRef==0 ){
142252       if( pNode->iNode==1 ){
142253         pRtree->iDepth = -1;
142254       }
142255       if( pNode->pParent ){
142256         rc = nodeRelease(pRtree, pNode->pParent);
142257       }
142258       if( rc==SQLITE_OK ){
142259         rc = nodeWrite(pRtree, pNode);
142260       }
142261       nodeHashDelete(pRtree, pNode);
142262       sqlite3_free(pNode);
142263     }
142264   }
142265   return rc;
142266 }
142267 
142268 /*
142269 ** Return the 64-bit integer value associated with cell iCell of
142270 ** node pNode. If pNode is a leaf node, this is a rowid. If it is
142271 ** an internal node, then the 64-bit integer is a child page number.
142272 */
142273 static i64 nodeGetRowid(
142274   Rtree *pRtree,
142275   RtreeNode *pNode,
142276   int iCell
142277 ){
142278   assert( iCell<NCELL(pNode) );
142279   return readInt64(&pNode->zData[4 + pRtree->nBytesPerCell*iCell]);
142280 }
142281 
142282 /*
142283 ** Return coordinate iCoord from cell iCell in node pNode.
142284 */
142285 static void nodeGetCoord(
142286   Rtree *pRtree,
142287   RtreeNode *pNode,
142288   int iCell,
142289   int iCoord,
142290   RtreeCoord *pCoord           /* Space to write result to */
142291 ){
142292   readCoord(&pNode->zData[12 + pRtree->nBytesPerCell*iCell + 4*iCoord], pCoord);
142293 }
142294 
142295 /*
142296 ** Deserialize cell iCell of node pNode. Populate the structure pointed
142297 ** to by pCell with the results.
142298 */
142299 static void nodeGetCell(
142300   Rtree *pRtree,
142301   RtreeNode *pNode,
142302   int iCell,
142303   RtreeCell *pCell
142304 ){
142305   int ii;
142306   pCell->iRowid = nodeGetRowid(pRtree, pNode, iCell);
142307   for(ii=0; ii<pRtree->nDim*2; ii++){
142308     nodeGetCoord(pRtree, pNode, iCell, ii, &pCell->aCoord[ii]);
142309   }
142310 }
142311 
142312 
142313 /* Forward declaration for the function that does the work of
142314 ** the virtual table module xCreate() and xConnect() methods.
142315 */
142316 static int rtreeInit(
142317   sqlite3 *, void *, int, const char *const*, sqlite3_vtab **, char **, int
142318 );
142319 
142320 /*
142321 ** Rtree virtual table module xCreate method.
142322 */
142323 static int rtreeCreate(
142324   sqlite3 *db,
142325   void *pAux,
142326   int argc, const char *const*argv,
142327   sqlite3_vtab **ppVtab,
142328   char **pzErr
142329 ){
142330   return rtreeInit(db, pAux, argc, argv, ppVtab, pzErr, 1);
142331 }
142332 
142333 /*
142334 ** Rtree virtual table module xConnect method.
142335 */
142336 static int rtreeConnect(
142337   sqlite3 *db,
142338   void *pAux,
142339   int argc, const char *const*argv,
142340   sqlite3_vtab **ppVtab,
142341   char **pzErr
142342 ){
142343   return rtreeInit(db, pAux, argc, argv, ppVtab, pzErr, 0);
142344 }
142345 
142346 /*
142347 ** Increment the r-tree reference count.
142348 */
142349 static void rtreeReference(Rtree *pRtree){
142350   pRtree->nBusy++;
142351 }
142352 
142353 /*
142354 ** Decrement the r-tree reference count. When the reference count reaches
142355 ** zero the structure is deleted.
142356 */
142357 static void rtreeRelease(Rtree *pRtree){
142358   pRtree->nBusy--;
142359   if( pRtree->nBusy==0 ){
142360     sqlite3_finalize(pRtree->pReadNode);
142361     sqlite3_finalize(pRtree->pWriteNode);
142362     sqlite3_finalize(pRtree->pDeleteNode);
142363     sqlite3_finalize(pRtree->pReadRowid);
142364     sqlite3_finalize(pRtree->pWriteRowid);
142365     sqlite3_finalize(pRtree->pDeleteRowid);
142366     sqlite3_finalize(pRtree->pReadParent);
142367     sqlite3_finalize(pRtree->pWriteParent);
142368     sqlite3_finalize(pRtree->pDeleteParent);
142369     sqlite3_free(pRtree);
142370   }
142371 }
142372 
142373 /*
142374 ** Rtree virtual table module xDisconnect method.
142375 */
142376 static int rtreeDisconnect(sqlite3_vtab *pVtab){
142377   rtreeRelease((Rtree *)pVtab);
142378   return SQLITE_OK;
142379 }
142380 
142381 /*
142382 ** Rtree virtual table module xDestroy method.
142383 */
142384 static int rtreeDestroy(sqlite3_vtab *pVtab){
142385   Rtree *pRtree = (Rtree *)pVtab;
142386   int rc;
142387   char *zCreate = sqlite3_mprintf(
142388     "DROP TABLE '%q'.'%q_node';"
142389     "DROP TABLE '%q'.'%q_rowid';"
142390     "DROP TABLE '%q'.'%q_parent';",
142391     pRtree->zDb, pRtree->zName,
142392     pRtree->zDb, pRtree->zName,
142393     pRtree->zDb, pRtree->zName
142394   );
142395   if( !zCreate ){
142396     rc = SQLITE_NOMEM;
142397   }else{
142398     rc = sqlite3_exec(pRtree->db, zCreate, 0, 0, 0);
142399     sqlite3_free(zCreate);
142400   }
142401   if( rc==SQLITE_OK ){
142402     rtreeRelease(pRtree);
142403   }
142404 
142405   return rc;
142406 }
142407 
142408 /*
142409 ** Rtree virtual table module xOpen method.
142410 */
142411 static int rtreeOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
142412   int rc = SQLITE_NOMEM;
142413   RtreeCursor *pCsr;
142414 
142415   pCsr = (RtreeCursor *)sqlite3_malloc(sizeof(RtreeCursor));
142416   if( pCsr ){
142417     memset(pCsr, 0, sizeof(RtreeCursor));
142418     pCsr->base.pVtab = pVTab;
142419     rc = SQLITE_OK;
142420   }
142421   *ppCursor = (sqlite3_vtab_cursor *)pCsr;
142422 
142423   return rc;
142424 }
142425 
142426 
142427 /*
142428 ** Free the RtreeCursor.aConstraint[] array and its contents.
142429 */
142430 static void freeCursorConstraints(RtreeCursor *pCsr){
142431   if( pCsr->aConstraint ){
142432     int i;                        /* Used to iterate through constraint array */
142433     for(i=0; i<pCsr->nConstraint; i++){
142434       sqlite3_rtree_geometry *pGeom = pCsr->aConstraint[i].pGeom;
142435       if( pGeom ){
142436         if( pGeom->xDelUser ) pGeom->xDelUser(pGeom->pUser);
142437         sqlite3_free(pGeom);
142438       }
142439     }
142440     sqlite3_free(pCsr->aConstraint);
142441     pCsr->aConstraint = 0;
142442   }
142443 }
142444 
142445 /*
142446 ** Rtree virtual table module xClose method.
142447 */
142448 static int rtreeClose(sqlite3_vtab_cursor *cur){
142449   Rtree *pRtree = (Rtree *)(cur->pVtab);
142450   int rc;
142451   RtreeCursor *pCsr = (RtreeCursor *)cur;
142452   freeCursorConstraints(pCsr);
142453   rc = nodeRelease(pRtree, pCsr->pNode);
142454   sqlite3_free(pCsr);
142455   return rc;
142456 }
142457 
142458 /*
142459 ** Rtree virtual table module xEof method.
142460 **
142461 ** Return non-zero if the cursor does not currently point to a valid
142462 ** record (i.e if the scan has finished), or zero otherwise.
142463 */
142464 static int rtreeEof(sqlite3_vtab_cursor *cur){
142465   RtreeCursor *pCsr = (RtreeCursor *)cur;
142466   return (pCsr->pNode==0);
142467 }
142468 
142469 /*
142470 ** The r-tree constraint passed as the second argument to this function is
142471 ** guaranteed to be a MATCH constraint.
142472 */
142473 static int testRtreeGeom(
142474   Rtree *pRtree,                  /* R-Tree object */
142475   RtreeConstraint *pConstraint,   /* MATCH constraint to test */
142476   RtreeCell *pCell,               /* Cell to test */
142477   int *pbRes                      /* OUT: Test result */
142478 ){
142479   int i;
142480   RtreeDValue aCoord[RTREE_MAX_DIMENSIONS*2];
142481   int nCoord = pRtree->nDim*2;
142482 
142483   assert( pConstraint->op==RTREE_MATCH );
142484   assert( pConstraint->pGeom );
142485 
142486   for(i=0; i<nCoord; i++){
142487     aCoord[i] = DCOORD(pCell->aCoord[i]);
142488   }
142489   return pConstraint->xGeom(pConstraint->pGeom, nCoord, aCoord, pbRes);
142490 }
142491 
142492 /*
142493 ** Cursor pCursor currently points to a cell in a non-leaf page.
142494 ** Set *pbEof to true if the sub-tree headed by the cell is filtered
142495 ** (excluded) by the constraints in the pCursor->aConstraint[]
142496 ** array, or false otherwise.
142497 **
142498 ** Return SQLITE_OK if successful or an SQLite error code if an error
142499 ** occurs within a geometry callback.
142500 */
142501 static int testRtreeCell(Rtree *pRtree, RtreeCursor *pCursor, int *pbEof){
142502   RtreeCell cell;
142503   int ii;
142504   int bRes = 0;
142505   int rc = SQLITE_OK;
142506 
142507   nodeGetCell(pRtree, pCursor->pNode, pCursor->iCell, &cell);
142508   for(ii=0; bRes==0 && ii<pCursor->nConstraint; ii++){
142509     RtreeConstraint *p = &pCursor->aConstraint[ii];
142510     RtreeDValue cell_min = DCOORD(cell.aCoord[(p->iCoord>>1)*2]);
142511     RtreeDValue cell_max = DCOORD(cell.aCoord[(p->iCoord>>1)*2+1]);
142512 
142513     assert(p->op==RTREE_LE || p->op==RTREE_LT || p->op==RTREE_GE
142514         || p->op==RTREE_GT || p->op==RTREE_EQ || p->op==RTREE_MATCH
142515     );
142516 
142517     switch( p->op ){
142518       case RTREE_LE: case RTREE_LT:
142519         bRes = p->rValue<cell_min;
142520         break;
142521 
142522       case RTREE_GE: case RTREE_GT:
142523         bRes = p->rValue>cell_max;
142524         break;
142525 
142526       case RTREE_EQ:
142527         bRes = (p->rValue>cell_max || p->rValue<cell_min);
142528         break;
142529 
142530       default: {
142531         assert( p->op==RTREE_MATCH );
142532         rc = testRtreeGeom(pRtree, p, &cell, &bRes);
142533         bRes = !bRes;
142534         break;
142535       }
142536     }
142537   }
142538 
142539   *pbEof = bRes;
142540   return rc;
142541 }
142542 
142543 /*
142544 ** Test if the cell that cursor pCursor currently points to
142545 ** would be filtered (excluded) by the constraints in the
142546 ** pCursor->aConstraint[] array. If so, set *pbEof to true before
142547 ** returning. If the cell is not filtered (excluded) by the constraints,
142548 ** set pbEof to zero.
142549 **
142550 ** Return SQLITE_OK if successful or an SQLite error code if an error
142551 ** occurs within a geometry callback.
142552 **
142553 ** This function assumes that the cell is part of a leaf node.
142554 */
142555 static int testRtreeEntry(Rtree *pRtree, RtreeCursor *pCursor, int *pbEof){
142556   RtreeCell cell;
142557   int ii;
142558   *pbEof = 0;
142559 
142560   nodeGetCell(pRtree, pCursor->pNode, pCursor->iCell, &cell);
142561   for(ii=0; ii<pCursor->nConstraint; ii++){
142562     RtreeConstraint *p = &pCursor->aConstraint[ii];
142563     RtreeDValue coord = DCOORD(cell.aCoord[p->iCoord]);
142564     int res;
142565     assert(p->op==RTREE_LE || p->op==RTREE_LT || p->op==RTREE_GE
142566         || p->op==RTREE_GT || p->op==RTREE_EQ || p->op==RTREE_MATCH
142567     );
142568     switch( p->op ){
142569       case RTREE_LE: res = (coord<=p->rValue); break;
142570       case RTREE_LT: res = (coord<p->rValue);  break;
142571       case RTREE_GE: res = (coord>=p->rValue); break;
142572       case RTREE_GT: res = (coord>p->rValue);  break;
142573       case RTREE_EQ: res = (coord==p->rValue); break;
142574       default: {
142575         int rc;
142576         assert( p->op==RTREE_MATCH );
142577         rc = testRtreeGeom(pRtree, p, &cell, &res);
142578         if( rc!=SQLITE_OK ){
142579           return rc;
142580         }
142581         break;
142582       }
142583     }
142584 
142585     if( !res ){
142586       *pbEof = 1;
142587       return SQLITE_OK;
142588     }
142589   }
142590 
142591   return SQLITE_OK;
142592 }
142593 
142594 /*
142595 ** Cursor pCursor currently points at a node that heads a sub-tree of
142596 ** height iHeight (if iHeight==0, then the node is a leaf). Descend
142597 ** to point to the left-most cell of the sub-tree that matches the
142598 ** configured constraints.
142599 */
142600 static int descendToCell(
142601   Rtree *pRtree,
142602   RtreeCursor *pCursor,
142603   int iHeight,
142604   int *pEof                 /* OUT: Set to true if cannot descend */
142605 ){
142606   int isEof;
142607   int rc;
142608   int ii;
142609   RtreeNode *pChild;
142610   sqlite3_int64 iRowid;
142611 
142612   RtreeNode *pSavedNode = pCursor->pNode;
142613   int iSavedCell = pCursor->iCell;
142614 
142615   assert( iHeight>=0 );
142616 
142617   if( iHeight==0 ){
142618     rc = testRtreeEntry(pRtree, pCursor, &isEof);
142619   }else{
142620     rc = testRtreeCell(pRtree, pCursor, &isEof);
142621   }
142622   if( rc!=SQLITE_OK || isEof || iHeight==0 ){
142623     goto descend_to_cell_out;
142624   }
142625 
142626   iRowid = nodeGetRowid(pRtree, pCursor->pNode, pCursor->iCell);
142627   rc = nodeAcquire(pRtree, iRowid, pCursor->pNode, &pChild);
142628   if( rc!=SQLITE_OK ){
142629     goto descend_to_cell_out;
142630   }
142631 
142632   nodeRelease(pRtree, pCursor->pNode);
142633   pCursor->pNode = pChild;
142634   isEof = 1;
142635   for(ii=0; isEof && ii<NCELL(pChild); ii++){
142636     pCursor->iCell = ii;
142637     rc = descendToCell(pRtree, pCursor, iHeight-1, &isEof);
142638     if( rc!=SQLITE_OK ){
142639       goto descend_to_cell_out;
142640     }
142641   }
142642 
142643   if( isEof ){
142644     assert( pCursor->pNode==pChild );
142645     nodeReference(pSavedNode);
142646     nodeRelease(pRtree, pChild);
142647     pCursor->pNode = pSavedNode;
142648     pCursor->iCell = iSavedCell;
142649   }
142650 
142651 descend_to_cell_out:
142652   *pEof = isEof;
142653   return rc;
142654 }
142655 
142656 /*
142657 ** One of the cells in node pNode is guaranteed to have a 64-bit
142658 ** integer value equal to iRowid. Return the index of this cell.
142659 */
142660 static int nodeRowidIndex(
142661   Rtree *pRtree,
142662   RtreeNode *pNode,
142663   i64 iRowid,
142664   int *piIndex
142665 ){
142666   int ii;
142667   int nCell = NCELL(pNode);
142668   for(ii=0; ii<nCell; ii++){
142669     if( nodeGetRowid(pRtree, pNode, ii)==iRowid ){
142670       *piIndex = ii;
142671       return SQLITE_OK;
142672     }
142673   }
142674   return SQLITE_CORRUPT_VTAB;
142675 }
142676 
142677 /*
142678 ** Return the index of the cell containing a pointer to node pNode
142679 ** in its parent. If pNode is the root node, return -1.
142680 */
142681 static int nodeParentIndex(Rtree *pRtree, RtreeNode *pNode, int *piIndex){
142682   RtreeNode *pParent = pNode->pParent;
142683   if( pParent ){
142684     return nodeRowidIndex(pRtree, pParent, pNode->iNode, piIndex);
142685   }
142686   *piIndex = -1;
142687   return SQLITE_OK;
142688 }
142689 
142690 /*
142691 ** Rtree virtual table module xNext method.
142692 */
142693 static int rtreeNext(sqlite3_vtab_cursor *pVtabCursor){
142694   Rtree *pRtree = (Rtree *)(pVtabCursor->pVtab);
142695   RtreeCursor *pCsr = (RtreeCursor *)pVtabCursor;
142696   int rc = SQLITE_OK;
142697 
142698   /* RtreeCursor.pNode must not be NULL. If is is NULL, then this cursor is
142699   ** already at EOF. It is against the rules to call the xNext() method of
142700   ** a cursor that has already reached EOF.
142701   */
142702   assert( pCsr->pNode );
142703 
142704   if( pCsr->iStrategy==1 ){
142705     /* This "scan" is a direct lookup by rowid. There is no next entry. */
142706     nodeRelease(pRtree, pCsr->pNode);
142707     pCsr->pNode = 0;
142708   }else{
142709     /* Move to the next entry that matches the configured constraints. */
142710     int iHeight = 0;
142711     while( pCsr->pNode ){
142712       RtreeNode *pNode = pCsr->pNode;
142713       int nCell = NCELL(pNode);
142714       for(pCsr->iCell++; pCsr->iCell<nCell; pCsr->iCell++){
142715         int isEof;
142716         rc = descendToCell(pRtree, pCsr, iHeight, &isEof);
142717         if( rc!=SQLITE_OK || !isEof ){
142718           return rc;
142719         }
142720       }
142721       pCsr->pNode = pNode->pParent;
142722       rc = nodeParentIndex(pRtree, pNode, &pCsr->iCell);
142723       if( rc!=SQLITE_OK ){
142724         return rc;
142725       }
142726       nodeReference(pCsr->pNode);
142727       nodeRelease(pRtree, pNode);
142728       iHeight++;
142729     }
142730   }
142731 
142732   return rc;
142733 }
142734 
142735 /*
142736 ** Rtree virtual table module xRowid method.
142737 */
142738 static int rtreeRowid(sqlite3_vtab_cursor *pVtabCursor, sqlite_int64 *pRowid){
142739   Rtree *pRtree = (Rtree *)pVtabCursor->pVtab;
142740   RtreeCursor *pCsr = (RtreeCursor *)pVtabCursor;
142741 
142742   assert(pCsr->pNode);
142743   *pRowid = nodeGetRowid(pRtree, pCsr->pNode, pCsr->iCell);
142744 
142745   return SQLITE_OK;
142746 }
142747 
142748 /*
142749 ** Rtree virtual table module xColumn method.
142750 */
142751 static int rtreeColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
142752   Rtree *pRtree = (Rtree *)cur->pVtab;
142753   RtreeCursor *pCsr = (RtreeCursor *)cur;
142754 
142755   if( i==0 ){
142756     i64 iRowid = nodeGetRowid(pRtree, pCsr->pNode, pCsr->iCell);
142757     sqlite3_result_int64(ctx, iRowid);
142758   }else{
142759     RtreeCoord c;
142760     nodeGetCoord(pRtree, pCsr->pNode, pCsr->iCell, i-1, &c);
142761 #ifndef SQLITE_RTREE_INT_ONLY
142762     if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
142763       sqlite3_result_double(ctx, c.f);
142764     }else
142765 #endif
142766     {
142767       assert( pRtree->eCoordType==RTREE_COORD_INT32 );
142768       sqlite3_result_int(ctx, c.i);
142769     }
142770   }
142771 
142772   return SQLITE_OK;
142773 }
142774 
142775 /*
142776 ** Use nodeAcquire() to obtain the leaf node containing the record with
142777 ** rowid iRowid. If successful, set *ppLeaf to point to the node and
142778 ** return SQLITE_OK. If there is no such record in the table, set
142779 ** *ppLeaf to 0 and return SQLITE_OK. If an error occurs, set *ppLeaf
142780 ** to zero and return an SQLite error code.
142781 */
142782 static int findLeafNode(Rtree *pRtree, i64 iRowid, RtreeNode **ppLeaf){
142783   int rc;
142784   *ppLeaf = 0;
142785   sqlite3_bind_int64(pRtree->pReadRowid, 1, iRowid);
142786   if( sqlite3_step(pRtree->pReadRowid)==SQLITE_ROW ){
142787     i64 iNode = sqlite3_column_int64(pRtree->pReadRowid, 0);
142788     rc = nodeAcquire(pRtree, iNode, 0, ppLeaf);
142789     sqlite3_reset(pRtree->pReadRowid);
142790   }else{
142791     rc = sqlite3_reset(pRtree->pReadRowid);
142792   }
142793   return rc;
142794 }
142795 
142796 /*
142797 ** This function is called to configure the RtreeConstraint object passed
142798 ** as the second argument for a MATCH constraint. The value passed as the
142799 ** first argument to this function is the right-hand operand to the MATCH
142800 ** operator.
142801 */
142802 static int deserializeGeometry(sqlite3_value *pValue, RtreeConstraint *pCons){
142803   RtreeMatchArg *p;
142804   sqlite3_rtree_geometry *pGeom;
142805   int nBlob;
142806 
142807   /* Check that value is actually a blob. */
142808   if( sqlite3_value_type(pValue)!=SQLITE_BLOB ) return SQLITE_ERROR;
142809 
142810   /* Check that the blob is roughly the right size. */
142811   nBlob = sqlite3_value_bytes(pValue);
142812   if( nBlob<(int)sizeof(RtreeMatchArg)
142813    || ((nBlob-sizeof(RtreeMatchArg))%sizeof(RtreeDValue))!=0
142814   ){
142815     return SQLITE_ERROR;
142816   }
142817 
142818   pGeom = (sqlite3_rtree_geometry *)sqlite3_malloc(
142819       sizeof(sqlite3_rtree_geometry) + nBlob
142820   );
142821   if( !pGeom ) return SQLITE_NOMEM;
142822   memset(pGeom, 0, sizeof(sqlite3_rtree_geometry));
142823   p = (RtreeMatchArg *)&pGeom[1];
142824 
142825   memcpy(p, sqlite3_value_blob(pValue), nBlob);
142826   if( p->magic!=RTREE_GEOMETRY_MAGIC
142827    || nBlob!=(int)(sizeof(RtreeMatchArg) + (p->nParam-1)*sizeof(RtreeDValue))
142828   ){
142829     sqlite3_free(pGeom);
142830     return SQLITE_ERROR;
142831   }
142832 
142833   pGeom->pContext = p->pContext;
142834   pGeom->nParam = p->nParam;
142835   pGeom->aParam = p->aParam;
142836 
142837   pCons->xGeom = p->xGeom;
142838   pCons->pGeom = pGeom;
142839   return SQLITE_OK;
142840 }
142841 
142842 /*
142843 ** Rtree virtual table module xFilter method.
142844 */
142845 static int rtreeFilter(
142846   sqlite3_vtab_cursor *pVtabCursor,
142847   int idxNum, const char *idxStr,
142848   int argc, sqlite3_value **argv
142849 ){
142850   Rtree *pRtree = (Rtree *)pVtabCursor->pVtab;
142851   RtreeCursor *pCsr = (RtreeCursor *)pVtabCursor;
142852 
142853   RtreeNode *pRoot = 0;
142854   int ii;
142855   int rc = SQLITE_OK;
142856 
142857   rtreeReference(pRtree);
142858 
142859   freeCursorConstraints(pCsr);
142860   pCsr->iStrategy = idxNum;
142861 
142862   if( idxNum==1 ){
142863     /* Special case - lookup by rowid. */
142864     RtreeNode *pLeaf;        /* Leaf on which the required cell resides */
142865     i64 iRowid = sqlite3_value_int64(argv[0]);
142866     rc = findLeafNode(pRtree, iRowid, &pLeaf);
142867     pCsr->pNode = pLeaf;
142868     if( pLeaf ){
142869       assert( rc==SQLITE_OK );
142870       rc = nodeRowidIndex(pRtree, pLeaf, iRowid, &pCsr->iCell);
142871     }
142872   }else{
142873     /* Normal case - r-tree scan. Set up the RtreeCursor.aConstraint array
142874     ** with the configured constraints.
142875     */
142876     if( argc>0 ){
142877       pCsr->aConstraint = sqlite3_malloc(sizeof(RtreeConstraint)*argc);
142878       pCsr->nConstraint = argc;
142879       if( !pCsr->aConstraint ){
142880         rc = SQLITE_NOMEM;
142881       }else{
142882         memset(pCsr->aConstraint, 0, sizeof(RtreeConstraint)*argc);
142883         assert( (idxStr==0 && argc==0)
142884                 || (idxStr && (int)strlen(idxStr)==argc*2) );
142885         for(ii=0; ii<argc; ii++){
142886           RtreeConstraint *p = &pCsr->aConstraint[ii];
142887           p->op = idxStr[ii*2];
142888           p->iCoord = idxStr[ii*2+1]-'a';
142889           if( p->op==RTREE_MATCH ){
142890             /* A MATCH operator. The right-hand-side must be a blob that
142891             ** can be cast into an RtreeMatchArg object. One created using
142892             ** an sqlite3_rtree_geometry_callback() SQL user function.
142893             */
142894             rc = deserializeGeometry(argv[ii], p);
142895             if( rc!=SQLITE_OK ){
142896               break;
142897             }
142898           }else{
142899 #ifdef SQLITE_RTREE_INT_ONLY
142900             p->rValue = sqlite3_value_int64(argv[ii]);
142901 #else
142902             p->rValue = sqlite3_value_double(argv[ii]);
142903 #endif
142904           }
142905         }
142906       }
142907     }
142908 
142909     if( rc==SQLITE_OK ){
142910       pCsr->pNode = 0;
142911       rc = nodeAcquire(pRtree, 1, 0, &pRoot);
142912     }
142913     if( rc==SQLITE_OK ){
142914       int isEof = 1;
142915       int nCell = NCELL(pRoot);
142916       pCsr->pNode = pRoot;
142917       for(pCsr->iCell=0; rc==SQLITE_OK && pCsr->iCell<nCell; pCsr->iCell++){
142918         assert( pCsr->pNode==pRoot );
142919         rc = descendToCell(pRtree, pCsr, pRtree->iDepth, &isEof);
142920         if( !isEof ){
142921           break;
142922         }
142923       }
142924       if( rc==SQLITE_OK && isEof ){
142925         assert( pCsr->pNode==pRoot );
142926         nodeRelease(pRtree, pRoot);
142927         pCsr->pNode = 0;
142928       }
142929       assert( rc!=SQLITE_OK || !pCsr->pNode || pCsr->iCell<NCELL(pCsr->pNode) );
142930     }
142931   }
142932 
142933   rtreeRelease(pRtree);
142934   return rc;
142935 }
142936 
142937 /*
142938 ** Set the pIdxInfo->estimatedRows variable to nRow. Unless this
142939 ** extension is currently being used by a version of SQLite too old to
142940 ** support estimatedRows. In that case this function is a no-op.
142941 */
142942 static void setEstimatedRows(sqlite3_index_info *pIdxInfo, i64 nRow){
142943 #if SQLITE_VERSION_NUMBER>=3008002
142944   if( sqlite3_libversion_number()>=3008002 ){
142945     pIdxInfo->estimatedRows = nRow;
142946   }
142947 #endif
142948 }
142949 
142950 /*
142951 ** Rtree virtual table module xBestIndex method. There are three
142952 ** table scan strategies to choose from (in order from most to
142953 ** least desirable):
142954 **
142955 **   idxNum     idxStr        Strategy
142956 **   ------------------------------------------------
142957 **     1        Unused        Direct lookup by rowid.
142958 **     2        See below     R-tree query or full-table scan.
142959 **   ------------------------------------------------
142960 **
142961 ** If strategy 1 is used, then idxStr is not meaningful. If strategy
142962 ** 2 is used, idxStr is formatted to contain 2 bytes for each
142963 ** constraint used. The first two bytes of idxStr correspond to
142964 ** the constraint in sqlite3_index_info.aConstraintUsage[] with
142965 ** (argvIndex==1) etc.
142966 **
142967 ** The first of each pair of bytes in idxStr identifies the constraint
142968 ** operator as follows:
142969 **
142970 **   Operator    Byte Value
142971 **   ----------------------
142972 **      =        0x41 ('A')
142973 **     <=        0x42 ('B')
142974 **      <        0x43 ('C')
142975 **     >=        0x44 ('D')
142976 **      >        0x45 ('E')
142977 **   MATCH       0x46 ('F')
142978 **   ----------------------
142979 **
142980 ** The second of each pair of bytes identifies the coordinate column
142981 ** to which the constraint applies. The leftmost coordinate column
142982 ** is 'a', the second from the left 'b' etc.
142983 */
142984 static int rtreeBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
142985   Rtree *pRtree = (Rtree*)tab;
142986   int rc = SQLITE_OK;
142987   int ii;
142988   i64 nRow;                       /* Estimated rows returned by this scan */
142989 
142990   int iIdx = 0;
142991   char zIdxStr[RTREE_MAX_DIMENSIONS*8+1];
142992   memset(zIdxStr, 0, sizeof(zIdxStr));
142993 
142994   assert( pIdxInfo->idxStr==0 );
142995   for(ii=0; ii<pIdxInfo->nConstraint && iIdx<(int)(sizeof(zIdxStr)-1); ii++){
142996     struct sqlite3_index_constraint *p = &pIdxInfo->aConstraint[ii];
142997 
142998     if( p->usable && p->iColumn==0 && p->op==SQLITE_INDEX_CONSTRAINT_EQ ){
142999       /* We have an equality constraint on the rowid. Use strategy 1. */
143000       int jj;
143001       for(jj=0; jj<ii; jj++){
143002         pIdxInfo->aConstraintUsage[jj].argvIndex = 0;
143003         pIdxInfo->aConstraintUsage[jj].omit = 0;
143004       }
143005       pIdxInfo->idxNum = 1;
143006       pIdxInfo->aConstraintUsage[ii].argvIndex = 1;
143007       pIdxInfo->aConstraintUsage[jj].omit = 1;
143008 
143009       /* This strategy involves a two rowid lookups on an B-Tree structures
143010       ** and then a linear search of an R-Tree node. This should be
143011       ** considered almost as quick as a direct rowid lookup (for which
143012       ** sqlite uses an internal cost of 0.0). It is expected to return
143013       ** a single row.
143014       */
143015       pIdxInfo->estimatedCost = 30.0;
143016       setEstimatedRows(pIdxInfo, 1);
143017       return SQLITE_OK;
143018     }
143019 
143020     if( p->usable && (p->iColumn>0 || p->op==SQLITE_INDEX_CONSTRAINT_MATCH) ){
143021       u8 op;
143022       switch( p->op ){
143023         case SQLITE_INDEX_CONSTRAINT_EQ: op = RTREE_EQ; break;
143024         case SQLITE_INDEX_CONSTRAINT_GT: op = RTREE_GT; break;
143025         case SQLITE_INDEX_CONSTRAINT_LE: op = RTREE_LE; break;
143026         case SQLITE_INDEX_CONSTRAINT_LT: op = RTREE_LT; break;
143027         case SQLITE_INDEX_CONSTRAINT_GE: op = RTREE_GE; break;
143028         default:
143029           assert( p->op==SQLITE_INDEX_CONSTRAINT_MATCH );
143030           op = RTREE_MATCH;
143031           break;
143032       }
143033       zIdxStr[iIdx++] = op;
143034       zIdxStr[iIdx++] = p->iColumn - 1 + 'a';
143035       pIdxInfo->aConstraintUsage[ii].argvIndex = (iIdx/2);
143036       pIdxInfo->aConstraintUsage[ii].omit = 1;
143037     }
143038   }
143039 
143040   pIdxInfo->idxNum = 2;
143041   pIdxInfo->needToFreeIdxStr = 1;
143042   if( iIdx>0 && 0==(pIdxInfo->idxStr = sqlite3_mprintf("%s", zIdxStr)) ){
143043     return SQLITE_NOMEM;
143044   }
143045 
143046   nRow = pRtree->nRowEst / (iIdx + 1);
143047   pIdxInfo->estimatedCost = (double)6.0 * (double)nRow;
143048   setEstimatedRows(pIdxInfo, nRow);
143049 
143050   return rc;
143051 }
143052 
143053 /*
143054 ** Return the N-dimensional volumn of the cell stored in *p.
143055 */
143056 static RtreeDValue cellArea(Rtree *pRtree, RtreeCell *p){
143057   RtreeDValue area = (RtreeDValue)1;
143058   int ii;
143059   for(ii=0; ii<(pRtree->nDim*2); ii+=2){
143060     area = (area * (DCOORD(p->aCoord[ii+1]) - DCOORD(p->aCoord[ii])));
143061   }
143062   return area;
143063 }
143064 
143065 /*
143066 ** Return the margin length of cell p. The margin length is the sum
143067 ** of the objects size in each dimension.
143068 */
143069 static RtreeDValue cellMargin(Rtree *pRtree, RtreeCell *p){
143070   RtreeDValue margin = (RtreeDValue)0;
143071   int ii;
143072   for(ii=0; ii<(pRtree->nDim*2); ii+=2){
143073     margin += (DCOORD(p->aCoord[ii+1]) - DCOORD(p->aCoord[ii]));
143074   }
143075   return margin;
143076 }
143077 
143078 /*
143079 ** Store the union of cells p1 and p2 in p1.
143080 */
143081 static void cellUnion(Rtree *pRtree, RtreeCell *p1, RtreeCell *p2){
143082   int ii;
143083   if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
143084     for(ii=0; ii<(pRtree->nDim*2); ii+=2){
143085       p1->aCoord[ii].f = MIN(p1->aCoord[ii].f, p2->aCoord[ii].f);
143086       p1->aCoord[ii+1].f = MAX(p1->aCoord[ii+1].f, p2->aCoord[ii+1].f);
143087     }
143088   }else{
143089     for(ii=0; ii<(pRtree->nDim*2); ii+=2){
143090       p1->aCoord[ii].i = MIN(p1->aCoord[ii].i, p2->aCoord[ii].i);
143091       p1->aCoord[ii+1].i = MAX(p1->aCoord[ii+1].i, p2->aCoord[ii+1].i);
143092     }
143093   }
143094 }
143095 
143096 /*
143097 ** Return true if the area covered by p2 is a subset of the area covered
143098 ** by p1. False otherwise.
143099 */
143100 static int cellContains(Rtree *pRtree, RtreeCell *p1, RtreeCell *p2){
143101   int ii;
143102   int isInt = (pRtree->eCoordType==RTREE_COORD_INT32);
143103   for(ii=0; ii<(pRtree->nDim*2); ii+=2){
143104     RtreeCoord *a1 = &p1->aCoord[ii];
143105     RtreeCoord *a2 = &p2->aCoord[ii];
143106     if( (!isInt && (a2[0].f<a1[0].f || a2[1].f>a1[1].f))
143107      || ( isInt && (a2[0].i<a1[0].i || a2[1].i>a1[1].i))
143108     ){
143109       return 0;
143110     }
143111   }
143112   return 1;
143113 }
143114 
143115 /*
143116 ** Return the amount cell p would grow by if it were unioned with pCell.
143117 */
143118 static RtreeDValue cellGrowth(Rtree *pRtree, RtreeCell *p, RtreeCell *pCell){
143119   RtreeDValue area;
143120   RtreeCell cell;
143121   memcpy(&cell, p, sizeof(RtreeCell));
143122   area = cellArea(pRtree, &cell);
143123   cellUnion(pRtree, &cell, pCell);
143124   return (cellArea(pRtree, &cell)-area);
143125 }
143126 
143127 #if VARIANT_RSTARTREE_CHOOSESUBTREE || VARIANT_RSTARTREE_SPLIT
143128 static RtreeDValue cellOverlap(
143129   Rtree *pRtree,
143130   RtreeCell *p,
143131   RtreeCell *aCell,
143132   int nCell,
143133   int iExclude
143134 ){
143135   int ii;
143136   RtreeDValue overlap = 0.0;
143137   for(ii=0; ii<nCell; ii++){
143138 #if VARIANT_RSTARTREE_CHOOSESUBTREE
143139     if( ii!=iExclude )
143140 #else
143141     assert( iExclude==-1 );
143142     UNUSED_PARAMETER(iExclude);
143143 #endif
143144     {
143145       int jj;
143146       RtreeDValue o = (RtreeDValue)1;
143147       for(jj=0; jj<(pRtree->nDim*2); jj+=2){
143148         RtreeDValue x1, x2;
143149 
143150         x1 = MAX(DCOORD(p->aCoord[jj]), DCOORD(aCell[ii].aCoord[jj]));
143151         x2 = MIN(DCOORD(p->aCoord[jj+1]), DCOORD(aCell[ii].aCoord[jj+1]));
143152 
143153         if( x2<x1 ){
143154           o = 0.0;
143155           break;
143156         }else{
143157           o = o * (x2-x1);
143158         }
143159       }
143160       overlap += o;
143161     }
143162   }
143163   return overlap;
143164 }
143165 #endif
143166 
143167 #if VARIANT_RSTARTREE_CHOOSESUBTREE
143168 static RtreeDValue cellOverlapEnlargement(
143169   Rtree *pRtree,
143170   RtreeCell *p,
143171   RtreeCell *pInsert,
143172   RtreeCell *aCell,
143173   int nCell,
143174   int iExclude
143175 ){
143176   RtreeDValue before, after;
143177   before = cellOverlap(pRtree, p, aCell, nCell, iExclude);
143178   cellUnion(pRtree, p, pInsert);
143179   after = cellOverlap(pRtree, p, aCell, nCell, iExclude);
143180   return (after-before);
143181 }
143182 #endif
143183 
143184 
143185 /*
143186 ** This function implements the ChooseLeaf algorithm from Gutman[84].
143187 ** ChooseSubTree in r*tree terminology.
143188 */
143189 static int ChooseLeaf(
143190   Rtree *pRtree,               /* Rtree table */
143191   RtreeCell *pCell,            /* Cell to insert into rtree */
143192   int iHeight,                 /* Height of sub-tree rooted at pCell */
143193   RtreeNode **ppLeaf           /* OUT: Selected leaf page */
143194 ){
143195   int rc;
143196   int ii;
143197   RtreeNode *pNode;
143198   rc = nodeAcquire(pRtree, 1, 0, &pNode);
143199 
143200   for(ii=0; rc==SQLITE_OK && ii<(pRtree->iDepth-iHeight); ii++){
143201     int iCell;
143202     sqlite3_int64 iBest = 0;
143203 
143204     RtreeDValue fMinGrowth = 0.0;
143205     RtreeDValue fMinArea = 0.0;
143206 #if VARIANT_RSTARTREE_CHOOSESUBTREE
143207     RtreeDValue fMinOverlap = 0.0;
143208     RtreeDValue overlap;
143209 #endif
143210 
143211     int nCell = NCELL(pNode);
143212     RtreeCell cell;
143213     RtreeNode *pChild;
143214 
143215     RtreeCell *aCell = 0;
143216 
143217 #if VARIANT_RSTARTREE_CHOOSESUBTREE
143218     if( ii==(pRtree->iDepth-1) ){
143219       int jj;
143220       aCell = sqlite3_malloc(sizeof(RtreeCell)*nCell);
143221       if( !aCell ){
143222         rc = SQLITE_NOMEM;
143223         nodeRelease(pRtree, pNode);
143224         pNode = 0;
143225         continue;
143226       }
143227       for(jj=0; jj<nCell; jj++){
143228         nodeGetCell(pRtree, pNode, jj, &aCell[jj]);
143229       }
143230     }
143231 #endif
143232 
143233     /* Select the child node which will be enlarged the least if pCell
143234     ** is inserted into it. Resolve ties by choosing the entry with
143235     ** the smallest area.
143236     */
143237     for(iCell=0; iCell<nCell; iCell++){
143238       int bBest = 0;
143239       RtreeDValue growth;
143240       RtreeDValue area;
143241       nodeGetCell(pRtree, pNode, iCell, &cell);
143242       growth = cellGrowth(pRtree, &cell, pCell);
143243       area = cellArea(pRtree, &cell);
143244 
143245 #if VARIANT_RSTARTREE_CHOOSESUBTREE
143246       if( ii==(pRtree->iDepth-1) ){
143247         overlap = cellOverlapEnlargement(pRtree,&cell,pCell,aCell,nCell,iCell);
143248       }else{
143249         overlap = 0.0;
143250       }
143251       if( (iCell==0)
143252        || (overlap<fMinOverlap)
143253        || (overlap==fMinOverlap && growth<fMinGrowth)
143254        || (overlap==fMinOverlap && growth==fMinGrowth && area<fMinArea)
143255       ){
143256         bBest = 1;
143257         fMinOverlap = overlap;
143258       }
143259 #else
143260       if( iCell==0||growth<fMinGrowth||(growth==fMinGrowth && area<fMinArea) ){
143261         bBest = 1;
143262       }
143263 #endif
143264       if( bBest ){
143265         fMinGrowth = growth;
143266         fMinArea = area;
143267         iBest = cell.iRowid;
143268       }
143269     }
143270 
143271     sqlite3_free(aCell);
143272     rc = nodeAcquire(pRtree, iBest, pNode, &pChild);
143273     nodeRelease(pRtree, pNode);
143274     pNode = pChild;
143275   }
143276 
143277   *ppLeaf = pNode;
143278   return rc;
143279 }
143280 
143281 /*
143282 ** A cell with the same content as pCell has just been inserted into
143283 ** the node pNode. This function updates the bounding box cells in
143284 ** all ancestor elements.
143285 */
143286 static int AdjustTree(
143287   Rtree *pRtree,                    /* Rtree table */
143288   RtreeNode *pNode,                 /* Adjust ancestry of this node. */
143289   RtreeCell *pCell                  /* This cell was just inserted */
143290 ){
143291   RtreeNode *p = pNode;
143292   while( p->pParent ){
143293     RtreeNode *pParent = p->pParent;
143294     RtreeCell cell;
143295     int iCell;
143296 
143297     if( nodeParentIndex(pRtree, p, &iCell) ){
143298       return SQLITE_CORRUPT_VTAB;
143299     }
143300 
143301     nodeGetCell(pRtree, pParent, iCell, &cell);
143302     if( !cellContains(pRtree, &cell, pCell) ){
143303       cellUnion(pRtree, &cell, pCell);
143304       nodeOverwriteCell(pRtree, pParent, &cell, iCell);
143305     }
143306 
143307     p = pParent;
143308   }
143309   return SQLITE_OK;
143310 }
143311 
143312 /*
143313 ** Write mapping (iRowid->iNode) to the <rtree>_rowid table.
143314 */
143315 static int rowidWrite(Rtree *pRtree, sqlite3_int64 iRowid, sqlite3_int64 iNode){
143316   sqlite3_bind_int64(pRtree->pWriteRowid, 1, iRowid);
143317   sqlite3_bind_int64(pRtree->pWriteRowid, 2, iNode);
143318   sqlite3_step(pRtree->pWriteRowid);
143319   return sqlite3_reset(pRtree->pWriteRowid);
143320 }
143321 
143322 /*
143323 ** Write mapping (iNode->iPar) to the <rtree>_parent table.
143324 */
143325 static int parentWrite(Rtree *pRtree, sqlite3_int64 iNode, sqlite3_int64 iPar){
143326   sqlite3_bind_int64(pRtree->pWriteParent, 1, iNode);
143327   sqlite3_bind_int64(pRtree->pWriteParent, 2, iPar);
143328   sqlite3_step(pRtree->pWriteParent);
143329   return sqlite3_reset(pRtree->pWriteParent);
143330 }
143331 
143332 static int rtreeInsertCell(Rtree *, RtreeNode *, RtreeCell *, int);
143333 
143334 #if VARIANT_GUTTMAN_LINEAR_SPLIT
143335 /*
143336 ** Implementation of the linear variant of the PickNext() function from
143337 ** Guttman[84].
143338 */
143339 static RtreeCell *LinearPickNext(
143340   Rtree *pRtree,
143341   RtreeCell *aCell,
143342   int nCell,
143343   RtreeCell *pLeftBox,
143344   RtreeCell *pRightBox,
143345   int *aiUsed
143346 ){
143347   int ii;
143348   for(ii=0; aiUsed[ii]; ii++);
143349   aiUsed[ii] = 1;
143350   return &aCell[ii];
143351 }
143352 
143353 /*
143354 ** Implementation of the linear variant of the PickSeeds() function from
143355 ** Guttman[84].
143356 */
143357 static void LinearPickSeeds(
143358   Rtree *pRtree,
143359   RtreeCell *aCell,
143360   int nCell,
143361   int *piLeftSeed,
143362   int *piRightSeed
143363 ){
143364   int i;
143365   int iLeftSeed = 0;
143366   int iRightSeed = 1;
143367   RtreeDValue maxNormalInnerWidth = (RtreeDValue)0;
143368 
143369   /* Pick two "seed" cells from the array of cells. The algorithm used
143370   ** here is the LinearPickSeeds algorithm from Gutman[1984]. The
143371   ** indices of the two seed cells in the array are stored in local
143372   ** variables iLeftSeek and iRightSeed.
143373   */
143374   for(i=0; i<pRtree->nDim; i++){
143375     RtreeDValue x1 = DCOORD(aCell[0].aCoord[i*2]);
143376     RtreeDValue x2 = DCOORD(aCell[0].aCoord[i*2+1]);
143377     RtreeDValue x3 = x1;
143378     RtreeDValue x4 = x2;
143379     int jj;
143380 
143381     int iCellLeft = 0;
143382     int iCellRight = 0;
143383 
143384     for(jj=1; jj<nCell; jj++){
143385       RtreeDValue left = DCOORD(aCell[jj].aCoord[i*2]);
143386       RtreeDValue right = DCOORD(aCell[jj].aCoord[i*2+1]);
143387 
143388       if( left<x1 ) x1 = left;
143389       if( right>x4 ) x4 = right;
143390       if( left>x3 ){
143391         x3 = left;
143392         iCellRight = jj;
143393       }
143394       if( right<x2 ){
143395         x2 = right;
143396         iCellLeft = jj;
143397       }
143398     }
143399 
143400     if( x4!=x1 ){
143401       RtreeDValue normalwidth = (x3 - x2) / (x4 - x1);
143402       if( normalwidth>maxNormalInnerWidth ){
143403         iLeftSeed = iCellLeft;
143404         iRightSeed = iCellRight;
143405       }
143406     }
143407   }
143408 
143409   *piLeftSeed = iLeftSeed;
143410   *piRightSeed = iRightSeed;
143411 }
143412 #endif /* VARIANT_GUTTMAN_LINEAR_SPLIT */
143413 
143414 #if VARIANT_GUTTMAN_QUADRATIC_SPLIT
143415 /*
143416 ** Implementation of the quadratic variant of the PickNext() function from
143417 ** Guttman[84].
143418 */
143419 static RtreeCell *QuadraticPickNext(
143420   Rtree *pRtree,
143421   RtreeCell *aCell,
143422   int nCell,
143423   RtreeCell *pLeftBox,
143424   RtreeCell *pRightBox,
143425   int *aiUsed
143426 ){
143427   #define FABS(a) ((a)<0.0?-1.0*(a):(a))
143428 
143429   int iSelect = -1;
143430   RtreeDValue fDiff;
143431   int ii;
143432   for(ii=0; ii<nCell; ii++){
143433     if( aiUsed[ii]==0 ){
143434       RtreeDValue left = cellGrowth(pRtree, pLeftBox, &aCell[ii]);
143435       RtreeDValue right = cellGrowth(pRtree, pLeftBox, &aCell[ii]);
143436       RtreeDValue diff = FABS(right-left);
143437       if( iSelect<0 || diff>fDiff ){
143438         fDiff = diff;
143439         iSelect = ii;
143440       }
143441     }
143442   }
143443   aiUsed[iSelect] = 1;
143444   return &aCell[iSelect];
143445 }
143446 
143447 /*
143448 ** Implementation of the quadratic variant of the PickSeeds() function from
143449 ** Guttman[84].
143450 */
143451 static void QuadraticPickSeeds(
143452   Rtree *pRtree,
143453   RtreeCell *aCell,
143454   int nCell,
143455   int *piLeftSeed,
143456   int *piRightSeed
143457 ){
143458   int ii;
143459   int jj;
143460 
143461   int iLeftSeed = 0;
143462   int iRightSeed = 1;
143463   RtreeDValue fWaste = 0.0;
143464 
143465   for(ii=0; ii<nCell; ii++){
143466     for(jj=ii+1; jj<nCell; jj++){
143467       RtreeDValue right = cellArea(pRtree, &aCell[jj]);
143468       RtreeDValue growth = cellGrowth(pRtree, &aCell[ii], &aCell[jj]);
143469       RtreeDValue waste = growth - right;
143470 
143471       if( waste>fWaste ){
143472         iLeftSeed = ii;
143473         iRightSeed = jj;
143474         fWaste = waste;
143475       }
143476     }
143477   }
143478 
143479   *piLeftSeed = iLeftSeed;
143480   *piRightSeed = iRightSeed;
143481 }
143482 #endif /* VARIANT_GUTTMAN_QUADRATIC_SPLIT */
143483 
143484 /*
143485 ** Arguments aIdx, aDistance and aSpare all point to arrays of size
143486 ** nIdx. The aIdx array contains the set of integers from 0 to
143487 ** (nIdx-1) in no particular order. This function sorts the values
143488 ** in aIdx according to the indexed values in aDistance. For
143489 ** example, assuming the inputs:
143490 **
143491 **   aIdx      = { 0,   1,   2,   3 }
143492 **   aDistance = { 5.0, 2.0, 7.0, 6.0 }
143493 **
143494 ** this function sets the aIdx array to contain:
143495 **
143496 **   aIdx      = { 0,   1,   2,   3 }
143497 **
143498 ** The aSpare array is used as temporary working space by the
143499 ** sorting algorithm.
143500 */
143501 static void SortByDistance(
143502   int *aIdx,
143503   int nIdx,
143504   RtreeDValue *aDistance,
143505   int *aSpare
143506 ){
143507   if( nIdx>1 ){
143508     int iLeft = 0;
143509     int iRight = 0;
143510 
143511     int nLeft = nIdx/2;
143512     int nRight = nIdx-nLeft;
143513     int *aLeft = aIdx;
143514     int *aRight = &aIdx[nLeft];
143515 
143516     SortByDistance(aLeft, nLeft, aDistance, aSpare);
143517     SortByDistance(aRight, nRight, aDistance, aSpare);
143518 
143519     memcpy(aSpare, aLeft, sizeof(int)*nLeft);
143520     aLeft = aSpare;
143521 
143522     while( iLeft<nLeft || iRight<nRight ){
143523       if( iLeft==nLeft ){
143524         aIdx[iLeft+iRight] = aRight[iRight];
143525         iRight++;
143526       }else if( iRight==nRight ){
143527         aIdx[iLeft+iRight] = aLeft[iLeft];
143528         iLeft++;
143529       }else{
143530         RtreeDValue fLeft = aDistance[aLeft[iLeft]];
143531         RtreeDValue fRight = aDistance[aRight[iRight]];
143532         if( fLeft<fRight ){
143533           aIdx[iLeft+iRight] = aLeft[iLeft];
143534           iLeft++;
143535         }else{
143536           aIdx[iLeft+iRight] = aRight[iRight];
143537           iRight++;
143538         }
143539       }
143540     }
143541 
143542 #if 0
143543     /* Check that the sort worked */
143544     {
143545       int jj;
143546       for(jj=1; jj<nIdx; jj++){
143547         RtreeDValue left = aDistance[aIdx[jj-1]];
143548         RtreeDValue right = aDistance[aIdx[jj]];
143549         assert( left<=right );
143550       }
143551     }
143552 #endif
143553   }
143554 }
143555 
143556 /*
143557 ** Arguments aIdx, aCell and aSpare all point to arrays of size
143558 ** nIdx. The aIdx array contains the set of integers from 0 to
143559 ** (nIdx-1) in no particular order. This function sorts the values
143560 ** in aIdx according to dimension iDim of the cells in aCell. The
143561 ** minimum value of dimension iDim is considered first, the
143562 ** maximum used to break ties.
143563 **
143564 ** The aSpare array is used as temporary working space by the
143565 ** sorting algorithm.
143566 */
143567 static void SortByDimension(
143568   Rtree *pRtree,
143569   int *aIdx,
143570   int nIdx,
143571   int iDim,
143572   RtreeCell *aCell,
143573   int *aSpare
143574 ){
143575   if( nIdx>1 ){
143576 
143577     int iLeft = 0;
143578     int iRight = 0;
143579 
143580     int nLeft = nIdx/2;
143581     int nRight = nIdx-nLeft;
143582     int *aLeft = aIdx;
143583     int *aRight = &aIdx[nLeft];
143584 
143585     SortByDimension(pRtree, aLeft, nLeft, iDim, aCell, aSpare);
143586     SortByDimension(pRtree, aRight, nRight, iDim, aCell, aSpare);
143587 
143588     memcpy(aSpare, aLeft, sizeof(int)*nLeft);
143589     aLeft = aSpare;
143590     while( iLeft<nLeft || iRight<nRight ){
143591       RtreeDValue xleft1 = DCOORD(aCell[aLeft[iLeft]].aCoord[iDim*2]);
143592       RtreeDValue xleft2 = DCOORD(aCell[aLeft[iLeft]].aCoord[iDim*2+1]);
143593       RtreeDValue xright1 = DCOORD(aCell[aRight[iRight]].aCoord[iDim*2]);
143594       RtreeDValue xright2 = DCOORD(aCell[aRight[iRight]].aCoord[iDim*2+1]);
143595       if( (iLeft!=nLeft) && ((iRight==nRight)
143596        || (xleft1<xright1)
143597        || (xleft1==xright1 && xleft2<xright2)
143598       )){
143599         aIdx[iLeft+iRight] = aLeft[iLeft];
143600         iLeft++;
143601       }else{
143602         aIdx[iLeft+iRight] = aRight[iRight];
143603         iRight++;
143604       }
143605     }
143606 
143607 #if 0
143608     /* Check that the sort worked */
143609     {
143610       int jj;
143611       for(jj=1; jj<nIdx; jj++){
143612         RtreeDValue xleft1 = aCell[aIdx[jj-1]].aCoord[iDim*2];
143613         RtreeDValue xleft2 = aCell[aIdx[jj-1]].aCoord[iDim*2+1];
143614         RtreeDValue xright1 = aCell[aIdx[jj]].aCoord[iDim*2];
143615         RtreeDValue xright2 = aCell[aIdx[jj]].aCoord[iDim*2+1];
143616         assert( xleft1<=xright1 && (xleft1<xright1 || xleft2<=xright2) );
143617       }
143618     }
143619 #endif
143620   }
143621 }
143622 
143623 #if VARIANT_RSTARTREE_SPLIT
143624 /*
143625 ** Implementation of the R*-tree variant of SplitNode from Beckman[1990].
143626 */
143627 static int splitNodeStartree(
143628   Rtree *pRtree,
143629   RtreeCell *aCell,
143630   int nCell,
143631   RtreeNode *pLeft,
143632   RtreeNode *pRight,
143633   RtreeCell *pBboxLeft,
143634   RtreeCell *pBboxRight
143635 ){
143636   int **aaSorted;
143637   int *aSpare;
143638   int ii;
143639 
143640   int iBestDim = 0;
143641   int iBestSplit = 0;
143642   RtreeDValue fBestMargin = 0.0;
143643 
143644   int nByte = (pRtree->nDim+1)*(sizeof(int*)+nCell*sizeof(int));
143645 
143646   aaSorted = (int **)sqlite3_malloc(nByte);
143647   if( !aaSorted ){
143648     return SQLITE_NOMEM;
143649   }
143650 
143651   aSpare = &((int *)&aaSorted[pRtree->nDim])[pRtree->nDim*nCell];
143652   memset(aaSorted, 0, nByte);
143653   for(ii=0; ii<pRtree->nDim; ii++){
143654     int jj;
143655     aaSorted[ii] = &((int *)&aaSorted[pRtree->nDim])[ii*nCell];
143656     for(jj=0; jj<nCell; jj++){
143657       aaSorted[ii][jj] = jj;
143658     }
143659     SortByDimension(pRtree, aaSorted[ii], nCell, ii, aCell, aSpare);
143660   }
143661 
143662   for(ii=0; ii<pRtree->nDim; ii++){
143663     RtreeDValue margin = 0.0;
143664     RtreeDValue fBestOverlap = 0.0;
143665     RtreeDValue fBestArea = 0.0;
143666     int iBestLeft = 0;
143667     int nLeft;
143668 
143669     for(
143670       nLeft=RTREE_MINCELLS(pRtree);
143671       nLeft<=(nCell-RTREE_MINCELLS(pRtree));
143672       nLeft++
143673     ){
143674       RtreeCell left;
143675       RtreeCell right;
143676       int kk;
143677       RtreeDValue overlap;
143678       RtreeDValue area;
143679 
143680       memcpy(&left, &aCell[aaSorted[ii][0]], sizeof(RtreeCell));
143681       memcpy(&right, &aCell[aaSorted[ii][nCell-1]], sizeof(RtreeCell));
143682       for(kk=1; kk<(nCell-1); kk++){
143683         if( kk<nLeft ){
143684           cellUnion(pRtree, &left, &aCell[aaSorted[ii][kk]]);
143685         }else{
143686           cellUnion(pRtree, &right, &aCell[aaSorted[ii][kk]]);
143687         }
143688       }
143689       margin += cellMargin(pRtree, &left);
143690       margin += cellMargin(pRtree, &right);
143691       overlap = cellOverlap(pRtree, &left, &right, 1, -1);
143692       area = cellArea(pRtree, &left) + cellArea(pRtree, &right);
143693       if( (nLeft==RTREE_MINCELLS(pRtree))
143694        || (overlap<fBestOverlap)
143695        || (overlap==fBestOverlap && area<fBestArea)
143696       ){
143697         iBestLeft = nLeft;
143698         fBestOverlap = overlap;
143699         fBestArea = area;
143700       }
143701     }
143702 
143703     if( ii==0 || margin<fBestMargin ){
143704       iBestDim = ii;
143705       fBestMargin = margin;
143706       iBestSplit = iBestLeft;
143707     }
143708   }
143709 
143710   memcpy(pBboxLeft, &aCell[aaSorted[iBestDim][0]], sizeof(RtreeCell));
143711   memcpy(pBboxRight, &aCell[aaSorted[iBestDim][iBestSplit]], sizeof(RtreeCell));
143712   for(ii=0; ii<nCell; ii++){
143713     RtreeNode *pTarget = (ii<iBestSplit)?pLeft:pRight;
143714     RtreeCell *pBbox = (ii<iBestSplit)?pBboxLeft:pBboxRight;
143715     RtreeCell *pCell = &aCell[aaSorted[iBestDim][ii]];
143716     nodeInsertCell(pRtree, pTarget, pCell);
143717     cellUnion(pRtree, pBbox, pCell);
143718   }
143719 
143720   sqlite3_free(aaSorted);
143721   return SQLITE_OK;
143722 }
143723 #endif
143724 
143725 #if VARIANT_GUTTMAN_SPLIT
143726 /*
143727 ** Implementation of the regular R-tree SplitNode from Guttman[1984].
143728 */
143729 static int splitNodeGuttman(
143730   Rtree *pRtree,
143731   RtreeCell *aCell,
143732   int nCell,
143733   RtreeNode *pLeft,
143734   RtreeNode *pRight,
143735   RtreeCell *pBboxLeft,
143736   RtreeCell *pBboxRight
143737 ){
143738   int iLeftSeed = 0;
143739   int iRightSeed = 1;
143740   int *aiUsed;
143741   int i;
143742 
143743   aiUsed = sqlite3_malloc(sizeof(int)*nCell);
143744   if( !aiUsed ){
143745     return SQLITE_NOMEM;
143746   }
143747   memset(aiUsed, 0, sizeof(int)*nCell);
143748 
143749   PickSeeds(pRtree, aCell, nCell, &iLeftSeed, &iRightSeed);
143750 
143751   memcpy(pBboxLeft, &aCell[iLeftSeed], sizeof(RtreeCell));
143752   memcpy(pBboxRight, &aCell[iRightSeed], sizeof(RtreeCell));
143753   nodeInsertCell(pRtree, pLeft, &aCell[iLeftSeed]);
143754   nodeInsertCell(pRtree, pRight, &aCell[iRightSeed]);
143755   aiUsed[iLeftSeed] = 1;
143756   aiUsed[iRightSeed] = 1;
143757 
143758   for(i=nCell-2; i>0; i--){
143759     RtreeCell *pNext;
143760     pNext = PickNext(pRtree, aCell, nCell, pBboxLeft, pBboxRight, aiUsed);
143761     RtreeDValue diff =
143762       cellGrowth(pRtree, pBboxLeft, pNext) -
143763       cellGrowth(pRtree, pBboxRight, pNext)
143764     ;
143765     if( (RTREE_MINCELLS(pRtree)-NCELL(pRight)==i)
143766      || (diff>0.0 && (RTREE_MINCELLS(pRtree)-NCELL(pLeft)!=i))
143767     ){
143768       nodeInsertCell(pRtree, pRight, pNext);
143769       cellUnion(pRtree, pBboxRight, pNext);
143770     }else{
143771       nodeInsertCell(pRtree, pLeft, pNext);
143772       cellUnion(pRtree, pBboxLeft, pNext);
143773     }
143774   }
143775 
143776   sqlite3_free(aiUsed);
143777   return SQLITE_OK;
143778 }
143779 #endif
143780 
143781 static int updateMapping(
143782   Rtree *pRtree,
143783   i64 iRowid,
143784   RtreeNode *pNode,
143785   int iHeight
143786 ){
143787   int (*xSetMapping)(Rtree *, sqlite3_int64, sqlite3_int64);
143788   xSetMapping = ((iHeight==0)?rowidWrite:parentWrite);
143789   if( iHeight>0 ){
143790     RtreeNode *pChild = nodeHashLookup(pRtree, iRowid);
143791     if( pChild ){
143792       nodeRelease(pRtree, pChild->pParent);
143793       nodeReference(pNode);
143794       pChild->pParent = pNode;
143795     }
143796   }
143797   return xSetMapping(pRtree, iRowid, pNode->iNode);
143798 }
143799 
143800 static int SplitNode(
143801   Rtree *pRtree,
143802   RtreeNode *pNode,
143803   RtreeCell *pCell,
143804   int iHeight
143805 ){
143806   int i;
143807   int newCellIsRight = 0;
143808 
143809   int rc = SQLITE_OK;
143810   int nCell = NCELL(pNode);
143811   RtreeCell *aCell;
143812   int *aiUsed;
143813 
143814   RtreeNode *pLeft = 0;
143815   RtreeNode *pRight = 0;
143816 
143817   RtreeCell leftbbox;
143818   RtreeCell rightbbox;
143819 
143820   /* Allocate an array and populate it with a copy of pCell and
143821   ** all cells from node pLeft. Then zero the original node.
143822   */
143823   aCell = sqlite3_malloc((sizeof(RtreeCell)+sizeof(int))*(nCell+1));
143824   if( !aCell ){
143825     rc = SQLITE_NOMEM;
143826     goto splitnode_out;
143827   }
143828   aiUsed = (int *)&aCell[nCell+1];
143829   memset(aiUsed, 0, sizeof(int)*(nCell+1));
143830   for(i=0; i<nCell; i++){
143831     nodeGetCell(pRtree, pNode, i, &aCell[i]);
143832   }
143833   nodeZero(pRtree, pNode);
143834   memcpy(&aCell[nCell], pCell, sizeof(RtreeCell));
143835   nCell++;
143836 
143837   if( pNode->iNode==1 ){
143838     pRight = nodeNew(pRtree, pNode);
143839     pLeft = nodeNew(pRtree, pNode);
143840     pRtree->iDepth++;
143841     pNode->isDirty = 1;
143842     writeInt16(pNode->zData, pRtree->iDepth);
143843   }else{
143844     pLeft = pNode;
143845     pRight = nodeNew(pRtree, pLeft->pParent);
143846     nodeReference(pLeft);
143847   }
143848 
143849   if( !pLeft || !pRight ){
143850     rc = SQLITE_NOMEM;
143851     goto splitnode_out;
143852   }
143853 
143854   memset(pLeft->zData, 0, pRtree->iNodeSize);
143855   memset(pRight->zData, 0, pRtree->iNodeSize);
143856 
143857   rc = AssignCells(pRtree, aCell, nCell, pLeft, pRight, &leftbbox, &rightbbox);
143858   if( rc!=SQLITE_OK ){
143859     goto splitnode_out;
143860   }
143861 
143862   /* Ensure both child nodes have node numbers assigned to them by calling
143863   ** nodeWrite(). Node pRight always needs a node number, as it was created
143864   ** by nodeNew() above. But node pLeft sometimes already has a node number.
143865   ** In this case avoid the all to nodeWrite().
143866   */
143867   if( SQLITE_OK!=(rc = nodeWrite(pRtree, pRight))
143868    || (0==pLeft->iNode && SQLITE_OK!=(rc = nodeWrite(pRtree, pLeft)))
143869   ){
143870     goto splitnode_out;
143871   }
143872 
143873   rightbbox.iRowid = pRight->iNode;
143874   leftbbox.iRowid = pLeft->iNode;
143875 
143876   if( pNode->iNode==1 ){
143877     rc = rtreeInsertCell(pRtree, pLeft->pParent, &leftbbox, iHeight+1);
143878     if( rc!=SQLITE_OK ){
143879       goto splitnode_out;
143880     }
143881   }else{
143882     RtreeNode *pParent = pLeft->pParent;
143883     int iCell;
143884     rc = nodeParentIndex(pRtree, pLeft, &iCell);
143885     if( rc==SQLITE_OK ){
143886       nodeOverwriteCell(pRtree, pParent, &leftbbox, iCell);
143887       rc = AdjustTree(pRtree, pParent, &leftbbox);
143888     }
143889     if( rc!=SQLITE_OK ){
143890       goto splitnode_out;
143891     }
143892   }
143893   if( (rc = rtreeInsertCell(pRtree, pRight->pParent, &rightbbox, iHeight+1)) ){
143894     goto splitnode_out;
143895   }
143896 
143897   for(i=0; i<NCELL(pRight); i++){
143898     i64 iRowid = nodeGetRowid(pRtree, pRight, i);
143899     rc = updateMapping(pRtree, iRowid, pRight, iHeight);
143900     if( iRowid==pCell->iRowid ){
143901       newCellIsRight = 1;
143902     }
143903     if( rc!=SQLITE_OK ){
143904       goto splitnode_out;
143905     }
143906   }
143907   if( pNode->iNode==1 ){
143908     for(i=0; i<NCELL(pLeft); i++){
143909       i64 iRowid = nodeGetRowid(pRtree, pLeft, i);
143910       rc = updateMapping(pRtree, iRowid, pLeft, iHeight);
143911       if( rc!=SQLITE_OK ){
143912         goto splitnode_out;
143913       }
143914     }
143915   }else if( newCellIsRight==0 ){
143916     rc = updateMapping(pRtree, pCell->iRowid, pLeft, iHeight);
143917   }
143918 
143919   if( rc==SQLITE_OK ){
143920     rc = nodeRelease(pRtree, pRight);
143921     pRight = 0;
143922   }
143923   if( rc==SQLITE_OK ){
143924     rc = nodeRelease(pRtree, pLeft);
143925     pLeft = 0;
143926   }
143927 
143928 splitnode_out:
143929   nodeRelease(pRtree, pRight);
143930   nodeRelease(pRtree, pLeft);
143931   sqlite3_free(aCell);
143932   return rc;
143933 }
143934 
143935 /*
143936 ** If node pLeaf is not the root of the r-tree and its pParent pointer is
143937 ** still NULL, load all ancestor nodes of pLeaf into memory and populate
143938 ** the pLeaf->pParent chain all the way up to the root node.
143939 **
143940 ** This operation is required when a row is deleted (or updated - an update
143941 ** is implemented as a delete followed by an insert). SQLite provides the
143942 ** rowid of the row to delete, which can be used to find the leaf on which
143943 ** the entry resides (argument pLeaf). Once the leaf is located, this
143944 ** function is called to determine its ancestry.
143945 */
143946 static int fixLeafParent(Rtree *pRtree, RtreeNode *pLeaf){
143947   int rc = SQLITE_OK;
143948   RtreeNode *pChild = pLeaf;
143949   while( rc==SQLITE_OK && pChild->iNode!=1 && pChild->pParent==0 ){
143950     int rc2 = SQLITE_OK;          /* sqlite3_reset() return code */
143951     sqlite3_bind_int64(pRtree->pReadParent, 1, pChild->iNode);
143952     rc = sqlite3_step(pRtree->pReadParent);
143953     if( rc==SQLITE_ROW ){
143954       RtreeNode *pTest;           /* Used to test for reference loops */
143955       i64 iNode;                  /* Node number of parent node */
143956 
143957       /* Before setting pChild->pParent, test that we are not creating a
143958       ** loop of references (as we would if, say, pChild==pParent). We don't
143959       ** want to do this as it leads to a memory leak when trying to delete
143960       ** the referenced counted node structures.
143961       */
143962       iNode = sqlite3_column_int64(pRtree->pReadParent, 0);
143963       for(pTest=pLeaf; pTest && pTest->iNode!=iNode; pTest=pTest->pParent);
143964       if( !pTest ){
143965         rc2 = nodeAcquire(pRtree, iNode, 0, &pChild->pParent);
143966       }
143967     }
143968     rc = sqlite3_reset(pRtree->pReadParent);
143969     if( rc==SQLITE_OK ) rc = rc2;
143970     if( rc==SQLITE_OK && !pChild->pParent ) rc = SQLITE_CORRUPT_VTAB;
143971     pChild = pChild->pParent;
143972   }
143973   return rc;
143974 }
143975 
143976 static int deleteCell(Rtree *, RtreeNode *, int, int);
143977 
143978 static int removeNode(Rtree *pRtree, RtreeNode *pNode, int iHeight){
143979   int rc;
143980   int rc2;
143981   RtreeNode *pParent = 0;
143982   int iCell;
143983 
143984   assert( pNode->nRef==1 );
143985 
143986   /* Remove the entry in the parent cell. */
143987   rc = nodeParentIndex(pRtree, pNode, &iCell);
143988   if( rc==SQLITE_OK ){
143989     pParent = pNode->pParent;
143990     pNode->pParent = 0;
143991     rc = deleteCell(pRtree, pParent, iCell, iHeight+1);
143992   }
143993   rc2 = nodeRelease(pRtree, pParent);
143994   if( rc==SQLITE_OK ){
143995     rc = rc2;
143996   }
143997   if( rc!=SQLITE_OK ){
143998     return rc;
143999   }
144000 
144001   /* Remove the xxx_node entry. */
144002   sqlite3_bind_int64(pRtree->pDeleteNode, 1, pNode->iNode);
144003   sqlite3_step(pRtree->pDeleteNode);
144004   if( SQLITE_OK!=(rc = sqlite3_reset(pRtree->pDeleteNode)) ){
144005     return rc;
144006   }
144007 
144008   /* Remove the xxx_parent entry. */
144009   sqlite3_bind_int64(pRtree->pDeleteParent, 1, pNode->iNode);
144010   sqlite3_step(pRtree->pDeleteParent);
144011   if( SQLITE_OK!=(rc = sqlite3_reset(pRtree->pDeleteParent)) ){
144012     return rc;
144013   }
144014 
144015   /* Remove the node from the in-memory hash table and link it into
144016   ** the Rtree.pDeleted list. Its contents will be re-inserted later on.
144017   */
144018   nodeHashDelete(pRtree, pNode);
144019   pNode->iNode = iHeight;
144020   pNode->pNext = pRtree->pDeleted;
144021   pNode->nRef++;
144022   pRtree->pDeleted = pNode;
144023 
144024   return SQLITE_OK;
144025 }
144026 
144027 static int fixBoundingBox(Rtree *pRtree, RtreeNode *pNode){
144028   RtreeNode *pParent = pNode->pParent;
144029   int rc = SQLITE_OK;
144030   if( pParent ){
144031     int ii;
144032     int nCell = NCELL(pNode);
144033     RtreeCell box;                            /* Bounding box for pNode */
144034     nodeGetCell(pRtree, pNode, 0, &box);
144035     for(ii=1; ii<nCell; ii++){
144036       RtreeCell cell;
144037       nodeGetCell(pRtree, pNode, ii, &cell);
144038       cellUnion(pRtree, &box, &cell);
144039     }
144040     box.iRowid = pNode->iNode;
144041     rc = nodeParentIndex(pRtree, pNode, &ii);
144042     if( rc==SQLITE_OK ){
144043       nodeOverwriteCell(pRtree, pParent, &box, ii);
144044       rc = fixBoundingBox(pRtree, pParent);
144045     }
144046   }
144047   return rc;
144048 }
144049 
144050 /*
144051 ** Delete the cell at index iCell of node pNode. After removing the
144052 ** cell, adjust the r-tree data structure if required.
144053 */
144054 static int deleteCell(Rtree *pRtree, RtreeNode *pNode, int iCell, int iHeight){
144055   RtreeNode *pParent;
144056   int rc;
144057 
144058   if( SQLITE_OK!=(rc = fixLeafParent(pRtree, pNode)) ){
144059     return rc;
144060   }
144061 
144062   /* Remove the cell from the node. This call just moves bytes around
144063   ** the in-memory node image, so it cannot fail.
144064   */
144065   nodeDeleteCell(pRtree, pNode, iCell);
144066 
144067   /* If the node is not the tree root and now has less than the minimum
144068   ** number of cells, remove it from the tree. Otherwise, update the
144069   ** cell in the parent node so that it tightly contains the updated
144070   ** node.
144071   */
144072   pParent = pNode->pParent;
144073   assert( pParent || pNode->iNode==1 );
144074   if( pParent ){
144075     if( NCELL(pNode)<RTREE_MINCELLS(pRtree) ){
144076       rc = removeNode(pRtree, pNode, iHeight);
144077     }else{
144078       rc = fixBoundingBox(pRtree, pNode);
144079     }
144080   }
144081 
144082   return rc;
144083 }
144084 
144085 static int Reinsert(
144086   Rtree *pRtree,
144087   RtreeNode *pNode,
144088   RtreeCell *pCell,
144089   int iHeight
144090 ){
144091   int *aOrder;
144092   int *aSpare;
144093   RtreeCell *aCell;
144094   RtreeDValue *aDistance;
144095   int nCell;
144096   RtreeDValue aCenterCoord[RTREE_MAX_DIMENSIONS];
144097   int iDim;
144098   int ii;
144099   int rc = SQLITE_OK;
144100   int n;
144101 
144102   memset(aCenterCoord, 0, sizeof(RtreeDValue)*RTREE_MAX_DIMENSIONS);
144103 
144104   nCell = NCELL(pNode)+1;
144105   n = (nCell+1)&(~1);
144106 
144107   /* Allocate the buffers used by this operation. The allocation is
144108   ** relinquished before this function returns.
144109   */
144110   aCell = (RtreeCell *)sqlite3_malloc(n * (
144111     sizeof(RtreeCell)     +         /* aCell array */
144112     sizeof(int)           +         /* aOrder array */
144113     sizeof(int)           +         /* aSpare array */
144114     sizeof(RtreeDValue)             /* aDistance array */
144115   ));
144116   if( !aCell ){
144117     return SQLITE_NOMEM;
144118   }
144119   aOrder    = (int *)&aCell[n];
144120   aSpare    = (int *)&aOrder[n];
144121   aDistance = (RtreeDValue *)&aSpare[n];
144122 
144123   for(ii=0; ii<nCell; ii++){
144124     if( ii==(nCell-1) ){
144125       memcpy(&aCell[ii], pCell, sizeof(RtreeCell));
144126     }else{
144127       nodeGetCell(pRtree, pNode, ii, &aCell[ii]);
144128     }
144129     aOrder[ii] = ii;
144130     for(iDim=0; iDim<pRtree->nDim; iDim++){
144131       aCenterCoord[iDim] += DCOORD(aCell[ii].aCoord[iDim*2]);
144132       aCenterCoord[iDim] += DCOORD(aCell[ii].aCoord[iDim*2+1]);
144133     }
144134   }
144135   for(iDim=0; iDim<pRtree->nDim; iDim++){
144136     aCenterCoord[iDim] = (aCenterCoord[iDim]/(nCell*(RtreeDValue)2));
144137   }
144138 
144139   for(ii=0; ii<nCell; ii++){
144140     aDistance[ii] = 0.0;
144141     for(iDim=0; iDim<pRtree->nDim; iDim++){
144142       RtreeDValue coord = (DCOORD(aCell[ii].aCoord[iDim*2+1]) -
144143                                DCOORD(aCell[ii].aCoord[iDim*2]));
144144       aDistance[ii] += (coord-aCenterCoord[iDim])*(coord-aCenterCoord[iDim]);
144145     }
144146   }
144147 
144148   SortByDistance(aOrder, nCell, aDistance, aSpare);
144149   nodeZero(pRtree, pNode);
144150 
144151   for(ii=0; rc==SQLITE_OK && ii<(nCell-(RTREE_MINCELLS(pRtree)+1)); ii++){
144152     RtreeCell *p = &aCell[aOrder[ii]];
144153     nodeInsertCell(pRtree, pNode, p);
144154     if( p->iRowid==pCell->iRowid ){
144155       if( iHeight==0 ){
144156         rc = rowidWrite(pRtree, p->iRowid, pNode->iNode);
144157       }else{
144158         rc = parentWrite(pRtree, p->iRowid, pNode->iNode);
144159       }
144160     }
144161   }
144162   if( rc==SQLITE_OK ){
144163     rc = fixBoundingBox(pRtree, pNode);
144164   }
144165   for(; rc==SQLITE_OK && ii<nCell; ii++){
144166     /* Find a node to store this cell in. pNode->iNode currently contains
144167     ** the height of the sub-tree headed by the cell.
144168     */
144169     RtreeNode *pInsert;
144170     RtreeCell *p = &aCell[aOrder[ii]];
144171     rc = ChooseLeaf(pRtree, p, iHeight, &pInsert);
144172     if( rc==SQLITE_OK ){
144173       int rc2;
144174       rc = rtreeInsertCell(pRtree, pInsert, p, iHeight);
144175       rc2 = nodeRelease(pRtree, pInsert);
144176       if( rc==SQLITE_OK ){
144177         rc = rc2;
144178       }
144179     }
144180   }
144181 
144182   sqlite3_free(aCell);
144183   return rc;
144184 }
144185 
144186 /*
144187 ** Insert cell pCell into node pNode. Node pNode is the head of a
144188 ** subtree iHeight high (leaf nodes have iHeight==0).
144189 */
144190 static int rtreeInsertCell(
144191   Rtree *pRtree,
144192   RtreeNode *pNode,
144193   RtreeCell *pCell,
144194   int iHeight
144195 ){
144196   int rc = SQLITE_OK;
144197   if( iHeight>0 ){
144198     RtreeNode *pChild = nodeHashLookup(pRtree, pCell->iRowid);
144199     if( pChild ){
144200       nodeRelease(pRtree, pChild->pParent);
144201       nodeReference(pNode);
144202       pChild->pParent = pNode;
144203     }
144204   }
144205   if( nodeInsertCell(pRtree, pNode, pCell) ){
144206 #if VARIANT_RSTARTREE_REINSERT
144207     if( iHeight<=pRtree->iReinsertHeight || pNode->iNode==1){
144208       rc = SplitNode(pRtree, pNode, pCell, iHeight);
144209     }else{
144210       pRtree->iReinsertHeight = iHeight;
144211       rc = Reinsert(pRtree, pNode, pCell, iHeight);
144212     }
144213 #else
144214     rc = SplitNode(pRtree, pNode, pCell, iHeight);
144215 #endif
144216   }else{
144217     rc = AdjustTree(pRtree, pNode, pCell);
144218     if( rc==SQLITE_OK ){
144219       if( iHeight==0 ){
144220         rc = rowidWrite(pRtree, pCell->iRowid, pNode->iNode);
144221       }else{
144222         rc = parentWrite(pRtree, pCell->iRowid, pNode->iNode);
144223       }
144224     }
144225   }
144226   return rc;
144227 }
144228 
144229 static int reinsertNodeContent(Rtree *pRtree, RtreeNode *pNode){
144230   int ii;
144231   int rc = SQLITE_OK;
144232   int nCell = NCELL(pNode);
144233 
144234   for(ii=0; rc==SQLITE_OK && ii<nCell; ii++){
144235     RtreeNode *pInsert;
144236     RtreeCell cell;
144237     nodeGetCell(pRtree, pNode, ii, &cell);
144238 
144239     /* Find a node to store this cell in. pNode->iNode currently contains
144240     ** the height of the sub-tree headed by the cell.
144241     */
144242     rc = ChooseLeaf(pRtree, &cell, (int)pNode->iNode, &pInsert);
144243     if( rc==SQLITE_OK ){
144244       int rc2;
144245       rc = rtreeInsertCell(pRtree, pInsert, &cell, (int)pNode->iNode);
144246       rc2 = nodeRelease(pRtree, pInsert);
144247       if( rc==SQLITE_OK ){
144248         rc = rc2;
144249       }
144250     }
144251   }
144252   return rc;
144253 }
144254 
144255 /*
144256 ** Select a currently unused rowid for a new r-tree record.
144257 */
144258 static int newRowid(Rtree *pRtree, i64 *piRowid){
144259   int rc;
144260   sqlite3_bind_null(pRtree->pWriteRowid, 1);
144261   sqlite3_bind_null(pRtree->pWriteRowid, 2);
144262   sqlite3_step(pRtree->pWriteRowid);
144263   rc = sqlite3_reset(pRtree->pWriteRowid);
144264   *piRowid = sqlite3_last_insert_rowid(pRtree->db);
144265   return rc;
144266 }
144267 
144268 /*
144269 ** Remove the entry with rowid=iDelete from the r-tree structure.
144270 */
144271 static int rtreeDeleteRowid(Rtree *pRtree, sqlite3_int64 iDelete){
144272   int rc;                         /* Return code */
144273   RtreeNode *pLeaf = 0;           /* Leaf node containing record iDelete */
144274   int iCell;                      /* Index of iDelete cell in pLeaf */
144275   RtreeNode *pRoot;               /* Root node of rtree structure */
144276 
144277 
144278   /* Obtain a reference to the root node to initialize Rtree.iDepth */
144279   rc = nodeAcquire(pRtree, 1, 0, &pRoot);
144280 
144281   /* Obtain a reference to the leaf node that contains the entry
144282   ** about to be deleted.
144283   */
144284   if( rc==SQLITE_OK ){
144285     rc = findLeafNode(pRtree, iDelete, &pLeaf);
144286   }
144287 
144288   /* Delete the cell in question from the leaf node. */
144289   if( rc==SQLITE_OK ){
144290     int rc2;
144291     rc = nodeRowidIndex(pRtree, pLeaf, iDelete, &iCell);
144292     if( rc==SQLITE_OK ){
144293       rc = deleteCell(pRtree, pLeaf, iCell, 0);
144294     }
144295     rc2 = nodeRelease(pRtree, pLeaf);
144296     if( rc==SQLITE_OK ){
144297       rc = rc2;
144298     }
144299   }
144300 
144301   /* Delete the corresponding entry in the <rtree>_rowid table. */
144302   if( rc==SQLITE_OK ){
144303     sqlite3_bind_int64(pRtree->pDeleteRowid, 1, iDelete);
144304     sqlite3_step(pRtree->pDeleteRowid);
144305     rc = sqlite3_reset(pRtree->pDeleteRowid);
144306   }
144307 
144308   /* Check if the root node now has exactly one child. If so, remove
144309   ** it, schedule the contents of the child for reinsertion and
144310   ** reduce the tree height by one.
144311   **
144312   ** This is equivalent to copying the contents of the child into
144313   ** the root node (the operation that Gutman's paper says to perform
144314   ** in this scenario).
144315   */
144316   if( rc==SQLITE_OK && pRtree->iDepth>0 && NCELL(pRoot)==1 ){
144317     int rc2;
144318     RtreeNode *pChild;
144319     i64 iChild = nodeGetRowid(pRtree, pRoot, 0);
144320     rc = nodeAcquire(pRtree, iChild, pRoot, &pChild);
144321     if( rc==SQLITE_OK ){
144322       rc = removeNode(pRtree, pChild, pRtree->iDepth-1);
144323     }
144324     rc2 = nodeRelease(pRtree, pChild);
144325     if( rc==SQLITE_OK ) rc = rc2;
144326     if( rc==SQLITE_OK ){
144327       pRtree->iDepth--;
144328       writeInt16(pRoot->zData, pRtree->iDepth);
144329       pRoot->isDirty = 1;
144330     }
144331   }
144332 
144333   /* Re-insert the contents of any underfull nodes removed from the tree. */
144334   for(pLeaf=pRtree->pDeleted; pLeaf; pLeaf=pRtree->pDeleted){
144335     if( rc==SQLITE_OK ){
144336       rc = reinsertNodeContent(pRtree, pLeaf);
144337     }
144338     pRtree->pDeleted = pLeaf->pNext;
144339     sqlite3_free(pLeaf);
144340   }
144341 
144342   /* Release the reference to the root node. */
144343   if( rc==SQLITE_OK ){
144344     rc = nodeRelease(pRtree, pRoot);
144345   }else{
144346     nodeRelease(pRtree, pRoot);
144347   }
144348 
144349   return rc;
144350 }
144351 
144352 /*
144353 ** Rounding constants for float->double conversion.
144354 */
144355 #define RNDTOWARDS  (1.0 - 1.0/8388608.0)  /* Round towards zero */
144356 #define RNDAWAY     (1.0 + 1.0/8388608.0)  /* Round away from zero */
144357 
144358 #if !defined(SQLITE_RTREE_INT_ONLY)
144359 /*
144360 ** Convert an sqlite3_value into an RtreeValue (presumably a float)
144361 ** while taking care to round toward negative or positive, respectively.
144362 */
144363 static RtreeValue rtreeValueDown(sqlite3_value *v){
144364   double d = sqlite3_value_double(v);
144365   float f = (float)d;
144366   if( f>d ){
144367     f = (float)(d*(d<0 ? RNDAWAY : RNDTOWARDS));
144368   }
144369   return f;
144370 }
144371 static RtreeValue rtreeValueUp(sqlite3_value *v){
144372   double d = sqlite3_value_double(v);
144373   float f = (float)d;
144374   if( f<d ){
144375     f = (float)(d*(d<0 ? RNDTOWARDS : RNDAWAY));
144376   }
144377   return f;
144378 }
144379 #endif /* !defined(SQLITE_RTREE_INT_ONLY) */
144380 
144381 
144382 /*
144383 ** The xUpdate method for rtree module virtual tables.
144384 */
144385 static int rtreeUpdate(
144386   sqlite3_vtab *pVtab,
144387   int nData,
144388   sqlite3_value **azData,
144389   sqlite_int64 *pRowid
144390 ){
144391   Rtree *pRtree = (Rtree *)pVtab;
144392   int rc = SQLITE_OK;
144393   RtreeCell cell;                 /* New cell to insert if nData>1 */
144394   int bHaveRowid = 0;             /* Set to 1 after new rowid is determined */
144395 
144396   rtreeReference(pRtree);
144397   assert(nData>=1);
144398 
144399   /* Constraint handling. A write operation on an r-tree table may return
144400   ** SQLITE_CONSTRAINT for two reasons:
144401   **
144402   **   1. A duplicate rowid value, or
144403   **   2. The supplied data violates the "x2>=x1" constraint.
144404   **
144405   ** In the first case, if the conflict-handling mode is REPLACE, then
144406   ** the conflicting row can be removed before proceeding. In the second
144407   ** case, SQLITE_CONSTRAINT must be returned regardless of the
144408   ** conflict-handling mode specified by the user.
144409   */
144410   if( nData>1 ){
144411     int ii;
144412 
144413     /* Populate the cell.aCoord[] array. The first coordinate is azData[3]. */
144414     assert( nData==(pRtree->nDim*2 + 3) );
144415 #ifndef SQLITE_RTREE_INT_ONLY
144416     if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
144417       for(ii=0; ii<(pRtree->nDim*2); ii+=2){
144418         cell.aCoord[ii].f = rtreeValueDown(azData[ii+3]);
144419         cell.aCoord[ii+1].f = rtreeValueUp(azData[ii+4]);
144420         if( cell.aCoord[ii].f>cell.aCoord[ii+1].f ){
144421           rc = SQLITE_CONSTRAINT;
144422           goto constraint;
144423         }
144424       }
144425     }else
144426 #endif
144427     {
144428       for(ii=0; ii<(pRtree->nDim*2); ii+=2){
144429         cell.aCoord[ii].i = sqlite3_value_int(azData[ii+3]);
144430         cell.aCoord[ii+1].i = sqlite3_value_int(azData[ii+4]);
144431         if( cell.aCoord[ii].i>cell.aCoord[ii+1].i ){
144432           rc = SQLITE_CONSTRAINT;
144433           goto constraint;
144434         }
144435       }
144436     }
144437 
144438     /* If a rowid value was supplied, check if it is already present in
144439     ** the table. If so, the constraint has failed. */
144440     if( sqlite3_value_type(azData[2])!=SQLITE_NULL ){
144441       cell.iRowid = sqlite3_value_int64(azData[2]);
144442       if( sqlite3_value_type(azData[0])==SQLITE_NULL
144443        || sqlite3_value_int64(azData[0])!=cell.iRowid
144444       ){
144445         int steprc;
144446         sqlite3_bind_int64(pRtree->pReadRowid, 1, cell.iRowid);
144447         steprc = sqlite3_step(pRtree->pReadRowid);
144448         rc = sqlite3_reset(pRtree->pReadRowid);
144449         if( SQLITE_ROW==steprc ){
144450           if( sqlite3_vtab_on_conflict(pRtree->db)==SQLITE_REPLACE ){
144451             rc = rtreeDeleteRowid(pRtree, cell.iRowid);
144452           }else{
144453             rc = SQLITE_CONSTRAINT;
144454             goto constraint;
144455           }
144456         }
144457       }
144458       bHaveRowid = 1;
144459     }
144460   }
144461 
144462   /* If azData[0] is not an SQL NULL value, it is the rowid of a
144463   ** record to delete from the r-tree table. The following block does
144464   ** just that.
144465   */
144466   if( sqlite3_value_type(azData[0])!=SQLITE_NULL ){
144467     rc = rtreeDeleteRowid(pRtree, sqlite3_value_int64(azData[0]));
144468   }
144469 
144470   /* If the azData[] array contains more than one element, elements
144471   ** (azData[2]..azData[argc-1]) contain a new record to insert into
144472   ** the r-tree structure.
144473   */
144474   if( rc==SQLITE_OK && nData>1 ){
144475     /* Insert the new record into the r-tree */
144476     RtreeNode *pLeaf = 0;
144477 
144478     /* Figure out the rowid of the new row. */
144479     if( bHaveRowid==0 ){
144480       rc = newRowid(pRtree, &cell.iRowid);
144481     }
144482     *pRowid = cell.iRowid;
144483 
144484     if( rc==SQLITE_OK ){
144485       rc = ChooseLeaf(pRtree, &cell, 0, &pLeaf);
144486     }
144487     if( rc==SQLITE_OK ){
144488       int rc2;
144489       pRtree->iReinsertHeight = -1;
144490       rc = rtreeInsertCell(pRtree, pLeaf, &cell, 0);
144491       rc2 = nodeRelease(pRtree, pLeaf);
144492       if( rc==SQLITE_OK ){
144493         rc = rc2;
144494       }
144495     }
144496   }
144497 
144498 constraint:
144499   rtreeRelease(pRtree);
144500   return rc;
144501 }
144502 
144503 /*
144504 ** The xRename method for rtree module virtual tables.
144505 */
144506 static int rtreeRename(sqlite3_vtab *pVtab, const char *zNewName){
144507   Rtree *pRtree = (Rtree *)pVtab;
144508   int rc = SQLITE_NOMEM;
144509   char *zSql = sqlite3_mprintf(
144510     "ALTER TABLE %Q.'%q_node'   RENAME TO \"%w_node\";"
144511     "ALTER TABLE %Q.'%q_parent' RENAME TO \"%w_parent\";"
144512     "ALTER TABLE %Q.'%q_rowid'  RENAME TO \"%w_rowid\";"
144513     , pRtree->zDb, pRtree->zName, zNewName
144514     , pRtree->zDb, pRtree->zName, zNewName
144515     , pRtree->zDb, pRtree->zName, zNewName
144516   );
144517   if( zSql ){
144518     rc = sqlite3_exec(pRtree->db, zSql, 0, 0, 0);
144519     sqlite3_free(zSql);
144520   }
144521   return rc;
144522 }
144523 
144524 /*
144525 ** This function populates the pRtree->nRowEst variable with an estimate
144526 ** of the number of rows in the virtual table. If possible, this is based
144527 ** on sqlite_stat1 data. Otherwise, use RTREE_DEFAULT_ROWEST.
144528 */
144529 static int rtreeQueryStat1(sqlite3 *db, Rtree *pRtree){
144530   const char *zSql = "SELECT stat FROM sqlite_stat1 WHERE tbl= ? || '_rowid'";
144531   sqlite3_stmt *p;
144532   int rc;
144533   i64 nRow = 0;
144534 
144535   rc = sqlite3_prepare_v2(db, zSql, -1, &p, 0);
144536   if( rc==SQLITE_OK ){
144537     sqlite3_bind_text(p, 1, pRtree->zName, -1, SQLITE_STATIC);
144538     if( sqlite3_step(p)==SQLITE_ROW ) nRow = sqlite3_column_int64(p, 0);
144539     rc = sqlite3_finalize(p);
144540   }else if( rc!=SQLITE_NOMEM ){
144541     rc = SQLITE_OK;
144542   }
144543 
144544   if( rc==SQLITE_OK ){
144545     if( nRow==0 ){
144546       pRtree->nRowEst = RTREE_DEFAULT_ROWEST;
144547     }else{
144548       pRtree->nRowEst = MAX(nRow, RTREE_MIN_ROWEST);
144549     }
144550   }
144551 
144552   return rc;
144553 }
144554 
144555 static sqlite3_module rtreeModule = {
144556   0,                          /* iVersion */
144557   rtreeCreate,                /* xCreate - create a table */
144558   rtreeConnect,               /* xConnect - connect to an existing table */
144559   rtreeBestIndex,             /* xBestIndex - Determine search strategy */
144560   rtreeDisconnect,            /* xDisconnect - Disconnect from a table */
144561   rtreeDestroy,               /* xDestroy - Drop a table */
144562   rtreeOpen,                  /* xOpen - open a cursor */
144563   rtreeClose,                 /* xClose - close a cursor */
144564   rtreeFilter,                /* xFilter - configure scan constraints */
144565   rtreeNext,                  /* xNext - advance a cursor */
144566   rtreeEof,                   /* xEof */
144567   rtreeColumn,                /* xColumn - read data */
144568   rtreeRowid,                 /* xRowid - read data */
144569   rtreeUpdate,                /* xUpdate - write data */
144570   0,                          /* xBegin - begin transaction */
144571   0,                          /* xSync - sync transaction */
144572   0,                          /* xCommit - commit transaction */
144573   0,                          /* xRollback - rollback transaction */
144574   0,                          /* xFindFunction - function overloading */
144575   rtreeRename,                /* xRename - rename the table */
144576   0,                          /* xSavepoint */
144577   0,                          /* xRelease */
144578   0                           /* xRollbackTo */
144579 };
144580 
144581 static int rtreeSqlInit(
144582   Rtree *pRtree,
144583   sqlite3 *db,
144584   const char *zDb,
144585   const char *zPrefix,
144586   int isCreate
144587 ){
144588   int rc = SQLITE_OK;
144589 
144590   #define N_STATEMENT 9
144591   static const char *azSql[N_STATEMENT] = {
144592     /* Read and write the xxx_node table */
144593     "SELECT data FROM '%q'.'%q_node' WHERE nodeno = :1",
144594     "INSERT OR REPLACE INTO '%q'.'%q_node' VALUES(:1, :2)",
144595     "DELETE FROM '%q'.'%q_node' WHERE nodeno = :1",
144596 
144597     /* Read and write the xxx_rowid table */
144598     "SELECT nodeno FROM '%q'.'%q_rowid' WHERE rowid = :1",
144599     "INSERT OR REPLACE INTO '%q'.'%q_rowid' VALUES(:1, :2)",
144600     "DELETE FROM '%q'.'%q_rowid' WHERE rowid = :1",
144601 
144602     /* Read and write the xxx_parent table */
144603     "SELECT parentnode FROM '%q'.'%q_parent' WHERE nodeno = :1",
144604     "INSERT OR REPLACE INTO '%q'.'%q_parent' VALUES(:1, :2)",
144605     "DELETE FROM '%q'.'%q_parent' WHERE nodeno = :1"
144606   };
144607   sqlite3_stmt **appStmt[N_STATEMENT];
144608   int i;
144609 
144610   pRtree->db = db;
144611 
144612   if( isCreate ){
144613     char *zCreate = sqlite3_mprintf(
144614 "CREATE TABLE \"%w\".\"%w_node\"(nodeno INTEGER PRIMARY KEY, data BLOB);"
144615 "CREATE TABLE \"%w\".\"%w_rowid\"(rowid INTEGER PRIMARY KEY, nodeno INTEGER);"
144616 "CREATE TABLE \"%w\".\"%w_parent\"(nodeno INTEGER PRIMARY KEY, parentnode INTEGER);"
144617 "INSERT INTO '%q'.'%q_node' VALUES(1, zeroblob(%d))",
144618       zDb, zPrefix, zDb, zPrefix, zDb, zPrefix, zDb, zPrefix, pRtree->iNodeSize
144619     );
144620     if( !zCreate ){
144621       return SQLITE_NOMEM;
144622     }
144623     rc = sqlite3_exec(db, zCreate, 0, 0, 0);
144624     sqlite3_free(zCreate);
144625     if( rc!=SQLITE_OK ){
144626       return rc;
144627     }
144628   }
144629 
144630   appStmt[0] = &pRtree->pReadNode;
144631   appStmt[1] = &pRtree->pWriteNode;
144632   appStmt[2] = &pRtree->pDeleteNode;
144633   appStmt[3] = &pRtree->pReadRowid;
144634   appStmt[4] = &pRtree->pWriteRowid;
144635   appStmt[5] = &pRtree->pDeleteRowid;
144636   appStmt[6] = &pRtree->pReadParent;
144637   appStmt[7] = &pRtree->pWriteParent;
144638   appStmt[8] = &pRtree->pDeleteParent;
144639 
144640   rc = rtreeQueryStat1(db, pRtree);
144641   for(i=0; i<N_STATEMENT && rc==SQLITE_OK; i++){
144642     char *zSql = sqlite3_mprintf(azSql[i], zDb, zPrefix);
144643     if( zSql ){
144644       rc = sqlite3_prepare_v2(db, zSql, -1, appStmt[i], 0);
144645     }else{
144646       rc = SQLITE_NOMEM;
144647     }
144648     sqlite3_free(zSql);
144649   }
144650 
144651   return rc;
144652 }
144653 
144654 /*
144655 ** The second argument to this function contains the text of an SQL statement
144656 ** that returns a single integer value. The statement is compiled and executed
144657 ** using database connection db. If successful, the integer value returned
144658 ** is written to *piVal and SQLITE_OK returned. Otherwise, an SQLite error
144659 ** code is returned and the value of *piVal after returning is not defined.
144660 */
144661 static int getIntFromStmt(sqlite3 *db, const char *zSql, int *piVal){
144662   int rc = SQLITE_NOMEM;
144663   if( zSql ){
144664     sqlite3_stmt *pStmt = 0;
144665     rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
144666     if( rc==SQLITE_OK ){
144667       if( SQLITE_ROW==sqlite3_step(pStmt) ){
144668         *piVal = sqlite3_column_int(pStmt, 0);
144669       }
144670       rc = sqlite3_finalize(pStmt);
144671     }
144672   }
144673   return rc;
144674 }
144675 
144676 /*
144677 ** This function is called from within the xConnect() or xCreate() method to
144678 ** determine the node-size used by the rtree table being created or connected
144679 ** to. If successful, pRtree->iNodeSize is populated and SQLITE_OK returned.
144680 ** Otherwise, an SQLite error code is returned.
144681 **
144682 ** If this function is being called as part of an xConnect(), then the rtree
144683 ** table already exists. In this case the node-size is determined by inspecting
144684 ** the root node of the tree.
144685 **
144686 ** Otherwise, for an xCreate(), use 64 bytes less than the database page-size.
144687 ** This ensures that each node is stored on a single database page. If the
144688 ** database page-size is so large that more than RTREE_MAXCELLS entries
144689 ** would fit in a single node, use a smaller node-size.
144690 */
144691 static int getNodeSize(
144692   sqlite3 *db,                    /* Database handle */
144693   Rtree *pRtree,                  /* Rtree handle */
144694   int isCreate,                   /* True for xCreate, false for xConnect */
144695   char **pzErr                    /* OUT: Error message, if any */
144696 ){
144697   int rc;
144698   char *zSql;
144699   if( isCreate ){
144700     int iPageSize = 0;
144701     zSql = sqlite3_mprintf("PRAGMA %Q.page_size", pRtree->zDb);
144702     rc = getIntFromStmt(db, zSql, &iPageSize);
144703     if( rc==SQLITE_OK ){
144704       pRtree->iNodeSize = iPageSize-64;
144705       if( (4+pRtree->nBytesPerCell*RTREE_MAXCELLS)<pRtree->iNodeSize ){
144706         pRtree->iNodeSize = 4+pRtree->nBytesPerCell*RTREE_MAXCELLS;
144707       }
144708     }else{
144709       *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
144710     }
144711   }else{
144712     zSql = sqlite3_mprintf(
144713         "SELECT length(data) FROM '%q'.'%q_node' WHERE nodeno = 1",
144714         pRtree->zDb, pRtree->zName
144715     );
144716     rc = getIntFromStmt(db, zSql, &pRtree->iNodeSize);
144717     if( rc!=SQLITE_OK ){
144718       *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
144719     }
144720   }
144721 
144722   sqlite3_free(zSql);
144723   return rc;
144724 }
144725 
144726 /*
144727 ** This function is the implementation of both the xConnect and xCreate
144728 ** methods of the r-tree virtual table.
144729 **
144730 **   argv[0]   -> module name
144731 **   argv[1]   -> database name
144732 **   argv[2]   -> table name
144733 **   argv[...] -> column names...
144734 */
144735 static int rtreeInit(
144736   sqlite3 *db,                        /* Database connection */
144737   void *pAux,                         /* One of the RTREE_COORD_* constants */
144738   int argc, const char *const*argv,   /* Parameters to CREATE TABLE statement */
144739   sqlite3_vtab **ppVtab,              /* OUT: New virtual table */
144740   char **pzErr,                       /* OUT: Error message, if any */
144741   int isCreate                        /* True for xCreate, false for xConnect */
144742 ){
144743   int rc = SQLITE_OK;
144744   Rtree *pRtree;
144745   int nDb;              /* Length of string argv[1] */
144746   int nName;            /* Length of string argv[2] */
144747   int eCoordType = (pAux ? RTREE_COORD_INT32 : RTREE_COORD_REAL32);
144748 
144749   const char *aErrMsg[] = {
144750     0,                                                    /* 0 */
144751     "Wrong number of columns for an rtree table",         /* 1 */
144752     "Too few columns for an rtree table",                 /* 2 */
144753     "Too many columns for an rtree table"                 /* 3 */
144754   };
144755 
144756   int iErr = (argc<6) ? 2 : argc>(RTREE_MAX_DIMENSIONS*2+4) ? 3 : argc%2;
144757   if( aErrMsg[iErr] ){
144758     *pzErr = sqlite3_mprintf("%s", aErrMsg[iErr]);
144759     return SQLITE_ERROR;
144760   }
144761 
144762   sqlite3_vtab_config(db, SQLITE_VTAB_CONSTRAINT_SUPPORT, 1);
144763 
144764   /* Allocate the sqlite3_vtab structure */
144765   nDb = (int)strlen(argv[1]);
144766   nName = (int)strlen(argv[2]);
144767   pRtree = (Rtree *)sqlite3_malloc(sizeof(Rtree)+nDb+nName+2);
144768   if( !pRtree ){
144769     return SQLITE_NOMEM;
144770   }
144771   memset(pRtree, 0, sizeof(Rtree)+nDb+nName+2);
144772   pRtree->nBusy = 1;
144773   pRtree->base.pModule = &rtreeModule;
144774   pRtree->zDb = (char *)&pRtree[1];
144775   pRtree->zName = &pRtree->zDb[nDb+1];
144776   pRtree->nDim = (argc-4)/2;
144777   pRtree->nBytesPerCell = 8 + pRtree->nDim*4*2;
144778   pRtree->eCoordType = eCoordType;
144779   memcpy(pRtree->zDb, argv[1], nDb);
144780   memcpy(pRtree->zName, argv[2], nName);
144781 
144782   /* Figure out the node size to use. */
144783   rc = getNodeSize(db, pRtree, isCreate, pzErr);
144784 
144785   /* Create/Connect to the underlying relational database schema. If
144786   ** that is successful, call sqlite3_declare_vtab() to configure
144787   ** the r-tree table schema.
144788   */
144789   if( rc==SQLITE_OK ){
144790     if( (rc = rtreeSqlInit(pRtree, db, argv[1], argv[2], isCreate)) ){
144791       *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
144792     }else{
144793       char *zSql = sqlite3_mprintf("CREATE TABLE x(%s", argv[3]);
144794       char *zTmp;
144795       int ii;
144796       for(ii=4; zSql && ii<argc; ii++){
144797         zTmp = zSql;
144798         zSql = sqlite3_mprintf("%s, %s", zTmp, argv[ii]);
144799         sqlite3_free(zTmp);
144800       }
144801       if( zSql ){
144802         zTmp = zSql;
144803         zSql = sqlite3_mprintf("%s);", zTmp);
144804         sqlite3_free(zTmp);
144805       }
144806       if( !zSql ){
144807         rc = SQLITE_NOMEM;
144808       }else if( SQLITE_OK!=(rc = sqlite3_declare_vtab(db, zSql)) ){
144809         *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
144810       }
144811       sqlite3_free(zSql);
144812     }
144813   }
144814 
144815   if( rc==SQLITE_OK ){
144816     *ppVtab = (sqlite3_vtab *)pRtree;
144817   }else{
144818     rtreeRelease(pRtree);
144819   }
144820   return rc;
144821 }
144822 
144823 
144824 /*
144825 ** Implementation of a scalar function that decodes r-tree nodes to
144826 ** human readable strings. This can be used for debugging and analysis.
144827 **
144828 ** The scalar function takes two arguments, a blob of data containing
144829 ** an r-tree node, and the number of dimensions the r-tree indexes.
144830 ** For a two-dimensional r-tree structure called "rt", to deserialize
144831 ** all nodes, a statement like:
144832 **
144833 **   SELECT rtreenode(2, data) FROM rt_node;
144834 **
144835 ** The human readable string takes the form of a Tcl list with one
144836 ** entry for each cell in the r-tree node. Each entry is itself a
144837 ** list, containing the 8-byte rowid/pageno followed by the
144838 ** <num-dimension>*2 coordinates.
144839 */
144840 static void rtreenode(sqlite3_context *ctx, int nArg, sqlite3_value **apArg){
144841   char *zText = 0;
144842   RtreeNode node;
144843   Rtree tree;
144844   int ii;
144845 
144846   UNUSED_PARAMETER(nArg);
144847   memset(&node, 0, sizeof(RtreeNode));
144848   memset(&tree, 0, sizeof(Rtree));
144849   tree.nDim = sqlite3_value_int(apArg[0]);
144850   tree.nBytesPerCell = 8 + 8 * tree.nDim;
144851   node.zData = (u8 *)sqlite3_value_blob(apArg[1]);
144852 
144853   for(ii=0; ii<NCELL(&node); ii++){
144854     char zCell[512];
144855     int nCell = 0;
144856     RtreeCell cell;
144857     int jj;
144858 
144859     nodeGetCell(&tree, &node, ii, &cell);
144860     sqlite3_snprintf(512-nCell,&zCell[nCell],"%lld", cell.iRowid);
144861     nCell = (int)strlen(zCell);
144862     for(jj=0; jj<tree.nDim*2; jj++){
144863 #ifndef SQLITE_RTREE_INT_ONLY
144864       sqlite3_snprintf(512-nCell,&zCell[nCell], " %f",
144865                        (double)cell.aCoord[jj].f);
144866 #else
144867       sqlite3_snprintf(512-nCell,&zCell[nCell], " %d",
144868                        cell.aCoord[jj].i);
144869 #endif
144870       nCell = (int)strlen(zCell);
144871     }
144872 
144873     if( zText ){
144874       char *zTextNew = sqlite3_mprintf("%s {%s}", zText, zCell);
144875       sqlite3_free(zText);
144876       zText = zTextNew;
144877     }else{
144878       zText = sqlite3_mprintf("{%s}", zCell);
144879     }
144880   }
144881 
144882   sqlite3_result_text(ctx, zText, -1, sqlite3_free);
144883 }
144884 
144885 static void rtreedepth(sqlite3_context *ctx, int nArg, sqlite3_value **apArg){
144886   UNUSED_PARAMETER(nArg);
144887   if( sqlite3_value_type(apArg[0])!=SQLITE_BLOB
144888    || sqlite3_value_bytes(apArg[0])<2
144889   ){
144890     sqlite3_result_error(ctx, "Invalid argument to rtreedepth()", -1);
144891   }else{
144892     u8 *zBlob = (u8 *)sqlite3_value_blob(apArg[0]);
144893     sqlite3_result_int(ctx, readInt16(zBlob));
144894   }
144895 }
144896 
144897 /*
144898 ** Register the r-tree module with database handle db. This creates the
144899 ** virtual table module "rtree" and the debugging/analysis scalar
144900 ** function "rtreenode".
144901 */
144902 SQLITE_PRIVATE int sqlite3RtreeInit(sqlite3 *db){
144903   const int utf8 = SQLITE_UTF8;
144904   int rc;
144905 
144906   rc = sqlite3_create_function(db, "rtreenode", 2, utf8, 0, rtreenode, 0, 0);
144907   if( rc==SQLITE_OK ){
144908     rc = sqlite3_create_function(db, "rtreedepth", 1, utf8, 0,rtreedepth, 0, 0);
144909   }
144910   if( rc==SQLITE_OK ){
144911 #ifdef SQLITE_RTREE_INT_ONLY
144912     void *c = (void *)RTREE_COORD_INT32;
144913 #else
144914     void *c = (void *)RTREE_COORD_REAL32;
144915 #endif
144916     rc = sqlite3_create_module_v2(db, "rtree", &rtreeModule, c, 0);
144917   }
144918   if( rc==SQLITE_OK ){
144919     void *c = (void *)RTREE_COORD_INT32;
144920     rc = sqlite3_create_module_v2(db, "rtree_i32", &rtreeModule, c, 0);
144921   }
144922 
144923   return rc;
144924 }
144925 
144926 /*
144927 ** A version of sqlite3_free() that can be used as a callback. This is used
144928 ** in two places - as the destructor for the blob value returned by the
144929 ** invocation of a geometry function, and as the destructor for the geometry
144930 ** functions themselves.
144931 */
144932 static void doSqlite3Free(void *p){
144933   sqlite3_free(p);
144934 }
144935 
144936 /*
144937 ** Each call to sqlite3_rtree_geometry_callback() creates an ordinary SQLite
144938 ** scalar user function. This C function is the callback used for all such
144939 ** registered SQL functions.
144940 **
144941 ** The scalar user functions return a blob that is interpreted by r-tree
144942 ** table MATCH operators.
144943 */
144944 static void geomCallback(sqlite3_context *ctx, int nArg, sqlite3_value **aArg){
144945   RtreeGeomCallback *pGeomCtx = (RtreeGeomCallback *)sqlite3_user_data(ctx);
144946   RtreeMatchArg *pBlob;
144947   int nBlob;
144948 
144949   nBlob = sizeof(RtreeMatchArg) + (nArg-1)*sizeof(RtreeDValue);
144950   pBlob = (RtreeMatchArg *)sqlite3_malloc(nBlob);
144951   if( !pBlob ){
144952     sqlite3_result_error_nomem(ctx);
144953   }else{
144954     int i;
144955     pBlob->magic = RTREE_GEOMETRY_MAGIC;
144956     pBlob->xGeom = pGeomCtx->xGeom;
144957     pBlob->pContext = pGeomCtx->pContext;
144958     pBlob->nParam = nArg;
144959     for(i=0; i<nArg; i++){
144960 #ifdef SQLITE_RTREE_INT_ONLY
144961       pBlob->aParam[i] = sqlite3_value_int64(aArg[i]);
144962 #else
144963       pBlob->aParam[i] = sqlite3_value_double(aArg[i]);
144964 #endif
144965     }
144966     sqlite3_result_blob(ctx, pBlob, nBlob, doSqlite3Free);
144967   }
144968 }
144969 
144970 /*
144971 ** Register a new geometry function for use with the r-tree MATCH operator.
144972 */
144973 SQLITE_API int sqlite3_rtree_geometry_callback(
144974   sqlite3 *db,
144975   const char *zGeom,
144976   int (*xGeom)(sqlite3_rtree_geometry *, int, RtreeDValue *, int *),
144977   void *pContext
144978 ){
144979   RtreeGeomCallback *pGeomCtx;      /* Context object for new user-function */
144980 
144981   /* Allocate and populate the context object. */
144982   pGeomCtx = (RtreeGeomCallback *)sqlite3_malloc(sizeof(RtreeGeomCallback));
144983   if( !pGeomCtx ) return SQLITE_NOMEM;
144984   pGeomCtx->xGeom = xGeom;
144985   pGeomCtx->pContext = pContext;
144986 
144987   /* Create the new user-function. Register a destructor function to delete
144988   ** the context object when it is no longer required.  */
144989   return sqlite3_create_function_v2(db, zGeom, -1, SQLITE_ANY,
144990       (void *)pGeomCtx, geomCallback, 0, 0, doSqlite3Free
144991   );
144992 }
144993 
144994 #if !SQLITE_CORE
144995 #ifdef _WIN32
144996 __declspec(dllexport)
144997 #endif
144998 SQLITE_API int sqlite3_rtree_init(
144999   sqlite3 *db,
145000   char **pzErrMsg,
145001   const sqlite3_api_routines *pApi
145002 ){
145003   SQLITE_EXTENSION_INIT2(pApi)
145004   return sqlite3RtreeInit(db);
145005 }
145006 #endif
145007 
145008 #endif
145009 
145010 /************** End of rtree.c ***********************************************/
145011 /************** Begin file icu.c *********************************************/
145012 /*
145013 ** 2007 May 6
145014 **
145015 ** The author disclaims copyright to this source code.  In place of
145016 ** a legal notice, here is a blessing:
145017 **
145018 **    May you do good and not evil.
145019 **    May you find forgiveness for yourself and forgive others.
145020 **    May you share freely, never taking more than you give.
145021 **
145022 *************************************************************************
145023 ** Id: icu.c,v 1.7 2007/12/13 21:54:11 drh Exp
145024 **
145025 ** This file implements an integration between the ICU library
145026 ** ("International Components for Unicode", an open-source library
145027 ** for handling unicode data) and SQLite. The integration uses
145028 ** ICU to provide the following to SQLite:
145029 **
145030 **   * An implementation of the SQL regexp() function (and hence REGEXP
145031 **     operator) using the ICU uregex_XX() APIs.
145032 **
145033 **   * Implementations of the SQL scalar upper() and lower() functions
145034 **     for case mapping.
145035 **
145036 **   * Integration of ICU and SQLite collation sequences.
145037 **
145038 **   * An implementation of the LIKE operator that uses ICU to
145039 **     provide case-independent matching.
145040 */
145041 
145042 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_ICU)
145043 
145044 /* Include ICU headers */
145045 #include <unicode/utypes.h>
145046 #include <unicode/uregex.h>
145047 #include <unicode/ustring.h>
145048 #include <unicode/ucol.h>
145049 
145050 /* #include <assert.h> */
145051 
145052 #ifndef SQLITE_CORE
145053   SQLITE_EXTENSION_INIT1
145054 #else
145055 #endif
145056 
145057 /*
145058 ** Maximum length (in bytes) of the pattern in a LIKE or GLOB
145059 ** operator.
145060 */
145061 #ifndef SQLITE_MAX_LIKE_PATTERN_LENGTH
145062 # define SQLITE_MAX_LIKE_PATTERN_LENGTH 50000
145063 #endif
145064 
145065 /*
145066 ** Version of sqlite3_free() that is always a function, never a macro.
145067 */
145068 static void xFree(void *p){
145069   sqlite3_free(p);
145070 }
145071 
145072 /*
145073 ** Compare two UTF-8 strings for equality where the first string is
145074 ** a "LIKE" expression. Return true (1) if they are the same and
145075 ** false (0) if they are different.
145076 */
145077 static int icuLikeCompare(
145078   const uint8_t *zPattern,   /* LIKE pattern */
145079   const uint8_t *zString,    /* The UTF-8 string to compare against */
145080   const UChar32 uEsc         /* The escape character */
145081 ){
145082   static const int MATCH_ONE = (UChar32)'_';
145083   static const int MATCH_ALL = (UChar32)'%';
145084 
145085   int iPattern = 0;       /* Current byte index in zPattern */
145086   int iString = 0;        /* Current byte index in zString */
145087 
145088   int prevEscape = 0;     /* True if the previous character was uEsc */
145089 
145090   while( zPattern[iPattern]!=0 ){
145091 
145092     /* Read (and consume) the next character from the input pattern. */
145093     UChar32 uPattern;
145094     U8_NEXT_UNSAFE(zPattern, iPattern, uPattern);
145095     assert(uPattern!=0);
145096 
145097     /* There are now 4 possibilities:
145098     **
145099     **     1. uPattern is an unescaped match-all character "%",
145100     **     2. uPattern is an unescaped match-one character "_",
145101     **     3. uPattern is an unescaped escape character, or
145102     **     4. uPattern is to be handled as an ordinary character
145103     */
145104     if( !prevEscape && uPattern==MATCH_ALL ){
145105       /* Case 1. */
145106       uint8_t c;
145107 
145108       /* Skip any MATCH_ALL or MATCH_ONE characters that follow a
145109       ** MATCH_ALL. For each MATCH_ONE, skip one character in the
145110       ** test string.
145111       */
145112       while( (c=zPattern[iPattern]) == MATCH_ALL || c == MATCH_ONE ){
145113         if( c==MATCH_ONE ){
145114           if( zString[iString]==0 ) return 0;
145115           U8_FWD_1_UNSAFE(zString, iString);
145116         }
145117         iPattern++;
145118       }
145119 
145120       if( zPattern[iPattern]==0 ) return 1;
145121 
145122       while( zString[iString] ){
145123         if( icuLikeCompare(&zPattern[iPattern], &zString[iString], uEsc) ){
145124           return 1;
145125         }
145126         U8_FWD_1_UNSAFE(zString, iString);
145127       }
145128       return 0;
145129 
145130     }else if( !prevEscape && uPattern==MATCH_ONE ){
145131       /* Case 2. */
145132       if( zString[iString]==0 ) return 0;
145133       U8_FWD_1_UNSAFE(zString, iString);
145134 
145135     }else if( !prevEscape && uPattern==uEsc){
145136       /* Case 3. */
145137       prevEscape = 1;
145138 
145139     }else{
145140       /* Case 4. */
145141       UChar32 uString;
145142       U8_NEXT_UNSAFE(zString, iString, uString);
145143       uString = u_foldCase(uString, U_FOLD_CASE_DEFAULT);
145144       uPattern = u_foldCase(uPattern, U_FOLD_CASE_DEFAULT);
145145       if( uString!=uPattern ){
145146         return 0;
145147       }
145148       prevEscape = 0;
145149     }
145150   }
145151 
145152   return zString[iString]==0;
145153 }
145154 
145155 /*
145156 ** Implementation of the like() SQL function.  This function implements
145157 ** the build-in LIKE operator.  The first argument to the function is the
145158 ** pattern and the second argument is the string.  So, the SQL statements:
145159 **
145160 **       A LIKE B
145161 **
145162 ** is implemented as like(B, A). If there is an escape character E,
145163 **
145164 **       A LIKE B ESCAPE E
145165 **
145166 ** is mapped to like(B, A, E).
145167 */
145168 static void icuLikeFunc(
145169   sqlite3_context *context,
145170   int argc,
145171   sqlite3_value **argv
145172 ){
145173   const unsigned char *zA = sqlite3_value_text(argv[0]);
145174   const unsigned char *zB = sqlite3_value_text(argv[1]);
145175   UChar32 uEsc = 0;
145176 
145177   /* Limit the length of the LIKE or GLOB pattern to avoid problems
145178   ** of deep recursion and N*N behavior in patternCompare().
145179   */
145180   if( sqlite3_value_bytes(argv[0])>SQLITE_MAX_LIKE_PATTERN_LENGTH ){
145181     sqlite3_result_error(context, "LIKE or GLOB pattern too complex", -1);
145182     return;
145183   }
145184 
145185 
145186   if( argc==3 ){
145187     /* The escape character string must consist of a single UTF-8 character.
145188     ** Otherwise, return an error.
145189     */
145190     int nE= sqlite3_value_bytes(argv[2]);
145191     const unsigned char *zE = sqlite3_value_text(argv[2]);
145192     int i = 0;
145193     if( zE==0 ) return;
145194     U8_NEXT(zE, i, nE, uEsc);
145195     if( i!=nE){
145196       sqlite3_result_error(context,
145197           "ESCAPE expression must be a single character", -1);
145198       return;
145199     }
145200   }
145201 
145202   if( zA && zB ){
145203     sqlite3_result_int(context, icuLikeCompare(zA, zB, uEsc));
145204   }
145205 }
145206 
145207 /*
145208 ** This function is called when an ICU function called from within
145209 ** the implementation of an SQL scalar function returns an error.
145210 **
145211 ** The scalar function context passed as the first argument is
145212 ** loaded with an error message based on the following two args.
145213 */
145214 static void icuFunctionError(
145215   sqlite3_context *pCtx,       /* SQLite scalar function context */
145216   const char *zName,           /* Name of ICU function that failed */
145217   UErrorCode e                 /* Error code returned by ICU function */
145218 ){
145219   char zBuf[128];
145220   sqlite3_snprintf(128, zBuf, "ICU error: %s(): %s", zName, u_errorName(e));
145221   zBuf[127] = '\0';
145222   sqlite3_result_error(pCtx, zBuf, -1);
145223 }
145224 
145225 /*
145226 ** Function to delete compiled regexp objects. Registered as
145227 ** a destructor function with sqlite3_set_auxdata().
145228 */
145229 static void icuRegexpDelete(void *p){
145230   URegularExpression *pExpr = (URegularExpression *)p;
145231   uregex_close(pExpr);
145232 }
145233 
145234 /*
145235 ** Implementation of SQLite REGEXP operator. This scalar function takes
145236 ** two arguments. The first is a regular expression pattern to compile
145237 ** the second is a string to match against that pattern. If either
145238 ** argument is an SQL NULL, then NULL Is returned. Otherwise, the result
145239 ** is 1 if the string matches the pattern, or 0 otherwise.
145240 **
145241 ** SQLite maps the regexp() function to the regexp() operator such
145242 ** that the following two are equivalent:
145243 **
145244 **     zString REGEXP zPattern
145245 **     regexp(zPattern, zString)
145246 **
145247 ** Uses the following ICU regexp APIs:
145248 **
145249 **     uregex_open()
145250 **     uregex_matches()
145251 **     uregex_close()
145252 */
145253 static void icuRegexpFunc(sqlite3_context *p, int nArg, sqlite3_value **apArg){
145254   UErrorCode status = U_ZERO_ERROR;
145255   URegularExpression *pExpr;
145256   UBool res;
145257   const UChar *zString = sqlite3_value_text16(apArg[1]);
145258 
145259   (void)nArg;  /* Unused parameter */
145260 
145261   /* If the left hand side of the regexp operator is NULL,
145262   ** then the result is also NULL.
145263   */
145264   if( !zString ){
145265     return;
145266   }
145267 
145268   pExpr = sqlite3_get_auxdata(p, 0);
145269   if( !pExpr ){
145270     const UChar *zPattern = sqlite3_value_text16(apArg[0]);
145271     if( !zPattern ){
145272       return;
145273     }
145274     pExpr = uregex_open(zPattern, -1, 0, 0, &status);
145275 
145276     if( U_SUCCESS(status) ){
145277       sqlite3_set_auxdata(p, 0, pExpr, icuRegexpDelete);
145278     }else{
145279       assert(!pExpr);
145280       icuFunctionError(p, "uregex_open", status);
145281       return;
145282     }
145283   }
145284 
145285   /* Configure the text that the regular expression operates on. */
145286   uregex_setText(pExpr, zString, -1, &status);
145287   if( !U_SUCCESS(status) ){
145288     icuFunctionError(p, "uregex_setText", status);
145289     return;
145290   }
145291 
145292   /* Attempt the match */
145293   res = uregex_matches(pExpr, 0, &status);
145294   if( !U_SUCCESS(status) ){
145295     icuFunctionError(p, "uregex_matches", status);
145296     return;
145297   }
145298 
145299   /* Set the text that the regular expression operates on to a NULL
145300   ** pointer. This is not really necessary, but it is tidier than
145301   ** leaving the regular expression object configured with an invalid
145302   ** pointer after this function returns.
145303   */
145304   uregex_setText(pExpr, 0, 0, &status);
145305 
145306   /* Return 1 or 0. */
145307   sqlite3_result_int(p, res ? 1 : 0);
145308 }
145309 
145310 /*
145311 ** Implementations of scalar functions for case mapping - upper() and
145312 ** lower(). Function upper() converts its input to upper-case (ABC).
145313 ** Function lower() converts to lower-case (abc).
145314 **
145315 ** ICU provides two types of case mapping, "general" case mapping and
145316 ** "language specific". Refer to ICU documentation for the differences
145317 ** between the two.
145318 **
145319 ** To utilise "general" case mapping, the upper() or lower() scalar
145320 ** functions are invoked with one argument:
145321 **
145322 **     upper('ABC') -> 'abc'
145323 **     lower('abc') -> 'ABC'
145324 **
145325 ** To access ICU "language specific" case mapping, upper() or lower()
145326 ** should be invoked with two arguments. The second argument is the name
145327 ** of the locale to use. Passing an empty string ("") or SQL NULL value
145328 ** as the second argument is the same as invoking the 1 argument version
145329 ** of upper() or lower().
145330 **
145331 **     lower('I', 'en_us') -> 'i'
145332 **     lower('I', 'tr_tr') -> 'ı' (small dotless i)
145333 **
145334 ** http://www.icu-project.org/userguide/posix.html#case_mappings
145335 */
145336 static void icuCaseFunc16(sqlite3_context *p, int nArg, sqlite3_value **apArg){
145337   const UChar *zInput;
145338   UChar *zOutput;
145339   int nInput;
145340   int nOutput;
145341 
145342   UErrorCode status = U_ZERO_ERROR;
145343   const char *zLocale = 0;
145344 
145345   assert(nArg==1 || nArg==2);
145346   if( nArg==2 ){
145347     zLocale = (const char *)sqlite3_value_text(apArg[1]);
145348   }
145349 
145350   zInput = sqlite3_value_text16(apArg[0]);
145351   if( !zInput ){
145352     return;
145353   }
145354   nInput = sqlite3_value_bytes16(apArg[0]);
145355 
145356   nOutput = nInput * 2 + 2;
145357   zOutput = sqlite3_malloc(nOutput);
145358   if( !zOutput ){
145359     return;
145360   }
145361 
145362   if( sqlite3_user_data(p) ){
145363     u_strToUpper(zOutput, nOutput/2, zInput, nInput/2, zLocale, &status);
145364   }else{
145365     u_strToLower(zOutput, nOutput/2, zInput, nInput/2, zLocale, &status);
145366   }
145367 
145368   if( !U_SUCCESS(status) ){
145369     icuFunctionError(p, "u_strToLower()/u_strToUpper", status);
145370     return;
145371   }
145372 
145373   sqlite3_result_text16(p, zOutput, -1, xFree);
145374 }
145375 
145376 /*
145377 ** Collation sequence destructor function. The pCtx argument points to
145378 ** a UCollator structure previously allocated using ucol_open().
145379 */
145380 static void icuCollationDel(void *pCtx){
145381   UCollator *p = (UCollator *)pCtx;
145382   ucol_close(p);
145383 }
145384 
145385 /*
145386 ** Collation sequence comparison function. The pCtx argument points to
145387 ** a UCollator structure previously allocated using ucol_open().
145388 */
145389 static int icuCollationColl(
145390   void *pCtx,
145391   int nLeft,
145392   const void *zLeft,
145393   int nRight,
145394   const void *zRight
145395 ){
145396   UCollationResult res;
145397   UCollator *p = (UCollator *)pCtx;
145398   res = ucol_strcoll(p, (UChar *)zLeft, nLeft/2, (UChar *)zRight, nRight/2);
145399   switch( res ){
145400     case UCOL_LESS:    return -1;
145401     case UCOL_GREATER: return +1;
145402     case UCOL_EQUAL:   return 0;
145403   }
145404   assert(!"Unexpected return value from ucol_strcoll()");
145405   return 0;
145406 }
145407 
145408 /*
145409 ** Implementation of the scalar function icu_load_collation().
145410 **
145411 ** This scalar function is used to add ICU collation based collation
145412 ** types to an SQLite database connection. It is intended to be called
145413 ** as follows:
145414 **
145415 **     SELECT icu_load_collation(<locale>, <collation-name>);
145416 **
145417 ** Where <locale> is a string containing an ICU locale identifier (i.e.
145418 ** "en_AU", "tr_TR" etc.) and <collation-name> is the name of the
145419 ** collation sequence to create.
145420 */
145421 static void icuLoadCollation(
145422   sqlite3_context *p,
145423   int nArg,
145424   sqlite3_value **apArg
145425 ){
145426   sqlite3 *db = (sqlite3 *)sqlite3_user_data(p);
145427   UErrorCode status = U_ZERO_ERROR;
145428   const char *zLocale;      /* Locale identifier - (eg. "jp_JP") */
145429   const char *zName;        /* SQL Collation sequence name (eg. "japanese") */
145430   UCollator *pUCollator;    /* ICU library collation object */
145431   int rc;                   /* Return code from sqlite3_create_collation_x() */
145432 
145433   assert(nArg==2);
145434   zLocale = (const char *)sqlite3_value_text(apArg[0]);
145435   zName = (const char *)sqlite3_value_text(apArg[1]);
145436 
145437   if( !zLocale || !zName ){
145438     return;
145439   }
145440 
145441   pUCollator = ucol_open(zLocale, &status);
145442   if( !U_SUCCESS(status) ){
145443     icuFunctionError(p, "ucol_open", status);
145444     return;
145445   }
145446   assert(p);
145447 
145448   rc = sqlite3_create_collation_v2(db, zName, SQLITE_UTF16, (void *)pUCollator,
145449       icuCollationColl, icuCollationDel
145450   );
145451   if( rc!=SQLITE_OK ){
145452     ucol_close(pUCollator);
145453     sqlite3_result_error(p, "Error registering collation function", -1);
145454   }
145455 }
145456 
145457 /*
145458 ** Register the ICU extension functions with database db.
145459 */
145460 SQLITE_PRIVATE int sqlite3IcuInit(sqlite3 *db){
145461   struct IcuScalar {
145462     const char *zName;                        /* Function name */
145463     int nArg;                                 /* Number of arguments */
145464     int enc;                                  /* Optimal text encoding */
145465     void *pContext;                           /* sqlite3_user_data() context */
145466     void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
145467   } scalars[] = {
145468     {"regexp", 2, SQLITE_ANY,          0, icuRegexpFunc},
145469 
145470     {"lower",  1, SQLITE_UTF16,        0, icuCaseFunc16},
145471     {"lower",  2, SQLITE_UTF16,        0, icuCaseFunc16},
145472     {"upper",  1, SQLITE_UTF16, (void*)1, icuCaseFunc16},
145473     {"upper",  2, SQLITE_UTF16, (void*)1, icuCaseFunc16},
145474 
145475     {"lower",  1, SQLITE_UTF8,         0, icuCaseFunc16},
145476     {"lower",  2, SQLITE_UTF8,         0, icuCaseFunc16},
145477     {"upper",  1, SQLITE_UTF8,  (void*)1, icuCaseFunc16},
145478     {"upper",  2, SQLITE_UTF8,  (void*)1, icuCaseFunc16},
145479 
145480     {"like",   2, SQLITE_UTF8,         0, icuLikeFunc},
145481     {"like",   3, SQLITE_UTF8,         0, icuLikeFunc},
145482 
145483     {"icu_load_collation",  2, SQLITE_UTF8, (void*)db, icuLoadCollation},
145484   };
145485 
145486   int rc = SQLITE_OK;
145487   int i;
145488 
145489   for(i=0; rc==SQLITE_OK && i<(int)(sizeof(scalars)/sizeof(scalars[0])); i++){
145490     struct IcuScalar *p = &scalars[i];
145491     rc = sqlite3_create_function(
145492         db, p->zName, p->nArg, p->enc, p->pContext, p->xFunc, 0, 0
145493     );
145494   }
145495 
145496   return rc;
145497 }
145498 
145499 #if !SQLITE_CORE
145500 #ifdef _WIN32
145501 __declspec(dllexport)
145502 #endif
145503 SQLITE_API int sqlite3_icu_init(
145504   sqlite3 *db,
145505   char **pzErrMsg,
145506   const sqlite3_api_routines *pApi
145507 ){
145508   SQLITE_EXTENSION_INIT2(pApi)
145509   return sqlite3IcuInit(db);
145510 }
145511 #endif
145512 
145513 #endif
145514 
145515 /************** End of icu.c *************************************************/
145516 /************** Begin file fts3_icu.c ****************************************/
145517 /*
145518 ** 2007 June 22
145519 **
145520 ** The author disclaims copyright to this source code.  In place of
145521 ** a legal notice, here is a blessing:
145522 **
145523 **    May you do good and not evil.
145524 **    May you find forgiveness for yourself and forgive others.
145525 **    May you share freely, never taking more than you give.
145526 **
145527 *************************************************************************
145528 ** This file implements a tokenizer for fts3 based on the ICU library.
145529 */
145530 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
145531 #ifdef SQLITE_ENABLE_ICU
145532 
145533 /* #include <assert.h> */
145534 /* #include <string.h> */
145535 
145536 #include <unicode/ubrk.h>
145537 /* #include <unicode/ucol.h> */
145538 /* #include <unicode/ustring.h> */
145539 #include <unicode/utf16.h>
145540 
145541 typedef struct IcuTokenizer IcuTokenizer;
145542 typedef struct IcuCursor IcuCursor;
145543 
145544 struct IcuTokenizer {
145545   sqlite3_tokenizer base;
145546   char *zLocale;
145547 };
145548 
145549 struct IcuCursor {
145550   sqlite3_tokenizer_cursor base;
145551 
145552   UBreakIterator *pIter;      /* ICU break-iterator object */
145553   int nChar;                  /* Number of UChar elements in pInput */
145554   UChar *aChar;               /* Copy of input using utf-16 encoding */
145555   int *aOffset;               /* Offsets of each character in utf-8 input */
145556 
145557   int nBuffer;
145558   char *zBuffer;
145559 
145560   int iToken;
145561 };
145562 
145563 /*
145564 ** Create a new tokenizer instance.
145565 */
145566 static int icuCreate(
145567   int argc,                            /* Number of entries in argv[] */
145568   const char * const *argv,            /* Tokenizer creation arguments */
145569   sqlite3_tokenizer **ppTokenizer      /* OUT: Created tokenizer */
145570 ){
145571   IcuTokenizer *p;
145572   int n = 0;
145573 
145574   if( argc>0 ){
145575     n = strlen(argv[0])+1;
145576   }
145577   p = (IcuTokenizer *)sqlite3_malloc(sizeof(IcuTokenizer)+n);
145578   if( !p ){
145579     return SQLITE_NOMEM;
145580   }
145581   memset(p, 0, sizeof(IcuTokenizer));
145582 
145583   if( n ){
145584     p->zLocale = (char *)&p[1];
145585     memcpy(p->zLocale, argv[0], n);
145586   }
145587 
145588   *ppTokenizer = (sqlite3_tokenizer *)p;
145589 
145590   return SQLITE_OK;
145591 }
145592 
145593 /*
145594 ** Destroy a tokenizer
145595 */
145596 static int icuDestroy(sqlite3_tokenizer *pTokenizer){
145597   IcuTokenizer *p = (IcuTokenizer *)pTokenizer;
145598   sqlite3_free(p);
145599   return SQLITE_OK;
145600 }
145601 
145602 /*
145603 ** Prepare to begin tokenizing a particular string.  The input
145604 ** string to be tokenized is pInput[0..nBytes-1].  A cursor
145605 ** used to incrementally tokenize this string is returned in
145606 ** *ppCursor.
145607 */
145608 static int icuOpen(
145609   sqlite3_tokenizer *pTokenizer,         /* The tokenizer */
145610   const char *zInput,                    /* Input string */
145611   int nInput,                            /* Length of zInput in bytes */
145612   sqlite3_tokenizer_cursor **ppCursor    /* OUT: Tokenization cursor */
145613 ){
145614   IcuTokenizer *p = (IcuTokenizer *)pTokenizer;
145615   IcuCursor *pCsr;
145616 
145617   const int32_t opt = U_FOLD_CASE_DEFAULT;
145618   UErrorCode status = U_ZERO_ERROR;
145619   int nChar;
145620 
145621   UChar32 c;
145622   int iInput = 0;
145623   int iOut = 0;
145624 
145625   *ppCursor = 0;
145626 
145627   if( zInput==0 ){
145628     nInput = 0;
145629     zInput = "";
145630   }else if( nInput<0 ){
145631     nInput = strlen(zInput);
145632   }
145633   nChar = nInput+1;
145634   pCsr = (IcuCursor *)sqlite3_malloc(
145635       sizeof(IcuCursor) +                /* IcuCursor */
145636       ((nChar+3)&~3) * sizeof(UChar) +   /* IcuCursor.aChar[] */
145637       (nChar+1) * sizeof(int)            /* IcuCursor.aOffset[] */
145638   );
145639   if( !pCsr ){
145640     return SQLITE_NOMEM;
145641   }
145642   memset(pCsr, 0, sizeof(IcuCursor));
145643   pCsr->aChar = (UChar *)&pCsr[1];
145644   pCsr->aOffset = (int *)&pCsr->aChar[(nChar+3)&~3];
145645 
145646   pCsr->aOffset[iOut] = iInput;
145647   U8_NEXT(zInput, iInput, nInput, c);
145648   while( c>0 ){
145649     int isError = 0;
145650     c = u_foldCase(c, opt);
145651     U16_APPEND(pCsr->aChar, iOut, nChar, c, isError);
145652     if( isError ){
145653       sqlite3_free(pCsr);
145654       return SQLITE_ERROR;
145655     }
145656     pCsr->aOffset[iOut] = iInput;
145657 
145658     if( iInput<nInput ){
145659       U8_NEXT(zInput, iInput, nInput, c);
145660     }else{
145661       c = 0;
145662     }
145663   }
145664 
145665   pCsr->pIter = ubrk_open(UBRK_WORD, p->zLocale, pCsr->aChar, iOut, &status);
145666   if( !U_SUCCESS(status) ){
145667     sqlite3_free(pCsr);
145668     return SQLITE_ERROR;
145669   }
145670   pCsr->nChar = iOut;
145671 
145672   ubrk_first(pCsr->pIter);
145673   *ppCursor = (sqlite3_tokenizer_cursor *)pCsr;
145674   return SQLITE_OK;
145675 }
145676 
145677 /*
145678 ** Close a tokenization cursor previously opened by a call to icuOpen().
145679 */
145680 static int icuClose(sqlite3_tokenizer_cursor *pCursor){
145681   IcuCursor *pCsr = (IcuCursor *)pCursor;
145682   ubrk_close(pCsr->pIter);
145683   sqlite3_free(pCsr->zBuffer);
145684   sqlite3_free(pCsr);
145685   return SQLITE_OK;
145686 }
145687 
145688 /*
145689 ** Extract the next token from a tokenization cursor.
145690 */
145691 static int icuNext(
145692   sqlite3_tokenizer_cursor *pCursor,  /* Cursor returned by simpleOpen */
145693   const char **ppToken,               /* OUT: *ppToken is the token text */
145694   int *pnBytes,                       /* OUT: Number of bytes in token */
145695   int *piStartOffset,                 /* OUT: Starting offset of token */
145696   int *piEndOffset,                   /* OUT: Ending offset of token */
145697   int *piPosition                     /* OUT: Position integer of token */
145698 ){
145699   IcuCursor *pCsr = (IcuCursor *)pCursor;
145700 
145701   int iStart = 0;
145702   int iEnd = 0;
145703   int nByte = 0;
145704 
145705   while( iStart==iEnd ){
145706     UChar32 c;
145707 
145708     iStart = ubrk_current(pCsr->pIter);
145709     iEnd = ubrk_next(pCsr->pIter);
145710     if( iEnd==UBRK_DONE ){
145711       return SQLITE_DONE;
145712     }
145713 
145714     while( iStart<iEnd ){
145715       int iWhite = iStart;
145716       U16_NEXT(pCsr->aChar, iWhite, pCsr->nChar, c);
145717       if( u_isspace(c) ){
145718         iStart = iWhite;
145719       }else{
145720         break;
145721       }
145722     }
145723     assert(iStart<=iEnd);
145724   }
145725 
145726   do {
145727     UErrorCode status = U_ZERO_ERROR;
145728     if( nByte ){
145729       char *zNew = sqlite3_realloc(pCsr->zBuffer, nByte);
145730       if( !zNew ){
145731         return SQLITE_NOMEM;
145732       }
145733       pCsr->zBuffer = zNew;
145734       pCsr->nBuffer = nByte;
145735     }
145736 
145737     u_strToUTF8(
145738         pCsr->zBuffer, pCsr->nBuffer, &nByte,    /* Output vars */
145739         &pCsr->aChar[iStart], iEnd-iStart,       /* Input vars */
145740         &status                                  /* Output success/failure */
145741     );
145742   } while( nByte>pCsr->nBuffer );
145743 
145744   *ppToken = pCsr->zBuffer;
145745   *pnBytes = nByte;
145746   *piStartOffset = pCsr->aOffset[iStart];
145747   *piEndOffset = pCsr->aOffset[iEnd];
145748   *piPosition = pCsr->iToken++;
145749 
145750   return SQLITE_OK;
145751 }
145752 
145753 /*
145754 ** The set of routines that implement the simple tokenizer
145755 */
145756 static const sqlite3_tokenizer_module icuTokenizerModule = {
145757   0,                           /* iVersion */
145758   icuCreate,                   /* xCreate  */
145759   icuDestroy,                  /* xCreate  */
145760   icuOpen,                     /* xOpen    */
145761   icuClose,                    /* xClose   */
145762   icuNext,                     /* xNext    */
145763 };
145764 
145765 /*
145766 ** Set *ppModule to point at the implementation of the ICU tokenizer.
145767 */
145768 SQLITE_PRIVATE void sqlite3Fts3IcuTokenizerModule(
145769   sqlite3_tokenizer_module const**ppModule
145770 ){
145771   *ppModule = &icuTokenizerModule;
145772 }
145773 
145774 #endif /* defined(SQLITE_ENABLE_ICU) */
145775 #endif /* !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3) */
145776 
145777 /************** End of fts3_icu.c ********************************************/
145778